14702
|
1 |
|
14705
|
2 |
use integral_geometry::Rect;
|
|
3 |
|
|
4 |
use std::{ffi, ffi::CString, mem, ptr, slice};
|
14702
|
5 |
|
|
6 |
#[derive(Debug)]
|
|
7 |
pub struct Texture2D {
|
|
8 |
pub handle: u32,
|
|
9 |
}
|
|
10 |
|
|
11 |
impl Drop for Texture2D {
|
|
12 |
fn drop(&mut self) {
|
|
13 |
if self.handle != 0 {
|
|
14 |
unsafe {
|
|
15 |
gl::DeleteTextures(1, &self.handle);
|
|
16 |
}
|
|
17 |
}
|
|
18 |
}
|
|
19 |
}
|
|
20 |
|
|
21 |
impl Texture2D {
|
|
22 |
pub fn with_data(
|
|
23 |
data: &[u8],
|
|
24 |
data_stride: u32,
|
|
25 |
width: u32,
|
|
26 |
height: u32,
|
|
27 |
internal_format: u32,
|
|
28 |
format: u32,
|
|
29 |
ty: u32,
|
14705
|
30 |
filter: u32,
|
14702
|
31 |
) -> Self {
|
|
32 |
let mut handle = 0;
|
14705
|
33 |
|
14702
|
34 |
unsafe {
|
|
35 |
gl::GenTextures(1, &mut handle);
|
14705
|
36 |
|
14702
|
37 |
gl::BindTexture(gl::TEXTURE_2D, handle);
|
|
38 |
gl::PixelStorei(gl::UNPACK_ROW_LENGTH, data_stride as i32);
|
|
39 |
gl::TexImage2D(
|
|
40 |
gl::TEXTURE_2D,
|
|
41 |
0,
|
|
42 |
internal_format as i32,
|
|
43 |
width as i32,
|
|
44 |
height as i32,
|
|
45 |
0,
|
|
46 |
format as u32,
|
|
47 |
ty,
|
14705
|
48 |
data.as_ptr() as *const _,
|
14702
|
49 |
);
|
|
50 |
|
|
51 |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
|
|
52 |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);
|
|
53 |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, filter as i32);
|
|
54 |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, filter as i32);
|
|
55 |
}
|
|
56 |
|
14705
|
57 |
Texture2D { handle }
|
14702
|
58 |
}
|
|
59 |
|
|
60 |
pub fn update(&self, region: Rect, data: &[u8], data_stride: u32, format: u32, ty: u32) {
|
|
61 |
unsafe {
|
|
62 |
gl::BindTexture(gl::TEXTURE_2D, self.handle);
|
|
63 |
gl::PixelStorei(gl::UNPACK_ROW_LENGTH, data_stride as i32);
|
|
64 |
gl::TexSubImage2D(
|
|
65 |
gl::TEXTURE_2D,
|
14705
|
66 |
0, // texture level
|
14702
|
67 |
region.left(), // texture region
|
|
68 |
region.top(),
|
|
69 |
region.width() as i32 - 1,
|
|
70 |
region.height() as i32 - 1,
|
14705
|
71 |
format, // data format
|
|
72 |
ty, // data type
|
14702
|
73 |
data.as_ptr() as *const _, // data ptr
|
|
74 |
);
|
|
75 |
}
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
#[derive(Debug)]
|
|
80 |
pub struct Buffer {
|
|
81 |
pub handle: u32,
|
|
82 |
pub ty: u32,
|
|
83 |
pub usage: u32,
|
|
84 |
}
|
|
85 |
|
|
86 |
impl Buffer {
|
|
87 |
pub fn empty(
|
|
88 |
ty: u32,
|
|
89 |
usage: u32,
|
|
90 |
//size: isize
|
|
91 |
) -> Buffer {
|
|
92 |
let mut buffer = 0;
|
|
93 |
|
|
94 |
unsafe {
|
|
95 |
gl::GenBuffers(1, &mut buffer);
|
|
96 |
gl::BindBuffer(ty, buffer);
|
|
97 |
//gl::BufferData(ty, size, ptr::null_mut(), usage);
|
|
98 |
}
|
|
99 |
|
|
100 |
Buffer {
|
|
101 |
handle: buffer,
|
|
102 |
ty,
|
|
103 |
usage,
|
|
104 |
}
|
|
105 |
}
|
14705
|
106 |
|
|
107 |
fn with_data(ty: u32, usage: u32, data: &[u8]) -> Buffer {
|
14702
|
108 |
let mut buffer = 0;
|
|
109 |
|
|
110 |
unsafe {
|
|
111 |
gl::GenBuffers(1, &mut buffer);
|
|
112 |
gl::BindBuffer(ty, buffer);
|
|
113 |
gl::BufferData(ty, data.len() as isize, data.as_ptr() as _, usage);
|
|
114 |
}
|
|
115 |
|
|
116 |
Buffer {
|
|
117 |
handle: buffer,
|
|
118 |
ty,
|
14705
|
119 |
usage,
|
14702
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
pub fn ty(&self) -> u32 {
|
|
124 |
self.ty
|
|
125 |
}
|
|
126 |
|
|
127 |
pub fn handle(&self) -> u32 {
|
|
128 |
self.handle
|
|
129 |
}
|
|
130 |
|
|
131 |
pub fn write_typed<T>(&self, data: &[T]) {
|
|
132 |
unsafe {
|
14705
|
133 |
let data =
|
|
134 |
slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * mem::size_of::<T>());
|
|
135 |
|
|
136 |
gl::BindBuffer(self.ty, self.handle);
|
|
137 |
gl::BufferData(
|
|
138 |
self.ty,
|
|
139 |
data.len() as isize,
|
|
140 |
data.as_ptr() as *const _ as *const _,
|
|
141 |
self.usage,
|
14702
|
142 |
);
|
|
143 |
}
|
|
144 |
}
|
14705
|
145 |
|
|
146 |
pub fn write(&self, data: &[u8]) {
|
14702
|
147 |
unsafe {
|
|
148 |
gl::BindBuffer(self.ty, self.handle);
|
14705
|
149 |
gl::BufferData(
|
|
150 |
self.ty,
|
|
151 |
data.len() as isize,
|
|
152 |
data.as_ptr() as *const _ as *const _,
|
|
153 |
self.usage,
|
|
154 |
);
|
14702
|
155 |
}
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
impl Drop for Buffer {
|
|
160 |
fn drop(&mut self) {
|
|
161 |
unsafe {
|
|
162 |
gl::DeleteBuffers(1, &self.handle);
|
|
163 |
}
|
|
164 |
}
|
|
165 |
}
|
|
166 |
|
|
167 |
#[derive(Debug)]
|
|
168 |
pub enum VariableBinding<'a> {
|
|
169 |
Attribute(&'a str, u32),
|
|
170 |
Uniform(&'a str, u32),
|
|
171 |
UniformBlock(&'a str, u32),
|
|
172 |
Sampler(&'a str, u32),
|
|
173 |
}
|
|
174 |
|
|
175 |
#[derive(Debug)]
|
|
176 |
pub struct Shader {
|
|
177 |
pub program: u32,
|
|
178 |
}
|
|
179 |
|
|
180 |
impl Drop for Shader {
|
|
181 |
fn drop(&mut self) {
|
|
182 |
unsafe {
|
|
183 |
gl::DeleteProgram(self.program);
|
|
184 |
}
|
|
185 |
}
|
|
186 |
}
|
|
187 |
|
|
188 |
impl Shader {
|
|
189 |
pub fn new<'a>(
|
|
190 |
vs: &str,
|
|
191 |
ps: Option<&str>,
|
14705
|
192 |
bindings: &[VariableBinding<'a>],
|
14702
|
193 |
) -> Result<Self, String> {
|
|
194 |
unsafe fn compile_shader(ty: u32, shdr: &str) -> Result<u32, String> {
|
|
195 |
let shader = gl::CreateShader(ty);
|
|
196 |
let len = shdr.len() as i32;
|
|
197 |
let shdr = shdr.as_ptr() as *const i8;
|
|
198 |
gl::ShaderSource(shader, 1, &shdr, &len);
|
|
199 |
gl::CompileShader(shader);
|
|
200 |
|
|
201 |
let mut success = 0i32;
|
|
202 |
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success as _);
|
|
203 |
|
|
204 |
if success == gl::FALSE as i32 {
|
|
205 |
let mut log_size = 0i32;
|
|
206 |
gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut log_size as _);
|
|
207 |
|
|
208 |
let mut log = vec![0u8; log_size as usize];
|
|
209 |
gl::GetShaderInfoLog(shader, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
|
|
210 |
|
|
211 |
gl::DeleteShader(shader);
|
|
212 |
Err(String::from_utf8_unchecked(log))
|
|
213 |
} else {
|
|
214 |
Ok(shader)
|
|
215 |
}
|
|
216 |
}
|
14705
|
217 |
|
14702
|
218 |
let vs = unsafe { compile_shader(gl::VERTEX_SHADER, vs)? };
|
|
219 |
let ps = if let Some(ps) = ps {
|
|
220 |
Some(unsafe { compile_shader(gl::FRAGMENT_SHADER, ps)? })
|
|
221 |
} else {
|
|
222 |
None
|
|
223 |
};
|
|
224 |
|
|
225 |
unsafe {
|
|
226 |
let program = gl::CreateProgram();
|
14705
|
227 |
|
14702
|
228 |
gl::AttachShader(program, vs);
|
|
229 |
if let Some(ps) = ps {
|
|
230 |
gl::AttachShader(program, ps);
|
|
231 |
}
|
|
232 |
|
|
233 |
for bind in bindings {
|
|
234 |
match bind {
|
|
235 |
&VariableBinding::Attribute(ref name, id) => {
|
|
236 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14705
|
237 |
gl::BindAttribLocation(
|
|
238 |
program,
|
|
239 |
id,
|
|
240 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
241 |
);
|
|
242 |
}
|
14702
|
243 |
_ => {}
|
|
244 |
}
|
|
245 |
}
|
|
246 |
|
|
247 |
gl::LinkProgram(program);
|
|
248 |
|
|
249 |
let mut success = 0i32;
|
|
250 |
gl::GetProgramiv(program, gl::LINK_STATUS, &mut success);
|
|
251 |
if success == gl::FALSE as i32 {
|
|
252 |
let mut log_size = 0i32;
|
|
253 |
gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut log_size as _);
|
|
254 |
|
|
255 |
let mut log = vec![0u8; log_size as usize];
|
|
256 |
gl::GetProgramInfoLog(program, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
|
|
257 |
|
|
258 |
gl::DeleteProgram(program);
|
|
259 |
return Err(String::from_utf8_unchecked(log));
|
|
260 |
}
|
|
261 |
|
|
262 |
//gl::DetachShader(program, vs);
|
|
263 |
if let Some(ps) = ps {
|
|
264 |
//gl::DetachShader(program, ps);
|
|
265 |
}
|
|
266 |
|
|
267 |
gl::UseProgram(program);
|
|
268 |
|
|
269 |
// after linking we setup sampler bindings as specified in the shader
|
|
270 |
for bind in bindings {
|
|
271 |
match bind {
|
|
272 |
VariableBinding::Uniform(name, id) => {
|
|
273 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14705
|
274 |
let index = gl::GetUniformLocation(
|
|
275 |
program,
|
|
276 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
277 |
);
|
14702
|
278 |
|
|
279 |
// TODO: impl for block?
|
14705
|
280 |
}
|
14702
|
281 |
VariableBinding::UniformBlock(name, id) => {
|
|
282 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14705
|
283 |
let index = gl::GetUniformBlockIndex(
|
|
284 |
program,
|
|
285 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
286 |
);
|
14702
|
287 |
|
|
288 |
gl::UniformBlockBinding(program, index, *id);
|
|
289 |
}
|
|
290 |
VariableBinding::Sampler(name, id) => {
|
|
291 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14705
|
292 |
let index = gl::GetUniformLocation(
|
|
293 |
program,
|
|
294 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
295 |
);
|
|
296 |
|
14702
|
297 |
gl::Uniform1i(index, *id as i32);
|
14705
|
298 |
}
|
14702
|
299 |
_ => {}
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
14705
|
303 |
Ok(Shader { program })
|
14702
|
304 |
}
|
|
305 |
}
|
|
306 |
|
|
307 |
pub fn bind(&self) {
|
|
308 |
unsafe {
|
|
309 |
gl::UseProgram(self.program);
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
pub fn set_matrix(&self, name: &str, matrix: *const f32) {
|
|
314 |
unsafe {
|
|
315 |
let c_str = CString::new(name).unwrap();
|
14705
|
316 |
let index = gl::GetUniformLocation(
|
|
317 |
self.program,
|
|
318 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
319 |
);
|
|
320 |
|
14702
|
321 |
gl::UniformMatrix4fv(index, 1, gl::FALSE, matrix);
|
|
322 |
}
|
|
323 |
}
|
|
324 |
|
|
325 |
pub fn bind_texture_2d(&self, index: u32, texture: &Texture2D) {
|
|
326 |
self.bind();
|
14705
|
327 |
|
14702
|
328 |
unsafe {
|
|
329 |
gl::ActiveTexture(gl::TEXTURE0 + index);
|
|
330 |
gl::BindTexture(gl::TEXTURE_2D, texture.handle);
|
|
331 |
}
|
|
332 |
}
|
|
333 |
}
|
|
334 |
|
|
335 |
pub enum InputFormat {
|
|
336 |
Float(u32, bool),
|
|
337 |
Integer(u32),
|
|
338 |
}
|
|
339 |
|
|
340 |
pub struct InputElement {
|
|
341 |
pub shader_slot: u32,
|
|
342 |
pub buffer_slot: u32,
|
|
343 |
pub format: InputFormat,
|
|
344 |
pub components: u32,
|
|
345 |
pub stride: u32,
|
|
346 |
pub offset: u32,
|
|
347 |
}
|
|
348 |
|
14705
|
349 |
// TODO:
|
14702
|
350 |
pub struct InputLayout {
|
|
351 |
pub elements: Vec<InputElement>,
|
|
352 |
}
|
|
353 |
|
|
354 |
pub struct LayoutGuard {
|
14705
|
355 |
vao: u32,
|
14702
|
356 |
}
|
|
357 |
|
|
358 |
impl Drop for LayoutGuard {
|
|
359 |
fn drop(&mut self) {
|
|
360 |
unsafe {
|
|
361 |
gl::DeleteVertexArrays(1, [self.vao].as_ptr());
|
|
362 |
gl::BindVertexArray(0);
|
|
363 |
}
|
|
364 |
}
|
|
365 |
}
|
|
366 |
|
|
367 |
impl InputLayout {
|
|
368 |
pub fn new(elements: Vec<InputElement>) -> Self {
|
14705
|
369 |
InputLayout { elements }
|
14702
|
370 |
}
|
|
371 |
|
14705
|
372 |
pub fn bind(
|
|
373 |
&mut self,
|
|
374 |
buffers: &[(u32, &Buffer)],
|
|
375 |
index_buffer: Option<&Buffer>,
|
|
376 |
) -> LayoutGuard {
|
14702
|
377 |
let mut vao = 0;
|
14705
|
378 |
|
14702
|
379 |
unsafe {
|
|
380 |
gl::GenVertexArrays(1, &mut vao);
|
|
381 |
gl::BindVertexArray(vao);
|
|
382 |
}
|
14705
|
383 |
|
14702
|
384 |
for &(slot, ref buffer) in buffers {
|
|
385 |
unsafe {
|
|
386 |
gl::BindBuffer(buffer.ty(), buffer.handle());
|
|
387 |
}
|
14705
|
388 |
|
14702
|
389 |
for attr in self.elements.iter().filter(|a| a.buffer_slot == slot) {
|
|
390 |
unsafe {
|
|
391 |
gl::EnableVertexAttribArray(attr.shader_slot);
|
|
392 |
match attr.format {
|
|
393 |
InputFormat::Float(fmt, normalized) => {
|
|
394 |
gl::VertexAttribPointer(
|
|
395 |
attr.shader_slot,
|
|
396 |
attr.components as i32,
|
|
397 |
fmt,
|
14705
|
398 |
if normalized { gl::TRUE } else { gl::FALSE },
|
14702
|
399 |
attr.stride as i32,
|
14705
|
400 |
attr.offset as *const _,
|
14702
|
401 |
);
|
|
402 |
}
|
|
403 |
InputFormat::Integer(fmt) => {
|
|
404 |
gl::VertexAttribIPointer(
|
|
405 |
attr.shader_slot,
|
|
406 |
attr.components as i32,
|
|
407 |
fmt,
|
|
408 |
attr.stride as i32,
|
14705
|
409 |
attr.offset as *const _,
|
14702
|
410 |
);
|
|
411 |
}
|
|
412 |
}
|
|
413 |
}
|
|
414 |
}
|
|
415 |
}
|
|
416 |
|
|
417 |
if let Some(buf) = index_buffer {
|
|
418 |
unsafe {
|
|
419 |
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, buf.handle());
|
|
420 |
}
|
|
421 |
}
|
|
422 |
|
14705
|
423 |
LayoutGuard { vao }
|
14702
|
424 |
}
|
|
425 |
}
|