# How can I use GLSL shaders within Mathematica?

The function SurfaceAppearance provides a number of preset shaders for use in Graphics and Graphics3D, however it is undocumented in Mathematica 14.0. Apparently, these shaders are written in GLSL.

I frequently use OpenCLLink / CUDALink for specialized image effects, and the ability to write GLSL shaders would be incredibly useful.

As it turns out, it is entirely possible in 14.0 to write your own custom shaders as this video by Yuzhu Lu demonstrates.

Here is an example where I use a fragment shader to warp an image inside a manipulate:

#version 330
uniform sampler2D theTexture;
uniform float time;
in vec2 v_texcoord;
out vec4 glFragColor;

void main()
{
vec2 newcoord = vec2(
v_texcoord.x + 0.05f*cos(time + 10 * v_texcoord.y),
v_texcoord.y + 0.05f*sin(time + 10 * v_texcoord.x)
);

vec4 texColor = texture(theTexture, newcoord);
glFragColor = texColor;
}


#version 330
uniform mat4 mvp_matrix;
in vec4 a_position;
in vec2 a_texcoord;
out vec2 v_texcoord;

void main()
{
gl_Position = mvp_matrix * a_position;
v_texcoord = a_texcoord;
}

Remove["Global*"];
"Attributes" -> <|"a_position" -> "ATTRIB_VERTEX",
"a_texcoord" -> "ATTRIB_TEXTURECOORD"|>, "Parameters" ->
<|"Matrix" -> {"mvp_matrix", "TRANSFORMMATRIX", "MATRIX4"},
"Time" -> {"time", 0.0, "NUMBER"}|>,
|>;
FEEvaluate[
Evaluate[
FEPrivateAddSurfaceAppearanceDefinition[customGLSLShader]]];
(* Check that it was loaded *)
FEEvaluate[FEPrivateListSurfaceAppearanceDefinitions["CustomShader"]]

tex = ExampleData[{"TestImage", "Mandrill"}];
Manipulate[
Graphics[{Texture[tex],

• Change the Graphics to Graphics3D and replace Rectangle[...] with Sphere[]` for even more hilarious results. Commented Apr 28 at 13:21