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 * @return True if the fragment shader outputs only 1.0 on the alpha channel
172 * @note Shaders that can output any value on the alpha channel
173 * including 1.0 should return false for this.
175 bool IsOutputOpaque();
178 * @return True if the fragment shader can output any value but 1.0 on the alpha channel
180 * @note Shaders that can output any value on the alpha channel
181 * including 1.0 should return false for this
183 bool IsOutputTransparent();
186 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
188 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
190 // no default properties
194 * Set the ID used to access textures
195 * @pre This method is not thread-safe, and should only be called from the update-thread.
196 * @param[in] updateBufferIndex The current update buffer index.
197 * @param[in] textureId The texture ID.
199 void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
202 * Gets the effect texture resource ID
203 * This is zero if there is effect texture
204 * @return the resource Id
206 Integration::ResourceId GetEffectTextureResourceId();
209 * Forwards the meta data from the update thread to the render thread for actual
210 * installation. (Installation is to a std::vector, which is not thread safe)
211 * @sa InstallUniformMetaInRender
212 * @pre This method should only be called from the update thread.
213 * @param[in] updateBufferIndex The current update buffer index.
214 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
216 void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
219 * Forwards coordinate type to render
220 * @sa InstallUniformMetaInRender
221 * @pre This method should only be called from the update thread.
222 * @param[in] updateBufferIndex The current update buffer index.
223 * @param[in] index of the metadata.
224 * @param[in] type the coordinate type.
226 void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
229 * Forwards the grid density.
230 * @pre This method is not thread-safe, and should only be called from the update thread.
231 * @param[in] updateBufferIndex The current update buffer index.
232 * @param[in] density The grid density.
234 void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
238 * @pre This method is not thread-safe, and should only be called from the update thread.
239 * @param[in] updateBufferIndex The current update buffer index.
240 * @param[in] hint The geometry hints.
242 void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
244 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
245 // The following methods are called in Render thread
246 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
249 * Set the ID used to access textures
250 * @pre This method is not thread-safe, and should only be called from the render thread.
251 * @param[in] textureId The texture ID.
253 void SetTextureId( Integration::ResourceId textureId );
256 * Get the texture id, that will be used in the next call to Shader::Apply()
257 * @return textureId The texture ID
259 Integration::ResourceId GetTextureIdToRender();
263 * @pre This method is not thread-safe, and should only be called from the update thread.
264 * @param[in] value The grid density
266 void SetGridDensity(float value);
269 * Get the grid density ID.
270 * @pre This method is not thread-safe, and should only be called from the render thread.
271 * @return The grid density.
273 float GetGridDensity();
276 * Installs metadata related to a newly installed uniform property.
277 * @pre This method is not thread-safe, and should only be called from the render-thread.
278 * @param[in] meta A pointer to a UniformMeta to be owned by the Shader.
280 void InstallUniformMetaInRender( UniformMeta* meta );
283 * Sets the uniform coordinate type
284 * @param index of the uniform
287 void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
290 * Set the program for a geometry type and subtype
291 * @param[in] geometryType The type of the object (geometry) that is to be rendered.
292 * @param[in] subType The subtype, one of ShaderSubTypes.
293 * @param[in] resourceId The resource ID for the program.
294 * @param[in] shaderData The program's vertex/fragment source and optionally compiled bytecode
295 * @param[in] programCache Owner of the Programs
296 * @param[in] modifiesGeometry True if the vertex shader changes geometry
298 void SetProgram( GeometryType geometryType,
299 Internal::ShaderSubTypes subType,
300 Integration::ResourceId resourceId,
301 Integration::ShaderDataPtr shaderData,
302 ProgramCache* programCache,
303 bool modifiesGeometry );
306 * Determine if subtypes are required for the given geometry type
307 * @param[in] geometryType The type of the object (geometry) that is to be rendered.
308 * @return TRUE if subtypes are required, FALSE if there is only one subtype available
310 bool AreSubtypesRequired(GeometryType geometryType);
313 * Get the program associated with the given type and subtype
314 * @param[in] context the context used to render.
315 * @param[in] type the type of the object (geometry) that is being rendered.
316 * @param[in] subType Identifier for geometry types with specialised default shaders
317 * @param[out] programIndex returns the program index to be passed onto SetUniforms.
318 * @return the program to use.
320 Program* GetProgram( Context& context,
322 ShaderSubTypes subType,
323 unsigned int& programIndex );
326 * Sets the shader specific uniforms including custom uniforms
327 * @pre The shader has been initialized.
328 * @pre This method is not thread-safe, and should only be called from the render-thread.
329 * @param[in] context The context used to render.
330 * @param[in] program to use.
331 * @param[in] bufferIndex The buffer to read shader properties from.
332 * @param[in] type the type of the object (geometry) that is being rendered.
333 * @param[in] subType Identifier for geometry types with specialised default shaders
335 void SetUniforms( Context& context,
337 BufferIndex bufferIndex,
338 unsigned int programIndex,
339 ShaderSubTypes subType = SHADER_DEFAULT );
343 Dali::ShaderEffect::GeometryHints mGeometryHints; ///< shader geometry hints for building the geometry
344 float mGridDensity; ///< grid density
345 Texture* mTexture; ///< Raw Pointer to Texture
346 Integration::ResourceId mRenderTextureId; ///< Copy of the texture ID for the render thread
347 Integration::ResourceId mUpdateTextureId; ///< Copy of the texture ID for update thread
349 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.
351 typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
352 UniformMetaContainer mUniformMetadata; ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
354 // These members are only safe to access during UpdateManager::Update()
355 RenderQueue* mRenderQueue; ///< Used for queuing a message for the next Render
357 // These members are only safe to access in render thread
358 TextureCache* mTextureCache; // Used for retrieving textures in the render thread
361 // Messages for Shader, to be processed in Update thread.
362 void SetTextureIdMessage( EventThreadServices& eventThreadServices, const Shader& shader, Integration::ResourceId textureId );
363 void SetGridDensityMessage( EventThreadServices& eventThreadServices, const Shader& shader, float density );
364 void SetHintsMessage( EventThreadServices& eventThreadServices, const Shader& shader, Dali::ShaderEffect::GeometryHints hint );
365 void InstallUniformMetaMessage( EventThreadServices& eventThreadServices, const Shader& shader, UniformMeta& meta );
366 void SetCoordinateTypeMessage( EventThreadServices& eventThreadServices, const Shader& shader, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
368 } // namespace SceneGraph
370 } // namespace Internal
374 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__