Added image renderer clip space culling
[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/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>
36
37 namespace Dali
38 {
39 class Matrix;
40 struct Vector4;
41
42 namespace Integration
43 {
44 typedef unsigned int ResourceId;
45 } // namespace Integration
46
47 namespace Internal
48 {
49
50 class Context;
51 class Program;
52 class ResourceManager;
53
54 namespace SceneGraph
55 {
56
57 class RenderQueue;
58 class UniformMeta;
59 class PostProcessResourceDispatcher;
60 class TextureCache;
61
62 /**
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
65  */
66 class Shader : public PropertyOwner
67 {
68 public:
69
70   /**
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).
76    */
77   struct ProgramContainer
78   {
79   public:
80     /**
81      * Constructor
82      */
83     ProgramContainer()
84     : mUseDefaultForAllSubtypes(false)
85     {
86     }
87
88     /**
89      * Array lookup
90      * @param[in] position The array index
91      */
92     Program*& operator[]( size_t position )
93     {
94       return mSubPrograms[position];
95     }
96
97     /**
98      * Resize the container
99      * @param[in] length The new size of the container
100      */
101     void Resize(size_t length)
102     {
103       mSubPrograms.Resize(length);
104     }
105
106     /**
107      * Get the number of elements in the container
108      * @return count of the number of elements in the container
109      */
110     size_t Count() const
111     {
112       return mSubPrograms.Count();
113     }
114
115     Dali::Vector<Program*> mSubPrograms; ///< The programs for each subtype
116     bool mUseDefaultForAllSubtypes;      ///< TRUE if the first program should be used for all subtypes
117   };
118
119   /**
120    * Constructor
121    * @param hints Geometry hints
122    */
123   Shader( Dali::ShaderEffect::GeometryHints& hints );
124
125   /**
126    * Virtual destructor
127    */
128   virtual ~Shader();
129
130   /**
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.
135    */
136   void Initialize( PostProcessResourceDispatcher& postProcessResourceDispatcher, RenderQueue& renderQueue, TextureCache& textureCache );
137
138   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139   // The following methods are called during UpdateManager::Update()
140   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141
142   /**
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.
147    */
148   bool GeometryHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
149   {
150     return mGeometryHints & hint;
151   }
152
153   /**
154    * Retrieve the set of geometry hints.
155    * @return The hints.
156    */
157   int GetGeometryHints() const
158   {
159     return mGeometryHints;
160   }
161
162   /**
163    * Set the geometry hints.
164    * @param[in] hints The hints.
165    */
166   void SetGeometryHints( int hints )
167   {
168     mGeometryHints = hints;
169   }
170
171   /**
172    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties
173    */
174   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex )
175   {
176     // no default properties
177   }
178
179   /**
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.
184    */
185   void ForwardTextureId( BufferIndex updateBufferIndex, Integration::ResourceId textureId );
186
187   /**
188    * Gets the effect texture resource ID
189    * This is zero if there is effect texture
190    * @return the resource Id
191    */
192   Integration::ResourceId GetEffectTextureResourceId();
193
194   /**
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.
201    */
202   void ForwardUniformMeta( BufferIndex updateBufferIndex, UniformMeta* meta );
203
204   /**
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.
209    */
210   void ForwardGridDensity( BufferIndex updateBufferIndex, float density );
211
212   /**
213    * Forwards hints.
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.
217    */
218   void ForwardHints( BufferIndex updateBufferIndex, int hint );
219
220   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
221   // The following methods are called in Render thread
222   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
223
224   /**
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.
228    */
229   void SetTextureId( Integration::ResourceId textureId );
230
231   /**
232    * Get the texture id, that will be used in the next call to Shader::Apply()
233    * @return textureId The texture ID
234    */
235   Integration::ResourceId GetTextureIdToRender();
236
237   /**
238    * Sets grid density
239    * @pre This method is not thread-safe, and should only be called from the update thread.
240    * @param[in] value The grid density
241    */
242   void SetGridDensity(float value);
243
244   /**
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.
248    */
249   float GetGridDensity();
250
251   /**
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.
255    */
256   void InstallUniformMetaInRender( UniformMeta* meta );
257
258   /**
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
266    */
267   void SetProgram( GeometryType geometryType,
268                    Internal::ShaderSubTypes subType,
269                    Integration::ResourceId resourceId,
270                    Integration::ShaderDataPtr shaderData,
271                    Context* context,
272                    bool areVerticesFixed );
273
274   /**
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
278    */
279   bool AreSubtypesRequired(GeometryType geometryType);
280
281   /**
282    * Get the program associated with the given type and subtype
283    */
284   Program& GetProgram( Context& context,
285                        GeometryType type,
286                        const ShaderSubTypes subType );
287
288   /**
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
301    */
302   Program& Apply( Context& context,
303                   BufferIndex bufferIndex,
304                   GeometryType type,
305                   const Matrix& model,
306                   const Matrix& view,
307                   const Matrix& modelview,
308                   const Matrix& projection,
309                   const Vector4& color,
310                   const ShaderSubTypes subType = SHADER_DEFAULT );
311
312   /**
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.
317    */
318   void SetFrameTime( float frametime );
319
320   /**
321    * @return The model view projection matrix
322    */
323   inline Matrix& GetMVPMatrix()
324   {
325     return mModelViewProjection;
326   }
327
328 private: // Data
329
330   typedef OwnerContainer< UniformMeta* > UniformMetaContainer;
331
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
337
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.
339
340   Matrix                         mModelViewProjection; ///< Model view projection
341   UniformMetaContainer           mUniformMetadata;     ///< A container of owned UniformMeta values; one for each property in PropertyOwner::mDynamicProperties
342
343   // These members are only safe to access during UpdateManager::Update()
344   RenderQueue*                   mRenderQueue;                   ///< Used for queuing a message for the next Render
345
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.
350 };
351
352 // Messages for Shader, to be processed in Update thread.
353
354 inline void SetTextureIdMessage( EventToUpdate& eventToUpdate, const Shader& shader, Integration::ResourceId textureId )
355 {
356   typedef MessageDoubleBuffered1< Shader, Integration::ResourceId > LocalType;
357
358   // Reserve some memory inside the message queue
359   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
360
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 );
363 }
364
365 inline void SetGridDensityMessage( EventToUpdate& eventToUpdate, const Shader& shader, float density )
366 {
367   typedef MessageDoubleBuffered1< Shader, float > LocalType;
368
369   // Reserve some memory inside the message queue
370   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
371
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 );
374 }
375
376 inline void SetHintsMessage( EventToUpdate& eventToUpdate, const Shader& shader, Dali::ShaderEffect::GeometryHints hint )
377 {
378   typedef MessageDoubleBuffered1< Shader, int > LocalType;
379
380   // Reserve some memory inside the message queue
381   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
382
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 );
385 }
386
387 inline void InstallUniformMetaMessage( EventToUpdate& eventToUpdate, const Shader& shader, UniformMeta& meta )
388 {
389   typedef MessageDoubleBuffered1< Shader, UniformMeta* > LocalType;
390
391   // Reserve some memory inside the message queue
392   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
393
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 );
396 }
397
398 } // namespace SceneGraph
399
400 } // namespace Internal
401
402 } // namespace Dali
403
404 #endif // __DALI_INTERNAL_SCENE_GRAPH_SHADER_H__