Shaders/Pipelines

Haiku provides two data structure ShaderPipeline and ShaderProgram and assiociated utility functions in two namespace, respectively, Haiku::Pipeline and Haiku::ShaderStage

To create a shader pipeline, you will have to create/load shaders programs from files or string and attach to it:

ShaderPipeline  prog;
ShaderProgram   frag;
ShaderProgram   vert;

ShaderStage::CreateFromFile(&vert, GL_VERTEX_SHADER  , "vertex_shader.glsl");
ShaderStage::CreateFromFile(&frag, GL_FRAGMENT_SHADER, "fragment_shader.glsl");

Pipeline::Create(&prog);
Pipeline::Attach(&prog,vert);
Pipeline::Attach(&prog,frag);
ShaderPipeline  prog;
ShaderProgram   frag;
ShaderProgram   vert;

ShaderStage::CreateFromFile(&vert, GL_VERTEX_SHADER  , "vertex_shader.glsl");
ShaderStage::CreateFromFile(&frag, GL_FRAGMENT_SHADER, "fragment_shader.glsl");

Pipeline::Create(&prog);
Pipeline::Attach(&prog,vert);
Pipeline::Attach(&prog,frag);
Pipeline::Link();

Then, to activate a shader pipeline you just have to activate it:

Pipeline::Activate(prog);

If you have to pass uniform variables to the shader, code depends on the requested OpenGL backend. Here’s an example setting the following glsl uniforms :

layout(location = 0) uniform float iTime;
layout(location = 1) uniform ivec2 iResolution;
layout(location = 2) uniform mat4  iWorld;
uniform float iTime;
uniform ivec2 iResolution;
uniform mat4  iWorld;

you can use the following CPU code :

mat4 world_matrix(1.f);
ShaderStage::SetUniformMat4f(frag, 2, &world_matrix.at[0]);
ShaderStage::SetUniform(frag, 0, application_timer);
ShaderStage::SetUniform(frag, 1, IO::FrameWidth(), IO::FrameHeight());
int itime_location = Pipeline::UniformLocation(prog,"iTime");
int iworld_location = Pipeline::UniformLocation(prog,"iWorld");
int iresolution_location = Pipeline::UniformLocation(prog,"iResolution");

mat4 world_matrix(1.f);
ShaderStage::SetUniformMat4f(frag, iworld_location, &world_matrix.at[0]);
ShaderStage::SetUniform(frag, itime_location, application_timer);
ShaderStage::SetUniform(frag, iresolution_location, IO::FrameWidth(), IO::FrameHeight());

Pipeline Description

struct ShaderPipeline

GPU program pipeline object.

Public Members

uint32_t id

OpenGL program pipeline id

namespace Pipeline

Functions

void Create(ShaderPipeline *pipeline, const ShaderProgram &vertex, const ShaderProgram &fragment)

Create a program pipeline object (with vertex and fragment shader)

void Create(ShaderPipeline *pipeline, const ShaderProgram &compute)

Create a program pipeline object (with a single compute shader)

void Create(ShaderPipeline *pipeline)

Create an empty program pipeline object.

void Destroy(ShaderPipeline *pipeline)

Destroys a program pipeline object.

void Attach(ShaderPipeline *pipeline, const ShaderProgram &stage)

Attach a shader stage to the pipeline.

void Link(ShaderPipeline *pipeline)

GL33 only : Links shader stages to the pipeline.

void Activate(const ShaderPipeline &pipeline)

Activate the program pipeline object.

void Dispatch(uint32_t num_groups_x, uint32_t num_groups_y, uint32_t num_groups_z)

Call dispatch function for compute pipelines.

Program Description

struct ShaderProgram

GPU shader stage program.

Public Members

std::string path

Shader source path.

std::string definitions

Shader definitions.

std::string source

Shader source code.

uint32_t type

Shader type.

uint32_t id

Shader program id.

namespace ShaderStage

Functions

void CreateScreenVertexShader(ShaderProgram *shader)

Create a screen vertex shader object.

void CreateFromFile(ShaderProgram *shader, uint32_t type, const std::string &filepath, bool reload_on_failure = true)

Create a shader stage from a file.

Loads an ASCII shader file. Tries to compile the shader source code. The shader program can be used later to create a Program Pipeline object.

