2 * Copyright (c) 2023 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/common/memory-pool-object-allocator.h>
27 #include <dali/internal/event/rendering/texture-impl.h>
28 #include <dali/internal/render/common/render-instruction.h>
29 #include <dali/internal/render/data-providers/node-data-provider.h>
30 #include <dali/internal/render/data-providers/uniform-map-data-provider.h>
31 #include <dali/internal/render/renderers/pipeline-cache.h>
32 #include <dali/internal/render/renderers/render-sampler.h>
33 #include <dali/internal/render/renderers/render-texture.h>
34 #include <dali/internal/render/renderers/render-vertex-buffer.h>
35 #include <dali/internal/render/renderers/shader-cache.h>
36 #include <dali/internal/render/renderers/uniform-buffer-view-pool.h>
37 #include <dali/internal/render/renderers/uniform-buffer-view.h>
38 #include <dali/internal/render/shaders/program.h>
39 #include <dali/internal/render/shaders/render-shader.h>
40 #include <dali/internal/update/common/uniform-map.h>
41 #include <dali/public-api/signals/render-callback.h>
43 namespace Dali::Internal
47 // Helper to get the property value getter by type
48 typedef const float& (PropertyInputImpl::*FuncGetter)(BufferIndex) const;
49 constexpr FuncGetter GetPropertyValueGetter(Property::Type type)
53 case Property::BOOLEAN:
55 return FuncGetter(&PropertyInputImpl::GetBoolean);
57 case Property::INTEGER:
59 return FuncGetter(&PropertyInputImpl::GetInteger);
63 return FuncGetter(&PropertyInputImpl::GetFloat);
65 case Property::VECTOR2:
67 return FuncGetter(&PropertyInputImpl::GetVector2);
69 case Property::VECTOR3:
71 return FuncGetter(&PropertyInputImpl::GetVector3);
73 case Property::VECTOR4:
75 return FuncGetter(&PropertyInputImpl::GetVector4);
77 case Property::MATRIX3:
79 return FuncGetter(&PropertyInputImpl::GetMatrix3);
81 case Property::MATRIX:
83 return FuncGetter(&PropertyInputImpl::GetMatrix);
93 * Helper function that returns size of uniform datatypes based
96 constexpr int GetPropertyValueSizeForUniform(Property::Type type)
100 case Property::Type::BOOLEAN:
104 case Property::Type::FLOAT:
106 return sizeof(float);
108 case Property::Type::INTEGER:
112 case Property::Type::VECTOR2:
114 return sizeof(Vector2);
116 case Property::Type::VECTOR3:
118 return sizeof(Vector3);
120 case Property::Type::VECTOR4:
122 return sizeof(Vector4);
124 case Property::Type::MATRIX3:
126 return sizeof(Matrix3);
128 case Property::Type::MATRIX:
130 return sizeof(Matrix);
140 * Helper function to calculate the correct alignment of data for uniform buffers
141 * @param dataSize size of uniform buffer
142 * @return aligned offset of data
144 inline uint32_t GetUniformBufferDataAlignment(uint32_t dataSize)
146 return ((dataSize / 256u) + ((dataSize % 256u) ? 1u : 0u)) * 256u;
155 MemoryPoolObjectAllocator<Renderer> gRenderRendererMemoryPool;
158 RendererKey Renderer::NewKey(SceneGraph::RenderDataProvider* dataProvider,
159 Render::Geometry* geometry,
160 uint32_t blendingBitmask,
161 const Vector4& blendColor,
162 FaceCullingMode::Type faceCullingMode,
163 bool preMultipliedAlphaEnabled,
164 DepthWriteMode::Type depthWriteMode,
165 DepthTestMode::Type depthTestMode,
166 DepthFunction::Type depthFunction,
167 StencilParameters& stencilParameters)
169 void* ptr = gRenderRendererMemoryPool.AllocateRawThreadSafe();
170 auto key = gRenderRendererMemoryPool.GetKeyFromPtr(static_cast<Renderer*>(ptr));
172 // Use placement new to construct renderer.
173 new(ptr) Renderer(dataProvider, geometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode, depthFunction, stencilParameters);
174 return RendererKey(key);
177 Renderer::Renderer(SceneGraph::RenderDataProvider* dataProvider,
178 Render::Geometry* geometry,
179 uint32_t blendingBitmask,
180 const Vector4& blendColor,
181 FaceCullingMode::Type faceCullingMode,
182 bool preMultipliedAlphaEnabled,
183 DepthWriteMode::Type depthWriteMode,
184 DepthTestMode::Type depthTestMode,
185 DepthFunction::Type depthFunction,
186 StencilParameters& stencilParameters)
187 : mGraphicsController(nullptr),
188 mRenderDataProvider(dataProvider),
190 mProgramCache(nullptr),
191 mStencilParameters(stencilParameters),
193 mIndexedDrawFirstElement(0),
194 mIndexedDrawElementsCount(0),
195 mDepthFunction(depthFunction),
196 mFaceCullingMode(faceCullingMode),
197 mDepthWriteMode(depthWriteMode),
198 mDepthTestMode(depthTestMode),
199 mPremultipliedAlphaEnabled(preMultipliedAlphaEnabled),
200 mShaderChanged(false)
202 if(blendingBitmask != 0u)
204 mBlendingOptions.SetBitmask(blendingBitmask);
207 mBlendingOptions.SetBlendColor(blendColor);
210 void Renderer::Initialize(Graphics::Controller& graphicsController, ProgramCache& programCache, Render::ShaderCache& shaderCache, Render::UniformBufferManager& uniformBufferManager, Render::PipelineCache& pipelineCache)
212 mGraphicsController = &graphicsController;
213 mProgramCache = &programCache;
214 mShaderCache = &shaderCache;
215 mUniformBufferManager = &uniformBufferManager;
216 mPipelineCache = &pipelineCache;
219 Renderer::~Renderer() = default;
221 void Renderer::operator delete(void* ptr)
223 gRenderRendererMemoryPool.FreeThreadSafe(static_cast<Renderer*>(ptr));
226 Renderer* Renderer::Get(RendererKey::KeyType rendererKey)
228 return gRenderRendererMemoryPool.GetPtrFromKey(rendererKey);
231 void Renderer::SetGeometry(Render::Geometry* geometry)
233 mGeometry = geometry;
236 void Renderer::SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size)
238 mDrawCommands.clear();
239 mDrawCommands.insert(mDrawCommands.end(), pDrawCommands, pDrawCommands + size);
242 void Renderer::BindTextures(Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures)
244 uint32_t textureUnit = 0;
246 auto textures(mRenderDataProvider->GetTextures());
247 auto samplers(mRenderDataProvider->GetSamplers());
249 std::vector<Graphics::TextureBinding> textureBindings;
251 if(textures != nullptr)
253 const std::uint32_t texturesCount(static_cast<std::uint32_t>(textures->Count()));
254 textureBindings.reserve(texturesCount);
256 for(uint32_t i = 0; i < texturesCount; ++i) // not expecting more than uint32_t of textures
258 if((*textures)[i] && (*textures)[i]->GetGraphicsObject())
260 Graphics::Texture* graphicsTexture = (*textures)[i]->GetGraphicsObject();
261 // if the sampler exists,
262 // if it's default, delete the graphics object
263 // otherwise re-initialize it if dirty
265 const Graphics::Sampler* graphicsSampler = samplers ? ((*samplers)[i] ? (*samplers)[i]->GetGraphicsObject()
269 boundTextures.PushBack(graphicsTexture);
270 const Graphics::TextureBinding textureBinding{graphicsTexture, graphicsSampler, textureUnit};
271 textureBindings.push_back(textureBinding);
278 if(!textureBindings.empty())
280 commandBuffer.BindTextures(textureBindings);
284 void Renderer::SetFaceCullingMode(FaceCullingMode::Type mode)
286 mFaceCullingMode = mode;
289 void Renderer::SetBlendingBitMask(uint32_t bitmask)
291 mBlendingOptions.SetBitmask(bitmask);
294 void Renderer::SetBlendColor(const Vector4& color)
296 mBlendingOptions.SetBlendColor(color);
299 void Renderer::SetIndexedDrawFirstElement(uint32_t firstElement)
301 mIndexedDrawFirstElement = firstElement;
304 void Renderer::SetIndexedDrawElementsCount(uint32_t elementsCount)
306 mIndexedDrawElementsCount = elementsCount;
309 void Renderer::EnablePreMultipliedAlpha(bool enable)
311 mPremultipliedAlphaEnabled = enable;
314 void Renderer::SetDepthWriteMode(DepthWriteMode::Type depthWriteMode)
316 mDepthWriteMode = depthWriteMode;
319 void Renderer::SetDepthTestMode(DepthTestMode::Type depthTestMode)
321 mDepthTestMode = depthTestMode;
324 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
326 return mDepthWriteMode;
329 DepthTestMode::Type Renderer::GetDepthTestMode() const
331 return mDepthTestMode;
334 void Renderer::SetDepthFunction(DepthFunction::Type depthFunction)
336 mDepthFunction = depthFunction;
339 DepthFunction::Type Renderer::GetDepthFunction() const
341 return mDepthFunction;
344 void Renderer::SetRenderMode(RenderMode::Type renderMode)
346 mStencilParameters.renderMode = renderMode;
349 RenderMode::Type Renderer::GetRenderMode() const
351 return mStencilParameters.renderMode;
354 void Renderer::SetStencilFunction(StencilFunction::Type stencilFunction)
356 mStencilParameters.stencilFunction = stencilFunction;
359 StencilFunction::Type Renderer::GetStencilFunction() const
361 return mStencilParameters.stencilFunction;
364 void Renderer::SetStencilFunctionMask(int stencilFunctionMask)
366 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
369 int Renderer::GetStencilFunctionMask() const
371 return mStencilParameters.stencilFunctionMask;
374 void Renderer::SetStencilFunctionReference(int stencilFunctionReference)
376 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
379 int Renderer::GetStencilFunctionReference() const
381 return mStencilParameters.stencilFunctionReference;
384 void Renderer::SetStencilMask(int stencilMask)
386 mStencilParameters.stencilMask = stencilMask;
389 int Renderer::GetStencilMask() const
391 return mStencilParameters.stencilMask;
394 void Renderer::SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail)
396 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
399 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
401 return mStencilParameters.stencilOperationOnFail;
404 void Renderer::SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail)
406 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
409 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
411 return mStencilParameters.stencilOperationOnZFail;
414 void Renderer::SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass)
416 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
419 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
421 return mStencilParameters.stencilOperationOnZPass;
424 void Renderer::Upload()
426 mGeometry->Upload(*mGraphicsController);
429 bool Renderer::Render(Graphics::CommandBuffer& commandBuffer,
430 BufferIndex bufferIndex,
431 const SceneGraph::NodeDataProvider& node,
432 const Matrix& modelMatrix,
433 const Matrix& modelViewMatrix,
434 const Matrix& viewMatrix,
435 const Matrix& projectionMatrix,
438 Vector<Graphics::Texture*>& boundTextures,
439 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
442 // Before doing anything test if the call happens in the right queue
443 if(mDrawCommands.empty() && queueIndex > 0)
448 // Check if there is render callback
451 if(!mRenderCallbackInput)
453 mRenderCallbackInput = std::unique_ptr<RenderCallbackInput>(new RenderCallbackInput);
456 Graphics::DrawNativeInfo info{};
457 info.api = Graphics::DrawNativeAPI::GLES;
458 info.callback = &static_cast<Dali::CallbackBase&>(*mRenderCallback);
459 info.userData = mRenderCallbackInput.get();
461 // Set storage for the context to be used
462 info.glesNativeInfo.eglSharedContextStoragePointer = &mRenderCallbackInput->eglContext;
463 info.reserved = nullptr;
465 auto& textureResources = mRenderCallback->GetTextureResources();
467 if(!textureResources.empty())
469 mRenderCallbackTextureBindings.clear();
470 mRenderCallbackInput->textureBindings.resize(textureResources.size());
472 for(auto& texture : textureResources)
474 auto& textureImpl = GetImplementation(texture);
475 auto graphicsTexture = textureImpl.GetRenderTextureKey()->GetGraphicsObject();
477 auto properties = mGraphicsController->GetTextureProperties(*graphicsTexture);
479 mRenderCallbackTextureBindings.emplace_back(graphicsTexture);
480 mRenderCallbackInput->textureBindings[i++] = properties.nativeHandle;
482 info.textureCount = mRenderCallbackTextureBindings.size();
483 info.textureList = mRenderCallbackTextureBindings.data();
486 // pass render callback input
487 mRenderCallbackInput->size = size;
488 mRenderCallbackInput->projection = projectionMatrix;
490 MatrixUtils::MultiplyProjectionMatrix(mRenderCallbackInput->mvp, modelViewMatrix, projectionMatrix);
493 commandBuffer.DrawNative(&info);
498 std::vector<DevelRenderer::DrawCommand*> commands;
499 for(auto& cmd : mDrawCommands)
501 if(cmd.queue == queueIndex)
503 commands.emplace_back(&cmd);
507 // Have commands but nothing to be drawn - abort
508 if(!mDrawCommands.empty() && commands.empty())
514 if(!mDrawCommands.empty())
516 blend = (commands[0]->queue != DevelRenderer::RENDER_QUEUE_OPAQUE) && blend;
520 ShaderDataPtr shaderData = mRenderDataProvider->GetShader().GetShaderData();
522 Program* program = Program::New(*mProgramCache,
524 *mGraphicsController);
527 DALI_LOG_ERROR("Failed to get program for shader at address %p.\n", reinterpret_cast<const void*>(&mRenderDataProvider->GetShader()));
531 // If program doesn't have Gfx program object assigned yet, prepare it.
532 if(!program->GetGraphicsProgramPtr())
534 const std::vector<char>& vertShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER);
535 const std::vector<char>& fragShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER);
536 Dali::Graphics::Shader& vertexShader = mShaderCache->GetShader(
538 Graphics::PipelineStage::VERTEX_SHADER,
539 shaderData->GetSourceMode());
541 Dali::Graphics::Shader& fragmentShader = mShaderCache->GetShader(
543 Graphics::PipelineStage::FRAGMENT_SHADER,
544 shaderData->GetSourceMode());
546 std::vector<Graphics::ShaderState> shaderStates{
547 Graphics::ShaderState()
548 .SetShader(vertexShader)
549 .SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER),
550 Graphics::ShaderState()
551 .SetShader(fragmentShader)
552 .SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER)};
554 auto createInfo = Graphics::ProgramCreateInfo();
555 createInfo.SetShaderState(shaderStates);
556 auto graphicsProgram = mGraphicsController->CreateProgram(createInfo, nullptr);
557 program->SetGraphicsProgram(std::move(graphicsProgram));
560 // Prepare the graphics pipeline. This may either re-use an existing pipeline or create a new one.
561 auto& pipeline = PrepareGraphicsPipeline(*program, instruction, node, blend);
563 commandBuffer.BindPipeline(pipeline);
565 BindTextures(commandBuffer, boundTextures);
567 std::size_t nodeIndex = BuildUniformIndexMap(bufferIndex, node, size, *program);
569 WriteUniformBuffer(bufferIndex, commandBuffer, program, instruction, node, modelMatrix, modelViewMatrix, viewMatrix, projectionMatrix, size, nodeIndex);
571 bool drawn = false; // Draw can fail if there are no vertex buffers or they haven't been uploaded yet
572 // @todo We should detect this case much earlier to prevent unnecessary work
574 if(mDrawCommands.empty())
576 drawn = mGeometry->Draw(*mGraphicsController, commandBuffer, mIndexedDrawFirstElement, mIndexedDrawElementsCount);
580 for(auto& cmd : commands)
582 mGeometry->Draw(*mGraphicsController, commandBuffer, cmd->firstIndex, cmd->elementCount);
589 std::size_t Renderer::BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program)
591 // Check if the map has changed
592 DALI_ASSERT_DEBUG(mRenderDataProvider && "No Uniform map data provider available");
594 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMapDataProvider();
595 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetCollectedUniformMap();
596 const SceneGraph::UniformMap& uniformMapNode = node.GetNodeUniformMap();
600 // Usual case is to only have 1 node, however we do allow multiple nodes to reuse the same
601 // renderer, so we have to cache uniform map per render item (node / renderer pair).
603 // Specially, if node don't have uniformMap, we mark nodePtr as nullptr.
604 // So, all nodes without uniformMap will share same UniformIndexMap, contains only render data providers.
605 const auto nodePtr = uniformMapNode.Count() ? &node : nullptr;
607 const auto nodeChangeCounter = nodePtr ? uniformMapNode.GetChangeCounter() : 0;
608 const auto renderItemMapChangeCounter = uniformMap.GetChangeCounter();
610 auto iter = std::find_if(mNodeIndexMap.begin(), mNodeIndexMap.end(), [nodePtr](RenderItemLookup& element) { return element.node == nodePtr; });
612 std::size_t renderItemMapIndex;
613 if(iter == mNodeIndexMap.end())
615 renderItemMapIndex = mUniformIndexMaps.size();
616 RenderItemLookup renderItemLookup;
617 renderItemLookup.node = nodePtr;
618 renderItemLookup.index = renderItemMapIndex;
619 renderItemLookup.nodeChangeCounter = nodeChangeCounter;
620 renderItemLookup.renderItemMapChangeCounter = renderItemMapChangeCounter;
621 mNodeIndexMap.emplace_back(renderItemLookup);
624 mUniformIndexMaps.resize(mUniformIndexMaps.size() + 1);
628 renderItemMapIndex = iter->index;
630 updateMaps = (nodeChangeCounter != iter->nodeChangeCounter) ||
631 (renderItemMapChangeCounter != iter->renderItemMapChangeCounter) ||
632 (mUniformIndexMaps[renderItemMapIndex].size() == 0);
634 iter->nodeChangeCounter = nodeChangeCounter;
635 iter->renderItemMapChangeCounter = renderItemMapChangeCounter;
638 if(updateMaps || mShaderChanged)
640 // Reset shader pointer
641 mShaderChanged = false;
643 const uint32_t mapCount = uniformMap.Count();
644 const uint32_t mapNodeCount = uniformMapNode.Count();
646 mUniformIndexMaps[renderItemMapIndex].clear(); // Clear contents, but keep memory if we don't change size
647 mUniformIndexMaps[renderItemMapIndex].resize(mapCount + mapNodeCount);
649 // Copy uniform map into mUniformIndexMap
650 uint32_t mapIndex = 0;
651 for(; mapIndex < mapCount; ++mapIndex)
653 mUniformIndexMaps[renderItemMapIndex][mapIndex].propertyValue = uniformMap.mUniformMap[mapIndex].propertyPtr;
654 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformName = uniformMap.mUniformMap[mapIndex].uniformName;
655 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHash = uniformMap.mUniformMap[mapIndex].uniformNameHash;
656 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHashNoArray = uniformMap.mUniformMap[mapIndex].uniformNameHashNoArray;
657 mUniformIndexMaps[renderItemMapIndex][mapIndex].arrayIndex = uniformMap.mUniformMap[mapIndex].arrayIndex;
660 for(uint32_t nodeMapIndex = 0; nodeMapIndex < mapNodeCount; ++nodeMapIndex)
662 auto hash = uniformMapNode[nodeMapIndex].uniformNameHash;
663 auto& name = uniformMapNode[nodeMapIndex].uniformName;
665 for(uint32_t i = 0; i < mapCount; ++i)
667 if(mUniformIndexMaps[renderItemMapIndex][i].uniformNameHash == hash &&
668 mUniformIndexMaps[renderItemMapIndex][i].uniformName == name)
670 mUniformIndexMaps[renderItemMapIndex][i].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
678 mUniformIndexMaps[renderItemMapIndex][mapIndex].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
679 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformName = uniformMapNode[nodeMapIndex].uniformName;
680 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHash = uniformMapNode[nodeMapIndex].uniformNameHash;
681 mUniformIndexMaps[renderItemMapIndex][mapIndex].uniformNameHashNoArray = uniformMapNode[nodeMapIndex].uniformNameHashNoArray;
682 mUniformIndexMaps[renderItemMapIndex][mapIndex].arrayIndex = uniformMapNode[nodeMapIndex].arrayIndex;
687 mUniformIndexMaps[renderItemMapIndex].resize(mapIndex);
689 return renderItemMapIndex;
692 void Renderer::WriteUniformBuffer(
693 BufferIndex bufferIndex,
694 Graphics::CommandBuffer& commandBuffer,
696 const SceneGraph::RenderInstruction& instruction,
697 const SceneGraph::NodeDataProvider& node,
698 const Matrix& modelMatrix,
699 const Matrix& modelViewMatrix,
700 const Matrix& viewMatrix,
701 const Matrix& projectionMatrix,
703 std::size_t nodeIndex)
706 uint32_t uboOffset{0u};
708 auto& reflection = mGraphicsController->GetProgramReflection(program->GetGraphicsProgram());
710 uint32_t uniformBlockAllocationBytes = program->GetUniformBlocksMemoryRequirements().totalSizeRequired;
712 // Create uniform buffer view from uniform buffer
713 Graphics::UniquePtr<Render::UniformBufferView> uboView{nullptr};
714 if(uniformBlockAllocationBytes)
716 auto uboPoolView = mUniformBufferManager->GetUniformBufferViewPool(bufferIndex);
717 uboView = uboPoolView->CreateUniformBufferView(uniformBlockAllocationBytes);
720 // update the uniform buffer
721 // pass shared UBO and offset, return new offset for next item to be used
722 // don't process bindings if there are no uniform buffers allocated
725 auto uboCount = reflection.GetUniformBlockCount();
726 mUniformBufferBindings.resize(uboCount);
728 std::vector<Graphics::UniformBufferBinding>* bindings{&mUniformBufferBindings};
730 mUniformBufferBindings[0].buffer = uboView->GetBuffer(&mUniformBufferBindings[0].offset);
732 // Write default uniforms
733 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_MATRIX), *uboView, modelMatrix);
734 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::VIEW_MATRIX), *uboView, viewMatrix);
735 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::PROJECTION_MATRIX), *uboView, projectionMatrix);
736 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_VIEW_MATRIX), *uboView, modelViewMatrix);
738 auto mvpUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::MVP_MATRIX);
739 if(mvpUniformInfo && !mvpUniformInfo->name.empty())
741 Matrix modelViewProjectionMatrix(false);
742 MatrixUtils::MultiplyProjectionMatrix(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
743 WriteDefaultUniform(mvpUniformInfo, *uboView, modelViewProjectionMatrix);
746 auto normalUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::NORMAL_MATRIX);
747 if(normalUniformInfo && !normalUniformInfo->name.empty())
749 Matrix3 normalMatrix(modelViewMatrix);
750 normalMatrix.Invert();
751 normalMatrix.Transpose();
752 WriteDefaultUniform(normalUniformInfo, *uboView, normalMatrix);
755 Vector4 finalColor; ///< Applied renderer's opacity color
756 const Vector4& color = node.GetRenderColor(bufferIndex); ///< Actor's original color
757 if(mPremultipliedAlphaEnabled)
759 const float& alpha = color.a * mRenderDataProvider->GetOpacity(bufferIndex);
760 finalColor = Vector4(color.r * alpha, color.g * alpha, color.b * alpha, alpha);
764 finalColor = Vector4(color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity(bufferIndex));
766 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::COLOR), *uboView, finalColor);
767 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::ACTOR_COLOR), *uboView, color);
769 // Write uniforms from the uniform map
770 FillUniformBuffer(*program, instruction, *uboView, bindings, uboOffset, bufferIndex, nodeIndex);
772 // Write uSize in the end, as it shouldn't be overridable by dynamic properties.
773 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::SIZE), *uboView, size);
775 commandBuffer.BindUniformBuffers(*bindings);
780 bool Renderer::WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo, Render::UniformBufferView& ubo, const T& data)
782 if(uniformInfo && !uniformInfo->name.empty())
784 WriteUniform(ubo, *uniformInfo, data);
791 void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const T& data)
793 WriteUniform(ubo, uniformInfo, &data, sizeof(T));
796 void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const void* data, uint32_t size)
798 ubo.Write(data, size, ubo.GetOffset() + uniformInfo.offset);
801 void Renderer::FillUniformBuffer(Program& program,
802 const SceneGraph::RenderInstruction& instruction,
803 Render::UniformBufferView& ubo,
804 std::vector<Graphics::UniformBufferBinding>*& outBindings,
806 BufferIndex updateBufferIndex,
807 std::size_t nodeIndex)
809 auto& reflection = mGraphicsController->GetProgramReflection(program.GetGraphicsProgram());
810 auto uboCount = reflection.GetUniformBlockCount();
813 uint32_t dataOffset = offset;
814 for(auto i = 0u; i < uboCount; ++i)
816 mUniformBufferBindings[i].dataSize = reflection.GetUniformBlockSize(i);
817 mUniformBufferBindings[i].binding = reflection.GetUniformBlockBinding(i);
819 dataOffset += GetUniformBufferDataAlignment(mUniformBufferBindings[i].dataSize);
820 mUniformBufferBindings[i].buffer = ubo.GetBuffer(&mUniformBufferBindings[i].offset);
822 for(auto iter = mUniformIndexMaps[nodeIndex].begin(),
823 end = mUniformIndexMaps[nodeIndex].end();
827 auto& uniform = *iter;
828 int arrayIndex = uniform.arrayIndex;
830 if(!uniform.uniformFunc)
832 auto uniformInfo = Graphics::UniformInfo{};
833 auto uniformFound = program.GetUniform(uniform.uniformName.GetStringView(),
834 uniform.uniformNameHash,
835 uniform.uniformNameHashNoArray,
838 uniform.uniformOffset = uniformInfo.offset;
839 uniform.uniformLocation = uniformInfo.location;
843 auto dst = ubo.GetOffset() + uniformInfo.offset;
844 const auto typeSize = GetPropertyValueSizeForUniform((*iter).propertyValue->GetType());
845 const auto dest = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
846 const auto func = GetPropertyValueGetter((*iter).propertyValue->GetType());
848 ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
852 uniform.uniformSize = typeSize;
853 uniform.uniformFunc = func;
858 auto dst = ubo.GetOffset() + uniform.uniformOffset;
859 const auto typeSize = uniform.uniformSize;
860 const auto dest = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
861 const auto func = uniform.uniformFunc;
863 ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
869 // write output bindings
870 outBindings = &mUniformBufferBindings;
876 void Renderer::SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const
878 sortAttributes.shader = &(mRenderDataProvider->GetShader());
879 sortAttributes.geometry = mGeometry;
882 void Renderer::SetShaderChanged(bool value)
884 mShaderChanged = value;
887 bool Renderer::Updated(BufferIndex bufferIndex)
889 if(mRenderCallback || mShaderChanged || mGeometry->AttributesChanged() || mRenderDataProvider->IsUpdated())
894 auto* textures = mRenderDataProvider->GetTextures();
897 for(auto iter = textures->Begin(), end = textures->End(); iter < end; ++iter)
899 auto texture = *iter;
900 if(texture && texture->Updated())
909 Vector4 Renderer::GetVisualTransformedUpdateArea(BufferIndex bufferIndex, const Vector4& originalUpdateArea) const noexcept
911 return mRenderDataProvider->GetVisualTransformedUpdateArea(bufferIndex, originalUpdateArea);
914 Graphics::Pipeline& Renderer::PrepareGraphicsPipeline(
916 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
917 const SceneGraph::NodeDataProvider& node,
920 // Prepare query info
921 PipelineCacheQueryInfo queryInfo{};
922 queryInfo.program = &program;
923 queryInfo.renderer = this;
924 queryInfo.geometry = mGeometry;
925 queryInfo.blendingEnabled = blend;
926 queryInfo.blendingOptions = &mBlendingOptions;
927 queryInfo.alphaPremultiplied = mPremultipliedAlphaEnabled;
928 queryInfo.cameraUsingReflection = instruction.GetCamera()->GetReflectionUsed();
930 auto pipelineResult = mPipelineCache->GetPipeline(queryInfo, true);
932 // should be never null?
933 return *pipelineResult.pipeline;
936 void Renderer::SetRenderCallback(RenderCallback* callback)
938 mRenderCallback = callback;
941 } // namespace Render
943 } // namespace Dali::Internal