MoonWorksGraphicsTests/TextureMipmaps/TextureMipmapsGame.cs

136 lines
4.0 KiB
C#
Raw Normal View History

2024-02-23 22:42:47 +00:00
using MoonWorks.Graphics;
2023-01-23 04:42:19 +00:00
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
2024-02-07 15:27:55 +00:00
class TextureMipmapsGame : Game
{
private GraphicsPipeline pipeline;
2024-02-23 22:42:47 +00:00
private GpuBuffer vertexBuffer;
private GpuBuffer indexBuffer;
2024-02-07 15:27:55 +00:00
private Texture texture;
private Sampler sampler;
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
private float scale = 0.5f;
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
public TextureMipmapsGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true)
{
Logger.LogInfo("Press Left and Right to shrink/expand the scale of the quad");
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
// Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadWithMatrix.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.frag"));
2023-01-23 04:42:19 +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.VertexShaderInfo = GraphicsShaderInfo.Create<TransformVertexUniform>(vertShaderModule, "main", 0);
pipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1;
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
2024-02-23 22:42:47 +00:00
// Create and populate the GPU resources
2024-02-07 15:27:55 +00:00
texture = Texture.CreateTexture2D(
GraphicsDevice,
256,
256,
TextureFormat.R8G8B8A8,
TextureUsageFlags.Sampler,
4
);
2023-01-23 04:42:19 +00:00
2024-02-23 22:42:47 +00:00
var resourceUploader = new ResourceUploader(GraphicsDevice);
vertexBuffer = resourceUploader.CreateBuffer(
[
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)),
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)),
2024-02-23 22:42:47 +00:00
],
BufferUsageFlags.Vertex
2024-02-07 15:27:55 +00:00
);
2024-02-23 22:42:47 +00:00
indexBuffer = resourceUploader.CreateBuffer<ushort>(
[
2024-02-07 15:27:55 +00:00
0, 1, 2,
2024-02-23 22:42:47 +00:00
0, 2, 3
],
BufferUsageFlags.Index
2024-02-07 15:27:55 +00:00
);
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
// Set the various mip levels
2024-02-23 22:42:47 +00:00
for (uint i = 0; i < texture.LevelCount; i += 1)
2024-02-07 15:27:55 +00:00
{
2024-02-23 22:42:47 +00:00
var w = texture.Width >> (int) i;
var h = texture.Height >> (int) i;
var slice = new TextureSlice
{
Texture = texture,
MipLevel = i,
BaseLayer = 0,
LayerCount = 1,
X = 0,
Y = 0,
Z = 0,
Width = w,
Height = h,
Depth = 1
};
2023-01-23 04:42:19 +00:00
2024-02-23 22:42:47 +00:00
resourceUploader.SetTextureDataFromCompressed(
slice,
TestUtils.GetTexturePath($"mip{i}.png")
);
2024-02-07 15:27:55 +00:00
}
2023-01-23 04:42:19 +00:00
2024-02-23 22:42:47 +00:00
resourceUploader.Upload();
resourceUploader.Dispose();
2024-02-07 15:27:55 +00:00
}
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
protected override void Update(System.TimeSpan delta)
{
if (TestUtils.CheckButtonDown(Inputs, TestUtils.ButtonType.Left))
{
scale = System.MathF.Max(0.01f, scale - 0.01f);
}
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
if (TestUtils.CheckButtonDown(Inputs, TestUtils.ButtonType.Right))
{
scale = System.MathF.Min(1f, scale + 0.01f);
}
}
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
protected override void Draw(double alpha)
{
TransformVertexUniform vertUniforms = new TransformVertexUniform(Matrix4x4.CreateScale(scale, scale, 1));
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
if (backbuffer != null)
{
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, Color.Black));
cmdbuf.BindGraphicsPipeline(pipeline);
cmdbuf.BindVertexBuffers(vertexBuffer);
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler));
2024-02-23 22:42:47 +00:00
cmdbuf.PushVertexShaderUniforms(vertUniforms);
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
2024-02-07 15:27:55 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
2023-01-23 04:42:19 +00:00
2024-02-07 15:27:55 +00:00
public static void Main(string[] args)
{
TextureMipmapsGame game = new TextureMipmapsGame();
game.Run();
}
}
2023-01-23 04:42:19 +00:00
}