Flutter Engine
 
Loading...
Searching...
No Matches
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
19
20namespace impeller {
21
22/// @brief Create an index-less vertex buffer from a fixed size array.
23template <class VertexType, size_t size>
24VertexBuffer 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
34template <class VertexType_, class IndexType_ = uint16_t>
36 public:
37 using VertexType = VertexType_;
38 using IndexType = IndexType_;
39
41
43
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
VertexBufferBuilder & AppendVertex(VertexType_ vertex)
const VertexType & Last() const
VertexBuffer CreateVertexBuffer(HostBuffer &data_host_buffer, HostBuffer &indexes_host_buffer) const
void IterateVertices(const std::function< void(VertexType &)> &iterator)
VertexBuffer CreateVertexBuffer(Allocator &device_allocator) const
VertexBufferBuilder & AppendIndex(IndexType_ index)
void SetLabel(const std::string &label)
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
constexpr impeller::IndexType GetIndexType() const
static int input(yyscan_t yyscanner)
#define FML_DCHECK(condition)
Definition logging.h:122
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Definition switch_defs.h:98
@ 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.