-
Notifications
You must be signed in to change notification settings - Fork 397
/
raw.rs
795 lines (673 loc) · 27.7 KB
/
raw.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
use gl;
use context::CommandContext;
use version::Version;
use version::Api;
use backend::Facade;
use context::Context;
use ContextExt;
use UniformsExt;
use std::{ffi, fmt, mem};
use std::collections::hash_map::{self, HashMap};
use std::rc::Rc;
use std::cell::RefCell;
use std::os::raw;
use DrawError;
use GlObject;
use ProgramExt;
use Handle;
use RawUniformValue;
use QueryExt;
use draw_parameters::TimeElapsedQuery;
use buffer::BufferSlice;
use BufferExt;
use BufferSliceExt;
use program::{ProgramCreationError, Binary, GetBinaryError};
use program::uniforms_storage::UniformsStorage;
use program::compute::ComputeCommand;
use program::reflection::{Uniform, UniformBlock, OutputPrimitives};
use program::reflection::{Attribute, TransformFeedbackMode, TransformFeedbackBuffer};
use program::reflection::{SubroutineData, ShaderStage};
use program::reflection::{reflect_uniforms, reflect_attributes, reflect_uniform_blocks};
use program::reflection::{reflect_transform_feedback, reflect_geometry_output_type};
use program::reflection::{reflect_tess_eval_output_type, reflect_shader_storage_blocks};
use program::reflection::{reflect_subroutine_data};
use program::shader::Shader;
use program::binary_header::{attach_glium_header, process_glium_header};
use uniforms::Uniforms;
use vertex::VertexFormat;
use vertex_array_object::VertexAttributesSystem;
/// A combination of shaders linked together.
pub struct RawProgram {
context: Rc<Context>,
id: Handle,
uniform_values: UniformsStorage,
uniforms: HashMap<String, Uniform>,
uniform_blocks: HashMap<String, UniformBlock>,
subroutine_data: SubroutineData,
attributes: HashMap<String, Attribute>,
frag_data_locations: RefCell<HashMap<String, Option<u32>>>,
tf_buffers: Vec<TransformFeedbackBuffer>,
ssbos: HashMap<String, UniformBlock>,
output_primitives: Option<OutputPrimitives>,
has_geometry_shader: bool,
has_tessellation_control_shader: bool,
has_tessellation_evaluation_shader: bool,
}
impl RawProgram {
/// Builds a new program from a list of shaders.
// TODO: the "has_*" parameters are bad
pub fn from_shaders<'a, F, I>(facade: &'a F, shaders: I, has_geometry_shader: bool,
has_tessellation_control_shader: bool,
has_tessellation_evaluation_shader: bool,
transform_feedback: Option<(Vec<String>, TransformFeedbackMode)>)
-> Result<RawProgram, ProgramCreationError>
where F: Facade, I: IntoIterator<Item = &'a Shader>
{
let mut ctxt = facade.get_context().make_current();
let shaders_ids = shaders.into_iter().map(|s| s.get_id()).collect::<Vec<_>>();
let id = unsafe {
let id = create_program(&mut ctxt);
// attaching shaders
for sh in shaders_ids.iter() {
match (id, sh) {
(Handle::Id(id), &Handle::Id(sh)) => {
assert!(ctxt.version >= &Version(Api::Gl, 2, 0) ||
ctxt.version >= &Version(Api::GlEs, 2, 0));
ctxt.gl.AttachShader(id, sh);
},
(Handle::Handle(id), &Handle::Handle(sh)) => {
assert!(ctxt.extensions.gl_arb_shader_objects);
ctxt.gl.AttachObjectARB(id, sh);
},
_ => unreachable!()
}
}
// transform feedback varyings
if let Some((names, mode)) = transform_feedback {
let id = match id {
Handle::Id(id) => id,
Handle::Handle(id) => unreachable!() // transf. feedback shouldn't be
// available with handles
};
let names = names.into_iter().map(|name| {
ffi::CString::new(name.into_bytes()).unwrap()
}).collect::<Vec<_>>();
let names_ptr = names.iter().map(|n| n.as_ptr()).collect::<Vec<_>>();
if ctxt.version >= &Version(Api::Gl, 3, 0) {
let mode = match mode {
TransformFeedbackMode::Interleaved => gl::INTERLEAVED_ATTRIBS,
TransformFeedbackMode::Separate => gl::SEPARATE_ATTRIBS,
};
ctxt.gl.TransformFeedbackVaryings(id, names_ptr.len() as gl::types::GLsizei,
names_ptr.as_ptr(), mode);
} else if ctxt.extensions.gl_ext_transform_feedback {
let mode = match mode {
TransformFeedbackMode::Interleaved => gl::INTERLEAVED_ATTRIBS_EXT,
TransformFeedbackMode::Separate => gl::SEPARATE_ATTRIBS_EXT,
};
ctxt.gl.TransformFeedbackVaryingsEXT(id, names_ptr.len()
as gl::types::GLsizei,
names_ptr.as_ptr(), mode);
} else {
unreachable!(); // has been checked in the frontend
}
}
// linking
{
ctxt.report_debug_output_errors.set(false);
match id {
Handle::Id(id) => {
assert!(ctxt.version >= &Version(Api::Gl, 2, 0) ||
ctxt.version >= &Version(Api::GlEs, 2, 0));
ctxt.gl.LinkProgram(id);
},
Handle::Handle(id) => {
assert!(ctxt.extensions.gl_arb_shader_objects);
ctxt.gl.LinkProgramARB(id);
}
}
ctxt.report_debug_output_errors.set(true);
}
// checking for errors
try!(check_program_link_errors(&mut ctxt, id));
id
};
let uniforms = unsafe { reflect_uniforms(&mut ctxt, id) };
let attributes = unsafe { reflect_attributes(&mut ctxt, id) };
let blocks = unsafe { reflect_uniform_blocks(&mut ctxt, id) };
let tf_buffers = unsafe { reflect_transform_feedback(&mut ctxt, id) };
let ssbos = unsafe { reflect_shader_storage_blocks(&mut ctxt, id) };
let subroutine_data = unsafe {
reflect_subroutine_data(&mut ctxt, id, has_geometry_shader,
has_tessellation_control_shader,
has_tessellation_evaluation_shader)
};
let output_primitives = if has_geometry_shader {
Some(unsafe { reflect_geometry_output_type(&mut ctxt, id) })
} else if has_tessellation_evaluation_shader {
Some(unsafe { reflect_tess_eval_output_type(&mut ctxt, id) })
} else {
None
};
Ok(RawProgram {
context: facade.get_context().clone(),
id: id,
uniforms: uniforms,
uniform_values: UniformsStorage::new(),
uniform_blocks: blocks,
subroutine_data: subroutine_data,
attributes: attributes,
frag_data_locations: RefCell::new(HashMap::new()),
tf_buffers: tf_buffers,
ssbos: ssbos,
output_primitives: output_primitives,
has_geometry_shader: has_geometry_shader,
has_tessellation_control_shader: has_tessellation_control_shader,
has_tessellation_evaluation_shader: has_tessellation_evaluation_shader,
})
}
/// Creates a program from binary.
pub fn from_binary<F>(facade: &F, binary: Binary)
-> Result<RawProgram, ProgramCreationError> where F: Facade
{
let (has_geometry_shader, has_tessellation_control_shader, has_tessellation_evaluation_shader) = {
match process_glium_header(&binary.content) {
Some(flags) => flags,
None => return Err(ProgramCreationError::BinaryHeaderError)
}
};
let mut ctxt = facade.get_context().make_current();
let id = unsafe {
let id = create_program(&mut ctxt);
match id {
Handle::Id(id) => {
assert!(ctxt.version >= &Version(Api::Gl, 2, 0));
ctxt.gl.ProgramBinary(id, binary.format,
binary.content[1..].as_ptr() as *const _,
(binary.content.len() - 1) as gl::types::GLsizei);
},
Handle::Handle(id) => unreachable!()
};
// checking for errors
try!(check_program_link_errors(&mut ctxt, id));
id
};
let (uniforms, attributes, blocks, tf_buffers, ssbos, subroutine_data) = unsafe {
(
reflect_uniforms(&mut ctxt, id),
reflect_attributes(&mut ctxt, id),
reflect_uniform_blocks(&mut ctxt, id),
reflect_transform_feedback(&mut ctxt, id),
reflect_shader_storage_blocks(&mut ctxt, id),
reflect_subroutine_data(&mut ctxt, id, has_geometry_shader,
has_tessellation_control_shader,
has_tessellation_evaluation_shader),
)
};
let output_primitives = if has_geometry_shader {
Some(unsafe { reflect_geometry_output_type(&mut ctxt, id) })
} else if has_tessellation_evaluation_shader {
Some(unsafe { reflect_tess_eval_output_type(&mut ctxt, id) })
} else {
None
};
Ok(RawProgram {
context: facade.get_context().clone(),
id: id,
uniforms: uniforms,
uniform_values: UniformsStorage::new(),
uniform_blocks: blocks,
subroutine_data: subroutine_data,
attributes: attributes,
frag_data_locations: RefCell::new(HashMap::new()),
tf_buffers: tf_buffers,
ssbos: ssbos,
output_primitives: output_primitives,
has_geometry_shader: has_geometry_shader,
has_tessellation_control_shader: has_tessellation_control_shader,
has_tessellation_evaluation_shader: has_tessellation_evaluation_shader,
})
}
/// Returns the program's compiled binary.
///
/// You can store the result in a file, then reload it later. This avoids having to compile
/// the source code every time.
pub fn get_binary(&self) -> Result<Binary, GetBinaryError> {
unsafe {
let ctxt = self.context.make_current();
if ctxt.version >= &Version(Api::Gl, 4, 1) ||
ctxt.extensions.gl_arb_get_programy_binary
{
let id = match self.id {
Handle::Id(id) => id,
Handle::Handle(_) => unreachable!()
};
let mut num_supported_formats = mem::uninitialized();
ctxt.gl.GetIntegerv(gl::NUM_PROGRAM_BINARY_FORMATS, &mut num_supported_formats);
if num_supported_formats == 0 {
return Err(GetBinaryError::NoFormats)
}
let mut buf_len = mem::uninitialized();
ctxt.gl.GetProgramiv(id, gl::PROGRAM_BINARY_LENGTH, &mut buf_len);
let mut format = mem::uninitialized();
let mut storage: Vec<u8> = Vec::with_capacity(buf_len as usize);
ctxt.gl.GetProgramBinary(id, buf_len, &mut buf_len, &mut format,
storage.as_mut_ptr() as *mut _);
storage.set_len(buf_len as usize);
attach_glium_header(&self, &mut storage);
Ok(Binary {
format: format,
content: storage,
})
} else {
Err(GetBinaryError::NotSupported)
}
}
}
/// Returns the *location* of an output fragment, if it exists.
///
/// The *location* is low-level information that is used internally by glium.
/// You probably don't need to call this function.
///
/// You can declare output fragments in your shaders by writing:
///
/// ```notrust
/// out vec4 foo;
/// ```
///
pub fn get_frag_data_location(&self, name: &str) -> Option<u32> {
// looking for a cached value
if let Some(result) = self.frag_data_locations.borrow_mut().get(name) {
return result.clone();
}
// querying opengl
let name_c = ffi::CString::new(name.as_bytes()).unwrap();
let ctxt = self.context.make_current();
let value = unsafe {
match self.id {
Handle::Id(id) => {
assert!(ctxt.version >= &Version(Api::Gl, 2, 0) ||
ctxt.version >= &Version(Api::GlEs, 2, 0));
ctxt.gl.GetFragDataLocation(id, name_c.as_bytes_with_nul().as_ptr()
as *const raw::c_char)
},
Handle::Handle(id) => {
// not supported
-1
}
}
};
let location = match value {
-1 => None,
a => Some(a as u32),
};
self.frag_data_locations.borrow_mut().insert(name.to_owned(), location);
location
}
/// Returns informations about a uniform variable, if it exists.
#[inline]
pub fn get_uniform(&self, name: &str) -> Option<&Uniform> {
self.uniforms.get(name)
}
/// Returns an iterator to the list of uniforms.
///
/// ## Example
///
/// ```no_run
/// # let program: glium::Program = unsafe { std::mem::uninitialized() };
/// for (name, uniform) in program.uniforms() {
/// println!("Name: {} - Type: {:?}", name, uniform.ty);
/// }
/// ```
#[inline]
pub fn uniforms(&self) -> hash_map::Iter<String, Uniform> {
self.uniforms.iter()
}
/// Returns a list of uniform blocks.
///
/// ## Example
///
/// ```no_run
/// # let program: glium::Program = unsafe { std::mem::uninitialized() };
/// for (name, uniform) in program.get_uniform_blocks() {
/// println!("Name: {}", name);
/// }
/// ```
#[inline]
pub fn get_uniform_blocks(&self) -> &HashMap<String, UniformBlock> {
&self.uniform_blocks
}
/// Returns the list of transform feedback varyings.
#[inline]
pub fn get_transform_feedback_buffers(&self) -> &[TransformFeedbackBuffer] {
&self.tf_buffers
}
/// True if the transform feedback output of this program matches the specified `VertexFormat`
/// and `stride`.
///
/// The `stride` is the number of bytes between two vertices.
pub fn transform_feedback_matches(&self, format: &VertexFormat, stride: usize) -> bool {
// TODO: doesn't support multiple buffers
if self.get_transform_feedback_buffers().len() != 1 {
return false;
}
let buf = &self.get_transform_feedback_buffers()[0];
if buf.stride != stride {
return false;
}
for elem in buf.elements.iter() {
if format.iter().find(|e| &e.0 == &*elem.name && e.1 == elem.offset && e.2 == elem.ty)
.is_none()
{
return false;
}
}
true
}
/// Returns the type of geometry that transform feedback would generate, or `None` if it
/// depends on the vertex/index data passed when drawing.
///
/// This corresponds to `GL_GEOMETRY_OUTPUT_TYPE` or `GL_TESS_GEN_MODE`. If the program doesn't
/// contain either a geometry shader or a tessellation evaluation shader, returns `None`.
#[inline]
pub fn get_output_primitives(&self) -> Option<OutputPrimitives> {
self.output_primitives
}
/// Returns true if the program contains a tessellation stage.
#[inline]
pub fn has_tessellation_shaders(&self) -> bool {
self.has_tessellation_control_shader() | self.has_tessellation_evaluation_shader()
}
/// Returns true if the program contains a tessellation control stage.
#[inline]
pub fn has_tessellation_control_shader(&self) -> bool {
self.has_tessellation_control_shader
}
/// Returns true if the program contains a tessellation evaluation stage.
#[inline]
pub fn has_tessellation_evaluation_shader(&self) -> bool {
self.has_tessellation_evaluation_shader
}
/// Returns true if the program contains a geometry shader.
#[inline]
pub fn has_geometry_shader(&self) -> bool {
self.has_geometry_shader
}
/// Returns informations about an attribute, if it exists.
#[inline]
pub fn get_attribute(&self, name: &str) -> Option<&Attribute> {
self.attributes.get(name)
}
/// Returns an iterator to the list of attributes.
///
/// ## Example
///
/// ```no_run
/// # let program: glium::Program = unsafe { std::mem::uninitialized() };
/// for (name, attribute) in program.attributes() {
/// println!("Name: {} - Type: {:?}", name, attribute.ty);
/// }
/// ```
#[inline]
pub fn attributes(&self) -> hash_map::Iter<String, Attribute> {
self.attributes.iter()
}
/// Returns the list of shader storage blocks.
///
/// ## Example
///
/// ```no_run
/// # let program: glium::Program = unsafe { std::mem::uninitialized() };
/// for (name, uniform) in program.get_shader_storage_blocks() {
/// println!("Name: {}", name);
/// }
/// ```
#[inline]
pub fn get_shader_storage_blocks(&self) -> &HashMap<String, UniformBlock> {
&self.ssbos
}
/// Returns data associated with the programs subroutines.
#[inline]
pub fn get_subroutine_data(&self) -> &SubroutineData {
&self.subroutine_data
}
/// Assumes that the program contains a compute shader and executes it.
///
/// # Safety
///
/// The program *must* contain a compute shader.
/// TODO: check inside the program if it has a compute shader instead of being unsafe
pub unsafe fn dispatch_compute<U>(&self, uniforms: U, x: u32, y: u32, z: u32)
-> Result<(), DrawError> // TODO: other error?
where U: Uniforms
{
let mut ctxt = self.context.make_current();
// TODO: return an error instead
assert!(x < ctxt.capabilities.max_compute_work_group_count.0 as u32);
assert!(y < ctxt.capabilities.max_compute_work_group_count.1 as u32);
assert!(z < ctxt.capabilities.max_compute_work_group_count.2 as u32);
assert!(ctxt.version >= &Version(Api::Gl, 4, 3) ||
ctxt.version >= &Version(Api::GlEs, 3, 1) ||
ctxt.extensions.gl_arb_compute_shader);
TimeElapsedQuery::end_conditional_render(&mut ctxt);
let mut fences = Vec::with_capacity(0);
self.use_program(&mut ctxt);
try!(uniforms.bind_uniforms(&mut ctxt, self, &mut fences));
ctxt.gl.DispatchCompute(x, y, z);
for fence in fences {
fence.insert(&mut ctxt);
}
Ok(())
}
/// Assumes that the program contains a compute shader and executes it.
///
/// # Safety
///
/// The program *must* contain a compute shader.
/// TODO: check inside the program if it has a compute shader instead of being unsafe
pub unsafe fn dispatch_compute_indirect<U>(&self, uniforms: U,
buffer: BufferSlice<ComputeCommand>)
-> Result<(), DrawError> // TODO: other error?
where U: Uniforms
{
let mut ctxt = self.context.make_current();
assert!(ctxt.version >= &Version(Api::Gl, 4, 3) ||
ctxt.version >= &Version(Api::GlEs, 3, 1) ||
ctxt.extensions.gl_arb_compute_shader);
TimeElapsedQuery::end_conditional_render(&mut ctxt);
buffer.prepare_and_bind_for_dispatch_indirect(&mut ctxt);
let offset = buffer.get_offset_bytes();
// an error is generated if the offset is not a multiple of 4
assert!(offset % 4 == 0);
if let Some(fence) = buffer.add_fence() {
fence.insert(&mut ctxt);
}
self.use_program(&mut ctxt);
let mut fences = Vec::with_capacity(0);
try!(uniforms.bind_uniforms(&mut ctxt, self, &mut fences));
ctxt.gl.DispatchComputeIndirect(offset as gl::types::GLintptr);
for fence in fences {
fence.insert(&mut ctxt);
}
Ok(())
}
}
impl fmt::Debug for RawProgram {
#[inline]
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
(format!("Program #{:?}", self.id)).fmt(formatter)
}
}
impl GlObject for RawProgram {
type Id = Handle;
#[inline]
fn get_id(&self) -> Handle {
self.id
}
}
impl ProgramExt for RawProgram {
#[inline]
fn use_program(&self, ctxt: &mut CommandContext) {
unsafe {
let program_id = self.get_id();
if ctxt.state.program != program_id {
match program_id {
Handle::Id(id) => ctxt.gl.UseProgram(id),
Handle::Handle(id) => ctxt.gl.UseProgramObjectARB(id),
}
ctxt.state.program = program_id;
}
}
}
#[inline]
fn set_uniform(&self, ctxt: &mut CommandContext, uniform_location: gl::types::GLint,
value: &RawUniformValue)
{
self.uniform_values.set_uniform_value(ctxt, self.id, uniform_location, value);
}
#[inline]
fn set_uniform_block_binding(&self, ctxt: &mut CommandContext, block_location: gl::types::GLuint,
value: gl::types::GLuint)
{
self.uniform_values.set_uniform_block_binding(ctxt, self.id, block_location, value);
}
#[inline]
fn set_shader_storage_block_binding(&self, ctxt: &mut CommandContext,
block_location: gl::types::GLuint,
value: gl::types::GLuint)
{
self.uniform_values.set_shader_storage_block_binding(ctxt, self.id, block_location, value);
}
#[inline]
fn set_subroutine_uniforms_for_stage(&self, ctxt: &mut CommandContext,
stage: ShaderStage,
indices: &[gl::types::GLuint])
{
self.uniform_values.set_subroutine_uniforms_for_stage(ctxt, self.id, stage, indices);
}
#[inline]
fn get_uniform(&self, name: &str) -> Option<&Uniform> {
self.uniforms.get(name)
}
#[inline]
fn get_uniform_blocks(&self) -> &HashMap<String, UniformBlock> {
&self.uniform_blocks
}
#[inline]
fn get_shader_storage_blocks(&self) -> &HashMap<String, UniformBlock> {
&self.ssbos
}
#[inline]
fn get_subroutine_data(&self) -> &SubroutineData {
&self.subroutine_data
}
}
impl Drop for RawProgram {
fn drop(&mut self) {
let mut ctxt = self.context.make_current();
// removing VAOs which contain this program
VertexAttributesSystem::purge_program(&mut ctxt, self.id);
// sending the destroy command
unsafe {
match self.id {
Handle::Id(id) => {
assert!(ctxt.version >= &Version(Api::Gl, 2, 0) ||
ctxt.version >= &Version(Api::GlEs, 2, 0));
if ctxt.state.program == Handle::Id(id) {
ctxt.gl.UseProgram(0);
ctxt.state.program = Handle::Id(0);
}
ctxt.gl.DeleteProgram(id);
},
Handle::Handle(id) => {
assert!(ctxt.extensions.gl_arb_shader_objects);
if ctxt.state.program == Handle::Handle(id) {
ctxt.gl.UseProgramObjectARB(0 as gl::types::GLhandleARB);
ctxt.state.program = Handle::Handle(0 as gl::types::GLhandleARB);
}
ctxt.gl.DeleteObjectARB(id);
}
}
}
}
}
/// Builds an empty program from within the GL context.
unsafe fn create_program(ctxt: &mut CommandContext) -> Handle {
let id = if ctxt.version >= &Version(Api::Gl, 2, 0) ||
ctxt.version >= &Version(Api::GlEs, 2, 0)
{
Handle::Id(ctxt.gl.CreateProgram())
} else if ctxt.extensions.gl_arb_shader_objects {
Handle::Handle(ctxt.gl.CreateProgramObjectARB())
} else {
unreachable!()
};
if id == Handle::Id(0) || id == Handle::Handle(0 as gl::types::GLhandleARB) {
panic!("glCreateProgram failed");
}
id
}
unsafe fn check_program_link_errors(ctxt: &mut CommandContext, id: Handle)
-> Result<(), ProgramCreationError>
{
let mut link_success: gl::types::GLint = mem::uninitialized();
match id {
Handle::Id(id) => {
assert!(ctxt.version >= &Version(Api::Gl, 2, 0) ||
ctxt.version >= &Version(Api::GlEs, 2, 0));
ctxt.gl.GetProgramiv(id, gl::LINK_STATUS, &mut link_success);
},
Handle::Handle(id) => {
assert!(ctxt.extensions.gl_arb_shader_objects);
ctxt.gl.GetObjectParameterivARB(id, gl::OBJECT_LINK_STATUS_ARB,
&mut link_success);
}
}
if link_success == 0 {
use ProgramCreationError::LinkingError;
match ctxt.gl.GetError() {
gl::NO_ERROR => (),
gl::INVALID_VALUE => {
return Err(LinkingError(format!("glLinkProgram triggered \
GL_INVALID_VALUE")));
},
gl::INVALID_OPERATION => {
return Err(LinkingError(format!("glLinkProgram triggered \
GL_INVALID_OPERATION")));
},
_ => {
return Err(LinkingError(format!("glLinkProgram triggered an \
unknown error")));
}
};
let mut error_log_size: gl::types::GLint = mem::uninitialized();
match id {
Handle::Id(id) => {
assert!(ctxt.version >= &Version(Api::Gl, 2, 0));
ctxt.gl.GetProgramiv(id, gl::INFO_LOG_LENGTH, &mut error_log_size);
},
Handle::Handle(id) => {
assert!(ctxt.extensions.gl_arb_shader_objects);
ctxt.gl.GetObjectParameterivARB(id, gl::OBJECT_INFO_LOG_LENGTH_ARB,
&mut error_log_size);
}
}
let mut error_log: Vec<u8> = Vec::with_capacity(error_log_size as usize);
match id {
Handle::Id(id) => {
assert!(ctxt.version >= &Version(Api::Gl, 2, 0));
ctxt.gl.GetProgramInfoLog(id, error_log_size, &mut error_log_size,
error_log.as_mut_ptr() as *mut gl::types::GLchar);
},
Handle::Handle(id) => {
assert!(ctxt.extensions.gl_arb_shader_objects);
ctxt.gl.GetInfoLogARB(id, error_log_size, &mut error_log_size,
error_log.as_mut_ptr() as *mut gl::types::GLchar);
}
}
error_log.set_len(error_log_size as usize);
let msg = String::from_utf8(error_log).unwrap();
return Err(LinkingError(msg));
}
Ok(())
}