GetBufferDataExample

refresh2
cosmonaut 2024-06-06 13:24:11 -07:00
parent 83bdf69e64
commit e85220318f
4 changed files with 146 additions and 136 deletions

View File

@ -0,0 +1,143 @@
using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Input;
using MoonWorks.Math.Float;
using System.Runtime.InteropServices;
namespace MoonWorksGraphicsTests;
class GetBufferDataExample : Example
{
public override void Init(Window window, GraphicsDevice graphicsDevice, Inputs inputs)
{
Window = window;
GraphicsDevice = graphicsDevice;
Window.SetTitle("GetBufferData");
var vertices = new System.Span<PositionVertex>(
[
new PositionVertex(new Vector3(0, 0, 0)),
new PositionVertex(new Vector3(0, 0, 1)),
new PositionVertex(new Vector3(0, 1, 0)),
new PositionVertex(new Vector3(0, 1, 1)),
new PositionVertex(new Vector3(1, 0, 0)),
new PositionVertex(new Vector3(1, 0, 1)),
new PositionVertex(new Vector3(1, 1, 0)),
new PositionVertex(new Vector3(1, 1, 1)),
]);
var otherVerts = new System.Span<PositionVertex>(
[
new PositionVertex(new Vector3(1, 2, 3)),
new PositionVertex(new Vector3(4, 5, 6)),
new PositionVertex(new Vector3(7, 8, 9))
]);
int vertexSize = Marshal.SizeOf<PositionVertex>();
var resourceUploader = new ResourceUploader(GraphicsDevice);
var vertexBuffer = resourceUploader.CreateBuffer(vertices, BufferUsageFlags.Vertex);
resourceUploader.Upload();
resourceUploader.Dispose();
var transferBuffer = new TransferBuffer(
GraphicsDevice,
TransferUsage.Buffer,
TransferBufferMapFlags.Read | TransferBufferMapFlags.Write,
vertexBuffer.Size
);
// Read back and print out the vertex values
var cmdbuf = GraphicsDevice.AcquireCommandBuffer();
var copyPass = cmdbuf.BeginCopyPass();
copyPass.DownloadFromBuffer(
vertexBuffer,
transferBuffer,
new BufferCopy(0, 0, vertexBuffer.Size)
);
cmdbuf.EndCopyPass(copyPass);
GraphicsDevice.Submit(cmdbuf);
PositionVertex[] readbackVertices = new PositionVertex[vertices.Length];
transferBuffer.GetData<PositionVertex>(readbackVertices);
for (int i = 0; i < readbackVertices.Length; i += 1)
{
Logger.LogInfo(readbackVertices[i].ToString());
}
// Change the first three vertices and upload
transferBuffer.SetData(otherVerts, false);
cmdbuf = GraphicsDevice.AcquireCommandBuffer();
copyPass = cmdbuf.BeginCopyPass();
copyPass.UploadToBuffer(transferBuffer, vertexBuffer, false);
copyPass.DownloadFromBuffer(vertexBuffer, transferBuffer, new BufferCopy(0, 0, vertexBuffer.Size));
cmdbuf.EndCopyPass(copyPass);
var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFence(fence);
GraphicsDevice.ReleaseFence(fence);
// Read the updated buffer
transferBuffer.GetData<PositionVertex>(readbackVertices);
Logger.LogInfo("=== Change first three vertices ===");
for (int i = 0; i < readbackVertices.Length; i += 1)
{
Logger.LogInfo(readbackVertices[i].ToString());
}
// Change the last two vertices and upload
cmdbuf = GraphicsDevice.AcquireCommandBuffer();
var lastTwoSpan = otherVerts.Slice(1, 2);
transferBuffer.SetData(lastTwoSpan, false);
copyPass = cmdbuf.BeginCopyPass();
copyPass.UploadToBuffer<PositionVertex>(
transferBuffer,
vertexBuffer,
0,
(uint)(vertices.Length - 2),
2,
false
);
copyPass.DownloadFromBuffer(vertexBuffer, transferBuffer, new BufferCopy(0, 0, vertexBuffer.Size));
cmdbuf.EndCopyPass(copyPass);
fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFence(fence);
GraphicsDevice.ReleaseFence(fence);
// Read the updated buffer
transferBuffer.GetData<PositionVertex>(readbackVertices);
Logger.LogInfo("=== Change last two vertices ===");
for (int i = 0; i < readbackVertices.Length; i += 1)
{
Logger.LogInfo(readbackVertices[i].ToString());
}
vertexBuffer.Dispose();
transferBuffer.Dispose();
}
public override void Update(System.TimeSpan delta) { }
public override void Draw(double alpha)
{
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture swapchainTexture = cmdbuf.AcquireSwapchainTexture(Window);
if (swapchainTexture != null)
{
var renderPass = cmdbuf.BeginRenderPass(
new ColorAttachmentInfo(
swapchainTexture,
false,
Color.Black
)
);
cmdbuf.EndRenderPass(renderPass);
}
GraphicsDevice.Submit(cmdbuf);
}
public override void Destroy()
{
}
}

