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
use super::*;

mod parameters;

pub use parameters::*;

#[derive(Debug, Copy, Clone)]
pub enum DrawMode {
    Points,
    Lines { line_width: f32 },
    LineStrip { line_width: f32 },
    LineLoop { line_width: f32 },
    Triangles,
    TriangleStrip,
    TriangleFan,
}

pub fn clear(framebuffer: &mut Framebuffer, color: Option<Color<f32>>, depth: Option<f32>) {
    let gl = &framebuffer.fbo.ugli.inner;
    framebuffer.fbo.bind();
    let mut flags = 0;
    if let Some(color) = color {
        flags |= raw::COLOR_BUFFER_BIT;
        gl.clear_color(color.r as _, color.g as _, color.b as _, color.a as _);
        gl.color_mask(raw::TRUE, raw::TRUE, raw::TRUE, raw::TRUE);
    }
    if let Some(depth) = depth {
        flags |= raw::DEPTH_BUFFER_BIT;
        gl.clear_depth(depth as _);
        gl.depth_mask(raw::TRUE);
    }
    gl.clear(flags);
    framebuffer.fbo.ugli.debug_check();
}

pub fn draw<V, U, DP>(
    framebuffer: &mut Framebuffer,
    program: &Program,
    mode: DrawMode,
    vertices: V,
    uniforms: U,
    draw_parameters: DP,
) where
    V: VertexDataSource,
    U: Uniforms,
    DP: std::borrow::Borrow<DrawParameters>,
{
    program.ugli.debug_check();
    let gl = &program.ugli.inner;

    framebuffer.fbo.bind();
    let draw_parameters = draw_parameters.borrow();
    draw_parameters.apply(gl, framebuffer.size());
    program.bind();
    unsafe {
        UNIFORM_TEXTURE_COUNT = 0;
    }
    uniforms.walk_uniforms(&mut UC { program });

    #[cfg(not(target_arch = "wasm32"))]
    let vao = Vao::new(gl);
    #[cfg(not(target_arch = "wasm32"))]
    vao.bind();

    let mut vertex_count = None;
    let mut instance_count = None;
    vertices.walk_data(Vdc {
        program,
        vertex_count: &mut vertex_count,
        instance_count: &mut instance_count,
    });
    let vertex_count = vertex_count.unwrap();
    if vertex_count == 0 {
        return;
    }
    let gl_mode = match mode {
        DrawMode::Points => raw::POINTS,
        DrawMode::Lines { line_width } => {
            gl.line_width(line_width as _);
            assert!(vertex_count % 2 == 0);
            raw::LINES
        }
        DrawMode::LineStrip { line_width } => {
            gl.line_width(line_width as _);
            assert!(vertex_count >= 2);
            raw::LINE_STRIP
        }
        DrawMode::LineLoop { line_width } => {
            gl.line_width(line_width as _);
            assert!(vertex_count >= 3);
            raw::LINE_LOOP
        }
        DrawMode::Triangles => {
            assert!(vertex_count % 3 == 0);
            raw::TRIANGLES
        }
        DrawMode::TriangleStrip => {
            assert!(vertex_count >= 3);
            raw::TRIANGLE_STRIP
        }
        DrawMode::TriangleFan => {
            assert!(vertex_count >= 3);
            raw::TRIANGLE_FAN
        }
    };

    if vertex_count != 0 {
        if let Some(instance_count) = instance_count {
            if instance_count != 0 {
                gl.draw_arrays_instanced(gl_mode, 0, vertex_count as _, instance_count as _);
            }
        } else {
            gl.draw_arrays(gl_mode, 0, vertex_count as _);
        }
    }

    for attribute_info in program.attributes.values() {
        gl.disable_vertex_attrib_array(attribute_info.location);
    }

    program.ugli.debug_check();

    struct UC<'a> {
        program: &'a Program,
    }
    impl<'a> UniformVisitor for UC<'a> {
        fn visit<U: Uniform>(&mut self, name: &str, uniform: &U) {
            if let Some(uniform_info) = self.program.uniforms.get(name) {
                uniform.apply(&self.program.ugli.inner, uniform_info);
            }
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    struct Vao<'a> {
        handle: raw::VertexArrayObject,
        gl: &'a raw::Context,
    }

    #[cfg(not(target_arch = "wasm32"))]
    impl<'a> Vao<'a> {
        fn new(gl: &'a raw::Context) -> Self {
            Self {
                handle: gl.create_vertex_array().unwrap(),
                gl,
            }
        }
        fn bind(&self) {
            self.gl.bind_vertex_array(&self.handle);
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    impl<'a> Drop for Vao<'a> {
        fn drop(&mut self) {
            self.gl.delete_vertex_array(&self.handle);
        }
    }

    struct Vdc<'a> {
        program: &'a Program,
        vertex_count: &'a mut Option<usize>,
        instance_count: &'a mut Option<usize>,
    }
    impl<'a> VertexDataVisitor for Vdc<'a> {
        fn visit<'b, D: Vertex + 'b, T: IntoVertexBufferSlice<'b, D>>(
            &mut self,
            data: T,
            divisor: Option<usize>,
        ) {
            let data = data.into_slice();
            if let Some(divisor) = divisor {
                let instance_count = data.len() * divisor;
                if let Some(current_instance_count) = *self.instance_count {
                    assert_eq!(current_instance_count, instance_count);
                } else {
                    *self.instance_count = Some(instance_count);
                }
            } else if let Some(current_vertex_count) = *self.vertex_count {
                assert_eq!(current_vertex_count, data.len());
            } else {
                *self.vertex_count = Some(data.len());
            }
            let sample = unsafe {
                #[allow(clippy::uninit_assumed_init)] // TODO: check
                mem::MaybeUninit::<D>::uninit().assume_init()
            };
            data.buffer.bind();
            sample.walk_attributes(Vac {
                sample: &sample,
                divisor,
                program: self.program,
                offset: data.range.start * mem::size_of::<D>(),
            });
            mem::forget(sample);
            struct Vac<'a, D: Vertex + 'a> {
                offset: usize,
                sample: &'a D,
                divisor: Option<usize>,
                program: &'a Program,
            }
            impl<'a, D: Vertex> VertexAttributeVisitor for Vac<'a, D> {
                fn visit<A: VertexAttribute>(&mut self, name: &str, attribute: &A) {
                    let gl = &self.program.ugli.inner;
                    if let Some(attribute_info) = self.program.attributes.get(name) {
                        let offset = self.offset + attribute as *const _ as usize
                            - self.sample as *const _ as usize;
                        gl.enable_vertex_attrib_array(attribute_info.location);
                        gl.vertex_attrib_pointer(
                            attribute_info.location,
                            A::SIZE as raw::Int,
                            A::TYPE as raw::Enum,
                            raw::FALSE,
                            mem::size_of::<D>() as raw::SizeI,
                            offset as raw::IntPtr,
                        );
                        if let Some(divisor) = self.divisor {
                            gl.vertex_attrib_divisor(attribute_info.location, divisor as raw::UInt);
                        } else {
                            gl.vertex_attrib_divisor(attribute_info.location, 0);
                        }
                    }
                }
            }
        }
    }
}