proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
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
//! Multi-backend renderer: render passes, draw calls, compute dispatch,
//! buffer upload / readback, and vertex layout descriptions.

use super::backend::{
    BackendCapabilities, BackendContext, BufferHandle, BufferUsage, ComputePipelineHandle,
    GpuBackend, GpuCommand, PipelineHandle, PipelineLayout, ShaderHandle, ShaderStage,
    SoftwareContext, TextureFormat, TextureHandle,
};
use glam::{Vec3, Vec4};

// ---------------------------------------------------------------------------
// Vertex layout
// ---------------------------------------------------------------------------

/// Describes one vertex attribute within a vertex layout.
#[derive(Debug, Clone)]
pub struct VertexAttribute {
    pub location: u32,
    pub format: AttributeFormat,
    pub offset: u32,
}

/// Format of a single vertex attribute.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttributeFormat {
    Float,
    Float2,
    Float3,
    Float4,
    UInt,
    Int,
}

impl AttributeFormat {
    /// Size in bytes of this attribute format.
    pub fn byte_size(&self) -> u32 {
        match self {
            Self::Float  => 4,
            Self::Float2 => 8,
            Self::Float3 => 12,
            Self::Float4 => 16,
            Self::UInt   => 4,
            Self::Int    => 4,
        }
    }
}

/// A vertex layout describing how vertex data is organized.
#[derive(Debug, Clone)]
pub struct VertexLayout {
    pub attributes: Vec<VertexAttribute>,
}

impl VertexLayout {
    pub fn new() -> Self {
        Self { attributes: Vec::new() }
    }

    pub fn push(mut self, location: u32, format: AttributeFormat, offset: u32) -> Self {
        self.attributes.push(VertexAttribute { location, format, offset });
        self
    }

    /// Total stride in bytes (sum of attribute sizes, or max offset+size).
    pub fn stride(&self) -> u32 {
        self.attributes.iter().map(|a| a.offset + a.format.byte_size()).max().unwrap_or(0)
    }
}

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

// ---------------------------------------------------------------------------
// Uniform block
// ---------------------------------------------------------------------------

/// A block of uniform data to be bound at a specific binding index.
#[derive(Debug, Clone)]
pub struct UniformBlock {
    pub data: Vec<u8>,
    pub binding: u32,
}

impl UniformBlock {
    pub fn new(binding: u32, data: Vec<u8>) -> Self {
        Self { data, binding }
    }

    pub fn from_f32_slice(binding: u32, values: &[f32]) -> Self {
        let data: Vec<u8> = values.iter().flat_map(|f| f.to_le_bytes()).collect();
        Self { data, binding }
    }
}

// ---------------------------------------------------------------------------
// Render pass
// ---------------------------------------------------------------------------

/// Describes a render pass with colour and depth attachments.
#[derive(Debug, Clone)]
pub struct RenderPass {
    pub color_attachments: Vec<TextureHandle>,
    pub depth_attachment: Option<TextureHandle>,
    pub clear_color: [f32; 4],
}

impl RenderPass {
    pub fn new() -> Self {
        Self {
            color_attachments: Vec::new(),
            depth_attachment: None,
            clear_color: [0.0, 0.0, 0.0, 1.0],
        }
    }

    pub fn with_color(mut self, tex: TextureHandle) -> Self {
        self.color_attachments.push(tex);
        self
    }

    pub fn with_depth(mut self, tex: TextureHandle) -> Self {
        self.depth_attachment = Some(tex);
        self
    }

    pub fn with_clear(mut self, r: f32, g: f32, b: f32, a: f32) -> Self {
        self.clear_color = [r, g, b, a];
        self
    }
}

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

// ---------------------------------------------------------------------------
// Draw call
// ---------------------------------------------------------------------------

/// A single draw call to execute within a render pass.
#[derive(Debug, Clone)]
pub struct DrawCall {
    pub pipeline: PipelineHandle,
    pub vertex_buffer: BufferHandle,
    pub index_buffer: Option<BufferHandle>,
    pub vertex_count: u32,
    pub index_count: u32,
    pub instance_buffer: Option<BufferHandle>,
    pub instance_count: u32,
    pub uniforms: Vec<UniformBlock>,
}

impl DrawCall {
    pub fn new(pipeline: PipelineHandle, vertex_buffer: BufferHandle, vertex_count: u32) -> Self {
        Self {
            pipeline,
            vertex_buffer,
            index_buffer: None,
            vertex_count,
            index_count: 0,
            instance_buffer: None,
            instance_count: 1,
            uniforms: Vec::new(),
        }
    }

