2 * Copyright (c) 2014 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/public-api/common/hash.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/internal/event/resources/resource-client.h>
30 #include <dali/internal/event/effects/shader-effect-impl.h>
31 #include <dali/internal/event/effects/shader-declarations.h>
33 // compile time generated shader strings
34 #include "dali-shaders.h"
38 const char* VERSION_SEPARATOR = "-";
39 const char* SHADER_SUFFIX = ".dali-bin";
42 // Use pre-compiler constants in order to utilize string concatenation
43 #define SHADER_DEF_USE_BONES "#define USE_BONES\n"
44 #define SHADER_DEF_USE_COLOR "#define USE_COLOR\n"
45 #define SHADER_DEF_USE_GRADIENT "#define USE_GRADIENT\n"
47 using namespace Dali::Integration;
55 ShaderFactory::ShaderFactory(ResourceClient& resourceClient)
56 : mResourceClient(resourceClient)
60 ShaderFactory::~ShaderFactory()
64 ResourceTicketPtr ShaderFactory::Load(const std::string& vertexSource, const std::string& fragmentSource, size_t& shaderHash)
66 ResourceTicketPtr ticket;
68 shaderHash = CalculateHash(vertexSource, fragmentSource);
69 std::stringstream stringHash;
70 stringHash << CORE_MAJOR_VERSION << VERSION_SEPARATOR << CORE_MINOR_VERSION << VERSION_SEPARATOR << CORE_MICRO_VERSION << VERSION_SEPARATOR;
71 stringHash << shaderHash;
73 filename.append( stringHash.str() );
74 filename.append( SHADER_SUFFIX );
76 ShaderResourceType resourceType(shaderHash, vertexSource, fragmentSource);
77 ResourceTypePath typePath(resourceType, filename);
79 // Search for a matching resource
80 ResourceTypePathIdIter iter = mResourceTypePathIdMap.end();
81 if ( !mResourceTypePathIdMap.empty() )
83 iter = mResourceTypePathIdMap.find( typePath );
86 if ( mResourceTypePathIdMap.end() != iter )
88 // The resource was previously requested
89 unsigned int resourceId = iter->second;
91 // The ticket may still be alive, request another copy
92 ticket = mResourceClient.RequestResourceTicket( resourceId );
94 // Clean-up the map of resource IDs, if the ticket has been discarded
97 mResourceTypePathIdMap.erase( iter );
101 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ShaderFactory::Load filename= %s already requested to Load\n", filename.c_str());
107 // Load the shader (loaded synchronously in Update thread so its ready by the time the set shader message is processed)
108 ticket = mResourceClient.LoadShader(resourceType, filename);
109 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ShaderFactory::Load Ticket ID:%d, path: \"%s\"\n", ticket->GetId(), filename.c_str());
111 mResourceTypePathIdMap.insert( ResourceTypePathIdPair( typePath, ticket->GetId() ) );
117 void ShaderFactory::LoadDefaultShaders()
119 mDefaultShader = ShaderEffect::New();
121 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_IMAGE, SHADER_DEFAULT, ImageVertex, ImageFragment, false );
124 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_EVENLY_LIT,
125 UntexturedMeshVertex,
126 UntexturedMeshFragment,
129 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_RIGGED_AND_EVENLY_LIT,
130 std::string( SHADER_DEF_USE_BONES ) + UntexturedMeshVertex,
131 UntexturedMeshFragment,
134 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_RIGGED_AND_VERTEX_COLOR,
135 std::string( SHADER_DEF_USE_BONES SHADER_DEF_USE_COLOR ) + UntexturedMeshVertex,
136 std::string( SHADER_DEF_USE_COLOR ) + UntexturedMeshFragment,
139 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_VERTEX_COLOR,
140 std::string( SHADER_DEF_USE_COLOR ) + UntexturedMeshVertex,
141 std::string( SHADER_DEF_USE_COLOR ) + UntexturedMeshFragment,
145 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXTURED_MESH, SHADER_EVENLY_LIT,
147 TexturedMeshFragment,
150 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXTURED_MESH, SHADER_RIGGED_AND_EVENLY_LIT,
151 std::string( SHADER_DEF_USE_BONES ) + TexturedMeshVertex,
152 TexturedMeshFragment,
156 } // namespace Internal