crash at hr = mcopyAllocator->Reset(); 3D12 ERROR: ID3D12CommandAllocator::Reset: A…
- User
crash at hr = m_copyAllocator->Reset(); 3D12 ERROR: ID3D12CommandAllocator::Reset: A command allocator 0x0000024649B7BF20:'Unnamed ID3D12CommandAllocator Object' is being reset before previous executions associated with the allocator have completed. [ EXECUTION ERROR #552: COMMAND_ALLOCATOR_SYNC] D3D12: BREAK enabled for the previous message, which was: [ ERROR EXECUTION #552: COMMAND_ALLOCATOR_SYNC ]
Assistant
The crash is occurring because the command allocator is being reset before the GPU has finished executing the commands that were recorded with it. This is a common synchronization issue in DirectX 12. Here's the fixed version with proper synchronization:
Fixed MeshUploadManager
#pragma once #include "pch.hpp" // Common precompiled headers (d3d12.h, wrl.h, etc.) #include "D3D12MemAlloc.hpp" // Include the D3D12 Memory Allocator header #include <vector> #include <queue> #include <mutex> #include <atomic> #include <cassert> namespace Rtx { /** * @brief Aligns a size to a specified alignment. * @param size The size to align. * @param alignment The alignment requirement. * @return The aligned size. */ inline UINT64 Align(UINT64 size, UINT64 alignment) { return (size + alignment - 1) & ~(alignment - 1); } /** * @brief Gets the size in bytes of a given rType. */ inline size_t GetTypeSize(rType type) { switch (type) { case rType::F32: return sizeof(float); case rType::U32: return sizeof(u32); case rType::U16: return sizeof(u16); case rType::I32: return sizeof(i32); default: return 0; } } // --- Mesh Description Structures --- // (Unchanged from your original code, assuming these are defined elsewhere) // struct MeshDescription { ... }; // struct Attribute { rType type; void* data; size_t size; u32 elementCount; size_t stride; }; // struct Indices { rType type; void* data; size_t size; }; /** * @brief Handle to a GPU mesh resource. Contains all views and metadata needed for rendering. */ struct GPUMeshHandle { D3D12MA::Allocation* allocation = nullptr; // D3D12MA allocation object. Microsoft::WRL::ComPtr<ID3D12Resource> resource; // The GPU buffer (VBs and IB combined). std::vector<D3D12_VERTEX_BUFFER_VIEW> vertexBufferViews; D3D12_INDEX_BUFFER_VIEW indexBufferView; bool hasIndices = false; UINT64 fenceValue = 0; // Fence value signaled when the upload is complete. u32 id = UINT32_MAX; /** * @brief Checks if the GPU has finished the upload copy for this mesh. * @param fence The fence to check against. */ bool IsReady(ID3D12Fence* fence) const { assert(fence != nullptr && "Fence must not be null"); return (fenceValue == 0 || fence->GetCompletedValue() >= fenceValue); } bool IsValid() const { return id != UINT32_MAX && resource != nullptr; } }; /** * @brief Manages asynchronous uploading of mesh data from CPU to GPU default heap memory. * * This manager uses a single large upload buffer as a ring buffer for staging. * It batches copy commands and uses fences to track GPU progress, allowing the ring * buffer space to be safely reclaimed. It also defers the release of mesh resources * until they are no longer in use by the GPU. */ class MeshUploadManager { public: MeshUploadManager() = default; ~MeshUploadManager() { Shutdown(); } /** * @brief Initializes the manager. * @param allocator A pointer to an initialized D3D12MA::Allocator. * @param device A pointer to the ID3D12Device. * @param uploadBufferSize The size of the internal staging ring buffer in bytes. */ bool Initialize(D3D12MA::Allocator* allocator, ID3D12Device* device, size_t uploadBufferSize = 256 * 1024 * 1024) { std::lock_guard lock(m_mutex); if (!allocator || !device) { return false; } m_d3d12ma_allocator = allocator; m_device = device; // Create a dedicated copy queue D3D12_COMMAND_QUEUE_DESC queueDesc = {}; queueDesc.Type = D3D12_COMMAND_LIST_TYPE_COPY; queueDesc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL; queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; queueDesc.NodeMask = 0; HRESULT hr = m_device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&m_copyQueue)); if (FAILED(hr)) { return false; } // Create synchronization primitives hr = m_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_fence)); if (FAILED(hr)) { m_copyQueue.Reset(); return false; } m_fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); if (m_fenceEvent == nullptr) { m_fence.Reset(); m_copyQueue.Reset(); return false; } // Create dedicated copy command list and allocator hr = m_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COPY, IID_PPV_ARGS(&m_copyAllocator)); if (FAILED(hr)) { m_fence.Reset(); CloseHandle(m_fenceEvent); m_fenceEvent = nullptr; m_copyQueue.Reset(); return false; } hr = m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COPY, m_copyAllocator.Get(), nullptr, IID_PPV_ARGS(&m_copyCommandList)); if (FAILED(hr)) { m_copyAllocator.Reset(); m_fence.Reset(); CloseHandle(m_fenceEvent); m_fenceEvent = nullptr; m_copyQueue.Reset(); return false; } m_copyCommandList->Close(); // Create and map the upload ring buffer m_uploadBufferSize = uploadBufferSize; if (!InitializeUploadBuffer()) { m_copyCommandList.Reset(); m_copyAllocator.Reset(); m_fence.Reset(); CloseHandle(m_fenceEvent); m_fenceEvent = nullptr; m_copyQueue.Reset(); return false; } return true; } /** * @brief Shuts down the manager, flushes all pending work, and releases resources. */ void Shutdown() { std::lock_guard lock(m_mutex); Flush(); // Wait for all uploads to complete. if (m_uploadBuffer) { m_uploadBuffer->Unmap(0, nullptr); m_uploadCpuPtr = nullptr; m_uploadBuffer.Reset(); } m_copyCommandList.Reset(); m_copyAllocator.Reset(); m_fence.Reset(); CloseHandle(m_fenceEvent); m_fenceEvent = nullptr; m_copyQueue.Reset(); // Clean up any remaining mesh handles and deferred releases for (auto& handle : m_meshHandles) { if (handle.IsValid()) { handle.resource.Reset(); handle.allocation->Release(); } } m_meshHandles.clear(); while (!m_deferredReleaseQueue.empty()) { m_deferredReleaseQueue.front().allocation->Release(); m_deferredReleaseQueue.pop(); } m_d3d12ma_allocator = nullptr; m_device.Reset(); } /** * @brief Enqueues a mesh for upload to the GPU. * This is thread-safe. It records copy commands but does not submit them. * @param desc The description of the mesh data. * @return A unique ID for the mesh. Returns UINT32_MAX on failure. */ u32 UploadMesh(const MeshDescription& desc) { std::lock_guard lock(m_mutex); if (!m_copyQueue || !m_fence) { return UINT32_MAX; } if (!m_hasPendingCopies) { // Wait for the previous command list to finish before resetting WaitForFenceValue(m_lastSubmittedFenceValue); // This is the first upload in a new batch. Reset the command list. HRESULT hr = m_copyAllocator->Reset(); if (FAILED(hr)) return UINT32_MAX; hr = m_copyCommandList->Reset(m_copyAllocator.Get(), nullptr); if (FAILED(hr)) return UINT32_MAX; } // 1. Calculate total size based on the MeshDescription size_t totalVertexDataSize = 0; size_t totalIndexDataSize = desc.indices.size; if (desc.interleaved) { if (desc.interleavedVertexCount == 0 || desc.count == 0) return UINT32_MAX; // For interleaved data, the stride is the sum of the sizes of all attributes. size_t vertexStride = 0; for (u32 i = 0; i < desc.count; ++i) { vertexStride += GetTypeSize(desc.attribute[i].type) * desc.attribute[i].elementCount; } totalVertexDataSize = vertexStride * desc.interleavedVertexCount; } else // SoA (Structure of Arrays) { // For SoA, total size is the sum of individual attribute buffer sizes. for (u32 i = 0; i < desc.count; ++i) { totalVertexDataSize += desc.attribute[i].size; } } const size_t totalRequiredSize = totalVertexDataSize + totalIndexDataSize; if (totalRequiredSize == 0 || totalRequiredSize > m_uploadBufferSize) { return UINT32_MAX; } // 2. Allocate space in the ring buffer size_t allocationOffset = FindSpaceInRingBuffer(totalRequiredSize); if (allocationOffset == (size_t)-1) { return UINT32_MAX; } // 3. Create destination GPU resource GPUMeshHandle newHandle; D3D12MA::ALLOCATION_DESC allocDesc = {}; allocDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; CD3DX12_RESOURCE_DESC bufferDesc = CD3DX12_RESOURCE_DESC::Buffer(totalRequiredSize); HRESULT hr = m_d3d12ma_allocator->CreateResource(&allocDesc, &bufferDesc, D3D12_RESOURCE_STATE_COMMON, nullptr, &newHandle.allocation, IID_PPV_ARGS(&newHandle.resource)); if (FAILED(hr)) return UINT32_MAX; // 4. Copy data and record commands uint8_t* uploadDst = m_uploadCpuPtr + allocationOffset; size_t currentCpuOffset = 0; size_t currentGpuOffset = 0; // --- 4a. Vertex Data --- if (desc.interleaved) { // Assumes the full interleaved data block is pointed to by the first attribute. memcpy(uploadDst, desc.attribute[0].data, totalVertexDataSize); m_copyCommandList->CopyBufferRegion(newHandle.resource.Get(), 0, m_uploadBuffer.Get(), allocationOffset, totalVertexDataSize); size_t vertexStride = 0; for (u32 i = 0; i < desc.count; ++i) vertexStride += GetTypeSize(desc.attribute[i].type) * desc.attribute[i].elementCount; D3D12_VERTEX_BUFFER_VIEW vbv = {}; vbv.BufferLocation = newHandle.resource->GetGPUVirtualAddress(); vbv.SizeInBytes = static_cast<UINT>(totalVertexDataSize); vbv.StrideInBytes = static_cast<UINT>(vertexStride); newHandle.vertexBufferViews.push_back(vbv); } else // SoA { for (u32 i = 0; i < desc.count; ++i) { const auto& attr = desc.attribute[i]; if (attr.size > 0 && attr.data != nullptr) { memcpy(uploadDst + currentCpuOffset, attr.data, attr.size); m_copyCommandList->CopyBufferRegion(newHandle.resource.Get(), currentGpuOffset, m_uploadBuffer.Get(), allocationOffset + currentCpuOffset, attr.size); D3D12_VERTEX_BUFFER_VIEW vbv = {}; vbv.BufferLocation = newHandle.resource->GetGPUVirtualAddress() + currentGpuOffset; vbv.SizeInBytes = static_cast<UINT>(attr.size); // Use the provided stride if available, otherwise calculate it. vbv.StrideInBytes = (attr.stride > 0) ? static_cast<UINT>(attr.stride) : static_cast<UINT>(GetTypeSize(attr.type) * attr.elementCount); newHandle.vertexBufferViews.push_back(vbv); currentCpuOffset += attr.size; currentGpuOffset += attr.size; } } } currentCpuOffset = totalVertexDataSize; currentGpuOffset = totalVertexDataSize; // --- 4b. Index Data --- if (desc.indices.size > 0 && desc.indices.data != nullptr) { newHandle.hasIndices = true; memcpy(uploadDst + currentCpuOffset, desc.indices.data, desc.indices.size); m_copyCommandList->CopyBufferRegion(newHandle.resource.Get(), currentGpuOffset, m_uploadBuffer.Get(), allocationOffset + currentCpuOffset, desc.indices.size); newHandle.indexBufferView.BufferLocation = newHandle.resource->GetGPUVirtualAddress() + currentGpuOffset; newHandle.indexBufferView.SizeInBytes = static_cast<UINT>(desc.indices.size); newHandle.indexBufferView.Format = (desc.indices.type == rType::U16) ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT; } m_hasPendingCopies = true; // 5. Store handle and return ID newHandle.fenceValue = m_fenceValue; u32 id = GetNewHandleID(); newHandle.id = id; m_meshHandles[id] = std::move(newHandle); m_inFlightUploadRanges.push({ allocationOffset, totalRequiredSize, m_fenceValue }); m_currentUploadOffset = allocationOffset + totalRequiredSize; return id; } /** * @brief Submits all pending copy commands to the GPU. * Should be called once per frame, before executing the main rendering command list. */ void SubmitUploads() { std::lock_guard lock(m_mutex); if (!m_hasPendingCopies) return; if (!m_copyQueue || !m_fence) { return; } HRESULT hr = m_copyCommandList->Close(); if (FAILED(hr)) { return; } ID3D12CommandList* lists[] = { m_copyCommandList.Get() }; m_copyQueue->ExecuteCommandLists(1, lists); hr = m_copyQueue->Signal(m_fence.Get(), m_fenceValue); if (FAILED(hr)) { return; } // Store the fence value for this submission m_lastSubmittedFenceValue = m_fenceValue; // Increment fence for the next submission batch m_fenceValue++; m_hasPendingCopies = false; } /** * @brief Releases a mesh by its ID, queueing its GPU resources for safe deletion. * @param id The ID of the mesh to release. */ void ReleaseMesh(u32 id) { std::lock_guard lock(m_mutex); if (id >= m_meshHandles.size() || !m_meshHandles[id].IsValid()) return; // Move the allocation to the deferred release queue. m_deferredReleaseQueue.push({ m_meshHandles[id].allocation, m_fenceValue }); // Invalidate the handle and add its ID to the free list for reuse. m_meshHandles[id] = {}; // Reset the handle. m_freeHandleIndices.push(id); } /** * @brief Gets a constant pointer to a mesh handle by its ID. * @param id The ID of the mesh. * @return A pointer to the MeshHandle, or nullptr if the ID is invalid. */ const GPUMeshHandle* GetMeshHandle(u32 id) const { std::lock_guard lock(m_mutex); if (id >= m_meshHandles.size() || !m_meshHandles[id].IsValid()) { return nullptr; } return &m_meshHandles[id]; } /** * @brief Reclaims memory from completed uploads and deferred releases. * Call this once per frame before queuing new uploads. */ void CleanUpCompletedOperations() { std::lock_guard lock(m_mutex); if (!m_fence) return; const UINT64 completedFenceValue = m_fence->GetCompletedValue(); // Reclaim space in the upload ring buffer while (!m_inFlightUploadRanges.empty() && m_inFlightUploadRanges.front().fenceValue <= completedFenceValue) { m_inFlightUploadRanges.pop(); } // Process the deferred release queue while (!m_deferredReleaseQueue.empty() && m_deferredReleaseQueue.front().fenceValue <= completedFenceValue) { m_deferredReleaseQueue.front().allocation->Release(); m_deferredReleaseQueue.pop(); } } /** * @brief Gets the fence used by the manager, for external synchronization. */ ID3D12Fence* GetFence() const { return m_fence.Get(); } private: // D3D12 Objects & Synchronization Microsoft::WRL::ComPtr<ID3D12Device> m_device; Microsoft::WRL::ComPtr<ID3D12CommandQueue> m_copyQueue; // Dedicated copy queue D3D12MA::Allocator* m_d3d12ma_allocator = nullptr; Microsoft::WRL::ComPtr<ID3D12Fence> m_fence; UINT64 m_fenceValue = 1; UINT64 m_lastSubmittedFenceValue = 0; // Track last submitted fence HANDLE m_fenceEvent = nullptr; // Copy-specific Command Objects Microsoft::WRL::ComPtr<ID3D12CommandAllocator> m_copyAllocator; Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> m_copyCommandList; bool m_hasPendingCopies = false; // Upload Ring Buffer Microsoft::WRL::ComPtr<ID3D12Resource> m_uploadBuffer; uint8_t* m_uploadCpuPtr = nullptr; size_t m_uploadBufferSize = 0; size_t m_currentUploadOffset = 0; struct UploadRange { size_t offset; size_t size; UINT64 fenceValue; }; std::queue<UploadRange> m_inFlightUploadRanges; // Mesh Handle Management std::vector<GPUMeshHandle> m_meshHandles; std::queue<u32> m_freeHandleIndices; // Deferred Release Management struct DeferredRelease { D3D12MA::Allocation* allocation; UINT64 fenceValue; }; std::queue<DeferredRelease> m_deferredReleaseQueue; // Thread Safety mutable std::mutex m_mutex; /** * @brief Creates the upload buffer resource and maps it. */ bool InitializeUploadBuffer() { CD3DX12_HEAP_PROPERTIES uploadHeap(D3D12_HEAP_TYPE_UPLOAD); CD3DX12_RESOURCE_DESC bufferDesc = CD3DX12_RESOURCE_DESC::Buffer(m_uploadBufferSize); HRESULT hr = m_device->CreateCommittedResource( &uploadHeap, D3D12_HEAP_FLAG_NONE, &bufferDesc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&m_uploadBuffer)); if (FAILED(hr)) { return false; } hr = m_uploadBuffer->Map(0, nullptr, reinterpret_cast<void**>(&m_uploadCpuPtr)); if (FAILED(hr)) { m_uploadBuffer.Reset(); return false; } return true; } /** * @brief Finds a contiguous block of memory in the ring buffer. * Waits for the GPU if the buffer is full but space could be reclaimed. */ size_t FindSpaceInRingBuffer(size_t size) { // Align to D3D12's required alignment for copy source/dest. size_t allocationOffset = Align(m_currentUploadOffset, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); if (allocationOffset + size > m_uploadBufferSize) // Needs to wrap { allocationOffset = 0; // Try from the beginning // Check if the space we need at the beginning is still in-flight if (!m_inFlightUploadRanges.empty()) { const auto& oldestRange = m_inFlightUploadRanges.front(); if (oldestRange.offset < size) { // Space is in use, we must wait for the GPU to finish const UINT64 fenceToWaitFor = oldestRange.fenceValue; if (m_fence->GetCompletedValue() < fenceToWaitFor) { m_fence->SetEventOnCompletion(fenceToWaitFor, m_fenceEvent); WaitForSingleObject(m_fenceEvent, INFINITE); } CleanUpCompletedOperations(); // Reclaim space } } // After waiting and cleaning, check again. if (!m_inFlightUploadRanges.empty() && m_inFlightUploadRanges.front().offset < size) { return (size_t)-1; // Failure } } return allocationOffset; } /** * @brief Waits for all submitted GPU work to complete. */ void Flush() { SubmitUploads(); // Ensure any pending work is on the GPU if (!m_fence) return; const UINT64 fenceToWaitFor = m_fenceValue - 1; if (m_fence->GetCompletedValue() < fenceToWaitFor) { m_fence->SetEventOnCompletion(fenceToWaitFor, m_fenceEvent); WaitForSingleObject(m_fenceEvent, INFINITE); } CleanUpCompletedOperations(); m_currentUploadOffset = 0; } /** * @brief Waits for a specific fence value to be completed by the GPU. */ void WaitForFenceValue(UINT64 fenceValue) { if (!m_fence) return; if (m_fence->GetCompletedValue() < fenceValue) { m_fence->SetEventOnCompletion(fenceValue, m_fenceEvent); WaitForSingleObject(m_fenceEvent, INFINITE); } } /** * @brief Gets a recycled or new ID for a mesh handle. */ u32 GetNewHandleID() { if (!m_freeHandleIndices.empty()) { u32 id = m_freeHandleIndices.front(); m_freeHandleIndices.pop(); return id; } u32 id = static_cast<u32>(m_meshHandles.size()); m_meshHandles.emplace_back(); return id; } }; } // namespace RtxKey Changes Made:
-
Added
m_lastSubmittedFenceValue: This tracks the fence value of the last submitted command list. -
Added
WaitForFenceValue()method: A helper method to wait for a specific fence value to complete. -
Fixed the Reset Issue: In
UploadMesh(), before resetting the command allocator, we now wait for the previous submission to complete:if (!m_hasPendingCopies) { // Wait for the previous command list to finish before resetting WaitForFenceValue(m_lastSubmittedFenceValue); // Now it's safe to reset HRESULT hr = m_copyAllocator->Reset(); // ... rest of reset code } -
Track Last Submitted Fence: In
SubmitUploads(), we store the current fence value before incrementing it:// Store the fence value for this submission m_lastSubmittedFenceValue = m_fenceValue; // Increment fence for the next submission batch m_fenceValue++;
Why This Fixes the Issue:
The problem was that the command allocator was being reset before the GPU had finished executing the commands from the previous submission. By tracking the last submitted fence value and waiting for it to complete before resetting the allocator, we ensure proper synchronization between CPU and GPU operations.
This approach allows the manager to:
- Batch multiple uploads together in a single command list
- Wait for the GPU to finish before starting a new batch
- Avoid the COMMAND_ALLOCATOR_SYNC error
The fix maintains the efficiency of batching while ensuring proper synchronization with the GPU.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.