To correctly split vertex and index buffers into separate resources and ensure proper synchronization barriers, we need to modify the resource creation, copying, and barrier transitions. Here's the corrected approach:
cpp Copy code
struct GPUMeshHandle {
ComPtr<ID3D12Resource> vertexBuffer;
ComPtr<ID3D12Resource> indexBuffer; // Optional
D3D12MA::Allocation* vertexAllocation;
D3D12MA::Allocation* indexAllocation;
std::vector<D3D12_VERTEX_BUFFER_VIEW> vertexBufferViews;
D3D12_INDEX_BUFFER_VIEW indexBufferView;
u32 indicesCount;
u64 fenceValue;
};
u32 UploadSOA(const MeshDescription& desc) {
std::lock_guard lock(m_mutex);
// Calculate sizes
u64 vertexDataSize = 0;
for (u32 i = 0; i < desc.count; ++i) {
vertexDataSize += desc.attribute[i].size;
}
const u64 indexDataSize = desc.indices.size;
const bool hasIndices = indexDataSize > 0;
// Create vertex buffer
GPUMeshHandle handle;
D3D12_RESOURCE_DESC vertexResDesc = CD3DX12_RESOURCE_DESC::Buffer(vertexDataSize);
D3D12MA::ALLOCATION_DESC vertexAllocDesc = {};
vertexAllocDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
m_allocator->CreateResource(
&vertexAllocDesc, &vertexResDesc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
&handle.vertexAllocation, IID_PPV_ARGS(&handle.vertexBuffer));
handle.vertexBuffer->SetName(L"Vertex Buffer");
// Create index buffer if needed
ComPtr<ID3D12Resource> indexBuffer;
D3D12MA::Allocation* indexAllocation = nullptr;
if (hasIndices) {
D3D12_RESOURCE_DESC indexResDesc = CD3DX12_RESOURCE_DESC::Buffer(indexDataSize);
D3D12MA::ALLOCATION_DESC indexAllocDesc = {};
indexAllocDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
m_allocator->CreateResource(
&indexAllocDesc, &indexResDesc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
&indexAllocation, IID_PPV_ARGS(&indexBuffer));
indexBuffer->SetName(L"Index Buffer");
handle.indexBuffer = indexBuffer;
handle.indexAllocation = indexAllocation;
}
// Create staging buffers
ComPtr<ID3D12Resource> vertexUploadBuffer;
D3D12MA::Allocation* vertexUploadAlloc = nullptr;
{
D3D12_RESOURCE_DESC uploadResDesc = CD3DX12_RESOURCE_DESC::Buffer(vertexDataSize);
D3D12MA::ALLOCATION_DESC uploadAllocDesc = {};
uploadAllocDesc.HeapType = D3D12_HEAP_TYPE_UPLOAD;
m_allocator->CreateResource(
&uploadAllocDesc, &uploadResDesc,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
&vertexUploadAlloc, IID_PPV_ARGS(&vertexUploadBuffer));
vertexUploadBuffer->SetName(L"Vertex Upload Buffer");
}
ComPtr<ID3D12Resource> indexUploadBuffer;
D3D12MA::Allocation* indexUploadAlloc = nullptr;
if (hasIndices) {
D3D12_RESOURCE_DESC uploadResDesc = CD3DX12_RESOURCE_DESC::Buffer(indexDataSize);
D3D12MA::ALLOCATION_DESC uploadAllocDesc = {};
uploadAllocDesc.HeapType = D3D12_HEAP_TYPE_UPLOAD;
m_allocator->CreateResource(
&uploadAllocDesc, &uploadResDesc,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
&indexUploadAlloc, IID_PPV_ARGS(&indexUploadBuffer));
indexUploadBuffer->SetName(L"Index Upload Buffer");
}
// Copy vertex data
{
UINT8* pData;
vertexUploadBuffer->Map(0, nullptr, reinterpret_cast<void**>(&pData));
u64 currentOffset = 0;
for (u32 i = 0; i < desc.count; ++i) {
memcpy(pData + currentOffset, desc.attribute[i].data, desc.attribute[i].size);
currentOffset += desc.attribute[i].size;
}
vertexUploadBuffer->Unmap(0, nullptr);
}
// Copy index data if needed
if (hasIndices) {
UINT8* pData;
indexUploadBuffer->Map(0, nullptr, reinterpret_cast<void**>(&pData));
memcpy(pData, desc.indices.data, indexDataSize);
indexUploadBuffer->Unmap(0, nullptr);
}
// Record commands
m_commandAllocator->Reset();
m_commandList->Reset(m_commandAllocator.Get(), nullptr);
// Copy vertex data
m_commandList->CopyResource(handle.vertexBuffer.Get(), vertexUploadBuffer.Get());
// Copy index data if needed
if (hasIndices) {
m_commandList->CopyResource(indexBuffer.Get(), indexUploadBuffer.Get());
}
// Transition vertex buffer
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);
// Transition index buffer if needed
if (hasIndices) {
D3D12_RESOURCE_BARRIER indexBarrier = CD3DX12_RESOURCE_BARRIER::Transition(
indexBuffer.Get(),
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_INDEX_BUFFER);
m_commandList->ResourceBarrier(1, &indexBarrier);
}
m_commandList->Close();
// Execute commands
ID3D12CommandList* ppCommandLists[] = {m_commandList.Get()};
m_copyQueue->ExecuteCommandLists(1, ppCommandLists);
// Signal fence
const u64 fencePoint = m_fenceValue++;
m_copyQueue->Signal(m_copyFence.Get(), fencePoint);
handle.fenceValue = fencePoint;
// Queue staging resources for deletion
m_inFlightResources.push({vertexUploadBuffer, vertexUploadAlloc, fencePoint});
if (hasIndices) {
m_inFlightResources.push({indexUploadBuffer, indexUploadAlloc, fencePoint});
}
// Setup vertex buffer views
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.vertexBufferViews.push_back(vbView);
currentOffset += desc.attribute[i].size;
}
// Setup index buffer view if needed
if (hasIndices) {
handle.indexBufferView.BufferLocation = indexBuffer->GetGPUVirtualAddress();
handle.indexBufferView.SizeInBytes = indexDataSize;
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 = static_cast<u32>(indexDataSize / stride);
}
// Store handle
const u32 newHandleId = m_handleCount++;
m_handles[newHandleId] = std::move(handle);
return newHandleId;
}
This implementation ensures proper synchronization while allowing the renderer to use resources immediately after upload without additional state transitions.