2 * Copyright (c) 2015 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/public-api/shader-effects/shader-effect.h> // Dali::ShaderEffect::GeometryHints // TODO: MESH_REWORK REMOVE
24 #include <dali/devel-api/rendering/shader.h> // Dali::Shader
26 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
27 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
28 #include <dali/internal/event/common/thread-local-storage.h>
29 #include <dali/internal/event/effects/shader-factory.h>
30 #include <dali/internal/event/resources/resource-ticket.h>
31 #include <dali/internal/update/manager/update-manager.h>
42 * |name |type |writable|animatable|constraint-input|enum for index-checking|
44 DALI_PROPERTY_TABLE_BEGIN
45 DALI_PROPERTY( "program", MAP, true, false, false, Dali::Shader::Property::PROGRAM )
46 DALI_PROPERTY( "shader-hints", INTEGER, true, false, true, Dali::Shader::Property::SHADER_HINTS )
47 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
49 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS };
53 return Dali::BaseHandle();
56 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
58 } // unnamed namespace
60 ShaderPtr Shader::New( const std::string& vertexShader,
61 const std::string& fragmentShader,
62 Dali::Shader::ShaderHints hints )
65 ShaderPtr shader( new Shader() );
66 shader->Initialize( vertexShader, fragmentShader, hints );
70 const SceneGraph::Shader* Shader::GetShaderSceneObject() const
75 unsigned int Shader::GetDefaultPropertyCount() const
77 return SHADER_IMPL.GetDefaultPropertyCount();
80 void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
82 SHADER_IMPL.GetDefaultPropertyIndices( indices );
85 const char* Shader::GetDefaultPropertyName(Property::Index index) const
87 return SHADER_IMPL.GetDefaultPropertyName( index );
90 Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
92 return SHADER_IMPL.GetDefaultPropertyIndex( name );
95 bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
97 return SHADER_IMPL.IsDefaultPropertyWritable( index );
100 bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
102 return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
105 bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
107 return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
110 Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
112 return SHADER_IMPL.GetDefaultPropertyType( index );
115 void Shader::SetDefaultProperty( Property::Index index,
116 const Property::Value& propertyValue )
120 case Dali::Shader::Property::PROGRAM:
122 // @todo MESH_REWORK Set program again?
123 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
126 case Dali::Shader::Property::SHADER_HINTS:
128 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
134 void Shader::SetSceneGraphProperty( Property::Index index,
135 const PropertyMetadata& entry,
136 const Property::Value& value )
138 SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
139 OnPropertySet(index, value);
142 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
144 Property::Value value;
148 case Dali::Shader::Property::PROGRAM:
150 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
153 case Dali::Shader::Property::SHADER_HINTS:
155 DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
163 const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
168 const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
173 const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
175 DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
176 const SceneGraph::PropertyBase* property = NULL;
180 property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
181 &Shader::FindAnimatableProperty,
182 &Shader::FindCustomProperty,
185 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
187 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
194 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
196 const PropertyInputImpl* property = NULL;
200 const SceneGraph::PropertyBase* baseProperty =
201 SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
202 &Shader::FindAnimatableProperty,
203 &Shader::FindCustomProperty,
205 property = static_cast<const PropertyInputImpl*>( baseProperty );
207 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
209 if( index == Dali::Shader::Property::SHADER_HINTS )
211 // @todo MESH_REWORK - return the property
215 DALI_ASSERT_ALWAYS( 0 && "Property is not a valid constraint input" );
223 int Shader::GetPropertyComponentIndex( Property::Index index ) const
225 return Property::INVALID_COMPONENT_INDEX;
228 bool Shader::OnStage() const
233 void Shader::Connect()
238 void Shader::Disconnect()
244 : mSceneObject( NULL ),
249 void Shader::Initialize(
250 const std::string& vertexSource,
251 const std::string& fragmentSource,
252 Dali::Shader::ShaderHints hints )
254 EventThreadServices& eventThreadServices = GetEventThreadServices();
255 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
257 // @todo MESH_REWORK - Pass hints directly to a new scene graph shader
258 int effectHint = Dali::ShaderEffect::HINT_NONE;
259 if( hints & Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT )
261 effectHint |= Dali::ShaderEffect::HINT_BLENDING;
264 if( hints & Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST )
266 effectHint |= Dali::ShaderEffect::HINT_DEPTH_BUFFER;
269 if( (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) == 0x0 )
271 effectHint |= Dali::ShaderEffect::HINT_DOESNT_MODIFY_GEOMETRY;
273 Dali::ShaderEffect::GeometryHints shaderEffectHint = static_cast<Dali::ShaderEffect::GeometryHints>( effectHint );
275 mSceneObject = new SceneGraph::Shader( shaderEffectHint );
277 // Add to update manager
278 AddShaderMessage( updateManager, *mSceneObject );
280 // Try to load a precompiled shader binary for the source pair:
281 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
282 ShaderFactory& shaderFactory = tls.GetShaderFactory();
284 Internal::ShaderDataPtr shaderData = shaderFactory.Load( vertexSource, fragmentSource, shaderHash );
286 // Add shader program to scene-object using a message to the UpdateManager
287 SetShaderProgramMessage( updateManager, *mSceneObject, shaderData, (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) != 0x0 );
288 eventThreadServices.RegisterObject( this );
293 if( EventThreadServices::IsCoreRunning() )
295 EventThreadServices& eventThreadServices = GetEventThreadServices();
296 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
297 RemoveShaderMessage( updateManager, *mSceneObject);
299 eventThreadServices.UnregisterObject( this );
304 } // namespace Internal