hoplite 0.1.9

A creative coding framework for Rust that gets out of your way
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
//! 3D mesh rendering pass with depth testing and texture support.
//!
//! This module provides the [`MeshPass`] render pass, which handles rendering of 3D meshes
//! with proper depth testing, camera transformations, and optional texturing. It is designed
//! to work within the render graph system and can composite its output over previous passes.
//!
//! # Architecture
//!
//! The mesh pass uses three bind groups:
//! - **Group 0**: Camera uniforms (view/projection matrices, camera position, time)
//! - **Group 1**: Model uniforms (model matrix, normal matrix, color)
//! - **Group 2**: Texture and sampler for the mesh surface
//!
//! # Example
//!
//! ```ignore
//! use hoplite::{MeshPass, DrawCall, Camera, Mesh, Transform};
//!
//! // Create the mesh pass
//! let mesh_pass = MeshPass::new(&gpu);
//!
//! // Queue draw calls
//! let draw_calls = vec![
//!     DrawCall {
//!         mesh: &my_mesh,
//!         transform: Transform::from_position([0.0, 0.0, 0.0]),
//!         color: Color::WHITE,
//!         texture: Some(&my_texture),
//!     },
//! ];
//!
//! // Render in a render pass
//! mesh_pass.render(&gpu, &mut render_pass, &camera, time, &draw_calls);
//! ```
//!
//! # Depth Buffer
//!
//! The mesh pass maintains its own depth buffer that automatically resizes to match
//! the screen dimensions. Call [`MeshPass::ensure_depth_size`] before rendering if
//! the window may have been resized.
//!
//! # Blitting
//!
//! The [`MeshPass::blit`] method allows compositing an input texture as the background
//! before rendering meshes. This is useful for layering the 3D scene over 2D content
//! from previous render passes.

use crate::camera::Camera;
use crate::draw2d::Color;
use crate::gpu::GpuContext;
use crate::mesh::{Mesh, Transform, Vertex3d};
use crate::texture::Texture;

/// Camera uniforms for 3D rendering.
///
/// This structure is uploaded to the GPU each frame and provides the shader
/// with all necessary camera and timing information.
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct CameraUniforms {
    /// Combined view-projection matrix for transforming world positions to clip space.
    pub view_proj: [[f32; 4]; 4],
    /// View matrix (world to camera space transformation).
    pub view: [[f32; 4]; 4],
    /// Projection matrix (camera to clip space transformation).
    pub proj: [[f32; 4]; 4],
    /// Camera position in world space, useful for lighting calculations.
    pub camera_pos: [f32; 3],
    /// Elapsed time in seconds, for animated shaders.
    pub time: f32,
}

/// Per-instance data stored in a storage buffer for instanced rendering.
///
/// This structure is uploaded to the GPU once per frame for all instances,
/// allowing efficient batched rendering with a single draw call per mesh type.
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct InstanceData {
    /// Model matrix (object to world space transformation).
    pub model: [[f32; 4]; 4],
    /// Normal matrix (inverse transpose of model matrix) for correct normal transformation.
    pub normal_matrix: [[f32; 4]; 4],
    /// RGBA color multiplier applied to the mesh.
    pub color: [f32; 4],
}

/// Legacy per-instance model uniforms (kept for compatibility).
///
/// This structure is uploaded to the GPU for each draw call and provides
/// the shader with model-specific transformation and color data.
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct ModelUniforms {
    /// Model matrix (object to world space transformation).
    pub model: [[f32; 4]; 4],
    /// Normal matrix (inverse transpose of model matrix) for correct normal transformation.
    pub normal_matrix: [[f32; 4]; 4],
    /// RGBA color multiplier applied to the mesh.
    pub color: [f32; 4],
}

/// A draw call queued for rendering.
///
/// Represents a single mesh to be rendered with its associated transform,
/// color tint, and optional texture. Draw calls are batched and processed
/// by [`MeshPass::render`].
///
/// # Example
///
/// ```ignore
/// let draw_call = DrawCall {
///     mesh: &cube_mesh,
///     transform: Transform::from_position([0.0, 1.0, 0.0])
///         .with_scale([2.0, 2.0, 2.0]),
///     color: Color::RED,
///     texture: None, // Uses default white texture
/// };
/// ```
pub struct DrawCall<'a> {
    /// Reference to the mesh geometry to render.
    pub mesh: &'a Mesh,
    /// World-space transform (position, rotation, scale) for the mesh.
    pub transform: Transform,
    /// Color tint applied to the mesh (multiplied with texture color).
    pub color: Color,
    /// Optional texture to apply. If `None`, a default white texture is used.
    pub texture: Option<&'a Texture>,
}

