Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / tests / compiler_tests / TypeTracking_test.cpp
1 //
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.
5 //
6 // TypeTracking_test.cpp:
7 //   Test for tracking types resulting from math operations, including their
8 //   precision.
9 //
10
11 #include "angle_gl.h"
12 #include "gtest/gtest.h"
13 #include "GLSLANG/ShaderLang.h"
14 #include "compiler/translator/TranslatorESSL.h"
15
16 class TypeTrackingTest : public testing::Test
17 {
18   public:
19     TypeTrackingTest() {}
20
21   protected:
22     virtual void SetUp()
23     {
24         ShBuiltInResources resources;
25         ShInitBuiltInResources(&resources);
26
27         mTranslator = new TranslatorESSL(GL_FRAGMENT_SHADER, SH_GLES3_SPEC);
28         ASSERT_TRUE(mTranslator->Init(resources));
29     }
30
31     virtual void TearDown()
32     {
33         delete mTranslator;
34     }
35
36     void compile(const std::string& shaderString)
37     {
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;
44     }
45
46     bool foundInIntermediateTree(const char* stringToFind)
47     {
48         return mInfoLog.find(stringToFind) != std::string::npos;
49     }
50
51   private:
52     TranslatorESSL *mTranslator;
53     std::string mInfoLog;
54 };
55
56 TEST_F(TypeTrackingTest, BuiltInFunctionResultPrecision)
57 {
58     const std::string &shaderString =
59         "precision mediump float;\n"
60         "uniform float f;\n"
61         "void main() {\n"
62         "   float ff = sin(f);\n"
63         "   gl_FragColor = vec4(ff);\n"
64         "}\n";
65     compile(shaderString);
66     ASSERT_TRUE(foundInIntermediateTree("sine (mediump float)"));
67 };
68
69 TEST_F(TypeTrackingTest, BinaryMathResultPrecision)
70 {
71     const std::string &shaderString =
72         "precision mediump float;\n"
73         "uniform float f;\n"
74         "void main() {\n"
75         "   float ff = f * 0.5;\n"
76         "   gl_FragColor = vec4(ff);\n"
77         "}\n";
78     compile(shaderString);
79     ASSERT_TRUE(foundInIntermediateTree("multiply (mediump float)"));
80 };
81
82 TEST_F(TypeTrackingTest, BuiltInVecFunctionResultTypeAndPrecision)
83 {
84     const std::string &shaderString =
85         "precision mediump float;\n"
86         "uniform vec2 a;\n"
87         "void main() {\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"
92         "}\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)"));
97 };
98
99 TEST_F(TypeTrackingTest, BuiltInFunctionChoosesHigherPrecision)
100 {
101     const std::string &shaderString =
102         "precision lowp float;\n"
103         "uniform mediump vec2 a;\n"
104         "uniform lowp vec2 b;\n"
105         "void main() {\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"
109         "}\n";
110     compile(shaderString);
111     ASSERT_TRUE(foundInIntermediateTree("dot-product (mediump float)"));
112     ASSERT_TRUE(foundInIntermediateTree("distance (mediump float)"));
113 };
114
115 TEST_F(TypeTrackingTest, BuiltInBoolFunctionResultType)
116 {
117     const std::string &shaderString =
118         "uniform bvec4 bees;\n"
119         "void main() {\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"
124         "}\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)"));
129 };
130
131 TEST_F(TypeTrackingTest, BuiltInVecToBoolFunctionResultType)
132 {
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"
139         "void main() {\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"
143         "}\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)"));
147 };
148
149 TEST_F(TypeTrackingTest, Texture2DResultTypeAndPrecision)
150 {
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"
156         "uniform vec2 a;\n"
157         "void main() {\n"
158         "   vec4 c = texture2D(s, a);\n"
159         "   gl_FragColor = c;\n"
160         "}\n";
161     compile(shaderString);
162     ASSERT_TRUE(foundInIntermediateTree("texture2D(s21;vf2; (lowp 4-component vector of float)"));
163 };
164
165 TEST_F(TypeTrackingTest, TextureCubeResultTypeAndPrecision)
166 {
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"
172         "uniform vec3 a;\n"
173         "void main() {\n"
174         "   vec4 c = textureCube(sc, a);\n"
175         "   gl_FragColor = c;\n"
176         "}\n";
177     compile(shaderString);
178     ASSERT_TRUE(foundInIntermediateTree("textureCube(sC1;vf3; (lowp 4-component vector of float)"));
179 };
180
181 TEST_F(TypeTrackingTest, TextureSizeResultTypeAndPrecision)
182 {
183     // ESSL 3.0 spec section 8: textureSize has predefined precision highp
184     const std::string &shaderString =
185         "#version 300 es\n"
186         "precision mediump float;\n"
187         "out vec4 my_FragColor;\n"
188         "uniform sampler2D s;\n"
189         "void main() {\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"
193         "   } else {\n"
194         "       my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
195         "   }\n"
196         "}\n";
197     compile(shaderString);
198     ASSERT_TRUE(foundInIntermediateTree("textureSize(s21;i1; (highp 2-component vector of int)"));
199 };