• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2017 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 // WebGLReadOutsideFramebufferTest.cpp : Test functions which read the framebuffer (readPixels,
8 // copyTexSubImage2D, copyTexImage2D) on areas outside the framebuffer.
9 
10 #include "test_utils/ANGLETest.h"
11 
12 #include "test_utils/gl_raii.h"
13 
14 namespace
15 {
16 
17 class PixelRect
18 {
19   public:
PixelRect(int width,int height)20     PixelRect(int width, int height) : mWidth(width), mHeight(height), mData(width * height) {}
21 
22     // Set each pixel to a different color consisting of the x,y position and a given tag.
23     // Making each pixel a different means any misplaced pixel will cause a failure.
24     // Encoding the position proved valuable in debugging.
fill(unsigned tag)25     void fill(unsigned tag)
26     {
27         for (int x = 0; x < mWidth; ++x)
28         {
29             for (int y = 0; y < mHeight; ++y)
30             {
31                 mData[x + y * mWidth] = angle::GLColor(x + (y << 8) + (tag << 16));
32             }
33         }
34     }
35 
toTexture2D(GLuint target,GLuint texid) const36     void toTexture2D(GLuint target, GLuint texid) const
37     {
38         glBindTexture(target, texid);
39         if (target == GL_TEXTURE_CUBE_MAP)
40         {
41             glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA,
42                          GL_UNSIGNED_BYTE, mData.data());
43             glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA,
44                          GL_UNSIGNED_BYTE, mData.data());
45             glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA,
46                          GL_UNSIGNED_BYTE, mData.data());
47             glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA,
48                          GL_UNSIGNED_BYTE, mData.data());
49             glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA,
50                          GL_UNSIGNED_BYTE, mData.data());
51             glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA,
52                          GL_UNSIGNED_BYTE, mData.data());
53         }
54         else
55         {
56             ASSERT_GLENUM_EQ(GL_TEXTURE_2D, target);
57             glTexImage2D(target, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
58                          mData.data());
59         }
60         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
61         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
62         glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
63         glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
64     }
65 
toTexture3D(GLuint texid,GLint depth) const66     void toTexture3D(GLuint texid, GLint depth) const
67     {
68         glBindTexture(GL_TEXTURE_3D, texid);
69 
70         glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, mWidth, mHeight, depth, 0, GL_RGBA,
71                      GL_UNSIGNED_BYTE, nullptr);
72         for (GLint z = 0; z < depth; z++)
73         {
74             glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, z, mWidth, mHeight, 1, GL_RGBA,
75                             GL_UNSIGNED_BYTE, mData.data());
76         }
77         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
78         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
79         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
80         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
81         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
82     }
83 
readFB(int x,int y)84     void readFB(int x, int y)
85     {
86         glReadPixels(x, y, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, mData.data());
87     }
88 
89     // Read pixels from 'other' into 'this' from position (x,y).
90     // Pixels outside 'other' are untouched or zeroed according to 'zeroOutside.'
readPixelRect(const PixelRect & other,int x,int y,bool zeroOutside)91     void readPixelRect(const PixelRect &other, int x, int y, bool zeroOutside)
92     {
93         for (int i = 0; i < mWidth; ++i)
94         {
95             for (int j = 0; j < mHeight; ++j)
96             {
97                 angle::GLColor *dest = &mData[i + j * mWidth];
98                 if (!other.getPixel(x + i, y + j, dest) && zeroOutside)
99                 {
100                     *dest = angle::GLColor(0);
101                 }
102             }
103         }
104     }
105 
getPixel(int x,int y,angle::GLColor * colorOut) const106     bool getPixel(int x, int y, angle::GLColor *colorOut) const
107     {
108         if (0 <= x && x < mWidth && 0 <= y && y < mHeight)
109         {
110             *colorOut = mData[x + y * mWidth];
111             return true;
112         }
113         return false;
114     }
115 
compare(const PixelRect & expected) const116     void compare(const PixelRect &expected) const
117     {
118         ASSERT_EQ(mWidth, expected.mWidth);
119         ASSERT_EQ(mHeight, expected.mHeight);
120 
121         for (int x = 0; x < mWidth; ++x)
122         {
123             for (int y = 0; y < mHeight; ++y)
124             {
125                 ASSERT_EQ(expected.mData[x + y * mWidth], mData[x + y * mWidth])
126                     << "at (" << x << ", " << y << ")";
127             }
128         }
129     }
130 
131   private:
132     int mWidth, mHeight;
133     std::vector<angle::GLColor> mData;
134 };
135 
136 }  // namespace
137 
138 namespace angle
139 {
140 
141 class WebGLReadOutsideFramebufferTest : public ANGLETest
142 {
143   public:
144     // Read framebuffer to 'pixelsOut' via glReadPixels.
TestReadPixels(int x,int y,int,PixelRect * pixelsOut)145     void TestReadPixels(int x, int y, int, PixelRect *pixelsOut) { pixelsOut->readFB(x, y); }
146 
147     // Read framebuffer to 'pixelsOut' via glCopyTexSubImage2D and GL_TEXTURE_2D.
TestCopyTexSubImage2D(int x,int y,int,PixelRect * pixelsOut)148     void TestCopyTexSubImage2D(int x, int y, int, PixelRect *pixelsOut)
149     {
150         // Init texture with given pixels.
151         GLTexture destTexture;
152         pixelsOut->toTexture2D(GL_TEXTURE_2D, destTexture.get());
153 
154         // Read framebuffer -> texture -> 'pixelsOut'
155         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, x, y, kReadWidth, kReadHeight);
156         readTexture2D(GL_TEXTURE_2D, destTexture.get(), kReadWidth, kReadHeight, pixelsOut);
157     }
158 
159     // Read framebuffer to 'pixelsOut' via glCopyTexSubImage2D and cube map.
TestCopyTexSubImageCube(int x,int y,int,PixelRect * pixelsOut)160     void TestCopyTexSubImageCube(int x, int y, int, PixelRect *pixelsOut)
161     {
162         // Init texture with given pixels.
163         GLTexture destTexture;
164         pixelsOut->toTexture2D(GL_TEXTURE_CUBE_MAP, destTexture.get());
165 
166         // Read framebuffer -> texture -> 'pixelsOut'
167         glCopyTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, 0, 0, x, y, kReadWidth, kReadHeight);
168         readTexture2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, destTexture.get(), kReadWidth, kReadHeight,
169                       pixelsOut);
170     }
171 
172     // Read framebuffer to 'pixelsOut' via glCopyTexSubImage3D.
TestCopyTexSubImage3D(int x,int y,int z,PixelRect * pixelsOut)173     void TestCopyTexSubImage3D(int x, int y, int z, PixelRect *pixelsOut)
174     {
175         // Init texture with given pixels.
176         GLTexture destTexture;
177         pixelsOut->toTexture3D(destTexture.get(), kTextureDepth);
178 
179         // Read framebuffer -> texture -> 'pixelsOut'
180         glCopyTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, z, x, y, kReadWidth, kReadHeight);
181         readTexture3D(destTexture, kReadWidth, kReadHeight, z, pixelsOut);
182     }
183 
184     // Read framebuffer to 'pixelsOut' via glCopyTexImage2D and GL_TEXTURE_2D.
TestCopyTexImage2D(int x,int y,int,PixelRect * pixelsOut)185     void TestCopyTexImage2D(int x, int y, int, PixelRect *pixelsOut)
186     {
187         // Init texture with given pixels.
188         GLTexture destTexture;
189         pixelsOut->toTexture2D(GL_TEXTURE_2D, destTexture.get());
190 
191         // Read framebuffer -> texture -> 'pixelsOut'
192         glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, kReadWidth, kReadHeight, 0);
193         readTexture2D(GL_TEXTURE_2D, destTexture.get(), kReadWidth, kReadHeight, pixelsOut);
194     }
195 
196     // Read framebuffer to 'pixelsOut' via glCopyTexImage2D and cube map.
TestCopyTexImageCube(int x,int y,int,PixelRect * pixelsOut)197     void TestCopyTexImageCube(int x, int y, int, PixelRect *pixelsOut)
198     {
199         // Init texture with given pixels.
200         GLTexture destTexture;
201         pixelsOut->toTexture2D(GL_TEXTURE_CUBE_MAP, destTexture.get());
202 
203         // Read framebuffer -> texture -> 'pixelsOut'
204         glCopyTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, x, y, kReadWidth, kReadHeight,
205                          0);
206         readTexture2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, destTexture, kReadWidth, kReadHeight,
207                       pixelsOut);
208     }
209 
210   protected:
211     static constexpr int kFbWidth      = 128;
212     static constexpr int kFbHeight     = 128;
213     static constexpr int kTextureDepth = 16;
214     static constexpr int kReadWidth    = 4;
215     static constexpr int kReadHeight   = 4;
216     static constexpr int kReadLayer    = 2;
217 
218     // Tag the framebuffer pixels differently than the initial read buffer pixels, so we know for
219     // sure which pixels are changed by reading.
220     static constexpr GLuint fbTag   = 0x1122;
221     static constexpr GLuint readTag = 0xaabb;
222 
WebGLReadOutsideFramebufferTest()223     WebGLReadOutsideFramebufferTest() : mFBData(kFbWidth, kFbHeight)
224     {
225         setWindowWidth(kFbWidth);
226         setWindowHeight(kFbHeight);
227         setConfigRedBits(8);
228         setConfigGreenBits(8);
229         setConfigBlueBits(8);
230         setConfigAlphaBits(8);
231         setWebGLCompatibilityEnabled(true);
232     }
233 
testSetUp()234     void testSetUp() override
235     {
236         constexpr char kVS[] = R"(
237 attribute vec3 a_position;
238 varying vec2 v_texCoord;
239 void main() {
240     v_texCoord = a_position.xy * 0.5 + 0.5;
241     gl_Position = vec4(a_position, 1);
242 })";
243         constexpr char kFS[] = R"(
244 precision mediump float;
245 varying vec2 v_texCoord;
246 uniform sampler2D u_texture;
247 void main() {
248     gl_FragColor = texture2D(u_texture, v_texCoord);
249 })";
250 
251         mProgram = CompileProgram(kVS, kFS);
252         glUseProgram(mProgram);
253         GLint uniformLoc = glGetUniformLocation(mProgram, "u_texture");
254         ASSERT_NE(-1, uniformLoc);
255         glUniform1i(uniformLoc, 0);
256 
257         glDisable(GL_BLEND);
258         glDisable(GL_DEPTH_TEST);
259 
260         // fill framebuffer with unique pixels
261         mFBData.fill(fbTag);
262         GLTexture fbTexture;
263         mFBData.toTexture2D(GL_TEXTURE_2D, fbTexture);
264         drawQuad(mProgram, "a_position", 0.0f, 1.0f, true);
265     }
266 
testTearDown()267     void testTearDown() override { glDeleteProgram(mProgram); }
268 
269     using TestFunc = void (WebGLReadOutsideFramebufferTest::*)(int x,
270                                                                int y,
271                                                                int z,
272                                                                PixelRect *dest);
273 
Main2D(TestFunc testFunc,bool zeroOutside)274     void Main2D(TestFunc testFunc, bool zeroOutside) { mainImpl(testFunc, zeroOutside, 0); }
275 
Main3D(TestFunc testFunc,bool zeroOutside)276     void Main3D(TestFunc testFunc, bool zeroOutside)
277     {
278         mainImpl(testFunc, zeroOutside, kReadLayer);
279     }
280 
mainImpl(TestFunc testFunc,bool zeroOutside,int readLayer)281     void mainImpl(TestFunc testFunc, bool zeroOutside, int readLayer)
282     {
283         PixelRect actual(kReadWidth, kReadHeight);
284         PixelRect expected(kReadWidth, kReadHeight);
285 
286         // Read a kReadWidth*kReadHeight rectangle of pixels from places that include:
287         // - completely outside framebuffer, on all sides of it (i,j < 0 or > 2)
288         // - completely inside framebuffer (i,j == 1)
289         // - straddling framebuffer boundary, at each corner and side
290         for (int i = -1; i < 4; ++i)
291         {
292             for (int j = -1; j < 4; ++j)
293             {
294                 int x = i * kFbWidth / 2 - kReadWidth / 2;
295                 int y = j * kFbHeight / 2 - kReadHeight / 2;
296 
297                 // Put unique pixel values into the read destinations.
298                 actual.fill(readTag);
299                 expected.readPixelRect(actual, 0, 0, false);
300 
301                 // Read from framebuffer into 'actual.'
302                 glBindFramebuffer(GL_FRAMEBUFFER, 0);
303                 (this->*testFunc)(x, y, readLayer, &actual);
304                 glBindFramebuffer(GL_FRAMEBUFFER, 0);
305 
306                 // Simulate framebuffer read, into 'expected.'
307                 expected.readPixelRect(mFBData, x, y, zeroOutside);
308 
309                 // See if they are the same.
310                 actual.compare(expected);
311             }
312         }
313     }
314 
315     // Get contents of given texture by drawing it into a framebuffer then reading with
316     // glReadPixels().
readTexture2D(GLuint target,GLuint texture,GLsizei width,GLsizei height,PixelRect * out)317     void readTexture2D(GLuint target, GLuint texture, GLsizei width, GLsizei height, PixelRect *out)
318     {
319         GLFramebuffer fbo;
320         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
321         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, texture, 0);
322         out->readFB(0, 0);
323     }
324 
325     // Get contents of current texture by drawing it into a framebuffer then reading with
326     // glReadPixels().
readTexture3D(GLuint texture,GLsizei width,GLsizei height,int zSlice,PixelRect * out)327     void readTexture3D(GLuint texture, GLsizei width, GLsizei height, int zSlice, PixelRect *out)
328     {
329         GLFramebuffer fbo;
330         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
331         glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0, zSlice);
332         out->readFB(0, 0);
333     }
334 
335     PixelRect mFBData;
336     GLuint mProgram;
337 };
338 
339 class WebGL2ReadOutsideFramebufferTest : public WebGLReadOutsideFramebufferTest
340 {};
341 
342 // Check that readPixels does not set a destination pixel when
343 // the corresponding source pixel is outside the framebuffer.
TEST_P(WebGLReadOutsideFramebufferTest,ReadPixels)344 TEST_P(WebGLReadOutsideFramebufferTest, ReadPixels)
345 {
346     Main2D(&WebGLReadOutsideFramebufferTest::TestReadPixels, false);
347 }
348 
349 // Check that copyTexSubImage2D does not set a destination pixel when
350 // the corresponding source pixel is outside the framebuffer.
TEST_P(WebGLReadOutsideFramebufferTest,CopyTexSubImage2D)351 TEST_P(WebGLReadOutsideFramebufferTest, CopyTexSubImage2D)
352 {
353     Main2D(&WebGLReadOutsideFramebufferTest::TestCopyTexSubImage2D, false);
354 
355     // TODO(fjhenigman): Figure out this failure.
356     // Cube map skipped on 64-bit Windows with D3D FL 9.3
357     ANGLE_SKIP_TEST_IF(GetParam() == ES2_D3D11_FL9_3());
358 
359     Main2D(&WebGLReadOutsideFramebufferTest::TestCopyTexSubImageCube, false);
360 }
361 
362 // Check that copyTexImage2D sets (0,0,0,0) for pixels outside the framebuffer.
TEST_P(WebGLReadOutsideFramebufferTest,CopyTexImage2D)363 TEST_P(WebGLReadOutsideFramebufferTest, CopyTexImage2D)
364 {
365     // http://anglebug.com/4092
366     ANGLE_SKIP_TEST_IF(IsVulkan() || IsD3D9() || IsD3D11());
367     Main2D(&WebGLReadOutsideFramebufferTest::TestCopyTexImage2D, true);
368 
369     // TODO(fjhenigman): Figure out this failure.
370     // Cube map skipped on 64-bit Windows with D3D FL 9.3
371     ANGLE_SKIP_TEST_IF(GetParam() == ES2_D3D11_FL9_3());
372 
373     Main2D(&WebGLReadOutsideFramebufferTest::TestCopyTexImageCube, true);
374 }
375 
376 // Check that copyTexSubImage3D does not set a destination pixel when
377 // the corresponding source pixel is outside the framebuffer.
TEST_P(WebGL2ReadOutsideFramebufferTest,CopyTexSubImage3D)378 TEST_P(WebGL2ReadOutsideFramebufferTest, CopyTexSubImage3D)
379 {
380     // TODO(hqle): Metal doesn't implement 3D texture yet.
381     // http://anglebug.com/4136 (ES2 renderer is mistakenly included in this test)
382     ANGLE_SKIP_TEST_IF(IsMetal());
383     // http://anglebug.com/4092
384     ANGLE_SKIP_TEST_IF(IsVulkan() || IsD3D9() || IsD3D11());
385     // Robust CopyTexSubImage3D behaviour is not implemented on OpenGL.
386     ANGLE_SKIP_TEST_IF(IsDesktopOpenGL() || IsOpenGLES());
387 
388     Main3D(&WebGLReadOutsideFramebufferTest::TestCopyTexSubImage3D, false);
389 }
390 
391 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(WebGLReadOutsideFramebufferTest);
392 
393 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(WebGL2ReadOutsideFramebufferTest);
394 
395 }  // namespace angle
396