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
use std;
use gl;
pub enum GlslVersion {
Unknown,
Glsl100,
Glsl300,
}
pub fn get_info_log(object: gl::types::GLuint) -> String {
unsafe {
let mut log_length: i32 = 0;
if gl::IsShader(object) == gl::TRUE {
gl::GetShaderiv(object, gl::INFO_LOG_LENGTH, &mut log_length);
} else if gl::IsProgram(object) == gl::TRUE {
gl::GetProgramiv(object, gl::INFO_LOG_LENGTH, &mut log_length);
} else {
return "GL: Not a shader or a program".to_owned();
}
let mut length: i32 = 0;
let mut buffer = [0u8; 512];
if gl::IsShader(object) == gl::TRUE {
gl::GetShaderInfoLog(object,
buffer.len() as i32,
&mut length,
buffer.as_mut_ptr() as *mut i8);
} else if gl::IsProgram(object) == gl::TRUE {
gl::GetProgramInfoLog(object,
buffer.len() as i32,
&mut length,
buffer.as_mut_ptr() as *mut i8);
}
let cstr = std::ffi::CStr::from_ptr(std::mem::transmute(&buffer));
format!("GL: {}", std::str::from_utf8(cstr.to_bytes()).expect("Info log is invalid"))
}
}
pub fn get_shading_lang_version() -> GlslVersion {
let version = unsafe {
let ptr = gl::GetString(gl::SHADING_LANGUAGE_VERSION);
let cstr: &std::ffi::CStr = std::ffi::CStr::from_ptr(ptr as *const i8);
std::str::from_utf8(cstr.to_bytes()).expect("Shading lang string is invalid")
};
if version.find("ES 3.").is_some() {
GlslVersion::Glsl300
} else if version.find("ES 1.").is_some() {
GlslVersion::Glsl100
} else {
GlslVersion::Unknown
}
}
pub fn create_shader(source: String,
shader_type: gl::types::GLenum)
-> Result<gl::types::GLuint, String> {
unsafe {
let shader = gl::CreateShader(shader_type);
let cstr = std::ffi::CString::new(source.as_bytes()).unwrap();
gl::ShaderSource(shader, 1, &cstr.as_ptr(), std::ptr::null());
gl::CompileShader(shader);
let mut status = gl::FALSE as gl::types::GLint;
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut status);
if status != (gl::TRUE as gl::types::GLint) {
let info_log = get_info_log(shader);
gl::DeleteShader(shader);
Err(info_log)
} else {
Ok(shader)
}
}
}
pub fn create_program(vertex_shader: gl::types::GLenum,
fragment_shader: gl::types::GLenum)
-> Result<gl::types::GLuint, String> {
unsafe {
let shader_program = gl::CreateProgram();
gl::AttachShader(shader_program, vertex_shader);
gl::AttachShader(shader_program, fragment_shader);
gl::LinkProgram(shader_program);
let mut link_ok = gl::FALSE as i32;
gl::GetProgramiv(shader_program, gl::LINK_STATUS, &mut link_ok);
if link_ok == gl::TRUE as i32 {
Ok(shader_program)
} else {
let info_log = get_info_log(shader_program);
gl::DeleteProgram(shader_program);
Err(info_log)
}
}
}
pub fn prepare_shader_program(vertex_source: String,
fragment_source: String)
-> Result<gl::types::GLuint, String> {
let vertex_shader = create_shader(vertex_source, gl::VERTEX_SHADER)?;
let fragment_shader = create_shader(fragment_source, gl::FRAGMENT_SHADER)?;
create_program(vertex_shader, fragment_shader)
}
pub fn get_attrib_location(program: gl::types::GLuint,
name: String)
-> Result<gl::types::GLint, String> {
let cstr = std::ffi::CString::new(name.as_bytes()).unwrap();
let location =
unsafe { gl::GetAttribLocation(program, cstr.as_bytes_with_nul().as_ptr() as *const i8) };
if location < 0 {
Err(format!("Could not get location for attribute '{}'", name))
} else {
Ok(location)
}
}
pub fn get_uniform_location(program: gl::types::GLuint,
name: String)
-> Result<gl::types::GLint, String> {
let cstr = std::ffi::CString::new(name.as_bytes()).unwrap();
let location =
unsafe { gl::GetUniformLocation(program, cstr.as_bytes_with_nul().as_ptr() as *const i8) };
if location < 0 {
Err(format!("Could not get location for uniform '{}'", name))
} else {
Ok(location)
}
}