diff --git a/Graphics/GraphicsAccessories/interface/GraphicsAccessories.hpp b/Graphics/GraphicsAccessories/interface/GraphicsAccessories.hpp index 6f746e7df4..f5c9de9579 100644 --- a/Graphics/GraphicsAccessories/interface/GraphicsAccessories.hpp +++ b/Graphics/GraphicsAccessories/interface/GraphicsAccessories.hpp @@ -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); diff --git a/Graphics/GraphicsAccessories/src/GraphicsAccessories.cpp b/Graphics/GraphicsAccessories/src/GraphicsAccessories.cpp index fe58b2f8d8..2e918e6a2e 100644 --- a/Graphics/GraphicsAccessories/src/GraphicsAccessories.cpp +++ b/Graphics/GraphicsAccessories/src/GraphicsAccessories.cpp @@ -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; diff --git a/Tests/DiligentCoreTest/src/GraphicsAccessories/GraphicsAccessoriesTest.cpp b/Tests/DiligentCoreTest/src/GraphicsAccessories/GraphicsAccessoriesTest.cpp index ff1f016e9f..7ca25b6a10 100644 --- a/Tests/DiligentCoreTest/src/GraphicsAccessories/GraphicsAccessoriesTest.cpp +++ b/Tests/DiligentCoreTest/src/GraphicsAccessories/GraphicsAccessoriesTest.cpp @@ -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(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(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(Swizzle); + RefMapping[1] = Swizzle == TEXTURE_COMPONENT_SWIZZLE_G ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : static_cast(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(Swizzle); + RefMapping[1] = Swizzle == TEXTURE_COMPONENT_SWIZZLE_G ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : static_cast(Swizzle); + RefMapping[2] = Swizzle == TEXTURE_COMPONENT_SWIZZLE_B ? TEXTURE_COMPONENT_SWIZZLE_IDENTITY : static_cast(Swizzle); + EXPECT_EQ(TestMapping, RefMapping); + } + + for (size_t Comp = 0; Comp < 4; ++Comp) + { + TextureComponentMapping Mapping{TextureComponentMapping::Identity()}; + Mapping[Comp] = static_cast(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)