• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 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 // ARB_texture_rectangle_test.cpp:
7 //   Test for the ARB_texture_rectangle extension
8 //
9 
10 #include "GLSLANG/ShaderLang.h"
11 #include "angle_gl.h"
12 #include "gtest/gtest.h"
13 #include "tests/test_utils/ShaderCompileTreeTest.h"
14 
15 using namespace sh;
16 
17 class ARBTextureRectangleTestNoExt : public ShaderCompileTreeTest
18 {
19   protected:
getShaderType() const20     ::GLenum getShaderType() const override { return GL_FRAGMENT_SHADER; }
getShaderSpec() const21     ShShaderSpec getShaderSpec() const override { return SH_GLES3_SPEC; }
22 };
23 
24 class ARBTextureRectangleTest : public ARBTextureRectangleTestNoExt
25 {
26   protected:
initResources(ShBuiltInResources * resources)27     void initResources(ShBuiltInResources *resources) override
28     {
29         resources->ARB_texture_rectangle = 1;
30     }
31 };
32 
33 // Check that if the extension is not supported, trying to use the features without having an
34 // extension directive fails.
TEST_F(ARBTextureRectangleTestNoExt,NewTypeAndBuiltinsWithoutExtensionDirective)35 TEST_F(ARBTextureRectangleTestNoExt, NewTypeAndBuiltinsWithoutExtensionDirective)
36 {
37     const std::string &shaderString =
38         R"(
39         precision mediump float;
40         uniform sampler2DRect tex;
41         void main()
42         {
43             vec4 color = texture2DRect(tex, vec2(1.0));
44             color = texture2DRectProj(tex, vec3(1.0));
45             color = texture2DRectProj(tex, vec4(1.0));
46         })";
47     if (compile(shaderString))
48     {
49         FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
50     }
51 }
52 
53 // Check that if the extension is not supported, trying to use the features fails.
TEST_F(ARBTextureRectangleTestNoExt,NewTypeAndBuiltinsWithExtensionDirective)54 TEST_F(ARBTextureRectangleTestNoExt, NewTypeAndBuiltinsWithExtensionDirective)
55 {
56     const std::string &shaderString =
57         R"(
58         #extension GL_ARB_texture_rectangle : enable
59         precision mediump float;
60         uniform sampler2DRect tex;
61         void main()
62         {
63             vec4 color = texture2DRect(tex, vec2(1.0));
64             color = texture2DRectProj(tex, vec3(1.0));
65             color = texture2DRectProj(tex, vec4(1.0));
66         })";
67     if (compile(shaderString))
68     {
69         FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
70     }
71 }
72 
73 // Check that new types and builtins are usable even with the #extension directive
74 // Issue #15 of ARB_texture_rectangle explains that the extension was specified before the
75 // #extension mechanism was in place so it doesn't require explicit enabling.
TEST_F(ARBTextureRectangleTest,NewTypeAndBuiltinsWithoutExtensionDirective)76 TEST_F(ARBTextureRectangleTest, NewTypeAndBuiltinsWithoutExtensionDirective)
77 {
78     const std::string &shaderString =
79         "precision mediump float;\n"
80         "uniform sampler2DRect tex;\n"
81         "void main() {\n"
82         "    vec4 color = texture2DRect(tex, vec2(1.0));"
83         "    color = texture2DRectProj(tex, vec3(1.0));"
84         "    color = texture2DRectProj(tex, vec4(1.0));"
85         "}\n";
86     if (!compile(shaderString))
87     {
88         FAIL() << "Shader compilation failed, expecting success:\n" << mInfoLog;
89     }
90 }
91 
92 // Test valid usage of the new types and builtins
TEST_F(ARBTextureRectangleTest,NewTypeAndBuiltingsWithExtensionDirective)93 TEST_F(ARBTextureRectangleTest, NewTypeAndBuiltingsWithExtensionDirective)
94 {
95     const std::string &shaderString =
96         "#extension GL_ARB_texture_rectangle : require\n"
97         "precision mediump float;\n"
98         "uniform sampler2DRect tex;\n"
99         "void main() {\n"
100         "    vec4 color = texture2DRect(tex, vec2(1.0));"
101         "    color = texture2DRectProj(tex, vec3(1.0));"
102         "    color = texture2DRectProj(tex, vec4(1.0));"
103         "}\n";
104     if (!compile(shaderString))
105     {
106         FAIL() << "Shader compilation failed, expecting success:\n" << mInfoLog;
107     }
108 }
109 
110 // Check that it is not possible to pass a sampler2DRect where sampler2D is expected, and vice versa
TEST_F(ARBTextureRectangleTest,Rect2DVs2DMismatch)111 TEST_F(ARBTextureRectangleTest, Rect2DVs2DMismatch)
112 {
113     const std::string &shaderString1 =
114         "#extension GL_ARB_texture_rectangle : require\n"
115         "precision mediump float;\n"
116         "uniform sampler2DRect tex;\n"
117         "void main() {\n"
118         "    vec4 color = texture2D(tex, vec2(1.0));"
119         "}\n";
120     if (compile(shaderString1))
121     {
122         FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
123     }
124 
125     const std::string &shaderString2 =
126         "#extension GL_ARB_texture_rectangle : require\n"
127         "precision mediump float;\n"
128         "uniform sampler2D tex;\n"
129         "void main() {\n"
130         "    vec4 color = texture2DRect(tex, vec2(1.0));"
131         "}\n";
132     if (compile(shaderString2))
133     {
134         FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
135     }
136 }
137 
138 // Disabling ARB_texture_rectangle in GLSL should work, even if it is enabled by default.
139 // See ARB_texture_rectangle spec: "a shader can still include all variations of #extension
140 // GL_ARB_texture_rectangle in its source code"
TEST_F(ARBTextureRectangleTest,DisableARBTextureRectangle)141 TEST_F(ARBTextureRectangleTest, DisableARBTextureRectangle)
142 {
143     const std::string &shaderString =
144         R"(
145         #extension GL_ARB_texture_rectangle : disable
146 
147         precision mediump float;
148 
149         uniform sampler2DRect s;
150         void main()
151         {})";
152     if (compile(shaderString))
153     {
154         FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
155     }
156 }
157 
158 // The compiler option to disable ARB_texture_rectangle should prevent shaders from
159 // enabling it.
TEST_F(ARBTextureRectangleTest,CompilerOption)160 TEST_F(ARBTextureRectangleTest, CompilerOption)
161 {
162     const std::string &shaderString =
163         R"(
164         #extension GL_ARB_texture_rectangle : enable
165         precision mediump float;
166         uniform sampler2DRect s;
167         void main() {})";
168     mCompileOptions.disableARBTextureRectangle = true;
169     if (compile(shaderString))
170     {
171         FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
172     }
173 }
174 
175 // The compiler option to disable ARB_texture_rectangle should be toggleable.
TEST_F(ARBTextureRectangleTest,ToggleCompilerOption)176 TEST_F(ARBTextureRectangleTest, ToggleCompilerOption)
177 {
178     const std::string &shaderString =
179         R"(
180         precision mediump float;
181         uniform sampler2DRect s;
182         void main() {})";
183     if (!compile(shaderString))
184     {
185         FAIL() << "Shader compilation failed, expecting success:\n" << mInfoLog;
186     }
187     mCompileOptions.disableARBTextureRectangle = true;
188     if (compile(shaderString))
189     {
190         FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
191     }
192     mCompileOptions.disableARBTextureRectangle = false;
193     if (!compile(shaderString))
194     {
195         FAIL() << "Shader compilation failed, expecting success:\n" << mInfoLog;
196     }
197 }
198