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.
18 #include <dali/internal/render/renderers/render-geometry.h>
21 #include <dali/internal/common/buffer-index.h>
22 #include <dali/internal/render/renderers/render-vertex-buffer.h>
23 #include <dali/internal/render/shaders/program.h>
33 mIndexBuffer(nullptr),
34 mGeometryType(Dali::Geometry::TRIANGLES),
35 mIndicesChanged(false),
36 mHasBeenUpdated(false),
37 mAttributesChanged(true)
41 Geometry::~Geometry() = default;
43 void Geometry::AddVertexBuffer(Render::VertexBuffer* vertexBuffer)
45 mVertexBuffers.PushBack(vertexBuffer);
46 mAttributesChanged = true;
49 const Vector<Render::VertexBuffer*>& Geometry::GetVertexBuffers() const
51 return mVertexBuffers;
54 void Geometry::SetIndexBuffer(Dali::Vector<uint16_t>& indices)
56 mIndices.Swap(indices);
57 mIndicesChanged = true;
60 void Geometry::RemoveVertexBuffer(const Render::VertexBuffer* vertexBuffer)
62 const auto&& end = mVertexBuffers.End();
63 for(auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter)
65 if(*iter == vertexBuffer)
67 //This will delete the gpu buffer associated to the RenderVertexBuffer if there is one
68 mVertexBuffers.Remove(iter);
69 mAttributesChanged = true;
75 void Geometry::OnRenderFinished()
77 mHasBeenUpdated = false;
78 mAttributesChanged = false;
81 void Geometry::Upload(Graphics::Controller& graphicsController)
90 mIndexBuffer = nullptr;
94 if(mIndexBuffer == nullptr)
96 mIndexBuffer = new GpuBuffer(graphicsController, 0 | Graphics::BufferUsage::INDEX_BUFFER);
99 uint32_t bufferSize = static_cast<uint32_t>(sizeof(uint16_t) * mIndices.Size());
100 mIndexBuffer->UpdateDataBuffer(graphicsController, bufferSize, &mIndices[0]);
103 mIndicesChanged = false;
106 for(auto&& buffer : mVertexBuffers)
108 if(!buffer->Update(graphicsController))
110 //Vertex buffer is not ready ( Size, data or format has not been specified yet )
115 mHasBeenUpdated = true;
120 Graphics::Controller& graphicsController,
121 Graphics::CommandBuffer& commandBuffer,
122 uint32_t elementBufferOffset,
123 uint32_t elementBufferCount)
125 //Bind buffers to attribute locations
126 const auto vertexBufferCount = static_cast<uint32_t>(mVertexBuffers.Count());
128 std::vector<const Graphics::Buffer*> buffers;
129 std::vector<uint32_t> offsets;
131 for(uint32_t i = 0; i < vertexBufferCount; ++i)
133 const GpuBuffer* gpuBuffer = mVertexBuffers[i]->GetGpuBuffer();
136 const Graphics::Buffer* buffer = gpuBuffer->GetGraphicsObject();
140 buffers.push_back(buffer);
141 offsets.push_back(0u);
144 //@todo Figure out why this is being drawn without geometry having been uploaded
151 commandBuffer.BindVertexBuffers(0, buffers, offsets);
153 uint32_t numIndices(0u);
154 intptr_t firstIndexOffset(0u);
157 numIndices = static_cast<uint32_t>(mIndices.Size());
159 if(elementBufferOffset != 0u)
161 elementBufferOffset = (elementBufferOffset >= numIndices) ? numIndices - 1 : elementBufferOffset;
162 firstIndexOffset = intptr_t(elementBufferOffset * sizeof(uint16_t));
163 numIndices -= elementBufferOffset;
166 if(elementBufferCount != 0u)
168 numIndices = std::min(elementBufferCount, numIndices);
173 if(mIndexBuffer && mGeometryType != Dali::Geometry::POINTS)
176 const Graphics::Buffer* ibo = mIndexBuffer->GetGraphicsObject();
179 commandBuffer.BindIndexBuffer(*ibo, 0, Graphics::Format::R16_UINT);
182 commandBuffer.DrawIndexed(numIndices, 1, firstIndexOffset, 0, 0);
186 // Un-indexed draw call
187 uint32_t numVertices(0u);
188 if(vertexBufferCount > 0)
190 // truncated, no value loss happening in practice
191 numVertices = static_cast<uint32_t>(mVertexBuffers[0]->GetElementCount());
194 commandBuffer.Draw(numVertices, 1, 0, 0);
199 Graphics::PrimitiveTopology Geometry::GetTopology() const
201 Graphics::PrimitiveTopology topology = Graphics::PrimitiveTopology::TRIANGLE_LIST;
203 switch(mGeometryType)
205 case Dali::Geometry::TRIANGLES:
207 topology = Graphics::PrimitiveTopology::TRIANGLE_LIST;
210 case Dali::Geometry::LINES:
212 topology = Graphics::PrimitiveTopology::LINE_LIST;
215 case Dali::Geometry::POINTS:
217 topology = Graphics::PrimitiveTopology::POINT_LIST;
220 case Dali::Geometry::TRIANGLE_STRIP:
222 topology = Graphics::PrimitiveTopology::TRIANGLE_STRIP;
225 case Dali::Geometry::TRIANGLE_FAN:
227 topology = Graphics::PrimitiveTopology::TRIANGLE_FAN;
230 case Dali::Geometry::LINE_LOOP:
232 topology = Graphics::PrimitiveTopology::LINE_LOOP;
235 case Dali::Geometry::LINE_STRIP:
237 topology = Graphics::PrimitiveTopology::LINE_STRIP;
244 } // namespace Render
245 } // namespace Internal