Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | List of all members
skgpu::graphite::DawnBuffer Class Reference

#include <DawnBuffer.h>

Inheritance diagram for skgpu::graphite::DawnBuffer:
skgpu::graphite::Buffer skgpu::graphite::Resource

Public Member Functions

bool isUnmappable () const override
 
const wgpu::Buffer & dawnBuffer () const
 
- Public Member Functions inherited from skgpu::graphite::Buffer
size_t size () const
 
void * map ()
 
void asyncMap (GpuFinishedProc=nullptr, GpuFinishedContext=nullptr)
 
void unmap ()
 
bool isMapped () const
 
const char * getResourceType () const override
 
- Public Member Functions inherited from skgpu::graphite::Resource
 Resource (const Resource &)=delete
 
 Resource (Resource &&)=delete
 
Resourceoperator= (const Resource &)=delete
 
Resourceoperator= (Resource &&)=delete
 
void ref () const
 
void unref () const
 
void refCommandBuffer () const
 
void unrefCommandBuffer () const
 
Ownership ownership () const
 
skgpu::Budgeted budgeted () const
 
size_t gpuMemorySize () const
 
UniqueID uniqueID () const
 
std::string getLabel () const
 
void setLabel (std::string_view label)
 
bool wasDestroyed () const
 
const GraphiteResourceKeykey () const
 
void setKey (const GraphiteResourceKey &key)
 
void dumpMemoryStatistics (SkTraceMemoryDump *traceMemoryDump) const
 
virtual void prepareForReturnToCache (const std::function< void()> &takeRef)
 

Static Public Member Functions

static sk_sp< DawnBufferMake (const DawnSharedContext *, size_t size, BufferType type, AccessPattern, std::string_view label)
 

Private Member Functions

void onMap () override
 
void onUnmap () override
 
void freeGpuData () override
 
void onDumpMemoryStatistics (SkTraceMemoryDump *traceMemoryDump, const char *dumpName) const override
 

Additional Inherited Members

- Protected Member Functions inherited from skgpu::graphite::Buffer
 Buffer (const SharedContext *sharedContext, size_t size, std::string_view label, bool commandBufferRefsAsUsageRefs=false)
 
- Protected Member Functions inherited from skgpu::graphite::Resource
 Resource (const SharedContext *, Ownership, skgpu::Budgeted, size_t gpuMemorySize, std::string_view label, bool commandBufferRefsAsUsageRefs=false)
 
virtual ~Resource ()
 
const SharedContextsharedContext () const
 
virtual void invokeReleaseProc ()
 
void setDeleteASAP ()
 
- Protected Attributes inherited from skgpu::graphite::Buffer
void * fMapPtr = nullptr
 

Detailed Description

Definition at line 23 of file DawnBuffer.h.

Member Function Documentation

◆ dawnBuffer()

const wgpu::Buffer & skgpu::graphite::DawnBuffer::dawnBuffer ( ) const
inline

Definition at line 33 of file DawnBuffer.h.

33{ return fBuffer; }

◆ freeGpuData()

void skgpu::graphite::DawnBuffer::freeGpuData ( )
overrideprivatevirtual

Implements skgpu::graphite::Resource.

Definition at line 313 of file DawnBuffer.cpp.

313 {
314 fBuffer = nullptr;
315}

◆ isUnmappable()

bool skgpu::graphite::DawnBuffer::isUnmappable ( ) const
overridevirtual

Reimplemented from skgpu::graphite::Buffer.

Definition at line 309 of file DawnBuffer.cpp.

309 {
310 return fBuffer.GetMapState() != wgpu::BufferMapState::Unmapped;
311}

◆ Make()

sk_sp< DawnBuffer > skgpu::graphite::DawnBuffer::Make ( const DawnSharedContext sharedContext,
size_t  size,
BufferType  type,
AccessPattern  accessPattern,
std::string_view  label 
)
static

Definition at line 30 of file DawnBuffer.cpp.

34 {
35 if (size <= 0) {
36 return nullptr;
37 }
38
39 wgpu::BufferUsage usage = wgpu::BufferUsage::None;
40
41 switch (type) {
43 usage = wgpu::BufferUsage::Vertex | wgpu::BufferUsage::CopyDst;
44 break;
46 usage = wgpu::BufferUsage::Index | wgpu::BufferUsage::CopyDst;
47 break;
49 usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::MapWrite;
50 break;
52 usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead;
53 break;
55 usage = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
56 break;
58 usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopyDst |
59 wgpu::BufferUsage::CopySrc;
60 break;
62 usage = wgpu::BufferUsage::Indirect | wgpu::BufferUsage::Storage |
63 wgpu::BufferUsage::CopyDst;
64 break;
66 usage = wgpu::BufferUsage::Vertex | wgpu::BufferUsage::Storage;
67 break;
69 usage = wgpu::BufferUsage::Index | wgpu::BufferUsage::Storage;
70 break;
71 }
72
74 accessPattern == AccessPattern::kHostVisible &&
76 usage |= wgpu::BufferUsage::MapWrite;
77 }
78
79 wgpu::BufferDescriptor desc;
80#ifdef SK_DEBUG
81 desc.label = kBufferTypeNames[static_cast<int>(type)];
82#endif
83 desc.usage = usage;
84 desc.size = size;
85 // Specifying mappedAtCreation avoids clearing the buffer on the GPU which can cause MapAsync to
86 // be very slow as it waits for GPU execution to complete.
87 desc.mappedAtCreation = SkToBool(usage & wgpu::BufferUsage::MapWrite);
88
89 auto buffer = sharedContext->device().CreateBuffer(&desc);
90 if (!buffer) {
91 return {};
92 }
93
94 void* mappedAtCreationPtr = nullptr;
95 if (desc.mappedAtCreation) {
96 mappedAtCreationPtr = buffer.GetMappedRange();
97 SkASSERT(mappedAtCreationPtr);
98 }
99
100 return sk_sp<DawnBuffer>(new DawnBuffer(sharedContext,
101 size,
102 std::move(buffer),
103 type,
104 mappedAtCreationPtr,
105 std::move(label)));
106}
#define SkASSERT(cond)
Definition SkAssert.h:116
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
size_t size() const
Definition Buffer.h:19
bool drawBufferCanBeMapped() const
Definition Caps.h:235
const SharedContext * sharedContext() const
Definition Resource.h:187
const Caps * caps() const
static const uint8_t buffer[]
static const char * kBufferTypeNames[kBufferTypeCount]
static void usage(char *argv0)