/// Handles 3D mesh rendering with depth testing.
///
/// `MeshPass` is a GPU render pass optimized for rendering textured 3D meshes
/// with proper depth sorting. It manages all necessary GPU resources including
/// pipelines, uniform buffers, and depth textures.
///
/// # Features
///
/// - **Depth testing**: Proper occlusion with a 32-bit floating point depth buffer
/// - **Texturing**: Per-mesh texture binding with a default white fallback
/// - **Color tinting**: Per-mesh color multiplier for variety without texture changes
/// - **Blitting**: Composite previous render pass output as background
/// - **Auto-resize**: Depth buffer automatically resizes to match screen dimensions
///
/// # Pipeline Configuration
///
/// - Back-face culling enabled (counter-clockwise front faces)
/// - Alpha blending for transparent meshes
/// - Depth write and Less-than comparison
///
/// # Usage
///
/// The typical render loop involves:
/// 1. Call [`ensure_depth_size`](Self::ensure_depth_size) if the window may have resized
/// 2. Optionally call [`blit`](Self::blit) to composite a background texture
/// 3. Call [`render`](Self::render) with your camera and draw calls
/// Maximum number of instances that can be rendered in a single batch.
const MAX_INSTANCES: usize = 4096;

pub struct MeshPass {
    pipeline: wgpu::RenderPipeline,
    camera_buffer: wgpu::Buffer,
    camera_bind_group: wgpu::BindGroup,
    instance_buffer: wgpu::Buffer,
    #[allow(dead_code)]
    instance_bind_group_layout: wgpu::BindGroupLayout,
    instance_bind_group: wgpu::BindGroup,
    /// The depth texture used for depth testing.
    pub(crate) depth_texture: wgpu::Texture,
    /// View into the depth texture for render pass attachment.
    pub(crate) depth_view: wgpu::TextureView,
    depth_size: (u32, u32),
    blit_pipeline: wgpu::RenderPipeline,
    blit_bind_group_layout: wgpu::BindGroupLayout,
    blit_sampler: wgpu::Sampler,
    texture_bind_group_layout: wgpu::BindGroupLayout,
    default_texture: Texture,
}

