2 // Copyright (c) 2014 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.
6 // TypeTracking_test.cpp:
7 // Test for tracking types resulting from math operations, including their
12 #include "gtest/gtest.h"
13 #include "GLSLANG/ShaderLang.h"
14 #include "compiler/translator/TranslatorESSL.h"
16 class TypeTrackingTest : public testing::Test
24 ShBuiltInResources resources;
25 ShInitBuiltInResources(&resources);
27 mTranslator = new TranslatorESSL(GL_FRAGMENT_SHADER, SH_GLES3_SPEC);
28 ASSERT_TRUE(mTranslator->Init(resources));
31 virtual void TearDown()
36 void compile(const std::string& shaderString)
38 const char *shaderStrings[] = { shaderString.c_str() };
39 bool compilationSuccess = mTranslator->compile(shaderStrings, 1, SH_INTERMEDIATE_TREE);
40 TInfoSink &infoSink = mTranslator->getInfoSink();
41 mInfoLog = infoSink.info.c_str();
42 if (!compilationSuccess)
43 FAIL() << "Shader compilation failed " << mInfoLog;
46 bool foundInIntermediateTree(const char* stringToFind)
48 return mInfoLog.find(stringToFind) != std::string::npos;
52 TranslatorESSL *mTranslator;
56 TEST_F(TypeTrackingTest, BuiltInFunctionResultPrecision)
58 const std::string &shaderString =
59 "precision mediump float;\n"
62 " float ff = sin(f);\n"
63 " gl_FragColor = vec4(ff);\n"
65 compile(shaderString);
66 ASSERT_TRUE(foundInIntermediateTree("sine (mediump float)"));
69 TEST_F(TypeTrackingTest, BinaryMathResultPrecision)
71 const std::string &shaderString =
72 "precision mediump float;\n"
75 " float ff = f * 0.5;\n"
76 " gl_FragColor = vec4(ff);\n"
78 compile(shaderString);
79 ASSERT_TRUE(foundInIntermediateTree("multiply (mediump float)"));
82 TEST_F(TypeTrackingTest, BuiltInVecFunctionResultTypeAndPrecision)
84 const std::string &shaderString =
85 "precision mediump float;\n"
88 " float b = length(a);\n"
89 " float c = dot(a, vec2(0.5));\n"
90 " float d = distance(vec2(0.5), a);\n"
91 " gl_FragColor = vec4(b, c, d, 1.0);\n"
93 compile(shaderString);
94 ASSERT_TRUE(foundInIntermediateTree("length (mediump float)"));
95 ASSERT_TRUE(foundInIntermediateTree("dot-product (mediump float)"));
96 ASSERT_TRUE(foundInIntermediateTree("distance (mediump float)"));
99 TEST_F(TypeTrackingTest, BuiltInFunctionChoosesHigherPrecision)
101 const std::string &shaderString =
102 "precision lowp float;\n"
103 "uniform mediump vec2 a;\n"
104 "uniform lowp vec2 b;\n"
106 " float c = dot(a, b);\n"
107 " float d = distance(b, a);\n"
108 " gl_FragColor = vec4(c, d, 0.0, 1.0);\n"
110 compile(shaderString);
111 ASSERT_TRUE(foundInIntermediateTree("dot-product (mediump float)"));
112 ASSERT_TRUE(foundInIntermediateTree("distance (mediump float)"));
115 TEST_F(TypeTrackingTest, BuiltInBoolFunctionResultType)
117 const std::string &shaderString =
118 "uniform bvec4 bees;\n"
120 " bool b = any(bees);\n"
121 " bool c = all(bees);\n"
122 " bvec4 d = not(bees);\n"
123 " gl_FragColor = vec4(b ? 1.0 : 0.0, c ? 1.0 : 0.0, d.x ? 1.0 : 0.0, 1.0);\n"
125 compile(shaderString);
126 ASSERT_TRUE(foundInIntermediateTree("any (bool)"));
127 ASSERT_TRUE(foundInIntermediateTree("all (bool)"));
128 ASSERT_TRUE(foundInIntermediateTree("Negate conditional (4-component vector of bool)"));
131 TEST_F(TypeTrackingTest, BuiltInVecToBoolFunctionResultType)
133 const std::string &shaderString =
134 "precision mediump float;\n"
135 "uniform vec2 apples;\n"
136 "uniform vec2 oranges;\n"
137 "uniform ivec2 foo;\n"
138 "uniform ivec2 bar;\n"
140 " bvec2 a = lessThan(apples, oranges);\n"
141 " bvec2 b = greaterThan(foo, bar);\n"
142 " gl_FragColor = vec4(any(a) ? 1.0 : 0.0, any(b) ? 1.0 : 0.0, 0.0, 1.0);\n"
144 compile(shaderString);
145 ASSERT_TRUE(foundInIntermediateTree("Less Than (2-component vector of bool)"));
146 ASSERT_TRUE(foundInIntermediateTree("Greater Than (2-component vector of bool)"));
149 TEST_F(TypeTrackingTest, Texture2DResultTypeAndPrecision)
151 // ESSL spec section 4.5.3: sampler2D and samplerCube are lowp by default
152 // ESSL spec section 8: For the texture functions, the precision of the return type matches the precision of the sampler type.
153 const std::string &shaderString =
154 "precision mediump float;\n"
155 "uniform sampler2D s;\n"
158 " vec4 c = texture2D(s, a);\n"
159 " gl_FragColor = c;\n"
161 compile(shaderString);
162 ASSERT_TRUE(foundInIntermediateTree("texture2D(s21;vf2; (lowp 4-component vector of float)"));
165 TEST_F(TypeTrackingTest, TextureCubeResultTypeAndPrecision)
167 // ESSL spec section 4.5.3: sampler2D and samplerCube are lowp by default
168 // ESSL spec section 8: For the texture functions, the precision of the return type matches the precision of the sampler type.
169 const std::string &shaderString =
170 "precision mediump float;\n"
171 "uniform samplerCube sc;\n"
174 " vec4 c = textureCube(sc, a);\n"
175 " gl_FragColor = c;\n"
177 compile(shaderString);
178 ASSERT_TRUE(foundInIntermediateTree("textureCube(sC1;vf3; (lowp 4-component vector of float)"));
181 TEST_F(TypeTrackingTest, TextureSizeResultTypeAndPrecision)
183 // ESSL 3.0 spec section 8: textureSize has predefined precision highp
184 const std::string &shaderString =
186 "precision mediump float;\n"
187 "out vec4 my_FragColor;\n"
188 "uniform sampler2D s;\n"
190 " ivec2 size = textureSize(s, 0);\n"
191 " if (size.x > 100) {\n"
192 " my_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
194 " my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
197 compile(shaderString);
198 ASSERT_TRUE(foundInIntermediateTree("textureSize(s21;i1; (highp 2-component vector of int)"));