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'))

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật