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