D3D11/Texture: Add layers/texture arrays

This commit is contained in:
Connor McLaughlin 2021-05-30 14:04:30 +10:00
parent e7fb42347f
commit f7aa7c659c
4 changed files with 60 additions and 40 deletions

View file

@ -11,9 +11,11 @@ Texture::Texture(ComPtr<ID3D11Texture2D> texture, ComPtr<ID3D11ShaderResourceVie
: m_texture(std::move(texture)), m_srv(std::move(srv)), m_rtv(std::move(rtv)) : m_texture(std::move(texture)), m_srv(std::move(srv)), m_rtv(std::move(rtv))
{ {
const D3D11_TEXTURE2D_DESC desc = GetDesc(); const D3D11_TEXTURE2D_DESC desc = GetDesc();
m_width = desc.Width; m_width = static_cast<u16>(desc.Width);
m_height = desc.Height; m_height = static_cast<u16>(desc.Height);
m_samples = static_cast<u16>(desc.SampleDesc.Count); m_layers = static_cast<u16>(desc.ArraySize);
m_levels = static_cast<u8>(desc.MipLevels);
m_samples = static_cast<u8>(desc.SampleDesc.Count);
} }
Texture::~Texture() Texture::~Texture()
@ -28,11 +30,19 @@ D3D11_TEXTURE2D_DESC Texture::GetDesc() const
return desc; return desc;
} }
bool Texture::Create(ID3D11Device* device, u32 width, u32 height, u32 levels, u32 samples, DXGI_FORMAT format, bool Texture::Create(ID3D11Device* device, u32 width, u32 height, u32 layers, u32 levels, u32 samples,
u32 bind_flags, const void* initial_data /* = nullptr */, u32 initial_data_stride /* = 0 */, DXGI_FORMAT format, u32 bind_flags, const void* initial_data /* = nullptr */,
bool dynamic) 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, dynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT, dynamic ? D3D11_CPU_ACCESS_WRITE : 0,
samples, 0, 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) if (bind_flags & D3D11_BIND_SHADER_RESOURCE)
{ {
const D3D11_SRV_DIMENSION srv_dimension = 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 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()); const HRESULT hr = device->CreateShaderResourceView(texture.Get(), &srv_desc, srv.GetAddressOf());
if (FAILED(hr)) 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_texture = std::move(texture);
m_srv = std::move(srv); m_srv = std::move(srv);
m_rtv = std::move(rtv); m_rtv = std::move(rtv);
m_width = width; m_width = static_cast<u16>(width);
m_height = height; m_height = static_cast<u16>(height);
m_levels = static_cast<u16>(levels); m_layers = static_cast<u16>(layers);
m_samples = static_cast<u16>(samples); m_levels = static_cast<u8>(levels);
m_samples = static_cast<u8>(samples);
return true; return true;
} }
@ -125,10 +138,11 @@ bool Texture::Adopt(ID3D11Device* device, ComPtr<ID3D11Texture2D> texture)
m_texture = std::move(texture); m_texture = std::move(texture);
m_srv = std::move(srv); m_srv = std::move(srv);
m_rtv = std::move(rtv); m_rtv = std::move(rtv);
m_width = desc.Width; m_width = static_cast<u16>(desc.Width);
m_height = desc.Height; m_height = static_cast<u16>(desc.Height);
m_levels = static_cast<u16>(desc.MipLevels); m_layers = static_cast<u16>(desc.ArraySize);
m_samples = static_cast<u16>(desc.SampleDesc.Count); m_levels = static_cast<u8>(desc.MipLevels);
m_samples = static_cast<u8>(desc.SampleDesc.Count);
return true; return true;
} }
@ -139,6 +153,7 @@ void Texture::Destroy()
m_texture.Reset(); m_texture.Reset();
m_width = 0; m_width = 0;
m_height = 0; m_height = 0;
m_layers = 0;
m_levels = 0; m_levels = 0;
m_samples = 0; m_samples = 0;
} }

View file

@ -21,10 +21,11 @@ public:
ALWAYS_INLINE ID3D11ShaderResourceView* const* GetD3DSRVArray() const { return m_srv.GetAddressOf(); } ALWAYS_INLINE ID3D11ShaderResourceView* const* GetD3DSRVArray() const { return m_srv.GetAddressOf(); }
ALWAYS_INLINE ID3D11RenderTargetView* const* GetD3DRTVArray() const { return m_rtv.GetAddressOf(); } ALWAYS_INLINE ID3D11RenderTargetView* const* GetD3DRTVArray() const { return m_rtv.GetAddressOf(); }
ALWAYS_INLINE u32 GetWidth() const { return m_width; } ALWAYS_INLINE u16 GetWidth() const { return m_width; }
ALWAYS_INLINE u32 GetHeight() const { return m_height; } ALWAYS_INLINE u16 GetHeight() const { return m_height; }
ALWAYS_INLINE u16 GetLevels() const { return m_levels; } ALWAYS_INLINE u16 GetLayers() const { return m_layers; }
ALWAYS_INLINE u16 GetSamples() const { return m_samples; } 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 bool IsMultisampled() const { return m_samples > 1; }
ALWAYS_INLINE DXGI_FORMAT GetFormat() const { return GetDesc().Format; } ALWAYS_INLINE DXGI_FORMAT GetFormat() const { return GetDesc().Format; }
D3D11_TEXTURE2D_DESC GetDesc() const; D3D11_TEXTURE2D_DESC GetDesc() const;
@ -34,8 +35,8 @@ public:
ALWAYS_INLINE operator ID3D11RenderTargetView*() const { return m_rtv.Get(); } ALWAYS_INLINE operator ID3D11RenderTargetView*() const { return m_rtv.Get(); }
ALWAYS_INLINE operator bool() const { return static_cast<bool>(m_texture); } ALWAYS_INLINE operator bool() const { return static_cast<bool>(m_texture); }
bool Create(ID3D11Device* device, u32 width, u32 height, u32 levels, u32 samples, DXGI_FORMAT format, u32 bind_flags, bool Create(ID3D11Device* device, u32 width, u32 height, u32 layers, u32 levels, u32 samples, DXGI_FORMAT format,
const void* initial_data = nullptr, u32 initial_data_stride = 0, bool dynamic = false); u32 bind_flags, const void* initial_data = nullptr, u32 initial_data_stride = 0, bool dynamic = false);
bool Adopt(ID3D11Device* device, ComPtr<ID3D11Texture2D> texture); bool Adopt(ID3D11Device* device, ComPtr<ID3D11Texture2D> texture);
void Destroy(); void Destroy();
@ -44,9 +45,10 @@ private:
ComPtr<ID3D11Texture2D> m_texture; ComPtr<ID3D11Texture2D> m_texture;
ComPtr<ID3D11ShaderResourceView> m_srv; ComPtr<ID3D11ShaderResourceView> m_srv;
ComPtr<ID3D11RenderTargetView> m_rtv; ComPtr<ID3D11RenderTargetView> m_rtv;
u32 m_width; u16 m_width;
u32 m_height; u16 m_height;
u16 m_levels; u16 m_layers;
u16 m_samples; u8 m_levels;
u8 m_samples;
}; };
} // namespace D3D11 } // namespace D3D11

