Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Member Functions | List of all members
skgpu::graphite::DawnResourceProvider Class Referencefinal

#include <DawnResourceProvider.h>

Inheritance diagram for skgpu::graphite::DawnResourceProvider:
skgpu::graphite::ResourceProvider

Public Member Functions

 DawnResourceProvider (SharedContext *sharedContext, SingleOwner *, uint32_t recorderID, size_t resourceBudget)
 
 ~DawnResourceProvider () override
 
sk_sp< TexturecreateWrappedTexture (const BackendTexture &) override
 
sk_sp< DawnTexturefindOrCreateDiscardableMSAALoadTexture (SkISize dimensions, const TextureInfo &msaaInfo)
 
wgpu::RenderPipeline findOrCreateBlitWithDrawPipeline (const RenderPassDesc &renderPassDesc)
 
sk_sp< DawnBufferfindOrCreateDawnBuffer (size_t size, BufferType type, AccessPattern, std::string_view label)
 
const wgpu::BindGroupLayout & getOrCreateUniformBuffersBindGroupLayout ()
 
const wgpu::BindGroupLayout & getOrCreateSingleTextureSamplerBindGroupLayout ()
 
const wgpu::BindGroup & findOrCreateUniformBuffersBindGroup (const std::array< std::pair< const DawnBuffer *, uint32_t >, 3 > &boundBuffersAndSizes)
 
const wgpu::BindGroup & findOrCreateSingleTextureSamplerBindGroup (const DawnSampler *sampler, const DawnTexture *texture)
 
- Public Member Functions inherited from skgpu::graphite::ResourceProvider
virtual ~ResourceProvider ()
 
sk_sp< GraphicsPipelinefindOrCreateGraphicsPipeline (const RuntimeEffectDictionary *, const GraphicsPipelineDesc &, const RenderPassDesc &)
 
sk_sp< ComputePipelinefindOrCreateComputePipeline (const ComputePipelineDesc &)
 
sk_sp< TexturefindOrCreateScratchTexture (SkISize, const TextureInfo &, skgpu::Budgeted)
 
sk_sp< TexturefindOrCreateDepthStencilAttachment (SkISize dimensions, const TextureInfo &)
 
sk_sp< TexturefindOrCreateDiscardableMSAAAttachment (SkISize dimensions, const TextureInfo &)
 
sk_sp< BufferfindOrCreateBuffer (size_t size, BufferType type, AccessPattern, std::string_view label)
 
sk_sp< SamplerfindOrCreateCompatibleSampler (const SamplerDesc &)
 
BackendTexture createBackendTexture (SkISize dimensions, const TextureInfo &)
 
void deleteBackendTexture (const BackendTexture &)
 
ProxyCacheproxyCache ()
 
size_t getResourceCacheLimit () const
 
size_t getResourceCacheCurrentBudgetedBytes () const
 
void dumpMemoryStatistics (SkTraceMemoryDump *traceMemoryDump) const
 
void freeGpuResources ()
 
void purgeResourcesNotUsedSince (StdSteadyClock::time_point purgeTime)
 

Private Member Functions

sk_sp< GraphicsPipelinecreateGraphicsPipeline (const RuntimeEffectDictionary *, const GraphicsPipelineDesc &, const RenderPassDesc &) override
 
sk_sp< ComputePipelinecreateComputePipeline (const ComputePipelineDesc &) override
 
sk_sp< TexturecreateTexture (SkISize, const TextureInfo &, skgpu::Budgeted) override
 
sk_sp< BuffercreateBuffer (size_t size, BufferType type, AccessPattern, std::string_view label) override
 
sk_sp< SamplercreateSampler (const SamplerDesc &) override
 
BackendTexture onCreateBackendTexture (SkISize dimensions, const TextureInfo &) override
 
void onDeleteBackendTexture (const BackendTexture &) override
 

Additional Inherited Members

- Protected Member Functions inherited from skgpu::graphite::ResourceProvider
 ResourceProvider (SharedContext *sharedContext, SingleOwner *singleOwner, uint32_t recorderID, size_t resourceBudget)
 
