VideoBackends / VideoCommon: add type enum to dictate whether a texture is a 2D texture, a texture array, or a cube map; support 2D texture type across backends

Co-authored-by: TellowKrinkle <tellowkrinkle@gmail.com>
This commit is contained in:
iwubcode
2023-12-09 19:00:11 -06:00
parent 370474a7cb
commit 12dd15c8dd
16 changed files with 271 additions and 81 deletions

View File

@ -45,9 +45,10 @@ std::unique_ptr<DXTexture> DXTexture::Create(const TextureConfig& config, std::s
if (config.IsComputeImage())
bindflags |= D3D11_BIND_UNORDERED_ACCESS;
CD3D11_TEXTURE2D_DESC desc(tex_format, config.width, config.height, config.layers, config.levels,
bindflags, D3D11_USAGE_DEFAULT, 0, config.samples, 0,
config.IsCubeMap() ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0);
CD3D11_TEXTURE2D_DESC desc(
tex_format, config.width, config.height, config.layers, config.levels, bindflags,
D3D11_USAGE_DEFAULT, 0, config.samples, 0,
config.type == AbstractTextureType::Texture_CubeMap ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0);
ComPtr<ID3D11Texture2D> d3d_texture;
HRESULT hr = D3D::device->CreateTexture2D(&desc, nullptr, d3d_texture.GetAddressOf());
if (FAILED(hr))
@ -72,7 +73,8 @@ std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ComPtr<ID3D11Texture2D> text
// Convert to our texture config format.
TextureConfig config(desc.Width, desc.Height, desc.MipLevels, desc.ArraySize,
desc.SampleDesc.Count,
D3DCommon::GetAbstractFormatForDXGIFormat(desc.Format), 0);
D3DCommon::GetAbstractFormatForDXGIFormat(desc.Format), 0,
AbstractTextureType::Texture_2DArray);
if (desc.BindFlags & D3D11_BIND_RENDER_TARGET)
config.flags |= AbstractTextureFlag_RenderTarget;
if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS)
@ -89,13 +91,33 @@ std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ComPtr<ID3D11Texture2D> text
bool DXTexture::CreateSRV()
{
D3D_SRV_DIMENSION dimension = D3D_SRV_DIMENSION_TEXTURE2DARRAY;
if (m_config.type == AbstractTextureType::Texture_2DArray)
{
if (m_config.IsMultisampled())
dimension = D3D_SRV_DIMENSION_TEXTURE2DMSARRAY;
else
dimension = D3D_SRV_DIMENSION_TEXTURE2DARRAY;
}
else if (m_config.type == AbstractTextureType::Texture_2D)
{
if (m_config.IsMultisampled())
dimension = D3D_SRV_DIMENSION_TEXTURE2DMS;
else
dimension = D3D_SRV_DIMENSION_TEXTURE2D;
}
else if (m_config.type == AbstractTextureType::Texture_CubeMap)
{
dimension = D3D_SRV_DIMENSION_TEXTURECUBE;
}
else
{
PanicAlertFmt("Failed to create D3D SRV - unhandled type");
return false;
}
const CD3D11_SHADER_RESOURCE_VIEW_DESC desc(
m_texture.Get(),
m_config.IsCubeMap() ? D3D11_SRV_DIMENSION_TEXTURECUBE :
m_config.IsMultisampled() ? D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY :
D3D11_SRV_DIMENSION_TEXTURE2DARRAY,
D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0, m_config.levels, 0,
m_config.layers);
m_texture.Get(), dimension, D3DCommon::GetSRVFormatForAbstractFormat(m_config.format), 0,
m_config.levels, 0, m_config.layers);
DEBUG_ASSERT(!m_srv);
HRESULT hr = D3D::device->CreateShaderResourceView(m_texture.Get(), &desc, m_srv.GetAddressOf());
if (FAILED(hr))

View File

