diff --git a/build/common/test_list.txt b/build/common/test_list.txt index 80c50761a1..09d40fb208 100644 --- a/build/common/test_list.txt +++ b/build/common/test_list.txt @@ -3,5 +3,6 @@ filament/test/test_filament_exposure --gtest_filter=-FilamentExposureWithEngineT libs/math/test_math libs/image/test_image compare libs/image/tests/reference/ libs/utils/test_utils +libs/filamat/test_filamat tools/matc/test_matc tools/cmgen/test_cmgen compare diff --git a/libs/filamat/include/filamat/MaterialBuilder.h b/libs/filamat/include/filamat/MaterialBuilder.h index 95606f63d0..197289099c 100644 --- a/libs/filamat/include/filamat/MaterialBuilder.h +++ b/libs/filamat/include/filamat/MaterialBuilder.h @@ -120,9 +120,6 @@ public: // set the interpolation mode MaterialBuilder& interpolation(Interpolation interpolation) noexcept; - // declares that this property is modified by the material - MaterialBuilder& set(Property p) noexcept; - // add a parameter (i.e.: a uniform) to this material MaterialBuilder& parameter(UniformType type, const char* name) noexcept; @@ -230,18 +227,18 @@ public: bool isSampler; }; + using PropertyList = bool[filament::MATERIAL_PROPERTIES_COUNT]; + using VariableList = utils::CString[filament::MATERIAL_VARIABLES_COUNT]; + // Preview the first shader that would generated in the MaterialPackage. // This is used to run Static Code Analysis before generating a package. // Outputs the chosen shader model in the model parameter const std::string peek(filament::driver::ShaderType type, - filament::driver::ShaderModel& model) noexcept; + filament::driver::ShaderModel& model, const PropertyList& properties) noexcept; // Returns true if any of the parameter samplers is of type samplerExternal bool hasExternalSampler() const noexcept; - using PropertyList = bool[filament::MATERIAL_PROPERTIES_COUNT]; - using VariableList = utils::CString[filament::MATERIAL_VARIABLES_COUNT]; - static constexpr size_t MAX_PARAMETERS_COUNT = 32; using ParameterList = Parameter[MAX_PARAMETERS_COUNT]; @@ -251,15 +248,15 @@ public: // returns a list of at least getParameterCount() parameters const ParameterList& getParameters() const noexcept { return mParameters; } - TargetApi getTargetApi() const { return mTargetApi; } - - Platform getPlatform() const { return mPlatform; } - uint8_t getVariantFilter() const { return mVariantFilter; } private: void prepareToBuild(MaterialInfo& info) noexcept; + // Return true if: + // The shader is syntactically and semantically valid + bool runStaticCodeAnalysis() noexcept; + bool isLit() const noexcept { return mShading != filament::Shading::UNLIT; } utils::CString mMaterialName; diff --git a/libs/filamat/src/MaterialBuilder.cpp b/libs/filamat/src/MaterialBuilder.cpp index dc32ac93e8..ec7e049540 100644 --- a/libs/filamat/src/MaterialBuilder.cpp +++ b/libs/filamat/src/MaterialBuilder.cpp @@ -117,32 +117,6 @@ MaterialBuilder& MaterialBuilder::interpolation(Interpolation interpolation) noe return *this; } -MaterialBuilder& MaterialBuilder::set(Property p) noexcept { - // Note: switch/case here is useful in case we're given an invalid property - switch (p) { - case Property::BASE_COLOR: - case Property::ROUGHNESS: - case Property::METALLIC: - case Property::REFLECTANCE: - case Property::AMBIENT_OCCLUSION: - case Property::CLEAR_COAT: - case Property::CLEAR_COAT_ROUGHNESS: - case Property::CLEAR_COAT_NORMAL: - case Property::ANISOTROPY: - case Property::ANISOTROPY_DIRECTION: - case Property::THICKNESS: - case Property::SUBSURFACE_POWER: - case Property::SUBSURFACE_COLOR: - case Property::SHEEN_COLOR: - case Property::EMISSIVE: - case Property::NORMAL: - assert(size_t(p) < filament::MATERIAL_PROPERTIES_COUNT); - mProperties[size_t(p)] = true; - break; - } - return *this; -} - MaterialBuilder& MaterialBuilder::variable(Variable v, const char* name) noexcept { switch (v) { case Variable::CUSTOM0: @@ -319,6 +293,28 @@ void MaterialBuilder::prepareToBuild(MaterialInfo& info) noexcept { info.samplerBindings.populate(&info.sib, mMaterialName.c_str()); } +bool MaterialBuilder::runStaticCodeAnalysis() noexcept { + using namespace filament::driver; + + GLSLTools glslTools; + + // Populate mProperties with the properties set in the shader. + if (!glslTools.findProperties(*this, mProperties, mTargetApi)) { + return false; + } + + // At this point the shader is syntactically correct. Perform semantic analysis now. + ShaderModel model; + + std::string shaderCode = peek(ShaderType::VERTEX, model, mProperties); + bool result = glslTools.analyzeVertexShader(shaderCode, model, mTargetApi); + if (!result) return result; + + shaderCode = peek(ShaderType::FRAGMENT, model, mProperties); + result = glslTools.analyzeFragmentShader(shaderCode, model, mTargetApi); + return result; +} + static void showErrorMessage(const char* materialName, uint8_t variant, MaterialBuilder::TargetApi targetApi, filament::driver::ShaderType shaderType, const std::string& shaderCode) { @@ -338,14 +334,15 @@ static void showErrorMessage(const char* materialName, uint8_t variant, Package MaterialBuilder::build() noexcept { GLSLTools::init(); - bool errorOccured = false; - - // Populate mProperties with the properties set in the shader. - GLSLTools glslTools; - if (!glslTools.process(*this)) { - errorOccured = true; + if (!runStaticCodeAnalysis()) { + // Return an empty package to signal a failure to build the material. + Package package(0); + package.setValid(false); + return package; } + bool errorOccured = false; + MaterialInfo info; prepareToBuild(info); @@ -580,9 +577,9 @@ MaterialBuilder& MaterialBuilder::postProcessor(PostProcessCallBack callback) { } const std::string MaterialBuilder::peek(filament::driver::ShaderType type, - filament::driver::ShaderModel& model) noexcept { + filament::driver::ShaderModel& model, const PropertyList& properties) noexcept { - ShaderGenerator sg(mProperties, mVariables, + ShaderGenerator sg(properties, mVariables, mMaterialCode, mMaterialLineOffset, mMaterialVertexCode, mMaterialVertexLineOffset); MaterialInfo info; diff --git a/libs/filamat/src/sca/GLSLTools.cpp b/libs/filamat/src/sca/GLSLTools.cpp index cf8ddf5f48..aede8425c3 100644 --- a/libs/filamat/src/sca/GLSLTools.cpp +++ b/libs/filamat/src/sca/GLSLTools.cpp @@ -132,28 +132,6 @@ bool GLSLTools::analyzeVertexShader(const std::string& shaderCode, ShaderModel m return true; } -bool GLSLTools::process(MaterialBuilder& builder) const noexcept { - PropertySet properties; - if (!findProperties(builder, properties)) { - return false; - } - - for (Property property : properties) { - builder.set(property); - } - - // At this point the shader is syntactically correct. Perform semantic analysis now. - ShaderModel model; - - std::string shaderCode = builder.peek(ShaderType::VERTEX, model); - bool result = analyzeVertexShader(shaderCode, model, builder.getTargetApi()); - if (!result) return result; - - shaderCode = builder.peek(ShaderType::FRAGMENT, model); - result = analyzeFragmentShader(shaderCode, model, builder.getTargetApi()); - return result; -} - void GLSLTools::init() { // According to glslang, InitializeProcess should be called exactly once per process. static bool initializeCalled = false; @@ -163,23 +141,19 @@ void GLSLTools::init() { } } -void GLSLTools::terminate() { - FinalizeProcess(); -} - -bool GLSLTools::findProperties(const filamat::MaterialBuilder& builderIn, PropertySet& properties) - const noexcept { +bool GLSLTools::findProperties(const filamat::MaterialBuilder& builderIn, + MaterialBuilder::PropertyList& properties, + MaterialBuilder::TargetApi targetApi) const noexcept { + filamat::MaterialBuilder builder(builderIn); // Some fields in MaterialInputs only exist if the property is set (e.g: normal, subsurface - // for cloth shading model). Copy the builder and give our shader all properties. This will - // enable us to parse and static code analyse the AST. - filamat::MaterialBuilder builder(builderIn); - for (auto hint : Enums::map()) { - builder.set(hint.second); - } + // for cloth shading model). Give our shader all properties. This will enable us to parse and + // static code analyse the AST. + MaterialBuilder::PropertyList allProperties; + std::fill_n(allProperties, filament::MATERIAL_PROPERTIES_COUNT, true); ShaderModel model; - std::string shaderCode = builder.peek(ShaderType::FRAGMENT, model); + std::string shaderCode = builder.peek(ShaderType::FRAGMENT, model, allProperties); const char* shaderCString = shaderCode.c_str(); TShader tShader(EShLanguage::EShLangFragment); @@ -187,7 +161,7 @@ bool GLSLTools::findProperties(const filamat::MaterialBuilder& builderIn, Proper GLSLangCleaner cleaner; int version = glslangVersionFromShaderModel(model); - EShMessages msg = glslangFlagsFromTargetApi(builderIn.getTargetApi()); + EShMessages msg = glslangFlagsFromTargetApi(targetApi); const TBuiltInResource* builtins = &DefaultTBuiltInResource; bool ok = tShader.parse(builtins, version, false, msg); if (!ok) { @@ -205,7 +179,7 @@ bool GLSLTools::findProperties(const filamat::MaterialBuilder& builderIn, Proper } bool GLSLTools::findPropertyWritesOperations(const std::string& functionName, size_t parameterIdx, - TIntermNode* rootNode, PropertySet& properties) const noexcept { + TIntermNode* rootNode, MaterialBuilder::PropertyList& properties) const noexcept { glslang::TIntermAggregate* functionMaterialDef = ASTUtils::getFunctionBySignature(functionName, *rootNode); @@ -258,7 +232,7 @@ bool GLSLTools::findPropertyWritesOperations(const std::string& functionName, si void GLSLTools::scanSymbolForProperty(Symbol& symbol, TIntermNode* rootNode, - PropertySet& properties) const noexcept { + MaterialBuilder::PropertyList& properties) const noexcept { for (Access access : symbol.getAccesses()) { if (access.type == Access::Type::FunctionCall) { // Do NOT look into prepareMaterial call. @@ -279,7 +253,7 @@ void GLSLTools::scanSymbolForProperty(Symbol& symbol, FunctionParameter::INOUT) { MaterialBuilder::Property p = Enums::toEnum(symbol.getDirectIndexStructName()); - properties.insert(p); + properties[size_t(p)] = true; } } else { findPropertyWritesOperations(access.string, access.parameterIdx, rootNode, @@ -292,7 +266,7 @@ void GLSLTools::scanSymbolForProperty(Symbol& symbol, if (access.type == Access::Type::DirectIndexForStruct) { if (Enums::isValid(access.string)) { MaterialBuilder::Property p = Enums::toEnum(access.string); - properties.insert(p); + properties[size_t(p)] = true; } return; } diff --git a/libs/filamat/src/sca/GLSLTools.h b/libs/filamat/src/sca/GLSLTools.h index f60d3a7761..ae08f0270b 100644 --- a/libs/filamat/src/sca/GLSLTools.h +++ b/libs/filamat/src/sca/GLSLTools.h @@ -113,7 +113,6 @@ private: class GLSLTools { public: static void init(); - static void terminate(); // Return true if: // The shader is syntactically and semantically valid AND @@ -124,24 +123,22 @@ public: filament::driver::ShaderModel model, filamat::MaterialBuilder::TargetApi targetApi) const noexcept; - // Return true if: - // The shader is syntactically and semantically valid AND - // The shader features a materialVertex( function bool analyzeVertexShader(const std::string& shaderCode, filament::driver::ShaderModel model, filamat::MaterialBuilder::TargetApi targetApi) const noexcept; // Analyze the first fragment shader the builder will construct and guess properties used (the // builder is modified accordingly). Return true if all operation succeeded. - bool process(filamat::MaterialBuilder& builder) const noexcept; + bool process(filamat::MaterialBuilder& builder, + const MaterialBuilder::PropertyList& properties) const noexcept; // Public for unit tests. using Property = filamat::MaterialBuilder::Property; - using PropertySet = std::set; // Use static code analysis on the fragment shader AST to guess properties used in user provided // glgl code. Populate properties accordingly. bool findProperties(const filamat::MaterialBuilder& builder, - PropertySet& properties) const noexcept; + MaterialBuilder::PropertyList& properties, + MaterialBuilder::TargetApi targetApi = MaterialBuilder::TargetApi::OPENGL) const noexcept; static int glslangVersionFromShaderModel(filament::driver::ShaderModel model); @@ -166,12 +163,12 @@ private: // findPropertyWritesOperations("material", 0, ...); // Does nothing if the parameter is not marked as OUT or INOUT bool findPropertyWritesOperations(const std::string& functionName, size_t parameterIdx, - TIntermNode* rootNode, PropertySet& properties) const noexcept; + TIntermNode* rootNode, MaterialBuilder::PropertyList& properties) const noexcept; // Look at a symbol access and find out if it affects filament MaterialInput fields. Will follow // function calls if necessary. - void scanSymbolForProperty(Symbol& symbol, TIntermNode* rootNode, PropertySet& properties) - const noexcept; + void scanSymbolForProperty(Symbol& symbol, TIntermNode* rootNode, + MaterialBuilder::PropertyList& properties) const noexcept; }; diff --git a/libs/filamat/tests/test_filamat.cpp b/libs/filamat/tests/test_filamat.cpp index 802d5ab872..b80ed187f4 100644 --- a/libs/filamat/tests/test_filamat.cpp +++ b/libs/filamat/tests/test_filamat.cpp @@ -18,8 +18,23 @@ #include "sca/ASTHelpers.h" +#include + using namespace ASTUtils; +static ::testing::AssertionResult PropertyListsMatch(const MaterialBuilder::PropertyList& expected, + const MaterialBuilder::PropertyList& actual) { + for (size_t i = 0; i < filament::MATERIAL_PROPERTIES_COUNT; i++) { + if (expected[i] != actual[i]) { + const auto& propString = Enums::toString(Property(i)); + return ::testing::AssertionFailure() + << "actual[" << propString << "] (" << actual[i] + << ") != expected[" << propString << "] (" << expected[i] << ")"; + } + } + return ::testing::AssertionSuccess(); +} + filamat::MaterialBuilder makeBuilder(const std::string shaderCode) { filamat::MaterialBuilder builder; builder.material(shaderCode.c_str()); @@ -48,10 +63,6 @@ protected: virtual void SetUp() { GLSLTools::init(); } - - virtual void TearDown() { - GLSLTools::terminate(); - } }; TEST_F(MaterialCompiler, StaticCodeAnalyzerNothingDetected) { @@ -63,10 +74,10 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerNothingDetected) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerNotFollowingINParameters) { @@ -82,10 +93,10 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerNotFollowingINParameters) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerDirectAssign) { @@ -98,11 +109,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerDirectAssign) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::BASE_COLOR); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::BASE_COLOR)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } @@ -116,11 +127,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerDirectAssignWithSwizzling) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::BASE_COLOR); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::BASE_COLOR)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerSymbolAsOutParameterWithAliasing) { @@ -138,11 +149,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerSymbolAsOutParameterWithAliasing) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::BASE_COLOR); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::BASE_COLOR)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerSymbolAsOutParameterWithAliasingAndSwizzling) { @@ -160,11 +171,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerSymbolAsOutParameterWithAliasingAndSw filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::BASE_COLOR); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::BASE_COLOR)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerSymbolInOutInChainWithDirectIndexIntoStruct) { @@ -187,11 +198,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerSymbolInOutInChainWithDirectIndexInto filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::BASE_COLOR); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::BASE_COLOR)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerSymbolInOutInChain) { @@ -214,11 +225,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerSymbolInOutInChain) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::BASE_COLOR); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::BASE_COLOR)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } // Tests all attributes in Property type. @@ -232,11 +243,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerBaseColor) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::BASE_COLOR); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::BASE_COLOR)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerRoughness) { std::string shaderCode(R"( @@ -248,11 +259,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerRoughness) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::ROUGHNESS); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::ROUGHNESS)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerMetallic) { std::string shaderCode(R"( @@ -264,11 +275,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerMetallic) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::METALLIC); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::METALLIC)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerReflectance) { std::string shaderCode(R"( @@ -280,11 +291,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerReflectance) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::REFLECTANCE); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::REFLECTANCE)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerAmbientOcclusion) { std::string shaderCode(R"( @@ -296,11 +307,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerAmbientOcclusion) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::AMBIENT_OCCLUSION); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::AMBIENT_OCCLUSION)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerClearCoat) { std::string shaderCode(R"( @@ -313,11 +324,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerClearCoat) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); builder.shading(filamat::MaterialBuilder::Shading::LIT); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::CLEAR_COAT); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::CLEAR_COAT)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerClearCoatRoughness) { std::string shaderCode(R"( @@ -330,11 +341,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerClearCoatRoughness) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); builder.shading(filamat::MaterialBuilder::Shading::LIT); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::CLEAR_COAT_ROUGHNESS); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::CLEAR_COAT_ROUGHNESS)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerClearCoatNormal) { std::string shaderCode(R"( @@ -347,11 +358,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerClearCoatNormal) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); builder.shading(filamat::MaterialBuilder::Shading::LIT); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::CLEAR_COAT_NORMAL); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::CLEAR_COAT_NORMAL)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerThickness) { std::string shaderCode(R"( @@ -364,11 +375,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerThickness) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); builder.shading(filamat::MaterialBuilder::Shading::SUBSURFACE); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::THICKNESS); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::THICKNESS)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerSubsurfacePower) { std::string shaderCode(R"( @@ -381,11 +392,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerSubsurfacePower) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); builder.shading(filamat::MaterialBuilder::Shading::SUBSURFACE); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::SUBSURFACE_POWER); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::SUBSURFACE_POWER)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerSubsurfaceColor) { std::string shaderCode(R"( @@ -398,11 +409,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerSubsurfaceColor) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); builder.shading(filamat::MaterialBuilder::Shading::SUBSURFACE); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::SUBSURFACE_COLOR); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::SUBSURFACE_COLOR)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerAnisotropicDirection) { std::string shaderCode(R"( @@ -415,11 +426,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerAnisotropicDirection) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); builder.shading(filamat::MaterialBuilder::Shading::LIT); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::ANISOTROPY_DIRECTION); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::ANISOTROPY_DIRECTION)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerAnisotropic) { std::string shaderCode(R"( @@ -432,11 +443,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerAnisotropic) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); builder.shading(filamat::MaterialBuilder::Shading::LIT); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::ANISOTROPY); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::ANISOTROPY)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerSheenColor) { std::string shaderCode(R"( @@ -449,11 +460,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerSheenColor) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); builder.shading(filamat::MaterialBuilder::Shading::CLOTH); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::SHEEN_COLOR); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::SHEEN_COLOR)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, StaticCodeAnalyzerNormal) { std::string shaderCode(R"( @@ -465,11 +476,11 @@ TEST_F(MaterialCompiler, StaticCodeAnalyzerNormal) { filamat::MaterialBuilder builder = makeBuilder(shaderCode); GLSLTools glslTools; - GLSLTools::PropertySet properties; + MaterialBuilder::PropertyList properties {false}; glslTools.findProperties(builder, properties); - GLSLTools::PropertySet expected; - expected.insert(filamat::MaterialBuilder::Property::NORMAL); - EXPECT_EQ(expected, properties); + MaterialBuilder::PropertyList expected {false}; + expected[size_t(filamat::MaterialBuilder::Property::NORMAL)] = true; + EXPECT_TRUE(PropertyListsMatch(expected, properties)); } TEST_F(MaterialCompiler, EmptyName) { std::string shaderCode(R"( diff --git a/samples/sample_cloth.cpp b/samples/sample_cloth.cpp index 29f7e9169c..09e2f7afe7 100644 --- a/samples/sample_cloth.cpp +++ b/samples/sample_cloth.cpp @@ -181,10 +181,6 @@ static void setup(Engine* engine, View* view, Scene* scene) { MaterialBuilder builder = MaterialBuilder() .name("DefaultMaterial") - .set(Property::NORMAL) - .set(Property::BASE_COLOR) - .set(Property::SHEEN_COLOR) - .set(Property::ROUGHNESS) .require(VertexAttribute::UV0) .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "normalMap") .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "basecolorMap") diff --git a/samples/sample_full_pbr.cpp b/samples/sample_full_pbr.cpp index 1a16c52c97..9ac2b9507d 100644 --- a/samples/sample_full_pbr.cpp +++ b/samples/sample_full_pbr.cpp @@ -282,19 +282,9 @@ static void setup(Engine* engine, View* view, Scene* scene) { MaterialBuilder builder = MaterialBuilder() .name("DefaultMaterial") - .set(Property::BASE_COLOR) - .set(Property::METALLIC) - .set(Property::ROUGHNESS) - .set(Property::AMBIENT_OCCLUSION) .material(shader.c_str()) .shading(Shading::LIT); - if (g_pbrConfig.clearCoat) { - builder.set(Property::CLEAR_COAT); - } - if (g_pbrConfig.anisotropy) { - builder.set(Property::ANISOTROPY); - } if (hasBaseColorMap) { builder .require(VertexAttribute::UV0) @@ -317,7 +307,6 @@ static void setup(Engine* engine, View* view, Scene* scene) { } if (hasNormalMap) { builder - .set(Property::NORMAL) .require(VertexAttribute::UV0) .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "normalMap"); } diff --git a/samples/sample_normal_map.cpp b/samples/sample_normal_map.cpp index 9d766e1288..ce48ab9b33 100644 --- a/samples/sample_normal_map.cpp +++ b/samples/sample_normal_map.cpp @@ -261,25 +261,19 @@ static void setup(Engine* engine, View*, Scene* scene) { MaterialBuilder builder = MaterialBuilder() .name("DefaultMaterial") - .set(Property::BASE_COLOR) - .set(Property::METALLIC) - .set(Property::ROUGHNESS) .material(shader.c_str()) .shading(Shading::LIT); if (hasNormalMap) { builder .require(VertexAttribute::UV0) - .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "normalMap") - .set(Property::NORMAL); + .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "normalMap"); } if (hasClearCoatNormalMap) { builder .require(VertexAttribute::UV0) - .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "clearCoatNormalMap") - .set(Property::CLEAR_COAT) - .set(Property::CLEAR_COAT_NORMAL); + .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "clearCoatNormalMap"); } if (hasBaseColorMap) { diff --git a/samples/sample_opacity_mask.cpp b/samples/sample_opacity_mask.cpp index 87249b6f85..e98392ea3b 100644 --- a/samples/sample_opacity_mask.cpp +++ b/samples/sample_opacity_mask.cpp @@ -197,9 +197,6 @@ static void setup(Engine* engine, View* view, Scene* scene) { .name("DefaultMaterial") .require(VertexAttribute::UV0) .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "opacityMaskMap") - .set(Property::BASE_COLOR) - .set(Property::METALLIC) - .set(Property::ROUGHNESS) .material(R"SHADER( void material(inout MaterialInputs material) { prepareMaterial(material); diff --git a/samples/sample_pbr.cpp b/samples/sample_pbr.cpp index 6c3db0e559..b4563eb704 100644 --- a/samples/sample_pbr.cpp +++ b/samples/sample_pbr.cpp @@ -219,9 +219,6 @@ static void setup(Engine* engine, View* view, Scene* scene) { MaterialBuilder builder = MaterialBuilder() .name("DefaultMaterial") - .set(Property::BASE_COLOR) - .set(Property::METALLIC) - .set(Property::ROUGHNESS) .material(shader.c_str()) .shading(Shading::LIT); diff --git a/samples/sample_position_offset.cpp b/samples/sample_position_offset.cpp index a158dfd85a..cae7b42bf4 100644 --- a/samples/sample_position_offset.cpp +++ b/samples/sample_position_offset.cpp @@ -129,8 +129,6 @@ static void setup(Engine* engine, View* view, Scene* scene) { Package pkg = MaterialBuilder() .name("PositionOffset") - .set(Property::BASE_COLOR) - .set(Property::ROUGHNESS) .material(R"SHADER( void material(inout MaterialInputs material) { prepareMaterial(material); diff --git a/samples/sample_subsurface.cpp b/samples/sample_subsurface.cpp index 5e913001f3..2c4e5d92e1 100644 --- a/samples/sample_subsurface.cpp +++ b/samples/sample_subsurface.cpp @@ -182,10 +182,6 @@ static void setup(Engine* engine, View* view, Scene* scene) { MaterialBuilder builder = MaterialBuilder() .name("DefaultMaterial") - .set(Property::NORMAL) - .set(Property::BASE_COLOR) - .set(Property::ROUGHNESS) - .set(Property::THICKNESS) .require(VertexAttribute::UV0) .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "normalMap") .parameter(MaterialBuilder::SamplerType::SAMPLER_2D, "basecolorMap") diff --git a/tools/matc/src/matc/ParametersProcessor.cpp b/tools/matc/src/matc/ParametersProcessor.cpp index e7adb45e23..728c729b86 100644 --- a/tools/matc/src/matc/ParametersProcessor.cpp +++ b/tools/matc/src/matc/ParametersProcessor.cpp @@ -31,7 +31,6 @@ namespace matc { static constexpr const char* PARAM_KEY_NAME = "name"; static constexpr const char* PARAM_KEY_INTERPOLATION = "interpolation"; -static constexpr const char* PARAM_KEY_DEFINES = "defines"; static constexpr const char* PARAM_KEY_PARAMETERS = "parameters"; static constexpr const char* PARAM_KEY_VARIABLES = "variables"; static constexpr const char* PARAM_KEY_REQUIRES = "requires"; @@ -51,7 +50,6 @@ static constexpr const char* PARAM_KEY_VARIANT_FILTER = "variantFilter"; ParametersProcessor::ParametersProcessor() { mConfigProcessor[PARAM_KEY_NAME] = &ParametersProcessor::processName; mConfigProcessor[PARAM_KEY_INTERPOLATION] = &ParametersProcessor::processInterpolation; - mConfigProcessor[PARAM_KEY_DEFINES] = &ParametersProcessor::processDefines; mConfigProcessor[PARAM_KEY_PARAMETERS] = &ParametersProcessor::processParameters; mConfigProcessor[PARAM_KEY_VARIABLES] = &ParametersProcessor::processVariables; mConfigProcessor[PARAM_KEY_REQUIRES] = &ParametersProcessor::processRequires; @@ -70,7 +68,6 @@ ParametersProcessor::ParametersProcessor() { mRootAsserts[PARAM_KEY_NAME] = JsonishValue::Type::STRING; mRootAsserts[PARAM_KEY_INTERPOLATION] = JsonishValue::Type::STRING; - mRootAsserts[PARAM_KEY_DEFINES] = JsonishValue::Type::ARRAY; mRootAsserts[PARAM_KEY_PARAMETERS] = JsonishValue::Type::ARRAY; mRootAsserts[PARAM_KEY_VARIABLES] = JsonishValue::Type::ARRAY; mRootAsserts[PARAM_KEY_REQUIRES] = JsonishValue::Type::ARRAY; @@ -170,25 +167,6 @@ bool ParametersProcessor::processInterpolation(filamat::MaterialBuilder& builder return true; } -bool ParametersProcessor::processDefines(filamat::MaterialBuilder& builder, - const JsonishValue& value) { - auto jsonArray = value.toJsonArray(); - for (auto v : jsonArray->getElements()) { - if (v->getType() != JsonishValue::Type::STRING) { - std::cerr << PARAM_KEY_DEFINES << " array values must be STRING." << std::endl; - return false; - } - - auto jsonString = v->toJsonString(); - if (!Enums::isValid(jsonString->getString())) { - return logEnumIssue(PARAM_KEY_DEFINES, *jsonString, Enums::map()); - } - builder.set(Enums::toEnum(jsonString->getString())); - } - - return true; -} - bool ParametersProcessor::processParameters(filamat::MaterialBuilder& builder, const JsonishValue& v) { auto jsonArray = v.toJsonArray();