- Protected Attributes inherited from skgpu::graphite::ResourceProvider
SharedContextfSharedContext
 
sk_sp< ResourceCachefResourceCache
 

Detailed Description

Definition at line 24 of file DawnResourceProvider.h.

Constructor & Destructor Documentation

◆ DawnResourceProvider()

skgpu::graphite::DawnResourceProvider::DawnResourceProvider ( SharedContext sharedContext,
SingleOwner singleOwner,
uint32_t  recorderID,
size_t  resourceBudget 
)

Definition at line 154 of file DawnResourceProvider.cpp.

158 : ResourceProvider(sharedContext, singleOwner, recorderID, resourceBudget)
159 , fUniformBufferBindGroupCache(kMaxNumberOfCachedBufferBindGroups)
160 , fSingleTextureSamplerBindGroups(kMaxNumberOfCachedTextureBindGroups) {}

◆ ~DawnResourceProvider()

skgpu::graphite::DawnResourceProvider::~DawnResourceProvider ( )
overridedefault

Member Function Documentation

◆ createBuffer()

sk_sp< Buffer > skgpu::graphite::DawnResourceProvider::createBuffer ( size_t  size,
BufferType  type,
AccessPattern  accessPattern,
std::string_view  label 
)
overrideprivatevirtual

Implements skgpu::graphite::ResourceProvider.

Definition at line 285 of file DawnResourceProvider.cpp.

288 {
289 return DawnBuffer::Make(this->dawnSharedContext(),
290 size,
291 type,
292 accessPattern,
293 std::move(label));
294}
static sk_sp< DawnBuffer > Make(const DawnSharedContext *, size_t size, BufferType type, AccessPattern, std::string_view label)

◆ createComputePipeline()

sk_sp< ComputePipeline > skgpu::graphite::DawnResourceProvider::createComputePipeline ( const ComputePipelineDesc desc)
overrideprivatevirtual

Implements skgpu::graphite::ResourceProvider.

Definition at line 274 of file DawnResourceProvider.cpp.

275 {
276 return DawnComputePipeline::Make(this->dawnSharedContext(), desc);
277}
static sk_sp< DawnComputePipeline > Make(const DawnSharedContext *, const ComputePipelineDesc &)

◆ createGraphicsPipeline()

sk_sp< GraphicsPipeline > skgpu::graphite::DawnResourceProvider::createGraphicsPipeline ( const RuntimeEffectDictionary runtimeDict,
const GraphicsPipelineDesc pipelineDesc,
const RenderPassDesc renderPassDesc 
)
overrideprivatevirtual

Implements skgpu::graphite::ResourceProvider.

Definition at line 263 of file DawnResourceProvider.cpp.

266 {
267 return DawnGraphicsPipeline::Make(this->dawnSharedContext(),
268 this,
269 runtimeDict,
270 pipelineDesc,
271 renderPassDesc);
272}
static sk_sp< DawnGraphicsPipeline > Make(const DawnSharedContext *sharedContext, DawnResourceProvider *resourceProvider, const RuntimeEffectDictionary *runtimeDict, const GraphicsPipelineDesc &pipelineDesc, const RenderPassDesc &renderPassDesc)

◆ createSampler()

sk_sp< Sampler > skgpu::graphite::DawnResourceProvider::createSampler ( const SamplerDesc samplerDesc)
overrideprivatevirtual

Implements skgpu::graphite::ResourceProvider.

Definition at line 296 of file DawnResourceProvider.cpp.

296 {
297 return DawnSampler::Make(this->dawnSharedContext(),
298 samplerDesc.samplingOptions(),
299 samplerDesc.tileModeX(),
300 samplerDesc.tileModeY());
301}
static sk_sp< DawnSampler > Make(const DawnSharedContext *, const SkSamplingOptions &samplingOptions, SkTileMode xTileMode, SkTileMode yTileMode)

◆ createTexture()

sk_sp< Texture > skgpu::graphite::DawnResourceProvider::createTexture ( SkISize  dimensions,
const TextureInfo info,
skgpu::Budgeted  budgeted 
)
overrideprivatevirtual

Implements skgpu::graphite::ResourceProvider.

