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

pub struct Shader { /* fields omitted */ }

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 a color to multiply the output by.

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 two 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.

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.

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

  • TetraError::PlatformError will be returned if the underlying graphics API encounters an error.
  • TetraError::FailedToLoadAsset will be returned if the files could not be loaded.
  • TetraError::InvalidShader will be returned if the shader could not be compiled.

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

  • TetraError::PlatformError will be returned if the underlying graphics API encounters an error.
  • TetraError::FailedToLoadAsset will be returned if the file could not be loaded.
  • TetraError::InvalidShader will be returned if the shader could not be compiled.

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

  • TetraError::PlatformError will be returned if the underlying graphics API encounters an error.
  • TetraError::FailedToLoadAsset will be returned if the file could not be loaded.
  • TetraError::InvalidShader will be returned if the shader could not be compiled.

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

  • TetraError::PlatformError will be returned if the underlying graphics API encounters an error.
  • TetraError::InvalidShader will be returned if the shader could not be compiled.

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

  • TetraError::PlatformError will be returned if the underlying graphics API encounters an error.
  • TetraError::InvalidShader will be returned if the shader could not be compiled.

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

  • TetraError::PlatformError will be returned if the underlying graphics API encounters an error.
  • TetraError::InvalidShader will be returned if the shader could not be compiled.

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.

Trait Implementations

impl Clone for Shader[src]

impl Debug for Shader[src]

impl PartialEq<Shader> for Shader[src]

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]

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

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

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

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

impl<T> SetParameter for T

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

type Owned = T

The resulting type after obtaining ownership.

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.

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.