From 87748a2fe3668a5684fdc60da48a3e2973d5a711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Capucho?= Date: Thu, 3 Jun 2021 20:34:54 +0100 Subject: [PATCH] [glsl-in] Add integration tests to CI (#943) * [glsl-in] Add glsl snapshots folder * [glsl-in] Fix incorrect angle brackets parsing * [glsl-in] Temporarily remove wgsl snapshot output --- src/front/glsl/parser.rs | 4 +- tests/in/glsl/210-bevy-2d-shader.frag | 27 ++++++++++++ tests/in/glsl/210-bevy-2d-shader.vert | 27 ++++++++++++ tests/in/glsl/210-bevy-shader.vert | 28 ++++++++++++ tests/in/glsl/277-casting.vert | 8 ++++ tests/in/glsl/280-matrix-cast.vert | 8 ++++ tests/in/glsl/484-preprocessor-if.vert | 10 +++++ tests/in/glsl/800-out-of-bounds-panic.vert | 26 +++++++++++ tests/in/glsl/896-push-constant.vert | 10 +++++ tests/in/glsl/931-constant-emitting.vert | 10 +++++ tests/in/glsl/932-for-loop-if.vert | 8 ++++ tests/out/210-bevy-2d-shader-frag.wgsl | 27 ++++++++++++ tests/out/210-bevy-2d-shader-vert.wgsl | 48 +++++++++++++++++++++ tests/out/800-out-of-bounds-panic-vert.wgsl | 36 ++++++++++++++++ tests/out/931-constant-emitting-vert.wgsl | 4 ++ tests/snapshots.rs | 47 ++++++++++++++++++++ 16 files changed, 326 insertions(+), 2 deletions(-) create mode 100644 tests/in/glsl/210-bevy-2d-shader.frag create mode 100644 tests/in/glsl/210-bevy-2d-shader.vert create mode 100644 tests/in/glsl/210-bevy-shader.vert create mode 100644 tests/in/glsl/277-casting.vert create mode 100644 tests/in/glsl/280-matrix-cast.vert create mode 100644 tests/in/glsl/484-preprocessor-if.vert create mode 100644 tests/in/glsl/800-out-of-bounds-panic.vert create mode 100644 tests/in/glsl/896-push-constant.vert create mode 100644 tests/in/glsl/931-constant-emitting.vert create mode 100644 tests/in/glsl/932-for-loop-if.vert create mode 100644 tests/out/210-bevy-2d-shader-frag.wgsl create mode 100644 tests/out/210-bevy-2d-shader-vert.wgsl create mode 100644 tests/out/800-out-of-bounds-panic-vert.wgsl create mode 100644 tests/out/931-constant-emitting-vert.wgsl diff --git a/src/front/glsl/parser.rs b/src/front/glsl/parser.rs index d97333dabd..cf9eaf16bc 100644 --- a/src/front/glsl/parser.rs +++ b/src/front/glsl/parser.rs @@ -1075,8 +1075,8 @@ impl<'source, 'program, 'options> Parser<'source, 'program, 'options> { TokenValue::NotEqual => BinaryOperator::NotEqual, TokenValue::GreaterEqual => BinaryOperator::GreaterEqual, TokenValue::LessEqual => BinaryOperator::LessEqual, - TokenValue::LeftAngle => BinaryOperator::Greater, - TokenValue::RightAngle => BinaryOperator::Less, + TokenValue::LeftAngle => BinaryOperator::Less, + TokenValue::RightAngle => BinaryOperator::Greater, TokenValue::LeftShift => BinaryOperator::ShiftLeft, TokenValue::RightShift => BinaryOperator::ShiftRight, TokenValue::Plus => BinaryOperator::Add, diff --git a/tests/in/glsl/210-bevy-2d-shader.frag b/tests/in/glsl/210-bevy-2d-shader.frag new file mode 100644 index 0000000000..93119ef259 --- /dev/null +++ b/tests/in/glsl/210-bevy-2d-shader.frag @@ -0,0 +1,27 @@ +// AUTHOR: mrk-its +// ISSUE: #210 +// FIX: #898 +#version 450 + +layout(location = 0) in vec2 v_Uv; + +layout(location = 0) out vec4 o_Target; + +layout(set = 1, binding = 0) uniform ColorMaterial_color { + vec4 Color; +}; + +# ifdef COLORMATERIAL_TEXTURE +layout(set = 1, binding = 1) uniform texture2D ColorMaterial_texture; +layout(set = 1, binding = 2) uniform sampler ColorMaterial_texture_sampler; +# endif + +void main() { + vec4 color = Color; +# ifdef COLORMATERIAL_TEXTURE + color *= texture( + sampler2D(ColorMaterial_texture, ColorMaterial_texture_sampler), + v_Uv); +# endif + o_Target = color; +} diff --git a/tests/in/glsl/210-bevy-2d-shader.vert b/tests/in/glsl/210-bevy-2d-shader.vert new file mode 100644 index 0000000000..1d99e1b177 --- /dev/null +++ b/tests/in/glsl/210-bevy-2d-shader.vert @@ -0,0 +1,27 @@ +// AUTHOR: mrk-its +// ISSUE: #210 +// FIX: #898 +#version 450 + +layout(location = 0) in vec3 Vertex_Position; +layout(location = 1) in vec3 Vertex_Normal; +layout(location = 2) in vec2 Vertex_Uv; + +layout(location = 0) out vec2 v_Uv; + +layout(set = 0, binding = 0) uniform Camera { + mat4 ViewProj; +}; + +layout(set = 2, binding = 0) uniform Transform { + mat4 Model; +}; +layout(set = 2, binding = 1) uniform Sprite_size { + vec2 size; +}; + +void main() { + v_Uv = Vertex_Uv; + vec3 position = Vertex_Position * vec3(size, 1.0); + gl_Position = ViewProj * Model * vec4(position, 1.0); +} diff --git a/tests/in/glsl/210-bevy-shader.vert b/tests/in/glsl/210-bevy-shader.vert new file mode 100644 index 0000000000..64cdca2ce2 --- /dev/null +++ b/tests/in/glsl/210-bevy-shader.vert @@ -0,0 +1,28 @@ +// AUTHOR: enfipy +// ISSUE: #210 +// FIX: #898 +#version 450 + +layout(location = 0) in vec3 Vertex_Position; +layout(location = 1) in vec3 Vertex_Normal; +layout(location = 2) in vec2 Vertex_Uv; + +layout(location = 0) out vec3 v_Position; +layout(location = 1) out vec3 v_Normal; +layout(location = 2) out vec2 v_Uv; + +layout(set = 0, binding = 0) uniform Camera { + mat4 ViewProj; +}; + +layout(set = 2, binding = 0) uniform Transform { + mat4 Model; +}; + +void main() { + v_Normal = (Model * vec4(Vertex_Normal, 1.0)).xyz; + v_Normal = mat3(Model) * Vertex_Normal; + v_Position = (Model * vec4(Vertex_Position, 1.0)).xyz; + v_Uv = Vertex_Uv; + gl_Position = ViewProj * vec4(v_Position, 1.0); +} diff --git a/tests/in/glsl/277-casting.vert b/tests/in/glsl/277-casting.vert new file mode 100644 index 0000000000..939be006b7 --- /dev/null +++ b/tests/in/glsl/277-casting.vert @@ -0,0 +1,8 @@ +// AUTHOR: Napokue +// ISSUE: #277 +// FIX: #278 +#version 450 + +void main() { + float a = float(1); +} diff --git a/tests/in/glsl/280-matrix-cast.vert b/tests/in/glsl/280-matrix-cast.vert new file mode 100644 index 0000000000..560a442608 --- /dev/null +++ b/tests/in/glsl/280-matrix-cast.vert @@ -0,0 +1,8 @@ +// AUTHOR: pjoe +// ISSUE: #280 +// FIX: #898 +#version 450 + +void main() { + mat4 a = mat4(1); +} diff --git a/tests/in/glsl/484-preprocessor-if.vert b/tests/in/glsl/484-preprocessor-if.vert new file mode 100644 index 0000000000..d2c92030e8 --- /dev/null +++ b/tests/in/glsl/484-preprocessor-if.vert @@ -0,0 +1,10 @@ +// AUTHOR: fintelia +// ISSUE: #484 +// FIX: https://github.com/Kangz/glslpp-rs/pull/30 +// NOTE: Shader altered to use correct syntax +#version 450 core + +#if 0 +#endif + +void main() { } diff --git a/tests/in/glsl/800-out-of-bounds-panic.vert b/tests/in/glsl/800-out-of-bounds-panic.vert new file mode 100644 index 0000000000..babca1668f --- /dev/null +++ b/tests/in/glsl/800-out-of-bounds-panic.vert @@ -0,0 +1,26 @@ +// AUTHOR: Herschel +// ISSUE: #800 +// FIX: #901 +#version 450 + +// Set 0: globals +layout(set = 0, binding = 0) uniform Globals { + mat4 view_matrix; +}; + +// Push constants: matrix + color +layout(push_constant) uniform VertexPushConstants { + mat4 world_matrix; +}; + +layout(location = 0) in vec2 position; +layout(location = 1) in vec4 color; + +layout(location = 0) out vec4 frag_color; + +void main() { + frag_color = color; + gl_Position = view_matrix * world_matrix * vec4(position, 0.0, 1.0); + // TODO: https://github.com/gfx-rs/naga/issues/901 + // gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0; +} diff --git a/tests/in/glsl/896-push-constant.vert b/tests/in/glsl/896-push-constant.vert new file mode 100644 index 0000000000..59ea1e4c4e --- /dev/null +++ b/tests/in/glsl/896-push-constant.vert @@ -0,0 +1,10 @@ +// AUTHOR: Foltik +// ISSUE: #896 +// FIX: #897 +#version 450 + +layout(push_constant) uniform PushConstants { + float example; +} c; + +void main() {} diff --git a/tests/in/glsl/931-constant-emitting.vert b/tests/in/glsl/931-constant-emitting.vert new file mode 100644 index 0000000000..b780eb7ac9 --- /dev/null +++ b/tests/in/glsl/931-constant-emitting.vert @@ -0,0 +1,10 @@ +// AUTHOR: jakobhellermann +// ISSUE: #931 +// FIX: #933 +#version 450 + +const int constant = 10; + +float function() { + return 0.0; +} diff --git a/tests/in/glsl/932-for-loop-if.vert b/tests/in/glsl/932-for-loop-if.vert new file mode 100644 index 0000000000..07dbdd751c --- /dev/null +++ b/tests/in/glsl/932-for-loop-if.vert @@ -0,0 +1,8 @@ +// AUTHOR: jakobhellermann +// ISSUE: #932 +// FIX: #935 +#version 450 + +void main() { + for (int i = 0; i < 1; i += 1) {} +} diff --git a/tests/out/210-bevy-2d-shader-frag.wgsl b/tests/out/210-bevy-2d-shader-frag.wgsl new file mode 100644 index 0000000000..2185714c4d --- /dev/null +++ b/tests/out/210-bevy-2d-shader-frag.wgsl @@ -0,0 +1,27 @@ +[[block]] +struct ColorMaterial_color { + Color: vec4; +}; + +struct FragmentOutput { + [[location(0), interpolate(perspective)]] member: vec4; +}; + +var v_Uv: vec2; +var o_Target: vec4; +[[group(1), binding(0)]] +var global: ColorMaterial_color; + +fn main() { + var color: vec4; + + color = global.Color; + o_Target = color; + return; +} + +[[stage(fragment)]] +fn main1() -> FragmentOutput { + main(); + return FragmentOutput(o_Target); +} diff --git a/tests/out/210-bevy-2d-shader-vert.wgsl b/tests/out/210-bevy-2d-shader-vert.wgsl new file mode 100644 index 0000000000..ce2e4cf1ca --- /dev/null +++ b/tests/out/210-bevy-2d-shader-vert.wgsl @@ -0,0 +1,48 @@ +[[block]] +struct Camera { + ViewProj: mat4x4; +}; + +[[block]] +struct Transform { + Model: mat4x4; +}; + +[[block]] +struct Sprite_size { + size: vec2; +}; + +struct VertexOutput { + [[location(0), interpolate(perspective)]] member: vec2; + [[builtin(position)]] member1: vec4; +}; + +var Vertex_Position: vec3; +var Vertex_Normal: vec3; +var Vertex_Uv: vec2; +var v_Uv: vec2; +[[group(0), binding(0)]] +var global: Camera; +[[group(2), binding(0)]] +var global1: Transform; +[[group(2), binding(1)]] +var global2: Sprite_size; +var gl_Position: vec4; + +fn main() { + var position: vec3; + + v_Uv = Vertex_Uv; + position = (Vertex_Position * vec3(global2.size, 1.0)); + gl_Position = ((global.ViewProj * global1.Model) * vec4(position, 1.0)); + return; +} + +[[stage(vertex)]] +fn main1([[location(0), interpolate(perspective)]] param: vec3, [[location(2), interpolate(perspective)]] param1: vec2) -> VertexOutput { + Vertex_Position = param; + Vertex_Uv = param1; + main(); + return VertexOutput(v_Uv, gl_Position); +} diff --git a/tests/out/800-out-of-bounds-panic-vert.wgsl b/tests/out/800-out-of-bounds-panic-vert.wgsl new file mode 100644 index 0000000000..175862c522 --- /dev/null +++ b/tests/out/800-out-of-bounds-panic-vert.wgsl @@ -0,0 +1,36 @@ +[[block]] +struct Globals { + view_matrix: mat4x4; +}; + +[[block]] +struct VertexPushConstants { + world_matrix: mat4x4; +}; + +struct VertexOutput { + [[location(0), interpolate(perspective)]] member: vec4; + [[builtin(position)]] member1: vec4; +}; + +[[group(0), binding(0)]] +var global: Globals; +var global1: VertexPushConstants; +var position: vec2; +var color: vec4; +var frag_color: vec4; +var gl_Position: vec4; + +fn main() { + frag_color = color; + gl_Position = ((global.view_matrix * global1.world_matrix) * vec4(position, 0.0, 1.0)); + return; +} + +[[stage(vertex)]] +fn main1([[location(0), interpolate(perspective)]] param: vec2, [[location(1), interpolate(perspective)]] param1: vec4) -> VertexOutput { + position = param; + color = param1; + main(); + return VertexOutput(frag_color, gl_Position); +} diff --git a/tests/out/931-constant-emitting-vert.wgsl b/tests/out/931-constant-emitting-vert.wgsl new file mode 100644 index 0000000000..43ccc88002 --- /dev/null +++ b/tests/out/931-constant-emitting-vert.wgsl @@ -0,0 +1,4 @@ +fn function() -> f32 { + return 0.0; +} + diff --git a/tests/snapshots.rs b/tests/snapshots.rs index 1df7c8bc54..25c6887a6e 100644 --- a/tests/snapshots.rs +++ b/tests/snapshots.rs @@ -368,6 +368,53 @@ fn convert_glsl( check_targets(&module, name, targets); } +#[cfg(feature = "glsl-in")] +#[allow(unused_variables)] +#[test] +fn convert_glsl_folder() { + let root = env!("CARGO_MANIFEST_DIR"); + + for entry in std::fs::read_dir(format!("{}/{}/glsl", root, DIR_IN)).unwrap() { + let entry = entry.unwrap(); + let file_name = entry.file_name().into_string().unwrap(); + + println!("Processing {}", file_name); + + let mut entry_points = naga::FastHashMap::default(); + let stage = match entry.path().extension().and_then(|s| s.to_str()).unwrap() { + "vert" => naga::ShaderStage::Vertex, + "frag" => naga::ShaderStage::Fragment, + "comp" => naga::ShaderStage::Compute, + ext => panic!("Unknown extension for glsl file {}", ext), + }; + entry_points.insert("main".to_string(), stage); + + let module = naga::front::glsl::parse_str( + &fs::read_to_string(entry.path()).expect("Couldn't find glsl file"), + &naga::front::glsl::Options { + entry_points, + defines: Default::default(), + }, + ) + .unwrap(); + + let info = naga::valid::Validator::new( + naga::valid::ValidationFlags::all(), + naga::valid::Capabilities::all(), + ) + .validate(&module) + .unwrap(); + + let dest = PathBuf::from(root) + .join(DIR_OUT) + .join(&file_name.replace(".", "-")); + + // FIXME: https://github.com/gfx-rs/naga/issues/945 + // #[cfg(feature = "wgsl-out")] + // check_output_wgsl(&module, &info, &dest); + } +} + #[cfg(feature = "glsl-in")] #[test] fn convert_glsl_quad() {