Skip to content

WebGPU/WGSL: Compiler Error, LightData struct member variable "type" is a reserved word. #2377

@scotbrew

Description

@scotbrew

When integrating MaterialX into USD WebGPU, we have come across a shader code issue in MaterialX.
In short, in the WebGPU/WGSL compiler, "type" is a reserved word, so cannot be used as a member variable name for the structure LightData GLSL code that WebGPU code is translated from.

As a test fix, "LightData.type" can be changed to "LightData.light_type" (or something similar). From sample shaders reviewed so far, this appears to be used internal to the shader source and is not set externally, so should minimize risk.

This is a blocker for USD WebGPU to use MaterialX materials as-is.

struct LightData
{
    int type;
    vec3 position;
    vec3 color;
    float intensity;
    float decay_rate;
    vec3 direction;
    float shadowOcclusion;
};

For the WGSL spec, see: https://www.w3.org/TR/WGSL/

Steps to Reproduce (standalone):

  1. In Chrome browser, Launch https://jsfiddle.net/cx20/vmkaqw2b/
  2. Switch bottom right shader code to "fragment"
  3. Change the GLSL code to be:
#version 450
layout(location = 0) out vec4 outColor;

struct LightData
{
    int type;
    vec3 position;
    vec3 color;
    float intensity;
    float decay_rate;
    vec3 direction;
    float shadowOcclusion;
};

void main() {
    LightData s;
    s.type = 2;
    s.color = vec3(1.0, 1.0, 1.0);
    outColor = vec4(0.0, 0.0, 1.0, 1.0);
}

Resulting WGSL / WebGPU code:

struct LightData {
  type : i32,
  position : vec3<f32>,
  color : vec3<f32>,
  intensity : f32,
  decay_rate : f32,
  direction : vec3<f32>,
  shadowOcclusion : f32,
}

var<private> outColor : vec4<f32>;

fn main_1() {
  var s : LightData;
  s.type = 2i;
  s.color = vec3<f32>(1.0f, 1.0f, 1.0f);
  outColor = vec4<f32>(0.0f, 0.0f, 1.0f, 1.0f);
  return;
}

struct main_out {
  @location(0)
  outColor_1 : vec4<f32>,
}

@fragment
fn main() -> main_out {
  main_1();
  return main_out(outColor);
}
  1. In a separate tab of the Chrome browser, open: https://google.github.io/tour-of-wgsl/. Copy the WGSL shader code to replace the @Fragment code on the right side. Change "fn main()" to "fn frag_main()".

Result:

error: 'type' is a reserved keyword
type : i32,
^^^^

Proposed Modification:

struct LightData
{
    int light_type;
    ...
};

and update other places that refer to it in the shader code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions