This repository was archived by the owner on May 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgpu.go
More file actions
2769 lines (2450 loc) · 100 KB
/
webgpu.go
File metadata and controls
2769 lines (2450 loc) · 100 KB
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
package wgpu
// TODO(dh): https://github.com/gfx-rs/wgpu/issues/3794#issuecomment-1954631975
// #include <stdlib.h>
// #include "./webgpu.h"
// #include "./wgpu.h"
//
// void requestAdapterCallback(WGPURequestAdapterStatus status, WGPUAdapter adapter, char *msg, void *data);
// void requestDeviceCallback(WGPURequestDeviceStatus status, WGPUDevice device, char *msg, void *data);
// void deviceLostCallback(WGPUDeviceLostReason reason, char *msg, uintptr_t hnd);
// void popErrorScopeCallback(WGPUErrorType type, char *message, void *userdata);
// void mapCallback(WGPUBufferMapAsyncStatus status, void *userdata);
// void doneCallback(WGPUQueueWorkDoneStatus status, void *userdata);
// void uncapturedErrorCallback(WGPUErrorType type, char *message, void *userdata);
import "C"
import (
"errors"
"fmt"
"runtime"
"runtime/cgo"
"structs"
"sync"
"unsafe"
"golang.org/x/exp/constraints"
"honnef.co/go/safeish"
)
type fp = *[0]byte
type up = unsafe.Pointer
//go:generate stringer -type requestAdapterStatus,requestDeviceStatus,PowerPreference,BackendType,AdapterType,DeviceLostReason,ErrorFilter,PrimitiveTopology,IndexFormat,FrontFace,CullMode,TextureFormat,VertexFormat,CompareFunction,StencilOperation,BlendOperation,BlendFactor,ColorWriteMask,VertexStepMode,PresentMode,CompositeAlphaMode,TextureUsage,TextureViewDimension,TextureAspect,LoadOp,StoreOp,QueryType,BufferUsage,MapMode,BufferMapState,ShaderStage,BufferBindingType,SamplerBindingType,TextureSampleType,StorageTextureAccess,TextureDimension,AddressMode,FilterMode,MipmapFilterMode -linecomment -output webgpu1_string.go
type requestAdapterStatus uint32
type PowerPreference uint32
type BackendType uint32
type AdapterType uint32
type DeviceLostReason uint32
type ErrorFilter uint32
type PrimitiveTopology uint32
type IndexFormat uint32
type FrontFace uint32
type CullMode uint32
type TextureFormat uint32
type VertexFormat uint32
type CompareFunction uint32
type StencilOperation uint32
type BlendOperation uint32
type BlendFactor uint32
type ColorWriteMask uint32
type VertexStepMode uint32
type PresentMode uint32
type CompositeAlphaMode uint32
type TextureUsage uint32
type TextureViewDimension uint32
type TextureAspect uint32
type LoadOp uint32
type StoreOp uint32
type QueryType uint32
type BufferUsage uint32
type MapMode uint32
type BufferMapState uint32
type ShaderStage uint32
type BufferBindingType uint32
type SamplerBindingType uint32
type TextureSampleType uint32
type StorageTextureAccess uint32
type TextureDimension uint32
type AddressMode uint32
type FilterMode uint32
type MipmapFilterMode uint32
//go:generate stringer -type FeatureName -output webgpu2_string.go
type FeatureName uint32
var ErrAdapterUnavailable = errors.New("no adapter available")
type RequestAdapterError struct {
Message string
}
func (err RequestAdapterError) Error() string { return err.Message }
const (
requestAdapterStatusSuccess requestAdapterStatus = C.WGPURequestAdapterStatus_Success // Success
requestAdapterStatusUnavailable requestAdapterStatus = C.WGPURequestAdapterStatus_Unavailable // Unavailable
requestAdapterStatusError requestAdapterStatus = C.WGPURequestAdapterStatus_Error // Error
requestAdapterStatusUnknown requestAdapterStatus = C.WGPURequestAdapterStatus_Unknown // Unknown
)
type requestDeviceStatus uint32
type RequestDeviceError struct {
Message string
}
func (err RequestDeviceError) Error() string { return err.Message }
const (
requestDeviceStatusSuccess requestDeviceStatus = C.WGPURequestDeviceStatus_Success // Success
requestDeviceStatusError requestDeviceStatus = C.WGPURequestDeviceStatus_Error // Error
requestDeviceStatusUnknown requestDeviceStatus = C.WGPURequestDeviceStatus_Unknown // Unknown
)
const (
FeatureNameUndefined FeatureName = C.WGPUFeatureName_Undefined // Undefined
FeatureNameDepthClipControl FeatureName = C.WGPUFeatureName_DepthClipControl // DepthClipControl
FeatureNameDepth32FloatStencil8 FeatureName = C.WGPUFeatureName_Depth32FloatStencil8 // Depth32FloatStencil8
FeatureNameTimestampQuery FeatureName = C.WGPUFeatureName_TimestampQuery // TimestampQuery
FeatureNameTextureCompressionBC FeatureName = C.WGPUFeatureName_TextureCompressionBC // TextureCompressionBC
FeatureNameTextureCompressionETC2 FeatureName = C.WGPUFeatureName_TextureCompressionETC2 // TextureCompressionETC2
FeatureNameTextureCompressionASTC FeatureName = C.WGPUFeatureName_TextureCompressionASTC // TextureCompressionASTC
FeatureNameIndirectFirstInstance FeatureName = C.WGPUFeatureName_IndirectFirstInstance // IndirectFirstInstance
FeatureNameShaderF16 FeatureName = C.WGPUFeatureName_ShaderF16 // ShaderF16
FeatureNameRG11B10UfloatRenderable FeatureName = C.WGPUFeatureName_RG11B10UfloatRenderable // RG11B10UfloatRenderable
FeatureNameBGRA8UnormStorage FeatureName = C.WGPUFeatureName_BGRA8UnormStorage // BGRA8UnormStorage
FeatureNameFloat32Filterable FeatureName = C.WGPUFeatureName_Float32Filterable // Float32Filterable
)
const (
PowerPreferenceUndefined PowerPreference = C.WGPUPowerPreference_Undefined // Undefined
PowerPreferenceLowPower PowerPreference = C.WGPUPowerPreference_LowPower // LowPower
PowerPreferenceHighPerformance PowerPreference = C.WGPUPowerPreference_HighPerformance // HighPerformance
)
const (
BackendTypeUndefined BackendType = C.WGPUBackendType_Undefined // Undefined
BackendTypeNull BackendType = C.WGPUBackendType_Null // Null
BackendTypeWebGPU BackendType = C.WGPUBackendType_WebGPU // WebGPU
BackendTypeD3D11 BackendType = C.WGPUBackendType_D3D11 // D3D11
BackendTypeD3D12 BackendType = C.WGPUBackendType_D3D12 // D3D12
BackendTypeMetal BackendType = C.WGPUBackendType_Metal // Metal
BackendTypeVulkan BackendType = C.WGPUBackendType_Vulkan // Vulkan
BackendTypeOpenGL BackendType = C.WGPUBackendType_OpenGL // OpenGL
BackendTypeOpenGLES BackendType = C.WGPUBackendType_OpenGLES // OpenGLES
)
const (
AdapterTypeDiscreteGPU AdapterType = C.WGPUAdapterType_DiscreteGPU // DiscreteGPU
AdapterTypeIntegratedGPU AdapterType = C.WGPUAdapterType_IntegratedGPU // IntegratedGPU
AdapterTypeCPU AdapterType = C.WGPUAdapterType_CPU // CPU
AdapterTypeUnknown AdapterType = C.WGPUAdapterType_Unknown // Unknown
)
const (
DeviceLostReasonUndefined DeviceLostReason = C.WGPUDeviceLostReason_Undefined // Undefined
DeviceLostReasonDestroyed DeviceLostReason = C.WGPUDeviceLostReason_Destroyed // Destroyed
)
const (
ErrorFilterValidation ErrorFilter = C.WGPUErrorFilter_Validation // Validation
ErrorFilterOutOfMemory ErrorFilter = C.WGPUErrorFilter_OutOfMemory // OutOfMemory
ErrorFilterInternal ErrorFilter = C.WGPUErrorFilter_Internal // Internal
)
const (
PrimitiveTopologyPointList PrimitiveTopology = C.WGPUPrimitiveTopology_PointList // PointList
PrimitiveTopologyLineList PrimitiveTopology = C.WGPUPrimitiveTopology_LineList // LineList
PrimitiveTopologyLineStrip PrimitiveTopology = C.WGPUPrimitiveTopology_LineStrip // LineStrip
PrimitiveTopologyTriangleList PrimitiveTopology = C.WGPUPrimitiveTopology_TriangleList // TriangleList
PrimitiveTopologyTriangleStrip PrimitiveTopology = C.WGPUPrimitiveTopology_TriangleStrip // TriangleStrip
)
const (
IndexFormatUndefined IndexFormat = C.WGPUIndexFormat_Undefined // Undefined
IndexFormatUint16 IndexFormat = C.WGPUIndexFormat_Uint16 // Uint16
IndexFormatUint32 IndexFormat = C.WGPUIndexFormat_Uint32 // Uint32
)
const (
FrontFaceCCW FrontFace = C.WGPUFrontFace_CCW // CCW
FrontFaceCW FrontFace = C.WGPUFrontFace_CW // CW
)
const (
CullModeNone CullMode = C.WGPUCullMode_None // None
CullModeFront CullMode = C.WGPUCullMode_Front // Front
CullModeBack CullMode = C.WGPUCullMode_Back // Back
)
const (
TextureFormatUndefined TextureFormat = C.WGPUTextureFormat_Undefined // Undefined
TextureFormatR8Unorm TextureFormat = C.WGPUTextureFormat_R8Unorm // R8Unorm
TextureFormatR8Snorm TextureFormat = C.WGPUTextureFormat_R8Snorm // R8Snorm
TextureFormatR8Uint TextureFormat = C.WGPUTextureFormat_R8Uint // R8Uint
TextureFormatR8Sint TextureFormat = C.WGPUTextureFormat_R8Sint // R8Sint
TextureFormatR16Uint TextureFormat = C.WGPUTextureFormat_R16Uint // R16Uint
TextureFormatR16Sint TextureFormat = C.WGPUTextureFormat_R16Sint // R16Sint
TextureFormatR16Float TextureFormat = C.WGPUTextureFormat_R16Float // R16Float
TextureFormatRG8Unorm TextureFormat = C.WGPUTextureFormat_RG8Unorm // RG8Unorm
TextureFormatRG8Snorm TextureFormat = C.WGPUTextureFormat_RG8Snorm // RG8Snorm
TextureFormatRG8Uint TextureFormat = C.WGPUTextureFormat_RG8Uint // RG8Uint
TextureFormatRG8Sint TextureFormat = C.WGPUTextureFormat_RG8Sint // RG8Sint
TextureFormatR32Float TextureFormat = C.WGPUTextureFormat_R32Float // R32Float
TextureFormatR32Uint TextureFormat = C.WGPUTextureFormat_R32Uint // R32Uint
TextureFormatR32Sint TextureFormat = C.WGPUTextureFormat_R32Sint // R32Sint
TextureFormatRG16Uint TextureFormat = C.WGPUTextureFormat_RG16Uint // RG16Uint
TextureFormatRG16Sint TextureFormat = C.WGPUTextureFormat_RG16Sint // RG16Sint
TextureFormatRG16Float TextureFormat = C.WGPUTextureFormat_RG16Float // RG16Float
TextureFormatRGBA8Unorm TextureFormat = C.WGPUTextureFormat_RGBA8Unorm // RGBA8Unorm
TextureFormatRGBA8UnormSrgb TextureFormat = C.WGPUTextureFormat_RGBA8UnormSrgb // RGBA8UnormSrgb
TextureFormatRGBA8Snorm TextureFormat = C.WGPUTextureFormat_RGBA8Snorm // RGBA8Snorm
TextureFormatRGBA8Uint TextureFormat = C.WGPUTextureFormat_RGBA8Uint // RGBA8Uint
TextureFormatRGBA8Sint TextureFormat = C.WGPUTextureFormat_RGBA8Sint // RGBA8Sint
TextureFormatBGRA8Unorm TextureFormat = C.WGPUTextureFormat_BGRA8Unorm // BGRA8Unorm
TextureFormatBGRA8UnormSrgb TextureFormat = C.WGPUTextureFormat_BGRA8UnormSrgb // BGRA8UnormSrgb
TextureFormatRGB10A2Uint TextureFormat = C.WGPUTextureFormat_RGB10A2Uint // RGB10A2Uint
TextureFormatRGB10A2Unorm TextureFormat = C.WGPUTextureFormat_RGB10A2Unorm // RGB10A2Unorm
TextureFormatRG11B10Ufloat TextureFormat = C.WGPUTextureFormat_RG11B10Ufloat // RG11B10Ufloat
TextureFormatRGB9E5Ufloat TextureFormat = C.WGPUTextureFormat_RGB9E5Ufloat // RGB9E5Ufloat
TextureFormatRG32Float TextureFormat = C.WGPUTextureFormat_RG32Float // RG32Float
TextureFormatRG32Uint TextureFormat = C.WGPUTextureFormat_RG32Uint // RG32Uint
TextureFormatRG32Sint TextureFormat = C.WGPUTextureFormat_RG32Sint // RG32Sint
TextureFormatRGBA16Uint TextureFormat = C.WGPUTextureFormat_RGBA16Uint // RGBA16Uint
TextureFormatRGBA16Sint TextureFormat = C.WGPUTextureFormat_RGBA16Sint // RGBA16Sint
TextureFormatRGBA16Float TextureFormat = C.WGPUTextureFormat_RGBA16Float // RGBA16Float
TextureFormatRGBA32Float TextureFormat = C.WGPUTextureFormat_RGBA32Float // RGBA32Float
TextureFormatRGBA32Uint TextureFormat = C.WGPUTextureFormat_RGBA32Uint // RGBA32Uint
TextureFormatRGBA32Sint TextureFormat = C.WGPUTextureFormat_RGBA32Sint // RGBA32Sint
TextureFormatStencil8 TextureFormat = C.WGPUTextureFormat_Stencil8 // Stencil8
TextureFormatDepth16Unorm TextureFormat = C.WGPUTextureFormat_Depth16Unorm // Depth16Unorm
TextureFormatDepth24Plus TextureFormat = C.WGPUTextureFormat_Depth24Plus // Depth24Plus
TextureFormatDepth24PlusStencil8 TextureFormat = C.WGPUTextureFormat_Depth24PlusStencil8 // Depth24PlusStencil8
TextureFormatDepth32Float TextureFormat = C.WGPUTextureFormat_Depth32Float // Depth32Float
TextureFormatDepth32FloatStencil8 TextureFormat = C.WGPUTextureFormat_Depth32FloatStencil8 // Depth32FloatStencil8
TextureFormatBC1RGBAUnorm TextureFormat = C.WGPUTextureFormat_BC1RGBAUnorm // BC1RGBAUnorm
TextureFormatBC1RGBAUnormSrgb TextureFormat = C.WGPUTextureFormat_BC1RGBAUnormSrgb // BC1RGBAUnormSrgb
TextureFormatBC2RGBAUnorm TextureFormat = C.WGPUTextureFormat_BC2RGBAUnorm // BC2RGBAUnorm
TextureFormatBC2RGBAUnormSrgb TextureFormat = C.WGPUTextureFormat_BC2RGBAUnormSrgb // BC2RGBAUnormSrgb
TextureFormatBC3RGBAUnorm TextureFormat = C.WGPUTextureFormat_BC3RGBAUnorm // BC3RGBAUnorm
TextureFormatBC3RGBAUnormSrgb TextureFormat = C.WGPUTextureFormat_BC3RGBAUnormSrgb // BC3RGBAUnormSrgb
TextureFormatBC4RUnorm TextureFormat = C.WGPUTextureFormat_BC4RUnorm // BC4RUnorm
TextureFormatBC4RSnorm TextureFormat = C.WGPUTextureFormat_BC4RSnorm // BC4RSnorm
TextureFormatBC5RGUnorm TextureFormat = C.WGPUTextureFormat_BC5RGUnorm // BC5RGUnorm
TextureFormatBC5RGSnorm TextureFormat = C.WGPUTextureFormat_BC5RGSnorm // BC5RGSnorm
TextureFormatBC6HRGBUfloat TextureFormat = C.WGPUTextureFormat_BC6HRGBUfloat // BC6HRGBUfloat
TextureFormatBC6HRGBFloat TextureFormat = C.WGPUTextureFormat_BC6HRGBFloat // BC6HRGBFloat
TextureFormatBC7RGBAUnorm TextureFormat = C.WGPUTextureFormat_BC7RGBAUnorm // BC7RGBAUnorm
TextureFormatBC7RGBAUnormSrgb TextureFormat = C.WGPUTextureFormat_BC7RGBAUnormSrgb // BC7RGBAUnormSrgb
TextureFormatETC2RGB8Unorm TextureFormat = C.WGPUTextureFormat_ETC2RGB8Unorm // ETC2RGB8Unorm
TextureFormatETC2RGB8UnormSrgb TextureFormat = C.WGPUTextureFormat_ETC2RGB8UnormSrgb // ETC2RGB8UnormSrgb
TextureFormatETC2RGB8A1Unorm TextureFormat = C.WGPUTextureFormat_ETC2RGB8A1Unorm // ETC2RGB8A1Unorm
TextureFormatETC2RGB8A1UnormSrgb TextureFormat = C.WGPUTextureFormat_ETC2RGB8A1UnormSrgb // ETC2RGB8A1UnormSrgb
TextureFormatETC2RGBA8Unorm TextureFormat = C.WGPUTextureFormat_ETC2RGBA8Unorm // ETC2RGBA8Unorm
TextureFormatETC2RGBA8UnormSrgb TextureFormat = C.WGPUTextureFormat_ETC2RGBA8UnormSrgb // ETC2RGBA8UnormSrgb
TextureFormatEACR11Unorm TextureFormat = C.WGPUTextureFormat_EACR11Unorm // EACR11Unorm
TextureFormatEACR11Snorm TextureFormat = C.WGPUTextureFormat_EACR11Snorm // EACR11Snorm
TextureFormatEACRG11Unorm TextureFormat = C.WGPUTextureFormat_EACRG11Unorm // EACRG11Unorm
TextureFormatEACRG11Snorm TextureFormat = C.WGPUTextureFormat_EACRG11Snorm // EACRG11Snorm
TextureFormatASTC4x4Unorm TextureFormat = C.WGPUTextureFormat_ASTC4x4Unorm // ASTC4x4Unorm
TextureFormatASTC4x4UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC4x4UnormSrgb // ASTC4x4UnormSrgb
TextureFormatASTC5x4Unorm TextureFormat = C.WGPUTextureFormat_ASTC5x4Unorm // ASTC5x4Unorm
TextureFormatASTC5x4UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC5x4UnormSrgb // ASTC5x4UnormSrgb
TextureFormatASTC5x5Unorm TextureFormat = C.WGPUTextureFormat_ASTC5x5Unorm // ASTC5x5Unorm
TextureFormatASTC5x5UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC5x5UnormSrgb // ASTC5x5UnormSrgb
TextureFormatASTC6x5Unorm TextureFormat = C.WGPUTextureFormat_ASTC6x5Unorm // ASTC6x5Unorm
TextureFormatASTC6x5UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC6x5UnormSrgb // ASTC6x5UnormSrgb
TextureFormatASTC6x6Unorm TextureFormat = C.WGPUTextureFormat_ASTC6x6Unorm // ASTC6x6Unorm
TextureFormatASTC6x6UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC6x6UnormSrgb // ASTC6x6UnormSrgb
TextureFormatASTC8x5Unorm TextureFormat = C.WGPUTextureFormat_ASTC8x5Unorm // ASTC8x5Unorm
TextureFormatASTC8x5UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC8x5UnormSrgb // ASTC8x5UnormSrgb
TextureFormatASTC8x6Unorm TextureFormat = C.WGPUTextureFormat_ASTC8x6Unorm // ASTC8x6Unorm
TextureFormatASTC8x6UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC8x6UnormSrgb // ASTC8x6UnormSrgb
TextureFormatASTC8x8Unorm TextureFormat = C.WGPUTextureFormat_ASTC8x8Unorm // ASTC8x8Unorm
TextureFormatASTC8x8UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC8x8UnormSrgb // ASTC8x8UnormSrgb
TextureFormatASTC10x5Unorm TextureFormat = C.WGPUTextureFormat_ASTC10x5Unorm // ASTC10x5Unorm
TextureFormatASTC10x5UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC10x5UnormSrgb // ASTC10x5UnormSrgb
TextureFormatASTC10x6Unorm TextureFormat = C.WGPUTextureFormat_ASTC10x6Unorm // ASTC10x6Unorm
TextureFormatASTC10x6UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC10x6UnormSrgb // ASTC10x6UnormSrgb
TextureFormatASTC10x8Unorm TextureFormat = C.WGPUTextureFormat_ASTC10x8Unorm // ASTC10x8Unorm
TextureFormatASTC10x8UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC10x8UnormSrgb // ASTC10x8UnormSrgb
TextureFormatASTC10x10Unorm TextureFormat = C.WGPUTextureFormat_ASTC10x10Unorm // ASTC10x10Unorm
TextureFormatASTC10x10UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC10x10UnormSrgb // ASTC10x10UnormSrgb
TextureFormatASTC12x10Unorm TextureFormat = C.WGPUTextureFormat_ASTC12x10Unorm // ASTC12x10Unorm
TextureFormatASTC12x10UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC12x10UnormSrgb // ASTC12x10UnormSrgb
TextureFormatASTC12x12Unorm TextureFormat = C.WGPUTextureFormat_ASTC12x12Unorm // ASTC12x12Unorm
TextureFormatASTC12x12UnormSrgb TextureFormat = C.WGPUTextureFormat_ASTC12x12UnormSrgb // ASTC12x12UnormSrgb
)
const (
VertexFormatUndefined VertexFormat = C.WGPUVertexFormat_Undefined // Undefined
VertexFormatUint8x2 VertexFormat = C.WGPUVertexFormat_Uint8x2 // Uint8x2
VertexFormatUint8x4 VertexFormat = C.WGPUVertexFormat_Uint8x4 // Uint8x4
VertexFormatSint8x2 VertexFormat = C.WGPUVertexFormat_Sint8x2 // Sint8x2
VertexFormatSint8x4 VertexFormat = C.WGPUVertexFormat_Sint8x4 // Sint8x4
VertexFormatUnorm8x2 VertexFormat = C.WGPUVertexFormat_Unorm8x2 // Unorm8x2
VertexFormatUnorm8x4 VertexFormat = C.WGPUVertexFormat_Unorm8x4 // Unorm8x4
VertexFormatSnorm8x2 VertexFormat = C.WGPUVertexFormat_Snorm8x2 // Snorm8x2
VertexFormatSnorm8x4 VertexFormat = C.WGPUVertexFormat_Snorm8x4 // Snorm8x4
VertexFormatUint16x2 VertexFormat = C.WGPUVertexFormat_Uint16x2 // Uint16x2
VertexFormatUint16x4 VertexFormat = C.WGPUVertexFormat_Uint16x4 // Uint16x4
VertexFormatSint16x2 VertexFormat = C.WGPUVertexFormat_Sint16x2 // Sint16x2
VertexFormatSint16x4 VertexFormat = C.WGPUVertexFormat_Sint16x4 // Sint16x4
VertexFormatUnorm16x2 VertexFormat = C.WGPUVertexFormat_Unorm16x2 // Unorm16x2
VertexFormatUnorm16x4 VertexFormat = C.WGPUVertexFormat_Unorm16x4 // Unorm16x4
VertexFormatSnorm16x2 VertexFormat = C.WGPUVertexFormat_Snorm16x2 // Snorm16x2
VertexFormatSnorm16x4 VertexFormat = C.WGPUVertexFormat_Snorm16x4 // Snorm16x4
VertexFormatFloat16x2 VertexFormat = C.WGPUVertexFormat_Float16x2 // Float16x2
VertexFormatFloat16x4 VertexFormat = C.WGPUVertexFormat_Float16x4 // Float16x4
VertexFormatFloat32 VertexFormat = C.WGPUVertexFormat_Float32 // Float32
VertexFormatFloat32x2 VertexFormat = C.WGPUVertexFormat_Float32x2 // Float32x2
VertexFormatFloat32x3 VertexFormat = C.WGPUVertexFormat_Float32x3 // Float32x3
VertexFormatFloat32x4 VertexFormat = C.WGPUVertexFormat_Float32x4 // Float32x4
VertexFormatUint32 VertexFormat = C.WGPUVertexFormat_Uint32 // Uint32
VertexFormatUint32x2 VertexFormat = C.WGPUVertexFormat_Uint32x2 // Uint32x2
VertexFormatUint32x3 VertexFormat = C.WGPUVertexFormat_Uint32x3 // Uint32x3
VertexFormatUint32x4 VertexFormat = C.WGPUVertexFormat_Uint32x4 // Uint32x4
VertexFormatSint32 VertexFormat = C.WGPUVertexFormat_Sint32 // Sint32
VertexFormatSint32x2 VertexFormat = C.WGPUVertexFormat_Sint32x2 // Sint32x2
VertexFormatSint32x3 VertexFormat = C.WGPUVertexFormat_Sint32x3 // Sint32x3
VertexFormatSint32x4 VertexFormat = C.WGPUVertexFormat_Sint32x4 // Sint32x4
)
const (
CompareFunctionUndefined CompareFunction = C.WGPUCompareFunction_Undefined // Undefined
CompareFunctionNever CompareFunction = C.WGPUCompareFunction_Never // Never
CompareFunctionLess CompareFunction = C.WGPUCompareFunction_Less // Less
CompareFunctionLessEqual CompareFunction = C.WGPUCompareFunction_LessEqual // LessEqual
CompareFunctionGreater CompareFunction = C.WGPUCompareFunction_Greater // Greater
CompareFunctionGreaterEqual CompareFunction = C.WGPUCompareFunction_GreaterEqual // GreaterEqual
CompareFunctionEqual CompareFunction = C.WGPUCompareFunction_Equal // Equal
CompareFunctionNotEqual CompareFunction = C.WGPUCompareFunction_NotEqual // NotEqual
CompareFunctionAlways CompareFunction = C.WGPUCompareFunction_Always // Always
)
const (
StencilOperationKeep StencilOperation = C.WGPUStencilOperation_Keep // Keep
StencilOperationZero StencilOperation = C.WGPUStencilOperation_Zero // Zero
StencilOperationReplace StencilOperation = C.WGPUStencilOperation_Replace // Replace
StencilOperationInvert StencilOperation = C.WGPUStencilOperation_Invert // Invert
StencilOperationIncrementClamp StencilOperation = C.WGPUStencilOperation_IncrementClamp // IncrementClamp
StencilOperationDecrementClamp StencilOperation = C.WGPUStencilOperation_DecrementClamp // DecrementClamp
StencilOperationIncrementWrap StencilOperation = C.WGPUStencilOperation_IncrementWrap // IncrementWrap
StencilOperationDecrementWrap StencilOperation = C.WGPUStencilOperation_DecrementWrap // DecrementWrap
)
const (
BlendOperationAdd BlendOperation = C.WGPUBlendOperation_Add // Add
BlendOperationSubtract BlendOperation = C.WGPUBlendOperation_Subtract // Subtract
BlendOperationReverseSubtract BlendOperation = C.WGPUBlendOperation_ReverseSubtract // ReverseSubtract
BlendOperationMin BlendOperation = C.WGPUBlendOperation_Min // Min
BlendOperationMax BlendOperation = C.WGPUBlendOperation_Max // Max
)
const (
BlendFactorZero BlendFactor = C.WGPUBlendFactor_Zero // Zero
BlendFactorOne BlendFactor = C.WGPUBlendFactor_One // One
BlendFactorSrc BlendFactor = C.WGPUBlendFactor_Src // Src
BlendFactorOneMinusSrc BlendFactor = C.WGPUBlendFactor_OneMinusSrc // OneMinusSrc
BlendFactorSrcAlpha BlendFactor = C.WGPUBlendFactor_SrcAlpha // SrcAlpha
BlendFactorOneMinusSrcAlpha BlendFactor = C.WGPUBlendFactor_OneMinusSrcAlpha // OneMinusSrcAlpha
BlendFactorDst BlendFactor = C.WGPUBlendFactor_Dst // Dst
BlendFactorOneMinusDst BlendFactor = C.WGPUBlendFactor_OneMinusDst // OneMinusDst
BlendFactorDstAlpha BlendFactor = C.WGPUBlendFactor_DstAlpha // DstAlpha
BlendFactorOneMinusDstAlpha BlendFactor = C.WGPUBlendFactor_OneMinusDstAlpha // OneMinusDstAlpha
BlendFactorSrcAlphaSaturated BlendFactor = C.WGPUBlendFactor_SrcAlphaSaturated // SrcAlphaSaturated
BlendFactorConstant BlendFactor = C.WGPUBlendFactor_Constant // Constant
BlendFactorOneMinusConstant BlendFactor = C.WGPUBlendFactor_OneMinusConstant // OneMinusConstant
)
const (
ColorWriteMaskNone ColorWriteMask = C.WGPUColorWriteMask_None // None
ColorWriteMaskRed ColorWriteMask = C.WGPUColorWriteMask_Red // Red
ColorWriteMaskGreen ColorWriteMask = C.WGPUColorWriteMask_Green // Green
ColorWriteMaskBlue ColorWriteMask = C.WGPUColorWriteMask_Blue // Blue
ColorWriteMaskAlpha ColorWriteMask = C.WGPUColorWriteMask_Alpha // Alpha
ColorWriteMaskAll ColorWriteMask = C.WGPUColorWriteMask_All // All
)
const (
VertexStepModeVertex VertexStepMode = C.WGPUVertexStepMode_Vertex // Vertex
VertexStepModeInstance VertexStepMode = C.WGPUVertexStepMode_Instance // Instance
VertexStepModeVertexBufferNotUsed VertexStepMode = C.WGPUVertexStepMode_VertexBufferNotUsed // VertexBufferNotUsed
)
const (
PresentModeFifo PresentMode = C.WGPUPresentMode_Fifo // Fifo
PresentModeFifoRelaxed PresentMode = C.WGPUPresentMode_FifoRelaxed // FifoRelaxed
PresentModeImmediate PresentMode = C.WGPUPresentMode_Immediate // Immediate
PresentModeMailbox PresentMode = C.WGPUPresentMode_Mailbox // Mailbox
)
const (
CompositeAlphaModeAuto CompositeAlphaMode = C.WGPUCompositeAlphaMode_Auto // Auto
CompositeAlphaModeOpaque CompositeAlphaMode = C.WGPUCompositeAlphaMode_Opaque // Opaque
CompositeAlphaModePremultiplied CompositeAlphaMode = C.WGPUCompositeAlphaMode_Premultiplied // Premultiplied
CompositeAlphaModeUnpremultiplied CompositeAlphaMode = C.WGPUCompositeAlphaMode_Unpremultiplied // Unpremultiplied
CompositeAlphaModeInherit CompositeAlphaMode = C.WGPUCompositeAlphaMode_Inherit // Inherit
)
const (
TextureUsageNone TextureUsage = C.WGPUTextureUsage_None // None
TextureUsageCopySrc TextureUsage = C.WGPUTextureUsage_CopySrc // CopySrc
TextureUsageCopyDst TextureUsage = C.WGPUTextureUsage_CopyDst // CopyDst
TextureUsageTextureBinding TextureUsage = C.WGPUTextureUsage_TextureBinding // TextureBinding
TextureUsageStorageBinding TextureUsage = C.WGPUTextureUsage_StorageBinding // StorageBinding
TextureUsageRenderAttachment TextureUsage = C.WGPUTextureUsage_RenderAttachment // RenderAttachment
)
const (
TextureViewDimensionUndefined TextureViewDimension = C.WGPUTextureViewDimension_Undefined // undefined
TextureViewDimension1D TextureViewDimension = C.WGPUTextureViewDimension_1D // 1D
TextureViewDimension2D TextureViewDimension = C.WGPUTextureViewDimension_2D // 2D
TextureViewDimension2DArray TextureViewDimension = C.WGPUTextureViewDimension_2DArray // 2DArray
TextureViewDimensionCube TextureViewDimension = C.WGPUTextureViewDimension_Cube // Cube
TextureViewDimensionCubeArray TextureViewDimension = C.WGPUTextureViewDimension_CubeArray // CubeArray
TextureViewDimension3D TextureViewDimension = C.WGPUTextureViewDimension_3D // 3D
)
const (
TextureAspectAll TextureAspect = C.WGPUTextureAspect_All // All
TextureAspectStencilOnly TextureAspect = C.WGPUTextureAspect_StencilOnly // StencilOnly
TextureAspectDepthOnly TextureAspect = C.WGPUTextureAspect_DepthOnly // DepthOnly
)
const (
LoadOpUndefined LoadOp = C.WGPULoadOp_Undefined // Undefined
LoadOpClear LoadOp = C.WGPULoadOp_Clear // Clear
LoadOpLoad LoadOp = C.WGPULoadOp_Load // Load
)
const (
StoreOpUndefined StoreOp = C.WGPUStoreOp_Undefined // Undefined
StoreOpStore StoreOp = C.WGPUStoreOp_Store // Store
StoreOpDiscard StoreOp = C.WGPUStoreOp_Discard // Discard
)
const (
QueryTypeOcclusion QueryType = C.WGPUQueryType_Occlusion // Occlusion
QueryTypeTimestamp QueryType = C.WGPUQueryType_Timestamp // Timestamp
)
const (
BufferUsageNone BufferUsage = C.WGPUBufferUsage_None // None
BufferUsageMapRead BufferUsage = C.WGPUBufferUsage_MapRead // MapRead
BufferUsageMapWrite BufferUsage = C.WGPUBufferUsage_MapWrite // MapWrite
BufferUsageCopySrc BufferUsage = C.WGPUBufferUsage_CopySrc // CopySrc
BufferUsageCopyDst BufferUsage = C.WGPUBufferUsage_CopyDst // CopyDst
BufferUsageIndex BufferUsage = C.WGPUBufferUsage_Index // Index
BufferUsageVertex BufferUsage = C.WGPUBufferUsage_Vertex // Vertex
BufferUsageUniform BufferUsage = C.WGPUBufferUsage_Uniform // Uniform
BufferUsageStorage BufferUsage = C.WGPUBufferUsage_Storage // Storage
BufferUsageIndirect BufferUsage = C.WGPUBufferUsage_Indirect // Indirect
BufferUsageQueryResolve BufferUsage = C.WGPUBufferUsage_QueryResolve // QueryResolve
)
const (
MapModeNone MapMode = C.WGPUMapMode_None // None
MapModeRead MapMode = C.WGPUMapMode_Read // Read
MapModeWrite MapMode = C.WGPUMapMode_Write // Write
)
const (
BufferMapStateUnmapped BufferMapState = C.WGPUBufferMapState_Unmapped // Unmapped
BufferMapStatePending BufferMapState = C.WGPUBufferMapState_Pending // Pending
BufferMapStateMapped BufferMapState = C.WGPUBufferMapState_Mapped // Mapped
)
const (
ShaderStageNone ShaderStage = C.WGPUShaderStage_None // None
ShaderStageVertex ShaderStage = C.WGPUShaderStage_Vertex // Vertex
ShaderStageFragment ShaderStage = C.WGPUShaderStage_Fragment // Fragment
ShaderStageCompute ShaderStage = C.WGPUShaderStage_Compute // Compute
)
const (
BufferBindingTypeUndefined BufferBindingType = C.WGPUBufferBindingType_Undefined // Undefined
BufferBindingTypeUniform BufferBindingType = C.WGPUBufferBindingType_Uniform // Uniform
BufferBindingTypeStorage BufferBindingType = C.WGPUBufferBindingType_Storage // Storage
BufferBindingTypeReadOnlyStorage BufferBindingType = C.WGPUBufferBindingType_ReadOnlyStorage // ReadOnlyStorage
)
const (
SamplerBindingTypeUndefined SamplerBindingType = C.WGPUSamplerBindingType_Undefined // Undefined
SamplerBindingTypeFiltering SamplerBindingType = C.WGPUSamplerBindingType_Filtering // Filtering
SamplerBindingTypeNonFiltering SamplerBindingType = C.WGPUSamplerBindingType_NonFiltering // NonFiltering
SamplerBindingTypeComparison SamplerBindingType = C.WGPUSamplerBindingType_Comparison // Comparison
)
const (
TextureSampleTypeUndefined TextureSampleType = C.WGPUTextureSampleType_Undefined // Undefined
TextureSampleTypeFloat TextureSampleType = C.WGPUTextureSampleType_Float // Float
TextureSampleTypeUnfilterableFloat TextureSampleType = C.WGPUTextureSampleType_UnfilterableFloat // UnfilterableFloat
TextureSampleTypeDepth TextureSampleType = C.WGPUTextureSampleType_Depth // Depth
TextureSampleTypeSint TextureSampleType = C.WGPUTextureSampleType_Sint // Sint
TextureSampleTypeUint TextureSampleType = C.WGPUTextureSampleType_Uint // Uint
)
const (
StorageTextureAccessUndefined StorageTextureAccess = C.WGPUStorageTextureAccess_Undefined // Undefined
StorageTextureAccessWriteOnly StorageTextureAccess = C.WGPUStorageTextureAccess_WriteOnly // WriteOnly
StorageTextureAccessReadOnly StorageTextureAccess = C.WGPUStorageTextureAccess_ReadOnly // ReadOnly
StorageTextureAccessReadWrite StorageTextureAccess = C.WGPUStorageTextureAccess_ReadWrite // ReadWrite
)
const (
TextureDimension1D TextureDimension = C.WGPUTextureDimension_1D // 1D
TextureDimension2D TextureDimension = C.WGPUTextureDimension_2D // 2D
TextureDimension3D TextureDimension = C.WGPUTextureDimension_3D // 3D
)
const (
AddressModeRepeat AddressMode = C.WGPUAddressMode_Repeat // Repeat
AddressModeMirrorRepeat AddressMode = C.WGPUAddressMode_MirrorRepeat // MirrorRepeat
AddressModeClampToEdge AddressMode = C.WGPUAddressMode_ClampToEdge // ClampToEdge
)
const (
FilterModeNearest FilterMode = C.WGPUFilterMode_Nearest // Nearest
FilterModeLinear FilterMode = C.WGPUFilterMode_Linear // Linear
)
const (
MipmapFilterModeNearest MipmapFilterMode = C.WGPUMipmapFilterMode_Nearest // Nearest
MipmapFilterModeLinear MipmapFilterMode = C.WGPUMipmapFilterMode_Linear // Linear
)
type Adapter C.struct_WGPUAdapterImpl
type BindGroupLayout C.struct_WGPUBindGroupLayoutImpl
type Instance C.struct_WGPUInstanceImpl
type PipelineLayout C.struct_WGPUPipelineLayoutImpl
type Queue C.struct_WGPUQueueImpl
type RenderPipeline C.struct_WGPURenderPipelineImpl
type Surface C.struct_WGPUSurfaceImpl
type Texture C.struct_WGPUTextureImpl
type TextureView C.struct_WGPUTextureViewImpl
type CommandBuffer C.struct_WGPUCommandBufferImpl
type RenderPassEncoder C.struct_WGPURenderPassEncoderImpl
type QuerySet C.struct_WGPUQuerySetImpl
type Buffer C.struct_WGPUBufferImpl
type CommandEncoder C.struct_WGPUCommandEncoderImpl
type ShaderModule C.struct_WGPUShaderModuleImpl
type Sampler C.struct_WGPUSamplerImpl
type BindGroup C.struct_WGPUBindGroupImpl
type ComputePassEncoder C.struct_WGPUComputePassEncoderImpl
type ComputePipeline C.struct_WGPUComputePipelineImpl
type RenderBundle C.struct_WGPURenderBundleImpl
type RenderBundleEncoder C.struct_WGPURenderBundleEncoderImpl
var (
ErrDeviceLost = errors.New("device lost")
ErrUnknown = errors.New("unknown error")
)
var (
ErrCurrentTextureTimeout = errors.New("timeout")
ErrCurrentTextureOutdated = errors.New("outdated")
ErrCurrentTextureLost = errors.New("lost")
ErrCurrentTextureOutOfMemory = errors.New("out of memory")
)
var (
ErrMapValidationError = errors.New("validation error")
ErrMapDestroyedBeforeCallback = errors.New("destroyed before callback")
ErrMapUnmappedBeforeCallback = errors.New("unmapped before callback")
ErrMapMappingAlreadyPending = errors.New("mapping already pending")
ErrMapOffsetOutOfRange = errors.New("offset out of range")
ErrMapSizeOutOfRange = errors.New("size out of range")
)
var cstrings sync.Map
func getString(s string) *C.char {
if c, ok := cstrings.Load(s); ok {
return c.(*C.char)
}
c := C.CString(s)
cstrings.Store(s, c)
return c
}
type Device struct {
hnd C.WGPUDevice
lost chan DeviceLost
mapMu sync.Mutex
// This may roll over on 32 bit, but if that results in a collision we'll
// have other problems (such as a handle having stuck around for months).
mapCounter uintptr
mapHandles map[uintptr]struct {
pinner runtime.Pinner
ch chan<- error
}
}
type DeviceLost struct {
Device *Device
Reason DeviceLostReason
Message string
}
type SurfaceCapabilities struct {
Formats []TextureFormat
PresentModes []PresentMode
AlphaModes []CompositeAlphaMode
}
func (s *Surface) Capabilities(adapter *Adapter) SurfaceCapabilities {
var caps C.WGPUSurfaceCapabilities
C.wgpuSurfaceGetCapabilities(s.c(), adapter.c(), &caps)
formats := cloneCArray[TextureFormat](caps.formats, caps.formatCount)
presentModes := cloneCArray[PresentMode](caps.presentModes, caps.presentModeCount)
alphaModes := cloneCArray[CompositeAlphaMode](caps.alphaModes, caps.alphaModeCount)
C.wgpuSurfaceCapabilitiesFreeMembers(caps)
return SurfaceCapabilities{
Formats: formats,
PresentModes: presentModes,
AlphaModes: alphaModes,
}
}
type instanceRequestAdapterResult struct {
status requestAdapterStatus
adapter C.WGPUAdapter
msg string
called bool
}
type InstanceDescriptor struct {
Extras *InstanceExtras
}
type AndroidNativeWindow struct {
Window unsafe.Pointer
}
type MetalLayer struct {
Layer unsafe.Pointer
}
type WaylandSurface struct {
Label string
Display unsafe.Pointer
Surface unsafe.Pointer
}
type WindowsHWND struct {
HINSTANCE unsafe.Pointer
HWND unsafe.Pointer
}
type XCBWindow struct {
Connection unsafe.Pointer
Window uint32
}
type XlibWindow struct {
Display unsafe.Pointer
Window uint64
}
var _ NativeSurface = AndroidNativeWindow{}
var _ NativeSurface = MetalLayer{}
var _ NativeSurface = WaylandSurface{}
var _ NativeSurface = WindowsHWND{}
var _ NativeSurface = XCBWindow{}
var _ NativeSurface = XlibWindow{}
func (AndroidNativeWindow) nativeSurface() {}
func (MetalLayer) nativeSurface() {}
func (WaylandSurface) nativeSurface() {}
func (WindowsHWND) nativeSurface() {}
func (XCBWindow) nativeSurface() {}
func (XlibWindow) nativeSurface() {}
// CloneCArray copies the elements of a dynamic C array (i.e. a pointer + a
// number of elements) into a newly allocated Go slice. It will convert from TC
// to TG in the process, requiring them to have the same size and memory layout.
func cloneCArray[TG, TC any, Int constraints.Integer](ptr *TC, n Int) []TG {
if ptr == nil {
return nil
}
if n == 0 {
return []TG{}
}
if unsafe.Sizeof(*new(TC)) != unsafe.Sizeof(*new(TG)) {
panic("TC and TG have different sizes")
}
out := make([]TG, n)
in := unsafe.Slice(safeish.Cast[*TG](ptr), n)
copy(out, in)
return out
}
func calloc[T any]() *T {
return (*T)(C.calloc(C.size_t(unsafe.Sizeof(*new(T))), 1))
}
func callocn[T any](n int) []T {
ptr := (*T)(C.calloc(C.size_t(unsafe.Sizeof(*new(T))), C.size_t(n)))
return unsafe.Slice(ptr, n)
}
func valloc[T any](v T) *T {
ptr := (*T)(C.malloc(C.size_t(unsafe.Sizeof(*new(T)))))
*ptr = v
return ptr
}
func free[T any](ptr *T) {
C.free(up(ptr))
}
func freen[E any, T ~[]E](ptr T) {
free(unsafe.SliceData(ptr))
}
func maybeCall(fn func()) {
if fn != nil {
fn()
}
}
func cstring(s string) *C.char {
if s == "" {
return nil
}
return C.CString(s)
}
func CreateInstance(desc InstanceDescriptor) *Instance {
cdesc := calloc[C.WGPUInstanceDescriptor]()
defer free(cdesc)
if desc.Extras != nil {
chain := calloc[C.WGPUInstanceExtras]()
defer free(chain)
chain.chain.sType = C.WGPUSType_InstanceExtras
chain.backends = C.WGPUInstanceBackendFlags(desc.Extras.Backends)
chain.flags = C.WGPUInstanceFlags(desc.Extras.Flags)
chain.dx12ShaderCompiler = C.WGPUDx12Compiler(desc.Extras.DX12ShaderCompiler)
chain.gles3MinorVersion = C.WGPUGles3MinorVersion(desc.Extras.GLES3MinorVersion)
chain.dxilPath = cstring(desc.Extras.DXILPath)
chain.dxcPath = cstring(desc.Extras.DXCPath)
defer free(chain.dxilPath)
defer free(chain.dxcPath)
cdesc.nextInChain = &chain.chain
}
hnd := C.wgpuCreateInstance(cdesc)
return safeish.Cast[*Instance](hnd)
}
// One of AndroidNativeWindow, MetalLayer, WaylandSurface, WindowsHWND,
// XCBWindow, or XlibWindow.
type NativeSurface interface {
nativeSurface()
}
type SurfaceDescriptor struct {
Label string
Native NativeSurface
}
func (ins *Instance) CreateSurface(desc SurfaceDescriptor) *Surface {
cdesc := &C.WGPUSurfaceDescriptor{
label: getString(desc.Label),
}
native := desc.Native
switch native := native.(type) {
case AndroidNativeWindow:
chain := calloc[C.WGPUSurfaceDescriptorFromAndroidNativeWindow]()
defer free(chain)
chain.chain.sType = C.WGPUSType_SurfaceDescriptorFromAndroidNativeWindow
chain.window = native.Window
cdesc.nextInChain = &chain.chain
case MetalLayer:
chain := calloc[C.WGPUSurfaceDescriptorFromMetalLayer]()
defer free(chain)
chain.chain.sType = C.WGPUSType_SurfaceDescriptorFromMetalLayer
chain.layer = native.Layer
cdesc.nextInChain = &chain.chain
case WaylandSurface:
chain := calloc[C.WGPUSurfaceDescriptorFromWaylandSurface]()
defer free(chain)
chain.chain.sType = C.WGPUSType_SurfaceDescriptorFromWaylandSurface
chain.display = native.Display
chain.surface = native.Surface
cdesc.nextInChain = &chain.chain
case WindowsHWND:
chain := calloc[C.WGPUSurfaceDescriptorFromWindowsHWND]()
defer free(chain)
chain.chain.sType = C.WGPUSType_SurfaceDescriptorFromWindowsHWND
chain.hinstance = native.HINSTANCE
chain.hwnd = native.HWND
cdesc.nextInChain = &chain.chain
case XCBWindow:
chain := calloc[C.WGPUSurfaceDescriptorFromXcbWindow]()
defer free(chain)
chain.chain.sType = C.WGPUSType_SurfaceDescriptorFromXcbWindow
chain.connection = native.Connection
chain.window = C.uint32_t(native.Window)
cdesc.nextInChain = &chain.chain
case XlibWindow:
chain := calloc[C.WGPUSurfaceDescriptorFromXlibWindow]()
defer free(chain)
chain.chain.sType = C.WGPUSType_SurfaceDescriptorFromXlibWindow
chain.display = native.Display
chain.window = C.uint64_t(native.Window)
cdesc.nextInChain = &chain.chain
case nil:
default:
panic(fmt.Sprintf("unhandled type %T", native))
}
hnd := C.wgpuInstanceCreateSurface(ins.c(), cdesc)
return safeish.Cast[*Surface](hnd)
}
type RequestAdapterOptions struct {
CompatibleSurface *Surface
PowerPreference PowerPreference
ForceFallbackAdapter bool
// wgpu doesn't support BackendType and requires the use of InstanceExtras.Backends
// with InstanceDescriptor instead.
}
func toBool(b bool) C.WGPUBool {
if b {
return 1
} else {
return 0
}
}
func (ins *Instance) RequestAdapter(desc RequestAdapterOptions) (*Adapter, error) {
var ret instanceRequestAdapterResult
cdesc := valloc(C.WGPURequestAdapterOptions{
compatibleSurface: desc.CompatibleSurface.c(),
powerPreference: C.WGPUPowerPreference(desc.PowerPreference),
backendType: C.WGPUBackendType_Undefined,
forceFallbackAdapter: toBool(desc.ForceFallbackAdapter),
})
defer free(cdesc)
C.wgpuInstanceRequestAdapter(ins.c(), cdesc, fp(C.requestAdapterCallback), up(&ret))
if !ret.called {
panic("callback wasn't called")
}
switch ret.status {
case requestAdapterStatusSuccess:
return safeish.Cast[*Adapter](ret.adapter), nil
case requestAdapterStatusUnavailable:
return nil, ErrAdapterUnavailable
default:
return nil, RequestAdapterError{Message: ret.msg}
}
}
//export requestAdapterCallback
func requestAdapterCallback(
status C.WGPURequestAdapterStatus,
adapter C.WGPUAdapter,
msg *C.char,
data up,
) {
ret := (*instanceRequestAdapterResult)(data)
ret.status = requestAdapterStatus(status)
ret.adapter = adapter
ret.msg = C.GoString(msg)
ret.called = true
}
//export requestDeviceCallback
func requestDeviceCallback(
status C.WGPURequestDeviceStatus,
device C.WGPUDevice,
msg *C.char,
data up,
) {
ret := (*adapterRequestDeviceResult)(data)
ret.status = requestDeviceStatus(status)
ret.device = device
ret.msg = C.GoString(msg)
ret.called = true
}
func (adp *Adapter) Features() []FeatureName {
n := C.wgpuAdapterEnumerateFeatures(adp.c(), nil)
if n <= 0 {
return nil
}
names := make([]C.WGPUFeatureName, n)
C.wgpuAdapterEnumerateFeatures(adp.c(), &names[0])
return *(*[]FeatureName)(up(&names))
}
type AdapterProperties struct {
VendorID uint32
VendorName string
Architecture string
DeviceID uint32
Name string
DriverDescription string
AdapterType AdapterType
BackendType BackendType
}
func (adp *Adapter) Properties() AdapterProperties {
var cprops C.struct_WGPUAdapterProperties
C.wgpuAdapterGetProperties(adp.c(), &cprops)
return AdapterProperties{
VendorID: uint32(cprops.vendorID),
VendorName: C.GoString(cprops.vendorName),
Architecture: C.GoString(cprops.architecture),
DeviceID: uint32(cprops.deviceID),
Name: C.GoString(cprops.name),
DriverDescription: C.GoString(cprops.driverDescription),
AdapterType: AdapterType(cprops.adapterType),
BackendType: BackendType(cprops.backendType),
}
}
var DefaultLimits = Limits{
// From https://www.w3.org/TR/webgpu/#limit-default
MaxTextureDimension1D: 8192,
MaxTextureDimension2D: 8192,
MaxTextureDimension3D: 2048,
MaxTextureArrayLayers: 256,
MaxBindGroups: 4,
MaxBindGroupsPlusVertexBuffers: 24,
MaxBindingsPerBindGroup: 1000,
MaxDynamicUniformBuffersPerPipelineLayout: 8,
MaxDynamicStorageBuffersPerPipelineLayout: 4,
MaxSampledTexturesPerShaderStage: 16,
MaxSamplersPerShaderStage: 16,
MaxStorageBuffersPerShaderStage: 8,
MaxStorageTexturesPerShaderStage: 4,
MaxUniformBuffersPerShaderStage: 12,
MaxUniformBufferBindingSize: 65536,
MaxStorageBufferBindingSize: 134217728,
MinUniformBufferOffsetAlignment: 256,
MinStorageBufferOffsetAlignment: 256,
MaxVertexBuffers: 8,
MaxBufferSize: 268435456,
MaxVertexAttributes: 16,
MaxVertexBufferArrayStride: 2048,
MaxInterStageShaderComponents: 64,
MaxInterStageShaderVariables: 16,
MaxColorAttachments: 8,
MaxColorAttachmentBytesPerSample: 32,
MaxComputeWorkgroupStorageSize: 16384,
MaxComputeInvocationsPerWorkgroup: 256,
MaxComputeWorkgroupSizeX: 256,
MaxComputeWorkgroupSizeY: 256,
MaxComputeWorkgroupSizeZ: 64,
MaxComputeWorkgroupsPerDimension: 65535,
}
type Limits struct {
_ structs.HostLayout
MaxTextureDimension1D uint32
MaxTextureDimension2D uint32
MaxTextureDimension3D uint32
MaxTextureArrayLayers uint32
MaxBindGroups uint32
MaxBindGroupsPlusVertexBuffers uint32
MaxBindingsPerBindGroup uint32
MaxDynamicUniformBuffersPerPipelineLayout uint32
MaxDynamicStorageBuffersPerPipelineLayout uint32
MaxSampledTexturesPerShaderStage uint32
MaxSamplersPerShaderStage uint32
MaxStorageBuffersPerShaderStage uint32
MaxStorageTexturesPerShaderStage uint32
MaxUniformBuffersPerShaderStage uint32
MaxUniformBufferBindingSize uint64
MaxStorageBufferBindingSize uint64
MinUniformBufferOffsetAlignment uint32
MinStorageBufferOffsetAlignment uint32
MaxVertexBuffers uint32
MaxBufferSize uint64
MaxVertexAttributes uint32
MaxVertexBufferArrayStride uint32
MaxInterStageShaderComponents uint32
MaxInterStageShaderVariables uint32
MaxColorAttachments uint32
MaxColorAttachmentBytesPerSample uint32
MaxComputeWorkgroupStorageSize uint32
MaxComputeInvocationsPerWorkgroup uint32
MaxComputeWorkgroupSizeX uint32
MaxComputeWorkgroupSizeY uint32
MaxComputeWorkgroupSizeZ uint32
MaxComputeWorkgroupsPerDimension uint32
}
var DefaultNativeLimits = NativeLimits{
MaxPushConstantSize: ^uint32(0),
MaxNonSamplerBindings: ^uint32(0),
}
type NativeLimits struct {
_ structs.HostLayout
MaxPushConstantSize uint32
MaxNonSamplerBindings uint32