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
#![allow(missing_docs)]

use std::sync::Once;

use crate::context::GLContext as ContextImpl;
use crate::resource::GLPrimitive;
use na::{Matrix2, Matrix3, Matrix4};

#[path = "../error.rs"]
mod error;

pub type GLenum = u32;
pub type GLintptr = isize;
pub type GLsizeiptr = isize;
pub struct UniformLocation(<ContextImpl as AbstractContext>::UniformLocation);
pub struct Buffer(<ContextImpl as AbstractContext>::Buffer);
pub struct Program(<ContextImpl as AbstractContext>::Program);
pub struct Shader(<ContextImpl as AbstractContext>::Shader);
pub struct Framebuffer(<ContextImpl as AbstractContext>::Framebuffer);
pub struct Renderbuffer(<ContextImpl as AbstractContext>::Renderbuffer);
pub struct Texture(<ContextImpl as AbstractContext>::Texture);
pub struct VertexArray(<ContextImpl as AbstractContext>::VertexArray);

impl Drop for Buffer {
    fn drop(&mut self) {
        unsafe {
            let ctxt = Context::get();
            if ctxt.is_buffer(Some(&self)) {
                verify!(ctxt.delete_buffer(Some(&self)))
            }
        }
    }
}

static mut CONTEXT_SINGLETON: Option<Context> = None;
static CONTEXT_INIT: Once = Once::new();

#[derive(Clone)]
pub struct Context {
    pub ctxt: ContextImpl,
}

impl Context {
    pub const FLOAT: u32 = ContextImpl::FLOAT;
    pub const INT: u32 = ContextImpl::INT;
    pub const UNSIGNED_INT: u32 = ContextImpl::UNSIGNED_INT;
    pub const UNSIGNED_SHORT: u32 = ContextImpl::UNSIGNED_SHORT;
    pub const STATIC_DRAW: u32 = ContextImpl::STATIC_DRAW;
    pub const DYNAMIC_DRAW: u32 = ContextImpl::DYNAMIC_DRAW;
    pub const STREAM_DRAW: u32 = ContextImpl::STREAM_DRAW;
    pub const ARRAY_BUFFER: u32 = ContextImpl::ARRAY_BUFFER;
    pub const ELEMENT_ARRAY_BUFFER: u32 = ContextImpl::ELEMENT_ARRAY_BUFFER;
    pub const VERTEX_SHADER: u32 = ContextImpl::VERTEX_SHADER;
    pub const FRAGMENT_SHADER: u32 = ContextImpl::FRAGMENT_SHADER;
    pub const COMPILE_STATUS: u32 = ContextImpl::COMPILE_STATUS;
    pub const FRAMEBUFFER: u32 = ContextImpl::FRAMEBUFFER;
    pub const RENDERBUFFER: u32 = ContextImpl::RENDERBUFFER;
    pub const DEPTH_ATTACHMENT: u32 = ContextImpl::DEPTH_ATTACHMENT;
    pub const COLOR_ATTACHMENT0: u32 = ContextImpl::COLOR_ATTACHMENT0;
    pub const TEXTURE_2D: u32 = ContextImpl::TEXTURE_2D;
    pub const DEPTH_COMPONENT: u32 = ContextImpl::DEPTH_COMPONENT;
    pub const DEPTH_COMPONENT16: u32 = ContextImpl::DEPTH_COMPONENT16;
    pub const UNSIGNED_BYTE: u32 = ContextImpl::UNSIGNED_BYTE;
    pub const TEXTURE_WRAP_S: u32 = ContextImpl::TEXTURE_WRAP_S;
    pub const TEXTURE_WRAP_T: u32 = ContextImpl::TEXTURE_WRAP_T;
    pub const TEXTURE_MIN_FILTER: u32 = ContextImpl::TEXTURE_MIN_FILTER;
    pub const TEXTURE_MAG_FILTER: u32 = ContextImpl::TEXTURE_MAG_FILTER;
    pub const LINEAR: u32 = ContextImpl::LINEAR;
    pub const NEAREST: u32 = ContextImpl::NEAREST;
    pub const CLAMP_TO_EDGE: u32 = ContextImpl::CLAMP_TO_EDGE;
    pub const RGB: u32 = ContextImpl::RGB;
    pub const RGBA: u32 = ContextImpl::RGBA;
    pub const TEXTURE0: u32 = ContextImpl::TEXTURE0;
    pub const TEXTURE1: u32 = ContextImpl::TEXTURE1;
    pub const REPEAT: u32 = ContextImpl::REPEAT;
    pub const MIRRORED_REPEAT: u32 = ContextImpl::MIRRORED_REPEAT;
    pub const LINEAR_MIPMAP_LINEAR: u32 = ContextImpl::LINEAR_MIPMAP_LINEAR;
    pub const TRIANGLES: u32 = ContextImpl::TRIANGLES;
    pub const CULL_FACE: u32 = ContextImpl::CULL_FACE;
    pub const FRONT_AND_BACK: u32 = ContextImpl::FRONT_AND_BACK;
    pub const FILL: u32 = ContextImpl::FILL;
    pub const LINE: u32 = ContextImpl::LINE;
    pub const POINT: u32 = ContextImpl::POINT;
    pub const LINES: u32 = ContextImpl::LINES;
    pub const POINTS: u32 = ContextImpl::POINTS;
    pub const TRIANGLE_STRIP: u32 = ContextImpl::TRIANGLE_STRIP;
    pub const COLOR_BUFFER_BIT: u32 = ContextImpl::COLOR_BUFFER_BIT;
    pub const DEPTH_BUFFER_BIT: u32 = ContextImpl::DEPTH_BUFFER_BIT;
    pub const CCW: u32 = ContextImpl::CCW;
    pub const DEPTH_TEST: u32 = ContextImpl::DEPTH_TEST;
    pub const SCISSOR_TEST: u32 = ContextImpl::SCISSOR_TEST;
    pub const PROGRAM_POINT_SIZE: u32 = ContextImpl::PROGRAM_POINT_SIZE;
    pub const LEQUAL: u32 = ContextImpl::LEQUAL;
    pub const BACK: u32 = ContextImpl::BACK;
    pub const PACK_ALIGNMENT: u32 = ContextImpl::PACK_ALIGNMENT;
    pub const BLEND: u32 = ContextImpl::BLEND;
    pub const SRC_ALPHA: u32 = ContextImpl::SRC_ALPHA;
    pub const ONE_MINUS_SRC_ALPHA: u32 = ContextImpl::ONE_MINUS_SRC_ALPHA;
    pub const ONE: u32 = ContextImpl::ONE;
    pub const UNPACK_ALIGNMENT: u32 = ContextImpl::UNPACK_ALIGNMENT;
    pub const ALPHA: u32 = ContextImpl::ALPHA;
    pub const RED: u32 = ContextImpl::RED;

