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
use crate::buffer::*;
use crate::commands::*;

use crate::pipeline::*;
use crate::texture::*;

#[derive(Default, Clone)]
pub struct Renderer {
    commands: Vec<Commands>,
    size: (u32, u32),
    primitive: DrawPrimitive,
    slot_count: u32,
}

impl Renderer {
    pub fn new(width: u32, height: u32) -> Self {
        Self {
            size: (width, height),
            commands: vec![Commands::Size { width, height }],
            primitive: DrawPrimitive::Triangles,
            slot_count: 0,
        }
    }

    pub fn begin(&mut self, options: Option<ClearOptions>) {
        let (color, stencil, depth) = match options {
            Some(opts) => (opts.color, opts.stencil, opts.depth),
            _ => (None, None, None),
        };

        self.commands.push(Commands::Begin {
            color,
            stencil,
            depth,
        });
    }

    pub fn set_primitive(&mut self, primitive: DrawPrimitive) {
        self.primitive = primitive;
    }

    pub fn end(&mut self) {
        self.commands.push(Commands::End);
        self.unbind_textures();
    }

    pub fn set_size(&mut self, width: u32, height: u32) {
        self.size = (width, height);
        self.commands.push(Commands::Size { width, height });
    }

    pub fn size(&self) -> (u32, u32) {
        self.size
    }

    pub fn width(&self) -> u32 {
        self.size.0
    }

    pub fn height(&self) -> u32 {
        self.size.1
    }

    pub fn set_viewport(&mut self, x: f32, y: f32, width: f32, height: f32) {
        self.commands.push(Commands::Viewport {
            x,
            y,
            width,
            height,
        });
    }

    pub fn set_scissors(&mut self, x: f32, y: f32, width: f32, height: f32) {
        self.commands.push(Commands::Scissors {
            x,
            y,
            width,
            height,
        });
    }

    pub fn set_pipeline(&mut self, pipeline: &Pipeline) {
        self.commands.push(Commands::Pipeline {
            id: pipeline.id(),
            options: pipeline.options,
        });
    }

    pub fn bind_buffer(&mut self, buffer: &Buffer) {
        self.commands.push(Commands::BindBuffer { id: buffer.id() });
    }

    pub fn bind_buffers(&mut self, buffers: &[&Buffer]) {
        self.commands
            .extend(buffers.iter().map(|b| Commands::BindBuffer { id: b.id() }));
    }

    pub fn draw(&mut self, offset: i32, count: i32) {
        self.commands.push(Commands::Draw {
            primitive: self.primitive,
            offset,
            count,
        })
    }

    pub fn draw_instanced(&mut self, offset: i32, count: i32, length: i32) {
        self.commands.push(Commands::DrawInstanced {
            primitive: self.primitive,
            offset,
            count,
            length,
        })
    }

    pub fn bind_texture(&mut self, location: u32, texture: &Texture) {
        self.bind_texture_slot(self.slot_count, location, texture);
        self.slot_count += 1;
    }

    pub fn bind_texture_slot(&mut self, slot: u32, location: u32, texture: &Texture) {
        self.commands.push(Commands::BindTexture {
            slot,
            location,
            id: texture.id(),
        })
    }

    pub fn unbind_textures(&mut self) {
        self.slot_count = 0;
    }

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

    pub fn commands(&self) -> &[Commands] {
        &self.commands
    }
}