From 1d909ec7a422b261588e860486b01db85e94fa6e Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 21 Feb 2016 17:18:02 +1000 Subject: [PATCH] D3D12: Implement non-blocking EFB access when EFB has not been modified --- Source/Core/VideoBackends/D3D12/D3DUtil.cpp | 6 + .../D3D12/FramebufferManager.cpp | 253 +++++++++++++++--- .../VideoBackends/D3D12/FramebufferManager.h | 29 +- Source/Core/VideoBackends/D3D12/Render.cpp | 218 +++------------ 4 files changed, 279 insertions(+), 227 deletions(-) diff --git a/Source/Core/VideoBackends/D3D12/D3DUtil.cpp b/Source/Core/VideoBackends/D3D12/D3DUtil.cpp index abf1088ac4..a1a7c512da 100644 --- a/Source/Core/VideoBackends/D3D12/D3DUtil.cpp +++ b/Source/Core/VideoBackends/D3D12/D3DUtil.cpp @@ -16,6 +16,7 @@ #include "VideoBackends/D3D12/D3DTexture.h" #include "VideoBackends/D3D12/D3DUtil.h" +#include "VideoBackends/D3D12/FramebufferManager.h" #include "VideoBackends/D3D12/Render.h" #include "VideoBackends/D3D12/StaticShaderCache.h" @@ -964,6 +965,11 @@ void DrawEFBPokeQuads(EFBAccessType type, InitColVertex(&vertex[3], x1, y2, z, col); InitColVertex(&vertex[4], x2, y1, z, col); InitColVertex(&vertex[5], x2, y2, z, col); + + if (type == POKE_COLOR) + FramebufferManager::UpdateEFBColorAccessCopy(point->x, point->y, col); + else if (type == POKE_Z) + FramebufferManager::UpdateEFBDepthAccessCopy(point->x, point->y, z); } // Issue the draw diff --git a/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp b/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp index 936266f609..9b7bebf608 100644 --- a/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp +++ b/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp @@ -22,11 +22,7 @@ unsigned int FramebufferManager::m_target_width; unsigned int FramebufferManager::m_target_height; D3DTexture2D*& FramebufferManager::GetEFBColorTexture() { return m_efb.color_tex; } -ID3D12Resource*& FramebufferManager::GetEFBColorStagingBuffer() { return m_efb.color_staging_buf; } - D3DTexture2D*& FramebufferManager::GetEFBDepthTexture() { return m_efb.depth_tex; } -D3DTexture2D*& FramebufferManager::GetEFBDepthReadTexture() { return m_efb.depth_read_texture; } -ID3D12Resource*& FramebufferManager::GetEFBDepthStagingBuffer() { return m_efb.depth_staging_buf; } D3DTexture2D*& FramebufferManager::GetEFBColorTempTexture() { return m_efb.color_temp_tex; } @@ -105,11 +101,6 @@ FramebufferManager::FramebufferManager() SAFE_RELEASE(buf12); D3D::SetDebugObjectName12(m_efb.color_temp_tex->GetTex12(), "EFB color temp texture"); - // AccessEFB - Sysmem buffer used to retrieve the pixel data from color_tex - texdesc12 = CD3DX12_RESOURCE_DESC::Buffer(64 * 1024); - CheckHR(D3D::device12->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), D3D12_HEAP_FLAG_NONE, &texdesc12, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&m_efb.color_staging_buf))); - CHECK(hr == S_OK, "create EFB color staging buffer (hr=%#x)", hr); - // EFB depth buffer - primary depth buffer texdesc12 = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R32_TYPELESS, m_target_width, m_target_height, m_efb.slices, 1, sample_desc.Count, sample_desc.Quality, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL); CheckHR(D3D::device12->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE, &texdesc12, D3D12_RESOURCE_STATE_COMMON, &optimized_clear_valueDSV, IID_PPV_ARGS(&buf12))); @@ -118,24 +109,6 @@ FramebufferManager::FramebufferManager() SAFE_RELEASE(buf12); D3D::SetDebugObjectName12(m_efb.depth_tex->GetTex12(), "EFB depth texture"); - // Render buffer for AccessEFB (depth data) - texdesc12 = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R32_FLOAT, 1, 1, m_efb.slices, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); - optimized_clear_valueRTV.Format = DXGI_FORMAT_R32_FLOAT; - hr = D3D::device12->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE, &texdesc12, D3D12_RESOURCE_STATE_COMMON, &optimized_clear_valueRTV, IID_PPV_ARGS(&buf12)); - CHECK(hr == S_OK, "create EFB depth read texture (hr=%#x)", hr); - - m_efb.depth_read_texture = new D3DTexture2D(buf12, D3D11_BIND_RENDER_TARGET, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, false, D3D12_RESOURCE_STATE_COMMON); - - SAFE_RELEASE(buf12); - D3D::SetDebugObjectName12(m_efb.depth_read_texture->GetTex12(), "EFB depth read texture (used in Renderer::AccessEFB)"); - - // AccessEFB - Sysmem buffer used to retrieve the pixel data from depth_read_texture - texdesc12 = CD3DX12_RESOURCE_DESC::Buffer(64 * 1024); - hr = D3D::device12->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), D3D12_HEAP_FLAG_NONE, &texdesc12, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&m_efb.depth_staging_buf)); - CHECK(hr == S_OK, "create EFB depth staging buffer (hr=%#x)", hr); - - D3D::SetDebugObjectName12(m_efb.depth_staging_buf, "EFB depth staging texture (used for Renderer::AccessEFB)"); - if (g_ActiveConfig.iMultisamples > 1) { // Framebuffer resolve textures (color+depth) @@ -159,6 +132,8 @@ FramebufferManager::FramebufferManager() m_efb.resolved_depth_tex = nullptr; } + InitializeEFBAccessCopies(); + s_xfbEncoder.Init(); } @@ -166,17 +141,12 @@ FramebufferManager::~FramebufferManager() { s_xfbEncoder.Shutdown(); + DestroyEFBAccessCopies(); + SAFE_RELEASE(m_efb.color_tex); - SAFE_RELEASE(m_efb.color_temp_tex); - - D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(m_efb.color_staging_buf); - - SAFE_RELEASE(m_efb.resolved_color_tex); SAFE_RELEASE(m_efb.depth_tex); - - D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(m_efb.depth_staging_buf); - - SAFE_RELEASE(m_efb.depth_read_texture); + SAFE_RELEASE(m_efb.color_temp_tex); + SAFE_RELEASE(m_efb.resolved_color_tex); SAFE_RELEASE(m_efb.resolved_depth_tex); } @@ -235,6 +205,217 @@ void FramebufferManager::ResolveDepthTexture() g_renderer->RestoreAPIState(); } +u32 FramebufferManager::ReadEFBColorAccessCopy(u32 x, u32 y) +{ + if (!m_efb.color_access_readback_map) + MapEFBColorAccessCopy(); + + u32 color; + size_t buffer_offset = y * m_efb.color_access_readback_pitch + x * sizeof(u32); + memcpy(&color, &m_efb.color_access_readback_map[buffer_offset], sizeof(color)); + return color; +} + +float FramebufferManager::ReadEFBDepthAccessCopy(u32 x, u32 y) +{ + if (!m_efb.depth_access_readback_map) + MapEFBDepthAccessCopy(); + + float depth; + size_t buffer_offset = y * m_efb.depth_access_readback_pitch + x * sizeof(float); + memcpy(&depth, &m_efb.depth_access_readback_map[buffer_offset], sizeof(depth)); + return depth; +} + +void FramebufferManager::UpdateEFBColorAccessCopy(u32 x, u32 y, u32 color) +{ + if (!m_efb.color_access_readback_map) + return; + + size_t buffer_offset = y * m_efb.color_access_readback_pitch + x * sizeof(u32); + memcpy(&m_efb.color_access_readback_map[buffer_offset], &color, sizeof(color)); +} + +void FramebufferManager::UpdateEFBDepthAccessCopy(u32 x, u32 y, float depth) +{ + if (!m_efb.depth_access_readback_map) + return; + + size_t buffer_offset = y * m_efb.depth_access_readback_pitch + x * sizeof(float); + memcpy(&m_efb.depth_access_readback_map[buffer_offset], &depth, sizeof(depth)); +} + +void FramebufferManager::InitializeEFBAccessCopies() +{ + D3D12_CLEAR_VALUE optimized_color_clear_value = { DXGI_FORMAT_R8G8B8A8_UNORM, { 0.0f, 0.0f, 0.0f, 1.0f } }; + D3D12_CLEAR_VALUE optimized_depth_clear_value = { DXGI_FORMAT_R32_FLOAT, { 1.0f } }; + CD3DX12_RESOURCE_DESC texdesc12; + ID3D12Resource* buf12; + HRESULT hr; + + // EFB access - color resize buffer + texdesc12 = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, EFB_WIDTH, EFB_HEIGHT, 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, D3D12_TEXTURE_LAYOUT_UNKNOWN, 0); + hr = D3D::device12->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE, &texdesc12, D3D12_RESOURCE_STATE_COMMON, &optimized_color_clear_value, IID_PPV_ARGS(&buf12)); + CHECK(hr == S_OK, "create EFB access color resize buffer (hr=%#x)", hr); + m_efb.color_access_resize_tex = new D3DTexture2D(buf12, D3D11_BIND_RENDER_TARGET, DXGI_FORMAT_R8G8B8A8_UNORM); + D3D::SetDebugObjectName12(m_efb.color_access_resize_tex->GetTex12(), "EFB access color resize buffer"); + buf12->Release(); + + // EFB access - color staging/readback buffer + m_efb.color_access_readback_pitch = D3D::AlignValue(EFB_WIDTH * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); + texdesc12 = CD3DX12_RESOURCE_DESC::Buffer(m_efb.color_access_readback_pitch * EFB_HEIGHT); + hr = D3D::device12->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), D3D12_HEAP_FLAG_NONE, &texdesc12, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&m_efb.color_access_readback_buffer)); + D3D::SetDebugObjectName12(m_efb.color_access_readback_buffer, "EFB access color readback buffer"); + + // EFB access - depth resize buffer + texdesc12 = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R32_FLOAT, EFB_WIDTH, EFB_HEIGHT, 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, D3D12_TEXTURE_LAYOUT_UNKNOWN, 0); + hr = D3D::device12->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE, &texdesc12, D3D12_RESOURCE_STATE_COMMON, &optimized_depth_clear_value, IID_PPV_ARGS(&buf12)); + CHECK(hr == S_OK, "create EFB access depth resize buffer (hr=%#x)", hr); + m_efb.depth_access_resize_tex = new D3DTexture2D(buf12, D3D11_BIND_RENDER_TARGET, DXGI_FORMAT_R32_FLOAT); + D3D::SetDebugObjectName12(m_efb.color_access_resize_tex->GetTex12(), "EFB access depth resize buffer"); + buf12->Release(); + + // EFB access - depth staging/readback buffer + m_efb.depth_access_readback_pitch = D3D::AlignValue(EFB_WIDTH * sizeof(float), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); + texdesc12 = CD3DX12_RESOURCE_DESC::Buffer(m_efb.depth_access_readback_pitch * EFB_HEIGHT); + hr = D3D::device12->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), D3D12_HEAP_FLAG_NONE, &texdesc12, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&m_efb.depth_access_readback_buffer)); + D3D::SetDebugObjectName12(m_efb.color_access_readback_buffer, "EFB access depth readback buffer"); +} + +void FramebufferManager::MapEFBColorAccessCopy() +{ + D3D::command_list_mgr->CPUAccessNotify(); + + ID3D12Resource* src_resource; + if (m_target_width != EFB_WIDTH || m_target_height != EFB_HEIGHT || g_ActiveConfig.iMultisamples > 1) + { + // for non-1xIR or multisampled cases, we need to copy to an intermediate texture first + m_efb.color_access_resize_tex->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_RENDER_TARGET); + + D3D12_VIEWPORT vp12 = { 0, 0, EFB_WIDTH, EFB_HEIGHT, D3D12_MIN_DEPTH, D3D12_MAX_DEPTH }; + D3D::current_command_list->RSSetViewports(1, &vp12); + D3D::current_command_list->OMSetRenderTargets(1, &m_efb.color_access_resize_tex->GetRTV12(), FALSE, nullptr); + D3D::SetPointCopySampler(); + + CD3DX12_RECT src_rect(0, 0, m_target_width, m_target_height); + D3D::DrawShadedTexQuad(m_efb.color_tex, &src_rect, m_target_width, m_target_height, + StaticShaderCache::GetColorCopyPixelShader(true), + StaticShaderCache::GetSimpleVertexShader(), + StaticShaderCache::GetSimpleVertexShaderInputLayout(), + {}, 1.0f, 0, DXGI_FORMAT_R8G8B8A8_UNORM, false, false); + + m_efb.color_access_resize_tex->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_COPY_SOURCE); + src_resource = m_efb.color_access_resize_tex->GetTex12(); + } + else + { + // Can source the EFB buffer + m_efb.color_tex->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_COPY_SOURCE); + src_resource = m_efb.color_tex->GetTex12(); + } + + // Copy to staging resource + D3D12_PLACED_SUBRESOURCE_FOOTPRINT dst_footprint = { 0, { DXGI_FORMAT_R8G8B8A8_UNORM, EFB_WIDTH, EFB_HEIGHT, 1, m_efb.color_access_readback_pitch } }; + CD3DX12_TEXTURE_COPY_LOCATION dst_location(m_efb.color_access_readback_buffer, dst_footprint); + CD3DX12_TEXTURE_COPY_LOCATION src_location(src_resource, 0); + D3D::current_command_list->CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, nullptr); + + // Block until completion + D3D::command_list_mgr->ExecuteQueuedWork(true); + + // Restore EFB resource state if it was sourced from here + if (src_resource == m_efb.color_tex->GetTex12()) + m_efb.color_tex->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_RENDER_TARGET); + + // Restore state after resetting command list + D3D::current_command_list->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTexture()->GetRTV12(), FALSE, &FramebufferManager::GetEFBDepthTexture()->GetDSV12()); + g_renderer->RestoreAPIState(); + + // Resource copy has finished, so safe to map now + m_efb.color_access_readback_buffer->Map(0, nullptr, reinterpret_cast(&m_efb.color_access_readback_map)); +} + +void FramebufferManager::MapEFBDepthAccessCopy() +{ + D3D::command_list_mgr->CPUAccessNotify(); + + ID3D12Resource* src_resource; + if (m_target_width != EFB_WIDTH || m_target_height != EFB_HEIGHT || g_ActiveConfig.iMultisamples > 1) + { + // for non-1xIR or multisampled cases, we need to copy to an intermediate texture first + m_efb.depth_access_resize_tex->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_RENDER_TARGET); + + D3D12_VIEWPORT vp12 = { 0, 0, EFB_WIDTH, EFB_HEIGHT, D3D12_MIN_DEPTH, D3D12_MAX_DEPTH }; + D3D::current_command_list->RSSetViewports(1, &vp12); + D3D::current_command_list->OMSetRenderTargets(1, &m_efb.depth_access_resize_tex->GetRTV12(), FALSE, nullptr); + D3D::SetPointCopySampler(); + + CD3DX12_RECT src_rect(0, 0, m_target_width, m_target_height); + D3D::DrawShadedTexQuad(m_efb.depth_tex, &src_rect, m_target_width, m_target_height, + (g_ActiveConfig.iMultisamples > 1) ? StaticShaderCache::GetDepthResolveToColorPixelShader() : StaticShaderCache::GetColorCopyPixelShader(false), + StaticShaderCache::GetSimpleVertexShader(), + StaticShaderCache::GetSimpleVertexShaderInputLayout(), + {}, 1.0f, 0, DXGI_FORMAT_R32_FLOAT, false, false); + + m_efb.depth_access_resize_tex->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_COPY_SOURCE); + src_resource = m_efb.depth_access_resize_tex->GetTex12(); + } + else + { + // Can source the EFB buffer + m_efb.depth_tex->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_COPY_SOURCE); + src_resource = m_efb.depth_tex->GetTex12(); + } + + // Copy to staging resource + D3D12_PLACED_SUBRESOURCE_FOOTPRINT dst_footprint = { 0,{ DXGI_FORMAT_R32_FLOAT, EFB_WIDTH, EFB_HEIGHT, 1, m_efb.depth_access_readback_pitch } }; + CD3DX12_TEXTURE_COPY_LOCATION dst_location(m_efb.depth_access_readback_buffer, dst_footprint); + CD3DX12_TEXTURE_COPY_LOCATION src_location(src_resource, 0); + D3D::current_command_list->CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, nullptr); + + // Block until completion + D3D::command_list_mgr->ExecuteQueuedWork(true); + + // Restore EFB resource state if it was sourced from here + if (src_resource == m_efb.depth_tex->GetTex12()) + m_efb.depth_tex->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_DEPTH_WRITE); + + // Restore state after resetting command list + D3D::current_command_list->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTexture()->GetRTV12(), FALSE, &FramebufferManager::GetEFBDepthTexture()->GetDSV12()); + g_renderer->RestoreAPIState(); + + // Resource copy has finished, so safe to map now + m_efb.depth_access_readback_buffer->Map(0, nullptr, reinterpret_cast(&m_efb.depth_access_readback_map)); +} + +void FramebufferManager::InvalidateEFBAccessCopies() +{ + if (m_efb.color_access_readback_map) + { + m_efb.color_access_readback_buffer->Unmap(0, nullptr); + m_efb.color_access_readback_map = nullptr; + } + + if (m_efb.depth_access_readback_map) + { + m_efb.depth_access_readback_buffer->Unmap(0, nullptr); + m_efb.depth_access_readback_map = nullptr; + } +} + +void FramebufferManager::DestroyEFBAccessCopies() +{ + InvalidateEFBAccessCopies(); + + SAFE_RELEASE(m_efb.color_access_resize_tex); + D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(m_efb.color_access_readback_buffer); + m_efb.color_access_readback_buffer = nullptr; + + SAFE_RELEASE(m_efb.depth_access_resize_tex); + D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(m_efb.depth_access_readback_buffer); + m_efb.depth_access_readback_buffer = nullptr; +} + void XFBSource::DecodeToTexture(u32 xfbAddr, u32 fbWidth, u32 fbHeight) { // DX12's XFB decoder does not use this function. diff --git a/Source/Core/VideoBackends/D3D12/FramebufferManager.h b/Source/Core/VideoBackends/D3D12/FramebufferManager.h index 78242197b2..13e9ae205c 100644 --- a/Source/Core/VideoBackends/D3D12/FramebufferManager.h +++ b/Source/Core/VideoBackends/D3D12/FramebufferManager.h @@ -61,11 +61,7 @@ public: ~FramebufferManager(); static D3DTexture2D*& GetEFBColorTexture(); - static ID3D12Resource*& GetEFBColorStagingBuffer(); - static D3DTexture2D*& GetEFBDepthTexture(); - static D3DTexture2D*& GetEFBDepthReadTexture(); - static ID3D12Resource*& GetEFBDepthStagingBuffer(); static D3DTexture2D*& GetResolvedEFBColorTexture(); static D3DTexture2D*& GetResolvedEFBDepthTexture(); @@ -74,6 +70,17 @@ public: static void ResolveDepthTexture(); + // Access EFB from CPU + static u32 ReadEFBColorAccessCopy(u32 x, u32 y); + static float ReadEFBDepthAccessCopy(u32 x, u32 y); + static void UpdateEFBColorAccessCopy(u32 x, u32 y, u32 color); + static void UpdateEFBDepthAccessCopy(u32 x, u32 y, float depth); + static void InitializeEFBAccessCopies(); + static void MapEFBColorAccessCopy(); + static void MapEFBDepthAccessCopy(); + static void InvalidateEFBAccessCopies(); + static void DestroyEFBAccessCopies(); + private: std::unique_ptr CreateXFBSource(unsigned int target_width, unsigned int target_height, unsigned int layers) override; void GetTargetSize(unsigned int* width, unsigned int* height) override; @@ -83,18 +90,24 @@ private: static struct Efb { D3DTexture2D* color_tex; - ID3D12Resource* color_staging_buf; D3DTexture2D* depth_tex; - ID3D12Resource* depth_staging_buf; - - D3DTexture2D* depth_read_texture; D3DTexture2D* color_temp_tex; D3DTexture2D* resolved_color_tex; D3DTexture2D* resolved_depth_tex; + D3DTexture2D* color_access_resize_tex; + ID3D12Resource* color_access_readback_buffer; + u8* color_access_readback_map; + u32 color_access_readback_pitch; + + D3DTexture2D* depth_access_resize_tex; + ID3D12Resource* depth_access_readback_buffer; + u8* depth_access_readback_map; + u32 depth_access_readback_pitch; + int slices; } m_efb; diff --git a/Source/Core/VideoBackends/D3D12/Render.cpp b/Source/Core/VideoBackends/D3D12/Render.cpp index e3fc160c9d..4f763a68cb 100644 --- a/Source/Core/VideoBackends/D3D12/Render.cpp +++ b/Source/Core/VideoBackends/D3D12/Render.cpp @@ -51,8 +51,6 @@ static bool s_last_xfb_mode = false; static Television s_television; -static ID3D12Resource* s_access_efb_constant_buffer = nullptr; - enum CLEAR_BLEND_DESC { CLEAR_BLEND_DESC_ALL_CHANNELS_ENABLED = 0, @@ -110,25 +108,6 @@ static void SetupDeviceObjects() g_framebuffer_manager = std::make_unique(); - float colmat[20] = { 0.0f }; - colmat[0] = colmat[5] = colmat[10] = 1.0f; - - CheckHR( - D3D::device12->CreateCommittedResource( - &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), - D3D12_HEAP_FLAG_NONE, - &CD3DX12_RESOURCE_DESC::Buffer(sizeof(colmat)), - D3D12_RESOURCE_STATE_GENERIC_READ, - nullptr, - IID_PPV_ARGS(&s_access_efb_constant_buffer) - ) - ); - - // Copy inital data to access_efb_cbuf12. - void* access_efb_constant_buffer_data = nullptr; - CheckHR(s_access_efb_constant_buffer->Map(0, nullptr, &access_efb_constant_buffer_data)); - memcpy(access_efb_constant_buffer_data, colmat, sizeof(colmat)); - D3D12_DEPTH_STENCIL_DESC depth_desc; depth_desc.DepthEnable = FALSE; depth_desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO; @@ -197,9 +176,6 @@ static void TeardownDeviceObjects() s_screenshot_texture = nullptr; } - D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(s_access_efb_constant_buffer); - s_access_efb_constant_buffer = nullptr; - s_television.Shutdown(); gx_state_cache.Clear(); @@ -394,192 +370,60 @@ void Renderer::SetColorMask() // - GX_PokeZMode (TODO) u32 Renderer::AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data) { - // EXISTINGD3D11TODO: This function currently is broken if anti-aliasing is enabled - - // Convert EFB dimensions to the ones of our render target - EFBRectangle efb_pixel_rc; - efb_pixel_rc.left = x; - efb_pixel_rc.top = y; - efb_pixel_rc.right = x + 1; - efb_pixel_rc.bottom = y + 1; - TargetRectangle target_pixel_rc = Renderer::ConvertEFBRectangle(efb_pixel_rc); - - // Take the mean of the resulting dimensions; TODO: Don't use the center pixel, compute the average color instead - D3D12_RECT rect_to_lock; - if (type == PEEK_COLOR || type == PEEK_Z) + if (type == PEEK_COLOR) { - rect_to_lock.left = (target_pixel_rc.left + target_pixel_rc.right) / 2; - rect_to_lock.top = (target_pixel_rc.top + target_pixel_rc.bottom) / 2; - rect_to_lock.right = rect_to_lock.left + 1; - rect_to_lock.bottom = rect_to_lock.top + 1; - } - else - { - rect_to_lock.left = target_pixel_rc.left; - rect_to_lock.right = target_pixel_rc.right; - rect_to_lock.top = target_pixel_rc.top; - rect_to_lock.bottom = target_pixel_rc.bottom; - } + u32 color = FramebufferManager::ReadEFBColorAccessCopy(x, y); - if (type == PEEK_Z) - { - D3D::command_list_mgr->CPUAccessNotify(); - - // depth buffers can only be completely CopySubresourceRegion'ed, so we're using DrawShadedTexQuad instead - // D3D12TODO: Is above statement true on D3D12? - D3D12_VIEWPORT vp12 = { 0.f, 0.f, 1.f, 1.f, D3D12_MIN_DEPTH, D3D12_MAX_DEPTH }; - D3D::current_command_list->RSSetViewports(1, &vp12); - - D3D::current_command_list->SetGraphicsRootConstantBufferView(DESCRIPTOR_TABLE_PS_CBVONE, s_access_efb_constant_buffer->GetGPUVirtualAddress()); - D3D::command_list_mgr->SetCommandListDirtyState(COMMAND_LIST_STATE_PS_CBV, true); - - FramebufferManager::GetEFBDepthReadTexture()->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_RENDER_TARGET); - D3D::current_command_list->OMSetRenderTargets(1, &FramebufferManager::GetEFBDepthReadTexture()->GetRTV12(), FALSE, nullptr); - - D3D::SetPointCopySampler(); - - D3D::DrawShadedTexQuad( - FramebufferManager::GetEFBDepthTexture(), - &rect_to_lock, - Renderer::GetTargetWidth(), - Renderer::GetTargetHeight(), - StaticShaderCache::GetColorCopyPixelShader(true), - StaticShaderCache::GetSimpleVertexShader(), - StaticShaderCache::GetSimpleVertexShaderInputLayout(), - D3D12_SHADER_BYTECODE(), - 1.0f, - 0, - DXGI_FORMAT_R32_FLOAT, - false, - FramebufferManager::GetEFBDepthReadTexture()->GetMultisampled() - ); - - // copy to system memory - D3D12_BOX src_box = CD3DX12_BOX(0, 0, 0, 1, 1, 1); - ID3D12Resource* readback_buffer = FramebufferManager::GetEFBDepthStagingBuffer(); - - D3D12_TEXTURE_COPY_LOCATION dst_location = {}; - dst_location.pResource = readback_buffer; - dst_location.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; - dst_location.PlacedFootprint.Offset = 0; - dst_location.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R32_FLOAT; - dst_location.PlacedFootprint.Footprint.Width = 1; - dst_location.PlacedFootprint.Footprint.Height = 1; - dst_location.PlacedFootprint.Footprint.Depth = 1; - dst_location.PlacedFootprint.Footprint.RowPitch = D3D::AlignValue(dst_location.PlacedFootprint.Footprint.Width * 4, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); - - D3D12_TEXTURE_COPY_LOCATION src_location = {}; - src_location.pResource = FramebufferManager::GetEFBDepthReadTexture()->GetTex12(); - src_location.SubresourceIndex = 0; - src_location.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; - - FramebufferManager::GetEFBDepthReadTexture()->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_COPY_SOURCE); - D3D::current_command_list->CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, &src_box); - - // Need to wait for the CPU to complete the copy (and all prior operations) before we can read it on the CPU. - D3D::command_list_mgr->ExecuteQueuedWork(true); - - FramebufferManager::GetEFBColorTexture()->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_RENDER_TARGET); - FramebufferManager::GetEFBDepthTexture()->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_DEPTH_WRITE ); - D3D::current_command_list->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTexture()->GetRTV12(), FALSE, &FramebufferManager::GetEFBDepthTexture()->GetDSV12()); - - // Restores proper viewport/scissor settings. - g_renderer->RestoreAPIState(); - - // read the data from system memory - void* readback_buffer_data = nullptr; - CheckHR(readback_buffer->Map(0, nullptr, &readback_buffer_data)); - - // depth buffer is inverted in the d3d backend - float val = 1.0f - reinterpret_cast(readback_buffer_data)[0]; - u32 ret = 0; - - if (bpmem.zcontrol.pixel_format == PEControl::RGB565_Z16) - { - // if Z is in 16 bit format you must return a 16 bit integer - ret = MathUtil::Clamp(static_cast(val * 65536.0f), 0, 0xFFFF); - } - else - { - ret = MathUtil::Clamp(static_cast(val * 16777216.0f), 0, 0xFFFFFF); - } - - // EXISTINGD3D11TODO: in RE0 this value is often off by one in Video_DX9 (where this code is derived from), which causes lighting to disappear - return ret; - } - else if (type == PEEK_COLOR) - { - D3D::command_list_mgr->CPUAccessNotify(); - - ID3D12Resource* readback_buffer = FramebufferManager::GetEFBColorStagingBuffer(); - - D3D12_BOX src_box = CD3DX12_BOX(rect_to_lock.left, rect_to_lock.top, 0, rect_to_lock.right, rect_to_lock.bottom, 1); - - D3D12_TEXTURE_COPY_LOCATION dst_location = {}; - dst_location.pResource = readback_buffer; - dst_location.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; - dst_location.PlacedFootprint.Offset = 0; - dst_location.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - dst_location.PlacedFootprint.Footprint.Width = 1; - dst_location.PlacedFootprint.Footprint.Height = 1; - dst_location.PlacedFootprint.Footprint.Depth = 1; - dst_location.PlacedFootprint.Footprint.RowPitch = D3D::AlignValue(dst_location.PlacedFootprint.Footprint.Width * 4, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); - - D3D12_TEXTURE_COPY_LOCATION src_location = {}; - src_location.pResource = FramebufferManager::GetResolvedEFBColorTexture()->GetTex12(); - src_location.SubresourceIndex = 0; - src_location.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; - - FramebufferManager::GetResolvedEFBColorTexture()->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_COPY_SOURCE); - D3D::current_command_list->CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, &src_box); - - // Need to wait for the CPU to complete the copy (and all prior operations) before we can read it on the CPU. - D3D::command_list_mgr->ExecuteQueuedWork(true); - - FramebufferManager::GetEFBColorTexture()->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_RENDER_TARGET); - FramebufferManager::GetEFBDepthTexture()->TransitionToResourceState(D3D::current_command_list, D3D12_RESOURCE_STATE_DEPTH_WRITE); - D3D::current_command_list->OMSetRenderTargets(1, &FramebufferManager::GetEFBColorTexture()->GetRTV12(), FALSE, &FramebufferManager::GetEFBDepthTexture()->GetDSV12()); - - // Restores proper viewport/scissor settings. - g_renderer->RestoreAPIState(); - - // read the data from system memory - void* readback_buffer_data = nullptr; - CheckHR(readback_buffer->Map(0, nullptr, &readback_buffer_data)); - - u32 ret = reinterpret_cast(readback_buffer_data)[0]; + // a little-endian value is expected to be returned + color = ((color & 0xFF00FF00) | ((color >> 16) & 0xFF) | ((color << 16) & 0xFF0000)); // check what to do with the alpha channel (GX_PokeAlphaRead) PixelEngine::UPEAlphaReadReg alpha_read_mode = PixelEngine::GetAlphaReadMode(); if (bpmem.zcontrol.pixel_format == PEControl::RGBA6_Z24) { - ret = RGBA8ToRGBA6ToRGBA8(ret); + color = RGBA8ToRGBA6ToRGBA8(color); } else if (bpmem.zcontrol.pixel_format == PEControl::RGB565_Z16) { - ret = RGBA8ToRGB565ToRGBA8(ret); + color = RGBA8ToRGB565ToRGBA8(color); } if (bpmem.zcontrol.pixel_format != PEControl::RGBA6_Z24) { - ret |= 0xFF000000; + color |= 0xFF000000; } if (alpha_read_mode.ReadMode == 2) { - return ret; // GX_READ_NONE + return color; // GX_READ_NONE } else if (alpha_read_mode.ReadMode == 1) { - return (ret | 0xFF000000); // GX_READ_FF + return (color | 0xFF000000); // GX_READ_FF } else /*if(alpha_read_mode.ReadMode == 0)*/ { - return (ret & 0x00FFFFFF); // GX_READ_00 + return (color & 0x00FFFFFF); // GX_READ_00 } } + else // if (type == PEEK_Z) + { + // depth buffer is inverted in the d3d backend + float depth = 1.0f - FramebufferManager::ReadEFBDepthAccessCopy(x, y); + u32 ret = 0; - return 0; + if (bpmem.zcontrol.pixel_format == PEControl::RGB565_Z16) + { + // if Z is in 16 bit format you must return a 16 bit integer + ret = MathUtil::Clamp(static_cast(depth * 65536.0f), 0, 0xFFFF); + } + else + { + ret = MathUtil::Clamp(static_cast(depth * 16777216.0f), 0, 0xFFFFFF); + } + + return ret; + } } void Renderer::PokeEFB(EFBAccessType type, const EfbPokeData* points, size_t num_points) @@ -706,6 +550,8 @@ void Renderer::ClearScreen(const EFBRectangle& rc, bool color_enable, bool alpha // Restores proper viewport/scissor settings. g_renderer->RestoreAPIState(); + + FramebufferManager::InvalidateEFBAccessCopies(); } void Renderer::ReinterpretPixelData(unsigned int convtype) @@ -906,6 +752,9 @@ void Renderer::SwapImpl(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height return; } + // Invalidate EFB access copies. Not strictly necessary, but this avoids having the buffers mapped when calling Present(). + FramebufferManager::InvalidateEFBAccessCopies(); + // Prepare to copy the XFBs to our backbuffer UpdateDrawRectangle(s_backbuffer_width, s_backbuffer_height); TargetRectangle target_rc = GetTargetRectangle(); @@ -1272,6 +1121,9 @@ void Renderer::ApplyState(bool use_dst_alpha) D3D::command_list_mgr->SetCommandListDirtyState(COMMAND_LIST_STATE_PSO, false); } + + // Always called prior to drawing, so we can invalidate the CPU EFB copies here. + FramebufferManager::InvalidateEFBAccessCopies(); } void Renderer::RestoreState()