X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-scene3d%2Fpublic-api%2Floader%2Fshader-definition.cpp;h=8cd505eef64efb7139b0678169543f689d6b942b;hb=HEAD;hp=a753155c8d4cfc034f48af1ca6fddb2f514946c8;hpb=7e4bad8795710fa9b6cf1df5e8f1497b8e460d7c;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-scene3d/public-api/loader/shader-definition.cpp b/dali-scene3d/public-api/loader/shader-definition.cpp index a753155..8cd505e 100644 --- a/dali-scene3d/public-api/loader/shader-definition.cpp +++ b/dali-scene3d/public-api/loader/shader-definition.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 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. @@ -15,16 +15,27 @@ * */ +// CLASS HEADER +#include + +// EXTERNAL INCLUDES +#include +#include + // INTERNAL INCLUDES -#include "dali-scene3d/public-api/loader/shader-definition.h" #include -#include "dali-scene3d/public-api/loader/utils.h" +#include -namespace Dali -{ -namespace Scene3D +#include + +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 { @@ -43,7 +54,7 @@ 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"; @@ -78,6 +89,39 @@ void ShaderDefinition::ApplyDefine(std::string& shaderCode, const std::string& d } } +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 { @@ -102,8 +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.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) @@ -112,6 +158,15 @@ 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); } } @@ -133,7 +188,24 @@ Shader ShaderDefinition::Load(RawData&& raw) const } } - Shader shader = Shader::New(raw.mVertexShaderSource, raw.mFragmentShaderSource, static_cast(hints)); + Property::Map map[2]; + map[0]["vertex"] = raw.mVertexShaderSource; + map[0]["fragment"] = raw.mFragmentShaderSource; + map[0]["renderPassTag"] = 0; + map[0]["hints"] = static_cast(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]); + 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); @@ -144,6 +216,4 @@ Shader ShaderDefinition::Load(RawData&& raw) const return shader; } -} // namespace Loader -} // namespace Scene3D -} // namespace Dali +} // namespace Dali::Scene3D::Loader