skia-rs-gpu 0.2.1

GPU backends for skia-rs
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
838
839
840
841
842
843
844
845
846
847
848
849
//! Command buffer recording and submission.
//!
//! This module provides abstractions for recording GPU commands and managing
//! command buffer submission.

use skia_rs_core::{Color, Rect};
use std::sync::Arc;

/// Draw command for batching.
#[derive(Debug, Clone)]
pub enum DrawCommand {
    /// Clear the render target.
    Clear {
        /// Clear color.
        color: Color,
    },
    /// Draw primitives.
    Draw {
        /// Vertex count.
        vertex_count: u32,
        /// Instance count.
        instance_count: u32,
        /// First vertex.
        first_vertex: u32,
        /// First instance.
        first_instance: u32,
    },
    /// Draw indexed primitives.
    DrawIndexed {
        /// Index count.
        index_count: u32,
        /// Instance count.
        instance_count: u32,
        /// First index.
        first_index: u32,
        /// Base vertex.
        base_vertex: i32,
        /// First instance.
        first_instance: u32,
    },
    /// Set scissor rect.
    SetScissor {
        /// Scissor rectangle.
        rect: ScissorRect,
    },
    /// Set viewport.
    SetViewport {
        /// Viewport configuration.
        viewport: Viewport,
    },
    /// Set blend constant.
    SetBlendConstant {
        /// Blend constant color.
        color: [f32; 4],
    },
    /// Set stencil reference.
    SetStencilReference {
        /// Reference value.
        reference: u32,
    },
    /// Push debug group.
    PushDebugGroup {
        /// Label.
        label: String,
    },
    /// Pop debug group.
    PopDebugGroup,
    /// Insert debug marker.
    InsertDebugMarker {
        /// Label.
        label: String,
    },
    /// Set pipeline.
    SetPipeline {
        /// Pipeline ID.
        pipeline_id: u64,
    },
    /// Set bind group.
    SetBindGroup {
        /// Group index.
        index: u32,
        /// Bind group ID.
        bind_group_id: u64,
        /// Dynamic offsets.
        dynamic_offsets: Vec<u32>,
    },
    /// Set vertex buffer.
    SetVertexBuffer {
        /// Slot index.
        slot: u32,
        /// Buffer ID.
        buffer_id: u64,
        /// Offset.
        offset: u64,
        /// Size (None for entire buffer).
        size: Option<u64>,
    },
    /// Set index buffer.
    SetIndexBuffer {
        /// Buffer ID.
        buffer_id: u64,
        /// Index format.
        format: IndexFormat,
        /// Offset.
        offset: u64,
        /// Size (None for entire buffer).
        size: Option<u64>,
    },
    /// Copy buffer to buffer.
    CopyBufferToBuffer {
        /// Source buffer ID.
        src: u64,
        /// Source offset.
        src_offset: u64,
        /// Destination buffer ID.
        dst: u64,
        /// Destination offset.
        dst_offset: u64,
        /// Size.
        size: u64,
    },
    /// Copy buffer to texture.
    CopyBufferToTexture {
        /// Source buffer ID.
        src_buffer: u64,
        /// Source layout.
        src_layout: ImageDataLayout,
        /// Destination texture ID.
        dst_texture: u64,
        /// Destination origin.
        dst_origin: [u32; 3],
        /// Copy size.
        size: [u32; 3],
    },
    /// Copy texture to buffer.
    CopyTextureToBuffer {
        /// Source texture ID.
        src_texture: u64,
        /// Source origin.
        src_origin: [u32; 3],
        /// Destination buffer ID.
        dst_buffer: u64,
        /// Destination layout.
        dst_layout: ImageDataLayout,
        /// Copy size.
        size: [u32; 3],
    },
    /// Copy texture to texture.
    CopyTextureToTexture {
        /// Source texture ID.
        src_texture: u64,
        /// Source origin.
        src_origin: [u32; 3],
        /// Destination texture ID.
        dst_texture: u64,
        /// Destination origin.
        dst_origin: [u32; 3],
        /// Copy size.
        size: [u32; 3],
    },
}

// Re-use IndexFormat from pipeline module
pub use crate::pipeline::IndexFormat;

/// Image data layout for copy operations.
#[derive(Debug, Clone, Copy)]
pub struct ImageDataLayout {
    /// Offset into the buffer.
    pub offset: u64,
    /// Bytes per row.
    pub bytes_per_row: Option<u32>,
    /// Rows per image (for 3D textures).
    pub rows_per_image: Option<u32>,
}

