Skip to content

Commit

Permalink
Added TextureComponentMappingFromString function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Dec 7, 2023
1 parent 828055b commit b5de158
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,17 @@ TEXTURE_FORMAT TexFormatToSRGB(TEXTURE_FORMAT Fmt);

String GetPipelineShadingRateFlagsString(PIPELINE_SHADING_RATE_FLAGS Flags);

/// Converts texture component mapping to a string, for example:
/// {R, G, B, A} -> "rgba"
/// {R, G, B, 1} -> "rgb1"
String GetTextureComponentMappingString(const TextureComponentMapping& Mapping);

/// Converts texture component mapping string to the mapping, for example:
/// "rgba" -> {R, G, B, A}
/// "rgb1" -> {R, G, B, 1}
bool TextureComponentMappingFromString(const String& MappingStr, TextureComponentMapping& Mapping);


/// Returns the sparse texture properties assuming the standard tile shapes
SparseTextureProperties GetStandardSparseTextureProperties(const TextureDesc& TexDesc);

Expand Down
27 changes: 27 additions & 0 deletions Graphics/GraphicsAccessories/src/GraphicsAccessories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2267,6 +2267,33 @@ String GetTextureComponentMappingString(const TextureComponentMapping& Mapping)
return Str;
}

bool TextureComponentMappingFromString(const String& MappingStr, TextureComponentMapping& Mapping)
{
Mapping = TextureComponentMapping::Identity();

bool AllOK = MappingStr.length() <= 4;

for (size_t Comp = 0; Comp < MappingStr.length(); ++Comp)
{
const auto 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')
Mapping[Comp] = Comp == 1 ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : TEXTURE_COMPONENT_SWIZZLE_G;
else if (Chr == 'b' || Chr == 'B')
Mapping[Comp] = Comp == 2 ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : TEXTURE_COMPONENT_SWIZZLE_B;
else if (Chr == 'a' || Chr == 'A')
Mapping[Comp] = Comp == 3 ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : TEXTURE_COMPONENT_SWIZZLE_A;
else if (Chr == '0')
Mapping[Comp] = TEXTURE_COMPONENT_SWIZZLE_ZERO;
else if (Chr == '1')
Mapping[Comp] = TEXTURE_COMPONENT_SWIZZLE_ONE;
else
AllOK = false;
}
return AllOK;
}

SparseTextureProperties GetStandardSparseTextureProperties(const TextureDesc& TexDesc)
{
constexpr Uint32 SparseBlockSize = 64 << 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,79 @@ TEST(GraphicsAccessories_GraphicsAccessories, GetTextureComponentMappingString)
TextureComponentMapping(TEXTURE_COMPONENT_SWIZZLE_IDENTITY, TEXTURE_COMPONENT_SWIZZLE_IDENTITY, TEXTURE_COMPONENT_SWIZZLE_IDENTITY, TEXTURE_COMPONENT_SWIZZLE_ONE))
.c_str(),
"rgb1");
for (size_t Comp = 0; Comp < 4; ++Comp)
{
for (Uint32 Swizzle = 0; Swizzle < TEXTURE_COMPONENT_SWIZZLE_COUNT; ++Swizzle)
{
TextureComponentMapping Mapping{TextureComponentMapping::Identity()};
Mapping[Comp] = static_cast<TEXTURE_COMPONENT_SWIZZLE>(Swizzle);
std::string RefStr = "rgba";
if (Swizzle != TEXTURE_COMPONENT_SWIZZLE_IDENTITY)
RefStr[Comp] = " 01rgba"[Swizzle];
EXPECT_STREQ(GetTextureComponentMappingString(Mapping).c_str(), RefStr.c_str());
}
}
}

TEST(GraphicsAccessories_GraphicsAccessories, TextureComponentMappingFromString)
{
{
TextureComponentMapping Mapping;
EXPECT_TRUE(TextureComponentMappingFromString("", Mapping));
EXPECT_EQ(Mapping, TextureComponentMapping::Identity());
}

for (Uint32 Swizzle = 0; Swizzle < TEXTURE_COMPONENT_SWIZZLE_COUNT; ++Swizzle)
{
{
const char* Strings[TEXTURE_COMPONENT_SWIZZLE_COUNT] = {"r", "0", "1", "r", "g", "b", "a"};
TextureComponentMapping TestMapping;
EXPECT_TRUE(TextureComponentMappingFromString(Strings[Swizzle], TestMapping));

TextureComponentMapping RefMapping;
RefMapping[0] = Swizzle == TEXTURE_COMPONENT_SWIZZLE_R ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : static_cast<TEXTURE_COMPONENT_SWIZZLE>(Swizzle);
EXPECT_EQ(TestMapping, RefMapping);
}

{
const char* Strings[TEXTURE_COMPONENT_SWIZZLE_COUNT] = {"rg", "00", "11", "rr", "gg", "bb", "aa"};
TextureComponentMapping TestMapping;
EXPECT_TRUE(TextureComponentMappingFromString(Strings[Swizzle], TestMapping));

TextureComponentMapping RefMapping;
RefMapping[0] = Swizzle == TEXTURE_COMPONENT_SWIZZLE_R ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : static_cast<TEXTURE_COMPONENT_SWIZZLE>(Swizzle);
RefMapping[1] = Swizzle == TEXTURE_COMPONENT_SWIZZLE_G ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : static_cast<TEXTURE_COMPONENT_SWIZZLE>(Swizzle);
EXPECT_EQ(TestMapping, RefMapping);
}

{
const char* Strings[TEXTURE_COMPONENT_SWIZZLE_COUNT] = {"rgb", "000", "111", "rrr", "ggg", "bbb", "aaa"};
TextureComponentMapping TestMapping;
EXPECT_TRUE(TextureComponentMappingFromString(Strings[Swizzle], TestMapping));

TextureComponentMapping RefMapping;
RefMapping[0] = Swizzle == TEXTURE_COMPONENT_SWIZZLE_R ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : static_cast<TEXTURE_COMPONENT_SWIZZLE>(Swizzle);
RefMapping[1] = Swizzle == TEXTURE_COMPONENT_SWIZZLE_G ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : static_cast<TEXTURE_COMPONENT_SWIZZLE>(Swizzle);
RefMapping[2] = Swizzle == TEXTURE_COMPONENT_SWIZZLE_B ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : static_cast<TEXTURE_COMPONENT_SWIZZLE>(Swizzle);
EXPECT_EQ(TestMapping, RefMapping);
}

for (size_t Comp = 0; Comp < 4; ++Comp)
{
TextureComponentMapping Mapping{TextureComponentMapping::Identity()};
Mapping[Comp] = static_cast<TEXTURE_COMPONENT_SWIZZLE>(Swizzle);
std::string Str = GetTextureComponentMappingString(Mapping);
if (Comp == 0 && Swizzle == TEXTURE_COMPONENT_SWIZZLE_R ||
Comp == 1 && Swizzle == TEXTURE_COMPONENT_SWIZZLE_G ||
Comp == 2 && Swizzle == TEXTURE_COMPONENT_SWIZZLE_B ||
Comp == 3 && Swizzle == TEXTURE_COMPONENT_SWIZZLE_A)
Mapping[Comp] = TEXTURE_COMPONENT_SWIZZLE_IDENTITY;

TextureComponentMapping MappingFromStr;
EXPECT_TRUE(TextureComponentMappingFromString(Str, MappingFromStr));
EXPECT_EQ(Mapping, MappingFromStr);
}
}
}

TEST(GraphicsAccessories_GraphicsAccessories, GetMipLevelProperties)
Expand Down

0 comments on commit b5de158

Please sign in to comment.