forked from servo/surfman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads.rs
1002 lines (917 loc) · 32.4 KB
/
threads.rs
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
// surfman/examples/threads.rs
//
// This example demonstrates how to create a multithreaded OpenGL application using `surfman`.
#![cfg(feature = "sm-raw-window-handle-06")]
use self::common::{ck, Buffer, Program, ResourceLoader, Shader, ShaderKind};
use euclid::default::{Point2D, Rect, Size2D, Vector2D};
use gl::types::{GLchar, GLenum, GLint, GLuint, GLvoid};
use std::sync::mpsc::{self, Receiver, Sender};
use std::thread;
use surfman::{declare_surfman, SurfaceAccess, SurfaceTexture, SurfaceType};
use surfman::{Adapter, Connection, Context, ContextDescriptor, Device, GLApi, Surface};
#[cfg(not(target_os = "android"))]
use self::common::FilesystemResourceLoader;
#[cfg(not(target_os = "android"))]
use surfman::{ContextAttributeFlags, ContextAttributes, GLVersion};
#[cfg(not(target_os = "android"))]
use winit::{
dpi::PhysicalSize,
event::{DeviceEvent, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use rwh_06::{HasDisplayHandle, HasWindowHandle};
pub mod common;
declare_surfman!();
const WINDOW_WIDTH: i32 = 800;
const WINDOW_HEIGHT: i32 = 600;
const SUBSCREEN_WIDTH: i32 = 256;
const SUBSCREEN_HEIGHT: i32 = 256;
const BALL_WIDTH: i32 = 192;
const BALL_HEIGHT: i32 = 192;
const CHECK_SIZE: f32 = 16.0;
const INITIAL_VELOCITY_X: f32 = 1.5;
const INITIAL_VELOCITY_Y: f32 = 0.0;
const GRAVITY: f32 = -0.2;
const INITIAL_ROTATION_X: f32 = 0.2;
const INITIAL_ROTATION_Y: f32 = 0.6;
const INITIAL_ROTATION_Z: f32 = 0.2;
const ROTATION_SPEED_X: f32 = 0.03;
const ROTATION_SPEED_Y: f32 = 0.01;
const ROTATION_SPEED_Z: f32 = 0.05;
const SPHERE_RADIUS: f32 = 96.0;
static QUAD_VERTEX_POSITIONS: [u8; 8] = [0, 0, 1, 0, 0, 1, 1, 1];
static CHECK_TRANSFORM: [f32; 4] = [
SUBSCREEN_WIDTH as f32 / CHECK_SIZE as f32,
0.0,
0.0,
SUBSCREEN_HEIGHT as f32 / CHECK_SIZE as f32,
];
static IDENTITY_TRANSFORM: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
static ZERO_TRANSLATION: [f32; 2] = [0.0, 0.0];
static NDC_TRANSFORM: [f32; 4] = [2.0, 0.0, 0.0, 2.0];
static NDC_TRANSLATION: [f32; 2] = [-1.0, -1.0];
static CHECK_COLOR_A: [f32; 4] = [0.8, 0.0, 0.0, 1.0];
static CHECK_COLOR_B: [f32; 4] = [0.9, 0.9, 0.9, 1.0];
static CAMERA_POSITION: [f32; 3] = [400.0, 300.0, -1000.0];
static LIGHT_POSITION: [f32; 3] = [600.0, 450.0, -500.0];
static GRIDLINE_COLOR: [f32; 4] = [
(0x9e as f32) / 255.0,
(0x2b as f32) / 255.0,
(0x86 as f32) / 255.0,
1.0,
];
static BACKGROUND_COLOR: [f32; 4] = [
(0xaa as f32) / 255.0,
(0xaa as f32) / 255.0,
(0xaa as f32) / 255.0,
1.0,
];
fn make_connection(window: &winit::window::Window) -> surfman::Connection {
let display_handle = window
.display_handle()
.expect("failed to get display handle from window");
let connection = Connection::from_display_handle(display_handle).unwrap();
connection
}
fn make_native_widget(
window: &winit::window::Window,
connection: &surfman::Connection,
window_size: Size2D<i32>,
) -> surfman::NativeWidget {
let raw_window_handle = window
.window_handle()
.expect("couldn't get window handle from window");
let native_widget = connection
.create_native_widget_from_window_handle(raw_window_handle, window_size)
.unwrap();
native_widget
}
#[cfg(not(target_os = "android"))]
fn main() {
use winit::{
event::RawKeyEvent,
keyboard::{KeyCode, PhysicalKey},
};
let event_loop = EventLoop::new().expect("couldn't create eventloop");
let window_size = Size2D::new(WINDOW_WIDTH, WINDOW_HEIGHT);
let physical_size = PhysicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT);
let window = WindowBuilder::new()
.with_title("Multithreaded example")
.with_inner_size(physical_size)
.build(&event_loop)
.unwrap();
window.set_visible(true);
let connection = make_connection(&window);
let window_size = window.inner_size();
let window_size = Size2D::new(window_size.width as i32, window_size.height as i32);
let native_widget = make_native_widget(&window, &connection, window_size);
let adapter = connection.create_low_power_adapter().unwrap();
let mut device = connection.create_device(&adapter).unwrap();
let context_attributes = ContextAttributes {
version: GLVersion::new(3, 0),
flags: ContextAttributeFlags::ALPHA,
};
let context_descriptor = device
.create_context_descriptor(&context_attributes)
.unwrap();
let surface_type = SurfaceType::Widget { native_widget };
let mut context = device.create_context(&context_descriptor, None).unwrap();
let surface = device
.create_surface(&context, SurfaceAccess::GPUOnly, surface_type)
.unwrap();
device
.bind_surface_to_context(&mut context, surface)
.unwrap();
device.make_context_current(&context).unwrap();
let mut app = App::new(
connection,
adapter,
device,
context,
Box::new(FilesystemResourceLoader),
window_size,
);
event_loop
.run(move |event, target| match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
}
| Event::DeviceEvent {
event:
DeviceEvent::Key(RawKeyEvent {
physical_key: PhysicalKey::Code(KeyCode::Escape),
..
}),
..
} => target.exit(),
_ => {
app.tick(true);
target.set_control_flow(ControlFlow::Poll)
}
})
.expect("failed to run event loop");
}
pub struct App {
main_from_worker_receiver: Receiver<Frame>,
main_to_worker_sender: Sender<Surface>,
grid_vertex_array: GridVertexArray,
blit_vertex_array: BlitVertexArray,
device: Device,
context: Context,
texture: Option<SurfaceTexture>,
frame: Frame,
window_size: Size2D<i32>,
}
impl Drop for App {
fn drop(&mut self) {
self.device.destroy_context(&mut self.context).unwrap();
}
}
impl App {
pub fn new(
connection: Connection,
adapter: Adapter,
device: Device,
mut context: Context,
resource_loader: Box<dyn ResourceLoader + Send>,
window_size: Size2D<i32>,
) -> App {
let context_descriptor = device.context_descriptor(&context);
gl::load_with(|symbol_name| device.get_proc_address(&context, symbol_name));
// Set up GL objects and state.
let gl_api = device.gl_api();
let surface_gl_texture_target = device.surface_gl_texture_target();
let grid_vertex_array =
GridVertexArray::new(gl_api, surface_gl_texture_target, &*resource_loader);
let blit_vertex_array =
BlitVertexArray::new(gl_api, surface_gl_texture_target, &*resource_loader);
// Set up communication channels, and spawn our worker thread.
let (worker_to_main_sender, main_from_worker_receiver) = mpsc::channel();
let (main_to_worker_sender, worker_from_main_receiver) = mpsc::channel();
thread::spawn(move || {
worker_thread(
connection,
adapter,
context_descriptor,
window_size,
resource_loader,
worker_to_main_sender,
worker_from_main_receiver,
)
});
// Fetch our initial surface.
let mut frame = match main_from_worker_receiver.recv() {
Err(_) => panic!(),
Ok(frame) => frame,
};
let texture = Some(
device
.create_surface_texture(&mut context, frame.surface.take().unwrap())
.unwrap(),
);
App {
main_from_worker_receiver,
main_to_worker_sender,
grid_vertex_array,
blit_vertex_array,
device,
texture,
frame,
context,
window_size,
}
}
pub fn tick(&mut self, present: bool) {
// Send back our old surface.
let surface = self
.device
.destroy_surface_texture(&mut self.context, self.texture.take().unwrap())
.unwrap();
self.main_to_worker_sender.send(surface).unwrap();
// Fetch a new frame.
self.frame = self.main_from_worker_receiver.recv().unwrap();
// Wrap it in a texture.
self.texture = Some(
self.device
.create_surface_texture(&mut self.context, self.frame.surface.take().unwrap())
.unwrap(),
);
unsafe {
self.device.make_context_current(&self.context).unwrap();
let framebuffer_object = match self.device.context_surface_info(&self.context) {
Ok(Some(surface_info)) => surface_info.framebuffer_object,
_ => 0,
};
gl::BindFramebuffer(gl::FRAMEBUFFER, framebuffer_object);
gl::Viewport(0, 0, self.window_size.width, self.window_size.height);
gl::ClearColor(0.0, 0.0, 1.0, 1.0);
ck();
gl::Clear(gl::COLOR_BUFFER_BIT);
ck();
// Draw gridlines.
gl::BindVertexArray(self.grid_vertex_array.object);
ck();
gl::UseProgram(self.grid_vertex_array.grid_program.program.object);
ck();
gl::UniformMatrix2fv(
self.grid_vertex_array.grid_program.transform_uniform,
1,
gl::FALSE,
NDC_TRANSFORM.as_ptr(),
);
gl::Uniform2fv(
self.grid_vertex_array.grid_program.translation_uniform,
1,
NDC_TRANSLATION.as_ptr(),
);
gl::UniformMatrix2fv(
self.grid_vertex_array.grid_program.tex_transform_uniform,
1,
gl::FALSE,
CHECK_TRANSFORM.as_ptr(),
);
gl::Uniform2fv(
self.grid_vertex_array.grid_program.tex_translation_uniform,
1,
ZERO_TRANSLATION.as_ptr(),
);
gl::Uniform4fv(
self.grid_vertex_array.grid_program.gridline_color_uniform,
1,
GRIDLINE_COLOR.as_ptr(),
);
gl::Uniform4fv(
self.grid_vertex_array.grid_program.bg_color_uniform,
1,
BACKGROUND_COLOR.as_ptr(),
);
gl::Uniform2f(
self.grid_vertex_array.grid_program.sphere_position_uniform,
self.frame.sphere_position.x,
self.frame.sphere_position.y,
);
gl::Uniform1f(
self.grid_vertex_array.grid_program.radius_uniform,
SPHERE_RADIUS,
);
gl::Uniform3fv(
self.grid_vertex_array.grid_program.camera_position_uniform,
1,
CAMERA_POSITION.as_ptr(),
);
gl::Uniform3fv(
self.grid_vertex_array.grid_program.light_position_uniform,
1,
LIGHT_POSITION.as_ptr(),
);
gl::DrawArrays(gl::TRIANGLE_STRIP, 0, 4);
ck();
// Draw subscreen.
let blit_transform: [f32; 4] = [
SUBSCREEN_WIDTH as f32 / self.window_size.width as f32 * 2.0,
0.0,
0.0,
SUBSCREEN_HEIGHT as f32 / self.window_size.height as f32 * 2.0,
];
let subscreen_translation = Point2D::new(
self.frame.viewport_origin.x / self.window_size.width as f32 * 2.0 - 1.0,
self.frame.viewport_origin.y / self.window_size.height as f32 * 2.0 - 1.0,
);
gl::BindVertexArray(self.blit_vertex_array.object);
ck();
gl::UseProgram(self.blit_vertex_array.blit_program.program.object);
ck();
gl::UniformMatrix2fv(
self.blit_vertex_array.blit_program.transform_uniform,
1,
gl::FALSE,
blit_transform.as_ptr(),
);
gl::Uniform2fv(
self.blit_vertex_array.blit_program.translation_uniform,
1,
[subscreen_translation.x, subscreen_translation.y].as_ptr(),
);
gl::UniformMatrix2fv(
self.blit_vertex_array.blit_program.tex_transform_uniform,
1,
gl::FALSE,
IDENTITY_TRANSFORM.as_ptr(),
);
gl::Uniform2fv(
self.blit_vertex_array.blit_program.tex_translation_uniform,
1,
ZERO_TRANSLATION.as_ptr(),
);
gl::ActiveTexture(gl::TEXTURE0);
ck();
gl::BindTexture(
self.device.surface_gl_texture_target(),
self.device
.surface_texture_object(self.texture.as_ref().unwrap()),
);
gl::Uniform1i(self.blit_vertex_array.blit_program.source_uniform, 0);
ck();
gl::Enable(gl::BLEND);
gl::BlendEquation(gl::FUNC_ADD);
gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
gl::DrawArrays(gl::TRIANGLE_STRIP, 0, 4);
ck();
gl::Disable(gl::BLEND);
}
if present {
let mut surface = self
.device
.unbind_surface_from_context(&mut self.context)
.unwrap()
.unwrap();
self.device
.present_surface(&mut self.context, &mut surface)
.unwrap();
self.device
.bind_surface_to_context(&mut self.context, surface)
.unwrap();
}
}
}
fn worker_thread(
connection: Connection,
adapter: Adapter,
context_descriptor: ContextDescriptor,
window_size: Size2D<i32>,
resource_loader: Box<dyn ResourceLoader>,
worker_to_main_sender: Sender<Frame>,
worker_from_main_receiver: Receiver<Surface>,
) {
// Open the device, create a context, and make it current.
let size = Size2D::new(SUBSCREEN_WIDTH, SUBSCREEN_HEIGHT);
let surface_type = SurfaceType::Generic { size };
let mut device = connection.create_device(&adapter).unwrap();
let mut context = device.create_context(&context_descriptor, None).unwrap();
let surface = device
.create_surface(&context, SurfaceAccess::GPUOnly, surface_type)
.unwrap();
device
.bind_surface_to_context(&mut context, surface)
.unwrap();
device.make_context_current(&context).unwrap();
// Set up GL objects and state.
let vertex_array = CheckVertexArray::new(
device.gl_api(),
device.surface_gl_texture_target(),
&*resource_loader,
);
// Initialize our origin and size.
let ball_origin = Point2D::new(
window_size.width as f32 * 0.5 - BALL_WIDTH as f32 * 0.5,
window_size.height as f32 * 0.65 - BALL_HEIGHT as f32 * 0.5,
);
let ball_size = Size2D::new(BALL_WIDTH as f32, BALL_HEIGHT as f32);
let mut ball_rect = Rect::new(ball_origin, ball_size);
let mut ball_velocity = Vector2D::new(INITIAL_VELOCITY_X, INITIAL_VELOCITY_Y);
let subscreen_offset = (Point2D::new(SUBSCREEN_WIDTH as f32, SUBSCREEN_HEIGHT as f32)
- Point2D::new(BALL_WIDTH as f32, BALL_HEIGHT as f32))
* 0.5;
// Initialize our rotation.
let mut theta_x = INITIAL_ROTATION_X;
let mut theta_y = INITIAL_ROTATION_Y;
let mut theta_z = INITIAL_ROTATION_Z;
// Send an initial surface back to the main thread.
let surface_type = SurfaceType::Generic { size };
let surface = Some(
device
.create_surface(&context, SurfaceAccess::GPUOnly, surface_type)
.unwrap(),
);
worker_to_main_sender
.send(Frame {
surface,
viewport_origin: ball_rect.origin - subscreen_offset,
sphere_position: ball_rect.center(),
})
.unwrap();
loop {
// Render to the surface.
unsafe {
let framebuffer_object = device
.context_surface_info(&context)
.unwrap()
.unwrap()
.framebuffer_object;
gl::BindFramebuffer(gl::FRAMEBUFFER, framebuffer_object);
gl::Viewport(0, 0, size.width, size.height);
gl::ClearColor(0.0, 0.0, 0.0, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
gl::BindVertexArray(vertex_array.object);
ck();
gl::UseProgram(vertex_array.check_program.program.object);
ck();
gl::UniformMatrix2fv(
vertex_array.check_program.transform_uniform,
1,
gl::FALSE,
NDC_TRANSFORM.as_ptr(),
);
gl::Uniform2fv(
vertex_array.check_program.translation_uniform,
1,
NDC_TRANSLATION.as_ptr(),
);
gl::UniformMatrix2fv(
vertex_array.check_program.tex_transform_uniform,
1,
gl::FALSE,
NDC_TRANSFORM.as_ptr(),
);
gl::Uniform2fv(
vertex_array.check_program.tex_translation_uniform,
1,
NDC_TRANSLATION.as_ptr(),
);
gl::Uniform3fv(
vertex_array.check_program.rotation_uniform,
1,
[theta_x, theta_y, theta_z].as_ptr(),
);
gl::Uniform4fv(
vertex_array.check_program.color_a_uniform,
1,
CHECK_COLOR_A.as_ptr(),
);
gl::Uniform4fv(
vertex_array.check_program.color_b_uniform,
1,
CHECK_COLOR_B.as_ptr(),
);
gl::Uniform2f(
vertex_array.check_program.viewport_origin_uniform,
ball_rect.origin.x - subscreen_offset.x,
ball_rect.origin.y - subscreen_offset.y,
);
gl::Uniform1f(vertex_array.check_program.radius_uniform, SPHERE_RADIUS);
gl::Uniform3fv(
vertex_array.check_program.camera_position_uniform,
1,
CAMERA_POSITION.as_ptr(),
);
gl::Uniform3fv(
vertex_array.check_program.light_position_uniform,
1,
LIGHT_POSITION.as_ptr(),
);
gl::Uniform2f(
vertex_array.check_program.sphere_position_uniform,
ball_rect.center().x,
ball_rect.center().y,
);
gl::DrawArrays(gl::TRIANGLE_STRIP, 0, 4);
ck();
}
let old_surface = device.unbind_surface_from_context(&mut context).unwrap();
let new_surface = match worker_from_main_receiver.recv() {
Ok(surface) => surface,
Err(_) => break,
};
device
.bind_surface_to_context(&mut context, new_surface)
.unwrap();
worker_to_main_sender
.send(Frame {
surface: old_surface,
viewport_origin: ball_rect.origin - subscreen_offset,
sphere_position: ball_rect.center(),
})
.unwrap();
// Advance ball.
ball_velocity += Vector2D::new(0.0, GRAVITY);
ball_rect = ball_rect.translate(ball_velocity);
// Bounce off edges.
if ball_rect.origin.y <= 0.0 {
ball_rect.origin.y = 0.0;
ball_velocity.y = f32::abs(ball_velocity.y);
}
if ball_rect.origin.x <= 0.0 {
ball_rect.origin.x = 0.0;
ball_velocity.x = f32::abs(ball_velocity.x);
}
if ball_rect.max_x() >= window_size.width as f32 {
ball_rect.origin.x = (window_size.width - BALL_WIDTH) as f32;
ball_velocity.x = -f32::abs(ball_velocity.x);
}
// Rotate.
theta_x += ROTATION_SPEED_X;
theta_y += ROTATION_SPEED_Y;
theta_z += ROTATION_SPEED_Z;
}
device.destroy_context(&mut context).unwrap();
}
struct Frame {
surface: Option<Surface>,
viewport_origin: Point2D<f32>,
sphere_position: Point2D<f32>,
}
struct BlitVertexArray {
object: GLuint,
blit_program: BlitProgram,
#[allow(dead_code)]
position_buffer: Buffer,
}
impl BlitVertexArray {
fn new(
gl_api: GLApi,
gl_texture_target: GLenum,
resource_loader: &dyn ResourceLoader,
) -> BlitVertexArray {
let blit_program = BlitProgram::new(gl_api, gl_texture_target, resource_loader);
unsafe {
let mut vertex_array = 0;
gl::GenVertexArrays(1, &mut vertex_array);
ck();
gl::BindVertexArray(vertex_array);
ck();
let position_buffer = Buffer::from_data(&QUAD_VERTEX_POSITIONS);
gl::BindBuffer(gl::ARRAY_BUFFER, position_buffer.object);
ck();
gl::VertexAttribPointer(
blit_program.position_attribute as GLuint,
2,
gl::UNSIGNED_BYTE,
gl::FALSE,
2,
0 as *const GLvoid,
);
ck();
gl::EnableVertexAttribArray(blit_program.position_attribute as GLuint);
ck();
BlitVertexArray {
object: vertex_array,
blit_program,
position_buffer,
}
}
}
}
struct GridVertexArray {
object: GLuint,
grid_program: GridProgram,
#[allow(dead_code)]
position_buffer: Buffer,
}
impl GridVertexArray {
fn new(
gl_api: GLApi,
gl_texture_target: GLenum,
resource_loader: &dyn ResourceLoader,
) -> GridVertexArray {
let grid_program = GridProgram::new(gl_api, gl_texture_target, resource_loader);
unsafe {
let mut vertex_array = 0;
gl::GenVertexArrays(1, &mut vertex_array);
ck();
gl::BindVertexArray(vertex_array);
ck();
let position_buffer = Buffer::from_data(&QUAD_VERTEX_POSITIONS);
gl::BindBuffer(gl::ARRAY_BUFFER, position_buffer.object);
ck();
gl::VertexAttribPointer(
grid_program.position_attribute as GLuint,
2,
gl::UNSIGNED_BYTE,
gl::FALSE,
2,
0 as *const GLvoid,
);
ck();
gl::EnableVertexAttribArray(grid_program.position_attribute as GLuint);
ck();
GridVertexArray {
object: vertex_array,
grid_program,
position_buffer,
}
}
}
}
struct CheckVertexArray {
object: GLuint,
check_program: CheckProgram,
#[allow(dead_code)]
position_buffer: Buffer,
}
impl CheckVertexArray {
fn new(
gl_api: GLApi,
gl_texture_target: GLenum,
resource_loader: &dyn ResourceLoader,
) -> CheckVertexArray {
let check_program = CheckProgram::new(gl_api, gl_texture_target, resource_loader);
unsafe {
let mut vertex_array = 0;
gl::GenVertexArrays(1, &mut vertex_array);
ck();
gl::BindVertexArray(vertex_array);
ck();
let position_buffer = Buffer::from_data(&QUAD_VERTEX_POSITIONS);
gl::BindBuffer(gl::ARRAY_BUFFER, position_buffer.object);
ck();
gl::VertexAttribPointer(
check_program.position_attribute as GLuint,
2,
gl::UNSIGNED_BYTE,
gl::FALSE,
2,
0 as *const GLvoid,
);
ck();
gl::EnableVertexAttribArray(check_program.position_attribute as GLuint);
ck();
CheckVertexArray {
object: vertex_array,
check_program,
position_buffer,
}
}
}
}
struct BlitProgram {
program: Program,
position_attribute: GLint,
transform_uniform: GLint,
translation_uniform: GLint,
tex_transform_uniform: GLint,
tex_translation_uniform: GLint,
source_uniform: GLint,
}
impl BlitProgram {
fn new(
gl_api: GLApi,
gl_texture_target: GLenum,
resource_loader: &dyn ResourceLoader,
) -> BlitProgram {
let vertex_shader = Shader::new(
"quad",
ShaderKind::Vertex,
gl_api,
gl_texture_target,
resource_loader,
);
let fragment_shader = Shader::new(
"blit",
ShaderKind::Fragment,
gl_api,
gl_texture_target,
resource_loader,
);
let program = Program::new(vertex_shader, fragment_shader);
unsafe {
let position_attribute =
gl::GetAttribLocation(program.object, c"aPosition".as_ptr().cast());
ck();
let transform_uniform =
gl::GetUniformLocation(program.object, c"uTransform".as_ptr().cast());
ck();
let translation_uniform =
gl::GetUniformLocation(program.object, c"uTranslation".as_ptr().cast());
ck();
let tex_transform_uniform =
gl::GetUniformLocation(program.object, c"uTexTransform".as_ptr().cast());
ck();
let tex_translation_uniform =
gl::GetUniformLocation(program.object, c"uTexTranslation".as_ptr().cast());
ck();
let source_uniform = gl::GetUniformLocation(program.object, c"uSource".as_ptr().cast());
ck();
BlitProgram {
program,
position_attribute,
transform_uniform,
translation_uniform,
tex_transform_uniform,
tex_translation_uniform,
source_uniform,
}
}
}
}
struct GridProgram {
program: Program,
position_attribute: GLint,
transform_uniform: GLint,
translation_uniform: GLint,
tex_transform_uniform: GLint,
tex_translation_uniform: GLint,
gridline_color_uniform: GLint,
bg_color_uniform: GLint,
radius_uniform: GLint,
sphere_position_uniform: GLint,
camera_position_uniform: GLint,
light_position_uniform: GLint,
}
impl GridProgram {
fn new(
gl_api: GLApi,
gl_texture_target: GLenum,
resource_loader: &dyn ResourceLoader,
) -> GridProgram {
let vertex_shader = Shader::new(
"quad",
ShaderKind::Vertex,
gl_api,
gl_texture_target,
resource_loader,
);
let fragment_shader = Shader::new(
"grid",
ShaderKind::Fragment,
gl_api,
gl_texture_target,
resource_loader,
);
let program = Program::new(vertex_shader, fragment_shader);
unsafe {
let position_attribute =
gl::GetAttribLocation(program.object, c"aPosition".as_ptr().cast());
ck();
let transform_uniform =
gl::GetUniformLocation(program.object, c"uTransform".as_ptr().cast());
ck();
let translation_uniform =
gl::GetUniformLocation(program.object, c"uTranslation".as_ptr().cast());
ck();
let tex_transform_uniform =
gl::GetUniformLocation(program.object, c"uTexTransform".as_ptr().cast());
ck();
let tex_translation_uniform =
gl::GetUniformLocation(program.object, c"uTexTranslation".as_ptr().cast());
ck();
let gridline_color_uniform =
gl::GetUniformLocation(program.object, c"uGridlineColor".as_ptr().cast());
ck();
let bg_color_uniform =
gl::GetUniformLocation(program.object, c"uBGColor".as_ptr().cast());
ck();
let radius_uniform = gl::GetUniformLocation(program.object, c"uRadius".as_ptr().cast());
ck();
let camera_position_uniform =
gl::GetUniformLocation(program.object, c"uCameraPosition".as_ptr().cast());
ck();
let light_position_uniform =
gl::GetUniformLocation(program.object, c"uLightPosition".as_ptr().cast());
ck();
let sphere_position_uniform =
gl::GetUniformLocation(program.object, c"uSpherePosition".as_ptr().cast());
ck();
GridProgram {
program,
position_attribute,
transform_uniform,
translation_uniform,
tex_transform_uniform,
tex_translation_uniform,
gridline_color_uniform,
bg_color_uniform,
radius_uniform,
camera_position_uniform,
light_position_uniform,
sphere_position_uniform,
}
}
}
}
struct CheckProgram {
program: Program,
position_attribute: GLint,
transform_uniform: GLint,
translation_uniform: GLint,
tex_transform_uniform: GLint,
tex_translation_uniform: GLint,
rotation_uniform: GLint,
color_a_uniform: GLint,
color_b_uniform: GLint,
viewport_origin_uniform: GLint,
radius_uniform: GLint,
camera_position_uniform: GLint,
light_position_uniform: GLint,
sphere_position_uniform: GLint,
}
impl CheckProgram {
fn new(
gl_api: GLApi,
gl_texture_target: GLenum,
resource_loader: &dyn ResourceLoader,
) -> CheckProgram {
let vertex_shader = Shader::new(
"quad",
ShaderKind::Vertex,
gl_api,
gl_texture_target,
resource_loader,
);
let fragment_shader = Shader::new(
"check",
ShaderKind::Fragment,
gl_api,
gl_texture_target,
resource_loader,
);
let program = Program::new(vertex_shader, fragment_shader);
unsafe {
let position_attribute =
gl::GetAttribLocation(program.object, c"aPosition".as_ptr().cast());
ck();
let transform_uniform =
gl::GetUniformLocation(program.object, c"uTransform".as_ptr().cast());
ck();
let translation_uniform =
gl::GetUniformLocation(program.object, c"uTranslation".as_ptr().cast());
ck();
let tex_transform_uniform =
gl::GetUniformLocation(program.object, c"uTexTransform".as_ptr().cast());
ck();
let tex_translation_uniform =
gl::GetUniformLocation(program.object, c"uTexTranslation".as_ptr().cast());
ck();
let rotation_uniform =
gl::GetUniformLocation(program.object, c"uRotation".as_ptr().cast());
ck();
let color_a_uniform =
gl::GetUniformLocation(program.object, c"uColorA".as_ptr().cast());
ck();
let color_b_uniform =
gl::GetUniformLocation(program.object, c"uColorB".as_ptr().cast());
ck();
let viewport_origin_uniform =
gl::GetUniformLocation(program.object, c"uViewportOrigin".as_ptr().cast());
ck();
let radius_uniform = gl::GetUniformLocation(program.object, c"uRadius".as_ptr().cast());
ck();
let camera_position_uniform =
gl::GetUniformLocation(program.object, c"uCameraPosition".as_ptr().cast());
ck();
let light_position_uniform =
gl::GetUniformLocation(program.object, c"uLightPosition".as_ptr().cast());
ck();
let sphere_position_uniform =
gl::GetUniformLocation(program.object, c"uSpherePosition".as_ptr().cast());
ck();
CheckProgram {
program,
position_attribute,
transform_uniform,
translation_uniform,
tex_transform_uniform,
tex_translation_uniform,
rotation_uniform,
color_a_uniform,
color_b_uniform,
viewport_origin_uniform,
radius_uniform,
light_position_uniform,
camera_position_uniform,
sphere_position_uniform,
}
}