MoonWorksGraphicsTests/Examples/TexturedQuadGame.cs

166 lines
5.6 KiB
C#
Raw Normal View History

2024-02-23 08:07:51 +00:00
using System;
2022-11-09 19:54:42 +00:00
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
class TexturedQuadGame : Game
{
private GraphicsPipeline pipeline;
2024-02-23 08:07:51 +00:00
private GpuBuffer vertexBuffer;
private GpuBuffer indexBuffer;
2022-11-09 19:54:42 +00:00
private Sampler[] samplers = new Sampler[6];
private string[] samplerNames = new string[]
{
"PointClamp",
"PointWrap",
"LinearClamp",
"LinearWrap",
"AnisotropicClamp",
"AnisotropicWrap"
};
private int currentSamplerIndex;
2023-04-04 06:13:37 +00:00
private Texture[] textures = new Texture[4];
private string[] imageLoadFormatNames = new string[]
{
2023-04-19 06:39:45 +00:00
"PNG from file",
"PNG from memory",
"QOI from file",
"QOI from memory"
2023-04-04 06:13:37 +00:00
};
private int currentTextureIndex;
private System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
2024-03-07 18:35:12 +00:00
public TexturedQuadGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
2022-11-09 19:54:42 +00:00
{
Logger.LogInfo("Press Left and Right to cycle between sampler states");
2022-11-09 19:54:42 +00:00
Logger.LogInfo("Setting sampler state to: " + samplerNames[0]);
2023-04-04 06:13:37 +00:00
Logger.LogInfo("Press Down to cycle between image load formats");
Logger.LogInfo("Setting image format to: " + imageLoadFormatNames[0]);
var pngBytes = System.IO.File.ReadAllBytes(TestUtils.GetTexturePath("ravioli.png"));
var qoiBytes = System.IO.File.ReadAllBytes(TestUtils.GetTexturePath("ravioli.qoi"));
Logger.LogInfo(pngBytes.Length.ToString());
Logger.LogInfo(qoiBytes.Length.ToString());
2022-11-09 19:54:42 +00:00
// Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.frag"));
2022-11-09 19:54:42 +00:00
// Create the graphics pipeline
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
2022-11-17 20:33:58 +00:00
MainWindow.SwapchainFormat,
vertShaderModule,
fragShaderModule
);
2023-01-06 20:38:30 +00:00
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
2022-11-09 19:54:42 +00:00
pipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1;
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
// Create samplers
samplers[0] = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
samplers[1] = new Sampler(GraphicsDevice, SamplerCreateInfo.PointWrap);
samplers[2] = new Sampler(GraphicsDevice, SamplerCreateInfo.LinearClamp);
samplers[3] = new Sampler(GraphicsDevice, SamplerCreateInfo.LinearWrap);
samplers[4] = new Sampler(GraphicsDevice, SamplerCreateInfo.AnisotropicClamp);
samplers[5] = new Sampler(GraphicsDevice, SamplerCreateInfo.AnisotropicWrap);
2024-02-23 08:07:51 +00:00
var vertexData = new Span<PositionTextureVertex>([
new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)),
new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(4, 0)),
new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(4, 4)),
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 4)),
]);
var indexData = new Span<ushort>([
0, 1, 2,
0, 2, 3,
]);
2022-11-09 19:54:42 +00:00
// Create and populate the GPU resources
var resourceUploader = new ResourceUploader(GraphicsDevice);
2024-02-23 08:07:51 +00:00
vertexBuffer = resourceUploader.CreateBuffer(vertexData, BufferUsageFlags.Vertex);
indexBuffer = resourceUploader.CreateBuffer(indexData, BufferUsageFlags.Index);
2024-02-23 08:07:51 +00:00
textures[0] = resourceUploader.CreateTexture2DFromCompressed(TestUtils.GetTexturePath("ravioli.png"));
textures[1] = resourceUploader.CreateTexture2DFromCompressed(pngBytes);
textures[2] = resourceUploader.CreateTexture2DFromCompressed(TestUtils.GetTexturePath("ravioli.qoi"));
textures[3] = resourceUploader.CreateTexture2DFromCompressed(qoiBytes);
2024-02-23 08:07:51 +00:00
resourceUploader.Upload();
resourceUploader.Dispose();
2022-11-09 19:54:42 +00:00
}
protected override void Update(System.TimeSpan delta)
{
int prevSamplerIndex = currentSamplerIndex;
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
2022-11-09 19:54:42 +00:00
{
currentSamplerIndex -= 1;
if (currentSamplerIndex < 0)
{
currentSamplerIndex = samplers.Length - 1;
}
}
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
2022-11-09 19:54:42 +00:00
{
currentSamplerIndex += 1;
if (currentSamplerIndex >= samplers.Length)
{
currentSamplerIndex = 0;
}
}
if (prevSamplerIndex != currentSamplerIndex)
{
Logger.LogInfo("Setting sampler state to: " + samplerNames[currentSamplerIndex]);
}
2023-04-04 06:13:37 +00:00
int prevTextureIndex = currentTextureIndex;
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom))
{
currentTextureIndex = (currentTextureIndex + 1) % imageLoadFormatNames.Length;
}
if (prevTextureIndex != currentTextureIndex)
{
Logger.LogInfo("Setting texture format to: " + imageLoadFormatNames[currentTextureIndex]);
}
2022-11-09 19:54:42 +00:00
}
protected override void Draw(double alpha)
{
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
if (backbuffer != null)
{
2024-03-07 22:24:54 +00:00
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.Black));
2022-11-09 19:54:42 +00:00
cmdbuf.BindGraphicsPipeline(pipeline);
cmdbuf.BindVertexBuffers(vertexBuffer);
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
2023-04-04 06:13:37 +00:00
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textures[currentTextureIndex], samplers[currentSamplerIndex]));
2024-02-23 08:07:51 +00:00
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
2022-11-09 19:54:42 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
public static void Main(string[] args)
{
TexturedQuadGame game = new TexturedQuadGame();
game.Run();
}
}
}