995f515dc3d3071733d763591d694210f85034b6
[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( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
83   {
84     sortAttributes.shader = &( mRenderDataProvider->GetShader() );
85     const SceneGraph::RenderDataProvider::Samplers& samplers = mRenderDataProvider->GetSamplers();
86
87     sortAttributes.textureResourceId = samplers.Empty() ? Integration::InvalidResourceId : samplers[ 0 ]->GetTextureId( bufferIndex );
88     sortAttributes.geometry = mRenderGeometry;
89   }
90
91 public: // Implementation of Renderer
92   /**
93    * @copydoc SceneGraph::Renderer::RequiresDepthTest()
94    */
95   virtual bool RequiresDepthTest() const;
96
97   /**
98    * @copydoc SceneGraph::Renderer::CheckResources()
99    */
100   virtual bool CheckResources();
101
102   /**
103    * @copydoc SceneGraph::Renderer::IsOutsideClipSpace()
104    */
105   virtual bool IsOutsideClipSpace( Context& context,
106                                    const Matrix& modelViewProjectionMatrix );
107
108   /**
109    * @copydoc SceneGraph::Renderer::DoSetUniforms()
110    */
111   virtual void DoSetUniforms( Context& context, BufferIndex bufferIndex, SceneGraph::Shader* shader, Program* program, unsigned int programIndex );
112
113   /**
114    * @copydoc SceneGraph::Renderer::DoSetCullFaceMode
115    */
116   virtual void DoSetCullFaceMode(Context& context, BufferIndex bufferIndex );
117
118   /**
119    * @copydoc SceneGraph::Renderer::DoSetBlending
120    */
121   virtual void DoSetBlending(Context& context, BufferIndex bufferIndex, bool blend );
122
123   /**
124    * @copydoc SceneGraph::Renderer::DoRender()
125    */
126   virtual void DoRender( Context& context,
127                          SceneGraph::TextureCache& textureCache,
128                          const SceneGraph::NodeDataProvider& node,
129                          BufferIndex bufferIndex,
130                          Program& program,
131                          const Matrix& modelViewMatrix,
132                          const Matrix& viewMatrix );
133
134 public: // Implementation of GlResourceOwner
135
136   /**
137    * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
138    */
139   virtual void GlContextDestroyed();
140
141   /**
142    * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
143    */
144   virtual void GlCleanup();
145
146 private:
147   struct UniformIndexMap;
148
149   /**
150    * Set the uniforms from properties according to the uniform map
151    * @param[in] node The node using the renderer
152    * @param[in] program The shader program on which to set the uniforms.
153    */
154   void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, Program& program );
155
156   /**
157    * Set the program uniform in the map from the mapped property
158    */
159   void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
160
161   /**
162    * Bind the material textures in the samplers and setup the samplers
163    * @param[in] textureCache The texture cache
164    * @param[in] bufferIndex The buffer index
165    * @param[in] program The shader program
166    * @param[in] samplers The samplers to bind
167    */
168   void BindTextures( SceneGraph::TextureCache& textureCache,
169                      BufferIndex bufferIndex,
170                      Program& program,
171                      const SceneGraph::RenderDataProvider::Samplers& samplers );
172
173   /**
174    * Bind a material texture to a texture unit, and set the sampler's texture uniform
175    * to that texture unit.
176    * @param[in] textureCache The texture cache
177    * @param[in] program The shader program
178    * @param[in] id The resource id of the texture to bind
179    * @param[in] texture The texture to bind
180    * @param[in] textureUnit The texture unit index to use
181    * @param[in] nameIndex The index of the texture uniform in the program
182    */
183   void BindTexture( SceneGraph::TextureCache& textureCache,
184                     Program& program,
185                     ResourceId id,
186                     Texture* texture,
187                     TextureUnit textureUnit,
188                     unsigned int nameIndex );
189
190   /**
191    * Apply the sampler modes to the texture.
192    * @param[in] bufferIndex The current buffer index
193    * @param[in] texture The texture to which to apply the sampler modes
194    * @param[in] textureUnit The texture unit of the texture
195    * @param[in] sampler The sampler from which to get the modes.
196    */
197   void ApplySampler( BufferIndex bufferIndex,
198                      Texture* texture,
199                      TextureUnit textureUnit,
200                      const SceneGraph::SamplerDataProvider& sampler );
201
202   /**
203    * Get the texture uniform index of the name sampler in the program.
204    * If not already registered in the program, then this performs the registration
205    * @param[in] program The shader program
206    * @param[in] sampler The sampler holding a texture unit uniform name to search for
207    * @return The texture uniform index in the program
208    */
209   unsigned int GetTextureUnitUniformIndex( Program& program,
210                                            const SceneGraph::SamplerDataProvider& sampler );
211
212
213
214 public: //@todo MESH_REWORK make private after merge with SceneGraph::Renderer
215   OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
216
217 private:
218   SceneGraph::RenderGeometry* mRenderGeometry;
219
220   struct TextureUnitUniformIndex
221   {
222     const SceneGraph::SamplerDataProvider* sampler;
223     unsigned int index;
224   };
225
226   typedef Dali::Vector< TextureUnitUniformIndex > TextureUnitUniforms;
227   TextureUnitUniforms mTextureUnitUniforms;
228
229   struct UniformIndexMap
230   {
231     unsigned int uniformIndex; // The index of the cached location in the Program
232     const PropertyInputImpl* propertyValue;
233   };
234
235   typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
236   UniformIndexMappings mUniformIndexMap;
237
238   Vector<GLint> mAttributesLocation;
239   bool mUpdateAttributesLocation;
240
241 };
242
243
244 } // SceneGraph
245 } // Internal
246 } // Dali
247
248 #endif // __DALI_INTERNAL_RENDER_NEW_RENDERER_H__