    pub fn init(get_ctxt: impl Fn() -> glow::Context) {
        unsafe {
            CONTEXT_INIT.call_once(|| {
                let ctxt = get_ctxt();
                CONTEXT_SINGLETON = Some(Context {
                    ctxt: ContextImpl::new(ctxt),
                });
            });
        }
    }

    pub fn get() -> Context {
        unsafe {
            CONTEXT_SINGLETON
                .clone()
                .expect("GL context not initialized.")
        }
    }

    pub fn get_error(&self) -> GLenum {
        self.ctxt.get_error()
    }

    pub fn uniform_matrix2fv(
        &self,
        location: Option<&UniformLocation>,
        transpose: bool,
        m: &Matrix2<f32>,
    ) {
        self.ctxt
            .uniform_matrix2fv(location.map(|e| &e.0), transpose, m)
    }

    pub fn uniform_matrix3fv(
        &self,
        location: Option<&UniformLocation>,
        transpose: bool,
        m: &Matrix3<f32>,
    ) {
        self.ctxt
            .uniform_matrix3fv(location.map(|e| &e.0), transpose, m)
    }

    pub fn uniform_matrix4fv(
        &self,
        location: Option<&UniformLocation>,
        transpose: bool,
        m: &Matrix4<f32>,
    ) {
        self.ctxt
            .uniform_matrix4fv(location.map(|e| &e.0), transpose, m)
    }

    pub fn uniform4f(&self, location: Option<&UniformLocation>, x: f32, y: f32, z: f32, w: f32) {
        self.ctxt.uniform4f(location.map(|e| &e.0), x, y, z, w)
    }

    pub fn uniform3f(&self, location: Option<&UniformLocation>, x: f32, y: f32, z: f32) {
        self.ctxt.uniform3f(location.map(|e| &e.0), x, y, z)
    }