Definition at line 279 of file DawnResourceProvider.cpp.

281 {
282 return DawnTexture::Make(this->dawnSharedContext(), dimensions, info, budgeted);
283}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
static sk_sp< Texture > Make(const DawnSharedContext *, SkISize dimensions, const TextureInfo &, skgpu::Budgeted)

◆ createWrappedTexture()

sk_sp< Texture > skgpu::graphite::DawnResourceProvider::createWrappedTexture ( const BackendTexture texture)
overridevirtual

Implements skgpu::graphite::ResourceProvider.

Definition at line 215 of file DawnResourceProvider.cpp.

215 {
216 // Convert to smart pointers. wgpu::Texture* constructor will increment the ref count.
217 wgpu::Texture dawnTexture = texture.getDawnTexturePtr();
218 wgpu::TextureView dawnTextureView = texture.getDawnTextureViewPtr();
219 SkASSERT(!dawnTexture || !dawnTextureView);
220
221 if (!dawnTexture && !dawnTextureView) {
222 return {};
223 }
224
225 if (dawnTexture) {
226 return DawnTexture::MakeWrapped(this->dawnSharedContext(),
227 texture.dimensions(),
228 texture.info(),
229 std::move(dawnTexture));
230 } else {
231 return DawnTexture::MakeWrapped(this->dawnSharedContext(),
232 texture.dimensions(),
233 texture.info(),
234 std::move(dawnTextureView));
235 }
236}
#define SkASSERT(cond)
Definition SkAssert.h:116
static sk_sp< Texture > MakeWrapped(const DawnSharedContext *, SkISize dimensions, const TextureInfo &, wgpu::Texture)
FlTexture * texture

◆ findOrCreateBlitWithDrawPipeline()

wgpu::RenderPipeline skgpu::graphite::DawnResourceProvider::findOrCreateBlitWithDrawPipeline ( const RenderPassDesc renderPassDesc)

Definition at line 164 of file DawnResourceProvider.cpp.

165 {
166 uint64_t renderPassKey =
167 this->dawnSharedContext()->dawnCaps()->getRenderPassDescKey(renderPassDesc);
168 wgpu::RenderPipeline pipeline = fBlitWithDrawPipelines[renderPassKey];
169 if (!pipeline) {
170 static constexpr char kVertexShaderText[] = R"(
171 var<private> fullscreenTriPositions : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
172 vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
173
174 @vertex
175 fn main(@builtin(vertex_index) vertexIndex : u32) -> @builtin(position) vec4<f32> {
176 return vec4(fullscreenTriPositions[vertexIndex], 1.0, 1.0);
177 }
178 )";
179
180 static constexpr char kFragmentShaderText[] = R"(
181 @group(0) @binding(0) var colorMap: texture_2d<f32>;
182
183 @fragment
184 fn main(@builtin(position) fragPosition : vec4<f32>) -> @location(0) vec4<f32> {
185 var coords : vec2<i32> = vec2<i32>(i32(fragPosition.x), i32(fragPosition.y));
186 return textureLoad(colorMap, coords, 0);
187 }
188 )";
189
190 auto vsModule = create_shader_module(dawnSharedContext()->device(), kVertexShaderText);
191 auto fsModule = create_shader_module(dawnSharedContext()->device(), kFragmentShaderText);
192
193 pipeline = create_blit_render_pipeline(
194 dawnSharedContext(),
195 /*label=*/"BlitWithDraw",
196 std::move(vsModule),
197 std::move(fsModule),
198 /*renderPassColorFormat=*/
199 renderPassDesc.fColorAttachment.fTextureInfo.dawnTextureSpec().getViewFormat(),
200 /*renderPassDepthStencilFormat=*/
201 renderPassDesc.fDepthStencilAttachment.fTextureInfo.isValid()
202 ? renderPassDesc.fDepthStencilAttachment.fTextureInfo.dawnTextureSpec()
203 .getViewFormat()
204 : wgpu::TextureFormat::Undefined,
205 /*numSamples=*/renderPassDesc.fColorAttachment.fTextureInfo.numSamples());
206
207 if (pipeline) {
208 fBlitWithDrawPipelines.set(renderPassKey, pipeline);
209 }
210 }
211
212 return pipeline;
213}
uint64_t getRenderPassDescKey(const RenderPassDesc &renderPassDesc) const
Definition DawnCaps.cpp:836
const DawnCaps * dawnCaps() const
V * set(K key, V val)
Definition SkTHash.h:472
VkDevice device
Definition main.cc:53

