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/internal/common/dali-hash.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/internal/event/resources/resource-client.h>
29 #include <dali/internal/event/effects/shader-effect-impl.h>
30 #include <dali/internal/event/effects/shader-declarations.h>
32 // the generated shader strings
33 #include "dali-shaders.h"
36 // Use pre-compiler constants in order to utilize string concatenation
37 #define SHADER_DEF_USE_BONES "#define USE_BONES\n"
38 #define SHADER_DEF_USE_LIGHTING "#define USE_LIGHTING\n"
39 #define SHADER_DEF_USE_COLOR "#define USE_COLOR\n"
40 #define SHADER_DEF_USE_GRADIENT "#define USE_GRADIENT\n"
42 using namespace Dali::Integration;
50 ShaderFactory::ShaderFactory(ResourceClient& resourceClient)
51 : mResourceClient(resourceClient)
55 ShaderFactory::~ShaderFactory()
59 ResourceTicketPtr ShaderFactory::Load(const std::string& vertexSource, const std::string& fragmentSource, size_t& shaderHash)
61 ResourceTicketPtr ticket;
63 shaderHash = CalculateHash(vertexSource, fragmentSource);
64 std::stringstream stringHash;
65 stringHash << shaderHash;
66 std::string filename = DALI_SHADERBIN_DIR;
67 filename += stringHash.str();
68 filename += ".dali-bin";
70 ShaderResourceType resourceType(shaderHash, vertexSource, fragmentSource);
71 ResourceTypePath typePath(resourceType, filename);
73 // Search for a matching resource
74 ResourceTypePathIdIter iter = mResourceTypePathIdMap.end();
75 if ( !mResourceTypePathIdMap.empty() )
77 iter = mResourceTypePathIdMap.find( typePath );
80 if ( mResourceTypePathIdMap.end() != iter )
82 // The resource was previously requested
83 unsigned int resourceId = iter->second;
85 // The ticket may still be alive, request another copy
86 ticket = mResourceClient.RequestResourceTicket( resourceId );
88 // Clean-up the map of resource IDs, if the ticket has been discarded
91 mResourceTypePathIdMap.erase( iter );
95 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ShaderFactory::Load filename= %s already requested to Load\n", filename.c_str());
101 // Load the shader (loaded synchronously in Update thread so its ready by the time the set shader message is processed)
102 ticket = mResourceClient.LoadShader(resourceType, filename);
103 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ShaderFactory::Load Ticket ID:%d, path: \"%s\"\n", ticket->GetId(), filename.c_str());
105 mResourceTypePathIdMap.insert( ResourceTypePathIdPair( typePath, ticket->GetId() ) );
111 void ShaderFactory::LoadDefaultShaders()
113 mDefaultShader = ShaderEffect::New();
115 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_IMAGE, SHADER_DEFAULT, ImageVertex, ImageFragment, false );
117 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXT, SHADER_DEFAULT, TextDistanceFieldVertex, TextDistanceFieldFragment, false );
119 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXT, SHADER_GRADIENT,
120 std::string( SHADER_DEF_USE_GRADIENT ) + TextDistanceFieldVertex,
121 std::string( SHADER_DEF_USE_GRADIENT ) + TextDistanceFieldFragment,
124 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXT, SHADER_GRADIENT_GLOW, TextDistanceFieldGlowVertex, TextDistanceFieldGlowFragment, false );
126 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXT, SHADER_GRADIENT_SHADOW, TextDistanceFieldShadowVertex, TextDistanceFieldShadowFragment, false );
128 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXT, SHADER_GRADIENT_OUTLINE, TextDistanceFieldOutlineVertex, TextDistanceFieldOutlineFragment, false );
130 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXT, SHADER_GRADIENT_OUTLINE_GLOW, TextDistanceFieldOutlineGlowVertex, TextDistanceFieldOutlineGlowFragment, false );
133 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_DEFAULT,
134 UntexturedMeshVertex,
135 std::string( SHADER_DEF_USE_LIGHTING ) + UntexturedMeshFragment,
138 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_EVENLY_LIT,
139 UntexturedMeshVertex,
140 UntexturedMeshFragment,
143 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_RIGGED_AND_LIT,
144 std::string( SHADER_DEF_USE_BONES ) + UntexturedMeshVertex,
145 std::string( SHADER_DEF_USE_LIGHTING ) + UntexturedMeshFragment,
148 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_RIGGED_AND_EVENLY_LIT,
149 std::string( SHADER_DEF_USE_BONES ) + UntexturedMeshVertex,
150 UntexturedMeshFragment,
153 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_RIGGED_AND_VERTEX_COLOR,
154 std::string( SHADER_DEF_USE_BONES SHADER_DEF_USE_COLOR ) + UntexturedMeshVertex,
155 std::string( SHADER_DEF_USE_COLOR ) + UntexturedMeshFragment,
158 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_VERTEX_COLOR,
159 std::string( SHADER_DEF_USE_COLOR ) + UntexturedMeshVertex,
160 std::string( SHADER_DEF_USE_COLOR ) + UntexturedMeshFragment,
164 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXTURED_MESH, SHADER_DEFAULT,
166 std::string( SHADER_DEF_USE_LIGHTING ) + TexturedMeshFragment,
169 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXTURED_MESH, SHADER_EVENLY_LIT,
171 TexturedMeshFragment,
174 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXTURED_MESH, SHADER_RIGGED_AND_LIT,
175 std::string( SHADER_DEF_USE_BONES ) + TexturedMeshVertex,
176 std::string( SHADER_DEF_USE_LIGHTING ) + TexturedMeshFragment,
179 mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXTURED_MESH, SHADER_RIGGED_AND_EVENLY_LIT,
180 std::string( SHADER_DEF_USE_BONES ) + TexturedMeshVertex,
181 TexturedMeshFragment,
185 } // namespace Internal