2022-11-09 19:54:42 +00:00
|
|
|
|
using MoonWorks;
|
|
|
|
|
using MoonWorks.Math.Float;
|
|
|
|
|
using MoonWorks.Graphics;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Test
|
|
|
|
|
{
|
|
|
|
|
class MSAAGame : Game
|
|
|
|
|
{
|
|
|
|
|
private GraphicsPipeline[] msaaPipelines = new GraphicsPipeline[4];
|
|
|
|
|
private GraphicsPipeline blitPipeline;
|
|
|
|
|
|
2023-01-31 03:39:13 +00:00
|
|
|
|
private Texture[] renderTargets = new Texture[4];
|
2022-11-09 19:54:42 +00:00
|
|
|
|
private Sampler rtSampler;
|
2024-02-23 21:09:23 +00:00
|
|
|
|
private GpuBuffer quadVertexBuffer;
|
|
|
|
|
private GpuBuffer quadIndexBuffer;
|
2022-11-09 19:54:42 +00:00
|
|
|
|
|
|
|
|
|
private SampleCount currentSampleCount = SampleCount.Four;
|
|
|
|
|
|
2024-03-07 18:35:12 +00:00
|
|
|
|
public MSAAGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
|
2022-11-09 19:54:42 +00:00
|
|
|
|
{
|
2022-12-23 04:48:07 +00:00
|
|
|
|
Logger.LogInfo("Press Left and Right to cycle between sample counts");
|
2022-11-09 19:54:42 +00:00
|
|
|
|
Logger.LogInfo("Setting sample count to: " + currentSampleCount);
|
|
|
|
|
|
|
|
|
|
// Create the MSAA pipelines
|
2023-07-01 17:41:42 +00:00
|
|
|
|
ShaderModule triangleVertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("RawTriangle.vert"));
|
|
|
|
|
ShaderModule triangleFragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag"));
|
2022-11-09 19:54:42 +00:00
|
|
|
|
|
|
|
|
|
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
|
2022-11-17 20:33:58 +00:00
|
|
|
|
TextureFormat.R8G8B8A8,
|
2022-11-09 19:54:42 +00:00
|
|
|
|
triangleVertShaderModule,
|
|
|
|
|
triangleFragShaderModule
|
|
|
|
|
);
|
|
|
|
|
for (int i = 0; i < msaaPipelines.Length; i += 1)
|
|
|
|
|
{
|
|
|
|
|
pipelineCreateInfo.MultisampleState.MultisampleCount = (SampleCount) i;
|
|
|
|
|
msaaPipelines[i] = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the blit pipeline
|
2023-07-01 17:41:42 +00:00
|
|
|
|
ShaderModule blitVertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.vert"));
|
|
|
|
|
ShaderModule blitFragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.frag"));
|
2022-11-09 19:54:42 +00:00
|
|
|
|
|
|
|
|
|
pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
|
2022-11-17 20:33:58 +00:00
|
|
|
|
MainWindow.SwapchainFormat,
|
2022-11-09 19:54:42 +00:00
|
|
|
|
blitVertShaderModule,
|
|
|
|
|
blitFragShaderModule
|
|
|
|
|
);
|
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;
|
|
|
|
|
blitPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
|
|
|
|
|
2023-01-31 03:39:13 +00:00
|
|
|
|
// Create the MSAA render textures
|
|
|
|
|
for (int i = 0; i < renderTargets.Length; i += 1)
|
|
|
|
|
{
|
|
|
|
|
renderTargets[i] = Texture.CreateTexture2D(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
MainWindow.Width,
|
|
|
|
|
MainWindow.Height,
|
|
|
|
|
TextureFormat.R8G8B8A8,
|
|
|
|
|
TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler,
|
|
|
|
|
1,
|
|
|
|
|
(SampleCount) i
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the sampler
|
2022-11-09 19:54:42 +00:00
|
|
|
|
rtSampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
|
|
|
|
|
|
|
|
|
|
// Create and populate the vertex and index buffers
|
2024-02-23 22:32:56 +00:00
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
2022-11-09 19:54:42 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
quadVertexBuffer = resourceUploader.CreateBuffer(
|
2024-02-23 21:09:23 +00:00
|
|
|
|
[
|
2022-11-09 19:54:42 +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 21:09:23 +00:00
|
|
|
|
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1))
|
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Vertex
|
2022-11-09 19:54:42 +00:00
|
|
|
|
);
|
2024-02-23 21:09:23 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
quadIndexBuffer = resourceUploader.CreateBuffer<ushort>(
|
2024-02-23 21:09:23 +00:00
|
|
|
|
[
|
2022-11-09 19:54:42 +00:00
|
|
|
|
0, 1, 2,
|
2024-02-23 21:09:23 +00:00
|
|
|
|
0, 2, 3
|
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Index
|
2022-11-09 19:54:42 +00:00
|
|
|
|
);
|
2024-02-23 21:09:23 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
resourceUploader.Upload();
|
|
|
|
|
resourceUploader.Dispose();
|
2022-11-09 19:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update(System.TimeSpan delta)
|
|
|
|
|
{
|
|
|
|
|
SampleCount prevSampleCount = currentSampleCount;
|
|
|
|
|
|
2022-12-23 04:48:07 +00:00
|
|
|
|
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
|
2022-11-09 19:54:42 +00:00
|
|
|
|
{
|
|
|
|
|
currentSampleCount -= 1;
|
|
|
|
|
if (currentSampleCount < 0)
|
|
|
|
|
{
|
|
|
|
|
currentSampleCount = SampleCount.Eight;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-23 04:48:07 +00:00
|
|
|
|
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
|
2022-11-09 19:54:42 +00:00
|
|
|
|
{
|
|
|
|
|
currentSampleCount += 1;
|
|
|
|
|
if (currentSampleCount > SampleCount.Eight)
|
|
|
|
|
{
|
|
|
|
|
currentSampleCount = SampleCount.One;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prevSampleCount != currentSampleCount)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogInfo("Setting sample count to: " + currentSampleCount);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Draw(double alpha)
|
|
|
|
|
{
|
|
|
|
|
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
|
|
|
|
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
|
|
|
|
|
if (backbuffer != null)
|
|
|
|
|
{
|
2023-01-31 03:39:13 +00:00
|
|
|
|
Texture rt = renderTargets[(int) currentSampleCount];
|
|
|
|
|
|
2024-03-07 22:24:54 +00:00
|
|
|
|
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(rt, WriteOptions.Cycle, Color.Black));
|
2022-11-09 19:54:42 +00:00
|
|
|
|
cmdbuf.BindGraphicsPipeline(msaaPipelines[(int) currentSampleCount]);
|
2024-02-23 21:09:23 +00:00
|
|
|
|
cmdbuf.DrawPrimitives(0, 1);
|
2022-11-09 19:54:42 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
|
2024-03-07 22:24:54 +00:00
|
|
|
|
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, LoadOp.DontCare));
|
2022-11-09 19:54:42 +00:00
|
|
|
|
cmdbuf.BindGraphicsPipeline(blitPipeline);
|
|
|
|
|
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(rt, rtSampler));
|
|
|
|
|
cmdbuf.BindVertexBuffers(quadVertexBuffer);
|
|
|
|
|
cmdbuf.BindIndexBuffer(quadIndexBuffer, IndexElementSize.Sixteen);
|
2024-02-23 21:09:23 +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)
|
|
|
|
|
{
|
|
|
|
|
MSAAGame game = new MSAAGame();
|
|
|
|
|
game.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|