◆ findOrCreateDawnBuffer()

sk_sp< DawnBuffer > skgpu::graphite::DawnResourceProvider::findOrCreateDawnBuffer ( size_t  size,
BufferType  type,
AccessPattern  accessPattern,
std::string_view  label 
)

Definition at line 336 of file DawnResourceProvider.cpp.

339 {
340 sk_sp<Buffer> buffer = this->findOrCreateBuffer(size, type, accessPattern, std::move(label));
341 DawnBuffer* ptr = static_cast<DawnBuffer*>(buffer.release());
342 return sk_sp<DawnBuffer>(ptr);
343}
sk_sp< Buffer > findOrCreateBuffer(size_t size, BufferType type, AccessPattern, std::string_view label)
static const uint8_t buffer[]

◆ findOrCreateDiscardableMSAALoadTexture()

sk_sp< DawnTexture > skgpu::graphite::DawnResourceProvider::findOrCreateDiscardableMSAALoadTexture ( SkISize  dimensions,
const TextureInfo msaaInfo 
)

Definition at line 238 of file DawnResourceProvider.cpp.

239 {
240 SkASSERT(msaaInfo.isValid());
241
242 // Derive the load texture's info from MSAA texture's info.
243 DawnTextureInfo dawnMsaaLoadTextureInfo;
244 msaaInfo.getDawnTextureInfo(&dawnMsaaLoadTextureInfo);
245 dawnMsaaLoadTextureInfo.fSampleCount = 1;
246 dawnMsaaLoadTextureInfo.fUsage |= wgpu::TextureUsage::TextureBinding;
247
248#if !defined(__EMSCRIPTEN__)
249 // MSAA texture can be transient attachment (memoryless) but the load texture cannot be.
250 // This is because the load texture will need to have its content retained between two passes
251 // loading:
252 // - first pass: the resolve texture is blitted to the load texture.
253 // - 2nd pass: the actual render pass is started and the load texture is blitted to the MSAA
254 // texture.
255 dawnMsaaLoadTextureInfo.fUsage &= (~wgpu::TextureUsage::TransientAttachment);
256#endif
257
258 auto texture = this->findOrCreateDiscardableMSAAAttachment(dimensions, dawnMsaaLoadTextureInfo);
259
260 return sk_sp<DawnTexture>(static_cast<DawnTexture*>(texture.release()));
261}
sk_sp< Texture > findOrCreateDiscardableMSAAAttachment(SkISize dimensions, const TextureInfo &)

◆ findOrCreateSingleTextureSamplerBindGroup()

const wgpu::BindGroup & skgpu::graphite::DawnResourceProvider::findOrCreateSingleTextureSamplerBindGroup ( const DawnSampler sampler,
const DawnTexture texture 
)

Definition at line 479 of file DawnResourceProvider.cpp.

480 {
481 auto key = make_texture_bind_group_key(sampler, texture);
482 auto* existingBindGroup = fSingleTextureSamplerBindGroups.find(key);
483 if (existingBindGroup) {
484 // cache hit.
485 return *existingBindGroup;
486 }
487
488 std::array<wgpu::BindGroupEntry, 2> entries;
489
490 entries[0].binding = 0;
491 entries[0].sampler = sampler->dawnSampler();
492 entries[1].binding = 1;
493 entries[1].textureView = texture->sampleTextureView();
494
495 wgpu::BindGroupDescriptor desc;
497 desc.entryCount = entries.size();
498 desc.entries = entries.data();
499
500 const auto& device = this->dawnSharedContext()->device();
501 auto bindGroup = device.CreateBindGroup(&desc);
502
503 return *fSingleTextureSamplerBindGroups.insert(key, bindGroup);
504}
V * find(const K &key)
Definition SkLRUCache.h:49
V * insert(const K &key, V value)
Definition SkLRUCache.h:62
const wgpu::BindGroupLayout & getOrCreateSingleTextureSamplerBindGroupLayout()
const wgpu::Device & device() const

