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

Account for padding of structure fields in MSL backend #1081

Closed
zesterer opened this issue Jul 12, 2021 · 1 comment
Closed

Account for padding of structure fields in MSL backend #1081

zesterer opened this issue Jul 12, 2021 · 1 comment
Labels
area: back-end Outputs of shader conversion kind: bug Something isn't working lang: Metal Metal Shading Language

Comments

@zesterer
Copy link

zesterer commented Jul 12, 2021

I updated Naga to the latest commit 2 days ago. My WGSL shader parses and passes validation, but produces the following error on Mac OS (i.e: when Naga attempts to general MSL):

[2021-07-06T17:59:47Z ERROR wgpu::backend::direct] wgpu error: Validation Error
    
    Caused by:
        In Device::create_render_pipeline
          note: label = `Render pipeline`
        Internal error in VERTEX shader: Error compiling the shader "
"Compilation failed:
program_source:151:66: error: cannot initialize an array element of type 'char' with an lvalue of type 'float3' (vector of 3 'float' values)
    VertexOut _e57 = VertexOut {_e52 * _e50, _e45.norm, _e45.uv, _e50.xyz};
                                                                 ^~~~~~~~
program_source:151:66: warning: suggest braces around initialization of subobject
    VertexOut _e57 = VertexOut {_e52 * _e50, _e45.norm, _e45.uv, _e50.xyz};
                                                                 ^~~~~~~~
                                                                 {       }
program_source:176:51: error: cannot initialize an array element of type 'char' with an lvalue of type 'metal::float3' (aka 'float3')
    VertexOut _e46 = VertexOut {_e42, _e43, _e44, _e45};
                                                  ^~~~
program_source:176:51: warning: suggest braces around initialization of subobject
    VertexOut _e46 = VertexOut {_e42, _e43, _e44, _e45};
                                                  ^~~~
                                                  {   }
"

The code I'm using is:

#include shaders.util.globals
#include shaders.util.light

[[block]]
struct Locals {
    m: mat4x4<f32>;
};
[[group(1), binding(0)]] var<uniform> locals: Locals;

struct VertexIn {
	[[location(0)]] pos: vec3<f32>;
	[[location(1)]] norm: vec3<f32>;
	[[location(2)]] uv: vec2<f32>;
};

struct VertexOut {
	[[builtin(position)]] pos: vec4<f32>;
	[[location(0)]] norm: vec3<f32>;
	[[location(1)]] uv: vec2<f32>;
	[[location(2)]] wpos: vec3<f32>;
};

[[stage(vertex)]]
fn vs_main(vert: VertexIn) -> VertexOut {
	let wpos = locals.m * vec4<f32>(vert.pos, 1.0);
	return VertexOut(
		globals.vp * wpos,
		vert.norm,
		vert.uv,
		wpos.xyz,
	);
}

[[group(1), binding(1)]] var color_texture: texture_2d<f32>;
[[group(1), binding(2)]] var color_sampler: sampler;

[[stage(fragment)]]
fn fs_main(frag: VertexOut) -> [[location(0)]] vec4<f32> {
	let albedo = textureSample(color_texture, color_sampler, frag.uv).rgb;
	return vec4<f32>(illuminate(frag.wpos, normalize(frag.norm), albedo, 0.5, 1.0), 1.0);
}

(Ignore the #include pragmas, they're gone by the time the code reaches Naga)

This issue can be 'fixed' by making all but the last field of VertexOut a similar size, like so:

struct VertexOut {
	[[builtin(position)]] pos: vec4<f32>;
	[[location(0)]] norm: vec4<f32>;
	[[location(1)]] uv: vec4<f32>;
	[[location(2)]] wpos: vec3<f32>;
};
@kvark kvark added area: back-end Outputs of shader conversion kind: bug Something isn't working lang: Metal Metal Shading Language labels Jul 12, 2021
@teoxoy
Copy link
Member

teoxoy commented Apr 5, 2022

This has been fixed by #1215

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: back-end Outputs of shader conversion kind: bug Something isn't working lang: Metal Metal Shading Language
Projects
None yet
Development

No branches or pull requests

4 participants