dan sinclair | 2a59901 | 2020-06-23 17:48:40 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
dan sinclair | 5f81262 | 2020-09-22 14:53:03 +0000 | [diff] [blame] | 15 | #include "src/ast/stage_decoration.h" |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 16 | #include "src/writer/msl/test_helper.h" |
dan sinclair | 2a59901 | 2020-06-23 17:48:40 +0000 | [diff] [blame] | 17 | |
| 18 | namespace tint { |
| 19 | namespace writer { |
| 20 | namespace msl { |
| 21 | namespace { |
| 22 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 23 | using MslGeneratorImplTest = TestHelper; |
dan sinclair | 2a59901 | 2020-06-23 17:48:40 +0000 | [diff] [blame] | 24 | |
dan sinclair | 2dbe9aa | 2020-09-21 15:16:20 +0000 | [diff] [blame] | 25 | TEST_F(MslGeneratorImplTest, Generate) { |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 26 | Func("my_func", ast::VariableList{}, ty.void_(), ast::StatementList{}, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 27 | ast::DecorationList{ |
Ben Clayton | 43073d8 | 2021-04-22 13:50:53 +0000 | [diff] [blame^] | 28 | Stage(ast::PipelineStage::kCompute), |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 29 | }); |
dan sinclair | 2a59901 | 2020-06-23 17:48:40 +0000 | [diff] [blame] | 30 | |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 31 | GeneratorImpl& gen = Build(); |
| 32 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 33 | ASSERT_TRUE(gen.Generate()) << gen.error(); |
| 34 | EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> |
dan sinclair | 2a59901 | 2020-06-23 17:48:40 +0000 | [diff] [blame] | 35 | |
dan sinclair | 2aaf7b5 | 2021-02-25 21:17:47 +0000 | [diff] [blame] | 36 | using namespace metal; |
dan sinclair | 987376c | 2021-01-12 04:34:53 +0000 | [diff] [blame] | 37 | kernel void my_func() { |
Ben Clayton | 7c59809 | 2021-01-13 16:34:51 +0000 | [diff] [blame] | 38 | return; |
dan sinclair | 2a59901 | 2020-06-23 17:48:40 +0000 | [diff] [blame] | 39 | } |
dan sinclair | 2dbe9aa | 2020-09-21 15:16:20 +0000 | [diff] [blame] | 40 | |
dan sinclair | 2a59901 | 2020-06-23 17:48:40 +0000 | [diff] [blame] | 41 | )"); |
| 42 | } |
| 43 | |
dan sinclair | 7caf6e5 | 2020-07-15 20:51:16 +0000 | [diff] [blame] | 44 | struct MslBuiltinData { |
| 45 | ast::Builtin builtin; |
| 46 | const char* attribute_name; |
| 47 | }; |
| 48 | inline std::ostream& operator<<(std::ostream& out, MslBuiltinData data) { |
| 49 | out << data.builtin; |
| 50 | return out; |
| 51 | } |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 52 | using MslBuiltinConversionTest = TestParamHelper<MslBuiltinData>; |
dan sinclair | 7caf6e5 | 2020-07-15 20:51:16 +0000 | [diff] [blame] | 53 | TEST_P(MslBuiltinConversionTest, Emit) { |
| 54 | auto params = GetParam(); |
| 55 | |
Ben Clayton | f12054e | 2021-01-21 16:15:00 +0000 | [diff] [blame] | 56 | GeneratorImpl& gen = Build(); |
| 57 | |
dan sinclair | 196e097 | 2020-11-13 18:13:24 +0000 | [diff] [blame] | 58 | EXPECT_EQ(gen.builtin_to_attribute(params.builtin), |
dan sinclair | 7caf6e5 | 2020-07-15 20:51:16 +0000 | [diff] [blame] | 59 | std::string(params.attribute_name)); |
| 60 | } |
| 61 | INSTANTIATE_TEST_SUITE_P( |
| 62 | MslGeneratorImplTest, |
| 63 | MslBuiltinConversionTest, |
| 64 | testing::Values(MslBuiltinData{ast::Builtin::kPosition, "position"}, |
dan sinclair | d7335fa | 2021-01-18 15:51:13 +0000 | [diff] [blame] | 65 | MslBuiltinData{ast::Builtin::kVertexIndex, "vertex_id"}, |
| 66 | MslBuiltinData{ast::Builtin::kInstanceIndex, "instance_id"}, |
dan sinclair | 7caf6e5 | 2020-07-15 20:51:16 +0000 | [diff] [blame] | 67 | MslBuiltinData{ast::Builtin::kFrontFacing, "front_facing"}, |
| 68 | MslBuiltinData{ast::Builtin::kFragCoord, "position"}, |
| 69 | MslBuiltinData{ast::Builtin::kFragDepth, "depth(any)"}, |
dan sinclair | 7caf6e5 | 2020-07-15 20:51:16 +0000 | [diff] [blame] | 70 | MslBuiltinData{ast::Builtin::kLocalInvocationId, |
| 71 | "thread_position_in_threadgroup"}, |
dan sinclair | d7335fa | 2021-01-18 15:51:13 +0000 | [diff] [blame] | 72 | MslBuiltinData{ast::Builtin::kLocalInvocationIndex, |
dan sinclair | 7caf6e5 | 2020-07-15 20:51:16 +0000 | [diff] [blame] | 73 | "thread_index_in_threadgroup"}, |
| 74 | MslBuiltinData{ast::Builtin::kGlobalInvocationId, |
James Price | 2b5acac | 2021-02-09 19:13:25 +0000 | [diff] [blame] | 75 | "thread_position_in_grid"}, |
James Price | 395b488 | 2021-04-16 19:57:34 +0000 | [diff] [blame] | 76 | MslBuiltinData{ast::Builtin::kWorkgroupId, |
| 77 | "threadgroup_position_in_grid"}, |
James Price | e7dab3c | 2021-02-16 18:21:41 +0000 | [diff] [blame] | 78 | MslBuiltinData{ast::Builtin::kSampleIndex, "sample_id"}, |
James Price | edd4d3c | 2021-04-14 16:44:38 +0000 | [diff] [blame] | 79 | MslBuiltinData{ast::Builtin::kSampleMask, "sample_mask"}, |
James Price | 2b5acac | 2021-02-09 19:13:25 +0000 | [diff] [blame] | 80 | MslBuiltinData{ast::Builtin::kSampleMaskIn, "sample_mask"}, |
| 81 | MslBuiltinData{ast::Builtin::kSampleMaskOut, |
| 82 | "sample_mask"})); |
dan sinclair | 7caf6e5 | 2020-07-15 20:51:16 +0000 | [diff] [blame] | 83 | |
dan sinclair | 2a59901 | 2020-06-23 17:48:40 +0000 | [diff] [blame] | 84 | } // namespace |
| 85 | } // namespace msl |
| 86 | } // namespace writer |
| 87 | } // namespace tint |