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/gl-resources/gpu-buffer.h>
23 #include <dali/internal/render/renderers/render-vertex-buffer.h>
24 #include <dali/internal/render/shaders/program.h>
34 mIndexBuffer(nullptr),
35 mGeometryType(Dali::Geometry::TRIANGLES),
36 mIndicesChanged(false),
37 mHasBeenUpdated(false),
38 mAttributesChanged(true)
42 Geometry::~Geometry() = default;
44 void Geometry::AddVertexBuffer(Render::VertexBuffer* vertexBuffer)
46 mVertexBuffers.PushBack(vertexBuffer);
47 mAttributesChanged = true;
50 const Vector<Render::VertexBuffer*>& Geometry::GetVertexBuffers() const
52 return mVertexBuffers;
55 void Geometry::SetIndexBuffer(Dali::Vector<uint16_t>& indices)
57 mIndices.Swap(indices);
58 mIndicesChanged = true;
61 void Geometry::RemoveVertexBuffer(const Render::VertexBuffer* vertexBuffer)
63 const auto&& end = mVertexBuffers.End();
64 for(auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter)
66 if(*iter == vertexBuffer)
68 //This will delete the gpu buffer associated to the RenderVertexBuffer if there is one
69 mVertexBuffers.Remove(iter);
70 mAttributesChanged = true;
76 void Geometry::OnRenderFinished()
78 mHasBeenUpdated = false;
79 mAttributesChanged = false;
82 void Geometry::Upload(Graphics::Controller& graphicsController)
91 mIndexBuffer = nullptr;
95 if(mIndexBuffer == nullptr)
97 mIndexBuffer = new GpuBuffer(graphicsController, 0 | Graphics::BufferUsage::INDEX_BUFFER);
100 uint32_t bufferSize = static_cast<uint32_t>(sizeof(uint16_t) * mIndices.Size());
101 mIndexBuffer->UpdateDataBuffer(graphicsController, bufferSize, &mIndices[0]);
104 mIndicesChanged = false;
107 for(auto&& buffer : mVertexBuffers)
109 if(!buffer->Update(graphicsController))
111 //Vertex buffer is not ready ( Size, data or format has not been specified yet )
116 mHasBeenUpdated = true;
121 Graphics::Controller& graphicsController,
122 Graphics::CommandBuffer& commandBuffer,
123 uint32_t elementBufferOffset,
124 uint32_t elementBufferCount)
126 //Bind buffers to attribute locations
127 const uint32_t vertexBufferCount = static_cast<uint32_t>(mVertexBuffers.Count());
129 std::vector<const Graphics::Buffer*> buffers;
130 std::vector<uint32_t> offsets;
132 for(uint32_t i = 0; i < vertexBufferCount; ++i)
134 const GpuBuffer* gpuBuffer = mVertexBuffers[i]->GetGpuBuffer();
137 const Graphics::Buffer* buffer = gpuBuffer->GetGraphicsObject();
141 buffers.push_back(buffer);
142 offsets.push_back(0u);
145 //@todo Figure out why this is being drawn without geometry having been uploaded
147 if(buffers.size() == 0)
152 commandBuffer.BindVertexBuffers(0, buffers, offsets);
154 uint32_t numIndices(0u);
155 intptr_t firstIndexOffset(0u);
158 numIndices = static_cast<uint32_t>(mIndices.Size());
160 if(elementBufferOffset != 0u)
162 elementBufferOffset = (elementBufferOffset >= numIndices) ? numIndices - 1 : elementBufferOffset;
163 firstIndexOffset = elementBufferOffset * sizeof(GLushort);
164 numIndices -= elementBufferOffset;
167 if(elementBufferCount != 0u)
169 numIndices = std::min(elementBufferCount, numIndices);
174 if(mIndexBuffer && mGeometryType != Dali::Geometry::POINTS)
177 const Graphics::Buffer* ibo = mIndexBuffer->GetGraphicsObject();
180 commandBuffer.BindIndexBuffer(*ibo, 0, Graphics::Format::R16_UINT);
183 commandBuffer.DrawIndexed(numIndices, 1, firstIndexOffset, 0, 0);
188 GLsizei numVertices(0u);
189 if(vertexBufferCount > 0)
191 // truncated, no value loss happening in practice
192 numVertices = static_cast<GLsizei>(mVertexBuffers[0]->GetElementCount());
195 commandBuffer.Draw(numVertices, 1, 0, 0);
200 Graphics::PrimitiveTopology Geometry::GetTopology() const
202 Graphics::PrimitiveTopology topology = Graphics::PrimitiveTopology::TRIANGLE_LIST;
204 switch(mGeometryType)
206 case Dali::Geometry::TRIANGLES:
208 topology = Graphics::PrimitiveTopology::TRIANGLE_LIST;
211 case Dali::Geometry::LINES:
213 topology = Graphics::PrimitiveTopology::LINE_LIST;
216 case Dali::Geometry::POINTS:
218 topology = Graphics::PrimitiveTopology::POINT_LIST;
221 case Dali::Geometry::TRIANGLE_STRIP:
223 topology = Graphics::PrimitiveTopology::TRIANGLE_STRIP;
226 case Dali::Geometry::TRIANGLE_FAN:
228 topology = Graphics::PrimitiveTopology::TRIANGLE_FAN;
231 case Dali::Geometry::LINE_LOOP:
233 topology = Graphics::PrimitiveTopology::LINE_LOOP;
236 case Dali::Geometry::LINE_STRIP:
238 topology = Graphics::PrimitiveTopology::LINE_STRIP;
245 } // namespace Render
246 } // namespace Internal