MoonWorksGraphicsTests/BasicStencil/BasicStencilGame.cs

112 lines
3.6 KiB
C#
Raw Normal View History

using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
2024-02-07 15:27:55 +00:00
class BasicStencilGame : Game
{
private GraphicsPipeline maskerPipeline;
private GraphicsPipeline maskeePipeline;
2024-02-23 18:59:38 +00:00
private GpuBuffer vertexBuffer;
2024-02-07 15:27:55 +00:00
private Texture depthStencilTexture;
2024-02-07 15:27:55 +00:00
public BasicStencilGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true)
{
// Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColor.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag"));
2024-02-07 15:27:55 +00:00
// Create the graphics pipelines
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
MainWindow.SwapchainFormat,
vertShaderModule,
fragShaderModule
);
pipelineCreateInfo.AttachmentInfo.HasDepthStencilAttachment = true;
pipelineCreateInfo.AttachmentInfo.DepthStencilFormat = TextureFormat.D16S8;
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionColorVertex>();
pipelineCreateInfo.DepthStencilState = new DepthStencilState
{
StencilTestEnable = true,
2024-03-03 07:10:52 +00:00
FrontStencilState = new StencilOpState
2024-02-07 15:27:55 +00:00
{
CompareOp = CompareOp.Never,
FailOp = StencilOp.Replace,
2024-03-03 07:10:52 +00:00
},
Reference = 1,
WriteMask = 0xFF
2024-02-07 15:27:55 +00:00
};
maskerPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
2024-02-07 15:27:55 +00:00
pipelineCreateInfo.DepthStencilState = new DepthStencilState
{
StencilTestEnable = true,
2024-03-03 07:10:52 +00:00
FrontStencilState = new StencilOpState
2024-02-07 15:27:55 +00:00
{
CompareOp = CompareOp.Equal,
2024-03-03 07:10:52 +00:00
},
Reference = 0,
CompareMask = 0xFF,
WriteMask = 0
2024-02-07 15:27:55 +00:00
};
maskeePipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
2024-02-07 15:27:55 +00:00
// Create and populate the GPU resources
depthStencilTexture = Texture.CreateTexture2D(
GraphicsDevice,
MainWindow.Width,
MainWindow.Height,
TextureFormat.D16S8,
TextureUsageFlags.DepthStencilTarget
);
var resourceUploader = new ResourceUploader(GraphicsDevice);
2024-02-23 18:59:38 +00:00
vertexBuffer = resourceUploader.CreateBuffer(
2024-02-23 18:59:38 +00:00
[
2024-02-07 15:27:55 +00:00
new PositionColorVertex(new Vector3(-0.5f, 0.5f, 0), Color.Yellow),
new PositionColorVertex(new Vector3(0.5f, 0.5f, 0), Color.Yellow),
new PositionColorVertex(new Vector3(0, -0.5f, 0), Color.Yellow),
2024-02-07 15:27:55 +00:00
new PositionColorVertex(new Vector3(-1, 1, 0), Color.Red),
new PositionColorVertex(new Vector3(1, 1, 0), Color.Lime),
new PositionColorVertex(new Vector3(0, -1, 0), Color.Blue),
2024-02-23 18:59:38 +00:00
],
BufferUsageFlags.Vertex
2024-02-07 15:27:55 +00:00
);
2024-02-23 18:59:38 +00:00
resourceUploader.Upload();
resourceUploader.Dispose();
2024-02-07 15:27:55 +00:00
}
2024-02-07 15:27:55 +00:00
protected override void Update(System.TimeSpan delta) { }
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)
{
cmdbuf.BeginRenderPass(
2024-03-01 23:03:29 +00:00
new DepthStencilAttachmentInfo(depthStencilTexture, WriteOptions.SafeDiscard, new DepthStencilValue(0, 0), StoreOp.DontCare, StoreOp.DontCare),
new ColorAttachmentInfo(backbuffer, WriteOptions.SafeDiscard, Color.Black)
2024-02-07 15:27:55 +00:00
);
cmdbuf.BindGraphicsPipeline(maskerPipeline);
cmdbuf.BindVertexBuffers(vertexBuffer);
2024-02-23 18:59:38 +00:00
cmdbuf.DrawPrimitives(0, 1);
2024-02-07 15:27:55 +00:00
cmdbuf.BindGraphicsPipeline(maskeePipeline);
2024-02-23 18:59:38 +00:00
cmdbuf.DrawPrimitives(3, 1);
2024-02-07 15:27:55 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
2024-02-07 15:27:55 +00:00
public static void Main(string[] args)
{
BasicStencilGame p = new BasicStencilGame();
p.Run();
}
}
}