Parameters:
  • shader – The shader program object pointer

  • type – OpenGL GLenum shader type (ex: GL_VERTEX_SHADER)

  • filepath – The path of the shader source code file

  • reload_on_failure – Reloads the shader on failure (allows to edit shader file on disk)

void CreateFromString(ShaderProgram *shader, uint32_t type, const std::string &source, bool reload_on_failure = false)

Create a shader stage from a string.

Parameters:
  • shader – The shader program object pointer

  • type – OpenGL GLenum shader type (ex: GL_VERTEX_SHADER)

  • source – The shader source string

  • reload_on_failure – Reloads the shader on failure (default is false)

void CreateShadertoyFromFile(ShaderProgram *shader, const std::string &filepath, bool reload_on_failure = true)

Create a shadertoy-like stage from a file.

Parameters:
  • shader – The shader program object pointer

  • filepath – The path of the shader source code file

  • reload_on_failure – Reloads the shader on failure (allows to edit shader file on disk)

void ReloadFromFile(ShaderPipeline *pipeline, ShaderProgram *shader, const std::string &filepath, bool reload_on_failure = true)

Reloads a shader stage from a file and rebind it to the requested pipeline.

Parameters:
  • pipeline – The program pipeline object pointer

  • shader – The shader program object pointer

  • filepath – The path of the shader source code file

  • reload_on_failure – Reloads the shader on failure (allows to edit shader file on disk)

void ReloadShadertoyFromFile(ShaderPipeline *pipeline, ShaderProgram *shader, const std::string &filepath, bool reload_on_failure = true)

Reloads a shadertoy from a file and rebind it to the requested pipeline.

Parameters:
  • pipeline – The program pipeline object pointer

  • shader – The shader program object pointer

  • filepath – The path of the shader source code file

  • reload_on_failure – Reloads the shader on failure (allows to edit shader file on disk)

void AddDefinitions(ShaderProgram *program, const std::string &definitions)

Add prefix to the shader source code (definitions).

void Destroy(ShaderProgram *program)

Destroys a shader stage.

void SetUniform(const ShaderProgram &shader, uint32_t unit, uint32_t val)

Sets uniform shader variable from an unsigned variable.

void SetUniform(const ShaderProgram &shader, uint32_t unit, int32_t val)

Sets uniform shader variable from an integer variable.

void SetUniform(const ShaderProgram &shader, uint32_t unit, float val)

Sets uniform shader variable from a float variable.

void SetUniform(const ShaderProgram &shader, uint32_t unit, bool val)

Sets uniform shader variable from a boolean variable.

void SetUniform(const ShaderProgram &shader, uint32_t unit, uint32_t x, uint32_t y)

Sets uniform shader uvec2 variable from 2 unsigned variables.

void SetUniform(const ShaderProgram &shader, uint32_t unit, int32_t x, int32_t y)

Sets uniform shader ivec2 variable from 2 integer variables.

void SetUniform(const ShaderProgram &shader, uint32_t unit, float x, float y)

Sets uniform shader vec2 variable from 2 float variables.

void SetUniform(const ShaderProgram &shader, uint32_t unit, float x, float y, float z)

Sets uniform shader vec3 variable from 3 float variables.

void SetUniform(const ShaderProgram &shader, uint32_t unit, float x, float y, float z, float w)

Sets uniform shader vec4 variable from 4 float variables.

void SetUniformVec2u(const ShaderProgram &shader, uint32_t unit, uint32_t *val)

Sets uniform shader variable from a 2-unsigned vector ptr.

void SetUniformVec2i(const ShaderProgram &shader, uint32_t unit, int32_t *val)

Sets uniform shader variable from a 2-integer vector ptr.

void SetUniformVec2f(const ShaderProgram &shader, uint32_t unit, float *val)

Sets uniform shader variable from a 2-float vector ptr.

void SetUniformVec3f(const ShaderProgram &shader, uint32_t unit, float *val)

Sets uniform shader variable from a 3-float vector ptr.

void SetUniformVec4f(const ShaderProgram &shader, uint32_t unit, float *val)

Sets uniform shader variable from a 4-float vector ptr.

void SetUniformMat4f(const ShaderProgram &shader, uint32_t unit, float *val)

Sets uniform shader variable from a 4x4 float matrix ptr.