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