How To Pass Variables to a GPU Program

In this article, we saw how to use a GPU program with GLSL Hacker. Now we’re going to see how we can pass information (variables) to a GPU program.

GLSL Hacker does not re-invent the wheel. In OpenGL, shader input variables are called uniform variables or uniforms in short. OpenGL has a set of functions to deal with uniform variables: glUniform1f(), glUniform4f() and so on. GLSL Hacker uses the same way and syntax to pass data to a shader. All functions that pass values to a shader are in the gh_gpu_program lib:

OK let’s see a simple example. We have a shader that performs texturing and we want to modulate the final fragment color with a custom color. The fragment shader has two uniforms: the texture sampler (tex0 on texture unit 0) and the custom color (color):