Debian Codegraph @

tools/graphite/dawn/GraphiteDawnTestContext.cpp

raw
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 "tools/graphite/dawn/GraphiteDawnTestContext.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 "include/gpu/graphite/dawn/DawnGraphiteTypes.h"
14 #include "include/private/base/SkOnce.h"
15 #include "src/gpu/graphite/ContextOptionsPriv.h"
16 #include "tools/gpu/ContextType.h"
17 #include "tools/graphite/TestOptions.h"
18 #include "tools/graphite/dawn/GraphiteDawnToggles.h"
19
20 #include "dawn/dawn_proc.h"
21
22 #define LOG_ADAPTER 0
23
24 namespace skiatest::graphite {
25
26 // TODO: http://crbug.com/dawn/2450 - Currently manually setting the device to null and calling
27 // tick/process events one last time to ensure that the device is lost accordingly at
28 // destruction. Once device lost is, by default, a spontaneous event, remove this.
29 DawnTestContext::~DawnTestContext() {
30 fBackendContext.fDevice = nullptr;
31 tick();
32 }
33
34 std::unique_ptr<GraphiteTestContext> DawnTestContext::Make(wgpu::BackendType backend) {
35 static std::unique_ptr<dawn::native::Instance> sInstance;
36 static SkOnce sOnce;
37
38 // Creation of Instance is cheap but calling EnumerateAdapters can be expensive the first time,
39 // but then the results are cached on the Instance object. So save the Instance here so we can
40 // avoid the overhead of EnumerateAdapters on every test.
41 sOnce([&] {
42 DawnProcTable backendProcs = dawn::native::GetProcs();
43 dawnProcSetProcs(&backendProcs);
44 wgpu::InstanceDescriptor desc{};
45 // need for WaitAny with timeout > 0
46 static const auto kTimedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
47 desc.requiredFeatureCount = 1;
48 desc.requiredFeatures = &kTimedWaitAny;
49 wgpu::DawnTogglesDescriptor togglesDesc = GetInstanceToggles();
50 desc.nextInChain = &togglesDesc;
51 sInstance = std::make_unique<dawn::native::Instance>(&desc);
52 });
53
54 dawn::native::Adapter matchedAdaptor;
55
56 wgpu::RequestAdapterOptions options;
57 options.featureLevel =
58 backend == wgpu::BackendType::OpenGL || backend == wgpu::BackendType::OpenGLES
59 ? wgpu::FeatureLevel::Compatibility
60 : wgpu::FeatureLevel::Core;
61 wgpu::DawnTogglesDescriptor togglesDesc = GetAdapterToggles();
62 options.nextInChain = &togglesDesc;
63 std::vector<dawn::native::Adapter> adapters = sInstance->EnumerateAdapters(&options);
64 SkASSERT(!adapters.empty());
65 // Sort adapters by adapterType(DiscreteGPU, IntegratedGPU, CPU) and
66 // backendType(WebGPU, D3D11, D3D12, Metal, Vulkan, OpenGL, OpenGLES).
67 std::sort(
68 adapters.begin(), adapters.end(), [](dawn::native::Adapter a, dawn::native::Adapter b) {
69 wgpu::Adapter wgpuA = a.Get();
70 wgpu::Adapter wgpuB = b.Get();
71 wgpu::AdapterInfo infoA;
72 wgpu::AdapterInfo infoB;
73 wgpuA.GetInfo(&infoA);
74 wgpuB.GetInfo(&infoB);
75 return std::tuple(infoA.adapterType, infoA.backendType) <
76 std::tuple(infoB.adapterType, infoB.backendType);
77 });
78
79 for (const auto& adapter : adapters) {
80 wgpu::Adapter wgpuAdapter = adapter.Get();
81 wgpu::AdapterInfo props;
82 wgpuAdapter.GetInfo(&props);
83 if (backend == props.backendType) {
84 matchedAdaptor = adapter;
85 break;
86 }
87 }
88
89 if (!matchedAdaptor) {
90 return nullptr;
91 }
92
93 #if LOG_ADAPTER
94 {
95 wgpu::AdapterInfo debugInfo;
96 wgpu::Adapter debugAdapter = matchedAdaptor.Get();
97 debugAdapter.GetInfo(&debugInfo);
98 SkDebugf("GPU: %s\nDriver: %s\n", debugInfo.device.data, debugInfo.description.data);
99 }
100 #endif
101
102 std::vector<wgpu::FeatureName> features;
103 wgpu::Adapter adapter = matchedAdaptor.Get();
104 skiatest::graphite::AddPreferredFeatures(adapter, features);
105
106 wgpu::DeviceDescriptor desc;
107 desc.requiredFeatureCount = features.size();
108 desc.requiredFeatures = features.data();
109 desc.nextInChain = &togglesDesc;
110
111 wgpu::Limits limits = {};
112 adapter.GetLimits(&limits);
113 desc.requiredLimits = &limits;
114
115 desc.SetDeviceLostCallback(
116 wgpu::CallbackMode::AllowSpontaneous,
117 [](const wgpu::Device&, wgpu::DeviceLostReason reason, wgpu::StringView message) {
118 if (reason != wgpu::DeviceLostReason::Destroyed) {
119 SK_ABORT("Device lost: %.*s\n", static_cast<int>(message.length), message.data);
120 }
121 });
122 desc.SetUncapturedErrorCallback([](const wgpu::Device&, wgpu::ErrorType,
123 wgpu::StringView message) {
124 SkDebugf("Device error: %.*s\n", static_cast<int>(message.length), message.data);
125 });
126
127 wgpu::Device device = adapter.CreateDevice(&desc);
128 SkASSERT(device);
129
130 skgpu::graphite::DawnBackendContext backendContext;
131 backendContext.fInstance = wgpu::Instance(sInstance->Get());
132 backendContext.fDevice = device;
133 backendContext.fQueue = device.GetQueue();
134 return std::unique_ptr<GraphiteTestContext>(new DawnTestContext(backendContext));
135 }
136
137 skgpu::ContextType DawnTestContext::contextType() {
138 wgpu::AdapterInfo info;
139 fBackendContext.fDevice.GetAdapter().GetInfo(&info);
140 switch (info.backendType) {
141 case wgpu::BackendType::D3D11:
142 return skgpu::ContextType::kDawn_D3D11;
143
144 case wgpu::BackendType::D3D12:
145 return skgpu::ContextType::kDawn_D3D12;
146
147 case wgpu::BackendType::Metal:
148 return skgpu::ContextType::kDawn_Metal;
149
150 case wgpu::BackendType::Vulkan:
151 return skgpu::ContextType::kDawn_Vulkan;
152
153 case wgpu::BackendType::OpenGL:
154 return skgpu::ContextType::kDawn_OpenGL;
155
156 case wgpu::BackendType::OpenGLES:
157 return skgpu::ContextType::kDawn_OpenGLES;
158 default:
159 SK_ABORT("unexpected Dawn backend");
160 return skgpu::ContextType::kMock;
161 }
162 }
163
164 std::unique_ptr<skgpu::graphite::Context> DawnTestContext::makeContext(const TestOptions& options) {
165 skgpu::graphite::ContextOptions revisedContextOptions(options.fContextOptions);
166 skgpu::graphite::ContextOptionsPriv contextOptionsPriv;
167 if (!options.fContextOptions.fOptionsPriv) {
168 revisedContextOptions.fOptionsPriv = &contextOptionsPriv;
169 }
170 // Needed to make synchronous readPixels work
171 revisedContextOptions.fOptionsPriv->fStoreContextRefInRecorder = true;
172
173 auto backendContext = fBackendContext;
174 if (options.fNeverYieldToWebGPU) {
175 backendContext.fTick = nullptr;
176 }
177
178 return skgpu::graphite::ContextFactory::MakeDawn(backendContext, revisedContextOptions);
179 }
180
181 void DawnTestContext::tick() { fBackendContext.fTick(fBackendContext.fInstance); }
182
183 } // namespace skiatest::graphite