    pub fn uniform2f(&self, location: Option<&UniformLocation>, x: f32, y: f32) {
        self.ctxt.uniform2f(location.map(|e| &e.0), x, y)
    }

    pub fn uniform1f(&self, location: Option<&UniformLocation>, x: f32) {
        self.ctxt.uniform1f(location.map(|e| &e.0), x)
    }

    pub fn uniform3i(&self, location: Option<&UniformLocation>, x: i32, y: i32, z: i32) {
        self.ctxt.uniform3i(location.map(|e| &e.0), x, y, z)
    }

    pub fn uniform2i(&self, location: Option<&UniformLocation>, x: i32, y: i32) {
        self.ctxt.uniform2i(location.map(|e| &e.0), x, y)
    }

    pub fn uniform1i(&self, location: Option<&UniformLocation>, x: i32) {
        self.ctxt.uniform1i(location.map(|e| &e.0), x)
    }

    pub fn create_vertex_array(&self) -> Option<VertexArray> {
        self.ctxt.create_vertex_array().map(|e| VertexArray(e))
    }

    pub fn delete_vertex_array(&self, vertex_array: Option<&VertexArray>) {
        self.ctxt.delete_vertex_array(vertex_array.map(|e| &e.0))
    }

    pub fn bind_vertex_array(&self, vertex_array: Option<&VertexArray>) {
        self.ctxt.bind_vertex_array(vertex_array.map(|e| &e.0))
    }

    pub fn create_buffer(&self) -> Option<Buffer> {
        self.ctxt.create_buffer().map(|e| Buffer(e))
    }

    pub fn delete_buffer(&self, buffer: Option<&Buffer>) {
        self.ctxt.delete_buffer(buffer.map(|e| &e.0))
    }

    pub fn bind_buffer(&self, target: GLenum, buffer: Option<&Buffer>) {
        self.ctxt.bind_buffer(target, buffer.map(|e| &e.0))
    }

    pub fn is_buffer(&self, buffer: Option<&Buffer>) -> bool {
        self.ctxt.is_buffer(buffer.map(|e| &e.0))
    }

    pub fn buffer_data_uninitialized(&self, target: GLenum, len: usize, usage: GLenum) {
        self.ctxt.buffer_data_uninitialized(target, len, usage)
    }

    pub fn buffer_data<T: GLPrimitive>(&self, target: GLenum, data: &[T], usage: GLenum) {
        self.ctxt.buffer_data(target, data, usage)
    }

    pub fn buffer_sub_data<T: GLPrimitive>(&self, target: GLenum, offset: u32, data: &[T]) {
        self.ctxt.buffer_sub_data(target, offset, data)
    }

    pub fn create_shader(&self, type_: GLenum) -> Option<Shader> {
        self.ctxt.create_shader(type_).map(|e| Shader(e))
    }

    pub fn create_program(&self) -> Option<Program> {
        self.ctxt.create_program().map(|e| Program(e))
    }

    pub fn delete_program(&self, program: Option<&Program>) {
        self.ctxt.delete_program(program.map(|e| &e.0))
    }

    pub fn delete_shader(&self, shader: Option<&Shader>) {
        self.ctxt.delete_shader(shader.map(|e| &e.0))
    }

    pub fn is_shader(&self, shader: Option<&Shader>) -> bool {
        self.ctxt.is_shader(shader.map(|e| &e.0))
    }
    pub fn is_program(&self, program: Option<&Program>) -> bool {
        self.ctxt.is_program(program.map(|e| &e.0))
    }

    pub fn shader_source(&self, shader: &Shader, source: &str) {
        self.ctxt.shader_source(&shader.0, source)
    }

    pub fn compile_shader(&self, shader: &Shader) {
        self.ctxt.compile_shader(&shader.0)
    }

    pub fn link_program(&self, program: &Program) {
        self.ctxt.link_program(&program.0)
    }

    pub fn use_program(&self, program: Option<&Program>) {
        self.ctxt.use_program(program.map(|e| &e.0))
    }

    pub fn attach_shader(&self, program: &Program, shader: &Shader) {
        self.ctxt.attach_shader(&program.0, &shader.0)
    }

    pub fn get_shader_parameter_int(&self, shader: &Shader, pname: GLenum) -> Option<i32> {
        self.ctxt.get_shader_parameter_int(&shader.0, pname)
    }

    pub fn get_shader_info_log(&self, shader: &Shader) -> Option<String> {
        self.ctxt.get_shader_info_log(&shader.0)
    }