    pub fn with_instances(mut self, buffer: BufferHandle, count: u32) -> Self {
        self.instance_buffer = Some(buffer);
        self.instance_count = count;
        self
    }

    pub fn with_index_buffer(mut self, buffer: BufferHandle, count: u32) -> Self {
        self.index_buffer = Some(buffer);
        self.index_count = count;
        self
    }

    pub fn with_uniform(mut self, block: UniformBlock) -> Self {
        self.uniforms.push(block);
        self
    }
}

// ---------------------------------------------------------------------------
// Frame state
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FrameState {
    Idle,
    Recording,
}

// ---------------------------------------------------------------------------
// MultiBackendRenderer
// ---------------------------------------------------------------------------

/// The main renderer that dispatches draw calls and compute through a
/// `BackendContext`.
pub struct MultiBackendRenderer {
    pub backend: Box<dyn BackendContext>,
    pub capabilities: BackendCapabilities,
    frame_state: FrameState,
    recorded_commands: Vec<GpuCommand>,
    frame_count: u64,
}

impl MultiBackendRenderer {
    pub fn new(backend: Box<dyn BackendContext>, capabilities: BackendCapabilities) -> Self {
        Self {
            backend,
            capabilities,
            frame_state: FrameState::Idle,
            recorded_commands: Vec::new(),
            frame_count: 0,
        }
    }

    /// Create a renderer backed by the software context.
    pub fn software() -> Self {
        let caps = BackendCapabilities::for_backend(GpuBackend::Software);
        Self::new(Box::new(SoftwareContext::new()), caps)
    }

    /// Build a renderer from an existing pipeline's backend selection.
    /// In practice this would inspect the live Pipeline; here we default to Software.
    pub fn from_existing_pipeline(_pipeline: &()) -> Self {
        Self::software()
    }

    // -- frame lifecycle ----------------------------------------------------

    /// Begin recording a new frame.
    pub fn begin_frame(&mut self) {
        self.frame_state = FrameState::Recording;
        self.recorded_commands.clear();
    }

    /// Finish recording and submit all commands.
    pub fn end_frame(&mut self) {
        let cmds: Vec<GpuCommand> = std::mem::take(&mut self.recorded_commands);
        self.backend.submit(&cmds);
        self.frame_state = FrameState::Idle;
        self.frame_count += 1;
    }

    /// Present the current frame to the display.
    pub fn present(&mut self) {
        self.backend.present();
    }

    /// Number of frames completed so far.
    pub fn frame_count(&self) -> u64 {
        self.frame_count
    }

    // -- draw ---------------------------------------------------------------

    /// Execute a series of draw calls within a render pass.
    pub fn draw(&mut self, _pass: &RenderPass, calls: &[DrawCall]) {
        for call in calls {
            // Bind uniforms
            for uniform in &call.uniforms {
                let ubuf = self.backend.create_buffer(uniform.data.len(), BufferUsage::UNIFORM);
                self.backend.write_buffer(ubuf, &uniform.data);
                self.recorded_commands.push(GpuCommand::SetBindGroup {
                    index: uniform.binding,
                    buffers: vec![ubuf],
                });
            }

            if let Some(idx_buf) = call.index_buffer {
                self.recorded_commands.push(GpuCommand::DrawIndexed {
                    pipeline: call.pipeline,
                    vertex_buffer: call.vertex_buffer,
                    index_buffer: idx_buf,
                    index_count: call.index_count,
                    instance_count: call.instance_count,
                });
            } else {
                self.recorded_commands.push(GpuCommand::Draw {
                    pipeline: call.pipeline,
                    vertex_buffer: call.vertex_buffer,
                    vertex_count: call.vertex_count,
                    instance_count: call.instance_count,
                });
            }
        }
    }

    // -- compute ------------------------------------------------------------

    /// Dispatch a compute shader.
    pub fn dispatch_compute(
        &mut self,
        pipeline: ComputePipelineHandle,
        workgroups: [u32; 3],
        buffers: &[BufferHandle],
    ) {
        if !buffers.is_empty() {
            self.recorded_commands.push(GpuCommand::SetBindGroup {
                index: 0,
                buffers: buffers.to_vec(),
            });
        }
        self.recorded_commands.push(GpuCommand::Dispatch {
            pipeline,
            x: workgroups[0],
            y: workgroups[1],
            z: workgroups[2],
        });
    }

    // -- buffer operations --------------------------------------------------

    /// Read data back from a GPU buffer.
    pub fn read_buffer(&self, buffer: BufferHandle) -> Vec<u8> {
        self.backend.read_buffer(buffer)
    }