@ -140,7 +140,8 @@ std::unique_ptr<DXTexture> DXTexture::CreateAdopted(ID3D12Resource* resource)
}
TextureConfig config(static_cast<u32>(desc.Width), desc.Height, desc.MipLevels,
desc.DepthOrArraySize, desc.SampleDesc.Count, format, 0);
desc.DepthOrArraySize, desc.SampleDesc.Count, format, 0,
AbstractTextureType::Texture_2DArray);
if (desc.Flags &
(D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL))
{
@ -165,14 +166,34 @@ bool DXTexture::CreateSRVDescriptor()
return false;
}
D3D12_SHADER_RESOURCE_VIEW_DESC desc = {
D3DCommon::GetSRVFormatForAbstractFormat(m_config.format),
m_config.IsCubeMap() ? D3D12_SRV_DIMENSION_TEXTURECUBE :
m_config.IsMultisampled() ? D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY :
D3D12_SRV_DIMENSION_TEXTURE2DARRAY,
D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING};
D3D12_SRV_DIMENSION dimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
if (m_config.type == AbstractTextureType::Texture_2DArray)
{
if (m_config.IsMultisampled())
dimension = D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY;
else
dimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
}
else if (m_config.type == AbstractTextureType::Texture_2D)
{
if (m_config.IsMultisampled())
dimension = D3D12_SRV_DIMENSION_TEXTURE2DMS;
else
dimension = D3D12_SRV_DIMENSION_TEXTURE2D;
}
else if (m_config.type == AbstractTextureType::Texture_CubeMap)
{
dimension = D3D12_SRV_DIMENSION_TEXTURECUBE;
}
else
{
PanicAlertFmt("Failed to allocate SRV - unhandled type");
return false;
}
D3D12_SHADER_RESOURCE_VIEW_DESC desc = {D3DCommon::GetSRVFormatForAbstractFormat(m_config.format),
dimension, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING};
if (m_config.IsCubeMap())
if (m_config.type == AbstractTextureType::Texture_CubeMap)
{
desc.TextureCube.MostDetailedMip = 0;
desc.TextureCube.MipLevels = m_config.levels;

View File

@ -36,14 +36,29 @@ bool Metal::Gfx::IsHeadless() const
// MARK: Texture Creation
static MTLTextureType FromAbstract(AbstractTextureType type, bool multisample)
{
switch (type)
{
case AbstractTextureType::Texture_2D:
return multisample ? MTLTextureType2DMultisample : MTLTextureType2D;
case AbstractTextureType::Texture_2DArray:
return multisample ? MTLTextureType2DMultisampleArray : MTLTextureType2DArray;
case AbstractTextureType::Texture_CubeMap:
return MTLTextureTypeCube;
}
ASSERT(false);
return MTLTextureType2DArray;
}
std::unique_ptr<AbstractTexture> Metal::Gfx::CreateTexture(const TextureConfig& config,
std::string_view name)
{
@autoreleasepool
{
MRCOwned<MTLTextureDescriptor*> desc = MRCTransfer([MTLTextureDescriptor new]);
[desc setTextureType:config.samples > 1 ? MTLTextureType2DMultisampleArray :
MTLTextureType2DArray];
[desc setTextureType:FromAbstract(config.type, config.samples > 1)];
[desc setPixelFormat:Util::FromAbstract(config.format)];
[desc setWidth:config.width];
[desc setHeight:config.height];
@ -490,8 +505,8 @@ void Metal::Gfx::SetupSurface()
[m_layer setDrawableSize:{static_cast<double>(info.width), static_cast<double>(info.height)}];
TextureConfig cfg(info.width, info.height, 1, 1, 1, info.format,
AbstractTextureFlag_RenderTarget);
TextureConfig cfg(info.width, info.height, 1, 1, 1, info.format, AbstractTextureFlag_RenderTarget,
AbstractTextureType::Texture_2DArray);
m_bb_texture = std::make_unique<Texture>(nullptr, cfg);
m_backbuffer = std::make_unique<Framebuffer>(
m_bb_texture.get(), nullptr, std::vector<AbstractTexture*>{}, info.width, info.height, 1, 1);

View File

@ -140,29 +140,61 @@ OGLTexture::OGLTexture(const TextureConfig& tex_config, std::string_view name)
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, m_config.levels - 1);
GLenum gl_internal_format = GetGLInternalFormatForTextureFormat(m_config.format, true);
if (g_ogl_config.bSupportsTextureStorage && m_config.IsCubeMap())
if (g_ogl_config.bSupportsTextureStorage && m_config.type == AbstractTextureType::Texture_CubeMap)
{
glTexStorage2D(target, m_config.levels, gl_internal_format, m_config.width, m_config.height);
}
else if (tex_config.IsMultisampled())
{
ASSERT(g_ogl_config.bSupportsMSAA);
if (g_ogl_config.SupportedMultisampleTexStorage != MultisampleTexStorageType::TexStorageNone)
if (m_config.type == AbstractTextureType::Texture_2DArray)
{
glTexStorage3DMultisample(target, tex_config.samples, gl_internal_format, m_config.width,
if (g_ogl_config.SupportedMultisampleTexStorage != MultisampleTexStorageType::TexStorageNone)
{
glTexStorage3DMultisample(target, tex_config.samples, gl_internal_format, m_config.width,
m_config.height, m_config.layers, GL_FALSE);
}
else
{
ASSERT(!g_ogl_config.bIsES);
glTexImage3DMultisample(target, tex_config.samples, gl_internal_format, m_config.width,
m_config.height, m_config.layers, GL_FALSE);
}
}
else if (m_config.type == AbstractTextureType::Texture_2D)
{
if (g_ogl_config.SupportedMultisampleTexStorage != MultisampleTexStorageType::TexStorageNone)
{
glTexStorage2DMultisample(target, tex_config.samples, gl_internal_format, m_config.width,
m_config.height, GL_FALSE);
}
else
{
ASSERT(!g_ogl_config.bIsES);
glTexImage2DMultisample(target, tex_config.samples, gl_internal_format, m_config.width,
m_config.height, GL_FALSE);
}
}
else
{
ASSERT(!g_ogl_config.bIsES);
glTexImage3DMultisample(target, tex_config.samples, gl_internal_format, m_config.width,
m_config.height, m_config.layers, GL_FALSE);
ASSERT(false);
}
}
else if (g_ogl_config.bSupportsTextureStorage)
{
glTexStorage3D(target, m_config.levels, gl_internal_format, m_config.width, m_config.height,
m_config.layers);
if (m_config.type == AbstractTextureType::Texture_2DArray)
{
glTexStorage3D(target, m_config.levels, gl_internal_format, m_config.width, m_config.height,
m_config.layers);
}
else if (m_config.type == AbstractTextureType::Texture_2D)
{
glTexStorage2D(target, m_config.levels, gl_internal_format, m_config.width, m_config.height);
}
else
{
ASSERT(false);
}
}
if (m_config.IsRenderTarget())
@ -271,7 +303,7 @@ void OGLTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8
GLenum gl_internal_format = GetGLInternalFormatForTextureFormat(m_config.format, false);
if (IsCompressedFormat(m_config.format))
{
if (m_config.IsCubeMap())
if (m_config.type == AbstractTextureType::Texture_CubeMap)
{
if (g_ogl_config.bSupportsTextureStorage)
{
@ -285,7 +317,20 @@ void OGLTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8
width, height, 0, static_cast<GLsizei>(buffer_size), buffer);
}
}
else
else if (m_config.type == AbstractTextureType::Texture_2D)
{
if (g_ogl_config.bSupportsTextureStorage)
{
glCompressedTexSubImage2D(target, level, 0, 0, width, height, gl_internal_format,
static_cast<GLsizei>(buffer_size), buffer);
}
else
{
glCompressedTexImage2D(target, level, gl_internal_format, width, height, 0,
static_cast<GLsizei>(buffer_size), buffer);
}
}
else if (m_config.type == AbstractTextureType::Texture_2DArray)
{
if (g_ogl_config.bSupportsTextureStorage)
{
@ -298,12 +343,16 @@ void OGLTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8
static_cast<GLsizei>(buffer_size), buffer);
}
}
else
{
PanicAlertFmt("Failed to handle compressed texture load - unhandled type");
}
}
else
{
GLenum gl_format = GetGLFormatForTextureFormat(m_config.format);
GLenum gl_type = GetGLTypeForTextureFormat(m_config.format);
if (m_config.IsCubeMap())
if (m_config.type == AbstractTextureType::Texture_CubeMap)
{
if (g_ogl_config.bSupportsTextureStorage)
{
@ -316,7 +365,19 @@ void OGLTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8
height, 0, gl_format, gl_type, buffer);
}
}
else
else if (m_config.type == AbstractTextureType::Texture_2D)
{
if (g_ogl_config.bSupportsTextureStorage)
{
glTexSubImage2D(target, level, 0, 0, width, height, gl_format, gl_type, buffer);
}
else
{
glTexImage2D(target, level, gl_internal_format, width, height, 0, gl_format, gl_type,
buffer);
}
}
else if (m_config.type == AbstractTextureType::Texture_2DArray)
{
if (g_ogl_config.bSupportsTextureStorage)
{
@ -328,6 +389,10 @@ void OGLTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8
buffer);
}
}
else
{
PanicAlertFmt("Failed to handle texture load - unhandled type");
}
}
if (row_length != width)

