Skip to content

Commit

Permalink
Added helpers to deal with typeless depth
Browse files Browse the repository at this point in the history
  • Loading branch information
adepke committed Apr 7, 2022
1 parent db09a68 commit 9123c8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
30 changes: 26 additions & 4 deletions VanguardEngine/Source/Rendering/ResourceFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <dxgiformat.h>

// Returns the format size in bits.
uint32_t GetResourceFormatSize(DXGI_FORMAT format)
inline uint32_t GetResourceFormatSize(DXGI_FORMAT format)
{
switch (format)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -136,7 +136,7 @@ bool IsResourceFormatSRGB(DXGI_FORMAT format)
}
}

DXGI_FORMAT ConvertResourceFormatToSRGB(DXGI_FORMAT linearFormat)
inline DXGI_FORMAT ConvertResourceFormatToSRGB(DXGI_FORMAT linearFormat)
{
switch (linearFormat)
{
Expand All @@ -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)
{
Expand All @@ -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;
}
}
12 changes: 2 additions & 10 deletions VanguardEngine/Source/Rendering/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
{
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9123c8c

Please sign in to comment.