optic-render 0.0.1

OpenGL 4.6 renderer for Optic engine — EGL context, shaders, meshes, textures, cameras, canvas
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
use cgmath;
use optic_core::{ATTRType, DrawMode, OpticError, OpticErrorKind, OpticResult};

use crate::GL;

use gl::types::{GLenum, GLint, GLsizei, GLsizeiptr};
use std::ffi::c_void;
use std::ptr;

use crate::asset::attr::ATTRInfo;
use crate::asset::attr::DataType;
use crate::handles::instance::InstanceBuffer;
use crate::handles::Shader;

/// Low-level OpenGL mesh handle wrapping VAO, VBO, IBO, and instance state.
///
/// Created by [`Mesh3DFile::ship`](crate::asset::Mesh3DFile::ship) or
/// [`Mesh2DFile::ship`](crate::asset::Mesh2DFile::ship). Contains the vertex
/// layouts, draw mode, index state, and instance buffer binding.
#[derive(Clone, Debug)]
pub struct MeshHandle {
    pub layouts: Vec<(ATTRInfo, u32)>,
    pub draw_mode: DrawMode,
    pub has_indices: bool,
    pub vert_count: u32,
    pub ind_count: u32,
    pub vao_id: u32,
    pub buf_id: u32,
    pub ind_id: u32,
    pub vert_stride: u32,
    pub instance_buf_id: u32,
    pub instance_count: u32,
}

impl MeshHandle {
    /// Issues the draw call for this mesh (instanced or non-instanced, indexed or array).
    pub fn draw(&self) {
        GL::bind_vao(self.vao_id);
        if self.instance_buf_id != 0 && self.instance_count > 0 {
            match self.has_indices {
                false => self.draw_array_instanced(),
                true => {
                    GL::bind_ebo(self.ind_id);
                    self.draw_indexed_instanced();
                }
            }
        } else {
            match self.has_indices {
                false => self.draw_array(),
                true => {
                    GL::bind_ebo(self.ind_id);
                    self.draw_indexed();
                }
            }
        }
    }

    fn draw_indexed(&self) {
        unsafe {
            gl::DrawElements(
                match_draw_mode(&self.draw_mode),
                self.ind_count as GLsizei,
                gl::UNSIGNED_INT,
                ptr::null(),
            );
        }
    }

    fn draw_array(&self) {
        unsafe {
            gl::DrawArrays(match_draw_mode(&self.draw_mode), 0, self.vert_count as GLsizei);
        }
    }

    fn draw_indexed_instanced(&self) {
        unsafe {
            gl::DrawElementsInstanced(
                match_draw_mode(&self.draw_mode),
                self.ind_count as GLsizei,
                gl::UNSIGNED_INT,
                ptr::null(),
                self.instance_count as GLsizei,
            );
        }
    }

    fn draw_array_instanced(&self) {
        unsafe {
            gl::DrawArraysInstanced(
                match_draw_mode(&self.draw_mode),
                0,
                self.vert_count as GLsizei,
                self.instance_count as GLsizei,
            );
        }
    }

    /// Binds an instance buffer to this mesh for instanced rendering.
    ///
    /// Instance attributes are appended after the vertex attributes using
    /// `glVertexAttribDivisor(1)`.
    pub fn set_instances(&mut self, buffer: &InstanceBuffer) {
        if buffer.count() == 0 {
            self.instance_buf_id = 0;
            self.instance_count = 0;
            return;
        }

        GL::bind_vao(self.vao_id);
        GL::bind_buffer(buffer.buf_id);

        let base_attr = self.layouts.len() as u32;
        let mut offset = 0usize;
        for (i, (info, _)) in buffer.layouts.iter().enumerate() {
            let location = base_attr + i as u32;
            let attr_size = info.elem_count * info.byte_count;
            set_attr_layout(info, location, buffer.stride as usize, offset);
            unsafe { gl::VertexAttribDivisor(location, 1); }
            offset += attr_size;
        }

        self.instance_buf_id = buffer.buf_id;
        self.instance_count = buffer.count();
    }