    pub fn vertex_attrib_pointer(
        &self,
        index: u32,
        size: i32,
        type_: GLenum,
        normalized: bool,
        stride: i32,
        offset: GLintptr,
    ) {
        self.ctxt
            .vertex_attrib_pointer(index, size, type_, normalized, stride, offset)
    }

    pub fn enable_vertex_attrib_array(&self, index: u32) {
        self.ctxt.enable_vertex_attrib_array(index)
    }

    pub fn disable_vertex_attrib_array(&self, index: u32) {
        self.ctxt.disable_vertex_attrib_array(index)
    }

    pub fn get_attrib_location(&self, program: &Program, name: &str) -> i32 {
        self.ctxt.get_attrib_location(&program.0, name)
    }

    pub fn get_uniform_location(&self, program: &Program, name: &str) -> Option<UniformLocation> {
        self.ctxt
            .get_uniform_location(&program.0, name)
            .map(|e| UniformLocation(e))
    }

    pub fn viewport(&self, x: i32, y: i32, width: i32, height: i32) {
        self.ctxt.viewport(x, y, width, height)
    }

    pub fn scissor(&self, x: i32, y: i32, width: i32, height: i32) {
        self.ctxt.scissor(x, y, width, height)
    }

    pub fn create_framebuffer(&self) -> Option<Framebuffer> {
        self.ctxt.create_framebuffer().map(|e| Framebuffer(e))
    }

    pub fn is_framebuffer(&self, framebuffer: Option<&Framebuffer>) -> bool {
        self.ctxt.is_framebuffer(framebuffer.map(|e| &e.0))
    }

    pub fn bind_framebuffer(&self, target: GLenum, framebuffer: Option<&Framebuffer>) {
        self.ctxt
            .bind_framebuffer(target, framebuffer.map(|e| &e.0))
    }

    pub fn delete_framebuffer(&self, framebuffer: Option<&Framebuffer>) {
        self.ctxt.delete_framebuffer(framebuffer.map(|e| &e.0))
    }

    pub fn framebuffer_texture2d(
        &self,
        target: GLenum,
        attachment: GLenum,
        textarget: GLenum,
        texture: Option<&Texture>,
        level: i32,
    ) {
        self.ctxt
            .framebuffer_texture2d(target, attachment, textarget, texture.map(|e| &e.0), level)
    }

    pub fn create_renderbuffer(&self) -> Option<Renderbuffer> {
        self.ctxt.create_renderbuffer().map(|b| Renderbuffer(b))
    }

    pub fn delete_renderbuffer(&self, buffer: Option<&Renderbuffer>) {
        self.ctxt.delete_renderbuffer(buffer.map(|b| &b.0))
    }

    pub fn is_renderbuffer(&self, buffer: Option<&Renderbuffer>) -> bool {
        self.ctxt.is_renderbuffer(buffer.map(|b| &b.0))
    }

    pub fn bind_renderbuffer(&self, buffer: Option<&Renderbuffer>) {
        self.ctxt.bind_renderbuffer(buffer.map(|b| &b.0))
    }

    pub fn renderbuffer_storage(&self, internal_format: GLenum, width: i32, height: i32) {
        self.ctxt
            .renderbuffer_storage(internal_format, width, height)
    }

    pub fn framebuffer_renderbuffer(
        &self,
        attachment: GLenum,
        renderbuffer: Option<&Renderbuffer>,
    ) {
        self.ctxt
            .framebuffer_renderbuffer(attachment, renderbuffer.map(|b| &b.0))
    }

    pub fn bind_texture(&self, target: GLenum, texture: Option<&Texture>) {
        self.ctxt.bind_texture(target, texture.map(|e| &e.0))
    }

    pub fn tex_image2d(
        &self,
        target: GLenum,
        level: i32,
        internalformat: i32,
        width: i32,
        height: i32,
        border: i32,
        format: GLenum,
        pixels: Option<&[u8]>,
    ) {
        self.ctxt.tex_image2d(
            target,
            level,
            internalformat,
            width,
            height,
            border,
            format,
            pixels,
        )
    }

    pub fn tex_image2di(
        &self,
        target: GLenum,
        level: i32,
        internalformat: i32,
        width: i32,
        height: i32,
        border: i32,
        format: GLenum,
        pixels: Option<&[i32]>,
    ) {
        self.ctxt.tex_image2di(
            target,
            level,
            internalformat,
            width,
            height,
            border,
            format,
            pixels,
        )
    }