◆ findOrCreateUniformBuffersBindGroup()

const wgpu::BindGroup & skgpu::graphite::DawnResourceProvider::findOrCreateUniformBuffersBindGroup ( const std::array< std::pair< const DawnBuffer *, uint32_t >, 3 > &  boundBuffersAndSizes)

Definition at line 435 of file DawnResourceProvider.cpp.

436 {
437 auto key = make_ubo_bind_group_key(boundBuffersAndSizes);
438 auto* existingBindGroup = fUniformBufferBindGroupCache.find(key);
439 if (existingBindGroup) {
440 // cache hit.
441 return *existingBindGroup;
442 }
443
444 // Translate to wgpu::BindGroupDescriptor
445 std::array<wgpu::BindGroupEntry, 3> entries;
446
447 constexpr uint32_t kBindingIndices[] = {
451 };
452
453 for (uint32_t i = 0; i < boundBuffersAndSizes.size(); ++i) {
454 const DawnBuffer* boundBuffer = boundBuffersAndSizes[i].first;
455 const uint32_t bindingSize = boundBuffersAndSizes[i].second;
456
457 entries[i].binding = kBindingIndices[i];
458 entries[i].offset = 0;
459 if (boundBuffer) {
460 entries[i].buffer = boundBuffer->dawnBuffer();
461 entries[i].size = SkAlignTo(bindingSize, kBufferBindingSizeAlignment);
462 } else {
463 entries[i].buffer = this->getOrCreateNullBuffer();
464 entries[i].size = wgpu::kWholeSize;
465 }
466 }
467
468 wgpu::BindGroupDescriptor desc;
470 desc.entryCount = entries.size();
471 desc.entries = entries.data();
472
473 const auto& device = this->dawnSharedContext()->device();
474 auto bindGroup = device.CreateBindGroup(&desc);
475
476 return *fUniformBufferBindGroupCache.insert(key, bindGroup);
477}
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition SkAlign.h:33
static constexpr unsigned int kRenderStepUniformBufferIndex
static constexpr unsigned int kPaintUniformBufferIndex
static constexpr unsigned int kIntrinsicUniformBufferIndex
const wgpu::BindGroupLayout & getOrCreateUniformBuffersBindGroupLayout()

◆ getOrCreateSingleTextureSamplerBindGroupLayout()

const wgpu::BindGroupLayout & skgpu::graphite::DawnResourceProvider::getOrCreateSingleTextureSamplerBindGroupLayout ( )

Definition at line 387 of file DawnResourceProvider.cpp.

387 {
388 if (fSingleTextureSamplerBindGroupLayout) {
389 return fSingleTextureSamplerBindGroupLayout;
390 }
391
392 std::array<wgpu::BindGroupLayoutEntry, 2> entries;
393
394 entries[0].binding = 0;
395 entries[0].visibility = wgpu::ShaderStage::Fragment;
396 entries[0].sampler.type = wgpu::SamplerBindingType::Filtering;
397
398 entries[1].binding = 1;
399 entries[1].visibility = wgpu::ShaderStage::Fragment;
400 entries[1].texture.sampleType = wgpu::TextureSampleType::Float;
401 entries[1].texture.viewDimension = wgpu::TextureViewDimension::e2D;
402 entries[1].texture.multisampled = false;
403
404 wgpu::BindGroupLayoutDescriptor groupLayoutDesc;
405#if defined(SK_DEBUG)
406 groupLayoutDesc.label = "Single texture + sampler bind group layout";
407#endif
408
409 groupLayoutDesc.entryCount = entries.size();
410 groupLayoutDesc.entries = entries.data();
411 fSingleTextureSamplerBindGroupLayout =
412 this->dawnSharedContext()->device().CreateBindGroupLayout(&groupLayoutDesc);
413
414 return fSingleTextureSamplerBindGroupLayout;
415}

