2 * Copyright (c) 2021 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-program.h>
23 #include <dali/graphics-api/graphics-types.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/gl-resources/context.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/shaders/program.h>
34 #include <dali/internal/render/shaders/scene-graph-shader.h>
35 #include <dali/internal/update/common/uniform-map.h>
43 // Size of uniform buffer page used when resizing
44 constexpr uint32_t UBO_PAGE_SIZE = 8192u;
46 // UBO allocation threshold below which the UBO will shrink
47 constexpr auto UBO_SHRINK_THRESHOLD = 0.75f;
49 // Helper to get the vertex input format
50 Dali::Graphics::VertexInputFormat GetPropertyVertexFormat(Property::Type propertyType)
52 Dali::Graphics::VertexInputFormat type{};
57 case Property::STRING:
60 case Property::EXTENTS: // i4?
61 case Property::RECTANGLE: // i4/f4?
62 case Property::ROTATION:
64 type = Dali::Graphics::VertexInputFormat::UNDEFINED;
67 case Property::BOOLEAN:
69 type = Dali::Graphics::VertexInputFormat::UNDEFINED; // type = GL_BYTE; @todo new type for this?
72 case Property::INTEGER:
74 type = Dali::Graphics::VertexInputFormat::INTEGER; // (short)
79 type = Dali::Graphics::VertexInputFormat::FLOAT;
82 case Property::VECTOR2:
84 type = Dali::Graphics::VertexInputFormat::FVECTOR2;
87 case Property::VECTOR3:
89 type = Dali::Graphics::VertexInputFormat::FVECTOR3;
92 case Property::VECTOR4:
94 type = Dali::Graphics::VertexInputFormat::FVECTOR4;
97 case Property::MATRIX3:
99 type = Dali::Graphics::VertexInputFormat::FLOAT;
102 case Property::MATRIX:
104 type = Dali::Graphics::VertexInputFormat::FLOAT;
112 constexpr Graphics::CullMode ConvertCullFace(Dali::FaceCullingMode::Type mode)
116 case Dali::FaceCullingMode::NONE:
118 return Graphics::CullMode::NONE;
120 case Dali::FaceCullingMode::FRONT:
122 return Graphics::CullMode::FRONT;
124 case Dali::FaceCullingMode::BACK:
126 return Graphics::CullMode::BACK;
128 case Dali::FaceCullingMode::FRONT_AND_BACK:
130 return Graphics::CullMode::FRONT_AND_BACK;
133 return Graphics::CullMode::NONE;
136 constexpr Graphics::BlendFactor ConvertBlendFactor(BlendFactor::Type blendFactor)
140 case BlendFactor::ZERO:
141 return Graphics::BlendFactor::ZERO;
142 case BlendFactor::ONE:
143 return Graphics::BlendFactor::ONE;
144 case BlendFactor::SRC_COLOR:
145 return Graphics::BlendFactor::SRC_COLOR;
146 case BlendFactor::ONE_MINUS_SRC_COLOR:
147 return Graphics::BlendFactor::ONE_MINUS_SRC_COLOR;
148 case BlendFactor::SRC_ALPHA:
149 return Graphics::BlendFactor::SRC_ALPHA;
150 case BlendFactor::ONE_MINUS_SRC_ALPHA:
151 return Graphics::BlendFactor::ONE_MINUS_SRC_ALPHA;
152 case BlendFactor::DST_ALPHA:
153 return Graphics::BlendFactor::DST_ALPHA;
154 case BlendFactor::ONE_MINUS_DST_ALPHA:
155 return Graphics::BlendFactor::ONE_MINUS_DST_ALPHA;
156 case BlendFactor::DST_COLOR:
157 return Graphics::BlendFactor::DST_COLOR;
158 case BlendFactor::ONE_MINUS_DST_COLOR:
159 return Graphics::BlendFactor::ONE_MINUS_DST_COLOR;
160 case BlendFactor::SRC_ALPHA_SATURATE:
161 return Graphics::BlendFactor::SRC_ALPHA_SATURATE;
162 case BlendFactor::CONSTANT_COLOR:
163 return Graphics::BlendFactor::CONSTANT_COLOR;
164 case BlendFactor::ONE_MINUS_CONSTANT_COLOR:
165 return Graphics::BlendFactor::ONE_MINUS_CONSTANT_COLOR;
166 case BlendFactor::CONSTANT_ALPHA:
167 return Graphics::BlendFactor::CONSTANT_ALPHA;
168 case BlendFactor::ONE_MINUS_CONSTANT_ALPHA:
169 return Graphics::BlendFactor::ONE_MINUS_CONSTANT_ALPHA;
171 return Graphics::BlendFactor{};
174 constexpr Graphics::BlendOp ConvertBlendEquation(DevelBlendEquation::Type blendEquation)
176 switch(blendEquation)
178 case DevelBlendEquation::ADD:
179 return Graphics::BlendOp::ADD;
180 case DevelBlendEquation::SUBTRACT:
181 return Graphics::BlendOp::SUBTRACT;
182 case DevelBlendEquation::REVERSE_SUBTRACT:
183 return Graphics::BlendOp::REVERSE_SUBTRACT;
184 case DevelBlendEquation::COLOR:
185 case DevelBlendEquation::COLOR_BURN:
186 case DevelBlendEquation::COLOR_DODGE:
187 case DevelBlendEquation::DARKEN:
188 case DevelBlendEquation::DIFFERENCE:
189 case DevelBlendEquation::EXCLUSION:
190 case DevelBlendEquation::HARD_LIGHT:
191 case DevelBlendEquation::HUE:
192 case DevelBlendEquation::LIGHTEN:
193 case DevelBlendEquation::LUMINOSITY:
194 case DevelBlendEquation::MAX:
195 case DevelBlendEquation::MIN:
196 case DevelBlendEquation::MULTIPLY:
197 case DevelBlendEquation::OVERLAY:
198 case DevelBlendEquation::SATURATION:
199 case DevelBlendEquation::SCREEN:
200 case DevelBlendEquation::SOFT_LIGHT:
201 return Graphics::BlendOp{};
203 return Graphics::BlendOp{};
207 * Helper function to calculate the correct alignment of data for uniform buffers
208 * @param dataSize size of uniform buffer
209 * @return aligned offset of data
211 inline uint32_t GetUniformBufferDataAlignment(uint32_t dataSize)
213 return ((dataSize / 256u) + ((dataSize % 256u) ? 1u : 0u)) * 256u;
220 Renderer* Renderer::New(SceneGraph::RenderDataProvider* dataProvider,
221 Render::Geometry* geometry,
222 uint32_t blendingBitmask,
223 const Vector4& blendColor,
224 FaceCullingMode::Type faceCullingMode,
225 bool preMultipliedAlphaEnabled,
226 DepthWriteMode::Type depthWriteMode,
227 DepthTestMode::Type depthTestMode,
228 DepthFunction::Type depthFunction,
229 StencilParameters& stencilParameters)
231 return new Renderer(dataProvider, geometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode, depthFunction, stencilParameters);
234 Renderer::Renderer(SceneGraph::RenderDataProvider* dataProvider,
235 Render::Geometry* geometry,
236 uint32_t blendingBitmask,
237 const Vector4& blendColor,
238 FaceCullingMode::Type faceCullingMode,
239 bool preMultipliedAlphaEnabled,
240 DepthWriteMode::Type depthWriteMode,
241 DepthTestMode::Type depthTestMode,
242 DepthFunction::Type depthFunction,
243 StencilParameters& stencilParameters)
244 : mGraphicsController(nullptr),
245 mRenderDataProvider(dataProvider),
248 mProgramCache(nullptr),
250 mAttributeLocations(),
252 mStencilParameters(stencilParameters),
254 mIndexedDrawFirstElement(0),
255 mIndexedDrawElementsCount(0),
256 mDepthFunction(depthFunction),
257 mFaceCullingMode(faceCullingMode),
258 mDepthWriteMode(depthWriteMode),
259 mDepthTestMode(depthTestMode),
260 mUpdateAttributeLocations(true),
261 mPremultipledAlphaEnabled(preMultipliedAlphaEnabled),
262 mShaderChanged(false),
265 if(blendingBitmask != 0u)
267 mBlendingOptions.SetBitmask(blendingBitmask);
270 mBlendingOptions.SetBlendColor(blendColor);
273 void Renderer::Initialize(Context& context, Graphics::Controller& graphicsController, ProgramCache& programCache, Render::ShaderCache& shaderCache, Render::UniformBufferManager& uniformBufferManager)
276 mGraphicsController = &graphicsController;
277 mProgramCache = &programCache;
278 mShaderCache = &shaderCache;
279 mUniformBufferManager = &uniformBufferManager;
282 Renderer::~Renderer() = default;
284 void Renderer::SetGeometry(Render::Geometry* geometry)
286 mGeometry = geometry;
287 mUpdateAttributeLocations = true;
289 void Renderer::SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size)
291 mDrawCommands.clear();
292 mDrawCommands.insert(mDrawCommands.end(), pDrawCommands, pDrawCommands + size);
295 void Renderer::GlContextDestroyed()
297 mGeometry->GlContextDestroyed();
300 void Renderer::GlCleanup()
304 void Renderer::SetUniformFromProperty(BufferIndex bufferIndex, Program& program, UniformIndexMap& map)
306 GLint location = program.GetUniformLocation(map.uniformIndex);
307 if(Program::UNIFORM_UNKNOWN != location)
309 // switch based on property type to use correct GL uniform setter
310 switch(map.propertyValue->GetType())
312 case Property::INTEGER:
314 program.SetUniform1i(location, map.propertyValue->GetInteger(bufferIndex));
317 case Property::FLOAT:
319 program.SetUniform1f(location, map.propertyValue->GetFloat(bufferIndex));
322 case Property::VECTOR2:
324 Vector2 value(map.propertyValue->GetVector2(bufferIndex));
325 program.SetUniform2f(location, value.x, value.y);
329 case Property::VECTOR3:
331 Vector3 value(map.propertyValue->GetVector3(bufferIndex));
332 program.SetUniform3f(location, value.x, value.y, value.z);
336 case Property::VECTOR4:
338 Vector4 value(map.propertyValue->GetVector4(bufferIndex));
339 program.SetUniform4f(location, value.x, value.y, value.z, value.w);
343 case Property::ROTATION:
345 Quaternion value(map.propertyValue->GetQuaternion(bufferIndex));
346 program.SetUniform4f(location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w);
350 case Property::MATRIX:
352 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
353 program.SetUniformMatrix4fv(location, 1, value.AsFloat());
357 case Property::MATRIX3:
359 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
360 program.SetUniformMatrix3fv(location, 1, value.AsFloat());
366 // Other property types are ignored
373 void Renderer::BindTextures(Program& program, Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures)
375 uint32_t textureUnit = 0;
377 GLint uniformLocation(-1);
378 std::vector<Render::Sampler*>& samplers(mRenderDataProvider->GetSamplers());
379 std::vector<Render::Texture*>& textures(mRenderDataProvider->GetTextures());
381 std::vector<Graphics::TextureBinding> textureBindings;
382 for(uint32_t i = 0; i < static_cast<uint32_t>(textures.size()); ++i) // not expecting more than uint32_t of textures
384 if(textures[i] && textures[i]->GetGraphicsObject())
386 if(program.GetSamplerUniformLocation(i, uniformLocation))
388 // if the sampler exists,
389 // if it's default, delete the graphics object
390 // otherwise re-initialize it if dirty
392 const Graphics::Sampler* graphicsSampler = (samplers[i] ? samplers[i]->GetGraphicsObject()
395 boundTextures.PushBack(textures[i]->GetGraphicsObject());
396 const Graphics::TextureBinding textureBinding{textures[i]->GetGraphicsObject(), graphicsSampler, textureUnit};
397 textureBindings.push_back(textureBinding);
399 program.SetUniform1i(uniformLocation, textureUnit); // Get through shader reflection
405 if(textureBindings.size() > 0)
407 commandBuffer.BindTextures(textureBindings);
411 void Renderer::SetFaceCullingMode(FaceCullingMode::Type mode)
413 mFaceCullingMode = mode;
417 void Renderer::SetBlendingBitMask(uint32_t bitmask)
419 mBlendingOptions.SetBitmask(bitmask);
423 void Renderer::SetBlendColor(const Vector4& color)
425 mBlendingOptions.SetBlendColor(color);
429 void Renderer::SetIndexedDrawFirstElement(uint32_t firstElement)
431 mIndexedDrawFirstElement = firstElement;
435 void Renderer::SetIndexedDrawElementsCount(uint32_t elementsCount)
437 mIndexedDrawElementsCount = elementsCount;
441 void Renderer::EnablePreMultipliedAlpha(bool enable)
443 mPremultipledAlphaEnabled = enable;
447 void Renderer::SetDepthWriteMode(DepthWriteMode::Type depthWriteMode)
449 mDepthWriteMode = depthWriteMode;
453 void Renderer::SetDepthTestMode(DepthTestMode::Type depthTestMode)
455 mDepthTestMode = depthTestMode;
459 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
461 return mDepthWriteMode;
464 DepthTestMode::Type Renderer::GetDepthTestMode() const
466 return mDepthTestMode;
469 void Renderer::SetDepthFunction(DepthFunction::Type depthFunction)
471 mDepthFunction = depthFunction;
475 DepthFunction::Type Renderer::GetDepthFunction() const
477 return mDepthFunction;
480 void Renderer::SetRenderMode(RenderMode::Type renderMode)
482 mStencilParameters.renderMode = renderMode;
486 RenderMode::Type Renderer::GetRenderMode() const
488 return mStencilParameters.renderMode;
491 void Renderer::SetStencilFunction(StencilFunction::Type stencilFunction)
493 mStencilParameters.stencilFunction = stencilFunction;
497 StencilFunction::Type Renderer::GetStencilFunction() const
499 return mStencilParameters.stencilFunction;
502 void Renderer::SetStencilFunctionMask(int stencilFunctionMask)
504 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
508 int Renderer::GetStencilFunctionMask() const
510 return mStencilParameters.stencilFunctionMask;
513 void Renderer::SetStencilFunctionReference(int stencilFunctionReference)
515 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
519 int Renderer::GetStencilFunctionReference() const
521 return mStencilParameters.stencilFunctionReference;
524 void Renderer::SetStencilMask(int stencilMask)
526 mStencilParameters.stencilMask = stencilMask;
530 int Renderer::GetStencilMask() const
532 return mStencilParameters.stencilMask;
535 void Renderer::SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail)
537 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
541 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
543 return mStencilParameters.stencilOperationOnFail;
546 void Renderer::SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail)
548 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
552 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
554 return mStencilParameters.stencilOperationOnZFail;
557 void Renderer::SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass)
559 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
563 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
565 return mStencilParameters.stencilOperationOnZPass;
568 void Renderer::Upload()
570 mGeometry->Upload(*mGraphicsController);
573 void Renderer::Render(Context& context,
574 BufferIndex bufferIndex,
575 const SceneGraph::NodeDataProvider& node,
576 const Matrix& modelMatrix,
577 const Matrix& modelViewMatrix,
578 const Matrix& viewMatrix,
579 const Matrix& projectionMatrix,
582 Vector<Graphics::Texture*>& boundTextures,
583 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
586 // Before doing anything test if the call happens in the right queue
587 if(mDrawCommands.empty() && queueIndex > 0)
593 std::vector<DevelRenderer::DrawCommand*> commands;
594 for(auto& cmd : mDrawCommands)
596 if(cmd.queue == queueIndex)
598 commands.emplace_back(&cmd);
602 // Have commands but nothing to be drawn - abort
603 if(!mDrawCommands.empty() && commands.empty())
608 // Create command buffer if not present
609 if(!mGraphicsCommandBuffer)
611 mGraphicsCommandBuffer = mGraphicsController->CreateCommandBuffer(
612 Graphics::CommandBufferCreateInfo()
613 .SetLevel(Graphics::CommandBufferLevel::SECONDARY),
618 mGraphicsCommandBuffer->Reset();
621 auto& commandBuffer = mGraphicsCommandBuffer;
624 if(!mDrawCommands.empty())
626 blend = (commands[0]->queue == DevelRenderer::RENDER_QUEUE_OPAQUE ? false : blend);
630 ShaderDataPtr shaderData = mRenderDataProvider->GetShader().GetShaderData();
631 const std::vector<char>& vertShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER);
632 const std::vector<char>& fragShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER);
633 Dali::Graphics::Shader& vertexShader = mShaderCache->GetShader(
635 Graphics::PipelineStage::VERTEX_SHADER,
636 shaderData->GetSourceMode());
638 Dali::Graphics::Shader& fragmentShader = mShaderCache->GetShader(
640 Graphics::PipelineStage::FRAGMENT_SHADER,
641 shaderData->GetSourceMode());
643 std::vector<Graphics::ShaderState> shaderStates{
644 Graphics::ShaderState()
645 .SetShader(vertexShader)
646 .SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER),
647 Graphics::ShaderState()
648 .SetShader(fragmentShader)
649 .SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER)};
651 auto createInfo = Graphics::ProgramCreateInfo();
652 createInfo.SetShaderState(shaderStates);
654 auto graphicsProgram = mGraphicsController->CreateProgram(createInfo, nullptr);
655 Program* program = Program::New(*mProgramCache,
657 *mGraphicsController,
658 std::move(graphicsProgram),
659 (shaderData->GetHints() & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0);
663 DALI_LOG_ERROR("Failed to get program for shader at address %p.\n", reinterpret_cast<void*>(&mRenderDataProvider->GetShader()));
667 // Temporarily create a pipeline here - this will be used for transporting
668 // topology, vertex format, attrs, rasterization state
669 mGraphicsPipeline = PrepareGraphicsPipeline(*program, instruction, blend, std::move(mGraphicsPipeline));
671 commandBuffer->BindPipeline(*mGraphicsPipeline.get());
673 BindTextures(*program, *commandBuffer.get(), boundTextures);
675 BuildUniformIndexMap(bufferIndex, node, size, *program);
677 WriteUniformBuffer(bufferIndex, *commandBuffer.get(), program, instruction, node, modelMatrix, modelViewMatrix, viewMatrix, projectionMatrix, size);
679 bool drawn = false; // Draw can fail if there are no vertex buffers or they haven't been uploaded yet
680 // @todo We should detect this case much earlier to prevent unnecessary work
682 //@todo manage mDrawCommands in the same way as above command buffer?!
683 if(mDrawCommands.empty())
685 drawn = mGeometry->Draw(*mGraphicsController, *commandBuffer.get(), mIndexedDrawFirstElement, mIndexedDrawElementsCount);
689 for(auto& cmd : commands)
691 // @todo This should generate a command buffer per cmd
692 // Tests WILL fail. (Temporarily commented out)
693 mGeometry->Draw(*mGraphicsController, *commandBuffer.get(), cmd->firstIndex, cmd->elementCount);
697 // Command buffer contains Texture bindings, vertex bindings, index buffer binding, pipeline(vertex format)
698 // @todo We should return the command buffer(s) and let the calling method submit
699 // If not drawn, then don't add command buffer to submit info, and if empty, don't
703 Graphics::SubmitInfo submitInfo{{}, 0 | Graphics::SubmitFlagBits::FLUSH};
704 submitInfo.cmdBuffer.push_back(commandBuffer.get());
705 mGraphicsController->SubmitCommandBuffers(submitInfo);
711 void Renderer::BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program)
713 // Check if the map has changed
714 DALI_ASSERT_DEBUG(mRenderDataProvider && "No Uniform map data provider available");
716 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
718 if(uniformMapDataProvider.GetUniformMapChanged(bufferIndex) ||
719 node.GetUniformMapChanged(bufferIndex) ||
720 mUniformIndexMap.Count() == 0 ||
723 // Reset shader pointer
724 mShaderChanged = false;
726 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap(bufferIndex);
727 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap(bufferIndex);
729 uint32_t maxMaps = static_cast<uint32_t>(uniformMap.Count() + uniformMapNode.Count()); // 4,294,967,295 maps should be enough
730 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
731 mUniformIndexMap.Resize(maxMaps);
733 uint32_t mapIndex = 0;
734 for(; mapIndex < uniformMap.Count(); ++mapIndex)
736 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex].propertyPtr;
737 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform(uniformMap[mapIndex].uniformName);
738 mUniformIndexMap[mapIndex].uniformName = uniformMap[mapIndex].uniformName;
739 mUniformIndexMap[mapIndex].uniformNameHash = uniformMap[mapIndex].uniformNameHash;
740 mUniformIndexMap[mapIndex].uniformNameHashNoArray = uniformMap[mapIndex].uniformNameHashNoArray;
741 mUniformIndexMap[mapIndex].arrayIndex = uniformMap[mapIndex].arrayIndex;
744 for(uint32_t nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count(); ++nodeMapIndex)
746 uint32_t uniformIndex = program.RegisterUniform(uniformMapNode[nodeMapIndex].uniformName);
748 for(uint32_t i = 0; i < uniformMap.Count(); ++i)
750 if(mUniformIndexMap[i].uniformIndex == uniformIndex)
752 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
760 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
761 mUniformIndexMap[mapIndex].uniformName = uniformMapNode[nodeMapIndex].uniformName;
762 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
763 mUniformIndexMap[mapIndex].uniformNameHash = uniformMapNode[nodeMapIndex].uniformNameHash;
764 mUniformIndexMap[mapIndex].uniformNameHashNoArray = uniformMapNode[nodeMapIndex].uniformNameHashNoArray;
765 mUniformIndexMap[mapIndex].arrayIndex = uniformMapNode[nodeMapIndex].arrayIndex;
770 mUniformIndexMap.Resize(mapIndex);
774 void Renderer::WriteUniformBuffer(
775 BufferIndex bufferIndex,
776 Graphics::CommandBuffer& commandBuffer,
778 const SceneGraph::RenderInstruction& instruction,
779 const SceneGraph::NodeDataProvider& node,
780 const Matrix& modelMatrix,
781 const Matrix& modelViewMatrix,
782 const Matrix& viewMatrix,
783 const Matrix& projectionMatrix,
787 uint32_t uniformBlockAllocationBytes{0u};
788 uint32_t uniformBlockMaxSize{0u};
789 uint32_t uboOffset{0u};
791 auto& reflection = mGraphicsController->GetProgramReflection(program->GetGraphicsProgram());
792 for(auto i = 0u; i < reflection.GetUniformBlockCount(); ++i)
794 auto blockSize = GetUniformBufferDataAlignment(reflection.GetUniformBlockSize(i));
795 if(uniformBlockMaxSize < blockSize)
797 uniformBlockMaxSize = blockSize;
799 uniformBlockAllocationBytes += blockSize;
802 auto pagedAllocation = ((uniformBlockAllocationBytes / UBO_PAGE_SIZE + 1u)) * UBO_PAGE_SIZE;
804 // Allocate twice memory as required by the uniform buffers
805 // todo: memory usage backlog to use optimal allocation
806 if(uniformBlockAllocationBytes && !mUniformBuffer[bufferIndex])
808 mUniformBuffer[bufferIndex] = mUniformBufferManager->AllocateUniformBuffer(pagedAllocation);
810 else if(uniformBlockAllocationBytes && (mUniformBuffer[bufferIndex]->GetSize() < pagedAllocation ||
811 (pagedAllocation < uint32_t(float(mUniformBuffer[bufferIndex]->GetSize()) * UBO_SHRINK_THRESHOLD))))
813 mUniformBuffer[bufferIndex]->Reserve(pagedAllocation);
817 if(mUniformBuffer[bufferIndex])
819 mUniformBuffer[bufferIndex]->Fill(0, 0u, 0u);
822 // update the uniform buffer
823 // pass shared UBO and offset, return new offset for next item to be used
824 // don't process bindings if there are no uniform buffers allocated
825 auto ubo = mUniformBuffer[bufferIndex].get();
828 auto uboCount = reflection.GetUniformBlockCount();
829 mUniformBufferBindings.resize(uboCount);
831 std::vector<Graphics::UniformBufferBinding>* bindings{&mUniformBufferBindings};
833 // Write default uniforms
834 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_MATRIX), *ubo, *bindings, modelMatrix);
835 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::VIEW_MATRIX), *ubo, *bindings, viewMatrix);
836 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::PROJECTION_MATRIX), *ubo, *bindings, projectionMatrix);
837 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_VIEW_MATRIX), *ubo, *bindings, modelViewMatrix);
839 auto mvpUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::MVP_MATRIX);
840 if(mvpUniformInfo && !mvpUniformInfo->name.empty())
842 Matrix modelViewProjectionMatrix(false);
843 Matrix::Multiply(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
844 WriteDefaultUniform(mvpUniformInfo, *ubo, *bindings, modelViewProjectionMatrix);
847 auto normalUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::NORMAL_MATRIX);
848 if(normalUniformInfo && !normalUniformInfo->name.empty())
850 Matrix3 normalMatrix(modelViewMatrix);
851 normalMatrix.Invert();
852 normalMatrix.Transpose();
853 WriteDefaultUniform(normalUniformInfo, *ubo, *bindings, normalMatrix);
857 const Vector4& color = node.GetRenderColor(bufferIndex);
858 if(mPremultipledAlphaEnabled)
860 float alpha = color.a * mRenderDataProvider->GetOpacity(bufferIndex);
861 finalColor = Vector4(color.r * alpha, color.g * alpha, color.b * alpha, alpha);
865 finalColor = Vector4(color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity(bufferIndex));
867 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::COLOR), *ubo, *bindings, finalColor);
869 // Write uniforms from the uniform map
870 FillUniformBuffer(*program, instruction, *ubo, bindings, uboOffset, bufferIndex);
872 // Write uSize in the end, as it shouldn't be overridable by dynamic properties.
873 WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::SIZE), *ubo, *bindings, size);
875 commandBuffer.BindUniformBuffers(*bindings);
880 bool Renderer::WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo, Render::UniformBuffer& ubo, const std::vector<Graphics::UniformBufferBinding>& bindings, const T& data)
882 if(uniformInfo && !uniformInfo->name.empty())
884 WriteUniform(ubo, bindings, *uniformInfo, data);
891 void Renderer::WriteUniform(Render::UniformBuffer& ubo, const std::vector<Graphics::UniformBufferBinding>& bindings, const Graphics::UniformInfo& uniformInfo, const T& data)
893 WriteUniform(ubo, bindings, uniformInfo, &data, sizeof(T));
896 void Renderer::WriteUniform(Render::UniformBuffer& ubo, const std::vector<Graphics::UniformBufferBinding>& bindings, const Graphics::UniformInfo& uniformInfo, const void* data, uint32_t size)
898 ubo.Write(data, size, bindings[uniformInfo.bufferIndex].offset + uniformInfo.offset);
901 void Renderer::FillUniformBuffer(Program& program,
902 const SceneGraph::RenderInstruction& instruction,
903 Render::UniformBuffer& ubo,
904 std::vector<Graphics::UniformBufferBinding>*& outBindings,
906 BufferIndex updateBufferIndex)
908 auto& reflection = mGraphicsController->GetProgramReflection(program.GetGraphicsProgram());
909 auto uboCount = reflection.GetUniformBlockCount();
912 uint32_t dataOffset = offset;
913 for(auto i = 0u; i < uboCount; ++i)
915 mUniformBufferBindings[i].dataSize = reflection.GetUniformBlockSize(i);
916 mUniformBufferBindings[i].binding = reflection.GetUniformBlockBinding(i);
917 mUniformBufferBindings[i].offset = dataOffset;
919 dataOffset += GetUniformBufferDataAlignment(mUniformBufferBindings[i].dataSize);
920 mUniformBufferBindings[i].buffer = ubo.GetBuffer();
922 for(UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
923 end = mUniformIndexMap.End();
927 // @todo This means parsing the uniform string every frame. Instead, store the array index if present.
928 int arrayIndex = (*iter).arrayIndex;
930 auto uniformInfo = Graphics::UniformInfo{};
931 auto uniformFound = program.GetUniform((*iter).uniformName.GetCString(),
932 (*iter).uniformNameHashNoArray ? (*iter).uniformNameHashNoArray
933 : (*iter).uniformNameHash,
938 auto dst = mUniformBufferBindings[uniformInfo.bufferIndex].offset + uniformInfo.offset;
940 switch((*iter).propertyValue->GetType())
942 case Property::Type::BOOLEAN:
944 ubo.Write(&(*iter).propertyValue->GetBoolean(updateBufferIndex),
946 dst + static_cast<uint32_t>(sizeof(bool)) * arrayIndex);
949 case Property::Type::INTEGER:
951 ubo.Write(&(*iter).propertyValue->GetInteger(updateBufferIndex),
953 dst + static_cast<int32_t>(sizeof(int32_t)) * arrayIndex);
956 case Property::Type::FLOAT:
958 ubo.Write(&(*iter).propertyValue->GetFloat(updateBufferIndex),
960 dst + static_cast<uint32_t>(sizeof(float)) * arrayIndex);
963 case Property::Type::VECTOR2:
965 ubo.Write(&(*iter).propertyValue->GetVector2(updateBufferIndex),
967 dst + static_cast<uint32_t>(sizeof(Vector2)) * arrayIndex);
970 case Property::Type::VECTOR3:
972 ubo.Write(&(*iter).propertyValue->GetVector3(updateBufferIndex),
974 dst + static_cast<uint32_t>(sizeof(Vector3)) * arrayIndex);
977 case Property::Type::VECTOR4:
979 ubo.Write(&(*iter).propertyValue->GetVector4(updateBufferIndex),
981 dst + static_cast<uint32_t>(sizeof(Vector4)) * arrayIndex);
984 case Property::Type::MATRIX:
986 ubo.Write(&(*iter).propertyValue->GetMatrix(updateBufferIndex),
988 dst + static_cast<uint32_t>(sizeof(Matrix)) * arrayIndex);
991 case Property::Type::MATRIX3:
993 // todo: handle data padding properly
996 //const auto& matrix = &(*iter).propertyValue->GetMatrix3(updateBufferIndex);
997 //for(int i = 0; i < 3; ++i)
999 //ubo.Write(&matrix->AsFloat()[i * 3],
1000 // sizeof(float) * 3,
1001 // dst + (i * static_cast<uint32_t>(sizeof(Vector4))));
1004 ubo.Write(&(*iter).propertyValue->GetMatrix3(updateBufferIndex),
1006 dst + static_cast<uint32_t>(sizeof(Matrix3)) * arrayIndex);
1016 // write output bindings
1017 outBindings = &mUniformBufferBindings;
1020 offset = dataOffset;
1023 void Renderer::SetSortAttributes(BufferIndex bufferIndex,
1024 SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const
1026 sortAttributes.shader = &(mRenderDataProvider->GetShader());
1027 sortAttributes.geometry = mGeometry;
1030 void Renderer::SetShaderChanged(bool value)
1032 mShaderChanged = value;
1035 bool Renderer::Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node)
1043 if(mShaderChanged || mUpdateAttributeLocations || mGeometry->AttributesChanged())
1048 for(const auto& texture : mRenderDataProvider->GetTextures())
1050 if(texture && texture->IsNativeImage())
1056 uint64_t hash = 0xc70f6907UL;
1057 const SceneGraph::CollectedUniformMap& uniformMapNode = node->GetUniformMap(bufferIndex);
1058 for(const auto& uniformProperty : uniformMapNode)
1060 hash = uniformProperty.propertyPtr->Hash(bufferIndex, hash);
1063 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
1064 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap(bufferIndex);
1065 for(const auto& uniformProperty : uniformMap)
1067 hash = uniformProperty.propertyPtr->Hash(bufferIndex, hash);
1070 if(mUniformsHash != hash)
1072 mUniformsHash = hash;
1079 Graphics::UniquePtr<Graphics::Pipeline> Renderer::PrepareGraphicsPipeline(
1081 const Dali::Internal::SceneGraph::RenderInstruction& instruction,
1083 Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
1085 Graphics::InputAssemblyState inputAssemblyState{};
1086 Graphics::VertexInputState vertexInputState{};
1087 Graphics::ProgramState programState{};
1088 uint32_t bindingIndex{0u};
1090 if(mUpdateAttributeLocations || mGeometry->AttributesChanged())
1092 mAttributeLocations.Clear();
1093 mUpdateAttributeLocations = true;
1096 auto& reflection = mGraphicsController->GetProgramReflection(program.GetGraphicsProgram());
1102 for(auto&& vertexBuffer : mGeometry->GetVertexBuffers())
1104 const VertexBuffer::Format& vertexFormat = *vertexBuffer->GetFormat();
1106 vertexInputState.bufferBindings.emplace_back(vertexFormat.size, // stride
1107 Graphics::VertexInputRate::PER_VERTEX);
1109 const uint32_t attributeCount = vertexBuffer->GetAttributeCount();
1110 for(uint32_t i = 0; i < attributeCount; ++i)
1112 if(mUpdateAttributeLocations)
1114 auto attributeName = vertexBuffer->GetAttributeName(i);
1115 int32_t pLocation = reflection.GetVertexAttributeLocation(std::string(attributeName.GetStringView()));
1118 DALI_LOG_WARNING("Attribute not found in the shader: %s\n", attributeName.GetCString());
1120 mAttributeLocations.PushBack(pLocation);
1123 uint32_t location = static_cast<uint32_t>(mAttributeLocations[base + i]);
1125 vertexInputState.attributes.emplace_back(location,
1127 vertexFormat.components[i].offset,
1128 GetPropertyVertexFormat(vertexFormat.components[i].type));
1130 base += attributeCount;
1133 mUpdateAttributeLocations = false;
1136 inputAssemblyState.SetTopology(mGeometry->GetTopology());
1139 programState.SetProgram(program.GetGraphicsProgram());
1141 Graphics::RasterizationState rasterizationState{};
1143 //Set cull face mode
1144 const Dali::Internal::SceneGraph::Camera* cam = instruction.GetCamera();
1145 if(cam->GetReflectionUsed())
1147 auto adjFaceCullingMode = mFaceCullingMode;
1148 switch(mFaceCullingMode)
1150 case FaceCullingMode::Type::FRONT:
1152 adjFaceCullingMode = FaceCullingMode::Type::BACK;
1155 case FaceCullingMode::Type::BACK:
1157 adjFaceCullingMode = FaceCullingMode::Type::FRONT;
1162 // nothing to do, leave culling as it is
1165 rasterizationState.SetCullMode(ConvertCullFace(adjFaceCullingMode));
1169 rasterizationState.SetCullMode(ConvertCullFace(mFaceCullingMode));
1172 rasterizationState.SetFrontFace(Graphics::FrontFace::COUNTER_CLOCKWISE);
1177 switch(mGeometry->GetTopology())
1179 case Graphics::PrimitiveTopology::TRIANGLE_LIST:
1180 case Graphics::PrimitiveTopology::TRIANGLE_STRIP:
1181 case Graphics::PrimitiveTopology::TRIANGLE_FAN:
1182 rasterizationState.SetPolygonMode(Graphics::PolygonMode::FILL);
1184 case Graphics::PrimitiveTopology::LINE_LIST:
1185 case Graphics::PrimitiveTopology::LINE_LOOP:
1186 case Graphics::PrimitiveTopology::LINE_STRIP:
1187 rasterizationState.SetPolygonMode(Graphics::PolygonMode::LINE);
1189 case Graphics::PrimitiveTopology::POINT_LIST:
1190 rasterizationState.SetPolygonMode(Graphics::PolygonMode::POINT);
1194 // @todo How to signal a blend barrier is needed?
1195 //if(mBlendingOptions.IsAdvancedBlendEquationApplied() && mPremultipledAlphaEnabled)
1197 // context.BlendBarrier();
1200 Graphics::ColorBlendState colorBlendState{};
1201 colorBlendState.SetBlendEnable(false);
1205 colorBlendState.SetBlendEnable(true);
1207 Graphics::BlendOp rgbOp = ConvertBlendEquation(mBlendingOptions.GetBlendEquationRgb());
1208 Graphics::BlendOp alphaOp = ConvertBlendEquation(mBlendingOptions.GetBlendEquationRgb());
1209 if(mBlendingOptions.IsAdvancedBlendEquationApplied() && mPremultipledAlphaEnabled)
1211 if(rgbOp != alphaOp)
1213 DALI_LOG_ERROR("Advanced Blend Equation MUST be applied by using BlendEquation.\n");
1219 .SetSrcColorBlendFactor(ConvertBlendFactor(mBlendingOptions.GetBlendSrcFactorRgb()))
1220 .SetSrcAlphaBlendFactor(ConvertBlendFactor(mBlendingOptions.GetBlendSrcFactorAlpha()))
1221 .SetDstColorBlendFactor(ConvertBlendFactor(mBlendingOptions.GetBlendDestFactorRgb()))
1222 .SetDstAlphaBlendFactor(ConvertBlendFactor(mBlendingOptions.GetBlendDestFactorAlpha()))
1223 .SetColorBlendOp(rgbOp)
1224 .SetAlphaBlendOp(alphaOp);
1226 // Blend color is optional and rarely used
1227 Vector4* blendColor = const_cast<Vector4*>(mBlendingOptions.GetBlendColor());
1230 colorBlendState.SetBlendConstants(blendColor->AsFloat());
1234 // Take the program into use so we can send uniforms to it
1235 // @todo Remove this call entirely!
1240 // @todo Should instead set framebuffer once through Renderpass, rather than modifying
1241 // pipeline repeatedly.
1242 Graphics::FramebufferState framebufferState{};
1243 if(instruction.mFrameBuffer)
1245 instruction.mFrameBuffer->Bind(); // Ensure graphics object is created.
1246 framebufferState.SetFramebuffer(*instruction.mFrameBuffer->GetGraphicsObject());
1249 // Create a new pipeline
1250 // @todo Passed as pointers - shallow copy will break. Implementation MUST deep copy.
1251 return mGraphicsController->CreatePipeline(
1252 Graphics::PipelineCreateInfo()
1253 .SetInputAssemblyState(&inputAssemblyState)
1254 .SetVertexInputState(&vertexInputState)
1255 .SetRasterizationState(&rasterizationState)
1256 .SetColorBlendState(&colorBlendState)
1257 .SetFramebufferState(&framebufferState)
1258 .SetProgramState(&programState)
1259 .SetNextExtension(&mLegacyProgram),
1260 std::move(oldPipeline));
1263 } // namespace Render
1265 } // namespace Internal