2024-02-23 21:32:49 +00:00
|
|
|
|
using MoonWorks.Graphics;
|
2023-01-23 02:25:01 +00:00
|
|
|
|
using MoonWorks.Math.Float;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Test
|
|
|
|
|
{
|
2024-02-07 15:27:55 +00:00
|
|
|
|
class RenderTextureMipmapsGame : Game
|
|
|
|
|
{
|
|
|
|
|
private GraphicsPipeline pipeline;
|
2024-02-23 21:32:49 +00:00
|
|
|
|
private GpuBuffer vertexBuffer;
|
|
|
|
|
private GpuBuffer indexBuffer;
|
2024-02-07 15:27:55 +00:00
|
|
|
|
private Texture texture;
|
|
|
|
|
|
|
|
|
|
private Sampler[] samplers = new Sampler[5];
|
|
|
|
|
|
|
|
|
|
private float scale = 0.5f;
|
|
|
|
|
private int currentSamplerIndex = 0;
|
|
|
|
|
private Color[] colors = new Color[]
|
|
|
|
|
{
|
|
|
|
|
Color.Red,
|
|
|
|
|
Color.Green,
|
|
|
|
|
Color.Blue,
|
|
|
|
|
Color.Yellow,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private string GetSamplerString(int index)
|
|
|
|
|
{
|
|
|
|
|
switch (index)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
return "PointClamp";
|
|
|
|
|
case 1:
|
|
|
|
|
return "LinearClamp";
|
|
|
|
|
case 2:
|
|
|
|
|
return "PointClamp with Mip LOD Bias = 0.25";
|
|
|
|
|
case 3:
|
|
|
|
|
return "PointClamp with Min LOD = 1";
|
|
|
|
|
case 4:
|
|
|
|
|
return "PointClamp with Max LOD = 1";
|
|
|
|
|
default:
|
|
|
|
|
throw new System.Exception("Unknown sampler!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 18:35:12 +00:00
|
|
|
|
public RenderTextureMipmapsGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
|
2024-02-07 15:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.LogInfo("Press Left and Right to shrink/expand the scale of the quad");
|
|
|
|
|
Logger.LogInfo("Press Down to cycle through sampler states");
|
|
|
|
|
Logger.LogInfo(GetSamplerString(currentSamplerIndex));
|
|
|
|
|
|
|
|
|
|
// Load the shaders
|
|
|
|
|
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadWithMatrix.vert"));
|
|
|
|
|
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.frag"));
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
// Create samplers
|
|
|
|
|
SamplerCreateInfo samplerCreateInfo = SamplerCreateInfo.PointClamp;
|
|
|
|
|
samplers[0] = new Sampler(GraphicsDevice, samplerCreateInfo);
|
|
|
|
|
|
|
|
|
|
samplerCreateInfo = SamplerCreateInfo.LinearClamp;
|
|
|
|
|
samplers[1] = new Sampler(GraphicsDevice, samplerCreateInfo);
|
|
|
|
|
|
|
|
|
|
samplerCreateInfo = SamplerCreateInfo.PointClamp;
|
|
|
|
|
samplerCreateInfo.MipLodBias = 0.25f;
|
|
|
|
|
samplers[2] = new Sampler(GraphicsDevice, samplerCreateInfo);
|
|
|
|
|
|
|
|
|
|
samplerCreateInfo = SamplerCreateInfo.PointClamp;
|
|
|
|
|
samplerCreateInfo.MinLod = 1;
|
|
|
|
|
samplers[3] = new Sampler(GraphicsDevice, samplerCreateInfo);
|
|
|
|
|
|
|
|
|
|
samplerCreateInfo = SamplerCreateInfo.PointClamp;
|
|
|
|
|
samplerCreateInfo.MaxLod = 1;
|
|
|
|
|
samplers[4] = new Sampler(GraphicsDevice, samplerCreateInfo);
|
|
|
|
|
|
|
|
|
|
// Create and populate the GPU resources
|
2024-02-23 22:32:56 +00:00
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
2024-02-23 21:32:49 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
vertexBuffer = resourceUploader.CreateBuffer(
|
2024-02-23 21:32:49 +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)),
|
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Vertex
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
indexBuffer = resourceUploader.CreateBuffer<ushort>(
|
2024-02-23 21:32:49 +00:00
|
|
|
|
[
|
|
|
|
|
0, 1, 2,
|
|
|
|
|
0, 2, 3,
|
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Index
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
resourceUploader.Upload();
|
|
|
|
|
resourceUploader.Dispose();
|
2024-02-23 21:32:49 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
texture = Texture.CreateTexture2D(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
MainWindow.Width,
|
|
|
|
|
MainWindow.Height,
|
|
|
|
|
TextureFormat.R8G8B8A8,
|
|
|
|
|
TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler,
|
|
|
|
|
4
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
|
|
|
|
|
|
|
|
|
// Clear each mip level to a different color
|
|
|
|
|
for (uint i = 0; i < texture.LevelCount; i += 1)
|
|
|
|
|
{
|
|
|
|
|
ColorAttachmentInfo attachmentInfo = new ColorAttachmentInfo
|
|
|
|
|
{
|
2024-03-01 23:03:29 +00:00
|
|
|
|
TextureSlice = new TextureSlice
|
|
|
|
|
{
|
|
|
|
|
Texture = texture,
|
|
|
|
|
Layer = 0,
|
|
|
|
|
MipLevel = i
|
|
|
|
|
},
|
2024-02-07 15:27:55 +00:00
|
|
|
|
ClearColor = colors[i],
|
|
|
|
|
LoadOp = LoadOp.Clear,
|
2024-03-01 23:03:29 +00:00
|
|
|
|
StoreOp = StoreOp.Store,
|
|
|
|
|
WriteOption = WriteOptions.SafeOverwrite
|
2024-02-07 15:27:55 +00:00
|
|
|
|
};
|
|
|
|
|
cmdbuf.BeginRenderPass(attachmentInfo);
|
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.Submit(cmdbuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update(System.TimeSpan delta)
|
|
|
|
|
{
|
|
|
|
|
if (TestUtils.CheckButtonDown(Inputs, TestUtils.ButtonType.Left))
|
|
|
|
|
{
|
|
|
|
|
scale = System.MathF.Max(0.01f, scale - 0.01f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TestUtils.CheckButtonDown(Inputs, TestUtils.ButtonType.Right))
|
|
|
|
|
{
|
|
|
|
|
scale = System.MathF.Min(1f, scale + 0.01f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom))
|
|
|
|
|
{
|
|
|
|
|
currentSamplerIndex = (currentSamplerIndex + 1) % samplers.Length;
|
|
|
|
|
Logger.LogInfo(GetSamplerString(currentSamplerIndex));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Draw(double alpha)
|
|
|
|
|
{
|
|
|
|
|
TransformVertexUniform vertUniforms = new TransformVertexUniform(Matrix4x4.CreateScale(scale, scale, 1));
|
|
|
|
|
|
|
|
|
|
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));
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.BindGraphicsPipeline(pipeline);
|
|
|
|
|
cmdbuf.BindVertexBuffers(vertexBuffer);
|
|
|
|
|
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
|
|
|
|
|
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, samplers[currentSamplerIndex]));
|
2024-02-23 21:32:49 +00:00
|
|
|
|
cmdbuf.PushVertexShaderUniforms(vertUniforms);
|
|
|
|
|
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
}
|
|
|
|
|
GraphicsDevice.Submit(cmdbuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
RenderTextureMipmapsGame game = new RenderTextureMipmapsGame();
|
|
|
|
|
game.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-23 02:25:01 +00:00
|
|
|
|
}
|