abstract class Prism::Shader::Program

Overview

Represents a shader program. This utilizes a ReferencePool(CompiledProgram) in order to re-use shader programs. Orphaned shaders will be garbage collected and their OpenGL resources released.

The key feature of this class is you can bind to uniforms by name. All of the complicated location binding is handled automatically.

Direct Known Subclasses

Defined in:

prism/shader/program.cr

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(file_name : String, &shader_reader : String -> String) #

Creates a new shader from file_name.

The vertex and fragment shaders will be interpolated from file_name, therefore file_name should be extension-less.

Shader programs may include other files using the #include "file.ext" statement. These includes are resolved by shader_reader which receives the name of the included file.

NOTE file_name will be passed to shader_reader as well.

Example

Shader.new "forward-point" do |path|
  File.read(File.join("my/shader/directory", path))
  # Ends up reading:
  # forward-point.vs
  # lighting.glh <- embeded into forward-point.vs
  # forward-point.fs
end

[View source]

Class Method Detail

def self.pool #

Retrieves the ReferencePool(CompiledProgram) pool. The pool allows you to re-use CompiledPrograms, and will automatically manage references to each CompiledProgram. See ReferencePool for more information.


[View source]

Instance Method Detail

def finalize #

[View source]
def set_uniform(name : String, value : Array(Shader::UniformStruct)) #

Sets an array of custom uniforms.


[View source]
def set_uniform(name : String, value : Shader::UniformStruct) #

Sets a custom uniform. The value will be serialized and it's generated values stored in the uniform.


[View source]
def set_uniform(name : String, value : LibGL::Int) #

Sets an integer uniform variable value


[View source]
def set_uniform(name : String, value : LibGL::Float) #

Sets a float uniform variable value


[View source]
def set_uniform(name : String, value : Vector3f) #

Sets a 3 dimensional float vector value to a uniform variable


[View source]
def set_uniform(name : String, value : Vector2f) #

Sets a 2 dimensional float vector value to a uniform variable


[View source]
def set_uniform(name : String, value : Matrix4f) #

Sets a 4 dimensional matrix float value to a uniform variable


[View source]
def set_uniform(name : String, value : Bool) #

Sets a boolean uniform variable value. Booleans are represented as floats in GLSL


[View source]
def set_uniform(name : String, value : Prism::Texture) #

Sets a texture uniform variable value.


[View source]
def start #

Binds the program to OpenGL so it can run. First we enable the program, then we bind values to all of the uniforms. Finally, we enable all of the attributes.


[View source]
def stop #

Unbinds the program from OpenGL so it won't run.


[View source]

Macro Detail

macro uniform(name, type) #

Generates a uniform property.

You can define uniforms a few different ways. Below are two examples that bind to the projection_matrix uniform.

uniform projection_matrix, Matrix4f
uniform "projection_matrix", Matrix4f

Now on your Shader::Program instance you can set the uniform

shader.projection_matrix = myMatrix

You can also use camel case for your uniform names. The generated methods will be underscored, but the uniform itself will remain camel case.

uniform projectionMatrix, Matrix4f

# later on...
shader.projection_matrix = myMatrix

# in the glsl code..
uniform vec4 projectionMatrix

[View source]