1a7f11fa918f70ebb01b3ca05483362051f2605e
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / shader-impl.cpp
1 /*
2  * Copyright (c) 2018 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/rendering/shader-impl.h> // Dali::Internal::Shader
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/devel-api/scripting/scripting.h>
24 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
25 #include <dali/internal/event/common/thread-local-storage.h>
26 #include <dali/internal/event/effects/shader-factory.h>
27 #include <dali/internal/update/manager/update-manager.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33
34 namespace
35 {
36
37 /**
38  *            |name             |type    |writable|animatable|constraint-input|enum for index-checking|
39  */
40 DALI_PROPERTY_TABLE_BEGIN
41 DALI_PROPERTY( "program",       MAP,     true,     false,     false,  Dali::Shader::Property::PROGRAM )
42 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX, ShaderDefaultProperties )
43
44 Dali::Scripting::StringEnum ShaderHintsTable[] =
45   { { "NONE",                     Dali::Shader::Hint::NONE},
46     { "OUTPUT_IS_TRANSPARENT",    Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT},
47     { "MODIFIES_GEOMETRY",        Dali::Shader::Hint::MODIFIES_GEOMETRY}
48   };
49
50 const uint32_t ShaderHintsTableSize = static_cast<uint32_t>( sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] ) );
51
52 BaseHandle Create()
53 {
54   return Dali::BaseHandle();
55 }
56
57 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create, ShaderDefaultProperties );
58
59 #define TOKEN_STRING(x) (#x)
60
61 void AppendString(std::string& to, const std::string& append)
62 {
63   if(to.size())
64   {
65     to += ",";
66   }
67   to += append;
68 }
69
70 Property::Value HintString(const Dali::Shader::Hint::Value& hints)
71 {
72   std::string s;
73
74   if(hints == Dali::Shader::Hint::NONE)
75   {
76     s = "NONE";
77   }
78
79   if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
80   {
81     AppendString(s, "OUTPUT_IS_TRANSPARENT");
82   }
83
84   if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
85   {
86     AppendString(s, "MODIFIES_GEOMETRY");
87   }
88
89   return Property::Value(s);
90 }
91
92 } // unnamed namespace
93
94 ShaderPtr Shader::New( const std::string& vertexShader,
95                        const std::string& fragmentShader,
96                        Dali::Shader::Hint::Value hints )
97 {
98   // create scene object first so it's guaranteed to exist for the event side
99   auto sceneObject = new SceneGraph::Shader( hints );
100   OwnerPointer< SceneGraph::Shader > transferOwnership( sceneObject );
101   // pass the pointer to base for message passing
102   ShaderPtr shader( new Shader( sceneObject ) );
103   // transfer scene object ownership to update manager
104   auto&& services = shader->GetEventThreadServices();
105   SceneGraph::UpdateManager& updateManager = services.GetUpdateManager();
106   AddShaderMessage( updateManager, transferOwnership );
107
108   services.RegisterObject( shader.Get() );
109   shader->SetShader( vertexShader, fragmentShader, hints );
110
111   return shader;
112 }
113
114 const SceneGraph::Shader& Shader::GetShaderSceneObject() const
115 {
116   return static_cast<const SceneGraph::Shader&>( GetSceneObject() );
117 }
118
119 void Shader::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
120 {
121   switch(index)
122   {
123     case Dali::Shader::Property::PROGRAM:
124     {
125       if( propertyValue.GetType() == Property::MAP )
126       {
127         Dali::Property::Map* map = propertyValue.GetMap();
128         if( map )
129         {
130           std::string vertex;
131           std::string fragment;
132           Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
133
134           if( Property::Value* value = map->Find("vertex") )
135           {
136             vertex = value->Get<std::string>();
137           }
138
139           if( Property::Value* value = map->Find("fragment") )
140           {
141             fragment = value->Get<std::string>();
142           }
143
144           if( Property::Value* value = map->Find("hints") )
145           {
146             static_cast<void>( // ignore return
147               Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
148                                                                      ShaderHintsTable, ShaderHintsTableSize, hints)
149               );
150           }
151
152           SetShader( vertex, fragment, hints );
153         }
154       }
155       else
156       {
157         DALI_LOG_WARNING( "Shader program property should be a map\n" );
158       }
159       break;
160     }
161   }
162 }
163
164 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
165 {
166   Property::Value value;
167
168   switch(index)
169   {
170     case Dali::Shader::Property::PROGRAM:
171     {
172       Dali::Property::Map map;
173       if( mShaderData )
174       {
175         map["vertex"] = Property::Value(mShaderData->GetVertexShader());
176         map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
177         map["hints"] = HintString(mShaderData->GetHints());
178       }
179       value = map;
180       break;
181     }
182   }
183
184   return value;
185 }
186
187 Property::Value Shader::GetDefaultPropertyCurrentValue( Property::Index index ) const
188 {
189   return GetDefaultProperty( index ); // Event-side only properties
190 }
191
192 Shader::Shader( const SceneGraph::Shader* sceneObject )
193 : Object( sceneObject ),
194   mShaderData( nullptr )
195 {
196 }
197
198 void Shader::SetShader( const std::string& vertexSource,
199                         const std::string& fragmentSource,
200                         Dali::Shader::Hint::Value hints )
201 {
202   // Try to load a pre-compiled shader binary for the source pair:
203   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
204   ShaderFactory& shaderFactory = tls.GetShaderFactory();
205   size_t shaderHash;
206   mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
207
208   // Add shader program to scene-object using a message to the UpdateManager
209   EventThreadServices& eventThreadServices = GetEventThreadServices();
210   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
211   SetShaderProgramMessage( updateManager, GetShaderSceneObject(), mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
212 }
213
214 Shader::~Shader()
215 {
216   if( EventThreadServices::IsCoreRunning() )
217   {
218     EventThreadServices& eventThreadServices = GetEventThreadServices();
219     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
220     RemoveShaderMessage( updateManager, &GetShaderSceneObject() );
221
222     eventThreadServices.UnregisterObject( this );
223   }
224 }
225
226
227 } // namespace Internal
228 } // namespace Dali