MoonWorksGraphicsTests/CompressedTextures/CompressedTexturesGame.cs

132 lines
3.9 KiB
C#
Raw Normal View History

2024-02-23 20:00:15 +00:00
using MoonWorks.Graphics;
2023-01-07 01:53:33 +00:00
using MoonWorks.Math.Float;
using System.IO;
namespace MoonWorks.Test
{
2024-02-07 15:27:55 +00:00
class CompressedTexturesGame : Game
{
private GraphicsPipeline pipeline;
2024-02-23 20:00:15 +00:00
private GpuBuffer vertexBuffer;
private GpuBuffer indexBuffer;
2024-02-07 15:27:55 +00:00
private Sampler sampler;
private Texture[] textures;
private string[] textureNames = new string[]
{
"BC1",
"BC2",
"BC3",
"BC7"
};
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
private int currentTextureIndex;
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
public CompressedTexturesGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true)
{
Logger.LogInfo("Press Left and Right to cycle between textures");
Logger.LogInfo("Setting texture to: " + textureNames[0]);
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
// Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.frag"));
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
// Create the graphics pipeline
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
MainWindow.SwapchainFormat,
vertShaderModule,
fragShaderModule
);
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
pipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1;
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
// Create sampler
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.LinearWrap);
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
// Create texture array
textures = new Texture[textureNames.Length];
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
// Create and populate the GPU resources
var resourceUploader = new ResourceUploader(GraphicsDevice);
2023-01-07 01:53:33 +00:00
vertexBuffer = resourceUploader.CreateBuffer(
2024-02-23 20:00:15 +00:00
[
2024-02-07 15:27:55 +00:00
new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)),
new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(1, 0)),
new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)),
2024-02-23 20:00:15 +00:00
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1))
],
BufferUsageFlags.Vertex
2024-02-07 15:27:55 +00:00
);
2024-02-23 20:00:15 +00:00
indexBuffer = resourceUploader.CreateBuffer<ushort>(
2024-02-23 20:00:15 +00:00
[
2024-02-07 15:27:55 +00:00
0, 1, 2,
0, 2, 3,
2024-02-23 20:00:15 +00:00
],
BufferUsageFlags.Index
2024-02-07 15:27:55 +00:00
);
2024-02-23 20:00:15 +00:00
2024-02-07 15:27:55 +00:00
for (int i = 0; i < textureNames.Length; i += 1)
{
Logger.LogInfo(textureNames[i]);
textures[i] = resourceUploader.CreateTextureFromDDS(TestUtils.GetTexturePath(textureNames[i] + ".dds"));
2024-02-07 15:27:55 +00:00
}
2024-02-23 20:00:15 +00:00
resourceUploader.Upload();
resourceUploader.Dispose();
2024-02-07 15:27:55 +00:00
}
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
protected override void Update(System.TimeSpan delta)
{
int prevSamplerIndex = currentTextureIndex;
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
{
currentTextureIndex -= 1;
if (currentTextureIndex < 0)
{
currentTextureIndex = textureNames.Length - 1;
}
}
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
{
currentTextureIndex += 1;
if (currentTextureIndex >= textureNames.Length)
{
currentTextureIndex = 0;
}
}
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
if (prevSamplerIndex != currentTextureIndex)
{
Logger.LogInfo("Setting texture to: " + textureNames[currentTextureIndex]);
}
}
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
protected override void Draw(double alpha)
{
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
if (backbuffer != null)
{
2024-03-01 23:03:29 +00:00
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.SafeDiscard, Color.Black));
2024-02-07 15:27:55 +00:00
cmdbuf.BindGraphicsPipeline(pipeline);
cmdbuf.BindVertexBuffers(vertexBuffer);
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textures[currentTextureIndex], sampler));
2024-02-23 20:00:15 +00:00
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
2024-02-07 15:27:55 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
2023-01-07 01:53:33 +00:00
2024-02-07 15:27:55 +00:00
public static void Main(string[] args)
{
CompressedTexturesGame game = new CompressedTexturesGame();
game.Run();
}
}
2023-01-07 01:53:33 +00:00
}