View File

@ -34,9 +34,26 @@ public:
GLuint GetGLTextureId() const { return m_texId; }
GLenum GetGLTarget() const
{
return m_config.IsCubeMap() ? GL_TEXTURE_CUBE_MAP :
IsMultisampled() ? GL_TEXTURE_2D_MULTISAMPLE_ARRAY :
GL_TEXTURE_2D_ARRAY;
if (m_config.type == AbstractTextureType::Texture_2DArray)
{
if (m_config.IsMultisampled())
return GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
else
return GL_TEXTURE_2D_ARRAY;
}
else if (m_config.type == AbstractTextureType::Texture_2D)
{
if (m_config.IsMultisampled())
return GL_TEXTURE_2D_MULTISAMPLE;
else
return GL_TEXTURE_2D;
}
else if (m_config.type == AbstractTextureType::Texture_CubeMap)
{
return GL_TEXTURE_CUBE_MAP;
}
return GL_TEXTURE_2D_ARRAY;
}
static GLenum GetGLInternalFormatForTextureFormat(AbstractTextureFormat format, bool storage);
GLenum GetGLFormatForImageTexture() const;

View File

@ -60,15 +60,17 @@ void StateTracker::DestroyInstance()
bool StateTracker::Initialize()
{
// Create a dummy texture which can be used in place of a real binding.
m_dummy_texture =
VKTexture::Create(TextureConfig(1, 1, 1, 1, 1, AbstractTextureFormat::RGBA8, 0), "");
m_dummy_texture = VKTexture::Create(TextureConfig(1, 1, 1, 1, 1, AbstractTextureFormat::RGBA8, 0,
AbstractTextureType::Texture_2DArray),
"");
if (!m_dummy_texture)
return false;
m_dummy_texture->TransitionToLayout(g_command_buffer_mgr->GetCurrentInitCommandBuffer(),
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
// Create a dummy compute texture which can be used in place of a real binding
m_dummy_compute_texture = VKTexture::Create(
TextureConfig(1, 1, 1, 1, 1, AbstractTextureFormat::RGBA8, AbstractTextureFlag_ComputeImage),
TextureConfig(1, 1, 1, 1, 1, AbstractTextureFormat::RGBA8, AbstractTextureFlag_ComputeImage,
AbstractTextureType::Texture_2DArray),
"");
if (!m_dummy_compute_texture)
return false;

View File

@ -428,8 +428,9 @@ bool SwapChain::SetupSwapChainImages()
images.data());
ASSERT(res == VK_SUCCESS);
const TextureConfig texture_config(TextureConfig(
m_width, m_height, 1, m_layers, 1, m_texture_format, AbstractTextureFlag_RenderTarget));
const TextureConfig texture_config(
TextureConfig(m_width, m_height, 1, m_layers, 1, m_texture_format,
AbstractTextureFlag_RenderTarget, AbstractTextureType::Texture_2DArray));
const VkRenderPass load_render_pass = g_object_cache->GetRenderPass(
m_surface_format.format, VK_FORMAT_UNDEFINED, 1, VK_ATTACHMENT_LOAD_OP_LOAD);
const VkRenderPass clear_render_pass = g_object_cache->GetRenderPass(

View File

@ -70,8 +70,9 @@ std::unique_ptr<VKTexture> VKTexture::Create(const TextureConfig& tex_config, st
VkImageCreateInfo image_info = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
nullptr,
tex_config.IsCubeMap() ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT :
static_cast<VkImageCreateFlags>(0),
tex_config.type == AbstractTextureType::Texture_CubeMap ?
VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT :
static_cast<VkImageCreateFlags>(0),
VK_IMAGE_TYPE_2D,
GetVkFormatForHostTextureFormat(tex_config.format),
{tex_config.width, tex_config.height, 1},
@ -107,8 +108,26 @@ std::unique_ptr<VKTexture> VKTexture::Create(const TextureConfig& tex_config, st
std::unique_ptr<VKTexture> texture = std::make_unique<VKTexture>(
tex_config, alloc, image, name, VK_IMAGE_LAYOUT_UNDEFINED, ComputeImageLayout::Undefined);
if (!texture->CreateView(tex_config.IsCubeMap() ? VK_IMAGE_VIEW_TYPE_CUBE :
VK_IMAGE_VIEW_TYPE_2D_ARRAY))
VkImageViewType image_view_type = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
if (tex_config.type == AbstractTextureType::Texture_CubeMap)
{
image_view_type = VK_IMAGE_VIEW_TYPE_CUBE;
}
else if (tex_config.type == AbstractTextureType::Texture_2D)
{
image_view_type = VK_IMAGE_VIEW_TYPE_2D;
}
else if (tex_config.type == AbstractTextureType::Texture_2DArray)
{
image_view_type = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
}
else
{
PanicAlertFmt("Unhandled texture type.");
return nullptr;
}
if (!texture->CreateView(image_view_type))
return nullptr;
return texture;