Hash :
6f80f0f0
Author :
Date :
2022-08-06T02:29:19
Translator: Clean up the compile flag passing interface Historically, compile flags were sent to the translator as a bitmask. Recently, we were getting close to running out of bits. Additionally, direct-to-metal work had started to introduce constants to be passed to the translator, which were misplaced in ShBuiltInResources and Caps. Recent work on Pixel Local Storage adds even more constants, aggravating the situation. In this change, the interface to passing compile flags is reworked. A struct is passed (instead of a bitmask) that has one bit for each flag. This can be indefinitely extended. Additionally, the constants needed by metal and PLS are also placed in this struct. In turn, the backends can set these options directly, and don't have to hack them into Caps to further get hacked into ShBuiltInResources. Bug: angleproject:7559 Change-Id: If93f1e1b8818ad3a0ac708ab04ab93b4b397d114 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3812562 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Amirali Abdolrashidi <abdolrashidi@google.com> Commit-Queue: Amirali Abdolrashidi <abdolrashidi@google.com> Auto-Submit: 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 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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
//
// Copyright 2017 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.
//
// MSLOutput_test.cpp:
// Tests for MSL output.
//
#include <regex>
#include "GLSLANG/ShaderLang.h"
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "tests/test_utils/compiler_test.h"
using namespace sh;
class MSLVertexOutputTest : public MatchOutputCodeTest
{
public:
MSLVertexOutputTest() : MatchOutputCodeTest(GL_VERTEX_SHADER, SH_MSL_METAL_OUTPUT)
{
ShCompileOptions defaultCompileOptions = {};
defaultCompileOptions.variables = true;
setDefaultCompileOptions(defaultCompileOptions);
}
};
class MSLOutputTest : public MatchOutputCodeTest
{
public:
MSLOutputTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_MSL_METAL_OUTPUT)
{
ShCompileOptions defaultCompileOptions = {};
defaultCompileOptions.variables = true;
setDefaultCompileOptions(defaultCompileOptions);
}
};
// Test that having dynamic indexing of a vector inside the right hand side of logical or doesn't
// trigger asserts in MSL output.
TEST_F(MSLOutputTest, DynamicIndexingOfVectorOnRightSideOfLogicalOr)
{
const std::string &shaderString =
"#version 300 es\n"
"precision highp float;\n"
"out vec4 my_FragColor;\n"
"uniform int u1;\n"
"void main() {\n"
" bvec4 v = bvec4(true, true, true, false);\n"
" my_FragColor = vec4(v[u1 + 1] || v[u1]);\n"
"}\n";
compile(shaderString);
}
// Test that having an array constructor as a statement doesn't trigger an assert in MSL output.
TEST_F(MSLOutputTest, ArrayConstructorStatement)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
out vec4 outColor;
void main()
{
outColor = vec4(0.0, 0.0, 0.0, 1.0);
float[1](outColor[1]++);
})";
compile(shaderString);
}
// Test an array of arrays constructor as a statement.
TEST_F(MSLOutputTest, ArrayOfArraysStatement)
{
const std::string &shaderString =
R"(#version 310 es
precision mediump float;
out vec4 outColor;
void main()
{
outColor = vec4(0.0, 0.0, 0.0, 1.0);
float[2][2](float[2](outColor[1]++, 0.0), float[2](1.0, 2.0));
})";
compile(shaderString);
}
// Test dynamic indexing of a vector. This makes sure that helper functions added for dynamic
// indexing have correct data that subsequent traversal steps rely on.
TEST_F(MSLOutputTest, VectorDynamicIndexing)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
out vec4 outColor;
uniform int i;
void main()
{
vec4 foo = vec4(0.0, 0.0, 0.0, 1.0);
foo[i] = foo[i + 1];
outColor = foo;
})";
compile(shaderString);
}
// Test returning an array from a user-defined function. This makes sure that function symbols are
// changed consistently when the user-defined function is changed to have an array out parameter.
TEST_F(MSLOutputTest, ArrayReturnValue)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float u;
out vec4 outColor;
float[2] getArray(float f)
{
return float[2](f, f + 1.0);
}
void main()
{
float[2] arr = getArray(u);
outColor = vec4(arr[0], arr[1], 0.0, 1.0);
})";
compile(shaderString);
}
// Test that writing parameters without a name doesn't assert.
TEST_F(MSLOutputTest, ParameterWithNoName)
{
const std::string &shaderString =
R"(precision mediump float;
uniform vec4 v;
vec4 s(vec4)
{
return v;
}
void main()
{
gl_FragColor = s(v);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, Macro)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
#define FOO vec4
out vec4 outColor;
void main()
{
outColor = FOO(1.0, 2.0, 3.0, 4.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, UniformSimple)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
uniform float x;
void main()
{
outColor = vec4(x, x, x, x);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragmentOutSimple)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 2.0, 3.0, 4.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragmentOutIndirect1)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
void foo()
{
outColor = vec4(1.0, 2.0, 3.0, 4.0);
}
void bar()
{
foo();
}
void main()
{
bar();
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragmentOutIndirect2)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
void foo();
void bar()
{
foo();
}
void foo()
{
outColor = vec4(1.0, 2.0, 3.0, 4.0);
}
void main()
{
bar();
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragmentOutIndirect3)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
float foo(float x, float y)
{
outColor = vec4(x, y, 3.0, 4.0);
return 7.0;
}
float bar(float x)
{
return foo(x, 2.0);
}
float baz()
{
return 13.0;
}
float identity(float x)
{
return x;
}
void main()
{
identity(bar(baz()));
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, VertexInOut)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
in float in0;
out float out0;
void main()
{
out0 = in0;
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, SymbolSharing)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out vec4 outColor;
struct Foo {
float x;
float y;
};
void doFoo(Foo foo, float zw);
void doFoo(Foo foo, float zw)
{
foo.x = foo.y;
outColor = vec4(foo.x, foo.y, zw, zw);
}
void main()
{
Foo foo;
foo.x = 2.0;
foo.y = 2.0;
doFoo(foo, 3.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, StructDecl)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
out float out0;
struct Foo {
float value;
};
void main()
{
Foo foo;
out0 = foo.value;
}
)";
compile(shaderString);
}
TEST_F(MSLOutputTest, Structs)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
struct Foo {
float value;
};
out vec4 out0;
struct Bar {
Foo foo;
};
void go();
uniform UniInstance {
Bar bar;
float instance;
} uniInstance;
uniform UniGlobal {
Foo foo;
float global;
};
void main()
{
go();
}
struct Baz {
Bar bar;
} baz;
void go()
{
out0.x = baz.bar.foo.value;
out0.y = global;
out0.z = uniInstance.instance;
out0.w = 0.0;
}
)";
compile(shaderString);
}
TEST_F(MSLOutputTest, KeywordConflict)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
struct fragment {
float kernel;
} device;
struct Foo {
fragment frag;
} foo;
out float vertex;
float kernel;
float stage_in(float x)
{
return x;
}
void metal(float metal, float fragment);
void metal(float metal, float fragment)
{
vertex = metal * fragment * foo.frag.kernel;
}
void main()
{
metal(stage_in(stage_in(kernel * device.kernel)), foo.frag.kernel);
})";
compile(shaderString);
}
TEST_F(MSLVertexOutputTest, Vertex)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
void main()
{
gl_Position = vec4(1.0,1.0,1.0,1.0);
})";
compile(shaderString);
}
TEST_F(MSLVertexOutputTest, LastReturn)
{
const std::string &shaderString =
R"(#version 300 es
in highp vec4 a_position;
in highp vec4 a_coords;
out highp vec4 v_color;
void main (void)
{
gl_Position = a_position;
v_color = vec4(a_coords.xyz, 1.0);
return;
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, LastReturn)
{
const std::string &shaderString =
R"(#version 300 es
in mediump vec4 v_coords;
layout(location = 0) out mediump vec4 o_color;
void main (void)
{
o_color = vec4(v_coords.xyz, 1.0);
return;
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, FragColor)
{
const std::string &shaderString = R"(
void main ()
{
gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, MatrixIn)
{
const std::string &shaderString =
R"(#version 300 es
precision highp float;
in mat4 mat;
out float out0;
void main()
{
out0 = mat[0][0];
}
)";
compile(shaderString);
}
TEST_F(MSLOutputTest, WhileTrue)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
while (true)
{
break;
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, ForTrue)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
for (;true;)
{
break;
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, ForEmpty)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
for (;;)
{
break;
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, ForComplex)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
for (int i = 0, j = 2; i < j; ++i) {
if (i == 0) continue;
if (i == 42) break;
my_FragColor.x += float(i);
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, ForSymbol)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
bool cond = true;
for (;cond;)
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
cond = false;
}
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, DoWhileSymbol)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
bool cond = false;
do
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
} while (cond);
})";
compile(shaderString);
}
TEST_F(MSLOutputTest, AnonymousStruct)
{
const std::string &shaderString =
R"(
precision mediump float;
struct { vec4 v; } anonStruct;
void main() {
anonStruct.v = vec4(0.0,1.0,0.0,1.0);
gl_FragColor = anonStruct.v;
})";
compile(shaderString);
// TODO(anglebug.com/6395): This success condition is expected to fail now.
// When WebKit build is able to run the tests, this should be changed to something else.
// ASSERT_TRUE(foundInCode(SH_MSL_METAL_OUTPUT, "__unnamed"));
}