impl Default for ImageDataLayout {
    fn default() -> Self {
        Self {
            offset: 0,
            bytes_per_row: None,
            rows_per_image: None,
        }
    }
}

/// Scissor rectangle.
#[derive(Debug, Clone, Copy)]
pub struct ScissorRect {
    /// X position.
    pub x: u32,
    /// Y position.
    pub y: u32,
    /// Width.
    pub width: u32,
    /// Height.
    pub height: u32,
}

impl ScissorRect {
    /// Create a new scissor rect.
    pub fn new(x: u32, y: u32, width: u32, height: u32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Create from a rect (clamping to u32 bounds).
    pub fn from_rect(rect: &Rect) -> Self {
        Self {
            x: rect.left.max(0.0) as u32,
            y: rect.top.max(0.0) as u32,
            width: rect.width().max(0.0) as u32,
            height: rect.height().max(0.0) as u32,
        }
    }
}

/// Viewport configuration.
#[derive(Debug, Clone, Copy)]
pub struct Viewport {
    /// X position.
    pub x: f32,
    /// Y position.
    pub y: f32,
    /// Width.
    pub width: f32,
    /// Height.
    pub height: f32,
    /// Minimum depth.
    pub min_depth: f32,
    /// Maximum depth.
    pub max_depth: f32,
}

impl Viewport {
    /// Create a new viewport.
    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
            min_depth: 0.0,
            max_depth: 1.0,
        }
    }

    /// Set depth range.
    pub fn with_depth(mut self, min: f32, max: f32) -> Self {
        self.min_depth = min;
        self.max_depth = max;
        self
    }
}

/// Command buffer for recording GPU commands.
#[derive(Debug, Default)]
pub struct CommandBuffer {
    /// Recorded commands.
    commands: Vec<DrawCommand>,
    /// Current debug group depth.
    debug_depth: u32,
}

impl CommandBuffer {
    /// Create a new empty command buffer.
    pub fn new() -> Self {
        Self {
            commands: Vec::new(),
            debug_depth: 0,
        }
    }

