• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ast/override_decoration.h"
16 #include "src/writer/hlsl/test_helper.h"
17 
18 namespace tint {
19 namespace writer {
20 namespace hlsl {
21 namespace {
22 
23 using HlslGeneratorImplTest_ModuleConstant = TestHelper;
24 
TEST_F(HlslGeneratorImplTest_ModuleConstant,Emit_ModuleConstant)25 TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_ModuleConstant) {
26   auto* var = Const("pos", ty.array<f32, 3>(), array<f32, 3>(1.f, 2.f, 3.f));
27   WrapInFunction(Decl(var));
28 
29   GeneratorImpl& gen = Build();
30 
31   ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error();
32   EXPECT_EQ(gen.result(), "static const float pos[3] = {1.0f, 2.0f, 3.0f};\n");
33 }
34 
TEST_F(HlslGeneratorImplTest_ModuleConstant,Emit_SpecConstant)35 TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_SpecConstant) {
36   auto* var = GlobalConst("pos", ty.f32(), Expr(3.0f),
37                           ast::DecorationList{
38                               create<ast::OverrideDecoration>(23),
39                           });
40 
41   GeneratorImpl& gen = Build();
42 
43   ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error();
44   EXPECT_EQ(gen.result(), R"(#ifndef WGSL_SPEC_CONSTANT_23
45 #define WGSL_SPEC_CONSTANT_23 3.0f
46 #endif
47 static const float pos = WGSL_SPEC_CONSTANT_23;
48 )");
49 }
50 
TEST_F(HlslGeneratorImplTest_ModuleConstant,Emit_SpecConstant_NoConstructor)51 TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_SpecConstant_NoConstructor) {
52   auto* var = GlobalConst("pos", ty.f32(), nullptr,
53                           ast::DecorationList{
54                               create<ast::OverrideDecoration>(23),
55                           });
56 
57   GeneratorImpl& gen = Build();
58 
59   ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error();
60   EXPECT_EQ(gen.result(), R"(#ifndef WGSL_SPEC_CONSTANT_23
61 #error spec constant required for constant id 23
62 #endif
63 static const float pos = WGSL_SPEC_CONSTANT_23;
64 )");
65 }
66 
TEST_F(HlslGeneratorImplTest_ModuleConstant,Emit_SpecConstant_NoId)67 TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_SpecConstant_NoId) {
68   auto* a = GlobalConst("a", ty.f32(), Expr(3.0f),
69                         ast::DecorationList{
70                             create<ast::OverrideDecoration>(0),
71                         });
72   auto* b = GlobalConst("b", ty.f32(), Expr(2.0f),
73                         ast::DecorationList{
74                             create<ast::OverrideDecoration>(),
75                         });
76 
77   GeneratorImpl& gen = Build();
78 
79   ASSERT_TRUE(gen.EmitProgramConstVariable(a)) << gen.error();
80   ASSERT_TRUE(gen.EmitProgramConstVariable(b)) << gen.error();
81   EXPECT_EQ(gen.result(), R"(#ifndef WGSL_SPEC_CONSTANT_0
82 #define WGSL_SPEC_CONSTANT_0 3.0f
83 #endif
84 static const float a = WGSL_SPEC_CONSTANT_0;
85 #ifndef WGSL_SPEC_CONSTANT_1
86 #define WGSL_SPEC_CONSTANT_1 2.0f
87 #endif
88 static const float b = WGSL_SPEC_CONSTANT_1;
89 )");
90 }
91 
92 }  // namespace
93 }  // namespace hlsl
94 }  // namespace writer
95 }  // namespace tint
96