update GetBufferData

refresh2
cosmonaut 2024-02-23 13:04:59 -08:00
parent c878a26b74
commit c2f1a02ba3
1 changed files with 56 additions and 30 deletions

View File

@ -1,5 +1,4 @@
using MoonWorks; using MoonWorks.Graphics;
using MoonWorks.Graphics;
using MoonWorks.Math.Float; using MoonWorks.Math.Float;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -9,9 +8,9 @@ namespace MoonWorks.Test
{ {
public GetBufferDataGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true) public GetBufferDataGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true)
{ {
PositionVertex[] vertices = new PositionVertex[] var vertices = new System.Span<PositionVertex>(
{ [
new PositionVertex(new Vector3(0, 0, 0)), new PositionVertex(new Vector3(0, 0, 0)),
new PositionVertex(new Vector3(0, 0, 1)), new PositionVertex(new Vector3(0, 0, 1)),
new PositionVertex(new Vector3(0, 1, 0)), new PositionVertex(new Vector3(0, 1, 0)),
new PositionVertex(new Vector3(0, 1, 1)), new PositionVertex(new Vector3(0, 1, 1)),
@ -19,69 +18,97 @@ namespace MoonWorks.Test
new PositionVertex(new Vector3(1, 0, 1)), new PositionVertex(new Vector3(1, 0, 1)),
new PositionVertex(new Vector3(1, 1, 0)), new PositionVertex(new Vector3(1, 1, 0)),
new PositionVertex(new Vector3(1, 1, 1)), new PositionVertex(new Vector3(1, 1, 1)),
}; ]);
PositionVertex[] otherVerts = new PositionVertex[] var otherVerts = new System.Span<PositionVertex>(
{ [
new PositionVertex(new Vector3(1, 2, 3)), new PositionVertex(new Vector3(1, 2, 3)),
new PositionVertex(new Vector3(4, 5, 6)), new PositionVertex(new Vector3(4, 5, 6)),
new PositionVertex(new Vector3(7, 8, 9)) new PositionVertex(new Vector3(7, 8, 9))
}; ]);
int vertexSize = Marshal.SizeOf<PositionVertex>(); int vertexSize = Marshal.SizeOf<PositionVertex>();
Buffer vertexBuffer = Buffer.Create<PositionVertex>( var resourceInitializer = new ResourceInitializer(GraphicsDevice);
GraphicsDevice,
BufferUsageFlags.Vertex, var vertexBuffer = resourceInitializer.CreateBuffer(vertices, BufferUsageFlags.Vertex);
(uint) vertices.Length
); resourceInitializer.Upload();
resourceInitializer.Dispose();
var transferBuffer = new TransferBuffer(GraphicsDevice, vertexBuffer.Size);
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer(); CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
cmdbuf.SetBufferData(vertexBuffer, vertices);
cmdbuf.BeginCopyPass();
cmdbuf.DownloadFromBuffer(vertexBuffer, transferBuffer);
cmdbuf.EndCopyPass();
var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf); var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
// Wait for the vertices to finish uploading... // Wait for the vertices to finish copying...
GraphicsDevice.WaitForFences(fence); GraphicsDevice.WaitForFences(fence);
GraphicsDevice.ReleaseFence(fence); GraphicsDevice.ReleaseFence(fence);
// Read back and print out the vertex values // Read back and print out the vertex values
PositionVertex[] readbackVertices = new PositionVertex[vertices.Length]; PositionVertex[] readbackVertices = new PositionVertex[vertices.Length];
vertexBuffer.GetData(readbackVertices); transferBuffer.GetData<PositionVertex>(readbackVertices);
for (int i = 0; i < readbackVertices.Length; i += 1) for (int i = 0; i < readbackVertices.Length; i += 1)
{ {
Logger.LogInfo(readbackVertices[i].ToString()); Logger.LogInfo(readbackVertices[i].ToString());
} }
// Change the first three vertices // Change the first three vertices and upload
cmdbuf = GraphicsDevice.AcquireCommandBuffer(); cmdbuf = GraphicsDevice.AcquireCommandBuffer();
cmdbuf.SetBufferData(vertexBuffer, otherVerts); transferBuffer.SetData(otherVerts, SetDataOptions.Overwrite);
cmdbuf.BeginCopyPass();
cmdbuf.UploadToBuffer(transferBuffer, vertexBuffer);
cmdbuf.EndCopyPass();
fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFences(fence);
GraphicsDevice.ReleaseFence(fence);
// Download the data
cmdbuf = GraphicsDevice.AcquireCommandBuffer();
cmdbuf.BeginCopyPass();
cmdbuf.DownloadFromBuffer(vertexBuffer, transferBuffer);
cmdbuf.EndCopyPass();
fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf); fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFences(fence); GraphicsDevice.WaitForFences(fence);
GraphicsDevice.ReleaseFence(fence); GraphicsDevice.ReleaseFence(fence);
// Read the updated buffer // Read the updated buffer
vertexBuffer.GetData(readbackVertices); transferBuffer.GetData<PositionVertex>(readbackVertices);
Logger.LogInfo("=== Change first three vertices ==="); Logger.LogInfo("=== Change first three vertices ===");
for (int i = 0; i < readbackVertices.Length; i += 1) for (int i = 0; i < readbackVertices.Length; i += 1)
{ {
Logger.LogInfo(readbackVertices[i].ToString()); Logger.LogInfo(readbackVertices[i].ToString());
} }
// Change the last two vertices // Change the last two vertices and upload
cmdbuf = GraphicsDevice.AcquireCommandBuffer(); cmdbuf = GraphicsDevice.AcquireCommandBuffer();
cmdbuf.SetBufferData( var lastTwoSpan = otherVerts.Slice(1, 2);
transferBuffer.SetData(lastTwoSpan, SetDataOptions.Overwrite);
cmdbuf.BeginCopyPass();
cmdbuf.UploadToBuffer(
transferBuffer,
vertexBuffer, vertexBuffer,
otherVerts, new BufferCopy(0, (uint) (vertexSize * (vertices.Length - 2)), (uint) (vertexSize * 2)));
(uint) (vertexSize * (vertices.Length - 2)), cmdbuf.EndCopyPass();
1, fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
2 GraphicsDevice.WaitForFences(fence);
); GraphicsDevice.ReleaseFence(fence);
cmdbuf = GraphicsDevice.AcquireCommandBuffer();
cmdbuf.BeginCopyPass();
cmdbuf.DownloadFromBuffer(vertexBuffer, transferBuffer);
cmdbuf.EndCopyPass();
fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf); fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFences(fence); GraphicsDevice.WaitForFences(fence);
GraphicsDevice.ReleaseFence(fence); GraphicsDevice.ReleaseFence(fence);
// Read the updated buffer // Read the updated buffer
vertexBuffer.GetData(readbackVertices); transferBuffer.GetData<PositionVertex>(readbackVertices);
Logger.LogInfo("=== Change last two vertices ==="); Logger.LogInfo("=== Change last two vertices ===");
for (int i = 0; i < readbackVertices.Length; i += 1) for (int i = 0; i < readbackVertices.Length; i += 1)
{ {
@ -89,7 +116,6 @@ namespace MoonWorks.Test
} }
} }
protected override void Update(System.TimeSpan delta) { } protected override void Update(System.TimeSpan delta) { }
protected override void Draw(double alpha) protected override void Draw(double alpha)