| dan sinclair | e4392c9 | 2020-03-17 21:00:22 +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 | |
| 15 | #include "src/writer/spirv/builder.h" |
| 16 | |
| dan sinclair | 7b7ff74 | 2020-03-20 19:27:05 +0000 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
| dan sinclair | e4392c9 | 2020-03-17 21:00:22 +0000 | [diff] [blame] | 19 | #include "gtest/gtest.h" |
| dan sinclair | 7b7ff74 | 2020-03-20 19:27:05 +0000 | [diff] [blame] | 20 | #include "spirv/unified1/spirv.h" |
| dan sinclair | e4392c9 | 2020-03-17 21:00:22 +0000 | [diff] [blame] | 21 | #include "spirv/unified1/spirv.hpp11" |
| dan sinclair | 7b7ff74 | 2020-03-20 19:27:05 +0000 | [diff] [blame] | 22 | #include "src/ast/import.h" |
| dan sinclair | e4392c9 | 2020-03-17 21:00:22 +0000 | [diff] [blame] | 23 | #include "src/ast/module.h" |
| dan sinclair | dc200f7 | 2020-03-23 20:59:12 +0000 | [diff] [blame] | 24 | #include "src/writer/spirv/spv_dump.h" |
| dan sinclair | e4392c9 | 2020-03-17 21:00:22 +0000 | [diff] [blame] | 25 | |
| 26 | namespace tint { |
| 27 | namespace writer { |
| 28 | namespace spirv { |
| dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 29 | namespace { |
| dan sinclair | e4392c9 | 2020-03-17 21:00:22 +0000 | [diff] [blame] | 30 | |
| 31 | using BuilderTest = testing::Test; |
| 32 | |
| dan sinclair | 7b7ff74 | 2020-03-20 19:27:05 +0000 | [diff] [blame] | 33 | TEST_F(BuilderTest, InsertsPreambleWithImport) { |
| 34 | ast::Module m; |
| 35 | m.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "glsl")); |
| 36 | |
| 37 | Builder b; |
| 38 | ASSERT_TRUE(b.Build(m)); |
| 39 | ASSERT_EQ(b.preamble().size(), 4); |
| 40 | |
| dan sinclair | dc200f7 | 2020-03-23 20:59:12 +0000 | [diff] [blame] | 41 | EXPECT_EQ(DumpBuilder(b), R"(OpCapability Shader |
| 42 | OpCapability VulkanMemoryModel |
| 43 | %1 = OpExtInstImport "GLSL.std.450" |
| 44 | OpMemoryModel Logical Vulkan |
| 45 | )"); |
| dan sinclair | 7b7ff74 | 2020-03-20 19:27:05 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | TEST_F(BuilderTest, InsertsPreambleWithoutImport) { |
| dan sinclair | e4392c9 | 2020-03-17 21:00:22 +0000 | [diff] [blame] | 49 | ast::Module m; |
| 50 | Builder b; |
| 51 | ASSERT_TRUE(b.Build(m)); |
| dan sinclair | dc200f7 | 2020-03-23 20:59:12 +0000 | [diff] [blame] | 52 | EXPECT_EQ(DumpBuilder(b), R"(OpCapability Shader |
| 53 | OpCapability VulkanMemoryModel |
| 54 | OpMemoryModel Logical Vulkan |
| 55 | )"); |
| dan sinclair | e4392c9 | 2020-03-17 21:00:22 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| dan sinclair | 7b7ff74 | 2020-03-20 19:27:05 +0000 | [diff] [blame] | 58 | TEST_F(BuilderTest, TracksIdBounds) { |
| 59 | Builder b; |
| 60 | |
| 61 | for (size_t i = 0; i < 5; i++) { |
| 62 | EXPECT_EQ(b.next_id(), i + 1); |
| 63 | } |
| 64 | |
| 65 | EXPECT_EQ(6, b.id_bound()); |
| 66 | } |
| 67 | |
| dan sinclair | 989cee6 | 2020-03-26 15:31:43 +0000 | [diff] [blame] | 68 | } // namespace |
| dan sinclair | e4392c9 | 2020-03-17 21:00:22 +0000 | [diff] [blame] | 69 | } // namespace spirv |
| 70 | } // namespace writer |
| 71 | } // namespace tint |