    /// Updates a single vertex attribute value on the GPU.
    ///
    /// The type `D` must match the attribute's declared type at creation time.
    pub fn update_vertex<D: DataType>(&self, index: u32, attr_index: usize, value: D) -> OpticResult<()> {
        if index >= self.vert_count {
            return Err(OpticError::new(
                OpticErrorKind::Custom,
                &format!("vertex index {index} out of bounds (count: {})", self.vert_count),
            ));
        }
        if attr_index >= self.layouts.len() {
            return Err(OpticError::new(
                OpticErrorKind::Custom,
                &format!("attr index {attr_index} out of bounds (layout count: {})", self.layouts.len()),
            ));
        }
        let slot_info = &self.layouts[attr_index].0;
        if slot_info.byte_count != D::BYTE_COUNT || slot_info.elem_count != D::ELEM_COUNT || slot_info.typ != D::ATTR_FORMAT {
            return Err(OpticError::new(
                OpticErrorKind::Custom,
                &format!(
                    "type mismatch: attribute {} expects {:?}[{}], got {:?}[{}]",
                    slot_info.name.as_string(),
                    slot_info.typ,
                    slot_info.elem_count,
                    D::ATTR_FORMAT,
                    D::ELEM_COUNT,
                ),
            ));
        }
        let bytes = value.u8ify();
        let off = self.compute_vert_attr_offset(attr_index, index);
        subfill_buffer(self.buf_id, off, &bytes);
        Ok(())
    }

    /// Reads a single vertex attribute value from the GPU.
    pub fn get_vertex<D: DataType>(&self, index: u32, attr_index: usize) -> OpticResult<D> {
        if index >= self.vert_count {
            return Err(OpticError::new(
                OpticErrorKind::Custom,
                &format!("vertex index {index} out of bounds (count: {})", self.vert_count),
            ));
        }
        if attr_index >= self.layouts.len() {
            return Err(OpticError::new(
                OpticErrorKind::Custom,
                &format!("attr index {attr_index} out of bounds (layout count: {})", self.layouts.len()),
            ));
        }
        let slot_info = &self.layouts[attr_index].0;
        if slot_info.byte_count != D::BYTE_COUNT || slot_info.elem_count != D::ELEM_COUNT || slot_info.typ != D::ATTR_FORMAT {
            return Err(OpticError::new(
                OpticErrorKind::Custom,
                &format!(
                    "type mismatch: attribute {} expects {:?}[{}], got {:?}[{}]",
                    slot_info.name.as_string(),
                    slot_info.typ,
                    slot_info.elem_count,
                    D::ATTR_FORMAT,
                    D::ELEM_COUNT,
                ),
            ));
        }
        let off = self.compute_vert_attr_offset(attr_index, index);
        let size = slot_info.elem_count * slot_info.byte_count;
        let mut data = vec![0u8; size];
        unsafe {
            gl::BindBuffer(gl::ARRAY_BUFFER, self.buf_id);
            gl::GetBufferSubData(
                gl::ARRAY_BUFFER,
                off as isize,
                size as isize,
                data.as_mut_ptr() as *mut c_void,
            );
        }
        Ok(unsafe { std::ptr::read_unaligned(data.as_ptr() as *const D) })
    }

    /// Writes raw vertex data starting at `start_vertex`.
    ///
    /// `data` length must be a multiple of the vertex stride.
    pub fn write_range(&self, start_vertex: u32, data: &[u8]) -> OpticResult<()> {
        let stride = self.vert_stride as usize;
        if data.len() % stride != 0 {
            return Err(OpticError::new(
                OpticErrorKind::Custom,
                "write_range data length must be a multiple of vertex stride",
            ));
        }
        let vertex_count = data.len() / stride;
        if start_vertex + vertex_count as u32 > self.vert_count {
            return Err(OpticError::new(
                OpticErrorKind::Custom,
                "write_range extends past the vertex count",
            ));
        }
        let start_off = start_vertex as usize * stride;
        subfill_buffer(self.buf_id, start_off, data);
        Ok(())
    }

