| 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 <initializer_list>
9 #include <memory>
10 #include <type_traits>
11 #include <vector>
12
13 #include "impeller/base/strings.h"
14 #include "impeller/core/allocator.h"
15 #include "impeller/core/device_buffer.h"
16 #include "impeller/core/formats.h"
17 #include "impeller/core/host_buffer.h"
18 #include "impeller/core/vertex_buffer.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& host_buffer) {
26 return VertexBuffer{
27 .vertex_buffer =
28 host_buffer.Emplace(input.data(), sizeof(VertexType) * size,
29 alignof(VertexType)), //
30 .vertex_count = size, //
31 .index_type = IndexType::kNone, //
32 };
33 }
34
35 template <class VertexType_, class IndexType_ = uint16_t>
36 class VertexBufferBuilder {
37 public:
38 using VertexType = VertexType_;
39 using IndexType = IndexType_;
40
41 VertexBufferBuilder() = default;
42
43 ~VertexBufferBuilder() = default;
44
45 constexpr impeller::IndexType GetIndexType() const {
46 if (indices_.size() == 0) {
47 return impeller::IndexType::kNone;
48 }
49 if constexpr (sizeof(IndexType) == 2) {
50 return impeller::IndexType::k16bit;
51 }
52 if (sizeof(IndexType) == 4) {
53 return impeller::IndexType::k32bit;
54 }
55 return impeller::IndexType::kUnknown;
56 }
57
58 void SetLabel(const std::string& label) { label_ = label; }
59
60 void Reserve(size_t count) { return vertices_.reserve(count); }
61
62 void ReserveIndices(size_t count) { return indices_.reserve(count); }
63
64 bool HasVertices() const { return !vertices_.empty(); }
65
66 size_t GetVertexCount() const { return vertices_.size(); }
67
68 size_t GetIndexCount() const {
69 return indices_.size() > 0 ? indices_.size() : vertices_.size();
70 }
71
72 const VertexType& Last() const {
73 FML_DCHECK(!vertices_.empty());
74 return vertices_.back();
75 }
76
77 VertexBufferBuilder& AppendVertex(VertexType_ vertex) {
78 vertices_.emplace_back(std::move(vertex));
79 return *this;
80 }
81
82 VertexBufferBuilder& AddVertices(
83 std::initializer_list<VertexType_> vertices) {
84 vertices_.reserve(vertices.size());
85 for (auto& vertex : vertices) {
86 vertices_.emplace_back(std::move(vertex));
87 }
88 return *this;
89 }
90
91 VertexBufferBuilder& AppendIndex(IndexType_ index) {
92 indices_.emplace_back(index);
93 return *this;
94 }
95
96 VertexBuffer CreateVertexBuffer(HostBuffer& host_buffer) const {
97 VertexBuffer buffer;
98 buffer.vertex_buffer = CreateVertexBufferView(host_buffer);
99 buffer.index_buffer = CreateIndexBufferView(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& buffer) const {
127 return 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(SPrintF("%s Vertices", label_.c_str()));
141 }
142 return DeviceBuffer::AsBufferView(buffer);
143 }
144
145 std::vector<IndexType> CreateIndexBuffer() const { return indices_; }
146
147 BufferView CreateIndexBufferView(HostBuffer& buffer) const {
148 const auto index_buffer = CreateIndexBuffer();
149 if (index_buffer.size() == 0) {
150 return {};
151 }
152 return 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(SPrintF("%s Indices", label_.c_str()));
170 }
171 return DeviceBuffer::AsBufferView(buffer);
172 }
173 };
174
175 } // namespace impeller
176
177 #endif // FLUTTER_IMPELLER_RENDERER_VERTEX_BUFFER_BUILDER_H_
impeller::DeviceBuffer::AsBufferView
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
Definition: device_buffer.cc:18
impeller::VertexBuffer::index_type
IndexType index_type
Definition: vertex_buffer.h:29
host_buffer.h
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:95
impeller::IndexType::k16bit
@ k16bit
vertex_buffer.h
impeller::VertexBufferBuilder::GetVertexCount
size_t GetVertexCount() const
Definition: vertex_buffer_builder.h:66
device_buffer.h
impeller::VertexBufferBuilder::VertexType
VertexType_ VertexType
Definition: vertex_buffer_builder.h:38
impeller::HostBuffer
Definition: host_buffer.h:28
impeller::VertexBufferBuilder::~VertexBufferBuilder
~VertexBufferBuilder()=default
impeller::VertexBuffer
Definition: vertex_buffer.h:13
formats.h
impeller::VertexBufferBuilder::AddVertices
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
Definition: vertex_buffer_builder.h:82
impeller::IndexType::k32bit
@ k32bit
impeller::VertexBuffer::vertex_buffer
BufferView vertex_buffer
Definition: vertex_buffer.h:14
impeller::VertexBuffer::vertex_count
size_t vertex_count
Definition: vertex_buffer.h:23
impeller::VertexBufferBuilder::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(Allocator &device_allocator) const
Definition: vertex_buffer_builder.h:105
impeller::VertexBufferBuilder::VertexBufferBuilder
VertexBufferBuilder()=default
impeller::VertexBufferBuilder::HasVertices
bool HasVertices() const
Definition: vertex_buffer_builder.h:64
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &host_buffer)
Create an index-less vertex buffer from a fixed size array.
Definition: vertex_buffer_builder.h:24
impeller::VertexBufferBuilder
Definition: vertex_buffer_builder.h:36
impeller::IndexType
IndexType
Definition: formats.h:343
impeller::VertexBufferBuilder::GetIndexCount
size_t GetIndexCount() const
Definition: vertex_buffer_builder.h:68
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:23
strings.h
impeller::VertexBufferBuilder::AppendIndex
VertexBufferBuilder & AppendIndex(IndexType_ index)
Definition: vertex_buffer_builder.h:91
impeller::VertexBufferBuilder::SetLabel
void SetLabel(const std::string &label)
Definition: vertex_buffer_builder.h:58
impeller::VertexBufferBuilder::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
Definition: vertex_buffer_builder.h:96
impeller::VertexBufferBuilder::IterateVertices
void IterateVertices(const std::function< void(VertexType &)> &iterator)
Definition: vertex_buffer_builder.h:115
impeller::VertexBufferBuilder::ReserveIndices
void ReserveIndices(size_t count)
Definition: vertex_buffer_builder.h:62
impeller::VertexBuffer::index_buffer
BufferView index_buffer
The index buffer binding used by the vertex shader stage.
Definition: vertex_buffer.h:18
allocator.h
impeller::VertexBufferBuilder::IndexType
IndexType_ IndexType
Definition: vertex_buffer_builder.h:39
impeller::BufferView
Definition: buffer_view.h:15
impeller::VertexBufferBuilder::GetIndexType
constexpr impeller::IndexType GetIndexType() const
Definition: vertex_buffer_builder.h:45
impeller::VertexBufferBuilder::AppendVertex
VertexBufferBuilder & AppendVertex(VertexType_ vertex)
Definition: vertex_buffer_builder.h:77
impeller::VertexBufferBuilder::Reserve
void Reserve(size_t count)
Definition: vertex_buffer_builder.h:60
impeller::HostBuffer::SetLabel
void SetLabel(std::string label)
Definition: host_buffer.cc:40
impeller
Definition: allocation.cc:12
impeller::IndexType::kUnknown
@ kUnknown
impeller::VertexBufferBuilder::Last
const VertexType & Last() const
Definition: vertex_buffer_builder.h:72