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   EXPECT_EQ(false, sampler->GetCompareEnable());
49   EXPECT_EQ(CompareOp::kNever, sampler->GetCompareOp());
50 }
51 
TEST_F(AmberScriptParserTest,SamplerCustomValues)52 TEST_F(AmberScriptParserTest, SamplerCustomValues) {
53   std::string in = R"(
54 SAMPLER sampler MAG_FILTER linear \
55   MIN_FILTER linear \
56   ADDRESS_MODE_U clamp_to_edge \
57   ADDRESS_MODE_V clamp_to_border \
58   ADDRESS_MODE_W mirrored_repeat \
59   BORDER_COLOR float_opaque_white \
60   MIN_LOD 2.5 \
61   MAX_LOD 5.0 \
62   NORMALIZED_COORDS \
63   COMPARE on \
64   COMPARE_OP greater)";
65 
66   Parser parser;
67   Result r = parser.Parse(in);
68   ASSERT_TRUE(r.IsSuccess()) << r.Error();
69 
70   auto script = parser.GetScript();
71   const auto& samplers = script->GetSamplers();
72   ASSERT_EQ(1U, samplers.size());
73 
74   ASSERT_TRUE(samplers[0] != nullptr);
75   EXPECT_EQ("sampler", samplers[0]->GetName());
76 
77   auto* sampler = samplers[0].get();
78   EXPECT_EQ(FilterType::kLinear, sampler->GetMagFilter());
79   EXPECT_EQ(FilterType::kLinear, sampler->GetMinFilter());
80   EXPECT_EQ(FilterType::kNearest, sampler->GetMipmapMode());
81   EXPECT_EQ(AddressMode::kClampToEdge, sampler->GetAddressModeU());
82   EXPECT_EQ(AddressMode::kClampToBorder, sampler->GetAddressModeV());
83   EXPECT_EQ(AddressMode::kMirroredRepeat, sampler->GetAddressModeW());
84   EXPECT_EQ(BorderColor::kFloatOpaqueWhite, sampler->GetBorderColor());
85   EXPECT_EQ(2.5, sampler->GetMinLOD());
86   EXPECT_EQ(5.0, sampler->GetMaxLOD());
87   EXPECT_EQ(true, sampler->GetNormalizedCoords());
88   EXPECT_EQ(true, sampler->GetCompareEnable());
89   EXPECT_EQ(CompareOp::kGreater, sampler->GetCompareOp());
90 }
91 
TEST_F(AmberScriptParserTest,SamplerUnexpectedParameter)92 TEST_F(AmberScriptParserTest, SamplerUnexpectedParameter) {
93   std::string in = R"(
94 SAMPLER sampler MAG_FILTER linear \
95   FOO \
96   ADDRESS_MODE_U clamp_to_edge)";
97 
98   Parser parser;
99   Result r = parser.Parse(in);
100   ASSERT_FALSE(r.IsSuccess());
101   EXPECT_EQ("3: unexpected sampler parameter FOO", r.Error());
102 }
103 
TEST_F(AmberScriptParserTest,SamplerInvalidMagFilter)104 TEST_F(AmberScriptParserTest, SamplerInvalidMagFilter) {
105   std::string in = "SAMPLER sampler MAG_FILTER foo";
106 
107   Parser parser;
108   Result r = parser.Parse(in);
109   ASSERT_FALSE(r.IsSuccess());
110   EXPECT_EQ("1: invalid MAG_FILTER value foo", r.Error());
111 }
112 
TEST_F(AmberScriptParserTest,SamplerInvalidMinFilter)113 TEST_F(AmberScriptParserTest, SamplerInvalidMinFilter) {
114   std::string in = "SAMPLER sampler MIN_FILTER foo";
115 
116   Parser parser;
117   Result r = parser.Parse(in);
118   ASSERT_FALSE(r.IsSuccess());
119   EXPECT_EQ("1: invalid MIN_FILTER value foo", r.Error());
120 }
121 
TEST_F(AmberScriptParserTest,SamplerInvalidAddressModeU)122 TEST_F(AmberScriptParserTest, SamplerInvalidAddressModeU) {
123   std::string in = "SAMPLER sampler ADDRESS_MODE_U foo";
124 
125   Parser parser;
126   Result r = parser.Parse(in);
127   ASSERT_FALSE(r.IsSuccess());
128   EXPECT_EQ("1: invalid ADDRESS_MODE_U value foo", r.Error());
129 }
130 
TEST_F(AmberScriptParserTest,SamplerInvalidAddressModeV)131 TEST_F(AmberScriptParserTest, SamplerInvalidAddressModeV) {
132   std::string in = "SAMPLER sampler ADDRESS_MODE_V foo";
133 
134   Parser parser;
135   Result r = parser.Parse(in);
136   ASSERT_FALSE(r.IsSuccess());
137   EXPECT_EQ("1: invalid ADDRESS_MODE_V value foo", r.Error());
138 }
139 
TEST_F(AmberScriptParserTest,SamplerInvalidBorderColor)140 TEST_F(AmberScriptParserTest, SamplerInvalidBorderColor) {
141   std::string in = "SAMPLER sampler BORDER_COLOR foo";
142 
143   Parser parser;
144   Result r = parser.Parse(in);
145   ASSERT_FALSE(r.IsSuccess());
146   EXPECT_EQ("1: invalid BORDER_COLOR value foo", r.Error());
147 }
148 
TEST_F(AmberScriptParserTest,SamplerInvalidMinLod)149 TEST_F(AmberScriptParserTest, SamplerInvalidMinLod) {
150   std::string in = "SAMPLER sampler MIN_LOD foo";
151 
152   Parser parser;
153   Result r = parser.Parse(in);
154   ASSERT_FALSE(r.IsSuccess());
155   EXPECT_EQ("1: invalid token when looking for MIN_LOD value", r.Error());
156 }
157 
TEST_F(AmberScriptParserTest,SamplerInvalidMaxLod)158 TEST_F(AmberScriptParserTest, SamplerInvalidMaxLod) {
159   std::string in = "SAMPLER sampler MAX_LOD foo";
160 
161   Parser parser;
162   Result r = parser.Parse(in);
163   ASSERT_FALSE(r.IsSuccess());
164   EXPECT_EQ("1: invalid token when looking for MAX_LOD value", r.Error());
165 }
166 
TEST_F(AmberScriptParserTest,SamplerMaxLodSmallerThanMinLod)167 TEST_F(AmberScriptParserTest, SamplerMaxLodSmallerThanMinLod) {
168   std::string in = "SAMPLER sampler MIN_LOD 2.0 MAX_LOD 1.0";
169 
170   Parser parser;
171   Result r = parser.Parse(in);
172   ASSERT_FALSE(r.IsSuccess());
173   EXPECT_EQ("1: max LOD needs to be greater than or equal to min LOD",
174             r.Error());
175 }
176 
TEST_F(AmberScriptParserTest,SamplerUnnormalizedCoordsSetsLod)177 TEST_F(AmberScriptParserTest, SamplerUnnormalizedCoordsSetsLod) {
178   std::string in = R"(
179 SAMPLER sampler \
180   MIN_LOD 2.0 \
181   MAX_LOD 3.0 \
182   UNNORMALIZED_COORDS
183 )";
184 
185   Parser parser;
186   Result r = parser.Parse(in);
187   ASSERT_TRUE(r.IsSuccess());
188   auto script = parser.GetScript();
189   const auto& samplers = script->GetSamplers();
190   ASSERT_EQ(1U, samplers.size());
191 
192   ASSERT_TRUE(samplers[0] != nullptr);
193   EXPECT_EQ("sampler", samplers[0]->GetName());
194 
195   auto* sampler = samplers[0].get();
196   EXPECT_EQ(0.0f, sampler->GetMinLOD());
197   EXPECT_EQ(0.0f, sampler->GetMaxLOD());
198 }
199 
200 }  // namespace amberscript
201 }  // namespace amber
202