    /// Create with capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            commands: Vec::with_capacity(capacity),
            debug_depth: 0,
        }
    }

    /// Record a command.
    pub fn record(&mut self, command: DrawCommand) {
        match &command {
            DrawCommand::PushDebugGroup { .. } => self.debug_depth += 1,
            DrawCommand::PopDebugGroup => {
                self.debug_depth = self.debug_depth.saturating_sub(1);
            }
            _ => {}
        }
        self.commands.push(command);
    }

    /// Clear the render target.
    pub fn clear(&mut self, color: Color) {
        self.record(DrawCommand::Clear { color });
    }

    /// Draw primitives.
    pub fn draw(&mut self, vertex_count: u32, instance_count: u32) {
        self.record(DrawCommand::Draw {
            vertex_count,
            instance_count,
            first_vertex: 0,
            first_instance: 0,
        });
    }

    /// Draw primitives with offsets.
    pub fn draw_with_offsets(
        &mut self,
        vertex_count: u32,
        instance_count: u32,
        first_vertex: u32,
        first_instance: u32,
    ) {
        self.record(DrawCommand::Draw {
            vertex_count,
            instance_count,
            first_vertex,
            first_instance,
        });
    }

    /// Draw indexed primitives.
    pub fn draw_indexed(&mut self, index_count: u32, instance_count: u32) {
        self.record(DrawCommand::DrawIndexed {
            index_count,
            instance_count,
            first_index: 0,
            base_vertex: 0,
            first_instance: 0,
        });
    }

    /// Draw indexed primitives with offsets.
    pub fn draw_indexed_with_offsets(
        &mut self,
        index_count: u32,
        instance_count: u32,
        first_index: u32,
        base_vertex: i32,
        first_instance: u32,
    ) {
        self.record(DrawCommand::DrawIndexed {
            index_count,
            instance_count,
            first_index,
            base_vertex,
            first_instance,
        });
    }

    /// Set scissor rect.
    pub fn set_scissor(&mut self, rect: ScissorRect) {
        self.record(DrawCommand::SetScissor { rect });
    }

    /// Set viewport.
    pub fn set_viewport(&mut self, viewport: Viewport) {
        self.record(DrawCommand::SetViewport { viewport });
    }

    /// Set blend constant.
    pub fn set_blend_constant(&mut self, color: [f32; 4]) {
        self.record(DrawCommand::SetBlendConstant { color });
    }

    /// Set stencil reference.
    pub fn set_stencil_reference(&mut self, reference: u32) {
        self.record(DrawCommand::SetStencilReference { reference });
    }

    /// Push a debug group.
    pub fn push_debug_group(&mut self, label: impl Into<String>) {
        self.record(DrawCommand::PushDebugGroup {
            label: label.into(),
        });
    }

    /// Pop a debug group.
    pub fn pop_debug_group(&mut self) {
        self.record(DrawCommand::PopDebugGroup);
    }

    /// Insert a debug marker.
    pub fn insert_debug_marker(&mut self, label: impl Into<String>) {
        self.record(DrawCommand::InsertDebugMarker {
            label: label.into(),
        });
    }

    /// Set the current pipeline.
    pub fn set_pipeline(&mut self, pipeline_id: u64) {
        self.record(DrawCommand::SetPipeline { pipeline_id });
    }

    /// Set a bind group.
    pub fn set_bind_group(&mut self, index: u32, bind_group_id: u64, dynamic_offsets: &[u32]) {
        self.record(DrawCommand::SetBindGroup {
            index,
            bind_group_id,
            dynamic_offsets: dynamic_offsets.to_vec(),
        });
    }

    /// Set a vertex buffer.
    pub fn set_vertex_buffer(&mut self, slot: u32, buffer_id: u64, offset: u64, size: Option<u64>) {
        self.record(DrawCommand::SetVertexBuffer {
            slot,
            buffer_id,
            offset,
            size,
        });
    }

    /// Set the index buffer.
    pub fn set_index_buffer(
        &mut self,
        buffer_id: u64,
        format: IndexFormat,
        offset: u64,
        size: Option<u64>,
    ) {
        self.record(DrawCommand::SetIndexBuffer {
            buffer_id,
            format,
            offset,
            size,
        });
    }

    /// Copy buffer to buffer.
    pub fn copy_buffer_to_buffer(
        &mut self,
        src: u64,
        src_offset: u64,
        dst: u64,
        dst_offset: u64,
        size: u64,
    ) {
        self.record(DrawCommand::CopyBufferToBuffer {
            src,
            src_offset,
            dst,
            dst_offset,
            size,
        });
    }

    /// Get the recorded commands.
    pub fn commands(&self) -> &[DrawCommand] {
        &self.commands
    }

    /// Take ownership of the commands.
    pub fn take_commands(&mut self) -> Vec<DrawCommand> {
        std::mem::take(&mut self.commands)
    }

    /// Clear all recorded commands.
    pub fn reset(&mut self) {
        self.commands.clear();
        self.debug_depth = 0;
    }

    /// Check if the buffer is empty.
    pub fn is_empty(&self) -> bool {
        self.commands.is_empty()
    }

    /// Get the number of commands.
    pub fn len(&self) -> usize {
        self.commands.len()
    }
}

/// Command encoder for recording render and compute passes.
pub struct CommandEncoder {
    /// Command buffer being built.
    buffer: CommandBuffer,
    /// Label for debugging.
    label: Option<String>,
}

impl CommandEncoder {
    /// Create a new command encoder.
    pub fn new() -> Self {
        Self {
            buffer: CommandBuffer::new(),
            label: None,
        }
    }

    /// Create with a label.
    pub fn with_label(label: impl Into<String>) -> Self {
        Self {
            buffer: CommandBuffer::new(),
            label: Some(label.into()),
        }
    }

