Lazar Ljubenović
Lazar Ljubenović

Reputation: 19764

Why does adding a default branch in a switch statement of a WebGL2 fragment shader trigger dead code elimination for a uniform?

I have the following illustrative fragment shader.

#version 300 es
precision highp float;

flat in int v_Discriminator;

uniform float u_Numbers[2];
out vec4 out_Color;

void main () {
  vec4 gray = vec4(0.5, 0.5, 0.5, 1.0);
  switch (v_Discriminator) {
    case 0:
      out_Color = u_Numbers[0] * gray;
    case 1:
      out_Color = u_Numbers[1] * gray;
    default:
      out_Color = gray;
  }
}

When I query the location of the u_Numbers uniform, I get null. Why does this happen?

I've narrowed down the reason to the presence of default branch. More precisely, to the fact that u_Numbers is not used in the default branch. If I reference u_Numbers there, I get the uniform location.

Similarly, if I remove the default branch completely, I also get the location, i.e. the uniform doesn't get removed due to DCE (dead code elimination).

switch (v_Discriminator) {
  case 0:
    out_Color = u_Numbers[0] * gray;
  case 1:
    out_Color = u_Numbers[1] * gray;
  default:
    out_Color = u_Numbers[1] * gray;
    // u_Numbers is referenced here => now the uniform exists
}
switch (v_Discriminator) {
  case 0:
    out_Color = u_Numbers[0] * gray;
  case 1:
    out_Color = u_Numbers[1] * gray;
  // no default branch => now the uniform exists
}

It's tied only to the default branch. Even if no other branches reference the uniform, its presence in the default branch seems to be enough.

switch (v_Discriminator) {
  case 0:
    out_Color = gray; // doesn't matter that we've removed references
  case 1:
    out_Color = gray; // the uniform still exists
  default:
    out_Color = u_Numbers[1] * gray;
}

Another interesting thing is that putting it in a function removes this effect completely. That is, the following snippet runs fine.

vec4 getColor () {
  vec4 gray = vec4(0.5, 0.5, 0.5, 1.0);
  switch (v_Discriminator) {
    case 0:
      return u_Numbers[0] * gray;
    case 1:
      return u_Numbers[1] * gray;
    default:
      return gray; // even though there's no reference,
                   // the uniform still exists
  }
}

void main () {
  out_Color = getColor();
}

Here's a snippet you can play around with. The first two functions are just helpers for compiling shaders. The meat of the problem is near the fragment shader at the bottom and the two calls after it which demonstrate the issue.

function createAndCompileGlShader (gl, type, source) {
  const shader = gl.createShader(type)
  if (shader == null) {
    throw new Error(`Could not create a shader.`)
  }
  gl.shaderSource(shader, source)
  gl.compileShader(shader)
  const shaderCompiledSuccessfully = gl.getShaderParameter(shader, gl.COMPILE_STATUS)
  if (!shaderCompiledSuccessfully) {
    console.error(gl.getShaderInfoLog(shader))
    throw new Error(`Could not compile the shader.`)
  }
  return shader
}

function createGlProgram (gl, vertexShader, fragmentShader): WebGLProgram {
  const glProgram = gl.createProgram()
  if (glProgram == null) throw new Error(`Expected glProgram to be non-null.`)
  gl.attachShader(glProgram, vertexShader)
  gl.attachShader(glProgram, fragmentShader)
  gl.linkProgram(glProgram)
  const linkedSuccessfully = gl.getProgramParameter(glProgram, gl.LINK_STATUS)
  if (!linkedSuccessfully) {
    console.error(gl.getProgramInfoLog(glProgram))
    throw new Error(`Oops!`)
  }
  return glProgram
}

const canvas = document.createElement('canvas')
document.body.append(canvas)
const gl = canvas.getContext('webgl2')

const vertexShader = createAndCompileGlShader(gl, gl.VERTEX_SHADER, `#version 300 es

flat out int v_Discriminator;

void main () {
  gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
  v_Discriminator = 0;
}
  `)

  const fragmentShader = createAndCompileGlShader(gl, gl.FRAGMENT_SHADER, `#version 300 es
  precision highp float;

  flat in int v_Discriminator;

  uniform float u_Numbers[2];
  out vec4 out_Color;

  void main () {
    vec4 gray = vec4(0.5, 0.5, 0.5, 1.0);
    switch (v_Discriminator) {
      case 0:
        out_Color = u_Numbers[0] * gray;
      case 1:
        out_Color = u_Numbers[1] * gray;
      default:
        out_Color = gray;
    }
  }
    `)

const glProgram = createGlProgram(gl, vertexShader, fragmentShader)

console.log('location: ', gl.getUniformLocation(glProgram, 'u_Numbers'))

Upvotes: 0

Views: 37

Answers (0)

Related Questions