View file

@ -258,18 +258,18 @@ bool GPU_HW_D3D11::CreateFramebuffer()
const DXGI_FORMAT texture_format = DXGI_FORMAT_R8G8B8A8_UNORM; const DXGI_FORMAT texture_format = DXGI_FORMAT_R8G8B8A8_UNORM;
const DXGI_FORMAT depth_format = DXGI_FORMAT_D16_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) || 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) || 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) || D3D11_BIND_SHADER_RESOURCE) ||
!m_display_texture.Create( !m_display_texture.Create(
m_device.Get(), m_device.Get(),
((m_downsample_mode == GPUDownsampleMode::Adaptive) ? VRAM_WIDTH : GPU_MAX_DISPLAY_WIDTH) * m_resolution_scale, ((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) || 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) || D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET) ||
!m_vram_readback_texture.Create(m_device.Get(), VRAM_WIDTH / 2, VRAM_HEIGHT, texture_format, false)) !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(); const u32 levels = GetAdaptiveDownsamplingMipLevels();
if (!m_downsample_texture.Create(m_device.Get(), texture_width, texture_height, static_cast<u16>(levels), 1, if (!m_downsample_texture.Create(m_device.Get(), texture_width, texture_height, 1, static_cast<u16>(levels), 1,
texture_format, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET) || texture_format, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET) ||
!m_downsample_weight_texture.Create(m_device.Get(), texture_width >> (levels - 1), !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)) D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET))
{ {
return false; return false;
@ -317,7 +317,7 @@ bool GPU_HW_D3D11::CreateFramebuffer()
} }
else if (m_downsample_mode == GPUDownsampleMode::Box) 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)) D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET))
{ {
return false; return false;
@ -750,7 +750,7 @@ bool GPU_HW_D3D11::BlitVRAMReplacementTexture(const TextureReplacementTexture* t
if (m_vram_replacement_texture.GetWidth() < tex->GetWidth() || if (m_vram_replacement_texture.GetWidth() < tex->GetWidth() ||
m_vram_replacement_texture.GetHeight() < tex->GetHeight()) 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(), DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_BIND_SHADER_RESOURCE, tex->GetPixels(),
tex->GetByteStride(), true)) tex->GetByteStride(), true))
{ {

View file

@ -132,7 +132,7 @@ std::unique_ptr<HostDisplayTexture> D3D11HostDisplay::CreateTexture(u32 width, u
return {}; return {};
D3D11::Texture tex; 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<u32>(format)], D3D11_BIND_SHADER_RESOURCE, data, s_display_pixel_format_mapping[static_cast<u32>(format)], D3D11_BIND_SHADER_RESOURCE, data,
data_stride, dynamic)) data_stride, dynamic))
{ {
@ -755,7 +755,7 @@ bool D3D11HostDisplay::RenderScreenshot(u32 width, u32 height, std::vector<u32>*
static constexpr HostDisplayPixelFormat hdformat = HostDisplayPixelFormat::RGBA8; static constexpr HostDisplayPixelFormat hdformat = HostDisplayPixelFormat::RGBA8;
D3D11::Texture render_texture; 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)) !m_readback_staging_texture.EnsureSize(m_context.Get(), width, height, format, false))
{ {
return false; return false;
@ -1048,9 +1048,12 @@ bool D3D11HostDisplay::CheckPostProcessingRenderTargets(u32 target_width, u32 ta
if (m_post_processing_input_texture.GetWidth() != target_width || if (m_post_processing_input_texture.GetWidth() != target_width ||
m_post_processing_input_texture.GetHeight() != target_height) 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; return false;
} }
}
const u32 target_count = (static_cast<u32>(m_post_processing_stages.size()) - 1); const u32 target_count = (static_cast<u32>(m_post_processing_stages.size()) - 1);
for (u32 i = 0; i < target_count; i++) for (u32 i = 0; i < target_count; i++)
@ -1058,7 +1061,7 @@ bool D3D11HostDisplay::CheckPostProcessingRenderTargets(u32 target_width, u32 ta
PostProcessingStage& pps = m_post_processing_stages[i]; PostProcessingStage& pps = m_post_processing_stages[i];
if (pps.output_texture.GetWidth() != target_width || pps.output_texture.GetHeight() != target_height) 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; return false;
} }
} }