    /// Begin a render pass.
    pub fn begin_render_pass(&mut self, desc: &RenderPassDescriptor) -> RenderPassEncoder<'_> {
        if let Some(color) = desc.clear_color {
            self.buffer.clear(Color::from_argb(
                (color[3] * 255.0) as u8,
                (color[0] * 255.0) as u8,
                (color[1] * 255.0) as u8,
                (color[2] * 255.0) as u8,
            ));
        }
        RenderPassEncoder { encoder: self }
    }

    /// Begin a compute pass.
    pub fn begin_compute_pass(&mut self) -> ComputePassEncoder<'_> {
        ComputePassEncoder { encoder: self }
    }

    /// Copy buffer to buffer.
    pub fn copy_buffer_to_buffer(
        &mut self,
        src: u64,
        src_offset: u64,
        dst: u64,
        dst_offset: u64,
        size: u64,
    ) {
        self.buffer
            .copy_buffer_to_buffer(src, src_offset, dst, dst_offset, size);
    }

    /// Copy buffer to texture.
    pub fn copy_buffer_to_texture(
        &mut self,
        src_buffer: u64,
        src_layout: ImageDataLayout,
        dst_texture: u64,
        dst_origin: [u32; 3],
        size: [u32; 3],
    ) {
        self.buffer.record(DrawCommand::CopyBufferToTexture {
            src_buffer,
            src_layout,
            dst_texture,
            dst_origin,
            size,
        });
    }

    /// Copy texture to buffer.
    pub fn copy_texture_to_buffer(
        &mut self,
        src_texture: u64,
        src_origin: [u32; 3],
        dst_buffer: u64,
        dst_layout: ImageDataLayout,
        size: [u32; 3],
    ) {
        self.buffer.record(DrawCommand::CopyTextureToBuffer {
            src_texture,
            src_origin,
            dst_buffer,
            dst_layout,
            size,
        });
    }

    /// Copy texture to texture.
    pub fn copy_texture_to_texture(
        &mut self,
        src_texture: u64,
        src_origin: [u32; 3],
        dst_texture: u64,
        dst_origin: [u32; 3],
        size: [u32; 3],
    ) {
        self.buffer.record(DrawCommand::CopyTextureToTexture {
            src_texture,
            src_origin,
            dst_texture,
            dst_origin,
            size,
        });
    }

    /// Push a debug group.
    pub fn push_debug_group(&mut self, label: &str) {
        self.buffer.push_debug_group(label);
    }

    /// Pop a debug group.
    pub fn pop_debug_group(&mut self) {
        self.buffer.pop_debug_group();
    }

    /// Insert a debug marker.
    pub fn insert_debug_marker(&mut self, label: &str) {
        self.buffer.insert_debug_marker(label);
    }

    /// Finish recording and return the command buffer.
    pub fn finish(self) -> CommandBuffer {
        self.buffer
    }
}

impl Default for CommandEncoder {
    fn default() -> Self {
        Self::new()
    }
}

// Re-use RenderPassDescriptor from surface module
pub use crate::surface::RenderPassDescriptor;

/// Render pass encoder.
pub struct RenderPassEncoder<'a> {
    encoder: &'a mut CommandEncoder,
}

impl<'a> RenderPassEncoder<'a> {
    /// Set the current pipeline.
    pub fn set_pipeline(&mut self, pipeline_id: u64) {
        self.encoder.buffer.set_pipeline(pipeline_id);
    }

    /// Set a bind group.
    pub fn set_bind_group(&mut self, index: u32, bind_group_id: u64, dynamic_offsets: &[u32]) {
        self.encoder
            .buffer
            .set_bind_group(index, bind_group_id, dynamic_offsets);
    }

    /// Set a vertex buffer.
    pub fn set_vertex_buffer(&mut self, slot: u32, buffer_id: u64, offset: u64, size: Option<u64>) {
        self.encoder
            .buffer
            .set_vertex_buffer(slot, buffer_id, offset, size);
    }

    /// Set the index buffer.
    pub fn set_index_buffer(
        &mut self,
        buffer_id: u64,
        format: IndexFormat,
        offset: u64,
        size: Option<u64>,
    ) {
        self.encoder
            .buffer
            .set_index_buffer(buffer_id, format, offset, size);
    }

    /// Set scissor rect.
    pub fn set_scissor_rect(&mut self, x: u32, y: u32, width: u32, height: u32) {
        self.encoder
            .buffer
            .set_scissor(ScissorRect::new(x, y, width, height));
    }

    /// Set viewport.
    pub fn set_viewport(
        &mut self,
        x: f32,
        y: f32,
        width: f32,
        height: f32,
        min_depth: f32,
        max_depth: f32,
    ) {
        self.encoder.buffer.set_viewport(Viewport {
            x,
            y,
            width,
            height,
            min_depth,
            max_depth,
        });
    }

    /// Set blend constant.
    pub fn set_blend_constant(&mut self, color: [f32; 4]) {
        self.encoder.buffer.set_blend_constant(color);
    }

    /// Set stencil reference.
    pub fn set_stencil_reference(&mut self, reference: u32) {
        self.encoder.buffer.set_stencil_reference(reference);
    }

