Reputation: 8356
I updated my Windows 10 to version 1607 which includes additional D3D debug layer checking. My vertex buffer updating code worked without warnings/errors before the update, but now I'm getting an error when calling CopyBufferRegion
:
Resource state (0xAC3: D3D12_RESOURCE_STATE_GENERIC_READ) of resource (0x000001DB301B9750:'') (subresource: 0) is invalid for use as a destination buffer. Expected State Bits: 0x400: D3D12_RESOURCE_STATE_COPY_DEST, Actual State: 0xAC3: D3D12_RESOURCE_STATE_GENERIC_READ
If I transition vb
into the wanted state (D3D12_RESOURCE_STATE_COPY_DEST
) I instead get an error like this:
D3D12 ERROR: ID3D12CommandList::ResourceBarrier: Certain heaps are restricted to certain D3D12_RESOURCE_STATES states, and cannot be changed. D3D12_HEAP_TYPE_UPLOAD requires D3D12_RESOURCE_STATE_GENERIC_READ. D3D12_HEAP_TYPE_READBACK requires D3D12_RESOURCE_STATE_COPY_DEST. [ RESOURCE_MANIPULATION ERROR #741: RESOURCE_BARRIER_INVALID_HEAP]
Here is my code. It creates a vertex buffer and uploads vertex data, resizing the buffer if necessary:
void ae3d::VertexBuffer::UploadVB( void* faces, void* vertices, unsigned ibSize )
{
D3D12_HEAP_PROPERTIES uploadProp = {};
uploadProp.Type = D3D12_HEAP_TYPE_UPLOAD;
uploadProp.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
uploadProp.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
uploadProp.CreationNodeMask = 1;
uploadProp.VisibleNodeMask = 1;
D3D12_RESOURCE_DESC bufferProp = {};
bufferProp.Alignment = 0;
bufferProp.DepthOrArraySize = 1;
bufferProp.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
bufferProp.Flags = D3D12_RESOURCE_FLAG_NONE;
bufferProp.Format = DXGI_FORMAT_UNKNOWN;
bufferProp.Height = 1;
bufferProp.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
bufferProp.MipLevels = 1;
bufferProp.SampleDesc.Count = 1;
bufferProp.SampleDesc.Quality = 0;
bufferProp.Width = ibOffset + ibSize;
// this branch resizes the buffer and causes validation errors.
if (vb != nullptr && bufferProp.Width <= sizeBytes)
{
ID3D12Resource* stagingBuffer = nullptr;
HRESULT hr = GfxDeviceGlobal::device->CreateCommittedResource( &uploadProp, D3D12_HEAP_FLAG_NONE, &bufferProp,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS( &stagingBuffer ) );
AE3D_CHECK_D3D( hr, "Failed to create vertex staging resource" );
char* vbUploadPtr = nullptr;
hr = stagingBuffer->Map( 0, nullptr, reinterpret_cast<void**>(&vbUploadPtr) );
if (FAILED( hr ))
{
ae3d::System::Assert( false, "Unable to map vertex staging buffer!\n" );
return;
}
memcpy_s( vbUploadPtr, ibOffset, vertices, ibOffset );
memcpy_s( vbUploadPtr + ibOffset, ibSize, faces, ibSize );
stagingBuffer->Unmap( 0, nullptr );
// vb state is invalid at this point, needs D3D12_RESOURCE_STATE_COPY_DEST
GfxDeviceGlobal::graphicsCommandList->CopyBufferRegion( vb, 0, stagingBuffer, 0, ibOffset + ibSize );
Global::frameVBUploads.push_back( stagingBuffer );
sizeBytes = ibOffset + ibSize;
return;
}
sizeBytes = ibOffset + ibSize;
HRESULT hr = GfxDeviceGlobal::device->CreateCommittedResource(
&uploadProp,
D3D12_HEAP_FLAG_NONE,
&bufferProp,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS( &vb ) );
if (FAILED( hr ))
{
ae3d::System::Assert( false, "Unable to create vertex buffer!\n" );
return;
}
Global::vbs.push_back( vb );
char* vbUploadPtr = nullptr;
hr = vb->Map( 0, nullptr, reinterpret_cast<void**>(&vbUploadPtr) );
if (FAILED( hr ))
{
ae3d::System::Assert( false, "Unable to map vertex buffer!\n" );
return;
}
memcpy_s( vbUploadPtr, ibOffset, vertices, ibOffset );
memcpy_s( vbUploadPtr + ibOffset, ibSize, faces, ibSize );
vb->Unmap( 0, nullptr );
}
How can I fix these debug layer errors?
Upvotes: 3
Views: 1725
Reputation: 4663
Upload buffer can only be in D3D12_RESOURCE_STATE_GENERIC_READ
state (link). You need to create a default buffer with D3D12_RESOURCE_STATE_COPY_DEST
state. Then after mapping of upload heap on cpu you can copy contents of upload buffer to default buffer on gpu with ID3D12GraphicsCommandList::CopyBufferRegion() method. Don't forget to transition default buffer to usable state after and be sure that upload heap still exist at the moment the gpu executed the copying.
Upvotes: 2