View File

@ -1,135 +0,0 @@
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
using System.Runtime.InteropServices;
namespace MoonWorks.Test
{
class GetBufferDataGame : Game
{
public GetBufferDataGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
{
var vertices = new System.Span<PositionVertex>(
[
new PositionVertex(new Vector3(0, 0, 0)),
new PositionVertex(new Vector3(0, 0, 1)),
new PositionVertex(new Vector3(0, 1, 0)),
new PositionVertex(new Vector3(0, 1, 1)),
new PositionVertex(new Vector3(1, 0, 0)),
new PositionVertex(new Vector3(1, 0, 1)),
new PositionVertex(new Vector3(1, 1, 0)),
new PositionVertex(new Vector3(1, 1, 1)),
]);
var otherVerts = new System.Span<PositionVertex>(
[
new PositionVertex(new Vector3(1, 2, 3)),
new PositionVertex(new Vector3(4, 5, 6)),
new PositionVertex(new Vector3(7, 8, 9))
]);
int vertexSize = Marshal.SizeOf<PositionVertex>();
var resourceUploader = new ResourceUploader(GraphicsDevice);
var vertexBuffer = resourceUploader.CreateBuffer(vertices, BufferUsageFlags.Vertex);
// Wait for the vertices to finish copying...
resourceUploader.UploadAndWait();
resourceUploader.Dispose();
var transferBuffer = new TransferBuffer(GraphicsDevice, TransferUsage.Buffer, vertexBuffer.Size);
// Read back and print out the vertex values
GraphicsDevice.DownloadFromBuffer(
vertexBuffer,
transferBuffer,
TransferOptions.Unsafe
);
PositionVertex[] readbackVertices = new PositionVertex[vertices.Length];
transferBuffer.GetData<PositionVertex>(readbackVertices);
for (int i = 0; i < readbackVertices.Length; i += 1)
{
Logger.LogInfo(readbackVertices[i].ToString());
}
// Change the first three vertices and upload
transferBuffer.SetData(otherVerts, TransferOptions.Unsafe);
var cmdbuf = GraphicsDevice.AcquireCommandBuffer();
cmdbuf.BeginCopyPass();
cmdbuf.UploadToBuffer(transferBuffer, vertexBuffer, WriteOptions.Unsafe);
cmdbuf.EndCopyPass();
var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFences(fence);
GraphicsDevice.ReleaseFence(fence);
// Download the data
GraphicsDevice.DownloadFromBuffer(
vertexBuffer,
transferBuffer,
TransferOptions.Unsafe
);
// Read the updated buffer
transferBuffer.GetData<PositionVertex>(readbackVertices);
Logger.LogInfo("=== Change first three vertices ===");
for (int i = 0; i < readbackVertices.Length; i += 1)
{
Logger.LogInfo(readbackVertices[i].ToString());
}
// Change the last two vertices and upload
cmdbuf = GraphicsDevice.AcquireCommandBuffer();
var lastTwoSpan = otherVerts.Slice(1, 2);
transferBuffer.SetData(lastTwoSpan, TransferOptions.Unsafe);
cmdbuf.BeginCopyPass();
cmdbuf.UploadToBuffer<PositionVertex>(
transferBuffer,
vertexBuffer,
0,
(uint)(vertices.Length - 2),
2,
WriteOptions.Unsafe
);
cmdbuf.EndCopyPass();
fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFences(fence);
GraphicsDevice.ReleaseFence(fence);
GraphicsDevice.DownloadFromBuffer(
vertexBuffer,
transferBuffer,
TransferOptions.Unsafe
);
// Read the updated buffer
transferBuffer.GetData<PositionVertex>(readbackVertices);
Logger.LogInfo("=== Change last two vertices ===");
for (int i = 0; i < readbackVertices.Length; i += 1)
{
Logger.LogInfo(readbackVertices[i].ToString());
}
}
protected override void Update(System.TimeSpan delta) { }
protected override void Draw(double alpha)
{
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture? swapchainTexture = cmdbuf.AcquireSwapchainTexture(MainWindow);
if (swapchainTexture != null)
{
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(swapchainTexture, WriteOptions.Cycle, Color.Black));
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
public static void Main(string[] args)
{
GetBufferDataGame game = new GetBufferDataGame();
game.Run();
}
}
}

View File

@ -36,6 +36,7 @@
<Compile Include="Examples\CullFaceExample.cs" /> <Compile Include="Examples\CullFaceExample.cs" />
<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" />
</ItemGroup> </ItemGroup>
<Import Project=".\CopyMoonlibs.targets" /> <Import Project=".\CopyMoonlibs.targets" />

View File

@ -19,7 +19,8 @@ class Program : Game
new CubeExample(), new CubeExample(),
new CullFaceExample(), new CullFaceExample(),
new DepthMSAAExample(), new DepthMSAAExample(),
new DrawIndirectExample() new DrawIndirectExample(),
new GetBufferDataExample()
]; ];
int ExampleIndex = 0; int ExampleIndex = 0;