◆ getOrCreateUniformBuffersBindGroupLayout()

const wgpu::BindGroupLayout & skgpu::graphite::DawnResourceProvider::getOrCreateUniformBuffersBindGroupLayout ( )

Definition at line 345 of file DawnResourceProvider.cpp.

345 {
346 if (fUniformBuffersBindGroupLayout) {
347 return fUniformBuffersBindGroupLayout;
348 }
349
350 std::array<wgpu::BindGroupLayoutEntry, 3> entries;
352 entries[0].visibility = wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment;
353 entries[0].buffer.type = wgpu::BufferBindingType::Uniform;
354 entries[0].buffer.hasDynamicOffset = true;
355 entries[0].buffer.minBindingSize = 0;
356
358 entries[1].visibility = wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment;
359 entries[1].buffer.type = fSharedContext->caps()->storageBufferPreferred()
360 ? wgpu::BufferBindingType::ReadOnlyStorage
361 : wgpu::BufferBindingType::Uniform;
362 entries[1].buffer.hasDynamicOffset = true;
363 entries[1].buffer.minBindingSize = 0;
364
366 entries[2].visibility = wgpu::ShaderStage::Fragment;
367 entries[2].buffer.type = fSharedContext->caps()->storageBufferPreferred()
368 ? wgpu::BufferBindingType::ReadOnlyStorage
369 : wgpu::BufferBindingType::Uniform;
370 entries[2].buffer.hasDynamicOffset = true;
371 entries[2].buffer.minBindingSize = 0;
372
373 wgpu::BindGroupLayoutDescriptor groupLayoutDesc;
374#if defined(SK_DEBUG)
375 groupLayoutDesc.label = "Uniform buffers bind group layout";
376#endif
377
378 groupLayoutDesc.entryCount = entries.size();
379 groupLayoutDesc.entries = entries.data();
380 fUniformBuffersBindGroupLayout =
381 this->dawnSharedContext()->device().CreateBindGroupLayout(&groupLayoutDesc);
382
383 return fUniformBuffersBindGroupLayout;
384}
bool storageBufferPreferred() const
Definition Caps.h:232
const Caps * caps() const

◆ onCreateBackendTexture()

BackendTexture skgpu::graphite::DawnResourceProvider::onCreateBackendTexture ( SkISize  dimensions,
const TextureInfo info 
)
overrideprivatevirtual

Implements skgpu::graphite::ResourceProvider.

Definition at line 303 of file DawnResourceProvider.cpp.

304 {
305 wgpu::Texture texture = DawnTexture::MakeDawnTexture(this->dawnSharedContext(),
306 dimensions,
307 info);
308 if (!texture) {
309 return {};
310 }
311
312 return BackendTexture(texture.MoveToCHandle());
313}
static wgpu::Texture MakeDawnTexture(const DawnSharedContext *, SkISize dimensions, const TextureInfo &)

◆ onDeleteBackendTexture()

void skgpu::graphite::DawnResourceProvider::onDeleteBackendTexture ( const BackendTexture texture)
overrideprivatevirtual

Implements skgpu::graphite::ResourceProvider.

Definition at line 315 of file DawnResourceProvider.cpp.

315 {
316 SkASSERT(texture.isValid());
317 SkASSERT(texture.backend() == BackendApi::kDawn);
318
319 // Automatically release the pointers in wgpu::TextureView & wgpu::Texture's dtor.
320 // Acquire() won't increment the ref count.
321 wgpu::TextureView::Acquire(texture.getDawnTextureViewPtr());
322 // We need to explicitly call Destroy() here since since that is the recommended way to delete
323 // a Dawn texture predictably versus just dropping a ref and relying on garbage collection.
324 //
325 // Additionally this helps to work around an issue where Skia may have cached a BindGroup that
326 // references the underlying texture. Skia currently doesn't destroy BindGroups when its use of
327 // the texture goes away, thus a ref to the texture remains on the BindGroup and memory is never
328 // cleared up unless we call Destroy() here.
329 wgpu::Texture::Acquire(texture.getDawnTexturePtr()).Destroy();
330}

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