    fn compute_vert_attr_offset(&self, attr_index: usize, vertex_index: u32) -> usize {
        let mut offset = vertex_index as usize * self.vert_stride as usize;
        for i in 0..attr_index {
            let si = &self.layouts[i].0;
            offset += si.elem_count * si.byte_count;
        }
        offset
    }

    /// Deletes the VAO, VBO, and IBO from the GPU.
    pub fn delete(self) {
        unsafe {
            gl::DeleteVertexArrays(1, &self.vao_id);
            gl::DeleteBuffers(1, &self.buf_id);
            if self.has_indices {
                gl::DeleteBuffers(1, &self.ind_id);
            }
        }
    }
}

fn match_draw_mode(dm: &DrawMode) -> GLenum {
    match dm {
        DrawMode::Points => gl::POINTS,
        DrawMode::Lines => gl::LINES,
        DrawMode::Triangles => gl::TRIANGLES,
        DrawMode::Strip => gl::TRIANGLE_STRIP,
    }
}

macro_rules! mesh_struct {
    ($mesh:ident, $transform:ty) => {
        /// High-level mesh with visibility, shader, transform, and draw mode.
        ///
        /// Cloning is cheap — the handle shares the same GPU buffers.
        #[derive(Clone, Debug)]
        pub struct $mesh {
            pub visibility: bool,
            pub handle: MeshHandle,
            pub shader: Option<Shader>,
            pub transform: $transform,
            pub draw_mode: DrawMode,
        }

        impl $mesh {
            /// Attaches a shader to this mesh.
            pub fn set_shader(&mut self, shader: Shader) { self.shader = Some(shader); }
            /// Detaches the current shader.
            pub fn remove_shader(&mut self) { self.shader = None; }
            /// Returns the current draw mode.
            pub fn get_draw_mode(&self) -> DrawMode { self.handle.draw_mode }
            /// Sets the draw mode.
            pub fn set_draw_mode(&mut self, draw_mode: DrawMode) { self.handle.draw_mode = draw_mode; }
            /// Returns the index count.
            pub fn index_count(&self) -> u32 { self.handle.ind_count }
            /// Returns the vertex count.
            pub fn vertex_count(&self) -> u32 { self.handle.vert_count }
            /// Returns `true` if this mesh uses indexed drawing.
            pub fn has_indices(&self) -> bool { self.handle.has_indices }
            /// Returns `true` when the mesh has no vertices.
            pub fn is_empty(&self) -> bool { self.vertex_count() == 0 }
            /// Returns `true` when the mesh is both visible and non-empty.
            pub fn is_visible(&self) -> bool { self.visibility && !self.is_empty() }
            /// Shows or hides this mesh.
            pub fn set_visibility(&mut self, enable: bool) { self.visibility = enable; }
            /// Toggles visibility.
            pub fn toggle_visibility(&mut self) { self.visibility = !self.visibility; }
            /// Recomputes the transformation matrix.
            pub fn update(&mut self) { self.transform.calc_matrix(); }
            /// Deletes the GPU resources for this mesh's handle.
            pub fn delete(self) { self.handle.delete(); }
        }
    };
}

// A 3D mesh with position, rotation, and scale.
mesh_struct!(Mesh3D, crate::util::transform::Transform3D);
// A 2D mesh with position, rotation, scale, and layer.
mesh_struct!(Mesh2D, crate::util::transform::Transform2D);

impl Mesh3D {
    /// Prints debug information about this mesh to stdout.
    pub fn log_info(&self) {
        let shader_id = self.shader.as_ref().map(|s| s.id).unwrap_or(0);
        let mode = format!("{:?}", self.get_draw_mode());
        println!(
            "[Mesh3D] vis={} verts={} inds={} has_idx={} shader={} mode={} vao={} buf={} ind={}",
            self.visibility,
            self.vertex_count(),
            self.index_count(),
            self.has_indices(),
            shader_id,
            mode,
            self.handle.vao_id,
            self.handle.buf_id,
            self.handle.ind_id,
        );
    }

