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)
191 if(blendingBitmask != 0u)
193 mBlendingOptions.SetBitmask(blendingBitmask);
196 mBlendingOptions.SetBlendColor(blendColor);
199 void Renderer::Initialize(Graphics::Controller& graphicsController, ProgramCache& programCache, Render::ShaderCache& shaderCache, Render::UniformBufferManager& uniformBufferManager, Render::PipelineCache& pipelineCache)
201 mGraphicsController = &graphicsController;
202 mProgramCache = &programCache;
203 mShaderCache = &shaderCache;
204 mUniformBufferManager = &uniformBufferManager;
205 mPipelineCache = &pipelineCache;
208 Renderer::~Renderer() = default;
210 void Renderer::SetGeometry(Render::Geometry* geometry)
212 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;
267 void Renderer::SetBlendingBitMask(uint32_t bitmask)
269 mBlendingOptions.SetBitmask(bitmask);
272 void Renderer::SetBlendColor(const Vector4& color)
274 mBlendingOptions.SetBlendColor(color);
277 void Renderer::SetIndexedDrawFirstElement(uint32_t firstElement)
279 mIndexedDrawFirstElement = firstElement;
282 void Renderer::SetIndexedDrawElementsCount(uint32_t elementsCount)
284 mIndexedDrawElementsCount = elementsCount;
287 void Renderer::EnablePreMultipliedAlpha(bool enable)
289 mPremultipliedAlphaEnabled = enable;
292 void Renderer::SetDepthWriteMode(DepthWriteMode::Type depthWriteMode)
294 mDepthWriteMode = depthWriteMode;
297 void Renderer::SetDepthTestMode(DepthTestMode::Type depthTestMode)
299 mDepthTestMode = depthTestMode;
302 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
304 return mDepthWriteMode;
307 DepthTestMode::Type Renderer::GetDepthTestMode() const
309 return mDepthTestMode;
312 void Renderer::SetDepthFunction(DepthFunction::Type depthFunction)
314 mDepthFunction = depthFunction;
317 DepthFunction::Type Renderer::GetDepthFunction() const
319 return mDepthFunction;
322 void Renderer::SetRenderMode(RenderMode::Type renderMode)
324 mStencilParameters.renderMode = renderMode;
327 RenderMode::Type Renderer::GetRenderMode() const
329 return mStencilParameters.renderMode;
332 void Renderer::SetStencilFunction(StencilFunction::Type stencilFunction)
334 mStencilParameters.stencilFunction = stencilFunction;
337 StencilFunction::Type Renderer::GetStencilFunction() const
339 return mStencilParameters.stencilFunction;
342 void Renderer::SetStencilFunctionMask(int stencilFunctionMask)
344 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
347 int Renderer::GetStencilFunctionMask() const
349 return mStencilParameters.stencilFunctionMask;
352 void Renderer::SetStencilFunctionReference(int stencilFunctionReference)
354 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
357 int Renderer::GetStencilFunctionReference() const
359 return mStencilParameters.stencilFunctionReference;
362 void Renderer::SetStencilMask(int stencilMask)
364 mStencilParameters.stencilMask = stencilMask;
367 int Renderer::GetStencilMask() const
369 return mStencilParameters.stencilMask;
372 void Renderer::SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail)
374 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
377 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
379 return mStencilParameters.stencilOperationOnFail;
382 void Renderer::SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail)
384 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
387 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
389 return mStencilParameters.stencilOperationOnZFail;
392 void Renderer::SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass)
394 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
397 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
399 return mStencilParameters.stencilOperationOnZPass;
402 void Renderer::Upload()
404 mGeometry->Upload(*mGraphicsController);
407 bool Renderer::Render(Graphics::CommandBuffer& commandBuffer,
408 BufferIndex bufferIndex,
409 const SceneGraph::NodeDataProvider& node,
410 const Matrix& modelMatrix,
411 const Matrix& modelViewMatrix,
412 const Matrix& viewMatrix,
413 const Matrix& projectionMatrix,
416 Vector<Graphics::Texture*>& boundTextures,
417 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
420 // Before doing anything test if the call happens in the right queue
421 if(mDrawCommands.empty() && queueIndex > 0)
426 // Check if there is render callback
429 Graphics::DrawNativeInfo info{};
430 info.api = Graphics::DrawNativeAPI::GLES;
431 info.callback = &static_cast<Dali::CallbackBase&>(*mRenderCallback);
432 info.userData = &mRenderCallbackInput;
434 // Set storage for the context to be used
435 info.glesNativeInfo.eglSharedContextStoragePointer = &mRenderCallbackInput.eglContext;
436 info.reserved = nullptr;
438 auto& textureResources = mRenderCallback->GetTextureResources();
440 if(!textureResources.empty())
442 mRenderCallbackTextureBindings.clear();
443 mRenderCallbackInput.textureBindings.resize(textureResources.size());
445 for(auto& texture : textureResources)
447 auto& textureImpl = GetImplementation(texture);
448 auto graphicsTexture = textureImpl.GetRenderObject()->GetGraphicsObject();
450 auto properties = mGraphicsController->GetTextureProperties(*graphicsTexture);
452 mRenderCallbackTextureBindings.emplace_back(graphicsTexture);
453 mRenderCallbackInput.textureBindings[i++] = properties.nativeHandle;
455 info.textureCount = mRenderCallbackTextureBindings.size();
456 info.textureList = mRenderCallbackTextureBindings.data();
459 // pass render callback input
460 mRenderCallbackInput.size = size;
461 mRenderCallbackInput.projection = projectionMatrix;
463 MatrixUtils::Multiply(mRenderCallbackInput.mvp, modelViewMatrix, projectionMatrix);
466 commandBuffer.DrawNative(&info);
471 std::vector<DevelRenderer::DrawCommand*> commands;
472 for(auto& cmd : mDrawCommands)
474 if(cmd.queue == queueIndex)
476 commands.emplace_back(&cmd);
480 // Have commands but nothing to be drawn - abort
481 if(!mDrawCommands.empty() && commands.empty())
487 if(!mDrawCommands.empty())
489 blend = (commands[0]->queue != DevelRenderer::RENDER_QUEUE_OPAQUE) && blend;
493 ShaderDataPtr shaderData = mRenderDataProvider->GetShader().GetShaderData();
495 Program* program = Program::New(*mProgramCache,
497 *mGraphicsController);
500 DALI_LOG_ERROR("Failed to get program for shader at address %p.\n", reinterpret_cast<const void*>(&mRenderDataProvider->GetShader()));
504 // If program doesn't have Gfx program object assigned yet, prepare it.
505 if(!program->GetGraphicsProgramPtr())
507 const std::vector<char>& vertShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER);
508 const std::vector<char>& fragShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER);
509 Dali::Graphics::Shader& vertexShader = mShaderCache->GetShader(
511 Graphics::PipelineStage::VERTEX_SHADER,
512 shaderData->GetSourceMode());
514 Dali::Graphics::Shader& fragmentShader = mShaderCache->GetShader(
516 Graphics::PipelineStage::FRAGMENT_SHADER,
517 shaderData->GetSourceMode());
519 std::vector<Graphics::ShaderState> shaderStates{
520 Graphics::ShaderState()
521 .SetShader(vertexShader)
522 .SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER),
523 Graphics::ShaderState()
524 .SetShader(fragmentShader)
525 .SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER)};
527 auto createInfo = Graphics::ProgramCreateInfo();
528 createInfo.SetShaderState(shaderStates);
529 auto graphicsProgram = mGraphicsController->CreateProgram(createInfo, nullptr);
530 program->SetGraphicsProgram(std::move(graphicsProgram));
533 // Prepare the graphics pipeline. This may either re-use an existing pipeline or create a new one.
534 auto& pipeline = PrepareGraphicsPipeline(*program, instruction, node, blend);
536 commandBuffer.BindPipeline(pipeline);
538 BindTextures(commandBuffer, boundTextures);
540 std::size_t nodeIndex = BuildUniformIndexMap(bufferIndex, node, size, *program);
542 WriteUniformBuffer(bufferIndex, commandBuffer, program, instruction, node, modelMatrix, modelViewMatrix, viewMatrix, projectionMatrix, size, nodeIndex);
544 bool drawn = false; // Draw can fail if there are no vertex buffers or they haven't been uploaded yet
545 // @todo We should detect this case much earlier to prevent unnecessary work
547 if(mDrawCommands.empty())
549 drawn = mGeometry->Draw(*mGraphicsController, commandBuffer, mIndexedDrawFirstElement, mIndexedDrawElementsCount);
553 for(auto& cmd : commands)
555 mGeometry->Draw(*mGraphicsController, commandBuffer, cmd->firstIndex, cmd->elementCount);
562 std::size_t Renderer::BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program)
564 // Check if the map has changed
565 DALI_ASSERT_DEBUG(mRenderDataProvider && "No Uniform map data provider available");
567 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMapDataProvider();
568 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetCollectedUniformMap();
569 const SceneGraph::UniformMap& uniformMapNode = node.GetNodeUniformMap();
573 // Usual case is to only have 1 node, however we do allow multiple nodes to reuse the same
574 // renderer, so we have to cache uniform map per render item (node / renderer pair).
576 // Specially, if node don't have uniformMap, we mark nodePtr as nullptr.
577 // So, all nodes without uniformMap will share same UniformIndexMap, contains only render data providers.
578 const auto nodePtr = uniformMapNode.Count() ? &node : nullptr;
580 const auto nodeChangeCounter = nodePtr ? uniformMapNode.GetChangeCounter() : 0;
581 const auto renderItemMapChangeCounter = uniformMap.GetChangeCounter();
583 auto iter = std::find_if(mNodeIndexMap.begin(), mNodeIndexMap.end(), [nodePtr](RenderItemLookup& element) { return element.node == nodePtr; });
585 std::size_t renderItemMapIndex;
586 if(iter == mNodeIndexMap.end())
588 renderItemMapIndex = mUniformIndexMaps.size();
589 RenderItemLookup renderItemLookup;
590 renderItemLookup.node = nodePtr;
591 renderItemLookup.index = renderItemMapIndex;
592 renderItemLookup.nodeChangeCounter = nodeChangeCounter;
593 renderItemLookup.renderItemMapChangeCounter = renderItemMapChangeCounter;
594 mNodeIndexMap.emplace_back(renderItemLookup);
597 mUniformIndexMaps.resize(mUniformIndexMaps.size() + 1);
601 renderItemMapIndex = iter->index;
603 updateMaps = (nodeChangeCounter != iter->nodeChangeCounter) ||
604 (renderItemMapChangeCounter != iter->renderItemMapChangeCounter) ||
605 (mUniformIndexMaps[renderItemMapIndex].size() == 0);
607 iter->nodeChangeCounter = nodeChangeCounter;
608 iter->renderItemMapChangeCounter = renderItemMapChangeCounter;
611 if(updateMaps || mShaderChanged)
613 // Reset shader pointer
614 mShaderChanged = false;
616 const uint32_t mapCount = uniformMap.Count();
617 const uint32_t mapNodeCount = uniformMapNode.Count();
619 mUniformIndexMaps[renderItemMapIndex].clear(); // Clear contents, but keep memory if we don't change size
620 mUniformIndexMaps[renderItemMapIndex].resize(mapCount + mapNodeCount);
622 // Copy uniform map into mUniformIndexMap
623 uint32_t mapIndex = 0;
624 for(; mapIndex < mapCount; ++mapIndex)
626 mUniformIndexMaps[renderItemMapIndex][mapIndex].propertyValue = uniformMap.mUniformMap[mapIndex].propertyPtr;
627 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformName = uniformMap.mUniformMap[mapIndex].uniformName;
628 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHash = uniformMap.mUniformMap[mapIndex].uniformNameHash;
629 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHashNoArray = uniformMap.mUniformMap[mapIndex].uniformNameHashNoArray;
630 mUniformIndexMaps[renderItemMapIndex][mapIndex].arrayIndex = uniformMap.mUniformMap[mapIndex].arrayIndex;
633 for(uint32_t nodeMapIndex = 0; nodeMapIndex < mapNodeCount; ++nodeMapIndex)
635 auto hash = uniformMapNode[nodeMapIndex].uniformNameHash;
636 auto& name = uniformMapNode[nodeMapIndex].uniformName;
638 for(uint32_t i = 0; i < mapCount; ++i)
640 if(mUniformIndexMaps[renderItemMapIndex][i].uniformNameHash == hash &&
641 mUniformIndexMaps[renderItemMapIndex][i].uniformName == name)
643 mUniformIndexMaps[renderItemMapIndex][i].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
651 mUniformIndexMaps[renderItemMapIndex][mapIndex].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
652 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformName = uniformMapNode[nodeMapIndex].uniformName;
653 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHash = uniformMapNode[nodeMapIndex].uniformNameHash;
654 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHashNoArray = uniformMapNode[nodeMapIndex].uniformNameHashNoArray;
655 mUniformIndexMaps[renderItemMapIndex][mapIndex].arrayIndex = uniformMapNode[nodeMapIndex].arrayIndex;
660 mUniformIndexMaps[renderItemMapIndex].resize(mapIndex);
662 return renderItemMapIndex;
665 void Renderer::WriteUniformBuffer(
666 BufferIndex bufferIndex,
667 Graphics::CommandBuffer& commandBuffer,
669 const SceneGraph::RenderInstruction& instruction,
670 const SceneGraph::NodeDataProvider& node,
671 const Matrix& modelMatrix,
672 const Matrix& modelViewMatrix,
673 const Matrix& viewMatrix,
674 const Matrix& projectionMatrix,
676 std::size_t nodeIndex)
679 uint32_t uboOffset{0u};
681 auto& reflection = mGraphicsController->GetProgramReflection(program->GetGraphicsProgram());
683 uint32_t uniformBlockAllocationBytes = program->GetUniformBlocksMemoryRequirements().totalSizeRequired;
685 // Create uniform buffer view from uniform buffer
686 Graphics::UniquePtr<Render::UniformBufferView> uboView{nullptr};
687 if(uniformBlockAllocationBytes)
689 auto uboPoolView = mUniformBufferManager->GetUniformBufferViewPool(bufferIndex);
690 uboView = uboPoolView->CreateUniformBufferView(uniformBlockAllocationBytes);
693 // update the uniform buffer
694 // pass shared UBO and offset, return new offset for next item to be used
695 // don't process bindings if there are no uniform buffers allocated
698 auto uboCount = reflection.GetUniformBlockCount();
699 mUniformBufferBindings.resize(uboCount);
701 std::vector<Graphics::UniformBufferBinding>* bindings{&mUniformBufferBindings};
703 mUniformBufferBindings[0].buffer = uboView->GetBuffer(&mUniformBufferBindings[0].offset);
705 // Write default uniforms
706 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_MATRIX), *uboView, modelMatrix);
707 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::VIEW_MATRIX), *uboView, viewMatrix);
708 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::PROJECTION_MATRIX), *uboView, projectionMatrix);
709 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_VIEW_MATRIX), *uboView, modelViewMatrix);
711 auto mvpUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::MVP_MATRIX);
712 if(mvpUniformInfo && !mvpUniformInfo->name.empty())
714 Matrix modelViewProjectionMatrix(false);
715 MatrixUtils::Multiply(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
716 WriteDefaultUniform(mvpUniformInfo, *uboView, modelViewProjectionMatrix);
719 auto normalUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::NORMAL_MATRIX);
720 if(normalUniformInfo && !normalUniformInfo->name.empty())
722 Matrix3 normalMatrix(modelViewMatrix);
723 normalMatrix.Invert();
724 normalMatrix.Transpose();
725 WriteDefaultUniform(normalUniformInfo, *uboView, normalMatrix);
728 Vector4 finalColor; ///< Applied renderer's opacity color
729 const Vector4& color = node.GetRenderColor(bufferIndex); ///< Actor's original color
730 if(mPremultipliedAlphaEnabled)
732 const float& alpha = color.a * mRenderDataProvider->GetOpacity(bufferIndex);
733 finalColor = Vector4(color.r * alpha, color.g * alpha, color.b * alpha, alpha);
737 finalColor = Vector4(color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity(bufferIndex));
739 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::COLOR), *uboView, finalColor);
740 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::ACTOR_COLOR), *uboView, color);
742 // Write uniforms from the uniform map
743 FillUniformBuffer(*program, instruction, *uboView, bindings, uboOffset, bufferIndex, nodeIndex);
745 // Write uSize in the end, as it shouldn't be overridable by dynamic properties.
746 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::SIZE), *uboView, size);
748 commandBuffer.BindUniformBuffers(*bindings);
753 bool Renderer::WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo, Render::UniformBufferView& ubo, const T& data)
755 if(uniformInfo && !uniformInfo->name.empty())
757 WriteUniform(ubo, *uniformInfo, data);
764 void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const T& data)
766 WriteUniform(ubo, uniformInfo, &data, sizeof(T));
769 void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const void* data, uint32_t size)
771 ubo.Write(data, size, ubo.GetOffset() + uniformInfo.offset);
774 void Renderer::FillUniformBuffer(Program& program,
775 const SceneGraph::RenderInstruction& instruction,
776 Render::UniformBufferView& ubo,
777 std::vector<Graphics::UniformBufferBinding>*& outBindings,
779 BufferIndex updateBufferIndex,
780 std::size_t nodeIndex)
782 auto& reflection = mGraphicsController->GetProgramReflection(program.GetGraphicsProgram());
783 auto uboCount = reflection.GetUniformBlockCount();
786 uint32_t dataOffset = offset;
787 for(auto i = 0u; i < uboCount; ++i)
789 mUniformBufferBindings[i].dataSize = reflection.GetUniformBlockSize(i);
790 mUniformBufferBindings[i].binding = reflection.GetUniformBlockBinding(i);
792 dataOffset += GetUniformBufferDataAlignment(mUniformBufferBindings[i].dataSize);
793 mUniformBufferBindings[i].buffer = ubo.GetBuffer(&mUniformBufferBindings[i].offset);
795 for(auto iter = mUniformIndexMaps[nodeIndex].begin(),
796 end = mUniformIndexMaps[nodeIndex].end();
800 auto& uniform = *iter;
801 int arrayIndex = uniform.arrayIndex;
803 if(!uniform.uniformFunc)
805 auto uniformInfo = Graphics::UniformInfo{};
806 auto uniformFound = program.GetUniform(uniform.uniformName.GetStringView(),
807 uniform.uniformNameHash,
808 uniform.uniformNameHashNoArray,
811 uniform.uniformOffset = uniformInfo.offset;
812 uniform.uniformLocation = uniformInfo.location;
816 auto dst = ubo.GetOffset() + uniformInfo.offset;
817 const auto typeSize = GetPropertyValueSizeForUniform((*iter).propertyValue->GetType());
818 const auto dest = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
819 const auto func = GetPropertyValueGetter((*iter).propertyValue->GetType());
821 ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
825 uniform.uniformSize = typeSize;
826 uniform.uniformFunc = func;
831 auto dst = ubo.GetOffset() + uniform.uniformOffset;
832 const auto typeSize = uniform.uniformSize;
833 const auto dest = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
834 const auto func = uniform.uniformFunc;
836 ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
842 // write output bindings
843 outBindings = &mUniformBufferBindings;
849 void Renderer::SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const
851 sortAttributes.shader = &(mRenderDataProvider->GetShader());
852 sortAttributes.geometry = mGeometry;
855 void Renderer::SetShaderChanged(bool value)
857 mShaderChanged = value;
860 bool Renderer::Updated(BufferIndex bufferIndex)
862 if(mRenderCallback || mShaderChanged || mGeometry->AttributesChanged() || mRenderDataProvider->IsUpdated())
867 auto* textures = mRenderDataProvider->GetTextures();
870 for(auto iter = textures->Begin(), end = textures->End(); iter < end; ++iter)
872 auto texture = *iter;
873 if(texture && texture->Updated())
882 Vector4 Renderer::GetVisualTransformedUpdateArea(BufferIndex bufferIndex, const Vector4& originalUpdateArea) const noexcept
884 return mRenderDataProvider->GetVisualTransformedUpdateArea(bufferIndex, originalUpdateArea);
887 Graphics::Pipeline& Renderer::PrepareGraphicsPipeline(
889 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
890 const SceneGraph::NodeDataProvider& node,
893 // Prepare query info
894 PipelineCacheQueryInfo queryInfo{};
895 queryInfo.program = &program;
896 queryInfo.renderer = this;
897 queryInfo.geometry = mGeometry;
898 queryInfo.blendingEnabled = blend;
899 queryInfo.blendingOptions = &mBlendingOptions;
900 queryInfo.alphaPremultiplied = mPremultipliedAlphaEnabled;
901 queryInfo.cameraUsingReflection = instruction.GetCamera()->GetReflectionUsed();
903 auto pipelineResult = mPipelineCache->GetPipeline(queryInfo, true);
905 // should be never null?
906 return *pipelineResult.pipeline;
909 void Renderer::SetRenderCallback(RenderCallback* callback)
911 mRenderCallback = callback;
914 } // namespace Render
916 } // namespace Dali::Internal