Struct tetra::graphics::Shader[][src]

pub struct Shader { /* fields omitted */ }
Expand description

A shader program, consisting of a vertex shader and a fragment shader.

Data Format

Shaders are written using GLSL.

Vertex Shaders

Vertex shaders take in data via three attributes:

  • a_position - A vec2 representing the position of the vertex in world space.
  • a_uv - A vec2 representing the texture co-ordinates that are associated with the vertex.
  • a_color - A vec4 representing the color of the vertex. This will be multiplied by u_diffuse and the color sampled from u_texture (see ‘Uniforms’ below).

Position data should be output as a vec4 to the built-in gl_Position variable.

Fragment Shaders

Color data should be output as a vec4 to the first output of the shader. This can be the built-in gl_FragColor variable, if you so desire.

Uniforms

By default, the shader is provided with three uniform variables:

  • u_projection - A mat4 which can be used to translate world space co-ordinates into screen space.
  • u_texture - A sampler2D which can be used to access color data from the currently active texture.
  • u_diffuse - A vec4 representing the color of the current geometry. This is currently only used to pass through the DrawParams::color for a Mesh, and will otherwise be set to Color::WHITE.

You can also set data into your own uniform variables via the set_uniform method.

Performance

Creating a Shader is a relatively expensive operation. If you can, store them in your State struct rather than recreating them each frame.

Cloning a Shader is a very cheap operation, as the underlying data is shared between the original instance and the clone via reference-counting. This does mean, however, that updating a Shader (for example, setting a uniform) will also update any other clones of that Shader.

Examples

The shaders example demonstrates how to draw using a custom shader, supplying inputs via uniform variables.

Implementations

impl Shader[src]

pub fn new<P>(
    ctx: &mut Context,
    vertex_path: P,
    fragment_path: P
) -> Result<Shader> where
    P: AsRef<Path>, 
[src]

Creates a new shader program from the given files.

Errors

pub fn from_vertex_file<P>(ctx: &mut Context, path: P) -> Result<Shader> where
    P: AsRef<Path>, 
[src]

Creates a new shader program from the given vertex shader file.

The default fragment shader will be used.

Errors

pub fn from_fragment_file<P>(ctx: &mut Context, path: P) -> Result<Shader> where
    P: AsRef<Path>, 
[src]

Creates a new shader program from the given fragment shader file.

The default vertex shader will be used.

Errors

pub fn from_string(
    ctx: &mut Context,
    vertex_shader: &str,
    fragment_shader: &str
) -> Result<Shader>
[src]

Creates a new shader program from the given strings.

Errors

pub fn from_vertex_string<P>(ctx: &mut Context, shader: &str) -> Result<Shader>[src]

Creates a new shader program from the given vertex shader string.

The default fragment shader will be used.

Errors

pub fn from_fragment_string<P>(
    ctx: &mut Context,
    shader: &str
) -> Result<Shader>
[src]

Creates a new shader program from the given fragment shader string.

The default vertex shader will be used.

Errors

pub fn set_uniform<V>(&self, ctx: &mut Context, name: &str, value: V) where
    V: UniformValue
[src]

Sets the value of the specifed uniform parameter.

See the UniformValue trait’s docs for a list of which types can be used as a uniform, and what their corresponding GLSL types are.

Trait Implementations

impl Clone for Shader[src]

fn clone(&self) -> Shader[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Shader[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl PartialEq<Shader> for Shader[src]

fn eq(&self, other: &Shader) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &Shader) -> bool[src]

This method tests for !=.

impl StructuralPartialEq for Shader[src]

Auto Trait Implementations

impl !RefUnwindSafe for Shader

impl !Send for Shader

impl !Sync for Shader

impl Unpin for Shader

impl !UnwindSafe for Shader

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.