2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/render/renderers/render-renderer.h>
22 #include <dali/graphics-api/graphics-types.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/internal/common/image-sampler.h>
25 #include <dali/internal/common/matrix-utils.h>
26 #include <dali/internal/event/rendering/texture-impl.h>
27 #include <dali/internal/render/common/render-instruction.h>
28 #include <dali/internal/render/data-providers/node-data-provider.h>
29 #include <dali/internal/render/data-providers/uniform-map-data-provider.h>
30 #include <dali/internal/render/renderers/pipeline-cache.h>
31 #include <dali/internal/render/renderers/render-sampler.h>
32 #include <dali/internal/render/renderers/render-texture.h>
33 #include <dali/internal/render/renderers/render-vertex-buffer.h>
34 #include <dali/internal/render/renderers/shader-cache.h>
35 #include <dali/internal/render/renderers/uniform-buffer-view-pool.h>
36 #include <dali/internal/render/renderers/uniform-buffer-view.h>
37 #include <dali/internal/render/shaders/program.h>
38 #include <dali/internal/render/shaders/render-shader.h>
39 #include <dali/internal/update/common/uniform-map.h>
40 #include <dali/public-api/signals/render-callback.h>
42 namespace Dali::Internal
46 // Helper to get the property value getter by type
47 typedef const float& (PropertyInputImpl::*FuncGetter)(BufferIndex) const;
48 constexpr FuncGetter GetPropertyValueGetter(Property::Type type)
52 case Property::BOOLEAN:
54 return FuncGetter(&PropertyInputImpl::GetBoolean);
56 case Property::INTEGER:
58 return FuncGetter(&PropertyInputImpl::GetInteger);
62 return FuncGetter(&PropertyInputImpl::GetFloat);
64 case Property::VECTOR2:
66 return FuncGetter(&PropertyInputImpl::GetVector2);
68 case Property::VECTOR3:
70 return FuncGetter(&PropertyInputImpl::GetVector3);
72 case Property::VECTOR4:
74 return FuncGetter(&PropertyInputImpl::GetVector4);
76 case Property::MATRIX3:
78 return FuncGetter(&PropertyInputImpl::GetMatrix3);
80 case Property::MATRIX:
82 return FuncGetter(&PropertyInputImpl::GetMatrix);
92 * Helper function that returns size of uniform datatypes based
95 constexpr int GetPropertyValueSizeForUniform(Property::Type type)
99 case Property::Type::BOOLEAN:
103 case Property::Type::FLOAT:
105 return sizeof(float);
107 case Property::Type::INTEGER:
111 case Property::Type::VECTOR2:
113 return sizeof(Vector2);
115 case Property::Type::VECTOR3:
117 return sizeof(Vector3);
119 case Property::Type::VECTOR4:
121 return sizeof(Vector4);
123 case Property::Type::MATRIX3:
125 return sizeof(Matrix3);
127 case Property::Type::MATRIX:
129 return sizeof(Matrix);
139 * Helper function to calculate the correct alignment of data for uniform buffers
140 * @param dataSize size of uniform buffer
141 * @return aligned offset of data
143 inline uint32_t GetUniformBufferDataAlignment(uint32_t dataSize)
145 return ((dataSize / 256u) + ((dataSize % 256u) ? 1u : 0u)) * 256u;
152 Renderer* Renderer::New(SceneGraph::RenderDataProvider* dataProvider,
153 Render::Geometry* geometry,
154 uint32_t blendingBitmask,
155 const Vector4& blendColor,
156 FaceCullingMode::Type faceCullingMode,
157 bool preMultipliedAlphaEnabled,
158 DepthWriteMode::Type depthWriteMode,
159 DepthTestMode::Type depthTestMode,
160 DepthFunction::Type depthFunction,
161 StencilParameters& stencilParameters)
163 return new Renderer(dataProvider, geometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode, depthFunction, stencilParameters);
166 Renderer::Renderer(SceneGraph::RenderDataProvider* dataProvider,
167 Render::Geometry* geometry,
168 uint32_t blendingBitmask,
169 const Vector4& blendColor,
170 FaceCullingMode::Type faceCullingMode,
171 bool preMultipliedAlphaEnabled,
172 DepthWriteMode::Type depthWriteMode,
173 DepthTestMode::Type depthTestMode,
174 DepthFunction::Type depthFunction,
175 StencilParameters& stencilParameters)
176 : mGraphicsController(nullptr),
177 mRenderDataProvider(dataProvider),
179 mProgramCache(nullptr),
180 mStencilParameters(stencilParameters),
182 mIndexedDrawFirstElement(0),
183 mIndexedDrawElementsCount(0),
184 mDepthFunction(depthFunction),
185 mFaceCullingMode(faceCullingMode),
186 mDepthWriteMode(depthWriteMode),
187 mDepthTestMode(depthTestMode),
188 mPremultipliedAlphaEnabled(preMultipliedAlphaEnabled),
189 mShaderChanged(false),
192 if(blendingBitmask != 0u)
194 mBlendingOptions.SetBitmask(blendingBitmask);
197 mBlendingOptions.SetBlendColor(blendColor);
200 void Renderer::Initialize(Graphics::Controller& graphicsController, ProgramCache& programCache, Render::ShaderCache& shaderCache, Render::UniformBufferManager& uniformBufferManager, Render::PipelineCache& pipelineCache)
202 mGraphicsController = &graphicsController;
203 mProgramCache = &programCache;
204 mShaderCache = &shaderCache;
205 mUniformBufferManager = &uniformBufferManager;
206 mPipelineCache = &pipelineCache;
209 Renderer::~Renderer() = default;
211 void Renderer::SetGeometry(Render::Geometry* geometry)
213 mGeometry = geometry;
216 void Renderer::SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size)
218 mDrawCommands.clear();
219 mDrawCommands.insert(mDrawCommands.end(), pDrawCommands, pDrawCommands + size);
222 void Renderer::BindTextures(Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures)
224 uint32_t textureUnit = 0;
226 const Dali::Vector<Render::Texture*>* textures(mRenderDataProvider->GetTextures());
227 const Dali::Vector<Render::Sampler*>* samplers(mRenderDataProvider->GetSamplers());
229 std::vector<Graphics::TextureBinding> textureBindings;
231 if(textures != nullptr)
233 const std::uint32_t texturesCount(static_cast<std::uint32_t>(textures->Count()));
234 textureBindings.reserve(texturesCount);
236 for(uint32_t i = 0; i < texturesCount; ++i) // not expecting more than uint32_t of textures
238 if((*textures)[i] && (*textures)[i]->GetGraphicsObject())
240 Graphics::Texture* graphicsTexture = (*textures)[i]->GetGraphicsObject();
241 // if the sampler exists,
242 // if it's default, delete the graphics object
243 // otherwise re-initialize it if dirty
245 const Graphics::Sampler* graphicsSampler = samplers ? ((*samplers)[i] ? (*samplers)[i]->GetGraphicsObject()
249 boundTextures.PushBack(graphicsTexture);
250 const Graphics::TextureBinding textureBinding{graphicsTexture, graphicsSampler, textureUnit};
251 textureBindings.push_back(textureBinding);
258 if(!textureBindings.empty())
260 commandBuffer.BindTextures(textureBindings);
264 void Renderer::SetFaceCullingMode(FaceCullingMode::Type mode)
266 mFaceCullingMode = mode;
270 void Renderer::SetBlendingBitMask(uint32_t bitmask)
272 mBlendingOptions.SetBitmask(bitmask);
276 void Renderer::SetBlendColor(const Vector4& color)
278 mBlendingOptions.SetBlendColor(color);
282 void Renderer::SetIndexedDrawFirstElement(uint32_t firstElement)
284 mIndexedDrawFirstElement = firstElement;
288 void Renderer::SetIndexedDrawElementsCount(uint32_t elementsCount)
290 mIndexedDrawElementsCount = elementsCount;
294 void Renderer::EnablePreMultipliedAlpha(bool enable)
296 mPremultipliedAlphaEnabled = enable;
300 void Renderer::SetDepthWriteMode(DepthWriteMode::Type depthWriteMode)
302 mDepthWriteMode = depthWriteMode;
306 void Renderer::SetDepthTestMode(DepthTestMode::Type depthTestMode)
308 mDepthTestMode = depthTestMode;
312 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
314 return mDepthWriteMode;
317 DepthTestMode::Type Renderer::GetDepthTestMode() const
319 return mDepthTestMode;
322 void Renderer::SetDepthFunction(DepthFunction::Type depthFunction)
324 mDepthFunction = depthFunction;
328 DepthFunction::Type Renderer::GetDepthFunction() const
330 return mDepthFunction;
333 void Renderer::SetRenderMode(RenderMode::Type renderMode)
335 mStencilParameters.renderMode = renderMode;
339 RenderMode::Type Renderer::GetRenderMode() const
341 return mStencilParameters.renderMode;
344 void Renderer::SetStencilFunction(StencilFunction::Type stencilFunction)
346 mStencilParameters.stencilFunction = stencilFunction;
350 StencilFunction::Type Renderer::GetStencilFunction() const
352 return mStencilParameters.stencilFunction;
355 void Renderer::SetStencilFunctionMask(int stencilFunctionMask)
357 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
361 int Renderer::GetStencilFunctionMask() const
363 return mStencilParameters.stencilFunctionMask;
366 void Renderer::SetStencilFunctionReference(int stencilFunctionReference)
368 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
372 int Renderer::GetStencilFunctionReference() const
374 return mStencilParameters.stencilFunctionReference;
377 void Renderer::SetStencilMask(int stencilMask)
379 mStencilParameters.stencilMask = stencilMask;
383 int Renderer::GetStencilMask() const
385 return mStencilParameters.stencilMask;
388 void Renderer::SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail)
390 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
394 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
396 return mStencilParameters.stencilOperationOnFail;
399 void Renderer::SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail)
401 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
405 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
407 return mStencilParameters.stencilOperationOnZFail;
410 void Renderer::SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass)
412 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
416 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
418 return mStencilParameters.stencilOperationOnZPass;
421 void Renderer::Upload()
423 mGeometry->Upload(*mGraphicsController);
426 bool Renderer::Render(Graphics::CommandBuffer& commandBuffer,
427 BufferIndex bufferIndex,
428 const SceneGraph::NodeDataProvider& node,
429 const Matrix& modelMatrix,
430 const Matrix& modelViewMatrix,
431 const Matrix& viewMatrix,
432 const Matrix& projectionMatrix,
435 Vector<Graphics::Texture*>& boundTextures,
436 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
439 // Before doing anything test if the call happens in the right queue
440 if(mDrawCommands.empty() && queueIndex > 0)
445 // Check if there is render callback
448 Graphics::DrawNativeInfo info{};
449 info.api = Graphics::DrawNativeAPI::GLES;
450 info.callback = &static_cast<Dali::CallbackBase&>(*mRenderCallback);
451 info.userData = &mRenderCallbackInput;
453 // Set storage for the context to be used
454 info.glesNativeInfo.eglSharedContextStoragePointer = &mRenderCallbackInput.eglContext;
455 info.reserved = nullptr;
457 auto& textureResources = mRenderCallback->GetTextureResources();
459 if(!textureResources.empty())
461 mRenderCallbackTextureBindings.clear();
462 mRenderCallbackInput.textureBindings.resize(textureResources.size());
464 for(auto& texture : textureResources)
466 auto& textureImpl = GetImplementation(texture);
467 auto graphicsTexture = textureImpl.GetRenderObject()->GetGraphicsObject();
469 auto properties = mGraphicsController->GetTextureProperties(*graphicsTexture);
471 mRenderCallbackTextureBindings.emplace_back(graphicsTexture);
472 mRenderCallbackInput.textureBindings[i++] = properties.nativeHandle;
474 info.textureCount = mRenderCallbackTextureBindings.size();
475 info.textureList = mRenderCallbackTextureBindings.data();
478 // pass render callback input
479 mRenderCallbackInput.size = size;
480 mRenderCallbackInput.projection = projectionMatrix;
482 MatrixUtils::Multiply(mRenderCallbackInput.mvp, modelViewMatrix, projectionMatrix);
485 commandBuffer.DrawNative(&info);
490 std::vector<DevelRenderer::DrawCommand*> commands;
491 for(auto& cmd : mDrawCommands)
493 if(cmd.queue == queueIndex)
495 commands.emplace_back(&cmd);
499 // Have commands but nothing to be drawn - abort
500 if(!mDrawCommands.empty() && commands.empty())
506 if(!mDrawCommands.empty())
508 blend = (commands[0]->queue != DevelRenderer::RENDER_QUEUE_OPAQUE) && blend;
512 ShaderDataPtr shaderData = mRenderDataProvider->GetShader().GetShaderData();
514 Program* program = Program::New(*mProgramCache,
516 *mGraphicsController);
519 DALI_LOG_ERROR("Failed to get program for shader at address %p.\n", reinterpret_cast<const void*>(&mRenderDataProvider->GetShader()));
523 // If program doesn't have Gfx program object assigned yet, prepare it.
524 if(!program->GetGraphicsProgramPtr())
526 const std::vector<char>& vertShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER);
527 const std::vector<char>& fragShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER);
528 Dali::Graphics::Shader& vertexShader = mShaderCache->GetShader(
530 Graphics::PipelineStage::VERTEX_SHADER,
531 shaderData->GetSourceMode());
533 Dali::Graphics::Shader& fragmentShader = mShaderCache->GetShader(
535 Graphics::PipelineStage::FRAGMENT_SHADER,
536 shaderData->GetSourceMode());
538 std::vector<Graphics::ShaderState> shaderStates{
539 Graphics::ShaderState()
540 .SetShader(vertexShader)
541 .SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER),
542 Graphics::ShaderState()
543 .SetShader(fragmentShader)
544 .SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER)};
546 auto createInfo = Graphics::ProgramCreateInfo();
547 createInfo.SetShaderState(shaderStates);
548 auto graphicsProgram = mGraphicsController->CreateProgram(createInfo, nullptr);
549 program->SetGraphicsProgram(std::move(graphicsProgram));
552 // Prepare the graphics pipeline. This may either re-use an existing pipeline or create a new one.
553 auto& pipeline = PrepareGraphicsPipeline(*program, instruction, node, blend);
555 commandBuffer.BindPipeline(pipeline);
557 BindTextures(commandBuffer, boundTextures);
559 std::size_t nodeIndex = BuildUniformIndexMap(bufferIndex, node, size, *program);
561 WriteUniformBuffer(bufferIndex, commandBuffer, program, instruction, node, modelMatrix, modelViewMatrix, viewMatrix, projectionMatrix, size, nodeIndex);
563 bool drawn = false; // Draw can fail if there are no vertex buffers or they haven't been uploaded yet
564 // @todo We should detect this case much earlier to prevent unnecessary work
566 if(mDrawCommands.empty())
568 drawn = mGeometry->Draw(*mGraphicsController, commandBuffer, mIndexedDrawFirstElement, mIndexedDrawElementsCount);
572 for(auto& cmd : commands)
574 mGeometry->Draw(*mGraphicsController, commandBuffer, cmd->firstIndex, cmd->elementCount);
582 std::size_t Renderer::BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program)
584 // Check if the map has changed
585 DALI_ASSERT_DEBUG(mRenderDataProvider && "No Uniform map data provider available");
587 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMapDataProvider();
588 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetCollectedUniformMap();
589 const SceneGraph::UniformMap& uniformMapNode = node.GetNodeUniformMap();
593 // Usual case is to only have 1 node, however we do allow multiple nodes to reuse the same
594 // renderer, so we have to cache uniform map per render item (node / renderer pair).
596 // Specially, if node don't have uniformMap, we mark nodePtr as nullptr.
597 // So, all nodes without uniformMap will share same UniformIndexMap, contains only render data providers.
598 const auto nodePtr = uniformMapNode.Count() ? &node : nullptr;
600 const auto nodeChangeCounter = nodePtr ? uniformMapNode.GetChangeCounter() : 0;
601 const auto renderItemMapChangeCounter = uniformMap.GetChangeCounter();
603 auto iter = std::find_if(mNodeIndexMap.begin(), mNodeIndexMap.end(), [nodePtr](RenderItemLookup& element) { return element.node == nodePtr; });
605 std::size_t renderItemMapIndex;
606 if(iter == mNodeIndexMap.end())
608 renderItemMapIndex = mUniformIndexMaps.size();
609 RenderItemLookup renderItemLookup;
610 renderItemLookup.node = nodePtr;
611 renderItemLookup.index = renderItemMapIndex;
612 renderItemLookup.nodeChangeCounter = nodeChangeCounter;
613 renderItemLookup.renderItemMapChangeCounter = renderItemMapChangeCounter;
614 mNodeIndexMap.emplace_back(renderItemLookup);
617 mUniformIndexMaps.resize(mUniformIndexMaps.size() + 1);
621 renderItemMapIndex = iter->index;
623 updateMaps = (nodeChangeCounter != iter->nodeChangeCounter) ||
624 (renderItemMapChangeCounter != iter->renderItemMapChangeCounter) ||
625 (mUniformIndexMaps[renderItemMapIndex].size() == 0);
627 iter->nodeChangeCounter = nodeChangeCounter;
628 iter->renderItemMapChangeCounter = renderItemMapChangeCounter;
631 if(updateMaps || mShaderChanged)
633 // Reset shader pointer
634 mShaderChanged = false;
636 const uint32_t mapCount = uniformMap.Count();
637 const uint32_t mapNodeCount = uniformMapNode.Count();
639 mUniformIndexMaps[renderItemMapIndex].clear(); // Clear contents, but keep memory if we don't change size
640 mUniformIndexMaps[renderItemMapIndex].resize(mapCount + mapNodeCount);
642 // Copy uniform map into mUniformIndexMap
643 uint32_t mapIndex = 0;
644 for(; mapIndex < mapCount; ++mapIndex)
646 mUniformIndexMaps[renderItemMapIndex][mapIndex].propertyValue = uniformMap.mUniformMap[mapIndex].propertyPtr;
647 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformName = uniformMap.mUniformMap[mapIndex].uniformName;
648 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHash = uniformMap.mUniformMap[mapIndex].uniformNameHash;
649 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHashNoArray = uniformMap.mUniformMap[mapIndex].uniformNameHashNoArray;
650 mUniformIndexMaps[renderItemMapIndex][mapIndex].arrayIndex = uniformMap.mUniformMap[mapIndex].arrayIndex;
653 for(uint32_t nodeMapIndex = 0; nodeMapIndex < mapNodeCount; ++nodeMapIndex)
655 auto hash = uniformMapNode[nodeMapIndex].uniformNameHash;
656 auto& name = uniformMapNode[nodeMapIndex].uniformName;
658 for(uint32_t i = 0; i < mapCount; ++i)
660 if(mUniformIndexMaps[renderItemMapIndex][i].uniformNameHash == hash &&
661 mUniformIndexMaps[renderItemMapIndex][i].uniformName == name)
663 mUniformIndexMaps[renderItemMapIndex][i].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
671 mUniformIndexMaps[renderItemMapIndex][mapIndex].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
672 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformName = uniformMapNode[nodeMapIndex].uniformName;
673 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHash = uniformMapNode[nodeMapIndex].uniformNameHash;
674 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHashNoArray = uniformMapNode[nodeMapIndex].uniformNameHashNoArray;
675 mUniformIndexMaps[renderItemMapIndex][mapIndex].arrayIndex = uniformMapNode[nodeMapIndex].arrayIndex;
680 mUniformIndexMaps[renderItemMapIndex].resize(mapIndex);
682 return renderItemMapIndex;
685 void Renderer::WriteUniformBuffer(
686 BufferIndex bufferIndex,
687 Graphics::CommandBuffer& commandBuffer,
689 const SceneGraph::RenderInstruction& instruction,
690 const SceneGraph::NodeDataProvider& node,
691 const Matrix& modelMatrix,
692 const Matrix& modelViewMatrix,
693 const Matrix& viewMatrix,
694 const Matrix& projectionMatrix,
696 std::size_t nodeIndex)
699 uint32_t uboOffset{0u};
701 auto& reflection = mGraphicsController->GetProgramReflection(program->GetGraphicsProgram());
703 uint32_t uniformBlockAllocationBytes = program->GetUniformBlocksMemoryRequirements().totalSizeRequired;
705 // Create uniform buffer view from uniform buffer
706 Graphics::UniquePtr<Render::UniformBufferView> uboView{nullptr};
707 if(uniformBlockAllocationBytes)
709 auto uboPoolView = mUniformBufferManager->GetUniformBufferViewPool(bufferIndex);
710 uboView = uboPoolView->CreateUniformBufferView(uniformBlockAllocationBytes);
713 // update the uniform buffer
714 // pass shared UBO and offset, return new offset for next item to be used
715 // don't process bindings if there are no uniform buffers allocated
718 auto uboCount = reflection.GetUniformBlockCount();
719 mUniformBufferBindings.resize(uboCount);
721 std::vector<Graphics::UniformBufferBinding>* bindings{&mUniformBufferBindings};
723 mUniformBufferBindings[0].buffer = uboView->GetBuffer(&mUniformBufferBindings[0].offset);
725 // Write default uniforms
726 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_MATRIX), *uboView, modelMatrix);
727 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::VIEW_MATRIX), *uboView, viewMatrix);
728 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::PROJECTION_MATRIX), *uboView, projectionMatrix);
729 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_VIEW_MATRIX), *uboView, modelViewMatrix);
731 auto mvpUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::MVP_MATRIX);
732 if(mvpUniformInfo && !mvpUniformInfo->name.empty())
734 Matrix modelViewProjectionMatrix(false);
735 MatrixUtils::Multiply(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
736 WriteDefaultUniform(mvpUniformInfo, *uboView, modelViewProjectionMatrix);
739 auto normalUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::NORMAL_MATRIX);
740 if(normalUniformInfo && !normalUniformInfo->name.empty())
742 Matrix3 normalMatrix(modelViewMatrix);
743 normalMatrix.Invert();
744 normalMatrix.Transpose();
745 WriteDefaultUniform(normalUniformInfo, *uboView, normalMatrix);
748 Vector4 finalColor; ///< Applied renderer's opacity color
749 const Vector4& color = node.GetRenderColor(bufferIndex); ///< Actor's original color
750 if(mPremultipliedAlphaEnabled)
752 const float& alpha = color.a * mRenderDataProvider->GetOpacity(bufferIndex);
753 finalColor = Vector4(color.r * alpha, color.g * alpha, color.b * alpha, alpha);
757 finalColor = Vector4(color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity(bufferIndex));
759 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::COLOR), *uboView, finalColor);
760 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::ACTOR_COLOR), *uboView, color);
762 // Write uniforms from the uniform map
763 FillUniformBuffer(*program, instruction, *uboView, bindings, uboOffset, bufferIndex, nodeIndex);
765 // Write uSize in the end, as it shouldn't be overridable by dynamic properties.
766 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::SIZE), *uboView, size);
768 commandBuffer.BindUniformBuffers(*bindings);
773 bool Renderer::WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo, Render::UniformBufferView& ubo, const T& data)
775 if(uniformInfo && !uniformInfo->name.empty())
777 WriteUniform(ubo, *uniformInfo, data);
784 void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const T& data)
786 WriteUniform(ubo, uniformInfo, &data, sizeof(T));
789 void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const void* data, uint32_t size)
791 ubo.Write(data, size, ubo.GetOffset() + uniformInfo.offset);
794 void Renderer::FillUniformBuffer(Program& program,
795 const SceneGraph::RenderInstruction& instruction,
796 Render::UniformBufferView& ubo,
797 std::vector<Graphics::UniformBufferBinding>*& outBindings,
799 BufferIndex updateBufferIndex,
800 std::size_t nodeIndex)
802 auto& reflection = mGraphicsController->GetProgramReflection(program.GetGraphicsProgram());
803 auto uboCount = reflection.GetUniformBlockCount();
806 uint32_t dataOffset = offset;
807 for(auto i = 0u; i < uboCount; ++i)
809 mUniformBufferBindings[i].dataSize = reflection.GetUniformBlockSize(i);
810 mUniformBufferBindings[i].binding = reflection.GetUniformBlockBinding(i);
812 dataOffset += GetUniformBufferDataAlignment(mUniformBufferBindings[i].dataSize);
813 mUniformBufferBindings[i].buffer = ubo.GetBuffer(&mUniformBufferBindings[i].offset);
815 for(auto iter = mUniformIndexMaps[nodeIndex].begin(),
816 end = mUniformIndexMaps[nodeIndex].end();
820 auto& uniform = *iter;
821 int arrayIndex = uniform.arrayIndex;
823 if(!uniform.uniformFunc)
825 auto uniformInfo = Graphics::UniformInfo{};
826 auto uniformFound = program.GetUniform(uniform.uniformName.GetStringView(),
827 uniform.uniformNameHash,
828 uniform.uniformNameHashNoArray,
831 uniform.uniformOffset = uniformInfo.offset;
832 uniform.uniformLocation = uniformInfo.location;
836 auto dst = ubo.GetOffset() + uniformInfo.offset;
837 const auto typeSize = GetPropertyValueSizeForUniform((*iter).propertyValue->GetType());
838 const auto dest = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
839 const auto func = GetPropertyValueGetter((*iter).propertyValue->GetType());
841 ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
845 uniform.uniformSize = typeSize;
846 uniform.uniformFunc = func;
851 auto dst = ubo.GetOffset() + uniform.uniformOffset;
852 const auto typeSize = uniform.uniformSize;
853 const auto dest = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
854 const auto func = uniform.uniformFunc;
856 ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
862 // write output bindings
863 outBindings = &mUniformBufferBindings;
869 void Renderer::SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const
871 sortAttributes.shader = &(mRenderDataProvider->GetShader());
872 sortAttributes.geometry = mGeometry;
875 void Renderer::SetShaderChanged(bool value)
877 mShaderChanged = value;
880 bool Renderer::Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node)
888 if(mRenderCallback || mShaderChanged || mGeometry->AttributesChanged())
893 auto* textures = mRenderDataProvider->GetTextures();
896 for(auto iter = textures->Begin(), end = textures->End(); iter < end; ++iter)
898 auto texture = *iter;
899 if(texture && texture->IsNativeImage())
906 // Hash the property values. If the values are different, then rendering is required.
907 uint64_t hash = 0xc70f6907UL;
908 const SceneGraph::UniformMap& uniformMapNode = node->GetNodeUniformMap();
909 for(uint32_t i = 0u, count = uniformMapNode.Count(); i < count; ++i)
911 hash = uniformMapNode[i].propertyPtr->Hash(bufferIndex, hash);
914 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMapDataProvider();
915 const SceneGraph::CollectedUniformMap& collectedUniformMap = uniformMapDataProvider.GetCollectedUniformMap();
916 for(uint32_t i = 0u, count = collectedUniformMap.Count(); i < count; ++i)
918 hash = collectedUniformMap.mUniformMap[i].propertyPtr->Hash(bufferIndex, hash);
921 if(mUniformsHash != hash)
923 mUniformsHash = hash;
930 Graphics::Pipeline& Renderer::PrepareGraphicsPipeline(
932 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
933 const SceneGraph::NodeDataProvider& node,
936 if(mGeometry->AttributesChanged())
941 // Prepare query info
942 PipelineCacheQueryInfo queryInfo{};
943 queryInfo.program = &program;
944 queryInfo.renderer = this;
945 queryInfo.geometry = mGeometry;
946 queryInfo.blendingEnabled = blend;
947 queryInfo.blendingOptions = &mBlendingOptions;
948 queryInfo.alphaPremultiplied = mPremultipliedAlphaEnabled;
949 queryInfo.cameraUsingReflection = instruction.GetCamera()->GetReflectionUsed();
951 auto pipelineResult = mPipelineCache->GetPipeline(queryInfo, true);
953 // should be never null?
954 return *pipelineResult.pipeline;
957 void Renderer::SetRenderCallback(RenderCallback* callback)
959 mRenderCallback = callback;
962 } // namespace Render
964 } // namespace Dali::Internal