    /// Renders this mesh with the given view and projection matrices.
    ///
    /// Binds the shader, sets `uView`, `uProj`, `uTfm` uniforms, binds
    /// textures and storage buffers, then issues the draw call.
    pub fn render(&self, view: &cgmath::Matrix4<f32>, proj: &cgmath::Matrix4<f32>) {
        if !self.is_visible() { return; }
        let shader = match &self.shader { None => return, Some(sh) => sh };
        shader.bind();

        shader.set_m4_f32("uView", *view);
        shader.set_m4_f32("uProj", *proj);
        shader.set_m4_f32("uTfm", self.transform.matrix());

        shader.bind_textures();
        shader.bind_storages();
        self.handle.draw();
    }
}

impl Mesh2D {
    /// Prints debug information about this mesh to stdout.
    pub fn log_info(&self) {
        let shader_id = self.shader.as_ref().map(|s| s.id).unwrap_or(0);
        let mode = format!("{:?}", self.get_draw_mode());
        println!(
            "[Mesh2D] vis={} verts={} inds={} has_idx={} shader={} mode={} vao={} buf={} ind={}",
            self.visibility,
            self.vertex_count(),
            self.index_count(),
            self.has_indices(),
            shader_id,
            mode,
            self.handle.vao_id,
            self.handle.buf_id,
            self.handle.ind_id,
        );
    }

    /// Renders this 2D mesh with an orthographic projection matrix.
    ///
    /// Sets `uProj`, `uTfm`, `uLayer` uniforms, binds textures and
    /// storage buffers, then issues the draw call.
    pub fn render(&self, proj: &cgmath::Matrix4<f32>) {
        if !self.is_visible() { return; }
        let shader = match &self.shader { None => return, Some(sh) => sh };
        shader.bind();

        shader.set_m4_f32("uProj", *proj);
        let tfm = self.transform.matrix();
        let layer = self.transform.layer() as u32;
        shader.set_m4_f32("uTfm", tfm);
        shader.set_u32("uLayer", layer);

        shader.bind_textures();
        shader.bind_storages();
        self.handle.draw();
    }
}

/// Creates a VAO + VBO pair and returns their IDs.
pub fn create_mesh_buffer() -> (u32, u32) {
    let (mut v_id, mut b_id) = (0u32, 0u32);
    unsafe {
        gl::GenVertexArrays(1, &mut v_id);
        gl::GenBuffers(1, &mut b_id);
    }
    (v_id, b_id)
}

/// Configures a vertex attribute pointer for the given layout.
pub fn set_attr_layout(attr: &ATTRInfo, attr_id: u32, stride: usize, local_offset: usize) {
    unsafe {
        gl::VertexAttribPointer(
            attr_id,
            attr.elem_count as GLint,
            match_attr_type(&attr.typ),
            gl::FALSE,
            stride as GLsizei,
            match local_offset {
                0 => ptr::null(),
                _ => local_offset as *const c_void,
            },
        );
        gl::EnableVertexAttribArray(attr_id);
    }
}

/// Uploads data to a VBO (full buffer replace).
pub fn fill_buffer(id: u32, data: &[u8]) {
    unsafe {
        GL::bind_buffer(id);
        gl::BufferData(
            gl::ARRAY_BUFFER,
            data.len() as GLsizeiptr,
            data.as_ptr() as *const c_void,
            gl::DYNAMIC_DRAW,
        );
    }
}

/// Uploads data to a sub-range of a VBO.
pub fn subfill_buffer(id: u32, offset: usize, data: &[u8]) {
    unsafe {
        GL::bind_buffer(id);
        gl::BufferSubData(
            gl::ARRAY_BUFFER,
            offset as isize,
            data.len() as isize,
            data.as_ptr() as *const c_void,
        );
    }
}

