1 // Copyright 2019 The Amber 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 parseried.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "gtest/gtest.h"
16 #include "src/amberscript/parser.h"
17
18 namespace amber {
19 namespace amberscript {
20
21 using AmberScriptParserTest = testing::Test;
22
TEST_F(AmberScriptParserTest,SamplerDefaultValues)23 TEST_F(AmberScriptParserTest, SamplerDefaultValues) {
24 std::string in = "SAMPLER sampler";
25
26 Parser parser;
27 Result r = parser.Parse(in);
28 ASSERT_TRUE(r.IsSuccess()) << r.Error();
29
30 auto script = parser.GetScript();
31 const auto& samplers = script->GetSamplers();
32 ASSERT_EQ(1U, samplers.size());
33
34 ASSERT_TRUE(samplers[0] != nullptr);
35 EXPECT_EQ("sampler", samplers[0]->GetName());
36
37 auto* sampler = samplers[0].get();
38 EXPECT_EQ(FilterType::kNearest, sampler->GetMagFilter());
39 EXPECT_EQ(FilterType::kNearest, sampler->GetMinFilter());
40 EXPECT_EQ(FilterType::kNearest, sampler->GetMipmapMode());
41 EXPECT_EQ(AddressMode::kRepeat, sampler->GetAddressModeU());
42 EXPECT_EQ(AddressMode::kRepeat, sampler->GetAddressModeV());
43 EXPECT_EQ(AddressMode::kRepeat, sampler->GetAddressModeW());
44 EXPECT_EQ(BorderColor::kFloatTransparentBlack, sampler->GetBorderColor());
45 EXPECT_EQ(0.0, sampler->GetMinLOD());
46 EXPECT_EQ(1.0, sampler->GetMaxLOD());
47 EXPECT_EQ(true, sampler->GetNormalizedCoords());
48 }
49
TEST_F(AmberScriptParserTest,SamplerCustomValues)50 TEST_F(AmberScriptParserTest, SamplerCustomValues) {
51 std::string in = R"(
52 SAMPLER sampler MAG_FILTER linear \
53 MIN_FILTER linear \
54 ADDRESS_MODE_U clamp_to_edge \
55 ADDRESS_MODE_V clamp_to_border \
56 ADDRESS_MODE_W mirrored_repeat \
57 BORDER_COLOR float_opaque_white \
58 MIN_LOD 2.5 \
59 MAX_LOD 5.0 \
60 NORMALIZED_COORDS)";
61
62 Parser parser;
63 Result r = parser.Parse(in);
64 ASSERT_TRUE(r.IsSuccess()) << r.Error();
65
66 auto script = parser.GetScript();
67 const auto& samplers = script->GetSamplers();
68 ASSERT_EQ(1U, samplers.size());
69
70 ASSERT_TRUE(samplers[0] != nullptr);
71 EXPECT_EQ("sampler", samplers[0]->GetName());
72
73 auto* sampler = samplers[0].get();
74 EXPECT_EQ(FilterType::kLinear, sampler->GetMagFilter());
75 EXPECT_EQ(FilterType::kLinear, sampler->GetMinFilter());
76 EXPECT_EQ(FilterType::kNearest, sampler->GetMipmapMode());
77 EXPECT_EQ(AddressMode::kClampToEdge, sampler->GetAddressModeU());
78 EXPECT_EQ(AddressMode::kClampToBorder, sampler->GetAddressModeV());
79 EXPECT_EQ(AddressMode::kMirroredRepeat, sampler->GetAddressModeW());
80 EXPECT_EQ(BorderColor::kFloatOpaqueWhite, sampler->GetBorderColor());
81 EXPECT_EQ(2.5, sampler->GetMinLOD());
82 EXPECT_EQ(5.0, sampler->GetMaxLOD());
83 EXPECT_EQ(true, sampler->GetNormalizedCoords());
84 }
85
TEST_F(AmberScriptParserTest,SamplerUnexpectedParameter)86 TEST_F(AmberScriptParserTest, SamplerUnexpectedParameter) {
87 std::string in = R"(
88 SAMPLER sampler MAG_FILTER linear \
89 FOO \
90 ADDRESS_MODE_U clamp_to_edge)";
91
92 Parser parser;
93 Result r = parser.Parse(in);
94 ASSERT_FALSE(r.IsSuccess());
95 EXPECT_EQ("3: unexpected sampler parameter FOO", r.Error());
96 }
97
TEST_F(AmberScriptParserTest,SamplerInvalidMagFilter)98 TEST_F(AmberScriptParserTest, SamplerInvalidMagFilter) {
99 std::string in = "SAMPLER sampler MAG_FILTER foo";
100
101 Parser parser;
102 Result r = parser.Parse(in);
103 ASSERT_FALSE(r.IsSuccess());
104 EXPECT_EQ("1: invalid MAG_FILTER value foo", r.Error());
105 }
106
TEST_F(AmberScriptParserTest,SamplerInvalidMinFilter)107 TEST_F(AmberScriptParserTest, SamplerInvalidMinFilter) {
108 std::string in = "SAMPLER sampler MIN_FILTER foo";
109
110 Parser parser;
111 Result r = parser.Parse(in);
112 ASSERT_FALSE(r.IsSuccess());
113 EXPECT_EQ("1: invalid MIN_FILTER value foo", r.Error());
114 }
115
TEST_F(AmberScriptParserTest,SamplerInvalidAddressModeU)116 TEST_F(AmberScriptParserTest, SamplerInvalidAddressModeU) {
117 std::string in = "SAMPLER sampler ADDRESS_MODE_U foo";
118
119 Parser parser;
120 Result r = parser.Parse(in);
121 ASSERT_FALSE(r.IsSuccess());
122 EXPECT_EQ("1: invalid ADDRESS_MODE_U value foo", r.Error());
123 }
124
TEST_F(AmberScriptParserTest,SamplerInvalidAddressModeV)125 TEST_F(AmberScriptParserTest, SamplerInvalidAddressModeV) {
126 std::string in = "SAMPLER sampler ADDRESS_MODE_V foo";
127
128 Parser parser;
129 Result r = parser.Parse(in);
130 ASSERT_FALSE(r.IsSuccess());
131 EXPECT_EQ("1: invalid ADDRESS_MODE_V value foo", r.Error());
132 }
133
TEST_F(AmberScriptParserTest,SamplerInvalidBorderColor)134 TEST_F(AmberScriptParserTest, SamplerInvalidBorderColor) {
135 std::string in = "SAMPLER sampler BORDER_COLOR foo";
136
137 Parser parser;
138 Result r = parser.Parse(in);
139 ASSERT_FALSE(r.IsSuccess());
140 EXPECT_EQ("1: invalid BORDER_COLOR value foo", r.Error());
141 }
142
TEST_F(AmberScriptParserTest,SamplerInvalidMinLod)143 TEST_F(AmberScriptParserTest, SamplerInvalidMinLod) {
144 std::string in = "SAMPLER sampler MIN_LOD foo";
145
146 Parser parser;
147 Result r = parser.Parse(in);
148 ASSERT_FALSE(r.IsSuccess());
149 EXPECT_EQ("1: invalid token when looking for MIN_LOD value", r.Error());
150 }
151
TEST_F(AmberScriptParserTest,SamplerInvalidMaxLod)152 TEST_F(AmberScriptParserTest, SamplerInvalidMaxLod) {
153 std::string in = "SAMPLER sampler MAX_LOD foo";
154
155 Parser parser;
156 Result r = parser.Parse(in);
157 ASSERT_FALSE(r.IsSuccess());
158 EXPECT_EQ("1: invalid token when looking for MAX_LOD value", r.Error());
159 }
160
TEST_F(AmberScriptParserTest,SamplerMaxLodSmallerThanMinLod)161 TEST_F(AmberScriptParserTest, SamplerMaxLodSmallerThanMinLod) {
162 std::string in = "SAMPLER sampler MIN_LOD 2.0 MAX_LOD 1.0";
163
164 Parser parser;
165 Result r = parser.Parse(in);
166 ASSERT_FALSE(r.IsSuccess());
167 EXPECT_EQ("1: max LOD needs to be greater than or equal to min LOD",
168 r.Error());
169 }
170
TEST_F(AmberScriptParserTest,SamplerUnnormalizedCoordsSetsLod)171 TEST_F(AmberScriptParserTest, SamplerUnnormalizedCoordsSetsLod) {
172 std::string in = R"(
173 SAMPLER sampler \
174 MIN_LOD 2.0 \
175 MAX_LOD 3.0 \
176 UNNORMALIZED_COORDS
177 )";
178
179 Parser parser;
180 Result r = parser.Parse(in);
181 ASSERT_TRUE(r.IsSuccess());
182 auto script = parser.GetScript();
183 const auto& samplers = script->GetSamplers();
184 ASSERT_EQ(1U, samplers.size());
185
186 ASSERT_TRUE(samplers[0] != nullptr);
187 EXPECT_EQ("sampler", samplers[0]->GetName());
188
189 auto* sampler = samplers[0].get();
190 EXPECT_EQ(0.0f, sampler->GetMinLOD());
191 EXPECT_EQ(0.0f, sampler->GetMaxLOD());
192 }
193
194 } // namespace amberscript
195 } // namespace amber
196