2024-02-23 21:04:59 +00:00
|
|
|
|
using MoonWorks.Graphics;
|
2022-11-12 03:51:31 +00:00
|
|
|
|
using MoonWorks.Math.Float;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Test
|
|
|
|
|
{
|
2024-02-07 15:27:55 +00:00
|
|
|
|
class GetBufferDataGame : Game
|
|
|
|
|
{
|
2024-03-07 18:35:12 +00:00
|
|
|
|
public GetBufferDataGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
|
2024-02-07 15:27:55 +00:00
|
|
|
|
{
|
2024-02-23 21:04:59 +00:00
|
|
|
|
var vertices = new System.Span<PositionVertex>(
|
|
|
|
|
[
|
|
|
|
|
new PositionVertex(new Vector3(0, 0, 0)),
|
2024-02-07 15:27:55 +00:00
|
|
|
|
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)),
|
2024-02-23 21:04:59 +00:00
|
|
|
|
]);
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-23 21:04:59 +00:00
|
|
|
|
var otherVerts = new System.Span<PositionVertex>(
|
|
|
|
|
[
|
|
|
|
|
new PositionVertex(new Vector3(1, 2, 3)),
|
2024-02-07 15:27:55 +00:00
|
|
|
|
new PositionVertex(new Vector3(4, 5, 6)),
|
|
|
|
|
new PositionVertex(new Vector3(7, 8, 9))
|
2024-02-23 21:04:59 +00:00
|
|
|
|
]);
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
int vertexSize = Marshal.SizeOf<PositionVertex>();
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
2024-02-23 21:04:59 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
var vertexBuffer = resourceUploader.CreateBuffer(vertices, BufferUsageFlags.Vertex);
|
2024-02-23 21:04:59 +00:00
|
|
|
|
|
2024-03-06 08:15:44 +00:00
|
|
|
|
// Wait for the vertices to finish copying...
|
|
|
|
|
resourceUploader.UploadAndWait();
|
2024-02-23 22:32:56 +00:00
|
|
|
|
resourceUploader.Dispose();
|
2024-02-23 21:04:59 +00:00
|
|
|
|
|
|
|
|
|
var transferBuffer = new TransferBuffer(GraphicsDevice, vertexBuffer.Size);
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Read back and print out the vertex values
|
2024-03-06 08:15:44 +00:00
|
|
|
|
GraphicsDevice.DownloadFromBuffer(
|
|
|
|
|
vertexBuffer,
|
|
|
|
|
transferBuffer,
|
|
|
|
|
TransferOptions.Overwrite
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
PositionVertex[] readbackVertices = new PositionVertex[vertices.Length];
|
2024-02-23 21:04:59 +00:00
|
|
|
|
transferBuffer.GetData<PositionVertex>(readbackVertices);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
for (int i = 0; i < readbackVertices.Length; i += 1)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogInfo(readbackVertices[i].ToString());
|
|
|
|
|
}
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-23 21:04:59 +00:00
|
|
|
|
// Change the first three vertices and upload
|
2024-03-01 23:03:29 +00:00
|
|
|
|
transferBuffer.SetData(otherVerts, TransferOptions.Overwrite);
|
2024-03-06 08:15:44 +00:00
|
|
|
|
|
|
|
|
|
var cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
2024-02-23 21:04:59 +00:00
|
|
|
|
cmdbuf.BeginCopyPass();
|
2024-03-01 23:03:29 +00:00
|
|
|
|
cmdbuf.UploadToBuffer(transferBuffer, vertexBuffer, WriteOptions.SafeOverwrite);
|
2024-02-23 21:04:59 +00:00
|
|
|
|
cmdbuf.EndCopyPass();
|
2024-03-06 08:15:44 +00:00
|
|
|
|
var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
|
2024-02-23 21:04:59 +00:00
|
|
|
|
GraphicsDevice.WaitForFences(fence);
|
|
|
|
|
GraphicsDevice.ReleaseFence(fence);
|
|
|
|
|
|
|
|
|
|
// Download the data
|
2024-03-06 08:15:44 +00:00
|
|
|
|
GraphicsDevice.DownloadFromBuffer(
|
|
|
|
|
vertexBuffer,
|
|
|
|
|
transferBuffer,
|
|
|
|
|
TransferOptions.Overwrite
|
|
|
|
|
);
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Read the updated buffer
|
2024-02-23 21:04:59 +00:00
|
|
|
|
transferBuffer.GetData<PositionVertex>(readbackVertices);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
Logger.LogInfo("=== Change first three vertices ===");
|
|
|
|
|
for (int i = 0; i < readbackVertices.Length; i += 1)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogInfo(readbackVertices[i].ToString());
|
|
|
|
|
}
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-23 21:04:59 +00:00
|
|
|
|
// Change the last two vertices and upload
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
2024-02-23 21:04:59 +00:00
|
|
|
|
var lastTwoSpan = otherVerts.Slice(1, 2);
|
2024-03-01 23:03:29 +00:00
|
|
|
|
transferBuffer.SetData(lastTwoSpan, TransferOptions.Overwrite);
|
2024-02-23 21:04:59 +00:00
|
|
|
|
cmdbuf.BeginCopyPass();
|
2024-02-23 23:40:18 +00:00
|
|
|
|
cmdbuf.UploadToBuffer<PositionVertex>(
|
2024-02-23 21:04:59 +00:00
|
|
|
|
transferBuffer,
|
2024-02-07 15:27:55 +00:00
|
|
|
|
vertexBuffer,
|
2024-02-23 23:40:18 +00:00
|
|
|
|
0,
|
|
|
|
|
(uint)(vertices.Length - 2),
|
2024-03-01 23:03:29 +00:00
|
|
|
|
2,
|
|
|
|
|
WriteOptions.SafeOverwrite
|
2024-02-23 23:40:18 +00:00
|
|
|
|
);
|
2024-02-23 21:04:59 +00:00
|
|
|
|
cmdbuf.EndCopyPass();
|
|
|
|
|
fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
|
|
|
|
|
GraphicsDevice.WaitForFences(fence);
|
|
|
|
|
GraphicsDevice.ReleaseFence(fence);
|
|
|
|
|
|
2024-03-06 08:15:44 +00:00
|
|
|
|
GraphicsDevice.DownloadFromBuffer(
|
|
|
|
|
vertexBuffer,
|
|
|
|
|
transferBuffer,
|
|
|
|
|
TransferOptions.Overwrite
|
|
|
|
|
);
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Read the updated buffer
|
2024-02-23 21:04:59 +00:00
|
|
|
|
transferBuffer.GetData<PositionVertex>(readbackVertices);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
Logger.LogInfo("=== Change last two vertices ===");
|
|
|
|
|
for (int i = 0; i < readbackVertices.Length; i += 1)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogInfo(readbackVertices[i].ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
protected override void Update(System.TimeSpan delta) { }
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
protected override void Draw(double alpha)
|
|
|
|
|
{
|
|
|
|
|
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
|
|
|
|
Texture? swapchainTexture = cmdbuf.AcquireSwapchainTexture(MainWindow);
|
|
|
|
|
if (swapchainTexture != null)
|
|
|
|
|
{
|
2024-03-01 23:03:29 +00:00
|
|
|
|
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(swapchainTexture, WriteOptions.SafeDiscard, Color.Black));
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
}
|
|
|
|
|
GraphicsDevice.Submit(cmdbuf);
|
|
|
|
|
}
|
2022-11-12 03:51:31 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
GetBufferDataGame game = new GetBufferDataGame();
|
|
|
|
|
game.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-12 03:51:31 +00:00
|
|
|
|
}
|