/// Resizes a VBO without uploading data (contents become undefined).
pub fn resize_buffer(id: u32, size: usize) {
    unsafe {
        GL::bind_buffer(id);
        gl::BufferData(
            gl::ARRAY_BUFFER,
            size as GLsizeiptr,
            ptr::null(),
            gl::DYNAMIC_DRAW,
        );
    }
}

/// Creates an IBO (element array buffer) and returns its ID.
pub fn create_index_buffer() -> u32 {
    let mut id = 0u32;
    unsafe { gl::GenBuffers(1, &mut id); }
    id
}

/// Uploads index data to an IBO.
pub fn fill_index_buffer(id: u32, data: &[u32]) {
    unsafe {
        GL::bind_ebo(id);
        gl::BufferData(
            gl::ELEMENT_ARRAY_BUFFER,
            (data.len() * size_of::<u32>()) as GLsizeiptr,
            data.as_ptr() as *const c_void,
            gl::DYNAMIC_DRAW,
        );
    }
}

/// A GPU-side shader storage buffer (SSBO) for compute or vertex pulling.
///
/// Supports dynamic resizing, filling, sub-range updates, and read-back.
///
/// # Example
///
/// ```ignore
/// use optic_render::handles::StorageBuffer;
///
/// let mut ssbo = StorageBuffer::new(1024);
/// ssbo.fill(&[1u8; 1024]);
/// let data = ssbo.fetch();
/// ```
pub struct StorageBuffer {
    pub id: u32,
    pub size: usize,
}

impl StorageBuffer {
    /// Creates a new SSBO with the given byte size (zero-initialised).
    pub fn new(size: usize) -> Self {
        let id = create_storage_buffer();
        resize_storage_buffer(id, size);
        Self { id, size }
    }

    /// Resizes the buffer (contents become undefined if size changed).
    pub fn resize(&mut self, size: usize) {
        self.bind();
        if size != self.size {
            self.size = size;
            resize_storage_buffer(self.id, self.size);
        }
    }

    /// Replaces the full buffer contents.
    pub fn fill(&mut self, data: &[u8]) {
        self.bind();
        self.resize(data.len());
        fill_storage_buffer(self.id, data);
    }

    /// Writes data at a byte offset, growing the buffer if needed.
    pub fn subfill(&mut self, offset: usize, data: &[u8]) {
        self.bind();
        let len = data.len() + offset;
        self.resize(len);
        subfill_storage_buffer(self.id, offset, data);
    }

    /// Reads the entire buffer back to the CPU.
    pub fn fetch(&self) -> Vec<u8> {
        self.bind();
        read_storage_buffer(self.id, self.size)
    }

    /// Deletes the GPU buffer.
    pub fn delete(self) {
        unsafe { gl::DeleteBuffers(1, &self.id); }
    }

    fn bind(&self) {
        unsafe { gl::BindBuffer(gl::SHADER_STORAGE_BUFFER, self.id); }
    }
}

fn create_storage_buffer() -> u32 {
    let mut id = 0u32;
    unsafe { gl::GenBuffers(1, &mut id); }
    id
}

fn fill_storage_buffer(id: u32, data: &[u8]) {
    unsafe {
        gl::BindBuffer(gl::SHADER_STORAGE_BUFFER, id);
        gl::BufferData(
            gl::SHADER_STORAGE_BUFFER,
            data.len() as GLsizeiptr,
            data.as_ptr() as *const c_void,
            gl::DYNAMIC_DRAW,
        );
    }
}

fn subfill_storage_buffer(id: u32, offset: usize, data: &[u8]) {
    unsafe {
        gl::BindBuffer(gl::SHADER_STORAGE_BUFFER, id);
        gl::BufferSubData(
            gl::SHADER_STORAGE_BUFFER,
            offset as isize,
            data.len() as isize,
            data.as_ptr() as *const c_void,
        );
    }
}

