blob: f30c5ecf5227f7c3141a9b746ee648d1b7c0abab [file] [log] [blame]
Stephen White985741a2019-07-18 11:43:45 -04001/*
2 * Copyright 2019 Google Inc.
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
Stephen Whitea521c962019-12-04 09:57:48 -05008#include "dawn/webgpu_cpp.h"
Mike Klein52337de2019-07-25 09:00:52 -05009#include "tools/gpu/dawn/DawnTestContext.h"
Stephen White985741a2019-07-18 11:43:45 -040010
11#ifdef SK_BUILD_FOR_UNIX
12#include "GL/glx.h"
13#endif
14
15#ifdef SK_BUILD_FOR_WIN
16#include <windows.h>
17#endif
18
19#define USE_OPENGL_BACKEND 0
20
21#ifdef SK_DAWN
Stephen Whitea521c962019-12-04 09:57:48 -050022#include "dawn/webgpu.h"
Stephen White1e2adcf2019-10-16 09:48:53 -040023#include "dawn/dawn_proc.h"
Stephen White985741a2019-07-18 11:43:45 -040024#include "include/gpu/GrContext.h"
25#include "tools/AutoreleasePool.h"
26#if USE_OPENGL_BACKEND
27#include "dawn_native/OpenGLBackend.h"
28#endif
29
30#if defined(SK_BUILD_FOR_MAC) && USE_OPENGL_BACKEND
31#include <dlfcn.h>
32static void* getProcAddressMacOS(const char* procName) {
33 return dlsym(RTLD_DEFAULT, procName);
34}
35#endif
36
37namespace {
38
39#ifdef SK_BUILD_FOR_WIN
40class ProcGetter {
41public:
42 typedef void(*Proc)();
43
44 ProcGetter()
45 : fModule(LoadLibraryA("opengl32.dll")) {
46 SkASSERT(!fInstance);
47 fInstance = this;
48 }
49
50 ~ProcGetter() {
51 if (fModule) {
52 FreeLibrary(fModule);
53 }
54 fInstance = nullptr;
55 }
56
57 static void* getProcAddress(const char* name) {
58 return fInstance->getProc(name);
59 }
60
61private:
62 Proc getProc(const char* name) {
63 PROC proc;
Stephen White883c7e92019-10-17 10:44:37 -040064 if ((proc = GetProcAddress(fModule, name))) {
Stephen White985741a2019-07-18 11:43:45 -040065 return (Proc) proc;
66 }
Stephen White883c7e92019-10-17 10:44:37 -040067 if ((proc = wglGetProcAddress(name))) {
Stephen White985741a2019-07-18 11:43:45 -040068 return (Proc) proc;
69 }
70 return nullptr;
71 }
72
73 HMODULE fModule;
74 static ProcGetter* fInstance;
75};
76
77ProcGetter* ProcGetter::fInstance;
78#endif
79
Stephen White985741a2019-07-18 11:43:45 -040080class DawnTestContextImpl : public sk_gpu_test::DawnTestContext {
81public:
Stephen White3cc8d4f2019-10-30 09:56:23 -040082 static wgpu::Device createDevice(const dawn_native::Instance& instance,
Stephen White985741a2019-07-18 11:43:45 -040083 dawn_native::BackendType type) {
84 DawnProcTable backendProcs = dawn_native::GetProcs();
Stephen White1e2adcf2019-10-16 09:48:53 -040085 dawnProcSetProcs(&backendProcs);
Stephen White985741a2019-07-18 11:43:45 -040086
87 std::vector<dawn_native::Adapter> adapters = instance.GetAdapters();
88 for (dawn_native::Adapter adapter : adapters) {
89 if (adapter.GetBackendType() == type) {
Stephen Whitef40cef82020-05-29 13:58:39 -040090 return wgpu::Device::Acquire(adapter.CreateDevice());
Stephen White985741a2019-07-18 11:43:45 -040091 }
92 }
93 return nullptr;
94 }
95
96 static DawnTestContext* Create(DawnTestContext* sharedContext) {
97 std::unique_ptr<dawn_native::Instance> instance = std::make_unique<dawn_native::Instance>();
Stephen White3cc8d4f2019-10-30 09:56:23 -040098 wgpu::Device device;
Stephen White985741a2019-07-18 11:43:45 -040099 if (sharedContext) {
100 device = sharedContext->getDevice();
101 } else {
102 dawn_native::BackendType type;
103#if USE_OPENGL_BACKEND
104 dawn_native::opengl::AdapterDiscoveryOptions adapterOptions;
105 adapterOptions.getProc = reinterpret_cast<void*(*)(const char*)>(
106#if defined(SK_BUILD_FOR_UNIX)
107 glXGetProcAddress
108#elif defined(SK_BUILD_FOR_MAC)
109 getProcAddressMacOS
110#elif defined(SK_BUILD_FOR_WIN)
111 ProcGetter::getProcAddress
112#endif
113 );
114 instance->DiscoverAdapters(&adapterOptions);
115 type = dawn_native::BackendType::OpenGL;
116#else
117 instance->DiscoverDefaultAdapters();
118#if defined(SK_BUILD_FOR_MAC)
119 type = dawn_native::BackendType::Metal;
120#elif defined(SK_BUILD_FOR_WIN)
121 type = dawn_native::BackendType::D3D12;
122#elif defined(SK_BUILD_FOR_UNIX)
123 type = dawn_native::BackendType::Vulkan;
124#endif
125#endif
126 device = createDevice(*instance, type);
127 }
128 if (!device) {
129 return nullptr;
130 }
131 return new DawnTestContextImpl(std::move(instance), device);
132 }
133
134 ~DawnTestContextImpl() override { this->teardown(); }
135
136 void testAbandon() override {}
137
Stephen White985741a2019-07-18 11:43:45 -0400138 void finish() override {}
139
140 sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override {
141 return GrContext::MakeDawn(fDevice, options);
142 }
143
144protected:
145 void teardown() override {
146 INHERITED::teardown();
147 }
148
149private:
150 DawnTestContextImpl(std::unique_ptr<dawn_native::Instance> instance,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400151 const wgpu::Device& device)
Stephen White64c8b812020-06-03 14:05:48 -0400152 : DawnTestContext(std::move(instance), device) {
Greg Daniel02497d42020-02-21 15:46:27 -0500153 fFenceSupport = true;
Stephen White985741a2019-07-18 11:43:45 -0400154 }
155
Robert Phillipsedf3f382020-02-13 12:59:19 -0500156 void onPlatformMakeNotCurrent() const override {}
Stephen White985741a2019-07-18 11:43:45 -0400157 void onPlatformMakeCurrent() const override {}
158 std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; }
Stephen White985741a2019-07-18 11:43:45 -0400159
160 typedef sk_gpu_test::DawnTestContext INHERITED;
161};
162} // anonymous namespace
163
164namespace sk_gpu_test {
165DawnTestContext* CreatePlatformDawnTestContext(DawnTestContext* sharedContext) {
166 return DawnTestContextImpl::Create(sharedContext);
167}
168} // namespace sk_gpu_test
169
170#endif