Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / 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    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
171    */
172   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
173   {
174     // no default properties
175   }
176
177   /**
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.
182    */
183   void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
184
185   /**
186    * Gets the effect texture resource ID
187    * This is zero if there is effect texture
188    * @return the resource Id
189    */
190   Integration::ResourceId GetEffectTextureResourceId();
191
192   /**
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.
199    */
200   void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
201
202   /**
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.
209    */
210   void ForwardCoordinateType( BufferIndex updateBufferIndex, unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
211
212   /**
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.
217    */
218   void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
219
220   /**
221    * Forwards hints.
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.
225    */
226   void ForwardHints( BufferIndex updateBufferIndex, Dali::ShaderEffect::GeometryHints hint );
227
228   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
229   // The following methods are called in Render thread
230   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
231
232   /**
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.
236    */
237   void SetTextureId( Integration::ResourceId textureId );
238
239   /**
240    * Get the texture id, that will be used in the next call to Shader::Apply()
241    * @return textureId The texture ID
242    */
243   Integration::ResourceId GetTextureIdToRender();
244
245   /**
246    * Sets grid density
247    * @pre This method is not thread-safe, and should only be called from the update thread.
248    * @param[in] value The grid density
249    */
250   void SetGridDensity(float value);
251
252   /**
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.
256    */
257   float GetGridDensity();
258
259   /**
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.
263    */
264   void InstallUniformMetaInRender( UniformMeta* meta );
265
266   /**
267    * Sets the uniform coordinate type
268    * @param index of the uniform
269    * @param type to set
270    */
271   void SetCoordinateTypeInRender( unsigned int index, Dali::ShaderEffect::UniformCoordinateType type );
272
273   /**
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
281    */
282   void SetProgram( GeometryType geometryType,
283                    Internal::ShaderSubTypes subType,
284                    Integration::ResourceId resourceId,
285                    Integration::ShaderDataPtr shaderData,
286                    ProgramCache* programCache,
287                    bool modifiesGeometry );
288
289   /**
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
293    */
294   bool AreSubtypesRequired(GeometryType geometryType);
295
296   /**
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.
303    */
304   Program* GetProgram( Context& context,
305                        GeometryType type,
306                        ShaderSubTypes subType,
307                        unsigned int& programIndex );
308
309   /**
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
318    */
319   void SetUniforms( Context& context,
320                     Program& program,
321                     BufferIndex bufferIndex,
322                     unsigned int programIndex,
323                     ShaderSubTypes subType = SHADER_DEFAULT );
324
325 private: // Data
326
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
332
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.
334
335   typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
336   UniformMetaContainer           mUniformMetadata;     ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
337
338   // These members are only safe to access during UpdateManager::Update()
339   RenderQueue*                   mRenderQueue;                   ///< Used for queuing a message for the next Render
340
341   // These members are only safe to access in render thread
342   TextureCache*                  mTextureCache; // Used for retrieving textures in the render thread
343 };
344
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 );
351
352 } // namespace SceneGraph
353
354 } // namespace Internal
355
356 } // namespace Dali
357
358 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__