d3d12: Ping pong between data to avoid gpu stall as much as possible

This commit is contained in:
vlj 2015-06-02 23:54:24 +02:00 committed by Vincent Lejeune
parent 461bf12c4f
commit 7db3599648
5 changed files with 98 additions and 65 deletions

View file

@ -414,8 +414,8 @@ void D3D12GSRender::setScaleOffset()
D3D12_CONSTANT_BUFFER_VIEW_DESC constantBufferViewDesc = {};
constantBufferViewDesc.BufferLocation = scaleOffsetBuffer->GetGPUVirtualAddress();
constantBufferViewDesc.SizeInBytes = (UINT)256;
D3D12_CPU_DESCRIPTOR_HANDLE Handle = m_perFrameStorage.m_scaleOffsetDescriptorHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += m_perFrameStorage.m_currentScaleOffsetBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
D3D12_CPU_DESCRIPTOR_HANDLE Handle = getCurrentResourceStorage().m_scaleOffsetDescriptorHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += getCurrentResourceStorage().m_currentScaleOffsetBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
m_device->CreateConstantBufferView(&constantBufferViewDesc, Handle);
m_constantsData.m_resourceStoredSinceLastSync.push_back(std::make_tuple(heapOffset, 256, scaleOffsetBuffer));
}
@ -450,8 +450,8 @@ void D3D12GSRender::FillVertexShaderConstantsBuffer()
D3D12_CONSTANT_BUFFER_VIEW_DESC constantBufferViewDesc = {};
constantBufferViewDesc.BufferLocation = constantsBuffer->GetGPUVirtualAddress();
constantBufferViewDesc.SizeInBytes = 512 * 4 * sizeof(float);
D3D12_CPU_DESCRIPTOR_HANDLE Handle = m_perFrameStorage.m_constantsBufferDescriptorsHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += m_perFrameStorage.m_constantsBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
D3D12_CPU_DESCRIPTOR_HANDLE Handle = getCurrentResourceStorage().m_constantsBufferDescriptorsHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += getCurrentResourceStorage().m_constantsBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
m_device->CreateConstantBufferView(&constantBufferViewDesc, Handle);
m_constantsData.m_resourceStoredSinceLastSync.push_back(std::make_tuple(heapOffset, 512 * 4 * sizeof(float), constantsBuffer));
}
@ -523,8 +523,8 @@ void D3D12GSRender::FillPixelShaderConstantsBuffer()
D3D12_CONSTANT_BUFFER_VIEW_DESC constantBufferViewDesc = {};
constantBufferViewDesc.BufferLocation = constantsBuffer->GetGPUVirtualAddress();
constantBufferViewDesc.SizeInBytes = (UINT)bufferSize;
D3D12_CPU_DESCRIPTOR_HANDLE Handle = m_perFrameStorage.m_constantsBufferDescriptorsHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += m_perFrameStorage.m_constantsBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
D3D12_CPU_DESCRIPTOR_HANDLE Handle = getCurrentResourceStorage().m_constantsBufferDescriptorsHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += getCurrentResourceStorage().m_constantsBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
m_device->CreateConstantBufferView(&constantBufferViewDesc, Handle);
m_constantsData.m_resourceStoredSinceLastSync.push_back(std::make_tuple(heapOffset, bufferSize, constantsBuffer));
}

View file

