Expand description
A uniform is a global variable in your program. In order to draw something, you will need to
give glium
the values of all your uniforms. Objects that implement the Uniform
trait are
here to do that.
The currently preferred way to do this is to use the uniform!
macro provided by glium:
let uniforms = uniform! {
texture: tex,
matrix: matrix
};
Each field must implement the UniformValue
trait for this to work.
§Samplers
In order to customize the way a texture is being sampled, you must use a Sampler
.
let uniforms = uniform! {
texture: glium::uniforms::Sampler::new(&texture)
.magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest)
};
§Blocks
In GLSL, you can choose to use a uniform block. When you use a block, you first need to
upload the content of this block in the video memory thanks to a UniformBuffer
. Then you
can link the buffer to the name of the block, just like any other uniform.
let program = glium::Program::from_source(&display,
"
#version 110
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
",
"
#version 330
uniform layout(std140);
uniform MyBlock {
vec3 color;
};
void main() {
gl_FragColor = vec4(color, 1.0);
}
",
None);
let buffer = glium::uniforms::UniformBuffer::new(&display, (0.5f32, 0.5f32, 0.5f32)).unwrap();
let uniforms = uniform! {
MyBlock: &buffer
};
§Subroutines
OpenGL allows the use of subroutines, which are like function pointers. Subroutines can be used to change the functionality of a shader program at runtime. This method is usually a lot faster than using multiple programs that are switched during execution.
A subroutine uniform is unique per shader stage, and not per program.
let program = glium::Program::from_source(&display,
"
#version 150
in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
",
"
#version 150
#extension GL_ARB_shader_subroutine : require
out vec4 fragColor;
subroutine vec4 modify_t(vec4 color);
subroutine uniform modify_t modify_color;
subroutine(modify_t) vec4 delete_r(vec4 color)
{
return vec4(0, color.g, color.b, color.a);
}
subroutine(modify_t) vec4 delete_b(vec4 color)
{
return vec4(color.r, color.g, 0, color.a);
}
void main()
{
vec4 white= vec4(1, 1, 1, 1);
fragColor = modify_color(white);
}
", None);
let uniforms = uniform! {
modify_color: ("delete_b", glium::program::ShaderStage::Fragment)
};
Structs§
- Dynamic
Uniforms - Stores Uniforms dynamicly in a HashMap.
- Empty
Uniforms - Object that can be used when you don’t have any uniforms.
- Image
Unit - An image unit uniform marker
- Image
Unit Behavior - How we bind a texture to an image unit
- Sampler
- A sampler.
- Sampler
Behavior - Behavior of a sampler.
- Uniform
Buffer - Buffer that contains a uniform block.
- Uniforms
Storage - Stores uniforms.
Enums§
- Depth
Texture Comparison - The depth texture comparison operation to use when comparing the r value to the value in the currently bound texture.
- Image
Unit Access - States how the shader will access the image unit
- Image
Unit Error - Represents an error related to the use of an Image Unit
- Image
Unit Format - How the shader should interpret the data in the image
- Layout
Mismatch Error - Error about a block layout mismatch.
- Magnify
Sampler Filter - The function that the GPU will use when loading the value of a texel.
- Minify
Sampler Filter - The function that the GPU will use when loading the value of a texel.
- Sampler
Wrap Function - Function to use for out-of-bounds samples.
- Uniform
Type - Type of a uniform in a program.
- Uniform
Value - Represents a value to bind to a uniform.
Traits§
- AsUniform
Value - Value that can be used as the value of a uniform.
- Uniform
Block - Objects that are suitable for being inside a uniform block or a SSBO.
- Uniforms
- Object that contains the values of all the uniforms to bind to a program.