From 9123c8c0c2272abeb3dbc9e591332afa1043182d Mon Sep 17 00:00:00 2001 From: Andrew Depke Date: Wed, 6 Apr 2022 22:29:34 -0600 Subject: [PATCH] Added helpers to deal with typeless depth --- .../Source/Rendering/ResourceFormat.h | 30 ++++++++++++++++--- .../Source/Rendering/ResourceManager.cpp | 12 ++------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/VanguardEngine/Source/Rendering/ResourceFormat.h b/VanguardEngine/Source/Rendering/ResourceFormat.h index 89910bd..81d3dc4 100644 --- a/VanguardEngine/Source/Rendering/ResourceFormat.h +++ b/VanguardEngine/Source/Rendering/ResourceFormat.h @@ -5,7 +5,7 @@ #include // Returns the format size in bits. -uint32_t GetResourceFormatSize(DXGI_FORMAT format) +inline uint32_t GetResourceFormatSize(DXGI_FORMAT format) { switch (format) { @@ -119,7 +119,7 @@ uint32_t GetResourceFormatSize(DXGI_FORMAT format) return 0; } -bool IsResourceFormatSRGB(DXGI_FORMAT format) +inline bool IsResourceFormatSRGB(DXGI_FORMAT format) { switch (format) { @@ -136,7 +136,7 @@ bool IsResourceFormatSRGB(DXGI_FORMAT format) } } -DXGI_FORMAT ConvertResourceFormatToSRGB(DXGI_FORMAT linearFormat) +inline DXGI_FORMAT ConvertResourceFormatToSRGB(DXGI_FORMAT linearFormat) { switch (linearFormat) { @@ -152,7 +152,7 @@ DXGI_FORMAT ConvertResourceFormatToSRGB(DXGI_FORMAT linearFormat) return DXGI_FORMAT_UNKNOWN; } -DXGI_FORMAT ConvertResourceFormatToLinear(DXGI_FORMAT sRGBFormat) +inline DXGI_FORMAT ConvertResourceFormatToLinear(DXGI_FORMAT sRGBFormat) { switch (sRGBFormat) { @@ -166,4 +166,26 @@ DXGI_FORMAT ConvertResourceFormatToLinear(DXGI_FORMAT sRGBFormat) } return DXGI_FORMAT_UNKNOWN; +} + +inline DXGI_FORMAT ConvertResourceFormatToTypedDepth(DXGI_FORMAT typelessDepthFormat) +{ + // If the given format is typeless, we need to convert it to typed depth. + switch (typelessDepthFormat) + { + case DXGI_FORMAT_R32_TYPELESS: return DXGI_FORMAT_D32_FLOAT; + case DXGI_FORMAT_R24G8_TYPELESS: return DXGI_FORMAT_D24_UNORM_S8_UINT; + default: return typelessDepthFormat; + } +} + +inline DXGI_FORMAT ConvertResourceFormatToTypedNonDepth(DXGI_FORMAT typelessDepthFormat) +{ + // If the given format is typeless, we need to convert it to typed non-depth. + switch (typelessDepthFormat) + { + case DXGI_FORMAT_R32_TYPELESS: return DXGI_FORMAT_R32_FLOAT; + case DXGI_FORMAT_R24G8_TYPELESS: return DXGI_FORMAT_R24_UNORM_X8_TYPELESS; + default: return typelessDepthFormat; + } } \ No newline at end of file diff --git a/VanguardEngine/Source/Rendering/ResourceManager.cpp b/VanguardEngine/Source/Rendering/ResourceManager.cpp index 8d54c57..0e279ce 100644 --- a/VanguardEngine/Source/Rendering/ResourceManager.cpp +++ b/VanguardEngine/Source/Rendering/ResourceManager.cpp @@ -127,11 +127,7 @@ void ResourceManager::CreateResourceViews(TextureComponent& target) viewDesc.Format = target.description.format; // If the given format isn't a depth format, we need to convert. - switch (viewDesc.Format) - { - case DXGI_FORMAT_R32_TYPELESS: viewDesc.Format = DXGI_FORMAT_D32_FLOAT; break; - case DXGI_FORMAT_R24G8_TYPELESS: viewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; break; - } + viewDesc.Format = ConvertResourceFormatToTypedDepth(viewDesc.Format); switch (target.Native()->GetDesc().Dimension) // #TODO: Support texture arrays and multi-sample textures. { @@ -161,11 +157,7 @@ void ResourceManager::CreateResourceViews(TextureComponent& target) // Using a depth stencil via SRV requires special formatting. if (target.description.bindFlags & BindFlag::DepthStencil) { - switch (viewDesc.Format) - { - case DXGI_FORMAT_R32_TYPELESS: viewDesc.Format = DXGI_FORMAT_R32_FLOAT; break; - case DXGI_FORMAT_R24G8_TYPELESS: viewDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS; break; - } + viewDesc.Format = ConvertResourceFormatToTypedNonDepth(viewDesc.Format); } switch (target.Native()->GetDesc().Dimension) // #TODO: Support texture arrays and multi-sample textures.