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/public-api/shader-effects/shader-effect.h>
25 #include <dali/integration-api/shader-data.h>
27 #include <dali/internal/common/buffer-index.h>
28 #include <dali/internal/common/type-abstraction-enums.h>
30 #include <dali/internal/event/common/event-thread-services.h>
31 #include <dali/internal/event/effects/shader-declarations.h>
33 #include <dali/internal/update/common/property-owner.h>
35 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
36 #include <dali/internal/render/gl-resources/texture-declarations.h>
37 #include <dali/internal/render/common/render-manager.h>
45 typedef unsigned int ResourceId;
46 } // namespace Integration
51 class ProgramController;
62 * A base class for a collection of shader programs, to apply an effect to different geometry types.
63 * This class is also the default shader so its easier to override default behaviour
65 class Shader : public PropertyOwner
70 * This container contains pointers to the programs for each sub-type of a given geometry type.
71 * If a custom shader has overridden the subtypes (e.g. mesh custom shader),
72 * then the flag is used to indicate that there is only one shader in the
73 * vector that should be used.
74 * Note, it does not own the Programs it contains.
76 struct ProgramContainer
83 : mUseDefaultForAllSubtypes(false)
89 * @param[in] position The array index
91 Program*& operator[]( size_t position )
93 return mSubPrograms[position];
97 * Resize the container
98 * @param[in] length The new size of the container
100 void Resize(size_t length)
102 mSubPrograms.Resize( length, NULL );
106 * Get the number of elements in the container
107 * @return count of the number of elements in the container
111 return mSubPrograms.Count();
114 Dali::Vector<Program*> mSubPrograms; ///< The programs for each subtype
115 bool mUseDefaultForAllSubtypes; ///< TRUE if the first program should be used for all subtypes
120 * @param hints Geometry hints
122 Shader( Dali::ShaderEffect::GeometryHints& hints );
130 * Second stage initialization, called when added to the UpdateManager
131 * @param renderQueue Used to queue messages from update to render thread.
132 * @param textureCache Used to retrieve effect textures when rendering.
134 void Initialize( RenderQueue& renderQueue, TextureCache& textureCache );
136 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
137 // The following methods are called during UpdateManager::Update()
138 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141 * Query whether a shader geometry hint is set.
142 * @pre The shader has been initialized.
143 * @param[in] hint The geometry hint to check.
144 * @return True if the given geometry hint is set.
146 bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
148 return mGeometryHints & hint;
152 * Retrieve the set of geometry hints.
155 Dali::ShaderEffect::GeometryHints GetGeometryHints() const
157 return mGeometryHints;
161 * Set the geometry hints.
162 * @param[in] hints The hints.
164 void SetGeometryHints( Dali::ShaderEffect::GeometryHints hints )
166 mGeometryHints = hints;
170 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
172 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
174 // no default properties
178 * Set the ID used to access textures
179 * @pre This method is not thread-safe, and should only be called from the update-thread.
180 * @param[in] updateBufferIndex The current update buffer index.
181 * @param[in] textureId The texture ID.
183 void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
186 * Gets the effect texture resource ID
187 * This is zero if there is effect texture
188 * @return the resource Id
190 Integration::ResourceId GetEffectTextureResourceId();
193 * Forwards the meta data from the update thread to the render thread for actual
194 * installation. (Installation is to a std::vector, which is not thread safe)
195 * @sa InstallUniformMetaInRender
196 * @pre This method should only be called from the update thread.
197 * @param[in] updateBufferIndex The current update buffer index.
198 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
200 void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
203 * Forwards coordinate type to render
204 * @sa InstallUniformMetaInRender
205 * @pre This method should only be called from the update thread.
206 * @param[in] updateBufferIndex The current update buffer index.
207 * @param[in] index of the metadata.
208 * @param[in] type the coordinate type.
210 void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
213 * Forwards the grid 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] density The grid density.
218 void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
222 * @pre This method is not thread-safe, and should only be called from the update thread.
223 * @param[in] updateBufferIndex The current update buffer index.
224 * @param[in] hint The geometry hints.
226 void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
228 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
229 // The following methods are called in Render thread
230 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
233 * Set the ID used to access textures
234 * @pre This method is not thread-safe, and should only be called from the render thread.
235 * @param[in] textureId The texture ID.
237 void SetTextureId( Integration::ResourceId textureId );
240 * Get the texture id, that will be used in the next call to Shader::Apply()
241 * @return textureId The texture ID
243 Integration::ResourceId GetTextureIdToRender();
247 * @pre This method is not thread-safe, and should only be called from the update thread.
248 * @param[in] value The grid density
250 void SetGridDensity(float value);
253 * Get the grid density ID.
254 * @pre This method is not thread-safe, and should only be called from the render thread.
255 * @return The grid density.
257 float GetGridDensity();
260 * Installs metadata related to a newly installed uniform property.
261 * @pre This method is not thread-safe, and should only be called from the render-thread.
262 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
264 void InstallUniformMetaInRender( UniformMeta* meta );
267 * Sets the uniform coordinate type
268 * @param index of the uniform
271 void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
274 * Set the program for a geometry type and subtype
275 * @param[in] geometryType The type of the object (geometry) that is to be rendered.
276 * @param[in] subType The subtype, one of ShaderSubTypes.
277 * @param[in] resourceId The resource ID for the program.
278 * @param[in] shaderData The program's vertex/fragment source and optionally compiled bytecode
279 * @param[in] programCache Owner of the Programs
280 * @param[in] modifiesGeometry True if the vertex shader changes geometry
282 void SetProgram( GeometryType geometryType,
283 Internal::ShaderSubTypes subType,
284 Integration::ResourceId resourceId,
285 Integration::ShaderDataPtr shaderData,
286 ProgramCache* programCache,
287 bool modifiesGeometry );
290 * Determine if subtypes are required for the given geometry type
291 * @param[in] geometryType The type of the object (geometry) that is to be rendered.
292 * @return TRUE if subtypes are required, FALSE if there is only one subtype available
294 bool AreSubtypesRequired(GeometryType geometryType);
297 * Get the program associated with the given type and subtype
298 * @param[in] context the context used to render.
299 * @param[in] type the type of the object (geometry) that is being rendered.
300 * @param[in] subType Identifier for geometry types with specialised default shaders
301 * @param[out] programIndex returns the program index to be passed onto SetUniforms.
302 * @return the program to use.
304 Program* GetProgram( Context& context,
306 ShaderSubTypes subType,
307 unsigned int& programIndex );
310 * Sets the shader specific uniforms including custom uniforms
311 * @pre The shader has been initialized.
312 * @pre This method is not thread-safe, and should only be called from the render-thread.
313 * @param[in] context The context used to render.
314 * @param[in] program to use.
315 * @param[in] bufferIndex The buffer to read shader properties from.
316 * @param[in] type the type of the object (geometry) that is being rendered.
317 * @param[in] subType Identifier for geometry types with specialised default shaders
319 void SetUniforms( Context& context,
321 BufferIndex bufferIndex,
322 unsigned int programIndex,
323 ShaderSubTypes subType = SHADER_DEFAULT );
327 Dali::ShaderEffect::GeometryHints mGeometryHints; ///< shader geometry hints for building the geometry
328 float mGridDensity; ///< grid density
329 Texture* mTexture; ///< Raw Pointer to Texture
330 Integration::ResourceId mRenderTextureId; ///< Copy of the texture ID for the render thread
331 Integration::ResourceId mUpdateTextureId; ///< Copy of the texture ID for update thread
333 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.
335 typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
336 UniformMetaContainer mUniformMetadata; ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
338 // These members are only safe to access during UpdateManager::Update()
339 RenderQueue* mRenderQueue; ///< Used for queuing a message for the next Render
341 // These members are only safe to access in render thread
342 TextureCache* mTextureCache; // Used for retrieving textures in the render thread
345 // Messages for Shader, to be processed in Update thread.
346 void SetTextureIdMessage( EventThreadServices& eventThreadServices, const Shader& shader, Integration::ResourceId textureId );
347 void SetGridDensityMessage( EventThreadServices& eventThreadServices, const Shader& shader, float density );
348 void SetHintsMessage( EventThreadServices& eventThreadServices, const Shader& shader, Dali::ShaderEffect::GeometryHints hint );
349 void InstallUniformMetaMessage( EventThreadServices& eventThreadServices, const Shader& shader, UniformMeta& meta );
350 void SetCoordinateTypeMessage( EventThreadServices& eventThreadServices, const Shader& shader, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
352 } // namespace SceneGraph
354 } // namespace Internal
358 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__