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/render/common/render-instruction.h>
26 #include <dali/internal/render/data-providers/node-data-provider.h>
27 #include <dali/internal/render/data-providers/uniform-map-data-provider.h>
28 #include <dali/internal/render/renderers/pipeline-cache.h>
29 #include <dali/internal/render/renderers/render-sampler.h>
30 #include <dali/internal/render/renderers/render-texture.h>
31 #include <dali/internal/render/renderers/render-vertex-buffer.h>
32 #include <dali/internal/render/renderers/shader-cache.h>
33 #include <dali/internal/render/renderers/uniform-buffer-view-pool.h>
34 #include <dali/internal/render/renderers/uniform-buffer-view.h>
35 #include <dali/internal/render/shaders/program.h>
36 #include <dali/internal/render/shaders/render-shader.h>
37 #include <dali/internal/update/common/uniform-map.h>
38 #include <dali/public-api/signals/render-callback.h>
40 namespace Dali::Internal
44 // Helper to get the property value getter by type
45 typedef const float& (PropertyInputImpl::*FuncGetter)(BufferIndex) const;
46 constexpr FuncGetter GetPropertyValueGetter(Property::Type type)
50 case Property::BOOLEAN:
52 return FuncGetter(&PropertyInputImpl::GetBoolean);
54 case Property::INTEGER:
56 return FuncGetter(&PropertyInputImpl::GetInteger);
60 return FuncGetter(&PropertyInputImpl::GetFloat);
62 case Property::VECTOR2:
64 return FuncGetter(&PropertyInputImpl::GetVector2);
66 case Property::VECTOR3:
68 return FuncGetter(&PropertyInputImpl::GetVector3);
70 case Property::VECTOR4:
72 return FuncGetter(&PropertyInputImpl::GetVector4);
74 case Property::MATRIX3:
76 return FuncGetter(&PropertyInputImpl::GetMatrix3);
78 case Property::MATRIX:
80 return FuncGetter(&PropertyInputImpl::GetMatrix);
90 * Helper function that returns size of uniform datatypes based
93 constexpr int GetPropertyValueSizeForUniform(Property::Type type)
97 case Property::Type::BOOLEAN:
101 case Property::Type::FLOAT:
103 return sizeof(float);
105 case Property::Type::INTEGER:
109 case Property::Type::VECTOR2:
111 return sizeof(Vector2);
113 case Property::Type::VECTOR3:
115 return sizeof(Vector3);
117 case Property::Type::VECTOR4:
119 return sizeof(Vector4);
121 case Property::Type::MATRIX3:
123 return sizeof(Matrix3);
125 case Property::Type::MATRIX:
127 return sizeof(Matrix);
137 * Helper function to calculate the correct alignment of data for uniform buffers
138 * @param dataSize size of uniform buffer
139 * @return aligned offset of data
141 inline uint32_t GetUniformBufferDataAlignment(uint32_t dataSize)
143 return ((dataSize / 256u) + ((dataSize % 256u) ? 1u : 0u)) * 256u;
150 Renderer* Renderer::New(SceneGraph::RenderDataProvider* dataProvider,
151 Render::Geometry* geometry,
152 uint32_t blendingBitmask,
153 const Vector4& blendColor,
154 FaceCullingMode::Type faceCullingMode,
155 bool preMultipliedAlphaEnabled,
156 DepthWriteMode::Type depthWriteMode,
157 DepthTestMode::Type depthTestMode,
158 DepthFunction::Type depthFunction,
159 StencilParameters& stencilParameters)
161 return new Renderer(dataProvider, geometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode, depthFunction, stencilParameters);
164 Renderer::Renderer(SceneGraph::RenderDataProvider* dataProvider,
165 Render::Geometry* geometry,
166 uint32_t blendingBitmask,
167 const Vector4& blendColor,
168 FaceCullingMode::Type faceCullingMode,
169 bool preMultipliedAlphaEnabled,
170 DepthWriteMode::Type depthWriteMode,
171 DepthTestMode::Type depthTestMode,
172 DepthFunction::Type depthFunction,
173 StencilParameters& stencilParameters)
174 : mGraphicsController(nullptr),
175 mRenderDataProvider(dataProvider),
177 mProgramCache(nullptr),
178 mStencilParameters(stencilParameters),
180 mIndexedDrawFirstElement(0),
181 mIndexedDrawElementsCount(0),
182 mDepthFunction(depthFunction),
183 mFaceCullingMode(faceCullingMode),
184 mDepthWriteMode(depthWriteMode),
185 mDepthTestMode(depthTestMode),
186 mPremultipliedAlphaEnabled(preMultipliedAlphaEnabled),
187 mShaderChanged(false),
190 if(blendingBitmask != 0u)
192 mBlendingOptions.SetBitmask(blendingBitmask);
195 mBlendingOptions.SetBlendColor(blendColor);
198 void Renderer::Initialize(Graphics::Controller& graphicsController, ProgramCache& programCache, Render::ShaderCache& shaderCache, Render::UniformBufferManager& uniformBufferManager, Render::PipelineCache& pipelineCache)
200 mGraphicsController = &graphicsController;
201 mProgramCache = &programCache;
202 mShaderCache = &shaderCache;
203 mUniformBufferManager = &uniformBufferManager;
204 mPipelineCache = &pipelineCache;
207 Renderer::~Renderer() = default;
209 void Renderer::SetGeometry(Render::Geometry* geometry)
211 mGeometry = geometry;
214 void Renderer::SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size)
216 mDrawCommands.clear();
217 mDrawCommands.insert(mDrawCommands.end(), pDrawCommands, pDrawCommands + size);
220 void Renderer::BindTextures(Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures)
222 uint32_t textureUnit = 0;
224 const Dali::Vector<Render::Texture*>* textures(mRenderDataProvider->GetTextures());
225 const Dali::Vector<Render::Sampler*>* samplers(mRenderDataProvider->GetSamplers());
227 std::vector<Graphics::TextureBinding> textureBindings;
229 if(textures != nullptr)
231 const std::uint32_t texturesCount(static_cast<std::uint32_t>(textures->Count()));
232 textureBindings.reserve(texturesCount);
234 for(uint32_t i = 0; i < texturesCount; ++i) // not expecting more than uint32_t of textures
236 if((*textures)[i] && (*textures)[i]->GetGraphicsObject())
238 Graphics::Texture* graphicsTexture = (*textures)[i]->GetGraphicsObject();
239 // if the sampler exists,
240 // if it's default, delete the graphics object
241 // otherwise re-initialize it if dirty
243 const Graphics::Sampler* graphicsSampler = samplers ? ((*samplers)[i] ? (*samplers)[i]->GetGraphicsObject()
247 boundTextures.PushBack(graphicsTexture);
248 const Graphics::TextureBinding textureBinding{graphicsTexture, graphicsSampler, textureUnit};
249 textureBindings.push_back(textureBinding);
256 if(!textureBindings.empty())
258 commandBuffer.BindTextures(textureBindings);
262 void Renderer::SetFaceCullingMode(FaceCullingMode::Type mode)
264 mFaceCullingMode = mode;
268 void Renderer::SetBlendingBitMask(uint32_t bitmask)
270 mBlendingOptions.SetBitmask(bitmask);
274 void Renderer::SetBlendColor(const Vector4& color)
276 mBlendingOptions.SetBlendColor(color);
280 void Renderer::SetIndexedDrawFirstElement(uint32_t firstElement)
282 mIndexedDrawFirstElement = firstElement;
286 void Renderer::SetIndexedDrawElementsCount(uint32_t elementsCount)
288 mIndexedDrawElementsCount = elementsCount;
292 void Renderer::EnablePreMultipliedAlpha(bool enable)
294 mPremultipliedAlphaEnabled = enable;
298 void Renderer::SetDepthWriteMode(DepthWriteMode::Type depthWriteMode)
300 mDepthWriteMode = depthWriteMode;
304 void Renderer::SetDepthTestMode(DepthTestMode::Type depthTestMode)
306 mDepthTestMode = depthTestMode;
310 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
312 return mDepthWriteMode;
315 DepthTestMode::Type Renderer::GetDepthTestMode() const
317 return mDepthTestMode;
320 void Renderer::SetDepthFunction(DepthFunction::Type depthFunction)
322 mDepthFunction = depthFunction;
326 DepthFunction::Type Renderer::GetDepthFunction() const
328 return mDepthFunction;
331 void Renderer::SetRenderMode(RenderMode::Type renderMode)
333 mStencilParameters.renderMode = renderMode;
337 RenderMode::Type Renderer::GetRenderMode() const
339 return mStencilParameters.renderMode;
342 void Renderer::SetStencilFunction(StencilFunction::Type stencilFunction)
344 mStencilParameters.stencilFunction = stencilFunction;
348 StencilFunction::Type Renderer::GetStencilFunction() const
350 return mStencilParameters.stencilFunction;
353 void Renderer::SetStencilFunctionMask(int stencilFunctionMask)
355 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
359 int Renderer::GetStencilFunctionMask() const
361 return mStencilParameters.stencilFunctionMask;
364 void Renderer::SetStencilFunctionReference(int stencilFunctionReference)
366 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
370 int Renderer::GetStencilFunctionReference() const
372 return mStencilParameters.stencilFunctionReference;
375 void Renderer::SetStencilMask(int stencilMask)
377 mStencilParameters.stencilMask = stencilMask;
381 int Renderer::GetStencilMask() const
383 return mStencilParameters.stencilMask;
386 void Renderer::SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail)
388 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
392 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
394 return mStencilParameters.stencilOperationOnFail;
397 void Renderer::SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail)
399 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
403 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
405 return mStencilParameters.stencilOperationOnZFail;
408 void Renderer::SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass)
410 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
414 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
416 return mStencilParameters.stencilOperationOnZPass;
419 void Renderer::Upload()
421 mGeometry->Upload(*mGraphicsController);
424 bool Renderer::Render(Graphics::CommandBuffer& commandBuffer,
425 BufferIndex bufferIndex,
426 const SceneGraph::NodeDataProvider& node,
427 const Matrix& modelMatrix,
428 const Matrix& modelViewMatrix,
429 const Matrix& viewMatrix,
430 const Matrix& projectionMatrix,
433 Vector<Graphics::Texture*>& boundTextures,
434 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
437 // Before doing anything test if the call happens in the right queue
438 if(mDrawCommands.empty() && queueIndex > 0)
443 // Check if there is render callback
446 Graphics::DrawNativeInfo info{};
447 info.api = Graphics::DrawNativeAPI::GLES;
448 info.callback = &static_cast<Dali::CallbackBase&>(*mRenderCallback);
449 info.userData = &mRenderCallbackInput;
450 info.reserved = nullptr;
452 // pass render callback input
453 mRenderCallbackInput.size = size;
454 mRenderCallbackInput.projection = projectionMatrix;
455 Matrix::Multiply(mRenderCallbackInput.mvp, modelViewMatrix, projectionMatrix);
458 commandBuffer.DrawNative(&info);
463 std::vector<DevelRenderer::DrawCommand*> commands;
464 for(auto& cmd : mDrawCommands)
466 if(cmd.queue == queueIndex)
468 commands.emplace_back(&cmd);
472 // Have commands but nothing to be drawn - abort
473 if(!mDrawCommands.empty() && commands.empty())
479 if(!mDrawCommands.empty())
481 blend = (commands[0]->queue != DevelRenderer::RENDER_QUEUE_OPAQUE) && blend;
485 ShaderDataPtr shaderData = mRenderDataProvider->GetShader().GetShaderData();
487 Program* program = Program::New(*mProgramCache,
489 *mGraphicsController);
492 DALI_LOG_ERROR("Failed to get program for shader at address %p.\n", reinterpret_cast<const void*>(&mRenderDataProvider->GetShader()));
496 // If program doesn't have Gfx program object assigned yet, prepare it.
497 if(!program->GetGraphicsProgramPtr())
499 const std::vector<char>& vertShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER);
500 const std::vector<char>& fragShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER);
501 Dali::Graphics::Shader& vertexShader = mShaderCache->GetShader(
503 Graphics::PipelineStage::VERTEX_SHADER,
504 shaderData->GetSourceMode());
506 Dali::Graphics::Shader& fragmentShader = mShaderCache->GetShader(
508 Graphics::PipelineStage::FRAGMENT_SHADER,
509 shaderData->GetSourceMode());
511 std::vector<Graphics::ShaderState> shaderStates{
512 Graphics::ShaderState()
513 .SetShader(vertexShader)
514 .SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER),
515 Graphics::ShaderState()
516 .SetShader(fragmentShader)
517 .SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER)};
519 auto createInfo = Graphics::ProgramCreateInfo();
520 createInfo.SetShaderState(shaderStates);
521 auto graphicsProgram = mGraphicsController->CreateProgram(createInfo, nullptr);
522 program->SetGraphicsProgram(std::move(graphicsProgram));
525 // Prepare the graphics pipeline. This may either re-use an existing pipeline or create a new one.
526 auto& pipeline = PrepareGraphicsPipeline(*program, instruction, node, blend);
528 commandBuffer.BindPipeline(pipeline);
530 BindTextures(commandBuffer, boundTextures);
532 int nodeIndex = BuildUniformIndexMap(bufferIndex, node, size, *program);
534 WriteUniformBuffer(bufferIndex, commandBuffer, program, instruction, node, modelMatrix, modelViewMatrix, viewMatrix, projectionMatrix, size, nodeIndex);
536 bool drawn = false; // Draw can fail if there are no vertex buffers or they haven't been uploaded yet
537 // @todo We should detect this case much earlier to prevent unnecessary work
539 if(mDrawCommands.empty())
541 drawn = mGeometry->Draw(*mGraphicsController, commandBuffer, mIndexedDrawFirstElement, mIndexedDrawElementsCount);
545 for(auto& cmd : commands)
547 mGeometry->Draw(*mGraphicsController, commandBuffer, cmd->firstIndex, cmd->elementCount);
555 int Renderer::BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program)
557 // Check if the map has changed
558 DALI_ASSERT_DEBUG(mRenderDataProvider && "No Uniform map data provider available");
560 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMapDataProvider();
561 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetCollectedUniformMap();
562 const SceneGraph::UniformMap& uniformMapNode = node.GetNodeUniformMap();
566 // Usual case is to only have 1 node, however we do allow multiple nodes to reuse the same
567 // renderer, so we have to cache uniform map per render item (node / renderer pair).
569 // Specially, if node don't have uniformMap, we mark nodePtr as nullptr.
570 // So, all nodes without uniformMap will share same UniformIndexMap, contains only render data providers.
571 const auto nodePtr = uniformMapNode.Count() ? &node : nullptr;
573 const auto nodeChangeCounter = nodePtr ? uniformMapNode.GetChangeCounter() : 0;
574 const auto renderItemMapChangeCounter = uniformMap.GetChangeCounter();
576 auto iter = std::find_if(mNodeIndexMap.begin(), mNodeIndexMap.end(), [nodePtr](RenderItemLookup& element) { return element.node == nodePtr; });
578 int renderItemMapIndex;
579 if(iter == mNodeIndexMap.end())
581 renderItemMapIndex = mUniformIndexMaps.size();
582 RenderItemLookup renderItemLookup;
583 renderItemLookup.node = nodePtr;
584 renderItemLookup.index = renderItemMapIndex;
585 renderItemLookup.nodeChangeCounter = nodeChangeCounter;
586 renderItemLookup.renderItemMapChangeCounter = renderItemMapChangeCounter;
587 mNodeIndexMap.emplace_back(renderItemLookup);
590 mUniformIndexMaps.resize(mUniformIndexMaps.size() + 1);
594 renderItemMapIndex = iter->index;
596 updateMaps = (nodeChangeCounter != iter->nodeChangeCounter) ||
597 (renderItemMapChangeCounter != iter->renderItemMapChangeCounter) ||
598 (mUniformIndexMaps[renderItemMapIndex].size() == 0);
600 iter->nodeChangeCounter = nodeChangeCounter;
601 iter->renderItemMapChangeCounter = renderItemMapChangeCounter;
604 if(updateMaps || mShaderChanged)
606 // Reset shader pointer
607 mShaderChanged = false;
609 const uint32_t mapCount = uniformMap.Count();
610 const uint32_t mapNodeCount = uniformMapNode.Count();
612 mUniformIndexMaps[renderItemMapIndex].clear(); // Clear contents, but keep memory if we don't change size
613 mUniformIndexMaps[renderItemMapIndex].resize(mapCount + mapNodeCount);
615 // Copy uniform map into mUniformIndexMap
616 uint32_t mapIndex = 0;
617 for(; mapIndex < mapCount; ++mapIndex)
619 mUniformIndexMaps[renderItemMapIndex][mapIndex].propertyValue = uniformMap.mUniformMap[mapIndex].propertyPtr;
620 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformName = uniformMap.mUniformMap[mapIndex].uniformName;
621 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHash = uniformMap.mUniformMap[mapIndex].uniformNameHash;
622 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHashNoArray = uniformMap.mUniformMap[mapIndex].uniformNameHashNoArray;
623 mUniformIndexMaps[renderItemMapIndex][mapIndex].arrayIndex = uniformMap.mUniformMap[mapIndex].arrayIndex;
626 for(uint32_t nodeMapIndex = 0; nodeMapIndex < mapNodeCount; ++nodeMapIndex)
628 auto hash = uniformMapNode[nodeMapIndex].uniformNameHash;
629 auto& name = uniformMapNode[nodeMapIndex].uniformName;
631 for(uint32_t i = 0; i < mapCount; ++i)
633 if(mUniformIndexMaps[renderItemMapIndex][i].uniformNameHash == hash &&
634 mUniformIndexMaps[renderItemMapIndex][i].uniformName == name)
636 mUniformIndexMaps[renderItemMapIndex][i].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
644 mUniformIndexMaps[renderItemMapIndex][mapIndex].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
645 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformName = uniformMapNode[nodeMapIndex].uniformName;
646 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHash = uniformMapNode[nodeMapIndex].uniformNameHash;
647 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHashNoArray = uniformMapNode[nodeMapIndex].uniformNameHashNoArray;
648 mUniformIndexMaps[renderItemMapIndex][mapIndex].arrayIndex = uniformMapNode[nodeMapIndex].arrayIndex;
653 mUniformIndexMaps[renderItemMapIndex].resize(mapIndex);
655 return renderItemMapIndex;
658 void Renderer::WriteUniformBuffer(
659 BufferIndex bufferIndex,
660 Graphics::CommandBuffer& commandBuffer,
662 const SceneGraph::RenderInstruction& instruction,
663 const SceneGraph::NodeDataProvider& node,
664 const Matrix& modelMatrix,
665 const Matrix& modelViewMatrix,
666 const Matrix& viewMatrix,
667 const Matrix& projectionMatrix,
672 uint32_t uboOffset{0u};
674 auto& reflection = mGraphicsController->GetProgramReflection(program->GetGraphicsProgram());
676 uint32_t uniformBlockAllocationBytes = program->GetUniformBlocksMemoryRequirements().totalSizeRequired;
678 // Create uniform buffer view from uniform buffer
679 Graphics::UniquePtr<Render::UniformBufferView> uboView{nullptr};
680 if(uniformBlockAllocationBytes)
682 auto uboPoolView = mUniformBufferManager->GetUniformBufferViewPool(bufferIndex);
683 uboView = uboPoolView->CreateUniformBufferView(uniformBlockAllocationBytes);
686 // update the uniform buffer
687 // pass shared UBO and offset, return new offset for next item to be used
688 // don't process bindings if there are no uniform buffers allocated
691 auto uboCount = reflection.GetUniformBlockCount();
692 mUniformBufferBindings.resize(uboCount);
694 std::vector<Graphics::UniformBufferBinding>* bindings{&mUniformBufferBindings};
696 mUniformBufferBindings[0].buffer = uboView->GetBuffer(&mUniformBufferBindings[0].offset);
698 // Write default uniforms
699 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_MATRIX), *uboView, modelMatrix);
700 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::VIEW_MATRIX), *uboView, viewMatrix);
701 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::PROJECTION_MATRIX), *uboView, projectionMatrix);
702 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_VIEW_MATRIX), *uboView, modelViewMatrix);
704 auto mvpUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::MVP_MATRIX);
705 if(mvpUniformInfo && !mvpUniformInfo->name.empty())
707 Matrix modelViewProjectionMatrix(false);
708 Matrix::Multiply(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
709 WriteDefaultUniform(mvpUniformInfo, *uboView, modelViewProjectionMatrix);
712 auto normalUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::NORMAL_MATRIX);
713 if(normalUniformInfo && !normalUniformInfo->name.empty())
715 Matrix3 normalMatrix(modelViewMatrix);
716 normalMatrix.Invert();
717 normalMatrix.Transpose();
718 WriteDefaultUniform(normalUniformInfo, *uboView, normalMatrix);
721 Vector4 finalColor; ///< Applied renderer's opacity color
722 const Vector4& color = node.GetRenderColor(bufferIndex); ///< Actor's original color
723 if(mPremultipliedAlphaEnabled)
725 const float& alpha = color.a * mRenderDataProvider->GetOpacity(bufferIndex);
726 finalColor = Vector4(color.r * alpha, color.g * alpha, color.b * alpha, alpha);
730 finalColor = Vector4(color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity(bufferIndex));
732 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::COLOR), *uboView, finalColor);
733 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::ACTOR_COLOR), *uboView, color);
735 // Write uniforms from the uniform map
736 FillUniformBuffer(*program, instruction, *uboView, bindings, uboOffset, bufferIndex, nodeIndex);
738 // Write uSize in the end, as it shouldn't be overridable by dynamic properties.
739 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::SIZE), *uboView, size);
741 commandBuffer.BindUniformBuffers(*bindings);
746 bool Renderer::WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo, Render::UniformBufferView& ubo, const T& data)
748 if(uniformInfo && !uniformInfo->name.empty())
750 WriteUniform(ubo, *uniformInfo, data);
757 void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const T& data)
759 WriteUniform(ubo, uniformInfo, &data, sizeof(T));
762 void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const void* data, uint32_t size)
764 ubo.Write(data, size, ubo.GetOffset() + uniformInfo.offset);
767 void Renderer::FillUniformBuffer(Program& program,
768 const SceneGraph::RenderInstruction& instruction,
769 Render::UniformBufferView& ubo,
770 std::vector<Graphics::UniformBufferBinding>*& outBindings,
772 BufferIndex updateBufferIndex,
775 auto& reflection = mGraphicsController->GetProgramReflection(program.GetGraphicsProgram());
776 auto uboCount = reflection.GetUniformBlockCount();
779 uint32_t dataOffset = offset;
780 for(auto i = 0u; i < uboCount; ++i)
782 mUniformBufferBindings[i].dataSize = reflection.GetUniformBlockSize(i);
783 mUniformBufferBindings[i].binding = reflection.GetUniformBlockBinding(i);
785 dataOffset += GetUniformBufferDataAlignment(mUniformBufferBindings[i].dataSize);
786 mUniformBufferBindings[i].buffer = ubo.GetBuffer(&mUniformBufferBindings[i].offset);
788 for(auto iter = mUniformIndexMaps[nodeIndex].begin(),
789 end = mUniformIndexMaps[nodeIndex].end();
793 auto& uniform = *iter;
794 int arrayIndex = uniform.arrayIndex;
796 if(!uniform.uniformFunc)
798 auto uniformInfo = Graphics::UniformInfo{};
799 auto uniformFound = program.GetUniform(uniform.uniformName.GetStringView(),
800 uniform.uniformNameHash,
801 uniform.uniformNameHashNoArray,
804 uniform.uniformOffset = uniformInfo.offset;
805 uniform.uniformLocation = uniformInfo.location;
809 auto dst = ubo.GetOffset() + uniformInfo.offset;
810 const auto typeSize = GetPropertyValueSizeForUniform((*iter).propertyValue->GetType());
811 const auto dest = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
812 const auto func = GetPropertyValueGetter((*iter).propertyValue->GetType());
814 ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
818 uniform.uniformSize = typeSize;
819 uniform.uniformFunc = func;
824 auto dst = ubo.GetOffset() + uniform.uniformOffset;
825 const auto typeSize = uniform.uniformSize;
826 const auto dest = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
827 const auto func = uniform.uniformFunc;
829 ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
835 // write output bindings
836 outBindings = &mUniformBufferBindings;
842 void Renderer::SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const
844 sortAttributes.shader = &(mRenderDataProvider->GetShader());
845 sortAttributes.geometry = mGeometry;
848 void Renderer::SetShaderChanged(bool value)
850 mShaderChanged = value;
853 bool Renderer::Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node)
861 if(mRenderCallback || mShaderChanged || mGeometry->AttributesChanged())
866 auto* textures = mRenderDataProvider->GetTextures();
869 for(auto iter = textures->Begin(), end = textures->End(); iter < end; ++iter)
871 auto texture = *iter;
872 if(texture && texture->IsNativeImage())
879 // Hash the property values. If the values are different, then rendering is required.
880 uint64_t hash = 0xc70f6907UL;
881 const SceneGraph::UniformMap& uniformMapNode = node->GetNodeUniformMap();
882 for(uint32_t i = 0u, count = uniformMapNode.Count(); i < count; ++i)
884 hash = uniformMapNode[i].propertyPtr->Hash(bufferIndex, hash);
887 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMapDataProvider();
888 const SceneGraph::CollectedUniformMap& collectedUniformMap = uniformMapDataProvider.GetCollectedUniformMap();
889 for(uint32_t i = 0u, count = collectedUniformMap.Count(); i < count; ++i)
891 hash = collectedUniformMap.mUniformMap[i].propertyPtr->Hash(bufferIndex, hash);
894 if(mUniformsHash != hash)
896 mUniformsHash = hash;
903 Graphics::Pipeline& Renderer::PrepareGraphicsPipeline(
905 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
906 const SceneGraph::NodeDataProvider& node,
909 if(mGeometry->AttributesChanged())
914 // Prepare query info
915 PipelineCacheQueryInfo queryInfo{};
916 queryInfo.program = &program;
917 queryInfo.renderer = this;
918 queryInfo.geometry = mGeometry;
919 queryInfo.blendingEnabled = blend;
920 queryInfo.blendingOptions = &mBlendingOptions;
921 queryInfo.alphaPremultiplied = mPremultipliedAlphaEnabled;
922 queryInfo.cameraUsingReflection = instruction.GetCamera()->GetReflectionUsed();
924 auto pipelineResult = mPipelineCache->GetPipeline(queryInfo, true);
926 // should be never null?
927 return *pipelineResult.pipeline;
930 void Renderer::SetRenderCallback(RenderCallback* callback)
932 mRenderCallback = callback;
935 } // namespace Render
937 } // namespace Dali::Internal