Flutter Engine
The Flutter Engine
Classes | Public Member Functions | Friends | List of all members
skgpu::graphite::DrawBufferManager Class Reference

#include <BufferManager.h>

Public Member Functions

 DrawBufferManager (ResourceProvider *, const Caps *, UploadBufferManager *)
 
 ~DrawBufferManager ()
 
bool hasMappingFailed () const
 
std::pair< VertexWriter, BindBufferInfogetVertexWriter (size_t requiredBytes)
 
std::pair< IndexWriter, BindBufferInfogetIndexWriter (size_t requiredBytes)
 
std::pair< UniformWriter, BindBufferInfogetUniformWriter (size_t requiredBytes)
 
std::pair< UniformWriter, BindBufferInfogetSsboWriter (size_t requiredBytes)
 
std::pair< void *, BindBufferInfogetUniformPointer (size_t requiredBytes)
 
std::pair< void *, BindBufferInfogetStoragePointer (size_t requiredBytes)
 
BindBufferInfo getStorage (size_t requiredBytes, ClearBuffer cleared=ClearBuffer::kNo)
 
BindBufferInfo getVertexStorage (size_t requiredBytes)
 
BindBufferInfo getIndexStorage (size_t requiredBytes)
 
BindBufferInfo getIndirectStorage (size_t requiredBytes, ClearBuffer cleared=ClearBuffer::kNo)
 
ScratchBuffer getScratchStorage (size_t requiredBytes)
 
void returnVertexBytes (size_t unusedBytes)
 
size_t alignUniformBlockSize (size_t dataSize)
 
void transferToRecording (Recording *)
 

Friends

class ScratchBuffer
 

Detailed Description

DrawBufferManager controls writing to buffer data ranges within larger, cacheable Buffers and automatically handles either mapping or copying via transfer buffer depending on what the GPU hardware supports for the requested buffer type and use case. It is intended for repeatedly uploading dynamic data to the GPU.

Definition at line 99 of file BufferManager.h.

Constructor & Destructor Documentation

◆ DrawBufferManager()

skgpu::graphite::DrawBufferManager::DrawBufferManager ( ResourceProvider resourceProvider,
const Caps caps,
UploadBufferManager uploadManager 
)

Definition at line 115 of file BufferManager.cpp.

118 : fResourceProvider(resourceProvider)
119 , fCaps(caps)
120 , fUploadManager(uploadManager)
121 , fCurrentBuffers{{
122 { BufferType::kVertex, kVertexBufferSize, caps },
123 { BufferType::kIndex, kIndexBufferSize, caps },
124 { BufferType::kUniform, kUniformBufferSize, caps },
125 { BufferType::kStorage, kStorageBufferSize, caps }, // mapped storage
126 { BufferType::kStorage, kStorageBufferSize, caps }, // GPU-only storage
127 { BufferType::kVertexStorage, kVertexBufferSize, caps },
128 { BufferType::kIndexStorage, kIndexBufferSize, caps },
129 { BufferType::kIndirect, kStorageBufferSize, caps } }} {}

◆ ~DrawBufferManager()

skgpu::graphite::DrawBufferManager::~DrawBufferManager ( )

Definition at line 131 of file BufferManager.cpp.

131{}

Member Function Documentation

◆ alignUniformBlockSize()

size_t skgpu::graphite::DrawBufferManager::alignUniformBlockSize ( size_t  dataSize)
inline

Definition at line 146 of file BufferManager.h.

146 {
147 return SkAlignTo(dataSize, fCurrentBuffers[kUniformBufferIndex].fStartAlignment);
148 }
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition: SkAlign.h:33

◆ getIndexStorage()

BindBufferInfo skgpu::graphite::DrawBufferManager::getIndexStorage ( size_t  requiredBytes)

Definition at line 233 of file BufferManager.cpp.

233 {
234 if (!requiredBytes) {
235 return {};
236 }
237
238 auto& info = fCurrentBuffers[kIndexStorageBufferIndex];
239 return this->prepareBindBuffer(&info, requiredBytes, "IndexStorageBuffer");
240}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213

◆ getIndexWriter()

std::pair< IndexWriter, BindBufferInfo > skgpu::graphite::DrawBufferManager::getIndexWriter ( size_t  requiredBytes)

Definition at line 161 of file BufferManager.cpp.

161 {
162 if (!requiredBytes) {
163 return {};
164 }
165
166 auto& info = fCurrentBuffers[kIndexBufferIndex];
167 auto [ptr, bindInfo] = this->prepareMappedBindBuffer(&info, requiredBytes, "IndexBuffer");
168 return {IndexWriter(ptr, requiredBytes), bindInfo};
169}

◆ getIndirectStorage()

