[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / shader-definition.cpp
index a7727d6..8cd505e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -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);
     }
   }
 }
@@ -133,10 +146,10 @@ ShaderDefinition::LoadRaw(const std::string& shadersPath) const
   }
   else
   {
-    raw.mVertexShaderSource         = SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_VERT.data();
-    raw.mFragmentShaderSource       = SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_FRAG.data();
-    raw.mShadowVertexShaderSource   = SHADER_SHADOW_MAP_SHADER_VERT.data();
-    raw.mShadowFragmentShaderSource = SHADER_SHADOW_MAP_SHADER_FRAG.data();
+    raw.mVertexShaderSource         = Dali::Shader::GetVertexShaderPrefix() + SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_VERT.data();
+    raw.mFragmentShaderSource       = Dali::Shader::GetFragmentShaderPrefix() + SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_FRAG.data();
+    raw.mShadowVertexShaderSource   = Dali::Shader::GetVertexShaderPrefix() + SHADER_SHADOW_MAP_SHADER_VERT.data();
+    raw.mShadowFragmentShaderSource = Dali::Shader::GetFragmentShaderPrefix() + SHADER_SHADOW_MAP_SHADER_FRAG.data();
   }
 
   if(!fail)
@@ -146,12 +159,14 @@ ShaderDefinition::LoadRaw(const std::string& shadersPath) const
       ApplyDefine(raw.mVertexShaderSource, definevar);
       ApplyDefine(raw.mFragmentShaderSource, definevar);
       ApplyDefine(raw.mShadowVertexShaderSource, definevar);
+      ApplyDefine(raw.mShadowFragmentShaderSource, definevar);
     }
     for(const auto& macroDef : mMacros)
     {
       RedefineMacro(raw.mVertexShaderSource, macroDef.macro, macroDef.definition);
       RedefineMacro(raw.mFragmentShaderSource, macroDef.macro, macroDef.definition);
       RedefineMacro(raw.mShadowVertexShaderSource, macroDef.macro, macroDef.definition);
+      RedefineMacro(raw.mShadowFragmentShaderSource, macroDef.macro, macroDef.definition);
     }
   }
 
@@ -178,10 +193,12 @@ Shader ShaderDefinition::Load(RawData&& raw) const
   map[0]["fragment"]      = raw.mFragmentShaderSource;
   map[0]["renderPassTag"] = 0;
   map[0]["hints"]         = static_cast<Shader::Hint::Value>(hints);
+  map[0]["name"]          = "SCENE3D_PBR";
 
   map[1]["vertex"]        = raw.mShadowVertexShaderSource;
   map[1]["fragment"]      = raw.mShadowFragmentShaderSource;
   map[1]["renderPassTag"] = 10;
+  map[1]["name"]          = "SCENE3D_SHADOW_MAP";
 
   Property::Array array;
   array.PushBack(map[0]);