15306
|
1 |
use integral_geometry::{Rect, Size};
|
14726
|
2 |
|
15306
|
3 |
use std::{ffi, ffi::CString, mem, num::NonZeroU32, ptr, slice};
|
14723
|
4 |
|
14740
|
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 |
|
14723
|
33 |
#[derive(Debug)]
|
|
34 |
pub struct Texture2D {
|
15306
|
35 |
pub handle: Option<NonZeroU32>,
|
14723
|
36 |
}
|
|
37 |
|
|
38 |
impl Drop for Texture2D {
|
|
39 |
fn drop(&mut self) {
|
15306
|
40 |
if let Some(handle) = self.handle {
|
14723
|
41 |
unsafe {
|
15306
|
42 |
gl::DeleteTextures(1, &handle.get());
|
14723
|
43 |
}
|
|
44 |
}
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
15306
|
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 |
|
14723
|
65 |
impl Texture2D {
|
15306
|
66 |
pub fn new(size: Size, internal_format: u32, format: u32, ty: u32, filter: u32) -> Self {
|
|
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,
|
|
77 |
format as u32,
|
|
78 |
ty,
|
|
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 |
|
14723
|
92 |
pub fn with_data(
|
|
93 |
data: &[u8],
|
|
94 |
data_stride: u32,
|
15306
|
95 |
size: Size,
|
14723
|
96 |
internal_format: u32,
|
|
97 |
format: u32,
|
|
98 |
ty: u32,
|
14726
|
99 |
filter: u32,
|
14723
|
100 |
) -> Self {
|
15306
|
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 |
}
|
14726
|
117 |
|
15306
|
118 |
tex_params(filter);
|
|
119 |
Self {
|
|
120 |
handle: Some(handle),
|
|
121 |
}
|
|
122 |
} else {
|
|
123 |
Self { handle: None }
|
14723
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
pub fn update(&self, region: Rect, data: &[u8], data_stride: u32, format: u32, ty: u32) {
|
15306
|
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(),
|
|
137 |
region.right(),
|
|
138 |
region.bottom(),
|
|
139 |
format, // data format
|
|
140 |
ty, // data type
|
|
141 |
data.as_ptr() as *const _, // data ptr
|
|
142 |
);
|
|
143 |
}
|
14723
|
144 |
}
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
#[derive(Debug)]
|
|
149 |
pub struct Buffer {
|
|
150 |
pub handle: u32,
|
|
151 |
pub ty: u32,
|
|
152 |
pub usage: u32,
|
|
153 |
}
|
|
154 |
|
|
155 |
impl Buffer {
|
|
156 |
pub fn empty(
|
|
157 |
ty: u32,
|
|
158 |
usage: u32,
|
|
159 |
//size: isize
|
|
160 |
) -> Buffer {
|
|
161 |
let mut buffer = 0;
|
|
162 |
|
|
163 |
unsafe {
|
|
164 |
gl::GenBuffers(1, &mut buffer);
|
|
165 |
gl::BindBuffer(ty, buffer);
|
|
166 |
//gl::BufferData(ty, size, ptr::null_mut(), usage);
|
|
167 |
}
|
|
168 |
|
|
169 |
Buffer {
|
|
170 |
handle: buffer,
|
|
171 |
ty,
|
|
172 |
usage,
|
|
173 |
}
|
|
174 |
}
|
14726
|
175 |
|
|
176 |
fn with_data(ty: u32, usage: u32, data: &[u8]) -> Buffer {
|
14723
|
177 |
let mut buffer = 0;
|
|
178 |
|
|
179 |
unsafe {
|
|
180 |
gl::GenBuffers(1, &mut buffer);
|
|
181 |
gl::BindBuffer(ty, buffer);
|
|
182 |
gl::BufferData(ty, data.len() as isize, data.as_ptr() as _, usage);
|
|
183 |
}
|
|
184 |
|
|
185 |
Buffer {
|
|
186 |
handle: buffer,
|
|
187 |
ty,
|
14726
|
188 |
usage,
|
14723
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
pub fn ty(&self) -> u32 {
|
|
193 |
self.ty
|
|
194 |
}
|
|
195 |
|
|
196 |
pub fn handle(&self) -> u32 {
|
|
197 |
self.handle
|
|
198 |
}
|
|
199 |
|
|
200 |
pub fn write_typed<T>(&self, data: &[T]) {
|
|
201 |
unsafe {
|
14726
|
202 |
let data =
|
|
203 |
slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * mem::size_of::<T>());
|
|
204 |
|
|
205 |
gl::BindBuffer(self.ty, self.handle);
|
|
206 |
gl::BufferData(
|
|
207 |
self.ty,
|
|
208 |
data.len() as isize,
|
|
209 |
data.as_ptr() as *const _ as *const _,
|
|
210 |
self.usage,
|
14723
|
211 |
);
|
|
212 |
}
|
|
213 |
}
|
14726
|
214 |
|
|
215 |
pub fn write(&self, data: &[u8]) {
|
14723
|
216 |
unsafe {
|
|
217 |
gl::BindBuffer(self.ty, self.handle);
|
14726
|
218 |
gl::BufferData(
|
|
219 |
self.ty,
|
|
220 |
data.len() as isize,
|
|
221 |
data.as_ptr() as *const _ as *const _,
|
|
222 |
self.usage,
|
|
223 |
);
|
14723
|
224 |
}
|
|
225 |
}
|
|
226 |
}
|
|
227 |
|
|
228 |
impl Drop for Buffer {
|
|
229 |
fn drop(&mut self) {
|
|
230 |
unsafe {
|
|
231 |
gl::DeleteBuffers(1, &self.handle);
|
|
232 |
}
|
|
233 |
}
|
|
234 |
}
|
|
235 |
|
|
236 |
#[derive(Debug)]
|
|
237 |
pub enum VariableBinding<'a> {
|
|
238 |
Attribute(&'a str, u32),
|
|
239 |
Uniform(&'a str, u32),
|
|
240 |
UniformBlock(&'a str, u32),
|
|
241 |
Sampler(&'a str, u32),
|
|
242 |
}
|
|
243 |
|
|
244 |
#[derive(Debug)]
|
|
245 |
pub struct Shader {
|
|
246 |
pub program: u32,
|
|
247 |
}
|
|
248 |
|
|
249 |
impl Drop for Shader {
|
|
250 |
fn drop(&mut self) {
|
|
251 |
unsafe {
|
|
252 |
gl::DeleteProgram(self.program);
|
|
253 |
}
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
impl Shader {
|
|
258 |
pub fn new<'a>(
|
|
259 |
vs: &str,
|
|
260 |
ps: Option<&str>,
|
14726
|
261 |
bindings: &[VariableBinding<'a>],
|
14723
|
262 |
) -> Result<Self, String> {
|
|
263 |
unsafe fn compile_shader(ty: u32, shdr: &str) -> Result<u32, String> {
|
|
264 |
let shader = gl::CreateShader(ty);
|
|
265 |
let len = shdr.len() as i32;
|
|
266 |
let shdr = shdr.as_ptr() as *const i8;
|
|
267 |
gl::ShaderSource(shader, 1, &shdr, &len);
|
|
268 |
gl::CompileShader(shader);
|
|
269 |
|
|
270 |
let mut success = 0i32;
|
|
271 |
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success as _);
|
|
272 |
|
|
273 |
if success == gl::FALSE as i32 {
|
|
274 |
let mut log_size = 0i32;
|
|
275 |
gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut log_size as _);
|
|
276 |
|
|
277 |
let mut log = vec![0u8; log_size as usize];
|
|
278 |
gl::GetShaderInfoLog(shader, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
|
|
279 |
|
|
280 |
gl::DeleteShader(shader);
|
|
281 |
Err(String::from_utf8_unchecked(log))
|
|
282 |
} else {
|
|
283 |
Ok(shader)
|
|
284 |
}
|
|
285 |
}
|
14726
|
286 |
|
14723
|
287 |
let vs = unsafe { compile_shader(gl::VERTEX_SHADER, vs)? };
|
|
288 |
let ps = if let Some(ps) = ps {
|
|
289 |
Some(unsafe { compile_shader(gl::FRAGMENT_SHADER, ps)? })
|
|
290 |
} else {
|
|
291 |
None
|
|
292 |
};
|
|
293 |
|
|
294 |
unsafe {
|
|
295 |
let program = gl::CreateProgram();
|
14726
|
296 |
|
14723
|
297 |
gl::AttachShader(program, vs);
|
|
298 |
if let Some(ps) = ps {
|
|
299 |
gl::AttachShader(program, ps);
|
|
300 |
}
|
|
301 |
|
|
302 |
for bind in bindings {
|
|
303 |
match bind {
|
|
304 |
&VariableBinding::Attribute(ref name, id) => {
|
|
305 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14726
|
306 |
gl::BindAttribLocation(
|
|
307 |
program,
|
|
308 |
id,
|
|
309 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
310 |
);
|
|
311 |
}
|
14723
|
312 |
_ => {}
|
|
313 |
}
|
|
314 |
}
|
|
315 |
|
|
316 |
gl::LinkProgram(program);
|
|
317 |
|
|
318 |
let mut success = 0i32;
|
|
319 |
gl::GetProgramiv(program, gl::LINK_STATUS, &mut success);
|
|
320 |
if success == gl::FALSE as i32 {
|
|
321 |
let mut log_size = 0i32;
|
|
322 |
gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut log_size as _);
|
|
323 |
|
|
324 |
let mut log = vec![0u8; log_size as usize];
|
|
325 |
gl::GetProgramInfoLog(program, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
|
|
326 |
|
|
327 |
gl::DeleteProgram(program);
|
|
328 |
return Err(String::from_utf8_unchecked(log));
|
|
329 |
}
|
|
330 |
|
|
331 |
//gl::DetachShader(program, vs);
|
|
332 |
if let Some(ps) = ps {
|
|
333 |
//gl::DetachShader(program, ps);
|
|
334 |
}
|
|
335 |
|
|
336 |
gl::UseProgram(program);
|
|
337 |
|
|
338 |
// after linking we setup sampler bindings as specified in the shader
|
|
339 |
for bind in bindings {
|
|
340 |
match bind {
|
|
341 |
VariableBinding::Uniform(name, id) => {
|
|
342 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14726
|
343 |
let index = gl::GetUniformLocation(
|
|
344 |
program,
|
|
345 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
346 |
);
|
14723
|
347 |
|
|
348 |
// TODO: impl for block?
|
14726
|
349 |
}
|
14723
|
350 |
VariableBinding::UniformBlock(name, id) => {
|
|
351 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14726
|
352 |
let index = gl::GetUniformBlockIndex(
|
|
353 |
program,
|
|
354 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
355 |
);
|
14723
|
356 |
|
|
357 |
gl::UniformBlockBinding(program, index, *id);
|
|
358 |
}
|
|
359 |
VariableBinding::Sampler(name, id) => {
|
|
360 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14726
|
361 |
let index = gl::GetUniformLocation(
|
|
362 |
program,
|
|
363 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
364 |
);
|
|
365 |
|
14723
|
366 |
gl::Uniform1i(index, *id as i32);
|
14726
|
367 |
}
|
14723
|
368 |
_ => {}
|
|
369 |
}
|
|
370 |
}
|
|
371 |
|
14726
|
372 |
Ok(Shader { program })
|
14723
|
373 |
}
|
|
374 |
}
|
|
375 |
|
|
376 |
pub fn bind(&self) {
|
|
377 |
unsafe {
|
|
378 |
gl::UseProgram(self.program);
|
|
379 |
}
|
|
380 |
}
|
|
381 |
|
|
382 |
pub fn set_matrix(&self, name: &str, matrix: *const f32) {
|
|
383 |
unsafe {
|
|
384 |
let c_str = CString::new(name).unwrap();
|
14726
|
385 |
let index = gl::GetUniformLocation(
|
|
386 |
self.program,
|
|
387 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
388 |
);
|
|
389 |
|
14723
|
390 |
gl::UniformMatrix4fv(index, 1, gl::FALSE, matrix);
|
|
391 |
}
|
|
392 |
}
|
|
393 |
|
|
394 |
pub fn bind_texture_2d(&self, index: u32, texture: &Texture2D) {
|
|
395 |
self.bind();
|
14726
|
396 |
|
15306
|
397 |
if let Some(handle) = texture.handle {
|
|
398 |
unsafe {
|
|
399 |
gl::ActiveTexture(gl::TEXTURE0 + index);
|
|
400 |
gl::BindTexture(gl::TEXTURE_2D, handle.get());
|
|
401 |
}
|
14723
|
402 |
}
|
|
403 |
}
|
|
404 |
}
|
|
405 |
|
|
406 |
pub enum InputFormat {
|
|
407 |
Float(u32, bool),
|
|
408 |
Integer(u32),
|
|
409 |
}
|
|
410 |
|
|
411 |
pub struct InputElement {
|
|
412 |
pub shader_slot: u32,
|
|
413 |
pub buffer_slot: u32,
|
|
414 |
pub format: InputFormat,
|
|
415 |
pub components: u32,
|
|
416 |
pub stride: u32,
|
|
417 |
pub offset: u32,
|
|
418 |
}
|
|
419 |
|
14726
|
420 |
// TODO:
|
14723
|
421 |
pub struct InputLayout {
|
|
422 |
pub elements: Vec<InputElement>,
|
|
423 |
}
|
|
424 |
|
|
425 |
pub struct LayoutGuard {
|
14726
|
426 |
vao: u32,
|
14723
|
427 |
}
|
|
428 |
|
|
429 |
impl Drop for LayoutGuard {
|
|
430 |
fn drop(&mut self) {
|
|
431 |
unsafe {
|
|
432 |
gl::DeleteVertexArrays(1, [self.vao].as_ptr());
|
|
433 |
gl::BindVertexArray(0);
|
|
434 |
}
|
|
435 |
}
|
|
436 |
}
|
|
437 |
|
|
438 |
impl InputLayout {
|
|
439 |
pub fn new(elements: Vec<InputElement>) -> Self {
|
14726
|
440 |
InputLayout { elements }
|
14723
|
441 |
}
|
|
442 |
|
14726
|
443 |
pub fn bind(
|
|
444 |
&mut self,
|
|
445 |
buffers: &[(u32, &Buffer)],
|
|
446 |
index_buffer: Option<&Buffer>,
|
|
447 |
) -> LayoutGuard {
|
14723
|
448 |
let mut vao = 0;
|
14726
|
449 |
|
14723
|
450 |
unsafe {
|
|
451 |
gl::GenVertexArrays(1, &mut vao);
|
|
452 |
gl::BindVertexArray(vao);
|
|
453 |
}
|
14726
|
454 |
|
14723
|
455 |
for &(slot, ref buffer) in buffers {
|
|
456 |
unsafe {
|
|
457 |
gl::BindBuffer(buffer.ty(), buffer.handle());
|
|
458 |
}
|
14726
|
459 |
|
14723
|
460 |
for attr in self.elements.iter().filter(|a| a.buffer_slot == slot) {
|
|
461 |
unsafe {
|
|
462 |
gl::EnableVertexAttribArray(attr.shader_slot);
|
|
463 |
match attr.format {
|
|
464 |
InputFormat::Float(fmt, normalized) => {
|
|
465 |
gl::VertexAttribPointer(
|
|
466 |
attr.shader_slot,
|
|
467 |
attr.components as i32,
|
|
468 |
fmt,
|
14726
|
469 |
if normalized { gl::TRUE } else { gl::FALSE },
|
14723
|
470 |
attr.stride as i32,
|
14726
|
471 |
attr.offset as *const _,
|
14723
|
472 |
);
|
|
473 |
}
|
|
474 |
InputFormat::Integer(fmt) => {
|
|
475 |
gl::VertexAttribIPointer(
|
|
476 |
attr.shader_slot,
|
|
477 |
attr.components as i32,
|
|
478 |
fmt,
|
|
479 |
attr.stride as i32,
|
14726
|
480 |
attr.offset as *const _,
|
14723
|
481 |
);
|
|
482 |
}
|
|
483 |
}
|
|
484 |
}
|
|
485 |
}
|
|
486 |
}
|
|
487 |
|
|
488 |
if let Some(buf) = index_buffer {
|
|
489 |
unsafe {
|
|
490 |
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, buf.handle());
|
|
491 |
}
|
|
492 |
}
|
|
493 |
|
14726
|
494 |
LayoutGuard { vao }
|
14723
|
495 |
}
|
|
496 |
}
|