impl MeshPass {
    /// Creates a new mesh rendering pass.
    ///
    /// This initializes all GPU resources including:
    /// - The mesh rendering pipeline with depth testing
    /// - Camera and model uniform buffers
    /// - A blit pipeline for background compositing
    /// - A default 1x1 white texture for untextured meshes
    /// - A depth buffer sized to the current screen dimensions
    ///
    /// # Arguments
    ///
    /// * `gpu` - The GPU context containing the device, queue, and surface configuration
    pub fn new(gpu: &GpuContext) -> Self {
        let device = &gpu.device;

        // Create shader
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Mesh Shader"),
            source: wgpu::ShaderSource::Wgsl(include_str!("shaders/mesh.wgsl").into()),
        });

        // Camera uniform buffer (group 0)
        let camera_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Camera Uniforms"),
            size: std::mem::size_of::<CameraUniforms>() as u64,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let camera_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Camera Bind Group Layout"),
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                }],
            });

        let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Camera Bind Group"),
            layout: &camera_bind_group_layout,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: camera_buffer.as_entire_binding(),
            }],
        });

        // Instance storage buffer (group 1) - holds all instance data for batched rendering
        let instance_buffer = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("Instance Storage Buffer"),
            size: (std::mem::size_of::<InstanceData>() * MAX_INSTANCES) as u64,
            usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let instance_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Instance Bind Group Layout"),
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Storage { read_only: true },
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                }],
            });

        let instance_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Instance Bind Group"),
            layout: &instance_bind_group_layout,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: instance_buffer.as_entire_binding(),
            }],
        });

        // Texture bind group layout (group 2)
        let texture_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Texture Bind Group Layout"),
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Texture {
                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
                            view_dimension: wgpu::TextureViewDimension::D2,
                            multisampled: false,
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                ],
            });

        // Create a 1x1 white default texture for untextured meshes
        let default_texture =
            Texture::from_rgba(gpu, &[255, 255, 255, 255], 1, 1, "Default White Texture");

        // Pipeline layout
        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("Mesh Pipeline Layout"),
            bind_group_layouts: &[
                &camera_bind_group_layout,
                &instance_bind_group_layout,
                &texture_bind_group_layout,
            ],
            push_constant_ranges: &[],
        });

        // Depth texture
        let (depth_texture, depth_view) = Self::create_depth_texture(gpu);

        // Blit pipeline for compositing input texture as background
        let blit_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Blit Shader"),
            source: wgpu::ShaderSource::Wgsl(include_str!("shaders/blit.wgsl").into()),
        });

        let blit_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("Blit Sampler"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::FilterMode::Nearest,
            ..Default::default()
        });

        let blit_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Blit Bind Group Layout"),
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Texture {
                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
                            view_dimension: wgpu::TextureViewDimension::D2,
                            multisampled: false,
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                ],
            });

        let blit_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("Blit Pipeline Layout"),
            bind_group_layouts: &[&blit_bind_group_layout],
            push_constant_ranges: &[],
        });

        let blit_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("Blit Pipeline"),
            layout: Some(&blit_pipeline_layout),
            vertex: wgpu::VertexState {
                module: &blit_shader,
                entry_point: Some("vs"),
                buffers: &[],
                compilation_options: Default::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &blit_shader,
                entry_point: Some("fs"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: gpu.config.format,
                    blend: Some(wgpu::BlendState::REPLACE),
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: Default::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                ..Default::default()
            },
            depth_stencil: None,
            multisample: wgpu::MultisampleState::default(),
            multiview: None,
            cache: None,
        });

        // Render pipeline
        let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("Mesh Pipeline"),
            layout: Some(&pipeline_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some("vs"),
                buffers: &[Vertex3d::LAYOUT],
                compilation_options: Default::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some("fs"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: gpu.config.format,
                    blend: Some(wgpu::BlendState::ALPHA_BLENDING),
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: Default::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                cull_mode: Some(wgpu::Face::Back),
                front_face: wgpu::FrontFace::Ccw,
                ..Default::default()
            },
            depth_stencil: Some(wgpu::DepthStencilState {
                format: wgpu::TextureFormat::Depth32Float,
                depth_write_enabled: true,
                depth_compare: wgpu::CompareFunction::Less,
                stencil: wgpu::StencilState::default(),
                bias: wgpu::DepthBiasState::default(),
            }),
            multisample: wgpu::MultisampleState::default(),
            multiview: None,
            cache: None,
        });

        Self {
            pipeline,
            camera_buffer,
            camera_bind_group,
            instance_buffer,
            instance_bind_group_layout,
            instance_bind_group,
            depth_texture,
            depth_view,
            depth_size: (gpu.width(), gpu.height()),
            blit_pipeline,
            blit_bind_group_layout,
            blit_sampler,
            texture_bind_group_layout,
            default_texture,
        }
    }

    /// Creates a bind group for a texture.
    ///
    /// This creates a GPU bind group that binds a texture and its sampler
    /// for use in the mesh shader. The bind group is assigned to group 2
    /// in the pipeline layout.
    ///
    /// # Arguments
    ///
    /// * `gpu` - The GPU context
    /// * `texture` - The texture to create a bind group for
    ///
    /// # Returns
    ///
    /// A `wgpu::BindGroup` ready to be bound during rendering.
    pub fn create_texture_bind_group(
        &self,
        gpu: &GpuContext,
        texture: &Texture,
    ) -> wgpu::BindGroup {
        gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Mesh Texture Bind Group"),
            layout: &self.texture_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&texture.view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&texture.sampler),
                },
            ],
        })
    }

    fn create_depth_texture(gpu: &GpuContext) -> (wgpu::Texture, wgpu::TextureView) {
        let texture = gpu.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("Depth Texture"),
            size: wgpu::Extent3d {
                width: gpu.width(),
                height: gpu.height(),
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Depth32Float,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        });
        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        (texture, view)
    }

    /// Ensures the depth buffer matches the current screen size.
    ///
    /// Call this method at the start of each frame if the window may have been
    /// resized. If the depth buffer dimensions don't match the GPU context's
    /// current dimensions, a new depth texture is created.
    ///
    /// # Arguments
    ///
    /// * `gpu` - The GPU context with the current screen dimensions
    pub fn ensure_depth_size(&mut self, gpu: &GpuContext) {
        if self.depth_size != (gpu.width(), gpu.height()) {
            let (texture, view) = Self::create_depth_texture(gpu);
            self.depth_texture = texture;
            self.depth_view = view;
            self.depth_size = (gpu.width(), gpu.height());
        }
    }

    /// Blits (copies) an input texture to the render target.
    ///
    /// This method renders a fullscreen quad textured with the input texture,
    /// effectively copying it to the current render target. It's typically used
    /// to composite the output of a previous render pass (such as 2D content)
    /// as the background before rendering 3D meshes on top.
    ///
    /// The blit uses linear filtering and replaces the destination completely
    /// (no blending).
    ///
    /// # Arguments
    ///
    /// * `gpu` - The GPU context
    /// * `render_pass` - The active render pass to draw into
    /// * `input_view` - The texture view to copy from
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Composite the 2D pass output as background
    /// mesh_pass.blit(&gpu, &mut render_pass, &draw2d_output_view);
    /// // Then render 3D meshes on top
    /// mesh_pass.render(&gpu, &mut render_pass, &camera, time, &draw_calls);
    /// ```
    pub fn blit(
        &self,
        gpu: &GpuContext,
        render_pass: &mut wgpu::RenderPass,
        input_view: &wgpu::TextureView,
    ) {
        let bind_group = gpu.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("Blit Bind Group"),
            layout: &self.blit_bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(input_view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&self.blit_sampler),
                },
            ],
        });

        render_pass.set_pipeline(&self.blit_pipeline);
        render_pass.set_bind_group(0, &bind_group, &[]);
        render_pass.draw(0..3, 0..1);
    }

    /// Renders a list of draw calls.
    ///
    /// This is the main rendering method for the mesh pass. It updates camera
    /// uniforms once per frame, then iterates through all draw calls, updating
    /// per-instance uniforms and issuing draw commands for each mesh.
    ///
    /// # Arguments
    ///
    /// * `gpu` - The GPU context
    /// * `render_pass` - The active render pass to draw into (must have depth attachment)
    /// * `camera` - The camera providing view and projection matrices
    /// * `time` - Elapsed time in seconds (passed to shaders for animation)
    /// * `draw_calls` - Slice of draw calls to render
    ///
    /// # Behavior
    ///
    /// - Returns early if `draw_calls` is empty
    /// - Camera uniforms are updated once at the start
    /// - For each draw call:
    ///   - Model and normal matrices are computed from the transform
    ///   - A texture bind group is created (using default white if no texture specified)
    ///   - The mesh is drawn with indexed rendering
    ///
    /// # Performance
    ///
    /// Uses instanced rendering with a storage buffer for per-instance data.
    /// All instance transforms and colors are uploaded once before the render pass,
    /// then batched draw calls render all instances of each mesh type efficiently.
    pub fn render(
        &self,
        gpu: &GpuContext,
        render_pass: &mut wgpu::RenderPass,
        camera: &Camera,
        time: f32,
        draw_calls: &[DrawCall],
    ) {
        if draw_calls.is_empty() {
            return;
        }

        // Update camera uniforms
        let view = camera.view_matrix();
        let proj = camera.projection_matrix(gpu.aspect(), 0.1, 1000.0);
        let view_proj = proj * view;

        let camera_uniforms = CameraUniforms {
            view_proj: view_proj.to_cols_array_2d(),
            view: view.to_cols_array_2d(),
            proj: proj.to_cols_array_2d(),
            camera_pos: camera.position.to_array(),
            time,
        };

        gpu.queue.write_buffer(
            &self.camera_buffer,
            0,
            bytemuck::cast_slice(&[camera_uniforms]),
        );

        // Build instance data for all draw calls and group by (mesh, texture)
        // We use raw pointers as keys since we need to identify unique mesh/texture combinations
        use std::collections::HashMap;

        // Key: (mesh pointer, texture pointer)
        // Value: (mesh reference, texture reference, list of instance indices)
        type BatchKey = (*const Mesh, *const Texture);
        let mut batches: HashMap<BatchKey, (&Mesh, &Texture, Vec<u32>)> = HashMap::new();

        let mut instance_data: Vec<InstanceData> =
            Vec::with_capacity(draw_calls.len().min(MAX_INSTANCES));

        for call in draw_calls.iter().take(MAX_INSTANCES) {
            let model_matrix = call.transform.matrix();
            let normal_matrix = model_matrix.inverse().transpose();

            let instance_idx = instance_data.len() as u32;
            instance_data.push(InstanceData {
                model: model_matrix.to_cols_array_2d(),
                normal_matrix: normal_matrix.to_cols_array_2d(),
                color: [call.color.r, call.color.g, call.color.b, call.color.a],
            });

            let texture = call.texture.unwrap_or(&self.default_texture);
            let key: BatchKey = (call.mesh as *const Mesh, texture as *const Texture);

            batches
                .entry(key)
                .or_insert_with(|| (call.mesh, texture, Vec::new()))
                .2
                .push(instance_idx);
        }

        // Upload all instance data in one write before the render pass
        if !instance_data.is_empty() {
            gpu.queue.write_buffer(
                &self.instance_buffer,
                0,
                bytemuck::cast_slice(&instance_data),
            );
        }

        render_pass.set_pipeline(&self.pipeline);
        render_pass.set_bind_group(0, &self.camera_bind_group, &[]);
        render_pass.set_bind_group(1, &self.instance_bind_group, &[]);

        // Render each batch with instanced drawing
        for (_key, (mesh, texture, indices)) in batches {
            let texture_bind_group = self.create_texture_bind_group(gpu, texture);
            render_pass.set_bind_group(2, &texture_bind_group, &[]);

            render_pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
            render_pass.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);

            // Draw all instances of this mesh/texture combination
            // Each instance uses its index into the storage buffer
            for &instance_idx in &indices {
                render_pass.draw_indexed(0..mesh.index_count, 0, instance_idx..instance_idx + 1);
            }
        }
    }
}