Skip to content

Commit

Permalink
GraphicsAccessories: added ComputeRenderTargetFormatsHash function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Feb 2, 2025
1 parent 235fabe commit 088da89
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -948,4 +948,6 @@ SHADER_STATUS GetPipelineStateCreateInfoShadersStatus(const CreateInfoType& CI,
return OverallStatus;
}

size_t ComputeRenderTargetFormatsHash(Uint32 NumRenderTargets, const TEXTURE_FORMAT RTVFormats[], TEXTURE_FORMAT DSVFormat);

} // namespace Diligent
88 changes: 49 additions & 39 deletions Graphics/GraphicsAccessories/src/GraphicsAccessories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "BasicMath.hpp"
#include "Cast.hpp"
#include "StringTools.hpp"
#include "HashUtils.hpp"

namespace Diligent
{
Expand Down Expand Up @@ -427,7 +428,7 @@ const TextureFormatAttribs& GetTextureFormatAttribs(TEXTURE_FORMAT Format)
{
if (Format >= TEX_FORMAT_UNKNOWN && Format < TEX_FORMAT_NUM_FORMATS)
{
const auto& Attribs = FmtAttribs[Format];
const TextureFormatAttribs& Attribs = FmtAttribs[Format];
VERIFY(Attribs.Format == Format, "Unexpected format");
return Attribs;
}
Expand Down Expand Up @@ -1268,7 +1269,7 @@ String GetTextureDescString(const TextureDesc& Desc)
Str += ToString(Desc.ArraySize);
}

auto FmtName = GetTextureFormatAttribs(Desc.Format).Name;
const char* FmtName = GetTextureFormatAttribs(Desc.Format).Name;
Str += "; Format: ";
Str += FmtName;

Expand Down Expand Up @@ -1443,9 +1444,9 @@ String GetResourceStateString(RESOURCE_STATE State)
if (!str.empty())
str.push_back('|');

auto lsb = State & ~(State - 1);
Uint32 lsb = State & ~(State - 1);

const auto* StateFlagString = GetResourceStateFlagString(static_cast<RESOURCE_STATE>(lsb));
const char* StateFlagString = GetResourceStateFlagString(static_cast<RESOURCE_STATE>(lsb));
str.append(StateFlagString);
State = static_cast<RESOURCE_STATE>(State & ~lsb);
}
Expand Down Expand Up @@ -1726,7 +1727,7 @@ static void PrintShaderCodeVariables(std::stringstream& ss, size_t LevelIdent, s
int MaxBasicTypeLen = 0;
for (Uint32 i = 0; i < NumVars; ++i)
{
const auto& Var = pVars[i];
const ShaderCodeVariableDesc& Var = pVars[i];
if (Var.Name != nullptr)
MaxNameLen = std::max(MaxNameLen, static_cast<int>(strlen(Var.Name)));
if (Var.TypeName != nullptr)
Expand All @@ -1739,7 +1740,7 @@ static void PrintShaderCodeVariables(std::stringstream& ss, size_t LevelIdent, s

for (Uint32 i = 0; i < NumVars; ++i)
{
const auto& Var = pVars[i];
const ShaderCodeVariableDesc& Var = pVars[i];
ss << std::setw(static_cast<int>(LevelIdent) + MaxNameLen) << (Var.Name ? Var.Name : "?")
<< ": " << std::setw(MaxTypeLen) << (Var.TypeName ? Var.TypeName : "")
<< ' ' << std::setw(MaxClassLen) << GetShaderCodeVariableClassString(Var.Class)
Expand Down Expand Up @@ -1864,7 +1865,7 @@ BIND_FLAGS SwapChainUsageFlagsToBindFlags(SWAP_CHAIN_USAGE_FLAGS SwapChainUsage)
static_assert(SWAP_CHAIN_USAGE_LAST == 8, "Did you add a new swap chain usage flag? Please handle it here.");
while (SwapChainUsage != SWAP_CHAIN_USAGE_NONE)
{
auto SCUsageBit = ExtractLSB(SwapChainUsage);
SWAP_CHAIN_USAGE_FLAGS SCUsageBit = ExtractLSB(SwapChainUsage);
switch (SCUsageBit)
{
case SWAP_CHAIN_USAGE_RENDER_TARGET:
Expand Down Expand Up @@ -2054,8 +2055,8 @@ if ( (State & ExclusiveState) != 0 && (State & ~ExclusiveState) != 0 )\

MipLevelProperties GetMipLevelProperties(const TextureDesc& TexDesc, Uint32 MipLevel)
{
MipLevelProperties MipProps;
const auto& FmtAttribs = GetTextureFormatAttribs(TexDesc.Format);
MipLevelProperties MipProps;
const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs(TexDesc.Format);

MipProps.LogicalWidth = std::max(TexDesc.GetWidth() >> MipLevel, 1u);
MipProps.LogicalHeight = std::max(TexDesc.GetHeight() >> MipLevel, 1u);
Expand Down Expand Up @@ -2366,7 +2367,7 @@ Uint64 GetStagingTextureLocationOffset(const TextureDesc& TexDesc,
Uint64 ArraySliceSize = 0;
for (Uint32 mip = 0; mip < TexDesc.MipLevels; ++mip)
{
auto MipInfo = GetMipLevelProperties(TexDesc, mip);
MipLevelProperties MipInfo = GetMipLevelProperties(TexDesc, mip);
ArraySliceSize += AlignUp(MipInfo.MipSize, Alignment);
}

Expand All @@ -2377,7 +2378,7 @@ Uint64 GetStagingTextureLocationOffset(const TextureDesc& TexDesc,

for (Uint32 mip = 0; mip < MipLevel; ++mip)
{
auto MipInfo = GetMipLevelProperties(TexDesc, mip);
MipLevelProperties MipInfo = GetMipLevelProperties(TexDesc, mip);
Offset += AlignUp(MipInfo.MipSize, Alignment);
}

Expand All @@ -2388,8 +2389,8 @@ Uint64 GetStagingTextureLocationOffset(const TextureDesc& TexDesc,
}
else if (LocationX != 0 || LocationY != 0 || LocationZ != 0)
{
const auto& MipLevelAttribs = GetMipLevelProperties(TexDesc, MipLevel);
const auto& FmtAttribs = GetTextureFormatAttribs(TexDesc.Format);
const MipLevelProperties& MipLevelAttribs = GetMipLevelProperties(TexDesc, MipLevel);
const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs(TexDesc.Format);
VERIFY(LocationX < MipLevelAttribs.LogicalWidth && LocationY < MipLevelAttribs.LogicalHeight && LocationZ < MipLevelAttribs.Depth,
"Specified location is out of bounds");
if (FmtAttribs.ComponentType == COMPONENT_TYPE_COMPRESSED)
Expand Down Expand Up @@ -2419,18 +2420,18 @@ BufferToTextureCopyInfo GetBufferToTextureCopyInfo(TEXTURE_FORMAT Format,
{
BufferToTextureCopyInfo CopyInfo;

const auto& FmtAttribs = GetTextureFormatAttribs(Format);
const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs(Format);
VERIFY_EXPR(Region.IsValid());
const auto UpdateRegionWidth = Region.Width();
const auto UpdateRegionHeight = Region.Height();
const auto UpdateRegionDepth = Region.Depth();
const Uint32 UpdateRegionWidth = Region.Width();
const Uint32 UpdateRegionHeight = Region.Height();
const Uint32 UpdateRegionDepth = Region.Depth();
if (FmtAttribs.ComponentType == COMPONENT_TYPE_COMPRESSED)
{
// Align update region size by the block size
VERIFY_EXPR(IsPowerOfTwo(FmtAttribs.BlockWidth));
VERIFY_EXPR(IsPowerOfTwo(FmtAttribs.BlockHeight));
const auto BlockAlignedRegionWidth = AlignUp(UpdateRegionWidth, Uint32{FmtAttribs.BlockWidth});
const auto BlockAlignedRegionHeight = AlignUp(UpdateRegionHeight, Uint32{FmtAttribs.BlockHeight});
const Uint32 BlockAlignedRegionWidth = AlignUp(UpdateRegionWidth, Uint32{FmtAttribs.BlockWidth});
const Uint32 BlockAlignedRegionHeight = AlignUp(UpdateRegionHeight, Uint32{FmtAttribs.BlockHeight});

CopyInfo.RowSize = Uint64{BlockAlignedRegionWidth} / Uint32{FmtAttribs.BlockWidth} * Uint32{FmtAttribs.ComponentSize};
CopyInfo.RowCount = BlockAlignedRegionHeight / FmtAttribs.BlockHeight;
Expand Down Expand Up @@ -2472,8 +2473,8 @@ void CopyTextureSubresource(const TextureSubResData& SrcSubres,
VERIFY(DstRowStride >= RowSize, "Dst data row stride (", DstRowStride, ") is smaller than the row size (", RowSize, ")");
for (Uint32 z = 0; z < NumDepthSlices; ++z)
{
const auto* pSrcSlice = reinterpret_cast<const Uint8*>(SrcSubres.pData) + SrcSubres.DepthStride * z;
auto* pDstSlice = reinterpret_cast<Uint8*>(pDstData) + DstDepthStride * z;
const Uint8* pSrcSlice = reinterpret_cast<const Uint8*>(SrcSubres.pData) + SrcSubres.DepthStride * z;
Uint8* pDstSlice = reinterpret_cast<Uint8*>(pDstData) + DstDepthStride * z;

for (Uint32 y = 0; y < NumRows; ++y)
{
Expand Down Expand Up @@ -2706,7 +2707,7 @@ String GetPipelineShadingRateFlagsString(PIPELINE_SHADING_RATE_FLAGS Flags)
String Result;
while (Flags != PIPELINE_SHADING_RATE_FLAG_NONE)
{
auto Bit = ExtractLSB(Flags);
PIPELINE_SHADING_RATE_FLAGS Bit = ExtractLSB(Flags);

if (!Result.empty())
Result += " | ";
Expand Down Expand Up @@ -2756,7 +2757,7 @@ bool TextureComponentMappingFromString(const String& MappingStr, TextureComponen

for (size_t Comp = 0; Comp < MappingStr.length(); ++Comp)
{
const auto Chr = MappingStr[Comp];
const char Chr = MappingStr[Comp];
if (Chr == 'r' || Chr == 'R')
Mapping[Comp] = Comp == 0 ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : TEXTURE_COMPONENT_SWIZZLE_R;
else if (Chr == 'g' || Chr == 'G')
Expand All @@ -2777,9 +2778,9 @@ bool TextureComponentMappingFromString(const String& MappingStr, TextureComponen

SparseTextureProperties GetStandardSparseTextureProperties(const TextureDesc& TexDesc)
{
constexpr Uint32 SparseBlockSize = 64 << 10;
const auto& FmtAttribs = GetTextureFormatAttribs(TexDesc.Format);
const Uint32 TexelSize = FmtAttribs.GetElementSize();
constexpr Uint32 SparseBlockSize = 64 << 10;
const TextureFormatAttribs& FmtAttribs = GetTextureFormatAttribs(TexDesc.Format);
const Uint32 TexelSize = FmtAttribs.GetElementSize();
VERIFY_EXPR(IsPowerOfTwo(TexelSize));
VERIFY_EXPR(TexelSize >= 1 && TexelSize <= 16);
VERIFY_EXPR(TexDesc.Is2D() || TexDesc.Is3D());
Expand Down Expand Up @@ -2860,7 +2861,7 @@ SparseTextureProperties GetStandardSparseTextureProperties(const TextureDesc& Te
}
}

const auto BytesPerTile =
const Uint32 BytesPerTile =
(Props.TileSize[0] / FmtAttribs.BlockWidth) *
(Props.TileSize[1] / FmtAttribs.BlockHeight) *
Props.TileSize[2] * TexDesc.SampleCount * TexelSize;
Expand All @@ -2870,14 +2871,14 @@ SparseTextureProperties GetStandardSparseTextureProperties(const TextureDesc& Te
Props.FirstMipInTail = ~0u;
for (Uint32 Mip = 0; Mip < TexDesc.MipLevels; ++Mip)
{
const auto MipProps = GetMipLevelProperties(TexDesc, Mip);
const auto MipWidth = MipProps.StorageWidth;
const auto MipHeight = MipProps.StorageHeight;
const auto MipDepth = MipProps.Depth;
const MipLevelProperties MipProps = GetMipLevelProperties(TexDesc, Mip);
const Uint32 MipWidth = MipProps.StorageWidth;
const Uint32 MipHeight = MipProps.StorageHeight;
const Uint32 MipDepth = MipProps.Depth;

// When the size of a texture mipmap level is at least one standard tile shape for its
// format, the mipmap level is guaranteed to be nonpacked.
const auto IsUnpacked =
const bool IsUnpacked =
MipWidth >= Props.TileSize[0] &&
MipHeight >= Props.TileSize[1] &&
MipDepth >= Props.TileSize[2];
Expand All @@ -2894,7 +2895,7 @@ SparseTextureProperties GetStandardSparseTextureProperties(const TextureDesc& Te
}
else
{
const auto NumTilesInMip = GetNumSparseTilesInBox(Box{0, MipWidth, 0, MipHeight, 0, MipDepth}, Props.TileSize);
const uint3 NumTilesInMip = GetNumSparseTilesInBox(Box{0, MipWidth, 0, MipHeight, 0, MipDepth}, Props.TileSize);
SliceSize += Uint64{NumTilesInMip.x} * NumTilesInMip.y * NumTilesInMip.z * SparseBlockSize;
}
}
Expand Down Expand Up @@ -2937,13 +2938,13 @@ std::vector<Uint32> ResolveInputLayoutAutoOffsetsAndStrides(LayoutElement* pLayo

for (Uint32 i = 0; i < NumElements; ++i)
{
auto& LayoutElem = pLayoutElements[i];
LayoutElement& LayoutElem = pLayoutElements[i];

if (LayoutElem.ValueType == VT_FLOAT32 || LayoutElem.ValueType == VT_FLOAT16)
LayoutElem.IsNormalized = false; // Floating point values cannot be normalized

auto BuffSlot = LayoutElem.BufferSlot;
auto& CurrAutoStride = TightStrides[BuffSlot];
Uint32 BuffSlot = LayoutElem.BufferSlot;
Uint32& CurrAutoStride = TightStrides[BuffSlot];
// If offset is not explicitly specified, use current auto stride value
if (LayoutElem.RelativeOffset == LAYOUT_ELEMENT_AUTO_OFFSET)
{
Expand All @@ -2970,9 +2971,9 @@ std::vector<Uint32> ResolveInputLayoutAutoOffsetsAndStrides(LayoutElement* pLayo

for (Uint32 i = 0; i < NumElements; ++i)
{
auto& LayoutElem = pLayoutElements[i];
LayoutElement& LayoutElem = pLayoutElements[i];

auto BuffSlot = LayoutElem.BufferSlot;
Uint32 BuffSlot = LayoutElem.BufferSlot;
// If no input elements explicitly defined stride for this buffer slot, use automatic stride
if (Strides[BuffSlot] == LAYOUT_ELEMENT_AUTO_STRIDE)
{
Expand All @@ -2992,7 +2993,7 @@ std::vector<Uint32> ResolveInputLayoutAutoOffsetsAndStrides(LayoutElement* pLayo
}

// Set strides for all unused slots to 0
for (auto& Stride : Strides)
for (Uint32& Stride : Strides)
{
if (Stride == LAYOUT_ELEMENT_AUTO_STRIDE)
Stride = 0;
Expand All @@ -3001,4 +3002,13 @@ std::vector<Uint32> ResolveInputLayoutAutoOffsetsAndStrides(LayoutElement* pLayo
return Strides;
}

size_t ComputeRenderTargetFormatsHash(Uint32 NumRenderTargets, const TEXTURE_FORMAT RTVFormats[], TEXTURE_FORMAT DSVFormat)
{
size_t Hash = ComputeHash(NumRenderTargets);
for (Uint32 rt = 0; rt < NumRenderTargets; ++rt)
HashCombine(Hash, RTVFormats[rt]);
HashCombine(Hash, DSVFormat);
return Hash;
}

} // namespace Diligent
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2024 Diligent Graphics LLC
* Copyright 2019-2025 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -26,6 +26,8 @@
*/

#include <array>
#include <unordered_set>
#include <vector>

#include "GraphicsAccessories.hpp"
#include "../../../../Graphics/GraphicsEngine/include/PrivateConstants.h"
Expand Down Expand Up @@ -1527,4 +1529,36 @@ TEST(GraphicsAccessories_GraphicsAccessories, ProcessPipelineStateCreateInfoShad
}
}



TEST(GraphicsAccessories_GraphicsAccessories, ComputeRenderTargetFormatsHash)
{
std::unordered_set<size_t> Hashes;

auto Test = [&Hashes](Uint32 NumRenderTargets, const TEXTURE_FORMAT RTVFormats[], TEXTURE_FORMAT DepthFormat) {
size_t Hash = ComputeRenderTargetFormatsHash(NumRenderTargets, RTVFormats, DepthFormat);
EXPECT_NE(Hash, size_t{0});
EXPECT_TRUE(Hashes.insert(Hash).second);
};

for (TEXTURE_FORMAT DsvFmt : {TEX_FORMAT_UNKNOWN, TEX_FORMAT_D16_UNORM, TEX_FORMAT_D32_FLOAT})
{
constexpr TEXTURE_FORMAT RTVs[] = {
TEX_FORMAT_RGBA8_UNORM_SRGB,
TEX_FORMAT_RGBA16_FLOAT,
TEX_FORMAT_RG8_UNORM,
TEX_FORMAT_RG16_UNORM,
TEX_FORMAT_R32_FLOAT,
TEX_FORMAT_RG16_FLOAT,
TEX_FORMAT_R8_UINT,
TEX_FORMAT_RG16_SINT,
};
for (Uint32 NumRTVs = 0; NumRTVs <= _countof(RTVs); ++NumRTVs)
{
Test(NumRTVs, RTVs, DsvFmt);
}
}
EXPECT_EQ(Hashes.size(), size_t{3 * 9});
}

} // namespace

0 comments on commit 088da89

Please sign in to comment.