◆ onDumpMemoryStatistics()

void skgpu::graphite::DawnBuffer::onDumpMemoryStatistics ( SkTraceMemoryDump traceMemoryDump,
const char *  dumpName 
) const
overrideprivatevirtual

Reimplemented from skgpu::graphite::Resource.

Definition at line 317 of file DawnBuffer.cpp.

318 {
319 Buffer::onDumpMemoryStatistics(traceMemoryDump, dumpName);
320 traceMemoryDump->dumpStringValue(
321 dumpName, "backend_label", kBufferTypeNames[static_cast<int>(fType)]);
322}
virtual void dumpStringValue(const char *, const char *, const char *)
virtual void onDumpMemoryStatistics(SkTraceMemoryDump *traceMemoryDump, const char *dumpName) const
Definition Resource.h:196

◆ onMap()

void skgpu::graphite::DawnBuffer::onMap ( )
overrideprivatevirtual

Implements skgpu::graphite::Buffer.

Definition at line 190 of file DawnBuffer.cpp.

190 {
191#if defined(__EMSCRIPTEN__)
192 SKGPU_LOG_W("Synchronous buffer mapping not supported in Dawn. Failing map request.");
193#else
194 SkASSERT(!this->sharedContext()->caps()->bufferMapsAreAsync());
195 SkASSERT(fBuffer);
196 SkASSERT((fBuffer.GetUsage() & wgpu::BufferUsage::MapRead) ||
197 (fBuffer.GetUsage() & wgpu::BufferUsage::MapWrite));
198 bool isWrite = fBuffer.GetUsage() & wgpu::BufferUsage::MapWrite;
199
200 // Use wgpu::Future and WaitAny with timeout=0 to trigger callback immediately.
201 // This should work because our resource tracking mechanism should make sure that
202 // the buffer is free of any GPU use at this point.
203 wgpu::BufferMapCallbackInfo callbackInfo{};
204 callbackInfo.mode = wgpu::CallbackMode::WaitAnyOnly;
205 callbackInfo.userdata = this;
206 callbackInfo.callback = [](WGPUBufferMapAsyncStatus s, void* userData) {
207 auto buffer = static_cast<DawnBuffer*>(userData);
208 buffer->mapCallback(s);
209 };
210
211 wgpu::FutureWaitInfo mapWaitInfo{};
212
213 mapWaitInfo.future = fBuffer.MapAsync(isWrite ? wgpu::MapMode::Write : wgpu::MapMode::Read,
214 0,
215 fBuffer.GetSize(),
216 callbackInfo);
217
218 wgpu::Device device = static_cast<const DawnSharedContext*>(sharedContext())->device();
219 wgpu::Instance instance = device.GetAdapter().GetInstance();
220 [[maybe_unused]] auto status = instance.WaitAny(1, &mapWaitInfo, /*timeoutNS=*/0);
221
222 if (status != wgpu::WaitStatus::Success) {
223 // WaitAny(timeout=0) might fail in this scenario:
224 // - Allocates a buffer.
225 // - Encodes a command buffer to copy a texture to this buffer.
226 // - Submits the command buffer. If OOM happens, this command buffer will fail to
227 // be submitted.
228 // - The buffer is *supposed* to be free of any GPU use since the command buffer that would
229 // have used it wasn't submitted successfully.
230 // - If we try to map this buffer at this point, internally Dawn will try to use GPU to
231 // clear this buffer to zeros, since this is its 1st use. WaitAny(timeout=0) won't work
232 // since the buffer now has a pending GPU clear operation.
233 //
234 // To work around this, we need to try again with a blocking WaitAny(), to wait for the
235 // clear operation to finish.
236 // Notes:
237 // - This fallback should be rare since it is caused by an OOM error during buffer
238 // readbacks.
239 // - For buffer writing cases, since we use mappedAtCreation, the GPU clear won't happen.
240 status = instance.WaitAny(
241 1, &mapWaitInfo, /*timeoutNS=*/std::numeric_limits<uint64_t>::max());
242 }
243
244 SkASSERT(status == wgpu::WaitStatus::Success);
245 SkASSERT(mapWaitInfo.completed);
246#endif // defined(__EMSCRIPTEN__)
247}
#define SKGPU_LOG_W(fmt,...)
Definition Log.h:40
VkDevice device
Definition main.cc:53
VkInstance instance
Definition main.cc:48
struct MyStruct s
SK_API bool Read(SkStreamSeekable *src, SkDocumentPage *dstArray, int dstArrayCount, const SkDeserialProcs *=nullptr)

◆ onUnmap()

void skgpu::graphite::DawnBuffer::onUnmap ( )
overrideprivatevirtual

Implements skgpu::graphite::Buffer.

Definition at line 249 of file DawnBuffer.cpp.

249 {
250 SkASSERT(fBuffer);
251 SkASSERT(this->isUnmappable());
252
253 fMapPtr = nullptr;
254 fBuffer.Unmap();
255}
bool isUnmappable() const override

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