Hash :
f3d5dac3
Author :
Date :
2021-08-23T17:25:15
Vulkan: SPIR-V Gen: Drop dependency to glslang
The SPIR-V gen path is now made default. Compilation through glslang is
still supported for debugging, and is enabled on the GLSL* end2end tests
for smoke testing. On release builds, glslang is not supported.
To test with glslang, add the following gn arg (only necessary if dcheck
is disabled):
angle_enable_spirv_gen_through_glslang = true
Then enable the generateSPIRVThroughGlslang feature. This can be done
by setting an environment variable:
ANGLE_FEATURE_OVERRIDES_ENABLED=generateSPIRVThroughGlslang ./angle_deqp_gles2_tests
Binary size saving:
- 1.3MB on Linux (SPIR-V gen itself: 240KB)
- 730KB on Android (SPIR-V gen itself: 140KB)
Perf tests:
- LinkProgramBenchmark.Run/vulkan_compile_single_thread
* Through glslang:
truncated mean: 1287033.36
* Direct SPIR-V Gen:
truncated mean: 244495.91 (~80% reduction)
- LinkProgramBenchmark.Run/vulkan_compile_multi_thread
* Through glslang:
truncated mean: 4565894.83
* Direct SPIR-V Gen:
truncated mean: 1158164.10 (~75% reduction)
Bug: angleproject:4889
Bug: angleproject:6210
Change-Id: I486342702977c8114e90073b97183aba115a8b2d
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3115140
Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Tim Van Patten <timvp@google.com>
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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
//
// Copyright 2021 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.
//
// Precise_test.cpp:
// Test that precise produces the right number of NoContraction decorations in the generated
// SPIR-V.
//
#include "GLSLANG/ShaderLang.h"
#include "angle_gl.h"
#include "common/spirv/spirv_instruction_parser_autogen.h"
#include "gtest/gtest.h"
namespace spirv = angle::spirv;
namespace
{
class PreciseTest : public testing::TestWithParam<bool>
{
public:
void SetUp() override
{
std::map<ShShaderOutput, std::string> shaderOutputList = {
{SH_SPIRV_VULKAN_OUTPUT, "SH_SPIRV_VULKAN_OUTPUT"}};
Initialize(shaderOutputList);
}
void TearDown() override
{
for (auto shaderOutputType : mShaderOutputList)
{
DestroyCompiler(shaderOutputType.first);
}
}
void Initialize(std::map<ShShaderOutput, std::string> &shaderOutputList)
{
mShaderOutputList = std::move(shaderOutputList);
for (auto shaderOutputType : mShaderOutputList)
{
sh::InitBuiltInResources(&mResourceList[shaderOutputType.first]);
mCompilerList[shaderOutputType.first] = nullptr;
}
}
void DestroyCompiler(ShShaderOutput shaderOutputType)
{
if (mCompilerList[shaderOutputType])
{
sh::Destruct(mCompilerList[shaderOutputType]);
mCompilerList[shaderOutputType] = nullptr;
}
}
void InitializeCompiler()
{
for (auto shaderOutputType : mShaderOutputList)
{
InitializeCompiler(shaderOutputType.first);
}
}
void InitializeCompiler(ShShaderOutput shaderOutputType)
{
DestroyCompiler(shaderOutputType);
mCompilerList[shaderOutputType] = sh::ConstructCompiler(
GL_VERTEX_SHADER, SH_GLES3_2_SPEC, shaderOutputType, &mResourceList[shaderOutputType]);
ASSERT_TRUE(mCompilerList[shaderOutputType] != nullptr)
<< "Compiler for " << mShaderOutputList[shaderOutputType]
<< " could not be constructed.";
}
testing::AssertionResult TestShaderCompile(ShShaderOutput shaderOutputType,
const char *shaderSource)
{
const char *shaderStrings[] = {shaderSource};
const ShCompileOptions options = SH_VARIABLES | SH_OBJECT_CODE;
bool success = sh::Compile(mCompilerList[shaderOutputType], shaderStrings, 1, options);
if (success)
{
return ::testing::AssertionSuccess()
<< "Compilation success(" << mShaderOutputList[shaderOutputType] << ")";
}
return ::testing::AssertionFailure() << sh::GetInfoLog(mCompilerList[shaderOutputType]);
}
void TestShaderCompile(const char *shaderSource, size_t expectedNoContractionDecorationCount)
{
for (auto shaderOutputType : mShaderOutputList)
{
EXPECT_TRUE(TestShaderCompile(shaderOutputType.first, shaderSource));
const spirv::Blob &blob =
sh::GetObjectBinaryBlob(mCompilerList[shaderOutputType.first]);
ValidateDecorations(blob, expectedNoContractionDecorationCount);
}
}
void ValidateDecorations(const spirv::Blob &blob, size_t expectedNoContractionDecorationCount);
private:
std::map<ShShaderOutput, std::string> mShaderOutputList;
std::map<ShShaderOutput, ShHandle> mCompilerList;
std::map<ShShaderOutput, ShBuiltInResources> mResourceList;
};
// Parse the SPIR-V and verify that there are as many NoContraction decorations as expected.
void PreciseTest::ValidateDecorations(const spirv::Blob &blob,
size_t expectedNoContractionDecorationCount)
{
size_t currentWord = spirv::kHeaderIndexInstructions;
size_t noContractionDecorationCount = 0;
while (currentWord < blob.size())
{
uint32_t wordCount;
spv::Op opCode;
const uint32_t *instruction = &blob[currentWord];
spirv::GetInstructionOpAndLength(instruction, &opCode, &wordCount);
currentWord += wordCount;
// Early out when the decorations section is visited.
if (opCode == spv::OpTypeVoid || opCode == spv::OpTypeInt || opCode == spv::OpTypeFloat ||
opCode == spv::OpTypeBool)
{
break;
}
if (opCode == spv::OpMemberDecorate)
{
spirv::IdRef type;
spirv::LiteralInteger member;
spv::Decoration decoration;
spirv::ParseMemberDecorate(instruction, &type, &member, &decoration, nullptr);
// NoContraction should be applied to arithmetic instructions, and should not be seen on
// block members.
EXPECT_NE(decoration, spv::DecorationNoContraction);
}
else if (opCode == spv::OpDecorate)
{
spirv::IdRef target;
spv::Decoration decoration;
spirv::ParseDecorate(instruction, &target, &decoration, nullptr);
if (decoration == spv::DecorationNoContraction)
{
++noContractionDecorationCount;
}
}
}
EXPECT_EQ(noContractionDecorationCount, expectedNoContractionDecorationCount);
}
// Test that precise on a local variable works.
TEST_F(PreciseTest, LocalVariable)
{
constexpr char kVS[] = R"(#version 320 es
uniform float u;
void main()
{
float f1 = u, f2 = u; // f1 is precise, but f2 isn't.
f1 += 1.0; // NoContraction
f2 += 1.0;
float f3 = f1 * f1; // NoContraction
f3 /= 2.0; // NoContraction
int i1 = int(f3); // i1 is precise
++i1; // NoContraction
--i1; // NoContraction
i1++; // NoContraction
i1--; // NoContraction
int i2 = i1 % 3;
f2 -= float(i2);
precise float p = float(i1) / 1.5; // NoContraction
gl_Position = vec4(p, f2, 0, 0);
})";
InitializeCompiler();
TestShaderCompile(kVS, 8);
}
// Test that precise on gl_Position works.
TEST_F(PreciseTest, Position)
{
constexpr char kVS[] = R"(#version 320 es
uniform float u;
out float o;
precise gl_Position;
void main()
{
mat4 m1 = mat4(u); // m1 is precise, even if not all components are used to determine the
// gl_Position.
vec4 v1 = vec4(u); // v1 is precise
vec4 v2 = m1 * v1;
v1 *= m1; // NoContraction
m1 *= m1; // NoContraction
m1 *= u; // NoContraction
v1 *= u; // NoContraction
float f1 = dot(v1, v1);
float f2 = dot(v1, v1); // NoContraction
gl_Position = vec4(m1[0][0], v1[0], f2, 0);
o = f1;
})";
InitializeCompiler();
TestShaderCompile(kVS, 5);
}
// Test that precise on struct member works.
TEST_F(PreciseTest, StructMember)
{
constexpr char kVS[] = R"(#version 320 es
uniform float u;
struct S1
{
precise float f;
int i;
};
struct S2
{
float f;
};
struct S3
{
precise uint u;
S1 s1[2];
precise S2 s2;
};
layout(std430) buffer B
{
S3 o1;
S3 o2;
S3 o3;
};
void main()
{
S2 a = S2(u), b = S2(u), c = S2(u); // a and c are precise
++a.f; // NoContraction
o1.s2 = a;
c.f += a.f; // NoContraction
o2.s1[0].i = int(a.f);
o2.s1[0].i *= 2;
o2.s1[0].i /= int(b.f);
o1.s1[1].i = int(u);
--o1.s1[1].i; // NoContraction
o2.s1[0].f = c.f;
o3.u = o1.u + uint(o1.s1[1].i); // NoContraction
})";
InitializeCompiler();
TestShaderCompile(kVS, 4);
}
// Test that precise on function parameters and return value works.
TEST_F(PreciseTest, Functions)
{
constexpr char kVS[] = R"(#version 320 es
uniform float u;
struct S1
{
float f;
int i;
};
precise float f1(S1 s, out int io)
{
float f = s.f; // f is precise
f *= float(s.i); // NoContraction
io = s.i;
++io;
return s.f / f; // NoContraction
}
void f2(S1 s, precise out S1 so)
{
float f = s.f; // f is precise
f /= float(s.i); // NoContraction
int i = s.i; // i is precise
++i; // NoContraction
so = S1(f, i);
}
void main()
{
precise S1 s1;
S1 s2;
int i;
float f = f1(s1, i); // f1's return value being precise doesn't affect f
f2(s1, s2); // f2's out parameter being precise doesn't affect s2
i /= 2;
f *= 2.0;
s2.f += float(s2.i);
gl_Position = vec4(s1.f);
})";
InitializeCompiler();
TestShaderCompile(kVS, 4);
}
// Test that struct constructors only apply precise to the precise fields.
TEST_F(PreciseTest, StructConstructor)
{
constexpr char kVS[] = R"(#version 320 es
uniform float u;
struct S1
{
precise float f;
int i;
precise vec4 v;
mat4 m;
};
void main()
{
float f = u; // f is precise
int i = int(u);
vec4 v1 = vec4(u); // v1 is precise
vec4 v2 = vec4(u);
f += 1.0; // NoContraction
i--;
i--;
v1 *= 2.0; // NoContraction
v1 *= 2.0; // NoContraction
v1 *= 2.0; // NoContraction
v1 *= 2.0; // NoContraction
v2 /= 3.0;
v2 /= 3.0;
v2 /= 3.0;
v2 /= 3.0;
v2 /= 3.0;
v2 /= 3.0;
v2 /= 3.0;
v2 /= 3.0;
v2 /= 3.0;
S1 s = S1(f, i, v1, mat4(v2, v2, v2, v2));
gl_Position = vec4(s.f, float(s.i), s.v[0], s.m[0][0]);
})";
InitializeCompiler();
TestShaderCompile(kVS, 5);
}
// Test that function call arguments become precise when the return value is assigned to a precise
// object.
TEST_F(PreciseTest, FunctionParams)
{
constexpr char kVS[] = R"(#version 320 es
uniform float u;
struct S1
{
precise float f;
int i;
precise vec4 v;
mat4 m;
};
S1 func(float f, int i, vec4 v, mat4 m)
{
m /= f;
--i;
v *= m;
return S1(f, i, v, m);
}
void main()
{
float f = u; // f is precise
int i = int(u); // i is precise
vec4 v1 = vec4(u); // v1 is precise
vec4 v2 = vec4(u); // v2 is precise
f += 1.0; // NoContraction
i--; // NoContraction
i--; // NoContraction
v1 *= 2.0; // NoContraction
v1 *= 2.0; // NoContraction
v1 *= 2.0; // NoContraction
v1 *= 2.0; // NoContraction
v2 /= 3.0; // NoContraction
v2 /= 3.0; // NoContraction
v2 /= 3.0; // NoContraction
v2 /= 3.0; // NoContraction
v2 /= 3.0; // NoContraction
v2 /= 3.0; // NoContraction
v2 /= 3.0; // NoContraction
v2 /= 3.0; // NoContraction
v2 /= 3.0; // NoContraction
// s.f and s.v1 are precise, but to calculate them, all parameters of the function must be made
// precise.
S1 s = func(f, i, v1, mat4(v2, v2, v2, v2));
gl_Position = vec4(s.f, float(s.i), s.v[0], s.m[0][0]);
})";
InitializeCompiler();
TestShaderCompile(kVS, 16);
}
} // anonymous namespace