-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgpu.odin
More file actions
1963 lines (1642 loc) · 68 KB
/
webgpu.odin
File metadata and controls
1963 lines (1642 loc) · 68 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
#+build js
package gpu
/*
NOTE: All casts/transmutes are guaranteed to be compatible across wgpu bindings.
*/
// Core
import "base:runtime"
import "core:log"
import "core:slice"
import "core:strings"
import sa "core:container/small_array"
// Vendor
import "vendor:wgpu"
wgpu_init :: proc(allocator := context.allocator) {
// Global procedures
create_instance_impl = wgpu_create_instance
// Adapter procedures
adapter_get_info = wgpu_adapter_get_info
adapter_info_free_members = wgpu_adapter_info_free_members
adapter_get_features = wgpu_adapter_get_features
adapter_has_feature = wgpu_adapter_has_feature
adapter_get_limits = wgpu_adapter_get_limits
adapter_request_device = wgpu_adapter_request_device
adapter_get_texture_format_capabilities = wgpu_adapter_get_texture_format_capabilities
adapter_get_label = wgpu_adapter_get_label
adapter_set_label = wgpu_adapter_set_label
adapter_add_ref = wgpu_adapter_add_ref
adapter_release = wgpu_adapter_release
// Bind Group procedures
bind_group_get_label = wgpu_bind_group_get_label
bind_group_set_label = wgpu_bind_group_set_label
bind_group_add_ref = wgpu_bind_group_add_ref
bind_group_release = wgpu_bind_group_release
// Bind Group Layout procedures
bind_group_layout_get_label = wgpu_bind_group_layout_get_label
bind_group_layout_set_label = wgpu_bind_group_layout_set_label
bind_group_layout_add_ref = wgpu_bind_group_layout_add_ref
bind_group_layout_release = wgpu_bind_group_layout_release
// Buffer procedures
buffer_destroy = wgpu_buffer_destroy
buffer_unmap = wgpu_buffer_unmap
buffer_get_map_state = wgpu_buffer_get_map_state
buffer_get_size = wgpu_buffer_get_size
buffer_get_usage = wgpu_buffer_get_usage
buffer_map_async = wgpu_buffer_map_async
buffer_set_label = wgpu_buffer_set_label
buffer_get_label = wgpu_buffer_get_label
buffer_add_ref = wgpu_buffer_add_ref
buffer_release = wgpu_buffer_release
// Command Encoder procedures
command_buffer_get_label = wgpu_command_buffer_get_label
command_buffer_set_label = wgpu_command_buffer_set_label
command_buffer_add_ref = wgpu_command_buffer_add_ref
command_buffer_release = wgpu_command_buffer_release
// Command Encoder procedures
command_encoder_begin_compute_pass = wgpu_command_encoder_begin_compute_pass
command_encoder_begin_render_pass = wgpu_command_encoder_begin_render_pass
command_encoder_copy_texture_to_texture = wgpu_command_encoder_copy_texture_to_texture
command_encoder_finish = wgpu_command_encoder_finish
command_encoder_clear_buffer = wgpu_command_encoder_clear_buffer
command_encoder_resolve_query_set = wgpu_command_encoder_resolve_query_set
command_encoder_write_timestamp = wgpu_command_encoder_write_timestamp
command_encoder_copy_buffer_to_buffer = wgpu_command_encoder_copy_buffer_to_buffer
command_encoder_copy_buffer_to_texture = wgpu_command_encoder_copy_buffer_to_texture
command_encoder_copy_texture_to_buffer = wgpu_command_encoder_copy_texture_to_buffer
command_encoder_get_label = wgpu_command_encoder_get_label
command_encoder_set_label = wgpu_command_encoder_set_label
command_encoder_add_ref = wgpu_command_encoder_add_ref
command_encoder_release = wgpu_command_encoder_release
// Device procedures
device_create_bind_group = wgpu_device_create_bind_group
device_create_bind_group_layout = wgpu_device_create_bind_group_layout
device_create_pipeline_layout = wgpu_device_create_pipeline_layout
device_create_buffer = wgpu_device_create_buffer
device_create_sampler = wgpu_device_create_sampler
device_create_shader_module = wgpu_device_create_shader_module
device_create_texture = wgpu_device_create_texture
device_create_command_encoder = wgpu_device_create_command_encoder
device_create_render_pipeline = wgpu_device_create_render_pipeline
device_get_queue = wgpu_device_get_queue
device_get_features = wgpu_device_get_features
device_get_label = wgpu_device_get_label
device_set_label = wgpu_device_set_label
device_add_ref = wgpu_device_add_ref
device_release = wgpu_device_release
// // Instance procedures
instance_create_surface = wgpu_instance_create_surface
instance_request_adapter = wgpu_instance_request_adapter
instance_get_label = wgpu_instance_get_label
instance_set_label = wgpu_instance_set_label
instance_add_ref = wgpu_instance_add_ref
instance_release = wgpu_instance_release
// Pipeline Layout procedures
pipeline_layout_get_label = wgpu_pipeline_layout_get_label
pipeline_layout_set_label = wgpu_pipeline_layout_set_label
pipeline_layout_add_ref = wgpu_pipeline_layout_add_ref
pipeline_layout_release = wgpu_pipeline_layout_release
// Queue procedures
queue_submit = wgpu_queue_submit
queue_write_buffer_impl = wgpu_queue_write_buffer
queue_write_texture = wgpu_queue_write_texture
queue_get_label = wgpu_queue_get_label
queue_set_label = wgpu_queue_set_label
queue_add_ref = wgpu_queue_add_ref
queue_release = wgpu_queue_release
// Render Pass procedures
render_pass_set_pipeline = wgpu_render_pass_set_pipeline
render_pass_set_bind_group = wgpu_render_pass_set_bind_group
render_pass_set_vertex_buffer = wgpu_render_pass_set_vertex_buffer
render_pass_set_index_buffer = wgpu_render_pass_set_index_buffer
render_pass_set_stencil_reference = wgpu_render_pass_set_stencil_reference
render_pass_draw = wgpu_render_pass_draw
render_pass_draw_indexed = wgpu_render_pass_draw_indexed
render_pass_set_scissor_rect = wgpu_render_pass_set_scissor_rect
render_pass_set_viewport = wgpu_render_pass_set_viewport
render_pass_end = wgpu_render_pass_end
render_pass_get_label = wgpu_render_pass_get_label
render_pass_set_label = wgpu_render_pass_set_label
render_pass_add_ref = wgpu_render_pass_add_ref
render_pass_release = wgpu_render_pass_release
// Render Pipeline procedures
render_pipeline_get_label = wgpu_render_pipeline_get_label
render_pipeline_set_label = wgpu_render_pipeline_set_label
render_pipeline_add_ref = wgpu_render_pipeline_add_ref
render_pipeline_release = wgpu_render_pipeline_release
// Sampler procedures
sampler_get_label = wgpu_sampler_get_label
sampler_set_label = wgpu_sampler_set_label
sampler_add_ref = wgpu_sampler_add_ref
sampler_release = wgpu_sampler_release
// Shader Module procedures
shader_module_get_label = wgpu_shader_module_get_label
shader_module_set_label = wgpu_shader_module_set_label
shader_module_add_ref = wgpu_shader_module_add_ref
shader_module_release = wgpu_shader_module_release
// Surface procedures
surface_configure = wgpu_surface_configure
surface_present = wgpu_surface_present
surface_get_capabilities = wgpu_surface_get_capabilities
surface_get_current_texture = wgpu_surface_get_current_texture
surface_get_label = wgpu_surface_get_label
surface_set_label = wgpu_surface_set_label
surface_add_ref = wgpu_surface_add_ref
surface_release = wgpu_surface_release
// Surface Capabilities procedures
surface_capabilities_free_members = wgpu_surface_capabilities_free_members
// Texture procedures
texture_create_view_impl = wgpu_texture_create_view
texture_get_label = wgpu_texture_get_label
texture_set_label = wgpu_texture_set_label
texture_add_ref = wgpu_texture_add_ref
texture_release = wgpu_texture_release
texture_get_depth_or_array_layers = wgpu_texture_get_depth_or_array_layers
texture_get_dimension = wgpu_texture_get_dimension
texture_get_format = wgpu_texture_get_format
texture_get_height = wgpu_texture_get_height
texture_get_mip_level_count = wgpu_texture_get_mip_level_count
texture_get_sample_count = wgpu_texture_get_sample_count
texture_get_usage = wgpu_texture_get_usage
texture_get_width = wgpu_texture_get_width
texture_get_size = wgpu_texture_get_size
texture_get_descriptor = wgpu_texture_get_descriptor
// Texture view procedures
texture_view_get_label = wgpu_texture_view_get_label
texture_view_set_label = wgpu_texture_view_set_label
texture_view_add_ref = wgpu_texture_view_add_ref
texture_view_release = wgpu_texture_view_release
}
// -----------------------------------------------------------------------------
// Global procedures that are not specific to an object
// -----------------------------------------------------------------------------
@(require_results)
wgpu_create_instance :: proc(
descriptor: Maybe(Instance_Descriptor) = nil,
allocator := context.allocator,
loc := #caller_location,
) -> Instance {
return Instance(wgpu.CreateInstance(nil))
}
@(require_results)
wgpu_instance_create_surface :: proc(
instance: Instance,
descriptor: Surface_Descriptor,
loc := #caller_location,
) -> Surface {
selector := wgpu.SurfaceSourceCanvasHTMLSelector {
chain = {
sType = .SurfaceSourceCanvasHTMLSelector,
},
}
#partial switch &t in descriptor.target {
case Surface_Source_Canvas_HTML_Selector:
if t.selector == "" {
panic("Invalid HTML selector", loc)
}
selector.selector = t.selector
case:
panic("Unsupported surface descriptor target", loc)
}
desc := wgpu.SurfaceDescriptor {
nextInChain = &selector.chain,
label = descriptor.label,
}
surface := wgpu.InstanceCreateSurface(wgpu.Instance(instance), &desc)
return Surface(surface)
}
wgpu_instance_request_adapter :: proc(
instance: Instance,
callback_info: Request_Adapter_Callback_Info,
options: Maybe(Request_Adapter_Options) = nil,
loc := #caller_location,
) {
raw_callback_info := wgpu.RequestAdapterCallbackInfo {
callback = cast(wgpu.RequestAdapterCallback)callback_info.callback,
userdata1 = callback_info.userdata1,
userdata2 = callback_info.userdata2,
}
if opt, opt_ok := options.?; opt_ok {
raw_options := wgpu.RequestAdapterOptions {
// featureLevel = {},
powerPreference = wgpu.PowerPreference(opt.power_preference),
forceFallbackAdapter = b32(opt.force_fallback_adapter),
backendType = .WebGPU,
compatibleSurface = wgpu.Surface(opt.compatible_surface),
}
wgpu.InstanceRequestAdapter(wgpu.Instance(instance), &raw_options, raw_callback_info)
} else {
wgpu.InstanceRequestAdapter(wgpu.Instance(instance), nil, raw_callback_info)
}
}
@(require_results)
wgpu_instance_get_label :: proc(instance: Instance, loc := #caller_location) -> string {
return ""
}
wgpu_instance_set_label :: proc(instance: Instance, label: string, loc := #caller_location) {
}
wgpu_instance_add_ref :: proc(instance: Instance, loc := #caller_location) {
wgpu.InstanceAddRef(wgpu.Instance(instance))
}
wgpu_instance_release :: proc(instance: Instance, loc := #caller_location) {
wgpu.InstanceRelease(wgpu.Instance(instance))
}
// -----------------------------------------------------------------------------
// Pipeline Layout procedures
// -----------------------------------------------------------------------------
@(require_results)
wgpu_pipeline_layout_get_label :: proc(
pipeline_layout: Pipeline_Layout,
loc := #caller_location,
) -> string {
return ""
}
wgpu_pipeline_layout_set_label :: proc(
pipeline_layout: Pipeline_Layout,
label: string,
loc := #caller_location,
) {
wgpu.PipelineLayoutSetLabel(wgpu.PipelineLayout(pipeline_layout), label)
}
wgpu_pipeline_layout_add_ref :: proc(pipeline_layout: Pipeline_Layout, loc := #caller_location) {
wgpu.PipelineLayoutAddRef(wgpu.PipelineLayout(pipeline_layout))
}
wgpu_pipeline_layout_release :: proc(pipeline_layout: Pipeline_Layout, loc := #caller_location) {
wgpu.PipelineLayoutRelease(wgpu.PipelineLayout(pipeline_layout))
}
// -----------------------------------------------------------------------------
// Adapter procedures
// -----------------------------------------------------------------------------
@(require_results)
wgpu_adapter_get_info :: proc(
adapter: Adapter,
allocator := context.allocator,
loc := #caller_location,
) -> (
info: Adapter_Info,
) {
raw_info, status := wgpu.AdapterGetInfo(wgpu.Adapter(adapter))
if status != .Success { return }
info.name = strings.clone(raw_info.device, allocator)
info.vendor = raw_info.vendorID
info.device = raw_info.deviceID
info.device_type = wgpu_conv_from_adapter_type(raw_info.adapterType)
info.driver = strings.clone(raw_info.vendor, allocator)
info.driver_info = strings.clone(raw_info.description, allocator)
info.backend = wgpu_conv_from_backend_type(raw_info.backendType)
return
}
wgpu_adapter_info_free_members :: proc(self: Adapter_Info, allocator := context.allocator) {
context.allocator = allocator
delete(self.name)
delete(self.driver)
delete(self.driver_info)
}
@(require_results)
wgpu_adapter_get_features :: proc(
adapter: Adapter,
loc := #caller_location,
) -> (
features: Features,
) {
supported := wgpu.AdapterGetFeatures(wgpu.Adapter(adapter))
defer wgpu.SupportedFeaturesFreeMembers(supported)
feature_names := supported.features[:supported.featureCount]
return wgpu_conv_from_feature_names(feature_names)
}
@(require_results)
wgpu_adapter_has_feature :: proc(
adapter: Adapter,
features: Features,
loc := #caller_location,
) -> bool {
if features == {} {
return true
}
available := wgpu_adapter_get_features(adapter)
if available == {} {
return false
}
for f in features {
if f not_in available {
return false
}
}
return true
}
@(require_results)
wgpu_adapter_get_limits :: proc(adapter: Adapter, loc := #caller_location) -> (limits: Limits) {
raw_limits, status := wgpu.AdapterGetLimits(wgpu.Adapter(adapter))
if status != .Success {
return
}
return wgpu_conv_from_limits(raw_limits)
}
wgpu_adapter_request_device :: proc(
adapter: Adapter,
callback_info: Request_Device_Callback_Info,
descriptor: Maybe(Device_Descriptor) = nil,
loc := #caller_location,
) {
raw_callback_info := wgpu.RequestDeviceCallbackInfo {
callback = cast(wgpu.RequestDeviceCallback)callback_info.callback,
userdata1 = callback_info.userdata1,
userdata2 = callback_info.userdata2,
}
desc, desc_ok := descriptor.?
if !desc_ok {
wgpu.AdapterRequestDevice(wgpu.Adapter(adapter), nil, raw_callback_info)
return
}
raw_desc: wgpu.DeviceDescriptor
raw_desc.label = desc.label
features: sa.Small_Array(len(wgpu.FeatureName), wgpu.FeatureName)
// Check for unsupported features
if desc.required_features != {} {
for f in desc.required_features {
to_feature := WGPU_CONV_TO_FEATURE_NAME_LUT[f]
if to_feature != .Undefined {
sa.push_back(&features, WGPU_CONV_TO_FEATURE_NAME_LUT[f])
}
}
raw_desc.requiredFeatureCount = uint(sa.len(features))
raw_desc.requiredFeatures = raw_data(sa.slice(&features))
}
// If no limits is provided, default to the most restrictive limits
limits := desc.required_limits if desc.required_limits != {} else LIMITS_MINIMUM_DEFAULT
raw_limits := wgpu.Limits {
maxTextureDimension1D = limits.max_texture_dimension_1d,
maxTextureDimension2D = limits.max_texture_dimension_2d,
maxTextureDimension3D = limits.max_texture_dimension_3d,
maxTextureArrayLayers = limits.max_texture_array_layers,
maxBindGroups = limits.max_bind_groups,
maxBindGroupsPlusVertexBuffers = limits.max_bind_groups_plus_vertex_buffers,
maxBindingsPerBindGroup = limits.max_bindings_per_bind_group,
maxDynamicUniformBuffersPerPipelineLayout = limits.max_dynamic_uniform_buffers_per_pipeline_layout,
maxDynamicStorageBuffersPerPipelineLayout = limits.max_dynamic_storage_buffers_per_pipeline_layout,
maxSampledTexturesPerShaderStage = limits.max_sampled_textures_per_shader_stage,
maxSamplersPerShaderStage = limits.max_samplers_per_shader_stage,
maxStorageBuffersPerShaderStage = limits.max_storage_buffers_per_shader_stage,
maxStorageTexturesPerShaderStage = limits.max_storage_textures_per_shader_stage,
maxUniformBuffersPerShaderStage = limits.max_uniform_buffers_per_shader_stage,
maxUniformBufferBindingSize = limits.max_uniform_buffer_binding_size,
maxStorageBufferBindingSize = limits.max_storage_buffer_binding_size,
minUniformBufferOffsetAlignment = limits.min_uniform_buffer_offset_alignment,
minStorageBufferOffsetAlignment = limits.min_storage_buffer_offset_alignment,
maxVertexBuffers = limits.max_vertex_buffers,
maxBufferSize = limits.max_buffer_size,
maxVertexAttributes = limits.max_vertex_attributes,
maxVertexBufferArrayStride = limits.max_vertex_buffer_array_stride,
maxInterStageShaderVariables = limits.max_inter_stage_shader_variables,
maxColorAttachments = limits.max_color_attachments,
maxColorAttachmentBytesPerSample = limits.max_color_attachment_bytes_per_sample,
maxComputeWorkgroupStorageSize = limits.max_compute_workgroup_storage_size,
maxComputeInvocationsPerWorkgroup = limits.max_compute_invocations_per_workgroup,
maxComputeWorkgroupSizeX = limits.max_compute_workgroup_size_x,
maxComputeWorkgroupSizeY = limits.max_compute_workgroup_size_y,
maxComputeWorkgroupSizeZ = limits.max_compute_workgroup_size_z,
maxComputeWorkgroupsPerDimension = limits.max_compute_workgroups_per_dimension,
}
raw_desc.requiredLimits = &raw_limits
raw_desc.deviceLostCallbackInfo = {
callback = cast(wgpu.DeviceLostCallback)desc.device_lost_callback_info.callback,
userdata1 = desc.device_lost_callback_info.userdata1,
userdata2 = desc.device_lost_callback_info.userdata2,
}
raw_desc.uncapturedErrorCallbackInfo = {
callback = cast(wgpu.UncapturedErrorCallback)desc.uncaptured_error_callback_info.callback,
userdata1 = desc.uncaptured_error_callback_info.userdata1,
userdata2 = desc.uncaptured_error_callback_info.userdata2,
}
wgpu.AdapterRequestDevice(wgpu.Adapter(adapter), &raw_desc, raw_callback_info)
}
@(require_results)
wgpu_adapter_get_texture_format_capabilities :: proc(
adapter: Adapter,
format: Texture_Format,
loc := #caller_location,
) -> Texture_Format_Capabilities {
unimplemented()
}
@(require_results)
wgpu_adapter_get_label :: proc(adapter: Adapter, loc := #caller_location) -> string {
return ""
}
wgpu_adapter_set_label :: proc(adapter: Adapter, label: string, loc := #caller_location) {
}
wgpu_adapter_add_ref :: proc(adapter: Adapter, loc := #caller_location) {
wgpu.AdapterAddRef(wgpu.Adapter(adapter))
}
wgpu_adapter_release :: proc(adapter: Adapter, loc := #caller_location) {
wgpu.AdapterRelease(wgpu.Adapter(adapter))
}
// -----------------------------------------------------------------------------
// Bind Group procedures
// -----------------------------------------------------------------------------
@(require_results)
wgpu_bind_group_get_label :: proc(bind_group: Bind_Group, loc := #caller_location) -> string {
return ""
}
wgpu_bind_group_set_label :: proc(bind_group: Bind_Group, label: string, loc := #caller_location) {
wgpu.BindGroupSetLabel(wgpu.BindGroup(bind_group), label)
}
wgpu_bind_group_add_ref :: proc(bind_group: Bind_Group, loc := #caller_location) {
wgpu.BindGroupAddRef(wgpu.BindGroup(bind_group))
}
wgpu_bind_group_release :: proc(bind_group: Bind_Group, loc := #caller_location) {
wgpu.BindGroupRelease(wgpu.BindGroup(bind_group))
}
// -----------------------------------------------------------------------------
// Bind Group Layout procedures
// -----------------------------------------------------------------------------
@(require_results)
wgpu_bind_group_layout_get_label :: proc(
bind_group_layout: Bind_Group_Layout,
loc := #caller_location,
) -> string {
return ""
}
// @(default_calling_convention="c")
// foreign _ {
// // FIX(bindings): label should be a string (StringView), not a cstring
// wgpuBindGroupLayoutSetLabel :: proc(bindGroupLayout: wgpu.BindGroupLayout, label: string) ---
// }
wgpu_bind_group_layout_set_label :: proc(
bind_group_layout: Bind_Group_Layout,
label: string,
loc := #caller_location,
) {
// wgpuBindGroupLayoutSetLabel(wgpu.BindGroupLayout(bind_group_layout), label)
}
wgpu_bind_group_layout_add_ref :: proc(
bind_group_layout: Bind_Group_Layout,
loc := #caller_location,
) {
wgpu.BindGroupLayoutAddRef(wgpu.BindGroupLayout(bind_group_layout))
}
wgpu_bind_group_layout_release :: proc(
bind_group_layout: Bind_Group_Layout,
loc := #caller_location,
) {
wgpu.BindGroupLayoutRelease(wgpu.BindGroupLayout(bind_group_layout))
}
// -----------------------------------------------------------------------------
// Buffer procedures
// -----------------------------------------------------------------------------
wgpu_buffer_destroy :: proc(buffer: Buffer, loc := #caller_location) {
wgpu.BufferDestroy(wgpu.Buffer(buffer))
}
wgpu_buffer_unmap :: proc(buffer: Buffer, loc := #caller_location) {
wgpu.BufferUnmap(wgpu.Buffer(buffer))
}
wgpu_buffer_get_map_state :: proc(
buffer: Buffer,
loc := #caller_location,
) -> Buffer_Map_State {
return cast(Buffer_Map_State)wgpu.BufferGetMapState(wgpu.Buffer(buffer))
}
wgpu_buffer_get_size :: proc(buffer: Buffer, loc := #caller_location) -> u64 {
return wgpu.BufferGetSize(wgpu.Buffer(buffer))
}
wgpu_buffer_get_usage :: proc(buffer: Buffer, loc := #caller_location) -> (ret: Buffer_Usages) {
return wgpu_conv_from_buffer_usage_flags(wgpu.BufferGetUsage(wgpu.Buffer(buffer)))
}
buffer_get_const_mapped_range :: proc(
buffer: Buffer,
#any_int offset: uint,
#any_int size: uint,
loc := #caller_location,
) -> rawptr {
return wgpu.RawBufferGetConstMappedRange(wgpu.Buffer(buffer), offset, size)
}
buffer_get_mapped_range :: proc(
buffer: Buffer,
#any_int offset: uint,
#any_int size: uint,
loc := #caller_location,
) -> rawptr {
return wgpu.RawBufferGetMappedRange(wgpu.Buffer(buffer), offset, size)
}
wgpu_buffer_map_async :: proc(
buffer: Buffer,
mode: Map_Modes,
offset: uint,
size: uint,
callback_info: Buffer_Map_Callback_Info,
loc := #caller_location,
) -> (
future: Future,
) {
unimplemented()
}
@(require_results)
wgpu_buffer_get_label :: proc(buffer: Buffer, loc := #caller_location) -> string {
return ""
}
wgpu_buffer_set_label :: proc(buffer: Buffer, label: string, loc := #caller_location) {
wgpu.BufferSetLabel(wgpu.Buffer(buffer), label)
}
wgpu_buffer_add_ref :: proc(buffer: Buffer, loc := #caller_location) {
wgpu.BufferAddRef(wgpu.Buffer(buffer))
}
wgpu_buffer_release :: proc(buffer: Buffer, loc := #caller_location) {
wgpu.BufferRelease(wgpu.Buffer(buffer))
}
// -----------------------------------------------------------------------------
// Command Buffer procedures
// -----------------------------------------------------------------------------
@(require_results)
wgpu_command_buffer_get_label :: proc(
command_buffer: Command_Buffer,
loc := #caller_location,
) -> string {
return ""
}
wgpu_command_buffer_set_label :: proc(
command_buffer: Command_Buffer,
label: string,
loc := #caller_location,
) {
wgpu.CommandBufferSetLabel(wgpu.CommandBuffer(command_buffer), label)
}
wgpu_command_buffer_add_ref :: proc(command_buffer: Command_Buffer, loc := #caller_location) {
wgpu.CommandBufferAddRef(wgpu.CommandBuffer(command_buffer))
}
wgpu_command_buffer_release :: proc(command_buffer: Command_Buffer, loc := #caller_location) {
wgpu.CommandBufferRelease(wgpu.CommandBuffer(command_buffer))
}
// -----------------------------------------------------------------------------
// Command Encoder procedures
// -----------------------------------------------------------------------------
wgpu_command_encoder_begin_compute_pass :: proc(
encoder: Command_Encoder,
descriptor: Maybe(Compute_Pass_Descriptor) = nil,
loc := #caller_location,
) -> Compute_Pass {
unimplemented()
}
wgpu_command_encoder_begin_render_pass :: proc(
command_encoder: Command_Encoder,
descriptor: Render_Pass_Descriptor,
loc := #caller_location,
) -> Render_Pass {
desc: wgpu.RenderPassDescriptor
desc.label = descriptor.label
// Color attachments
color_attachments: sa.Small_Array(MAX_COLOR_ATTACHMENTS, wgpu.RenderPassColorAttachment)
if len(descriptor.color_attachments) > 0 {
// Validate color attachment count doesn't exceed maximum
assert(len(descriptor.color_attachments) <= MAX_COLOR_ATTACHMENTS,
"Too many color attachments", loc)
for &attachment in descriptor.color_attachments {
attachment_raw := wgpu.RenderPassColorAttachment {
view = wgpu.TextureView(attachment.view),
resolveTarget = wgpu.TextureView(attachment.resolve_target),
depthSlice = DEPTH_SLICE_UNDEFINED,
loadOp = cast(wgpu.LoadOp)attachment.ops.load,
storeOp = cast(wgpu.StoreOp)attachment.ops.store,
clearValue = wgpu.Color {
attachment.ops.clear_value.r,
attachment.ops.clear_value.g,
attachment.ops.clear_value.b,
attachment.ops.clear_value.a,
},
}
sa.push_back(&color_attachments, attachment_raw)
}
desc.colorAttachmentCount = uint(sa.len(color_attachments))
desc.colorAttachments = raw_data(sa.slice(&color_attachments))
}
// Depth/Stencil attachment
depth_stencil: wgpu.RenderPassDepthStencilAttachment
if descriptor.depth_stencil_attachment != nil {
dsa := descriptor.depth_stencil_attachment
depth_stencil.view = wgpu.TextureView(dsa.view)
// Handle depth operations
depth_stencil.depthLoadOp = cast(wgpu.LoadOp)dsa.depth_ops.load
depth_stencil.depthStoreOp = cast(wgpu.StoreOp)dsa.depth_ops.store
depth_stencil.depthClearValue = dsa.depth_ops.clear_value
depth_stencil.depthReadOnly = b32(dsa.depth_ops.read_only)
// Handle stencil operations
depth_stencil.stencilLoadOp = cast(wgpu.LoadOp)dsa.stencil_ops.load
depth_stencil.stencilStoreOp = cast(wgpu.StoreOp)dsa.stencil_ops.store
depth_stencil.stencilClearValue = dsa.stencil_ops.clear_value
depth_stencil.stencilReadOnly = b32(dsa.stencil_ops.read_only)
desc.depthStencilAttachment = &depth_stencil
}
timestamp_writes: wgpu.RenderPassTimestampWrites
if descriptor.timestamp_writes != nil {
tw := descriptor.timestamp_writes
timestamp_writes.beginningOfPassWriteIndex = tw.beginning_of_pass_write_index
timestamp_writes.endOfPassWriteIndex = tw.end_of_pass_write_index
timestamp_writes.querySet = wgpu.QuerySet(tw.query_set)
desc.timestampWrites = ×tamp_writes
}
if descriptor.occlusion_query_set != nil {
desc.occlusionQuerySet = wgpu.QuerySet(descriptor.occlusion_query_set)
}
return Render_Pass(wgpu.CommandEncoderBeginRenderPass(wgpu.CommandEncoder(command_encoder), &desc))
}
wgpu_command_encoder_clear_buffer :: proc(
encoder: Command_Encoder,
buffer: Buffer,
offset: u64,
size: u64,
loc := #caller_location,
) {
unimplemented()
}
wgpu_command_encoder_resolve_query_set :: proc(
encoder: Command_Encoder,
query_set: Query_Set,
first_query: u32,
query_count: u32,
destination: Buffer,
destination_offset: u64,
loc := #caller_location,
) {
unimplemented()
}
wgpu_command_encoder_write_timestamp :: proc(
encoder: Command_Encoder,
querySet: Query_Set,
queryIndex: u32,
loc := #caller_location,
) {
unimplemented()
}
wgpu_command_encoder_copy_buffer_to_buffer :: proc(
command_encoder: Command_Encoder,
source: Buffer,
source_offset: u64,
destination: Buffer,
destination_offset: u64,
size: u64,
loc := #caller_location,
) {
wgpu.CommandEncoderCopyBufferToBuffer(
wgpu.CommandEncoder(command_encoder),
wgpu.Buffer(source),
source_offset,
wgpu.Buffer(destination),
destination_offset,
size,
)
}
wgpu_command_encoder_copy_buffer_to_texture :: proc(
command_encoder: Command_Encoder,
source: Texel_Copy_Buffer_Info,
destination: Texel_Copy_Texture_Info,
copy_size: Extent_3D,
loc := #caller_location,
) {
raw_source := wgpu.TexelCopyBufferInfo {
layout = transmute(wgpu.TexelCopyBufferLayout)source.layout,
buffer = wgpu.Buffer(source.buffer),
}
raw_dst := wgpu.TexelCopyTextureInfo {
texture = wgpu.Texture(destination.texture),
mipLevel = destination.mip_level,
origin = wgpu_conv_to_origin_3d(destination.origin),
aspect = wgpu_conv_to_texture_aspect(destination.aspect),
}
raw_copy_size := transmute(wgpu.Extent3D)copy_size
wgpu.CommandEncoderCopyBufferToTexture(
wgpu.CommandEncoder(command_encoder),
&raw_source,
&raw_dst,
&raw_copy_size,
)
}
wgpu_command_encoder_copy_texture_to_buffer :: proc(
command_encoder: Command_Encoder,
source: Texel_Copy_Texture_Info,
destination: Texel_Copy_Buffer_Info,
copy_size: Extent_3D,
loc := #caller_location,
) {
raw_source := wgpu.TexelCopyTextureInfo {
texture = wgpu.Texture(source.texture),
mipLevel = source.mip_level,
origin = wgpu_conv_to_origin_3d(source.origin),
aspect = wgpu_conv_to_texture_aspect(source.aspect),
}
raw_dst := wgpu.TexelCopyBufferInfo {
layout = transmute(wgpu.TexelCopyBufferLayout)destination.layout,
buffer = wgpu.Buffer(destination.buffer),
}
raw_copy_size := transmute(wgpu.Extent3D)copy_size
wgpu.CommandEncoderCopyTextureToBuffer(
wgpu.CommandEncoder(command_encoder),
&raw_source,
&raw_dst,
&raw_copy_size,
)
}
wgpu_command_encoder_copy_texture_to_texture :: proc(
command_encoder: Command_Encoder,
source: Texel_Copy_Texture_Info,
destination: Texel_Copy_Texture_Info,
copy_size: Extent_3D,
loc := #caller_location,
) {
raw_source := wgpu.TexelCopyTextureInfo {
texture = wgpu.Texture(source.texture),
mipLevel = source.mip_level,
origin = wgpu_conv_to_origin_3d(source.origin),
aspect = wgpu_conv_to_texture_aspect(source.aspect),
}
raw_dst := wgpu.TexelCopyTextureInfo {
texture = wgpu.Texture(destination.texture),
mipLevel = destination.mip_level,
origin = wgpu_conv_to_origin_3d(destination.origin),
aspect = wgpu_conv_to_texture_aspect(destination.aspect),
}
raw_copy_size := transmute(wgpu.Extent3D)copy_size
wgpu.CommandEncoderCopyTextureToTexture(
wgpu.CommandEncoder(command_encoder),
&raw_source,
&raw_dst,
&raw_copy_size,
)
}
@(require_results)
wgpu_command_encoder_finish :: proc(
command_encoder: Command_Encoder,
loc := #caller_location,
) -> Command_Buffer {
return Command_Buffer(wgpu.CommandEncoderFinish(wgpu.CommandEncoder(command_encoder)))
}
@(require_results)
wgpu_command_encoder_get_label :: proc(
command_encoder: Command_Encoder,
loc := #caller_location,
) -> string {
return ""
}
wgpu_command_encoder_set_label :: proc(
command_encoder: Command_Encoder,
label: string,
loc := #caller_location,
) {
wgpu.CommandEncoderSetLabel(wgpu.CommandEncoder(command_encoder), label)
}
wgpu_command_encoder_add_ref :: proc(command_encoder: Command_Encoder, loc := #caller_location) {
wgpu.CommandEncoderAddRef(wgpu.CommandEncoder(command_encoder))
}
wgpu_command_encoder_release :: proc(command_encoder: Command_Encoder, loc := #caller_location) {
wgpu.CommandEncoderRelease(wgpu.CommandEncoder(command_encoder))
}
// -----------------------------------------------------------------------------
// Device procedures
// -----------------------------------------------------------------------------
wgpu_device_create_bind_group :: proc(
device: Device,
descriptor: Bind_Group_Descriptor,
loc := #caller_location,
) -> Bind_Group {
assert(descriptor.layout != nil, "Invalid bind group layout", loc)
ta := context.temp_allocator
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
entries_total := len(descriptor.entries)
entries: []wgpu.BindGroupEntry
if entries_total > 0 {
entries = make([]wgpu.BindGroupEntry, entries_total, ta)
}
for &entry, i in descriptor.entries {
raw_entry := &entries[i]
raw_entry.binding = entry.binding
switch &res in entry.resource {
case Buffer_Binding:
actual_size := wgpu_buffer_get_size(res.buffer, loc)
assert(res.offset <= actual_size, "buffer offset exceeds buffer size", loc)
if res.size == 0 || res.size == WHOLE_SIZE {
// Use remaining buffer from offset to end
actual_size = actual_size - res.offset
} else {
// Validate the requested region fits within the buffer
assert(res.offset + res.size <= actual_size,
"buffer region (offset + size) exceeds buffer bounds", loc)
actual_size = res.size
}
raw_entry.buffer = wgpu.Buffer(res.buffer)
raw_entry.size = actual_size
raw_entry.offset = res.offset
case Sampler:
raw_entry.sampler = wgpu.Sampler(res)
case Texture_View:
raw_entry.textureView = wgpu.TextureView(res)
case []Buffer_Binding:
// TODO
case []Sampler:
// TODO
case []Texture_View:
// TODO
}
}
desc := wgpu.BindGroupDescriptor {
label = descriptor.label,
layout = wgpu.BindGroupLayout(descriptor.layout),
entryCount = len(entries),
entries = raw_data(entries),
}
return Bind_Group(wgpu.DeviceCreateBindGroup(wgpu.Device(device), &desc))
}