From a584ccc7d882213d93586cae9063d9865311aaad Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sat, 9 Sep 2017 15:24:41 +1000 Subject: AbstractTexture: Support BGRA8 formats Used for some driver's swap chains, and EFB to RAM. --- Source/Core/VideoBackends/D3D/DXTexture.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'Source/Core/VideoBackends/D3D/DXTexture.cpp') diff --git a/Source/Core/VideoBackends/D3D/DXTexture.cpp b/Source/Core/VideoBackends/D3D/DXTexture.cpp index 5f7a4e39eb..30f664b5b4 100644 --- a/Source/Core/VideoBackends/D3D/DXTexture.cpp +++ b/Source/Core/VideoBackends/D3D/DXTexture.cpp @@ -40,7 +40,11 @@ DXGI_FORMAT GetDXGIFormatForHostFormat(AbstractTextureFormat format) case AbstractTextureFormat::BPTC: return DXGI_FORMAT_BC7_UNORM; case AbstractTextureFormat::RGBA8: + return DXGI_FORMAT_R8G8B8A8_UNORM; + case AbstractTextureFormat::BGRA8: + return DXGI_FORMAT_B8G8R8A8_UNORM; default: + PanicAlert("Unhandled texture format."); return DXGI_FORMAT_R8G8B8A8_UNORM; } } -- cgit v1.2.3 From f43d85921dc85ae586d69b21d28a7578835a55bd Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 22 Oct 2017 00:49:40 +1000 Subject: VideoBackends: Add AbstractStagingTexture class Can be used for asynchronous readback or upload of textures. --- Source/Core/VideoBackends/D3D/DXTexture.cpp | 134 +++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 1 deletion(-) (limited to 'Source/Core/VideoBackends/D3D/DXTexture.cpp') diff --git a/Source/Core/VideoBackends/D3D/DXTexture.cpp b/Source/Core/VideoBackends/D3D/DXTexture.cpp index 30f664b5b4..92d3d112e9 100644 --- a/Source/Core/VideoBackends/D3D/DXTexture.cpp +++ b/Source/Core/VideoBackends/D3D/DXTexture.cpp @@ -217,8 +217,140 @@ void DXTexture::CopyRectangleFromTexture(const AbstractTexture* source, void DXTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer, size_t buffer_size) { - size_t src_pitch = CalculateHostTextureLevelPitch(m_config.format, row_length); + size_t src_pitch = CalculateStrideForFormat(m_config.format, row_length); D3D::context->UpdateSubresource(m_texture->GetTex(), level, nullptr, buffer, static_cast(src_pitch), 0); } + +DXStagingTexture::DXStagingTexture(StagingTextureType type, const TextureConfig& config, + ID3D11Texture2D* tex) + : AbstractStagingTexture(type, config), m_tex(tex) +{ +} + +DXStagingTexture::~DXStagingTexture() +{ + if (IsMapped()) + DXStagingTexture::Unmap(); + SAFE_RELEASE(m_tex); +} + +std::unique_ptr DXStagingTexture::Create(StagingTextureType type, + const TextureConfig& config) +{ + D3D11_USAGE usage; + UINT cpu_flags; + if (type == StagingTextureType::Readback) + { + usage = D3D11_USAGE_STAGING; + cpu_flags = D3D11_CPU_ACCESS_READ; + } + else if (type == StagingTextureType::Upload) + { + usage = D3D11_USAGE_DYNAMIC; + cpu_flags = D3D11_CPU_ACCESS_WRITE; + } + else + { + usage = D3D11_USAGE_STAGING; + cpu_flags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; + } + + CD3D11_TEXTURE2D_DESC desc(GetDXGIFormatForHostFormat(config.format), config.width, config.height, + 1, 1, 0, usage, cpu_flags); + + ID3D11Texture2D* texture; + HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, &texture); + CHECK(SUCCEEDED(hr), "Create staging texture"); + if (FAILED(hr)) + return nullptr; + + return std::unique_ptr(new DXStagingTexture(type, config, texture)); +} + +void DXStagingTexture::CopyFromTexture(const AbstractTexture* src, + const MathUtil::Rectangle& src_rect, u32 src_layer, + u32 src_level, const MathUtil::Rectangle& dst_rect) +{ + _assert_(m_type == StagingTextureType::Readback); + _assert_(src_rect.GetWidth() == dst_rect.GetWidth() && + src_rect.GetHeight() == dst_rect.GetHeight()); + _assert_(src_rect.left >= 0 && static_cast(src_rect.right) <= src->GetConfig().width && + src_rect.top >= 0 && static_cast(src_rect.bottom) <= src->GetConfig().height); + _assert_(dst_rect.left >= 0 && static_cast(dst_rect.right) <= m_config.width && + dst_rect.top >= 0 && static_cast(dst_rect.bottom) <= m_config.height); + + if (IsMapped()) + DXStagingTexture::Unmap(); + + CD3D11_BOX src_box(src_rect.left, src_rect.top, 0, src_rect.right, src_rect.bottom, 1); + D3D::context->CopySubresourceRegion( + m_tex, 0, static_cast(dst_rect.left), static_cast(dst_rect.top), 0, + static_cast(src)->GetRawTexIdentifier()->GetTex(), + D3D11CalcSubresource(src_level, src_layer, src->GetConfig().levels), &src_box); + + m_needs_flush = true; +} + +void DXStagingTexture::CopyToTexture(const MathUtil::Rectangle& src_rect, AbstractTexture* dst, + const MathUtil::Rectangle& dst_rect, u32 dst_layer, + u32 dst_level) +{ + _assert_(m_type == StagingTextureType::Upload); + _assert_(src_rect.GetWidth() == dst_rect.GetWidth() && + src_rect.GetHeight() == dst_rect.GetHeight()); + _assert_(src_rect.left >= 0 && static_cast(src_rect.right) <= m_config.width && + src_rect.top >= 0 && static_cast(src_rect.bottom) <= m_config.height); + _assert_(dst_rect.left >= 0 && static_cast(dst_rect.right) <= dst->GetConfig().width && + dst_rect.top >= 0 && static_cast(dst_rect.bottom) <= dst->GetConfig().height); + + if (IsMapped()) + DXStagingTexture::Unmap(); + + CD3D11_BOX src_box(src_rect.left, src_rect.top, 0, src_rect.right, src_rect.bottom, 1); + D3D::context->CopySubresourceRegion( + static_cast(dst)->GetRawTexIdentifier()->GetTex(), + D3D11CalcSubresource(dst_level, dst_layer, dst->GetConfig().levels), + static_cast(dst_rect.left), static_cast(dst_rect.top), 0, m_tex, 0, &src_box); +} + +bool DXStagingTexture::Map() +{ + if (m_map_pointer) + return true; + + D3D11_MAP map_type; + if (m_type == StagingTextureType::Readback) + map_type = D3D11_MAP_READ; + else if (m_type == StagingTextureType::Upload) + map_type = D3D11_MAP_WRITE; + else + map_type = D3D11_MAP_READ_WRITE; + + D3D11_MAPPED_SUBRESOURCE sr; + HRESULT hr = D3D::context->Map(m_tex, 0, map_type, 0, &sr); + CHECK(SUCCEEDED(hr), "Map readback texture"); + if (FAILED(hr)) + return false; + + m_map_pointer = reinterpret_cast(sr.pData); + m_map_stride = sr.RowPitch; + return true; +} + +void DXStagingTexture::Unmap() +{ + if (!m_map_pointer) + return; + + D3D::context->Unmap(m_tex, 0); + m_map_pointer = nullptr; +} + +void DXStagingTexture::Flush() +{ + // Flushing is handled by the API. + m_needs_flush = false; +} + } // namespace DX11 -- cgit v1.2.3 From 56afebeb44f1ace990d9383e9b840fdc27610029 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 30 Oct 2017 21:51:42 +1000 Subject: AbstractTexture: Seperate CopyRectangleFromTexture to two methods ScaleRectangleFromTexture, which does a draw, and CopyRectangleFromTexture, which where possible, does a bit-for-bit copy. --- Source/Core/VideoBackends/D3D/DXTexture.cpp | 49 ++++++++++++++++------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'Source/Core/VideoBackends/D3D/DXTexture.cpp') diff --git a/Source/Core/VideoBackends/D3D/DXTexture.cpp b/Source/Core/VideoBackends/D3D/DXTexture.cpp index 92d3d112e9..722db73a5f 100644 --- a/Source/Core/VideoBackends/D3D/DXTexture.cpp +++ b/Source/Core/VideoBackends/D3D/DXTexture.cpp @@ -165,29 +165,36 @@ void DXTexture::Unmap() D3D::context->Unmap(m_staging_texture, 0); } -void DXTexture::CopyRectangleFromTexture(const AbstractTexture* source, - const MathUtil::Rectangle& srcrect, - const MathUtil::Rectangle& dstrect) +void DXTexture::CopyRectangleFromTexture(const AbstractTexture* src, + const MathUtil::Rectangle& src_rect, u32 src_layer, + u32 src_level, const MathUtil::Rectangle& dst_rect, + u32 dst_layer, u32 dst_level) +{ + const DXTexture* srcentry = static_cast(src); + _assert_(src_rect.GetWidth() == dst_rect.GetWidth() && + src_rect.GetHeight() == dst_rect.GetHeight()); + + D3D11_BOX src_box; + src_box.left = src_rect.left; + src_box.top = src_rect.top; + src_box.right = src_rect.right; + src_box.bottom = src_rect.bottom; + src_box.front = 0; + src_box.back = 1; + + D3D::context->CopySubresourceRegion( + m_texture->GetTex(), D3D11CalcSubresource(dst_level, dst_layer, m_config.levels), + dst_rect.left, dst_rect.top, 0, srcentry->m_texture->GetTex(), + D3D11CalcSubresource(src_level, src_layer, srcentry->m_config.levels), &src_box); +} + +void DXTexture::ScaleRectangleFromTexture(const AbstractTexture* source, + const MathUtil::Rectangle& srcrect, + const MathUtil::Rectangle& dstrect) { const DXTexture* srcentry = static_cast(source); - if (srcrect.GetWidth() == dstrect.GetWidth() && srcrect.GetHeight() == dstrect.GetHeight()) - { - D3D11_BOX srcbox; - srcbox.left = srcrect.left; - srcbox.top = srcrect.top; - srcbox.right = srcrect.right; - srcbox.bottom = srcrect.bottom; - srcbox.front = 0; - srcbox.back = srcentry->m_config.layers; - - D3D::context->CopySubresourceRegion(m_texture->GetTex(), 0, dstrect.left, dstrect.top, 0, - srcentry->m_texture->GetTex(), 0, &srcbox); - return; - } - else if (!m_config.rendertarget) - { - return; - } + _assert_(m_config.rendertarget); + g_renderer->ResetAPIState(); // reset any game specific settings const D3D11_VIEWPORT vp = CD3D11_VIEWPORT(float(dstrect.left), float(dstrect.top), -- cgit v1.2.3 From db1d9de933a914ff0c35a179d3ba1112fb0bcab1 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 19 Nov 2017 17:46:00 +1000 Subject: AbstractTexture: Drop slow map readback path --- Source/Core/VideoBackends/D3D/DXTexture.cpp | 69 ----------------------------- 1 file changed, 69 deletions(-) (limited to 'Source/Core/VideoBackends/D3D/DXTexture.cpp') diff --git a/Source/Core/VideoBackends/D3D/DXTexture.cpp b/Source/Core/VideoBackends/D3D/DXTexture.cpp index 722db73a5f..2c37c61ffe 100644 --- a/Source/Core/VideoBackends/D3D/DXTexture.cpp +++ b/Source/Core/VideoBackends/D3D/DXTexture.cpp @@ -84,7 +84,6 @@ DXTexture::DXTexture(const TextureConfig& tex_config) : AbstractTexture(tex_conf DXTexture::~DXTexture() { m_texture->Release(); - SAFE_RELEASE(m_staging_texture); } D3DTexture2D* DXTexture::GetRawTexIdentifier() const @@ -97,74 +96,6 @@ void DXTexture::Bind(unsigned int stage) D3D::stateman->SetTexture(stage, m_texture->GetSRV()); } -std::optional DXTexture::MapFullImpl() -{ - CD3D11_TEXTURE2D_DESC staging_texture_desc(DXGI_FORMAT_R8G8B8A8_UNORM, m_config.width, - m_config.height, 1, 1, 0, D3D11_USAGE_STAGING, - D3D11_CPU_ACCESS_READ); - - HRESULT hr = D3D::device->CreateTexture2D(&staging_texture_desc, nullptr, &m_staging_texture); - if (FAILED(hr)) - { - WARN_LOG(VIDEO, "Failed to create texture dumping readback texture: %X", static_cast(hr)); - return {}; - } - - // Copy the selected data to the staging texture - D3D::context->CopyResource(m_staging_texture, m_texture->GetTex()); - - // Map the staging texture to client memory, and encode it as a .png image. - D3D11_MAPPED_SUBRESOURCE map; - hr = D3D::context->Map(m_staging_texture, 0, D3D11_MAP_READ, 0, &map); - if (FAILED(hr)) - { - WARN_LOG(VIDEO, "Failed to map texture dumping readback texture: %X", static_cast(hr)); - return {}; - } - - return AbstractTexture::RawTextureInfo{reinterpret_cast(map.pData), map.RowPitch, - m_config.width, m_config.height}; -} - -std::optional DXTexture::MapRegionImpl(u32 level, u32 x, u32 y, - u32 width, u32 height) -{ - CD3D11_TEXTURE2D_DESC staging_texture_desc(DXGI_FORMAT_R8G8B8A8_UNORM, width, height, 1, 1, 0, - D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ); - - HRESULT hr = D3D::device->CreateTexture2D(&staging_texture_desc, nullptr, &m_staging_texture); - if (FAILED(hr)) - { - WARN_LOG(VIDEO, "Failed to create texture dumping readback texture: %X", static_cast(hr)); - return {}; - } - - // Copy the selected data to the staging texture - CD3D11_BOX src_box(x, y, 0, width, height, 1); - D3D::context->CopySubresourceRegion(m_staging_texture, 0, 0, 0, 0, m_texture->GetTex(), - D3D11CalcSubresource(level, 0, m_config.levels), &src_box); - - // Map the staging texture to client memory, and encode it as a .png image. - D3D11_MAPPED_SUBRESOURCE map; - hr = D3D::context->Map(m_staging_texture, 0, D3D11_MAP_READ, 0, &map); - if (FAILED(hr)) - { - WARN_LOG(VIDEO, "Failed to map texture dumping readback texture: %X", static_cast(hr)); - return {}; - } - - return AbstractTexture::RawTextureInfo{reinterpret_cast(map.pData), map.RowPitch, - m_config.width, m_config.height}; -} - -void DXTexture::Unmap() -{ - if (!m_staging_texture) - return; - - D3D::context->Unmap(m_staging_texture, 0); -} - void DXTexture::CopyRectangleFromTexture(const AbstractTexture* src, const MathUtil::Rectangle& src_rect, u32 src_layer, u32 src_level, const MathUtil::Rectangle& dst_rect, -- cgit v1.2.3