+void Renderer::Upload()
+{
+ mGeometry->Upload(*mGraphicsController);
+}
+
+bool Renderer::Render(Graphics::CommandBuffer& commandBuffer,
+ BufferIndex bufferIndex,
+ const SceneGraph::NodeDataProvider& node,
+ const Matrix& modelMatrix,
+ const Matrix& modelViewMatrix,
+ const Matrix& viewMatrix,
+ const Matrix& projectionMatrix,
+ const Vector3& size,
+ bool blend,
+ Vector<Graphics::Texture*>& boundTextures,
+ const Dali::Internal::SceneGraph::RenderInstruction& instruction,
+ uint32_t queueIndex)
+{
+ // Before doing anything test if the call happens in the right queue
+ if(mDrawCommands.empty() && queueIndex > 0)
+ {
+ return false;
+ }
+
+ // Prepare commands
+ std::vector<DevelRenderer::DrawCommand*> commands;
+ for(auto& cmd : mDrawCommands)
+ {
+ if(cmd.queue == queueIndex)
+ {
+ commands.emplace_back(&cmd);
+ }
+ }
+
+ // Have commands but nothing to be drawn - abort
+ if(!mDrawCommands.empty() && commands.empty())
+ {
+ return false;
+ }
+
+ // Set blending mode
+ if(!mDrawCommands.empty())
+ {
+ blend = (commands[0]->queue != DevelRenderer::RENDER_QUEUE_OPAQUE) && blend;
+ }
+
+ // Create Program
+ ShaderDataPtr shaderData = mRenderDataProvider->GetShader().GetShaderData();
+ const std::vector<char>& vertShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER);
+ const std::vector<char>& fragShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER);
+ Dali::Graphics::Shader& vertexShader = mShaderCache->GetShader(
+ vertShader,
+ Graphics::PipelineStage::VERTEX_SHADER,
+ shaderData->GetSourceMode());
+
+ Dali::Graphics::Shader& fragmentShader = mShaderCache->GetShader(
+ fragShader,
+ Graphics::PipelineStage::FRAGMENT_SHADER,
+ shaderData->GetSourceMode());
+
+ std::vector<Graphics::ShaderState> shaderStates{
+ Graphics::ShaderState()
+ .SetShader(vertexShader)
+ .SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER),
+ Graphics::ShaderState()
+ .SetShader(fragmentShader)
+ .SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER)};
+
+ auto createInfo = Graphics::ProgramCreateInfo();
+ createInfo.SetShaderState(shaderStates);
+
+ auto graphicsProgram = mGraphicsController->CreateProgram(createInfo, nullptr);
+ Program* program = Program::New(*mProgramCache,
+ shaderData,
+ *mGraphicsController,
+ std::move(graphicsProgram));
+
+ if(!program)
+ {
+ DALI_LOG_ERROR("Failed to get program for shader at address %p.\n", reinterpret_cast<void*>(&mRenderDataProvider->GetShader()));
+ return false;
+ }
+
+ // Prepare the graphics pipeline. This may either re-use an existing pipeline or create a new one.
+ auto& pipeline = PrepareGraphicsPipeline(*program, instruction, node, blend);
+
+ commandBuffer.BindPipeline(pipeline);
+
+ BindTextures(commandBuffer, boundTextures);
+
+ BuildUniformIndexMap(bufferIndex, node, size, *program);
+
+ WriteUniformBuffer(bufferIndex, commandBuffer, program, instruction, node, modelMatrix, modelViewMatrix, viewMatrix, projectionMatrix, size);
+
+ bool drawn = false; // Draw can fail if there are no vertex buffers or they haven't been uploaded yet
+ // @todo We should detect this case much earlier to prevent unnecessary work
+
+ if(mDrawCommands.empty())
+ {
+ drawn = mGeometry->Draw(*mGraphicsController, commandBuffer, mIndexedDrawFirstElement, mIndexedDrawElementsCount);
+ }
+ else
+ {
+ for(auto& cmd : commands)
+ {
+ mGeometry->Draw(*mGraphicsController, commandBuffer, cmd->firstIndex, cmd->elementCount);
+ }
+ }
+
+ mUpdated = false;
+ return drawn;
+}
+
+void Renderer::BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program)
+{
+ // Check if the map has changed
+ DALI_ASSERT_DEBUG(mRenderDataProvider && "No Uniform map data provider available");
+
+ const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
+
+ if(uniformMapDataProvider.GetUniformMapChanged(bufferIndex) ||
+ node.GetUniformMapChanged(bufferIndex) ||
+ mUniformIndexMap.Count() == 0 ||
+ mShaderChanged)
+ {
+ // Reset shader pointer
+ mShaderChanged = false;
+
+ const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap(bufferIndex);
+ const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap(bufferIndex);
+
+ auto maxMaps = static_cast<uint32_t>(uniformMap.Count() + uniformMapNode.Count()); // 4,294,967,295 maps should be enough
+ mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
+ mUniformIndexMap.Resize(maxMaps);
+
+ // Copy uniform map into mUniformIndexMap
+ uint32_t mapIndex = 0;
+ for(; mapIndex < uniformMap.Count(); ++mapIndex)
+ {
+ mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex].propertyPtr;
+ mUniformIndexMap[mapIndex].uniformName = uniformMap[mapIndex].uniformName;
+ mUniformIndexMap[mapIndex].uniformNameHash = uniformMap[mapIndex].uniformNameHash;
+ mUniformIndexMap[mapIndex].uniformNameHashNoArray = uniformMap[mapIndex].uniformNameHashNoArray;
+ mUniformIndexMap[mapIndex].arrayIndex = uniformMap[mapIndex].arrayIndex;
+ }
+
+ for(uint32_t nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count(); ++nodeMapIndex)
+ {
+ auto hash = uniformMapNode[nodeMapIndex].uniformNameHash;
+ auto& name = uniformMapNode[nodeMapIndex].uniformName;
+ bool found(false);
+ for(uint32_t i = 0; i < uniformMap.Count(); ++i)
+ {
+ if(mUniformIndexMap[i].uniformNameHash == hash &&
+ mUniformIndexMap[i].uniformName == name)
+ {
+ mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
+ found = true;
+ break;
+ }
+ }
+
+ if(!found)
+ {
+ mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
+ mUniformIndexMap[mapIndex].uniformName = uniformMapNode[nodeMapIndex].uniformName;
+ mUniformIndexMap[mapIndex].uniformNameHash = uniformMapNode[nodeMapIndex].uniformNameHash;
+ mUniformIndexMap[mapIndex].uniformNameHashNoArray = uniformMapNode[nodeMapIndex].uniformNameHashNoArray;
+ mUniformIndexMap[mapIndex].arrayIndex = uniformMapNode[nodeMapIndex].arrayIndex;
+ ++mapIndex;
+ }
+ }
+
+ mUniformIndexMap.Resize(mapIndex);
+ }
+}
+
+void Renderer::WriteUniformBuffer(
+ BufferIndex bufferIndex,
+ Graphics::CommandBuffer& commandBuffer,
+ Program* program,
+ const SceneGraph::RenderInstruction& instruction,
+ const SceneGraph::NodeDataProvider& node,
+ const Matrix& modelMatrix,
+ const Matrix& modelViewMatrix,
+ const Matrix& viewMatrix,
+ const Matrix& projectionMatrix,
+ const Vector3& size)
+{
+ // Create the UBO
+ uint32_t uniformBlockAllocationBytes{0u};
+ uint32_t uniformBlockMaxSize{0u};
+ uint32_t uboOffset{0u};
+
+ auto& reflection = mGraphicsController->GetProgramReflection(program->GetGraphicsProgram());
+ for(auto i = 0u; i < reflection.GetUniformBlockCount(); ++i)
+ {
+ auto blockSize = GetUniformBufferDataAlignment(reflection.GetUniformBlockSize(i));
+ if(uniformBlockMaxSize < blockSize)
+ {
+ uniformBlockMaxSize = blockSize;
+ }
+ uniformBlockAllocationBytes += blockSize;
+ }
+
+ // Create uniform buffer view from uniform buffer
+ Graphics::UniquePtr<Render::UniformBufferView> uboView {nullptr};
+ if(uniformBlockAllocationBytes)
+ {
+ auto uboPoolView = mUniformBufferManager->GetUniformBufferViewPool( bufferIndex );
+
+ uboView = uboPoolView->CreateUniformBufferView( uniformBlockAllocationBytes );
+ }
+
+ // update the uniform buffer
+ // pass shared UBO and offset, return new offset for next item to be used
+ // don't process bindings if there are no uniform buffers allocated
+ if(uboView)
+ {
+ auto uboCount = reflection.GetUniformBlockCount();
+ mUniformBufferBindings.resize(uboCount);
+
+ std::vector<Graphics::UniformBufferBinding>* bindings{&mUniformBufferBindings};
+
+ mUniformBufferBindings[0].buffer = uboView->GetBuffer( &mUniformBufferBindings[0].offset );
+
+ // Write default uniforms
+ WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_MATRIX), *uboView, *bindings, modelMatrix);
+ WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::VIEW_MATRIX), *uboView, *bindings, viewMatrix);
+ WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::PROJECTION_MATRIX), *uboView, *bindings, projectionMatrix);
+ WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_VIEW_MATRIX), *uboView, *bindings, modelViewMatrix);
+
+ auto mvpUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::MVP_MATRIX);
+ if(mvpUniformInfo && !mvpUniformInfo->name.empty())
+ {
+ Matrix modelViewProjectionMatrix(false);
+ Matrix::Multiply(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
+ WriteDefaultUniform(mvpUniformInfo, *uboView, *bindings, modelViewProjectionMatrix);
+ }
+
+ auto normalUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::NORMAL_MATRIX);
+ if(normalUniformInfo && !normalUniformInfo->name.empty())
+ {
+ Matrix3 normalMatrix(modelViewMatrix);
+ normalMatrix.Invert();
+ normalMatrix.Transpose();
+ WriteDefaultUniform(normalUniformInfo, *uboView, *bindings, normalMatrix);
+ }
+
+ Vector4 finalColor;
+ const Vector4& color = node.GetRenderColor(bufferIndex);
+ if(mPremultipliedAlphaEnabled)
+ {
+ float alpha = color.a * mRenderDataProvider->GetOpacity(bufferIndex);
+ finalColor = Vector4(color.r * alpha, color.g * alpha, color.b * alpha, alpha);
+ }
+ else
+ {
+ finalColor = Vector4(color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity(bufferIndex));
+ }
+ WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::COLOR), *uboView, *bindings, finalColor);
+
+ // Write uniforms from the uniform map
+ FillUniformBuffer(*program, instruction, *uboView, bindings, uboOffset, bufferIndex);
+
+ // Write uSize in the end, as it shouldn't be overridable by dynamic properties.
+ WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::SIZE), *uboView, *bindings, size);
+
+ commandBuffer.BindUniformBuffers(*bindings);
+ }
+}
+
+template<class T>
+bool Renderer::WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo, Render::UniformBufferView& ubo, const std::vector<Graphics::UniformBufferBinding>& bindings, const T& data)
+{
+ if(uniformInfo && !uniformInfo->name.empty())
+ {
+ WriteUniform(ubo, bindings, *uniformInfo, data);
+ return true;
+ }
+ return false;
+}
+
+template<class T>
+void Renderer::WriteUniform(Render::UniformBufferView& ubo, const std::vector<Graphics::UniformBufferBinding>& bindings, const Graphics::UniformInfo& uniformInfo, const T& data)
+{
+ WriteUniform(ubo, bindings, uniformInfo, &data, sizeof(T));
+}
+
+void Renderer::WriteUniform(Render::UniformBufferView& ubo, const std::vector<Graphics::UniformBufferBinding>& bindings, const Graphics::UniformInfo& uniformInfo, const void* data, uint32_t size)
+{
+ ubo.Write(data, size, ubo.GetOffset() + uniformInfo.offset);
+}
+
+void Renderer::FillUniformBuffer(Program& program,
+ const SceneGraph::RenderInstruction& instruction,
+ Render::UniformBufferView& ubo,
+ std::vector<Graphics::UniformBufferBinding>*& outBindings,
+ uint32_t& offset,
+ BufferIndex updateBufferIndex)
+{
+ auto& reflection = mGraphicsController->GetProgramReflection(program.GetGraphicsProgram());
+ auto uboCount = reflection.GetUniformBlockCount();
+
+ // Setup bindings
+ uint32_t dataOffset = offset;
+ for(auto i = 0u; i < uboCount; ++i)
+ {
+ mUniformBufferBindings[i].dataSize = reflection.GetUniformBlockSize(i);
+ mUniformBufferBindings[i].binding = reflection.GetUniformBlockBinding(i);
+
+ dataOffset += GetUniformBufferDataAlignment(mUniformBufferBindings[i].dataSize);
+ mUniformBufferBindings[i].buffer = ubo.GetBuffer( &mUniformBufferBindings[i].offset );
+
+ for(UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
+ end = mUniformIndexMap.End();
+ iter != end;
+ ++iter)
+ {
+ // @todo This means parsing the uniform string every frame. Instead, store the array index if present.
+ int arrayIndex = (*iter).arrayIndex;
+
+ auto uniformInfo = Graphics::UniformInfo{};
+ auto uniformFound = program.GetUniform((*iter).uniformName.GetCString(),
+ (*iter).uniformNameHashNoArray ? (*iter).uniformNameHashNoArray
+ : (*iter).uniformNameHash,
+ uniformInfo);
+
+ if(uniformFound)
+ {
+ auto dst = ubo.GetOffset() + uniformInfo.offset;
+ switch((*iter).propertyValue->GetType())
+ {
+ case Property::Type::BOOLEAN:
+ {
+ ubo.Write(&(*iter).propertyValue->GetBoolean(updateBufferIndex),
+ sizeof(bool),
+ dst + static_cast<uint32_t>(sizeof(bool)) * arrayIndex);
+ break;
+ }
+ case Property::Type::INTEGER:
+ {
+ ubo.Write(&(*iter).propertyValue->GetInteger(updateBufferIndex),
+ sizeof(int32_t),
+ dst + static_cast<int32_t>(sizeof(int32_t)) * arrayIndex);
+ break;
+ }
+ case Property::Type::FLOAT:
+ {
+ ubo.Write(&(*iter).propertyValue->GetFloat(updateBufferIndex),
+ sizeof(float),
+ dst + static_cast<uint32_t>(sizeof(float)) * arrayIndex);
+ break;
+ }
+ case Property::Type::VECTOR2:
+ {
+ ubo.Write(&(*iter).propertyValue->GetVector2(updateBufferIndex),
+ sizeof(Vector2),
+ dst + static_cast<uint32_t>(sizeof(Vector2)) * arrayIndex);
+ break;
+ }
+ case Property::Type::VECTOR3:
+ {
+ ubo.Write(&(*iter).propertyValue->GetVector3(updateBufferIndex),
+ sizeof(Vector3),
+ dst + static_cast<uint32_t>(sizeof(Vector3)) * arrayIndex);
+ break;
+ }
+ case Property::Type::VECTOR4:
+ {
+ ubo.Write(&(*iter).propertyValue->GetVector4(updateBufferIndex),
+ sizeof(Vector4),
+ dst + static_cast<uint32_t>(sizeof(Vector4)) * arrayIndex);
+ break;
+ }
+ case Property::Type::MATRIX:
+ {
+ ubo.Write(&(*iter).propertyValue->GetMatrix(updateBufferIndex),
+ sizeof(Matrix),
+ dst + static_cast<uint32_t>(sizeof(Matrix)) * arrayIndex);
+ break;
+ }
+ case Property::Type::MATRIX3:
+ {
+ // todo: handle data padding properly
+ // Vulkan:
+ //
+ //const auto& matrix = &(*iter).propertyValue->GetMatrix3(updateBufferIndex);
+ //for(int i = 0; i < 3; ++i)
+ //{
+ //ubo.Write(&matrix->AsFloat()[i * 3],
+ // sizeof(float) * 3,
+ // dst + (i * static_cast<uint32_t>(sizeof(Vector4))));
+ //}
+ // GL:
+ ubo.Write(&(*iter).propertyValue->GetMatrix3(updateBufferIndex),
+ sizeof(Matrix3),
+ dst + static_cast<uint32_t>(sizeof(Matrix3)) * arrayIndex);
+ break;
+ }
+ default:
+ {
+ }
+ }
+ }
+ }
+ }
+ // write output bindings
+ outBindings = &mUniformBufferBindings;
+
+ // Update offset
+ offset = dataOffset;
+}
+
+void Renderer::SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const
+{
+ sortAttributes.shader = &(mRenderDataProvider->GetShader());
+ sortAttributes.geometry = mGeometry;
+}
+
+void Renderer::SetShaderChanged(bool value)
+{
+ mShaderChanged = value;
+}
+
+bool Renderer::Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node)