Skip to content

Instantly share code, notes, and snippets.

@yorung
Last active September 3, 2016 16:54
Show Gist options
  • Save yorung/22b16d97aa6d996647d86aa28ad8338c to your computer and use it in GitHub Desktop.
Save yorung/22b16d97aa6d996647d86aa28ad8338c to your computer and use it in GitHub Desktop.
[DX12] Create RTV and DSV on stack and discard immediately. Unlike D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, RTV and DSV can discard after API calls.
// DON'T DO THIS!!
// It seems working fine, however the DX12 debug layer treat this as following error.
//
// "An ID3D12DescriptorHeap object referenced in a command list ID3D12GraphicsCommandList Object "..." was
// deleted prior to executing the command list. This is invalid and can result in application instability.
// [ EXECUTION ERROR #921: OBJECT_DELETED_WHILE_STILL_IN_USE]"
//
ComPtr<ID3D12DescriptorHeap> rtvHeap, dsvHeap;
const D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = { D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1 };
device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&rtvHeap));
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = rtvHeap->GetCPUDescriptorHandleForHeapStart();
device->CreateRenderTargetView(renderTarget.Get(), nullptr, rtvHandle);
const D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = { D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1 };
device->CreateDescriptorHeap(&dsvHeapDesc, IID_PPV_ARGS(&dsvHeap));
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = dsvHeap->GetCPUDescriptorHandleForHeapStart();
device->CreateDepthStencilView(depthStencil.Get(), nullptr, dsvHandle);
commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, &dsvHandle);
const float clearColor[] = { 0.0f, 0.2f, 0.3f, 1.0f };
commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
commandList->ClearDepthStencilView(dsvHandle, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 1.0f, 0, 0, nullptr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment