d3d12: Use ComPtr for some others member.

This commit is contained in:
Vincent Lejeune 2015-08-15 23:31:00 +02:00 committed by Nekotekina
parent b54adebfc7
commit c2430d3af1
2 changed files with 39 additions and 57 deletions

View File

@ -103,10 +103,6 @@ void D3D12GSRender::ResourceStorage::Reset()
m_samplerDescriptorHeapIndex = 0; m_samplerDescriptorHeapIndex = 0;
m_commandAllocator->Reset(); m_commandAllocator->Reset();
m_textureUploadCommandAllocator->Reset();
m_downloadCommandAllocator->Reset();
for (ID3D12GraphicsCommandList *gfxCommandList : m_inflightCommandList)
gfxCommandList->Release();
m_inflightCommandList.clear(); m_inflightCommandList.clear();
} }
@ -114,9 +110,7 @@ void D3D12GSRender::ResourceStorage::Init(ID3D12Device *device)
{ {
m_RAMFramebuffer = nullptr; m_RAMFramebuffer = nullptr;
// Create a global command allocator // Create a global command allocator
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_commandAllocator)); ThrowIfFailed(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(m_commandAllocator.GetAddressOf())));
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_textureUploadCommandAllocator));
ThrowIfFailed(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COPY, IID_PPV_ARGS(&m_downloadCommandAllocator)));
D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc = {}; D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc = {};
descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
@ -124,7 +118,6 @@ void D3D12GSRender::ResourceStorage::Init(ID3D12Device *device)
descriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; descriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
ThrowIfFailed(device->CreateDescriptorHeap(&descriptorHeapDesc, IID_PPV_ARGS(&m_constantsBufferDescriptorsHeap))); ThrowIfFailed(device->CreateDescriptorHeap(&descriptorHeapDesc, IID_PPV_ARGS(&m_constantsBufferDescriptorsHeap)));
descriptorHeapDesc = {}; descriptorHeapDesc = {};
descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
descriptorHeapDesc.NumDescriptors = 10000; // For safety descriptorHeapDesc.NumDescriptors = 10000; // For safety
@ -150,17 +143,8 @@ void D3D12GSRender::ResourceStorage::Init(ID3D12Device *device)
void D3D12GSRender::ResourceStorage::Release() void D3D12GSRender::ResourceStorage::Release()
{ {
// NOTE: Should be released only if no command are in flight ! // NOTE: Should be released only after gfx pipeline last command has been finished.
m_constantsBufferDescriptorsHeap->Release(); m_inflightCommandList.clear();
m_scaleOffsetDescriptorHeap->Release();
m_textureDescriptorsHeap->Release();
m_samplerDescriptorHeap[0]->Release();
m_samplerDescriptorHeap[1]->Release();
for (auto &tmp : m_inflightCommandList)
tmp->Release();
m_commandAllocator->Release();
m_textureUploadCommandAllocator->Release();
m_downloadCommandAllocator->Release();
CloseHandle(m_frameFinishedHandle); CloseHandle(m_frameFinishedHandle);
} }
@ -431,11 +415,11 @@ void D3D12GSRender::Clear(u32 cmd)
{ {
assert(cmd == NV4097_CLEAR_SURFACE); assert(cmd == NV4097_CLEAR_SURFACE);
ID3D12GraphicsCommandList *commandList; ComPtr<ID3D12GraphicsCommandList> commandList;
ThrowIfFailed(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&commandList))); ThrowIfFailed(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator.Get(), nullptr, IID_PPV_ARGS(commandList.GetAddressOf())));
getCurrentResourceStorage().m_inflightCommandList.push_back(commandList); getCurrentResourceStorage().m_inflightCommandList.push_back(commandList);
PrepareRenderTargets(commandList); PrepareRenderTargets(commandList.Get());
/* if (m_set_color_mask) /* if (m_set_color_mask)
{ {
@ -504,16 +488,16 @@ void D3D12GSRender::Clear(u32 cmd)
} }
ThrowIfFailed(commandList->Close()); ThrowIfFailed(commandList->Close());
m_commandQueueGraphic->ExecuteCommandLists(1, (ID3D12CommandList**) &commandList); m_commandQueueGraphic->ExecuteCommandLists(1, (ID3D12CommandList**)commandList.GetAddressOf());
} }
void D3D12GSRender::Draw() void D3D12GSRender::Draw()
{ {
ID3D12GraphicsCommandList *commandList; ComPtr<ID3D12GraphicsCommandList> commandList;
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&commandList)); m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator.Get(), nullptr, IID_PPV_ARGS(commandList.GetAddressOf()));
getCurrentResourceStorage().m_inflightCommandList.push_back(commandList); getCurrentResourceStorage().m_inflightCommandList.push_back(commandList);
PrepareRenderTargets(commandList); PrepareRenderTargets(commandList.Get());
// Init vertex count // Init vertex count
// TODO: Very hackish, clean this // TODO: Very hackish, clean this
@ -564,9 +548,9 @@ void D3D12GSRender::Draw()
// Constants // Constants
setScaleOffset(); setScaleOffset();
commandList->SetDescriptorHeaps(1, &getCurrentResourceStorage().m_scaleOffsetDescriptorHeap); commandList->SetDescriptorHeaps(1, getCurrentResourceStorage().m_scaleOffsetDescriptorHeap.GetAddressOf());
commandList->SetGraphicsRootDescriptorTable(0, commandList->SetGraphicsRootDescriptorTable(0,
getGPUDescriptorHandle(getCurrentResourceStorage().m_scaleOffsetDescriptorHeap, getGPUDescriptorHandle(getCurrentResourceStorage().m_scaleOffsetDescriptorHeap.Get(),
getCurrentResourceStorage().m_currentScaleOffsetBufferIndex * g_descriptorStrideSRVCBVUAV) getCurrentResourceStorage().m_currentScaleOffsetBufferIndex * g_descriptorStrideSRVCBVUAV)
); );
getCurrentResourceStorage().m_currentScaleOffsetBufferIndex++; getCurrentResourceStorage().m_currentScaleOffsetBufferIndex++;
@ -577,9 +561,9 @@ void D3D12GSRender::Draw()
FillPixelShaderConstantsBuffer(); FillPixelShaderConstantsBuffer();
getCurrentResourceStorage().m_constantsBufferIndex++; getCurrentResourceStorage().m_constantsBufferIndex++;
commandList->SetDescriptorHeaps(1, &getCurrentResourceStorage().m_constantsBufferDescriptorsHeap); commandList->SetDescriptorHeaps(1, getCurrentResourceStorage().m_constantsBufferDescriptorsHeap.GetAddressOf());
commandList->SetGraphicsRootDescriptorTable(1, commandList->SetGraphicsRootDescriptorTable(1,
getGPUDescriptorHandle(getCurrentResourceStorage().m_constantsBufferDescriptorsHeap, getGPUDescriptorHandle(getCurrentResourceStorage().m_constantsBufferDescriptorsHeap.Get(),
currentBufferIndex * g_descriptorStrideSRVCBVUAV) currentBufferIndex * g_descriptorStrideSRVCBVUAV)
); );
commandList->SetPipelineState(m_PSO->first); commandList->SetPipelineState(m_PSO->first);
@ -587,7 +571,7 @@ void D3D12GSRender::Draw()
if (m_PSO->second > 0) if (m_PSO->second > 0)
{ {
std::chrono::time_point<std::chrono::system_clock> startTextureTime = std::chrono::system_clock::now(); std::chrono::time_point<std::chrono::system_clock> startTextureTime = std::chrono::system_clock::now();
size_t usedTexture = UploadTextures(commandList); size_t usedTexture = UploadTextures(commandList.Get());
// Fill empty slots // Fill empty slots
for (; usedTexture < m_PSO->second; usedTexture++) for (; usedTexture < m_PSO->second; usedTexture++)
@ -602,7 +586,7 @@ void D3D12GSRender::Draw()
D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0, D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0,
D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0); D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0);
m_device->CreateShaderResourceView(m_dummyTexture, &srvDesc, m_device->CreateShaderResourceView(m_dummyTexture, &srvDesc,
getCPUDescriptorHandle(getCurrentResourceStorage().m_textureDescriptorsHeap, getCPUDescriptorHandle(getCurrentResourceStorage().m_textureDescriptorsHeap.Get(),
(getCurrentResourceStorage().m_currentTextureIndex + usedTexture) * g_descriptorStrideSRVCBVUAV) (getCurrentResourceStorage().m_currentTextureIndex + usedTexture) * g_descriptorStrideSRVCBVUAV)
); );
@ -612,20 +596,20 @@ void D3D12GSRender::Draw()
samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP; samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP; samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;
m_device->CreateSampler(&samplerDesc, m_device->CreateSampler(&samplerDesc,
getCPUDescriptorHandle(getCurrentResourceStorage().m_samplerDescriptorHeap[getCurrentResourceStorage().m_samplerDescriptorHeapIndex], getCPUDescriptorHandle(getCurrentResourceStorage().m_samplerDescriptorHeap[getCurrentResourceStorage().m_samplerDescriptorHeapIndex].Get(),
(getCurrentResourceStorage().m_currentSamplerIndex + usedTexture) * g_descriptorStrideSamplers) (getCurrentResourceStorage().m_currentSamplerIndex + usedTexture) * g_descriptorStrideSamplers)
); );
} }
commandList->SetDescriptorHeaps(1, &getCurrentResourceStorage().m_textureDescriptorsHeap); commandList->SetDescriptorHeaps(1, getCurrentResourceStorage().m_textureDescriptorsHeap.GetAddressOf());
commandList->SetGraphicsRootDescriptorTable(2, commandList->SetGraphicsRootDescriptorTable(2,
getGPUDescriptorHandle(getCurrentResourceStorage().m_textureDescriptorsHeap, getGPUDescriptorHandle(getCurrentResourceStorage().m_textureDescriptorsHeap.Get(),
getCurrentResourceStorage().m_currentTextureIndex * g_descriptorStrideSRVCBVUAV) getCurrentResourceStorage().m_currentTextureIndex * g_descriptorStrideSRVCBVUAV)
); );
commandList->SetDescriptorHeaps(1, &getCurrentResourceStorage().m_samplerDescriptorHeap[getCurrentResourceStorage().m_samplerDescriptorHeapIndex]); commandList->SetDescriptorHeaps(1, getCurrentResourceStorage().m_samplerDescriptorHeap[getCurrentResourceStorage().m_samplerDescriptorHeapIndex].GetAddressOf());
commandList->SetGraphicsRootDescriptorTable(3, commandList->SetGraphicsRootDescriptorTable(3,
getGPUDescriptorHandle(getCurrentResourceStorage().m_samplerDescriptorHeap[getCurrentResourceStorage().m_samplerDescriptorHeapIndex], getGPUDescriptorHandle(getCurrentResourceStorage().m_samplerDescriptorHeap[getCurrentResourceStorage().m_samplerDescriptorHeapIndex].Get(),
getCurrentResourceStorage().m_currentTextureIndex * g_descriptorStrideSamplers) getCurrentResourceStorage().m_currentTextureIndex * g_descriptorStrideSamplers)
); );
@ -717,7 +701,7 @@ void D3D12GSRender::Draw()
commandList->DrawInstanced((UINT)m_renderingInfo.m_count, 1, (UINT)m_renderingInfo.m_baseVertex, 0); commandList->DrawInstanced((UINT)m_renderingInfo.m_count, 1, (UINT)m_renderingInfo.m_baseVertex, 0);
ThrowIfFailed(commandList->Close()); ThrowIfFailed(commandList->Close());
m_commandQueueGraphic->ExecuteCommandLists(1, (ID3D12CommandList**)&commandList); m_commandQueueGraphic->ExecuteCommandLists(1, (ID3D12CommandList**)commandList.GetAddressOf());
m_indexed_array.Reset(); m_indexed_array.Reset();
} }
@ -740,8 +724,8 @@ isFlipSurfaceInLocalMemory(u32 surfaceColorTarget)
void D3D12GSRender::Flip() void D3D12GSRender::Flip()
{ {
ID3D12GraphicsCommandList *commandList; ComPtr<ID3D12GraphicsCommandList> commandList;
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&commandList)); m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator.Get(), nullptr, IID_PPV_ARGS(commandList.GetAddressOf()));
getCurrentResourceStorage().m_inflightCommandList.push_back(commandList); getCurrentResourceStorage().m_inflightCommandList.push_back(commandList);
ID3D12Resource *resourceToFlip; ID3D12Resource *resourceToFlip;
@ -795,12 +779,12 @@ void D3D12GSRender::Flip()
&getTexture2DResourceDesc(w, h, DXGI_FORMAT_R8G8B8A8_UNORM, 1), &getTexture2DResourceDesc(w, h, DXGI_FORMAT_R8G8B8A8_UNORM, 1),
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_DEST,
nullptr, nullptr,
IID_PPV_ARGS(&storage.m_RAMFramebuffer) IID_PPV_ARGS(storage.m_RAMFramebuffer.GetAddressOf())
) )
); );
D3D12_TEXTURE_COPY_LOCATION src = {}, dst = {}; D3D12_TEXTURE_COPY_LOCATION src = {}, dst = {};
dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
dst.pResource = storage.m_RAMFramebuffer; dst.pResource = storage.m_RAMFramebuffer.Get();
src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; src.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
src.pResource = stagingTexture; src.pResource = stagingTexture;
src.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM; src.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
@ -810,8 +794,8 @@ void D3D12GSRender::Flip()
src.PlacedFootprint.Footprint.RowPitch = (UINT)rowPitch; src.PlacedFootprint.Footprint.RowPitch = (UINT)rowPitch;
commandList->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); commandList->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
commandList->ResourceBarrier(1, &getResourceBarrierTransition(storage.m_RAMFramebuffer, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_GENERIC_READ)); commandList->ResourceBarrier(1, &getResourceBarrierTransition(storage.m_RAMFramebuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_GENERIC_READ));
resourceToFlip = storage.m_RAMFramebuffer; resourceToFlip = storage.m_RAMFramebuffer.Get();
viewport_w = (float)w, viewport_h = (float)h; viewport_w = (float)w, viewport_h = (float)h;
} }
else else
@ -897,7 +881,7 @@ void D3D12GSRender::Flip()
if (isFlipSurfaceInLocalMemory(m_surface_color_target) && m_rtts.m_currentlyBoundRenderTargets[0] != nullptr) if (isFlipSurfaceInLocalMemory(m_surface_color_target) && m_rtts.m_currentlyBoundRenderTargets[0] != nullptr)
commandList->ResourceBarrier(1, &getResourceBarrierTransition(m_rtts.m_currentlyBoundRenderTargets[0], D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_RENDER_TARGET)); commandList->ResourceBarrier(1, &getResourceBarrierTransition(m_rtts.m_currentlyBoundRenderTargets[0], D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_RENDER_TARGET));
ThrowIfFailed(commandList->Close()); ThrowIfFailed(commandList->Close());
m_commandQueueGraphic->ExecuteCommandLists(1, (ID3D12CommandList**)&commandList); m_commandQueueGraphic->ExecuteCommandLists(1, (ID3D12CommandList**)commandList.GetAddressOf());
ThrowIfFailed(m_swapChain->Present(Ini.GSVSyncEnable.GetValue() ? 1 : 0, 0)); ThrowIfFailed(m_swapChain->Present(Ini.GSVSyncEnable.GetValue() ? 1 : 0, 0));
// Add an event signaling queue completion // Add an event signaling queue completion
@ -1106,7 +1090,7 @@ void D3D12GSRender::semaphorePGRAPHBackendRelease(u32 offset, u32 value)
m_readbackResources.m_resourceStoredSinceLastSync.push_back(std::make_tuple(heapOffset, sizeInByte, writeDest)); m_readbackResources.m_resourceStoredSinceLastSync.push_back(std::make_tuple(heapOffset, sizeInByte, writeDest));
ThrowIfFailed( ThrowIfFailed(
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&convertCommandList)) m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator.Get(), nullptr, IID_PPV_ARGS(&convertCommandList))
); );
D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc = {}; D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc = {};
@ -1172,7 +1156,7 @@ void D3D12GSRender::semaphorePGRAPHBackendRelease(u32 offset, u32 value)
if (needTransfer) if (needTransfer)
{ {
ThrowIfFailed( ThrowIfFailed(
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&downloadCommandList)) m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator.Get(), nullptr, IID_PPV_ARGS(&downloadCommandList))
); );
} }

View File

@ -319,25 +319,23 @@ private:
UINT64 m_fenceValue; UINT64 m_fenceValue;
HANDLE m_frameFinishedHandle; HANDLE m_frameFinishedHandle;
ID3D12CommandAllocator *m_commandAllocator; ComPtr<ID3D12CommandAllocator> m_commandAllocator;
ID3D12CommandAllocator *m_downloadCommandAllocator; std::list<ComPtr<ID3D12GraphicsCommandList> > m_inflightCommandList;
std::list<ID3D12GraphicsCommandList *> m_inflightCommandList;
// Constants storage // Constants storage
ID3D12DescriptorHeap *m_constantsBufferDescriptorsHeap; ComPtr<ID3D12DescriptorHeap> m_constantsBufferDescriptorsHeap;
size_t m_constantsBufferIndex; size_t m_constantsBufferIndex;
ID3D12DescriptorHeap *m_scaleOffsetDescriptorHeap; ComPtr<ID3D12DescriptorHeap> m_scaleOffsetDescriptorHeap;
size_t m_currentScaleOffsetBufferIndex; size_t m_currentScaleOffsetBufferIndex;
// Texture storage // Texture storage
ID3D12CommandAllocator *m_textureUploadCommandAllocator; ComPtr<ID3D12DescriptorHeap> m_textureDescriptorsHeap;
ID3D12DescriptorHeap *m_textureDescriptorsHeap; size_t m_currentTextureIndex;
ID3D12DescriptorHeap *m_samplerDescriptorHeap[2]; ComPtr<ID3D12DescriptorHeap> m_samplerDescriptorHeap[2];
size_t m_samplerDescriptorHeapIndex; size_t m_samplerDescriptorHeapIndex;
size_t m_currentSamplerIndex; size_t m_currentSamplerIndex;
size_t m_currentTextureIndex;
ID3D12Resource *m_RAMFramebuffer; ComPtr<ID3D12Resource> m_RAMFramebuffer;
void Reset(); void Reset();
void Init(ID3D12Device *device); void Init(ID3D12Device *device);