| 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 <map>
10 #include <vector>
11
12 #include "flutter/fml/macros.h"
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 #include "impeller/geometry/vector.h"
20
21 namespace impeller {
22
23 template <class VertexType_, class IndexType_ = uint16_t>
24 class VertexBufferBuilder {
25 public:
26 using VertexType = VertexType_;
27 using IndexType = IndexType_;
28
29 VertexBufferBuilder() = default;
30
31 ~VertexBufferBuilder() = default;
32
33 constexpr impeller::IndexType GetIndexType() const {
34 if (indices_.size() == 0) {
35 return impeller::IndexType::kNone;
36 }
37 if constexpr (sizeof(IndexType) == 2) {
38 return impeller::IndexType::k16bit;
39 }
40 if (sizeof(IndexType) == 4) {
41 return impeller::IndexType::k32bit;
42 }
43 return impeller::IndexType::kUnknown;
44 }
45
46 void SetLabel(const std::string& label) { label_ = label; }
47
48 void Reserve(size_t count) { return vertices_.reserve(count); }
49
50 void ReserveIndices(size_t count) { return indices_.reserve(count); }
51
52 bool HasVertices() const { return !vertices_.empty(); }
53
54 size_t GetVertexCount() const { return vertices_.size(); }
55
56 size_t GetIndexCount() const {
57 return indices_.size() > 0 ? indices_.size() : vertices_.size();
58 }
59
60 const VertexType& Last() const {
61 FML_DCHECK(!vertices_.empty());
62 return vertices_.back();
63 }
64
65 VertexBufferBuilder& AppendVertex(VertexType_ vertex) {
66 vertices_.emplace_back(std::move(vertex));
67 return *this;
68 }
69
70 VertexBufferBuilder& AddVertices(
71 std::initializer_list<VertexType_> vertices) {
72 vertices_.reserve(vertices.size());
73 for (auto& vertex : vertices) {
74 vertices_.emplace_back(std::move(vertex));
75 }
76 return *this;
77 }
78
79 VertexBufferBuilder& AppendIndex(IndexType_ index) {
80 indices_.emplace_back(index);
81 return *this;
82 }
83
84 VertexBuffer CreateVertexBuffer(HostBuffer& host_buffer) const {
85 VertexBuffer buffer;
86 buffer.vertex_buffer = CreateVertexBufferView(host_buffer);
87 buffer.index_buffer = CreateIndexBufferView(host_buffer);
88 buffer.vertex_count = GetIndexCount();
89 buffer.index_type = GetIndexType();
90 return buffer;
91 };
92
93 VertexBuffer CreateVertexBuffer(Allocator& device_allocator) const {
94 VertexBuffer buffer;
95 // This can be merged into a single allocation.
96 buffer.vertex_buffer = CreateVertexBufferView(device_allocator);
97 buffer.index_buffer = CreateIndexBufferView(device_allocator);
98 buffer.vertex_count = GetIndexCount();
99 buffer.index_type = GetIndexType();
100 return buffer;
101 };
102
103 void IterateVertices(const std::function<void(VertexType&)>& iterator) {
104 for (auto& vertex : vertices_) {
105 iterator(vertex);
106 }
107 }
108
109 private:
110 std::vector<VertexType> vertices_;
111 std::vector<IndexType> indices_;
112 std::string label_;
113
114 BufferView CreateVertexBufferView(HostBuffer& buffer) const {
115 return buffer.Emplace(vertices_.data(),
116 vertices_.size() * sizeof(VertexType),
117 alignof(VertexType));
118 }
119
120 BufferView CreateVertexBufferView(Allocator& allocator) const {
121 auto buffer = allocator.CreateBufferWithCopy(
122 reinterpret_cast<const uint8_t*>(vertices_.data()),
123 vertices_.size() * sizeof(VertexType));
124 if (!buffer) {
125 return {};
126 }
127 if (!label_.empty()) {
128 buffer->SetLabel(SPrintF("%s Vertices", label_.c_str()));
129 }
130 return DeviceBuffer::AsBufferView(buffer);
131 }
132
133 std::vector<IndexType> CreateIndexBuffer() const { return indices_; }
134
135 BufferView CreateIndexBufferView(HostBuffer& buffer) const {
136 const auto index_buffer = CreateIndexBuffer();
137 if (index_buffer.size() == 0) {
138 return {};
139 }
140 return buffer.Emplace(index_buffer.data(),
141 index_buffer.size() * sizeof(IndexType),
142 alignof(IndexType));
143 }
144
145 BufferView CreateIndexBufferView(Allocator& allocator) const {
146 const auto index_buffer = CreateIndexBuffer();
147 if (index_buffer.size() == 0) {
148 return {};
149 }
150 auto buffer = allocator.CreateBufferWithCopy(
151 reinterpret_cast<const uint8_t*>(index_buffer.data()),
152 index_buffer.size() * sizeof(IndexType));
153 if (!buffer) {
154 return {};
155 }
156 if (!label_.empty()) {
157 buffer->SetLabel(SPrintF("%s Indices", label_.c_str()));
158 }
159 return DeviceBuffer::AsBufferView(buffer);
160 }
161 };
162
163 } // namespace impeller
164
165 #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::IndexType::k16bit
@ k16bit
vertex_buffer.h
impeller::VertexBufferBuilder::GetVertexCount
size_t GetVertexCount() const
Definition: vertex_buffer_builder.h:54
device_buffer.h
impeller::VertexBufferBuilder::VertexType
VertexType_ VertexType
Definition: vertex_buffer_builder.h:26
impeller::HostBuffer
Definition: host_buffer.h:28
impeller::VertexBufferBuilder::~VertexBufferBuilder
~VertexBufferBuilder()=default
impeller::VertexBuffer
Definition: vertex_buffer.h:13
formats.h
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:94
impeller::VertexBufferBuilder::AddVertices
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
Definition: vertex_buffer_builder.h:70
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:93
impeller::VertexBufferBuilder::VertexBufferBuilder
VertexBufferBuilder()=default
impeller::VertexBufferBuilder::HasVertices
bool HasVertices() const
Definition: vertex_buffer_builder.h:52
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::VertexBufferBuilder
Definition: vertex_buffer_builder.h:24
impeller::IndexType
IndexType
Definition: formats.h:344
impeller::VertexBufferBuilder::GetIndexCount
size_t GetIndexCount() const
Definition: vertex_buffer_builder.h:56
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:22
strings.h
impeller::VertexBufferBuilder::AppendIndex
VertexBufferBuilder & AppendIndex(IndexType_ index)
Definition: vertex_buffer_builder.h:79
impeller::VertexBufferBuilder::SetLabel
void SetLabel(const std::string &label)
Definition: vertex_buffer_builder.h:46
impeller::VertexBufferBuilder::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
Definition: vertex_buffer_builder.h:84
impeller::VertexBufferBuilder::IterateVertices
void IterateVertices(const std::function< void(VertexType &)> &iterator)
Definition: vertex_buffer_builder.h:103
impeller::VertexBufferBuilder::ReserveIndices
void ReserveIndices(size_t count)
Definition: vertex_buffer_builder.h:50
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:27
impeller::BufferView
Definition: buffer_view.h:15
impeller::VertexBufferBuilder::GetIndexType
constexpr impeller::IndexType GetIndexType() const
Definition: vertex_buffer_builder.h:33
vector.h
impeller::VertexBufferBuilder::AppendVertex
VertexBufferBuilder & AppendVertex(VertexType_ vertex)
Definition: vertex_buffer_builder.h:65
impeller::VertexBufferBuilder::Reserve
void Reserve(size_t count)
Definition: vertex_buffer_builder.h:48
impeller::HostBuffer::SetLabel
void SetLabel(std::string label)
Definition: host_buffer.cc:37
impeller
Definition: aiks_blur_unittests.cc:20
impeller::IndexType::kUnknown
@ kUnknown
impeller::VertexBufferBuilder::Last
const VertexType & Last() const
Definition: vertex_buffer_builder.h:60