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