2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/effects/shader-factory.h>
25 #include <dali/public-api/dali-core-version.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/devel-api/common/hash.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/integration-api/platform-abstraction.h>
30 #include <dali/internal/event/common/thread-local-storage.h>
34 const char* VERSION_SEPARATOR = "-";
35 const char* SHADER_SUFFIX = ".dali-bin";
48 * @brief Generates a filename for a shader binary based on the hash value passed in.
49 * @param[in] shaderHash A hash over shader sources.
50 * @param[out] filename A string to overwrite with the filename.
52 void shaderBinaryFilename( size_t shaderHash, std::string& filename )
54 std::stringstream binaryShaderFilenameBuilder( std::ios_base::out );
55 binaryShaderFilenameBuilder << CORE_MAJOR_VERSION << VERSION_SEPARATOR << CORE_MINOR_VERSION << VERSION_SEPARATOR << CORE_MICRO_VERSION << VERSION_SEPARATOR
58 filename = binaryShaderFilenameBuilder.str();
63 ShaderFactory::ShaderFactory()
67 ShaderFactory::~ShaderFactory()
69 // Let all the cached objects destroy themselves:
70 for( int i = 0, cacheSize = mShaderBinaryCache.Size(); i < cacheSize; ++i )
72 if( mShaderBinaryCache[i] )
74 mShaderBinaryCache[i]->Unreference();
79 ShaderDataPtr ShaderFactory::Load( const std::string& vertexSource, const std::string& fragmentSource, const Dali::Shader::Hint::Value hints, size_t& shaderHash )
81 // Work out the filename for the binary that the glsl source will be compiled and linked to:
82 shaderHash = CalculateHash( vertexSource.c_str(), fragmentSource.c_str() );
83 std::string binaryShaderFilename;
84 shaderBinaryFilename( shaderHash, binaryShaderFilename );
86 ShaderDataPtr shaderData;
88 /// Check a cache of previously loaded shaders:
89 for( int i = 0, cacheSize = mShaderBinaryCache.Size(); i < cacheSize; ++i )
91 if( mShaderBinaryCache[i]->GetHashValue() == shaderHash )
93 shaderData = mShaderBinaryCache[i];
95 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Mem cache hit on path: \"%s\"\n", binaryShaderFilename.c_str() );
100 // If memory cache failed check the file system for a binary or return a source-only ShaderData:
101 if( shaderData.Get() == NULL )
103 // Allocate the structure that returns the loaded shader:
104 shaderData = new ShaderData( vertexSource, fragmentSource, hints );
105 shaderData->SetHashValue( shaderHash );
106 shaderData->GetBuffer().Clear();
108 // Try to load the binary (this will fail if the shader source has never been compiled before):
109 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
110 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
111 const bool loaded = platformAbstraction.LoadShaderBinaryFile( binaryShaderFilename, shaderData->GetBuffer() );
115 MemoryCacheInsert( *shaderData );
118 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, loaded ?
119 "loaded on path: \"%s\"\n" :
120 "failed to load on path: \"%s\"\n",
121 binaryShaderFilename.c_str());
127 void ShaderFactory::SaveBinary( Internal::ShaderDataPtr shaderData )
129 // Save the binary to the file system:
130 std::string binaryShaderFilename;
131 shaderBinaryFilename( shaderData->GetHashValue(), binaryShaderFilename );
133 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
134 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
135 const bool saved = platformAbstraction.SaveShaderBinaryFile( binaryShaderFilename, &shaderData->GetBuffer()[0], shaderData->GetBufferSize() );
137 // Save the binary into to memory cache:
138 MemoryCacheInsert( *shaderData );
140 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, saved ? "Saved to file: %s\n" : "Save to file failed: %s\n", binaryShaderFilename.c_str() );
141 if( saved ) {} // Avoid unused variable warning in release builds
144 void ShaderFactory::MemoryCacheInsert( ShaderData& shaderData )
146 DALI_ASSERT_DEBUG( shaderData.GetBufferSize() > 0 );
148 // Save the binary into to memory cache:
149 if( shaderData.GetBufferSize() > 0 )
151 mShaderBinaryCache.Reserve( mShaderBinaryCache.Size() + 1 ); // Make sure the push won't throw after we inc the ref count.
152 shaderData.Reference();
153 mShaderBinaryCache.PushBack( &shaderData );
154 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "CACHED BINARY FOR HASH: %u\n", shaderData.GetHashValue() );
158 } // namespace Internal