Repository navigation
Expand file tree
/
Copy pathgl.rs
More file actions
1507 lines (1337 loc) · 50.5 KB
/
Copy pathgl.rs
File metadata and controls
1507 lines (1337 loc) · 50.5 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
<
8000
div data-line-number="462" class="react-line-number react-code-text" style="padding-right:16px">462463
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
use std::ffi::CString;
use crate::window;
mod cache;
use super::*;
use cache::*;
/// Raw OpenGL bindings
/// Highly unsafe, some of the functions could be missing due to incompatible GL version
/// or all of them might be missing alltogether if rendering context is not a GL one.
pub mod raw_gl {
use super::*;
#[doc(inline)]
pub use crate::native::gl::*;
pub fn texture_format_into_gl(format: TextureFormat) -> (GLenum, GLenum, GLenum) {
format.into()
}
}
#[derive(Clone, Copy, Debug)]
struct Buffer {
gl_buf: GLuint,
buffer_type: BufferType,
size: usize,
// Dimension of the indices for this buffer,
// used only as a type argument for glDrawElements and can be
// 1, 2 or 4
index_type: Option<u32>,
}
#[derive(Debug)]
struct ShaderUniform {
gl_loc: UniformLocation,
uniform_type: UniformType,
array_count: i32,
}
struct ShaderInternal {
program: GLuint,
images: Vec<ShaderImage>,
uniforms: Vec<ShaderUniform>,
}
#[derive(Clone, Copy, Debug)]
struct Texture {
raw: GLuint,
params: TextureParams,
}
/// Converts from TextureFormat to (internal_format, format, pixel_type)
impl From<TextureFormat> for (GLenum, GLenum, GLenum) {
fn from(format: TextureFormat) -> Self {
match format {
TextureFormat::RGB8 => (GL_RGB, GL_RGB, GL_UNSIGNED_BYTE),
TextureFormat::RGBA8 => (GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE),
TextureFormat::RGBA16F => (GL_RGBA16F, GL_RGBA, GL_FLOAT),
TextureFormat::Depth => (GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT),
TextureFormat::Depth32 => (GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT),
#[cfg(target_arch = "wasm32")]
TextureFormat::Alpha => (GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE),
#[cfg(not(target_arch = "wasm32"))]
TextureFormat::Alpha => (GL_R8, GL_RED, GL_UNSIGNED_BYTE), // texture updates will swizzle Red -> Alpha to match WASM
}
}
}
impl From<TextureKind> for GLuint {
fn from(kind: TextureKind) -> GLuint {
match kind {
TextureKind::Texture2D => GL_TEXTURE_2D,
TextureKind::CubeMap => GL_TEXTURE_CUBE_MAP,
}
}
}
impl From<Equation> for GLenum {
fn from(eq: Equation) -> Self {
match eq {
Equation::Add => GL_FUNC_ADD,
Equation::Subtract => GL_FUNC_SUBTRACT,
Equation::ReverseSubtract => GL_FUNC_REVERSE_SUBTRACT,
}
}
}
impl From<BlendFactor> for GLenum {
fn from(factor: BlendFactor) -> GLenum {
match factor {
BlendFactor::Zero => GL_ZERO,
BlendFactor:: GL_ONE,
BlendFactor::Value(BlendValue::SourceColor) => GL_SRC_COLOR,
BlendFactor::Value(BlendValue::SourceAlpha) => GL_SRC_ALPHA,
BlendFactor::Value(BlendValue::DestinationColor) => GL_DST_COLOR,
BlendFactor::Value(BlendValue::DestinationAlpha) => GL_DST_ALPHA,
BlendFactor::OneMinusValue(BlendValue::SourceColor) => GL_ONE_MINUS_SRC_COLOR,
BlendFactor::OneMinusValue(BlendValue::SourceAlpha) => GL_ONE_MINUS_SRC_ALPHA,
BlendFactor::OneMinusValue(BlendValue::DestinationColor) => GL_ONE_MINUS_DST_COLOR,
BlendFactor::OneMinusValue(BlendValue::DestinationAlpha) => GL_ONE_MINUS_DST_ALPHA,
BlendFactor::SourceAlphaSaturate => GL_SRC_ALPHA_SATURATE,
}
}
}
impl From<StencilOp> for GLenum {
fn from(op: StencilOp) -> Self {
match op {
StencilOp::Keep => GL_KEEP,
StencilOp::Zero => GL_ZERO,
StencilOp::Replace => GL_REPLACE,
StencilOp::IncrementClamp => GL_INCR,
StencilOp::DecrementClamp => GL_DECR,
StencilOp::Invert => GL_INVERT,
StencilOp::IncrementWrap => GL_INCR_WRAP,
StencilOp::DecrementWrap => GL_DECR_WRAP,
}
}
}
impl From<CompareFunc> for GLenum {
fn from(cf: CompareFunc) -> Self {
match cf {
CompareFunc::Always => GL_ALWAYS,
CompareFunc::Never => GL_NEVER,
CompareFunc::Less => GL_LESS,
CompareFunc::Equal => GL_EQUAL,
CompareFunc::LessOrEqual => GL_LEQUAL,
CompareFunc::Greater => GL_GREATER,
CompareFunc::NotEqual => GL_NOTEQUAL,
CompareFunc::GreaterOrEqual => GL_GEQUAL,
}
}
}
impl Texture {
pub fn new(
ctx: &mut GlContext,
_access: TextureAccess,
source: TextureSource,
params: TextureParams,
) -> Texture {
if let TextureSource::Bytes(bytes_data) = source {
assert_eq!(
params.format.size(params.width, params.height) as usize,
bytes_data.len()
);
}
let (internal_format, format, pixel_type) = params.format.into();
ctx.cache.store_texture_binding(0);
let mut texture: GLuint = 0;
unsafe {
glGenTextures(1, &mut texture as *mut _);
ctx.cache.bind_texture(0, params.kind.into(), texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // miniquad always uses row alignment of 1
if cfg!(not(target_arch = "wasm32")) {
// if not WASM
if params.format == TextureFormat::Alpha {
// if alpha miniquad texture, the value on non-WASM is stored in red channel
// swizzle red -> alpha
glTexParameteri(params.kind.into(), GL_TEXTURE_SWIZZLE_A, GL_RED as _);
} else {
// keep alpha -> alpha
glTexParameteri(params.kind.into(), GL_TEXTURE_SWIZZLE_A, GL_ALPHA as _);
}
}
match source {
TextureSource::Empty => {
// not quite sure if glTexImage2D(null) is really a requirement
// but it was like this for quite a while and apparantly it works?
glTexImage2D(
GL_TEXTURE_2D,
0,
internal_format as i32,
params.width as i32,
params.height as i32,
0,
format,
pixel_type,
std::ptr::null() as _,
);
}
TextureSource::Bytes(source) => {
assert!(params.kind == TextureKind::Texture2D, "incompatible TextureKind and TextureSource. Cubemaps require TextureSource::Array of 6 textures.");
glTexImage2D(
GL_TEXTURE_2D,
0,
internal_format as i32,
params.width as i32,
params.height as i32,
0,
format,
pixel_type,
source.as_ptr() as *const _,
);
}
TextureSource::Array(array) => {
if params.kind == TextureKind::CubeMap {
assert!(
array.len() == 6,
"Cubemaps require TextureSource::Array of 6 textures."
);
}
for (cubemap_face, mipmaps) in array.iter().enumerate() {
if mipmaps.len() != 1 {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, array.len() as _);
}
for (mipmap_level, bytes) in mipmaps.iter().enumerate() {
let target = match params.kind {
TextureKind::Texture2D => GL_TEXTURE_2D,
TextureKind::CubeMap => {
GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubemap_face as u32
}
};
glTexImage2D(
target,
mipmap_level as _,
internal_format as i32,
params.width as i32,
params.height as i32,
0,
format,
pixel_type,
bytes.as_ptr() as *const _,
);
}
}
}
}
let wrap = match params.wrap {
TextureWrap::Repeat => GL_REPEAT,
TextureWrap::Mirror => GL_MIRRORED_REPEAT,
TextureWrap::Clamp => GL_CLAMP_TO_EDGE,
};
let min_filter = Self::gl_filter(params.min_filter, params.mipmap_filter);
let mag_filter = match params.mag_filter {
FilterMode::Nearest => GL_NEAREST,
FilterMode::Linear => GL_LINEAR,
};
glTexParameteri(params.kind.into(), GL_TEXTURE_WRAP_S, wrap as i32);
glTexParameteri(params.kind.into(), GL_TEXTURE_WRAP_T, wrap as i32);
glTexParameteri(params.kind.into(), GL_TEXTURE_MIN_FILTER, min_filter as i32);
glTexParameteri(params.kind.into(), GL_TEXTURE_MAG_FILTER, mag_filter as i32);
}
ctx.cache.restore_texture_binding(0);
Texture {
raw: texture,
params,
}
}
pub fn delete(&self) {
unsafe {
glDeleteTextures(1, &self.raw as *const _);
}
}
pub fn resize(&mut self, ctx: &mut GlContext, width: u32, height: u32, source: Option<&[u8]>) {
ctx.cache.store_texture_binding(0);
ctx.cache.bind_texture(0, self.params.kind.into(), self.raw);
let (internal_format, format, pixel_type) = self.params.format.into();
self.params.width = width;
self.params.height = height;
unsafe {
glTexImage2D(
GL_TEXTURE_2D,
0,
internal_format as i32,
self.params.width as i32,
self.params.height as i32,
0,
format,
pixel_type,
match source {
Some(source) => source.as_ptr() as *const _,
Option::None => std::ptr::null(),
},
);
}
ctx.cache.restore_texture_binding(0);
}
pub fn update_texture_part(
&self,
ctx: &mut GlContext,
x_offset: i32,
y_offset: i32,
width: i32,
height: i32,
source: &[u8],
) {
assert_eq!(self.size(width as _, height as _), source.len());
assert!(x_offset + width <= self.params.width as _);
assert!(y_offset + height <= self.params.height as _);
ctx.cache.store_texture_binding(0);
ctx.cache.bind_texture(0, self.params.kind.into(), self.raw);
let (_, format, pixel_type) = self.params.format.into();
unsafe {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // miniquad always uses row alignment of 1
if cfg!(not(target_arch = "wasm32")) {
// if not WASM
if self.params.format == TextureFormat::Alpha {
// if alpha miniquad texture, the value on non-WASM is stored in red channel
// swizzle red -> alpha
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED as _);
} else {
// keep alpha -> alpha
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA as _);
}
}
glTexSubImage2D(
GL_TEXTURE_2D,
0,
x_offset as _,
y_offset as _,
width as _,
height as _,
format,
pixel_type,
source.as_ptr() as *const _,
);
}
ctx.cache.restore_texture_binding(0);
}
/// Read texture data into CPU memory
pub fn read_pixels(&self, bytes: &mut [u8]) {
let (_, format, pixel_type) = self.params.format.into();
let mut fbo = 0;
unsafe {
let mut binded_fbo: i32 = 0;
glGetIntegerv(gl::GL_DRAW_FRAMEBUFFER_BINDING, &mut binded_fbo);
glGenFramebuffers(1, &mut fbo);
glBindFramebuffer(gl::GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(
gl::GL_FRAMEBUFFER,
gl::GL_COLOR_ATTACHMENT0,
gl::GL_TEXTURE_2D,
self.raw,
0,
);
glReadPixels(
0,
0,
self.params.width as _,
self.params.height as _,
format,
pixel_type,
bytes.as_mut_ptr() as _,
);
glBindFramebuffer(gl::GL_FRAMEBUFFER, binded_fbo as _);
glDeleteFramebuffers(1, &fbo);
}
}
#[inline]
fn size(&self, width: u32, height: u32) -> usize {
self.params.format.size(width, height) as usize
}
fn gl_filter(filter: FilterMode, mipmap_filter: MipmapFilterMode) -> GLenum {
match filter {
FilterMode::Nearest => match mipmap_filter {
MipmapFilterMode::None => GL_NEAREST,
MipmapFilterMode::Nearest => GL_NEAREST_MIPMAP_NEAREST,
MipmapFilterMode::Linear => GL_NEAREST_MIPMAP_LINEAR,
},
FilterMode::Linear => match mipmap_filter {
MipmapFilterMode::None => GL_LINEAR,
MipmapFilterMode::Nearest => GL_LINEAR_MIPMAP_NEAREST,
MipmapFilterMode::Linear => GL_LINEAR_MIPMAP_LINEAR,
},
}
}
}
pub(crate) struct PipelineInternal {
layout: Vec<Option<VertexAttributeInternal>>,
shader: ShaderId,
params: PipelineParams,
}
type UniformLocation = Option<GLint>;
pub struct ShaderImage {
gl_loc: UniformLocation,
}
fn get_uniform_location(program: GLuint, name: &str) -> Option<i32> {
let cname = CString::new(name).unwrap_or_else(|e| panic!("{}", e));
let location = unsafe { glGetUniformLocation(program, cname.as_ptr()) };
if location == -1 {
return None;
}
Some(location)
}
pub(crate) struct RenderPassInternal {
gl_fb: GLuint,
color_textures: Vec<TextureId>,
depth_texture: Option<TextureId>,
}
struct Textures(Vec<Texture>);
impl Textures {
fn get(&self, texture: TextureId) -> Texture {
match texture.0 {
TextureIdInner::Raw(RawId::OpenGl(texture)) => Texture {
raw: texture,
params: Default::default(),
},
#[cfg(target_vendor = "apple")]
TextureIdInner::Raw(RawId::Metal(..)) => panic!("Metal texture in OpenGL context!"),
TextureIdInner::Managed(texture) => self.0[texture],
}
}
}
pub struct GlContext {
shaders: Vec<ShaderInternal>,
pipelines: Vec<PipelineInternal>,
passes: Vec<RenderPassInternal>,
buffers: Vec<Buffer>,
textures: Textures,
default_framebuffer: GLuint,
pub(crate) cache: GlCache,
pub(crate) features: Features,
}
impl GlContext {
pub fn new() -> GlContext {
unsafe {
let mut default_framebuffer: GLuint = 0;
glGetIntegerv(
GL_FRAMEBUFFER_BINDING,
&mut default_framebuffer as *mut _ as *mut _,
);
let mut vao = 0;
glGenVertexArrays(1, &mut vao as *mut _);
glBindVertexArray(vao);
GlContext {
default_framebuffer,
shaders: vec![],
pipelines: vec![],
passes: vec![],
buffers: vec![],
textures: Textures(vec![]),
features: Features {
instancing: !crate::native::gl::is_gl2(),
..Default::default()
},
cache: GlCache {
stored_index_buffer: 0,
stored_index_type: None,
stored_vertex_buffer: 0,
index_buffer: 0,
index_type: None,
vertex_buffer: 0,
cur_pipeline: None,
color_blend: None,
alpha_blend: None,
stencil: None,
color_write: (true, true, true, true),
cull_face: CullFace::Nothing,
stored_texture: 0,
stored_target: 0,
textures: [CachedTexture {
target: 0,
texture: 0,
}; MAX_SHADERSTAGE_IMAGES],
attributes: [None; MAX_VERTEX_ATTRIBUTES],
},
}
}
}
pub fn features(&self) -> &Features {
&self.features
}
}
fn load_shader_internal(
vertex_shader: &str,
fragment_shader: &str,
meta: ShaderMeta,
) -> Result<ShaderInternal, ShaderError> {
unsafe {
let vertex_shader = load_shader(GL_VERTEX_SHADER, vertex_shader)?;
let fragment_shader = load_shader(GL_FRAGMENT_SHADER, fragment_shader)?;
let program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
let mut link_status = 0;
glGetProgramiv(program, GL_LINK_STATUS, &mut link_status as *mut _);
if link_status == 0 {
let mut max_length: i32 = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &mut max_length as *mut _);
let mut error_message = vec![0u8; max_length as usize + 1];
glGetProgramInfoLog(
program,
max_length,
&mut max_length as *mut _,
error_message.as_mut_ptr() as *mut _,
);
assert!(max_length >= 1);
let error_message =
std::string::String::from_utf8_lossy(&error_message[0..max_length as usize - 1]);
return Err(ShaderError::LinkError(error_message.to_string()));
}
glUseProgram(program);
#[rustfmt::skip]
let images = meta.images.iter().map(|name| ShaderImage {
gl_loc: get_uniform_location(program, name),
}).collect();
#[rustfmt::skip]
let uniforms = meta.uniforms.uniforms.iter().scan(0, |offset, uniform| {
let res = ShaderUniform {
gl_loc: get_uniform_location(program, &uniform.name),
uniform_type: uniform.uniform_type,
array_count: uniform.array_count as _,
};
*offset += uniform.uniform_type.size() * uniform.array_count;
Some(res)
}).collect();
Ok(ShaderInternal {
program,
images,
uniforms,
})
}
}
pub fn load_shader(shader_type: GLenum, source: &str) -> Result<GLuint, ShaderError> {
unsafe {
let shader = glCreateShader(shader_type);
assert!(shader != 0);
let cstring = CString::new(source)?;
let csource = [cstring];
glShaderSource(shader, 1, csource.as_ptr() as *const _, std::ptr::null());
glCompileShader(shader);
let mut is_compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &mut is_compiled as *mut _);
if is_compiled == 0 {
let mut max_length: i32 = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &mut max_length as *mut _);
let mut error_message = vec![0u8; max_length as usize + 1];
glGetShaderInfoLog(
shader,
max_length,
&mut max_length as *mut _,
error_message.as_mut_ptr() as *mut _,
);
assert!(max_length >= 1);
let mut error_message =
std::string::String::from_utf8_lossy(&error_message[0..max_length as usize - 1])
.into_owned();
// On Wasm + Chrome, for unknown reason, string with zero-terminator is returned. On Firefox there is no zero-terminators in JavaScript string.
if error_message.ends_with('\0') {
error_message.pop();
}
return Err(ShaderError::CompilationError {
shader_type: match shader_type {
GL_VERTEX_SHADER => ShaderType::Vertex,
GL_FRAGMENT_SHADER => ShaderType::Fragment,
_ => unreachable!(),
},
error_message,
});
}
Ok(shader)
}
}
impl GlContext {
fn set_blend(&mut self, color_blend: Option<BlendState>, alpha_blend: Option<BlendState>) {
if color_blend.is_none() && alpha_blend.is_some() {
panic!("AlphaBlend without ColorBlend");
}
if self.cache.color_blend == color_blend && self.cache.alpha_blend == alpha_blend {
return;
}
unsafe {
if let Some(color_blend) = color_blend {
if self.cache.color_blend.is_none() {
glEnable(GL_BLEND);
}
let BlendState {
equation: eq_rgb,
sfactor: src_rgb,
dfactor: dst_rgb,
} = color_blend;
if let Some(BlendState {
equation: eq_alpha,
sfactor: src_alpha,
dfactor: dst_alpha,
}) = alpha_blend
{
glBlendFuncSeparate(
src_rgb.into(),
dst_rgb.into(),
src_alpha.into(),
dst_alpha.into(),
);
glBlendEquationSeparate(eq_rgb.into(), eq_alpha.into());
} else {
glBlendFunc(src_rgb.into(), dst_rgb.into());
glBlendEquationSeparate(eq_rgb.into(), eq_rgb.into());
}
} else if self.cache.color_blend.is_some() {
glDisable(GL_BLEND);
}
}
self.cache.color_blend = color_blend;
self.cache.alpha_blend = alpha_blend;
}
fn set_stencil(&mut self, stencil_test: Option<StencilState>) {
if self.cache.stencil == stencil_test {
return;
}
unsafe {
if let Some(stencil) = stencil_test {
if self.cache.stencil.is_none() {
glEnable(GL_STENCIL_TEST);
}
let front = &stencil.front;
glStencilOpSeparate(
GL_FRONT,
front.fail_op.into(),
front.depth_fail_op.into(),
front.pass_op.into(),
);
glStencilFuncSeparate(
GL_FRONT,
front.test_func.into(),
front.test_ref,
front.test_mask,
);
glStencilMaskSeparate(GL_FRONT, front.write_mask);
let back = &stencil.back;
glStencilOpSeparate(
GL_BACK,
back.fail_op.into(),
back.depth_fail_op.into(),
back.pass_op.into(),
);
glStencilFuncSeparate(
GL_BACK,
back.test_func.into(),
back.test_ref.into(),
back.test_mask,
);
glStencilMaskSeparate(GL_BACK, back.write_mask);
} else if self.cache.stencil.is_some() {
glDisable(GL_STENCIL_TEST);
}
}
self.cache.stencil = stencil_test;
}
fn set_cull_face(&mut self, cull_face: CullFace) {
if self.cache.cull_face == cull_face {
return;
}
match cull_face {
CullFace::Nothing => unsafe {
glDisable(GL_CULL_FACE);
},
CullFace::Front => unsafe {
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
},
CullFace::Back => unsafe {
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
},
}
self.cache.cull_face = cull_face;
}
fn set_color_write(&mut self, color_write: ColorMask) {
if self.cache.color_write == color_write {
return;
}
let (r, g, b, a) = color_write;
unsafe { glColorMask(r as _, g as _, b as _, a as _) }
self.cache.color_write = color_write;
}
}
impl RenderingBackend for GlContext {
fn info(&self) -> ContextInfo {
let version_string = unsafe { glGetString(super::gl::GL_VERSION) };
let gl_version_string = unsafe { std::ffi::CStr::from_ptr(version_string as _) }
.to_str()
.unwrap()
.to_string();
let gles3 = gl_version_string.contains("OpenGL ES 3");
let gles2 = !gles3 && gl_version_string.contains("OpenGL ES");
let mut glsl_support = GlslSupport::default();
// this is not quite documented,
// but somehow even GL2.1 usually have all the compatibility extensions to support glsl100
// It was tested on really old windows machines, virtual machines etc. glsl100 always works!
glsl_support.v100 = true;
// on wasm miniquad always creates webgl1 context, with the only glsl available being version 100
#[cfg(target_arch = "wasm32")]
{
// on web, miniquad always loads EXT_shader_texture_lod and OES_standard_derivatives
glsl_support.v100_ext = true;
}
#[cfg(not(target_arch = "wasm32"))]
{
if gles3 {
glsl_support.v300es = true;
}
}
// there is no gl3.4, so 4+ and 3.3 covers all modern OpenGL
if gl_version_string.starts_with("4") || gl_version_string.starts_with("3.3") {
glsl_support.v330 = true;
} else
// gl 3.0, 3.1, 3.2 maps to 1.30, 1.40, 1.50 glsl versions
if gl_version_string.starts_with("3") {
glsl_support.v130 = true;
}
ContextInfo {
backend: Backend::OpenGl,
gl_version_string,
glsl_support,
features: self.features.clone(),
}
}
fn new_shader(
&mut self,
shader: ShaderSource,
meta: ShaderMeta,
) -> Result<ShaderId, ShaderError> {
let (fragment, vertex) = match shader {
ShaderSource::Glsl { fragment, vertex } => (fragment, vertex),
_ => panic!("Metal source on OpenGl context"),
};
let shader = load_shader_internal(vertex, fragment, meta)?;
self.shaders.push(shader);
Ok(ShaderId(self.shaders.len() - 1))
}
fn new_texture(
&mut self,
access: TextureAccess,
source: TextureSource,
params: TextureParams,
) -> TextureId {
let texture = Texture::new(self, access, source, params);
self.textures.0.push(texture);
TextureId(TextureIdInner::Managed(self.textures.0.len() - 1))
}
fn delete_texture(&mut self, texture: TextureId) {
let t = self.textures.get(texture);
t.delete();
}
fn texture_set_wrap(&mut self, texture: TextureId, wrap_x: TextureWrap, wrap_y: TextureWrap) {
let t = self.textures.get(texture);
self.cache.store_texture_binding(0);
self.cache.bind_texture(0, t.params.kind.into(), t.raw);
let wrap_x = match wrap_x {
TextureWrap::Repeat => GL_REPEAT,
TextureWrap::Mirror => GL_MIRRORED_REPEAT,
TextureWrap::Clamp => GL_CLAMP_TO_EDGE,
};
let wrap_y = match wrap_y {
TextureWrap::Repeat => GL_REPEAT,
TextureWrap::Mirror => GL_MIRRORED_REPEAT,
TextureWrap::Clamp => GL_CLAMP_TO_EDGE,
};
unsafe {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_x as i32);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_x as i32);
}
self.cache.restore_texture_binding(0);
}
fn texture_set_min_filter(
&mut self,
texture: TextureId,
filter: FilterMode,
mipmap_filter: MipmapFilterMode,
) {
let t = self.textures.get(texture);
self.cache.store_texture_binding(0);
self.cache.bind_texture(0, t.params.kind.into(), t.raw);
let filter = Texture::gl_filter(filter, mipmap_filter);
unsafe {
glTexParameteri(t.params.kind.into(), GL_TEXTURE_MIN_FILTER, filter as i32);
}
self.cache.restore_texture_binding(0);
}
fn texture_set_mag_filter(&mut self, texture: TextureId, filter: FilterMode) {
let t = self.textures.get(texture);
self.cache.store_texture_binding(0);
self.cache.bind_texture(0, t.params.kind.into(), t.raw);
let filter = match filter {
FilterMode::Nearest => GL_NEAREST,
FilterMode::Linear => GL_LINEAR,
};
unsafe {
glTexParameteri(t.params.kind.into(), GL_TEXTURE_MAG_FILTER, filter as i32);
}
self.cache.restore_texture_binding(0);
}
fn texture_resize(
&mut self,
texture: TextureId,
width: u32,
height: u32,
source: Option<&[u8]>,
) {
let mut t = self.textures.get(texture);
t.resize(self, width, height, source);
}
fn texture_read_pixels(&mut self, texture: TextureId, source: &mut [u8]) {
let t = self.textures.get(texture);
t.read_pixels(source);
}
fn texture_generate_mipmaps(&mut self, texture: TextureId) {
let t = self.textures.get(texture);
self.cache.store_texture_binding(0);
self.cache.bind_texture(0, t.params.kind.into(), t.raw);
unsafe {
glGenerateMipmap(t.params.kind.into());
}
self.cache.restore_texture_binding(0);
}
fn texture_update_part(
&mut self,
texture: TextureId,
x_offset: i32,
y_offset: i32,
width: i32,
height: i32,
source: &[u8],
) {
let t = self.textures.get(texture);
t.update_texture_part(self, x_offset, y_offset, width, height, source);
}
fn texture_params(&self, texture: TextureId) -> TextureParams {
let texture = self.textures.get(texture);
texture.params
}
unsafe fn texture_raw_id(&self, texture: TextureId) -> RawId {
let texture = self.textures.get(texture);
RawId::OpenGl(texture.raw)
}
fn new_render_pass_mrt(
&mut self,
color_img: &[TextureId],
depth_img: Option<TextureId>,
) -> RenderPass {
if color_img.is_empty() && depth_img.is_none() {
panic!("Render pass should have at least one non-none target");
}
let mut gl_fb = 0;
unsafe {
glGenFramebuffers(1, &mut gl_fb as *mut _);
glBindFramebuffer(GL_FRAMEBUFFER, gl_fb);
for (i, color_img) in color_img.iter().enumerate() {
glFramebufferTexture2D(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0 + i as u32,
GL_TEXTURE_2D,
self.textures.get(*color_img).raw,
0,
);
}
if let Some(depth_img) = depth_img {
glFramebufferTexture2D(
GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
GL_TEXTURE_2D,
self.textures.get(depth_img).raw,
0,
);
}
if color_img.len() > 1 {
let mut attachments = vec![];
for i in 0..color_img.len() {
attachments.push(GL_COLOR_ATTACHMENT0 + i as u32);
}
glDrawBuffers(2, attachments.as_ptr() as _);
}
glBindFramebuffer(GL_FRAMEBUFFER, self.default_framebuffer);
}
let pass = RenderPassInternal {
gl_fb,
color_textures: color_img.to_vec(),
depth_texture: depth_img,
};
self.passes.push(pass);
RenderPass(self.passes.len() - 1)
}
fn render_pass_color_attachments(&self, render_pass: RenderPass) -> &[TextureId] {
&self.passes[render_pass.0].color_textures
}
fn delete_render_pass(&mut self, render_pass: RenderPass) {
let render_pass = &mut self.passes[render_pass.0];
unsafe { glDeleteFramebuffers(1, &mut render_pass.gl_fb as *mut _) }
for color_texture in &render_pass.color_textures {
self.textures.get(*color_texture).delete();
}
if let Some(depth_texture) = render_pass.depth_texture {
self.textures.get(depth_texture).delete();
}
}
fn new_pipeline(
&mut self,
buffer_layout: &[BufferLayout],
attributes: &[VertexAttribute],
shader: ShaderId,
params: PipelineParams,
) -> Pipeline {
#[derive(Clone, Copy, Default)]
struct BufferCacheData {
stride: i32,
offset: i64,
}
let mut buffer_cache: Vec<BufferCacheData> =
vec![BufferCacheData::default(); buffer_layout.len()];
for VertexAttribute {
format,
buffer_index,