2023-01-27 03:22:38 +00:00
|
|
|
|
using MoonWorks;
|
|
|
|
|
using MoonWorks.Graphics;
|
2024-06-06 05:47:06 +00:00
|
|
|
|
using MoonWorks.Input;
|
2023-01-27 03:22:38 +00:00
|
|
|
|
using MoonWorks.Math.Float;
|
|
|
|
|
|
2024-06-05 21:19:43 +00:00
|
|
|
|
namespace MoonWorksGraphicsTests
|
2023-01-27 03:22:38 +00:00
|
|
|
|
{
|
2024-06-06 05:47:06 +00:00
|
|
|
|
class BasicStencilExample : Example
|
2024-02-07 15:27:55 +00:00
|
|
|
|
{
|
2024-06-06 05:47:06 +00:00
|
|
|
|
private GraphicsPipeline MaskerPipeline;
|
|
|
|
|
private GraphicsPipeline MaskeePipeline;
|
2024-06-11 17:08:37 +00:00
|
|
|
|
private Buffer VertexBuffer;
|
2024-06-06 05:47:06 +00:00
|
|
|
|
private Texture DepthStencilTexture;
|
2023-01-27 03:22:38 +00:00
|
|
|
|
|
2024-06-06 05:47:06 +00:00
|
|
|
|
public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs)
|
2024-02-07 15:27:55 +00:00
|
|
|
|
{
|
2024-06-05 21:19:43 +00:00
|
|
|
|
Window = window;
|
|
|
|
|
GraphicsDevice = graphicsDevice;
|
|
|
|
|
|
2024-06-06 05:47:06 +00:00
|
|
|
|
Window.SetTitle("BasicStencil");
|
|
|
|
|
|
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",
|
2024-06-10 23:54:27 +00:00
|
|
|
|
new ShaderCreateInfo
|
|
|
|
|
{
|
|
|
|
|
ShaderStage = ShaderStage.Vertex,
|
|
|
|
|
ShaderFormat = ShaderFormat.SPIRV
|
|
|
|
|
}
|
2024-06-05 21:19:43 +00:00
|
|
|
|
);
|
|
|
|
|
Shader fragShaderModule = new Shader(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
TestUtils.GetShaderPath("SolidColor.frag"),
|
|
|
|
|
"main",
|
2024-06-10 23:54:27 +00:00
|
|
|
|
new ShaderCreateInfo
|
|
|
|
|
{
|
|
|
|
|
ShaderStage = ShaderStage.Fragment,
|
|
|
|
|
ShaderFormat = ShaderFormat.SPIRV
|
|
|
|
|
}
|
2024-06-05 21:19:43 +00:00
|
|
|
|
);
|
2023-01-27 03:22:38 +00:00
|
|
|
|
|
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
|
|
|
|
};
|
2024-06-06 05:47:06 +00:00
|
|
|
|
MaskerPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
2023-01-27 03:22:38 +00:00
|
|
|
|
|
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
|
|
|
|
};
|
2024-06-06 05:47:06 +00:00
|
|
|
|
MaskeePipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
2023-01-27 03:22:38 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Create and populate the GPU resources
|
2024-06-06 05:47:06 +00:00
|
|
|
|
DepthStencilTexture = Texture.CreateTexture2D(
|
2024-02-07 15:27:55 +00:00
|
|
|
|
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
|
|
|
|
);
|
2023-01-27 03:22:38 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
2024-02-23 18:59:38 +00:00
|
|
|
|
|
2024-06-06 05:47:06 +00:00
|
|
|
|
VertexBuffer = resourceUploader.CreateBuffer(
|
2024-02-23 18:59:38 +00:00
|
|
|
|
[
|
2024-06-06 05:47:06 +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),
|
2023-01-27 03:22:38 +00:00
|
|
|
|
|
2024-06-06 05:47:06 +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
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
resourceUploader.Upload();
|
|
|
|
|
resourceUploader.Dispose();
|
2024-02-07 15:27:55 +00:00
|
|
|
|
}
|
2023-01-27 03:22:38 +00:00
|
|
|
|
|
2024-06-05 21:19:43 +00:00
|
|
|
|
public override void Update(System.TimeSpan delta) { }
|
2023-01-27 03:22:38 +00:00
|
|
|
|
|
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(
|
2024-06-06 05:47:06 +00:00
|
|
|
|
new DepthStencilAttachmentInfo(DepthStencilTexture, true, new DepthStencilValue(0, 0), StoreOp.DontCare, StoreOp.DontCare),
|
2024-06-05 21:19:43 +00:00
|
|
|
|
new ColorAttachmentInfo(swapchainTexture, false, Color.Black)
|
2024-02-07 15:27:55 +00:00
|
|
|
|
);
|
2024-06-06 05:47:06 +00:00
|
|
|
|
renderPass.BindGraphicsPipeline(MaskerPipeline);
|
|
|
|
|
renderPass.BindVertexBuffer(VertexBuffer);
|
2024-06-05 21:19:43 +00:00
|
|
|
|
renderPass.DrawPrimitives(0, 1);
|
2024-06-06 05:47:06 +00:00
|
|
|
|
renderPass.BindGraphicsPipeline(MaskeePipeline);
|
2024-06-05 21:19:43 +00:00
|
|
|
|
renderPass.DrawPrimitives(3, 1);
|
|
|
|
|
cmdbuf.EndRenderPass(renderPass);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
}
|
|
|
|
|
GraphicsDevice.Submit(cmdbuf);
|
|
|
|
|
}
|
2023-01-27 03:22:38 +00:00
|
|
|
|
|
2024-06-05 21:19:43 +00:00
|
|
|
|
public override void Destroy()
|
|
|
|
|
{
|
2024-06-06 05:47:06 +00:00
|
|
|
|
MaskerPipeline.Dispose();
|
|
|
|
|
MaskeePipeline.Dispose();
|
|
|
|
|
VertexBuffer.Dispose();
|
|
|
|
|
DepthStencilTexture.Dispose();
|
2024-06-05 21:19:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-27 03:22:38 +00:00
|
|
|
|
}
|