2f53a405e011f250895e588137c61ed70bde757b
[platform/core/uifw/dali-core.git] / dali / internal / update / graphics / graphics-algorithms.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/graphics/graphics-algorithms.h>
20
21 // EXTERNAL INCLUDES
22
23 #include <dali/graphics-api/graphics-api-controller.h>
24 #include <dali/graphics-api/graphics-api-frame.h>
25 #include <dali/graphics-api/graphics-api-render-command.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/internal/common/buffer-index.h>
29 #include <dali/internal/render/common/render-instruction-container.h>
30 #include <dali/internal/render/common/render-instruction.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace SceneGraph
37 {
38 namespace
39 {
40 void SubmitRenderItemList( Graphics::API::Controller&           graphics,
41                            Graphics::API::Frame&                frame,
42                            BufferIndex                          bufferIndex,
43                            Graphics::API::RenderCommandBuilder& commandBuilder,
44                            Matrix                               viewProjection,
45                            const RenderList&                    renderItemList )
46 {
47   // TODO: @todo Set shaders and other properties
48   //commandBuilder.Set( );
49
50   // TODO: @todo Clipping...
51
52   auto numberOfRenderItems = renderItemList.Count();
53
54   using DataT = struct
55   {
56     Matrix  world;
57     Vector4 color;
58     Vector3 size;
59   } __attribute__((aligned(16)));
60
61   auto uniformBuffer = graphics.CreateBuffer<DataT>( numberOfRenderItems );
62   auto data = uniformBuffer->GetData();
63   for( auto i = 0u; i < numberOfRenderItems; ++i )
64   {
65     auto& item = renderItemList.GetItem( i );
66     Matrix::Multiply( data[i].world, item.mModelMatrix, viewProjection );
67     data[i].color = item.mNode->GetWorldColor( bufferIndex );
68     data[i].size  = item.mSize;
69   }
70   commandBuilder.Set( Graphics::API::PrimitiveCount{numberOfRenderItems} );
71
72   auto buffers = std::vector<Graphics::API::BufferInfo>{};
73   buffers.emplace_back(std::move(uniformBuffer));
74
75   commandBuilder.Set( Graphics::API::BufferList{std::move(buffers)} );
76   auto cmd = commandBuilder.Build();
77   graphics.SubmitCommand( std::move(cmd) );
78 }
79
80 void SubmitInstruction( Graphics::API::Controller& graphics,
81                         Graphics::API::Frame&      frame,
82                         BufferIndex                bufferIndex,
83                         RenderInstruction&         instruction )
84 {
85   using namespace Graphics::API;
86
87   // Create constant buffer with static uniforms: view matrix, progrjection matrix
88
89   // TODO: @todo: buffer for constant uniforms
90   /*
91   auto contantUniforms = BuildBuffer<ProjectionMatrix, ViewMatrix>();
92   constantUniforms["uProjection"] = projectionMatrix;
93   constantUniforms["uViewMatrix"] = viewMatrix;
94   */
95
96   const Matrix* viewMatrix       = instruction.GetViewMatrix( bufferIndex );
97   const Matrix* projectionMatrix = instruction.GetProjectionMatrix( bufferIndex );
98   Matrix        viewProjection;
99   Matrix::Multiply( viewProjection, *viewMatrix, *projectionMatrix );
100
101   auto commandBuilder = RenderCommandBuilder{};
102
103   auto numberOfRenderLists = instruction.RenderListCount();
104   for( auto i = 0u; i < numberOfRenderLists; ++i )
105   {
106     SubmitRenderItemList(
107       graphics, frame, bufferIndex, commandBuilder, viewProjection, *instruction.GetRenderList( i ) );
108   }
109 }
110 } // namespace
111
112 void SubmitRenderInstructions( Graphics::API::Controller&  graphics,
113                                RenderInstructionContainer& renderInstructions,
114                                BufferIndex                 bufferIndex )
115 {
116   auto frame = Graphics::API::Frame{};
117   graphics.BeginFrame();
118
119   auto numberOfInstructions = renderInstructions.Count( bufferIndex );
120   for( size_t i = 0; i < numberOfInstructions; ++i )
121   {
122     RenderInstruction& instruction = renderInstructions.At( bufferIndex, i );
123
124     SubmitInstruction( graphics, frame, bufferIndex, instruction );
125   }
126
127   graphics.EndFrame( );
128 }
129
130 } // namespace SceneGraph
131 } // namespace Internal
132 } // namespace Dali