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