Dx12 Vertex Buffer issue #490
Answered
by
amerkoleci
Gavin-Williams
asked this question in
Q&A
-
I'm trying to upload some vertex data, and when I call Map (last block of code here) I get a 0x0 pointer, // initialize triangle vertex data
Span<Vertex> vertexData =
[
new() {
Position = new Vector3(0.0f, 0.25f, 0.0f),
Color = new Vector4(1.0f, 0.0f, 0.0f, 1.0f) },
new() {
Position = new Vector3(0.25f, -0.25f, 0.0f),
Color = new Vector4(0.0f, 1.0f, 0.0f, 1.0f) },
new() {
Position = new Vector3(-0.25f, -0.25f, 0.0f),
Color = new Vector4(0.0f, 0.0f, 1.0f, 1.0f) }
];
VertexCount = (uint)vertexData.Length;
// create vertex buffer in committed resource
// - a committed resource is a heap dedicated to a single resource
uint sizeInBytes = (uint)(sizeof(Vertex) * vertexData.Length);
ResourceDescription bufferDesc = ResourceDescription.Buffer(sizeInBytes);
if (Gpu.Device.CreateCommittedResource(
new HeapProperties(HeapType.Default),
HeapFlags.None,
bufferDesc,
ResourceStates.CopyDest,
null,
out VertexBuffer).Failure)
throw new Exception();
// create upload buffer in committed resource
if (Gpu.Device.CreateCommittedResource(
new HeapProperties(HeapType.Upload),
HeapFlags.None,
bufferDesc,
ResourceStates.GenericRead,
null,
out ID3D12Resource uploadBuffer).Failure)
throw new Exception();
// copy vertex data to mapped memory / upload buffer
// mapping creates an address in the PCI bus that the CPU can write to that
// will be copied to the GPU buffer.
void* busAddress = null;
uploadBuffer.Map(0, busAddress); // busAddress is 0x0 after here
fixed (Vertex* dataPtr = vertexData)
NativeMemory.Copy(dataPtr, busAddress, (uint)sizeInBytes);
uploadBuffer.Unmap(0); |
Beta Was this translation helpful? Give feedback.
Answered by
amerkoleci
Mar 3, 2025
Replies: 1 comment 1 reply
-
Hi, Otherwise use helper methods I've added https://github.com/amerkoleci/Vortice.Windows/blob/main/src/Vortice.Direct3D12/ID3D12Resource.cs#L45 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Gavin-Williams
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
You need to take address of the busAddress:
uploadBuffer.Map(0, &busAddress);
Otherwise use helper methods I've added
https://github.com/amerkoleci/Vortice.Windows/blob/main/src/Vortice.Direct3D12/ID3D12Resource.cs#L45