1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // ShCompile_test.cpp
7 // Test the sh::Compile interface with different parameters.
8 //
9
10 #include <clocale>
11 #include "GLSLANG/ShaderLang.h"
12 #include "angle_gl.h"
13 #include "common/angleutils.h"
14 #include "common/platform.h"
15 #include "gtest/gtest.h"
16
17 class ShCompileTest : public testing::Test
18 {
19 public:
ShCompileTest()20 ShCompileTest() {}
21
22 protected:
SetUp()23 void SetUp() override
24 {
25 sh::InitBuiltInResources(&mResources);
26 mCompiler = sh::ConstructCompiler(GL_FRAGMENT_SHADER, SH_WEBGL_SPEC,
27 SH_GLSL_COMPATIBILITY_OUTPUT, &mResources);
28 ASSERT_TRUE(mCompiler != nullptr) << "Compiler could not be constructed.";
29 }
30
TearDown()31 void TearDown() override
32 {
33 if (mCompiler)
34 {
35 sh::Destruct(mCompiler);
36 mCompiler = nullptr;
37 }
38 }
39
testCompile(const char ** shaderStrings,int stringCount,bool expectation)40 void testCompile(const char **shaderStrings, int stringCount, bool expectation)
41 {
42 ShCompileOptions options = SH_OBJECT_CODE | SH_VARIABLES | SH_INIT_OUTPUT_VARIABLES;
43 bool success = sh::Compile(mCompiler, shaderStrings, stringCount, options);
44 const std::string &compileLog = sh::GetInfoLog(mCompiler);
45 EXPECT_EQ(expectation, success) << compileLog;
46 }
47
48 ShBuiltInResources mResources;
49
50 class ScopedRestoreDefaultLocale : angle::NonCopyable
51 {
52 public:
53 ScopedRestoreDefaultLocale();
54 ~ScopedRestoreDefaultLocale();
55
56 private:
57 std::locale defaultLocale;
58 };
59
60 public:
61 ShHandle mCompiler;
62 };
63
ScopedRestoreDefaultLocale()64 ShCompileTest::ScopedRestoreDefaultLocale::ScopedRestoreDefaultLocale()
65 {
66 defaultLocale = std::locale();
67 }
68
~ScopedRestoreDefaultLocale()69 ShCompileTest::ScopedRestoreDefaultLocale::~ScopedRestoreDefaultLocale()
70 {
71 std::locale::global(defaultLocale);
72 }
73
74 class ShCompileComputeTest : public ShCompileTest
75 {
76 public:
ShCompileComputeTest()77 ShCompileComputeTest() {}
78
79 protected:
SetUp()80 void SetUp() override
81 {
82 sh::InitBuiltInResources(&mResources);
83 mCompiler = sh::ConstructCompiler(GL_COMPUTE_SHADER, SH_WEBGL3_SPEC,
84 SH_GLSL_COMPATIBILITY_OUTPUT, &mResources);
85 ASSERT_TRUE(mCompiler != nullptr) << "Compiler could not be constructed.";
86 }
87 };
88
89 // Test calling sh::Compile with compute shader source string.
TEST_F(ShCompileComputeTest,ComputeShaderString)90 TEST_F(ShCompileComputeTest, ComputeShaderString)
91 {
92 constexpr char kComputeShaderString[] =
93 R"(#version 310 es
94 layout(local_size_x=1) in;
95 void main()
96 {
97 })";
98
99 const char *shaderStrings[] = {kComputeShaderString};
100
101 testCompile(shaderStrings, 1, true);
102 }
103
104 // Test calling sh::Compile with more than one shader source string.
TEST_F(ShCompileTest,MultipleShaderStrings)105 TEST_F(ShCompileTest, MultipleShaderStrings)
106 {
107 const std::string &shaderString1 =
108 "precision mediump float;\n"
109 "void main() {\n";
110 const std::string &shaderString2 =
111 " gl_FragColor = vec4(0.0);\n"
112 "}";
113
114 const char *shaderStrings[] = {shaderString1.c_str(), shaderString2.c_str()};
115
116 testCompile(shaderStrings, 2, true);
117 }
118
119 // Test calling sh::Compile with a tokens split into different shader source strings.
TEST_F(ShCompileTest,TokensSplitInShaderStrings)120 TEST_F(ShCompileTest, TokensSplitInShaderStrings)
121 {
122 const std::string &shaderString1 =
123 "precision mediump float;\n"
124 "void ma";
125 const std::string &shaderString2 =
126 "in() {\n"
127 "#i";
128 const std::string &shaderString3 =
129 "f 1\n"
130 " gl_FragColor = vec4(0.0);\n"
131 "#endif\n"
132 "}";
133
134 const char *shaderStrings[] = {shaderString1.c_str(), shaderString2.c_str(),
135 shaderString3.c_str()};
136
137 testCompile(shaderStrings, 3, true);
138 }
139
140 // Parsing floats in shaders can run afoul of locale settings.
141 // Eg. in de_DE, `strtof("1.9")` will yield `1.0f`. (It's expecting "1,9")
TEST_F(ShCompileTest,DecimalSepLocale)142 TEST_F(ShCompileTest, DecimalSepLocale)
143 {
144 // Locale names are platform dependent, add platform-specific names of locales to be tested here
145 const std::string availableLocales[] = {
146 "de_DE",
147 "de-DE",
148 "de_DE.UTF-8",
149 "de_DE.ISO8859-1",
150 "de_DE.ISO8859-15",
151 "de_DE@euro",
152 "de_DE.88591",
153 "de_DE.88591.en",
154 "de_DE.iso88591",
155 "de_DE.ISO-8859-1",
156 "de_DE.ISO_8859-1",
157 "de_DE.iso885915",
158 "de_DE.ISO-8859-15",
159 "de_DE.ISO_8859-15",
160 "de_DE.8859-15",
161 "de_DE.8859-15@euro",
162 #if !defined(_WIN32)
163 // TODO(https://crbug.com/972372): Add this test back on Windows once the
164 // CRT no longer throws on code page sections ('ISO-8859-15@euro') that
165 // are >= 16 characters long.
166 "de_DE.ISO-8859-15@euro",
167 #endif
168 "de_DE.UTF-8@euro",
169 "de_DE.utf8",
170 "German_germany",
171 "German_Germany",
172 "German_Germany.1252",
173 "German_Germany.UTF-8",
174 "German",
175 // One ubuntu tester doesn't have a german locale, but da_DK.utf8 has similar float
176 // representation
177 "da_DK.utf8"
178 };
179
180 const auto localeExists = [](const std::string name) {
181 return bool(setlocale(LC_ALL, name.c_str()));
182 };
183
184 const char kSource[] = R"(
185 void main()
186 {
187 gl_FragColor = vec4(1.9);
188 })";
189 const char *parts[] = {kSource};
190
191 int testedLocales = 0;
192
193 // Ensure the locale is reset after the test runs.
194 ScopedRestoreDefaultLocale restoreLocale;
195
196 for (const std::string &locale : availableLocales)
197 {
198 // If the locale doesn't exist on the testing platform, the locale constructor will fail,
199 // throwing an exception
200 // We use setlocale() (through localeExists) to test whether a locale
201 // exists before calling the locale constructor
202 if (localeExists(locale))
203 {
204 std::locale localizedLoc(locale);
205
206 // std::locale::global() must be used instead of setlocale() to affect new streams'
207 // default locale
208 std::locale::global(std::locale::classic());
209 sh::Compile(mCompiler, parts, 1, SH_OBJECT_CODE);
210 std::string referenceOut = sh::GetObjectCode(mCompiler);
211 EXPECT_NE(referenceOut.find("1.9"), std::string::npos)
212 << "float formatted incorrectly with classic locale";
213
214 sh::ClearResults(mCompiler);
215
216 std::locale::global(localizedLoc);
217 sh::Compile(mCompiler, parts, 1, SH_OBJECT_CODE);
218 std::string localizedOut = sh::GetObjectCode(mCompiler);
219 EXPECT_NE(localizedOut.find("1.9"), std::string::npos)
220 << "float formatted incorrectly with locale (" << localizedLoc.name() << ") set";
221
222 ASSERT_EQ(referenceOut, localizedOut)
223 << "different output with locale (" << localizedLoc.name() << ") set";
224
225 testedLocales++;
226 }
227 }
228 }
229
230 // For testing Desktop GL Shaders
231 class ShCompileDesktopGLTest : public ShCompileTest
232 {
233 public:
ShCompileDesktopGLTest()234 ShCompileDesktopGLTest() {}
235
236 protected:
SetUp()237 void SetUp() override
238 {
239 sh::InitBuiltInResources(&mResources);
240 mCompiler = sh::ConstructCompiler(GL_FRAGMENT_SHADER, SH_GL_COMPATIBILITY_SPEC,
241 SH_GLSL_330_CORE_OUTPUT, &mResources);
242 ASSERT_TRUE(mCompiler != nullptr) << "Compiler could not be constructed.";
243 }
244 };
245
246 // Test calling sh::Compile with fragment shader source string
TEST_F(ShCompileDesktopGLTest,DesktopGLString)247 TEST_F(ShCompileDesktopGLTest, DesktopGLString)
248 {
249 constexpr char kFragmentShaderString[] =
250 R"(#version 330
251 void main()
252 {
253 })";
254
255 const char *shaderStrings[] = {kFragmentShaderString};
256
257 testCompile(shaderStrings, 1, true);
258 }
259
260 // Test calling sh::Compile with core version
TEST_F(ShCompileDesktopGLTest,FragmentShaderCoreVersion)261 TEST_F(ShCompileDesktopGLTest, FragmentShaderCoreVersion)
262 {
263 constexpr char kFragmentShaderString[] =
264 R"(#version 330 core
265 void main()
266 {
267 })";
268
269 const char *shaderStrings[] = {kFragmentShaderString};
270
271 testCompile(shaderStrings, 1, true);
272 }
273
274 // Implicit conversions in basic operations
TEST_F(ShCompileDesktopGLTest,ImplicitConversionBasicOperation)275 TEST_F(ShCompileDesktopGLTest, ImplicitConversionBasicOperation)
276 {
277 constexpr char kFragmentShaderString[] =
278 R"(#version 330 core
279 void main()
280 {
281 //float a = 1 + 1.5;
282 //float b = 1 - 1.5;
283 //float c = 1 * 1.5;
284 //float d = 1 / 1.5;
285 //float e = 1.5 + 1;
286 //float f = 1.5 - 1;
287 float g = 1.5 * 1;
288 //float h = 1.5 / 1;
289 })";
290
291 const char *shaderStrings[] = {kFragmentShaderString};
292
293 testCompile(shaderStrings, 1, true);
294 }
295
296 // Implicit conversions when assigning
TEST_F(ShCompileDesktopGLTest,ImplicitConversionAssign)297 TEST_F(ShCompileDesktopGLTest, ImplicitConversionAssign)
298 {
299 constexpr char kFragmentShaderString[] =
300 R"(#version 330 core
301 void main()
302 {
303 float a = 1;
304 uint b = 2u;
305 a = b;
306 a += b;
307 a -= b;
308 a *= b;
309 a /= b;
310 })";
311
312 const char *shaderStrings[] = {kFragmentShaderString};
313
314 testCompile(shaderStrings, 1, true);
315 }
316
317 // Implicit conversions for vectors
TEST_F(ShCompileDesktopGLTest,ImplicitConversionVector)318 TEST_F(ShCompileDesktopGLTest, ImplicitConversionVector)
319 {
320 constexpr char kFragmentShaderString[] =
321 R"(#version 330 core
322 void main()
323 {
324 vec3 a;
325 ivec3 b = ivec3(1, 1, 1);
326 a = b;
327 })";
328
329 const char *shaderStrings[] = {kFragmentShaderString};
330
331 testCompile(shaderStrings, 1, true);
332 }
333
334 // Implicit conversions should not convert between ints and uints
TEST_F(ShCompileDesktopGLTest,ImplicitConversionAssignFailed)335 TEST_F(ShCompileDesktopGLTest, ImplicitConversionAssignFailed)
336 {
337 constexpr char kFragmentShaderString[] =
338 R"(#version 330 core
339 void main()
340 {
341 int a = 1;
342 uint b = 2;
343 a = b;
344 })";
345
346 const char *shaderStrings[] = {kFragmentShaderString};
347
348 testCompile(shaderStrings, 1, false);
349 }
350
351 // GL shaders use implicit conversions between types
352 // Testing internal implicit conversions
TEST_F(ShCompileDesktopGLTest,ImplicitConversionFunction)353 TEST_F(ShCompileDesktopGLTest, ImplicitConversionFunction)
354 {
355 constexpr char kFragmentShaderString[] =
356 R"(#version 330 core
357 void main()
358 {
359 float cosTheta = clamp(0.5,0,1);
360 float exp = pow(0.5,2);
361 })";
362
363 const char *shaderStrings[] = {kFragmentShaderString};
364
365 testCompile(shaderStrings, 1, true);
366 }
367