Struct tetra::graphics::Shader

source ·
pub struct Shader { /* private fields */ }
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.

Bear in mind that there is a hardware-defined limit on how many uniform locations can be used per shader. OpenGL 3.0 guarantees there will be at least 1024 of these locations available, which sounds like a lot - however, some types can use up multiple locations (e.g. a vec2 uses 2, a mat4 uses 16, an array of 4 mat4s uses 64, and so on).

Performance

Creating a shader is quite an expensive operation, as it involves parsing and validating the GLSL code. Try to reuse shaders, rather than recreating them every frame.

You can clone a shader cheaply, as it is a reference-counted handle to a GPU resource. However, this does mean that modifying a shader (e.g. setting a uniform) will also affect any clones that exist of it.

Examples

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

Implementations§

source§

impl Shader

source

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

Creates a new shader program from the given files.

Errors
source

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

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

The default fragment shader will be used.

Errors
source

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

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

The default vertex shader will be used.

Errors
source

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

Creates a new shader program from the given strings.

Errors
source

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

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

The default fragment shader will be used.

Errors
source

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

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

The default vertex shader will be used.

Errors
source

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

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§

source§

impl Clone for Shader

source§

fn clone(&self) -> Shader

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Shader

source§

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

Formats the value using the given formatter. Read more
source§

impl PartialEq<Shader> for Shader

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for Shader

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,