BindBufferInfo skgpu::graphite::DrawBufferManager::getIndirectStorage ( size_t  requiredBytes,
ClearBuffer  cleared = ClearBuffer::kNo 
)

Definition at line 242 of file BufferManager.cpp.

242 {
243 if (!requiredBytes) {
244 return {};
245 }
246
247 auto& info = fCurrentBuffers[kIndirectStorageBufferIndex];
248 return this->prepareBindBuffer(&info,
249 requiredBytes,
250 "IndirectStorageBuffer",
251 /*supportCpuUpload=*/false,
252 cleared);
253}

◆ getScratchStorage()

ScratchBuffer skgpu::graphite::DrawBufferManager::getScratchStorage ( size_t  requiredBytes)

Definition at line 255 of file BufferManager.cpp.

255 {
256 if (!requiredBytes || fMappingFailed) {
257 return {};
258 }
259
260 // TODO: Generalize the pool to other buffer types.
261 auto& info = fCurrentBuffers[kStorageBufferIndex];
262 size_t bufferSize = sufficient_block_size(requiredBytes, info.fBlockSize);
263 sk_sp<Buffer> buffer = this->findReusableSbo(bufferSize);
264 if (!buffer) {
265 buffer = fResourceProvider->findOrCreateBuffer(
266 bufferSize, BufferType::kStorage, AccessPattern::kGpuOnly, "ScratchStorageBuffer");
267
268 if (!buffer) {
269 this->onFailedBuffer();
270 return {};
271 }
272 }
273 return {requiredBytes, info.fStartAlignment, std::move(buffer), this};
274}
sk_sp< Buffer > findOrCreateBuffer(size_t size, BufferType type, AccessPattern, std::string_view label)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126

◆ getSsboWriter()

std::pair< UniformWriter, BindBufferInfo > skgpu::graphite::DrawBufferManager::getSsboWriter ( size_t  requiredBytes)

Definition at line 181 of file BufferManager.cpp.

181 {
182 if (!requiredBytes) {
183 return {};
184 }
185
186 auto& info = fCurrentBuffers[kStorageBufferIndex];
187 auto [ptr, bindInfo] = this->prepareMappedBindBuffer(&info, requiredBytes, "StorageBuffer");
188 return {UniformWriter(ptr, requiredBytes), bindInfo};
189}

◆ getStorage()

BindBufferInfo skgpu::graphite::DrawBufferManager::getStorage ( size_t  requiredBytes,
ClearBuffer  cleared = ClearBuffer::kNo 
)

Definition at line 211 of file BufferManager.cpp.

211 {
212 if (!requiredBytes) {
213 return {};
214 }
215
216 auto& info = fCurrentBuffers[kGpuOnlyStorageBufferIndex];
217 return this->prepareBindBuffer(&info,
218 requiredBytes,
219 "StorageBuffer",
220 /*supportCpuUpload=*/false,
221 cleared);
222}

◆ getStoragePointer()

std::pair< void *, BindBufferInfo > skgpu::graphite::DrawBufferManager::getStoragePointer ( size_t  requiredBytes)

Definition at line 201 of file BufferManager.cpp.

202 {
203 if (!requiredBytes) {
204 return {};
205 }
206
207 auto& info = fCurrentBuffers[kStorageBufferIndex];
208 return this->prepareMappedBindBuffer(&info, requiredBytes, "StorageBuffer");
209}

◆ getUniformPointer()

std::pair< void *, BindBufferInfo > skgpu::graphite::DrawBufferManager::getUniformPointer ( size_t  requiredBytes)

Definition at line 191 of file BufferManager.cpp.

192 {
193 if (!requiredBytes) {
194 return {};
195 }
196
197 auto& info = fCurrentBuffers[kUniformBufferIndex];
198 return this->prepareMappedBindBuffer(&info, requiredBytes, "UniformBuffer");
199}

◆ getUniformWriter()

std::pair< UniformWriter, BindBufferInfo > skgpu::graphite::DrawBufferManager::getUniformWriter ( size_t  requiredBytes)

Definition at line 171 of file BufferManager.cpp.

171 {
172 if (!requiredBytes) {
173 return {};
174 }
175
176 auto& info = fCurrentBuffers[kUniformBufferIndex];
177 auto [ptr, bindInfo] = this->prepareMappedBindBuffer(&info, requiredBytes, "UniformBuffer");
178 return {UniformWriter(ptr, requiredBytes), bindInfo};
179}

◆ getVertexStorage()

BindBufferInfo skgpu::graphite::DrawBufferManager::getVertexStorage ( size_t  requiredBytes)

Definition at line 224 of file BufferManager.cpp.

224 {
225 if (!requiredBytes) {
226 return {};
227 }
228
229 auto& info = fCurrentBuffers[kVertexStorageBufferIndex];
230 return this->prepareBindBuffer(&info, requiredBytes, "VertexStorageBuffer");
231}

