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/DawnTypes.h"
13 #include "include/gpu/graphite/dawn/DawnUtils.h"
14 #include "include/private/base/SkOnce.h"
15
16 #include "dawn/dawn_proc.h"
17
18 #define LOG_ADAPTER 0
19
20 namespace skiatest::graphite {
21
Make()22 std::unique_ptr<GraphiteTestContext> DawnTestContext::Make() {
23 static std::unique_ptr<dawn::native::Instance> gInstance;
24 static dawn::native::Adapter gAdapter;
25 static SkOnce gOnce;
26
27 gOnce([]{
28 gInstance = std::make_unique<dawn::native::Instance>();
29
30 gInstance->DiscoverDefaultAdapters();
31 DawnProcTable backendProcs = dawn::native::GetProcs();
32 dawnProcSetProcs(&backendProcs);
33
34 std::vector<dawn::native::Adapter> adapters = gInstance->GetAdapters();
35 SkASSERT(!adapters.empty());
36 // Sort adapters by adapterType(DiscreteGPU, IntegratedGPU, CPU) and
37 // backendType(WebGPU, D3D11, D3D12, Metal, Vulkan, OpenGL, OpenGLES).
38 std::sort(
39 adapters.begin(), adapters.end(),
40 [](dawn::native::Adapter a, dawn::native::Adapter b) {
41 wgpu::AdapterProperties propA;
42 wgpu::AdapterProperties propB;
43 a.GetProperties(&propA);
44 b.GetProperties(&propB);
45 return std::tuple(propA.adapterType, propA.backendType) <
46 std::tuple(propB.adapterType, propB.backendType);
47 });
48
49 gAdapter = adapters.front();
50
51 #if LOG_ADAPTER
52 wgpu::AdapterProperties properties;
53 gAdapter.GetProperties(&properties);
54 SkDebugf("GPU: %s\nDriver: %s\n", properties.name, properties.driverDescription);
55 #endif
56 });
57
58 std::array<wgpu::FeatureName, 2> features = {
59 wgpu::FeatureName::DepthClipControl,
60 wgpu::FeatureName::Depth32FloatStencil8,
61 };
62 wgpu::DeviceDescriptor desc;
63 desc.requiredFeaturesCount = features.size();
64 desc.requiredFeatures = features.data();
65
66 #if !defined(SK_DEBUG)
67 wgpu::DawnTogglesDescriptor deviceTogglesDesc;
68 std::array<const char*, 1> toggles = {
69 "skip_validation",
70 };
71 deviceTogglesDesc.enabledTogglesCount = toggles.size();
72 deviceTogglesDesc.enabledToggles = toggles.data();
73 desc.nextInChain = &deviceTogglesDesc;
74 #endif
75 auto device = wgpu::Device::Acquire(gAdapter.CreateDevice(&desc));
76 SkASSERT(device);
77 device.SetUncapturedErrorCallback(
78 [](WGPUErrorType type, const char* message, void*) {
79 SkDebugf("Device error: %s\n", message);
80 },
81 0);
82 device.SetDeviceLostCallback(
83 [](WGPUDeviceLostReason reason, const char* message, void*) {
84 if (reason != WGPUDeviceLostReason_Destroyed) {
85 SK_ABORT("Device lost: %s\n", message);
86 }
87 },
88 0);
89
90 skgpu::graphite::DawnBackendContext backendContext;
91 backendContext.fDevice = device;
92 backendContext.fQueue = device.GetQueue();
93 return std::unique_ptr<GraphiteTestContext>(new DawnTestContext(backendContext));
94 }
95
makeContext()96 std::unique_ptr<skgpu::graphite::Context> DawnTestContext::makeContext() {
97 skgpu::graphite::ContextOptions contextOptions;
98 contextOptions.fStoreContextRefInRecorder = true; // Needed to make synchronous readPixels work
99 return skgpu::graphite::ContextFactory::MakeDawn(fBackendContext,
100 contextOptions);
101 }
102
103 } // namespace skiatest::graphite
104
105