diff --git a/rpcs3/Emu/RSX/D3D12/D3D12Buffer.cpp b/rpcs3/Emu/RSX/D3D12/D3D12Buffer.cpp index 617a2da8cd..80e1125dfd 100644 --- a/rpcs3/Emu/RSX/D3D12/D3D12Buffer.cpp +++ b/rpcs3/Emu/RSX/D3D12/D3D12Buffer.cpp @@ -229,7 +229,7 @@ D3D12_INDEX_BUFFER_VIEW D3D12GSRender::uploadIndexBuffers(bool indexed_draw) return indexBufferView; } -void D3D12GSRender::setScaleOffset() +void D3D12GSRender::setScaleOffset(size_t descriptorIndex) { float scaleOffsetMat[16] = { @@ -273,11 +273,11 @@ void D3D12GSRender::setScaleOffset() constantBufferViewDesc.BufferLocation = m_constantsData.m_heap->GetGPUVirtualAddress() + heapOffset; constantBufferViewDesc.SizeInBytes = (UINT)256; m_device->CreateConstantBufferView(&constantBufferViewDesc, - CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_scaleOffsetDescriptorHeap->GetCPUDescriptorHandleForHeapStart()) - .Offset((INT)getCurrentResourceStorage().m_currentScaleOffsetBufferIndex, g_descriptorStrideSRVCBVUAV)); + CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_descriptorsHeap->GetCPUDescriptorHandleForHeapStart()) + .Offset((INT)descriptorIndex, g_descriptorStrideSRVCBVUAV)); } -void D3D12GSRender::FillVertexShaderConstantsBuffer() +void D3D12GSRender::FillVertexShaderConstantsBuffer(size_t descriptorIndex) { for (const auto &entry : transform_constants) local_transform_constants[entry.first] = entry.second; @@ -305,11 +305,11 @@ void D3D12GSRender::FillVertexShaderConstantsBuffer() constantBufferViewDesc.BufferLocation = m_constantsData.m_heap->GetGPUVirtualAddress() + heapOffset; constantBufferViewDesc.SizeInBytes = (UINT)bufferSize; m_device->CreateConstantBufferView(&constantBufferViewDesc, - CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_constantsBufferDescriptorsHeap->GetCPUDescriptorHandleForHeapStart()) - .Offset((INT)getCurrentResourceStorage().m_constantsBufferIndex, g_descriptorStrideSRVCBVUAV)); + CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_descriptorsHeap->GetCPUDescriptorHandleForHeapStart()) + .Offset((INT)descriptorIndex, g_descriptorStrideSRVCBVUAV)); } -void D3D12GSRender::FillPixelShaderConstantsBuffer() +void D3D12GSRender::FillPixelShaderConstantsBuffer(size_t descriptorIndex) { // Get constant from fragment program const std::vector &fragmentOffset = m_cachePSO.getFragmentConstantOffsetsCache(&fragment_program); @@ -366,8 +366,8 @@ void D3D12GSRender::FillPixelShaderConstantsBuffer() constantBufferViewDesc.BufferLocation = m_constantsData.m_heap->GetGPUVirtualAddress() + heapOffset; constantBufferViewDesc.SizeInBytes = (UINT)bufferSize; m_device->CreateConstantBufferView(&constantBufferViewDesc, - CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_constantsBufferDescriptorsHeap->GetCPUDescriptorHandleForHeapStart()) - .Offset((INT)getCurrentResourceStorage().m_constantsBufferIndex, g_descriptorStrideSRVCBVUAV)); + CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_descriptorsHeap->GetCPUDescriptorHandleForHeapStart()) + .Offset((INT)descriptorIndex, g_descriptorStrideSRVCBVUAV)); } diff --git a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp index 6d517b1414..deb4614af7 100644 --- a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp +++ b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.cpp @@ -36,9 +36,7 @@ static void unloadD3D12FunctionPointers() void D3D12GSRender::ResourceStorage::Reset() { - m_constantsBufferIndex = 0; - m_currentScaleOffsetBufferIndex = 0; - m_currentTextureIndex = 0; + m_descriptorsHeapIndex = 0; m_currentSamplerIndex = 0; m_samplerDescriptorHeapIndex = 0; @@ -65,9 +63,7 @@ void D3D12GSRender::ResourceStorage::Init(ID3D12Device *device) ThrowIfFailed(m_commandList->Close()); D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc = { D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 10000, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE }; - ThrowIfFailed(device->CreateDescriptorHeap(&descriptorHeapDesc, IID_PPV_ARGS(&m_constantsBufferDescriptorsHeap))); - ThrowIfFailed(device->CreateDescriptorHeap(&descriptorHeapDesc, IID_PPV_ARGS(&m_scaleOffsetDescriptorHeap))); - ThrowIfFailed(device->CreateDescriptorHeap(&descriptorHeapDesc, IID_PPV_ARGS(&m_textureDescriptorsHeap))); + ThrowIfFailed(device->CreateDescriptorHeap(&descriptorHeapDesc, IID_PPV_ARGS(&m_descriptorsHeap))); D3D12_DESCRIPTOR_HEAP_DESC samplerHeapDesc = { D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER , 2048, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE }; ThrowIfFailed(device->CreateDescriptorHeap(&samplerHeapDesc, IID_PPV_ARGS(&m_samplerDescriptorHeap[0]))); @@ -205,16 +201,14 @@ D3D12GSRender::D3D12GSRender() // Samplers CD3DX12_DESCRIPTOR_RANGE(D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, textureCount, 0), }; - CD3DX12_ROOT_PARAMETER RP[4]; - RP[0].InitAsDescriptorTable(1, &descriptorRange[0]); - RP[1].InitAsDescriptorTable(1, &descriptorRange[1]); - RP[2].InitAsDescriptorTable(1, &descriptorRange[2]); - RP[3].InitAsDescriptorTable(1, &descriptorRange[3]); + CD3DX12_ROOT_PARAMETER RP[2]; + RP[0].InitAsDescriptorTable((textureCount > 0) ? 3 : 2, &descriptorRange[0]); + RP[1].InitAsDescriptorTable(1, &descriptorRange[3]); Microsoft::WRL::ComPtr rootSignatureBlob; Microsoft::WRL::ComPtr errorBlob; ThrowIfFailed(wrapD3D12SerializeRootSignature( - &CD3DX12_ROOT_SIGNATURE_DESC((textureCount > 0) ? 4 : 2, RP, 0, 0, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT), + &CD3DX12_ROOT_SIGNATURE_DESC((textureCount > 0) ? 2 : 1, RP, 0, 0, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT), D3D_ROOT_SIGNATURE_VERSION_1, &rootSignatureBlob, &errorBlob)); m_device->CreateRootSignature(0, @@ -442,26 +436,11 @@ void D3D12GSRender::end() std::chrono::time_point constantsDurationStart = std::chrono::system_clock::now(); + size_t currentDescriptorIndex = getCurrentResourceStorage().m_descriptorsHeapIndex; // Constants - setScaleOffset(); - getCurrentResourceStorage().m_commandList->SetDescriptorHeaps(1, getCurrentResourceStorage().m_scaleOffsetDescriptorHeap.GetAddressOf()); - getCurrentResourceStorage().m_commandList->SetGraphicsRootDescriptorTable(0, - CD3DX12_GPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_scaleOffsetDescriptorHeap->GetGPUDescriptorHandleForHeapStart()) - .Offset((INT)getCurrentResourceStorage().m_currentScaleOffsetBufferIndex, g_descriptorStrideSRVCBVUAV) - ); - getCurrentResourceStorage().m_currentScaleOffsetBufferIndex++; - - size_t currentBufferIndex = getCurrentResourceStorage().m_constantsBufferIndex; - FillVertexShaderConstantsBuffer(); - getCurrentResourceStorage().m_constantsBufferIndex++; - FillPixelShaderConstantsBuffer(); - getCurrentResourceStorage().m_constantsBufferIndex++; - - getCurrentResourceStorage().m_commandList->SetDescriptorHeaps(1, getCurrentResourceStorage().m_constantsBufferDescriptorsHeap.GetAddressOf()); - getCurrentResourceStorage().m_commandList->SetGraphicsRootDescriptorTable(1, - CD3DX12_GPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_constantsBufferDescriptorsHeap->GetGPUDescriptorHandleForHeapStart()) - .Offset((INT)currentBufferIndex, g_descriptorStrideSRVCBVUAV) - ); + setScaleOffset(currentDescriptorIndex); + FillVertexShaderConstantsBuffer(currentDescriptorIndex + 1); + FillPixelShaderConstantsBuffer(currentDescriptorIndex + 2); std::chrono::time_point constantsDurationEnd = std::chrono::system_clock::now(); m_timers.m_constantsDuration += std::chrono::duration_cast(constantsDurationEnd - constantsDurationStart).count(); @@ -471,7 +450,7 @@ void D3D12GSRender::end() std::chrono::time_point textureDurationStart = std::chrono::system_clock::now(); if (m_PSO->second > 0) { - size_t usedTexture = UploadTextures(getCurrentResourceStorage().m_commandList.Get()); + size_t usedTexture = UploadTextures(getCurrentResourceStorage().m_commandList.Get(), currentDescriptorIndex + 3); // Fill empty slots for (; usedTexture < m_PSO->second; usedTexture++) @@ -486,8 +465,8 @@ void D3D12GSRender::end() D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0, D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0); m_device->CreateShaderResourceView(m_dummyTexture, &srvDesc, - CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_textureDescriptorsHeap->GetCPUDescriptorHandleForHeapStart()) - .Offset((INT)getCurrentResourceStorage().m_currentTextureIndex + (INT)usedTexture, g_descriptorStrideSRVCBVUAV) + CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_descriptorsHeap->GetCPUDescriptorHandleForHeapStart()) + .Offset((INT)currentDescriptorIndex + 3 + (INT)usedTexture, g_descriptorStrideSRVCBVUAV) ); D3D12_SAMPLER_DESC samplerDesc = {}; @@ -501,21 +480,35 @@ void D3D12GSRender::end() ); } - getCurrentResourceStorage().m_commandList->SetDescriptorHeaps(1, getCurrentResourceStorage().m_textureDescriptorsHeap.GetAddressOf()); - getCurrentResourceStorage().m_commandList->SetGraphicsRootDescriptorTable(2, - CD3DX12_GPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_textureDescriptorsHeap->GetGPUDescriptorHandleForHeapStart()) - .Offset((INT)getCurrentResourceStorage().m_currentTextureIndex, g_descriptorStrideSRVCBVUAV) - ); + ID3D12DescriptorHeap *descriptors[] = + { + getCurrentResourceStorage().m_descriptorsHeap.Get(), + getCurrentResourceStorage().m_samplerDescriptorHeap[getCurrentResourceStorage().m_samplerDescriptorHeapIndex].Get(), + }; + getCurrentResourceStorage().m_commandList->SetDescriptorHeaps(2, descriptors); - getCurrentResourceStorage().m_commandList->SetDescriptorHeaps(1, getCurrentResourceStorage().m_samplerDescriptorHeap[getCurrentResourceStorage().m_samplerDescriptorHeapIndex].GetAddressOf()); - getCurrentResourceStorage().m_commandList->SetGraphicsRootDescriptorTable(3, + getCurrentResourceStorage().m_commandList->SetGraphicsRootDescriptorTable(0, + CD3DX12_GPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_descriptorsHeap->GetGPUDescriptorHandleForHeapStart()) + .Offset((INT)currentDescriptorIndex, g_descriptorStrideSRVCBVUAV) + ); + getCurrentResourceStorage().m_commandList->SetGraphicsRootDescriptorTable(1, CD3DX12_GPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_samplerDescriptorHeap[getCurrentResourceStorage().m_samplerDescriptorHeapIndex]->GetGPUDescriptorHandleForHeapStart()) .Offset((INT)getCurrentResourceStorage().m_currentSamplerIndex, g_descriptorStrideSamplers) ); - getCurrentResourceStorage().m_currentTextureIndex += usedTexture; getCurrentResourceStorage().m_currentSamplerIndex += usedTexture; + getCurrentResourceStorage().m_descriptorsHeapIndex += usedTexture + 3; } + else + { + getCurrentResourceStorage().m_commandList->SetDescriptorHeaps(1, getCurrentResourceStorage().m_descriptorsHeap.GetAddressOf()); + getCurrentResourceStorage().m_commandList->SetGraphicsRootDescriptorTable(0, + CD3DX12_GPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_descriptorsHeap->GetGPUDescriptorHandleForHeapStart()) + .Offset((INT)currentDescriptorIndex, g_descriptorStrideSRVCBVUAV) + ); + getCurrentResourceStorage().m_descriptorsHeapIndex += 3; + } + std::chrono::time_point textureDurationEnd = std::chrono::system_clock::now(); m_timers.m_textureDuration += std::chrono::duration_cast(textureDurationEnd - textureDurationStart).count(); diff --git a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.h b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.h index 0d1018c419..f5becb50ee 100644 --- a/rpcs3/Emu/RSX/D3D12/D3D12GSRender.h +++ b/rpcs3/Emu/RSX/D3D12/D3D12GSRender.h @@ -374,15 +374,11 @@ private: ComPtr m_commandAllocator; ComPtr m_commandList; - // Constants storage - ComPtr m_constantsBufferDescriptorsHeap; - size_t m_constantsBufferIndex; - ComPtr m_scaleOffsetDescriptorHeap; - size_t m_currentScaleOffsetBufferIndex; + // Descriptor heap + ComPtr m_descriptorsHeap; + size_t m_descriptorsHeapIndex; - // Texture storage - ComPtr m_textureDescriptorsHeap; - size_t m_currentTextureIndex; + // Sampler heap ComPtr m_samplerDescriptorHeap[2]; size_t m_samplerDescriptorHeapIndex; size_t m_currentSamplerIndex; @@ -481,16 +477,16 @@ private: D3D12_INDEX_BUFFER_VIEW uploadIndexBuffers(bool indexed_draw = false); - void setScaleOffset(); - void FillVertexShaderConstantsBuffer(); - void FillPixelShaderConstantsBuffer(); + void setScaleOffset(size_t descriptorIndex); + void FillVertexShaderConstantsBuffer(size_t descriptorIndex); + void FillPixelShaderConstantsBuffer(size_t descriptorIndex); /** * Fetch all textures recorded in the state in the render target cache and in the texture cache. * If a texture is not cached, populate cmdlist with uploads command. * Create necessary resource view/sampler descriptors in the per frame storage struct. * returns the number of texture uploaded. */ - size_t UploadTextures(ID3D12GraphicsCommandList *cmdlist); + size_t UploadTextures(ID3D12GraphicsCommandList *cmdlist, size_t descriptorIndex); /** * Creates render target if necessary. diff --git a/rpcs3/Emu/RSX/D3D12/D3D12Texture.cpp b/rpcs3/Emu/RSX/D3D12/D3D12Texture.cpp index dcbf259e4e..be00f3f838 100644 --- a/rpcs3/Emu/RSX/D3D12/D3D12Texture.cpp +++ b/rpcs3/Emu/RSX/D3D12/D3D12Texture.cpp @@ -284,7 +284,7 @@ size_t getTextureSize(const rsx::texture &texture) } } -size_t D3D12GSRender::UploadTextures(ID3D12GraphicsCommandList *cmdlist) +size_t D3D12GSRender::UploadTextures(ID3D12GraphicsCommandList *cmdlist, size_t descriptorIndex) { size_t usedTexture = 0; @@ -452,7 +452,8 @@ size_t D3D12GSRender::UploadTextures(ID3D12GraphicsCommandList *cmdlist) } m_device->CreateShaderResourceView(vramTexture, &srvDesc, - CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_textureDescriptorsHeap->GetCPUDescriptorHandleForHeapStart()).Offset((UINT)getCurrentResourceStorage().m_currentTextureIndex + (UINT)usedTexture, g_descriptorStrideSRVCBVUAV)); + CD3DX12_CPU_DESCRIPTOR_HANDLE(getCurrentResourceStorage().m_descriptorsHeap->GetCPUDescriptorHandleForHeapStart()) + .Offset((UINT)descriptorIndex + (UINT)usedTexture, g_descriptorStrideSRVCBVUAV)); if (getCurrentResourceStorage().m_currentSamplerIndex + 16 > 2048) {