    pub fn tex_sub_image2d(
        &self,
        target: GLenum,
        level: i32,
        xoffset: i32,
        yoffset: i32,
        width: i32,
        height: i32,
        format: GLenum,
        pixels: Option<&[u8]>,
    ) {
        self.ctxt.tex_sub_image2d(
            target, level, xoffset, yoffset, width, height, format, pixels,
        )
    }

    pub fn tex_parameteri(&self, target: GLenum, pname: GLenum, param: i32) {
        self.ctxt.tex_parameteri(target, pname, param)
    }

    pub fn is_texture(&self, texture: Option<&Texture>) -> bool {
        self.ctxt.is_texture(texture.map(|e| &e.0))
    }

    pub fn create_texture(&self) -> Option<Texture> {
        self.ctxt.create_texture().map(|e| Texture(e))
    }

    pub fn delete_texture(&self, texture: Option<&Texture>) {
        self.ctxt.delete_texture(texture.map(|e| &e.0))
    }

    pub fn active_texture(&self, texture: GLenum) {
        self.ctxt.active_texture(texture)
    }

    pub fn enable(&self, cap: GLenum) {
        self.ctxt.enable(cap)
    }

    pub fn disable(&self, cap: GLenum) {
        self.ctxt.disable(cap)
    }

    pub fn draw_elements(&self, mode: GLenum, count: i32, type_: GLenum, offset: GLintptr) {
        self.ctxt.draw_elements(mode, count, type_, offset)
    }

    pub fn draw_arrays(&self, mode: GLenum, first: i32, count: i32) {
        self.ctxt.draw_arrays(mode, first, count)
    }

    pub fn point_size(&self, size: f32) {
        self.ctxt.point_size(size)
    }

    pub fn line_width(&self, size: f32) {
        self.ctxt.line_width(size)
    }

    pub fn clear(&self, mask: u32) {
        self.ctxt.clear(mask)
    }

    pub fn clear_color(&self, r: f32, g: f32, b: f32, a: f32) {
        self.ctxt.clear_color(r, g, b, a)
    }

    pub fn polygon_mode(&self, face: GLenum, mode: GLenum) -> bool {
        self.ctxt.polygon_mode(face, mode)
    }

    pub fn front_face(&self, mode: GLenum) {
        self.ctxt.front_face(mode)
    }

    pub fn depth_func(&self, mode: GLenum) {
        self.ctxt.depth_func(mode)
    }

    pub fn cull_face(&self, mode: GLenum) {
        self.ctxt.cull_face(mode)
    }

    pub fn read_pixels(
        &self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        format: GLenum,
        pixels: Option<&mut [u8]>,
    ) {
        self.ctxt.read_pixels(x, y, width, height, format, pixels)
    }

    pub fn pixel_storei(&self, pname: GLenum, param: i32) {
        self.ctxt.pixel_storei(pname, param)
    }

    pub fn blend_func_separate(
        &self,
        src_rgb: GLenum,
        dst_rgb: GLenum,
        src_alpha: GLenum,
        dst_alpha: GLenum,
    ) {
        self.ctxt
            .blend_func_separate(src_rgb, dst_rgb, src_alpha, dst_alpha)
    }
}

