Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print errors in a more readable format in the player. #4137

Merged
merged 1 commit into from
Sep 14, 2023

Conversation

nical
Copy link
Contributor

@nical nical commented Sep 14, 2023

Checklist

  • Run cargo clippy.

Description

Use the Display impl to print errors instead of Debug.

For example when investigating an error with the player, shader validation errors go from

[2023-09-14T14:13:35Z ERROR naga::valid::compose] Vector component[1] type Scalar { kind: Sint, width: 4 }
thread 'main' panicked at 'Validation(ShaderError { source: "\n  struct Uniforms {\n    viewProjection: mat4x4f,\n    viewPosition: vec3f,\n    lightPosition: vec3f,\n    shininess: f32,\n  };\n\n  @group(0) @binding(0) var<uniform> uni: Uniforms;\n\n  struct Inst {\n    mat: mat4x4f,\n  };\n\n  @group(0) @binding(1) var<storage, read> perInst: array<Inst>;\n\n  struct VSInput {\n      @location(0) position: vec4f,\n      @location(1) normal: vec3f,\n      @location(2) color: vec4f,\n  };\n\n  struct VSOutput {\n    @builtin(position) position: vec4f,\n    @location(0) normal: vec3f,\n    @location(1) color: vec4f,\n    @location(2) surfaceToLight: vec3f,\n    @location(3) surfaceToView: vec3f,\n  };\n\n  @vertex\n  fn myVSMain(v: VSInput, @builtin(instance_index) instanceIndex: u32) -> VSOutput {\n    var vsOut: VSOutput;\n    let world = perInst[instanceIndex].mat;\n    vsOut.position = uni.viewProjection * world * v.position;\n    vsOut.normal = (world * vec4f(v.normal, 0)).xyz;\n    vsOut.color = v.color;\n\n    let surfaceWorldPosition = (world * v.position).xyz;\n    vsOut.surfaceToLight = uni.lightPosition - surfaceWorldPosition;\n    vsOut.surfaceToView = uni.viewPosition - surfaceWorldPosition;\n\n    return vsOut;\n  }\n\n  @fragment\n  fn myFSMain(v: VSOutput) -> @location(0) vec4f {\n    var normal = normalize(v.normal);\n\n    let surfaceToLightDirection = normalize(v.surfaceToLight);\n    let surfaceToViewDirection = normalize(v.surfaceToView);\n    let halfVector = normalize(surfaceToLightDirection + surfaceToViewDirection);\n\n    let light = dot(normal, surfaceToLightDirection) * 0.5 + 0.5;\n \n    var specular = 0.0;\n    if (light > 0.0) {\n      specular = pow(dot(normal, halfVector), uni.shininess);\n    }\n\n    return vec4f(v.color.rgb * light + specular, v.color.a);\n  }\n  ", label: None, inner: WithSpan { inner: EntryPoint { stage: Vertex, name: "myVSMain", source: Function(Expression { handle: [18], source: Compose(ComponentType { index: 1 }) }) }, spans: [(Span { start: 876, end: 894 }, "naga::Expression [18]")] } })', /home/nical/dev/rust/wgpu/player/src/lib.rs:284:21

to

shader compilation error:
---
  struct Uniforms {
    viewProjection: mat4x4f,
    viewPosition: vec3f,
    lightPosition: vec3f,
    shininess: f32,
  };

  @group(0) @binding(0) var<uniform> uni: Uniforms;

  struct Inst {
    mat: mat4x4f,
  };

  @group(0) @binding(1) var<storage, read> perInst: array<Inst>;

  struct VSInput {
      @location(0) position: vec4f,
      @location(1) normal: vec3f,
      @location(2) color: vec4f,
  };

  struct VSOutput {
    @builtin(position) position: vec4f,
    @location(0) normal: vec3f,
    @location(1) color: vec4f,
    @location(2) surfaceToLight: vec3f,
    @location(3) surfaceToView: vec3f,
  };

  @vertex
  fn myVSMain(v: VSInput, @builtin(instance_index) instanceIndex: u32) -> VSOutput {
    var vsOut: VSOutput;
    let world = perInst[instanceIndex].mat;
    vsOut.position = uni.viewProjection * world * v.position;
    vsOut.normal = (world * vec4f(v.normal, 0)).xyz;
    vsOut.color = v.color;

    let surfaceWorldPosition = (world * v.position).xyz;
    vsOut.surfaceToLight = uni.lightPosition - surfaceWorldPosition;
    vsOut.surfaceToView = uni.viewPosition - surfaceWorldPosition;

    return vsOut;
  }

  @fragment
  fn myFSMain(v: VSOutput) -> @location(0) vec4f {
    var normal = normalize(v.normal);

    let surfaceToLightDirection = normalize(v.surfaceToLight);
    let surfaceToViewDirection = normalize(v.surfaceToView);
    let halfVector = normalize(surfaceToLightDirection + surfaceToViewDirection);

    let light = dot(normal, surfaceToLightDirection) * 0.5 + 0.5;
 
    var specular = 0.0;
    if (light > 0.0) {
      specular = pow(dot(normal, halfVector), uni.shininess);
    }

    return vec4f(v.color.rgb * light + specular, v.color.a);
  }
  
---

Shader validation error: 
   ┌─ :36:29
   │
36 │     vsOut.normal = (world * vec4f(v.normal, 0)).xyz;
   │                             ^^^^^^^^^^^^^^^^^^ naga::Expression [18]

@nical nical merged commit 90b022d into gfx-rs:trunk Sep 14, 2023
20 checks passed
@nical nical deleted the player-errors branch September 14, 2023 16:12
bradwerth pushed a commit to bradwerth/wgpu that referenced this pull request Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants