Flutter Engine
The 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 <initializer_list>
9#include <map>
10#include <vector>
11
12#include "flutter/fml/macros.h"
20
21namespace impeller {
22
23template <class VertexType_, class IndexType_ = uint16_t>
25 public:
26 using VertexType = VertexType_;
27 using IndexType = IndexType_;
28
30
32
34 if (indices_.size() == 0) {
36 }
37 if constexpr (sizeof(IndexType) == 2) {
39 }
40 if (sizeof(IndexType) == 4) {
42 }
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
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
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 {
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 }
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 }
160 }
161};
162
163} // namespace impeller
164
165#endif // FLUTTER_IMPELLER_RENDERER_VERTEX_BUFFER_BUILDER_H_
int count
An object that allocates device memory.
Definition allocator.h:22
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
VertexBufferBuilder & AppendVertex(VertexType_ vertex)
const VertexType & Last() 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 const uint8_t buffer[]
#define FML_DCHECK(condition)
Definition logging.h:103
@ kNone
Does not use the index buffer.
std::string SPrintF(const char *format,...)
Definition strings.cc:12