Macro gfx::gfx_defines[][src]

macro_rules! gfx_defines {
    ($(#[$attr:meta])* vertex $name:ident {
            $( $(#[$field_attr:meta])* $field:ident : $ty:ty = $e:expr, )+
    }) => { ... };
    ($(#[$attr:meta])* constant $name:ident {
            $( $(#[$field_attr:meta])* $field:ident : $ty:ty = $e:expr, )+
    }) => { ... };
    (pipeline $name:ident {
            $( $field:ident : $ty:ty = $e:expr, )+
    }) => { ... };
    ($(#[$attr:meta])* vertex $name:ident {
            $( $(#[$field_attr:meta])* $field:ident : $ty:ty = $e:expr, )+
    } $($tail:tt)+) => { ... };
    ($(#[$attr:meta])* constant $name:ident {
            $( $(#[$field_attr:meta])* $field:ident : $ty:ty = $e:expr, )+
    } $($tail:tt)+) => { ... };
    ($keyword:ident $name:ident {
            $( $field:ident : $ty:ty = $e:expr, )+
    } $($tail:tt)+) => { ... };
}

Defines vertex, constant and pipeline formats in one block.

Example

#[macro_use]
extern crate gfx;

gfx_defines! {
    vertex Vertex {
        pos: [f32; 4] = "a_Pos",
        tex_coord: [f32; 2] = "a_TexCoord",
    }

    constant Locals {
        transform: [[f32; 4]; 4] = "u_Transform",
    }

    pipeline pipe {
        vbuf: gfx::VertexBuffer<Vertex> = (),
        // Global buffers are added for compatibility when
        // constant buffers are not supported.
        transform: gfx::Global<[[f32; 4]; 4]> = "u_Transform",
        locals: gfx::ConstantBuffer<Locals> = "Locals",
        color: gfx::TextureSampler<[f32; 4]> = "t_Color",
        out_color: gfx::RenderTarget<gfx::format::Rgba8> = "Target0",
        out_depth: gfx::DepthTarget<gfx::format::DepthStencil> =
            gfx::preset::depth::LESS_EQUAL_WRITE,
    }
}

impl Vertex {
    fn new(p: [i8; 3], tex: [i8; 2]) -> Vertex {
        Vertex {
            pos: [p[0] as f32, p[1] as f32, p[2] as f32, 1.0f32],
            tex_coord: [tex[0] as f32, tex[1] as f32],
        }
    }
}

fn main() {
    let vertex_data = [
        Vertex::new([-1, -1, 1], [0, 0]),
        Vertex::new([ 1, -1, 1], [1, 0]),
        // Add more vertices..
    ];
}

vertex and constant structures defined with gfx_defines! can be extended with attributes:

#[macro_use]
extern crate gfx;

gfx_defines! {
    #[derive(Default)]
    vertex Vertex {
        pos: [f32; 4] = "a_Pos",
        tex_coord: [f32; 2] = "a_TexCoord",
    }
}

fn main() {
    let vertex = Vertex::default();
    assert_eq!(vertex.pos[0], 0f32);
    assert_eq!(vertex.tex_coord[0], 0f32);
}

pipe

The pipeline state object or pso can consist of the following pso components:

Structure of a pipeline state object can be defined freely.

It should be noted however, that you can have multiple objects of everything but depth/stencil and scissor objects in a pipeline state object, which is the only restriction in the freedom of defining a pipeline state object.

vertex

Defines a vertex format to be passed onto a vertex buffer. Similar to pipeline state objects multiple vertex formats can be set.

constant

Defines a structure for shader constant data. This constant data is then appended into a constant buffer in the pso. Constant buffers are supported by DirectX 11 and OpenGL3 backend, but in OpenGL they are called Uniform Buffer Objects or UBOs.