From f7aa7c659c6af66244a0a04e2ef47cff6b03de3f Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 30 May 2021 14:04:30 +1000 Subject: [PATCH] D3D11/Texture: Add layers/texture arrays --- src/common/d3d11/texture.cpp | 49 ++++++++++++++-------- src/common/d3d11/texture.h | 22 +++++----- src/core/gpu_hw_d3d11.cpp | 18 ++++---- src/frontend-common/d3d11_host_display.cpp | 11 +++-- 4 files changed, 60 insertions(+), 40 deletions(-) diff --git a/src/common/d3d11/texture.cpp b/src/common/d3d11/texture.cpp index d7ad70a9d..ab4af3dca 100644 --- a/src/common/d3d11/texture.cpp +++ b/src/common/d3d11/texture.cpp @@ -11,9 +11,11 @@ Texture::Texture(ComPtr texture, ComPtr(desc.SampleDesc.Count); + m_width = static_cast(desc.Width); + m_height = static_cast(desc.Height); + m_layers = static_cast(desc.ArraySize); + m_levels = static_cast(desc.MipLevels); + m_samples = static_cast(desc.SampleDesc.Count); } Texture::~Texture() @@ -28,11 +30,19 @@ D3D11_TEXTURE2D_DESC Texture::GetDesc() const return desc; } -bool Texture::Create(ID3D11Device* device, u32 width, u32 height, u32 levels, u32 samples, DXGI_FORMAT format, - u32 bind_flags, const void* initial_data /* = nullptr */, u32 initial_data_stride /* = 0 */, - bool dynamic) +bool Texture::Create(ID3D11Device* device, u32 width, u32 height, u32 layers, u32 levels, u32 samples, + DXGI_FORMAT format, u32 bind_flags, const void* initial_data /* = nullptr */, + u32 initial_data_stride /* = 0 */, bool dynamic /* = false */) { - CD3D11_TEXTURE2D_DESC desc(format, width, height, 1, levels, bind_flags, + if (width > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION || height > D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION || + layers > D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION || (layers > 1 && samples > 1)) + { + Log_ErrorPrintf("Texture bounds (%ux%ux%u, %u mips, %u samples) are too large", width, height, layers, levels, + samples); + return false; + } + + CD3D11_TEXTURE2D_DESC desc(format, width, height, layers, levels, bind_flags, dynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT, dynamic ? D3D11_CPU_ACCESS_WRITE : 0, samples, 0, 0); @@ -55,7 +65,9 @@ bool Texture::Create(ID3D11Device* device, u32 width, u32 height, u32 levels, u3 if (bind_flags & D3D11_BIND_SHADER_RESOURCE) { const D3D11_SRV_DIMENSION srv_dimension = - (desc.SampleDesc.Count > 1) ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D; + (desc.SampleDesc.Count > 1) ? + D3D11_SRV_DIMENSION_TEXTURE2DMS : + (desc.ArraySize > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DARRAY : D3D11_SRV_DIMENSION_TEXTURE2D); const CD3D11_SHADER_RESOURCE_VIEW_DESC srv_desc(srv_dimension, desc.Format, 0, desc.MipLevels, 0, desc.ArraySize); const HRESULT hr = device->CreateShaderResourceView(texture.Get(), &srv_desc, srv.GetAddressOf()); if (FAILED(hr)) @@ -82,10 +94,11 @@ bool Texture::Create(ID3D11Device* device, u32 width, u32 height, u32 levels, u3 m_texture = std::move(texture); m_srv = std::move(srv); m_rtv = std::move(rtv); - m_width = width; - m_height = height; - m_levels = static_cast(levels); - m_samples = static_cast(samples); + m_width = static_cast(width); + m_height = static_cast(height); + m_layers = static_cast(layers); + m_levels = static_cast(levels); + m_samples = static_cast(samples); return true; } @@ -125,10 +138,11 @@ bool Texture::Adopt(ID3D11Device* device, ComPtr texture) m_texture = std::move(texture); m_srv = std::move(srv); m_rtv = std::move(rtv); - m_width = desc.Width; - m_height = desc.Height; - m_levels = static_cast(desc.MipLevels); - m_samples = static_cast(desc.SampleDesc.Count); + m_width = static_cast(desc.Width); + m_height = static_cast(desc.Height); + m_layers = static_cast(desc.ArraySize); + m_levels = static_cast(desc.MipLevels); + m_samples = static_cast(desc.SampleDesc.Count); return true; } @@ -139,8 +153,9 @@ void Texture::Destroy() m_texture.Reset(); m_width = 0; m_height = 0; + m_layers = 0; m_levels = 0; m_samples = 0; } -} // namespace D3D11 \ No newline at end of file +} // namespace D3D11 diff --git a/src/common/d3d11/texture.h b/src/common/d3d11/texture.h index 8919b4618..402fee2de 100644 --- a/src/common/d3d11/texture.h +++ b/src/common/d3d11/texture.h @@ -21,10 +21,11 @@ public: ALWAYS_INLINE ID3D11ShaderResourceView* const* GetD3DSRVArray() const { return m_srv.GetAddressOf(); } ALWAYS_INLINE ID3D11RenderTargetView* const* GetD3DRTVArray() const { return m_rtv.GetAddressOf(); } - ALWAYS_INLINE u32 GetWidth() const { return m_width; } - ALWAYS_INLINE u32 GetHeight() const { return m_height; } - ALWAYS_INLINE u16 GetLevels() const { return m_levels; } - ALWAYS_INLINE u16 GetSamples() const { return m_samples; } + ALWAYS_INLINE u16 GetWidth() const { return m_width; } + ALWAYS_INLINE u16 GetHeight() const { return m_height; } + ALWAYS_INLINE u16 GetLayers() const { return m_layers; } + ALWAYS_INLINE u8 GetLevels() const { return m_levels; } + ALWAYS_INLINE u8 GetSamples() const { return m_samples; } ALWAYS_INLINE bool IsMultisampled() const { return m_samples > 1; } ALWAYS_INLINE DXGI_FORMAT GetFormat() const { return GetDesc().Format; } D3D11_TEXTURE2D_DESC GetDesc() const; @@ -34,8 +35,8 @@ public: ALWAYS_INLINE operator ID3D11RenderTargetView*() const { return m_rtv.Get(); } ALWAYS_INLINE operator bool() const { return static_cast(m_texture); } - bool Create(ID3D11Device* device, u32 width, u32 height, u32 levels, u32 samples, DXGI_FORMAT format, u32 bind_flags, - const void* initial_data = nullptr, u32 initial_data_stride = 0, bool dynamic = false); + bool Create(ID3D11Device* device, u32 width, u32 height, u32 layers, u32 levels, u32 samples, DXGI_FORMAT format, + u32 bind_flags, const void* initial_data = nullptr, u32 initial_data_stride = 0, bool dynamic = false); bool Adopt(ID3D11Device* device, ComPtr texture); void Destroy(); @@ -44,9 +45,10 @@ private: ComPtr m_texture; ComPtr m_srv; ComPtr m_rtv; - u32 m_width; - u32 m_height; - u16 m_levels; - u16 m_samples; + u16 m_width; + u16 m_height; + u16 m_layers; + u8 m_levels; + u8 m_samples; }; } // namespace D3D11 \ No newline at end of file diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index 1b1f17a3b..56d959522 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -258,18 +258,18 @@ bool GPU_HW_D3D11::CreateFramebuffer() const DXGI_FORMAT texture_format = DXGI_FORMAT_R8G8B8A8_UNORM; const DXGI_FORMAT depth_format = DXGI_FORMAT_D16_UNORM; - if (!m_vram_texture.Create(m_device.Get(), texture_width, texture_height, 1, samples, texture_format, + if (!m_vram_texture.Create(m_device.Get(), texture_width, texture_height, 1, 1, samples, texture_format, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET) || - !m_vram_depth_texture.Create(m_device.Get(), texture_width, texture_height, 1, samples, depth_format, + !m_vram_depth_texture.Create(m_device.Get(), texture_width, texture_height, 1, 1, samples, depth_format, D3D11_BIND_DEPTH_STENCIL) || - !m_vram_read_texture.Create(m_device.Get(), texture_width, texture_height, 1, 1, texture_format, + !m_vram_read_texture.Create(m_device.Get(), texture_width, texture_height, 1, 1, 1, texture_format, D3D11_BIND_SHADER_RESOURCE) || !m_display_texture.Create( m_device.Get(), ((m_downsample_mode == GPUDownsampleMode::Adaptive) ? VRAM_WIDTH : GPU_MAX_DISPLAY_WIDTH) * m_resolution_scale, - GPU_MAX_DISPLAY_HEIGHT * m_resolution_scale, 1, 1, texture_format, + GPU_MAX_DISPLAY_HEIGHT * m_resolution_scale, 1, 1, 1, texture_format, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET) || - !m_vram_encoding_texture.Create(m_device.Get(), VRAM_WIDTH / 2, VRAM_HEIGHT, 1, 1, texture_format, + !m_vram_encoding_texture.Create(m_device.Get(), VRAM_WIDTH / 2, VRAM_HEIGHT, 1, 1, 1, texture_format, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET) || !m_vram_readback_texture.Create(m_device.Get(), VRAM_WIDTH / 2, VRAM_HEIGHT, texture_format, false)) { @@ -287,10 +287,10 @@ bool GPU_HW_D3D11::CreateFramebuffer() { const u32 levels = GetAdaptiveDownsamplingMipLevels(); - if (!m_downsample_texture.Create(m_device.Get(), texture_width, texture_height, static_cast(levels), 1, + if (!m_downsample_texture.Create(m_device.Get(), texture_width, texture_height, 1, static_cast(levels), 1, texture_format, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET) || !m_downsample_weight_texture.Create(m_device.Get(), texture_width >> (levels - 1), - texture_height >> (levels - 1), 1, 1, DXGI_FORMAT_R8_UNORM, + texture_height >> (levels - 1), 1, 1, 1, DXGI_FORMAT_R8_UNORM, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET)) { return false; @@ -317,7 +317,7 @@ bool GPU_HW_D3D11::CreateFramebuffer() } else if (m_downsample_mode == GPUDownsampleMode::Box) { - if (!m_downsample_texture.Create(m_device.Get(), VRAM_WIDTH, VRAM_HEIGHT, 1, 1, texture_format, + if (!m_downsample_texture.Create(m_device.Get(), VRAM_WIDTH, VRAM_HEIGHT, 1, 1, 1, texture_format, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET)) { return false; @@ -750,7 +750,7 @@ bool GPU_HW_D3D11::BlitVRAMReplacementTexture(const TextureReplacementTexture* t if (m_vram_replacement_texture.GetWidth() < tex->GetWidth() || m_vram_replacement_texture.GetHeight() < tex->GetHeight()) { - if (!m_vram_replacement_texture.Create(m_device.Get(), tex->GetWidth(), tex->GetHeight(), 1, 1, + if (!m_vram_replacement_texture.Create(m_device.Get(), tex->GetWidth(), tex->GetHeight(), 1, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_BIND_SHADER_RESOURCE, tex->GetPixels(), tex->GetByteStride(), true)) { diff --git a/src/frontend-common/d3d11_host_display.cpp b/src/frontend-common/d3d11_host_display.cpp index be27a3eb1..230441d4a 100644 --- a/src/frontend-common/d3d11_host_display.cpp +++ b/src/frontend-common/d3d11_host_display.cpp @@ -132,7 +132,7 @@ std::unique_ptr D3D11HostDisplay::CreateTexture(u32 width, u return {}; D3D11::Texture tex; - if (!tex.Create(m_device.Get(), width, height, levels, samples, + if (!tex.Create(m_device.Get(), width, height, layers, levels, samples, s_display_pixel_format_mapping[static_cast(format)], D3D11_BIND_SHADER_RESOURCE, data, data_stride, dynamic)) { @@ -755,7 +755,7 @@ bool D3D11HostDisplay::RenderScreenshot(u32 width, u32 height, std::vector* static constexpr HostDisplayPixelFormat hdformat = HostDisplayPixelFormat::RGBA8; D3D11::Texture render_texture; - if (!render_texture.Create(m_device.Get(), width, height, 1, 1, format, D3D11_BIND_RENDER_TARGET) || + if (!render_texture.Create(m_device.Get(), width, height, 1, 1, 1, format, D3D11_BIND_RENDER_TARGET) || !m_readback_staging_texture.EnsureSize(m_context.Get(), width, height, format, false)) { return false; @@ -1048,8 +1048,11 @@ bool D3D11HostDisplay::CheckPostProcessingRenderTargets(u32 target_width, u32 ta if (m_post_processing_input_texture.GetWidth() != target_width || m_post_processing_input_texture.GetHeight() != target_height) { - if (!m_post_processing_input_texture.Create(m_device.Get(), target_width, target_height, 1, 1, format, bind_flags)) + if (!m_post_processing_input_texture.Create(m_device.Get(), target_width, target_height, 1, 1, 1, format, + bind_flags)) + { return false; + } } const u32 target_count = (static_cast(m_post_processing_stages.size()) - 1); @@ -1058,7 +1061,7 @@ bool D3D11HostDisplay::CheckPostProcessingRenderTargets(u32 target_width, u32 ta PostProcessingStage& pps = m_post_processing_stages[i]; if (pps.output_texture.GetWidth() != target_width || pps.output_texture.GetHeight() != target_height) { - if (!pps.output_texture.Create(m_device.Get(), target_width, target_height, 1, 1, format, bind_flags)) + if (!pps.output_texture.Create(m_device.Get(), target_width, target_height, 1, 1, 1, format, bind_flags)) return false; } }