MoonWorksGraphicsTests/Examples/BasicStencilGame.cs

126 lines
3.7 KiB
C#
Raw Normal View History

using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
2024-06-05 21:19:43 +00:00
namespace MoonWorksGraphicsTests
{
2024-06-05 21:19:43 +00:00
class BasicStencilGame : Example
2024-02-07 15:27:55 +00:00
{
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-06-05 21:19:43 +00:00
public override void Init(Window window, GraphicsDevice graphicsDevice)
2024-02-07 15:27:55 +00:00
{
2024-06-05 21:19:43 +00:00
Window = window;
GraphicsDevice = graphicsDevice;
2024-02-07 15:27:55 +00:00
// Load the shaders
2024-06-05 21:19:43 +00:00
Shader vertShaderModule = new Shader(
GraphicsDevice,
TestUtils.GetShaderPath("PositionColor.vert"),
"main",
ShaderStage.Vertex,
ShaderFormat.SPIRV
);
Shader fragShaderModule = new Shader(
GraphicsDevice,
TestUtils.GetShaderPath("SolidColor.frag"),
"main",
ShaderStage.Fragment,
ShaderFormat.SPIRV
);
2024-02-07 15:27:55 +00:00
// Create the graphics pipelines
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
2024-06-05 21:19:43 +00:00
Window.SwapchainFormat,
2024-02-07 15:27:55 +00:00
vertShaderModule,
fragShaderModule
);
pipelineCreateInfo.AttachmentInfo.HasDepthStencilAttachment = true;
2024-06-05 21:19:43 +00:00
pipelineCreateInfo.AttachmentInfo.DepthStencilFormat = TextureFormat.D24_UNORM_S8_UINT;
2024-02-07 15:27:55 +00:00
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,
2024-06-05 21:19:43 +00:00
Window.Width,
Window.Height,
TextureFormat.D24_UNORM_S8_UINT,
TextureUsageFlags.DepthStencil
2024-02-07 15:27:55 +00:00
);
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-06-05 21:19:43 +00:00
public override void Update(System.TimeSpan delta) { }
2024-06-05 21:19:43 +00:00
public override void Draw(double alpha)
2024-02-07 15:27:55 +00:00
{
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
2024-06-05 21:19:43 +00:00
Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window);
if (swapchainTexture != null)
2024-02-07 15:27:55 +00:00
{
2024-06-05 21:19:43 +00:00
var renderPass = cmdbuf.BeginRenderPass(
new DepthStencilAttachmentInfo(depthStencilTexture, true, new DepthStencilValue(0, 0), StoreOp.DontCare, StoreOp.DontCare),
new ColorAttachmentInfo(swapchainTexture, false, Color.Black)
2024-02-07 15:27:55 +00:00
);
2024-06-05 21:19:43 +00:00
renderPass.BindGraphicsPipeline(maskerPipeline);
renderPass.BindVertexBuffer(vertexBuffer);
renderPass.DrawPrimitives(0, 1);
renderPass.BindGraphicsPipeline(maskeePipeline);
renderPass.DrawPrimitives(3, 1);
cmdbuf.EndRenderPass(renderPass);
2024-02-07 15:27:55 +00:00
}
GraphicsDevice.Submit(cmdbuf);
}
2024-06-05 21:19:43 +00:00
public override void Destroy()
{
throw new System.NotImplementedException();
}
}
}