◆ getVertexWriter()

std::pair< VertexWriter, BindBufferInfo > skgpu::graphite::DrawBufferManager::getVertexWriter ( size_t  requiredBytes)

Definition at line 141 of file BufferManager.cpp.

141 {
142 if (!requiredBytes) {
143 return {};
144 }
145
146 auto& info = fCurrentBuffers[kVertexBufferIndex];
147 auto [ptr, bindInfo] = this->prepareMappedBindBuffer(&info, requiredBytes, "VertexBuffer");
148 return {VertexWriter(ptr, requiredBytes), bindInfo};
149}

◆ hasMappingFailed()

bool skgpu::graphite::DrawBufferManager::hasMappingFailed ( ) const
inline

Definition at line 106 of file BufferManager.h.

106{ return fMappingFailed; }

◆ returnVertexBytes()

void skgpu::graphite::DrawBufferManager::returnVertexBytes ( size_t  unusedBytes)

Definition at line 151 of file BufferManager.cpp.

151 {
152 if (fMappingFailed) {
153 // The caller can be unaware that the written data went to no-where and will still call
154 // this function.
155 return;
156 }
157 SkASSERT(fCurrentBuffers[kVertexBufferIndex].fOffset >= unusedBytes);
158 fCurrentBuffers[kVertexBufferIndex].fOffset -= unusedBytes;
159}
#define SkASSERT(cond)
Definition: SkAssert.h:116

◆ transferToRecording()

void skgpu::graphite::DrawBufferManager::transferToRecording ( Recording recording)

Definition at line 299 of file BufferManager.cpp.

299 {
300 // We could allow this to be called when the mapping has failed, since the transfer will be a no
301 // op, but in practice, the caller will want to check the error state as soon as possible to
302 // limit any unnecessary resource preparation from other tasks.
303 SkASSERT(!fMappingFailed);
304
305 if (!fClearList.empty()) {
306 recording->priv().addTask(ClearBuffersTask::Make(std::move(fClearList)));
307 }
308
309 // Transfer the buffers in the reuse pool to the recording.
310 // TODO: Allow reuse across different Recordings?
311 for (auto& buffer : fReusableScratchStorageBuffers) {
312 recording->priv().addResourceRef(std::move(buffer));
313 }
314 fReusableScratchStorageBuffers.clear();
315
316 for (auto& [buffer, transferBuffer] : fUsedBuffers) {
317 if (transferBuffer) {
320 // Since the transfer buffer is managed by the UploadManager, we don't manually unmap
321 // it here or need to pass a ref into CopyBufferToBufferTask.
322 size_t copySize = buffer->size();
323 recording->priv().addTask(
324 CopyBufferToBufferTask::Make(transferBuffer.fBuffer,
325 transferBuffer.fOffset,
326 std::move(buffer),
327 /*dstOffset=*/0,
328 copySize));
329 } else {
330 if (buffer->isMapped()) {
331 buffer->unmap();
332 }
333 recording->priv().addResourceRef(std::move(buffer));
334 }
335 }
336 fUsedBuffers.clear();
337
338 // The current draw buffers have not been added to fUsedBuffers,
339 // so we need to handle them as well.
340 for (auto& info : fCurrentBuffers) {
341 if (!info.fBuffer) {
342 continue;
343 }
344 if (info.fTransferBuffer) {
345 // A transfer buffer should always be mapped at this stage
346 SkASSERT(info.fBuffer);
348 // Since the transfer buffer is managed by the UploadManager, we don't manually unmap
349 // it here or need to pass a ref into CopyBufferToBufferTask.
350 recording->priv().addTask(
351 CopyBufferToBufferTask::Make(info.fTransferBuffer.fBuffer,
352 info.fTransferBuffer.fOffset,
353 info.fBuffer,
354 /*dstOffset=*/0,
355 info.fBuffer->size()));
356 } else {
357 if (info.fBuffer->isMapped()) {
358 info.fBuffer->unmap();
359 }
360 recording->priv().addResourceRef(std::move(info.fBuffer));
361 }
362 info.fTransferBuffer = {};
363 info.fOffset = 0;
364 }
365}
bool drawBufferCanBeMapped() const
Definition: Caps.h:242
static sk_sp< ClearBuffersTask > Make(skia_private::TArray< ClearBufferInfo >)
static sk_sp< CopyBufferToBufferTask > Make(const Buffer *srcBuffer, size_t srcOffset, sk_sp< Buffer > dstBuffer, size_t dstOffset, size_t size)
Definition: CopyTask.cpp:18

Friends And Related Function Documentation

◆ ScratchBuffer

friend class ScratchBuffer
friend

Definition at line 155 of file BufferManager.h.


The documentation for this class was generated from the following files: