glium 0.36.0

Elegant and safe OpenGL wrapper. Glium is an intermediate layer between OpenGL and your application. You still need to manually handle the graphics pipeline, but without having to use OpenGL's old and error-prone API. Its objectives: - Be safe to use. Many aspects of OpenGL that can trigger a crash if misused are automatically handled by glium. - Provide an API that enforces good pratices such as RAII or stateless function calls. - Be compatible with all OpenGL versions that support shaders, providing unified API when things diverge. - Avoid all OpenGL errors beforehand. - Produce optimized OpenGL function calls, and allow the user to easily use modern OpenGL techniques.
Documentation
#![cfg(feature = "unstable")]
#![feature(test)]

#[macro_use]
extern crate glium;
extern crate test;

use glium::Surface;

use test::Bencher;

mod support;

#[bench]
fn init(b: &mut Bencher) {
    b.iter(|| support::build_context());
}

#[bench]
fn clear(b: &mut Bencher) {
    let display = support::build_context();

    b.iter(|| {
        let mut target = glium::Frame::new(display.clone(), (800, 600));
        target.clear_color(0.0, 0.0, 0.0, 1.0);
        target.finish()
    });
}

#[bench]
fn create_program(b: &mut Bencher) {
    let display = support::build_context();

    b.iter(|| {
        program!(&display,
            140 => {
                vertex: "
                    #version 140

                    in vec2 position;
                    in vec3 color;

                    out vec3 v_color;

                    void main() {
                        gl_Position = vec4(position, 0.0, 1.0);
                        v_color = color;
                    }
                ",

                fragment: "
                    #version 140

                    in vec3 v_color;
                    out vec4 f_color;

                    void main() {
                        f_color = vec4(v_color, 1.0);
                    }
                ",
            },
        )
    });
}

#[bench]
#[ignore]       // TODO: segfaults
fn draw_triangle(b: &mut Bencher) {
    let display = support::build_context();

    let vertex_buffer = {
        #[derive(Copy, Clone)]
        struct Vertex {
            position: [f32; 2],
            color: [f32; 3],
        }

        implement_vertex!(Vertex, position, color);

        glium::VertexBuffer::new(&display,
            &[
                Vertex { position: [-0.5, -0.5], color: [1.0, 0.0, 0.0] },
                Vertex { position: [ 0.0,  0.5], color: [0.0, 1.0, 0.0] },
                Vertex { position: [ 0.5, -0.5], color: [0.0, 0.0, 1.0] },
            ]
        ).unwrap()
    };

    let program = program!(&display,
        140 => {
            vertex: "
                #version 140

                in vec2 position;
                in vec3 color;

                out vec3 v_color;

                void main() {
                    gl_Position = vec4(position, 0.0, 1.0);
                    v_color = color;
                }
            ",

            fragment: "
                #version 140

                in vec3 v_color;
                out vec4 f_color;

                void main() {
                    f_color = vec4(v_color, 1.0);
                }
            ",
        },
    ).unwrap();

    b.iter(|| {
        let mut target = glium::Frame::new(display.clone(), (800, 600));
        target.clear_color(0.0, 0.0, 0.0, 1.0);
        target.draw(&vertex_buffer, &glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
                    &program, &uniform!{}, &Default::default()).unwrap();
        target.finish().unwrap();
    });
}

#[bench]
fn build_buffer(b: &mut Bencher) {
    let display = support::build_context();

    b.iter(|| {
        #[derive(Copy, Clone)]
        struct Vertex {
            position: [f32; 2],
            color: [f32; 3],
        }

        implement_vertex!(Vertex, position, color);

        glium::VertexBuffer::new(&display,
            &[
                Vertex { position: [-0.5, -0.5], color: [1.0, 0.0, 0.0] },
                Vertex { position: [ 0.0,  0.5], color: [0.0, 1.0, 0.0] },
                Vertex { position: [ 0.5, -0.5], color: [0.0, 0.0, 1.0] },
            ]
        ).unwrap()
    });
}