InstancingAndOffsetsExample

refresh2
cosmonaut 2024-06-06 14:11:14 -07:00
parent e85220318f
commit ab796356b9
4 changed files with 135 additions and 106 deletions

View File

@ -0,0 +1,131 @@
using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Input;
using MoonWorks.Math.Float;
namespace MoonWorksGraphicsTests;
class InstancingAndOffsetsExample : Example
{
private GraphicsPipeline Pipeline;
private GpuBuffer VertexBuffer;
private GpuBuffer IndexBuffer;
private bool useVertexOffset;
private bool useIndexOffset;
public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs)
{
Window = window;
GraphicsDevice = graphicsDevice;
Inputs = inputs;
Window.SetTitle("InstancingAndOffsets");
Logger.LogInfo("Press Left to toggle vertex offset\nPress Right to toggle index offset");
// Load the shaders
Shader vertShader = new Shader(
GraphicsDevice,
TestUtils.GetShaderPath("PositionColorInstanced.vert"),
"main",
ShaderStage.Vertex,
ShaderFormat.SPIRV
);
Shader fragShader = new Shader(
GraphicsDevice,
TestUtils.GetShaderPath("SolidColor.frag"),
"main",
ShaderStage.Fragment,
ShaderFormat.SPIRV
);
// Create the graphics pipeline
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
Window.SwapchainFormat,
vertShader,
fragShader
);
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionColorVertex>();
Pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
// Create and populate the vertex and index buffers
var resourceUploader = new ResourceUploader(GraphicsDevice);
VertexBuffer = resourceUploader.CreateBuffer(
[
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),
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),
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),
],
BufferUsageFlags.Vertex
);
IndexBuffer = resourceUploader.CreateBuffer<ushort>(
[
0, 1, 2,
3, 4, 5,
],
BufferUsageFlags.Index
);
resourceUploader.Upload();
resourceUploader.Dispose();
}
public override void Update(System.TimeSpan delta)
{
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
{
useVertexOffset = !useVertexOffset;
Logger.LogInfo("Using vertex offset: " + useVertexOffset);
}
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
{
useIndexOffset = !useIndexOffset;
Logger.LogInfo("Using index offset: " + useIndexOffset);
}
}
public override void Draw(double alpha)
{
uint vertexOffset = useVertexOffset ? 3u : 0;
uint indexOffset = useIndexOffset ? 3u : 0;
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window);
if (swapchainTexture != null)
{
var renderPass = cmdbuf.BeginRenderPass(
new ColorAttachmentInfo(
swapchainTexture,
false,
Color.Black
)
);
renderPass.BindGraphicsPipeline(Pipeline);
renderPass.BindVertexBuffer(VertexBuffer);
renderPass.BindIndexBuffer(IndexBuffer, IndexElementSize.Sixteen);
renderPass.DrawIndexedPrimitives(vertexOffset, indexOffset, 1, 16);
cmdbuf.EndRenderPass(renderPass);
}
GraphicsDevice.Submit(cmdbuf);
}
public override void Destroy()
{
Pipeline.Dispose();
VertexBuffer.Dispose();
IndexBuffer.Dispose();
}
}

View File

@ -1,105 +0,0 @@
using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
class InstancingAndOffsetsGame : Game
{
private GraphicsPipeline pipeline;
private GpuBuffer vertexBuffer;
private GpuBuffer indexBuffer;
private bool useVertexOffset;
private bool useIndexOffset;
public InstancingAndOffsetsGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
{
Logger.LogInfo("Press Left to toggle vertex offset\nPress Right to toggle index offset");
// Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColorInstanced.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag"));
// Create the graphics pipeline
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
MainWindow.SwapchainFormat,
vertShaderModule,
fragShaderModule
);
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionColorVertex>();
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
// Create and populate the vertex and index buffers
var resourceUploader = new ResourceUploader(GraphicsDevice);
vertexBuffer = resourceUploader.CreateBuffer(
[
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),
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),
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),
],
BufferUsageFlags.Vertex
);
indexBuffer = resourceUploader.CreateBuffer<ushort>(
[
0, 1, 2,
3, 4, 5,
],
BufferUsageFlags.Index
);
resourceUploader.Upload();
resourceUploader.Dispose();
}
protected override void Update(System.TimeSpan delta)
{
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
{
useVertexOffset = !useVertexOffset;
Logger.LogInfo("Using vertex offset: " + useVertexOffset);
}
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
{
useIndexOffset = !useIndexOffset;
Logger.LogInfo("Using index offset: " + useIndexOffset);
}
}
protected override void Draw(double alpha)
{
uint vertexOffset = useVertexOffset ? 3u : 0;
uint indexOffset = useIndexOffset ? 3u : 0;
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
if (backbuffer != null)
{
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.Black));
cmdbuf.BindGraphicsPipeline(pipeline);
cmdbuf.BindVertexBuffers(vertexBuffer);
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
cmdbuf.DrawInstancedPrimitives(vertexOffset, indexOffset, 1, 16);
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
public static void Main(string[] args)
{
InstancingAndOffsetsGame p = new InstancingAndOffsetsGame();
p.Run();
}
}
}

View File

@ -37,6 +37,7 @@
<Compile Include="Examples\DepthMSAAExample.cs" /> <Compile Include="Examples\DepthMSAAExample.cs" />
<Compile Include="Examples\DrawIndirectExample.cs" /> <Compile Include="Examples\DrawIndirectExample.cs" />
<Compile Include="Examples\GetBufferDataExample.cs" /> <Compile Include="Examples\GetBufferDataExample.cs" />
<Compile Include="Examples\InstancingAndOffsetsExample.cs" />
</ItemGroup> </ItemGroup>
<Import Project=".\CopyMoonlibs.targets" /> <Import Project=".\CopyMoonlibs.targets" />

View File

@ -20,7 +20,8 @@ class Program : Game
new CullFaceExample(), new CullFaceExample(),
new DepthMSAAExample(), new DepthMSAAExample(),
new DrawIndirectExample(), new DrawIndirectExample(),
new GetBufferDataExample() new GetBufferDataExample(),
new InstancingAndOffsetsExample()
]; ];
int ExampleIndex = 0; int ExampleIndex = 0;
@ -33,6 +34,7 @@ class Program : Game
bool debugMode = false bool debugMode = false
) : base(windowCreateInfo, frameLimiterSettings, preferredBackends, targetTimestep, debugMode) ) : base(windowCreateInfo, frameLimiterSettings, preferredBackends, targetTimestep, debugMode)
{ {
Logger.LogInfo("Welcome to the MoonWorks Graphics Tests program! Press Q and E to cycle through examples!");
Examples[ExampleIndex].Init(MainWindow, GraphicsDevice, Inputs); Examples[ExampleIndex].Init(MainWindow, GraphicsDevice, Inputs);
} }