@ -98,6 +98,7 @@ void D3D12GSRender::ResourceStorage::Reset()
void D3D12GSRender::ResourceStorage::Init(ID3D12Device *device)
{
m_frameFinished = 0;
// Create a global command allocator
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_commandAllocator));
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_textureUploadCommandAllocator));
@ -322,8 +323,10 @@ D3D12GSRender::D3D12GSRender()
IID_PPV_ARGS(&m_rootSignatures[textureCount]));
}
m_perFrameStorage.Init(m_device);
m_perFrameStorage.Reset();
m_perFrameStorage[0].Init(m_device);
m_perFrameStorage[0].Reset();
m_perFrameStorage[1].Init(m_device);
m_perFrameStorage[1].Reset();
vertexConstantShadowCopy = new float[512 * 4];
@ -391,7 +394,8 @@ D3D12GSRender::~D3D12GSRender()
m_dummyTexture->Release();
m_convertPSO->Release();
m_convertRootSignature->Release();
m_perFrameStorage.Release();
m_perFrameStorage[0].Release();
m_perFrameStorage[1].Release();
m_commandQueueGraphic->Release();
m_commandQueueCopy->Release();
m_backbufferAsRendertarget[0]->Release();
@ -436,8 +440,8 @@ void D3D12GSRender::ExecCMD(u32 cmd)
InitDrawBuffers();
ID3D12GraphicsCommandList *commandList;
check(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_perFrameStorage.m_commandAllocator, nullptr, IID_PPV_ARGS(&commandList)));
m_perFrameStorage.m_inflightCommandList.push_back(commandList);
check(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&commandList)));
getCurrentResourceStorage().m_inflightCommandList.push_back(commandList);
/* if (m_set_color_mask)
{
@ -686,8 +690,8 @@ void D3D12GSRender::ExecCMD()
InitDrawBuffers();
ID3D12GraphicsCommandList *commandList;
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_perFrameStorage.m_commandAllocator, nullptr, IID_PPV_ARGS(&commandList));
m_perFrameStorage.m_inflightCommandList.push_back(commandList);
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&commandList));
getCurrentResourceStorage().m_inflightCommandList.push_back(commandList);
if (m_indexed_array.m_count)
LoadVertexData(m_indexed_array.index_min, m_indexed_array.index_max - m_indexed_array.index_min + 1);
@ -711,20 +715,20 @@ void D3D12GSRender::ExecCMD()
// Constants
setScaleOffset();
commandList->SetDescriptorHeaps(1, &m_perFrameStorage.m_scaleOffsetDescriptorHeap);
D3D12_GPU_DESCRIPTOR_HANDLE Handle = m_perFrameStorage.m_scaleOffsetDescriptorHeap->GetGPUDescriptorHandleForHeapStart();
Handle.ptr += m_perFrameStorage.m_currentScaleOffsetBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
commandList->SetDescriptorHeaps(1, &getCurrentResourceStorage().m_scaleOffsetDescriptorHeap);
D3D12_GPU_DESCRIPTOR_HANDLE Handle = getCurrentResourceStorage().m_scaleOffsetDescriptorHeap->GetGPUDescriptorHandleForHeapStart();
Handle.ptr += getCurrentResourceStorage().m_currentScaleOffsetBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
commandList->SetGraphicsRootDescriptorTable(0, Handle);
m_perFrameStorage.m_currentScaleOffsetBufferIndex++;
getCurrentResourceStorage().m_currentScaleOffsetBufferIndex++;
size_t currentBufferIndex = m_perFrameStorage.m_constantsBufferIndex;
size_t currentBufferIndex = getCurrentResourceStorage().m_constantsBufferIndex;
FillVertexShaderConstantsBuffer();
m_perFrameStorage.m_constantsBufferIndex++;
getCurrentResourceStorage().m_constantsBufferIndex++;
FillPixelShaderConstantsBuffer();
m_perFrameStorage.m_constantsBufferIndex++;
getCurrentResourceStorage().m_constantsBufferIndex++;
commandList->SetDescriptorHeaps(1, &m_perFrameStorage.m_constantsBufferDescriptorsHeap);
Handle = m_perFrameStorage.m_constantsBufferDescriptorsHeap->GetGPUDescriptorHandleForHeapStart();
commandList->SetDescriptorHeaps(1, &getCurrentResourceStorage().m_constantsBufferDescriptorsHeap);
Handle = getCurrentResourceStorage().m_constantsBufferDescriptorsHeap->GetGPUDescriptorHandleForHeapStart();
Handle.ptr += currentBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
commandList->SetGraphicsRootDescriptorTable(1, Handle);
commandList->SetPipelineState(m_PSO->first);
@ -733,17 +737,17 @@ void D3D12GSRender::ExecCMD()
{
size_t usedTexture = UploadTextures();
Handle = m_perFrameStorage.m_textureDescriptorsHeap->GetGPUDescriptorHandleForHeapStart();
Handle.ptr += m_perFrameStorage.m_currentTextureIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
commandList->SetDescriptorHeaps(1, &m_perFrameStorage.m_textureDescriptorsHeap);
Handle = getCurrentResourceStorage().m_textureDescriptorsHeap->GetGPUDescriptorHandleForHeapStart();
Handle.ptr += getCurrentResourceStorage().m_currentTextureIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
commandList->SetDescriptorHeaps(1, &getCurrentResourceStorage().m_textureDescriptorsHeap);
commandList->SetGraphicsRootDescriptorTable(2, Handle);
Handle = m_perFrameStorage.m_samplerDescriptorHeap->GetGPUDescriptorHandleForHeapStart();
Handle.ptr += m_perFrameStorage.m_currentTextureIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
commandList->SetDescriptorHeaps(1, &m_perFrameStorage.m_samplerDescriptorHeap);
Handle = getCurrentResourceStorage().m_samplerDescriptorHeap->GetGPUDescriptorHandleForHeapStart();
Handle.ptr += getCurrentResourceStorage().m_currentTextureIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
commandList->SetDescriptorHeaps(1, &getCurrentResourceStorage().m_samplerDescriptorHeap);
commandList->SetGraphicsRootDescriptorTable(3, Handle);
m_perFrameStorage.m_currentTextureIndex += usedTexture;
getCurrentResourceStorage().m_currentTextureIndex += usedTexture;
}
size_t numRTT;
@ -843,8 +847,8 @@ void D3D12GSRender::ExecCMD()
void D3D12GSRender::Flip()
{
ID3D12GraphicsCommandList *commandList;
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_perFrameStorage.m_commandAllocator, nullptr, IID_PPV_ARGS(&commandList));
m_perFrameStorage.m_inflightCommandList.push_back(commandList);
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&commandList));
getCurrentResourceStorage().m_inflightCommandList.push_back(commandList);
switch (m_surface_color_target)
{
@ -887,43 +891,64 @@ void D3D12GSRender::Flip()
// Add an event signaling queue completion
Microsoft::WRL::ComPtr<ID3D12Fence> fence;
m_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
HANDLE handle = CreateEvent(0, 0, 0, 0);
fence->SetEventOnCompletion(1, handle);
getCurrentResourceStorage().m_frameFinished = CreateEvent(0, 0, 0, 0);
fence->SetEventOnCompletion(1, getCurrentResourceStorage().m_frameFinished);
m_commandQueueGraphic->Signal(fence.Get(), 1);
WaitForSingleObject(handle, INFINITE);
CloseHandle(handle);
m_perFrameStorage.Reset();
// Flush
getCurrentResourceStorage().Reset();
m_texturesCache.clear();
m_texturesRTTs.clear();
for (auto tmp : m_constantsData.m_resourceStoredSinceLastSync)
if (getNonCurrentResourceStorage().m_frameFinished)
{
std::get<2>(tmp)->Release();
m_constantsData.m_getPos = std::get<0>(tmp);
WaitForSingleObject(getNonCurrentResourceStorage().m_frameFinished, INFINITE);
CloseHandle(getNonCurrentResourceStorage().m_frameFinished);
for (auto tmp : getNonCurrentResourceStorage().m_inUseConstantsBuffers)
{
std::get<2>(tmp)->Release();
m_constantsData.m_getPos = std::get<0>(tmp);
}
for (auto tmp : getNonCurrentResourceStorage().m_inUseVertexIndexBuffers)
{
std::get<2>(tmp)->Release();
m_vertexIndexData.m_getPos = std::get<0>(tmp);
}
for (auto tmp : getNonCurrentResourceStorage().m_inUseTextureUploadBuffers)
{
std::get<2>(tmp)->Release();
m_textureUploadData.m_getPos = std::get<0>(tmp);
}
for (auto tmp : getNonCurrentResourceStorage().m_inUseTexture2D)
{
std::get<2>(tmp)->Release();
m_textureData.m_getPos = std::get<0>(tmp);
}
}
getNonCurrentResourceStorage().m_inUseConstantsBuffers = m_constantsData.m_resourceStoredSinceLastSync;
m_constantsData.m_resourceStoredSinceLastSync.clear();
for (auto tmp : m_vertexIndexData.m_resourceStoredSinceLastSync)
{
std::get<2>(tmp)->Release();
m_vertexIndexData.m_getPos = std::get<0>(tmp);
}
getNonCurrentResourceStorage().m_inUseVertexIndexBuffers = m_vertexIndexData.m_resourceStoredSinceLastSync;
m_vertexIndexData.m_resourceStoredSinceLastSync.clear();
for (auto tmp : m_textureUploadData.m_resourceStoredSinceLastSync)
{
std::get<2>(tmp)->Release();
m_textureUploadData.m_getPos = std::get<0>(tmp);
}
getNonCurrentResourceStorage().m_inUseTextureUploadBuffers = m_textureUploadData.m_resourceStoredSinceLastSync;
m_textureUploadData.m_resourceStoredSinceLastSync.clear();
for (auto tmp : m_textureData.m_resourceStoredSinceLastSync)
{
std::get<2>(tmp)->Release();
m_textureData.m_getPos = std::get<0>(tmp);
}
getNonCurrentResourceStorage().m_inUseTexture2D = m_textureData.m_resourceStoredSinceLastSync;
m_textureData.m_resourceStoredSinceLastSync.clear();
m_frame->Flip(nullptr);
}
D3D12GSRender::ResourceStorage& D3D12GSRender::getCurrentResourceStorage()
{
return m_perFrameStorage[m_swapChain->GetCurrentBackBufferIndex()];
}
D3D12GSRender::ResourceStorage& D3D12GSRender::getNonCurrentResourceStorage()
{
return m_perFrameStorage[1 - m_swapChain->GetCurrentBackBufferIndex()];
}
void D3D12GSRender::WriteDepthBuffer()
{
@ -1059,7 +1084,7 @@ void D3D12GSRender::semaphorePGRAPHBackendRelease(u32 offset, u32 value)
m_readbackResources.m_putPos.store(heapOffset + sizeInByte);
check(
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_perFrameStorage.m_commandAllocator, nullptr, IID_PPV_ARGS(&convertCommandList))
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&convertCommandList))
);
D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc = {};
@ -1125,7 +1150,7 @@ void D3D12GSRender::semaphorePGRAPHBackendRelease(u32 offset, u32 value)
if (needTransfer)
{
check(
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_perFrameStorage.m_commandAllocator, nullptr, IID_PPV_ARGS(&downloadCommandList))
m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&downloadCommandList))
);
}

View file

@ -80,12 +80,18 @@ private:
struct ResourceStorage
{
HANDLE m_frameFinished;
ID3D12CommandAllocator *m_commandAllocator;
ID3D12CommandAllocator *m_downloadCommandAllocator;
std::list<ID3D12GraphicsCommandList *> m_inflightCommandList;
std::vector<ID3D12Resource *> m_inflightResources;
std::vector<std::tuple<size_t, size_t, ID3D12Resource *> > m_inUseConstantsBuffers;
std::vector<std::tuple<size_t, size_t, ID3D12Resource *> > m_inUseVertexIndexBuffers;
std::vector<std::tuple<size_t, size_t, ID3D12Resource *> > m_inUseTextureUploadBuffers;
std::vector<std::tuple<size_t, size_t, ID3D12Resource *> > m_inUseTexture2D;
// Constants storage
ID3D12DescriptorHeap *m_constantsBufferDescriptorsHeap;
size_t m_constantsBufferIndex;
@ -103,7 +109,9 @@ private:
void Release();
};
ResourceStorage m_perFrameStorage;
ResourceStorage m_perFrameStorage[2];
ResourceStorage &getCurrentResourceStorage();
ResourceStorage &getNonCurrentResourceStorage();
// Constants storage
DataHeap m_constantsData;

View file

@ -22,8 +22,8 @@ void D3D12GSRender::InitDrawBuffers()
u32 address_z = GetAddress(m_surface_offset_z, m_context_dma_z - 0xfeed0000);
ID3D12GraphicsCommandList *copycmdlist;
check(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_perFrameStorage.m_commandAllocator, nullptr, IID_PPV_ARGS(&copycmdlist)));
m_perFrameStorage.m_inflightCommandList.push_back(copycmdlist);
check(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_commandAllocator, nullptr, IID_PPV_ARGS(&copycmdlist)));
getCurrentResourceStorage().m_inflightCommandList.push_back(copycmdlist);
// Make previous RTTs sampleable
for (unsigned i = 0; i < 4; i++)

View file

@ -186,7 +186,7 @@ size_t D3D12GSRender::UploadTextures()
{
// Upload at each iteration to take advantage of overlapping transfer
ID3D12GraphicsCommandList *commandList;
check(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_perFrameStorage.m_textureUploadCommandAllocator, nullptr, IID_PPV_ARGS(&commandList)));
check(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, getCurrentResourceStorage().m_textureUploadCommandAllocator, nullptr, IID_PPV_ARGS(&commandList)));
size_t heightInBlocks = (m_textures[i].GetHeight() + blockHeightInPixel - 1) / blockHeightInPixel;
size_t widthInBlocks = (m_textures[i].GetWidth() + blockWidthInPixel - 1) / blockWidthInPixel;
@ -285,7 +285,7 @@ size_t D3D12GSRender::UploadTextures()
commandList->Close();
m_commandQueueGraphic->ExecuteCommandLists(1, (ID3D12CommandList**)&commandList);
m_perFrameStorage.m_inflightCommandList.push_back(commandList);
getCurrentResourceStorage().m_inflightCommandList.push_back(commandList);
m_texturesCache[texaddr] = vramTexture;
}
@ -389,8 +389,8 @@ size_t D3D12GSRender::UploadTextures()
break;
}
D3D12_CPU_DESCRIPTOR_HANDLE Handle = m_perFrameStorage.m_textureDescriptorsHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += (m_perFrameStorage.m_currentTextureIndex + usedTexture) * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
D3D12_CPU_DESCRIPTOR_HANDLE Handle = getCurrentResourceStorage().m_textureDescriptorsHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += (getCurrentResourceStorage().m_currentTextureIndex + usedTexture) * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
m_device->CreateShaderResourceView(vramTexture, &srvDesc, Handle);
// TODO : Correctly define sampler
@ -405,8 +405,8 @@ size_t D3D12GSRender::UploadTextures()
samplerDesc.BorderColor[4] = (FLOAT)m_textures[i].GetBorderColor();
samplerDesc.MinLOD = (FLOAT)(m_textures[i].GetMinLOD() >> 8);
samplerDesc.MaxLOD = (FLOAT)(m_textures[i].GetMaxLOD() >> 8);
Handle = m_perFrameStorage.m_samplerDescriptorHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += (m_perFrameStorage.m_currentTextureIndex + usedTexture) * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
Handle = getCurrentResourceStorage().m_samplerDescriptorHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += (getCurrentResourceStorage().m_currentTextureIndex + usedTexture) * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
m_device->CreateSampler(&samplerDesc, Handle);
usedTexture++;