Here's the refactored UploadSOA function that splits vertex and index buffers into separate resources, allowing proper resource state transitions:
u32 UploadSOA(const MeshDescription& desc) {
std::lock_guard lock(m_mutex);
// 1. Calculate sizes for separate buffers
u64 vertexDataSize = 0;
for (u32 i = 0; i < desc.count; ++i) {
vertexDataSize += desc.attribute[i].size;
}
if (vertexDataSize == 0 && desc.indices.size == 0) {
return UINT32_MAX; // Invalid handle
}
// --- Create Resources ---
GPUMeshHandle handle;
ComPtr<ID3D12Resource> vertexUploadBuffer;
ComPtr<ID3D12Resource> indexUploadBuffer;
D3D12MA::Allocation* vertexUploadAllocation = nullptr;
D3D12MA::Allocation* indexUploadAllocation = nullptr;
// 2. Create vertex buffer resources (if we have vertex data)
if (vertexDataSize > 0) {
// Create destination vertex buffer in DEFAULT heap
D3D12MA::ALLOCATION_DESC vertexDestAllocDesc = {};
vertexDestAllocDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
D3D12_RESOURCE_DESC vertexDestResDesc = CD3DX12_RESOURCE_DESC::Buffer(
vertexDataSize, D3D12_RESOURCE_FLAG_NONE);
HRESULT hr = m_allocator->CreateResource(
&vertexDestAllocDesc, &vertexDestResDesc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
&handle.vertexAllocation, IID_PPV_ARGS(&handle.vertexBuffer));
if (FAILED(hr)) return UINT32_MAX;
handle.vertexBuffer->SetName(L"Mesh VertexBuffer");
// Create vertex staging buffer in UPLOAD heap
D3D12MA::ALLOCATION_DESC vertexUploadAllocDesc = {};
vertexUploadAllocDesc.HeapType = D3D12_HEAP_TYPE_UPLOAD;
D3D12_RESOURCE_DESC vertexUploadResDesc = CD3DX12_RESOURCE_DESC::Buffer(vertexDataSize);
hr = m_allocator->CreateResource(
&vertexUploadAllocDesc, &vertexUploadResDesc,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
&vertexUploadAllocation, IID_PPV_ARGS(&vertexUploadBuffer));
if (FAILED(hr)) return UINT32_MAX;
vertexUploadBuffer->SetName(L"Mesh VertexUploadBuffer");
}
// 3. Create index buffer resources (if we have index data)
if (desc.indices.size > 0) {
// Create destination index buffer in DEFAULT heap
D3D12MA::ALLOCATION_DESC indexDestAllocDesc = {};
indexDestAllocDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
D3D12_RESOURCE_DESC indexDestResDesc = CD3DX12_RESOURCE_DESC::Buffer(
desc.indices.size, D3D12_RESOURCE_FLAG_NONE);
HRESULT hr = m_allocator->CreateResource(
&indexDestAllocDesc, &indexDestResDesc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
&handle.indexAllocation, IID_PPV_ARGS(&handle.indexBuffer));
if (FAILED(hr)) return UINT32_MAX;
handle.indexBuffer->SetName(L"Mesh IndexBuffer");
// Create index staging buffer in UPLOAD heap
D3D12MA::ALLOCATION_DESC indexUploadAllocDesc = {};
indexUploadAllocDesc.HeapType = D3D12_HEAP_TYPE_UPLOAD;
D3D12_RESOURCE_DESC indexUploadResDesc = CD3DX12_RESOURCE_DESC::Buffer(desc.indices.size);
hr = m_allocator->CreateResource(
&indexUploadAllocDesc, &indexUploadResDesc,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
&indexUploadAllocation, IID_PPV_ARGS(&indexUploadBuffer));
if (FAILED(hr)) return UINT32_MAX;
indexUploadBuffer->SetName(L"Mesh IndexUploadBuffer");
}
// 4. Map and copy vertex data
if (vertexDataSize > 0) {
UINT8* pVertexData;
HRESULT hr = vertexUploadBuffer->Map(0, nullptr, reinterpret_cast<void**>(&pVertexData));
if (FAILED(hr)) return UINT32_MAX;
u64 currentOffset = 0;
for (u32 i = 0; i < desc.count; ++i) {
memcpy(pVertexData + currentOffset, desc.attribute[i].data, desc.attribute[i].size);
currentOffset += desc.attribute[i].size;
}
vertexUploadBuffer->Unmap(0, nullptr);
}
// 5. Map and copy index data
if (desc.indices.size > 0) {
UINT8* pIndexData;
HRESULT hr = indexUploadBuffer->Map(0, nullptr, reinterpret_cast<void**>(&pIndexData));
if (FAILED(hr)) return UINT32_MAX;
memcpy(pIndexData, desc.indices.data, desc.indices.size);
indexUploadBuffer->Unmap(0, nullptr);
}
// 6. Record copy commands and resource barriers
m_commandAllocator->Reset();
m_commandList->Reset(m_commandAllocator.Get(), nullptr);
// Copy vertex buffer and transition to vertex buffer state
if (vertexDataSize > 0) {
m_commandList->CopyResource(handle.vertexBuffer.Get(), vertexUploadBuffer.Get());
D3D12_RESOURCE_BARRIER vertexBarrier = CD3DX12_RESOURCE_BARRIER::Transition(
handle.vertexBuffer.Get(),
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
m_commandList->ResourceBarrier(1, &vertexBarrier);
}
// Copy index buffer and transition to index buffer state
if (desc.indices.size > 0) {
m_commandList->CopyResource(handle.indexBuffer.Get(), indexUploadBuffer.Get());
D3D12_RESOURCE_BARRIER indexBarrier = CD3DX12_RESOURCE_BARRIER::Transition(
handle.indexBuffer.Get(),
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_INDEX_BUFFER);
m_commandList->ResourceBarrier(1, &indexBarrier);
}
m_commandList->Close();
// 7. Execute commands and signal fence
ID3D12CommandList* ppCommandLists[] = {m_commandList.Get()};
m_copyQueue->ExecuteCommandLists(1, ppCommandLists);
const u64 fencePoint = m_fenceValue++;
m_copyQueue->Signal(m_copyFence.Get(), fencePoint);
handle.fenceValue = fencePoint;
// 8. Queue upload resources for deletion
if (vertexUploadBuffer) {
m_inFlightResources.push({vertexUploadBuffer, vertexUploadAllocation, fencePoint});
}
if (indexUploadBuffer) {
m_inFlightResources.push({indexUploadBuffer, indexUploadAllocation, fencePoint});
}
// 9. Set up vertex buffer views
if (vertexDataSize > 0) {
u64 currentOffset = 0;
for (u32 i = 0; i < desc.count; i++) {
D3D12_VERTEX_BUFFER_VIEW vbView;
vbView.BufferLocation = handle.vertexBuffer->GetGPUVirtualAddress() + currentOffset;
vbView.SizeInBytes = desc.attribute[i].size;
vbView.StrideInBytes = desc.attribute[i].stride;
handle.bufferViews.push_back(vbView);
currentOffset += desc.attribute[i].size;
}
}
// 10. Set up index buffer view
if (desc.indices.size > 0) {
handle.indexBufferView.BufferLocation = handle.indexBuffer->GetGPUVirtualAddress();
handle.indexBufferView.SizeInBytes = desc.indices.size;
handle.indexBufferView.Format = (desc.indices.type == rType::U16) ?
DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;
u32 stride = (desc.indices.type == rType::U16) ? sizeof(u16) : sizeof(u32);
handle.indicesCount = desc.indices.size / stride;
}
// 11. Store handle and return ID
const u32 newHandleId = m_handleCount++;
m_handles[newHandleId] = std::move(handle);
return newHandleId;
}
Updated GPUMeshHandle Structure
You'll also need to update your GPUMeshHandle structure to accommodate separate buffers:
struct GPUMeshHandle {
// Vertex buffer resources
ComPtr<ID3D12Resource> vertexBuffer;
D3D12MA::Allocation* vertexAllocation = nullptr;
std::vector<D3D12_VERTEX_BUFFER_VIEW> bufferViews;
// Index buffer resources
ComPtr<ID3D12Resource> indexBuffer;
D3D12MA::Allocation* indexAllocation = nullptr;
D3D12_INDEX_BUFFER_VIEW indexBufferView = {};
u32 indicesCount = 0;
// Synchronization
u64 fenceValue = 0;
// Destructor to clean up allocations
~GPUMeshHandle() {
if (vertexAllocation) {
vertexAllocation->Release();
vertexAllocation = nullptr;
}
if (indexAllocation) {
indexAllocation->Release();
indexAllocation = nullptr;
}
}
};
Key Improvements
-
Separate Resources: Vertex and index data are now in separate D3D12 resources, allowing proper state transitions.
-
Correct Resource States:
- Vertex buffers transition to
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER
- Index buffers transition to
D3D12_RESOURCE_STATE_INDEX_BUFFER
-
Error Handling: Added HRESULT checks for resource creation and mapping operations.
-
Resource Management: Each buffer type has its own allocation that's properly tracked and released.
-
Renderer-Ready: After upload completes, buffers are immediately usable by the renderer without additional barriers.
Usage in Renderer
Now when you bind these resources in your renderer, they're already in the correct states:
// Bind vertex buffers (already in VERTEX_AND_CONSTANT_BUFFER state)
commandList->IASetVertexBuffers(0, handle.bufferViews.size(), handle.bufferViews.data());
// Bind index buffer (already in INDEX_BUFFER state)
if (handle.indexBuffer) {
commandList->IASetIndexBuffer(&handle.indexBufferView);
}
This approach eliminates the resource barrier issues and provides cleaner separation of concerns between vertex and index data.