Skip to content

Commit

Permalink
#6052: MaterialSourceGenerator is now able to use DECAL_MACRO if the …
Browse files Browse the repository at this point in the history
…material settings are applicable
  • Loading branch information
codereader committed Aug 5, 2022
1 parent 42cb7ae commit 0e4fc6d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 19 additions & 2 deletions radiantcore/shaders/MaterialSourceGenerator.cpp
Expand Up @@ -462,17 +462,30 @@ std::ostream& operator<<(std::ostream& stream, ShaderTemplate& shaderTemplate)
stream << "\n";
}

// Macros go first
bool hasDecalMacro = shaderTemplate.getParseFlags() & Material::PF_HasDecalMacro;

if (hasDecalMacro)
{
stream << "\t" << "DECAL_MACRO" << "\n";
}

// Go through the material flags which reflect a single keyword
for (const auto& pair : shaders::MaterialFlagKeywords)
{
// Skip exporting noShadows is DECAL_MACRO is active
if (hasDecalMacro && pair.second == Material::FLAG_NOSHADOWS) continue;

if (shaderTemplate.getMaterialFlags() & pair.second)
{
stream << "\t" << pair.first << "\n";
}
}

// Polygon Offset
if (shaderTemplate.getMaterialFlags() & Material::FLAG_POLYGONOFFSET)
// DECAL_MACRO implies polygonOffset 1, prevent writing redundant information
if ((shaderTemplate.getMaterialFlags() & Material::FLAG_POLYGONOFFSET) != 0 &&
(shaderTemplate.getPolygonOffset() != 1 || !hasDecalMacro))
{
stream << fmt::format("\tpolygonOffset {0}\n", shaderTemplate.getPolygonOffset());
}
Expand Down Expand Up @@ -513,7 +526,8 @@ std::ostream& operator<<(std::ostream& stream, ShaderTemplate& shaderTemplate)
}

// Sort
if (shaderTemplate.getMaterialFlags() & Material::FLAG_HAS_SORT_DEFINED)
if (shaderTemplate.getMaterialFlags() & Material::FLAG_HAS_SORT_DEFINED &&
(!hasDecalMacro || shaderTemplate.getSortRequest() != Material::SORT_DECAL))
{
stream << "\tsort ";

Expand Down Expand Up @@ -638,6 +652,9 @@ std::ostream& operator<<(std::ostream& stream, ShaderTemplate& shaderTemplate)
// Surface flags
for (const auto& pair : SurfaceFlags)
{
// Skip exporting "discrete" is DECAL_MACRO is active
if (hasDecalMacro && pair.second == Material::SURF_DISCRETE) continue;

if (shaderTemplate.getSurfaceFlags() & pair.second)
{
stream << "\t" << pair.first << "\n";
Expand Down
5 changes: 4 additions & 1 deletion test/MaterialExport.cpp
Expand Up @@ -361,8 +361,11 @@ TEST_F(MaterialExportTest, DecalMacroUsage)
EXPECT_FALSE(material->getParseFlags() & Material::PF_HasDecalMacro);

material->setMaterialFlag(Material::FLAG_NOSHADOWS);
expectDefinitionContains(material, "DECAL_MACRO");
EXPECT_TRUE(material->getParseFlags() & Material::PF_HasDecalMacro);
expectDefinitionContains(material, "DECAL_MACRO");

// The implied keywords should be gone from the definition
expectDefinitionDoesNotContainAnyOf(material, { "discrete", "noShadows", "polygonOffset 1", "sort decal" });

// Setting decalInfo doesn't influence DECAL_MACRO
Material::DecalInfo info;
Expand Down

0 comments on commit 0e4fc6d

Please sign in to comment.