fn resize_storage_buffer(id: u32, size: usize) {
    unsafe {
        gl::BindBuffer(gl::SHADER_STORAGE_BUFFER, id);
        gl::BufferData(
            gl::SHADER_STORAGE_BUFFER,
            size as GLsizeiptr,
            ptr::null(),
            gl::DYNAMIC_DRAW,
        );
    }
}

fn read_storage_buffer(id: u32, size: usize) -> Vec<u8> {
    let mut data = vec![0u8; size];
    unsafe {
        gl::BindBuffer(gl::SHADER_STORAGE_BUFFER, id);
        gl::GetBufferSubData(
            gl::SHADER_STORAGE_BUFFER,
            0,
            size as GLsizeiptr,
            data.as_mut_ptr() as *mut c_void,
        );
    }
    data
}

fn match_attr_type(attr_type: &ATTRType) -> GLenum {
    match attr_type {
        ATTRType::I8 => gl::BYTE,
        ATTRType::U8 => gl::UNSIGNED_BYTE,
        ATTRType::I16 => gl::SHORT,
        ATTRType::U16 => gl::UNSIGNED_SHORT,
        ATTRType::I32 => gl::INT,
        ATTRType::U32 => gl::UNSIGNED_INT,
        ATTRType::F32 => gl::FLOAT,
        ATTRType::F64 => gl::DOUBLE,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mesh_handle_fields() {
        let mh = MeshHandle {
            layouts: vec![],
            draw_mode: DrawMode::Triangles,
            has_indices: false,
            vert_count: 42,
            ind_count: 0,
            vao_id: 0,
            buf_id: 0,
            ind_id: 0,
            vert_stride: 0,
            instance_buf_id: 0,
            instance_count: 0,
        };
        assert_eq!(mh.vert_count, 42);
        assert!(!mh.has_indices);
    }

    #[test]
    fn mesh3d_default_state() {
        let mh = MeshHandle {
            layouts: vec![],
            draw_mode: DrawMode::Triangles,
            has_indices: true,
            vert_count: 3,
            ind_count: 3,
            vao_id: 0,
            buf_id: 0,
            ind_id: 0,
            vert_stride: 0,
            instance_buf_id: 0,
            instance_count: 0,
        };
        let m3d = Mesh3D {
            visibility: true,
            handle: mh,
            shader: None,
            transform: crate::util::transform::Transform3D::default(),
            draw_mode: DrawMode::Triangles,
        };
        assert!(m3d.is_visible());
        assert!(!m3d.is_empty());
        assert_eq!(m3d.vertex_count(), 3);
        assert_eq!(m3d.index_count(), 3);
        assert!(m3d.has_indices());
        assert_eq!(m3d.get_draw_mode(), DrawMode::Triangles);
        assert!(m3d.shader.is_none());
    }

    #[test]
    fn mesh3d_visibility_toggle() {
        let mh = MeshHandle {
            layouts: vec![],
            draw_mode: DrawMode::Triangles,
            has_indices: false,
            vert_count: 3,
            ind_count: 0,
            vao_id: 0,
            buf_id: 0,
            ind_id: 0,
            vert_stride: 0,
            instance_buf_id: 0,
            instance_count: 0,
        };
        let mut m3d = Mesh3D {
            visibility: true,
            handle: mh,
            shader: None,
            transform: crate::util::transform::Transform3D::default(),
            draw_mode: DrawMode::Triangles,
        };
        assert!(m3d.is_visible());
        m3d.set_visibility(false);
        assert!(!m3d.is_visible());
        m3d.toggle_visibility();
        assert!(m3d.is_visible());
    }

    #[test]
    fn mesh3d_set_draw_mode() {
        let mh = MeshHandle {
            layouts: vec![],
            draw_mode: DrawMode::Triangles,
            has_indices: false,
            vert_count: 3,
            ind_count: 0,
            vao_id: 0,
            buf_id: 0,
            ind_id: 0,
            vert_stride: 0,
            instance_buf_id: 0,
            instance_count: 0,
        };
        let mut m3d = Mesh3D {
            visibility: true,
            handle: mh,
            shader: None,
            transform: crate::util::transform::Transform3D::default(),
            draw_mode: DrawMode::Triangles,
        };
        m3d.set_draw_mode(DrawMode::Points);
        assert_eq!(m3d.get_draw_mode(), DrawMode::Points);
    }

    #[test]
    fn mesh3d_shader_management() {
        let mh = MeshHandle {
            layouts: vec![],
            draw_mode: DrawMode::Triangles,
            has_indices: false,
            vert_count: 3,
            ind_count: 0,
            vao_id: 0,
            buf_id: 0,
            ind_id: 0,
            vert_stride: 0,
            instance_buf_id: 0,
            instance_count: 0,
        };
        let mut m3d = Mesh3D {
            visibility: true,
            handle: mh,
            shader: None,
            transform: crate::util::transform::Transform3D::default(),
            draw_mode: DrawMode::Triangles,
        };
        assert!(m3d.shader.is_none());
        let s = Shader::new(99, false);
        m3d.set_shader(s);
        assert!(m3d.shader.is_some());
        assert_eq!(m3d.shader.as_ref().unwrap().id, 99);
        m3d.remove_shader();
        assert!(m3d.shader.is_none());
    }

    #[test]
    fn mesh3d_update_calc_matrix() {
        let mh = MeshHandle {
            layouts: vec![],
            draw_mode: DrawMode::Triangles,
            has_indices: false,
            vert_count: 3,
            ind_count: 0,
            vao_id: 0,
            buf_id: 0,
            ind_id: 0,
            vert_stride: 0,
            instance_buf_id: 0,
            instance_count: 0,
        };
        let mut m3d = Mesh3D {
            visibility: true,
            handle: mh,
            shader: None,
            transform: crate::util::transform::Transform3D::default(),
            draw_mode: DrawMode::Triangles,
        };
        let ident = m3d.transform.matrix();
        m3d.transform.set_pos_all(10.0, 20.0, 30.0);
        m3d.update();
        let m = m3d.transform.matrix();
        assert!(ident != m);
    }

    #[test]
    fn mesh3d_is_empty_true() {
        let mh = MeshHandle {
            layouts: vec![],
            draw_mode: DrawMode::Triangles,
            has_indices: false,
            vert_count: 0,
            ind_count: 0,
            vao_id: 0,
            buf_id: 0,
            ind_id: 0,
            vert_stride: 0,
            instance_buf_id: 0,
            instance_count: 0,
        };
        let m3d = Mesh3D {
            visibility: true,
            handle: mh,
            shader: None,
            transform: crate::util::transform::Transform3D::default(),
            draw_mode: DrawMode::Triangles,
        };
        assert!(m3d.is_empty());
        assert!(!m3d.is_visible());
    }

    #[test]
    fn mesh2d_default_state() {
        let mh = MeshHandle {
            layouts: vec![],
            draw_mode: DrawMode::Triangles,
            has_indices: true,
            vert_count: 4,
            ind_count: 6,
            vao_id: 0,
            buf_id: 0,
            ind_id: 0,
            vert_stride: 0,
            instance_buf_id: 0,
            instance_count: 0,
        };
        let m2d = Mesh2D {
            visibility: true,
            handle: mh,
            shader: None,
            transform: crate::util::transform::Transform2D::default(),
            draw_mode: DrawMode::Triangles,
        };
        assert!(m2d.is_visible());
        assert_eq!(m2d.vertex_count(), 4);
        assert!(m2d.has_indices());
    }

    #[test]
    fn storage_buffer_create() {
        // Only test the struct fields, not GL creation
        let sb = StorageBuffer { id: 0, size: 0 };
        assert_eq!(sb.id, 0);
        assert_eq!(sb.size, 0);
    }
}