-
Notifications
You must be signed in to change notification settings - Fork 26
/
lib_webgpu_dawn.cpp
2689 lines (2302 loc) · 112 KB
/
lib_webgpu_dawn.cpp
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __EMSCRIPTEN__
#include "lib_webgpu.h"
#include "dawn/webgpu.h"
#include "dawn/dawn_proc.h"
#include "dawn/native/DawnNative.h"
#include <map>
#include <vector>
#include <assert.h>
#ifdef _WIN32
#include <Windows.h>
#endif
#define DAWN_UNSAFE_API 1
enum _WgpuObjectType {
kWebGPUInvalidObject = 0,
kWebGPUAdapter,
kWebGPUDevice,
kWebGPUBindGroupLayout,
kWebGPUBuffer,
kWebGPUTexture,
kWebGPUTextureView,
kWebGPUExternalTexture,
kWebGPUSampler,
kWebGPUBindGroup,
kWebGPUPipelineLayout,
kWebGPUShaderModule,
kWebGPUComputePipeline,
kWebGPURenderPipeline,
kWebGPUCommandBuffer,
kWebGPUCommandEncoder,
kWebGPUComputePassEncoder,
kWebGPURenderPassEncoder,
kWebGPURenderBundle,
kWebGPURenderBundleEncoder,
kWebGPUQueue,
kWebGPUQuerySet,
kWebGPUCanvasContext
};
enum _WGpuBufferMapState {
kWebGPUBufferMapStateUnmapped,
kWebGPUBufferMapStatePending,
kWebGPUBufferMapStateMappedForWriting,
kWebGPUBufferMapStateMappedForReading,
};
// lib_webgpu.js tracks derived objects and deletes them when the parent object is deleted.
// This may potentially cause bad pointers because WGpuObjectBase is a pointer to a _WGpuObject,
// and if the app still has a pointer to a derived object after it deletes the parent object,
// it would be a bad pointer. The app should make sure to delete derived objects before a parent
// object, such as deleting all texture_view's before deleting the texture.
struct _WGpuObject {
_WgpuObjectType type; // C/Dawn doesn't have the RTTI that JS has.
void* dawnObject;
};
struct _WGpuObjectBuffer : _WGpuObject {
_WGpuBufferMapState state;
};
namespace {
const WGPU_BUFFER_MAP_STATE Dawn_to_WGPU_BUFFER_MAP_STATE[] = {
WGPU_BUFFER_MAP_STATE_UNMAPPED,
WGPU_BUFFER_MAP_STATE_PENDING,
WGPU_BUFFER_MAP_STATE_MAPPED,
WGPU_BUFFER_MAP_STATE_MAPPED
};
template<typename T>
class RuntimeStatic {
public:
RuntimeStatic()
: _self(nullptr)
{}
~RuntimeStatic() {
delete _self;
}
T* operator->() {
if (!_self)
_self = new T;
return _self;
}
T& operator*() {
if (!_self)
_self = new T;
return *_self;
}
private:
T* _self;
};
RuntimeStatic<std::map<void*, WGpuObjectBase>> _dawn_to_webgpu;
RuntimeStatic<std::map<void*, void*>> _webgpu_to_dawn;
// Translate lib_webgpu enums to Dawn enums
const WGPUFeatureName WGPU_FEATURES_BITFIELD_to_Dawn[] = {
WGPUFeatureName_DepthClipControl,
WGPUFeatureName_Depth32FloatStencil8,
WGPUFeatureName_TextureCompressionBC,
WGPUFeatureName_Force32, // WGPU_FEATURE_TEXTURE_COMPRESSION_BC_SLICED_3D, no Dawn equivalent
WGPUFeatureName_TextureCompressionETC2,
WGPUFeatureName_TextureCompressionASTC,
WGPUFeatureName_TimestampQuery,
WGPUFeatureName_IndirectFirstInstance,
WGPUFeatureName_ShaderF16,
WGPUFeatureName_RG11B10UfloatRenderable,
WGPUFeatureName_BGRA8UnormStorage,
WGPUFeatureName_Float32Filterable,
WGPUFeatureName_ClipDistances,
WGPUFeatureName_DualSourceBlending,
};
const int _wgpu_num_features = 13;
const WGPUPowerPreference WGPU_POWER_PREFERENCE_to_Dawn[] = {
WGPUPowerPreference_Undefined,
WGPUPowerPreference_LowPower,
WGPUPowerPreference_HighPerformance,
};
#define wgpu_power_preference_to_dawn(mode) WGPU_POWER_PREFERENCE_to_Dawn[mode]
#define wgpu_buffer_usage_to_dawn(usage) (WGPUBufferUsage)usage
#define wgpu_map_mode_to_dawn(mode) (WGPUMapMode)mode
const WGPUTextureDimension WGPU_TEXTURE_DIMENSION_to_Dawn[] = {
WGPUTextureDimension_Force32,
WGPUTextureDimension_1D,
WGPUTextureDimension_2D,
WGPUTextureDimension_3D,
};
#define wgpu_texture_dimension_to_dawn(dim) WGPU_TEXTURE_DIMENSION_to_Dawn[dim]
const WGPU_TEXTURE_DIMENSION Dawn_to_WGPU_TEXTURE_DIMENSION[] = {
WGPU_TEXTURE_DIMENSION_1D,
WGPU_TEXTURE_DIMENSION_2D,
WGPU_TEXTURE_DIMENSION_3D,
};
#define dawn_to_wgpu_texture_dimension(dim) Dawn_to_WGPU_TEXTURE_DIMENSION[dim]
#define wgpu_texture_usage_flags_to_dawn(flags) (WGPUTextureUsage)flags
const WGPUTextureViewDimension WGPU_TEXTURE_VIEW_DIMENSION_to_Dawn[] = {
WGPUTextureViewDimension_Undefined,
WGPUTextureViewDimension_1D,
WGPUTextureViewDimension_2D,
WGPUTextureViewDimension_2DArray,
WGPUTextureViewDimension_Cube,
WGPUTextureViewDimension_CubeArray,
WGPUTextureViewDimension_3D,
};
#define wgpu_texture_view_dimension_to_dawn(dim) WGPU_TEXTURE_VIEW_DIMENSION_to_Dawn[dim]
WGPUTextureAspect WGPU_TEXTURE_ASPECT_to_Dawn[] = {
WGPUTextureAspect_Force32,
WGPUTextureAspect_All,
WGPUTextureAspect_StencilOnly,
WGPUTextureAspect_DepthOnly
};
#define wgpu_texture_aspect_to_dawn(aspect) WGPU_TEXTURE_ASPECT_to_Dawn[aspect]
const WGPUTextureFormat WGPU_TEXTURE_FORMAT_to_Dawn[] = {
WGPUTextureFormat_Undefined, // WGPU_TEXTURE_FORMAT_INVALID 0
// 8-bit formats
WGPUTextureFormat_R8Unorm, // WGPU_TEXTURE_FORMAT_R8UNORM 1
WGPUTextureFormat_R8Snorm, // WGPU_TEXTURE_FORMAT_R8SNORM 2
WGPUTextureFormat_R8Uint, // WGPU_TEXTURE_FORMAT_R8UINT 3
WGPUTextureFormat_R8Sint, // WGPU_TEXTURE_FORMAT_R8SINT
// 16-bit formats
WGPUTextureFormat_R16Uint, // WGPU_TEXTURE_FORMAT_R16UINT 5
WGPUTextureFormat_R16Sint, // WGPU_TEXTURE_FORMAT_R16SINT 6
WGPUTextureFormat_R16Float, // WGPU_TEXTURE_FORMAT_R16FLOAT 7
WGPUTextureFormat_RG8Unorm, // WGPU_TEXTURE_FORMAT_RG8UNORM 8
WGPUTextureFormat_RG8Snorm, // WGPU_TEXTURE_FORMAT_RG8SNORM 9
WGPUTextureFormat_RG8Uint, // WGPU_TEXTURE_FORMAT_RG8UINT 10
WGPUTextureFormat_RG8Sint, // WGPU_TEXTURE_FORMAT_RG8SINT 11
// 32-bit formats
WGPUTextureFormat_R32Uint, // WGPU_TEXTURE_FORMAT_R32UINT 12
WGPUTextureFormat_R32Sint, // WGPU_TEXTURE_FORMAT_R32SINT 13
WGPUTextureFormat_R32Float, // WGPU_TEXTURE_FORMAT_R32FLOAT 14
WGPUTextureFormat_RG16Uint, // WGPU_TEXTURE_FORMAT_RG16UINT 15
WGPUTextureFormat_RG16Sint, // WGPU_TEXTURE_FORMAT_RG16SINT 16
WGPUTextureFormat_RG16Float, // WGPU_TEXTURE_FORMAT_RG16FLOAT 17
WGPUTextureFormat_RGBA8Unorm, // WGPU_TEXTURE_FORMAT_RGBA8UNORM 18
WGPUTextureFormat_RGBA8UnormSrgb, // WGPU_TEXTURE_FORMAT_RGBA8UNORM_SRGB 19
WGPUTextureFormat_RGBA8Snorm, // WGPU_TEXTURE_FORMAT_RGBA8SNORM 20
WGPUTextureFormat_RGBA8Uint, // WGPU_TEXTURE_FORMAT_RGBA8UINT 21
WGPUTextureFormat_RGBA8Sint, // WGPU_TEXTURE_FORMAT_RGBA8SINT 22
WGPUTextureFormat_BGRA8Unorm, // WGPU_TEXTURE_FORMAT_BGRA8UNORM 23
WGPUTextureFormat_BGRA8UnormSrgb, // WGPU_TEXTURE_FORMAT_BGRA8UNORM_SRGB 24
// Packed 32-bit formats
WGPUTextureFormat_RGB9E5Ufloat, // WGPU_TEXTURE_FORMAT_RGB9E5UFLOAT 25
WGPUTextureFormat_Undefined, // WGPU_TEXTURE_FORMAT_RGB10A2UINT 26
WGPUTextureFormat_RGB10A2Unorm, // WGPU_TEXTURE_FORMAT_RGB10A2UNORM 27
WGPUTextureFormat_RG11B10Ufloat, // WGPU_TEXTURE_FORMAT_RG11B10UFLOAT 28
// 64-bit formats
WGPUTextureFormat_RG32Uint, // WGPU_TEXTURE_FORMAT_RG32UINT 29
WGPUTextureFormat_RG32Sint, // WGPU_TEXTURE_FORMAT_RG32SINT 30
WGPUTextureFormat_RG32Float, // WGPU_TEXTURE_FORMAT_RG32FLOAT 31
WGPUTextureFormat_RGBA16Uint, // WGPU_TEXTURE_FORMAT_RGBA16UINT 32
WGPUTextureFormat_RGBA16Sint, // WGPU_TEXTURE_FORMAT_RGBA16SINT 33
WGPUTextureFormat_RGBA16Float, // WGPU_TEXTURE_FORMAT_RGBA16FLOAT 34
// 128-bit formats
WGPUTextureFormat_RGBA32Uint, // WGPU_TEXTURE_FORMAT_RGBA32UINT 35
WGPUTextureFormat_RGBA32Sint, // WGPU_TEXTURE_FORMAT_RGBA32SINT 36
WGPUTextureFormat_RGBA32Float, // WGPU_TEXTURE_FORMAT_RGBA32FLOAT 37
// Depth/stencil formats
WGPUTextureFormat_Stencil8, // WGPU_TEXTURE_FORMAT_STENCIL8 38
WGPUTextureFormat_Depth16Unorm,
WGPUTextureFormat_Depth24Plus,
WGPUTextureFormat_Depth24PlusStencil8,
WGPUTextureFormat_Depth32Float,
WGPUTextureFormat_Depth32FloatStencil8,
// BC compressed formats usable if "texture-compression-bc" is both
// supported by the device/user agent and enabled in requestDevice.
WGPUTextureFormat_BC1RGBAUnorm,
WGPUTextureFormat_BC1RGBAUnormSrgb,
WGPUTextureFormat_BC2RGBAUnorm,
WGPUTextureFormat_BC2RGBAUnormSrgb,
WGPUTextureFormat_BC3RGBAUnorm,
WGPUTextureFormat_BC3RGBAUnormSrgb,
WGPUTextureFormat_BC4RUnorm,
WGPUTextureFormat_BC4RSnorm,
WGPUTextureFormat_BC5RGUnorm,
WGPUTextureFormat_BC5RGSnorm,
WGPUTextureFormat_BC6HRGBUfloat,
WGPUTextureFormat_BC6HRGBFloat,
WGPUTextureFormat_BC7RGBAUnorm,
WGPUTextureFormat_BC7RGBAUnormSrgb,
// ETC2 compressed formats usable if "texture-compression-etc2" is both
// supported by the device/user agent and enabled in requestDevice.
WGPUTextureFormat_ETC2RGB8Unorm,
WGPUTextureFormat_ETC2RGB8UnormSrgb,
WGPUTextureFormat_ETC2RGB8A1Unorm,
WGPUTextureFormat_ETC2RGB8A1UnormSrgb,
WGPUTextureFormat_ETC2RGBA8Unorm,
WGPUTextureFormat_ETC2RGBA8UnormSrgb,
WGPUTextureFormat_EACR11Unorm,
WGPUTextureFormat_EACR11Snorm,
WGPUTextureFormat_EACRG11Unorm,
WGPUTextureFormat_EACRG11Snorm,
// ASTC compressed formats usable if "texture-compression-astc" is both
// supported by the device/user agent and enabled in requestDevice.
WGPUTextureFormat_ASTC4x4Unorm,
WGPUTextureFormat_ASTC4x4UnormSrgb,
WGPUTextureFormat_ASTC5x4Unorm,
WGPUTextureFormat_ASTC5x4UnormSrgb,
WGPUTextureFormat_ASTC5x5Unorm,
WGPUTextureFormat_ASTC5x5UnormSrgb,
WGPUTextureFormat_ASTC6x5Unorm,
WGPUTextureFormat_ASTC6x5UnormSrgb,
WGPUTextureFormat_ASTC6x6Unorm,
WGPUTextureFormat_ASTC6x6UnormSrgb,
WGPUTextureFormat_ASTC8x5Unorm,
WGPUTextureFormat_ASTC8x5UnormSrgb,
WGPUTextureFormat_ASTC8x6Unorm,
WGPUTextureFormat_ASTC8x6UnormSrgb,
WGPUTextureFormat_ASTC8x8Unorm,
WGPUTextureFormat_ASTC8x8UnormSrgb,
WGPUTextureFormat_ASTC10x5Unorm,
WGPUTextureFormat_ASTC10x5UnormSrgb,
WGPUTextureFormat_ASTC10x6Unorm,
WGPUTextureFormat_ASTC10x6UnormSrgb,
WGPUTextureFormat_ASTC10x8Unorm,
WGPUTextureFormat_ASTC10x8UnormSrgb,
WGPUTextureFormat_ASTC10x10Unorm,
WGPUTextureFormat_ASTC10x10UnormSrgb,
WGPUTextureFormat_ASTC12x10Unorm,
WGPUTextureFormat_ASTC12x10UnormSrgb,
WGPUTextureFormat_ASTC12x12Unorm,
WGPUTextureFormat_ASTC12x12UnormSrgb,
// ?
WGPUTextureFormat_R8BG8Biplanar420Unorm,
};
#define wgpu_texture_format_to_dawn(format) WGPU_TEXTURE_FORMAT_to_Dawn[format]
const WGPU_TEXTURE_FORMAT Dawn_to_WGPU_TEXTURE_FORMAT[] = {
WGPU_TEXTURE_FORMAT_INVALID,
// 8-bit formats
WGPU_TEXTURE_FORMAT_R8UNORM,
WGPU_TEXTURE_FORMAT_R8SNORM,
WGPU_TEXTURE_FORMAT_R8UINT,
WGPU_TEXTURE_FORMAT_R8SINT,
// 16-bit formats
WGPU_TEXTURE_FORMAT_R16UINT,
WGPU_TEXTURE_FORMAT_R16SINT,
WGPU_TEXTURE_FORMAT_R16FLOAT,
WGPU_TEXTURE_FORMAT_RG8UNORM,
WGPU_TEXTURE_FORMAT_RG8SNORM,
WGPU_TEXTURE_FORMAT_RG8UINT,
WGPU_TEXTURE_FORMAT_RG8SINT,
// 32-bit formats
WGPU_TEXTURE_FORMAT_R32FLOAT,
WGPU_TEXTURE_FORMAT_R32UINT,
WGPU_TEXTURE_FORMAT_R32SINT,
WGPU_TEXTURE_FORMAT_RG16UINT,
WGPU_TEXTURE_FORMAT_RG16SINT,
WGPU_TEXTURE_FORMAT_RG16FLOAT,
WGPU_TEXTURE_FORMAT_RGBA8UNORM,
WGPU_TEXTURE_FORMAT_RGBA8UNORM_SRGB,
WGPU_TEXTURE_FORMAT_RGBA8SNORM,
WGPU_TEXTURE_FORMAT_RGBA8UINT,
WGPU_TEXTURE_FORMAT_RGBA8SINT,
WGPU_TEXTURE_FORMAT_BGRA8UNORM,
WGPU_TEXTURE_FORMAT_BGRA8UNORM_SRGB,
// Packed 32-bit formats
WGPU_TEXTURE_FORMAT_RGB10A2UINT,
WGPU_TEXTURE_FORMAT_RGB10A2UNORM,
WGPU_TEXTURE_FORMAT_RG11B10UFLOAT,
WGPU_TEXTURE_FORMAT_RGB9E5UFLOAT,
// 64-bit formats
WGPU_TEXTURE_FORMAT_RG32FLOAT,
WGPU_TEXTURE_FORMAT_RG32UINT,
WGPU_TEXTURE_FORMAT_RG32SINT,
WGPU_TEXTURE_FORMAT_RGBA16UINT,
WGPU_TEXTURE_FORMAT_RGBA16SINT,
WGPU_TEXTURE_FORMAT_RGBA16FLOAT,
// 128-bit formats
WGPU_TEXTURE_FORMAT_RGBA32FLOAT,
WGPU_TEXTURE_FORMAT_RGBA32UINT,
WGPU_TEXTURE_FORMAT_RGBA32SINT,
// Depth/stencil formats
WGPU_TEXTURE_FORMAT_STENCIL8,
WGPU_TEXTURE_FORMAT_DEPTH16UNORM,
WGPU_TEXTURE_FORMAT_DEPTH24PLUS,
WGPU_TEXTURE_FORMAT_DEPTH24PLUS_STENCIL8,
WGPU_TEXTURE_FORMAT_DEPTH32FLOAT,
WGPU_TEXTURE_FORMAT_DEPTH32FLOAT_STENCIL8,
// BC compressed formats usable if "texture-compression-bc" is both
// supported by the device/user agent and enabled in requestDevice.
WGPU_TEXTURE_FORMAT_BC1_RGBA_UNORM,
WGPU_TEXTURE_FORMAT_BC1_RGBA_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_BC2_RGBA_UNORM,
WGPU_TEXTURE_FORMAT_BC2_RGBA_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_BC3_RGBA_UNORM,
WGPU_TEXTURE_FORMAT_BC3_RGBA_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_BC4_R_UNORM,
WGPU_TEXTURE_FORMAT_BC4_R_SNORM,
WGPU_TEXTURE_FORMAT_BC5_RG_UNORM,
WGPU_TEXTURE_FORMAT_BC5_RG_SNORM,
WGPU_TEXTURE_FORMAT_BC6H_RGB_UFLOAT,
WGPU_TEXTURE_FORMAT_BC6H_RGB_FLOAT,
WGPU_TEXTURE_FORMAT_BC7_RGBA_UNORM,
WGPU_TEXTURE_FORMAT_BC7_RGBA_UNORM_SRGB,
// ETC2 compressed formats usable if "texture-compression-etc2" is both
// supported by the device/user agent and enabled in requestDevice.
WGPU_TEXTURE_FORMAT_ETC2_RGB8UNORM,
WGPU_TEXTURE_FORMAT_ETC2_RGB8UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ETC2_RGB8A1UNORM,
WGPU_TEXTURE_FORMAT_ETC2_RGB8A1UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ETC2_RGBA8UNORM,
WGPU_TEXTURE_FORMAT_ETC2_RGBA8UNORM_SRGB,
WGPU_TEXTURE_FORMAT_EAC_R11UNORM,
WGPU_TEXTURE_FORMAT_EAC_R11SNORM,
WGPU_TEXTURE_FORMAT_EAC_RG11UNORM,
WGPU_TEXTURE_FORMAT_EAC_RG11SNORM,
// ASTC compressed formats usable if "texture-compression-astc" is both
// supported by the device/user agent and enabled in requestDevice.
WGPU_TEXTURE_FORMAT_ASTC_4X4_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_4X4_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_5X4_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_5X4_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_5X5_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_5X5_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_6X5_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_6X5_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_6X6_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_6X6_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_8X5_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_8X5_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_8X6_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_8X6_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_8X8_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_8X8_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_10X5_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_10X5_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_10X6_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_10X6_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_10X8_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_10X8_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_10X10_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_10X10_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_12X10_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_12X10_UNORM_SRGB,
WGPU_TEXTURE_FORMAT_ASTC_12X12_UNORM,
WGPU_TEXTURE_FORMAT_ASTC_12X12_UNORM_SRGB,
};
#define dawn_to_wgpu_texture_format(format) Dawn_to_WGPU_TEXTURE_FORMAT[format]
const WGPUAddressMode WGPU_ADDRESS_MODE_to_Dawn[] = {
WGPUAddressMode_Force32,
WGPUAddressMode_ClampToEdge,
WGPUAddressMode_Repeat,
WGPUAddressMode_MirrorRepeat
};
#define wgpu_address_mode_to_dawn(mode) WGPU_ADDRESS_MODE_to_Dawn[mode]
const WGPUFilterMode WGPU_FILTER_MODE_to_Dawn[] = {
WGPUFilterMode_Force32,
WGPUFilterMode_Nearest,
WGPUFilterMode_Linear
};
#define wgpu_filter_mode_to_dawn(mode) WGPU_FILTER_MODE_to_Dawn[mode]
const WGPUMipmapFilterMode WGPU_MIPMAP_FILTER_MODE_to_Dawn[] = {
WGPUMipmapFilterMode_Force32,
WGPUMipmapFilterMode_Nearest,
WGPUMipmapFilterMode_Linear
};
#define wgpu_mipmap_filter_mode_to_dawn(mode) WGPU_MIPMAP_FILTER_MODE_to_Dawn[mode]
const WGPUCompareFunction WGPU_COMPARE_FUNCTION_to_Dawn[] = {
WGPUCompareFunction_Undefined,
WGPUCompareFunction_Never,
WGPUCompareFunction_Less,
WGPUCompareFunction_Equal,
WGPUCompareFunction_LessEqual,
WGPUCompareFunction_Greater,
WGPUCompareFunction_NotEqual,
WGPUCompareFunction_GreaterEqual,
WGPUCompareFunction_Always,
};
#define wgpu_compare_function_to_dawn(function) WGPU_COMPARE_FUNCTION_to_Dawn[function]
#define wgpu_shader_storage_flags_to_dawn(flags) (WGPUShaderStageFlags)flags
const WGPUBufferBindingType WGPU_BUFFER_BINDING_TYPE_to_Dawn[] = {
WGPUBufferBindingType_Undefined,
WGPUBufferBindingType_Uniform,
WGPUBufferBindingType_Storage,
WGPUBufferBindingType_ReadOnlyStorage,
};
#define wgpu_buffer_binding_type_to_dawn(type) WGPU_BUFFER_BINDING_TYPE_to_Dawn[type]
const WGPUSamplerBindingType WGPU_SAMPLER_BINDING_TYPE_to_Dawn[] = {
WGPUSamplerBindingType_Undefined,
WGPUSamplerBindingType_Filtering,
WGPUSamplerBindingType_NonFiltering,
WGPUSamplerBindingType_Comparison,
};
#define wgpu_sampler_binding_type_to_dawn(type) WGPU_SAMPLER_BINDING_TYPE_to_Dawn[type];
const WGPUTextureSampleType WGPU_TEXTURE_SAMPLE_TYPE_to_Dawn[] = {
WGPUTextureSampleType_Undefined,
WGPUTextureSampleType_Float,
WGPUTextureSampleType_UnfilterableFloat,
WGPUTextureSampleType_Depth,
WGPUTextureSampleType_Sint,
WGPUTextureSampleType_Uint
};
#define wgpu_texture_sample_type_to_dawn(type) WGPU_TEXTURE_SAMPLE_TYPE_to_Dawn[type]
const WGPUStorageTextureAccess WGPU_STORAGE_TEXTURE_ACCESS_to_Dawn[] = {
WGPUStorageTextureAccess_Undefined,
WGPUStorageTextureAccess_WriteOnly,
WGPUStorageTextureAccess_ReadOnly,
WGPUStorageTextureAccess_ReadWrite
};
#define wgpu_storage_texture_access_to_dawn(access) WGPU_STORAGE_TEXTURE_ACCESS_to_Dawn[access]
const WGPUPrimitiveTopology WGPU_PRIMITIVE_TOPOLOGY_to_Dawn[] = {
WGPUPrimitiveTopology_Force32,
WGPUPrimitiveTopology_PointList,
WGPUPrimitiveTopology_LineList,
WGPUPrimitiveTopology_LineStrip,
WGPUPrimitiveTopology_TriangleList,
WGPUPrimitiveTopology_TriangleStrip,
};
#define wgpu_primitive_topology_to_dawn(topology) WGPU_PRIMITIVE_TOPOLOGY_to_Dawn[topology]
const WGPUFrontFace WGPU_FRONT_FACE_to_Dawn[] = {
WGPUFrontFace_Force32,
WGPUFrontFace_CCW,
WGPUFrontFace_CW,
};
#define wgpu_front_face_to_dawn(type) WGPU_FRONT_FACE_to_Dawn[type]
const WGPUCullMode WGPU_CULL_MODE_to_Dawn[] = {
WGPUCullMode_Force32,
WGPUCullMode_None,
WGPUCullMode_Front,
WGPUCullMode_Back,
};
#define wgpu_cull_mode_to_dawn(mode) WGPU_CULL_MODE_to_Dawn[mode]
#define wgpu_color_write_flags_to_dawn(flags) (WGPUColorWriteMaskFlags)flags
const WGPUBlendFactor WGPU_BLEND_FACTOR_to_Dawn[] = {
WGPUBlendFactor_Force32, //WGPU_BLEND_FACTOR_INVALID
WGPUBlendFactor_Zero, //WGPU_BLEND_FACTOR_ZERO,
WGPUBlendFactor_One, //WGPU_BLEND_FACTOR_ONE,
WGPUBlendFactor_Src, //WGPU_BLEND_FACTOR_SRC,
WGPUBlendFactor_OneMinusSrc, //WGPU_BLEND_FACTOR_ONE_MINUS_SRC,
WGPUBlendFactor_SrcAlpha, //WGPU_BLEND_FACTOR_SRC_ALPHA,
WGPUBlendFactor_OneMinusSrcAlpha, //WGPU_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
WGPUBlendFactor_Dst, //WGPU_BLEND_FACTOR_DST,
WGPUBlendFactor_OneMinusDst, //WGPU_BLEND_FACTOR_ONE_MINUS_DST,
WGPUBlendFactor_DstAlpha, //WGPU_BLEND_FACTOR_DST_ALPHA,
WGPUBlendFactor_OneMinusDstAlpha, //WGPU_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
WGPUBlendFactor_SrcAlphaSaturated, //WGPU_BLEND_FACTOR_SRC_ALPHA_SATURATED,
WGPUBlendFactor_Constant, //WGPU_BLEND_FACTOR_CONSTANT,
WGPUBlendFactor_OneMinusConstant, //WGPU_BLEND_FACTOR_ONE_MINUS_CONSTANT
};
#define wgpu_blend_factor_to_dawn(factor) WGPU_BLEND_FACTOR_to_Dawn[factor]
const WGPUBlendOperation WGPU_BLEND_OPERATION_to_Dawn[] = {
WGPUBlendOperation_Force32,
WGPUBlendOperation_Add,
WGPUBlendOperation_Subtract,
WGPUBlendOperation_ReverseSubtract,
WGPUBlendOperation_Min,
WGPUBlendOperation_Max,
};
#define wgpu_blend_operation_to_dawn(op) WGPU_BLEND_OPERATION_to_Dawn[op]
const WGPUStencilOperation WGPU_STENCIL_OPERATION_to_Dawn[] = {
WGPUStencilOperation_Force32,
WGPUStencilOperation_Keep,
WGPUStencilOperation_Zero,
WGPUStencilOperation_Replace,
WGPUStencilOperation_Invert,
WGPUStencilOperation_IncrementClamp,
WGPUStencilOperation_DecrementClamp,
WGPUStencilOperation_IncrementWrap,
WGPUStencilOperation_DecrementWrap,
};
#define wgpu_stencil_operation_to_dawn(op) WGPU_STENCIL_OPERATION_to_Dawn[op]
const WGPUIndexFormat WGPU_INDEX_FORMAT_to_Dawn[] = {
WGPUIndexFormat_Undefined,
WGPUIndexFormat_Uint16,
WGPUIndexFormat_Uint32,
};
#define wgpu_index_format_to_dawn(format) WGPU_INDEX_FORMAT_to_Dawn[format]
const WGPUVertexFormat WGPU_VERTEX_FORMAT_to_Dawn[] = {
WGPUVertexFormat_Force32,
WGPUVertexFormat_Uint8x2,
WGPUVertexFormat_Uint8x4,
WGPUVertexFormat_Sint8x2,
WGPUVertexFormat_Sint8x4,
WGPUVertexFormat_Unorm8x2,
WGPUVertexFormat_Unorm8x4,
WGPUVertexFormat_Snorm8x2,
WGPUVertexFormat_Snorm8x4,
WGPUVertexFormat_Uint16x2,
WGPUVertexFormat_Uint16x4,
WGPUVertexFormat_Sint16x2,
WGPUVertexFormat_Sint16x4,
WGPUVertexFormat_Unorm16x2,
WGPUVertexFormat_Unorm16x4,
WGPUVertexFormat_Snorm16x2,
WGPUVertexFormat_Snorm16x4,
WGPUVertexFormat_Float16x2,
WGPUVertexFormat_Float16x4,
WGPUVertexFormat_Float32,
WGPUVertexFormat_Float32x2,
WGPUVertexFormat_Float32x3,
WGPUVertexFormat_Float32x4,
WGPUVertexFormat_Uint32,
WGPUVertexFormat_Uint32x2,
WGPUVertexFormat_Uint32x3,
WGPUVertexFormat_Uint32x4,
WGPUVertexFormat_Sint32,
WGPUVertexFormat_Sint32x2,
WGPUVertexFormat_Sint32x3,
WGPUVertexFormat_Sint32x4,
WGPUVertexFormat_Unorm10_10_10_2
};
#define wgpu_vertex_format_to_dawn(format) (format == 0 ? WGPUVertexFormat_Force32 : WGPU_VERTEX_FORMAT_to_Dawn[format - (WGPU_VERTEX_FORMAT_UINT8X2 - 1)])
const WGPUVertexStepMode WGPU_VERTEX_STEP_MODE_to_Dawn[] = {
WGPUVertexStepMode_VertexBufferNotUsed,
WGPUVertexStepMode_Vertex,
WGPUVertexStepMode_Instance
};
#define wgpu_vertex_step_mode_to_dawn(mode) WGPU_VERTEX_STEP_MODE_to_Dawn[mode]
const WGPULoadOp WGPU_LOAD_OP_to_Dawn[] = {
WGPULoadOp_Undefined,
WGPULoadOp_Load,
WGPULoadOp_Clear
};
#define wgpu_load_op_to_dawn(op) WGPU_LOAD_OP_to_Dawn[op]
const WGPUStoreOp WGPU_STORE_OP_to_Dawn[] = {
WGPUStoreOp_Undefined,
WGPUStoreOp_Store,
WGPUStoreOp_Discard
};
#define wgpu_store_op_to_dawn(op) WGPU_STORE_OP_to_Dawn[op]
const WGPUQueryType WGPU_QUERY_TYPE_to_Dawn[] = {
WGPUQueryType_Force32,
WGPUQueryType_Occlusion,
WGPUQueryType_Timestamp,
};
#define wgpu_query_type_to_dawn(type) WGPU_QUERY_TYPE_to_Dawn[type]
const WGPU_QUERY_TYPE Dawn_to_WGPU_QUERY_TYPE[] = {
WGPU_QUERY_TYPE_OCCLUSION,
WGPU_QUERY_TYPE_TIMESTAMP,
};
#define dawn_to_wgpu_query_type(type) Dawn_to_WGPU_QUERY_TYPE[type]
const WGPUErrorFilter WGPU_ERROR_FILTER_to_Dawn[] = {
WGPUErrorFilter_Force32,
WGPUErrorFilter_OutOfMemory,
WGPUErrorFilter_Validation,
WGPUErrorFilter_Internal
};
#define wgpu_error_filter_to_dawn(filter) WGPU_ERROR_FILTER_to_Dawn[filter]
WGPU_DEVICE_LOST_REASON Dawn_to_WGPU_DEVICE_LOST_REASON[] = {
WGPU_DEVICE_LOST_REASON_UNKNOWN,
WGPU_DEVICE_LOST_REASON_DESTROYED
};
#define dawn_to_wgpu_device_lost_reason(reason) Dawn_to_WGPU_DEVICE_LOST_REASON[reason]
const WGPUCompositeAlphaMode WGPU_CANVAS_ALPHA_MODE_to_Dawn[] = {
WGPUCompositeAlphaMode_Force32,
WGPUCompositeAlphaMode_Opaque,
WGPUCompositeAlphaMode_Premultiplied
};
#define wgpu_canvas_alpha_mode_to_dawn(alphaMode) WGPU_CANVAS_ALPHA_MODE_to_Dawn[alphaMode]
//////////////////////////////////////////////////////////////////////
#ifdef DAWN_UNSAFE_API
static const char* enabledToggles = "allow_unsafe_apis";
static WGPUDawnTogglesDescriptor instanceTogglesDesc{ { nullptr, WGPUSType_DawnTogglesDescriptor}, 1, &enabledToggles };
static WGPUInstanceDescriptor instanceDesc{ reinterpret_cast<WGPUChainedStruct*>(&instanceTogglesDesc) };
#endif
static dawn::native::Instance& GetDawnInstance() {
#if DAWN_UNSAFE_API
static dawn::native::Instance instance(&instanceDesc);
#else
static dawn::native::Instance instance;
#endif
return instance;
}
struct _WGpuCanvasContext {
WGPUSurface surface;
};
// Returns the number of leading zeros.
// Is there a standard C/C++ function for this?
static int clz32(int x) {
int r = 0;
if (!x)
return 32;
if (!(x & 0xffff0000u)) {
x <<= 16;
r += 16;
}
if (!(x & 0xff000000u)) {
x <<= 8;
r += 8;
}
if (!(x & 0xf0000000u)) {
x <<= 4;
r += 4;
}
if (!(x & 0xc0000000u)) {
x <<= 2;
r += 2;
}
if (!(x & 0x80000000u)) {
x <<= 1;
r += 1;
}
return r;
}
static inline _WGpuObject* _wgpu_get(WGpuObjectBase id) {
return (_WGpuObject*)id;
}
template<typename T> inline _WgpuObjectType _wgpu_get_type() { return kWebGPUInvalidObject; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUAdapter>() { return kWebGPUAdapter; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUDevice>() { return kWebGPUDevice; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUBindGroupLayout>() { return kWebGPUBindGroupLayout; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUBuffer>() { return kWebGPUBuffer; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUTexture>() { return kWebGPUTexture; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUTextureView>() { return kWebGPUTextureView; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUExternalTexture>() { return kWebGPUExternalTexture; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUSampler>() { return kWebGPUSampler; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUBindGroup>() { return kWebGPUBindGroup; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUPipelineLayout>() { return kWebGPUPipelineLayout; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUShaderModule>() { return kWebGPUShaderModule; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUComputePipeline>() { return kWebGPUComputePipeline; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPURenderPipeline>() { return kWebGPURenderPipeline; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUCommandEncoder>() { return kWebGPUCommandEncoder; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUComputePassEncoder>() { return kWebGPUComputePassEncoder; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPURenderPassEncoder>() { return kWebGPURenderPassEncoder; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUCommandBuffer>() { return kWebGPUCommandBuffer; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUQuerySet>() { return kWebGPUQuerySet; }
template<> inline _WgpuObjectType _wgpu_get_type<WGPUQueue>() { return kWebGPUQueue; }
template<> inline _WgpuObjectType _wgpu_get_type<_WGpuCanvasContext*>() { return kWebGPUCanvasContext; }
template<typename T>
static inline T _wgpu_get_dawn(WGpuObjectBase id) {
if (!id)
return nullptr;
assert(_wgpu_get_type<T>() == ((_WGpuObject*)id)->type);
return (T)(((_WGpuObject*)id)->dawnObject);
}
static WGpuObjectBase _wgpu_store(_WgpuObjectType type, void* dawnObject) {
_WGpuObject* wgpu;
if (type == kWebGPUBuffer)
wgpu = new _WGpuObjectBuffer{ type, dawnObject, kWebGPUBufferMapStateUnmapped };
else
wgpu = new _WGpuObject{ type, dawnObject };
(*_dawn_to_webgpu)[dawnObject] = wgpu;
return wgpu;
}
static WGpuObjectBase _wgpu_store_and_set_parent(_WgpuObjectType type, void* dawnObject, WGpuObjectBase parent) {
WGpuObjectBase id = _wgpu_store(type, dawnObject);
// derived objects currently aren't being tracked because deleting them can cause potential for memory errors.
return id;
}
void _wgpu_object_destroy(_WGpuObject* obj) {
// Dawn has separate Destroy functions for the different object types.
switch (obj->type) {
case kWebGPUAdapter: {
auto a = (*_webgpu_to_dawn).find(obj->dawnObject);
if (a != (*_webgpu_to_dawn).end()) {
delete (*a).second;
(*_webgpu_to_dawn).erase(a);
}
wgpuAdapterRelease((WGPUAdapter)obj->dawnObject);
break;
}
case kWebGPUDevice:
wgpuDeviceDestroy((WGPUDevice)obj->dawnObject);
break;
case kWebGPUBindGroupLayout:
wgpuBindGroupLayoutRelease((WGPUBindGroupLayout)obj->dawnObject);
break;
case kWebGPUBuffer:
wgpuBufferDestroy((WGPUBuffer)obj->dawnObject);
break;
case kWebGPUTexture:
wgpuTextureDestroy((WGPUTexture)obj->dawnObject);
break;
case kWebGPUTextureView:
wgpuTextureViewRelease((WGPUTextureView)obj->dawnObject);
break;
case kWebGPUExternalTexture:
wgpuExternalTextureDestroy((WGPUExternalTexture)obj->dawnObject);
break;
case kWebGPUSampler:
wgpuSamplerRelease((WGPUSampler)obj->dawnObject);
break;
case kWebGPUBindGroup:
wgpuBindGroupRelease((WGPUBindGroup)obj->dawnObject);
break;
case kWebGPUPipelineLayout:
wgpuPipelineLayoutRelease((WGPUPipelineLayout)obj->dawnObject);
break;
case kWebGPUShaderModule:
wgpuShaderModuleRelease((WGPUShaderModule)obj->dawnObject);
break;
case kWebGPUComputePipeline:
wgpuComputePipelineRelease((WGPUComputePipeline)obj->dawnObject);
break;
case kWebGPURenderPipeline:
wgpuRenderPipelineRelease((WGPURenderPipeline)obj->dawnObject);
break;
case kWebGPUCommandBuffer:
wgpuCommandBufferRelease((WGPUCommandBuffer)obj->dawnObject);
break;
case kWebGPUCommandEncoder:
wgpuCommandEncoderRelease((WGPUCommandEncoder)obj->dawnObject);
break;
case kWebGPUComputePassEncoder:
wgpuComputePassEncoderRelease((WGPUComputePassEncoder)obj->dawnObject);
break;
case kWebGPURenderPassEncoder:
wgpuRenderPassEncoderRelease((WGPURenderPassEncoder)obj->dawnObject);
break;
case kWebGPUQueue:
wgpuQueueRelease((WGPUQueue)obj->dawnObject);
break;
case kWebGPUCanvasContext:
break;
case kWebGPUQuerySet:
wgpuQuerySetDestroy((WGPUQuerySet)obj->dawnObject);
break;
default:
assert(false);
break;
}
obj->dawnObject = 0;
obj->type = kWebGPUInvalidObject;
}
} // namespace
extern "C" {
uint32_t wgpu_get_num_live_objects() {
return _dawn_to_webgpu->size();
}
void wgpu_object_destroy(WGpuObjectBase wgpuObject) {
if (wgpuObject == 0)
return;
_WGpuObject* obj = (_WGpuObject*)wgpuObject;
auto id = _dawn_to_webgpu->find(obj->dawnObject);
if (id == _dawn_to_webgpu->end())
return;
_dawn_to_webgpu->erase(id);
_wgpu_object_destroy(obj);
delete obj;
}
void wgpu_destroy_all_objects() {
for (auto i : *_dawn_to_webgpu) {
_WGpuObject* obj = (_WGpuObject*)i.second;
_wgpu_object_destroy(obj);
delete obj;
}
_dawn_to_webgpu->clear();
}
WGpuCanvasContext wgpu_canvas_get_webgpu_context(void *hwnd) {
WGPUSurfaceDescriptor surfaceDesc{};
#ifdef _WIN32
WGPUSurfaceSourceWindowsHWND chainedDesc = WGPU_SURFACE_SOURCE_WINDOWS_HWND_INIT;
chainedDesc.hinstance = GetModuleHandle(NULL);
chainedDesc.hwnd = hwnd;
chainedDesc.chain = { nullptr, WGPUSType_SurfaceSourceWindowsHWND };
surfaceDesc.nextInChain = reinterpret_cast<WGPUChainedStruct*>(&chainedDesc);
#endif
_WGpuCanvasContext* context = new _WGpuCanvasContext{};
context->surface = wgpuInstanceCreateSurface(GetDawnInstance().Get(), &surfaceDesc);
return _wgpu_store(kWebGPUCanvasContext, context);
}
WGPU_BOOL wgpu_is_valid_object(WGpuObjectBase obj) {
return obj != 0 && _dawn_to_webgpu->find(((_WGpuObject*)obj)->dawnObject) != _dawn_to_webgpu->end();
}
void wgpu_object_set_label(WGpuObjectBase objBase, const char* label) {
_WGpuObject* obj = _wgpu_get(objBase);
assert(obj);
switch (obj->type) {
case kWebGPUDevice:
wgpuDeviceSetLabel((WGPUDevice)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUBindGroupLayout:
wgpuBindGroupLayoutSetLabel((WGPUBindGroupLayout)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUBuffer:
wgpuBufferSetLabel((WGPUBuffer)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUTexture:
wgpuTextureSetLabel((WGPUTexture)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUTextureView:
wgpuTextureViewSetLabel((WGPUTextureView)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUExternalTexture:
wgpuExternalTextureSetLabel((WGPUExternalTexture)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUSampler:
wgpuSamplerSetLabel((WGPUSampler)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUBindGroup:
wgpuBindGroupSetLabel((WGPUBindGroup)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUPipelineLayout:
wgpuPipelineLayoutSetLabel((WGPUPipelineLayout)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUShaderModule:
wgpuShaderModuleSetLabel((WGPUShaderModule)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUComputePipeline:
wgpuComputePipelineSetLabel((WGPUComputePipeline)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPURenderPipeline:
wgpuRenderPipelineSetLabel((WGPURenderPipeline)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUCommandBuffer:
wgpuCommandBufferSetLabel((WGPUCommandBuffer)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUCommandEncoder:
wgpuCommandEncoderSetLabel((WGPUCommandEncoder)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUComputePassEncoder:
wgpuComputePassEncoderSetLabel((WGPUComputePassEncoder)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPURenderPassEncoder:
wgpuRenderPassEncoderSetLabel((WGPURenderPassEncoder)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUQueue:
wgpuQueueSetLabel((WGPUQueue)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
case kWebGPUQuerySet:
wgpuQuerySetSetLabel((WGPUQuerySet)obj->dawnObject, WGPUStringView{label, WGPU_STRLEN });
break;
}
}
int wgpu_object_get_label(WGpuObjectBase obj, char* dstLabel, uint32_t dstLabelSize) {
/* TODO: Dawn has no GetLabel. lib_webgpu.js stores the label in the JS object. */
return 0;
}
WGPU_BOOL navigator_gpu_request_adapter_async(const WGpuRequestAdapterOptions* options, WGpuRequestAdapterCallback adapterCallback, void* userData) {
WGpuAdapter adapter = navigator_gpu_request_adapter_sync(options);
if (adapterCallback)
adapterCallback(adapter, userData);
return true;
}
WGpuAdapter navigator_gpu_request_adapter_sync(const WGpuRequestAdapterOptions* options) {
DawnProcTable procTable = dawn::native::GetProcs();
dawnProcSetProcs(&procTable);
WGPURequestAdapterOptions requestOptions{};
if (options && options->powerPreference == WGPU_POWER_PREFERENCE_LOW_POWER) {
requestOptions.powerPreference = WGPUPowerPreference_LowPower;
} else {
requestOptions.powerPreference = WGPUPowerPreference_HighPerformance;
}
// When interacting with dawn the behavior should mimic chromes default selections
// So instead of specificying a backend, use undefined
requestOptions.backendType = WGPUBackendType_Undefined;
std::vector<dawn::native::Adapter> requestedAdapters = GetDawnInstance().EnumerateAdapters(&requestOptions);
if (!requestedAdapters.empty()) {
// Adapters supporting requested backend are sorted by power preference
// ie : when HighPerformance; Discrete_GPU = 0, Integrated = 1, CPU = 2
auto* finalAdapter = new dawn::native::Adapter(requestedAdapters[0]);
(*_webgpu_to_dawn)[finalAdapter->Get()] = finalAdapter;
return _wgpu_store(kWebGPUAdapter, finalAdapter->Get());
}
return {0};
}
void navigator_gpu_request_adapter_async_simple(WGpuRequestAdapterCallback adapterCallback) {
WGpuAdapter adapter = navigator_gpu_request_adapter_sync_simple();
if (adapterCallback)
adapterCallback(adapter, nullptr);
}
WGpuAdapter navigator_gpu_request_adapter_sync_simple() {
WGpuRequestAdapterOptions options = { WGPU_POWER_PREFERENCE_HIGH_PERFORMANCE, false };
return navigator_gpu_request_adapter_sync(&options);
}
WGPU_TEXTURE_FORMAT navigator_gpu_get_preferred_canvas_format(WGpuAdapter adapter, WGpuCanvasContext canvasContext) {
assert(wgpu_is_adapter(adapter));
assert(wgpu_is_canvas_context(canvasContext));
_WGpuCanvasContext* context = _wgpu_get_dawn<_WGpuCanvasContext*>(canvasContext);
WGPUSurfaceCapabilities capabilities = WGPU_SURFACE_CAPABILITIES_INIT;
wgpuSurfaceGetCapabilities(context->surface, _wgpu_get_dawn<WGPUAdapter>(adapter), &capabilities);
assert(capabilities.formats && capabilities.formatCount > 0);
return capabilities.formats[0];
}
WGPU_BOOL wgpu_is_adapter(WGpuObjectBase object) {
_WGpuObject* obj = _wgpu_get(object);
return obj && obj->type == kWebGPUAdapter;
}
WGPU_FEATURES_BITFIELD wgpu_adapter_or_device_get_features(WGpuAdapter adapterOrDevice) {
_WGpuObject* obj = _wgpu_get(adapterOrDevice);
assert(obj && (obj->type != kWebGPUDevice || obj->type != kWebGPUAdapter));
bool isDevice = obj->type == kWebGPUDevice;