| // Copyright 2021 The Dawn & Tint Authors |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are met: |
| // |
| // 1. Redistributions of source code must retain the above copyright notice, this |
| // list of conditions and the following disclaimer. |
| // |
| // 2. Redistributions in binary form must reproduce the above copyright notice, |
| // this list of conditions and the following disclaimer in the documentation |
| // and/or other materials provided with the distribution. |
| // |
| // 3. Neither the name of the copyright holder nor the names of its |
| // contributors may be used to endorse or promote products derived from |
| // this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| #include "src/tint/lang/core/builtin_value.h" |
| #include "src/tint/lang/wgsl/ast/call_statement.h" |
| #include "src/tint/lang/wgsl/resolver/resolver_helper_test.h" |
| #include "src/tint/utils/text/string_stream.h" |
| |
| namespace tint::resolver { |
| namespace { |
| |
| using namespace tint::core::fluent_types; // NOLINT |
| using namespace tint::core::number_suffixes; // NOLINT |
| |
| template <typename T> |
| using DataType = builder::DataType<T>; |
| class ResolverBuiltinsValidationTest : public resolver::TestHelper, public testing::Test {}; |
| namespace StageTest { |
| struct Params { |
| builder::ast_type_func_ptr type; |
| core::BuiltinValue builtin; |
| ast::PipelineStage stage; |
| bool is_valid; |
| }; |
| |
| template <typename T> |
| constexpr Params ParamsFor(core::BuiltinValue builtin, ast::PipelineStage stage, bool is_valid) { |
| return Params{DataType<T>::AST, builtin, stage, is_valid}; |
| } |
| static constexpr Params cases[] = { |
| ParamsFor<vec4<f32>>(core::BuiltinValue::kPosition, ast::PipelineStage::kVertex, false), |
| ParamsFor<vec4<f32>>(core::BuiltinValue::kPosition, ast::PipelineStage::kFragment, true), |
| ParamsFor<vec4<f32>>(core::BuiltinValue::kPosition, ast::PipelineStage::kCompute, false), |
| |
| ParamsFor<u32>(core::BuiltinValue::kVertexIndex, ast::PipelineStage::kVertex, true), |
| ParamsFor<u32>(core::BuiltinValue::kVertexIndex, ast::PipelineStage::kFragment, false), |
| ParamsFor<u32>(core::BuiltinValue::kVertexIndex, ast::PipelineStage::kCompute, false), |
| |
| ParamsFor<u32>(core::BuiltinValue::kInstanceIndex, ast::PipelineStage::kVertex, true), |
| ParamsFor<u32>(core::BuiltinValue::kInstanceIndex, ast::PipelineStage::kFragment, false), |
| ParamsFor<u32>(core::BuiltinValue::kInstanceIndex, ast::PipelineStage::kCompute, false), |
| |
| ParamsFor<bool>(core::BuiltinValue::kFrontFacing, ast::PipelineStage::kVertex, false), |
| ParamsFor<bool>(core::BuiltinValue::kFrontFacing, ast::PipelineStage::kFragment, true), |
| ParamsFor<bool>(core::BuiltinValue::kFrontFacing, ast::PipelineStage::kCompute, false), |
| |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kLocalInvocationId, |
| ast::PipelineStage::kVertex, |
| false), |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kLocalInvocationId, |
| ast::PipelineStage::kFragment, |
| false), |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kLocalInvocationId, |
| ast::PipelineStage::kCompute, |
| true), |
| |
| ParamsFor<u32>(core::BuiltinValue::kLocalInvocationIndex, ast::PipelineStage::kVertex, false), |
| ParamsFor<u32>(core::BuiltinValue::kLocalInvocationIndex, ast::PipelineStage::kFragment, false), |
| ParamsFor<u32>(core::BuiltinValue::kLocalInvocationIndex, ast::PipelineStage::kCompute, true), |
| |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kGlobalInvocationId, |
| ast::PipelineStage::kVertex, |
| false), |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kGlobalInvocationId, |
| ast::PipelineStage::kFragment, |
| false), |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kGlobalInvocationId, |
| ast::PipelineStage::kCompute, |
| true), |
| |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kWorkgroupId, ast::PipelineStage::kVertex, false), |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kWorkgroupId, ast::PipelineStage::kFragment, false), |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kWorkgroupId, ast::PipelineStage::kCompute, true), |
| |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kNumWorkgroups, ast::PipelineStage::kVertex, false), |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kNumWorkgroups, ast::PipelineStage::kFragment, false), |
| ParamsFor<vec3<u32>>(core::BuiltinValue::kNumWorkgroups, ast::PipelineStage::kCompute, true), |
| |
| ParamsFor<u32>(core::BuiltinValue::kSampleIndex, ast::PipelineStage::kVertex, false), |
| ParamsFor<u32>(core::BuiltinValue::kSampleIndex, ast::PipelineStage::kFragment, true), |
| ParamsFor<u32>(core::BuiltinValue::kSampleIndex, ast::PipelineStage::kCompute, false), |
| |
| ParamsFor<u32>(core::BuiltinValue::kSampleMask, ast::PipelineStage::kVertex, false), |
| ParamsFor<u32>(core::BuiltinValue::kSampleMask, ast::PipelineStage::kFragment, true), |
| ParamsFor<u32>(core::BuiltinValue::kSampleMask, ast::PipelineStage::kCompute, false), |
| }; |
| |
| using ResolverBuiltinsStageTest = ResolverTestWithParam<Params>; |
| TEST_P(ResolverBuiltinsStageTest, All_input) { |
| const Params& params = GetParam(); |
| |
| auto* p = GlobalVar("p", ty.vec4<f32>(), core::AddressSpace::kPrivate); |
| auto* input = |
| Param("input", params.type(*this), Vector{Builtin(Source{{12, 34}}, params.builtin)}); |
| switch (params.stage) { |
| case ast::PipelineStage::kVertex: |
| Func("main", Vector{input}, ty.vec4<f32>(), Vector{Return(p)}, |
| Vector{Stage(ast::PipelineStage::kVertex)}, |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kPosition), |
| }); |
| break; |
| case ast::PipelineStage::kFragment: |
| Func("main", Vector{input}, ty.void_(), tint::Empty, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| {}); |
| break; |
| case ast::PipelineStage::kCompute: |
| Func("main", Vector{input}, ty.void_(), tint::Empty, |
| Vector{ |
| Stage(ast::PipelineStage::kCompute), |
| WorkgroupSize(1_i), |
| }); |
| break; |
| default: |
| break; |
| } |
| |
| if (params.is_valid) { |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } else { |
| StringStream err; |
| err << "12:34 error: @builtin(" << params.builtin << ")"; |
| err << " cannot be used in input of " << params.stage << " pipeline stage"; |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), err.str()); |
| } |
| } |
| INSTANTIATE_TEST_SUITE_P(ResolverBuiltinsValidationTest, |
| ResolverBuiltinsStageTest, |
| testing::ValuesIn(cases)); |
| |
| TEST_F(ResolverBuiltinsValidationTest, FragDepthIsInput_Fail) { |
| // @fragment |
| // fn fs_main( |
| // @builtin(frag_depth) fd: f32, |
| // ) -> @location(0) f32 { return 1.0; } |
| Func("fs_main", |
| Vector{ |
| Param("fd", ty.f32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kFragDepth), |
| }), |
| }, |
| ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ( |
| r()->error(), |
| "12:34 error: @builtin(frag_depth) cannot be used in input of fragment pipeline stage"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, FragDepthIsInputStruct_Fail) { |
| // struct MyInputs { |
| // @builtin(frag_depth) ff: f32; |
| // }; |
| // @fragment |
| // fn fragShader(arg: MyInputs) -> @location(0) f32 { return 1.0; } |
| |
| auto* s = Structure("MyInputs", |
| Vector{ |
| Member("frag_depth", ty.f32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kFragDepth), |
| }), |
| }); |
| |
| Func("fragShader", |
| Vector{ |
| Param("arg", ty.Of(s)), |
| }, |
| ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: @builtin(frag_depth) cannot be used in input of " |
| "fragment pipeline stage\n" |
| "note: while analyzing entry point 'fragShader'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, StructBuiltinInsideEntryPoint_Ignored) { |
| // struct S { |
| // @builtin(vertex_index) idx: u32; |
| // }; |
| // @fragment |
| // fn fragShader() { var s : S; } |
| |
| Structure("S", Vector{ |
| Member("idx", ty.u32(), |
| Vector{ |
| Builtin(core::BuiltinValue::kVertexIndex), |
| }), |
| }); |
| |
| Func("fragShader", tint::Empty, ty.void_(), Vector{Decl(Var("s", ty("S")))}, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| EXPECT_TRUE(r()->Resolve()); |
| } |
| |
| } // namespace StageTest |
| |
| TEST_F(ResolverBuiltinsValidationTest, PositionNotF32_Struct_Fail) { |
| // struct MyInputs { |
| // @builtin(kPosition) p: vec4<u32>; |
| // }; |
| // @fragment |
| // fn fragShader(is_front: MyInputs) -> @location(0) f32 { return 1.0; } |
| |
| auto* s = Structure("MyInputs", |
| Vector{ |
| Member("position", ty.vec4<u32>(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kPosition), |
| }), |
| }); |
| Func("fragShader", |
| Vector{ |
| Param("arg", ty.Of(s)), |
| }, |
| ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(position) must be 'vec4<f32>'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, PositionNotF32_ReturnType_Fail) { |
| // @vertex |
| // fn main() -> @builtin(position) f32 { return 1.0; } |
| Func("main", tint::Empty, ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{Stage(ast::PipelineStage::kVertex)}, |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kPosition), |
| }); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(position) must be 'vec4<f32>'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, FragDepthNotF32_Struct_Fail) { |
| // struct MyInputs { |
| // @builtin(kFragDepth) p: i32; |
| // }; |
| // @fragment |
| // fn fragShader(is_front: MyInputs) -> @location(0) f32 { return 1.0; } |
| |
| auto* s = Structure("MyInputs", |
| Vector{ |
| Member("frag_depth", ty.i32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kFragDepth), |
| }), |
| }); |
| Func("fragShader", Vector{Param("arg", ty.Of(s))}, ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(frag_depth) must be 'f32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, SampleMaskNotU32_Struct_Fail) { |
| // struct MyInputs { |
| // @builtin(sample_mask) m: f32; |
| // }; |
| // @fragment |
| // fn fragShader(is_front: MyInputs) -> @location(0) f32 { return 1.0; } |
| |
| auto* s = Structure("MyInputs", |
| Vector{ |
| Member("m", ty.f32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kSampleMask), |
| }), |
| }); |
| Func("fragShader", Vector{Param("arg", ty.Of(s))}, ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(sample_mask) must be 'u32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, SampleMaskNotU32_ReturnType_Fail) { |
| // @fragment |
| // fn main() -> @builtin(sample_mask) i32 { return 1; } |
| Func("main", tint::Empty, ty.i32(), Vector{Return(1_i)}, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kSampleMask), |
| }); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(sample_mask) must be 'u32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, SampleMaskIsNotU32_Fail) { |
| // @fragment |
| // fn fs_main( |
| // @builtin(sample_mask) arg: bool |
| // ) -> @location(0) f32 { return 1.0; } |
| Func("fs_main", |
| Vector{ |
| Param("arg", ty.bool_(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kSampleMask), |
| }), |
| }, |
| ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(sample_mask) must be 'u32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, SampleIndexIsNotU32_Struct_Fail) { |
| // struct MyInputs { |
| // @builtin(sample_index) m: f32; |
| // }; |
| // @fragment |
| // fn fragShader(is_front: MyInputs) -> @location(0) f32 { return 1.0; } |
| |
| auto* s = Structure("MyInputs", |
| Vector{ |
| Member("m", ty.f32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kSampleIndex), |
| }), |
| }); |
| Func("fragShader", Vector{Param("arg", ty.Of(s))}, ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(sample_index) must be 'u32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, SampleIndexIsNotU32_Fail) { |
| // @fragment |
| // fn fs_main( |
| // @builtin(sample_index) arg: bool |
| // ) -> @location(0) f32 { return 1.0; } |
| Func("fs_main", |
| Vector{ |
| Param("arg", ty.bool_(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kSampleIndex), |
| }), |
| }, |
| ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(sample_index) must be 'u32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, PositionIsNotF32_Fail) { |
| // @fragment |
| // fn fs_main( |
| // @builtin(kPosition) p: vec3<f32>, |
| // ) -> @location(0) f32 { return 1.0; } |
| Func("fs_main", |
| Vector{ |
| Param("p", ty.vec3<f32>(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kPosition), |
| }), |
| }, |
| ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(position) must be 'vec4<f32>'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, FragDepthIsNotF32_Fail) { |
| // @fragment |
| // fn fs_main() -> @builtin(kFragDepth) f32 { var fd: i32; return fd; } |
| auto* fd = Var("fd", ty.i32()); |
| Func("fs_main", tint::Empty, ty.i32(), |
| Vector{ |
| Decl(fd), |
| Return(fd), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kFragDepth), |
| }); |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(frag_depth) must be 'f32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, VertexIndexIsNotU32_Fail) { |
| // @vertex |
| // fn main( |
| // @builtin(kVertexIndex) vi : f32, |
| // @builtin(kPosition) p :vec4<f32> |
| // ) -> @builtin(kPosition) vec4<f32> { return vec4<f32>(); } |
| auto* p = Param("p", ty.vec4<f32>(), |
| Vector{ |
| Builtin(core::BuiltinValue::kPosition), |
| }); |
| auto* vi = Param("vi", ty.f32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kVertexIndex), |
| }); |
| Func("main", Vector{vi, p}, ty.vec4<f32>(), Vector{Return(Expr("p"))}, |
| Vector{ |
| Stage(ast::PipelineStage::kVertex), |
| }, |
| Vector{ |
| Builtin(core::BuiltinValue::kPosition), |
| }); |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(vertex_index) must be 'u32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, InstanceIndexIsNotU32) { |
| // @vertex |
| // fn main( |
| // @builtin(kInstanceIndex) ii : f32, |
| // @builtin(kPosition) p :vec4<f32> |
| // ) -> @builtin(kPosition) vec4<f32> { return vec4<f32>(); } |
| auto* p = Param("p", ty.vec4<f32>(), |
| Vector{ |
| Builtin(core::BuiltinValue::kPosition), |
| }); |
| auto* ii = Param("ii", ty.f32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kInstanceIndex), |
| }); |
| Func("main", Vector{ii, p}, ty.vec4<f32>(), Vector{Return(Expr("p"))}, |
| Vector{ |
| Stage(ast::PipelineStage::kVertex), |
| }, |
| Vector{ |
| Builtin(core::BuiltinValue::kPosition), |
| }); |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(instance_index) must be 'u32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, FragmentBuiltin_Pass) { |
| // @fragment |
| // fn fs_main( |
| // @builtin(kPosition) p: vec4<f32>, |
| // @builtin(front_facing) ff: bool, |
| // @builtin(sample_index) si: u32, |
| // @builtin(sample_mask) sm : u32 |
| // ) -> @builtin(frag_depth) f32 { var fd: f32; return fd; } |
| auto* p = Param("p", ty.vec4<f32>(), |
| Vector{ |
| Builtin(core::BuiltinValue::kPosition), |
| }); |
| auto* ff = Param("ff", ty.bool_(), |
| Vector{ |
| Builtin(core::BuiltinValue::kFrontFacing), |
| }); |
| auto* si = Param("si", ty.u32(), |
| Vector{ |
| Builtin(core::BuiltinValue::kSampleIndex), |
| }); |
| auto* sm = Param("sm", ty.u32(), |
| Vector{ |
| Builtin(core::BuiltinValue::kSampleMask), |
| }); |
| auto* var_fd = Var("fd", ty.f32()); |
| Func("fs_main", Vector{p, ff, si, sm}, ty.f32(), |
| Vector{ |
| Decl(var_fd), |
| Return(var_fd), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Builtin(core::BuiltinValue::kFragDepth), |
| }); |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, VertexBuiltin_Pass) { |
| // @vertex |
| // fn main( |
| // @builtin(vertex_index) vi : u32, |
| // @builtin(instance_index) ii : u32, |
| // ) -> @builtin(position) vec4<f32> { var p :vec4<f32>; return p; } |
| auto* vi = Param("vi", ty.u32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kVertexIndex), |
| }); |
| |
| auto* ii = Param("ii", ty.u32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kInstanceIndex), |
| }); |
| auto* p = Var("p", ty.vec4<f32>()); |
| Func("main", Vector{vi, ii}, ty.vec4<f32>(), |
| Vector{ |
| Decl(p), |
| Return(p), |
| }, |
| Vector{Stage(ast::PipelineStage::kVertex)}, |
| Vector{ |
| Builtin(core::BuiltinValue::kPosition), |
| }); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, ComputeBuiltin_Pass) { |
| // @compute @workgroup_size(1) |
| // fn main( |
| // @builtin(local_invocationId) li_id: vec3<u32>, |
| // @builtin(local_invocationIndex) li_index: u32, |
| // @builtin(global_invocationId) gi: vec3<u32>, |
| // @builtin(workgroup_id) wi: vec3<u32>, |
| // @builtin(num_workgroups) nwgs: vec3<u32>, |
| // ) {} |
| |
| auto* li_id = Param("li_id", ty.vec3<u32>(), |
| Vector{ |
| Builtin(core::BuiltinValue::kLocalInvocationId), |
| }); |
| auto* li_index = Param("li_index", ty.u32(), |
| Vector{ |
| Builtin(core::BuiltinValue::kLocalInvocationIndex), |
| }); |
| auto* gi = Param("gi", ty.vec3<u32>(), |
| Vector{ |
| Builtin(core::BuiltinValue::kGlobalInvocationId), |
| }); |
| auto* wi = Param("wi", ty.vec3<u32>(), |
| Vector{ |
| Builtin(core::BuiltinValue::kWorkgroupId), |
| }); |
| auto* nwgs = Param("nwgs", ty.vec3<u32>(), |
| Vector{ |
| Builtin(core::BuiltinValue::kNumWorkgroups), |
| }); |
| |
| Func("main", Vector{li_id, li_index, gi, wi, nwgs}, ty.void_(), tint::Empty, |
| Vector{Stage(ast::PipelineStage::kCompute), |
| WorkgroupSize(Expr(Source{Source::Location{12, 34}}, 2_i))}); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, ComputeBuiltin_WorkGroupIdNotVec3U32) { |
| auto* wi = Param("wi", ty.f32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kWorkgroupId), |
| }); |
| Func("main", Vector{wi}, ty.void_(), tint::Empty, |
| Vector{Stage(ast::PipelineStage::kCompute), |
| WorkgroupSize(Expr(Source{Source::Location{12, 34}}, 2_i))}); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: store type of @builtin(workgroup_id) must be " |
| "'vec3<u32>'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, ComputeBuiltin_NumWorkgroupsNotVec3U32) { |
| auto* nwgs = Param("nwgs", ty.f32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kNumWorkgroups), |
| }); |
| Func("main", Vector{nwgs}, ty.void_(), tint::Empty, |
| Vector{Stage(ast::PipelineStage::kCompute), |
| WorkgroupSize(Expr(Source{Source::Location{12, 34}}, 2_i))}); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: store type of @builtin(num_workgroups) must be " |
| "'vec3<u32>'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, ComputeBuiltin_GlobalInvocationNotVec3U32) { |
| auto* gi = Param("gi", ty.vec3<i32>(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kGlobalInvocationId), |
| }); |
| Func("main", Vector{gi}, ty.void_(), tint::Empty, |
| Vector{Stage(ast::PipelineStage::kCompute), |
| WorkgroupSize(Expr(Source{Source::Location{12, 34}}, 2_i))}); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: store type of @builtin(global_invocation_id) must be " |
| "'vec3<u32>'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, ComputeBuiltin_LocalInvocationIndexNotU32) { |
| auto* li_index = Param("li_index", ty.vec3<u32>(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kLocalInvocationIndex), |
| }); |
| Func("main", Vector{li_index}, ty.void_(), tint::Empty, |
| Vector{Stage(ast::PipelineStage::kCompute), |
| WorkgroupSize(Expr(Source{Source::Location{12, 34}}, 2_i))}); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: store type of @builtin(local_invocation_index) must be " |
| "'u32'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, ComputeBuiltin_LocalInvocationNotVec3U32) { |
| auto* li_id = Param("li_id", ty.vec2<u32>(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kLocalInvocationId), |
| }); |
| Func("main", Vector{li_id}, ty.void_(), tint::Empty, |
| Vector{Stage(ast::PipelineStage::kCompute), |
| WorkgroupSize(Expr(Source{Source::Location{12, 34}}, 2_i))}); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: store type of @builtin(local_invocation_id) must be " |
| "'vec3<u32>'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, FragmentBuiltinStruct_Pass) { |
| // Struct MyInputs { |
| // @builtin(kPosition) p: vec4<f32>; |
| // @builtin(frag_depth) fd: f32; |
| // @builtin(sample_index) si: u32; |
| // @builtin(sample_mask) sm : u32;; |
| // }; |
| // @fragment |
| // fn fragShader(arg: MyInputs) -> @location(0) f32 { return 1.0; } |
| |
| auto* s = Structure("MyInputs", Vector{ |
| Member("position", ty.vec4<f32>(), |
| Vector{ |
| Builtin(core::BuiltinValue::kPosition), |
| }), |
| Member("front_facing", ty.bool_(), |
| Vector{ |
| Builtin(core::BuiltinValue::kFrontFacing), |
| }), |
| Member("sample_index", ty.u32(), |
| Vector{ |
| Builtin(core::BuiltinValue::kSampleIndex), |
| }), |
| Member("sample_mask", ty.u32(), |
| Vector{ |
| Builtin(core::BuiltinValue::kSampleMask), |
| }), |
| }); |
| Func("fragShader", Vector{Param("arg", ty.Of(s))}, ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, FrontFacingParamIsNotBool_Fail) { |
| // @fragment |
| // fn fs_main( |
| // @builtin(front_facing) is_front: i32; |
| // ) -> @location(0) f32 { return 1.0; } |
| |
| auto* is_front = Param("is_front", ty.i32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kFrontFacing), |
| }); |
| Func("fs_main", Vector{is_front}, ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(front_facing) must be 'bool'"); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, FrontFacingMemberIsNotBool_Fail) { |
| // struct MyInputs { |
| // @builtin(front_facing) pos: f32; |
| // }; |
| // @fragment |
| // fn fragShader(is_front: MyInputs) -> @location(0) f32 { return 1.0; } |
| |
| auto* s = Structure("MyInputs", |
| Vector{ |
| Member("pos", ty.f32(), |
| Vector{ |
| Builtin(Source{{12, 34}}, core::BuiltinValue::kFrontFacing), |
| }), |
| }); |
| Func("fragShader", Vector{Param("is_front", ty.Of(s))}, ty.f32(), |
| Vector{ |
| Return(1_f), |
| }, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }, |
| Vector{ |
| Location(0_a), |
| }); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "12:34 error: store type of @builtin(front_facing) must be 'bool'"); |
| } |
| |
| // TODO(crbug.com/tint/1846): This isn't a validation test, but this sits next to other @builtin |
| // tests. Clean this up. |
| TEST_F(ResolverBuiltinsValidationTest, StructMemberAttributeMapsToSemBuiltinEnum) { |
| // struct S { |
| // @builtin(front_facing) b : bool; |
| // }; |
| // @fragment |
| // fn f(s : S) {} |
| |
| auto* builtin = Builtin(core::BuiltinValue::kFrontFacing); |
| auto* s = Structure("S", Vector{ |
| Member("f", ty.bool_(), Vector{builtin}), |
| }); |
| Func("f", Vector{Param("b", ty.Of(s))}, ty.void_(), tint::Empty, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* builtin_expr = Sem().Get(builtin); |
| ASSERT_NE(builtin_expr, nullptr); |
| EXPECT_EQ(builtin_expr->Value(), core::BuiltinValue::kFrontFacing); |
| } |
| |
| // TODO(crbug.com/tint/1846): This isn't a validation test, but this sits next to other @builtin |
| // tests. Clean this up. |
| TEST_F(ResolverBuiltinsValidationTest, ParamAttributeMapsToSemBuiltinEnum) { |
| // @fragment |
| // fn f(@builtin(front_facing) b : bool) {} |
| |
| auto* builtin = Builtin(core::BuiltinValue::kFrontFacing); |
| Func("f", Vector{Param("b", ty.bool_(), Vector{builtin})}, ty.void_(), tint::Empty, |
| Vector{ |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* builtin_expr = Sem().Get(builtin); |
| ASSERT_NE(builtin_expr, nullptr); |
| EXPECT_EQ(builtin_expr->Value(), core::BuiltinValue::kFrontFacing); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Length_Float_Scalar) { |
| auto* builtin = Call("length", 1_f); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Length_Float_Vec2) { |
| auto* builtin = Call("length", Call<vec2<f32>>(1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Length_Float_Vec3) { |
| auto* builtin = Call("length", Call<vec3<f32>>(1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Length_Float_Vec4) { |
| auto* builtin = Call("length", Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Distance_Float_Scalar) { |
| auto* builtin = Call("distance", 1_f, 1_f); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Distance_Float_Vec2) { |
| auto* builtin = Call("distance", Call<vec2<f32>>(1_f, 1_f), Call<vec2<f32>>(1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Distance_Float_Vec3) { |
| auto* builtin = |
| Call("distance", Call<vec3<f32>>(1_f, 1_f, 1_f), Call<vec3<f32>>(1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Distance_Float_Vec4) { |
| auto* builtin = |
| Call("distance", Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f), Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Determinant_Mat2x2) { |
| auto* builtin = Call("determinant", |
| Call<mat2x2<f32>>(Call<vec2<f32>>(1_f, 1_f), Call<vec2<f32>>(1_f, 1_f))); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Determinant_Mat3x3) { |
| auto* builtin = Call("determinant", Call<mat3x3<f32>>(Call<vec3<f32>>(1_f, 1_f, 1_f), |
| Call<vec3<f32>>(1_f, 1_f, 1_f), |
| Call<vec3<f32>>(1_f, 1_f, 1_f))); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Determinant_Mat4x4) { |
| auto* builtin = Call("determinant", Call<mat4x4<f32>>(Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f), |
| Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f), |
| Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f), |
| Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f))); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Frexp_Scalar) { |
| auto* builtin = Call("frexp", 1_f); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* res_ty = TypeOf(builtin)->As<core::type::Struct>(); |
| ASSERT_TRUE(res_ty != nullptr); |
| auto members = res_ty->Members(); |
| ASSERT_EQ(members.Length(), 2u); |
| EXPECT_TRUE(members[0]->Type()->Is<core::type::F32>()); |
| EXPECT_TRUE(members[1]->Type()->Is<core::type::I32>()); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Frexp_Vec2) { |
| auto* builtin = Call("frexp", Call<vec2<f32>>(1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* res_ty = TypeOf(builtin)->As<core::type::Struct>(); |
| ASSERT_TRUE(res_ty != nullptr); |
| auto members = res_ty->Members(); |
| ASSERT_EQ(members.Length(), 2u); |
| ASSERT_TRUE(members[0]->Type()->Is<core::type::Vector>()); |
| ASSERT_TRUE(members[1]->Type()->Is<core::type::Vector>()); |
| EXPECT_EQ(members[0]->Type()->As<core::type::Vector>()->Width(), 2u); |
| EXPECT_TRUE(members[0]->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>()); |
| EXPECT_EQ(members[1]->Type()->As<core::type::Vector>()->Width(), 2u); |
| EXPECT_TRUE(members[1]->Type()->As<core::type::Vector>()->type()->Is<core::type::I32>()); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Frexp_Vec3) { |
| auto* builtin = Call("frexp", Call<vec3<f32>>(1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* res_ty = TypeOf(builtin)->As<core::type::Struct>(); |
| ASSERT_TRUE(res_ty != nullptr); |
| auto members = res_ty->Members(); |
| ASSERT_EQ(members.Length(), 2u); |
| ASSERT_TRUE(members[0]->Type()->Is<core::type::Vector>()); |
| ASSERT_TRUE(members[1]->Type()->Is<core::type::Vector>()); |
| EXPECT_EQ(members[0]->Type()->As<core::type::Vector>()->Width(), 3u); |
| EXPECT_TRUE(members[0]->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>()); |
| EXPECT_EQ(members[1]->Type()->As<core::type::Vector>()->Width(), 3u); |
| EXPECT_TRUE(members[1]->Type()->As<core::type::Vector>()->type()->Is<core::type::I32>()); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Frexp_Vec4) { |
| auto* builtin = Call("frexp", Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* res_ty = TypeOf(builtin)->As<core::type::Struct>(); |
| ASSERT_TRUE(res_ty != nullptr); |
| auto members = res_ty->Members(); |
| ASSERT_EQ(members.Length(), 2u); |
| ASSERT_TRUE(members[0]->Type()->Is<core::type::Vector>()); |
| ASSERT_TRUE(members[1]->Type()->Is<core::type::Vector>()); |
| EXPECT_EQ(members[0]->Type()->As<core::type::Vector>()->Width(), 4u); |
| EXPECT_TRUE(members[0]->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>()); |
| EXPECT_EQ(members[1]->Type()->As<core::type::Vector>()->Width(), 4u); |
| EXPECT_TRUE(members[1]->Type()->As<core::type::Vector>()->type()->Is<core::type::I32>()); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Modf_Scalar) { |
| auto* builtin = Call("modf", 1_f); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* res_ty = TypeOf(builtin)->As<core::type::Struct>(); |
| ASSERT_TRUE(res_ty != nullptr); |
| auto members = res_ty->Members(); |
| ASSERT_EQ(members.Length(), 2u); |
| EXPECT_TRUE(members[0]->Type()->Is<core::type::F32>()); |
| EXPECT_TRUE(members[1]->Type()->Is<core::type::F32>()); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Modf_Vec2) { |
| auto* builtin = Call("modf", Call<vec2<f32>>(1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* res_ty = TypeOf(builtin)->As<core::type::Struct>(); |
| ASSERT_TRUE(res_ty != nullptr); |
| auto members = res_ty->Members(); |
| ASSERT_EQ(members.Length(), 2u); |
| ASSERT_TRUE(members[0]->Type()->Is<core::type::Vector>()); |
| ASSERT_TRUE(members[1]->Type()->Is<core::type::Vector>()); |
| EXPECT_EQ(members[0]->Type()->As<core::type::Vector>()->Width(), 2u); |
| EXPECT_TRUE(members[0]->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>()); |
| EXPECT_EQ(members[1]->Type()->As<core::type::Vector>()->Width(), 2u); |
| EXPECT_TRUE(members[1]->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>()); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Modf_Vec3) { |
| auto* builtin = Call("modf", Call<vec3<f32>>(1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* res_ty = TypeOf(builtin)->As<core::type::Struct>(); |
| ASSERT_TRUE(res_ty != nullptr); |
| auto members = res_ty->Members(); |
| ASSERT_EQ(members.Length(), 2u); |
| ASSERT_TRUE(members[0]->Type()->Is<core::type::Vector>()); |
| ASSERT_TRUE(members[1]->Type()->Is<core::type::Vector>()); |
| EXPECT_EQ(members[0]->Type()->As<core::type::Vector>()->Width(), 3u); |
| EXPECT_TRUE(members[0]->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>()); |
| EXPECT_EQ(members[1]->Type()->As<core::type::Vector>()->Width(), 3u); |
| EXPECT_TRUE(members[1]->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>()); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Modf_Vec4) { |
| auto* builtin = Call("modf", Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| auto* res_ty = TypeOf(builtin)->As<core::type::Struct>(); |
| ASSERT_TRUE(res_ty != nullptr); |
| auto members = res_ty->Members(); |
| ASSERT_EQ(members.Length(), 2u); |
| ASSERT_TRUE(members[0]->Type()->Is<core::type::Vector>()); |
| ASSERT_TRUE(members[1]->Type()->Is<core::type::Vector>()); |
| EXPECT_EQ(members[0]->Type()->As<core::type::Vector>()->Width(), 4u); |
| EXPECT_TRUE(members[0]->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>()); |
| EXPECT_EQ(members[1]->Type()->As<core::type::Vector>()->Width(), 4u); |
| EXPECT_TRUE(members[1]->Type()->As<core::type::Vector>()->type()->Is<core::type::F32>()); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Cross_Float_Vec3) { |
| auto* builtin = Call("cross", Call<vec3<f32>>(1_f, 1_f, 1_f), Call<vec3<f32>>(1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Dot_Float_Vec2) { |
| auto* builtin = Call("dot", Call<vec2<f32>>(1_f, 1_f), Call<vec2<f32>>(1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Dot_Float_Vec3) { |
| auto* builtin = Call("dot", Call<vec3<f32>>(1_f, 1_f, 1_f), Call<vec3<f32>>(1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Dot_Float_Vec4) { |
| auto* builtin = |
| Call("dot", Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f), Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Select_Float_Scalar) { |
| auto* builtin = Call("select", Expr(1_f), Expr(1_f), Expr(true)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Select_Integer_Scalar) { |
| auto* builtin = Call("select", Expr(1_i), Expr(1_i), Expr(true)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Select_Boolean_Scalar) { |
| auto* builtin = Call("select", Expr(true), Expr(true), Expr(true)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Select_Float_Vec2) { |
| auto* builtin = Call("select", Call<vec2<f32>>(1_f, 1_f), Call<vec2<f32>>(1_f, 1_f), |
| Call<vec2<bool>>(true, true)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Select_Integer_Vec2) { |
| auto* builtin = Call("select", Call<vec2<i32>>(1_i, 1_i), Call<vec2<i32>>(1_i, 1_i), |
| Call<vec2<bool>>(true, true)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverBuiltinsValidationTest, Select_Boolean_Vec2) { |
| auto* builtin = Call("select", Call<vec2<bool>>(true, true), Call<vec2<bool>>(true, true), |
| Call<vec2<bool>>(true, true)); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| template <typename T> |
| class ResolverBuiltinsValidationTestWithParams : public resolver::TestHelper, |
| public testing::TestWithParam<T> {}; |
| |
| using FloatAllMatching = |
| ResolverBuiltinsValidationTestWithParams<std::tuple<std::string, uint32_t>>; |
| |
| TEST_P(FloatAllMatching, Scalar) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Expr(f32(i + 1))); |
| } |
| auto* builtin = Call(name, params); |
| Func("func", tint::Empty, ty.void_(), |
| Vector{ |
| Assign(Phony(), builtin), |
| }, |
| Vector{ |
| create<ast::StageAttribute>(ast::PipelineStage::kFragment), |
| }); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->Is<core::type::F32>()); |
| } |
| |
| TEST_P(FloatAllMatching, Vec2) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec2<f32>>(f32(i + 1), f32(i + 1))); |
| } |
| auto* builtin = Call(name, params); |
| Func("func", tint::Empty, ty.void_(), |
| Vector{ |
| Assign(Phony(), builtin), |
| }, |
| Vector{ |
| create<ast::StageAttribute>(ast::PipelineStage::kFragment), |
| }); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->is_float_vector()); |
| } |
| |
| TEST_P(FloatAllMatching, Vec3) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec3<f32>>(f32(i + 1), f32(i + 1), f32(i + 1))); |
| } |
| auto* builtin = Call(name, params); |
| Func("func", tint::Empty, ty.void_(), |
| Vector{ |
| Assign(Phony(), builtin), |
| }, |
| Vector{ |
| create<ast::StageAttribute>(ast::PipelineStage::kFragment), |
| }); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->is_float_vector()); |
| } |
| |
| TEST_P(FloatAllMatching, Vec4) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec4<f32>>(f32(i + 1), f32(i + 1), f32(i + 1), f32(i + 1))); |
| } |
| auto* builtin = Call(name, params); |
| Func("func", tint::Empty, ty.void_(), |
| Vector{ |
| Assign(Phony(), builtin), |
| }, |
| Vector{ |
| create<ast::StageAttribute>(ast::PipelineStage::kFragment), |
| }); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->is_float_vector()); |
| } |
| |
| INSTANTIATE_TEST_SUITE_P(ResolverBuiltinsValidationTest, |
| FloatAllMatching, |
| ::testing::Values(std::make_tuple("abs", 1), |
| std::make_tuple("acos", 1), |
| std::make_tuple("asin", 1), |
| std::make_tuple("atan", 1), |
| std::make_tuple("atan2", 2), |
| std::make_tuple("ceil", 1), |
| std::make_tuple("clamp", 3), |
| std::make_tuple("cos", 1), |
| std::make_tuple("cosh", 1), |
| std::make_tuple("dpdx", 1), |
| std::make_tuple("dpdxCoarse", 1), |
| std::make_tuple("dpdxFine", 1), |
| std::make_tuple("dpdy", 1), |
| std::make_tuple("dpdyCoarse", 1), |
| std::make_tuple("dpdyFine", 1), |
| std::make_tuple("exp", 1), |
| std::make_tuple("exp2", 1), |
| std::make_tuple("floor", 1), |
| std::make_tuple("fma", 3), |
| std::make_tuple("fract", 1), |
| std::make_tuple("fwidth", 1), |
| std::make_tuple("fwidthCoarse", 1), |
| std::make_tuple("fwidthFine", 1), |
| std::make_tuple("inverseSqrt", 1), |
| std::make_tuple("log", 1), |
| std::make_tuple("log2", 1), |
| std::make_tuple("max", 2), |
| std::make_tuple("min", 2), |
| std::make_tuple("mix", 3), |
| std::make_tuple("pow", 2), |
| std::make_tuple("round", 1), |
| std::make_tuple("sign", 1), |
| std::make_tuple("sin", 1), |
| std::make_tuple("sinh", 1), |
| std::make_tuple("smoothstep", 3), |
| std::make_tuple("sqrt", 1), |
| std::make_tuple("step", 2), |
| std::make_tuple("tan", 1), |
| std::make_tuple("tanh", 1), |
| std::make_tuple("trunc", 1))); |
| |
| using IntegerAllMatching = |
| ResolverBuiltinsValidationTestWithParams<std::tuple<std::string, uint32_t>>; |
| |
| TEST_P(IntegerAllMatching, ScalarUnsigned) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<u32>(1_i)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->Is<core::type::U32>()); |
| } |
| |
| TEST_P(IntegerAllMatching, Vec2Unsigned) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec2<u32>>(1_u, 1_u)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->is_unsigned_integer_vector()); |
| } |
| |
| TEST_P(IntegerAllMatching, Vec3Unsigned) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec3<u32>>(1_u, 1_u, 1_u)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->is_unsigned_integer_vector()); |
| } |
| |
| TEST_P(IntegerAllMatching, Vec4Unsigned) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec4<u32>>(1_u, 1_u, 1_u, 1_u)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->is_unsigned_integer_vector()); |
| } |
| |
| TEST_P(IntegerAllMatching, ScalarSigned) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<i32>(1_i)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->Is<core::type::I32>()); |
| } |
| |
| TEST_P(IntegerAllMatching, Vec2Signed) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec2<i32>>(1_i, 1_i)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->is_signed_integer_vector()); |
| } |
| |
| TEST_P(IntegerAllMatching, Vec3Signed) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec3<i32>>(1_i, 1_i, 1_i)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->is_signed_integer_vector()); |
| } |
| |
| TEST_P(IntegerAllMatching, Vec4Signed) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec4<i32>>(1_i, 1_i, 1_i, 1_i)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| EXPECT_TRUE(TypeOf(builtin)->is_signed_integer_vector()); |
| } |
| |
| INSTANTIATE_TEST_SUITE_P(ResolverBuiltinsValidationTest, |
| IntegerAllMatching, |
| ::testing::Values(std::make_tuple("abs", 1), |
| std::make_tuple("clamp", 3), |
| std::make_tuple("countOneBits", 1), |
| std::make_tuple("max", 2), |
| std::make_tuple("min", 2), |
| std::make_tuple("reverseBits", 1))); |
| |
| using BooleanVectorInput = |
| ResolverBuiltinsValidationTestWithParams<std::tuple<std::string, uint32_t>>; |
| |
| TEST_P(BooleanVectorInput, Vec2) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec2<bool>>(true, true)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_P(BooleanVectorInput, Vec3) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec3<bool>>(true, true, true)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_P(BooleanVectorInput, Vec4) { |
| std::string name = std::get<0>(GetParam()); |
| uint32_t num_params = std::get<1>(GetParam()); |
| |
| Vector<const ast::Expression*, 8> params; |
| for (uint32_t i = 0; i < num_params; ++i) { |
| params.Push(Call<vec4<bool>>(true, true, true, true)); |
| } |
| auto* builtin = Call(name, params); |
| WrapInFunction(builtin); |
| |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| INSTANTIATE_TEST_SUITE_P(ResolverBuiltinsValidationTest, |
| BooleanVectorInput, |
| ::testing::Values(std::make_tuple("all", 1), std::make_tuple("any", 1))); |
| |
| using DataPacking4x8 = ResolverBuiltinsValidationTestWithParams<std::string>; |
| |
| TEST_P(DataPacking4x8, Float_Vec4) { |
| auto name = GetParam(); |
| auto* builtin = Call(name, Call<vec4<f32>>(1_f, 1_f, 1_f, 1_f)); |
| WrapInFunction(builtin); |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| INSTANTIATE_TEST_SUITE_P(ResolverBuiltinsValidationTest, |
| DataPacking4x8, |
| ::testing::Values("pack4x8snorm", "pack4x8unorm")); |
| |
| using DataPacking2x16 = ResolverBuiltinsValidationTestWithParams<std::string>; |
| |
| TEST_P(DataPacking2x16, Float_Vec2) { |
| auto name = GetParam(); |
| auto* builtin = Call(name, Call<vec2<f32>>(1_f, 1_f)); |
| WrapInFunction(builtin); |
| EXPECT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| INSTANTIATE_TEST_SUITE_P(ResolverBuiltinsValidationTest, |
| DataPacking2x16, |
| ::testing::Values("pack2x16snorm", "pack2x16unorm", "pack2x16float")); |
| |
| } // namespace |
| } // namespace tint::resolver |