1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_RENDERER_H
5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/math/matrix.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/public-api/rendering/texture-set.h>
25 #include <dali/internal/common/blending-options.h>
26 #include <dali/internal/common/message.h>
27 #include <dali/internal/common/type-abstraction-enums.h>
28 #include <dali/internal/event/common/property-input-impl.h>
29 #include <dali/internal/event/effects/shader-declarations.h>
30 #include <dali/internal/render/data-providers/render-data-provider.h>
31 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
32 #include <dali/internal/render/renderers/render-geometry.h>
33 #include <dali/internal/update/manager/prepare-render-instructions.h>
34 #include <dali/integration-api/debug.h>
47 class SceneController;
50 class NodeDataProvider;
58 * Renderers are used to render meshes
59 * These objects are used during RenderManager::Render(), so properties modified during
60 * the Update must either be double-buffered, or set via a message added to the RenderQueue.
62 class Renderer : public GlResourceOwner
67 * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
69 struct StencilParameters
71 StencilParameters( RenderMode::Type renderMode, StencilFunction::Type stencilFunction, int stencilFunctionMask,
72 int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail,
73 StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass )
74 : stencilFunctionMask ( stencilFunctionMask ),
75 stencilFunctionReference ( stencilFunctionReference ),
76 stencilMask ( stencilMask ),
77 renderMode ( renderMode ),
78 stencilFunction ( stencilFunction ),
79 stencilOperationOnFail ( stencilOperationOnFail ),
80 stencilOperationOnZFail ( stencilOperationOnZFail ),
81 stencilOperationOnZPass ( stencilOperationOnZPass )
85 int stencilFunctionMask; ///< The stencil function mask
86 int stencilFunctionReference; ///< The stencil function reference
87 int stencilMask; ///< The stencil mask
88 RenderMode::Type renderMode:3; ///< The render mode
89 StencilFunction::Type stencilFunction:3; ///< The stencil function
90 StencilOperation::Type stencilOperationOnFail:3; ///< The stencil operation for stencil test fail
91 StencilOperation::Type stencilOperationOnZFail:3; ///< The stencil operation for depth test fail
92 StencilOperation::Type stencilOperationOnZPass:3; ///< The stencil operation for depth test pass
96 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
98 void GlContextDestroyed();
101 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
106 * Create a new renderer instance
107 * @param[in] dataProviders The data providers for the renderer
108 * @param[in] geometry The geometry for the renderer
109 * @param[in] blendingBitmask A bitmask of blending options.
110 * @param[in] blendColor The blend color to pass to GL
111 * @param[in] faceCullingMode The face-culling mode.
112 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
113 * @param[in] depthWriteMode Depth buffer write mode
114 * @param[in] depthTestMode Depth buffer test mode
115 * @param[in] depthFunction Depth function
116 * @param[in] stencilParameters Struct containing all stencil related options
118 static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
119 Render::Geometry* geometry,
120 unsigned int blendingBitmask,
121 const Vector4* blendColor,
122 FaceCullingMode::Type faceCullingMode,
123 bool preMultipliedAlphaEnabled,
124 DepthWriteMode::Type depthWriteMode,
125 DepthTestMode::Type depthTestMode,
126 DepthFunction::Type depthFunction,
127 StencilParameters& stencilParameters );
131 * @param[in] dataProviders The data providers for the renderer
132 * @param[in] geometry The geometry for the renderer
133 * @param[in] blendingBitmask A bitmask of blending options.
134 * @param[in] blendColor The blend color to pass to GL
135 * @param[in] faceCullingMode The face-culling mode.
136 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
137 * @param[in] depthWriteMode Depth buffer write mode
138 * @param[in] depthTestMode Depth buffer test mode
139 * @param[in] depthFunction Depth function
140 * @param[in] stencilParameters Struct containing all stencil related options
142 Renderer( SceneGraph::RenderDataProvider* dataProviders,
143 Render::Geometry* geometry,
144 unsigned int blendingBitmask,
145 const Vector4* blendColor,
146 FaceCullingMode::Type faceCullingMode,
147 bool preMultipliedAlphaEnabled,
148 DepthWriteMode::Type depthWriteMode,
149 DepthTestMode::Type depthTestMode,
150 DepthFunction::Type depthFunction,
151 StencilParameters& stencilParameters );
154 * Change the data providers of the renderer
155 * @param[in] dataProviders The data providers
157 void SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProviders );
160 * Change the geometry used by the renderer
161 * @param[in] geometry The new geometry
163 void SetGeometry( Render::Geometry* geometry );
165 * Second-phase construction.
166 * This is called when the renderer is inside render thread
167 * @param[in] context Context used by the renderer
168 * @param[in] textureCache The texture cache to use
170 void Initialize( Context& context, SceneGraph::TextureCache& textureCache );
178 * Set the face-culling mode.
179 * @param[in] mode The face-culling mode.
181 void SetFaceCullingMode( FaceCullingMode::Type mode );
184 * Set the bitmask for blending options
185 * @param[in] bitmask A bitmask of blending options.
187 void SetBlendingBitMask( unsigned int bitmask );
190 * Set the blend color for blending options
191 * @param[in] blendColor The blend color to pass to GL
193 void SetBlendColor( const Vector4* color );
196 * Set the first element index to draw by the indexed draw
197 * @param[in] firstElement index of first element to draw
199 void SetIndexedDrawFirstElement( size_t firstElement );
202 * Set the number of elements to draw by the indexed draw
203 * @param[in] elementsCount number of elements to draw
205 void SetIndexedDrawElementsCount( size_t elementsCount );
208 * @brief Set whether the Pre-multiplied Alpha Blending is required
210 * @param[in] preMultipled whether alpha is pre-multiplied.
212 void EnablePreMultipliedAlpha( bool preMultipled );
215 * Sets the depth write mode
216 * @param[in] depthWriteMode The depth write mode
218 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
221 * Query the Renderer's depth write mode
222 * @return The renderer depth write mode
224 DepthWriteMode::Type GetDepthWriteMode() const;
227 * Sets the depth test mode
228 * @param[in] depthTestMode The depth test mode
230 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
233 * Query the Renderer's depth test mode
234 * @return The renderer depth test mode
236 DepthTestMode::Type GetDepthTestMode() const;
239 * Sets the depth function
240 * @param[in] depthFunction The depth function
242 void SetDepthFunction( DepthFunction::Type depthFunction );
245 * Query the Renderer's depth function
246 * @return The renderer depth function
248 DepthFunction::Type GetDepthFunction() const;
251 * Sets the render mode
252 * @param[in] renderMode The render mode
254 void SetRenderMode( RenderMode::Type mode );
257 * Gets the render mode
258 * @return The render mode
260 RenderMode::Type GetRenderMode() const;
263 * Sets the stencil function
264 * @param[in] stencilFunction The stencil function
266 void SetStencilFunction( StencilFunction::Type stencilFunction );
269 * Gets the stencil function
270 * @return The stencil function
272 StencilFunction::Type GetStencilFunction() const;
275 * Sets the stencil function mask
276 * @param[in] stencilFunctionMask The stencil function mask
278 void SetStencilFunctionMask( int stencilFunctionMask );
281 * Gets the stencil function mask
282 * @return The stencil function mask
284 int GetStencilFunctionMask() const;
287 * Sets the stencil function reference
288 * @param[in] stencilFunctionReference The stencil function reference
290 void SetStencilFunctionReference( int stencilFunctionReference );
293 * Gets the stencil function reference
294 * @return The stencil function reference
296 int GetStencilFunctionReference() const;
299 * Sets the stencil mask
300 * @param[in] stencilMask The stencil mask
302 void SetStencilMask( int stencilMask );
305 * Gets the stencil mask
306 * @return The stencil mask
308 int GetStencilMask() const;
311 * Sets the stencil operation for when the stencil test fails
312 * @param[in] stencilOperationOnFail The stencil operation
314 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
317 * Gets the stencil operation for when the stencil test fails
318 * @return The stencil operation
320 StencilOperation::Type GetStencilOperationOnFail() const;
323 * Sets the stencil operation for when the depth test fails
324 * @param[in] stencilOperationOnZFail The stencil operation
326 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
329 * Gets the stencil operation for when the depth test fails
330 * @return The stencil operation
332 StencilOperation::Type GetStencilOperationOnZFail() const;
335 * Sets the stencil operation for when the depth test passes
336 * @param[in] stencilOperationOnZPass The stencil operation
338 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
341 * Gets the stencil operation for when the depth test passes
342 * @return The stencil operation
344 StencilOperation::Type GetStencilOperationOnZPass() const;
347 * Called to render during RenderManager::Render().
348 * @param[in] context The context used for rendering
349 * @param[in] textureCache The texture cache used to get textures
350 * @param[in] bufferIndex The index of the previous update buffer.
351 * @param[in] node The node using this renderer
352 * @param[in] defaultShader in case there is no custom shader
353 * @param[in] modelViewMatrix The model-view matrix.
354 * @param[in] viewMatrix The view matrix.
355 * @param[in] projectionMatrix The projection matrix.
357 void Render( Context& context,
358 SceneGraph::TextureCache& textureCache,
359 BufferIndex bufferIndex,
360 const SceneGraph::NodeDataProvider& node,
361 SceneGraph::Shader& defaultShader,
362 const Matrix& modelMatrix,
363 const Matrix& modelViewMatrix,
364 const Matrix& viewMatrix,
365 const Matrix& projectionMatrix,
370 * Write the renderer's sort attributes to the passed in reference
372 * @param[in] bufferIndex The current update buffer index.
373 * @param[out] sortAttributes
375 void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const;
379 struct UniformIndexMap;
382 Renderer( const Renderer& );
385 Renderer& operator=( const Renderer& rhs );
388 * Sets blending options
389 * @param context to use
390 * @param blend Wheter blending should be enabled or not
392 void SetBlending( Context& context, bool blend );
395 * Set the uniforms from properties according to the uniform map
396 * @param[in] bufferIndex The index of the previous update buffer.
397 * @param[in] node The node using the renderer
398 * @param[in] size The size of the renderer
399 * @param[in] program The shader program on which to set the uniforms.
401 void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
404 * Set the program uniform in the map from the mapped property
405 * @param[in] bufferIndex The index of the previous update buffer.
406 * @param[in] program The shader program
407 * @param[in] map The uniform
409 void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
412 * Bind the textures and setup the samplers
413 * @param[in] context The GL context
414 * @param[in] textureCache The texture cache
415 * @param[in] program The shader program
416 * @return False if create or bind failed, true if success.
418 bool BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program );
422 OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
425 SceneGraph::TextureCache* mTextureCache;
426 Render::Geometry* mGeometry;
428 struct UniformIndexMap
430 unsigned int uniformIndex; ///< The index of the cached location in the Program
431 const PropertyInputImpl* propertyValue;
434 typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
435 UniformIndexMappings mUniformIndexMap;
436 Vector<GLint> mAttributesLocation;
438 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
439 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
441 size_t mIndexedDrawFirstElement; ///< Offset of first element to draw
442 size_t mIndexedDrawElementsCount; ///< Number of elements to draw
444 DepthFunction::Type mDepthFunction:3; ///< The depth function
445 FaceCullingMode::Type mFaceCullingMode:2; ///< The mode of face culling
446 BlendMode::Type mBlendMode:2; ///< The mode of blending
447 DepthWriteMode::Type mDepthWriteMode:2; ///< The depth write mode
448 DepthTestMode::Type mDepthTestMode:2; ///< The depth test mode
449 bool mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
450 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
453 } // namespace SceneGraph
455 } // namespace Internal
459 #endif // DALI_INTERNAL_RENDER_RENDERER_H