1 //
2 // Copyright 2015 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
7 #include "test_utils/ANGLETest.h"
8 #include "test_utils/gl_raii.h"
9
10 using namespace angle;
11
12 class CubeMapTextureTest : public ANGLETest
13 {
14 protected:
CubeMapTextureTest()15 CubeMapTextureTest()
16 {
17 setWindowWidth(256);
18 setWindowHeight(256);
19 setConfigRedBits(8);
20 setConfigGreenBits(8);
21 setConfigBlueBits(8);
22 setConfigAlphaBits(8);
23 }
24
25 void testSetUp() override
26 {
27 mProgram = CompileProgram(essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
28 if (mProgram == 0)
29 {
30 FAIL() << "shader compilation failed.";
31 }
32
33 mColorLocation = glGetUniformLocation(mProgram, essl1_shaders::ColorUniform());
34
35 glUseProgram(mProgram);
36
37 glClearColor(0, 0, 0, 0);
38 glClearDepthf(0.0);
39 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
40
41 glEnable(GL_BLEND);
42 glDisable(GL_DEPTH_TEST);
43
44 ASSERT_GL_NO_ERROR();
45 }
46
47 void testTearDown() override { glDeleteProgram(mProgram); }
48
49 void runSampleCoordinateTransformTest(const char *shader);
50
51 GLuint mProgram;
52 GLint mColorLocation;
53 };
54
55 // Verify that rendering to the faces of a cube map consecutively will correctly render to each
56 // face.
TEST_P(CubeMapTextureTest, RenderToFacesConsecutively)57 TEST_P(CubeMapTextureTest, RenderToFacesConsecutively)
58 {
59 // TODO: Diagnose and fix. http://anglebug.com/2954
60 ANGLE_SKIP_TEST_IF(IsVulkan() && IsIntel() && IsWindows());
61
62 // http://anglebug.com/3145
63 ANGLE_SKIP_TEST_IF(IsVulkan() && IsIntel() && IsFuchsia());
64
65 const GLfloat faceColors[] = {
66 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
67 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
68 };
69
70 GLuint tex = 0;
71 glGenTextures(1, &tex);
72 glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
73 for (GLenum face = 0; face < 6; face++)
74 {
75 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, 1, 1, 0, GL_RGBA,
76 GL_UNSIGNED_BYTE, nullptr);
77 }
78 EXPECT_GL_NO_ERROR();
79
80 GLuint fbo = 0;
81 glGenFramebuffers(1, &fbo);
82 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
83 EXPECT_GL_NO_ERROR();
84
85 for (GLenum face = 0; face < 6; face++)
86 {
87 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
88 GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, tex, 0);
89 EXPECT_GL_NO_ERROR();
90
91 glUseProgram(mProgram);
92
93 const GLfloat *faceColor = faceColors + (face * 4);
94 glUniform4f(mColorLocation, faceColor[0], faceColor[1], faceColor[2], faceColor[3]);
95
96 drawQuad(mProgram, essl1_shaders::PositionAttrib(), 0.5f);
97 EXPECT_GL_NO_ERROR();
98 }
99
100 for (GLenum face = 0; face < 6; face++)
101 {
102 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
103 GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, tex, 0);
104 EXPECT_GL_NO_ERROR();
105
106 const GLfloat *faceColor = faceColors + (face * 4);
107 EXPECT_PIXEL_EQ(0, 0, faceColor[0] * 255, faceColor[1] * 255, faceColor[2] * 255,
108 faceColor[3] * 255);
109 EXPECT_GL_NO_ERROR();
110 }
111
112 glDeleteFramebuffers(1, &fbo);
113 glDeleteTextures(1, &tex);
114
115 EXPECT_GL_NO_ERROR();
116 }
117
runSampleCoordinateTransformTest(const char *shader)118 void CubeMapTextureTest::runSampleCoordinateTransformTest(const char *shader)
119 {
120 // Fails to compile the shader. anglebug.com/3776
121 ANGLE_SKIP_TEST_IF(IsOpenGL() && IsIntel() && IsWindows());
122
123 constexpr GLsizei kCubeFaceCount = 6;
124 constexpr GLsizei kCubeFaceSectionCount = 4;
125 constexpr GLsizei kCubeFaceSectionCountSqrt = 2;
126
127 constexpr GLColor faceColors[kCubeFaceCount][kCubeFaceSectionCount] = {
128 {GLColor(255, 0, 0, 255), GLColor(191, 0, 0, 255), GLColor(127, 0, 0, 255),
129 GLColor(63, 0, 0, 255)},
130 {GLColor(0, 255, 0, 255), GLColor(0, 191, 0, 255), GLColor(0, 127, 0, 255),
131 GLColor(0, 63, 0, 255)},
132 {GLColor(0, 0, 255, 255), GLColor(0, 0, 191, 255), GLColor(0, 0, 127, 255),
133 GLColor(0, 0, 63, 255)},
134 {GLColor(255, 63, 0, 255), GLColor(191, 127, 0, 255), GLColor(127, 191, 0, 255),
135 GLColor(63, 255, 0, 255)},
136 {GLColor(0, 255, 63, 255), GLColor(0, 191, 127, 255), GLColor(0, 127, 191, 255),
137 GLColor(0, 63, 255, 255)},
138 {GLColor(63, 0, 255, 255), GLColor(127, 0, 191, 255), GLColor(191, 0, 127, 255),
139 GLColor(255, 0, 63, 255)},
140 };
141
142 constexpr GLsizei kTextureSize = 32;
143
144 GLTexture tex;
145 glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
146 for (GLenum face = 0; face < kCubeFaceCount; face++)
147 {
148 std::vector<GLColor> faceData(kTextureSize * kTextureSize);
149
150 // Create the face with four sections, each with a solid color from |faceColors|.
151 for (size_t row = 0; row < kTextureSize / kCubeFaceSectionCountSqrt; ++row)
152 {
153 for (size_t col = 0; col < kTextureSize / kCubeFaceSectionCountSqrt; ++col)
154 {
155 for (size_t srow = 0; srow < kCubeFaceSectionCountSqrt; ++srow)
156 {
157 for (size_t scol = 0; scol < kCubeFaceSectionCountSqrt; ++scol)
158 {
159 size_t r = row + srow * kTextureSize / kCubeFaceSectionCountSqrt;
160 size_t c = col + scol * kTextureSize / kCubeFaceSectionCountSqrt;
161 size_t s = srow * kCubeFaceSectionCountSqrt + scol;
162 faceData[r * kTextureSize + c] = faceColors[face][s];
163 }
164 }
165 }
166 }
167
168 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, kTextureSize, kTextureSize,
169 0, GL_RGBA, GL_UNSIGNED_BYTE, faceData.data());
170 }
171 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
172 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
173 EXPECT_GL_NO_ERROR();
174
175 GLTexture fboTex;
176 glBindTexture(GL_TEXTURE_2D, fboTex);
177 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kCubeFaceCount, kCubeFaceSectionCount, 0, GL_RGBA,
178 GL_UNSIGNED_BYTE, nullptr);
179
180 GLFramebuffer fbo;
181 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
182 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboTex, 0);
183 EXPECT_GL_NO_ERROR();
184
185 ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), shader);
186 glUseProgram(program);
187
188 GLint texCubeLocation = glGetUniformLocation(program, "texCube");
189 ASSERT_NE(-1, texCubeLocation);
190 glUniform1i(texCubeLocation, 0);
191
192 drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
193 EXPECT_GL_NO_ERROR();
194
195 for (GLenum face = 0; face < kCubeFaceCount; face++)
196 {
197 // The following table defines the translation from textureCube coordinates to coordinates
198 // in each face. The framebuffer has width 6 and height 4. Every column corresponding to
199 // an x value represents one cube face. The values in rows are samples from the four
200 // sections of the face.
201 //
202 // Major Axis Direction Target sc tc ma
203 // +rx TEXTURE_CUBE_MAP_POSITIVE_X −rz −ry rx
204 // −rx TEXTURE_CUBE_MAP_NEGATIVE_X rz −ry rx
205 // +ry TEXTURE_CUBE_MAP_POSITIVE_Y rx rz ry
206 // −ry TEXTURE_CUBE_MAP_NEGATIVE_Y rx −rz ry
207 // +rz TEXTURE_CUBE_MAP_POSITIVE_Z rx −ry rz
208 // −rz TEXTURE_CUBE_MAP_NEGATIVE_Z −rx −ry rz
209 //
210 // This table is used only to determine the direction of growth for s and t. The shader
211 // always generates (row,col) coordinates (0, 0), (0, 1), (1, 0), (1, 1) which is the order
212 // the data is uploaded to the faces, but based on the table above, the sample order would
213 // be different.
214 constexpr size_t faceSampledSections[kCubeFaceCount][kCubeFaceSectionCount] = {
215 {3, 2, 1, 0}, {2, 3, 0, 1}, {0, 1, 2, 3}, {2, 3, 0, 1}, {2, 3, 0, 1}, {3, 2, 1, 0},
216 };
217
218 for (size_t section = 0; section < kCubeFaceSectionCount; ++section)
219 {
220 const GLColor sectionColor = faceColors[face][faceSampledSections[face][section]];
221
222 EXPECT_PIXEL_COLOR_EQ(face, section, sectionColor)
223 << "face " << face << ", section " << section;
224 }
225 }
226 EXPECT_GL_NO_ERROR();
227 }
228
229 // Verify that cube map sampling follows the rules that map cubemap coordinates to coordinates
230 // within each face. See section 3.7.5 of GLES2.0 (Cube Map Texture Selection).
TEST_P(CubeMapTextureTest, SampleCoordinateTransform)231 TEST_P(CubeMapTextureTest, SampleCoordinateTransform)
232 {
233 // http://anglebug.com/4092
234 ANGLE_SKIP_TEST_IF(IsWindows() && IsD3D9());
235 // Create a program that samples from 6x4 directions of the cubemap, draw and verify that the
236 // colors match the right color from |faceColors|.
237 constexpr char kFS[] = R"(precision mediump float;
238
239 uniform samplerCube texCube;
240
241 const mat4 coordInSection = mat4(
242 vec4(-0.5, -0.5, 0, 0),
243 vec4( 0.5, -0.5, 0, 0),
244 vec4(-0.5, 0.5, 0, 0),
245 vec4( 0.5, 0.5, 0, 0)
246 );
247
248 void main()
249 {
250 vec3 coord;
251 if (gl_FragCoord.x < 2.0)
252 {
253 coord.x = gl_FragCoord.x < 1.0 ? 1.0 : -1.0;
254 coord.zy = coordInSection[int(gl_FragCoord.y)].xy;
255 }
256 else if (gl_FragCoord.x < 4.0)
257 {
258 coord.y = gl_FragCoord.x < 3.0 ? 1.0 : -1.0;
259 coord.xz = coordInSection[int(gl_FragCoord.y)].xy;
260 }
261 else
262 {
263 coord.z = gl_FragCoord.x < 5.0 ? 1.0 : -1.0;
264 coord.xy = coordInSection[int(gl_FragCoord.y)].xy;
265 }
266
267 gl_FragColor = textureCube(texCube, coord);
268 })";
269
270 runSampleCoordinateTransformTest(kFS);
271 }
272
273 // On Android Vulkan, unequal x and y derivatives cause this test to fail.
TEST_P(CubeMapTextureTest, SampleCoordinateTransformGrad)274 TEST_P(CubeMapTextureTest, SampleCoordinateTransformGrad)
275 {
276 ANGLE_SKIP_TEST_IF(IsAndroid() && IsVulkan()); // anglebug.com/3814
277 ANGLE_SKIP_TEST_IF(IsD3D11()); // anglebug.com/3856
278 ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_shader_texture_lod"));
279 // http://anglebug.com/4092
280 ANGLE_SKIP_TEST_IF(IsWindows() && IsD3D9());
281
282 constexpr char kFS[] = R"(#extension GL_EXT_shader_texture_lod : require
283 precision mediump float;
284
285 uniform samplerCube texCube;
286
287 const mat4 coordInSection = mat4(
288 vec4(-0.5, -0.5, 0, 0),
289 vec4( 0.5, -0.5, 0, 0),
290 vec4(-0.5, 0.5, 0, 0),
291 vec4( 0.5, 0.5, 0, 0)
292 );
293
294 void main()
295 {
296 vec3 coord;
297 if (gl_FragCoord.x < 2.0)
298 {
299 coord.x = gl_FragCoord.x < 1.0 ? 1.0 : -1.0;
300 coord.zy = coordInSection[int(gl_FragCoord.y)].xy;
301 }
302 else if (gl_FragCoord.x < 4.0)
303 {
304 coord.y = gl_FragCoord.x < 3.0 ? 1.0 : -1.0;
305 coord.xz = coordInSection[int(gl_FragCoord.y)].xy;
306 }
307 else
308 {
309 coord.z = gl_FragCoord.x < 5.0 ? 1.0 : -1.0;
310 coord.xy = coordInSection[int(gl_FragCoord.y)].xy;
311 }
312
313 gl_FragColor = textureCubeGradEXT(texCube, coord,
314 vec3(10.0, 10.0, 0.0), vec3(0.0, 10.0, 10.0));
315 })";
316
317 runSampleCoordinateTransformTest(kFS);
318 }
319
320 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
321 // tests should be run against.
322 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(CubeMapTextureTest);
323