    /// Upload raw bytes to a GPU buffer.
    pub fn upload_buffer(&mut self, buffer: BufferHandle, data: &[u8]) {
        self.backend.write_buffer(buffer, data);
    }

    /// Upload raw pixel data to a texture.
    pub fn upload_texture(&mut self, texture: TextureHandle, data: &[u8]) {
        self.backend.write_texture(texture, data);
    }

    // -- resource creation (convenience) ------------------------------------

    /// Create a vertex buffer and fill it with the given data.
    pub fn create_vertex_buffer(&mut self, data: &[u8]) -> BufferHandle {
        let buf = self.backend.create_buffer(data.len(), BufferUsage::VERTEX);
        self.backend.write_buffer(buf, data);
        buf
    }

    /// Create an index buffer and fill it.
    pub fn create_index_buffer(&mut self, data: &[u8]) -> BufferHandle {
        let buf = self.backend.create_buffer(data.len(), BufferUsage::INDEX);
        self.backend.write_buffer(buf, data);
        buf
    }

    /// Create a uniform buffer and fill it.
    pub fn create_uniform_buffer(&mut self, data: &[u8]) -> BufferHandle {
        let buf = self.backend.create_buffer(data.len(), BufferUsage::UNIFORM);
        self.backend.write_buffer(buf, data);
        buf
    }

    /// Create a storage buffer.
    pub fn create_storage_buffer(&mut self, size: usize) -> BufferHandle {
        self.backend.create_buffer(size, BufferUsage::STORAGE)
    }

    /// Create a colour texture.
    pub fn create_color_texture(&mut self, w: u32, h: u32) -> TextureHandle {
        self.backend.create_texture(w, h, TextureFormat::RGBA8)
    }

    /// Create a depth texture.
    pub fn create_depth_texture(&mut self, w: u32, h: u32) -> TextureHandle {
        self.backend.create_texture(w, h, TextureFormat::Depth32F)
    }

    /// Destroy a buffer.
    pub fn destroy_buffer(&mut self, buf: BufferHandle) {
        self.backend.destroy_buffer(buf);
    }

    /// Destroy a texture.
    pub fn destroy_texture(&mut self, tex: TextureHandle) {
        self.backend.destroy_texture(tex);
    }

    /// Backend name.
    pub fn backend_name(&self) -> &str {
        self.backend.name()
    }
}

// ---------------------------------------------------------------------------
// RenderStats — collected per-frame
// ---------------------------------------------------------------------------

/// Statistics for a single frame.
#[derive(Debug, Clone, Default)]
pub struct RenderStats {
    pub draw_calls: u32,
    pub triangles: u32,
    pub compute_dispatches: u32,
    pub buffer_uploads: u32,
    pub texture_uploads: u32,
}

impl RenderStats {
    pub fn new() -> Self { Self::default() }

    pub fn record_draw(&mut self, tris: u32) {
        self.draw_calls += 1;
        self.triangles += tris;
    }

    pub fn record_compute(&mut self) {
        self.compute_dispatches += 1;
    }

    pub fn record_buffer_upload(&mut self) {
        self.buffer_uploads += 1;
    }

    pub fn record_texture_upload(&mut self) {
        self.texture_uploads += 1;
    }
}

// ---------------------------------------------------------------------------
// StatsCollector — rolling window of frame stats
// ---------------------------------------------------------------------------

/// Keeps a rolling window of per-frame stats for profiling.
pub struct StatsCollector {
    history: Vec<RenderStats>,
    max_history: usize,
}

impl StatsCollector {
    pub fn new(max_history: usize) -> Self {
        Self {
            history: Vec::with_capacity(max_history),
            max_history,
        }
    }

    pub fn push(&mut self, stats: RenderStats) {
        if self.history.len() >= self.max_history {
            self.history.remove(0);
        }
        self.history.push(stats);
    }

    pub fn average_draw_calls(&self) -> f32 {
        if self.history.is_empty() { return 0.0; }
        let sum: u32 = self.history.iter().map(|s| s.draw_calls).sum();
        sum as f32 / self.history.len() as f32
    }

    pub fn average_triangles(&self) -> f32 {
        if self.history.is_empty() { return 0.0; }
        let sum: u32 = self.history.iter().map(|s| s.triangles).sum();
        sum as f32 / self.history.len() as f32
    }

    pub fn total_frames(&self) -> usize {
        self.history.len()
    }

    pub fn clear(&mut self) {
        self.history.clear();
    }
}

