blob: de6fd7afb83c8dced285c007136be7cba849fc1e [file] [log] [blame]
dan sinclaire4392c92020-03-17 21:00:22 +00001// 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 sinclair7b7ff742020-03-20 19:27:05 +000017#include <memory>
18
dan sinclaire4392c92020-03-17 21:00:22 +000019#include "gtest/gtest.h"
dan sinclair7b7ff742020-03-20 19:27:05 +000020#include "spirv/unified1/spirv.h"
dan sinclaire4392c92020-03-17 21:00:22 +000021#include "spirv/unified1/spirv.hpp11"
dan sinclair7b7ff742020-03-20 19:27:05 +000022#include "src/ast/import.h"
dan sinclaire4392c92020-03-17 21:00:22 +000023#include "src/ast/module.h"
dan sinclairdc200f72020-03-23 20:59:12 +000024#include "src/writer/spirv/spv_dump.h"
dan sinclaire4392c92020-03-17 21:00:22 +000025
26namespace tint {
27namespace writer {
28namespace spirv {
dan sinclair989cee62020-03-26 15:31:43 +000029namespace {
dan sinclaire4392c92020-03-17 21:00:22 +000030
31using BuilderTest = testing::Test;
32
dan sinclair7b7ff742020-03-20 19:27:05 +000033TEST_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 sinclairdc200f72020-03-23 20:59:12 +000041 EXPECT_EQ(DumpBuilder(b), R"(OpCapability Shader
42OpCapability VulkanMemoryModel
43%1 = OpExtInstImport "GLSL.std.450"
44OpMemoryModel Logical Vulkan
45)");
dan sinclair7b7ff742020-03-20 19:27:05 +000046}
47
48TEST_F(BuilderTest, InsertsPreambleWithoutImport) {
dan sinclaire4392c92020-03-17 21:00:22 +000049 ast::Module m;
50 Builder b;
51 ASSERT_TRUE(b.Build(m));
dan sinclairdc200f72020-03-23 20:59:12 +000052 EXPECT_EQ(DumpBuilder(b), R"(OpCapability Shader
53OpCapability VulkanMemoryModel
54OpMemoryModel Logical Vulkan
55)");
dan sinclaire4392c92020-03-17 21:00:22 +000056}
57
dan sinclair7b7ff742020-03-20 19:27:05 +000058TEST_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 sinclair989cee62020-03-26 15:31:43 +000068} // namespace
dan sinclaire4392c92020-03-17 21:00:22 +000069} // namespace spirv
70} // namespace writer
71} // namespace tint