Flutter Impeller
vertex_buffer_builder.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_RENDERER_VERTEX_BUFFER_BUILDER_H_
6 #define FLUTTER_IMPELLER_RENDERER_VERTEX_BUFFER_BUILDER_H_
7 
8 #include <format>
9 #include <initializer_list>
10 #include <memory>
11 #include <type_traits>
12 #include <vector>
13 
16 #include "impeller/core/formats.h"
19 
20 namespace impeller {
21 
22 /// @brief Create an index-less vertex buffer from a fixed size array.
23 template <class VertexType, size_t size>
24 VertexBuffer CreateVertexBuffer(std::array<VertexType, size> input,
25  HostBuffer& data_host_buffer) {
26  return VertexBuffer{
27  .vertex_buffer = data_host_buffer.Emplace(
28  input.data(), sizeof(VertexType) * size, alignof(VertexType)),
29  .vertex_count = size,
30  .index_type = IndexType::kNone,
31  };
32 }
33 
34 template <class VertexType_, class IndexType_ = uint16_t>
36  public:
37  using VertexType = VertexType_;
38  using IndexType = IndexType_;
39 
40  VertexBufferBuilder() = default;
41 
42  ~VertexBufferBuilder() = default;
43 
44  constexpr impeller::IndexType GetIndexType() const {
45  if (indices_.size() == 0) {
47  }
48  if constexpr (sizeof(IndexType) == 2) {
50  }
51  if (sizeof(IndexType) == 4) {
53  }
55  }
56 
57  void SetLabel(const std::string& label) { label_ = label; }
58 
59  void Reserve(size_t count) { return vertices_.reserve(count); }
60 
61  void ReserveIndices(size_t count) { return indices_.reserve(count); }
62 
63  bool HasVertices() const { return !vertices_.empty(); }
64 
65  size_t GetVertexCount() const { return vertices_.size(); }
66 
67  size_t GetIndexCount() const {
68  return indices_.size() > 0 ? indices_.size() : vertices_.size();
69  }
70 
71  const VertexType& Last() const {
72  FML_DCHECK(!vertices_.empty());
73  return vertices_.back();
74  }
75 
76  VertexBufferBuilder& AppendVertex(VertexType_ vertex) {
77  vertices_.emplace_back(std::move(vertex));
78  return *this;
79  }
80 
82  std::initializer_list<VertexType_> vertices) {
83  vertices_.reserve(vertices.size());
84  for (auto& vertex : vertices) {
85  vertices_.emplace_back(std::move(vertex));
86  }
87  return *this;
88  }
89 
90  VertexBufferBuilder& AppendIndex(IndexType_ index) {
91  indices_.emplace_back(index);
92  return *this;
93  }
94 
96  HostBuffer& indexes_host_buffer) const {
97  VertexBuffer buffer;
98  buffer.vertex_buffer = CreateVertexBufferView(data_host_buffer);
99  buffer.index_buffer = CreateIndexBufferView(indexes_host_buffer);
100  buffer.vertex_count = GetIndexCount();
101  buffer.index_type = GetIndexType();
102  return buffer;
103  };
104 
105  VertexBuffer CreateVertexBuffer(Allocator& device_allocator) const {
106  VertexBuffer buffer;
107  // This can be merged into a single allocation.
108  buffer.vertex_buffer = CreateVertexBufferView(device_allocator);
109  buffer.index_buffer = CreateIndexBufferView(device_allocator);
110  buffer.vertex_count = GetIndexCount();
111  buffer.index_type = GetIndexType();
112  return buffer;
113  };
114 
115  void IterateVertices(const std::function<void(VertexType&)>& iterator) {
116  for (auto& vertex : vertices_) {
117  iterator(vertex);
118  }
119  }
120 
121  private:
122  std::vector<VertexType> vertices_;
123  std::vector<IndexType> indices_;
124  std::string label_;
125 
126  BufferView CreateVertexBufferView(HostBuffer& data_host_buffer) const {
127  return data_host_buffer.Emplace(vertices_.data(),
128  vertices_.size() * sizeof(VertexType),
129  alignof(VertexType));
130  }
131 
132  BufferView CreateVertexBufferView(Allocator& allocator) const {
133  auto buffer = allocator.CreateBufferWithCopy(
134  reinterpret_cast<const uint8_t*>(vertices_.data()),
135  vertices_.size() * sizeof(VertexType));
136  if (!buffer) {
137  return {};
138  }
139  if (!label_.empty()) {
140  buffer->SetLabel(std::format("{} Vertices", label_));
141  }
142  return DeviceBuffer::AsBufferView(std::move(buffer));
143  }
144 
145  std::vector<IndexType> CreateIndexBuffer() const { return indices_; }
146 
147  BufferView CreateIndexBufferView(HostBuffer& indexes_host_buffer) const {
148  const auto index_buffer = CreateIndexBuffer();
149  if (index_buffer.size() == 0) {
150  return {};
151  }
152  return indexes_host_buffer.Emplace(index_buffer.data(),
153  index_buffer.size() * sizeof(IndexType),
154  alignof(IndexType));
155  }
156 
157  BufferView CreateIndexBufferView(Allocator& allocator) const {
158  const auto index_buffer = CreateIndexBuffer();
159  if (index_buffer.size() == 0) {
160  return {};
161  }
162  auto buffer = allocator.CreateBufferWithCopy(
163  reinterpret_cast<const uint8_t*>(index_buffer.data()),
164  index_buffer.size() * sizeof(IndexType));
165  if (!buffer) {
166  return {};
167  }
168  if (!label_.empty()) {
169  buffer->SetLabel(std::format("{} Indices", label_));
170  }
171  return DeviceBuffer::AsBufferView(std::move(buffer));
172  }
173 };
174 
175 } // namespace impeller
176 
177 #endif // FLUTTER_IMPELLER_RENDERER_VERTEX_BUFFER_BUILDER_H_
An object that allocates device memory.
Definition: allocator.h:24
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:92
const VertexType & Last() const
VertexBuffer CreateVertexBuffer(HostBuffer &data_host_buffer, HostBuffer &indexes_host_buffer) const
void IterateVertices(const std::function< void(VertexType &)> &iterator)
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
VertexBuffer CreateVertexBuffer(Allocator &device_allocator) const
VertexBufferBuilder & AppendVertex(VertexType_ vertex)
void SetLabel(const std::string &label)
constexpr impeller::IndexType GetIndexType() const
VertexBufferBuilder & AppendIndex(IndexType_ index)
@ kNone
Does not use the index buffer.
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &data_host_buffer)
Create an index-less vertex buffer from a fixed size array.
BufferView index_buffer
The index buffer binding used by the vertex shader stage.
Definition: vertex_buffer.h:20