// ---------------------------------------------------------------------------
// Render queue — sorted draw calls
// ---------------------------------------------------------------------------

/// Priority for draw call ordering.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum RenderOrder {
    Background = 0,
    Opaque = 1,
    Transparent = 2,
    Overlay = 3,
    UI = 4,
}

/// A queued draw call with ordering metadata.
#[derive(Debug, Clone)]
pub struct QueuedDraw {
    pub order: RenderOrder,
    pub depth: u32,  // for sorting within the same order (front-to-back or back-to-front)
    pub call: DrawCall,
}

/// A draw-call queue that sorts by order, then depth.
pub struct RenderQueue {
    items: Vec<QueuedDraw>,
}

impl RenderQueue {
    pub fn new() -> Self {
        Self { items: Vec::new() }
    }

    pub fn push(&mut self, item: QueuedDraw) {
        self.items.push(item);
    }

    pub fn sort(&mut self) {
        self.items.sort_by(|a, b| {
            a.order.cmp(&b.order).then_with(|| {
                if a.order == RenderOrder::Transparent {
                    // Transparent: back to front (higher depth first)
                    b.depth.cmp(&a.depth)
                } else {
                    // Opaque: front to back (lower depth first)
                    a.depth.cmp(&b.depth)
                }
            })
        });
    }

    pub fn drain(&mut self) -> Vec<DrawCall> {
        self.sort();
        self.items.drain(..).map(|q| q.call).collect()
    }

