| // Copyright 2021 The Tint Authors. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "src/ast/call_statement.h" |
| #include "src/ast/stage_decoration.h" |
| #include "src/ast/struct_block_decoration.h" |
| #include "src/ast/variable_decl_statement.h" |
| #include "src/writer/hlsl/test_helper.h" |
| |
| namespace tint { |
| namespace writer { |
| namespace hlsl { |
| namespace { |
| |
| using HlslSanitizerTest = TestHelper; |
| |
| // TODO(crbug.com/tint/806): Remove |
| TEST_F(HlslSanitizerTest, ArrayLength_DEPRECATED) { |
| auto* sb_ty = Structure("SB", |
| { |
| Member("x", ty.f32()), |
| Member("arr", ty.array(ty.vec4<f32>())), |
| }, |
| { |
| create<ast::StructBlockDecoration>(), |
| }); |
| |
| Global("sb", ty.Of(sb_ty), ast::StorageClass::kStorage, ast::Access::kRead, |
| ast::DecorationList{ |
| create<ast::BindingDecoration>(0), |
| create<ast::GroupDecoration>(1), |
| }); |
| |
| Func("main", ast::VariableList{}, ty.void_(), |
| { |
| Decl(Var("len", ty.u32(), ast::StorageClass::kNone, |
| Call("arrayLength", MemberAccessor("sb", "arr")))), |
| }, |
| { |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| GeneratorImpl& gen = SanitizeAndBuild(); |
| |
| ASSERT_TRUE(gen.Generate(out)) << gen.error(); |
| |
| auto got = result(); |
| auto* expect = R"( |
| ByteAddressBuffer sb : register(t0, space1); |
| |
| void main() { |
| uint tint_symbol_1 = 0u; |
| sb.GetDimensions(tint_symbol_1); |
| const uint tint_symbol_2 = ((tint_symbol_1 - 16u) / 16u); |
| uint len = tint_symbol_2; |
| return; |
| } |
| |
| )"; |
| EXPECT_EQ(expect, got); |
| } |
| |
| TEST_F(HlslSanitizerTest, Call_ArrayLength) { |
| auto* s = Structure("my_struct", {Member(0, "a", ty.array<f32>(4))}, |
| {create<ast::StructBlockDecoration>()}); |
| Global("b", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, |
| ast::DecorationList{ |
| create<ast::BindingDecoration>(1), |
| create<ast::GroupDecoration>(2), |
| }); |
| |
| Func("a_func", ast::VariableList{}, ty.void_(), |
| ast::StatementList{ |
| Decl(Var("len", ty.u32(), ast::StorageClass::kNone, |
| Call("arrayLength", AddressOf(MemberAccessor("b", "a"))))), |
| }, |
| ast::DecorationList{ |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| GeneratorImpl& gen = SanitizeAndBuild(); |
| |
| ASSERT_TRUE(gen.Generate(out)) << gen.error(); |
| |
| auto got = result(); |
| auto* expect = R"( |
| ByteAddressBuffer b : register(t1, space2); |
| |
| void a_func() { |
| uint tint_symbol_1 = 0u; |
| b.GetDimensions(tint_symbol_1); |
| const uint tint_symbol_2 = ((tint_symbol_1 - 0u) / 4u); |
| uint len = tint_symbol_2; |
| return; |
| } |
| |
| )"; |
| EXPECT_EQ(expect, got); |
| } |
| |
| TEST_F(HlslSanitizerTest, Call_ArrayLength_OtherMembersInStruct) { |
| auto* s = Structure("my_struct", |
| { |
| Member(0, "z", ty.f32()), |
| Member(4, "a", ty.array<f32>(4)), |
| }, |
| {create<ast::StructBlockDecoration>()}); |
| Global("b", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, |
| ast::DecorationList{ |
| create<ast::BindingDecoration>(1), |
| create<ast::GroupDecoration>(2), |
| }); |
| |
| Func("a_func", ast::VariableList{}, ty.void_(), |
| ast::StatementList{ |
| Decl(Var("len", ty.u32(), ast::StorageClass::kNone, |
| Call("arrayLength", AddressOf(MemberAccessor("b", "a"))))), |
| }, |
| ast::DecorationList{ |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| GeneratorImpl& gen = SanitizeAndBuild(); |
| |
| ASSERT_TRUE(gen.Generate(out)) << gen.error(); |
| |
| auto got = result(); |
| auto* expect = R"( |
| ByteAddressBuffer b : register(t1, space2); |
| |
| void a_func() { |
| uint tint_symbol_1 = 0u; |
| b.GetDimensions(tint_symbol_1); |
| const uint tint_symbol_2 = ((tint_symbol_1 - 4u) / 4u); |
| uint len = tint_symbol_2; |
| return; |
| } |
| |
| )"; |
| |
| EXPECT_EQ(expect, got); |
| } |
| |
| TEST_F(HlslSanitizerTest, Call_ArrayLength_ViaLets) { |
| auto* s = Structure("my_struct", {Member(0, "a", ty.array<f32>(4))}, |
| {create<ast::StructBlockDecoration>()}); |
| Global("b", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, |
| ast::DecorationList{ |
| create<ast::BindingDecoration>(1), |
| create<ast::GroupDecoration>(2), |
| }); |
| |
| auto* p = Const("p", nullptr, AddressOf("b")); |
| auto* p2 = Const("p2", nullptr, AddressOf(MemberAccessor(Deref(p), "a"))); |
| |
| Func("a_func", ast::VariableList{}, ty.void_(), |
| ast::StatementList{ |
| Decl(p), |
| Decl(p2), |
| Decl(Var("len", ty.u32(), ast::StorageClass::kNone, |
| Call("arrayLength", p2))), |
| }, |
| ast::DecorationList{ |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| GeneratorImpl& gen = SanitizeAndBuild(); |
| |
| ASSERT_TRUE(gen.Generate(out)) << gen.error(); |
| |
| auto got = result(); |
| auto* expect = R"( |
| ByteAddressBuffer b : register(t1, space2); |
| |
| void a_func() { |
| uint tint_symbol_1 = 0u; |
| b.GetDimensions(tint_symbol_1); |
| const uint tint_symbol_2 = ((tint_symbol_1 - 0u) / 4u); |
| uint len = tint_symbol_2; |
| return; |
| } |
| |
| )"; |
| |
| EXPECT_EQ(expect, got); |
| } |
| |
| TEST_F(HlslSanitizerTest, PromoteArrayInitializerToConstVar) { |
| auto* array_init = array<i32, 4>(1, 2, 3, 4); |
| auto* array_index = IndexAccessor(array_init, 3); |
| auto* pos = Var("pos", ty.i32(), ast::StorageClass::kNone, array_index); |
| |
| Func("main", ast::VariableList{}, ty.void_(), |
| { |
| Decl(pos), |
| }, |
| { |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| GeneratorImpl& gen = SanitizeAndBuild(); |
| |
| ASSERT_TRUE(gen.Generate(out)) << gen.error(); |
| |
| auto got = result(); |
| auto* expect = R"(void main() { |
| const int tint_symbol[4] = {1, 2, 3, 4}; |
| int pos = tint_symbol[3]; |
| return; |
| } |
| |
| )"; |
| EXPECT_EQ(expect, got); |
| } |
| |
| TEST_F(HlslSanitizerTest, PromoteStructInitializerToConstVar) { |
| auto* str = Structure("S", { |
| Member("a", ty.i32()), |
| Member("b", ty.vec3<f32>()), |
| Member("c", ty.i32()), |
| }); |
| auto* struct_init = Construct(ty.Of(str), 1, vec3<f32>(2.f, 3.f, 4.f), 4); |
| auto* struct_access = MemberAccessor(struct_init, "b"); |
| auto* pos = |
| Var("pos", ty.vec3<f32>(), ast::StorageClass::kNone, struct_access); |
| |
| Func("main", ast::VariableList{}, ty.void_(), |
| { |
| Decl(pos), |
| }, |
| { |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| GeneratorImpl& gen = SanitizeAndBuild(); |
| |
| ASSERT_TRUE(gen.Generate(out)) << gen.error(); |
| |
| auto got = result(); |
| auto* expect = R"(struct S { |
| int a; |
| float3 b; |
| int c; |
| }; |
| |
| void main() { |
| const S tint_symbol = {1, float3(2.0f, 3.0f, 4.0f), 4}; |
| float3 pos = tint_symbol.b; |
| return; |
| } |
| |
| )"; |
| EXPECT_EQ(expect, got); |
| } |
| |
| TEST_F(HlslSanitizerTest, InlinePtrLetsBasic) { |
| // var v : i32; |
| // let p : ptr<function, i32> = &v; |
| // let x : i32 = *p; |
| auto* v = Var("v", ty.i32()); |
| auto* p = |
| Const("p", ty.pointer<i32>(ast::StorageClass::kFunction), AddressOf(v)); |
| auto* x = Var("x", ty.i32(), ast::StorageClass::kNone, Deref(p)); |
| |
| Func("main", ast::VariableList{}, ty.void_(), |
| { |
| Decl(v), |
| Decl(p), |
| Decl(x), |
| }, |
| { |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| GeneratorImpl& gen = SanitizeAndBuild(); |
| |
| ASSERT_TRUE(gen.Generate(out)) << gen.error(); |
| |
| auto got = result(); |
| auto* expect = R"(void main() { |
| int v = 0; |
| int x = v; |
| return; |
| } |
| |
| )"; |
| EXPECT_EQ(expect, got); |
| } |
| |
| TEST_F(HlslSanitizerTest, InlinePtrLetsComplexChain) { |
| // var m : mat4x4<f32>; |
| // let mp : ptr<function, mat4x4<f32>> = &m; |
| // let vp : ptr<function, vec4<f32>> = &(*mp)[2]; |
| // let fp : ptr<function, f32> = &(*vp)[1]; |
| // let f : f32 = *fp; |
| auto* m = Var("m", ty.mat4x4<f32>()); |
| auto* mp = |
| Const("mp", ty.pointer(ty.mat4x4<f32>(), ast::StorageClass::kFunction), |
| AddressOf(m)); |
| auto* vp = |
| Const("vp", ty.pointer(ty.vec4<f32>(), ast::StorageClass::kFunction), |
| AddressOf(IndexAccessor(Deref(mp), 2))); |
| auto* fp = Const("fp", ty.pointer<f32>(ast::StorageClass::kFunction), |
| AddressOf(IndexAccessor(Deref(vp), 1))); |
| auto* f = Var("f", ty.f32(), ast::StorageClass::kNone, Deref(fp)); |
| |
| Func("main", ast::VariableList{}, ty.void_(), |
| { |
| Decl(m), |
| Decl(mp), |
| Decl(vp), |
| Decl(fp), |
| Decl(f), |
| }, |
| { |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| GeneratorImpl& gen = SanitizeAndBuild(); |
| |
| ASSERT_TRUE(gen.Generate(out)) << gen.error(); |
| |
| auto got = result(); |
| auto* expect = R"(void main() { |
| float4x4 m = float4x4(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); |
| float f = m[2][1]; |
| return; |
| } |
| |
| )"; |
| EXPECT_EQ(expect, got); |
| } |
| |
| TEST_F(HlslSanitizerTest, InlineParam) { |
| // fn x(p : ptr<function, i32>) -> i32 { |
| // return *p; |
| // } |
| // |
| // [[stage(fragment)]] |
| // fn main() { |
| // var v : i32; |
| // let p : ptr<function, i32> = &v; |
| // var r : i32 = x(p); |
| // } |
| |
| Func("x", {Param("p", ty.pointer<i32>(ast::StorageClass::kFunction))}, |
| ty.i32(), {Return(Deref("p"))}); |
| |
| auto* v = Var("v", ty.i32()); |
| auto* p = Const("p", ty.pointer(ty.i32(), ast::StorageClass::kFunction), |
| AddressOf(v)); |
| auto* r = Var("r", ty.i32(), ast::StorageClass::kNone, Call("x", p)); |
| |
| Func("main", ast::VariableList{}, ty.void_(), |
| { |
| Decl(v), |
| Decl(p), |
| Decl(r), |
| }, |
| { |
| Stage(ast::PipelineStage::kFragment), |
| }); |
| |
| GeneratorImpl& gen = SanitizeAndBuild(); |
| |
| ASSERT_TRUE(gen.Generate(out)) << gen.error(); |
| |
| auto got = result(); |
| auto* expect = R"(int x(inout int p) { |
| return p; |
| } |
| |
| void main() { |
| int v = 0; |
| int r = x(v); |
| return; |
| } |
| |
| )"; |
| EXPECT_EQ(expect, got); |
| } |
| |
| } // namespace |
| } // namespace hlsl |
| } // namespace writer |
| } // namespace tint |