[dali_2.3.33] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / effects / shader-factory.cpp
1 /*
2  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/effects/shader-factory.h>
20
21 // EXTERNAL INCLUDES
22 #include <sstream>
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/common/hash.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/integration-api/platform-abstraction.h>
28 #include <dali/internal/event/common/thread-local-storage.h>
29 #include <dali/public-api/common/dali-common.h>
30 #include <dali/public-api/dali-core-version.h>
31
32 namespace
33 {
34 const char* VERSION_SEPARATOR = "-";
35 const char* SHADER_SUFFIX     = ".dali-bin";
36 } // namespace
37
38 namespace Dali
39 {
40 namespace Internal
41 {
42 namespace
43 {
44 /**
45  * @brief Generates a filename for a shader binary based on the hash value passed in.
46  * @param[in] shaderHash A hash over shader sources.
47  * @param[out] filename A string to overwrite with the filename.
48  */
49 void shaderBinaryFilename(size_t shaderHash, std::string& filename)
50 {
51   std::stringstream binaryShaderFilenameBuilder(std::ios_base::out);
52   binaryShaderFilenameBuilder << CORE_MAJOR_VERSION << VERSION_SEPARATOR << CORE_MINOR_VERSION << VERSION_SEPARATOR << CORE_MICRO_VERSION << VERSION_SEPARATOR
53                               << shaderHash
54                               << SHADER_SUFFIX;
55   filename = binaryShaderFilenameBuilder.str();
56 }
57
58 } // namespace
59
60 ShaderFactory::ShaderFactory() = default;
61
62 ShaderFactory::~ShaderFactory()
63 {
64   // Let all the cached objects destroy themselves:
65   for(std::size_t i = 0, cacheSize = mShaderBinaryCache.Size(); i < cacheSize; ++i)
66   {
67     if(mShaderBinaryCache[i])
68     {
69       mShaderBinaryCache[i]->Unreference();
70     }
71   }
72 }
73
74 ShaderDataPtr ShaderFactory::Load(std::string_view vertexSource, std::string_view fragmentSource, const Dali::Shader::Hint::Value hints, uint32_t renderPassTag, std::string_view name, size_t& shaderHash)
75 {
76   // Work out the filename for the binary that the glsl source will be compiled and linked to:
77   shaderHash = CalculateHash(vertexSource, fragmentSource);
78   std::string binaryShaderFilename;
79   shaderBinaryFilename(shaderHash, binaryShaderFilename);
80
81   ShaderDataPtr shaderData;
82
83   /// Check a cache of previously loaded shaders:
84   for(std::size_t i = 0, cacheSize = mShaderBinaryCache.Size(); i < cacheSize; ++i)
85   {
86     if(mShaderBinaryCache[i]->GetHashValue() == shaderHash)
87     {
88       shaderData = mShaderBinaryCache[i];
89
90       DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Mem cache hit on path: \"%s\"\n", binaryShaderFilename.c_str());
91       break;
92     }
93   }
94
95   // If memory cache failed check the file system for a binary or return a source-only ShaderData:
96   if(shaderData.Get() == nullptr)
97   {
98     // Allocate the structure that returns the loaded shader:
99     shaderData = new ShaderData(vertexSource, fragmentSource, hints, renderPassTag, name);
100     shaderData->SetHashValue(shaderHash);
101     shaderData->GetBuffer().Clear();
102
103     // Try to load the binary (this will fail if the shader source has never been compiled before):
104     ThreadLocalStorage&               tls                 = ThreadLocalStorage::Get();
105     Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
106     const bool                        loaded              = platformAbstraction.LoadShaderBinaryFile(binaryShaderFilename, shaderData->GetBuffer());
107
108     if(loaded)
109     {
110       MemoryCacheInsert(*shaderData);
111     }
112
113     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, loaded ? "loaded on path: \"%s\"\n" : "failed to load on path: \"%s\"\n", binaryShaderFilename.c_str());
114   }
115
116   return shaderData;
117 }
118
119 void ShaderFactory::SaveBinary(Internal::ShaderDataPtr shaderData)
120 {
121   // Save the binary to the file system:
122   std::string binaryShaderFilename;
123   shaderBinaryFilename(shaderData->GetHashValue(), binaryShaderFilename);
124
125   ThreadLocalStorage&               tls                 = ThreadLocalStorage::Get();
126   Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
127   const bool                        saved               = platformAbstraction.SaveShaderBinaryFile(binaryShaderFilename, &shaderData->GetBuffer()[0], static_cast<unsigned int>(shaderData->GetBufferSize())); // don't expect buffer larger than unsigned int
128
129   // Save the binary into to memory cache:
130   MemoryCacheInsert(*shaderData);
131
132   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, saved ? "Saved to file: %s\n" : "Save to file failed: %s\n", binaryShaderFilename.c_str());
133   if(saved)
134   {
135   } // Avoid unused variable warning in release builds
136 }
137
138 void ShaderFactory::MemoryCacheInsert(ShaderData& shaderData)
139 {
140   DALI_ASSERT_DEBUG(shaderData.GetBufferSize() > 0);
141
142   // Save the binary into to memory cache:
143   if(shaderData.GetBufferSize() > 0)
144   {
145     mShaderBinaryCache.Reserve(mShaderBinaryCache.Size() + 1); // Make sure the push won't throw after we inc the ref count.
146     shaderData.Reference();
147     mShaderBinaryCache.PushBack(&shaderData);
148     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "CACHED BINARY FOR HASH: %u\n", shaderData.GetHashValue());
149   }
150 }
151
152 } // namespace Internal
153
154 } // namespace Dali