Merge remote-tracking branch 'origin/tizen' into devel/new_mesh
[platform/core/uifw/dali-core.git] / dali / internal / event / effects / shader-factory.cpp
1 /*
2  * Copyright (c) 2014 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/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>
32
33 // compile time generated shader strings
34 #include "dali-shaders.h"
35
36 namespace
37 {
38 const char* VERSION_SEPARATOR = "-";
39 const char* SHADER_SUFFIX = ".dali-bin";
40 }
41
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"
46
47 using namespace Dali::Integration;
48
49 namespace Dali
50 {
51
52 namespace Internal
53 {
54
55 ShaderFactory::ShaderFactory(ResourceClient& resourceClient)
56 : mResourceClient(resourceClient)
57 {
58 }
59
60 ShaderFactory::~ShaderFactory()
61 {
62 }
63
64 ResourceTicketPtr ShaderFactory::Load(const std::string& vertexSource, const std::string& fragmentSource, size_t& shaderHash)
65 {
66   ResourceTicketPtr ticket;
67
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;
72   std::string filename;
73   filename.append( stringHash.str() );
74   filename.append( SHADER_SUFFIX );
75
76   ShaderResourceType resourceType(shaderHash, vertexSource, fragmentSource);
77   ResourceTypePath typePath(resourceType, filename);
78
79   // Search for a matching resource
80   ResourceTypePathIdIter iter = mResourceTypePathIdMap.end();
81   if ( !mResourceTypePathIdMap.empty() )
82   {
83     iter = mResourceTypePathIdMap.find( typePath );
84   }
85
86   if ( mResourceTypePathIdMap.end() != iter )
87   {
88     // The resource was previously requested
89     unsigned int resourceId = iter->second;
90
91     // The ticket may still be alive, request another copy
92     ticket = mResourceClient.RequestResourceTicket( resourceId );
93
94     // Clean-up the map of resource IDs, if the ticket has been discarded
95     if ( !ticket )
96     {
97       mResourceTypePathIdMap.erase( iter );
98     }
99     else
100     {
101       DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ShaderFactory::Load filename= %s already requested to Load\n", filename.c_str());
102     }
103   }
104
105   if ( !ticket )
106   {
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());
110
111     mResourceTypePathIdMap.insert( ResourceTypePathIdPair( typePath, ticket->GetId() ) );
112   }
113
114   return ticket;
115 }
116
117 void ShaderFactory::LoadDefaultShaders()
118 {
119   mDefaultShader = ShaderEffect::New();
120
121   mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_IMAGE, SHADER_DEFAULT, ImageVertex, ImageFragment, false );
122
123   // Untextured meshes
124   mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_EVENLY_LIT,
125                                       UntexturedMeshVertex,
126                                       UntexturedMeshFragment,
127                                       false );
128
129   mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_UNTEXTURED_MESH, SHADER_RIGGED_AND_EVENLY_LIT,
130                                       std::string( SHADER_DEF_USE_BONES ) + UntexturedMeshVertex,
131                                       UntexturedMeshFragment,
132                                       true );
133
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,
137                                       true );
138
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,
142                                       false );
143
144   // Textured meshes
145   mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXTURED_MESH, SHADER_EVENLY_LIT,
146                                       TexturedMeshVertex,
147                                       TexturedMeshFragment,
148                                       false );
149
150   mDefaultShader->SendProgramMessage( GEOMETRY_TYPE_TEXTURED_MESH, SHADER_RIGGED_AND_EVENLY_LIT,
151                                       std::string( SHADER_DEF_USE_BONES ) + TexturedMeshVertex,
152                                       TexturedMeshFragment,
153                                       true );
154 }
155
156 } // namespace Internal
157
158 } // namespace Dali