pub(crate) trait AbstractContextConst {
    const FLOAT: u32;
    const INT: u32;
    const UNSIGNED_INT: u32;
    const UNSIGNED_SHORT: u32;
    const STATIC_DRAW: u32;
    const DYNAMIC_DRAW: u32;
    const STREAM_DRAW: u32;
    const ARRAY_BUFFER: u32;
    const ELEMENT_ARRAY_BUFFER: u32;
    const VERTEX_SHADER: u32;
    const FRAGMENT_SHADER: u32;
    const COMPILE_STATUS: u32;
    const FRAMEBUFFER: u32;
    const RENDERBUFFER: u32;
    const DEPTH_ATTACHMENT: u32;
    const COLOR_ATTACHMENT0: u32;
    const TEXTURE_2D: u32;
    const DEPTH_COMPONENT: u32;
    const DEPTH_COMPONENT16: u32;
    const UNSIGNED_BYTE: u32;
    const TEXTURE_WRAP_S: u32;
    const TEXTURE_WRAP_T: u32;
    const TEXTURE_MIN_FILTER: u32;
    const TEXTURE_MAG_FILTER: u32;
    const LINEAR: u32;
    const NEAREST: u32;
    const CLAMP_TO_EDGE: u32;
    const RGB: u32;
    const RGBA: u32;
    const TEXTURE0: u32;
    const TEXTURE1: u32;
    const REPEAT: u32;
    const MIRRORED_REPEAT: u32;
    const LINEAR_MIPMAP_LINEAR: u32;
    const TRIANGLES: u32;
    const CULL_FACE: u32;
    const FRONT_AND_BACK: u32;
    const FILL: u32;
    const LINE: u32;
    const POINT: u32;
    const LINES: u32;
    const POINTS: u32;
    const TRIANGLE_STRIP: u32;
    const COLOR_BUFFER_BIT: u32;
    const DEPTH_BUFFER_BIT: u32;
    const CCW: u32;
    const DEPTH_TEST: u32;
    const SCISSOR_TEST: u32;
    const PROGRAM_POINT_SIZE: u32;
    const LEQUAL: u32;
    const BACK: u32;
    const PACK_ALIGNMENT: u32;
    const BLEND: u32;
    const SRC_ALPHA: u32;
    const ONE_MINUS_SRC_ALPHA: u32;
    const ONE: u32;
    const UNPACK_ALIGNMENT: u32;
    const ALPHA: u32;
    const RED: u32;
}

pub(crate) trait AbstractContext {
    type UniformLocation;
    type Buffer;
    type Shader;
    type Program;
    type Texture;
    type Framebuffer;
    type Renderbuffer;
    type VertexArray;

    fn get_error(&self) -> GLenum;
    fn uniform_matrix2fv(
        &self,
        location: Option<&Self::UniformLocation>,
        transpose: bool,
        m: &Matrix2<f32>,
    );
    fn uniform_matrix3fv(
        &self,
        location: Option<&Self::UniformLocation>,
        transpose: bool,
        m: &Matrix3<f32>,
    );
    fn uniform_matrix4fv(
        &self,
        location: Option<&Self::UniformLocation>,
        transpose: bool,
        m: &Matrix4<f32>,
    );
    fn uniform4f(&self, location: Option<&Self::UniformLocation>, x: f32, y: f32, z: f32, w: f32);
    fn uniform3f(&self, location: Option<&Self::UniformLocation>, x: f32, y: f32, z: f32);
    fn uniform2f(&self, location: Option<&Self::UniformLocation>, x: f32, y: f32);
    fn uniform1f(&self, location: Option<&Self::UniformLocation>, x: f32);
    fn uniform3i(&self, location: Option<&Self::UniformLocation>, x: i32, y: i32, z: i32);
    fn uniform2i(&self, location: Option<&Self::UniformLocation>, x: i32, y: i32);
    fn uniform1i(&self, location: Option<&Self::UniformLocation>, x: i32);

    fn create_vertex_array(&self) -> Option<Self::VertexArray>;
    fn delete_vertex_array(&self, vertex_array: Option<&Self::VertexArray>);
    fn bind_vertex_array(&self, vertex_array: Option<&Self::VertexArray>);
    fn create_buffer(&self) -> Option<Self::Buffer>;
    fn delete_buffer(&self, buffer: Option<&Self::Buffer>);
    fn is_buffer(&self, buffer: Option<&Self::Buffer>) -> bool;
    fn bind_buffer(&self, target: GLenum, buffer: Option<&Self::Buffer>);
    fn buffer_data_uninitialized(&self, target: GLenum, len: usize, usage: GLenum);
    fn buffer_data<T: GLPrimitive>(&self, target: GLenum, data: &[T], usage: GLenum);
    fn buffer_sub_data<T: GLPrimitive>(&self, target: GLenum, offset: u32, data: &[T]);

    fn create_shader(&self, type_: GLenum) -> Option<Self::Shader>;
    fn create_program(&self) -> Option<Self::Program>;
    fn delete_program(&self, program: Option<&Self::Program>);
    fn delete_shader(&self, shader: Option<&Self::Shader>);
    fn is_shader(&self, shader: Option<&Self::Shader>) -> bool;
    fn is_program(&self, program: Option<&Self::Program>) -> bool;
    fn shader_source(&self, shader: &Self::Shader, source: &str);
    fn compile_shader(&self, shader: &Self::Shader);
    fn link_program(&self, program: &Self::Program);
    fn use_program(&self, program: Option<&Self::Program>);
    fn attach_shader(&self, program: &Self::Program, shader: &Self::Shader);
    fn get_shader_parameter_int(&self, shader: &Self::Shader, pname: GLenum) -> Option<i32>;
    fn get_shader_info_log(&self, shader: &Self::Shader) -> Option<String>;
    fn vertex_attrib_pointer(
        &self,
        index: u32,
        size: i32,
        type_: GLenum,
        normalized: bool,
        stride: i32,
        offset: GLintptr,
    );
    fn enable_vertex_attrib_array(&self, index: u32);
    fn disable_vertex_attrib_array(&self, index: u32);

