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