1 /*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/graphite/dawn/DawnSharedContext.h"
9
10 #include "include/gpu/graphite/Context.h"
11 #include "include/gpu/graphite/ContextOptions.h"
12 #include "include/gpu/graphite/dawn/DawnBackendContext.h"
13 #include "src/gpu/graphite/Log.h"
14 #include "src/gpu/graphite/dawn/DawnResourceProvider.h"
15
16 #include "webgpu/webgpu_cpp.h" // NO_G3_REWRITE
17
18 namespace skgpu::graphite {
19 namespace {
20
CreateNoopFragment(const wgpu::Device & device)21 wgpu::ShaderModule CreateNoopFragment(const wgpu::Device& device) {
22 #ifdef WGPU_BREAKING_CHANGE_DROP_DESCRIPTOR
23 wgpu::ShaderSourceWGSL wgslDesc;
24 #else
25 wgpu::ShaderModuleWGSLDescriptor wgslDesc;
26 #endif
27 wgslDesc.code =
28 "@fragment\n"
29 "fn main() {}\n";
30 wgpu::ShaderModuleDescriptor smDesc;
31 smDesc.nextInChain = &wgslDesc;
32 smDesc.label = "no-op";
33 auto fsModule = device.CreateShaderModule(&smDesc);
34 return fsModule;
35 }
36
37 }
38
Make(const DawnBackendContext & backendContext,const ContextOptions & options)39 sk_sp<SharedContext> DawnSharedContext::Make(const DawnBackendContext& backendContext,
40 const ContextOptions& options) {
41 if (!backendContext.fDevice || !backendContext.fQueue) {
42 return {};
43 }
44
45 auto noopFragment = CreateNoopFragment(backendContext.fDevice);
46 if (!noopFragment) {
47 return {};
48 }
49
50 auto caps = std::make_unique<const DawnCaps>(backendContext, options);
51
52 return sk_sp<SharedContext>(new DawnSharedContext(backendContext,
53 std::move(caps),
54 std::move(noopFragment)));
55 }
56
DawnSharedContext(const DawnBackendContext & backendContext,std::unique_ptr<const DawnCaps> caps,wgpu::ShaderModule noopFragment)57 DawnSharedContext::DawnSharedContext(const DawnBackendContext& backendContext,
58 std::unique_ptr<const DawnCaps> caps,
59 wgpu::ShaderModule noopFragment)
60 : skgpu::graphite::SharedContext(std::move(caps), BackendApi::kDawn)
61 , fInstance(backendContext.fInstance)
62 , fDevice(backendContext.fDevice)
63 , fQueue(backendContext.fQueue)
64 , fTick(backendContext.fTick)
65 , fNoopFragment(std::move(noopFragment)) {}
66
~DawnSharedContext()67 DawnSharedContext::~DawnSharedContext() {
68 // need to clear out resources before any allocator is removed
69 this->globalCache()->deleteResources();
70 }
71
makeResourceProvider(SingleOwner * singleOwner,uint32_t recorderID,size_t resourceBudget,bool)72 std::unique_ptr<ResourceProvider> DawnSharedContext::makeResourceProvider(
73 SingleOwner* singleOwner,
74 uint32_t recorderID,
75 size_t resourceBudget,
76 bool /* avoidBufferAlloc */) {
77 return std::unique_ptr<ResourceProvider>(new DawnResourceProvider(this,
78 singleOwner,
79 recorderID,
80 resourceBudget));
81 }
82
deviceTick(Context * context)83 void DawnSharedContext::deviceTick(Context* context) {
84 #if !defined(__EMSCRIPTEN__)
85 this->device().Tick();
86 #endif
87 context->checkAsyncWorkCompletion();
88 };
89
90 } // namespace skgpu::graphite
91