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/render/shaders/scene-graph-shader.h>
22 #include <dali/internal/render/queue/render-queue.h>
23 #include <dali/internal/render/common/render-debug.h>
24 #include <dali/internal/render/gl-resources/context.h>
25 #include <dali/internal/render/gl-resources/texture.h>
26 #include <dali/internal/render/gl-resources/texture-cache.h>
27 #include <dali/internal/render/gl-resources/texture-units.h>
28 #include <dali/internal/render/shaders/program.h>
29 #include <dali/internal/render/shaders/uniform-meta.h>
30 #include <dali/internal/common/image-sampler.h>
33 #ifdef DALI_PRINT_RENDER_INFO
36 #define DALI_DEBUG_OSTREAM(streamName) std::stringstream streamName;
38 #define DALI_PRINT_UNIFORM(streamName,bufferIndex,name,value) \
40 streamName << " " << name << ": " << value; \
43 #define DALI_PRINT_CUSTOM_UNIFORM(streamName,bufferIndex,name,property) \
45 streamName << " " << name << ": "; \
46 property.DebugPrint( streamName, bufferIndex ); \
49 #define DALI_PRINT_SHADER_UNIFORMS(streamName) \
51 std::string debugString( streamName.str() ); \
52 DALI_LOG_RENDER_INFO( " %s\n", debugString.c_str() ); \
55 #else // DALI_PRINT_RENDER_INFO
57 #define DALI_DEBUG_OSTREAM(streamName)
58 #define DALI_PRINT_UNIFORM(streamName,bufferIndex,name,value)
59 #define DALI_PRINT_CUSTOM_UNIFORM(streamName,bufferIndex,name,property)
60 #define DALI_PRINT_SHADER_UNIFORMS(streamName)
62 #endif // DALI_PRINT_RENDER_INFO
70 template <> struct ParameterType< Dali::ShaderEffect::GeometryHints> : public BasicType< Dali::ShaderEffect::GeometryHints > {};
71 template <> struct ParameterType< Dali::ShaderEffect::UniformCoordinateType > : public BasicType< Dali::ShaderEffect::UniformCoordinateType > {};
76 namespace // unnamed namespace
79 // Convert Geometry type bitmask to an array index
80 inline unsigned int GetGeometryTypeIndex(GeometryType type)
82 unsigned int index = Log<GEOMETRY_TYPE_IMAGE>::value;
83 if ( type & GEOMETRY_TYPE_IMAGE )
85 index = Log<GEOMETRY_TYPE_IMAGE>::value;
87 else if ( type & GEOMETRY_TYPE_UNTEXTURED_MESH )
89 index = Log<GEOMETRY_TYPE_UNTEXTURED_MESH>::value;
91 else if ( type & GEOMETRY_TYPE_TEXTURED_MESH )
93 index = Log<GEOMETRY_TYPE_TEXTURED_MESH>::value;
98 } // unnamed namespace
101 Shader::Shader( Dali::ShaderEffect::GeometryHints& hints )
102 : mGeometryHints( hints ),
103 mGridDensity( Dali::ShaderEffect::DEFAULT_GRID_DENSITY ),
105 mRenderTextureId( 0 ),
106 mUpdateTextureId( 0 ),
108 mRenderQueue( NULL ),
109 mTextureCache( NULL )
117 void Shader::Initialize( RenderQueue& renderQueue, TextureCache& textureCache )
119 mRenderQueue = &renderQueue;
120 mTextureCache = &textureCache;
123 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
124 // The following methods are called during UpdateManager::Update()
125 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127 void Shader::ForwardTextureId( BufferIndex updateBufferIndex, ResourceId textureId )
129 mUpdateTextureId = textureId;
131 typedef MessageValue1< Shader, Integration::ResourceId > DerivedType;
133 // Reserve some memory inside the render queue
134 unsigned int* slot = mRenderQueue->ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
136 // Construct message in the render queue memory; note that delete should not be called on the return value
137 new (slot) DerivedType( this, &Shader::SetTextureId, textureId );
140 Integration::ResourceId Shader::GetEffectTextureResourceId()
142 return mUpdateTextureId;
145 void Shader::ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta )
147 // Defer setting uniform metadata until the next Render
149 typedef MessageValue1< Shader, UniformMeta* > DerivedType;
151 // Reserve some memory inside the render queue
152 unsigned int* slot = mRenderQueue->ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
154 // Construct message in the render queue memory; note that delete should not be called on the return value
155 new (slot) DerivedType( this, &Shader::InstallUniformMetaInRender, meta );
158 void Shader::ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type )
160 // Defer setting uniform coordinate type until the next Render
161 typedef MessageValue2< Shader, unsigned int, Dali::ShaderEffect::UniformCoordinateType > DerivedType;
163 // Reserve some memory inside the render queue
164 unsigned int* slot = mRenderQueue->ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
166 // Construct message in the render queue memory; note that delete should not be called on the return value
167 new (slot) DerivedType( this, &Shader::SetCoordinateTypeInRender, index, type );
170 void Shader::ForwardGridDensity( BufferIndex updateBufferIndex, float density )
172 typedef MessageValue1< Shader, float > DerivedType;
174 // Reserve some memory inside the render queue
175 unsigned int* slot = mRenderQueue->ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
177 // Construct message in the render queue memory; note that delete should not be called on the return value
178 new (slot) DerivedType( this, &Shader::SetGridDensity, density );
181 void Shader::ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint )
183 typedef MessageValue1< Shader, Dali::ShaderEffect::GeometryHints > DerivedType;
185 // Reserve some memory inside the render queue
186 unsigned int* slot = mRenderQueue->ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
188 // Construct message in the render queue memory; note that delete should not be called on the return value
189 new (slot) DerivedType( this, &Shader::SetGeometryHints, hint );
192 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
193 // The following methods are called during RenderManager::Render()
194 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
196 void Shader::SetTextureId( Integration::ResourceId textureId )
198 if ( mRenderTextureId != textureId )
200 mRenderTextureId = textureId;
205 Integration::ResourceId Shader::GetTextureIdToRender()
207 return mRenderTextureId;
210 void Shader::SetGridDensity( float density )
212 mGridDensity = density;
215 float Shader::GetGridDensity()
220 void Shader::InstallUniformMetaInRender( UniformMeta* meta )
222 mUniformMetadata.PushBack( meta );
225 void Shader::SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type )
227 DALI_ASSERT_DEBUG( index < mUniformMetadata.Count() );
228 mUniformMetadata[ index ]->SetCoordinateType( type );
231 void Shader::SetProgram( GeometryType geometryType,
232 Integration::ResourceId resourceId,
233 Integration::ShaderDataPtr shaderData,
234 ProgramCache* programCache,
235 bool modifiesGeometry )
237 DALI_LOG_TRACE_METHOD_FMT(Debug::Filter::gShader, "%d\n", resourceId);
239 mProgram = Program::New( *programCache, shaderData, modifiesGeometry );
240 // Implement: mProgram = programCache->GetProgram( shaderData, modifiesGeometry );
241 // The program cache owns the Program object so we don't need to worry here.
244 bool Shader::AreSubtypesRequired(GeometryType geometryType)
246 DALI_ASSERT_DEBUG(geometryType < GEOMETRY_TYPE_LAST);
250 Program* Shader::GetProgram( Context& context,
252 ShaderSubTypes subType,
253 unsigned int& programIndex )
255 DALI_ASSERT_DEBUG(type < GEOMETRY_TYPE_LAST);
256 DALI_DEBUG_OSTREAM(debugStream);
261 Program* Shader::GetProgram()
267 void Shader::SetUniforms( Context& context,
269 BufferIndex bufferIndex,
270 unsigned int programIndex,
271 ShaderSubTypes subType )
273 DALI_ASSERT_DEBUG( programIndex < Log<GEOMETRY_TYPE_LAST>::value );
274 DALI_DEBUG_OSTREAM(debugStream);
276 if( mRenderTextureId && ( mTexture == NULL ) )
278 mTexture = mTextureCache->GetTexture( mRenderTextureId );
280 DALI_ASSERT_DEBUG( mTexture != NULL );
283 GLint loc = Program::UNIFORM_UNKNOWN;
287 // if effect sampler uniform used by the program ?
288 const GLint loc = program.GetUniformLocation( Program::UNIFORM_EFFECT_SAMPLER );
289 if( Program::UNIFORM_UNKNOWN != loc )
291 // got effect texture, bind it to texture unit 1
292 mTextureCache->BindTexture( mTexture, mRenderTextureId, GL_TEXTURE_2D, TEXTURE_UNIT_SHADER);
294 // Apply the default sampling options for now
295 mTexture->ApplySampler( TEXTURE_UNIT_SHADER, ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) );
297 DALI_PRINT_UNIFORM( debugStream, bufferIndex, "sEffect", TEXTURE_UNIT_SHADER );
299 program.SetUniform1i( loc, TEXTURE_UNIT_SHADER );
303 // We should have one UniformMeta per uniform property
304 for ( unsigned int i = 0u; i < mUniformMetadata.Count(); ++i )
306 UniformMeta& metadata = *mUniformMetadata[i];
307 const PropertyBase& property = metadata.property;
309 // send the updated uniform to the program
310 if ( metadata.name.length() > 0 )
312 // 0 means program has not got a cache index for this uniform
313 if( 0 == metadata.cacheIndeces[ programIndex ][ subType ] )
315 // register cacheindex for this program
316 metadata.cacheIndeces[ programIndex ][ subType ] = program.RegisterUniform( metadata.name );
318 loc = program.GetUniformLocation( metadata.cacheIndeces[ programIndex ][ subType ] );
320 // if we find uniform with location
321 if ( Program::UNIFORM_UNKNOWN != loc )
323 DALI_PRINT_CUSTOM_UNIFORM( debugStream, bufferIndex, metadata.name, property );
325 // switch based on property type to use correct GL uniform setter
326 switch ( property.GetType() )
328 case Property::BOOLEAN :
330 program.SetUniform1i( loc, property.GetBoolean( bufferIndex ) );
333 case Property::INTEGER :
335 program.SetUniform1i( loc, property.GetInteger( bufferIndex ) );
338 case Property::UNSIGNED_INTEGER :
340 program.SetUniform1i( loc, property.GetUnsignedInteger( bufferIndex ) );
343 case Property::FLOAT :
345 program.SetUniform1f( loc, property.GetFloat( bufferIndex ) );
348 case Property::VECTOR2 :
350 Vector2 value( property.GetVector2( bufferIndex ) );
352 switch ( metadata.coordinateType )
354 case Dali::ShaderEffect::COORDINATE_TYPE_VIEWPORT_POSITION :
357 * Convert coordinates from viewport to GL view space
359 * Viewport coordinate
367 * GL view space coordinates
368 * (width/2,-height/2)
373 * (-width/2,height/2)
375 const Rect< int >& viewport = context.GetViewport();
376 value.x = viewport.width * 0.5f - value.x;
377 value.y = value.y - viewport.height * 0.5f;
381 case Dali::ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION :
383 // Check diagram in COORDINATE_TYPE_VIEWPORT_POSITION
387 case Dali::ShaderEffect::COORDINATE_TYPE_DEFAULT :
389 // nothing to do in this case
392 // no default so compiler will warn if a case is not handled
395 program.SetUniform2f( loc, value.x, value.y );
399 case Property::VECTOR3 :
401 Vector3 value( property.GetVector3( bufferIndex ) );
402 if( Dali::ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION == metadata.coordinateType)
407 program.SetUniform3f( loc, value.x, value.y, value.z );
411 case Property::VECTOR4 :
413 Vector4 value( property.GetVector4( bufferIndex ) );
414 if( Dali::ShaderEffect::COORDINATE_TYPE_VIEWPORT_DIRECTION == metadata.coordinateType)
419 program.SetUniform4f( loc, value.x, value.y, value.z, value.w );
423 case Property::MATRIX:
425 const Matrix& value = property.GetMatrix(bufferIndex);
426 program.SetUniformMatrix4fv(loc, 1, value.AsFloat() );
430 case Property::MATRIX3:
432 const Matrix3& value = property.GetMatrix3(bufferIndex);
433 program.SetUniformMatrix3fv(loc, 1, value.AsFloat() );
438 case Property::ROTATION:
439 case Property::STRING:
440 case Property::RECTANGLE:
442 case Property::ARRAY:
443 case Property::TYPE_COUNT:
445 DALI_LOG_ERROR( "Invalid property type for a uniform" );
453 DALI_PRINT_SHADER_UNIFORMS(debugStream);
459 void SetTextureIdMessage( EventThreadServices& eventThreadServices, const Shader& shader, Integration::ResourceId textureId )
461 typedef MessageDoubleBuffered1< Shader, Integration::ResourceId > LocalType;
463 // Reserve some memory inside the message queue
464 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
466 // Construct message in the message queue memory; note that delete should not be called on the return value
467 new (slot) LocalType( &shader, &Shader::ForwardTextureId, textureId );
470 void SetGridDensityMessage( EventThreadServices& eventThreadServices, const Shader& shader, float density )
472 typedef MessageDoubleBuffered1< Shader, float > LocalType;
474 // Reserve some memory inside the message queue
475 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
477 // Construct message in the message queue memory; note that delete should not be called on the return value
478 new (slot) LocalType( &shader, &Shader::ForwardGridDensity, density );
481 void SetHintsMessage( EventThreadServices& eventThreadServices, const Shader& shader, Dali::ShaderEffect::GeometryHints hint )
483 typedef MessageDoubleBuffered1< Shader, Dali::ShaderEffect::GeometryHints > LocalType;
485 // Reserve some memory inside the message queue
486 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
488 // Construct message in the message queue memory; note that delete should not be called on the return value
489 new (slot) LocalType( &shader, &Shader::ForwardHints, hint );
492 void InstallUniformMetaMessage( EventThreadServices& eventThreadServices, const Shader& shader, UniformMeta& meta )
494 typedef MessageDoubleBuffered1< Shader, UniformMeta* > LocalType;
496 // Reserve some memory inside the message queue
497 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
499 // Construct message in the message queue memory; note that delete should not be called on the return value
500 new (slot) LocalType( &shader, &Shader::ForwardUniformMeta, &meta );
503 void SetCoordinateTypeMessage( EventThreadServices& eventThreadServices, const Shader& shader, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type )
505 typedef MessageDoubleBuffered2< Shader, unsigned int, Dali::ShaderEffect::UniformCoordinateType > LocalType;
507 // Reserve some memory inside the message queue
508 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
510 // Construct message in the message queue memory; note that delete should not be called on the return value
511 new (slot) LocalType( &shader, &Shader::ForwardCoordinateType, index, type );
515 } // namespace SceneGraph
517 } // namespace Internal