From ccf9470241309114bdc46d9dc80bb9b99aed4a41 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 15 Mar 2016 22:26:01 +1000 Subject: [PATCH] D3D12: Specify read/write ranges when calling Map/Unmap --- Source/Core/VideoBackends/D3D12/BoundingBox.cpp | 6 ++++-- Source/Core/VideoBackends/D3D12/D3DBase.cpp | 3 +-- Source/Core/VideoBackends/D3D12/D3DStreamBuffer.cpp | 9 ++++++--- Source/Core/VideoBackends/D3D12/D3DTexture.cpp | 8 ++++++-- .../Core/VideoBackends/D3D12/FramebufferManager.cpp | 12 ++++++++---- .../Core/VideoBackends/D3D12/PSTextureEncoder.cpp | 10 +++++++--- Source/Core/VideoBackends/D3D12/PerfQuery.cpp | 4 ++-- Source/Core/VideoBackends/D3D12/Render.cpp | 13 +++++++++---- Source/Core/VideoBackends/D3D12/TextureCache.cpp | 6 ++++-- Source/Core/VideoBackends/D3D12/XFBEncoder.cpp | 6 ++++-- 10 files changed, 51 insertions(+), 26 deletions(-) diff --git a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp index 197ab2bdaa..b9ee9b4929 100644 --- a/Source/Core/VideoBackends/D3D12/BoundingBox.cpp +++ b/Source/Core/VideoBackends/D3D12/BoundingBox.cpp @@ -78,7 +78,8 @@ void BBox::Invalidate() if (!s_bbox_staging_buffer_map) return; - s_bbox_staging_buffer->Unmap(0, nullptr); + D3D12_RANGE write_range = {}; + s_bbox_staging_buffer->Unmap(0, &write_range); s_bbox_staging_buffer_map = nullptr; } @@ -139,7 +140,8 @@ int BBox::Get(int index) D3D::command_list_mgr->ExecuteQueuedWork(true); - CheckHR(s_bbox_staging_buffer->Map(0, nullptr, &s_bbox_staging_buffer_map)); + D3D12_RANGE read_range = { 0, BBOX_BUFFER_SIZE }; + CheckHR(s_bbox_staging_buffer->Map(0, &read_range, &s_bbox_staging_buffer_map)); } int value; diff --git a/Source/Core/VideoBackends/D3D12/D3DBase.cpp b/Source/Core/VideoBackends/D3D12/D3DBase.cpp index bc1d535b93..dc32f782e9 100644 --- a/Source/Core/VideoBackends/D3D12/D3DBase.cpp +++ b/Source/Core/VideoBackends/D3D12/D3DBase.cpp @@ -400,8 +400,7 @@ HRESULT Create(HWND wnd) D3D12_MESSAGE_ID id_list[] = { D3D12_MESSAGE_ID_CREATEGRAPHICSPIPELINESTATE_DEPTHSTENCILVIEW_NOT_SET, // Benign. D3D12_MESSAGE_ID_CREATEGRAPHICSPIPELINESTATE_RENDERTARGETVIEW_NOT_SET, // Benign. - D3D12_MESSAGE_ID_CREATEINPUTLAYOUT_TYPE_MISMATCH, // Benign. - D3D12_MESSAGE_ID_MAP_INVALID_NULLRANGE, // Benign. + D3D12_MESSAGE_ID_CREATEINPUTLAYOUT_TYPE_MISMATCH // Benign. }; filter.DenyList.NumIDs = ARRAYSIZE(id_list); filter.DenyList.pIDList = id_list; diff --git a/Source/Core/VideoBackends/D3D12/D3DStreamBuffer.cpp b/Source/Core/VideoBackends/D3D12/D3DStreamBuffer.cpp index 1af9945fea..cfb67209c8 100644 --- a/Source/Core/VideoBackends/D3D12/D3DStreamBuffer.cpp +++ b/Source/Core/VideoBackends/D3D12/D3DStreamBuffer.cpp @@ -29,7 +29,8 @@ D3DStreamBuffer::~D3DStreamBuffer() { D3D::command_list_mgr->RemoveQueueFenceCallback(this); - m_buffer->Unmap(0, nullptr); + D3D12_RANGE write_range = { 0, m_buffer_size }; + m_buffer->Unmap(0, &write_range); D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(m_buffer); } @@ -94,7 +95,8 @@ void D3DStreamBuffer::AllocateBuffer(size_t size) // First, put existing buffer (if it exists) in deferred destruction list. if (m_buffer) { - m_buffer->Unmap(0, nullptr); + D3D12_RANGE write_range = { 0, m_buffer_size }; + m_buffer->Unmap(0, &write_range); D3D::command_list_mgr->DestroyResourceAfterCurrentCommandListExecuted(m_buffer); m_buffer = nullptr; } @@ -110,7 +112,8 @@ void D3DStreamBuffer::AllocateBuffer(size_t size) ) ); - CheckHR(m_buffer->Map(0, nullptr, &m_buffer_cpu_address)); + D3D12_RANGE read_range = {}; + CheckHR(m_buffer->Map(0, &read_range, &m_buffer_cpu_address)); m_buffer_gpu_address = m_buffer->GetGPUVirtualAddress(); m_buffer_size = size; diff --git a/Source/Core/VideoBackends/D3D12/D3DTexture.cpp b/Source/Core/VideoBackends/D3D12/D3DTexture.cpp index 12fed7c4e2..9749a36a76 100644 --- a/Source/Core/VideoBackends/D3D12/D3DTexture.cpp +++ b/Source/Core/VideoBackends/D3D12/D3DTexture.cpp @@ -51,7 +51,8 @@ void ReplaceRGBATexture2D(ID3D12Resource* texture12, const u8* buffer, unsigned nullptr, IID_PPV_ARGS(&upload_buffer))); - CheckHR(upload_buffer->Map(0, nullptr, reinterpret_cast(&dest_data))); + D3D12_RANGE read_range = {}; + CheckHR(upload_buffer->Map(0, &read_range, reinterpret_cast(&dest_data))); } else { @@ -92,8 +93,11 @@ void ReplaceRGBATexture2D(ID3D12Resource* texture12, const u8* buffer, unsigned // We block here because otherwise if there was a large number of texture uploads, we may run out of memory. if (!s_texture_upload_stream_buffer || upload_buffer != s_texture_upload_stream_buffer->GetBuffer()) { + D3D12_RANGE write_range = { 0, upload_size }; + upload_buffer->Unmap(0, &write_range); + D3D::command_list_mgr->ExecuteQueuedWork(true); - upload_buffer->Unmap(0, nullptr); + upload_buffer->Release(); } } diff --git a/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp b/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp index fb925aaaf0..f7cfb2b9a7 100644 --- a/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp +++ b/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp @@ -332,7 +332,8 @@ void FramebufferManager::MapEFBColorAccessCopy() D3D::command_list_mgr->ExecuteQueuedWork(true); // 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)); + D3D12_RANGE read_range = { 0, m_efb.color_access_readback_pitch * EFB_HEIGHT }; + m_efb.color_access_readback_buffer->Map(0, &read_range, reinterpret_cast(&m_efb.color_access_readback_map)); } void FramebufferManager::MapEFBDepthAccessCopy() @@ -380,20 +381,23 @@ void FramebufferManager::MapEFBDepthAccessCopy() D3D::command_list_mgr->ExecuteQueuedWork(true); // 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)); + D3D12_RANGE read_range = { 0, m_efb.depth_access_readback_pitch * EFB_HEIGHT }; + m_efb.depth_access_readback_buffer->Map(0, &read_range, reinterpret_cast(&m_efb.depth_access_readback_map)); } void FramebufferManager::InvalidateEFBAccessCopies() { + D3D12_RANGE write_range = {}; + if (m_efb.color_access_readback_map) { - m_efb.color_access_readback_buffer->Unmap(0, nullptr); + m_efb.color_access_readback_buffer->Unmap(0, &write_range); 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_buffer->Unmap(0, &write_range); m_efb.depth_access_readback_map = nullptr; } } diff --git a/Source/Core/VideoBackends/D3D12/PSTextureEncoder.cpp b/Source/Core/VideoBackends/D3D12/PSTextureEncoder.cpp index e4e63ecd54..fda14dd7e1 100644 --- a/Source/Core/VideoBackends/D3D12/PSTextureEncoder.cpp +++ b/Source/Core/VideoBackends/D3D12/PSTextureEncoder.cpp @@ -105,7 +105,9 @@ void PSTextureEncoder::Init() D3D::SetDebugObjectName12(m_encode_params_buffer, "efb encoder params buffer"); - CheckHR(m_encode_params_buffer->Map(0, nullptr, &m_encode_params_buffer_data)); + // NOTE: This upload buffer is okay to overwrite each time, since we block until completion when it's used anyway. + D3D12_RANGE read_range = {}; + CheckHR(m_encode_params_buffer->Map(0, &read_range, &m_encode_params_buffer_data)); m_ready = true; } @@ -223,7 +225,8 @@ void PSTextureEncoder::Encode(u8* dst, u32 format, u32 native_width, u32 bytes_p // Transfer staging buffer to GameCube/Wii RAM void* readback_data_map; - CheckHR(m_out_readback_buffer->Map(0, nullptr, &readback_data_map)); + D3D12_RANGE read_range = { 0, dst_location.PlacedFootprint.Footprint.RowPitch * num_blocks_y }; + CheckHR(m_out_readback_buffer->Map(0, &read_range, &readback_data_map)); u8* src = static_cast(readback_data_map); u32 read_stride = std::min(bytes_per_row, dst_location.PlacedFootprint.Footprint.RowPitch); @@ -235,7 +238,8 @@ void PSTextureEncoder::Encode(u8* dst, u32 format, u32 native_width, u32 bytes_p src += dst_location.PlacedFootprint.Footprint.RowPitch; } - m_out_readback_buffer->Unmap(0, nullptr); + D3D12_RANGE write_range = {}; + m_out_readback_buffer->Unmap(0, &write_range); } D3D12_SHADER_BYTECODE PSTextureEncoder::SetStaticShader(unsigned int dst_format, PEControl::PixelFormat src_format, diff --git a/Source/Core/VideoBackends/D3D12/PerfQuery.cpp b/Source/Core/VideoBackends/D3D12/PerfQuery.cpp index e86f2e9396..5f4b5508e5 100644 --- a/Source/Core/VideoBackends/D3D12/PerfQuery.cpp +++ b/Source/Core/VideoBackends/D3D12/PerfQuery.cpp @@ -119,8 +119,8 @@ void PerfQuery::FlushOne() UINT64 result; memcpy(&result, reinterpret_cast(readback_buffer_map) + sizeof(UINT64) * index, sizeof(UINT64)); - D3D12_RANGE empty_range = {}; - m_query_readback_buffer->Unmap(0, &empty_range); + D3D12_RANGE write_range = {}; + m_query_readback_buffer->Unmap(0, &write_range); // NOTE: Reported pixel metrics should be referenced to native resolution m_results[entry.query_type] += (u32)(result * EFB_WIDTH / g_renderer->GetTargetWidth() * EFB_HEIGHT / g_renderer->GetTargetHeight()); diff --git a/Source/Core/VideoBackends/D3D12/Render.cpp b/Source/Core/VideoBackends/D3D12/Render.cpp index 05cc323c0b..17653bd193 100644 --- a/Source/Core/VideoBackends/D3D12/Render.cpp +++ b/Source/Core/VideoBackends/D3D12/Render.cpp @@ -665,11 +665,13 @@ bool Renderer::SaveScreenshot(const std::string& filename, const TargetRectangle D3D::command_list_mgr->ExecuteQueuedWork(true); void* screenshot_texture_map; - CheckHR(s_screenshot_texture->Map(0, nullptr, &screenshot_texture_map)); + D3D12_RANGE read_range = { 0, dst_location.PlacedFootprint.Footprint.RowPitch * (source_box.bottom - source_box.top) }; + CheckHR(s_screenshot_texture->Map(0, &read_range, &screenshot_texture_map)); saved_png = TextureToPng(static_cast(screenshot_texture_map), dst_location.PlacedFootprint.Footprint.RowPitch, filename, source_box.right - source_box.left, source_box.bottom - source_box.top, false); - s_screenshot_texture->Unmap(0, nullptr); + D3D12_RANGE write_range = {}; + s_screenshot_texture->Unmap(0, &write_range); if (saved_png) { @@ -871,9 +873,12 @@ void Renderer::SwapImpl(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height } void* screenshot_texture_map; - CheckHR(s_screenshot_texture->Map(0, nullptr, &screenshot_texture_map)); + D3D12_RANGE read_range = { 0, dst_location.PlacedFootprint.Footprint.RowPitch * source_height }; + CheckHR(s_screenshot_texture->Map(0, &read_range, &screenshot_texture_map)); formatBufferDump(static_cast(screenshot_texture_map), &frame_data[0], source_width, source_height, dst_location.PlacedFootprint.Footprint.RowPitch); - s_screenshot_texture->Unmap(0, nullptr); + + D3D12_RANGE write_range = {}; + s_screenshot_texture->Unmap(0, &write_range); FlipImageData(&frame_data[0], w, h); AVIDump::AddFrame(&frame_data[0], source_width, source_height); diff --git a/Source/Core/VideoBackends/D3D12/TextureCache.cpp b/Source/Core/VideoBackends/D3D12/TextureCache.cpp index ceabf01734..4b235d1037 100644 --- a/Source/Core/VideoBackends/D3D12/TextureCache.cpp +++ b/Source/Core/VideoBackends/D3D12/TextureCache.cpp @@ -83,7 +83,8 @@ bool TextureCache::TCacheEntry::Save(const std::string& filename, unsigned int l // Map readback buffer and save to file. void* readback_texture_map; - CheckHR(s_texture_cache_entry_readback_buffer->Map(0, nullptr, &readback_texture_map)); + D3D12_RANGE read_range = { 0, required_readback_buffer_size }; + CheckHR(s_texture_cache_entry_readback_buffer->Map(0, &read_range, &readback_texture_map)); bool saved = TextureToPng( static_cast(readback_texture_map), @@ -93,7 +94,8 @@ bool TextureCache::TCacheEntry::Save(const std::string& filename, unsigned int l dst_location.PlacedFootprint.Footprint.Height ); - s_texture_cache_entry_readback_buffer->Unmap(0, nullptr); + D3D12_RANGE write_range = {}; + s_texture_cache_entry_readback_buffer->Unmap(0, &write_range); return saved; } diff --git a/Source/Core/VideoBackends/D3D12/XFBEncoder.cpp b/Source/Core/VideoBackends/D3D12/XFBEncoder.cpp index f70de0d812..670cda05e2 100644 --- a/Source/Core/VideoBackends/D3D12/XFBEncoder.cpp +++ b/Source/Core/VideoBackends/D3D12/XFBEncoder.cpp @@ -132,7 +132,8 @@ void XFBEncoder::EncodeTextureToRam(u8* dst, u32 dst_pitch, u32 dst_height, // Copy from the readback buffer to dst. // Can't be done as one memcpy due to pitch difference. void* readback_texture_map; - CheckHR(m_readback_buffer->Map(0, nullptr, &readback_texture_map)); + D3D12_RANGE read_range = { 0, readback_pitch * dst_height }; + CheckHR(m_readback_buffer->Map(0, &read_range, &readback_texture_map)); for (u32 row = 0; row < dst_height; row++) { @@ -141,7 +142,8 @@ void XFBEncoder::EncodeTextureToRam(u8* dst, u32 dst_pitch, u32 dst_height, memcpy(row_dst, row_src, std::min(dst_pitch, readback_pitch)); } - m_readback_buffer->Unmap(0, nullptr); + D3D12_RANGE write_range = {}; + m_readback_buffer->Unmap(0, &write_range); } void XFBEncoder::DecodeToTexture(D3DTexture2D* dst_texture, const u8* src, u32 src_width, u32 src_height)