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):
- In Chrome browser, Launch https://jsfiddle.net/cx20/vmkaqw2b/
- Switch bottom right shader code to "fragment"
- 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);
}
- 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.
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.
For the WGSL spec, see: https://www.w3.org/TR/WGSL/
Steps to Reproduce (standalone):
Resulting WGSL / WebGPU code:
Result:
Proposed Modification:
and update other places that refer to it in the shader code.