2 * Copyright (c) 2015 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>
31 #include <dali/internal/event/effects/shader-effect-impl.h>
32 #include <dali/internal/event/effects/shader-declarations.h>
34 // compile time generated shader strings
35 #include "dali-shaders.h"
39 const char* VERSION_SEPARATOR = "-";
40 const char* SHADER_SUFFIX = ".dali-bin";
53 * @brief Generates a filename for a shader binary based on the hash value passed in.
54 * @param[in] shaderHash A hash over shader sources.
55 * @param[out] filename A string to overwrite with the filename.
57 void shaderBinaryFilename( size_t shaderHash, std::string& filename )
59 std::stringstream binaryShaderFilenameBuilder( std::ios_base::out );
60 binaryShaderFilenameBuilder << CORE_MAJOR_VERSION << VERSION_SEPARATOR << CORE_MINOR_VERSION << VERSION_SEPARATOR << CORE_MICRO_VERSION << VERSION_SEPARATOR
63 filename = binaryShaderFilenameBuilder.str();
68 ShaderFactory::ShaderFactory()
72 ShaderFactory::~ShaderFactory()
74 // Let all the cached objects destroy themselves:
75 for( int i = 0, cacheSize = mShaderBinaryCache.Size(); i < cacheSize; ++i )
77 if( mShaderBinaryCache[i] )
79 mShaderBinaryCache[i]->Unreference();
84 ShaderDataPtr ShaderFactory::Load( const std::string& vertexSource, const std::string& fragmentSource, const Dali::Shader::Hint::Value hints, size_t& shaderHash )
86 // Work out the filename for the binary that the glsl source will be compiled and linked to:
87 shaderHash = CalculateHash( vertexSource.c_str(), fragmentSource.c_str() );
88 std::string binaryShaderFilename;
89 shaderBinaryFilename( shaderHash, binaryShaderFilename );
91 ShaderDataPtr shaderData;
93 /// Check a cache of previously loaded shaders:
94 for( int i = 0, cacheSize = mShaderBinaryCache.Size(); i < cacheSize; ++i )
96 if( mShaderBinaryCache[i]->GetHashValue() == shaderHash )
98 shaderData = mShaderBinaryCache[i];
100 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Mem cache hit on path: \"%s\"\n", binaryShaderFilename.c_str() );
105 // If memory cache failed check the file system for a binary or return a source-only ShaderData:
106 if( shaderData.Get() == NULL )
108 // Allocate the structure that returns the loaded shader:
109 shaderData = new ShaderData( vertexSource, fragmentSource, hints );
110 shaderData->SetHashValue( shaderHash );
111 shaderData->GetBuffer().Clear();
113 // Try to load the binary (this will fail if the shader source has never been compiled before):
114 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
115 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
116 const bool loaded = platformAbstraction.LoadShaderBinaryFile( binaryShaderFilename, shaderData->GetBuffer() );
120 MemoryCacheInsert( *shaderData );
123 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, loaded ?
124 "loaded on path: \"%s\"\n" :
125 "failed to load on path: \"%s\"\n",
126 binaryShaderFilename.c_str());
132 void ShaderFactory::SaveBinary( Internal::ShaderDataPtr shaderData )
134 // Save the binary to the file system:
135 std::string binaryShaderFilename;
136 shaderBinaryFilename( shaderData->GetHashValue(), binaryShaderFilename );
138 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
139 Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
140 const bool saved = platformAbstraction.SaveShaderBinaryFile( binaryShaderFilename, &shaderData->GetBuffer()[0], shaderData->GetBufferSize() );
142 // Save the binary into to memory cache:
143 MemoryCacheInsert( *shaderData );
145 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, saved ? "Saved to file: %s\n" : "Save to file failed: %s\n", binaryShaderFilename.c_str() );
146 if( saved ) {} // Avoid unused variable warning in release builds
149 void ShaderFactory::LoadDefaultShaders()
151 mDefaultShader = ShaderEffect::New();
154 void ShaderFactory::MemoryCacheInsert( ShaderData& shaderData )
156 DALI_ASSERT_DEBUG( shaderData.GetBufferSize() > 0 );
158 // Save the binary into to memory cache:
159 if( shaderData.GetBufferSize() > 0 )
161 mShaderBinaryCache.Reserve( mShaderBinaryCache.Size() + 1 ); // Make sure the push won't throw after we inc the ref count.
162 shaderData.Reference();
163 mShaderBinaryCache.PushBack( &shaderData );
164 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "CACHED BINARY FOR HASH: %u\n", shaderData.GetHashValue() );
168 } // namespace Internal