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( StencilMode::Type stencilMode, 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 stencilFunction ( stencilFunction ),
78 stencilOperationOnFail ( stencilOperationOnFail ),
79 stencilOperationOnZFail ( stencilOperationOnZFail ),
80 stencilOperationOnZPass ( stencilOperationOnZPass ),
81 stencilMode ( stencilMode )
85 int stencilFunctionMask; ///< The stencil function mask
86 int stencilFunctionReference; ///< The stencil function reference
87 int stencilMask; ///< The stencil mask
88 StencilFunction::Type stencilFunction:3; ///< The stencil function
89 StencilOperation::Type stencilOperationOnFail:3; ///< The stencil operation for stencil test fail
90 StencilOperation::Type stencilOperationOnZFail:3; ///< The stencil operation for depth test fail
91 StencilOperation::Type stencilOperationOnZPass:3; ///< The stencil operation for depth test pass
92 StencilMode::Type stencilMode:2; ///< The stencil mode
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
117 * @param[in] writeToColorBuffer Set to True to write to the color buffer
119 static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
120 Render::Geometry* geometry,
121 unsigned int blendingBitmask,
122 const Vector4* blendColor,
123 FaceCullingMode::Type faceCullingMode,
124 bool preMultipliedAlphaEnabled,
125 DepthWriteMode::Type depthWriteMode,
126 DepthTestMode::Type depthTestMode,
127 DepthFunction::Type depthFunction,
128 StencilParameters& stencilParameters,
129 bool writeToColorBuffer );
133 * @param[in] dataProviders The data providers for the renderer
134 * @param[in] geometry The geometry for the renderer
135 * @param[in] blendingBitmask A bitmask of blending options.
136 * @param[in] blendColor The blend color to pass to GL
137 * @param[in] faceCullingMode The face-culling mode.
138 * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
139 * @param[in] depthWriteMode Depth buffer write mode
140 * @param[in] depthTestMode Depth buffer test mode
141 * @param[in] depthFunction Depth function
142 * @param[in] stencilParameters Struct containing all stencil related options
143 * @param[in] writeToColorBuffer Set to True to write to the color buffer
145 Renderer( SceneGraph::RenderDataProvider* dataProviders,
146 Render::Geometry* geometry,
147 unsigned int blendingBitmask,
148 const Vector4* blendColor,
149 FaceCullingMode::Type faceCullingMode,
150 bool preMultipliedAlphaEnabled,
151 DepthWriteMode::Type depthWriteMode,
152 DepthTestMode::Type depthTestMode,
153 DepthFunction::Type depthFunction,
154 StencilParameters& stencilParameters,
155 bool writeToColorBuffer );
158 * Change the data providers of the renderer
159 * @param[in] dataProviders The data providers
161 void SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProviders );
164 * Change the geometry used by the renderer
165 * @param[in] geometry The new geometry
167 void SetGeometry( Render::Geometry* geometry );
169 * Second-phase construction.
170 * This is called when the renderer is inside render thread
171 * @param[in] context Context used by the renderer
172 * @param[in] textureCache The texture cache to use
174 void Initialize( Context& context, SceneGraph::TextureCache& textureCache );
182 * Set the face-culling mode.
183 * @param[in] mode The face-culling mode.
185 void SetFaceCullingMode( FaceCullingMode::Type mode );
188 * Set the bitmask for blending options
189 * @param[in] bitmask A bitmask of blending options.
191 void SetBlendingBitMask( unsigned int bitmask );
194 * Set the blend color for blending options
195 * @param[in] blendColor The blend color to pass to GL
197 void SetBlendColor( const Vector4* color );
200 * Set the first element index to draw by the indexed draw
201 * @param[in] firstElement index of first element to draw
203 void SetIndexedDrawFirstElement( size_t firstElement );
206 * Set the number of elements to draw by the indexed draw
207 * @param[in] elementsCount number of elements to draw
209 void SetIndexedDrawElementsCount( size_t elementsCount );
212 * @brief Set whether the Pre-multiplied Alpha Blending is required
214 * @param[in] preMultipled whether alpha is pre-multiplied.
216 void EnablePreMultipliedAlpha( bool preMultipled );
219 * Sets the depth write mode
220 * @param[in] depthWriteMode The depth write mode
222 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
225 * Query the Renderer's depth write mode
226 * @return The renderer depth write mode
228 DepthWriteMode::Type GetDepthWriteMode() const;
231 * Sets the depth test mode
232 * @param[in] depthTestMode The depth test mode
234 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
237 * Query the Renderer's depth test mode
238 * @return The renderer depth test mode
240 DepthTestMode::Type GetDepthTestMode() const;
243 * Sets the depth function
244 * @param[in] depthFunction The depth function
246 void SetDepthFunction( DepthFunction::Type depthFunction );
249 * Query the Renderer's depth function
250 * @return The renderer depth function
252 DepthFunction::Type GetDepthFunction() const;
255 * Sets the stencil mode
256 * @param[in] stencilMode The stencil function
258 void SetStencilMode( StencilMode::Type stencilMode );
261 * Gets the stencil mode
262 * @return The stencil function
264 StencilMode::Type GetStencilMode() const;
267 * Sets the stencil function
268 * @param[in] stencilFunction The stencil function
270 void SetStencilFunction( StencilFunction::Type stencilFunction );
273 * Gets the stencil function
274 * @return The stencil function
276 StencilFunction::Type GetStencilFunction() const;
279 * Sets the stencil function mask
280 * @param[in] stencilFunctionMask The stencil function mask
282 void SetStencilFunctionMask( int stencilFunctionMask );
285 * Gets the stencil function mask
286 * @return The stencil function mask
288 int GetStencilFunctionMask() const;
291 * Sets the stencil function reference
292 * @param[in] stencilFunctionReference The stencil function reference
294 void SetStencilFunctionReference( int stencilFunctionReference );
297 * Gets the stencil function reference
298 * @return The stencil function reference
300 int GetStencilFunctionReference() const;
303 * Sets the stencil mask
304 * @param[in] stencilMask The stencil mask
306 void SetStencilMask( int stencilMask );
309 * Gets the stencil mask
310 * @return The stencil mask
312 int GetStencilMask() const;
315 * Sets the stencil operation for when the stencil test fails
316 * @param[in] stencilOperationOnFail The stencil operation
318 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
321 * Gets the stencil operation for when the stencil test fails
322 * @return The stencil operation
324 StencilOperation::Type GetStencilOperationOnFail() const;
327 * Sets the stencil operation for when the depth test fails
328 * @param[in] stencilOperationOnZFail The stencil operation
330 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
333 * Gets the stencil operation for when the depth test fails
334 * @return The stencil operation
336 StencilOperation::Type GetStencilOperationOnZFail() const;
339 * Sets the stencil operation for when the depth test passes
340 * @param[in] stencilOperationOnZPass The stencil operation
342 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
345 * Gets the stencil operation for when the depth test passes
346 * @return The stencil operation
348 StencilOperation::Type GetStencilOperationOnZPass() const;
351 * Sets whether or not to write to the color buffer
352 * @param[in] writeToColorBuffer True to write to the color buffer
354 void SetWriteToColorBuffer( bool writeToColorBuffer );
357 * Gets whether or not to write to the color buffer
358 * @return True to write to the color buffer
360 bool GetWriteToColorBuffer() const;
363 * Called to render during RenderManager::Render().
364 * @param[in] context The context used for rendering
365 * @param[in] textureCache The texture cache used to get textures
366 * @param[in] bufferIndex The index of the previous update buffer.
367 * @param[in] node The node using this renderer
368 * @param[in] defaultShader in case there is no custom shader
369 * @param[in] modelViewMatrix The model-view matrix.
370 * @param[in] viewMatrix The view matrix.
371 * @param[in] projectionMatrix The projection matrix.
373 void Render( Context& context,
374 SceneGraph::TextureCache& textureCache,
375 BufferIndex bufferIndex,
376 const SceneGraph::NodeDataProvider& node,
377 SceneGraph::Shader& defaultShader,
378 const Matrix& modelMatrix,
379 const Matrix& modelViewMatrix,
380 const Matrix& viewMatrix,
381 const Matrix& projectionMatrix,
386 * Write the renderer's sort attributes to the passed in reference
388 * @param[in] bufferIndex The current update buffer index.
389 * @param[out] sortAttributes
391 void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const;
395 struct UniformIndexMap;
398 Renderer( const Renderer& );
401 Renderer& operator=( const Renderer& rhs );
404 * Sets blending options
405 * @param context to use
406 * @param blend Wheter blending should be enabled or not
408 void SetBlending( Context& context, bool blend );
411 * Set the uniforms from properties according to the uniform map
412 * @param[in] bufferIndex The index of the previous update buffer.
413 * @param[in] node The node using the renderer
414 * @param[in] size The size of the renderer
415 * @param[in] program The shader program on which to set the uniforms.
417 void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
420 * Set the program uniform in the map from the mapped property
421 * @param[in] bufferIndex The index of the previous update buffer.
422 * @param[in] program The shader program
423 * @param[in] map The uniform
425 void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
428 * Bind the textures and setup the samplers
429 * @param[in] context The GL context
430 * @param[in] textureCache The texture cache
431 * @param[in] program The shader program
432 * @return False if create or bind failed, true if success.
434 bool BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program );
438 OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
441 SceneGraph::TextureCache* mTextureCache;
442 Render::Geometry* mGeometry;
444 struct UniformIndexMap
446 unsigned int uniformIndex; ///< The index of the cached location in the Program
447 const PropertyInputImpl* propertyValue;
450 typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
451 UniformIndexMappings mUniformIndexMap;
452 Vector<GLint> mAttributesLocation;
454 StencilParameters mStencilParameters; ///< Struct containing all stencil related options
455 BlendingOptions mBlendingOptions; ///< Blending options including blend color, blend func and blend equation
457 size_t mIndexedDrawFirstElement; ///< Offset of first element to draw
458 size_t mIndexedDrawElementsCount; ///< Number of elements to draw
460 DepthFunction::Type mDepthFunction:3; ///< The depth function
461 FaceCullingMode::Type mFaceCullingMode:2; ///< The mode of face culling
462 BlendMode::Type mBlendMode:2; ///< The mode of blending
463 DepthWriteMode::Type mDepthWriteMode:2; ///< The depth write mode
464 DepthTestMode::Type mDepthTestMode:2; ///< The depth test mode
465 bool mWriteToColorBuffer:1; ///< True if we are writing to the color buffer
466 bool mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
467 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
470 } // namespace SceneGraph
472 } // namespace Internal
476 #endif // DALI_INTERNAL_RENDER_RENDERER_H