    /// Draw primitives.
    pub fn draw(
        &mut self,
        vertex_count: u32,
        instance_count: u32,
        first_vertex: u32,
        first_instance: u32,
    ) {
        self.encoder.buffer.draw_with_offsets(
            vertex_count,
            instance_count,
            first_vertex,
            first_instance,
        );
    }

    /// Draw indexed primitives.
    pub fn draw_indexed(
        &mut self,
        index_count: u32,
        instance_count: u32,
        first_index: u32,
        base_vertex: i32,
        first_instance: u32,
    ) {
        self.encoder.buffer.draw_indexed_with_offsets(
            index_count,
            instance_count,
            first_index,
            base_vertex,
            first_instance,
        );
    }

    /// Push a debug group.
    pub fn push_debug_group(&mut self, label: &str) {
        self.encoder.buffer.push_debug_group(label);
    }

    /// Pop a debug group.
    pub fn pop_debug_group(&mut self) {
        self.encoder.buffer.pop_debug_group();
    }

    /// Insert a debug marker.
    pub fn insert_debug_marker(&mut self, label: &str) {
        self.encoder.buffer.insert_debug_marker(label);
    }
}

/// Compute pass encoder.
pub struct ComputePassEncoder<'a> {
    encoder: &'a mut CommandEncoder,
}

impl<'a> ComputePassEncoder<'a> {
    /// Set the current pipeline.
    pub fn set_pipeline(&mut self, pipeline_id: u64) {
        self.encoder.buffer.set_pipeline(pipeline_id);
    }

    /// Set a bind group.
    pub fn set_bind_group(&mut self, index: u32, bind_group_id: u64, dynamic_offsets: &[u32]) {
        self.encoder
            .buffer
            .set_bind_group(index, bind_group_id, dynamic_offsets);
    }

    /// Dispatch compute work.
    pub fn dispatch_workgroups(&mut self, x: u32, y: u32, z: u32) {
        // For now, we'll record this as a draw command
        // A proper implementation would have a dedicated compute dispatch command
        self.encoder.buffer.draw_with_offsets(x, y, z, 0);
    }

    /// Push a debug group.
    pub fn push_debug_group(&mut self, label: &str) {
        self.encoder.buffer.push_debug_group(label);
    }

    /// Pop a debug group.
    pub fn pop_debug_group(&mut self) {
        self.encoder.buffer.pop_debug_group();
    }

    /// Insert a debug marker.
    pub fn insert_debug_marker(&mut self, label: &str) {
        self.encoder.buffer.insert_debug_marker(label);
    }
}

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

    #[test]
    fn test_command_buffer() {
        let mut buffer = CommandBuffer::new();
        buffer.clear(Color::RED);
        buffer.draw(6, 1);

        assert_eq!(buffer.len(), 2);
        assert!(!buffer.is_empty());
    }

    #[test]
    fn test_command_encoder() {
        let mut encoder = CommandEncoder::new();

        {
            let mut pass =
                encoder.begin_render_pass(&RenderPassDescriptor::color_clear(0.0, 0.0, 0.0, 1.0));
            pass.set_pipeline(1);
            pass.draw(6, 1, 0, 0);
        }

        let buffer = encoder.finish();
        assert!(buffer.len() >= 2);
    }

    #[test]
    fn test_scissor_rect() {
        let rect = Rect::from_xywh(10.0, 20.0, 100.0, 200.0);
        let scissor = ScissorRect::from_rect(&rect);

        assert_eq!(scissor.x, 10);
        assert_eq!(scissor.y, 20);
        assert_eq!(scissor.width, 100);
        assert_eq!(scissor.height, 200);
    }

    #[test]
    fn test_viewport() {
        let viewport = Viewport::new(0.0, 0.0, 800.0, 600.0).with_depth(0.0, 1.0);

        assert_eq!(viewport.width, 800.0);
        assert_eq!(viewport.height, 600.0);
        assert_eq!(viewport.min_depth, 0.0);
        assert_eq!(viewport.max_depth, 1.0);
    }

    #[test]
    fn test_debug_groups() {
        let mut buffer = CommandBuffer::new();

        buffer.push_debug_group("outer");
        buffer.push_debug_group("inner");
        buffer.pop_debug_group();
        buffer.pop_debug_group();

        assert_eq!(buffer.debug_depth, 0);
    }
}