| Flutter Impeller |

host_buffer.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_CORE_HOST_BUFFER_H_
6 #define FLUTTER_IMPELLER_CORE_HOST_BUFFER_H_
7
8 #include <algorithm>
9 #include <memory>
10 #include <string>
11 #include <type_traits>
12
13 #include "impeller/base/allocation.h"
14 #include "impeller/core/buffer.h"
15 #include "impeller/core/buffer_view.h"
16 #include "impeller/core/platform.h"
17
18 namespace impeller {
19
20 class HostBuffer final : public Buffer {
21 public:
22 static std::shared_ptr<HostBuffer> Create();
23
24 // |Buffer|
25 virtual ~HostBuffer();
26
27 void SetLabel(std::string label);
28
29 //----------------------------------------------------------------------------
30 /// @brief Emplace uniform data onto the host buffer. Ensure that backend
31 /// specific uniform alignment requirements are respected.
32 ///
33 /// @param[in] uniform The uniform struct to emplace onto the buffer.
34 ///
35 /// @tparam UniformType The type of the uniform struct.
36 ///
37 /// @return The buffer view.
38 ///
39 template <class UniformType,
40 class = std::enable_if_t<std::is_standard_layout_v<UniformType>>>
41 [[nodiscard]] BufferView EmplaceUniform(const UniformType& uniform) {
42 const auto alignment =
43 std::max(alignof(UniformType), DefaultUniformAlignment());
44 return Emplace(reinterpret_cast<const void*>(&uniform), // buffer
45 sizeof(UniformType), // size
46 alignment // alignment
47 );
48 }
49
50 //----------------------------------------------------------------------------
51 /// @brief Emplace storage buffer data onto the host buffer. Ensure that
52 /// backend specific uniform alignment requirements are respected.
53 ///
54 /// @param[in] uniform The storage buffer to emplace onto the buffer.
55 ///
56 /// @tparam StorageBufferType The type of the shader storage buffer.
57 ///
58 /// @return The buffer view.
59 ///
60 template <
61 class StorageBufferType,
62 class = std::enable_if_t<std::is_standard_layout_v<StorageBufferType>>>
63 [[nodiscard]] BufferView EmplaceStorageBuffer(
64 const StorageBufferType& buffer) {
65 const auto alignment =
66 std::max(alignof(StorageBufferType), DefaultUniformAlignment());
67 return Emplace(&buffer, // buffer
68 sizeof(StorageBufferType), // size
69 alignment // alignment
70 );
71 }
72
73 //----------------------------------------------------------------------------
74 /// @brief Emplace non-uniform data (like contiguous vertices) onto the
75 /// host buffer.
76 ///
77 /// @param[in] buffer The buffer data.
78 ///
79 /// @tparam BufferType The type of the buffer data.
80 ///
81 /// @return The buffer view.
82 ///
83 template <class BufferType,
84 class = std::enable_if_t<std::is_standard_layout_v<BufferType>>>
85 [[nodiscard]] BufferView Emplace(const BufferType& buffer) {
86 return Emplace(reinterpret_cast<const void*>(&buffer), // buffer
87 sizeof(BufferType), // size
88 alignof(BufferType) // alignment
89 );
90 }
91
92 [[nodiscard]] BufferView Emplace(const void* buffer,
93 size_t length,
94 size_t align);
95
96 using EmplaceProc = std::function<void(uint8_t* buffer)>;
97
98 //----------------------------------------------------------------------------
99 /// @brief Emplaces undefined data onto the managed buffer and gives the
100 /// caller a chance to update it using the specified callback. The
101 /// buffer is guaranteed to have enough space for length bytes. It
102 /// is the responsibility of the caller to not exceed the bounds
103 /// of the buffer returned in the EmplaceProc.
104 ///
105 /// @param[in] cb A callback that will be passed a ptr to the
106 /// underlying host buffer.
107 ///
108 /// @return The buffer view.
109 ///
110 BufferView Emplace(size_t length, size_t align, const EmplaceProc& cb);
111
112 //----------------------------------------------------------------------------
113 /// @brief Resets the contents of the HostBuffer to nothing so it can be
114 /// reused.
115 void Reset();
116
117 //----------------------------------------------------------------------------
118 /// @brief Returns the capacity of the HostBuffer in memory in bytes.
119 size_t GetSize() const;
120
121 //----------------------------------------------------------------------------
122 /// @brief Returns the size of the currently allocated HostBuffer memory in
123 /// bytes.
124 size_t GetLength() const;
125
126 private:
127 struct HostBufferState : public Buffer, public Allocation {
128 std::shared_ptr<const DeviceBuffer> GetDeviceBuffer(
129 Allocator& allocator) const override;
130
131 [[nodiscard]] std::pair<uint8_t*, Range> Emplace(const void* buffer,
132 size_t length);
133
134 std::pair<uint8_t*, Range> Emplace(size_t length,
135 size_t align,
136 const EmplaceProc& cb);
137
138 std::pair<uint8_t*, Range> Emplace(const void* buffer,
139 size_t length,
140 size_t align);
141
142 void Reset();
143
144 mutable std::shared_ptr<DeviceBuffer> device_buffer;
145 mutable size_t device_buffer_generation = 0u;
146 size_t generation = 1u;
147 std::string label;
148 };
149
150 std::shared_ptr<HostBufferState> state_ = std::make_shared<HostBufferState>();
151
152 // |Buffer|
153 std::shared_ptr<const DeviceBuffer> GetDeviceBuffer(
154 Allocator& allocator) const override;
155
156 [[nodiscard]] BufferView Emplace(const void* buffer, size_t length);
157
158 HostBuffer();
159
160 HostBuffer(const HostBuffer&) = delete;
161
162 HostBuffer& operator=(const HostBuffer&) = delete;
163 };
164
165 } // namespace impeller
166
167 #endif // FLUTTER_IMPELLER_CORE_HOST_BUFFER_H_
impeller::HostBuffer::EmplaceProc
std::function< void(uint8_t *buffer)> EmplaceProc
Definition: host_buffer.h:96
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:15
allocation.h
impeller::HostBuffer
Definition: host_buffer.h:20
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:85
impeller::HostBuffer::GetSize
size_t GetSize() const
Returns the capacity of the HostBuffer in memory in bytes.
Definition: host_buffer.cc:67
impeller::HostBuffer::Create
static std::shared_ptr< HostBuffer > Create()
Definition: host_buffer.cc:18
impeller::HostBuffer::~HostBuffer
virtual ~HostBuffer()
impeller::HostBuffer::GetLength
size_t GetLength() const
Returns the size of the currently allocated HostBuffer memory in bytes.
Definition: host_buffer.cc:71
impeller::Buffer
Definition: buffer.h:15
impeller::Allocator
An object that allocates device memory.
Definition: allocator.h:22
buffer.h
impeller::Allocation
Definition: allocation.h:16
platform.h
impeller::BufferView
Definition: buffer_view.h:13
buffer_view.h
impeller::HostBuffer::Reset
void Reset()
Resets the contents of the HostBuffer to nothing so it can be reused.
Definition: host_buffer.cc:63
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:41
impeller::HostBuffer::EmplaceStorageBuffer
BufferView EmplaceStorageBuffer(const StorageBufferType &buffer)
Emplace storage buffer data onto the host buffer. Ensure that backend specific uniform alignment requ...
Definition: host_buffer.h:63
impeller::HostBuffer::SetLabel
void SetLabel(std::string label)
Definition: host_buffer.cc:26
impeller
Definition: aiks_context.cc:10