-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdawn.patch
707 lines (685 loc) Β· 26.5 KB
/
dawn.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
diff --git a/DEPS b/DEPS
index 018c84ffc..dea1ab010 100644
--- a/DEPS
+++ b/DEPS
@@ -50,10 +50,10 @@ vars = {
# RBE project to download rewrapper config files for. Only needed if
# different from the project used in 'rbe_instance'
'rewrapper_cfg_project': '',
- # reclient CIPD package
- 'reclient_package': 'infra/rbe/client/',
- # reclient CIPD package version
- 'reclient_version': 're_client_version:0.143.0.518e369-gomaip',
+ # # reclient CIPD package
+ # 'reclient_package': 'infra/rbe/client/',
+ # # reclient CIPD package version
+ # 'reclient_version': 're_client_version:0.143.0.518e369-gomaip',
# siso CIPD package version.
'siso_version': 'git_revision:0b6159c39573013a79a47f8544200df07a6a74fc',
@@ -419,17 +419,17 @@ deps = {
'dep_type': 'cipd',
},
- # RBE dependencies
- 'buildtools/reclient': {
- 'packages': [
- {
- 'package': Var('reclient_package') + '${{platform}}',
- 'version': Var('reclient_version'),
- }
- ],
- 'dep_type': 'cipd',
- 'condition': 'dawn_standalone',
- },
+ # # RBE dependencies
+ # 'buildtools/reclient': {
+ # 'packages': [
+ # {
+ # 'package': Var('reclient_package') + '${{platform}}',
+ # 'version': Var('reclient_version'),
+ # }
+ # ],
+ # 'dep_type': 'cipd',
+ # 'condition': 'dawn_standalone',
+ # },
# Misc dependencies inherited from Tint
'third_party/protobuf': {
@@ -676,22 +676,22 @@ hooks = [
],
},
- # Download remote exec cfg files
- {
- 'name': 'fetch_reclient_cfgs',
- 'pattern': '.',
- 'condition': 'download_remoteexec_cfg and dawn_standalone',
- 'action': ['python3',
- 'buildtools/reclient_cfgs/fetch_reclient_cfgs.py',
- '--rbe_instance',
- Var('rbe_instance'),
- '--reproxy_cfg_template',
- 'reproxy.cfg.template',
- '--rewrapper_cfg_project',
- Var('rewrapper_cfg_project'),
- '--quiet',
- ],
- },
+ # # Download remote exec cfg files
+ # {
+ # 'name': 'fetch_reclient_cfgs',
+ # 'pattern': '.',
+ # 'condition': 'download_remoteexec_cfg and dawn_standalone',
+ # 'action': ['python3',
+ # 'buildtools/reclient_cfgs/fetch_reclient_cfgs.py',
+ # '--rbe_instance',
+ # Var('rbe_instance'),
+ # '--reproxy_cfg_template',
+ # 'reproxy.cfg.template',
+ # '--rewrapper_cfg_project',
+ # Var('rewrapper_cfg_project'),
+ # '--quiet',
+ # ],
+ # },
# Configure Siso for developer builds.
{
'name': 'configure_siso',
diff --git a/include/webgpu/webgpu_sdl.h b/include/webgpu/webgpu_sdl.h
new file mode 100644
index 000000000..98ae22ee3
--- /dev/null
+++ b/include/webgpu/webgpu_sdl.h
@@ -0,0 +1,60 @@
+// Copyright 2022 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef INCLUDE_WEBGPU_WEBGPU_SDL_H_
+#define INCLUDE_WEBGPU_WEBGPU_SDL_H_
+
+#include <memory>
+
+#include "dawn/dawn_proc.h"
+#include "webgpu/webgpu_cpp.h"
+
+#if defined(WGPU_SDL_SHARED_LIBRARY)
+#if defined(_WIN32)
+#if defined(WGPU_SDL_IMPLEMENTATION)
+#define WGPU_SDL_EXPORT __declspec(dllexport)
+#else
+#define WGPU_SDL_EXPORT __declspec(dllimport)
+#endif
+#else // defined(_WIN32)
+#if defined(WGPU_SDL_IMPLEMENTATION)
+#define WGPU_SDL_EXPORT __attribute__((visibility("default")))
+#else
+#define WGPU_SDL_EXPORT
+#endif
+#endif // defined(_WIN32)
+#else // defined(WGPU_SDL_SHARED_LIBRARY)
+#define WGPU_SDL_EXPORT
+#endif // defined(WGPU_SDL_SHARED_LIBRARY)
+
+namespace wgpu::sdl {
+
+WGPU_SDL_EXPORT WGPUSurface CreateSurfaceForWindow(const DawnProcTable *procs, const WGPUInstance& instance, void* nativeData);
+
+} // namespace wgpu::sdl
+
+#endif // INCLUDE_WEBGPU_WEBGPU_SDL_H_
diff --git a/src/dawn/CMakeLists.txt b/src/dawn/CMakeLists.txt
index 38ec75e00..1513430f3 100644
--- a/src/dawn/CMakeLists.txt
+++ b/src/dawn/CMakeLists.txt
@@ -107,6 +107,8 @@ if (DAWN_BUILD_NODE_BINDINGS)
add_subdirectory(node)
endif()
+add_subdirectory(sdl)
+
###############################################################################
# libdawn_proc
# Only built when not building for Emscripten
diff --git a/src/dawn/node/CMakeLists.txt b/src/dawn/node/CMakeLists.txt
index d8860f618..cd4716fda 100644
--- a/src/dawn/node/CMakeLists.txt
+++ b/src/dawn/node/CMakeLists.txt
@@ -103,7 +103,7 @@ set_target_properties(dawn_node PROPERTIES
CXX_STANDARD 17
)
target_link_libraries(dawn_node dawn_node_binding dawn_node_interop dawn_native dawncpp dawn_proc
- libtint)
+ libtint dawn_sdl)
target_include_directories(dawn_node PRIVATE
"${PROJECT_SOURCE_DIR}"
"${NODE_ADDON_API_DIR}"
diff --git a/src/dawn/node/Module.cpp b/src/dawn/node/Module.cpp
index 0ac36d0e3..220e420eb 100644
--- a/src/dawn/node/Module.cpp
+++ b/src/dawn/node/Module.cpp
@@ -30,11 +30,17 @@
#include <tuple>
#include <utility>
#include <vector>
+#include <map>
#include "dawn/dawn_proc.h"
#include "src/dawn/node/binding/Flags.h"
#include "src/dawn/node/binding/GPU.h"
-#include "tint/tint.h"
+#include "src/dawn/node/binding/GPUDevice.h"
+#include "src/dawn/node/binding/GPUTexture.h"
+#include "src/dawn/node/binding/GPUTextureView.h"
+#include "src/dawn/node/binding/Converter.h"
+
+#include "webgpu/webgpu_sdl.h"
#ifdef DAWN_EMIT_COVERAGE
extern "C" {
@@ -44,10 +50,21 @@ int __llvm_profile_write_file(void);
}
#endif // DAWN_EMIT_COVERAGE
+const DawnProcTable *procs;
+
+Napi::FunctionReference constructor;
+
+std::map<std::string, WGPUPresentMode> wgpuPresentModes = {
+ { "fifo", WGPUPresentMode_Fifo },
+ { "fifoRelaxed", WGPUPresentMode_FifoRelaxed },
+ { "immediate", WGPUPresentMode_Immediate },
+ { "mailbox", WGPUPresentMode_Mailbox }
+};
+
namespace {
Napi::Value CreateGPU(const Napi::CallbackInfo& info) {
- const auto& env = info.Env();
+ const Napi::Env& env = info.Env();
std::tuple<std::vector<std::string>> args;
if (auto res = wgpu::interop::FromJS(info, args); !res) {
@@ -69,7 +86,174 @@ Napi::Value CreateGPU(const Napi::CallbackInfo& info) {
}
// Construct a wgpu::interop::GPU interface, implemented by wgpu::bindings::GPU.
- return wgpu::interop::GPU::Create<wgpu::binding::GPU>(env, std::move(flags));
+ auto gpu = wgpu::interop::GPU::Create<wgpu::binding::GPU>(env, std::move(flags));
+ Napi::Object jsGpu = gpu;
+
+ return jsGpu;
+}
+
+class Renderer : public Napi::ObjectWrap<Renderer> {
+
+public:
+
+ static void Init(const Napi::Env &env) {
+ Napi::Function func = DefineClass(env, "Renderer", {
+ InstanceMethod("getPreferredFormat", &Renderer::getPreferredFormat),
+ InstanceMethod("getCurrentTexture", &Renderer::getCurrentTexture),
+ InstanceMethod("getCurrentTextureView", &Renderer::getCurrentTextureView),
+ InstanceMethod("swap", &Renderer::swap),
+ InstanceMethod("resize", &Renderer::resize),
+ });
+
+ constructor = Napi::Persistent(func);
+ constructor.SuppressDestruct();
+ }
+
+ Renderer(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Renderer>(info) {
+ const Napi::Env& env = info.Env();
+
+ Napi::Object jsDevice = info[0].As<Napi::Object>();
+ _device = Napi::Persistent(jsDevice);
+
+ Napi::Object jsWindow = info[1].As<Napi::Object>();
+ _window = Napi::Persistent(jsWindow);
+
+ Napi::String jsPresentMode = info[2].As<Napi::String>();
+ _wgpuPresentMode = wgpuPresentModes[jsPresentMode.Utf8Value()];
+
+ _wgpuDevice = reinterpret_cast<wgpu::binding::GPUDevice*>(wgpu::interop::GPUDevice::Unwrap(_device.Value()))->device_.Get();
+ _wgpuAdapter = procs->deviceGetAdapter(_wgpuDevice);
+ _wgpuInstance = procs->adapterGetInstance(_wgpuAdapter);
+
+ _createSurface(env);
+ };
+
+ static Napi::Object NewInstance(Napi::Env env, Napi::Value device, Napi::Value window, Napi::Value presentMode) {
+ return constructor.New({ device, window, presentMode });
+ }
+
+ Napi::Value getPreferredFormat(const Napi::CallbackInfo& info) {
+ const Napi::Env& env = info.Env();
+
+ wgpu::interop::GPUTextureFormat format;
+ wgpu::binding::Converter convert(env);
+ bool success = convert(format, static_cast<wgpu::TextureFormat>(_wgpuPreferredFormat));
+ return success ? wgpu::interop::ToJS(env, format) : env.Null();
+ }
+
+ Napi::Value getCurrentTexture(const Napi::CallbackInfo& info) {
+ const Napi::Env& env = info.Env();
+
+ WGPUSurfaceTexture wgpuSurfaceTexture = {};
+ procs->surfaceGetCurrentTexture(_wgpuSurface, &wgpuSurfaceTexture);
+
+ return wgpu::interop::GPUTexture::Create<wgpu::binding::GPUTexture>(env,
+ wgpu::Device::Acquire(_wgpuDevice),
+ wgpu::TextureDescriptor(),
+ wgpu::Texture::Acquire(wgpuSurfaceTexture.texture)
+ );
+ }
+
+ Napi::Value getCurrentTextureView(const Napi::CallbackInfo& info) {
+ const Napi::Env& env = info.Env();
+
+ WGPUSurfaceTexture wgpuSurfaceTexture = {};
+ procs->surfaceGetCurrentTexture(_wgpuSurface, &wgpuSurfaceTexture);
+
+ WGPUTextureView wgpuTextureView = procs->textureCreateView(wgpuSurfaceTexture.texture, nullptr);
+
+ return wgpu::interop::GPUTextureView::Create<wgpu::binding::GPUTextureView>(env,
+ wgpu::TextureViewDescriptor(),
+ wgpu::TextureView::Acquire(wgpuTextureView)
+ );
+ }
+
+ Napi::Value swap(const Napi::CallbackInfo& info) {
+ const Napi::Env& env = info.Env();
+ procs->surfacePresent(_wgpuSurface);
+ return env.Undefined();
+ }
+
+ Napi::Value resize(const Napi::CallbackInfo& info) {
+ const Napi::Env& env = info.Env();
+ _createSurface(env);
+ return env.Undefined();
+ }
+
+private:
+
+ Napi::ObjectReference _device;
+ Napi::ObjectReference _window;
+ WGPUPresentMode _wgpuPresentMode;
+ WGPUDevice _wgpuDevice;
+ WGPUAdapter _wgpuAdapter;
+ WGPUInstance _wgpuInstance;
+ WGPUSurface _wgpuSurface;
+ WGPUTextureFormat _wgpuPreferredFormat;
+
+ void _createSurface(const Napi::Env &env) {
+ Napi::Buffer<char> buffer = { env, _window.Get("_native").As<Napi::Object>().Get("gpu") };
+ _wgpuSurface = wgpu::sdl::CreateSurfaceForWindow(procs, _wgpuInstance, buffer.Data());
+
+ WGPUSurfaceCapabilities wgpuCapabilities = {};
+ procs->surfaceGetCapabilities(_wgpuSurface, _wgpuAdapter, &wgpuCapabilities);
+
+ _wgpuPreferredFormat = wgpuCapabilities.formats[0];
+
+ WGPUSurfaceConfiguration wgpuConfig = {};
+ wgpuConfig.device = _wgpuDevice;
+ wgpuConfig.format = _wgpuPreferredFormat;
+ wgpuConfig.usage = WGPUTextureUsage_RenderAttachment;
+ wgpuConfig.alphaMode = WGPUCompositeAlphaMode_Opaque;
+ wgpuConfig.width = _window.Get("_pixelWidth").ToNumber().Uint32Value();
+ wgpuConfig.height = _window.Get("_pixelHeight").ToNumber().Uint32Value();
+ wgpuConfig.presentMode = _wgpuPresentMode;
+ procs->surfaceConfigure(_wgpuSurface, &wgpuConfig);
+ }
+};
+
+Napi::Value RenderGPUDeviceToWindow(const Napi::CallbackInfo& info) {
+ const Napi::Env& env = info.Env();
+
+ if (info.Length() != 1) {
+ Napi::Error::New(env, "renderGPUDeviceToWindow expects exactly 1 argument").ThrowAsJavaScriptException();
+ return env.Undefined();
+ }
+ Napi::Value jsArg = info[0];
+
+ if (!jsArg.IsObject()) {
+ Napi::Error::New(env, "argument to renderGPUDeviceToWindow must be an object").ThrowAsJavaScriptException();
+ return env.Undefined();
+ }
+ Napi::Object jsObj = jsArg.ToObject();
+
+ Napi::Value jsDevice = jsObj.Get("device");
+ if (!jsDevice.IsObject()) {
+ Napi::Error::New(env, "device argument to renderGPUDeviceToWindow must be an object").ThrowAsJavaScriptException();
+ return env.Undefined();
+ }
+
+ Napi::Value jsWindow = jsObj.Get("window");
+ if (!jsWindow.IsObject()) {
+ Napi::Error::New(env, "window argument to renderGPUDeviceToWindow must be an object").ThrowAsJavaScriptException();
+ return env.Undefined();
+ }
+
+ Napi::Value jsPresentMode = jsObj.Get("presentMode");
+ if (jsPresentMode.IsUndefined()) {
+ jsPresentMode = Napi::String::New(env, "fifo");
+ } else if (!jsPresentMode.IsString()) {
+ Napi::Error::New(env, "presentMode argument to renderGPUDeviceToWindow must be a string").ThrowAsJavaScriptException();
+ return env.Undefined();
+ } else {
+ std::string presentModeStr = jsPresentMode.As<Napi::String>().Utf8Value();
+ if (wgpuPresentModes.find(presentModeStr) == wgpuPresentModes.end()) {
+ Napi::Error::New(env, "presentMode argument to renderGPUDeviceToWindow has invalid value").ThrowAsJavaScriptException();
+ return env.Undefined();
+ }
+ }
+
+ return Renderer::NewInstance(env, jsDevice, jsWindow, jsPresentMode);
}
#ifdef DAWN_EMIT_COVERAGE
@@ -105,13 +289,18 @@ struct Coverage {
// object.
NAPI_MODULE_EXPORT Napi::Object Initialize(Napi::Env env, Napi::Object exports) {
// Set all the Dawn procedure function pointers.
- dawnProcSetProcs(&dawn::native::GetProcs());
+ procs = &dawn::native::GetProcs();
+ dawnProcSetProcs(procs);
// Register all the interop types
exports.Set(Napi::String::New(env, "globals"), wgpu::interop::Initialize(env));
// Export function that creates and returns the wgpu::interop::GPU interface
- exports.Set(Napi::String::New(env, "create"), Napi::Function::New<CreateGPU>(env));
+ exports.Set(Napi::String::New(env, "_create"), Napi::Function::New<CreateGPU>(env));
+
+ Renderer::Init(env);
+
+ exports.Set(Napi::String::New(env, "renderGPUDeviceToWindow"), Napi::Function::New<RenderGPUDeviceToWindow>(env));
#ifdef DAWN_EMIT_COVERAGE
Coverage* coverage = new Coverage();
diff --git a/src/dawn/node/binding/AsyncRunner.cpp b/src/dawn/node/binding/AsyncRunner.cpp
index 403883397..4e878803e 100644
--- a/src/dawn/node/binding/AsyncRunner.cpp
+++ b/src/dawn/node/binding/AsyncRunner.cpp
@@ -64,7 +64,7 @@ void AsyncRunner::ScheduleProcessEvents(Napi::Env env) {
auto weak_self = weak_this_;
env.Global()
- .Get("setImmediate")
+ .Get("setTimeout")
.As<Napi::Function>()
.Call({
// TODO(crbug.com/dawn/1127): Create once, reuse.
@@ -76,9 +76,15 @@ void AsyncRunner::ScheduleProcessEvents(Napi::Env env) {
}
self->process_events_queued_ = false;
+
+ if (self->tasks_waiting_ == 0) {
+ return ;
+ }
+
wgpuInstanceProcessEvents(self->instance_->Get());
self->ScheduleProcessEvents(env);
}),
+ Napi::Number::New(env, 100)
});
}
diff --git a/src/dawn/node/binding/GPU.h b/src/dawn/node/binding/GPU.h
index 64c2a35ff..2631e7492 100644
--- a/src/dawn/node/binding/GPU.h
+++ b/src/dawn/node/binding/GPU.h
@@ -51,9 +51,10 @@ class GPU final : public interop::GPU {
interop::GPUTextureFormat getPreferredCanvasFormat(Napi::Env) override;
interop::Interface<interop::WGSLLanguageFeatures> getWgslLanguageFeatures(Napi::Env) override;
+ std::unique_ptr<dawn::native::Instance> instance_;
+
private:
const Flags flags_;
- std::unique_ptr<dawn::native::Instance> instance_;
std::shared_ptr<AsyncRunner> async_;
};
diff --git a/src/dawn/node/binding/GPUDevice.h b/src/dawn/node/binding/GPUDevice.h
index a3a2531e8..596820f53 100644
--- a/src/dawn/node/binding/GPUDevice.h
+++ b/src/dawn/node/binding/GPUDevice.h
@@ -135,9 +135,10 @@ class GPUDevice final : public interop::GPUDevice {
std::optional<std::variant<interop::EventListenerOptions, bool>> options) override;
bool dispatchEvent(Napi::Env, interop::Interface<interop::Event> event) override;
+ wgpu::Device device_;
+
private:
Napi::Env env_;
- wgpu::Device device_;
std::shared_ptr<AsyncRunner> async_;
// This promise's JS object lives as long as the device because it is stored in .lost
diff --git a/src/dawn/sdl/BUILD.gn b/src/dawn/sdl/BUILD.gn
new file mode 100644
index 000000000..e21b8881b
--- /dev/null
+++ b/src/dawn/sdl/BUILD.gn
@@ -0,0 +1,50 @@
+# Copyright 2022 The Dawn & Tint Authors
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# 3. Neither the name of the copyright holder nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import("../../../scripts/dawn_overrides_with_defaults.gni")
+
+import("${dawn_root}/scripts/dawn_component.gni")
+import("${dawn_root}/scripts/dawn_features.gni")
+
+###############################################################################
+# SDL utils
+###############################################################################
+
+static_library("sdl") {
+ defines = [ "WGPU_SDL_IMPLEMENTATION" ]
+
+ sources = [ "utils.cpp" ]
+ deps = [
+ "${dawn_root}/src/dawn:cpp",
+ "${dawn_root}/src/dawn:proc",
+ "${dawn_root}/src/dawn/common",
+ ]
+
+ public_deps = [
+ "${dawn_root}/include/dawn:cpp_headers",
+ ]
+}
diff --git a/src/dawn/sdl/CMakeLists.txt b/src/dawn/sdl/CMakeLists.txt
new file mode 100644
index 000000000..f8f8a79cf
--- /dev/null
+++ b/src/dawn/sdl/CMakeLists.txt
@@ -0,0 +1,49 @@
+# Copyright 2022 The Dawn & Tint Authors
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# 3. Neither the name of the copyright holder nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+add_library(dawn_sdl STATIC ${DAWN_PLACEHOLDER_FILE})
+common_compile_options(dawn_sdl)
+target_sources(dawn_sdl
+ INTERFACE
+ "${DAWN_INCLUDE_DIR}/webgpu/webgpu_sdl.h"
+ PRIVATE
+ "utils.cpp"
+)
+target_link_libraries(dawn_sdl
+ PUBLIC
+ dawncpp_headers
+ dawn_common
+ PRIVATE
+ dawn_internal_config
+)
+
+target_compile_definitions(dawn_sdl PRIVATE "WGPU_IMPLEMENTATION")
+if(BUILD_SHARED_LIBS)
+ target_compile_definitions(dawn_sdl PRIVATE "WGPU_SHARED_LIBRARY")
+endif()
+
+add_library(webgpu_sdl ALIAS dawn_sdl)
diff --git a/src/dawn/sdl/utils.cpp b/src/dawn/sdl/utils.cpp
new file mode 100644
index 000000000..a774d8cae
--- /dev/null
+++ b/src/dawn/sdl/utils.cpp
@@ -0,0 +1,107 @@
+// Copyright 2020 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include <cstdlib>
+#include <memory>
+#include <utility>
+
+#include "dawn/common/Platform.h"
+#include "webgpu/webgpu_sdl.h"
+
+namespace wgpu::sdl {
+
+#if DAWN_PLATFORM_IS(WINDOWS)
+ #include <windows.h>
+ struct NativeData {
+ HWND hwnd;
+ HINSTANCE hinstance;
+ };
+#elif defined(DAWN_ENABLE_BACKEND_METAL)
+ class CALayer;
+ struct NativeData {
+ CALayer *layer;
+ };
+#elif defined(DAWN_USE_X11)
+ #include <X11/Xlib.h>
+ struct NativeData {
+ Display* display;
+ Window window;
+ };
+#endif
+
+std::unique_ptr<WGPUChainedStruct, void (*)(WGPUChainedStruct*)> GetSurfaceDescriptor(NativeData* native) {
+#if DAWN_PLATFORM_IS(WINDOWS)
+ WGPUSurfaceSourceWindowsHWND* desc = new WGPUSurfaceSourceWindowsHWND();
+ desc->chain.next = nullptr;
+ desc->chain.sType = WGPUSType_SurfaceSourceWindowsHWND;
+ desc->hwnd = native->hwnd;
+ desc->hinstance = native->hinstance;
+ return {
+ reinterpret_cast<WGPUChainedStruct*>(desc),
+ [](WGPUChainedStruct* desc) {
+ delete reinterpret_cast<WGPUSurfaceSourceWindowsHWND*>(desc);
+ }
+ };
+#elif defined(DAWN_ENABLE_BACKEND_METAL)
+ WGPUSurfaceSourceMetalLayer* desc = new WGPUSurfaceSourceMetalLayer();
+ desc->chain.next = nullptr;
+ desc->chain.sType = WGPUSType_SurfaceSourceMetalLayer;
+ desc->layer = native->layer;
+ return {
+ reinterpret_cast<WGPUChainedStruct*>(desc),
+ [](WGPUChainedStruct* desc) {
+ delete reinterpret_cast<WGPUSurfaceSourceMetalLayer*>(desc);
+ }
+ };
+#elif defined(DAWN_USE_X11)
+ WGPUSurfaceSourceXlibWindow* desc = new WGPUSurfaceSourceXlibWindow();
+ desc->chain.next = nullptr;
+ desc->chain.sType = WGPUSType_SurfaceSourceXlibWindow;
+ desc->display = native->display;
+ desc->window = native->window;
+ return {
+ reinterpret_cast<WGPUChainedStruct*>(desc),
+ [](WGPUChainedStruct* desc) {
+ delete reinterpret_cast<WGPUSurfaceSourceXlibWindow*>(desc);
+ }
+ };
+#else
+ return { nullptr, [](WGPUChainedStruct*) {} };
+#endif
+}
+
+WGPUSurface CreateSurfaceForWindow(const DawnProcTable *procs, const WGPUInstance& instance, void* window) {
+ std::unique_ptr<WGPUChainedStruct, void (*)(WGPUChainedStruct*)> chainedDescriptor = GetSurfaceDescriptor((NativeData*) window);
+
+ WGPUSurfaceDescriptor surfaceDesc = {};
+ surfaceDesc.nextInChain = chainedDescriptor.get();
+ WGPUSurface surface = procs->instanceCreateSurface(instance, &surfaceDesc);
+
+ return surface;
+}
+
+} // namespace wgpu::sdl