    pub fn len(&self) -> usize { self.items.len() }
    pub fn is_empty(&self) -> bool { self.items.is_empty() }
    pub fn clear(&mut self) { self.items.clear(); }
}

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

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_renderer() -> MultiBackendRenderer {
        MultiBackendRenderer::software()
    }

    #[test]
    fn attribute_format_sizes() {
        assert_eq!(AttributeFormat::Float.byte_size(), 4);
        assert_eq!(AttributeFormat::Float3.byte_size(), 12);
        assert_eq!(AttributeFormat::Float4.byte_size(), 16);
    }

    #[test]
    fn vertex_layout_stride() {
        let layout = VertexLayout::new()
            .push(0, AttributeFormat::Float3, 0)   // pos: 0..12
            .push(1, AttributeFormat::Float2, 12);  // uv:  12..20
        assert_eq!(layout.stride(), 20);
    }

    #[test]
    fn uniform_block_from_f32() {
        let block = UniformBlock::from_f32_slice(0, &[1.0, 2.0]);
        assert_eq!(block.data.len(), 8);
        assert_eq!(block.binding, 0);
    }

    #[test]
    fn render_pass_builder() {
        let mut r = make_renderer();
        let color = r.create_color_texture(800, 600);
        let depth = r.create_depth_texture(800, 600);
        let pass = RenderPass::new()
            .with_color(color)
            .with_depth(depth)
            .with_clear(0.1, 0.1, 0.1, 1.0);
        assert_eq!(pass.color_attachments.len(), 1);
        assert!(pass.depth_attachment.is_some());
        assert_eq!(pass.clear_color[0], 0.1);
    }

    #[test]
    fn draw_call_builder() {
        let mut r = make_renderer();
        let vbuf = r.create_vertex_buffer(&[0u8; 64]);
        let pipe = r.backend.create_pipeline(
            r.backend.create_shader("v", ShaderStage::Vertex),
            r.backend.create_shader("f", ShaderStage::Fragment),
            &PipelineLayout::default(),
        );
        let call = DrawCall::new(pipe, vbuf, 3)
            .with_instances(vbuf, 10)
            .with_uniform(UniformBlock::new(0, vec![0u8; 64]));
        assert_eq!(call.instance_count, 10);
        assert_eq!(call.uniforms.len(), 1);
    }

    #[test]
    fn begin_end_frame() {
        let mut r = make_renderer();
        assert_eq!(r.frame_count(), 0);
        r.begin_frame();
        r.end_frame();
        assert_eq!(r.frame_count(), 1);
        r.present();
    }

    #[test]
    fn draw_within_pass() {
        let mut r = make_renderer();
        let vbuf = r.create_vertex_buffer(&[0u8; 48]);
        let pipe = r.backend.create_pipeline(
            r.backend.create_shader("v", ShaderStage::Vertex),
            r.backend.create_shader("f", ShaderStage::Fragment),
            &PipelineLayout::default(),
        );
        let pass = RenderPass::new();
        let call = DrawCall::new(pipe, vbuf, 3);

        r.begin_frame();
        r.draw(&pass, &[call]);
        r.end_frame();
        assert_eq!(r.frame_count(), 1);
    }

    #[test]
    fn dispatch_compute() {
        let mut r = make_renderer();
        let cs = r.backend.create_shader("compute", ShaderStage::Compute);
        let cp = r.backend.create_compute_pipeline(cs, &PipelineLayout::default());
        let buf = r.create_storage_buffer(256);

        r.begin_frame();
        r.dispatch_compute(cp, [4, 1, 1], &[buf]);
        r.end_frame();
    }

    #[test]
    fn upload_and_read_buffer() {
        let mut r = make_renderer();
        let buf = r.backend.create_buffer(8, BufferUsage::STORAGE);
        r.upload_buffer(buf, &[1, 2, 3, 4, 5, 6, 7, 8]);
        assert_eq!(r.read_buffer(buf), vec![1, 2, 3, 4, 5, 6, 7, 8]);
    }

    #[test]
    fn upload_texture() {
        let mut r = make_renderer();
        let tex = r.create_color_texture(2, 2);
        let pixels = vec![128u8; 16];
        r.upload_texture(tex, &pixels);
    }

    #[test]
    fn backend_name() {
        let r = make_renderer();
        assert_eq!(r.backend_name(), "Software");
    }

    #[test]
    fn render_stats() {
        let mut stats = RenderStats::new();
        stats.record_draw(100);
        stats.record_draw(200);
        stats.record_compute();
        assert_eq!(stats.draw_calls, 2);
        assert_eq!(stats.triangles, 300);
        assert_eq!(stats.compute_dispatches, 1);
    }

    #[test]
    fn stats_collector_rolling() {
        let mut collector = StatsCollector::new(3);
        for i in 0..5 {
            let mut s = RenderStats::new();
            s.record_draw(i * 10);
            collector.push(s);
        }
        assert_eq!(collector.total_frames(), 3);
        // last 3 frames: 20, 30, 40 draw calls=1 each
        assert!((collector.average_draw_calls() - 1.0).abs() < 0.01);
    }

    #[test]
    fn render_queue_sorting() {
        let mut r = make_renderer();
        let vbuf = r.create_vertex_buffer(&[0u8; 12]);
        let pipe = r.backend.create_pipeline(
            r.backend.create_shader("v", ShaderStage::Vertex),
            r.backend.create_shader("f", ShaderStage::Fragment),
            &PipelineLayout::default(),
        );

        let mut queue = RenderQueue::new();
        queue.push(QueuedDraw {
            order: RenderOrder::Transparent,
            depth: 10,
            call: DrawCall::new(pipe, vbuf, 3),
        });
        queue.push(QueuedDraw {
            order: RenderOrder::Opaque,
            depth: 5,
            call: DrawCall::new(pipe, vbuf, 3),
        });
        queue.push(QueuedDraw {
            order: RenderOrder::Background,
            depth: 0,
            call: DrawCall::new(pipe, vbuf, 3),
        });
        queue.push(QueuedDraw {
            order: RenderOrder::Transparent,
            depth: 20,
            call: DrawCall::new(pipe, vbuf, 3),
        });

        let calls = queue.drain();
        assert_eq!(calls.len(), 4);
    }

    #[test]
    fn render_queue_empty() {
        let queue = RenderQueue::new();
        assert!(queue.is_empty());
        assert_eq!(queue.len(), 0);
    }

    #[test]
    fn draw_indexed() {
        let mut r = make_renderer();
        let vbuf = r.create_vertex_buffer(&[0u8; 48]);
        let ibuf = r.create_index_buffer(&[0u8; 12]);
        let pipe = r.backend.create_pipeline(
            r.backend.create_shader("v", ShaderStage::Vertex),
            r.backend.create_shader("f", ShaderStage::Fragment),
            &PipelineLayout::default(),
        );
        let call = DrawCall::new(pipe, vbuf, 3)
            .with_index_buffer(ibuf, 6);
        assert_eq!(call.index_count, 6);

        r.begin_frame();
        r.draw(&RenderPass::new(), &[call]);
        r.end_frame();
    }

    #[test]
    fn destroy_resources() {
        let mut r = make_renderer();
        let buf = r.create_vertex_buffer(&[0u8; 16]);
        let tex = r.create_color_texture(4, 4);
        r.destroy_buffer(buf);
        r.destroy_texture(tex);
        assert!(r.read_buffer(buf).is_empty());
    }

    #[test]
    fn from_existing_pipeline() {
        let unit = ();
        let r = MultiBackendRenderer::from_existing_pipeline(&unit);
        assert_eq!(r.backend_name(), "Software");
    }
}