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

Accept only vec3 (not vecN) for the cross built-in #6171

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ By @teoxoy [#6134](https://github.com/gfx-rs/wgpu/pull/6134).

- Fix incorrect hlsl image output type conversion. By @atlv24 in [#6123](https://github.com/gfx-rs/wgpu/pull/6123)

#### Naga

- Accept only `vec3` (not `vecN`) for the `cross` built-in. By @ErichDonGubler in [#6171](https://github.com/gfx-rs/wgpu/pull/6171).

#### General

- If GL context creation fails retry with GLES. By @Rapdorian in [#5996](https://github.com/gfx-rs/wgpu/pull/5996)
Expand Down
25 changes: 24 additions & 1 deletion naga/src/valid/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ impl super::Validator {
));
}
}
Mf::Outer | Mf::Cross | Mf::Reflect => {
Mf::Outer | Mf::Reflect => {
let arg1_ty = match (arg1_ty, arg2_ty, arg3_ty) {
(Some(ty1), None, None) => ty1,
_ => return Err(ExpressionError::WrongArgumentCount(fun)),
Expand All @@ -1184,6 +1184,29 @@ impl super::Validator {
));
}
}
Mf::Cross => {
let arg1_ty = match (arg1_ty, arg2_ty, arg3_ty) {
(Some(ty1), None, None) => ty1,
_ => return Err(ExpressionError::WrongArgumentCount(fun)),
};
match *arg_ty {
Ti::Vector {
scalar:
Sc {
kind: Sk::Float, ..
},
size: crate::VectorSize::Tri,
} => {}
_ => return Err(ExpressionError::InvalidArgumentType(fun, 0, arg)),
}
if arg1_ty != arg_ty {
return Err(ExpressionError::InvalidArgumentType(
fun,
1,
arg1.unwrap(),
));
}
}
Mf::Refract => {
let (arg1_ty, arg2_ty) = match (arg1_ty, arg2_ty, arg3_ty) {
(Some(ty1), Some(ty2), None) => (ty1, ty2),
Expand Down
3 changes: 3 additions & 0 deletions naga/tests/in/cross.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@compute @workgroup_size(1) fn main() {
let a = cross(vec3(0., 1., 2.), vec3(0., 1., 2.));
}
12 changes: 12 additions & 0 deletions naga/tests/out/glsl/cross.main.Compute.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 310 es

precision highp float;
precision highp int;

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;


void main() {
vec3 a = cross(vec3(0.0, 1.0, 2.0), vec3(0.0, 1.0, 2.0));
}

5 changes: 5 additions & 0 deletions naga/tests/out/hlsl/cross.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[numthreads(1, 1, 1)]
void main()
{
float3 a = cross(float3(0.0, 1.0, 2.0), float3(0.0, 1.0, 2.0));
}
12 changes: 12 additions & 0 deletions naga/tests/out/hlsl/cross.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(
vertex:[
],
fragment:[
],
compute:[
(
entry_point:"main",
target_profile:"cs_5_1",
),
],
)
11 changes: 11 additions & 0 deletions naga/tests/out/msl/cross.msl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// language: metal1.0
#include <metal_stdlib>
#include <simd/simd.h>

using metal::uint;


kernel void main_(
) {
metal::float3 a = metal::cross(metal::float3(0.0, 1.0, 2.0), metal::float3(0.0, 1.0, 2.0));
}
24 changes: 24 additions & 0 deletions naga/tests/out/spv/cross.spvasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; SPIR-V
; Version: 1.1
; Generator: rspirv
; Bound: 14
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %6 "main"
OpExecutionMode %6 LocalSize 1 1 1
%2 = OpTypeVoid
%4 = OpTypeFloat 32
%3 = OpTypeVector %4 3
%7 = OpTypeFunction %2
%8 = OpConstant %4 0.0
%9 = OpConstant %4 1.0
%10 = OpConstant %4 2.0
%11 = OpConstantComposite %3 %8 %9 %10
%6 = OpFunction %2 None %7
%5 = OpLabel
OpBranch %12
%12 = OpLabel
%13 = OpExtInst %3 %1 Cross %11 %11
OpReturn
OpFunctionEnd
4 changes: 4 additions & 0 deletions naga/tests/out/wgsl/cross.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@compute @workgroup_size(1, 1, 1)
fn main() {
let a = cross(vec3<f32>(0f, 1f, 2f), vec3<f32>(0f, 1f, 2f));
}
4 changes: 4 additions & 0 deletions naga/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,10 @@ fn convert_wgsl() {
Targets::IR | Targets::SPIRV | Targets::METAL,
),
("vertex-pulling-transform", Targets::METAL),
(
"cross",
Targets::SPIRV | Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL,
),
];

for &(name, targets) in inputs.iter() {
Expand Down
51 changes: 51 additions & 0 deletions naga/tests/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,54 @@ fn emit_workgroup_uniform_load_result() {
variant(true).expect("module should validate");
assert!(variant(false).is_err());
}

#[cfg(feature = "wgsl-in")]
#[test]
fn bad_cross_builtin_args() {
let cases = [
(
"vec2(0., 1.)",
"\
error: Entry point main at Compute is invalid
┌─ wgsl:3:13
3 │ let a = cross(vec2(0., 1.), vec2(0., 1.));
│ ^^^^^ naga::Expression [6]
= Expression [6] is invalid
= Argument [0] to Cross as expression [2] has an invalid type.

",
),
(
"vec4(0., 1., 2., 3.)",
"\
error: Entry point main at Compute is invalid
┌─ wgsl:3:13
3 │ let a = cross(vec4(0., 1., 2., 3.), vec4(0., 1., 2., 3.));
│ ^^^^^ naga::Expression [10]
= Expression [10] is invalid
= Argument [0] to Cross as expression [4] has an invalid type.

",
),
];

for (invalid_arg, expected_err) in cases {
let source = format!(
"\
@compute @workgroup_size(1)
fn main() {{
let a = cross({invalid_arg}, {invalid_arg});
}}
"
);
let module = naga::front::wgsl::parse_str(&source).unwrap();
let err = valid::Validator::new(Default::default(), valid::Capabilities::all())
.validate_no_overrides(&module)
.expect_err("module should be invalid");
assert_eq!(err.emit_to_string(&source), expected_err);
}
}
Loading