-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopengl.odin
More file actions
4029 lines (3410 loc) · 135 KB
/
opengl.odin
File metadata and controls
4029 lines (3410 loc) · 135 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 windows, linux
package gpu
// Core
import "base:runtime"
import "core:log"
import "core:slice"
import "core:strings"
import "core:sync"
import sa "core:container/small_array"
import intr "base:intrinsics"
// Vendor
import gl "vendor:OpenGL"
gl_init :: proc(allocator := context.allocator) {
// Set platform specific procedures
when ODIN_OS == .Windows {
// Global procedures
create_instance_impl = gl_win32_create_instance
// Adapter procedures
adapter_release = gl_win32_adapter_release
// Instance procedures
instance_create_surface = gl_win32_instance_create_surface
instance_request_adapter = gl_win32_instance_request_adapter
instance_release = gl_win32_instance_release
// Surface procedures
surface_get_capabilities = gl_win32_surface_get_capabilities
surface_release = gl_win32_surface_release
} else when ODIN_OS == .Linux {
// Global procedures
create_instance_impl = gl_linux_create_instance
// Adapter procedures
adapter_release = gl_linux_adapter_release
// Instance procedures
instance_create_surface = gl_linux_instance_create_surface
instance_request_adapter = gl_linux_instance_request_adapter
instance_release = gl_linux_instance_release
// Surface procedures
surface_get_capabilities = gl_linux_surface_get_capabilities
surface_release = gl_linux_surface_release
} else {
unreachable()
}
// Adapter procedures
adapter_get_info = gl_adapter_get_info
adapter_info_free_members = gl_adapter_info_free_members
adapter_get_features = gl_adapter_get_features
adapter_get_limits = gl_adapter_get_limits
adapter_has_feature = gl_adapter_has_feature
adapter_request_device = gl_adapter_request_device
adapter_get_texture_format_capabilities = gl_adapter_get_texture_format_capabilities
adapter_get_label = gl_adapter_get_label
adapter_set_label = gl_adapter_set_label
adapter_add_ref = gl_adapter_add_ref
// Bind Group procedures
bind_group_get_label = gl_bind_group_get_label
bind_group_set_label = gl_bind_group_set_label
bind_group_add_ref = gl_bind_group_add_ref
bind_group_release = gl_bind_group_release
// Bind Group Layout procedures
bind_group_layout_get_label = gl_bind_group_layout_get_label
bind_group_layout_set_label = gl_bind_group_layout_set_label
bind_group_layout_add_ref = gl_bind_group_layout_add_ref
bind_group_layout_release = gl_bind_group_layout_release
// Buffer procedures
buffer_destroy = gl_buffer_destroy
buffer_get_map_state = gl_buffer_get_map_state
buffer_get_size = gl_buffer_get_size
buffer_get_usage = gl_buffer_get_usage
buffer_map_async = gl_buffer_map_async
buffer_unmap = gl_buffer_unmap
buffer_get_label = gl_buffer_get_label
buffer_set_label = gl_buffer_set_label
buffer_add_ref = gl_buffer_add_ref
buffer_release = gl_buffer_release
// Command Buffer procedures
command_buffer_get_label = gl_command_buffer_get_label
command_buffer_set_label = gl_command_buffer_set_label
command_buffer_add_ref = gl_command_buffer_add_ref
command_buffer_release = gl_command_buffer_release
// Command Encoder procedures
command_encoder_begin_compute_pass = gl_command_encoder_begin_compute_pass
command_encoder_begin_render_pass = gl_command_encoder_begin_render_pass
command_encoder_clear_buffer = gl_command_encoder_clear_buffer
command_encoder_resolve_query_set = gl_ommand_encoder_resolve_query_set
command_encoder_write_timestamp = gl_command_encoder_write_timestamp
command_encoder_copy_buffer_to_buffer = gl_command_encoder_copy_buffer_to_buffer
command_encoder_copy_buffer_to_texture = gl_command_encoder_copy_buffer_to_texture
command_encoder_copy_texture_to_buffer = gl_command_encoder_copy_texture_to_buffer
command_encoder_copy_texture_to_texture = gl_command_encoder_copy_texture_to_texture
command_encoder_finish = gl_command_encoder_finish
command_encoder_get_label = gl_command_encoder_get_label
command_encoder_set_label = gl_command_encoder_set_label
command_encoder_add_ref = gl_command_encoder_add_ref
command_encoder_release = gl_command_encoder_release
// Compute Pass procedures
compute_pass_dispatch_workgroups = gl_compute_pass_dispatch_workgroups
compute_pass_dispatch_workgroups_indirect = gl_compute_pass_dispatch_workgroups_indirect
compute_pass_end = gl_compute_pass_end
compute_pass_insert_debug_marker = gl_compute_pass_insert_debug_marker
compute_pass_pop_debug_group = gl_compute_pass_pop_debug_group
compute_pass_push_debug_group = gl_compute_pass_push_debug_group
compute_pass_set_bind_group = gl_compute_pass_set_bind_group
compute_pass_set_pipeline = gl_compute_pass_set_pipeline
compute_pass_get_label = gl_compute_pass_get_label
compute_pass_set_label = gl_compute_pass_set_label
compute_pass_add_ref = gl_compute_pass_add_ref
compute_pass_release = gl_compute_pass_release
// Compute Pipeline procedures
compute_pipeline_get_bind_group_layout = gl_compute_pipeline_get_bind_group_layout
compute_pipeline_get_label = gl_compute_pipeline_get_label
compute_pipeline_set_label = gl_compute_pipeline_set_label
compute_pipeline_add_ref = gl_compute_pipeline_add_ref
compute_pipeline_release = gl_compute_pipeline_release
// Device procedures
device_create_bind_group = gl_device_create_bind_group
device_create_bind_group_layout = gl_device_create_bind_group_layout
device_create_buffer = gl_device_create_buffer
device_get_queue = gl_device_get_queue
device_create_texture = gl_device_create_texture
device_create_sampler = gl_device_create_sampler
device_create_command_encoder = gl_device_create_command_encoder
device_create_pipeline_layout = gl_device_create_pipeline_layout
device_create_shader_module = gl_device_create_shader_module
device_create_render_pipeline = gl_device_create_render_pipeline
device_get_features = gl_device_get_features
device_get_limits = gl_device_get_limits
device_get_label = gl_device_get_label
device_set_label = gl_device_set_label
device_add_ref = gl_device_add_ref
device_release = gl_device_release
// Instance procedures
instance_enumerate_adapters = gl_instance_enumerate_adapters
instance_get_label = gl_instance_get_label
instance_set_label = gl_instance_set_label
instance_add_ref = gl_instance_add_ref
// Pipeline Layout procedures
pipeline_layout_get_label = gl_pipeline_layout_get_label
pipeline_layout_set_label = gl_pipeline_layout_set_label
pipeline_layout_add_ref = gl_pipeline_layout_add_ref
pipeline_layout_release = gl_pipeline_layout_release
// Surface procedures
surface_capabilities_free_members = gl_surface_capabilities_free_members
surface_configure = gl_surface_configure
surface_get_current_texture = gl_surface_get_current_texture
surface_present = gl_surface_present
surface_get_label = gl_surface_get_label
surface_set_label = gl_surface_set_label
surface_add_ref = gl_surface_add_ref
// Queue procedures
queue_submit = gl_queue_submit
queue_write_buffer_impl = gl_queue_write_buffer
queue_write_texture = gl_queue_write_texture
queue_get_label = gl_queue_get_label
queue_set_label = gl_queue_set_label
queue_add_ref = gl_queue_add_ref
queue_release = gl_queue_release
// Sampler procedures
sampler_get_label = gl_sampler_get_label
sampler_set_label = gl_sampler_set_label
sampler_add_ref = gl_sampler_add_ref
sampler_release = gl_sampler_release
// Texture procedures
texture_create_view_impl = gl_texture_create_view
texture_get_usage = gl_texture_get_usage
texture_get_dimension = gl_texture_get_dimension
texture_get_size = gl_texture_get_size
texture_get_width = gl_texture_get_width
texture_get_height = gl_texture_get_height
texture_get_format = gl_texture_get_format
texture_get_mip_level_count = gl_texture_get_mip_level_count
texture_get_sample_count = gl_texture_get_sample_count
texture_get_descriptor = gl_texture_get_descriptor
texture_get_label = gl_texture_get_label
texture_set_label = gl_texture_set_label
texture_add_ref = gl_texture_add_ref
texture_release = gl_texture_release
// Texture View procedures
texture_view_get_label = gl_texture_view_get_label
texture_view_set_label = gl_texture_view_set_label
texture_view_add_ref = gl_texture_view_add_ref
texture_view_release = gl_texture_view_release
// Render Pass procedures
render_pass_begin_occlusion_query = gl_render_pass_begin_occlusion_query
render_pass_set_scissor_rect = gl_render_pass_set_scissor_rect
render_pass_set_viewport = gl_render_pass_set_viewport
render_pass_set_stencil_reference = gl_render_pass_set_stencil_reference
render_pass_draw = gl_render_pass_draw
render_pass_draw_indexed = gl_render_pass_draw_indexed
render_pass_draw_indexed_indirect = gl_render_pass_draw_indexed_indirect
render_pass_draw_indirect = gl_render_pass_draw_indirect
render_pass_end_occlusion_query = gl_render_pass_end_occlusion_query
render_pass_execute_bundles = gl_render_pass_execute_bundles
render_pass_insert_debug_marker = gl_render_pass_insert_debug_marker
render_pass_pop_debug_group = gl_render_pass_pop_debug_group
render_pass_push_debug_group = gl_render_pass_push_debug_group
render_pass_set_bind_group = gl_render_pass_set_bind_group
render_pass_set_pipeline = gl_render_pass_set_pipeline
render_pass_set_vertex_buffer = gl_render_pass_set_vertex_buffer
render_pass_set_index_buffer = gl_render_pass_set_index_buffer
render_pass_end = gl_render_pass_end
render_pass_get_label = gl_render_pass_get_label
render_pass_set_label = gl_render_pass_set_label
render_pass_add_ref = gl_render_pass_add_ref
render_pass_release = gl_render_pass_release
// Render Bundle procedures
render_bundle_get_label = gl_render_bundle_get_label
render_bundle_set_label = gl_render_bundle_set_label
render_bundle_add_ref = gl_render_bundle_add_ref
render_bundle_release = gl_render_bundle_release
// Shader Module procedures
shader_module_get_label = gl_shader_module_get_label
shader_module_set_label = gl_shader_module_set_label
shader_module_add_ref = gl_shader_module_add_ref
shader_module_release = gl_shader_module_release
// Render Pipeline procedures
render_pipeline_get_bind_group_layout = gl_render_pipeline_get_bind_group_layout
render_pipeline_get_label = gl_render_pipeline_get_label
render_pipeline_set_label = gl_render_pipeline_set_label
render_pipeline_add_ref = gl_render_pipeline_add_ref
render_pipeline_release = gl_render_pipeline_release
}
// -----------------------------------------------------------------------------
// Global procedures that are not specific to an object
// -----------------------------------------------------------------------------
// Note: Platform specific
// -----------------------------------------------------------------------------
// Adapter procedures
// -----------------------------------------------------------------------------
@(require_results)
gl_adapter_get_features :: proc(
adapter: Adapter,
loc := #caller_location,
) -> (features: Features) {
major, minor := GL_MAJOR_VERSION, GL_MINOR_VERSION
version := major * 10 + minor
// Helper to check if version meets a threshold
supported :: proc(#any_int version, maj, min: u32) -> bool {
return version >= (maj * 10 + min)
}
// Always enabled (emulated or core basics).
features += {
// Emulated with uniforms in GL.
.Push_Constants,
// Always (queryable formats).
.Texture_Adapter_Specific_Format_Features,
// Core in GL 4.4+ (ARB_clear_texture), but emulatable earlier.
.Clear_Texture,
// Core since GL 3.0 (packed depth-stencil).
.Depth32_Float_Stencil8,
// Core since GL 1.5 (glMapBuffer).
.Mappable_Primary_Buffers,
// Core since GL 1.0 (GL_LINE).
.Polygon_Mode_Line,
// Core since GL 1.0 (GL_POINT).
.Polygon_Mode_Point,
// Core since GL 1.3, but full support via ARB_texture_border_clamp
// (core 1.3) or EXT_texture_border_clamp.
.Address_Mode_Clamp_To_Border,
// Core since GL 3.0, but requires ARB_color_buffer_float or
// EXT_color_buffer_float for full filtering.
.Float32_Filterable,
// Core since GL 3.0 (gl_ClipDistance), or EXT_clip_cull_distance.
.Clip_Distances,
// Core since GL 3.0 (e.g., GL_RG16_SNORM).
.Texture_Format_16Bit_Norm,
// Core since GL 3.2 (gl_PrimitiveID), or via OES_geometry_shader/ARB_geometry_shader4.
.Shader_Primitive_Index,
// Core since GL 4.1 (ARB_vertex_attrib_64bit).
.Vertex_Attribute_64Bit,
// Core since GL 3.1 (ARB_uniform_buffer_object).
// .Uniform_Buffer_Binding_Arrays,
}
// Version-gated core features.
if supported(version, 3, 3) {
features += {
// Core since GL 3.3 (ARB_timer_query).
.Timestamp_Query,
// Core since GL 3.3.
.Timestamp_Query_Inside_Encoders,
// Core since GL 3.3.
.Timestamp_Query_Inside_Passes,
// Core since GL 3.3 (ARB_blend_func_extended).
.Dual_Source_Blending,
}
}
if supported(version, 4, 0) {
features += {
// Core since GL 4.0 (ARB_gpu_shader_fp64).
.Shader_F64,
// Core since GL 4.0 (ARB_gpu_shader_int64).
.Shader_Int64,
}
}
if supported(version, 4, 2) {
features += {
// Core since GL 4.2 (ARB_base_instance), requires
// ARB_shader_draw_parameters for full.
.Indirect_First_Instance,
// Core since GL 4.2 (ARB_shader_atomic_counters).
// .Shader_Float32_Atomic,
// Core since GL 4.2 (ARB_shader_image_load_store).
.Texture_Atomic,
// Core since GL 4.2 (layout(early_fragment_tests)).
.Shader_Early_Depth_Test,
// Core since GL 4.2 (ARB_shader_image_load_store).
.Bgra8_Unorm_Storage,
}
}
if supported(version, 4, 3) {
features += {
// Core since GL 4.3 (ARB_shader_storage_buffer_object).
.Buffer_Binding_Array,
// Core since GL 4.3.
.Storage_Resource_Binding_Array,
// Core since GL 4.3 (SSBOs in vertex shaders).
.Vertex_Writable_Storage,
// Core since GL 4.3, or ARB_ES3_compatibility.
.Texture_Compression_ETC2,
}
}
if supported(version, 4, 4) {
features += {
// Core since GL 4.4 (ARB_clear_texture).
.Clear_Texture,
}
}
if supported(version, 4, 5) {
features += {
// Core since 3.3 (ARB_depth_clamp)
// but clip control is ARB_clip_control (core 4.5)
.Depth_Clip_Control,
}
}
if supported(version, 4, 6) {
features += {
// Core since GL 4.6 (ARB_pipeline_statistics_query).
.Pipeline_Statistics_Query,
// Core since GL 4.6 (ARB_indirect_parameters).
.Multi_Draw_Indirect_Count,
}
}
// Extension-based features.
if gl_check_extension_support("GL_ARB_depth_clamp") ||
gl_check_extension_support("GL_EXT_depth_clamp") {
features += {.Depth_Clip_Control} // Enables depth clamping (unclipped depth).
}
if gl_check_extension_support("GL_EXT_texture_compression_s3tc") ||
gl_check_extension_support("GL_ARB_texture_compression_bptc") {
features += {.Texture_Compression_BC, .Texture_Compression_BC_Sliced_3D}
}
if gl_check_extension_support("GL_KHR_texture_compression_astc_ldr") ||
gl_check_extension_support("WEBGL_compressed_texture_astc") ||
gl_check_extension_support("GL_OES_texture_compression_astc") {
features += {.Texture_Compression_ASTC, .Texture_Compression_ASTC_Sliced_3D}
}
if gl_check_extension_support("GL_KHR_texture_compression_astc_hdr") {
features += {.Texture_Compression_ASTC_Hdr}
}
if gl_check_extension_support("GL_ARB_bindless_texture") {
features += {.Texture_Binding_Array}
}
if gl_check_extension_support("GL_ARB_gpu_shader5") || supported(version, 4, 0) {
features += {
.Sampled_Texture_And_Storage_Buffer_Array_Non_Uniform_Indexing,
// .Storage_Texture_Array_Non_Uniform_Indexing,
}
}
if gl_check_extension_support("GL_NV_conservative_raster") ||
gl_check_extension_support("GL_INTEL_conservative_rasterization") {
features += {.Conservative_Rasterization}
}
if gl_check_extension_support("GL_OVR_multiview") ||
gl_check_extension_support("GL_OVR_multiview2") {
features += {.Multiview}
}
if gl_check_extension_support("GL_ARB_shader_ballot") &&
gl_check_extension_support("GL_ARB_shader_group_vote") {
features += {.Subgroup, .Subgroup_Vertex, .Subgroup_Barrier}
}
// if gl_check_extension_support("GL_NV_shader_atomic_int64") {
// features += {.Shader_Int64_Atomic_Min_Max, .Shader_Int64_Atomic_All_Ops}
// }
if gl_check_extension_support("GL_AMD_gpu_shader_half_float") ||
gl_check_extension_support("GL_NV_gpu_shader5") {
features += {.Shader_F16}
}
if gl_check_extension_support("GL_AMD_gpu_shader_int16") ||
gl_check_extension_support("GL_NV_gpu_shader5") {
features += {.Shader_I16}
}
if gl_is_format_renderable(gl.R11F_G11F_B10F) {
features += {.Rg11B10_Ufloat_Renderable}
}
if gl_check_extension_support("GL_ARB_sparse_texture") {
features += {.Partially_Bound_Binding_Array}
}
// if gl_check_extension_support("GL_ARB_get_program_binary") || supported(version, 4, 1) {
// features += {.Pipeline_Cache}
// }
if gl_check_extension_support("GL_ARB_timer_query") {
features += {
.Timestamp_Query,
.Timestamp_Query_Inside_Encoders,
.Timestamp_Query_Inside_Passes,
}
}
if gl_check_extension_support("GL_EXT_blend_func_extended") ||
gl_check_extension_support("GL_ARB_blend_func_extended") {
features += {.Dual_Source_Blending}
}
if gl_check_extension_support("GL_EXT_clip_cull_distance") || supported(version, 3, 0) {
features += {.Clip_Distances}
}
if gl_check_extension_support("GL_ARB_color_buffer_float") ||
gl_check_extension_support("GL_EXT_color_buffer_float") ||
gl_check_extension_support("OES_texture_float_linear") {
features += {.Float32_Filterable}
}
return
}
@(require_results)
gl_adapter_get_info :: proc(
adapter: Adapter,
allocator := context.allocator,
loc: runtime.Source_Code_Location,
) -> (
info: Adapter_Info,
) {
impl := get_impl(GL_Adapter_Impl, adapter, loc)
info.name = strings.clone(string(impl.renderer), allocator)
info.vendor = 0
info.device = 0
info.device_type = .Other
// info.driver = strings.clone(string(impl.vendor), allocator)
info.driver_info = strings.clone(string(impl.version), allocator)
info.backend = .Gl
return
}
gl_adapter_info_free_members :: proc(self: Adapter_Info, allocator := context.allocator) {
context.allocator = allocator
if len(self.name) > 0 do delete(self.name)
// if len(self.driver) > 0 do delete(self.driver)
if len(self.driver_info) > 0 do delete(self.driver_info)
}
@(require_results)
gl_adapter_get_limits :: proc(adapter: Adapter, loc := #caller_location) -> (limits: Limits) {
major, minor := GL_MAJOR_VERSION, GL_MINOR_VERSION
version := u32(major * 10 + minor)
supported :: proc(version, maj, min: u32) -> bool {
return version >= (maj * 10 + min)
}
// Start with default limits
limits = LIMITS_DOWNLEVEL
// Texture Limits
max_texture_size: i32
gl.GetIntegerv(gl.MAX_TEXTURE_SIZE, &max_texture_size)
limits.max_texture_dimension_1d = u32(max_texture_size)
limits.max_texture_dimension_2d = u32(max_texture_size)
max_texture_3d_size: i32
gl.GetIntegerv(gl.MAX_3D_TEXTURE_SIZE, &max_texture_3d_size)
limits.max_texture_dimension_3d = u32(max_texture_3d_size)
max_array_texture_layers: i32
gl.GetIntegerv(gl.MAX_ARRAY_TEXTURE_LAYERS, &max_array_texture_layers)
limits.max_texture_array_layers = u32(max_array_texture_layers)
// Binding Limits
//
// Since we flatten bindings in OpenGL, leave max_bind_groups and
// max_bindings_per_bind_group at default values
max_uniform_buffer_bindings: i32
gl.GetIntegerv(gl.MAX_UNIFORM_BUFFER_BINDINGS, &max_uniform_buffer_bindings)
limits.max_dynamic_uniform_buffers_per_pipeline_layout = u32(max_uniform_buffer_bindings)
max_shader_storage_buffer_bindings: i32 = 0
if supported(version, 4, 3) ||
gl_check_extension_support("GL_ARB_shader_storage_buffer_object") {
gl.GetIntegerv(gl.MAX_SHADER_STORAGE_BUFFER_BINDINGS, &max_shader_storage_buffer_bindings)
limits.max_dynamic_storage_buffers_per_pipeline_layout =
u32(max_shader_storage_buffer_bindings)
}
// Per-Stage Sampler Limits
max_texture_image_units: i32
gl.GetIntegerv(gl.MAX_TEXTURE_IMAGE_UNITS, &max_texture_image_units)
max_vertex_texture_image_units: i32
gl.GetIntegerv(gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS, &max_vertex_texture_image_units)
// Use the minimum across stages
limits.max_sampled_textures_per_shader_stage = u32(min(
max_texture_image_units,
max_vertex_texture_image_units,
))
limits.max_samplers_per_shader_stage = u32(max_texture_image_units)
// Storage Buffer Limits
max_storage_buffer_bindings: i32 = 0
if supported(version, 4, 3) ||
gl_check_extension_support("GL_ARB_shader_storage_buffer_object") {
gl.GetIntegerv(gl.MAX_SHADER_STORAGE_BUFFER_BINDINGS, &max_storage_buffer_bindings)
limits.max_storage_buffers_per_shader_stage = u32(max_storage_buffer_bindings)
// Per-stage storage buffer limits for compatibility
max_fragment_ss_blocks: i32
gl.GetIntegerv(gl.MAX_FRAGMENT_SHADER_STORAGE_BLOCKS, &max_fragment_ss_blocks)
max_vertex_ss_blocks: i32
gl.GetIntegerv(gl.MAX_VERTEX_SHADER_STORAGE_BLOCKS, &max_vertex_ss_blocks)
}
// Storage Texture Limits
if supported(version, 4, 2) || gl_check_extension_support("GL_ARB_shader_image_load_store") {
// Note: OpenGL ES can have zero vertex image uniforms, so use compute as reference
max_compute_image_uniforms: i32
gl.GetIntegerv(gl.MAX_COMPUTE_IMAGE_UNIFORMS, &max_compute_image_uniforms)
limits.max_storage_textures_per_shader_stage = u32(max_compute_image_uniforms)
// Per-stage limits for compatibility
max_fragment_image_uniforms: i32
gl.GetIntegerv(gl.MAX_FRAGMENT_IMAGE_UNIFORMS, &max_fragment_image_uniforms)
max_vertex_image_uniforms: i32
gl.GetIntegerv(gl.MAX_VERTEX_IMAGE_UNIFORMS, &max_vertex_image_uniforms)
}
// Uniform Buffer Limits
limits.max_uniform_buffers_per_shader_stage = u32(max_uniform_buffer_bindings)
max_uniform_block_size: i32
gl.GetIntegerv(gl.MAX_UNIFORM_BLOCK_SIZE, &max_uniform_block_size)
limits.max_uniform_buffer_binding_size = u64(max_uniform_block_size)
max_shader_storage_block_size: i32 = 0
if supported(version, 4, 3) ||
gl_check_extension_support("GL_ARB_shader_storage_buffer_object") {
gl.GetIntegerv(gl.MAX_SHADER_STORAGE_BLOCK_SIZE, &max_shader_storage_block_size)
limits.max_storage_buffer_binding_size = u64(max_shader_storage_block_size)
}
// Alignment Requirements
min_uniform_offset_alignment: i32
gl.GetIntegerv(gl.UNIFORM_BUFFER_OFFSET_ALIGNMENT, &min_uniform_offset_alignment)
limits.min_uniform_buffer_offset_alignment = u32(min_uniform_offset_alignment)
min_storage_offset_alignment: i32 = 256
if supported(version, 4, 3) ||
gl_check_extension_support("GL_ARB_shader_storage_buffer_object") {
gl.GetIntegerv(gl.SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT, &min_storage_offset_alignment)
}
limits.min_storage_buffer_offset_alignment = u32(min_storage_offset_alignment)
// Vertex Limits
max_vertex_attrib_bindings: i32 = 16
if supported(version, 4, 3) || gl_check_extension_support("GL_ARB_vertex_attrib_binding") {
gl.GetIntegerv(gl.MAX_VERTEX_ATTRIB_BINDINGS, &max_vertex_attrib_bindings)
}
limits.max_vertex_buffers = u32(max_vertex_attrib_bindings)
limits.max_buffer_size = MAX_BUFFER_SIZE
max_vertex_attribs: i32
gl.GetIntegerv(gl.MAX_VERTEX_ATTRIBS, &max_vertex_attribs)
limits.max_vertex_attributes = u32(max_vertex_attribs)
max_vertex_attrib_stride: i32 = 2048
if supported(version, 4, 3) || gl_check_extension_support("GL_ARB_vertex_attrib_binding") {
gl.GetIntegerv(gl.MAX_VERTEX_ATTRIB_STRIDE, &max_vertex_attrib_stride)
if max_vertex_attrib_stride == 0 {
max_vertex_attrib_stride = 2048 // Driver fallback
}
}
limits.max_vertex_buffer_array_stride = u32(max_vertex_attrib_stride)
// Inter-Stage Shader Limits
max_varying_vectors: i32
gl.GetIntegerv(gl.MAX_VARYING_VECTORS, &max_varying_vectors)
limits.max_inter_stage_shader_variables = min(
u32(max_varying_vectors * 4), // Convert vectors to components
MAX_INTER_STAGE_SHADER_VARIABLES * 4,
)
// Render Target Limits
max_color_attachments: i32
gl.GetIntegerv(gl.MAX_COLOR_ATTACHMENTS, &max_color_attachments)
max_draw_buffers: i32
gl.GetIntegerv(gl.MAX_DRAW_BUFFERS, &max_draw_buffers)
// Use minimum of both limits
limits.max_color_attachments = u32(min(max_color_attachments, max_draw_buffers))
// Dawn leaves this undefined for GL - we can calculate a conservative value
// Assume RGBA32F (16 bytes) per attachment as maximum
limits.max_color_attachment_bytes_per_sample = limits.max_color_attachments * 16
// Compute Limits
if supported(version, 4, 3) || gl_check_extension_support("GL_ARB_compute_shader") {
max_compute_shared_memory: i32
gl.GetIntegerv(gl.MAX_COMPUTE_SHARED_MEMORY_SIZE, &max_compute_shared_memory)
limits.max_compute_workgroup_storage_size = u32(max_compute_shared_memory)
max_compute_invocations: i32
gl.GetIntegerv(gl.MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &max_compute_invocations)
limits.max_compute_invocations_per_workgroup = u32(max_compute_invocations)
max_wg_size_x: i32
gl.GetIntegeri_v(gl.MAX_COMPUTE_WORK_GROUP_SIZE, 0, &max_wg_size_x)
limits.max_compute_workgroup_size_x = u32(max_wg_size_x)
max_wg_size_y: i32
gl.GetIntegeri_v(gl.MAX_COMPUTE_WORK_GROUP_SIZE, 1, &max_wg_size_y)
limits.max_compute_workgroup_size_y = u32(max_wg_size_y)
max_wg_size_z: i32
gl.GetIntegeri_v(gl.MAX_COMPUTE_WORK_GROUP_SIZE, 2, &max_wg_size_z)
limits.max_compute_workgroup_size_z = u32(max_wg_size_z)
// Get minimum across all dimensions for workgroups per dimension
max_wg_count_x: i32
gl.GetIntegeri_v(gl.MAX_COMPUTE_WORK_GROUP_COUNT, 0, &max_wg_count_x)
max_wg_count_y: i32
gl.GetIntegeri_v(gl.MAX_COMPUTE_WORK_GROUP_COUNT, 1, &max_wg_count_y)
max_wg_count_z: i32
gl.GetIntegeri_v(gl.MAX_COMPUTE_WORK_GROUP_COUNT, 2, &max_wg_count_z)
limits.max_compute_workgroups_per_dimension = u32(min(
max_wg_count_x,
max_wg_count_y,
max_wg_count_z,
))
}
// Unsupported in OpenGL
limits.min_subgroup_size = 0
limits.max_subgroup_size = 0
limits.max_push_constant_size = 256 // Emulated via uniforms
limits.max_non_sampler_bindings = 1000000 // Effectively unlimited
// Mesh/Task shaders - not in OpenGL
limits.max_task_workgroup_total_count = 0
limits.max_task_workgroups_per_dimension = 0
limits.max_mesh_output_layers = 0
limits.max_mesh_multiview_count = 0
// Ray tracing - not in OpenGL
limits.max_blas_primitive_count = 0
limits.max_blas_geometry_count = 0
limits.max_tlas_instance_count = 0
limits.max_acceleration_structures_per_shader_stage = 0
return
}
@(require_results)
gl_adapter_has_feature :: proc(
adapter: Adapter,
features: Features,
loc := #caller_location,
) -> bool {
unimplemented()
}
gl_adapter_request_device :: proc(
adapter: Adapter,
callback_info: Request_Device_Callback_Info,
descriptor: Maybe(Device_Descriptor) = nil,
loc := #caller_location,
) {
impl := get_impl(GL_Adapter_Impl, adapter, loc)
assert(callback_info.callback != nil, "No callback provided for device request", loc)
assert(adapter != nil, "Invalid adapter", loc)
assert(impl.instance != nil, "Invalid instance", loc)
instance_impl := get_impl(GL_Instance_Impl, impl.instance, loc)
invoke_callback :: proc(
callback_info: Request_Device_Callback_Info,
status: Request_Device_Status,
device: Device,
message: string,
) {
callback_info.callback(
status,
device,
message,
callback_info.userdata1,
callback_info.userdata2,
)
}
// Default state
gl.Enable(gl.SCISSOR_TEST)
gl.Enable(gl.PRIMITIVE_RESTART_FIXED_INDEX)
gl.Enable(gl.TEXTURE_CUBE_MAP_SEAMLESS)
if .Debug in instance_impl.flags {
gl.Enable(gl.DEBUG_OUTPUT)
gl.Enable(gl.DEBUG_OUTPUT_SYNCHRONOUS)
gl.DebugMessageCallback(gl_message_callback, instance_impl)
}
device_impl := adapter_new_handle(GL_Device_Impl, adapter, loc)
queue_impl := device_new_handle(GL_Queue_Impl, Device(device_impl), loc)
device_impl.queue = queue_impl
device_impl.backend = instance_impl.backend
device_impl.shader_formats = instance_impl.shader_formats
// Initialize base command allocator
cmd_impl := device_new_handle(GL_Command_Encoder_Impl, Device(device_impl), loc)
gl_adapter_add_ref(adapter, loc)
command_allocator_init(&cmd_impl.cmd_allocator, allocator = impl.allocator, loc = loc)
device_impl.encoder = cmd_impl
cmdbuf_impl := command_encoder_new_handle(GL_Command_Buffer_Impl, Command_Encoder(cmd_impl), loc)
cmd_impl.cmdbuf = cmdbuf_impl
// Check if polygon offset clamp is supported
if GL_MAJOR_VERSION == 4 && GL_MINOR_VERSION >= 6 {
device_impl.polygon_offset_clamp = true
} else {
device_impl.polygon_offset_clamp =
gl_check_extension_support("GL_ARB_polygon_offset_clamp")
}
pool_init(&device_impl.pending_maps, impl.allocator)
invoke_callback(callback_info, .Success, Device(device_impl), "")
}
gl_adapter_get_texture_format_capabilities :: proc(
adapter: Adapter,
format: Texture_Format,
loc := #caller_location,
) -> Texture_Format_Capabilities {
unimplemented()
}
@(require_results)
gl_adapter_get_label :: proc(adapter: Adapter, loc := #caller_location) -> string {
impl := get_impl(GL_Adapter_Impl, adapter, loc)
return string_buffer_get_string(&impl.label)
}
gl_adapter_set_label :: proc(adapter: Adapter, label: string, loc := #caller_location) {
impl := get_impl(GL_Adapter_Impl, adapter, loc)
string_buffer_init(&impl.label, label)
}
gl_adapter_add_ref :: proc(adapter: Adapter, loc := #caller_location) {
impl := get_impl(GL_Adapter_Impl, adapter, loc)
ref_count_add(&impl.ref, loc)
}
// -----------------------------------------------------------------------------
// Bind Group procedures
// -----------------------------------------------------------------------------
GL_Bind_Group_Entry :: struct {
binding: u32,
resource: GL_Binding_Resource,
}
GL_Binding_Resource :: union {
GL_Buffer_Binding,
GL_Sampler_Binding,
GL_Texture_View_Binding,
[]GL_Buffer_Binding,
[]GL_Sampler_Binding,
[]GL_Texture_View_Binding,
}
GL_Buffer_Binding :: struct {
buffer: ^GL_Buffer_Impl,
offset: u64,
size: u64,
}
GL_Sampler_Binding :: struct {
sampler: ^GL_Sampler_Impl,
}
GL_Texture_View_Binding :: struct {
texture_view: ^GL_Texture_View_Impl,
}
GL_Bind_Group_Impl :: struct {
using base: Bind_Group_Base,
entries: []GL_Bind_Group_Entry,
}
@(require_results)
gl_bind_group_get_label :: proc(bind_group: Bind_Group, loc := #caller_location) -> string {
impl := get_impl(GL_Bind_Group_Impl, bind_group, loc)
return string_buffer_get_string(&impl.label)
}
gl_bind_group_set_label :: proc(bind_group: Bind_Group, label: string, loc := #caller_location) {
impl := get_impl(GL_Bind_Group_Impl, bind_group, loc)
string_buffer_init(&impl.label, label)
}
gl_bind_group_add_ref :: proc(bind_group: Bind_Group, loc := #caller_location) {
impl := get_impl(GL_Bind_Group_Impl, bind_group, loc)
ref_count_add(&impl.ref, loc)
}
gl_bind_group_release :: proc(bind_group: Bind_Group, loc := #caller_location) {
impl := get_impl(GL_Bind_Group_Impl, bind_group, loc)
if release := ref_count_sub(&impl.ref, loc); release {
context.allocator = impl.allocator
for &entry in impl.entries {
switch &res in entry.resource {
case GL_Buffer_Binding:
gl_buffer_release(Buffer(res.buffer), loc)
case GL_Sampler_Binding:
gl_sampler_release(Sampler(res.sampler), loc)
case GL_Texture_View_Binding:
gl_texture_view_release(Texture_View(res.texture_view), loc)
case []GL_Buffer_Binding:
for &buffer_entry in res {
gl_buffer_release(Buffer(buffer_entry.buffer), loc)
}
case []GL_Sampler_Binding:
for &sampler_entry in res {
gl_sampler_release(Sampler(sampler_entry.sampler), loc)
}
case []GL_Texture_View_Binding:
for &view_entry in res {
gl_texture_view_release(Texture_View(view_entry.texture_view), loc)
}
}
}
delete(impl.entries)
free(impl)
}
}
// -----------------------------------------------------------------------------
// Bind Group Layout procedures
// -----------------------------------------------------------------------------
GL_Bind_Group_Layout_Impl :: struct {
using base: Bind_Group_Layout_Base,
entries: []GL_Bind_Group_Layout_Entry,
}
GL_Bind_Group_Layout_Entry :: struct {
binding: u32,
visibility: Shader_Stages,
type: GL_Binding_Type,
count: u32,
}
GL_Binding_Type :: union {
GL_Buffer_Binding_Layout,
GL_Sampler_Binding_Layout,
GL_Texture_Binding_Layout,
GL_Storage_Texture_Binding_Layout,
GL_Acceleration_Structure_Binding_Layout,
}
GL_Buffer_Binding_Layout :: struct {
type: Buffer_Binding_Type,
has_dynamic_offset: bool,
min_binding_size: u64,
gl_target: u32, // GL_UNIFORM_BUFFER or GL_SHADER_STORAGE_BUFFER
}
GL_Sampler_Binding_Layout :: struct {
type: Sampler_Binding_Type,
}
GL_Texture_Binding_Layout :: struct {
sample_type: Texture_Sample_Type,
view_dimension: Texture_View_Dimension,
multisampled: bool,
gl_target: u32, // GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP, etc.
}
GL_Storage_Texture_Binding_Layout :: struct {
access: Storage_Texture_Access,
format: Texture_Format,
view_dimension: Texture_View_Dimension,
gl_target: u32, // GL_TEXTURE_2D, etc.
gl_format: u32, // Internal format for glBindImageTexture
}
GL_Acceleration_Structure_Binding_Layout :: struct {
vertex_return: bool,
}
@(require_results)
gl_bind_group_layout_get_label :: proc(
bind_group_layout: Bind_Group_Layout,
loc := #caller_location,
) -> string {
impl := get_impl(GL_Bind_Group_Layout_Impl, bind_group_layout, loc)
return string_buffer_get_string(&impl.label)
}
gl_bind_group_layout_set_label :: proc(
bind_group_layout: Bind_Group_Layout,
label: string,
loc := #caller_location,
) {
impl := get_impl(GL_Bind_Group_Layout_Impl, bind_group_layout, loc)
string_buffer_init(&impl.label, label)
}
gl_bind_group_layout_add_ref :: proc(
bind_group_layout: Bind_Group_Layout,
loc := #caller_location,
) {
impl := get_impl(GL_Bind_Group_Layout_Impl, bind_group_layout, loc)
ref_count_add(&impl.ref, loc)
}
gl_bind_group_layout_release :: proc(