webgpu

WGPU how to get 'uniform' information from compiled shader


I am looking for a way to fetch all available uniforms in a WGPU shader, essentially, I am looking for the wgpu equivalent of this OpenGL code:

   gl.GetProgramiv(id, GL_ACTIVE_UNIFORMS, &active_uniforms);

   for (int i = 0; i < active_uniforms; i++){
      GLsizei length;
      GLsizei size;
      GLenum type;
      GLchar name[max_name_length + 1] = { 0 };

      gl.GetActiveUniform(id, i, max_name_length, &length, &size, &type, name);
      name[length] = '\0';
      
      uniforms.push(...)
    }
    // iterate over all unifroms
    uint32 buffer_size = ...
    // create uniform buffer big enough to hold all of them

Context: I am writing a toy 2D renderer. I have a 'Material' class that is supposed to hold shader + uniform data.

It will upload the data to the GPU just before a render pass. I need to create a buffer for this uniform data, and I need to know how big this buffer should be. The size of the buffer, depends on the number of uniforms defined in the shader code.

maybe irrelevant: I am writing this in Typescript for the web, not Rust/C++


Solution

  • WebGPU does not provide any "uniform" data nor any other reflection data. It's up to you to compute the offsets yourself, use a tool, or to use a library.