Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 <array>
10#include <functional>
11#include <memory>
12#include <string>
13#include <type_traits>
14
18
19namespace impeller {
20
21/// Approximately the same size as the max frames in flight.
22static const constexpr size_t kHostBufferArenaSize = 3u;
23
24/// The host buffer class manages one more 1024 Kb blocks of device buffer
25/// allocations.
26///
27/// These are reset per-frame.
29 public:
30 static std::shared_ptr<HostBuffer> Create(
31 const std::shared_ptr<Allocator>& allocator);
32
33 // |Buffer|
34 virtual ~HostBuffer();
35
36 void SetLabel(std::string label);
37
38 //----------------------------------------------------------------------------
39 /// @brief Emplace uniform data onto the host buffer. Ensure that backend
40 /// specific uniform alignment requirements are respected.
41 ///
42 /// @param[in] uniform The uniform struct to emplace onto the buffer.
43 ///
44 /// @tparam UniformType The type of the uniform struct.
45 ///
46 /// @return The buffer view.
47 ///
48 template <class UniformType,
49 class = std::enable_if_t<std::is_standard_layout_v<UniformType>>>
50 [[nodiscard]] BufferView EmplaceUniform(const UniformType& uniform) {
51 const auto alignment =
52 std::max(alignof(UniformType), DefaultUniformAlignment());
53 return Emplace(reinterpret_cast<const void*>(&uniform), // buffer
54 sizeof(UniformType), // size
55 alignment // alignment
56 );
57 }
58
59 //----------------------------------------------------------------------------
60 /// @brief Emplace storage buffer data onto the host buffer. Ensure that
61 /// backend specific uniform alignment requirements are respected.
62 ///
63 /// @param[in] uniform The storage buffer to emplace onto the buffer.
64 ///
65 /// @tparam StorageBufferType The type of the shader storage buffer.
66 ///
67 /// @return The buffer view.
68 ///
69 template <
70 class StorageBufferType,
71 class = std::enable_if_t<std::is_standard_layout_v<StorageBufferType>>>
73 const StorageBufferType& buffer) {
74 const auto alignment =
75 std::max(alignof(StorageBufferType), DefaultUniformAlignment());
76 return Emplace(&buffer, // buffer
77 sizeof(StorageBufferType), // size
78 alignment // alignment
79 );
80 }
81
82 //----------------------------------------------------------------------------
83 /// @brief Emplace non-uniform data (like contiguous vertices) onto the
84 /// host buffer.
85 ///
86 /// @param[in] buffer The buffer data.
87 ///
88 /// @tparam BufferType The type of the buffer data.
89 ///
90 /// @return The buffer view.
91 ///
92 template <class BufferType,
93 class = std::enable_if_t<std::is_standard_layout_v<BufferType>>>
94 [[nodiscard]] BufferView Emplace(const BufferType& buffer) {
95 return Emplace(reinterpret_cast<const void*>(&buffer), // buffer
96 sizeof(BufferType), // size
97 alignof(BufferType) // alignment
98 );
99 }
100
101 [[nodiscard]] BufferView Emplace(const void* buffer,
102 size_t length,
103 size_t align);
104
105 using EmplaceProc = std::function<void(uint8_t* buffer)>;
106
107 //----------------------------------------------------------------------------
108 /// @brief Emplaces undefined data onto the managed buffer and gives the
109 /// caller a chance to update it using the specified callback. The
110 /// buffer is guaranteed to have enough space for length bytes. It
111 /// is the responsibility of the caller to not exceed the bounds
112 /// of the buffer returned in the EmplaceProc.
113 ///
114 /// @param[in] cb A callback that will be passed a ptr to the
115 /// underlying host buffer.
116 ///
117 /// @return The buffer view.
118 ///
119 BufferView Emplace(size_t length, size_t align, const EmplaceProc& cb);
120
121 //----------------------------------------------------------------------------
122 /// @brief Resets the contents of the HostBuffer to nothing so it can be
123 /// reused.
124 void Reset();
125
126 /// Test only internal state.
132
133 /// @brief Retrieve internal buffer state for test expectations.
135
136 private:
137 [[nodiscard]] std::tuple<Range, std::shared_ptr<DeviceBuffer>>
138 EmplaceInternal(const void* buffer, size_t length);
139
140 std::tuple<Range, std::shared_ptr<DeviceBuffer>>
141 EmplaceInternal(size_t length, size_t align, const EmplaceProc& cb);
142
143 std::tuple<Range, std::shared_ptr<DeviceBuffer>>
144 EmplaceInternal(const void* buffer, size_t length, size_t align);
145
146 size_t GetLength() const { return offset_; }
147
148 void MaybeCreateNewBuffer();
149
150 const std::shared_ptr<DeviceBuffer>& GetCurrentBuffer() const;
151
152 [[nodiscard]] BufferView Emplace(const void* buffer, size_t length);
153
154 explicit HostBuffer(const std::shared_ptr<Allocator>& allocator);
155
156 HostBuffer(const HostBuffer&) = delete;
157
158 HostBuffer& operator=(const HostBuffer&) = delete;
159
160 std::shared_ptr<Allocator> allocator_;
161 std::array<std::vector<std::shared_ptr<DeviceBuffer>>, kHostBufferArenaSize>
162 device_buffers_;
163 size_t current_buffer_ = 0u;
164 size_t offset_ = 0u;
165 size_t frame_index_ = 0u;
166 std::string label_;
167};
168
169} // namespace impeller
170
171#endif // FLUTTER_IMPELLER_CORE_HOST_BUFFER_H_
static sk_sp< Effect > Create()
void SetLabel(std::string label)
TestStateQuery GetStateForTest()
Retrieve internal buffer state for test expectations.
BufferView Emplace(const BufferType &buffer)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition host_buffer.h:94
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition host_buffer.h:50
BufferView EmplaceStorageBuffer(const StorageBufferType &buffer)
Emplace storage buffer data onto the host buffer. Ensure that backend specific uniform alignment requ...
Definition host_buffer.h:72
std::function< void(uint8_t *buffer)> EmplaceProc
void Reset()
Resets the contents of the HostBuffer to nothing so it can be reused.
static const uint8_t buffer[]
size_t length
static const constexpr size_t kHostBufferArenaSize
Approximately the same size as the max frames in flight.
Definition host_buffer.h:22
constexpr size_t DefaultUniformAlignment()
Definition platform.h:15
Test only internal state.