Uniform map handling
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_NEW_RENDERER_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_NEW_RENDERER_H__
3
4 /*
5  * Copyright (c) 2015 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 #include <dali/integration-api/resource-declarations.h> // For resource id
22 #include <dali/internal/render/data-providers/material-data-provider.h>
23 #include <dali/internal/render/data-providers/uniform-map-data-provider.h>
24 #include <dali/internal/render/gl-resources/texture-units.h>
25 #include <dali/internal/render/renderers/scene-graph-renderer.h>
26 #include <dali/internal/render/renderers/render-geometry.h>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32 class PropertyInputImpl;
33
34 namespace SceneGraph
35 {
36 class GeometryDataProvider;
37 class MaterialDataProvider;
38 class NodeDataProvider;
39 class RenderGeometry;
40 class SamplerDataProvider;
41 class ShaderDataProvider;
42 class UniformMapDataProvider;
43
44 /**
45  * The new geometry renderer.
46  *
47  * @todo MESH_REWORK It will be merged into the base class eventually
48  */
49 class NewRenderer : public Renderer
50 {
51 private:
52   struct UniformIndexMap;
53
54 public:
55   typedef Integration::ResourceId ResourceId;
56
57   /**
58    * Create a new renderer instance
59    * @param[in] nodeDataProvider The node data provider
60    * @param[in] uniformMapDataProvider The uniform map data provider
61    * @param[in] geometryDataProvider The geometry data provider
62    * @param[in] materialDataProvider The material data provider
63    */
64   static NewRenderer* New( NodeDataProvider& nodeDataProvider,
65                            const UniformMapDataProvider& uniformMapDataProvider,
66                            const GeometryDataProvider* geometryDataProvider,
67                            const MaterialDataProvider* materialDataProvider );
68   /**
69    * Constructor.
70    * @param[in] nodeDataProvider The node data provider
71    * @param[in] uniformMapDataProvider The uniform map data provider
72    * @param[in] geometryDataProvider The geometry data provider
73    * @param[in] materialDataProvider The material data provider
74    */
75   NewRenderer( NodeDataProvider& nodeDataProvider,
76                const UniformMapDataProvider& uniformMapDataProvider,
77                const GeometryDataProvider* geometryDataProvider,
78                const MaterialDataProvider* materialDataProvider );
79
80   virtual ~NewRenderer();
81
82   /**
83    * Change the geometry data provider of the renderer
84    * @param[in] geoemtryDataProvider The geometry data provider
85    */
86   void SetGeometryDataProvider( const GeometryDataProvider* geometryDataProvider );
87
88   /**
89    * Change the material data provider of the renderer
90    * @param[in] materialDataProvider The material data provider
91    */
92   void SetMaterialDataProvider( const MaterialDataProvider* materialDataProvider );
93
94 public: // Implementation of Renderer
95   /**
96    * @copydoc SceneGraph::Renderer::RequiresDepthTest()
97    */
98   virtual bool RequiresDepthTest() const;
99
100   /**
101    * @copydoc SceneGraph::Renderer::CheckResources()
102    */
103   virtual bool CheckResources();
104
105   /**
106    * @copydoc SceneGraph::Renderer::ResolveGeometryTypes()
107    */
108   virtual void ResolveGeometryTypes( BufferIndex bufferIndex,
109                                      GeometryType& outType,
110                                      ShaderSubTypes& outSubType );
111
112   /**
113    * @copydoc SceneGraph::Renderer::IsOutsideClipSpace()
114    */
115   virtual bool IsOutsideClipSpace( const Matrix& modelMatrix,
116                                    const Matrix& modelViewProjectionMatrix );
117
118   /**
119    * @copydoc SceneGraph::Renderer::DoSetUniforms()
120    */
121   virtual void DoSetUniforms(Shader* shader, Context* context, Program* program, BufferIndex bufferIndex, unsigned int programIndex, ShaderSubTypes subType );
122
123   /**
124    * @copydoc SceneGraph::Renderer::DoRender()
125    */
126   virtual void DoRender( BufferIndex bufferIndex,
127                          Program& program,
128                          const Matrix& modelViewMatrix,
129                          const Matrix& viewMatrix );
130
131 public: // Implementation of GlResourceOwner
132
133   /**
134    * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
135    */
136   virtual void GlContextDestroyed();
137
138   /**
139    * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
140    */
141   virtual void GlCleanup();
142
143 private:
144   /**
145    * Set the uniforms from properties according to the uniform map
146    * @param[in] program The shader program on which to set the uniforms.
147    */
148   void SetUniforms( BufferIndex bufferIndex, Program& program );
149
150   /**
151    * Set the program uniform in the map from the mapped property
152    */
153   void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
154
155   /**
156    * Bind the material textures in the samplers and setup the samplers
157    * @param[in] bufferIndex The buffer index
158    * @param[in] program The shader program
159    * @param[in] samplers The samplers to bind
160    */
161   void BindTextures( BufferIndex bufferIndex,
162                      Program& program,
163                      const MaterialDataProvider::Samplers& samplers );
164
165   /**
166    * Bind a material texture to a texture unit, and set the sampler's texture uniform
167    * to that texture unit.
168    * @param[in] program The shader program
169    * @param[in] id The resource id of the texture to bind
170    * @param[in] texture The texture to bind
171    * @param[in] textureUnit The texture unit index to use
172    * @param[in] nameIndex The index of the texture uniform in the program
173    */
174   void BindTexture( Program& program,
175                     ResourceId id,
176                     Texture* texture,
177                     TextureUnit textureUnit,
178                     unsigned int nameIndex );
179
180   /**
181    * Apply the sampler modes to the texture.
182    * @param[in] bufferIndex The current buffer index
183    * @param[in] texture The texture to which to apply the sampler modes
184    * @param[in] textureUnit The texture unit of the texture
185    * @param[in] sampler The sampler from which to get the modes.
186    */
187   void ApplySampler( BufferIndex bufferIndex,
188                      Texture* texture,
189                      TextureUnit textureUnit,
190                      const SamplerDataProvider& sampler );
191
192   /**
193    * Get the texture uniform index of the name sampler in the program.
194    * If not already registered in the program, then this performs the registration
195    * @param[in] program The shader program
196    * @param[in] sampler The sampler holding a texture unit uniform name to search for
197    * @return The texture uniform index in the program
198    */
199   unsigned int GetTextureUnitUniformIndex( Program& program,
200                                            const SamplerDataProvider& sampler );
201
202
203 public:
204   // @todo MESH_REWORK Make private - add getters
205   //const NodeDataProvider&     mNodeDataProvider;
206   //const ShaderDataProvider&   mShaderDataProvider;
207   const UniformMapDataProvider& mUniformMapDataProvider;
208   const MaterialDataProvider* mMaterialDataProvider;
209   const GeometryDataProvider* mGeometryDataProvider;
210
211 private:
212
213   RenderGeometry mRenderGeometry;
214
215   struct TextureUnitUniformIndex
216   {
217     const SamplerDataProvider* sampler;
218     unsigned int index;
219   };
220
221   typedef Dali::Vector< TextureUnitUniformIndex > TextureUnitUniforms;
222   TextureUnitUniforms mTextureUnitUniforms;
223
224   struct UniformIndexMap
225   {
226     unsigned int uniformIndex; // The index of the cached location in the Program
227     const PropertyInputImpl* propertyValue;
228   };
229
230   typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
231   UniformIndexMappings mUniformIndexMap;
232 };
233
234
235 } // SceneGraph
236 } // Internal
237 } // Dali
238
239 #endif // __DALI_INTERNAL_SCENE_GRAPH_NEW_RENDERER_H__