MoonWorksGraphicsTests/Examples/DrawIndirectGame.cs

80 lines
2.7 KiB
C#
Raw Normal View History

2024-02-23 20:51:42 +00:00
using MoonWorks.Graphics;
2023-01-03 18:27:48 +00:00
using MoonWorks.Math.Float;
using System.Runtime.InteropServices;
namespace MoonWorks.Test
{
2024-02-07 15:27:55 +00:00
class DrawIndirectGame : Game
{
private GraphicsPipeline graphicsPipeline;
2024-02-23 20:51:42 +00:00
private GpuBuffer vertexBuffer;
private GpuBuffer drawBuffer;
2023-01-03 18:27:48 +00:00
2024-03-07 18:35:12 +00:00
public DrawIndirectGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
2024-02-07 15:27:55 +00:00
{
// Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColor.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag"));
2023-01-03 18:27:48 +00:00
2024-02-07 15:27:55 +00:00
// Create the graphics pipeline
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
MainWindow.SwapchainFormat,
vertShaderModule,
fragShaderModule
);
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionColorVertex>();
graphicsPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
2023-01-03 18:27:48 +00:00
2024-02-07 15:27:55 +00:00
// Create and populate the vertex buffer
var resourceUploader = new ResourceUploader(GraphicsDevice);
2023-01-03 18:27:48 +00:00
vertexBuffer = resourceUploader.CreateBuffer(
2024-02-23 20:51:42 +00:00
[
2024-02-07 15:27:55 +00:00
new PositionColorVertex(new Vector3(-0.5f, -1, 0), Color.Blue),
new PositionColorVertex(new Vector3(-1f, 1, 0), Color.Green),
new PositionColorVertex(new Vector3(0f, 1, 0), Color.Red),
2023-01-03 18:27:48 +00:00
2024-02-07 15:27:55 +00:00
new PositionColorVertex(new Vector3(.5f, -1, 0), Color.Blue),
new PositionColorVertex(new Vector3(1f, 1, 0), Color.Green),
new PositionColorVertex(new Vector3(0f, 1, 0), Color.Red),
2024-02-23 20:51:42 +00:00
],
BufferUsageFlags.Vertex
2024-02-07 15:27:55 +00:00
);
2024-02-23 20:51:42 +00:00
drawBuffer = resourceUploader.CreateBuffer(
2024-02-23 20:51:42 +00:00
[
2024-02-07 15:27:55 +00:00
new IndirectDrawCommand(3, 1, 3, 0),
new IndirectDrawCommand(3, 1, 0, 0),
2024-02-23 20:51:42 +00:00
],
BufferUsageFlags.Indirect
2024-02-07 15:27:55 +00:00
);
2024-02-23 20:51:42 +00:00
resourceUploader.Upload();
resourceUploader.Dispose();
2024-02-07 15:27:55 +00:00
}
2023-01-03 18:27:48 +00:00
2024-02-07 15:27:55 +00:00
protected override void Update(System.TimeSpan delta) { }
2023-01-03 18:27:48 +00:00
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)
{
2024-03-07 22:24:54 +00:00
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.CornflowerBlue));
2024-02-07 15:27:55 +00:00
cmdbuf.BindGraphicsPipeline(graphicsPipeline);
cmdbuf.BindVertexBuffers(new BufferBinding(vertexBuffer, 0));
2024-02-23 20:51:42 +00:00
cmdbuf.DrawPrimitivesIndirect(drawBuffer, 0, 2, (uint) Marshal.SizeOf<IndirectDrawCommand>());
2024-02-07 15:27:55 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
2023-01-03 18:27:48 +00:00
2024-02-07 15:27:55 +00:00
public static void Main(string[] args)
{
DrawIndirectGame game = new DrawIndirectGame();
game.Run();
}
}
2023-01-03 18:27:48 +00:00
}