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
use gl::types::*;
use crate::{
backend::shader::{DebugUniforms, Uniforms},
BlendMode,
};
fn update_blend_mode(blend_mode: BlendMode) {
unsafe {
// SAFETY:
// `gl::SRC_ALPHA` is a valid `sfactor`
// both `gl::ONE_MINUS_SRC_ALPHA` is a valid `dfactor`
match blend_mode {
BlendMode::Alpha => gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA),
BlendMode::Additive => gl::BlendFunc(gl::SRC_ALPHA, gl::ONE),
}
}
}
/// TODO: in case `update_program` fails, there might not be a current program object, meaning
/// that `glUniform` can error.
#[derive(Debug)]
pub struct OpenGlState {
uniforms: Uniforms,
debug_uniforms: DebugUniforms,
program: GLuint,
vao: GLuint,
target_dimensions: (u32, u32),
viewport_dimensions: (u32, u32),
blend_mode: BlendMode,
depth_active: bool,
depth: f32,
framebuffer: GLuint,
texture: GLuint,
source_scale: (u32, u32),
source_rotation: i32,
color_modulation: [[f32; 4]; 4],
source_texture_dimensions: (u32, u32),
source_texture_offset: (u32, u32),
source_position: (i32, i32),
source_dimensions: (u32, u32),
invert_color: bool,
flip_vertically: bool,
flip_horizontally: bool,
debug_color: (f32, f32, f32, f32),
debug_start_end: (f32, f32, f32, f32),
}
impl OpenGlState {
pub fn new(
uniforms: Uniforms,
debug_uniforms: DebugUniforms,
(program, vao): (GLuint, GLuint),
window_dimensions: (u32, u32),
) -> Self {
unsafe {
// SAFETY: i am the senate
super::update_program(program);
// SAFETY: vao was previously returned from `glGenVertexArrays`.
gl::BindVertexArray(vao);
let target_dimensions = window_dimensions;
// SAFETY: `target_dimensions` is declared as a `vec2`
gl::Uniform2f(
uniforms.target_dimensions,
target_dimensions.0 as f32,
target_dimensions.1 as f32,
);
let viewport_dimensions = window_dimensions;
// SAFETY: both `width` and `height` are positive
gl::Viewport(0, 0, viewport_dimensions.0 as _, viewport_dimensions.1 as _);
let blend_mode = BlendMode::Alpha;
update_blend_mode(blend_mode);
let depth_active = false;
let depth = 0.0;
// SAFETY: `gl::DEPTH_TEST` is a valid `cap`.
gl::Disable(gl::DEPTH_TEST);
// SAFETY: `depth` is declared as a `float`
gl::Uniform1f(uniforms.depth, depth);
let framebuffer = 0;
// SAFETY:
// `gl::FRAMEBUFFER` is a valid target
// `framebuffer` was previously returned from `glGenFramebuffers`
gl::BindFramebuffer(gl::FRAMEBUFFER, framebuffer);
let texture = 0;
// SAFETY:
// `gl::TEXTURE_2D` is a valid target
// `texture` is 0, which represents the default texture (TODO: is this actually true)
gl::BindTexture(gl::TEXTURE_2D, texture);
assert_eq!(gl::NO_ERROR, gl::GetError());
let source_scale = (1, 1);
// SAFETY: `source_scale` is declared as a `uvec2`
gl::Uniform2ui(uniforms.source_scale, source_scale.0, source_scale.1);
let source_rotation = 0;
// An angle of 0 means identity matrix
// SAFETY: `source_rotation` is declared as a `mat2`
let rot_mat: [[f32; 2]; 2] = [[1.0, 0.0], [0.0, 1.0]];
gl::UniformMatrix2fv(
uniforms.source_rotation,
1,
gl::FALSE,
rot_mat.as_ptr().cast::<f32>(),
);
// By default, all uniforms are 0
let color_modulation = [
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
];
let source_texture_dimensions = (128, 128);
// SAFETY: `source_texture_dimensions` is declared as a `vec2`
gl::Uniform2f(
uniforms.source_texture_dimensions,
source_texture_dimensions.0 as f32,
source_texture_dimensions.1 as f32,
);
let source_texture_offset = (0, 0);
// SAFETY: `source_texture_offset` is declared as a `uvec2`
gl::Uniform2ui(
uniforms.source_texture_offset,
source_texture_offset.0,
source_texture_offset.1,
);
let source_position = (0, 0);
// SAFETY: `source_position` is declared as a `vec2`
gl::Uniform2f(
uniforms.source_position,
source_position.0 as f32,
source_position.1 as f32,
);
let source_dimensions = (128, 128);
// SAFETY: `source_dimensions` is declared as a `uvec2`
gl::Uniform2ui(
uniforms.source_dimensions,
source_dimensions.0,
source_dimensions.1,
);
let invert_color = false;
// SAFETY: `invert_color` is declared as a `bool`
gl::Uniform1ui(uniforms.invert_color, invert_color as _);
let flip_vertically = false;
// SAFETY: `flip_vertically` is declared as a `bool`
gl::Uniform1ui(uniforms.flip_vertically, flip_vertically as _);
let flip_horizontally = false;
// SAFETY: `flip_horizontally` is declared as a `bool`
gl::Uniform1ui(uniforms.flip_horizontally, flip_horizontally as _);
Self {
uniforms,
vao,
debug_uniforms,
program,
target_dimensions,
viewport_dimensions,
blend_mode,
depth_active,
depth,
framebuffer,
texture,
source_scale,
source_rotation,
color_modulation,
source_texture_dimensions,
source_texture_offset,
source_position,
source_dimensions,
invert_color,
flip_vertically,
flip_horizontally,
// set `debug_color` and `debug_start_end` to the default value
debug_color: (0.0, 0.0, 0.0, 0.0),
debug_start_end: (0.0, 0.0, 0.0, 0.0),
}
}
}
pub fn update_program(&mut self, program: GLuint) {
if program != self.program {
self.program = program;
unsafe {
// SAFETY: i am the senate
super::update_program(self.program)
}
}
}
pub fn update_vao(&mut self, vao: GLuint) {
if vao != self.vao {
self.vao = vao;
unsafe {
// SAFETY: vao was previously returned from `glGenVertexArrays`.
gl::BindVertexArray(vao);
}
}
}
pub fn update_target_dimensions(&mut self, target_dimensions: (u32, u32)) {
if target_dimensions != self.target_dimensions {
self.target_dimensions = target_dimensions;
unsafe {
// SAFETY:
// TODO: in case `update_program` fails, there might not be a current program object.
// `uniforms.target_dimensions` is declared as a `vec2`
gl::Uniform2f(
self.uniforms.target_dimensions,
self.target_dimensions.0 as f32,
self.target_dimensions.1 as f32,
);
}
}
}
pub fn update_viewport_dimensions(&mut self, viewport_dimensions: (u32, u32)) {
if viewport_dimensions != self.viewport_dimensions {
self.viewport_dimensions = viewport_dimensions;
unsafe {
// SAFETY: both `width` and `height` are positive
gl::Viewport(0, 0, viewport_dimensions.0 as _, viewport_dimensions.1 as _);
}
}
}
pub fn update_blend_mode(&mut self, blend_mode: BlendMode) {
if blend_mode != self.blend_mode {
self.blend_mode = blend_mode;
update_blend_mode(self.blend_mode);
}
}
pub fn disable_depth(&mut self) {
if self.depth_active {
self.depth_active = false;
unsafe {
// SAFETY: `gl::DEPTH_TEST` is a valid `cap`.
gl::Disable(gl::DEPTH_TEST);
}
}
}
// we want to use the precise depth in the shader,
// so checking for equality should be fine here.
#[allow(clippy::float_cmp)]
pub fn update_depth(&mut self, depth: Option<f32>) {
if let Some(depth) = depth {
unsafe {
if !self.depth_active {
self.depth_active = true;
// SAFETY: `gl::DEPTH_TEST` is a valid `cap`.
gl::Enable(gl::DEPTH_TEST);
}
if depth != self.depth {
self.depth = depth;
// SAFETY: `depth` is declared as a `float`
gl::Uniform1f(self.uniforms.depth, self.depth);
}
}
} else {
self.disable_depth()
}
}
pub fn update_framebuffer(&mut self, framebuffer: GLuint) {
if framebuffer != self.framebuffer {
self.framebuffer = framebuffer;
unsafe {
// SAFETY:
// `gl::FRAMEBUFFER` is a valid target
// `framebuffer` was previously returned from `glGenFramebuffers`
gl::BindFramebuffer(gl::FRAMEBUFFER, self.framebuffer);
}
}
}
pub fn update_texture(&mut self, texture: GLuint) {
if texture != self.texture {
self.texture = texture;
unsafe {
// SAFETY:
// `gl::TEXTURE_2D` is a valid target
// `self.texture` was created using `glGenTexture`
// and is only ever bound to `gl::TEXTURE_2D`
gl::BindTexture(gl::TEXTURE_2D, self.texture);
}
}
}
pub fn update_source_scale(&mut self, source_scale: (u32, u32)) {
if source_scale != self.source_scale {
self.source_scale = source_scale;
unsafe {
// SAFETY: `source_scale` is declared as a `uvec2`
gl::Uniform2ui(
self.uniforms.source_scale,
self.source_scale.0,
self.source_scale.1,
);
}
}
}
pub fn update_source_rotation(&mut self, source_rotation: i32) {
if source_rotation != self.source_rotation {
// Build rotation matrices
let angle = (source_rotation as f32).to_radians();
let rot_mat: [[f32; 2]; 2] = [[angle.cos(), -angle.sin()], [angle.sin(), angle.cos()]];
self.source_rotation = source_rotation;
unsafe {
gl::UniformMatrix2fv(
self.uniforms.source_rotation,
1,
gl::FALSE,
rot_mat.as_ptr().cast(),
);
}
}
}
pub fn update_color_modulation(&mut self, color_modulation: [[f32; 4]; 4]) {
if color_modulation != self.color_modulation {
self.color_modulation = color_modulation;
let color_modulation: *const _ = &self.color_modulation;
unsafe {
// SAFETY:
// `color_modulation` is declared as a `mat4`
// `self.color_modulation` is an array of 16 `GLfloat`.
gl::UniformMatrix4fv(
self.uniforms.color_modulation,
1,
gl::TRUE,
color_modulation.cast(),
)
}
}
}
pub fn update_source_texture_dimensions(&mut self, source_texture_dimensions: (u32, u32)) {
if source_texture_dimensions != self.source_texture_dimensions {
self.source_texture_dimensions = source_texture_dimensions;
unsafe {
// SAFETY: `source_texture_dimensions` is declared as a `vec2`
gl::Uniform2f(
self.uniforms.source_texture_dimensions,
self.source_texture_dimensions.0 as f32,
self.source_texture_dimensions.1 as f32,
);
}
}
}
pub fn update_source_texture_offset(&mut self, source_texture_offset: (u32, u32)) {
if source_texture_offset != self.source_texture_offset {
self.source_texture_offset = source_texture_offset;
unsafe {
// SAFETY: `source_texture_offset` is declared as a `uvec2`
gl::Uniform2ui(
self.uniforms.source_texture_offset,
self.source_texture_offset.0,
self.source_texture_offset.1,
);
}
}
}
pub fn update_source_position(&mut self, source_position: (i32, i32)) {
if source_position != self.source_position {
self.source_position = source_position;
unsafe {
// SAFETY: `source_position` is declared as a `vec2`
gl::Uniform2f(
self.uniforms.source_position,
self.source_position.0 as f32,
self.source_position.1 as f32,
);
}
}
}
pub fn update_source_dimensions(&mut self, source_dimensions: (u32, u32)) {
if source_dimensions != self.source_dimensions {
self.source_dimensions = source_dimensions;
unsafe {
// SAFETY: `source_dimensions` is declared as a `uvec2`
gl::Uniform2ui(
self.uniforms.source_dimensions,
self.source_dimensions.0,
self.source_dimensions.1,
);
}
}
}
pub fn update_invert_color(&mut self, invert_color: bool) {
if invert_color != self.invert_color {
self.invert_color = invert_color;
unsafe {
// SAFETY: `invert_color` is declared as a `bool`
gl::Uniform1ui(self.uniforms.invert_color, self.invert_color as _);
}
}
}
pub fn update_flip_vertically(&mut self, flip_vertically: bool) {
if flip_vertically != self.flip_vertically {
self.flip_vertically = flip_vertically;
unsafe {
// SAFETY: `flip_vertically` is declared as a `bool`
gl::Uniform1ui(self.uniforms.flip_vertically, self.flip_vertically as _);
}
}
}
pub fn update_flip_horizontally(&mut self, flip_horizontally: bool) {
if flip_horizontally != self.flip_horizontally {
self.flip_horizontally = flip_horizontally;
unsafe {
// SAFETY: `flip_horizontally` is declared as a `bool`
gl::Uniform1ui(self.uniforms.flip_horizontally, self.flip_horizontally as _);
}
}
}
pub fn update_debug_color(&mut self, debug_color: (f32, f32, f32, f32)) {
if debug_color != self.debug_color {
self.debug_color = debug_color;
unsafe {
// SAFETY: `line_color` is declared as `vec4`
gl::Uniform4f(
self.debug_uniforms.line_color,
debug_color.0,
debug_color.1,
debug_color.2,
debug_color.3,
);
}
}
}
pub fn update_debug_start_end(&mut self, debug_start_end: (f32, f32, f32, f32)) {
if debug_start_end != self.debug_start_end {
self.debug_start_end = debug_start_end;
}
unsafe {
// SAFETY: `start_end` is declared as `vec4`
gl::Uniform4f(
self.debug_uniforms.start_end,
debug_start_end.0,
debug_start_end.1,
debug_start_end.2,
debug_start_end.3,
);
}
}
}