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