Merge branch 'tizen' of platform/core/uifw/dali-core into devel/new_mesh
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / scene-graph-shader.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/shader-effects/shader-effect.h>
24
25 #include <dali/integration-api/shader-data.h>
26
27 #include <dali/internal/common/buffer-index.h>
28 #include <dali/internal/common/type-abstraction-enums.h>
29
30 #include <dali/internal/event/common/event-thread-services.h>
31 #include <dali/internal/event/effects/shader-declarations.h>
32
33 #include <dali/internal/update/common/property-owner.h>
34
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>
38
39
40 namespace Dali
41 {
42
43 namespace Integration
44 {
45 typedef unsigned int ResourceId;
46 } // namespace Integration
47
48 namespace Internal
49 {
50
51 class ProgramController;
52 class Program;
53
54 namespace SceneGraph
55 {
56
57 class RenderQueue;
58 class UniformMeta;
59 class TextureCache;
60
61 /**
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
64  */
65 class Shader : public PropertyOwner
66 {
67 public:
68
69   /**
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.
75    */
76   struct ProgramContainer
77   {
78   public:
79     /**
80      * Constructor
81      */
82     ProgramContainer()
83     : mUseDefaultForAllSubtypes(false)
84     {
85     }
86
87     /**
88      * Array lookup
89      * @param[in] position The array index
90      */
91     Program*& operator[]( size_t position )
92     {
93       return mSubPrograms[position];
94     }
95
96     /**
97      * Resize the container
98      * @param[in] length The new size of the container
99      */
100     void Resize(size_t length)
101     {
102       mSubPrograms.Resize( length, NULL );
103     }
104
105     /**
106      * Get the number of elements in the container
107      * @return count of the number of elements in the container
108      */
109     size_t Count() const
110     {
111       return mSubPrograms.Count();
112     }
113
114     Dali::Vector<Program*> mSubPrograms; ///< The programs for each subtype
115     bool mUseDefaultForAllSubtypes;      ///< TRUE if the first program should be used for all subtypes
116   };
117
118   /**
119    * Constructor
120    * @param hints Geometry hints
121    */
122   Shader( Dali::ShaderEffect::GeometryHints& hints );
123
124   /**
125    * Virtual destructor
126    */
127   virtual ~Shader();
128
129   /**
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.
133    */
134   void Initialize( RenderQueue& renderQueue, TextureCache& textureCache );
135
136   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
137   // The following methods are called during UpdateManager::Update()
138   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139
140   /**
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.
145    */
146   bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
147   {
148     return mGeometryHints & hint;
149   }
150
151   /**
152    * Retrieve the set of geometry hints.
153    * @return The hints.
154    */
155   Dali::ShaderEffect::GeometryHints GetGeometryHints() const
156   {
157     return mGeometryHints;
158   }
159
160   /**
161    * Set the geometry hints.
162    * @param[in] hints The hints.
163    */
164   void SetGeometryHints( Dali::ShaderEffect::GeometryHints hints )
165   {
166     mGeometryHints = hints;
167   }
168
169   /**
170    * @return True if the fragment shader outputs only 1.0 on the alpha channel
171    *
172    * @note Shaders that can output any value on the alpha channel
173    * including 1.0 should return false for this.
174    */
175   bool IsOutputOpaque();
176
177   /**
178    * @return True if the fragment shader can output any value but 1.0 on the alpha channel
179    *
180    * @note Shaders that can output any value on the alpha channel
181    * including 1.0 should return false for this
182    */
183   bool IsOutputTransparent();
184
185   /**
186    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
187    */
188   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
189   {
190     // no default properties
191   }
192
193   /**
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.
198    */
199   void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
200
201   /**
202    * Gets the effect texture resource ID
203    * This is zero if there is effect texture
204    * @return the resource Id
205    */
206   Integration::ResourceId GetEffectTextureResourceId();
207
208   /**
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.
215    */
216   void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
217
218   /**
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.
225    */
226   void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
227
228   /**
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.
233    */
234   void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
235
236   /**
237    * Forwards hints.
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.
241    */
242   void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
243
244   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
245   // The following methods are called in Render thread
246   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
247
248   /**
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.
252    */
253   void SetTextureId( Integration::ResourceId textureId );
254
255   /**
256    * Get the texture id, that will be used in the next call to Shader::Apply()
257    * @return textureId The texture ID
258    */
259   Integration::ResourceId GetTextureIdToRender();
260
261   /**
262    * Sets grid density
263    * @pre This method is not thread-safe, and should only be called from the update thread.
264    * @param[in] value The grid density
265    */
266   void SetGridDensity(float value);
267
268   /**
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.
272    */
273   float GetGridDensity();
274
275   /**
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.
279    */
280   void InstallUniformMetaInRender( UniformMeta* meta );
281
282   /**
283    * Sets the uniform coordinate type
284    * @param index of the uniform
285    * @param type to set
286    */
287   void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
288
289   /**
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
297    */
298   void SetProgram( GeometryType geometryType,
299                    Internal::ShaderSubTypes subType,
300                    Integration::ResourceId resourceId,
301                    Integration::ShaderDataPtr shaderData,
302                    ProgramCache* programCache,
303                    bool modifiesGeometry );
304
305   /**
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
309    */
310   bool AreSubtypesRequired(GeometryType geometryType);
311
312   /**
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.
319    */
320   Program* GetProgram( Context& context,
321                        GeometryType type,
322                        ShaderSubTypes subType,
323                        unsigned int& programIndex );
324
325   /**
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
334    */
335   void SetUniforms( Context& context,
336                     Program& program,
337                     BufferIndex bufferIndex,
338                     unsigned int programIndex,
339                     ShaderSubTypes subType = SHADER_DEFAULT );
340
341 private: // Data
342
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
348
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.
350
351   typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
352   UniformMetaContainer           mUniformMetadata;     ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
353
354   // These members are only safe to access during UpdateManager::Update()
355   RenderQueue*                   mRenderQueue;                   ///< Used for queuing a message for the next Render
356
357   // These members are only safe to access in render thread
358   TextureCache*                  mTextureCache; // Used for retrieving textures in the render thread
359 };
360
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 );
367
368 } // namespace SceneGraph
369
370 } // namespace Internal
371
372 } // namespace Dali
373
374 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__