2 * Copyright (c) 2018 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/rendering/shader-impl.h> // Dali::Internal::Shader
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>
38 * |name |type |writable|animatable|constraint-input|enum for index-checking|
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 )
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}
50 const uint32_t ShaderHintsTableSize = static_cast<uint32_t>( sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] ) );
54 return Dali::BaseHandle();
57 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create, ShaderDefaultProperties );
59 #define TOKEN_STRING(x) (#x)
61 void AppendString(std::string& to, const std::string& append)
70 Property::Value HintString(const Dali::Shader::Hint::Value& hints)
74 if(hints == Dali::Shader::Hint::NONE)
79 if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
81 AppendString(s, "OUTPUT_IS_TRANSPARENT");
84 if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
86 AppendString(s, "MODIFIES_GEOMETRY");
89 return Property::Value(s);
92 } // unnamed namespace
94 ShaderPtr Shader::New( const std::string& vertexShader,
95 const std::string& fragmentShader,
96 Dali::Shader::Hint::Value hints )
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 );
108 services.RegisterObject( shader.Get() );
109 shader->SetShader( vertexShader, fragmentShader, hints );
114 const SceneGraph::Shader& Shader::GetShaderSceneObject() const
116 return static_cast<const SceneGraph::Shader&>( GetSceneObject() );
119 void Shader::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
123 case Dali::Shader::Property::PROGRAM:
125 if( propertyValue.GetType() == Property::MAP )
127 const Dali::Property::Map* map = propertyValue.GetMap();
131 std::string fragment;
132 Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
134 if( Property::Value* value = map->Find("vertex") )
136 vertex = value->Get<std::string>();
139 if( Property::Value* value = map->Find("fragment") )
141 fragment = value->Get<std::string>();
144 if( Property::Value* value = map->Find("hints") )
146 static_cast<void>( // ignore return
147 Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
148 ShaderHintsTable, ShaderHintsTableSize, hints)
152 SetShader( vertex, fragment, hints );
157 DALI_LOG_WARNING( "Shader program property should be a map\n" );
164 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
166 Property::Value value;
170 case Dali::Shader::Property::PROGRAM:
172 Dali::Property::Map map;
175 map["vertex"] = Property::Value(mShaderData->GetVertexShader());
176 map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
177 map["hints"] = HintString(mShaderData->GetHints());
187 Property::Value Shader::GetDefaultPropertyCurrentValue( Property::Index index ) const
189 return GetDefaultProperty( index ); // Event-side only properties
192 Shader::Shader( const SceneGraph::Shader* sceneObject )
193 : Object( sceneObject ),
194 mShaderData( nullptr )
198 void Shader::SetShader( const std::string& vertexSource,
199 const std::string& fragmentSource,
200 Dali::Shader::Hint::Value hints )
202 // Try to load a pre-compiled shader binary for the source pair:
203 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
204 ShaderFactory& shaderFactory = tls.GetShaderFactory();
206 mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
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 );
216 if( EventThreadServices::IsCoreRunning() )
218 EventThreadServices& eventThreadServices = GetEventThreadServices();
219 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
220 RemoveShaderMessage( updateManager, &GetShaderSceneObject() );
222 eventThreadServices.UnregisterObject( this );
226 std::string Shader::GetShaderVersionPrefix()
228 Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
229 return tls.GetShaderVersionPrefix();
232 std::string Shader::GetVertexShaderPrefix()
234 Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
235 return tls.GetVertexShaderPrefix();
238 std::string Shader::GetFragmentShaderPrefix()
240 Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
241 return tls.GetFragmentShaderPrefix();
244 } // namespace Internal