forked from ziglibs/zgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zgl.zig
2209 lines (1969 loc) · 74.7 KB
/
zgl.zig
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
const std = @import("std");
const binding = @import("binding.zig");
comptime {
std.testing.refAllDecls(@This());
}
const types = @import("types.zig");
pub usingnamespace types;
pub const ErrorHandling = enum {
/// OpenGL functions will log the error, but will not assert that no error happened
log,
/// Asserts that no errors will happen.
assert,
/// No error checking will be executed. Gotta go fast!
none,
};
const error_handling: ErrorHandling =
std.meta.globalOption("opengl_error_handling", ErrorHandling) orelse
if (std.debug.runtime_safety) .assert else .none;
/// Checks if a OpenGL error happend and may yield it.
/// This function is configurable via `opengl_error_handling` in the root file.
/// In Debug mode, unexpected error codes will be unreachable, in all release modes
/// they will be safely wrapped to `error.UnexpectedError`.
fn checkError() void {
if (error_handling == .none)
return;
var error_code = binding.getError();
if (error_code == binding.NO_ERROR)
return;
while (error_code != binding.NO_ERROR) : (error_code = binding.getError()) {
const name = switch (error_code) {
binding.INVALID_ENUM => "invalid enum",
binding.INVALID_VALUE => "invalid value",
binding.INVALID_OPERATION => "invalid operation",
binding.STACK_OVERFLOW => "stack overflow",
binding.STACK_UNDERFLOW => "stack underflow",
binding.OUT_OF_MEMORY => "out of memory",
binding.INVALID_FRAMEBUFFER_OPERATION => "invalid framebuffer operation",
// binding.INVALID_FRAMEBUFFER_OPERATION_EXT => Error.InvalidFramebufferOperation,
// binding.INVALID_FRAMEBUFFER_OPERATION_OES => Error.InvalidFramebufferOperation,
//binding.TABLE_TOO_LARGE => "Table too large",
// binding.TABLE_TOO_LARGE_EXT => Error.TableTooLarge,
//binding.TEXTURE_TOO_LARGE_EXT => "Texture too large",
else => "unknown error",
};
std.log.scoped(.OpenGL).err("OpenGL failure: {s}\n", .{name});
switch (error_handling) {
.log => {},
.assert => @panic("OpenGL error"),
.none => unreachable,
}
}
}
/// Integer conversion helper.
fn cs2gl(size: usize) types.SizeI {
return @intCast(types.SizeI, size);
}
fn ui2gl(val: usize) types.UInt {
return @intCast(types.UInt, val);
}
fn b2gl(b: bool) types.Boolean {
return if (b)
binding.TRUE
else
binding.FALSE;
}
pub const DebugSource = enum {
api,
window_system,
shader_compiler,
third_party,
application,
other,
};
pub const DebugMessageType = enum {
@"error",
deprecated_behavior,
undefined_behavior,
portability,
performance,
other,
};
pub const DebugSeverity = enum {
high,
medium,
low,
notification,
};
fn DebugMessageCallbackHandler(comptime Context: type) type {
return if (Context == void)
fn (source: DebugSource, msg_type: DebugMessageType, id: usize, severity: DebugSeverity, message: []const u8) void
else
fn (context: Context, source: DebugSource, msg_type: DebugMessageType, id: usize, severity: DebugSeverity, message: []const u8) void;
}
/// Sets the OpenGL debug callback handler in zig style.
/// `context` may be a pointer or `{}`.
pub fn debugMessageCallback(context: anytype, comptime handler: DebugMessageCallbackHandler(@TypeOf(context))) void {
const is_void = (@TypeOf(context) == void);
const Context = @TypeOf(context);
const H = struct {
fn translateSource(source: types.UInt) DebugSource {
return switch (source) {
binding.DEBUG_SOURCE_API => DebugSource.api,
// binding.DEBUG_SOURCE_API_ARB => DebugSource.api,
// binding.DEBUG_SOURCE_API_KHR => DebugSource.api,
binding.DEBUG_SOURCE_WINDOW_SYSTEM => DebugSource.window_system,
// binding.DEBUG_SOURCE_WINDOW_SYSTEM_ARB => DebugSource.window_system,
// binding.DEBUG_SOURCE_WINDOW_SYSTEM_KHR => DebugSource.window_system,
binding.DEBUG_SOURCE_SHADER_COMPILER => DebugSource.shader_compiler,
// binding.DEBUG_SOURCE_SHADER_COMPILER_ARB => DebugSource.shader_compiler,
// binding.DEBUG_SOURCE_SHADER_COMPILER_KHR => DebugSource.shader_compiler,
binding.DEBUG_SOURCE_THIRD_PARTY => DebugSource.third_party,
// binding.DEBUG_SOURCE_THIRD_PARTY_ARB => DebugSource.third_party,
// binding.DEBUG_SOURCE_THIRD_PARTY_KHR => DebugSource.third_party,
binding.DEBUG_SOURCE_APPLICATION => DebugSource.application,
// binding.DEBUG_SOURCE_APPLICATION_ARB => DebugSource.application,
// binding.DEBUG_SOURCE_APPLICATION_KHR => DebugSource.application,
binding.DEBUG_SOURCE_OTHER => DebugSource.other,
// binding.DEBUG_SOURCE_OTHER_ARB => DebugSource.other,
// binding.DEBUG_SOURCE_OTHER_KHR => DebugSource.other,
else => DebugSource.other,
};
}
fn translateMessageType(msg_type: types.UInt) DebugMessageType {
return switch (msg_type) {
binding.DEBUG_TYPE_ERROR => DebugMessageType.@"error",
// binding.DEBUG_TYPE_ERROR_ARB => DebugMessageType.@"error",
// binding.DEBUG_TYPE_ERROR_KHR => DebugMessageType.@"error",
binding.DEBUG_TYPE_DEPRECATED_BEHAVIOR => DebugMessageType.deprecated_behavior,
// binding.DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB => DebugMessageType.deprecated_behavior,
// binding.DEBUG_TYPE_DEPRECATED_BEHAVIOR_KHR => DebugMessageType.deprecated_behavior,
binding.DEBUG_TYPE_UNDEFINED_BEHAVIOR => DebugMessageType.undefined_behavior,
// binding.DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB => DebugMessageType.undefined_behavior,
// binding.DEBUG_TYPE_UNDEFINED_BEHAVIOR_KHR => DebugMessageType.undefined_behavior,
binding.DEBUG_TYPE_PORTABILITY => DebugMessageType.portability,
// binding.DEBUG_TYPE_PORTABILITY_ARB => DebugMessageType.portability,
// binding.DEBUG_TYPE_PORTABILITY_KHR => DebugMessageType.portability,
binding.DEBUG_TYPE_PERFORMANCE => DebugMessageType.performance,
// binding.DEBUG_TYPE_PERFORMANCE_ARB => DebugMessageType.performance,
// binding.DEBUG_TYPE_PERFORMANCE_KHR => DebugMessageType.performance,
binding.DEBUG_TYPE_OTHER => DebugMessageType.other,
// binding.DEBUG_TYPE_OTHER_ARB => DebugMessageType.other,
// binding.DEBUG_TYPE_OTHER_KHR => DebugMessageType.other,
else => DebugMessageType.other,
};
}
fn translateSeverity(sev: types.UInt) DebugSeverity {
return switch (sev) {
binding.DEBUG_SEVERITY_HIGH => DebugSeverity.high,
// binding.DEBUG_SEVERITY_HIGH_AMD => DebugSeverity.high,
// binding.DEBUG_SEVERITY_HIGH_ARB => DebugSeverity.high,
// binding.DEBUG_SEVERITY_HIGH_KHR => DebugSeverity.high,
binding.DEBUG_SEVERITY_MEDIUM => DebugSeverity.medium,
// binding.DEBUG_SEVERITY_MEDIUM_AMD => DebugSeverity.medium,
// binding.DEBUG_SEVERITY_MEDIUM_ARB => DebugSeverity.medium,
// binding.DEBUG_SEVERITY_MEDIUM_KHR => DebugSeverity.medium,
binding.DEBUG_SEVERITY_LOW => DebugSeverity.low,
// binding.DEBUG_SEVERITY_LOW_AMD => DebugSeverity.low,
// binding.DEBUG_SEVERITY_LOW_ARB => DebugSeverity.low,
// binding.DEBUG_SEVERITY_LOW_KHR => DebugSeverity.low,
binding.DEBUG_SEVERITY_NOTIFICATION => DebugSeverity.notification,
// binding.DEBUG_SEVERITY_NOTIFICATION_KHR => DebugSeverity.notification,
else => DebugSeverity.high,
};
}
fn callback(
c_source: types.Enum,
c_msg_type: types.Enum,
id: types.UInt,
c_severity: types.Enum,
length: types.SizeI,
c_message: [*c]const types.Char,
userParam: ?*const anyopaque,
) callconv(.C) void {
const debug_source = translateSource(c_source);
const msg_type = translateMessageType(c_msg_type);
const severity = translateSeverity(c_severity);
const message = c_message[0..@intCast(usize, length)];
if (is_void) {
handler(debug_source, msg_type, id, severity, message);
} else {
handler(@intToPtr(Context, @ptrToInt(userParam)), debug_source, msg_type, id, severity, message);
}
}
};
if (is_void)
binding.debugMessageCallback(H.callback, null)
else
binding.debugMessageCallback(H.callback, @ptrCast(?*const anyopaque, context));
checkError();
}
pub fn clearColor(r: f32, g: f32, b: f32, a: f32) void {
binding.clearColor(r, g, b, a);
checkError();
}
pub fn clearDepth(depth: f32) void {
binding.clearDepth(depth);
checkError();
}
pub fn clear(mask: struct { color: bool = false, depth: bool = false, stencil: bool = false }) void {
binding.clear(@as(types.BitField, if (mask.color) binding.COLOR_BUFFER_BIT else 0) |
@as(types.BitField, if (mask.depth) binding.DEPTH_BUFFER_BIT else 0) |
@as(types.BitField, if (mask.stencil) binding.STENCIL_BUFFER_BIT else 0));
checkError();
}
///////////////////////////////////////////////////////////////////////////////
// Vertex Arrays
pub fn createVertexArrays(items: []types.VertexArray) void {
binding.createVertexArrays(cs2gl(items.len), @ptrCast([*]types.UInt, items.ptr));
checkError();
}
pub fn createVertexArray() types.VertexArray {
var vao: types.VertexArray = undefined;
createVertexArrays(@ptrCast([*]types.VertexArray, &vao)[0..1]);
return vao;
}
pub fn genVertexArrays(items: []types.VertexArray) void {
binding.genVertexArrays(cs2gl(items.len), @ptrCast([*]types.UInt, items.ptr));
checkError();
}
pub fn genVertexArray() types.VertexArray {
var vao: types.VertexArray = undefined;
genVertexArrays(@ptrCast([*]types.VertexArray, &vao)[0..1]);
return vao;
}
pub fn bindVertexArray(vao: types.VertexArray) void {
binding.bindVertexArray(@enumToInt(vao));
checkError();
}
pub fn deleteVertexArrays(items: []const types.VertexArray) void {
binding.deleteVertexArrays(cs2gl(items.len), @ptrCast([*]const types.UInt, items.ptr));
}
pub fn deleteVertexArray(vao: types.VertexArray) void {
deleteVertexArrays(@ptrCast([*]const types.VertexArray, &vao)[0..1]);
}
pub fn enableVertexAttribArray(index: u32) void {
binding.enableVertexAttribArray(index);
checkError();
}
pub fn vertexAttribDivisor(index: u32, divisor: u32) void {
binding.vertexAttribDivisor(index, divisor);
checkError();
}
pub fn disableVertexAttribArray(index: u32) void {
binding.disableVertexAttribArray(index);
checkError();
}
pub fn enableVertexArrayAttrib(vertexArray: types.VertexArray, index: u32) void {
binding.enableVertexArrayAttrib(@enumToInt(vertexArray), index);
checkError();
}
pub fn disableVertexArrayAttrib(vertexArray: types.VertexArray, index: u32) void {
binding.disableVertexArrayAttrib(@enumToInt(vertexArray), index);
checkError();
}
pub const Type = enum(types.Enum) {
byte = binding.BYTE,
short = binding.SHORT,
int = binding.INT,
fixed = binding.FIXED,
float = binding.FLOAT,
half_float = binding.HALF_FLOAT,
double = binding.DOUBLE,
unsigned_byte = binding.UNSIGNED_BYTE,
unsigned_short = binding.UNSIGNED_SHORT,
unsigned_int = binding.UNSIGNED_INT,
int_2_10_10_10_rev = binding.INT_2_10_10_10_REV,
unsigned_int_2_10_10_10_rev = binding.UNSIGNED_INT_2_10_10_10_REV,
unsigned_int_10_f_11_f_11_f_rev = binding.UNSIGNED_INT_10F_11F_11F_REV,
};
pub fn vertexAttribFormat(attribindex: u32, size: u32, attribute_type: Type, normalized: bool, relativeoffset: usize) void {
binding.vertexAttribFormat(
attribindex,
@intCast(types.Int, size),
@enumToInt(attribute_type),
b2gl(normalized),
ui2gl(relativeoffset),
);
checkError();
}
pub fn vertexAttribIFormat(attribindex: u32, size: u32, attribute_type: Type, relativeoffset: usize) void {
binding.vertexAttribIFormat(
attribindex,
@intCast(types.Int, size),
@enumToInt(attribute_type),
ui2gl(relativeoffset),
);
checkError();
}
pub fn vertexAttribLFormat(attribindex: u32, size: u32, attribute_type: Type, relativeoffset: usize) void {
binding.vertexAttribLFormat(
attribindex,
@intCast(types.Int, size),
@enumToInt(attribute_type),
ui2gl(relativeoffset),
);
checkError();
}
/// NOTE: if you use any integer type, it will cast to a floating point, you are probably looking for vertexAttribIPointer()
pub fn vertexAttribPointer(attribindex: u32, size: u32, attribute_type: Type, normalized: bool, stride: usize, relativeoffset: usize) void {
binding.vertexAttribPointer(
attribindex,
@intCast(types.Int, size),
@enumToInt(attribute_type),
b2gl(normalized),
cs2gl(stride),
@intToPtr(*allowzero const anyopaque, relativeoffset),
);
checkError();
}
pub fn vertexAttribIPointer(attribindex: u32, size: u32, attribute_type: Type, stride: usize, relativeoffset: usize) void {
binding.vertexAttribIPointer(
attribindex,
@intCast(types.Int, size),
@enumToInt(attribute_type),
cs2gl(stride),
@intToPtr(*allowzero const anyopaque, relativeoffset),
);
checkError();
}
pub fn vertexArrayAttribFormat(
vertexArray: types.VertexArray,
attribindex: u32,
size: u32,
attribute_type: Type,
normalized: bool,
relativeoffset: usize,
) void {
binding.vertexArrayAttribFormat(
@enumToInt(vertexArray),
attribindex,
@intCast(types.Int, size),
@enumToInt(attribute_type),
b2gl(normalized),
ui2gl(relativeoffset),
);
checkError();
}
pub fn vertexArrayAttribIFormat(vertexArray: types.VertexArray, attribindex: u32, size: u32, attribute_type: Type, relativeoffset: usize) void {
binding.vertexArrayAttribIFormat(
@enumToInt(vertexArray),
attribindex,
@intCast(
types.Int,
size,
),
@enumToInt(attribute_type),
ui2gl(relativeoffset),
);
checkError();
}
pub fn vertexArrayAttribLFormat(vertexArray: types.VertexArray, attribindex: u32, size: u32, attribute_type: Type, relativeoffset: usize) void {
binding.vertexArrayAttribLFormat(
@enumToInt(vertexArray),
attribindex,
@intCast(
types.Int,
size,
),
@enumToInt(attribute_type),
@intCast(types.UInt, relativeoffset),
);
checkError();
}
pub fn vertexAttribBinding(attribindex: u32, bindingindex: u32) void {
binding.vertexAttribBinding(
attribindex,
bindingindex,
);
checkError();
}
pub fn vertexArrayAttribBinding(vertexArray: types.VertexArray, attribindex: u32, bindingindex: u32) void {
binding.vertexArrayAttribBinding(
@enumToInt(vertexArray),
attribindex,
bindingindex,
);
checkError();
}
pub fn bindVertexBuffer(bindingindex: u32, buffer: types.Buffer, offset: usize, stride: usize) void {
binding.bindVertexBuffer(bindingindex, @enumToInt(buffer), cs2gl(offset), cs2gl(stride));
checkError();
}
pub fn vertexArrayVertexBuffer(vertexArray: types.VertexArray, bindingindex: u32, buffer: types.Buffer, offset: usize, stride: usize) void {
binding.vertexArrayVertexBuffer(@enumToInt(vertexArray), bindingindex, @enumToInt(buffer), cs2gl(offset), cs2gl(stride));
checkError();
}
pub fn vertexArrayElementBuffer(vertexArray: types.VertexArray, buffer: types.Buffer) void {
binding.vertexArrayElementBuffer(@enumToInt(vertexArray), @enumToInt(buffer));
checkError();
}
///////////////////////////////////////////////////////////////////////////////
// Buffer
pub const BufferTarget = enum(types.Enum) {
/// Vertex attributes
array_buffer = binding.ARRAY_BUFFER,
/// Atomic counter storage
atomic_counter_buffer = binding.ATOMIC_COUNTER_BUFFER,
/// Buffer copy source
copy_read_buffer = binding.COPY_READ_BUFFER,
/// Buffer copy destination
copy_write_buffer = binding.COPY_WRITE_BUFFER,
/// Indirect compute dispatch commands
dispatch_indirect_buffer = binding.DISPATCH_INDIRECT_BUFFER,
/// Indirect command arguments
draw_indirect_buffer = binding.DRAW_INDIRECT_BUFFER,
/// Vertex array indices
element_array_buffer = binding.ELEMENT_ARRAY_BUFFER,
/// Pixel read target
pixel_pack_buffer = binding.PIXEL_PACK_BUFFER,
/// Texture data source
pixel_unpack_buffer = binding.PIXEL_UNPACK_BUFFER,
/// Query result buffer
query_buffer = binding.QUERY_BUFFER,
/// Read-write storage for shaders
shader_storage_buffer = binding.SHADER_STORAGE_BUFFER,
/// Texture data buffer
texture_buffer = binding.TEXTURE_BUFFER,
/// Transform feedback buffer
transform_feedback_buffer = binding.TRANSFORM_FEEDBACK_BUFFER,
/// Uniform block storage
uniform_buffer = binding.UNIFORM_BUFFER,
};
pub fn createBuffers(items: []types.Buffer) void {
binding.createBuffers(cs2gl(items.len), @ptrCast([*]types.UInt, items.ptr));
checkError();
}
pub fn createBuffer() types.Buffer {
var buf: types.Buffer = undefined;
createBuffers(@ptrCast([*]types.Buffer, &buf)[0..1]);
return buf;
}
pub fn genBuffers(items: []types.Buffer) void {
binding.genBuffers(cs2gl(items.len), @ptrCast([*]types.UInt, items.ptr));
checkError();
}
pub fn genBuffer() types.Buffer {
var buf: types.Buffer = undefined;
genBuffers(@ptrCast([*]types.Buffer, &buf)[0..1]);
return buf;
}
pub fn bindBuffer(buf: types.Buffer, target: BufferTarget) void {
binding.bindBuffer(@enumToInt(target), @enumToInt(buf));
checkError();
}
pub fn deleteBuffers(items: []const types.Buffer) void {
binding.deleteBuffers(cs2gl(items.len), @ptrCast([*]const types.UInt, items.ptr));
}
pub fn deleteBuffer(buf: types.Buffer) void {
deleteBuffers(@ptrCast([*]const types.Buffer, &buf)[0..1]);
}
pub const BufferUsage = enum(types.Enum) {
stream_draw = binding.STREAM_DRAW,
stream_read = binding.STREAM_READ,
stream_copy = binding.STREAM_COPY,
static_draw = binding.STATIC_DRAW,
static_read = binding.STATIC_READ,
static_copy = binding.STATIC_COPY,
dynamic_draw = binding.DYNAMIC_DRAW,
dynamic_read = binding.DYNAMIC_READ,
dynamic_copy = binding.DYNAMIC_COPY,
};
// using align(1) as we are not required to have aligned data here
pub fn namedBufferData(buf: types.Buffer, comptime T: type, items: []align(1) const T, usage: BufferUsage) void {
binding.namedBufferData(
@enumToInt(buf),
cs2gl(@sizeOf(T) * items.len),
items.ptr,
@enumToInt(usage),
);
checkError();
}
pub fn namedBufferUninitialized(buf: types.Buffer, comptime T: type, count: usize, usage: BufferUsage) void {
binding.namedBufferData(
@enumToInt(buf),
cs2gl(@sizeOf(T) * count),
null,
@enumToInt(usage),
);
checkError();
}
pub fn bufferData(target: BufferTarget, comptime T: type, items: []align(1) const T, usage: BufferUsage) void {
binding.bufferData(
@enumToInt(target),
cs2gl(@sizeOf(T) * items.len),
items.ptr,
@enumToInt(usage),
);
checkError();
}
pub fn bufferUninitialized(target: BufferTarget, comptime T: type, count: usize, usage: BufferUsage) void {
binding.bufferData(
@enumToInt(target),
cs2gl(@sizeOf(T) * count),
null,
@enumToInt(usage),
);
checkError();
}
pub fn bufferSubData(target: BufferTarget, offset: usize, comptime T: type, items: []align(1) const T) void {
binding.bufferSubData(@enumToInt(target), cs2gl(offset), cs2gl(@sizeOf(T) * items.len), items.ptr);
checkError();
}
pub const BufferStorageFlags = packed struct {
dynamic_storage: bool = false,
map_read: bool = false,
map_write: bool = false,
map_persistent: bool = false,
map_coherent: bool = false,
client_storage: bool = false,
};
pub fn namedBufferStorage(buf: types.Buffer, comptime T: type, count: usize, items: ?[*]align(1) const T, flags: BufferStorageFlags) void {
var flag_bits: binding.GLbitfield = 0;
if (flags.dynamic_storage) flag_bits |= binding.DYNAMIC_STORAGE_BIT;
if (flags.map_read) flag_bits |= binding.MAP_READ_BIT;
if (flags.map_write) flag_bits |= binding.MAP_WRITE_BIT;
if (flags.map_persistent) flag_bits |= binding.MAP_PERSISTENT_BIT;
if (flags.map_coherent) flag_bits |= binding.MAP_COHERENT_BIT;
if (flags.client_storage) flag_bits |= binding.CLIENT_STORAGE_BIT;
binding.namedBufferStorage(
@enumToInt(buf),
cs2gl(@sizeOf(T) * count),
items,
flag_bits,
);
checkError();
}
pub const BufferMapTarget = enum(types.Enum) {
array_buffer = binding.ARRAY_BUFFER,
atomic_counter_buffer = binding.ATOMIC_COUNTER_BUFFER,
copy_read_buffer = binding.COPY_READ_BUFFER,
copy_write_buffer = binding.COPY_WRITE_BUFFER,
dispatch_indirect_buffer = binding.DISPATCH_INDIRECT_BUFFER,
draw_indirect_buffer = binding.DRAW_INDIRECT_BUFFER,
element_array_buffer = binding.ELEMENT_ARRAY_BUFFER,
pixel_pack_buffer = binding.PIXEL_PACK_BUFFER,
pixel_unpack_buffer = binding.PIXEL_UNPACK_BUFFER,
query_buffer = binding.QUERY_BUFFER,
shader_storage_buffer = binding.SHADER_STORAGE_BUFFER,
texture_buffer = binding.TEXTURE_BUFFER,
transform_feedback_buffer = binding.TRANSFORM_FEEDBACK_BUFFER,
uniform_buffer = binding.UNIFORM_BUFFER,
};
pub const BufferMapAccess = enum(types.Enum) {
read_only = binding.READ_ONLY,
write_only = binding.WRITE_ONLY,
read_write = binding.READ_WRITE,
};
pub fn mapBuffer(
target: BufferMapTarget,
comptime T: type,
access: BufferMapAccess,
) [*]align(1) T {
const ptr = binding.mapBuffer(
@enumToInt(target),
@enumToInt(access),
);
checkError();
return @ptrCast([*]align(1) T, ptr);
}
pub fn unmapBuffer(target: BufferMapTarget) bool {
const ok = binding.unmapBuffer(@enumToInt(target));
checkError();
return ok == binding.TRUE;
}
pub const BufferMapFlags = packed struct {
read: bool = false,
write: bool = false,
persistent: bool = false,
coherent: bool = false,
};
pub fn mapNamedBufferRange(
buf: types.Buffer,
comptime T: type,
offset: usize,
count: usize,
flags: BufferMapFlags,
) []align(1) T {
var flag_bits: binding.GLbitfield = 0;
if (flags.read) flag_bits |= binding.MAP_READ_BIT;
if (flags.write) flag_bits |= binding.MAP_WRITE_BIT;
if (flags.persistent) flag_bits |= binding.MAP_PERSISTENT_BIT;
if (flags.coherent) flag_bits |= binding.MAP_COHERENT_BIT;
const ptr = binding.mapNamedBufferRange(
@enumToInt(buf),
@intCast(binding.GLintptr, offset),
@intCast(binding.GLsizeiptr, @sizeOf(T) * count),
flag_bits,
);
checkError();
const values = @ptrCast([*]align(1) T, ptr);
return values[0..count];
}
pub fn unmapNamedBuffer(buf: types.Buffer) bool {
const ok = binding.unmapNamedBuffer(@enumToInt(buf));
checkError();
return ok != 0;
}
///////////////////////////////////////////////////////////////////////////////
// Shaders
pub const ShaderType = enum(types.Enum) {
compute = binding.COMPUTE_SHADER,
vertex = binding.VERTEX_SHADER,
tess_control = binding.TESS_CONTROL_SHADER,
tess_evaluation = binding.TESS_EVALUATION_SHADER,
geometry = binding.GEOMETRY_SHADER,
fragment = binding.FRAGMENT_SHADER,
};
pub fn createShader(shaderType: ShaderType) types.Shader {
const shader = @intToEnum(types.Shader, binding.createShader(@enumToInt(shaderType)));
if (shader == .invalid) {
checkError();
unreachable;
}
return shader;
}
pub fn deleteShader(shader: types.Shader) void {
binding.deleteShader(@enumToInt(shader));
checkError();
}
pub fn compileShader(shader: types.Shader) void {
binding.compileShader(@enumToInt(shader));
checkError();
}
pub fn shaderSource(shader: types.Shader, comptime N: comptime_int, sources: *const [N][]const u8) void {
var lengths: [N]types.Int = undefined;
for (lengths) |*len, i| {
len.* = @intCast(types.Int, sources[i].len);
}
var ptrs: [N]*const types.Char = undefined;
for (ptrs) |*ptr, i| {
ptr.* = @ptrCast(*const types.Char, sources[i].ptr);
}
binding.shaderSource(@enumToInt(shader), N, &ptrs, &lengths);
checkError();
}
pub const ShaderParameter = enum(types.Enum) {
shader_type = binding.SHADER_TYPE,
delete_status = binding.DELETE_STATUS,
compile_status = binding.COMPILE_STATUS,
info_log_length = binding.INFO_LOG_LENGTH,
shader_source_length = binding.SHADER_SOURCE_LENGTH,
};
pub fn getShader(shader: types.Shader, parameter: ShaderParameter) types.Int {
var value: types.Int = undefined;
binding.getShaderiv(@enumToInt(shader), @enumToInt(parameter), &value);
checkError();
return value;
}
pub fn getShaderInfoLog(shader: types.Shader, allocator: std.mem.Allocator) ![:0]const u8 {
const length = getShader(shader, .info_log_length);
const log = try allocator.allocSentinel(u8, @intCast(usize, length), 0);
errdefer allocator.free(log);
binding.getShaderInfoLog(@enumToInt(shader), cs2gl(log.len), null, log.ptr);
checkError();
return log;
}
///////////////////////////////////////////////////////////////////////////////
// Program
pub fn createProgram() types.Program {
const program = @intToEnum(types.Program, binding.createProgram());
if (program == .invalid) {
checkError();
unreachable;
}
return program;
}
pub fn deleteProgram(program: types.Program) void {
binding.deleteProgram(@enumToInt(program));
checkError();
}
pub fn linkProgram(program: types.Program) void {
binding.linkProgram(@enumToInt(program));
checkError();
}
pub fn attachShader(program: types.Program, shader: types.Shader) void {
binding.attachShader(@enumToInt(program), @enumToInt(shader));
checkError();
}
pub fn detachShader(program: types.Program, shader: types.Shader) void {
binding.detachShader(@enumToInt(program), @enumToInt(shader));
checkError();
}
pub fn useProgram(program: types.Program) void {
binding.useProgram(@enumToInt(program));
checkError();
}
pub const ProgramParameter = enum(types.Enum) {
delete_status = binding.DELETE_STATUS,
link_status = binding.LINK_STATUS,
validate_status = binding.VALIDATE_STATUS,
info_log_length = binding.INFO_LOG_LENGTH,
attached_shaders = binding.ATTACHED_SHADERS,
active_atomic_counter_buffers = binding.ACTIVE_ATOMIC_COUNTER_BUFFERS,
active_attributes = binding.ACTIVE_ATTRIBUTES,
active_attribute_max_length = binding.ACTIVE_ATTRIBUTE_MAX_LENGTH,
active_uniforms = binding.ACTIVE_UNIFORMS,
active_uniform_blocks = binding.ACTIVE_UNIFORM_BLOCKS,
active_uniform_block_max_name_length = binding.ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH,
active_uniform_max_length = binding.ACTIVE_UNIFORM_MAX_LENGTH,
compute_work_group_size = binding.COMPUTE_WORK_GROUP_SIZE,
program_binary_length = binding.PROGRAM_BINARY_LENGTH,
transform_feedback_buffer_mode = binding.TRANSFORM_FEEDBACK_BUFFER_MODE,
transform_feedback_varyings = binding.TRANSFORM_FEEDBACK_VARYINGS,
transform_feedback_varying_max_length = binding.TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH,
geometry_vertices_out = binding.GEOMETRY_VERTICES_OUT,
geometry_input_type = binding.GEOMETRY_INPUT_TYPE,
geometry_output_type = binding.GEOMETRY_OUTPUT_TYPE,
};
pub fn getProgram(program: types.Program, parameter: ProgramParameter) types.Int {
var value: types.Int = undefined;
binding.getProgramiv(@enumToInt(program), @enumToInt(parameter), &value);
checkError();
return value;
}
pub fn getProgramInfoLog(program: types.Program, allocator: std.mem.Allocator) ![:0]const u8 {
const length = getProgram(program, .info_log_length);
const log = try allocator.allocSentinel(u8, @intCast(usize, length), 0);
errdefer allocator.free(log);
binding.getProgramInfoLog(@enumToInt(program), cs2gl(log.len), null, log.ptr);
checkError();
return log;
}
pub fn getUniformLocation(program: types.Program, name: [:0]const u8) ?u32 {
const loc = binding.getUniformLocation(@enumToInt(program), name.ptr);
checkError();
if (loc < 0)
return null;
return @intCast(u32, loc);
}
pub fn getAttribLocation(program: types.Program, name: [:0]const u8) ?u32 {
const loc = binding.getAttribLocation(@enumToInt(program), name.ptr);
checkError();
if (loc < 0)
return null;
return @intCast(u32, loc);
}
pub fn bindAttribLocation(program: types.Program, attribute: u32, name: [:0]const u8) void {
binding.bindAttribLocation(@enumToInt(program), attribute, name.ptr);
checkError();
}
///////////////////////////////////////////////////////////////////////////////
// Uniforms
pub fn programUniform1ui(program: types.Program, location: ?u32, value: u32) void {
if (location) |loc| {
binding.programUniform1ui(@enumToInt(program), @intCast(types.Int, loc), value);
checkError();
}
}
pub fn programUniform1i(program: types.Program, location: ?u32, value: i32) void {
if (location) |loc| {
binding.programUniform1i(@enumToInt(program), @intCast(types.Int, loc), value);
checkError();
}
}
pub fn programUniform3ui(program: types.Program, location: ?u32, x: u32, y: u32, z: u32) void {
if (location) |loc| {
binding.programUniform3ui(@enumToInt(program), @intCast(types.Int, loc), x, y, z);
checkError();
}
}
pub fn programUniform3i(program: types.Program, location: ?u32, x: i32, y: i32, z: i32) void {
if (location) |loc| {
binding.programUniform3i(@enumToInt(program), @intCast(types.Int, loc), x, y, z);
checkError();
}
}
pub fn programUniform2i(program: types.Program, location: ?u32, v0: i32, v1: i32) void {
if (location) |loc| {
binding.programUniform2i(@enumToInt(program), @intCast(types.Int, loc), v0, v1);
checkError();
}
}
pub fn programUniform1f(program: types.Program, location: ?u32, value: f32) void {
if (location) |loc| {
binding.programUniform1f(@enumToInt(program), @intCast(types.Int, loc), value);
checkError();
}
}
pub fn programUniform2f(program: types.Program, location: ?u32, x: f32, y: f32) void {
if (location) |loc| {
binding.programUniform2f(@enumToInt(program), @intCast(types.Int, loc), x, y);
checkError();
}
}
pub fn programUniform3f(program: types.Program, location: ?u32, x: f32, y: f32, z: f32) void {
if (location) |loc| {
binding.programUniform3f(@enumToInt(program), @intCast(types.Int, loc), x, y, z);
checkError();
}
}
pub fn programUniform4f(program: types.Program, location: ?u32, x: f32, y: f32, z: f32, w: f32) void {
if (location) |loc| {
binding.programUniform4f(@enumToInt(program), @intCast(types.Int, loc), x, y, z, w);
checkError();
}
}
pub fn programUniformMatrix4(program: types.Program, location: ?u32, transpose: bool, items: []const [4][4]f32) void {
if (location) |loc| {
binding.programUniformMatrix4fv(
@enumToInt(program),
@intCast(types.Int, loc),
cs2gl(items.len),
b2gl(transpose),
@ptrCast(*const f32, items.ptr),
);
checkError();
}
}
pub fn uniform1f(location: ?u32, v0: f32) void {
if (location) |loc| {
binding.uniform1f(@intCast(types.Int, loc), v0);
checkError();
}
}
pub fn uniform2f(location: ?u32, v0: f32, v1: f32) void {
if (location) |loc| {
binding.uniform2f(@intCast(types.Int, loc), v0, v1);
checkError();
}
}
pub fn uniform3f(location: ?u32, v0: f32, v1: f32, v2: f32) void {
if (location) |loc| {
binding.uniform3f(@intCast(types.Int, loc), v0, v1, v2);
checkError();
}
}
pub fn uniform4f(location: ?u32, v0: f32, v1: f32, v2: f32, v3: f32) void {
if (location) |loc| {
binding.uniform4f(@intCast(types.Int, loc), v0, v1, v2, v3);
checkError();
}
}
pub fn uniform1i(location: ?u32, v0: i32) void {
if (location) |loc| {
binding.uniform1i(@intCast(types.Int, loc), v0);
checkError();
}
}
pub fn uniform2i(location: ?u32, v0: i32, v1: i32) void {
if (location) |loc| {
binding.uniform2i(@intCast(types.Int, loc), v0, v1);
checkError();
}
}
pub fn uniform3i(location: ?u32, v0: i32, v1: i32, v2: i32) void {
if (location) |loc| {
binding.uniform3i(@intCast(types.Int, loc), v0, v1, v2);
checkError();
}
}
pub fn uniform4i(location: ?u32, v0: i32, v1: i32, v2: i32, v3: i32) void {
if (location) |loc| {
binding.uniform4i(@intCast(types.Int, loc), v0, v1, v2, v3);
checkError();
}
}
pub fn uniform1ui(location: ?u32, v0: u32) void {
if (location) |loc| {
binding.uniform1ui(@intCast(types.Int, loc), v0);
checkError();
}
}
pub fn uniform2ui(location: ?u32, v0: u32, v1: u32) void {
if (location) |loc| {
binding.uniform2ui(@intCast(types.Int, loc), v0, v1);
checkError();
}
}