1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/internal/common/buffer-index.h>
24 #include <dali/internal/common/event-to-update.h>
25 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
26 #include <dali/internal/update/resources/resource-manager-declarations.h>
27 #include <dali/internal/render/gl-resources/texture-declarations.h>
28 #include <dali/internal/render/common/render-manager.h>
29 #include <dali/internal/update/common/property-owner.h>
30 #include <dali/internal/event/effects/shader-declarations.h>
31 #include <dali/integration-api/shader-data.h>
32 #include <dali/public-api/math/compile-time-math.h>
33 #include <dali/public-api/math/matrix.h>
34 #include <dali/public-api/shader-effects/shader-effect.h>
35 #include <dali/internal/common/type-abstraction-enums.h>
44 typedef unsigned int ResourceId;
45 } // namespace Integration
52 class ResourceManager;
59 class PostProcessResourceDispatcher;
63 * A base class for a collection of shader programs, to apply an effect to different geometry types.
64 * This class is also the default shader so its easier to override default behaviour
66 class Shader : public PropertyOwner
71 * This container contains pointers to the programs for each sub-type of a given geometry type.
72 * If a custom shader has overridden the subtypes (e.g. mesh custom shader),
73 * then the flag is used to indicate that there is only one shader in the
74 * vector that should be used.
75 * Note, it does not own the Programs it contains (they are owned by the Context).
77 struct ProgramContainer
84 : mUseDefaultForAllSubtypes(false)
90 * @param[in] position The array index
92 Program*& operator[]( size_t position )
94 return mSubPrograms[position];
98 * Resize the container
99 * @param[in] length The new size of the container
101 void Resize(size_t length)
103 mSubPrograms.Resize(length);
107 * Get the number of elements in the container
108 * @return count of the number of elements in the container
112 return mSubPrograms.Count();
115 Dali::Vector<Program*> mSubPrograms; ///< The programs for each subtype
116 bool mUseDefaultForAllSubtypes; ///< TRUE if the first program should be used for all subtypes
121 * @param hints Geometry hints
123 Shader( Dali::ShaderEffect::GeometryHints& hints );
131 * Second stage initialization, called when added to the UpdateManager
132 * @param postProcessResourceDispatcher Used to save the compiled GL shader in the next update.
133 * @param renderQueue Used to queue messages from update to render thread.
134 * @param textureCache Used to retrieve effect textures when rendering.
136 void Initialize( PostProcessResourceDispatcher& postProcessResourceDispatcher, RenderQueue& renderQueue, TextureCache& textureCache );
138 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139 // The following methods are called during UpdateManager::Update()
140 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
143 * Query whether a shader geometry hint is set.
144 * @pre The shader has been initialized.
145 * @param[in] hint The geometry hint to check.
146 * @return True if the given geometry hint is set.
148 bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
150 return mGeometryHints & hint;
154 * Retrieve the set of geometry hints.
157 int GetGeometryHints() const
159 return mGeometryHints;
163 * Set the geometry hints.
164 * @param[in] hints The hints.
166 void SetGeometryHints( int hints )
168 mGeometryHints = hints;
172 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
174 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
176 // no default properties
180 * Set the ID used to access textures
181 * @pre This method is not thread-safe, and should only be called from the update-thread.
182 * @param[in] updateBufferIndex The current update buffer index.
183 * @param[in] textureId The texture ID.
185 void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
188 * Gets the effect texture resource ID
189 * This is zero if there is effect texture
190 * @return the resource Id
192 Integration::ResourceId GetEffectTextureResourceId();
195 * Forwards the meta data from the update thread to the render thread for actual
196 * installation. (Installation is to a std::vector, which is not thread safe)
197 * @sa InstallUniformMetaInRender
198 * @pre This method should only be called from the update thread.
199 * @param[in] updateBufferIndex The current update buffer index.
200 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
202 void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
205 * Forwards the grid density.
206 * @pre This method is not thread-safe, and should only be called from the update thread.
207 * @param[in] updateBufferIndex The current update buffer index.
208 * @param[in] density The grid density.
210 void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
214 * @pre This method is not thread-safe, and should only be called from the update thread.
215 * @param[in] updateBufferIndex The current update buffer index.
216 * @param[in] hint The geometry hints.
218 void ForwardHints( BufferIndex updateBufferIndex, int hint );
220 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
221 // The following methods are called in Render thread
222 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
225 * Set the ID used to access textures
226 * @pre This method is not thread-safe, and should only be called from the render thread.
227 * @param[in] textureId The texture ID.
229 void SetTextureId( Integration::ResourceId textureId );
232 * Get the texture id, that will be used in the next call to Shader::Apply()
233 * @return textureId The texture ID
235 Integration::ResourceId GetTextureIdToRender();
239 * @pre This method is not thread-safe, and should only be called from the update thread.
240 * @param[in] value The grid density
242 void SetGridDensity(float value);
245 * Get the grid density ID.
246 * @pre This method is not thread-safe, and should only be called from the render thread.
247 * @return The grid density.
249 float GetGridDensity();
252 * Installs metadata related to a newly installed uniform property.
253 * @pre This method is not thread-safe, and should only be called from the render-thread.
254 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
256 void InstallUniformMetaInRender( UniformMeta* meta );
259 * Set the program for a geometry type and subtype
260 * @param[in] geometryType The type of the object (geometry) that is to be rendered.
261 * @param[in] subType The subtype, one of ShaderSubTypes.
262 * @param[in] resourceId The resource ID for the program.
263 * @param[in] shaderData The program's vertex/fragment source and optionally compiled bytecode
264 * @param[in] context Reference to the GL context.
265 * @param[in] areVerticesFixed True if the vertex shader does not change vertex position
267 void SetProgram( GeometryType geometryType,
268 Internal::ShaderSubTypes subType,
269 Integration::ResourceId resourceId,
270 Integration::ShaderDataPtr shaderData,
272 bool areVerticesFixed );
275 * Determine if subtypes are required for the given geometry type
276 * @param[in] geometryType The type of the object (geometry) that is to be rendered.
277 * @return TRUE if subtypes are required, FALSE if there is only one subtype available
279 bool AreSubtypesRequired(GeometryType geometryType);
282 * Get the program associated with the given type and subtype
284 Program& GetProgram( Context& context,
286 const ShaderSubTypes subType );
289 * Applies the shader effect specific program and sets the common uniforms
290 * @pre The shader has been initialized.
291 * @pre This method is not thread-safe, and should only be called from the render-thread.
292 * @param[in] context The context used to render.
293 * @param[in] bufferIndex The buffer to read shader properties from.
294 * @param[in] type the type of the object (geometry) that is being rendered.
295 * @param[in] model model matrix of the object.
296 * @param[in] view view matrix of the object.
297 * @param[in] modelview matrix of the object.
298 * @param[in] projection matrix for the camera.
299 * @param[in] color to be used.
300 * @param[in] subType Identifier for geometry types with specialised default shaders
302 Program& Apply( Context& context,
303 BufferIndex bufferIndex,
307 const Matrix& modelview,
308 const Matrix& projection,
309 const Vector4& color,
310 const ShaderSubTypes subType = SHADER_DEFAULT );
313 * Applies the shader effect specific program and sets the common uniforms
314 * @pre The shader has been initialized.
315 * @pre This method is not thread-safe, and should only be called from the render-thread.
316 * @param[in] frametime time elapsed between the two last updates.
318 void SetFrameTime( float frametime );
321 * @return The model view projection matrix
323 inline Matrix& GetMVPMatrix()
325 return mModelViewProjection;
330 typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
332 int mGeometryHints; ///< shader geometry hints for building the geometry
333 float mGridDensity; ///< grid density
334 Texture* mTexture; ///< Raw Pointer to Texture
335 Integration::ResourceId mRenderTextureId; ///< Copy of the texture ID for the render thread
336 Integration::ResourceId mUpdateTextureId; ///< Copy of the texture ID for update thread
338 std::vector<ProgramContainer> mPrograms; ///< 2D array of Program*. Access by [Log<GEOMETRY_TYPE_XXX>::value][index]. An index of 0 selects the default program for that geometry type.
340 Matrix mModelViewProjection; ///< Model view projection
341 UniformMetaContainer mUniformMetadata; ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
343 // These members are only safe to access during UpdateManager::Update()
344 RenderQueue* mRenderQueue; ///< Used for queuing a message for the next Render
346 // These members are only safe to access in render thread
347 PostProcessResourceDispatcher* mPostProcessDispatcher; ///< Used for saving shaders through the resource manager
348 TextureCache* mTextureCache; // Used for retrieving textures in the render thread
349 float mFrametime; ///< Used for setting the frametime delta shader uniform.
352 // Messages for Shader, to be processed in Update thread.
354 inline void SetTextureIdMessage( EventToUpdate& eventToUpdate, const Shader& shader, Integration::ResourceId textureId )
356 typedef MessageDoubleBuffered1< Shader, Integration::ResourceId > LocalType;
358 // Reserve some memory inside the message queue
359 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
361 // Construct message in the message queue memory; note that delete should not be called on the return value
362 new (slot) LocalType( &shader, &Shader::ForwardTextureId, textureId );
365 inline void SetGridDensityMessage( EventToUpdate& eventToUpdate, const Shader& shader, float density )
367 typedef MessageDoubleBuffered1< Shader, float > LocalType;
369 // Reserve some memory inside the message queue
370 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
372 // Construct message in the message queue memory; note that delete should not be called on the return value
373 new (slot) LocalType( &shader, &Shader::ForwardGridDensity, density );
376 inline void SetHintsMessage( EventToUpdate& eventToUpdate, const Shader& shader, Dali::ShaderEffect::GeometryHints hint )
378 typedef MessageDoubleBuffered1< Shader, int > LocalType;
380 // Reserve some memory inside the message queue
381 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
383 // Construct message in the message queue memory; note that delete should not be called on the return value
384 new (slot) LocalType( &shader, &Shader::ForwardHints, hint );
387 inline void InstallUniformMetaMessage( EventToUpdate& eventToUpdate, const Shader& shader, UniformMeta& meta )
389 typedef MessageDoubleBuffered1< Shader, UniformMeta* > LocalType;
391 // Reserve some memory inside the message queue
392 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
394 // Construct message in the message queue memory; note that delete should not be called on the return value
395 new (slot) LocalType( &shader, &Shader::ForwardUniformMeta, &meta );
398 } // namespace SceneGraph
400 } // namespace Internal
404 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__