/*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
*
*/
+// CLASS HEADER
+#include <dali-scene3d/public-api/loader/shader-definition.h>
+
+// EXTERNAL INCLUDES
+#include <dali/public-api/object/property-array.h>
+#include <regex>
+
// INTERNAL INCLUDES
-#include "dali-scene3d/public-api/loader/shader-definition.h"
#include <dali-scene3d/internal/graphics/builtin-shader-extern-gen.h>
-#include "dali-scene3d/public-api/loader/utils.h"
+#include <dali-scene3d/public-api/loader/utils.h>
-namespace Dali
-{
-namespace Scene3D
+#include <dali/integration-api/debug.h>
+
+namespace
{
-namespace Loader
+#if defined(DEBUG_ENABLED)
+Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_MODEL_SHADER_DEFINITION");
+#endif
+} // namespace
+
+namespace Dali::Scene3D::Loader
{
namespace
{
-const std::string SHADER_HINT_OUTPUT_IS_TRANSPARENT("OUTPUT_IS_TRANSPARENT"); ///< Might generate transparent alpha from opaque inputs.
-const std::string SHADER_HINT_MODIFIES_GEOMETRY("MODIFIES_GEOMETRY"); ///< Might change position of vertices, this option disables any culling optimizations.
-
+const char* SHADER_HINT_OUTPUT_IS_TRANSPARENT("OUTPUT_IS_TRANSPARENT"); ///< Might generate transparent alpha from opaque inputs.
+const char* SHADER_HINT_MODIFIES_GEOMETRY("MODIFIES_GEOMETRY"); ///< Might change position of vertices, this option disables any culling optimizations.
} // namespace
ShaderDefinition::ShaderDefinition(const ShaderDefinition& other)
{
}
-void ShaderDefinition::ApplyDefine(std::string& shaderCode, const std::string& definevar)
+void ApplyDefine(std::string& shaderCode, const std::string& definevar)
{
const std::string IF_1 = "#if 1";
}
}
+void RedefineMacro(std::string& shaderCode, const std::string& macro, const std::string& value)
+{
+ if(!value.empty())
+ {
+ 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)
+ {
+ 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::size_t start = shaderCode.rfind("\n", invocation);
+ std::size_t end = shaderCode.find("\n", invocation);
+ shaderCode.erase(start, end - start);
+ }
+ }
+}
+
ShaderDefinition::RawData
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.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();
}
if(!fail)
{
ApplyDefine(raw.mVertexShaderSource, definevar);
ApplyDefine(raw.mFragmentShaderSource, definevar);
+ ApplyDefine(raw.mShadowVertexShaderSource, 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);
}
}
}
}
- Shader shader = Shader::New(raw.mVertexShaderSource, raw.mFragmentShaderSource, static_cast<Shader::Hint::Value>(hints));
+ Property::Map map[2];
+ map[0]["vertex"] = raw.mVertexShaderSource;
+ map[0]["fragment"] = raw.mFragmentShaderSource;
+ map[0]["renderPassTag"] = 0;
+ map[0]["hints"] = static_cast<Shader::Hint::Value>(hints);
+
+ map[1]["vertex"] = raw.mShadowVertexShaderSource;
+ map[1]["fragment"] = raw.mShadowFragmentShaderSource;
+ map[1]["renderPassTag"] = 10;
+
+ Property::Array array;
+ array.PushBack(map[0]);
+ array.PushBack(map[1]);
+
+ DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Vert Shader src: \n%s\n", raw.mVertexShaderSource.c_str());
+ Shader shader = Shader::New(array);
for(Property::Map::SizeType i0 = 0, i1 = mUniforms.Count(); i0 != i1; ++i0)
{
auto pair = mUniforms.GetKeyValue(i0);
return shader;
}
-} // namespace Loader
-} // namespace Scene3D
-} // namespace Dali
+} // namespace Dali::Scene3D::Loader