blob: 9ad9b821b6e59c123d16b495627f2527ee8a4ea9 [file] [log] [blame]
// Copyright 2020 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/stage_decoration.h"
#include "src/ast/struct_block_decoration.h"
#include "src/ast/variable_decl_statement.h"
#include "src/writer/msl/test_helper.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslGeneratorImplTest = TestHelper;
TEST_F(MslGeneratorImplTest, Emit_Function) {
Func("my_func", ast::VariableList{}, ty.void_(),
ast::StatementList{
Return(),
},
{});
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"( #include <metal_stdlib>
using namespace metal;
void my_func() {
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Function_WithParams) {
ast::VariableList params;
params.push_back(Param("a", ty.f32()));
params.push_back(Param("b", ty.i32()));
Func("my_func", params, ty.void_(),
ast::StatementList{
Return(),
},
{});
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"( #include <metal_stdlib>
using namespace metal;
void my_func(float a, int b) {
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Decoration_EntryPoint_NoReturn_Void) {
Func("main", ast::VariableList{}, ty.void_(),
ast::StatementList{/* no explicit return */},
{Stage(ast::PipelineStage::kFragment)});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
fragment void main() {
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Decoration_EntryPoint_WithInOutVars) {
// fn frag_main([[location(0)]] foo : f32) -> [[location(1)]] f32 {
// return foo;
// }
auto* foo_in = Param("foo", ty.f32(), {Location(0)});
Func("frag_main", ast::VariableList{foo_in}, ty.f32(), {Return("foo")},
{Stage(ast::PipelineStage::kFragment)}, {Location(1)});
GeneratorImpl& gen = SanitizeAndBuild();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct tint_symbol_1 {
float foo [[user(locn0)]];
};
struct tint_symbol_2 {
float value [[color(1)]];
};
fragment tint_symbol_2 frag_main(tint_symbol_1 tint_symbol [[stage_in]]) {
float const foo = tint_symbol.foo;
tint_symbol_2 const tint_symbol_3 = {.value=foo};
return tint_symbol_3;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Decoration_EntryPoint_WithInOut_Builtins) {
// fn frag_main([[position(0)]] coord : vec4<f32>) -> [[frag_depth]] f32 {
// return coord.x;
// }
auto* coord_in =
Param("coord", ty.vec4<f32>(), {Builtin(ast::Builtin::kPosition)});
Func("frag_main", ast::VariableList{coord_in}, ty.f32(),
{Return(MemberAccessor("coord", "x"))},
{Stage(ast::PipelineStage::kFragment)},
{Builtin(ast::Builtin::kFragDepth)});
GeneratorImpl& gen = SanitizeAndBuild();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct tint_symbol_1 {
float value [[depth(any)]];
};
fragment tint_symbol_1 frag_main(float4 coord [[position]]) {
tint_symbol_1 const tint_symbol_2 = {.value=coord.x};
return tint_symbol_2;
}
)");
}
TEST_F(MslGeneratorImplTest,
Emit_Decoration_EntryPoint_SharedStruct_DifferentStages) {
// struct Interface {
// [[location(1)]] col1 : f32;
// [[location(2)]] col2 : f32;
// [[builtin(position)]] pos : vec4<f32>;
// };
// fn vert_main() -> Interface {
// return Interface(0.4, 0.6, vec4<f32>());
// }
// fn frag_main(colors : Interface) {
// const r = colors.col1;
// const g = colors.col2;
// }
auto* interface_struct = Structure(
"Interface",
{
Member("col1", ty.f32(), {Location(1)}),
Member("col2", ty.f32(), {Location(2)}),
Member("pos", ty.vec4<f32>(), {Builtin(ast::Builtin::kPosition)}),
});
Func("vert_main", {}, ty.Of(interface_struct),
{Return(Construct(ty.Of(interface_struct), Expr(0.5f), Expr(0.25f),
Construct(ty.vec4<f32>())))},
{Stage(ast::PipelineStage::kVertex)});
Func("frag_main", {Param("colors", ty.Of(interface_struct))}, ty.void_(),
{
WrapInStatement(
Const("r", ty.f32(), MemberAccessor("colors", "col1"))),
WrapInStatement(
Const("g", ty.f32(), MemberAccessor("colors", "col2"))),
},
{Stage(ast::PipelineStage::kFragment)});
GeneratorImpl& gen = SanitizeAndBuild();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct Interface {
float col1;
float col2;
float4 pos;
};
struct tint_symbol {
float col1 [[user(locn1)]];
float col2 [[user(locn2)]];
float4 pos [[position]];
};
struct tint_symbol_4 {
float col1 [[user(locn1)]];
float col2 [[user(locn2)]];
};
vertex tint_symbol vert_main() {
Interface const tint_symbol_1 = {.col1=0.5f, .col2=0.25f, .pos=float4()};
tint_symbol const tint_symbol_5 = {.col1=tint_symbol_1.col1, .col2=tint_symbol_1.col2, .pos=tint_symbol_1.pos};
return tint_symbol_5;
}
fragment void frag_main(float4 tint_symbol_3 [[position]], tint_symbol_4 tint_symbol_2 [[stage_in]]) {
Interface const colors = {.col1=tint_symbol_2.col1, .col2=tint_symbol_2.col2, .pos=tint_symbol_3};
float const r = colors.col1;
float const g = colors.col2;
return;
}
)");
}
TEST_F(MslGeneratorImplTest,
Emit_Decoration_EntryPoint_SharedStruct_HelperFunction) {
// struct VertexOutput {
// [[builtin(position)]] pos : vec4<f32>;
// };
// fn foo(x : f32) -> VertexOutput {
// return VertexOutput(vec4<f32>(x, x, x, 1.0));
// }
// fn vert_main1() -> VertexOutput {
// return foo(0.5);
// }
// fn vert_main2() -> VertexOutput {
// return foo(0.25);
// }
auto* vertex_output_struct = Structure(
"VertexOutput",
{Member("pos", ty.vec4<f32>(), {Builtin(ast::Builtin::kPosition)})});
Func("foo", {Param("x", ty.f32())}, ty.Of(vertex_output_struct),
{Return(Construct(ty.Of(vertex_output_struct),
Construct(ty.vec4<f32>(), "x", "x", "x", Expr(1.f))))},
{});
Func("vert_main1", {}, ty.Of(vertex_output_struct),
{Return(Expr(Call("foo", Expr(0.5f))))},
{Stage(ast::PipelineStage::kVertex)});
Func("vert_main2", {}, ty.Of(vertex_output_struct),
{Return(Expr(Call("foo", Expr(0.25f))))},
{Stage(ast::PipelineStage::kVertex)});
GeneratorImpl& gen = SanitizeAndBuild();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct VertexOutput {
float4 pos;
};
struct tint_symbol {
float4 pos [[position]];
};
struct tint_symbol_2 {
float4 pos [[position]];
};
VertexOutput foo(float x) {
VertexOutput const tint_symbol_4 = {.pos=float4(x, x, x, 1.0f)};
return tint_symbol_4;
}
vertex tint_symbol vert_main1() {
VertexOutput const tint_symbol_1 = foo(0.5f);
tint_symbol const tint_symbol_5 = {.pos=tint_symbol_1.pos};
return tint_symbol_5;
}
vertex tint_symbol_2 vert_main2() {
VertexOutput const tint_symbol_3 = foo(0.25f);
tint_symbol_2 const tint_symbol_6 = {.pos=tint_symbol_3.pos};
return tint_symbol_6;
}
)");
}
TEST_F(MslGeneratorImplTest,
Emit_FunctionDecoration_EntryPoint_With_RW_StorageBuffer) {
auto* s = Structure("Data",
{
Member("a", ty.i32()),
Member("b", ty.f32()),
},
{create<ast::StructBlockDecoration>()});
Global("coord", ty.Of(s), ast::StorageClass::kStorage,
ast::Access::kReadWrite,
ast::DecorationList{
create<ast::BindingDecoration>(0),
create<ast::GroupDecoration>(1),
});
auto* var = Var("v", ty.f32(), ast::StorageClass::kNone,
MemberAccessor("coord", "b"));
Func("frag_main", ast::VariableList{}, ty.void_(),
ast::StatementList{
Decl(var),
Return(),
},
{
Stage(ast::PipelineStage::kFragment),
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct Data {
/* 0x0000 */ int a;
/* 0x0004 */ float b;
};
fragment void frag_main(device Data& coord [[buffer(0)]]) {
float v = coord.b;
return;
}
)");
}
TEST_F(MslGeneratorImplTest,
Emit_FunctionDecoration_EntryPoint_With_RO_StorageBuffer) {
auto* s = Structure("Data",
{
Member("a", ty.i32()),
Member("b", ty.f32()),
},
{create<ast::StructBlockDecoration>()});
Global("coord", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead,
ast::DecorationList{
create<ast::BindingDecoration>(0),
create<ast::GroupDecoration>(1),
});
auto* var = Var("v", ty.f32(), ast::StorageClass::kNone,
MemberAccessor("coord", "b"));
Func("frag_main", ast::VariableList{}, ty.void_(),
ast::StatementList{
Decl(var),
Return(),
},
{
Stage(ast::PipelineStage::kFragment),
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct Data {
/* 0x0000 */ int a;
/* 0x0004 */ float b;
};
fragment void frag_main(const device Data& coord [[buffer(0)]]) {
float v = coord.b;
return;
}
)");
}
TEST_F(MslGeneratorImplTest,
Emit_Decoration_Called_By_EntryPoint_With_Uniform) {
auto* ubo_ty = Structure("UBO", {Member("coord", ty.vec4<f32>())},
{create<ast::StructBlockDecoration>()});
auto* ubo = Global("ubo", ty.Of(ubo_ty), ast::StorageClass::kUniform,
ast::DecorationList{
create<ast::BindingDecoration>(0),
create<ast::GroupDecoration>(1),
});
Func("sub_func",
{
Param("param", ty.f32()),
},
ty.f32(),
{
Return(MemberAccessor(MemberAccessor(ubo, "coord"), "x")),
});
auto* var =
Var("v", ty.f32(), ast::StorageClass::kNone, Call("sub_func", 1.0f));
Func("frag_main", {}, ty.void_(),
{
Decl(var),
Return(),
},
{
Stage(ast::PipelineStage::kFragment),
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct UBO {
/* 0x0000 */ packed_float4 coord;
};
float sub_func(constant UBO& ubo, float param) {
return ubo.coord.x;
}
fragment void frag_main(constant UBO& ubo [[buffer(0)]]) {
float v = sub_func(ubo, 1.0f);
return;
}
)");
}
TEST_F(MslGeneratorImplTest,
Emit_FunctionDecoration_Called_By_EntryPoint_With_RW_StorageBuffer) {
auto* s = Structure("Data",
{
Member("a", ty.i32()),
Member("b", ty.f32()),
},
{create<ast::StructBlockDecoration>()});
Global("coord", ty.Of(s), ast::StorageClass::kStorage,
ast::Access::kReadWrite,
ast::DecorationList{
create<ast::BindingDecoration>(0),
create<ast::GroupDecoration>(1),
});
ast::VariableList params;
params.push_back(Param("param", ty.f32()));
auto body = ast::StatementList{Return(MemberAccessor("coord", "b"))};
Func("sub_func", params, ty.f32(), body, {});
auto* var =
Var("v", ty.f32(), ast::StorageClass::kNone, Call("sub_func", 1.0f));
Func("frag_main", ast::VariableList{}, ty.void_(),
ast::StatementList{
Decl(var),
Return(),
},
{
Stage(ast::PipelineStage::kFragment),
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct Data {
/* 0x0000 */ int a;
/* 0x0004 */ float b;
};
float sub_func(device Data& coord, float param) {
return coord.b;
}
fragment void frag_main(device Data& coord [[buffer(0)]]) {
float v = sub_func(coord, 1.0f);
return;
}
)");
}
TEST_F(MslGeneratorImplTest,
Emit_FunctionDecoration_Called_By_EntryPoint_With_RO_StorageBuffer) {
auto* s = Structure("Data",
{
Member("a", ty.i32()),
Member("b", ty.f32()),
},
{create<ast::StructBlockDecoration>()});
Global("coord", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead,
ast::DecorationList{
create<ast::BindingDecoration>(0),
create<ast::GroupDecoration>(1),
});
ast::VariableList params;
params.push_back(Param("param", ty.f32()));
auto body = ast::StatementList{Return(MemberAccessor("coord", "b"))};
Func("sub_func", params, ty.f32(), body, {});
auto* var =
Var("v", ty.f32(), ast::StorageClass::kNone, Call("sub_func", 1.0f));
Func("frag_main", ast::VariableList{}, ty.void_(),
ast::StatementList{
Decl(var),
Return(),
},
{
Stage(ast::PipelineStage::kFragment),
});
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct Data {
/* 0x0000 */ int a;
/* 0x0004 */ float b;
};
float sub_func(const device Data& coord, float param) {
return coord.b;
}
fragment void frag_main(const device Data& coord [[buffer(0)]]) {
float v = sub_func(coord, 1.0f);
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Function_WithArrayParams) {
ast::VariableList params;
params.push_back(Param("a", ty.array<f32, 5>()));
Func("my_func", params, ty.void_(),
{
Return(),
});
GeneratorImpl& gen = SanitizeAndBuild();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"( #include <metal_stdlib>
using namespace metal;
struct tint_array_wrapper {
float arr[5];
};
void my_func(tint_array_wrapper a) {
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Function_WithArrayReturn) {
Func("my_func", {}, ty.array<f32, 5>(),
{
Return(Construct(ty.array<f32, 5>())),
});
GeneratorImpl& gen = SanitizeAndBuild();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"( #include <metal_stdlib>
using namespace metal;
struct tint_array_wrapper {
float arr[5];
};
tint_array_wrapper my_func() {
tint_array_wrapper const tint_symbol = {.arr={}};
return tint_symbol;
}
)");
}
// https://crbug.com/tint/297
TEST_F(MslGeneratorImplTest,
Emit_Function_Multiple_EntryPoint_With_Same_ModuleVar) {
// [[block]] struct Data {
// d : f32;
// };
// [[binding(0), group(0)]] var<storage> data : Data;
//
// [[stage(compute), workgroup_size(1)]]
// fn a() {
// return;
// }
//
// [[stage(compute), workgroup_size(1)]]
// fn b() {
// return;
// }
auto* s = Structure("Data", {Member("d", ty.f32())},
{create<ast::StructBlockDecoration>()});
Global("data", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kReadWrite,
ast::DecorationList{
create<ast::BindingDecoration>(0),
create<ast::GroupDecoration>(0),
});
{
auto* var = Var("v", ty.f32(), ast::StorageClass::kNone,
MemberAccessor("data", "d"));
Func("a", ast::VariableList{}, ty.void_(),
ast::StatementList{
Decl(var),
Return(),
},
{
Stage(ast::PipelineStage::kCompute),
WorkgroupSize(1),
});
}
{
auto* var = Var("v", ty.f32(), ast::StorageClass::kNone,
MemberAccessor("data", "d"));
Func("b", ast::VariableList{}, ty.void_(),
ast::StatementList{Decl(var), Return()},
{
Stage(ast::PipelineStage::kCompute),
WorkgroupSize(1),
});
}
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal;
struct Data {
/* 0x0000 */ float d;
};
kernel void a(device Data& data [[buffer(0)]]) {
float v = data.d;
return;
}
kernel void b(device Data& data [[buffer(0)]]) {
float v = data.d;
return;
}
)");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint