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.
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 inline constexpr size_t GetSizeOfIndexFromIndexType(Dali::Graphics::Format graphicsFormat)
35 switch(graphicsFormat)
37 case Dali::Graphics::Format::R16_UINT:
39 return sizeof(uint16_t);
41 case Dali::Graphics::Format::R32_UINT:
43 return sizeof(uint32_t);
47 // TODO : Not implmeneted.
48 return sizeof(uint16_t);
52 } // unnamed namespace
55 mIndexBuffer(nullptr),
56 mIndexType(Dali::Graphics::Format::R16_UINT),
57 mGeometryType(Dali::Geometry::TRIANGLES),
58 mIndicesChanged(false),
59 mHasBeenUpdated(false),
60 mAttributesChanged(true)
64 Geometry::~Geometry() = default;
66 void Geometry::AddVertexBuffer(Render::VertexBuffer* vertexBuffer)
68 mVertexBuffers.PushBack(vertexBuffer);
69 mAttributesChanged = true;
72 const Vector<Render::VertexBuffer*>& Geometry::GetVertexBuffers() const
74 return mVertexBuffers;
77 void Geometry::SetIndexBuffer(Uint16ContainerType& indices)
79 mIndices.Swap(indices);
80 mIndicesChanged = true;
81 mIndexType = Graphics::Format::R16_UINT;
84 void Geometry::SetIndexBuffer(Uint32ContainerType& indices)
86 // mIndices type is not matched with indices. Copy memory hardly.
87 mIndices.ResizeUninitialized(indices.Count() * 2);
88 memcpy(mIndices.Begin(), indices.Begin(), indices.Count() * sizeof(uint32_t));
89 mIndicesChanged = true;
90 mIndexType = Graphics::Format::R32_UINT;
93 void Geometry::RemoveVertexBuffer(const Render::VertexBuffer* vertexBuffer)
95 const auto&& end = mVertexBuffers.End();
96 for(auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter)
98 if(*iter == vertexBuffer)
100 //This will delete the gpu buffer associated to the RenderVertexBuffer if there is one
101 mVertexBuffers.Remove(iter);
102 mAttributesChanged = true;
108 void Geometry::OnRenderFinished()
110 mHasBeenUpdated = false;
111 mAttributesChanged = false;
114 void Geometry::Upload(Graphics::Controller& graphicsController)
123 mIndexBuffer = nullptr;
127 if(mIndexBuffer == nullptr)
129 mIndexBuffer = new GpuBuffer(graphicsController, 0 | Graphics::BufferUsage::INDEX_BUFFER);
132 uint32_t bufferSize = static_cast<uint32_t>(sizeof(uint16_t) * mIndices.Size());
133 mIndexBuffer->UpdateDataBuffer(graphicsController, bufferSize, &mIndices[0]);
136 mIndicesChanged = false;
139 for(auto&& buffer : mVertexBuffers)
141 if(!buffer->Update(graphicsController))
143 //Vertex buffer is not ready ( Size, data or format has not been specified yet )
148 mHasBeenUpdated = true;
152 bool Geometry::BindVertexAttributes(Graphics::CommandBuffer& commandBuffer)
154 //Bind buffers to attribute locations
155 const auto vertexBufferCount = static_cast<uint32_t>(mVertexBuffers.Count());
157 std::vector<const Graphics::Buffer*> buffers;
158 std::vector<uint32_t> offsets;
160 for(uint32_t i = 0; i < vertexBufferCount; ++i)
162 const GpuBuffer* gpuBuffer = mVertexBuffers[i]->GetGpuBuffer();
165 const Graphics::Buffer* buffer = gpuBuffer->GetGraphicsObject();
169 buffers.push_back(buffer);
170 offsets.push_back(0u);
173 //@todo Figure out why this is being drawn without geometry having been uploaded
180 commandBuffer.BindVertexBuffers(0, buffers, offsets);
186 Graphics::Controller& graphicsController,
187 Graphics::CommandBuffer& commandBuffer,
188 uint32_t elementBufferOffset,
189 uint32_t elementBufferCount)
191 uint32_t numIndices(0u);
192 intptr_t firstIndexOffset(0u);
195 std::size_t sizeOfIndex = GetSizeOfIndexFromIndexType(mIndexType);
197 numIndices = static_cast<uint32_t>(mIndices.Size() * sizeof(uint16_t) / sizeOfIndex);
199 if(elementBufferOffset != 0u)
201 elementBufferOffset = (elementBufferOffset >= numIndices) ? numIndices - 1 : elementBufferOffset;
202 firstIndexOffset = intptr_t(elementBufferOffset * sizeOfIndex);
203 numIndices -= elementBufferOffset;
206 if(elementBufferCount != 0u)
208 numIndices = std::min(elementBufferCount, numIndices);
213 if(mIndexBuffer && mGeometryType != Dali::Geometry::POINTS)
216 const Graphics::Buffer* ibo = mIndexBuffer->GetGraphicsObject();
219 commandBuffer.BindIndexBuffer(*ibo, 0, mIndexType);
222 commandBuffer.DrawIndexed(numIndices, 1, firstIndexOffset, 0, 0);
226 // Un-indexed draw call
227 uint32_t numVertices(0u);
229 if(mVertexBuffers.Count() > 0)
231 // truncated, no value loss happening in practice
232 numVertices = static_cast<uint32_t>(mVertexBuffers[0]->GetElementCount());
235 commandBuffer.Draw(numVertices, 1, 0, 0);
240 Graphics::PrimitiveTopology Geometry::GetTopology() const
242 Graphics::PrimitiveTopology topology = Graphics::PrimitiveTopology::TRIANGLE_LIST;
244 switch(mGeometryType)
246 case Dali::Geometry::TRIANGLES:
248 topology = Graphics::PrimitiveTopology::TRIANGLE_LIST;
251 case Dali::Geometry::LINES:
253 topology = Graphics::PrimitiveTopology::LINE_LIST;
256 case Dali::Geometry::POINTS:
258 topology = Graphics::PrimitiveTopology::POINT_LIST;
261 case Dali::Geometry::TRIANGLE_STRIP:
263 topology = Graphics::PrimitiveTopology::TRIANGLE_STRIP;
266 case Dali::Geometry::TRIANGLE_FAN:
268 topology = Graphics::PrimitiveTopology::TRIANGLE_FAN;
271 case Dali::Geometry::LINE_LOOP:
273 topology = Graphics::PrimitiveTopology::LINE_LOOP;
276 case Dali::Geometry::LINE_STRIP:
278 topology = Graphics::PrimitiveTopology::LINE_STRIP;
285 } // namespace Render
286 } // namespace Internal