    fn get_attrib_location(&self, program: &Self::Program, name: &str) -> i32;
    fn get_uniform_location(
        &self,
        program: &Self::Program,
        name: &str,
    ) -> Option<Self::UniformLocation>;

    fn viewport(&self, x: i32, y: i32, width: i32, height: i32);
    fn scissor(&self, x: i32, y: i32, width: i32, height: i32);
    fn create_framebuffer(&self) -> Option<Self::Framebuffer>;
    fn is_framebuffer(&self, framebuffer: Option<&Self::Framebuffer>) -> bool;
    fn bind_framebuffer(&self, target: GLenum, framebuffer: Option<&Self::Framebuffer>);
    fn delete_framebuffer(&self, framebuffer: Option<&Self::Framebuffer>);
    fn framebuffer_texture2d(
        &self,
        target: GLenum,
        attachment: GLenum,
        textarget: GLenum,
        texture: Option<&Self::Texture>,
        level: i32,
    );

    fn create_renderbuffer(&self) -> Option<Self::Renderbuffer>;
    fn is_renderbuffer(&self, buffer: Option<&Self::Renderbuffer>) -> bool;
    fn delete_renderbuffer(&self, buffer: Option<&Self::Renderbuffer>);
    fn bind_renderbuffer(&self, buffer: Option<&Self::Renderbuffer>);
    fn renderbuffer_storage(&self, internal_format: GLenum, width: i32, height: i32);
    fn framebuffer_renderbuffer(
        &self,
        attachment: GLenum,
        renderbuffer: Option<&Self::Renderbuffer>,
    );

    fn bind_texture(&self, target: GLenum, texture: Option<&Self::Texture>);
    fn tex_image2d(
        &self,
        target: GLenum,
        level: i32,
        internalformat: i32,
        width: i32,
        height: i32,
        border: i32,
        format: GLenum,
        pixels: Option<&[u8]>,
    );
    fn tex_image2di(
        &self,
        target: GLenum,
        level: i32,
        internalformat: i32,
        width: i32,
        height: i32,
        border: i32,
        format: GLenum,
        pixels: Option<&[i32]>,
    );
    fn tex_sub_image2d(
        &self,
        target: GLenum,
        level: i32,
        xoffset: i32,
        yoffset: i32,
        width: i32,
        height: i32,
        format: GLenum,
        pixels: Option<&[u8]>,
    );
    fn tex_parameteri(&self, target: GLenum, pname: GLenum, param: i32);
    fn is_texture(&self, texture: Option<&Self::Texture>) -> bool;
    fn create_texture(&self) -> Option<Self::Texture>;
    fn delete_texture(&self, texture: Option<&Self::Texture>);
    fn active_texture(&self, texture: GLenum);

    fn enable(&self, cap: GLenum);
    fn disable(&self, cap: GLenum);

    fn draw_elements(&self, mode: GLenum, count: i32, type_: GLenum, offset: GLintptr);
    fn draw_arrays(&self, mode: GLenum, first: i32, count: i32);

    fn point_size(&self, size: f32);
    fn line_width(&self, size: f32);

    fn clear(&self, mask: u32);
    fn clear_color(&self, r: f32, g: f32, b: f32, a: f32);

    fn polygon_mode(&self, face: GLenum, mode: GLenum) -> bool;

    fn front_face(&self, mode: GLenum);
    fn depth_func(&self, mode: GLenum);
    fn cull_face(&self, mode: GLenum);

    fn read_pixels(
        &self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        format: GLenum,
        pixels: Option<&mut [u8]>,
    );
    fn pixel_storei(&self, pname: GLenum, param: i32);

    fn blend_func_separate(
        &self,
        src_rgb: GLenum,
        dst_rgb: GLenum,
        src_alpha: GLenum,
        dst_alpha: GLenum,
    );
}