Path: blob/main_old/src/tests/compiler_tests/MSLOutput_test.cpp
1693 views
//1// Copyright 2017 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// MSLOutput_test.cpp:6// Tests for MSL output.7//89#include <regex>10#include "GLSLANG/ShaderLang.h"11#include "angle_gl.h"12#include "gtest/gtest.h"13#include "tests/test_utils/compiler_test.h"1415using namespace sh;1617class MSLVertexOutputTest : public MatchOutputCodeTest18{19public:20MSLVertexOutputTest() : MatchOutputCodeTest(GL_VERTEX_SHADER, 0, SH_MSL_METAL_OUTPUT) {}21};2223class MSLOutputTest : public MatchOutputCodeTest24{25public:26MSLOutputTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, 0, SH_MSL_METAL_OUTPUT) {}27};2829// Test that having dynamic indexing of a vector inside the right hand side of logical or doesn't30// trigger asserts in MSL output.31TEST_F(MSLOutputTest, DynamicIndexingOfVectorOnRightSideOfLogicalOr)32{33const std::string &shaderString =34"#version 300 es\n"35"precision highp float;\n"36"out vec4 my_FragColor;\n"37"uniform int u1;\n"38"void main() {\n"39" bvec4 v = bvec4(true, true, true, false);\n"40" my_FragColor = vec4(v[u1 + 1] || v[u1]);\n"41"}\n";42compile(shaderString, SH_VARIABLES);43}4445// Test that having an array constructor as a statement doesn't trigger an assert in MSL output.46TEST_F(MSLOutputTest, ArrayConstructorStatement)47{48const std::string &shaderString =49R"(#version 300 es50precision mediump float;51out vec4 outColor;52void main()53{54outColor = vec4(0.0, 0.0, 0.0, 1.0);55float[1](outColor[1]++);56})";57compile(shaderString, SH_VARIABLES);58}5960// Test an array of arrays constructor as a statement.61TEST_F(MSLOutputTest, ArrayOfArraysStatement)62{63const std::string &shaderString =64R"(#version 310 es65precision mediump float;66out vec4 outColor;67void main()68{69outColor = vec4(0.0, 0.0, 0.0, 1.0);70float[2][2](float[2](outColor[1]++, 0.0), float[2](1.0, 2.0));71})";72compile(shaderString, SH_VARIABLES);73}7475// Test dynamic indexing of a vector. This makes sure that helper functions added for dynamic76// indexing have correct data that subsequent traversal steps rely on.77TEST_F(MSLOutputTest, VectorDynamicIndexing)78{79const std::string &shaderString =80R"(#version 300 es81precision mediump float;82out vec4 outColor;83uniform int i;84void main()85{86vec4 foo = vec4(0.0, 0.0, 0.0, 1.0);87foo[i] = foo[i + 1];88outColor = foo;89})";90compile(shaderString, SH_VARIABLES);91}9293// Test returning an array from a user-defined function. This makes sure that function symbols are94// changed consistently when the user-defined function is changed to have an array out parameter.95TEST_F(MSLOutputTest, ArrayReturnValue)96{97const std::string &shaderString =98R"(#version 300 es99precision mediump float;100uniform float u;101out vec4 outColor;102103float[2] getArray(float f)104{105return float[2](f, f + 1.0);106}107108void main()109{110float[2] arr = getArray(u);111outColor = vec4(arr[0], arr[1], 0.0, 1.0);112})";113compile(shaderString, SH_VARIABLES);114}115116// Test that writing parameters without a name doesn't assert.117TEST_F(MSLOutputTest, ParameterWithNoName)118{119const std::string &shaderString =120R"(precision mediump float;121122uniform vec4 v;123124vec4 s(vec4)125{126return v;127}128void main()129{130gl_FragColor = s(v);131})";132compile(shaderString, SH_VARIABLES);133}134135TEST_F(MSLOutputTest, Macro)136{137const std::string &shaderString =138R"(#version 300 es139precision highp float;140141#define FOO vec4142143out vec4 outColor;144145void main()146{147outColor = FOO(1.0, 2.0, 3.0, 4.0);148})";149compile(shaderString, SH_VARIABLES);150}151152TEST_F(MSLOutputTest, UniformSimple)153{154const std::string &shaderString =155R"(#version 300 es156precision highp float;157158out vec4 outColor;159uniform float x;160161void main()162{163outColor = vec4(x, x, x, x);164})";165compile(shaderString, SH_VARIABLES);166}167168TEST_F(MSLOutputTest, FragmentOutSimple)169{170const std::string &shaderString =171R"(#version 300 es172precision highp float;173174out vec4 outColor;175176void main()177{178outColor = vec4(1.0, 2.0, 3.0, 4.0);179})";180compile(shaderString, SH_VARIABLES);181}182183TEST_F(MSLOutputTest, FragmentOutIndirect1)184{185const std::string &shaderString =186R"(#version 300 es187precision highp float;188189out vec4 outColor;190191void foo()192{193outColor = vec4(1.0, 2.0, 3.0, 4.0);194}195196void bar()197{198foo();199}200201void main()202{203bar();204})";205compile(shaderString, SH_VARIABLES);206}207208TEST_F(MSLOutputTest, FragmentOutIndirect2)209{210const std::string &shaderString =211R"(#version 300 es212precision highp float;213214out vec4 outColor;215216void foo();217218void bar()219{220foo();221}222223void foo()224{225outColor = vec4(1.0, 2.0, 3.0, 4.0);226}227228void main()229{230bar();231})";232compile(shaderString, SH_VARIABLES);233}234235TEST_F(MSLOutputTest, FragmentOutIndirect3)236{237const std::string &shaderString =238R"(#version 300 es239precision highp float;240241out vec4 outColor;242243float foo(float x, float y)244{245outColor = vec4(x, y, 3.0, 4.0);246return 7.0;247}248249float bar(float x)250{251return foo(x, 2.0);252}253254float baz()255{256return 13.0;257}258259float identity(float x)260{261return x;262}263264void main()265{266identity(bar(baz()));267})";268compile(shaderString, SH_VARIABLES);269}270271TEST_F(MSLOutputTest, VertexInOut)272{273const std::string &shaderString =274R"(#version 300 es275precision highp float;276in float in0;277out float out0;278void main()279{280out0 = in0;281})";282compile(shaderString, SH_VARIABLES);283}284285TEST_F(MSLOutputTest, SymbolSharing)286{287const std::string &shaderString =288R"(#version 300 es289precision highp float;290291out vec4 outColor;292293struct Foo {294float x;295float y;296};297298void doFoo(Foo foo, float zw);299300void doFoo(Foo foo, float zw)301{302foo.x = foo.y;303outColor = vec4(foo.x, foo.y, zw, zw);304}305306void main()307{308Foo foo;309foo.x = 2.0;310foo.y = 2.0;311doFoo(foo, 3.0);312})";313compile(shaderString, SH_VARIABLES);314}315316TEST_F(MSLOutputTest, StructDecl)317{318const std::string &shaderString =319R"(#version 300 es320precision highp float;321322out float out0;323324struct Foo {325float value;326};327328void main()329{330Foo foo;331out0 = foo.value;332}333)";334compile(shaderString, SH_VARIABLES);335}336337TEST_F(MSLOutputTest, Structs)338{339const std::string &shaderString =340R"(#version 300 es341precision highp float;342343struct Foo {344float value;345};346347out vec4 out0;348349struct Bar {350Foo foo;351};352353void go();354355uniform UniInstance {356Bar bar;357float instance;358} uniInstance;359360uniform UniGlobal {361Foo foo;362float global;363};364365void main()366{367go();368}369370struct Baz {371Bar bar;372} baz;373374void go()375{376out0.x = baz.bar.foo.value;377out0.y = global;378out0.z = uniInstance.instance;379out0.w = 0.0;380}381382)";383compile(shaderString, SH_VARIABLES);384}385386TEST_F(MSLOutputTest, KeywordConflict)387{388const std::string &shaderString =389R"(#version 300 es390precision highp float;391392struct fragment {393float kernel;394} device;395396struct Foo {397fragment frag;398} foo;399400out float vertex;401float kernel;402403float stage_in(float x)404{405return x;406}407408void metal(float metal, float fragment);409void metal(float metal, float fragment)410{411vertex = metal * fragment * foo.frag.kernel;412}413414void main()415{416metal(stage_in(stage_in(kernel * device.kernel)), foo.frag.kernel);417})";418compile(shaderString, SH_VARIABLES);419}420421TEST_F(MSLVertexOutputTest, Vertex)422{423const std::string &shaderString =424R"(#version 300 es425precision highp float;426void main()427{428gl_Position = vec4(1.0,1.0,1.0,1.0);429})";430compile(shaderString, SH_VARIABLES);431}432433TEST_F(MSLVertexOutputTest, LastReturn)434{435const std::string &shaderString =436R"(#version 300 es437in highp vec4 a_position;438in highp vec4 a_coords;439out highp vec4 v_color;440441void main (void)442{443gl_Position = a_position;444v_color = vec4(a_coords.xyz, 1.0);445return;446})";447compile(shaderString, SH_VARIABLES);448}449450TEST_F(MSLOutputTest, LastReturn)451{452const std::string &shaderString =453R"(#version 300 es454in mediump vec4 v_coords;455layout(location = 0) out mediump vec4 o_color;456457void main (void)458{459o_color = vec4(v_coords.xyz, 1.0);460return;461})";462compile(shaderString, SH_VARIABLES);463}464465TEST_F(MSLOutputTest, FragColor)466{467const std::string &shaderString = R"(468void main ()469{470gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);471})";472compile(shaderString, SH_VARIABLES);473}474475TEST_F(MSLOutputTest, MatrixIn)476{477const std::string &shaderString =478R"(#version 300 es479precision highp float;480481in mat4 mat;482out float out0;483484void main()485{486out0 = mat[0][0];487}488)";489compile(shaderString, SH_VARIABLES);490}491492TEST_F(MSLOutputTest, WhileTrue)493{494const std::string &shaderString =495R"(#version 300 es496precision mediump float;497498uniform float uf;499out vec4 my_FragColor;500501void main()502{503my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);504while (true)505{506break;507}508})";509compile(shaderString, SH_VARIABLES);510}511512TEST_F(MSLOutputTest, ForTrue)513{514const std::string &shaderString =515R"(#version 300 es516precision mediump float;517518uniform float uf;519out vec4 my_FragColor;520521void main()522{523my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);524for (;true;)525{526break;527}528})";529compile(shaderString, SH_VARIABLES);530}531532TEST_F(MSLOutputTest, ForEmpty)533{534const std::string &shaderString =535R"(#version 300 es536precision mediump float;537538uniform float uf;539out vec4 my_FragColor;540541void main()542{543my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);544for (;;)545{546break;547}548})";549compile(shaderString, SH_VARIABLES);550}551552TEST_F(MSLOutputTest, ForComplex)553{554const std::string &shaderString =555R"(#version 300 es556precision mediump float;557558uniform float uf;559out vec4 my_FragColor;560561void main()562{563my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);564for (int i = 0, j = 2; i < j; ++i) {565if (i == 0) continue;566if (i == 42) break;567my_FragColor.x += float(i);568}569})";570compile(shaderString, SH_VARIABLES);571}572573TEST_F(MSLOutputTest, ForSymbol)574{575const std::string &shaderString =576R"(#version 300 es577precision mediump float;578579uniform float uf;580out vec4 my_FragColor;581582void main()583{584bool cond = true;585for (;cond;)586{587my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);588cond = false;589}590})";591compile(shaderString, SH_VARIABLES);592}593594TEST_F(MSLOutputTest, DoWhileSymbol)595{596const std::string &shaderString =597R"(#version 300 es598precision mediump float;599600uniform float uf;601out vec4 my_FragColor;602603void main()604{605bool cond = false;606do607{608my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);609} while (cond);610})";611compile(shaderString, SH_VARIABLES);612}613614TEST_F(MSLOutputTest, AnonymousStruct)615{616const std::string &shaderString =617"#version 300 es\n"618"precision highp float;\n"619"out vec4 my_FragColor;\n"620"uniform int u1;\n"621"void main() {\n"622" bvec4 v = bvec4(true, true, true, false);\n"623" my_FragColor = vec4(v[u1 + 1] || v[u1]);\n"624"}\n";625compile(shaderString, SH_VARIABLES);626}627628629