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 {
|
15782
|
35 |
handle: Option<NonZeroU32>,
|
|
36 |
size: Size,
|
14723
|
37 |
}
|
|
38 |
|
|
39 |
impl Drop for Texture2D {
|
|
40 |
fn drop(&mut self) {
|
15306
|
41 |
if let Some(handle) = self.handle {
|
14723
|
42 |
unsafe {
|
15306
|
43 |
gl::DeleteTextures(1, &handle.get());
|
14723
|
44 |
}
|
|
45 |
}
|
|
46 |
}
|
|
47 |
}
|
|
48 |
|
15306
|
49 |
fn new_texture() -> Option<NonZeroU32> {
|
|
50 |
let mut handle = 0;
|
|
51 |
unsafe {
|
|
52 |
gl::GenTextures(1, &mut handle);
|
|
53 |
}
|
|
54 |
NonZeroU32::new(handle)
|
|
55 |
}
|
|
56 |
|
15782
|
57 |
fn tex_params(filter: TextureFilter) {
|
15306
|
58 |
unsafe {
|
|
59 |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
|
|
60 |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);
|
|
61 |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, filter as i32);
|
|
62 |
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, filter as i32);
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
15782
|
66 |
#[derive(Clone, Copy, Debug)]
|
|
67 |
pub enum TextureFormat {
|
|
68 |
Rgba = gl::RGBA as isize,
|
|
69 |
}
|
|
70 |
|
|
71 |
#[derive(Clone, Copy, Debug)]
|
|
72 |
pub enum TextureInternalFormat {
|
|
73 |
Rgba8 = gl::RGBA as isize,
|
|
74 |
}
|
|
75 |
|
|
76 |
#[derive(Clone, Copy, Debug)]
|
|
77 |
pub enum TextureDataType {
|
|
78 |
UnsignedByte = gl::UNSIGNED_BYTE as isize,
|
|
79 |
}
|
|
80 |
|
|
81 |
#[derive(Clone, Copy, Debug)]
|
|
82 |
pub enum TextureFilter {
|
|
83 |
Nearest = gl::NEAREST as isize,
|
|
84 |
Linear = gl::LINEAR as isize,
|
|
85 |
}
|
|
86 |
|
|
87 |
#[inline]
|
|
88 |
fn get_u32(value: Option<NonZeroU32>) -> u32 {
|
|
89 |
value.map_or(0, |v| v.get())
|
|
90 |
}
|
|
91 |
|
|
92 |
fn is_out_of_bounds(data: &[u8], data_stride: Option<NonZeroU32>, texture_size: Size) -> bool {
|
|
93 |
let data_stride = get_u32(data_stride);
|
|
94 |
data_stride == 0 && texture_size.area() * 4 > data.len()
|
|
95 |
|| data_stride != 0
|
|
96 |
&& texture_size.width > data_stride as usize
|
|
97 |
&& (texture_size.height * data_stride as usize) * 4 > data.len()
|
|
98 |
}
|
|
99 |
|
14723
|
100 |
impl Texture2D {
|
15782
|
101 |
pub fn new(size: Size, internal_format: TextureInternalFormat, filter: TextureFilter) -> Self {
|
15306
|
102 |
if let Some(handle) = new_texture() {
|
|
103 |
unsafe {
|
|
104 |
gl::BindTexture(gl::TEXTURE_2D, handle.get());
|
|
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,
|
15782
|
112 |
TextureFormat::Rgba as u32,
|
|
113 |
TextureDataType::UnsignedByte as u32,
|
15306
|
114 |
std::ptr::null(),
|
|
115 |
)
|
|
116 |
}
|
|
117 |
|
|
118 |
tex_params(filter);
|
|
119 |
Self {
|
|
120 |
handle: Some(handle),
|
15782
|
121 |
size,
|
15306
|
122 |
}
|
|
123 |
} else {
|
15782
|
124 |
Self { handle: None, size }
|
15306
|
125 |
}
|
|
126 |
}
|
|
127 |
|
14723
|
128 |
pub fn with_data(
|
|
129 |
data: &[u8],
|
15782
|
130 |
data_stride: Option<NonZeroU32>,
|
15306
|
131 |
size: Size,
|
15782
|
132 |
internal_format: TextureInternalFormat,
|
|
133 |
format: TextureFormat,
|
15783
|
134 |
data_type: TextureDataType,
|
15782
|
135 |
filter: TextureFilter,
|
14723
|
136 |
) -> Self {
|
15782
|
137 |
if is_out_of_bounds(data, data_stride, size) {
|
|
138 |
return Self { handle: None, size };
|
|
139 |
}
|
|
140 |
|
15306
|
141 |
if let Some(handle) = new_texture() {
|
|
142 |
unsafe {
|
|
143 |
gl::BindTexture(gl::TEXTURE_2D, handle.get());
|
15782
|
144 |
gl::PixelStorei(gl::UNPACK_ROW_LENGTH, get_u32(data_stride) as i32);
|
15306
|
145 |
gl::TexImage2D(
|
|
146 |
gl::TEXTURE_2D,
|
|
147 |
0,
|
|
148 |
internal_format as i32,
|
|
149 |
size.width as i32,
|
|
150 |
size.height as i32,
|
|
151 |
0,
|
|
152 |
format as u32,
|
15783
|
153 |
data_type as u32,
|
15306
|
154 |
data.as_ptr() as *const _,
|
|
155 |
)
|
|
156 |
}
|
14726
|
157 |
|
15306
|
158 |
tex_params(filter);
|
|
159 |
Self {
|
|
160 |
handle: Some(handle),
|
15782
|
161 |
size,
|
15306
|
162 |
}
|
|
163 |
} else {
|
15782
|
164 |
Self { handle: None, size }
|
14723
|
165 |
}
|
|
166 |
}
|
|
167 |
|
15782
|
168 |
pub fn update(
|
|
169 |
&self,
|
|
170 |
region: Rect,
|
|
171 |
data: &[u8],
|
|
172 |
data_stride: Option<NonZeroU32>,
|
|
173 |
format: TextureFormat,
|
15783
|
174 |
data_type: TextureDataType,
|
15782
|
175 |
) {
|
|
176 |
if is_out_of_bounds(data, data_stride, self.size) {
|
|
177 |
return;
|
|
178 |
}
|
|
179 |
|
15306
|
180 |
if let Some(handle) = self.handle {
|
|
181 |
unsafe {
|
|
182 |
gl::BindTexture(gl::TEXTURE_2D, handle.get());
|
15782
|
183 |
gl::PixelStorei(gl::UNPACK_ROW_LENGTH, get_u32(data_stride) as i32);
|
15306
|
184 |
gl::TexSubImage2D(
|
|
185 |
gl::TEXTURE_2D,
|
15782
|
186 |
0,
|
|
187 |
region.left(),
|
15306
|
188 |
region.top(),
|
15310
|
189 |
region.width() as i32,
|
|
190 |
region.height() as i32,
|
15782
|
191 |
format as u32,
|
15783
|
192 |
data_type as u32,
|
15782
|
193 |
data.as_ptr() as *const _,
|
15306
|
194 |
);
|
|
195 |
}
|
14723
|
196 |
}
|
|
197 |
}
|
15310
|
198 |
|
|
199 |
pub fn retrieve(&self, data: &mut [u8]) {
|
15782
|
200 |
if self.size.area() * 4 > data.len() {
|
|
201 |
return;
|
|
202 |
}
|
|
203 |
|
15310
|
204 |
if let Some(handle) = self.handle {
|
|
205 |
unsafe {
|
|
206 |
gl::BindTexture(gl::TEXTURE_2D, handle.get());
|
|
207 |
gl::GetTexImage(
|
|
208 |
gl::TEXTURE_2D,
|
15782
|
209 |
0,
|
|
210 |
TextureFormat::Rgba as u32,
|
|
211 |
TextureDataType::UnsignedByte as u32,
|
|
212 |
data.as_mut_ptr() as *mut _,
|
15310
|
213 |
);
|
|
214 |
}
|
|
215 |
}
|
|
216 |
}
|
14723
|
217 |
}
|
|
218 |
|
15783
|
219 |
#[derive(Clone, Copy, Debug)]
|
|
220 |
#[repr(u32)]
|
|
221 |
pub enum BufferType {
|
|
222 |
Array = gl::ARRAY_BUFFER,
|
|
223 |
ElementArray = gl::ELEMENT_ARRAY_BUFFER,
|
|
224 |
}
|
|
225 |
|
|
226 |
#[derive(Clone, Copy, Debug)]
|
|
227 |
#[repr(u32)]
|
|
228 |
pub enum BufferUsage {
|
|
229 |
DynamicDraw = gl::DYNAMIC_DRAW,
|
|
230 |
}
|
|
231 |
|
14723
|
232 |
#[derive(Debug)]
|
|
233 |
pub struct Buffer {
|
15783
|
234 |
pub handle: Option<NonZeroU32>,
|
|
235 |
pub buffer_type: BufferType,
|
|
236 |
pub usage: BufferUsage,
|
14723
|
237 |
}
|
|
238 |
|
|
239 |
impl Buffer {
|
15783
|
240 |
pub fn empty(buffer_type: BufferType, usage: BufferUsage) -> Buffer {
|
14723
|
241 |
let mut buffer = 0;
|
|
242 |
|
|
243 |
unsafe {
|
|
244 |
gl::GenBuffers(1, &mut buffer);
|
|
245 |
}
|
|
246 |
|
|
247 |
Buffer {
|
15783
|
248 |
handle: NonZeroU32::new(buffer),
|
|
249 |
buffer_type: buffer_type,
|
14723
|
250 |
usage,
|
|
251 |
}
|
|
252 |
}
|
14726
|
253 |
|
15783
|
254 |
fn with_data(buffer_type: BufferType, usage: BufferUsage, data: &[u8]) -> Buffer {
|
14723
|
255 |
let mut buffer = 0;
|
|
256 |
|
|
257 |
unsafe {
|
|
258 |
gl::GenBuffers(1, &mut buffer);
|
15783
|
259 |
if buffer != 0 {
|
|
260 |
gl::BindBuffer(buffer_type as u32, buffer);
|
|
261 |
gl::BufferData(
|
|
262 |
buffer_type as u32,
|
|
263 |
data.len() as isize,
|
|
264 |
data.as_ptr() as _,
|
|
265 |
usage as u32,
|
|
266 |
);
|
|
267 |
}
|
14723
|
268 |
}
|
|
269 |
|
|
270 |
Buffer {
|
15783
|
271 |
handle: NonZeroU32::new(buffer),
|
|
272 |
buffer_type,
|
14726
|
273 |
usage,
|
14723
|
274 |
}
|
|
275 |
}
|
|
276 |
|
15783
|
277 |
pub fn ty(&self) -> BufferType {
|
|
278 |
self.buffer_type
|
14723
|
279 |
}
|
|
280 |
|
15783
|
281 |
pub fn handle(&self) -> Option<NonZeroU32> {
|
14723
|
282 |
self.handle
|
|
283 |
}
|
|
284 |
|
|
285 |
pub fn write_typed<T>(&self, data: &[T]) {
|
15783
|
286 |
if let Some(handle) = self.handle {
|
|
287 |
unsafe {
|
|
288 |
gl::BindBuffer(self.buffer_type as u32, handle.get());
|
|
289 |
gl::BufferData(
|
|
290 |
self.buffer_type as u32,
|
|
291 |
(data.len() * mem::size_of::<T>()) as isize,
|
|
292 |
data.as_ptr() as *const _,
|
|
293 |
self.usage as u32,
|
|
294 |
);
|
|
295 |
}
|
14723
|
296 |
}
|
|
297 |
}
|
14726
|
298 |
|
|
299 |
pub fn write(&self, data: &[u8]) {
|
15783
|
300 |
if let Some(handle) = self.handle {
|
|
301 |
unsafe {
|
|
302 |
gl::BindBuffer(self.buffer_type as u32, handle.get());
|
|
303 |
gl::BufferData(
|
|
304 |
self.buffer_type as u32,
|
|
305 |
data.len() as isize,
|
|
306 |
data.as_ptr() as *const _,
|
|
307 |
self.usage as u32,
|
|
308 |
);
|
|
309 |
}
|
14723
|
310 |
}
|
|
311 |
}
|
|
312 |
}
|
|
313 |
|
|
314 |
impl Drop for Buffer {
|
|
315 |
fn drop(&mut self) {
|
15783
|
316 |
if let Some(handle) = self.handle {
|
|
317 |
let handle = handle.get();
|
|
318 |
unsafe {
|
|
319 |
gl::DeleteBuffers(1, &handle);
|
|
320 |
}
|
14723
|
321 |
}
|
|
322 |
}
|
|
323 |
}
|
|
324 |
|
|
325 |
#[derive(Debug)]
|
|
326 |
pub enum VariableBinding<'a> {
|
|
327 |
Attribute(&'a str, u32),
|
|
328 |
Uniform(&'a str, u32),
|
|
329 |
UniformBlock(&'a str, u32),
|
|
330 |
Sampler(&'a str, u32),
|
|
331 |
}
|
|
332 |
|
|
333 |
#[derive(Debug)]
|
|
334 |
pub struct Shader {
|
|
335 |
pub program: u32,
|
|
336 |
}
|
|
337 |
|
|
338 |
impl Drop for Shader {
|
|
339 |
fn drop(&mut self) {
|
|
340 |
unsafe {
|
|
341 |
gl::DeleteProgram(self.program);
|
|
342 |
}
|
|
343 |
}
|
|
344 |
}
|
|
345 |
|
|
346 |
impl Shader {
|
|
347 |
pub fn new<'a>(
|
|
348 |
vs: &str,
|
|
349 |
ps: Option<&str>,
|
14726
|
350 |
bindings: &[VariableBinding<'a>],
|
14723
|
351 |
) -> Result<Self, String> {
|
15783
|
352 |
unsafe fn compile_shader(shader_type: u32, shader_code: &str) -> Result<u32, String> {
|
|
353 |
let shader = gl::CreateShader(shader_type);
|
|
354 |
let len = shader_code.len() as i32;
|
|
355 |
let code_strings = shader_code.as_ptr() as *const i8;
|
|
356 |
gl::ShaderSource(shader, 1, &code_strings, &len);
|
14723
|
357 |
gl::CompileShader(shader);
|
|
358 |
|
|
359 |
let mut success = 0i32;
|
|
360 |
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success as _);
|
|
361 |
|
|
362 |
if success == gl::FALSE as i32 {
|
|
363 |
let mut log_size = 0i32;
|
|
364 |
gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut log_size as _);
|
|
365 |
|
|
366 |
let mut log = vec![0u8; log_size as usize];
|
|
367 |
gl::GetShaderInfoLog(shader, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
|
|
368 |
|
|
369 |
gl::DeleteShader(shader);
|
|
370 |
Err(String::from_utf8_unchecked(log))
|
|
371 |
} else {
|
|
372 |
Ok(shader)
|
|
373 |
}
|
|
374 |
}
|
14726
|
375 |
|
14723
|
376 |
let vs = unsafe { compile_shader(gl::VERTEX_SHADER, vs)? };
|
|
377 |
let ps = if let Some(ps) = ps {
|
|
378 |
Some(unsafe { compile_shader(gl::FRAGMENT_SHADER, ps)? })
|
|
379 |
} else {
|
|
380 |
None
|
|
381 |
};
|
|
382 |
|
|
383 |
unsafe {
|
|
384 |
let program = gl::CreateProgram();
|
14726
|
385 |
|
14723
|
386 |
gl::AttachShader(program, vs);
|
|
387 |
if let Some(ps) = ps {
|
|
388 |
gl::AttachShader(program, ps);
|
|
389 |
}
|
|
390 |
|
|
391 |
for bind in bindings {
|
|
392 |
match bind {
|
|
393 |
&VariableBinding::Attribute(ref name, id) => {
|
|
394 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14726
|
395 |
gl::BindAttribLocation(
|
|
396 |
program,
|
|
397 |
id,
|
|
398 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
399 |
);
|
|
400 |
}
|
14723
|
401 |
_ => {}
|
|
402 |
}
|
|
403 |
}
|
|
404 |
|
|
405 |
gl::LinkProgram(program);
|
|
406 |
|
|
407 |
let mut success = 0i32;
|
|
408 |
gl::GetProgramiv(program, gl::LINK_STATUS, &mut success);
|
|
409 |
if success == gl::FALSE as i32 {
|
|
410 |
let mut log_size = 0i32;
|
|
411 |
gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut log_size as _);
|
|
412 |
|
|
413 |
let mut log = vec![0u8; log_size as usize];
|
|
414 |
gl::GetProgramInfoLog(program, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
|
|
415 |
|
|
416 |
gl::DeleteProgram(program);
|
|
417 |
return Err(String::from_utf8_unchecked(log));
|
|
418 |
}
|
|
419 |
|
|
420 |
//gl::DetachShader(program, vs);
|
|
421 |
if let Some(ps) = ps {
|
|
422 |
//gl::DetachShader(program, ps);
|
|
423 |
}
|
|
424 |
|
|
425 |
gl::UseProgram(program);
|
|
426 |
|
|
427 |
// after linking we setup sampler bindings as specified in the shader
|
|
428 |
for bind in bindings {
|
|
429 |
match bind {
|
|
430 |
VariableBinding::Uniform(name, id) => {
|
|
431 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14726
|
432 |
let index = gl::GetUniformLocation(
|
|
433 |
program,
|
|
434 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
435 |
);
|
14723
|
436 |
|
|
437 |
// TODO: impl for block?
|
14726
|
438 |
}
|
14723
|
439 |
VariableBinding::UniformBlock(name, id) => {
|
|
440 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14726
|
441 |
let index = gl::GetUniformBlockIndex(
|
|
442 |
program,
|
|
443 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
444 |
);
|
14723
|
445 |
|
|
446 |
gl::UniformBlockBinding(program, index, *id);
|
|
447 |
}
|
|
448 |
VariableBinding::Sampler(name, id) => {
|
|
449 |
let c_str = CString::new(name.as_bytes()).unwrap();
|
14726
|
450 |
let index = gl::GetUniformLocation(
|
|
451 |
program,
|
|
452 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
453 |
);
|
|
454 |
|
14723
|
455 |
gl::Uniform1i(index, *id as i32);
|
14726
|
456 |
}
|
14723
|
457 |
_ => {}
|
|
458 |
}
|
|
459 |
}
|
|
460 |
|
14726
|
461 |
Ok(Shader { program })
|
14723
|
462 |
}
|
|
463 |
}
|
|
464 |
|
|
465 |
pub fn bind(&self) {
|
|
466 |
unsafe {
|
|
467 |
gl::UseProgram(self.program);
|
|
468 |
}
|
|
469 |
}
|
|
470 |
|
|
471 |
pub fn set_matrix(&self, name: &str, matrix: *const f32) {
|
|
472 |
unsafe {
|
|
473 |
let c_str = CString::new(name).unwrap();
|
14726
|
474 |
let index = gl::GetUniformLocation(
|
|
475 |
self.program,
|
|
476 |
c_str.to_bytes_with_nul().as_ptr() as *const _,
|
|
477 |
);
|
|
478 |
|
14723
|
479 |
gl::UniformMatrix4fv(index, 1, gl::FALSE, matrix);
|
|
480 |
}
|
|
481 |
}
|
|
482 |
|
|
483 |
pub fn bind_texture_2d(&self, index: u32, texture: &Texture2D) {
|
|
484 |
self.bind();
|
14726
|
485 |
|
15306
|
486 |
if let Some(handle) = texture.handle {
|
|
487 |
unsafe {
|
|
488 |
gl::ActiveTexture(gl::TEXTURE0 + index);
|
|
489 |
gl::BindTexture(gl::TEXTURE_2D, handle.get());
|
|
490 |
}
|
14723
|
491 |
}
|
|
492 |
}
|
|
493 |
}
|
|
494 |
|
|
495 |
pub enum InputFormat {
|
|
496 |
Float(u32, bool),
|
|
497 |
Integer(u32),
|
|
498 |
}
|
|
499 |
|
|
500 |
pub struct InputElement {
|
|
501 |
pub shader_slot: u32,
|
|
502 |
pub buffer_slot: u32,
|
|
503 |
pub format: InputFormat,
|
|
504 |
pub components: u32,
|
|
505 |
pub stride: u32,
|
|
506 |
pub offset: u32,
|
|
507 |
}
|
|
508 |
|
14726
|
509 |
// TODO:
|
14723
|
510 |
pub struct InputLayout {
|
|
511 |
pub elements: Vec<InputElement>,
|
|
512 |
}
|
|
513 |
|
|
514 |
pub struct LayoutGuard {
|
14726
|
515 |
vao: u32,
|
14723
|
516 |
}
|
|
517 |
|
|
518 |
impl Drop for LayoutGuard {
|
|
519 |
fn drop(&mut self) {
|
|
520 |
unsafe {
|
|
521 |
gl::DeleteVertexArrays(1, [self.vao].as_ptr());
|
|
522 |
gl::BindVertexArray(0);
|
|
523 |
}
|
|
524 |
}
|
|
525 |
}
|
|
526 |
|
|
527 |
impl InputLayout {
|
|
528 |
pub fn new(elements: Vec<InputElement>) -> Self {
|
14726
|
529 |
InputLayout { elements }
|
14723
|
530 |
}
|
|
531 |
|
14726
|
532 |
pub fn bind(
|
|
533 |
&mut self,
|
|
534 |
buffers: &[(u32, &Buffer)],
|
|
535 |
index_buffer: Option<&Buffer>,
|
|
536 |
) -> LayoutGuard {
|
14723
|
537 |
let mut vao = 0;
|
14726
|
538 |
|
14723
|
539 |
unsafe {
|
|
540 |
gl::GenVertexArrays(1, &mut vao);
|
|
541 |
gl::BindVertexArray(vao);
|
|
542 |
}
|
14726
|
543 |
|
14723
|
544 |
for &(slot, ref buffer) in buffers {
|
15783
|
545 |
if let Some(handle) = buffer.handle() {
|
|
546 |
unsafe {
|
|
547 |
gl::BindBuffer(buffer.ty() as u32, handle.get());
|
|
548 |
}
|
14723
|
549 |
}
|
14726
|
550 |
|
14723
|
551 |
for attr in self.elements.iter().filter(|a| a.buffer_slot == slot) {
|
|
552 |
unsafe {
|
|
553 |
gl::EnableVertexAttribArray(attr.shader_slot);
|
|
554 |
match attr.format {
|
|
555 |
InputFormat::Float(fmt, normalized) => {
|
|
556 |
gl::VertexAttribPointer(
|
|
557 |
attr.shader_slot,
|
|
558 |
attr.components as i32,
|
|
559 |
fmt,
|
14726
|
560 |
if normalized { gl::TRUE } else { gl::FALSE },
|
14723
|
561 |
attr.stride as i32,
|
14726
|
562 |
attr.offset as *const _,
|
14723
|
563 |
);
|
|
564 |
}
|
|
565 |
InputFormat::Integer(fmt) => {
|
|
566 |
gl::VertexAttribIPointer(
|
|
567 |
attr.shader_slot,
|
|
568 |
attr.components as i32,
|
|
569 |
fmt,
|
|
570 |
attr.stride as i32,
|
14726
|
571 |
attr.offset as *const _,
|
14723
|
572 |
);
|
|
573 |
}
|
|
574 |
}
|
|
575 |
}
|
|
576 |
}
|
|
577 |
}
|
|
578 |
|
|
579 |
if let Some(buf) = index_buffer {
|
15783
|
580 |
if let Some(handle) = buf.handle() {
|
|
581 |
unsafe {
|
|
582 |
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, handle.get());
|
|
583 |
}
|
14723
|
584 |
}
|
|
585 |
}
|
|
586 |
|
14726
|
587 |
LayoutGuard { vao }
|
14723
|
588 |
}
|
|
589 |
}
|