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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
pub mod consts {
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
use std::rc::Rc;
use crate::context::{DataType, ShaderType};
use consts::Gl as InnerGl;
#[derive(Copy, Clone, Debug)]
pub struct AttributeLocation(u32);
#[derive(Copy, Clone, Debug)]
pub struct UniformLocation(u32);
#[derive(Copy, Clone, Debug)]
pub struct Shader(u32);
#[derive(Copy, Clone, Debug)]
pub struct Program(u32);
#[derive(Copy, Clone, Debug)]
pub struct Buffer(u32);
#[derive(Copy, Clone, Debug)]
pub struct Framebuffer(u32);
#[derive(Copy, Clone, Debug)]
pub struct Texture(u32);
#[derive(Copy, Clone, Debug)]
pub struct VertexArrayObject(u32);
pub type Sync = consts::types::GLsync;
pub struct ActiveInfo {
size: u32,
type_: u32,
name: String,
}
impl ActiveInfo {
pub fn new(size: u32, type_: u32, name: String) -> ActiveInfo {
ActiveInfo { size, type_, name }
}
pub fn size(&self) -> i32 {
self.size as i32
}
pub fn type_(&self) -> u32 {
self.type_
}
pub fn name(&self) -> String {
self.name.clone()
}
}
#[derive(Clone)]
pub struct GLContext {
inner: Rc<InnerGl>,
}
impl GLContext {
pub fn load_with<F>(loadfn: F) -> Self
where
for<'r> F: FnMut(&'r str) -> *const consts::types::GLvoid,
{
let gl = Self {
inner: Rc::new(InnerGl::load_with(loadfn)),
};
gl.bind_vertex_array(&gl.create_vertex_array().unwrap());
gl.enable(consts::TEXTURE_CUBE_MAP_SEAMLESS);
gl
}
pub fn finish(&self) {
unsafe {
self.inner.Finish();
}
}
pub fn create_shader(&self, type_: ShaderType) -> Option<Shader> {
let id = unsafe { self.inner.CreateShader(type_.to_const()) };
Some(Shader(id))
}
pub fn compile_shader(&self, source: &str, shader: &Shader) {
let header = "#version 330 core\n";
let s: &str = &[header, source].concat();
use std::ffi::{CStr, CString};
let c_str: &CStr = &CString::new(s).unwrap();
unsafe {
self.inner
.ShaderSource(shader.0, 1, &c_str.as_ptr(), std::ptr::null());
self.inner.CompileShader(shader.0);
}
}
pub fn get_shader_info_log(&self, shader: &Shader) -> Option<String> {
let mut len: consts::types::GLint = 0;
unsafe {
self.inner
.GetShaderiv(shader.0, consts::INFO_LOG_LENGTH, &mut len);
}
if len == 0 {
None
} else {
let error = create_whitespace_cstring_with_len(len as usize);
unsafe {
self.inner.GetShaderInfoLog(
shader.0,
len,
std::ptr::null_mut(),
error.as_ptr() as *mut consts::types::GLchar,
);
}
Some(error.to_string_lossy().into_owned())
}
}
pub fn delete_shader(&self, shader: Option<&Shader>) {
unsafe {
self.inner.DeleteShader(shader.unwrap().0);
}
}
pub fn attach_shader(&self, program: &Program, shader: &Shader) {
unsafe {
self.inner.AttachShader(program.0, shader.0);
}
}
pub fn detach_shader(&self, program: &Program, shader: &Shader) {
unsafe {
self.inner.DetachShader(program.0, shader.0);
}
}
pub fn get_program_parameter(&self, program: &Program, pname: u32) -> u32 {
let mut out = 0;
unsafe {
self.inner.GetProgramiv(program.0, pname, &mut out);
}
out as u32
}
pub fn get_active_attrib(&self, program: &Program, index: u32) -> ActiveInfo {
let mut length = 128;
let mut size = 0;
let mut _type = 0;
let name = create_whitespace_cstring_with_len(length as usize);
unsafe {
self.inner.GetActiveAttrib(
program.0,
index,
length,
&mut length,
&mut size,
&mut _type,
name.as_ptr() as *mut consts::types::GLchar,
);
}
let mut s = name.to_string_lossy().into_owned();
s.truncate(length as usize);
ActiveInfo::new(size as u32, _type as u32, s)
}
pub fn get_active_uniform(&self, program: &Program, index: u32) -> ActiveInfo {
let mut length = 128;
let mut size = 0;
let mut _type = 0;
let name = create_whitespace_cstring_with_len(length as usize);
unsafe {
self.inner.GetActiveUniform(
program.0,
index,
length,
&mut length,
&mut size,
&mut _type,
name.as_ptr() as *mut consts::types::GLchar,
);
}
let mut s = name.to_string_lossy().into_owned();
s.truncate(length as usize);
ActiveInfo::new(size as u32, _type as u32, s)
}
pub fn create_buffer(&self) -> Option<Buffer> {
let mut id: u32 = 0;
unsafe {
self.inner.GenBuffers(1, &mut id);
}
Some(Buffer(id))
}
pub fn delete_buffer(&self, buffer: &Buffer) {
unsafe {
self.inner.DeleteBuffers(1, [buffer.0].as_ptr());
}
}
pub fn bind_buffer_base(&self, target: u32, index: u32, buffer: &Buffer) {
let pname = match target {
consts::ARRAY_BUFFER => consts::ARRAY_BUFFER_BINDING,
consts::ELEMENT_ARRAY_BUFFER => consts::ELEMENT_ARRAY_BUFFER_BINDING,
consts::UNIFORM_BUFFER => consts::UNIFORM_BUFFER_BINDING,
_ => unreachable!(),
};
unsafe {
let mut current = -1;
self.inner.GetIntegerv(pname, &mut current);
if current != 0 {
println!("{}", current);
panic!();
}
self.inner.BindBufferBase(target, index, buffer.0);
}
}
pub fn bind_buffer(&self, target: u32, buffer: &Buffer) {
unsafe {
self.inner.BindBuffer(target, buffer.0);
}
}
pub fn unbind_buffer(&self, target: u32) {
unsafe {
self.inner.BindBuffer(target, 0);
}
}
pub fn get_uniform_block_index(&self, program: &Program, name: &str) -> u32 {
let c_str = std::ffi::CString::new(name).unwrap();
unsafe { self.inner.GetUniformBlockIndex(program.0, c_str.as_ptr()) }
}
pub fn uniform_block_binding(&self, program: &Program, location: u32, index: u32) {
unsafe {
self.inner.UniformBlockBinding(program.0, location, index);
}
}
pub fn buffer_data(&self, target: u32, size_in_bytes: u32, usage: u32) {
unsafe {
self.inner.BufferData(
target,
size_in_bytes as consts::types::GLsizeiptr,
std::ptr::null() as *const consts::types::GLvoid,
usage,
);
}
}
pub fn buffer_data_u8(&self, target: u32, data: &[u8], usage: u32) {
unsafe {
self.inner.BufferData(
target,
(data.len() * std::mem::size_of::<u8>()) as consts::types::GLsizeiptr,
data.as_ptr() as *const consts::types::GLvoid,
usage,
);
}
}
pub fn buffer_data_u16(&self, target: u32, data: &[u16], usage: u32) {
unsafe {
self.inner.BufferData(
target,
(data.len() * std::mem::size_of::<u16>()) as consts::types::GLsizeiptr,
data.as_ptr() as *const consts::types::GLvoid,
usage,
);
}
}
pub fn buffer_data_u32(&self, target: u32, data: &[u32], usage: u32) {
unsafe {
self.inner.BufferData(
target,
(data.len() * std::mem::size_of::<u32>()) as consts::types::GLsizeiptr,
data.as_ptr() as *const consts::types::GLvoid,
usage,
);
}
}
pub fn buffer_data_f32(&self, target: u32, data: &[f32], usage: u32) {
unsafe {
self.inner.BufferData(
target,
(data.len() * std::mem::size_of::<f32>()) as consts::types::GLsizeiptr,
data.as_ptr() as *const consts::types::GLvoid,
usage,
);
}
}
pub fn create_vertex_array(&self) -> Option<VertexArrayObject> {
let mut id: u32 = 0;
unsafe {
self.inner.GenVertexArrays(1, &mut id);
}
Some(VertexArrayObject(id))
}
pub fn bind_vertex_array(&self, array: &VertexArrayObject) {
unsafe {
self.inner.BindVertexArray(array.0);
}
}
pub fn create_program(&self) -> Program {
unsafe { Program(self.inner.CreateProgram()) }
}
pub fn link_program(&self, program: &Program) -> bool {
unsafe {
self.inner.LinkProgram(program.0);
}
let mut success: consts::types::GLint = 1;
unsafe {
self.inner
.GetProgramiv(program.0, consts::LINK_STATUS, &mut success);
}
success == 1
}
pub fn get_program_info_log(&self, program: &Program) -> Option<String> {
let mut len: consts::types::GLint = 0;
unsafe {
self.inner
.GetProgramiv(program.0, consts::INFO_LOG_LENGTH, &mut len);
}
if len == 0 {
None
} else {
let error = create_whitespace_cstring_with_len(len as usize);
unsafe {
self.inner.GetProgramInfoLog(
program.0,
len,
std::ptr::null_mut(),
error.as_ptr() as *mut consts::types::GLchar,
);
}
Some(error.to_string_lossy().into_owned())
}
}
pub fn use_program(&self, program: &Program) {
unsafe {
self.inner.UseProgram(program.0);
}
}
pub fn unuse_program(&self) {
unsafe {
self.inner.UseProgram(0);
}
}
pub fn delete_program(&self, program: &Program) {
unsafe {
self.inner.DeleteProgram(program.0);
}
}
pub fn get_attrib_location(&self, program: &Program, name: &str) -> Option<AttributeLocation> {
let c_str = std::ffi::CString::new(name).unwrap();
let location = unsafe { self.inner.GetAttribLocation(program.0, c_str.as_ptr()) };
if location == -1 {
None
} else {
Some(AttributeLocation(location as _))
}
}
pub fn enable_vertex_attrib_array(&self, location: AttributeLocation) {
unsafe {
self.inner.EnableVertexAttribArray(location.0);
}
}
pub fn disable_vertex_attrib_array(&self, location: AttributeLocation) {
unsafe {
self.inner.DisableVertexAttribArray(location.0);
}
}
pub fn vertex_attrib_pointer(
&self,
location: AttributeLocation,
size: u32,
data_type: DataType,
normalized: bool,
stride: u32,
offset: u32,
) {
unsafe {
self.inner.VertexAttribPointer(
location.0 as consts::types::GLuint,
size as consts::types::GLint,
data_type.to_const(),
normalized as consts::types::GLboolean,
(stride * data_type.byte_size()) as consts::types::GLint,
(offset * data_type.byte_size()) as *const consts::types::GLvoid,
);
}
}
pub fn vertex_attrib_divisor(&self, location: AttributeLocation, divisor: u32) {
unsafe {
self.inner.VertexAttribDivisor(
location.0 as consts::types::GLuint,
divisor as consts::types::GLuint,
);
}
}
pub fn get_uniform_location(&self, program: &Program, name: &str) -> Option<UniformLocation> {
let c_str = std::ffi::CString::new(name).unwrap();
let location = unsafe { self.inner.GetUniformLocation(program.0, c_str.as_ptr()) };
if location == -1 {
None
} else {
Some(UniformLocation(location as _))
}
}
pub fn uniform1i(&self, location: &UniformLocation, data: i32) {
unsafe {
self.inner.Uniform1i(location.0 as i32, data);
}
}
pub fn uniform1f(&self, location: &UniformLocation, data: f32) {
unsafe {
self.inner.Uniform1f(location.0 as i32, data);
}
}
pub fn uniform2fv(&self, location: &UniformLocation, data: &[f32]) {
unsafe {
self.inner.Uniform2fv(location.0 as i32, 1, data.as_ptr());
}
}
pub fn uniform3fv(&self, location: &UniformLocation, data: &[f32]) {
unsafe {
self.inner.Uniform3fv(location.0 as i32, 1, data.as_ptr());
}
}
pub fn uniform4fv(&self, location: &UniformLocation, data: &[f32]) {
unsafe {
self.inner.Uniform4fv(location.0 as i32, 1, data.as_ptr());
}
}
pub fn uniform_matrix2fv(&self, location: &UniformLocation, data: &[f32]) {
unsafe {
self.inner
.UniformMatrix2fv(location.0 as i32, 1, consts::FALSE, data.as_ptr());
}
}
pub fn uniform_matrix3fv(&self, location: &UniformLocation, data: &[f32]) {
unsafe {
self.inner
.UniformMatrix3fv(location.0 as i32, 1, consts::FALSE, data.as_ptr());
}
}
pub fn uniform_matrix4fv(&self, location: &UniformLocation, data: &[f32]) {
unsafe {
self.inner
.UniformMatrix4fv(location.0 as i32, 1, consts::FALSE, data.as_ptr());
}
}
pub fn draw_buffers(&self, draw_buffers: &[u32]) {
unsafe {
self.inner
.DrawBuffers(draw_buffers.len() as i32, draw_buffers.as_ptr());
}
}
pub fn create_framebuffer(&self) -> Option<Framebuffer> {
let mut id: u32 = 0;
unsafe {
self.inner.GenFramebuffers(1, &mut id);
}
Some(Framebuffer(id))
}
pub fn bind_framebuffer(&self, target: u32, framebuffer: Option<&Framebuffer>) {
let id = match framebuffer {
Some(fb) => fb.0,
None => 0,
};
unsafe {
self.inner.BindFramebuffer(target, id);
}
}
pub fn delete_framebuffer(&self, framebuffer: Option<&Framebuffer>) {
let id = match framebuffer {
Some(fb) => &fb.0,
None => &0,
};
unsafe {
self.inner.DeleteFramebuffers(1, id);
}
}
pub fn check_framebuffer_status(&self) -> Result<(), String> {
let status = unsafe { self.inner.CheckFramebufferStatus(consts::FRAMEBUFFER) };
match status {
consts::FRAMEBUFFER_COMPLETE => Ok(()),
consts::FRAMEBUFFER_INCOMPLETE_ATTACHMENT => {
Err("FRAMEBUFFER_INCOMPLETE_ATTACHMENT".to_string())
}
consts::FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER => {
Err("FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER".to_string())
}
consts::FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT => {
Err("FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT".to_string())
}
consts::FRAMEBUFFER_UNSUPPORTED => Err("FRAMEBUFFER_UNSUPPORTED".to_string()),
consts::FRAMEBUFFER_UNDEFINED => Err("FRAMEBUFFER_UNDEFINED".to_string()),
consts::FRAMEBUFFER_INCOMPLETE_READ_BUFFER => {
Err("FRAMEBUFFER_INCOMPLETE_READ_BUFFER".to_string())
}
consts::FRAMEBUFFER_INCOMPLETE_MULTISAMPLE => {
Err("FRAMEBUFFER_INCOMPLETE_MULTISAMPLE".to_string())
}
consts::FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS => {
Err("FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS".to_string())
}
_ => Err("Unknown framebuffer error".to_string()),
}
}
pub fn blit_framebuffer(
&self,
src_x0: u32,
src_y0: u32,
src_x1: u32,
src_y1: u32,
dst_x0: u32,
dst_y0: u32,
dst_x1: u32,
dst_y1: u32,
mask: u32,
filter: u32,
) {
unsafe {
self.inner.BlitFramebuffer(
src_x0 as i32,
src_y0 as i32,
src_x1 as i32,
src_y1 as i32,
dst_x0 as i32,
dst_y0 as i32,
dst_x1 as i32,
dst_y1 as i32,
mask,
filter,
);
}
}
pub fn viewport(&self, x: i32, y: i32, width: i32, height: i32) {
unsafe {
self.inner.Viewport(x, y, width, height);
}
}
pub fn clear_color(&self, red: f32, green: f32, blue: f32, alpha: f32) {
unsafe {
self.inner.ClearColor(red, green, blue, alpha);
}
}
pub fn clear_depth(&self, depth: f32) {
unsafe {
self.inner.ClearDepth(depth as f64);
}
}
pub fn clear(&self, mask: u32) {
unsafe {
self.inner.Clear(mask);
}
}
pub fn enable(&self, cap: u32) {
unsafe {
self.inner.Enable(cap);
}
}
pub fn disable(&self, cap: u32) {
unsafe {
self.inner.Disable(cap);
}
}
pub fn blend_func(&self, sfactor: u32, dfactor: u32) {
unsafe {
self.inner.BlendFunc(sfactor, dfactor);
}
}
pub fn blend_func_separate(&self, src_rgb: u32, dst_rgb: u32, src_alpha: u32, dst_alpha: u32) {
unsafe {
self.inner
.BlendFuncSeparate(src_rgb, dst_rgb, src_alpha, dst_alpha);
}
}
pub fn blend_equation(&self, mode: u32) {
unsafe {
self.inner.BlendEquation(mode);
}
}
pub fn blend_equation_separate(&self, mode_rgb: u32, mode_alpha: u32) {
unsafe {
self.inner.BlendEquationSeparate(mode_rgb, mode_alpha);
}
}
pub fn cull_face(&self, mode: u32) {
unsafe {
self.inner.CullFace(mode);
}
}
pub fn scissor(&self, x: i32, y: i32, width: i32, height: i32) {
unsafe {
self.inner.Scissor(x, y, width, height);
}
}
pub fn depth_func(&self, func: u32) {
unsafe {
self.inner.DepthFunc(func);
}
}
pub fn color_mask(&self, red: bool, green: bool, blue: bool, alpha: bool) {
unsafe {
self.inner.ColorMask(
if red { consts::TRUE } else { consts::FALSE },
if green { consts::TRUE } else { consts::FALSE },
if blue { consts::TRUE } else { consts::FALSE },
if alpha { consts::TRUE } else { consts::FALSE },
);
}
}
pub fn depth_mask(&self, flag: bool) {
unsafe {
if flag {
self.inner.DepthMask(consts::TRUE);
} else {
self.inner.DepthMask(consts::FALSE);
}
}
}
pub fn create_texture(&self) -> Option<Texture> {
let mut id: u32 = 0;
unsafe {
self.inner.GenTextures(1, &mut id);
}
Some(Texture(id))
}
pub fn active_texture(&self, texture: u32) {
unsafe {
self.inner.ActiveTexture(texture);
}
}
pub fn bind_texture(&self, target: u32, texture: &Texture) {
unsafe {
self.inner.BindTexture(target, texture.0);
}
}
pub fn generate_mipmap(&self, target: u32) {
unsafe {
self.inner.GenerateMipmap(target);
}
}
pub fn tex_storage_2d(
&self,
target: u32,
levels: u32,
internalformat: u32,
width: u32,
height: u32,
) {
unsafe {
self.inner.TexStorage2D(
target,
levels as i32,
internalformat,
width as i32,
height as i32,
);
}
}
pub fn tex_storage_3d(
&self,
target: u32,
levels: u32,
internalformat: u32,
width: u32,
height: u32,
depth: u32,
) {
unsafe {
self.inner.TexStorage3D(
target,
levels as i32,
internalformat,
width as i32,
height as i32,
depth as i32,
);
}
}
pub fn tex_image_2d(
&self,
target: u32,
level: u32,
internalformat: u32,
width: u32,
height: u32,
border: u32,
format: u32,
data_type: DataType,
) {
unsafe {
self.inner.TexImage2D(
target,
level as i32,
internalformat as i32,
width as i32,
height as i32,
border as i32,
format,
data_type.to_const(),
std::ptr::null() as *const consts::types::GLvoid,
);
}
}
pub fn tex_image_2d_with_u8_data(
&self,
target: u32,
level: u32,
internalformat: u32,
width: u32,
height: u32,
border: u32,
format: u32,
data_type: DataType,
pixels: &[u8],
) {
unsafe {
self.inner.TexImage2D(
target,
level as i32,
internalformat as i32,
width as i32,
height as i32,
border as i32,
format,
data_type.to_const(),
pixels.as_ptr() as *const consts::types::GLvoid,
);
}
}
pub fn tex_sub_image_2d_with_u8_data(
&self,
target: u32,
level: u32,
x_offset: u32,
y_offset: u32,
width: u32,
height: u32,
format: u32,
data_type: DataType,
pixels: &[u8],
) {
unsafe {
self.inner.TexSubImage2D(
target,
level as i32,
x_offset as i32,
y_offset as i32,
width as i32,
height as i32,
format,
data_type.to_const(),
pixels.as_ptr() as *const consts::types::GLvoid,
);
}
}
pub fn tex_image_2d_with_f32_data(
&self,
target: u32,
level: u32,
internalformat: u32,
width: u32,
height: u32,
border: u32,
format: u32,
data_type: DataType,
pixels: &[f32],
) {
unsafe {
self.inner.TexImage2D(
target,
level as i32,
internalformat as i32,
width as i32,
height as i32,
border as i32,
format,
data_type.to_const(),
pixels.as_ptr() as *const consts::types::GLvoid,
);
}
}
pub fn tex_sub_image_2d_with_f32_data(
&self,
target: u32,
level: u32,
x_offset: u32,
y_offset: u32,
width: u32,
height: u32,
format: u32,
data_type: DataType,
pixels: &[f32],
) {
unsafe {
self.inner.TexSubImage2D(
target,
level as i32,
x_offset as i32,
y_offset as i32,
width as i32,
height as i32,
format,
data_type.to_const(),
pixels.as_ptr() as *const consts::types::GLvoid,
);
}
}
pub fn tex_sub_image_2d_with_u16_data(
&self,
target: u32,
level: u32,
x_offset: u32,
y_offset: u32,
width: u32,
height: u32,
format: u32,
data_type: DataType,
pixels: &[u16],
) {
unsafe {
self.inner.TexSubImage2D(
target,
level as i32,
x_offset as i32,
y_offset as i32,
width as i32,
height as i32,
format,
data_type.to_const(),
pixels.as_ptr() as *const consts::types::GLvoid,
);
}
}
pub fn tex_sub_image_2d_with_u32_data(
&self,
target: u32,
level: u32,
x_offset: u32,
y_offset: u32,
width: u32,
height: u32,
format: u32,
data_type: DataType,
pixels: &[u32],
) {
unsafe {
self.inner.TexSubImage2D(
target,
level as i32,
x_offset as i32,
y_offset as i32,
width as i32,
height as i32,
format,
data_type.to_const(),
pixels.as_ptr() as *const consts::types::GLvoid,
);
}
}
pub fn tex_image_3d(
&self,
target: u32,
level: u32,
internalformat: u32,
width: u32,
height: u32,
depth: u32,
format: u32,
data_type: DataType,
) {
unsafe {
self.inner.TexImage3D(
target,
level as i32,
internalformat as i32,
width as i32,
height as i32,
depth as i32,
0,
format,
data_type.to_const(),
std::ptr::null() as *const consts::types::GLvoid,
);
}
}
pub fn tex_image_3d_with_u16_data(
&self,
target: u32,
level: u32,
internalformat: u32,
width: u32,
height: u32,
depth: u32,
border: u32,
format: u32,
data_type: DataType,
pixels: &[u16],
) {
unsafe {
self.inner.TexImage3D(
target,
level as i32,
internalformat as i32,
width as i32,
height as i32,
depth as i32,
border as i32,
format,
data_type.to_const(),
pixels.as_ptr() as *const consts::types::GLvoid,
);
}
}
pub fn tex_parameteri(&self, target: u32, pname: u32, param: i32) {
unsafe {
self.inner.TexParameteri(target, pname, param);
}
}
pub fn delete_texture(&self, texture: &Texture) {
unsafe {
self.inner.DeleteTextures(1, &texture.0);
}
}
pub fn framebuffer_texture_2d(
&self,
target: u32,
attachment: u32,
textarget: u32,
texture: &Texture,
level: u32,
) {
unsafe {
self.inner
.FramebufferTexture2D(target, attachment, textarget, texture.0, level as i32);
}
}
pub fn framebuffer_texture_layer(
&self,
target: u32,
attachment: u32,
texture: &Texture,
level: u32,
layer: u32,
) {
unsafe {
self.inner.FramebufferTextureLayer(
target,
attachment,
texture.0,
level as i32,
layer as i32,
);
}
}
pub fn draw_arrays(&self, mode: u32, first: u32, count: u32) {
unsafe {
self.inner.DrawArrays(
mode as consts::types::GLenum,
first as i32,
count as i32,
);
}
}
pub fn draw_arrays_instanced(&self, mode: u32, first: u32, count: u32, instance_count: u32) {
unsafe {
self.inner.DrawArraysInstanced(
mode as consts::types::GLenum,
first as i32,
count as consts::types::GLint,
instance_count as consts::types::GLint,
);
}
}
pub fn draw_elements(&self, mode: u32, count: u32, data_type: DataType, offset: u32) {
unsafe {
self.inner.DrawElements(
mode as consts::types::GLenum,
count as consts::types::GLint,
data_type.to_const(),
(offset * data_type.byte_size()) as *const consts::types::GLvoid,
);
}
}
pub fn draw_elements_instanced(
&self,
mode: u32,
count: u32,
data_type: DataType,
offset: u32,
instance_count: u32,
) {
unsafe {
self.inner.DrawElementsInstanced(
mode as consts::types::GLenum,
count as consts::types::GLint,
data_type.to_const(),
(offset * data_type.byte_size()) as *const consts::types::GLvoid,
instance_count as consts::types::GLint,
);
}
}
pub fn read_pixels_with_u8_data(
&self,
x: u32,
y: u32,
width: u32,
height: u32,
format: u32,
data_type: DataType,
dst_data: &mut [u8],
) {
unsafe {
self.inner.ReadPixels(
x as i32,
y as i32,
width as i32,
height as i32,
format,
data_type.to_const(),
dst_data.as_ptr() as *mut consts::types::GLvoid,
)
}
}
pub fn read_pixels_with_u16_data(
&self,
x: u32,
y: u32,
width: u32,
height: u32,
format: u32,
data_type: DataType,
dst_data: &mut [u16],
) {
unsafe {
self.inner.ReadPixels(
x as i32,
y as i32,
width as i32,
height as i32,
format,
data_type.to_const(),
dst_data.as_ptr() as *mut consts::types::GLvoid,
)
}
}
pub fn read_pixels_with_f32_data(
&self,
x: u32,
y: u32,
width: u32,
height: u32,
format: u32,
data_type: DataType,
dst_data: &mut [f32],
) {
unsafe {
self.inner.ReadPixels(
x as i32,
y as i32,
width as i32,
height as i32,
format,
data_type.to_const(),
dst_data.as_ptr() as *mut consts::types::GLvoid,
)
}
}
pub fn read_pixels_with_u32_data(
&self,
x: u32,
y: u32,
width: u32,
height: u32,
format: u32,
data_type: DataType,
dst_data: &mut [u32],
) {
unsafe {
self.inner.ReadPixels(
x as i32,
y as i32,
width as i32,
height as i32,
format,
data_type.to_const(),
dst_data.as_ptr() as *mut consts::types::GLvoid,
)
}
}
pub fn flush(&self) {
unsafe {
self.inner.Flush();
}
}
pub fn fence_sync(&self) -> Sync {
unsafe { self.inner.FenceSync(consts::SYNC_GPU_COMMANDS_COMPLETE, 0) }
}
pub fn client_wait_sync(&self, sync: &Sync, flags: u32, timeout: u32) -> u32 {
unsafe { self.inner.ClientWaitSync(*sync, flags, timeout as u64) }
}
pub fn delete_sync(&self, sync: &Sync) {
unsafe {
self.inner.DeleteSync(*sync);
}
}
}
fn create_whitespace_cstring_with_len(len: usize) -> std::ffi::CString {
let mut buffer: Vec<u8> = Vec::with_capacity(len + 1);
buffer.extend([b' '].iter().cycle().take(len));
unsafe { std::ffi::CString::from_vec_unchecked(buffer) }
}
impl ShaderType {
fn to_const(&self) -> u32 {
match self {
ShaderType::Vertex => consts::VERTEX_SHADER,
ShaderType::Fragment => consts::FRAGMENT_SHADER,
}
}
}