2023-01-22 20:14:23 +00:00
|
|
|
|
using MoonWorks;
|
|
|
|
|
using MoonWorks.Graphics;
|
|
|
|
|
using MoonWorks.Math.Float;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Test
|
|
|
|
|
{
|
2024-02-07 15:27:55 +00:00
|
|
|
|
class InstancingAndOffsetsGame : Game
|
|
|
|
|
{
|
|
|
|
|
private GraphicsPipeline pipeline;
|
2024-02-23 21:06:53 +00:00
|
|
|
|
private GpuBuffer vertexBuffer;
|
|
|
|
|
private GpuBuffer indexBuffer;
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
private bool useVertexOffset;
|
|
|
|
|
private bool useIndexOffset;
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-03-07 18:35:12 +00:00
|
|
|
|
public InstancingAndOffsetsGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
|
2024-02-07 15:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.LogInfo("Press Left to toggle vertex offset\nPress Right to toggle index offset");
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Load the shaders
|
|
|
|
|
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColorInstanced.vert"));
|
|
|
|
|
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag"));
|
2023-01-22 20:14:23 +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>();
|
|
|
|
|
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Create and populate the vertex and index buffers
|
2024-02-23 22:32:56 +00:00
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
vertexBuffer = resourceUploader.CreateBuffer(
|
2024-02-23 21:06:53 +00:00
|
|
|
|
[
|
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),
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
new PositionColorVertex(new Vector3(-1, 1, 0), Color.Orange),
|
|
|
|
|
new PositionColorVertex(new Vector3(1, 1, 0), Color.Green),
|
|
|
|
|
new PositionColorVertex(new Vector3(0, -1, 0), Color.Aqua),
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
new PositionColorVertex(new Vector3(-1, 1, 0), Color.White),
|
|
|
|
|
new PositionColorVertex(new Vector3(1, 1, 0), Color.White),
|
|
|
|
|
new PositionColorVertex(new Vector3(0, -1, 0), Color.White),
|
2024-02-23 21:06:53 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Vertex
|
2024-02-07 15:27:55 +00:00
|
|
|
|
);
|
2024-02-23 21:06:53 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
indexBuffer = resourceUploader.CreateBuffer<ushort>(
|
2024-02-23 21:06:53 +00:00
|
|
|
|
[
|
2024-02-07 15:27:55 +00:00
|
|
|
|
0, 1, 2,
|
|
|
|
|
3, 4, 5,
|
2024-02-23 21:06:53 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Index
|
2024-02-07 15:27:55 +00:00
|
|
|
|
);
|
2024-02-23 21:06:53 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
resourceUploader.Upload();
|
|
|
|
|
resourceUploader.Dispose();
|
2024-02-07 15:27:55 +00:00
|
|
|
|
}
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
protected override void Update(System.TimeSpan delta)
|
|
|
|
|
{
|
|
|
|
|
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
|
|
|
|
|
{
|
|
|
|
|
useVertexOffset = !useVertexOffset;
|
|
|
|
|
Logger.LogInfo("Using vertex offset: " + useVertexOffset);
|
|
|
|
|
}
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
|
|
|
|
|
{
|
|
|
|
|
useIndexOffset = !useIndexOffset;
|
|
|
|
|
Logger.LogInfo("Using index offset: " + useIndexOffset);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
protected override void Draw(double alpha)
|
|
|
|
|
{
|
|
|
|
|
uint vertexOffset = useVertexOffset ? 3u : 0;
|
|
|
|
|
uint indexOffset = useIndexOffset ? 3u : 0;
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
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.Black));
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.BindGraphicsPipeline(pipeline);
|
|
|
|
|
cmdbuf.BindVertexBuffers(vertexBuffer);
|
|
|
|
|
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
|
2024-02-23 21:06:53 +00:00
|
|
|
|
cmdbuf.DrawInstancedPrimitives(vertexOffset, indexOffset, 1, 16);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
}
|
|
|
|
|
GraphicsDevice.Submit(cmdbuf);
|
|
|
|
|
}
|
2023-01-22 20:14:23 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
InstancingAndOffsetsGame p = new InstancingAndOffsetsGame();
|
|
|
|
|
p.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-22 20:14:23 +00:00
|
|
|
|
}
|