Removed unused macro invocation from shader 14/301614/2
authorDavid Steele <david.steele@samsung.com>
Mon, 20 Nov 2023 17:25:56 +0000 (17:25 +0000)
committerDavid Steele <david.steele@samsung.com>
Mon, 20 Nov 2023 18:14:29 +0000 (18:14 +0000)
Change-Id: I38953dd21347753de90b567f427b60dde0489637

automated-tests/src/dali-scene3d/utc-Dali-ShaderManager.cpp
dali-scene3d/internal/model-components/model-primitive-impl.cpp
dali-scene3d/public-api/loader/shader-definition.cpp
dali-scene3d/public-api/loader/shader-manager.cpp
dali-scene3d/public-api/loader/shader-option.cpp

index 191a631..f17dcb3 100644 (file)
@@ -255,6 +255,7 @@ int UtcDaliShaderManagerProduceShader(void)
     MeshDefinition     meshDefinition;
     MaterialDefinition materialDefinition;
     ShaderParameters   shaderParameter{meshDefinition, materialDefinition, nodeDefinition};
+
     // Only define skinning accessors for skinning test...
     if(permutationSet.permutations.size() > 1)
     {
@@ -280,6 +281,8 @@ int UtcDaliShaderManagerProduceShader(void)
       {
         option1.AddOption(optionType);
       }
+      // No permutation uses extra joint weights.
+      option1.AddJointMacros(0);
       rendererState = (rendererState | permutation->rendererStateSet) & ~permutation->rendererStateClear;
     }
     option1.AddOption(ShaderOption::Type::THREE_TEXTURE);
index e58327c..48519f7 100644 (file)
@@ -310,6 +310,10 @@ void ModelPrimitive::ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag
       shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::SKINNING);
       shaderOption.AddJointMacros(mNumberOfJointSets);
     }
+    else
+    {
+      shaderOption.AddJointMacros(0);
+    }
     if(mHasVertexColor)
     {
       shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::COLOR_ATTRIBUTE);
@@ -344,13 +348,13 @@ void ModelPrimitive::ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag
       {
         Property::Map oldMap = GetMap(mShader);
         DALI_LOG_WRITE_FILE(tmpFilename("oldShader", ".txt"), "Vertex Shader:\n"
-                            << oldMap["vertex"] << "\n\nFragmentShader: " << oldMap["fragment"] << "\n");
+                                                                << oldMap["vertex"] << "\n\nFragmentShader: " << oldMap["fragment"] << "\n");
       }
       if(newShader)
       {
         Property::Map newMap = GetMap(newShader);
         DALI_LOG_WRITE_FILE(tmpFilename("newShader", ".txt"), "Vertex Shader:\n"
-                            << newMap["vertex"] << "\n\nFragmentShader: " << newMap["fragment"] << "\n");
+                                                                << newMap["vertex"] << "\n\nFragmentShader: " << newMap["fragment"] << "\n");
       }
 #endif
     }
index a7727d6..5aba19e 100644 (file)
@@ -91,20 +91,33 @@ void ApplyDefine(std::string& shaderCode, const std::string& definevar)
 
 void RedefineMacro(std::string& shaderCode, const std::string& macro, const std::string& value)
 {
-  std::string definition = "#define " + macro;
-  std::size_t found      = shaderCode.find(definition);
-  if(found != std::string::npos)
+  if(!value.empty())
   {
-    std::size_t insertionPoint = found + definition.length();
+    std::string definition = "#define " + macro;
+    std::size_t found      = shaderCode.find(definition);
+    if(found != std::string::npos)
+    {
+      std::size_t insertionPoint = found + definition.length();
 
-    // Automatically insert line-continuation character into value
-    std::regex                 re("\n");
-    std::sregex_token_iterator first{value.begin(), value.end(), re, -1}, last;
-    for(auto i = first; i != last; ++i)
+      // Automatically insert line-continuation character into value
+      std::regex                 re("\n");
+      std::sregex_token_iterator first{value.begin(), value.end(), re, -1}, last;
+      for(auto i = first; i != last; ++i)
+      {
+        std::string line = std::string(" \\\n") + (*i).str();
+        shaderCode.insert(insertionPoint, line);
+        insertionPoint += line.length();
+      }
+    }
+  }
+  else
+  {
+    std::size_t invocation = shaderCode.rfind(macro);
+    if(invocation != std::string::npos)
     {
-      std::string line = std::string(" \\\n") + (*i).str();
-      shaderCode.insert(insertionPoint, line);
-      insertionPoint += line.length();
+      std::size_t start = shaderCode.rfind("\n", invocation);
+      std::size_t end   = shaderCode.find("\n", invocation);
+      shaderCode.erase(start, end - start);
     }
   }
 }
index 6adb452..35ebd63 100644 (file)
@@ -118,6 +118,10 @@ ShaderOption MakeOption(const MaterialDefinition& materialDef, const MeshDefinit
     option.AddOption(ShaderOption::Type::SKINNING);
     option.AddJointMacros(meshDef.mJoints.size());
   }
+  else
+  {
+    option.AddJointMacros(0);
+  }
 
   if(MaskMatch(meshDef.mFlags, MeshDefinition::FLIP_UVS_VERTICAL))
   {
index 4654348..f504a99 100644 (file)
@@ -117,6 +117,11 @@ void ShaderOption::AddJointMacros(size_t numberOfJointSets)
     AddMacroDefinition(ADD_EXTRA_SKINNING_ATTRIBUTES, attributes.str());
     AddMacroDefinition(ADD_EXTRA_WEIGHTS, weights.str());
   }
+  else
+  {
+    AddMacroDefinition(ADD_EXTRA_SKINNING_ATTRIBUTES, std::string{});
+    AddMacroDefinition(ADD_EXTRA_WEIGHTS, std::string{});
+  }
 }
 
 void ShaderOption::AddMacroDefinition(std::string macro, std::string definition)