blob: d24247a1d98900028b7c81daf03066753484dec3 [file] [log] [blame]
Jamie Madillfa05f602015-05-07 13:47:11 -04001//
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
Corentin Wallezd3970de2015-05-14 11:07:48 -04007#include "test_utils/ANGLETest.h"
Jamie Madillf67115c2014-04-22 13:14:05 -04008
Jamie Madillfa05f602015-05-07 13:47:11 -04009using namespace angle;
Austin Kinross18b931d2014-09-29 12:58:31 -070010
Jamie Madillfa05f602015-05-07 13:47:11 -040011namespace
12{
13
Olli Etuaho4a8329f2016-01-11 17:12:57 +020014class TexCoordDrawTest : public ANGLETest
Jamie Madillf67115c2014-04-22 13:14:05 -040015{
Jamie Madillbc393df2015-01-29 13:46:07 -050016 protected:
Olli Etuaho4a8329f2016-01-11 17:12:57 +020017 TexCoordDrawTest() : ANGLETest(), mProgram(0)
Jamie Madillf67115c2014-04-22 13:14:05 -040018 {
19 setWindowWidth(128);
20 setWindowHeight(128);
21 setConfigRedBits(8);
22 setConfigGreenBits(8);
23 setConfigBlueBits(8);
24 setConfigAlphaBits(8);
25 }
26
Olli Etuaho4a8329f2016-01-11 17:12:57 +020027 virtual std::string getVertexShaderSource()
Jamie Madillf67115c2014-04-22 13:14:05 -040028 {
Olli Etuaho4a8329f2016-01-11 17:12:57 +020029 return std::string(SHADER_SOURCE
Geoff Langc41e42d2014-04-28 10:58:16 -040030 (
31 precision highp float;
32 attribute vec4 position;
33 varying vec2 texcoord;
34
35 void main()
36 {
Olli Etuaho4a8329f2016-01-11 17:12:57 +020037 gl_Position = vec4(position.xy, 0.0, 1.0);
Geoff Langc41e42d2014-04-28 10:58:16 -040038 texcoord = (position.xy * 0.5) + 0.5;
39 }
Olli Etuaho4a8329f2016-01-11 17:12:57 +020040 )
Geoff Langc41e42d2014-04-28 10:58:16 -040041 );
Olli Etuaho4a8329f2016-01-11 17:12:57 +020042 }
Geoff Langc41e42d2014-04-28 10:58:16 -040043
Olli Etuaho4a8329f2016-01-11 17:12:57 +020044 virtual std::string getFragmentShaderSource() = 0;
45
46 void SetUp() override
47 {
48 ANGLETest::SetUp();
49 const std::string vertexShaderSource = getVertexShaderSource();
50 const std::string fragmentShaderSource = getFragmentShaderSource();
51
52 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
53 ASSERT_NE(0u, mProgram);
54 ASSERT_GL_NO_ERROR();
55 }
56
57 void TearDown() override
58 {
59 glDeleteProgram(mProgram);
60 ANGLETest::TearDown();
61 }
62
63 // Returns the created texture ID.
64 GLuint create2DTexture()
65 {
66 GLuint texture2D;
67 glGenTextures(1, &texture2D);
68 glBindTexture(GL_TEXTURE_2D, texture2D);
69 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
70 EXPECT_GL_NO_ERROR();
71 return texture2D;
72 }
73
74 GLuint mProgram;
75};
76
77class Texture2DTest : public TexCoordDrawTest
78{
79 protected:
80 Texture2DTest() : TexCoordDrawTest(), mTexture2D(0), mTexture2DUniformLocation(-1) {}
81
82 std::string getFragmentShaderSource() override
83 {
84 return std::string(SHADER_SOURCE
Geoff Langc41e42d2014-04-28 10:58:16 -040085 (
86 precision highp float;
87 uniform sampler2D tex;
88 varying vec2 texcoord;
89
90 void main()
91 {
92 gl_FragColor = texture2D(tex, texcoord);
93 }
Olli Etuaho4a8329f2016-01-11 17:12:57 +020094 )
Geoff Langc41e42d2014-04-28 10:58:16 -040095 );
Olli Etuaho4a8329f2016-01-11 17:12:57 +020096 }
Geoff Langc41e42d2014-04-28 10:58:16 -040097
Olli Etuaho4a8329f2016-01-11 17:12:57 +020098 void SetUp() override
99 {
100 TexCoordDrawTest::SetUp();
101 mTexture2D = create2DTexture();
Jamie Madilld4cfa572014-07-08 10:00:32 -0400102
Jamie Madill9aca0592014-10-06 16:26:59 -0400103 ASSERT_GL_NO_ERROR();
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200104
105 mTexture2DUniformLocation = glGetUniformLocation(mProgram, "tex");
106 ASSERT_NE(-1, mTexture2DUniformLocation);
Jamie Madillf67115c2014-04-22 13:14:05 -0400107 }
108
Jamie Madillfa05f602015-05-07 13:47:11 -0400109 void TearDown() override
Jamie Madillf67115c2014-04-22 13:14:05 -0400110 {
Jamie Madilld4cfa572014-07-08 10:00:32 -0400111 glDeleteTextures(1, &mTexture2D);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200112 TexCoordDrawTest::TearDown();
Jamie Madillf67115c2014-04-22 13:14:05 -0400113 }
114
Jamie Madillbc393df2015-01-29 13:46:07 -0500115 // Tests CopyTexSubImage with floating point textures of various formats.
116 void testFloatCopySubImage(int sourceImageChannels, int destImageChannels)
117 {
Geoff Langbde666a2015-04-07 17:17:08 -0400118 // TODO(jmadill): Figure out why this is broken on Intel D3D11
119 if (isIntel() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
120 {
121 std::cout << "Test skipped on Intel D3D11." << std::endl;
122 return;
123 }
124
Geoff Langfbfa47c2015-03-31 11:26:00 -0400125 if (getClientVersion() < 3)
126 {
127 if (!extensionEnabled("GL_OES_texture_float"))
128 {
129 std::cout << "Test skipped due to missing GL_OES_texture_float." << std::endl;
130 return;
131 }
132
133 if ((sourceImageChannels < 3 || destImageChannels < 3) && !extensionEnabled("GL_EXT_texture_rg"))
134 {
135 std::cout << "Test skipped due to missing GL_EXT_texture_rg." << std::endl;
136 return;
137 }
138 }
139
Jamie Madillbc393df2015-01-29 13:46:07 -0500140 GLfloat sourceImageData[4][16] =
141 {
142 { // R
143 1.0f,
144 0.0f,
145 0.0f,
146 1.0f
147 },
148 { // RG
149 1.0f, 0.0f,
150 0.0f, 1.0f,
151 0.0f, 0.0f,
152 1.0f, 1.0f
153 },
154 { // RGB
155 1.0f, 0.0f, 0.0f,
156 0.0f, 1.0f, 0.0f,
157 0.0f, 0.0f, 1.0f,
158 1.0f, 1.0f, 0.0f
159 },
160 { // RGBA
161 1.0f, 0.0f, 0.0f, 1.0f,
162 0.0f, 1.0f, 0.0f, 1.0f,
163 0.0f, 0.0f, 1.0f, 1.0f,
164 1.0f, 1.0f, 0.0f, 1.0f
165 },
166 };
167
168 GLenum imageFormats[] =
169 {
170 GL_R32F,
171 GL_RG32F,
172 GL_RGB32F,
173 GL_RGBA32F,
174 };
175
176 GLenum sourceUnsizedFormats[] =
177 {
178 GL_RED,
179 GL_RG,
180 GL_RGB,
181 GL_RGBA,
182 };
183
184 GLuint textures[2];
185
186 glGenTextures(2, textures);
187
188 GLfloat *imageData = sourceImageData[sourceImageChannels - 1];
189 GLenum sourceImageFormat = imageFormats[sourceImageChannels - 1];
190 GLenum sourceUnsizedFormat = sourceUnsizedFormats[sourceImageChannels - 1];
191 GLenum destImageFormat = imageFormats[destImageChannels - 1];
192
193 glBindTexture(GL_TEXTURE_2D, textures[0]);
194 glTexStorage2DEXT(GL_TEXTURE_2D, 1, sourceImageFormat, 2, 2);
195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
197 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, sourceUnsizedFormat, GL_FLOAT, imageData);
198
hendrikwb27f79a2015-03-04 11:26:46 -0800199 if (sourceImageChannels < 3 && !extensionEnabled("GL_EXT_texture_rg"))
Jamie Madillbc393df2015-01-29 13:46:07 -0500200 {
201 // This is not supported
202 ASSERT_GL_ERROR(GL_INVALID_OPERATION);
203 }
204 else
205 {
206 ASSERT_GL_NO_ERROR();
207 }
208
209 GLuint fbo;
210 glGenFramebuffers(1, &fbo);
211 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
212 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
213
214 glBindTexture(GL_TEXTURE_2D, textures[1]);
215 glTexStorage2DEXT(GL_TEXTURE_2D, 1, destImageFormat, 2, 2);
216 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
217 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
218
219 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 2, 2);
220 ASSERT_GL_NO_ERROR();
221
222 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200223 drawQuad(mProgram, "position", 0.5f);
Jamie Madillbc393df2015-01-29 13:46:07 -0500224
225 int testImageChannels = std::min(sourceImageChannels, destImageChannels);
226
227 EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
228 if (testImageChannels > 1)
229 {
230 EXPECT_PIXEL_EQ(getWindowHeight() - 1, 0, 0, 255, 0, 255);
231 EXPECT_PIXEL_EQ(getWindowHeight() - 1, getWindowWidth() - 1, 255, 255, 0, 255);
232 if (testImageChannels > 2)
233 {
234 EXPECT_PIXEL_EQ(0, getWindowWidth() - 1, 0, 0, 255, 255);
235 }
236 }
237
238 glDeleteFramebuffers(1, &fbo);
239 glDeleteTextures(2, textures);
240
241 ASSERT_GL_NO_ERROR();
242 }
243
Jamie Madilld4cfa572014-07-08 10:00:32 -0400244 GLuint mTexture2D;
Jamie Madilld4cfa572014-07-08 10:00:32 -0400245 GLint mTexture2DUniformLocation;
Jamie Madillf67115c2014-04-22 13:14:05 -0400246};
247
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200248class Texture2DTestWithDrawScale : public Texture2DTest
Jamie Madill2453dbc2015-07-14 11:35:42 -0400249{
250 protected:
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200251 Texture2DTestWithDrawScale() : Texture2DTest(), mDrawScaleUniformLocation(-1) {}
252
253 std::string getVertexShaderSource() override
Jamie Madill2453dbc2015-07-14 11:35:42 -0400254 {
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200255 return std::string(SHADER_SOURCE
256 (
257 precision highp float;
258 attribute vec4 position;
259 varying vec2 texcoord;
260
261 uniform vec2 drawScale;
262
263 void main()
264 {
265 gl_Position = vec4(position.xy * drawScale, 0.0, 1.0);
266 texcoord = (position.xy * 0.5) + 0.5;
267 }
268 )
269 );
Jamie Madill2453dbc2015-07-14 11:35:42 -0400270 }
271
272 void SetUp() override
273 {
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200274 Texture2DTest::SetUp();
275 mDrawScaleUniformLocation = glGetUniformLocation(mProgram, "drawScale");
276 ASSERT_NE(-1, mDrawScaleUniformLocation);
Jamie Madill2453dbc2015-07-14 11:35:42 -0400277
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200278 glUseProgram(mProgram);
279 glUniform2f(mDrawScaleUniformLocation, 1.0f, 1.0f);
280 glUseProgram(0);
281 ASSERT_GL_NO_ERROR();
282 }
283
284 GLint mDrawScaleUniformLocation;
285};
286
287class TextureCubeTest : public TexCoordDrawTest
288{
289 protected:
290 TextureCubeTest()
291 : TexCoordDrawTest(),
292 mTexture2D(0),
293 mTextureCube(0),
294 mTexture2DUniformLocation(-1),
295 mTextureCubeUniformLocation(-1)
296 {
297 }
298
299 std::string getFragmentShaderSource() override
300 {
301 return std::string(SHADER_SOURCE
302 (
303 precision highp float;
304 uniform sampler2D tex2D;
305 uniform samplerCube texCube;
306 varying vec2 texcoord;
307
308 void main()
309 {
310 gl_FragColor = texture2D(tex2D, texcoord);
311 gl_FragColor += textureCube(texCube, vec3(texcoord, 0));
312 }
313 )
314 );
315 }
316
317 void SetUp() override
318 {
319 TexCoordDrawTest::SetUp();
320
321 glGenTextures(1, &mTextureCube);
322 glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
323 glTexStorage2DEXT(GL_TEXTURE_CUBE_MAP, 1, GL_RGBA8, 1, 1);
324 EXPECT_GL_NO_ERROR();
325
326 mTexture2D = create2DTexture();
327
328 mTexture2DUniformLocation = glGetUniformLocation(mProgram, "tex2D");
329 ASSERT_NE(-1, mTexture2DUniformLocation);
330 mTextureCubeUniformLocation = glGetUniformLocation(mProgram, "texCube");
331 ASSERT_NE(-1, mTextureCubeUniformLocation);
332 }
333
334 void TearDown() override
335 {
336 glDeleteTextures(1, &mTextureCube);
337 TexCoordDrawTest::TearDown();
338 }
339
340 GLuint mTexture2D;
341 GLuint mTextureCube;
342 GLint mTexture2DUniformLocation;
343 GLint mTextureCubeUniformLocation;
344};
345
346class Texture2DArrayTestES3 : public TexCoordDrawTest
347{
348 protected:
349 Texture2DArrayTestES3() : TexCoordDrawTest(), m2DArrayTexture(0), mTextureArrayLocation(-1) {}
350
351 std::string getVertexShaderSource() override
352 {
353 return std::string(
Jamie Madill2453dbc2015-07-14 11:35:42 -0400354 "#version 300 es\n"
355 "out vec2 texcoord;\n"
356 "in vec4 position;\n"
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200357 "void main()\n"
358 "{\n"
359 " gl_Position = vec4(position.xy, 0.0, 1.0);\n"
360 " texcoord = (position.xy * 0.5) + 0.5;\n"
361 "}\n");
362 }
Jamie Madill2453dbc2015-07-14 11:35:42 -0400363
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200364 std::string getFragmentShaderSource() override
365 {
366 return std::string(
Jamie Madill2453dbc2015-07-14 11:35:42 -0400367 "#version 300 es\n"
368 "precision highp float;\n"
Olli Etuaho183d7e22015-11-20 15:59:09 +0200369 "uniform highp sampler2DArray tex2DArray;\n"
Jamie Madill2453dbc2015-07-14 11:35:42 -0400370 "in vec2 texcoord;\n"
371 "out vec4 fragColor;\n"
372 "void main()\n"
373 "{\n"
374 " fragColor = texture(tex2DArray, vec3(texcoord.x, texcoord.y, 0.0));\n"
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200375 "}\n");
376 }
Jamie Madill2453dbc2015-07-14 11:35:42 -0400377
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200378 void SetUp() override
379 {
380 TexCoordDrawTest::SetUp();
Jamie Madill2453dbc2015-07-14 11:35:42 -0400381
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200382 mTextureArrayLocation = glGetUniformLocation(mProgram, "tex2DArray");
Jamie Madill2453dbc2015-07-14 11:35:42 -0400383 ASSERT_NE(-1, mTextureArrayLocation);
384
385 glGenTextures(1, &m2DArrayTexture);
386 ASSERT_GL_NO_ERROR();
387 }
388
389 void TearDown() override
390 {
391 glDeleteTextures(1, &m2DArrayTexture);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200392 TexCoordDrawTest::TearDown();
Jamie Madill2453dbc2015-07-14 11:35:42 -0400393 }
394
395 GLuint m2DArrayTexture;
Jamie Madill2453dbc2015-07-14 11:35:42 -0400396 GLint mTextureArrayLocation;
397};
398
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200399TEST_P(Texture2DTest, NegativeAPISubImage)
Jamie Madillf67115c2014-04-22 13:14:05 -0400400{
Jamie Madilld4cfa572014-07-08 10:00:32 -0400401 glBindTexture(GL_TEXTURE_2D, mTexture2D);
Jamie Madillf67115c2014-04-22 13:14:05 -0400402 EXPECT_GL_ERROR(GL_NO_ERROR);
403
404 const GLubyte *pixels[20] = { 0 };
405 glTexSubImage2D(GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
406 EXPECT_GL_ERROR(GL_INVALID_VALUE);
407}
Geoff Langc41e42d2014-04-28 10:58:16 -0400408
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200409TEST_P(Texture2DTest, ZeroSizedUploads)
Geoff Langc41e42d2014-04-28 10:58:16 -0400410{
Jamie Madilld4cfa572014-07-08 10:00:32 -0400411 glBindTexture(GL_TEXTURE_2D, mTexture2D);
Geoff Langc41e42d2014-04-28 10:58:16 -0400412 EXPECT_GL_ERROR(GL_NO_ERROR);
413
414 // Use the texture first to make sure it's in video memory
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200415 glUseProgram(mProgram);
Jamie Madilld4cfa572014-07-08 10:00:32 -0400416 glUniform1i(mTexture2DUniformLocation, 0);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200417 drawQuad(mProgram, "position", 0.5f);
Geoff Langc41e42d2014-04-28 10:58:16 -0400418
419 const GLubyte *pixel[4] = { 0 };
420
421 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
422 EXPECT_GL_NO_ERROR();
423
424 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
425 EXPECT_GL_NO_ERROR();
426
427 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
428 EXPECT_GL_NO_ERROR();
429}
Jamie Madilld4cfa572014-07-08 10:00:32 -0400430
431// Test drawing with two texture types, to trigger an ANGLE bug in validation
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200432TEST_P(TextureCubeTest, CubeMapBug)
Jamie Madilld4cfa572014-07-08 10:00:32 -0400433{
434 glActiveTexture(GL_TEXTURE0);
435 glBindTexture(GL_TEXTURE_2D, mTexture2D);
436 glActiveTexture(GL_TEXTURE1);
437 glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
438 EXPECT_GL_ERROR(GL_NO_ERROR);
439
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200440 glUseProgram(mProgram);
441 glUniform1i(mTexture2DUniformLocation, 0);
442 glUniform1i(mTextureCubeUniformLocation, 1);
443 drawQuad(mProgram, "position", 0.5f);
Jamie Madilld4cfa572014-07-08 10:00:32 -0400444 EXPECT_GL_NO_ERROR();
445}
Jamie Madill9aca0592014-10-06 16:26:59 -0400446
447// Copy of a test in conformance/textures/texture-mips, to test generate mipmaps
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200448TEST_P(Texture2DTestWithDrawScale, MipmapsTwice)
Jamie Madill9aca0592014-10-06 16:26:59 -0400449{
450 int px = getWindowWidth() / 2;
451 int py = getWindowHeight() / 2;
452
453 glActiveTexture(GL_TEXTURE0);
454 glBindTexture(GL_TEXTURE_2D, mTexture2D);
455
456 // Fill with red
457 std::vector<GLubyte> pixels(4 * 16 * 16);
458 for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
459 {
460 pixels[pixelId * 4 + 0] = 255;
461 pixels[pixelId * 4 + 1] = 0;
462 pixels[pixelId * 4 + 2] = 0;
463 pixels[pixelId * 4 + 3] = 255;
464 }
465
466 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
467 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
468 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
469 glGenerateMipmap(GL_TEXTURE_2D);
470
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200471 glUseProgram(mProgram);
Jamie Madill9aca0592014-10-06 16:26:59 -0400472 glUniform1i(mTexture2DUniformLocation, 0);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200473 glUniform2f(mDrawScaleUniformLocation, 0.0625f, 0.0625f);
474 drawQuad(mProgram, "position", 0.5f);
Jamie Madill9aca0592014-10-06 16:26:59 -0400475 EXPECT_GL_NO_ERROR();
476 EXPECT_PIXEL_EQ(px, py, 255, 0, 0, 255);
477
478 // Fill with blue
479 for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
480 {
481 pixels[pixelId * 4 + 0] = 0;
482 pixels[pixelId * 4 + 1] = 0;
483 pixels[pixelId * 4 + 2] = 255;
484 pixels[pixelId * 4 + 3] = 255;
485 }
486
487 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
488 glGenerateMipmap(GL_TEXTURE_2D);
489
490 // Fill with green
491 for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
492 {
493 pixels[pixelId * 4 + 0] = 0;
494 pixels[pixelId * 4 + 1] = 255;
495 pixels[pixelId * 4 + 2] = 0;
496 pixels[pixelId * 4 + 3] = 255;
497 }
498
499 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
500 glGenerateMipmap(GL_TEXTURE_2D);
501
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200502 drawQuad(mProgram, "position", 0.5f);
Jamie Madill9aca0592014-10-06 16:26:59 -0400503
504 EXPECT_GL_NO_ERROR();
505 EXPECT_PIXEL_EQ(px, py, 0, 255, 0, 255);
506}
Jamie Madillf8fccb32014-11-12 15:05:26 -0500507
Jamie Madilleb32a2e2014-12-10 14:27:53 -0500508// Test creating a FBO with a cube map render target, to test an ANGLE bug
509// https://code.google.com/p/angleproject/issues/detail?id=849
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200510TEST_P(TextureCubeTest, CubeMapFBO)
Jamie Madilleb32a2e2014-12-10 14:27:53 -0500511{
512 GLuint fbo;
513 glGenFramebuffers(1, &fbo);
514 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
515
516 glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
517 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, mTextureCube, 0);
518
Corentin Wallez322653b2015-06-17 18:33:56 +0200519 EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
Jamie Madilleb32a2e2014-12-10 14:27:53 -0500520
521 glDeleteFramebuffers(1, &fbo);
522
523 EXPECT_GL_NO_ERROR();
524}
Gregoire Payen de La Garanderie88fe1ad2015-01-19 15:09:26 +0000525
526// Test that glTexSubImage2D works properly when glTexStorage2DEXT has initialized the image with a default color.
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200527TEST_P(Texture2DTest, TexStorage)
Gregoire Payen de La Garanderie88fe1ad2015-01-19 15:09:26 +0000528{
529 int width = getWindowWidth();
530 int height = getWindowHeight();
531
532 GLuint tex2D;
533 glGenTextures(1, &tex2D);
534 glActiveTexture(GL_TEXTURE0);
535 glBindTexture(GL_TEXTURE_2D, tex2D);
536
537 // Fill with red
538 std::vector<GLubyte> pixels(3 * 16 * 16);
539 for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
540 {
541 pixels[pixelId * 3 + 0] = 255;
542 pixels[pixelId * 3 + 1] = 0;
543 pixels[pixelId * 3 + 2] = 0;
544 }
545
546 // ANGLE internally uses RGBA as the DirectX format for RGB images
547 // therefore glTexStorage2DEXT initializes the image to a default color to get a consistent alpha color.
548 // The data is kept in a CPU-side image and the image is marked as dirty.
549 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGB8, 16, 16);
550
551 // Initializes the color of the upper-left 8x8 pixels, leaves the other pixels untouched.
552 // glTexSubImage2D should take into account that the image is dirty.
553 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 8, 8, GL_RGB, GL_UNSIGNED_BYTE, pixels.data());
554 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
555 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
556
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200557 glUseProgram(mProgram);
Gregoire Payen de La Garanderie88fe1ad2015-01-19 15:09:26 +0000558 glUniform1i(mTexture2DUniformLocation, 0);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200559 drawQuad(mProgram, "position", 0.5f);
Gregoire Payen de La Garanderie88fe1ad2015-01-19 15:09:26 +0000560 glDeleteTextures(1, &tex2D);
561 EXPECT_GL_NO_ERROR();
Gregoire Payen de La Garanderie88fe1ad2015-01-19 15:09:26 +0000562 EXPECT_PIXEL_EQ(width / 4, height / 4, 255, 0, 0, 255);
Geoff Langfbfa47c2015-03-31 11:26:00 -0400563
564 // Validate that the region of the texture without data has an alpha of 1.0
565 GLubyte pixel[4];
566 glReadPixels(3 * width / 4, 3 * height / 4, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
567 EXPECT_EQ(pixel[3], 255);
Gregoire Payen de La Garanderie88fe1ad2015-01-19 15:09:26 +0000568}
569
570// Test that glTexSubImage2D combined with a PBO works properly when glTexStorage2DEXT has initialized the image with a default color.
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200571TEST_P(Texture2DTest, TexStorageWithPBO)
Gregoire Payen de La Garanderie88fe1ad2015-01-19 15:09:26 +0000572{
573 if (extensionEnabled("NV_pixel_buffer_object"))
574 {
575 int width = getWindowWidth();
576 int height = getWindowHeight();
577
578 GLuint tex2D;
579 glGenTextures(1, &tex2D);
580 glActiveTexture(GL_TEXTURE0);
581 glBindTexture(GL_TEXTURE_2D, tex2D);
582
583 // Fill with red
584 std::vector<GLubyte> pixels(3 * 16 * 16);
585 for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
586 {
587 pixels[pixelId * 3 + 0] = 255;
588 pixels[pixelId * 3 + 1] = 0;
589 pixels[pixelId * 3 + 2] = 0;
590 }
591
592 // Read 16x16 region from red backbuffer to PBO
593 GLuint pbo;
594 glGenBuffers(1, &pbo);
595 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
596 glBufferData(GL_PIXEL_UNPACK_BUFFER, 3 * 16 * 16, pixels.data(), GL_STATIC_DRAW);
597
598 // ANGLE internally uses RGBA as the DirectX format for RGB images
599 // therefore glTexStorage2DEXT initializes the image to a default color to get a consistent alpha color.
600 // The data is kept in a CPU-side image and the image is marked as dirty.
601 glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGB8, 16, 16);
602
603 // Initializes the color of the upper-left 8x8 pixels, leaves the other pixels untouched.
604 // glTexSubImage2D should take into account that the image is dirty.
605 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 8, 8, GL_RGB, GL_UNSIGNED_BYTE, NULL);
606 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
607 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
608
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200609 glUseProgram(mProgram);
Gregoire Payen de La Garanderie88fe1ad2015-01-19 15:09:26 +0000610 glUniform1i(mTexture2DUniformLocation, 0);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200611 drawQuad(mProgram, "position", 0.5f);
Gregoire Payen de La Garanderie88fe1ad2015-01-19 15:09:26 +0000612 glDeleteTextures(1, &tex2D);
613 glDeleteTextures(1, &pbo);
614 EXPECT_GL_NO_ERROR();
615 EXPECT_PIXEL_EQ(3 * width / 4, 3 * height / 4, 0, 0, 0, 255);
616 EXPECT_PIXEL_EQ(width / 4, height / 4, 255, 0, 0, 255);
617 }
618}
Jamie Madillbc393df2015-01-29 13:46:07 -0500619
620// See description on testFloatCopySubImage
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200621TEST_P(Texture2DTest, CopySubImageFloat_R_R)
Jamie Madillbc393df2015-01-29 13:46:07 -0500622{
623 testFloatCopySubImage(1, 1);
624}
625
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200626TEST_P(Texture2DTest, CopySubImageFloat_RG_R)
Jamie Madillbc393df2015-01-29 13:46:07 -0500627{
628 testFloatCopySubImage(2, 1);
629}
630
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200631TEST_P(Texture2DTest, CopySubImageFloat_RG_RG)
Jamie Madillbc393df2015-01-29 13:46:07 -0500632{
633 testFloatCopySubImage(2, 2);
634}
635
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200636TEST_P(Texture2DTest, CopySubImageFloat_RGB_R)
Jamie Madillbc393df2015-01-29 13:46:07 -0500637{
638 testFloatCopySubImage(3, 1);
639}
640
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200641TEST_P(Texture2DTest, CopySubImageFloat_RGB_RG)
Jamie Madillbc393df2015-01-29 13:46:07 -0500642{
643 testFloatCopySubImage(3, 2);
644}
645
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200646TEST_P(Texture2DTest, CopySubImageFloat_RGB_RGB)
Jamie Madillbc393df2015-01-29 13:46:07 -0500647{
648 testFloatCopySubImage(3, 3);
649}
650
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200651TEST_P(Texture2DTest, CopySubImageFloat_RGBA_R)
Jamie Madillbc393df2015-01-29 13:46:07 -0500652{
653 testFloatCopySubImage(4, 1);
654}
655
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200656TEST_P(Texture2DTest, CopySubImageFloat_RGBA_RG)
Jamie Madillbc393df2015-01-29 13:46:07 -0500657{
658 testFloatCopySubImage(4, 2);
659}
660
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200661TEST_P(Texture2DTest, CopySubImageFloat_RGBA_RGB)
Jamie Madillbc393df2015-01-29 13:46:07 -0500662{
663 testFloatCopySubImage(4, 3);
664}
665
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200666TEST_P(Texture2DTest, CopySubImageFloat_RGBA_RGBA)
Jamie Madillbc393df2015-01-29 13:46:07 -0500667{
668 testFloatCopySubImage(4, 4);
669}
Austin Kinross07285142015-03-26 11:36:16 -0700670
671// Port of https://www.khronos.org/registry/webgl/conformance-suites/1.0.3/conformance/textures/texture-npot.html
672// Run against GL_ALPHA/UNSIGNED_BYTE format, to ensure that D3D11 Feature Level 9_3 correctly handles GL_ALPHA
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200673TEST_P(Texture2DTest, TextureNPOT_GL_ALPHA_UBYTE)
Austin Kinross07285142015-03-26 11:36:16 -0700674{
675 const int npotTexSize = 5;
676 const int potTexSize = 4; // Should be less than npotTexSize
677 GLuint tex2D;
678
679 if (extensionEnabled("GL_OES_texture_npot"))
680 {
681 // This test isn't applicable if texture_npot is enabled
682 return;
683 }
684
685 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
686
687 glActiveTexture(GL_TEXTURE0);
688 glGenTextures(1, &tex2D);
689 glBindTexture(GL_TEXTURE_2D, tex2D);
690
691 std::vector<GLubyte> pixels(1 * npotTexSize * npotTexSize);
692 for (size_t pixelId = 0; pixelId < npotTexSize * npotTexSize; ++pixelId)
693 {
694 pixels[pixelId] = 64;
695 }
696
697 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
698 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
699
700 // Check that an NPOT texture not on level 0 generates INVALID_VALUE
701 glTexImage2D(GL_TEXTURE_2D, 1, GL_ALPHA, npotTexSize, npotTexSize, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels.data());
702 EXPECT_GL_ERROR(GL_INVALID_VALUE);
703
704 // Check that an NPOT texture on level 0 succeeds
705 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, npotTexSize, npotTexSize, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels.data());
706 EXPECT_GL_NO_ERROR();
707
708 // Check that generateMipmap fails on NPOT
709 glGenerateMipmap(GL_TEXTURE_2D);
710 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
711
712 // Check that nothing is drawn if filtering is not correct for NPOT
713 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
714 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
715 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
716 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
717 glClear(GL_COLOR_BUFFER_BIT);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200718 drawQuad(mProgram, "position", 1.0f);
Austin Kinross07285142015-03-26 11:36:16 -0700719 EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 0, 0, 0, 255);
720
721 // NPOT texture with TEXTURE_MIN_FILTER not NEAREST or LINEAR should draw with 0,0,0,255
722 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
723 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
724 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
725 glClear(GL_COLOR_BUFFER_BIT);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200726 drawQuad(mProgram, "position", 1.0f);
Austin Kinross07285142015-03-26 11:36:16 -0700727 EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 0, 0, 0, 255);
728
729 // NPOT texture with TEXTURE_MIN_FILTER set to LINEAR should draw
730 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
731 glClear(GL_COLOR_BUFFER_BIT);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200732 drawQuad(mProgram, "position", 1.0f);
Austin Kinross07285142015-03-26 11:36:16 -0700733 EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 0, 0, 0, 64);
734
735 // Check that glTexImage2D for POT texture succeeds
736 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, potTexSize, potTexSize, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels.data());
737 EXPECT_GL_NO_ERROR();
738
739 // Check that generateMipmap for an POT texture succeeds
740 glGenerateMipmap(GL_TEXTURE_2D);
741 EXPECT_GL_NO_ERROR();
742
743 // POT texture with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw
744 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
745 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
746 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
747 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
748 glClear(GL_COLOR_BUFFER_BIT);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200749 drawQuad(mProgram, "position", 1.0f);
Austin Kinross07285142015-03-26 11:36:16 -0700750 EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 0, 0, 0, 64);
751 EXPECT_GL_NO_ERROR();
752}
Jamie Madillfa05f602015-05-07 13:47:11 -0400753
Austin Kinross08528e12015-10-07 16:24:40 -0700754// Test to ensure that glTexSubImage2D always accepts data for non-power-of-two subregions.
755// ANGLE previously rejected this if GL_OES_texture_npot wasn't active, which is incorrect.
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200756TEST_P(Texture2DTest, NPOTSubImageParameters)
Austin Kinross08528e12015-10-07 16:24:40 -0700757{
758 glActiveTexture(GL_TEXTURE0);
759 glBindTexture(GL_TEXTURE_2D, mTexture2D);
760
761 // Create an 8x8 (i.e. power-of-two) texture.
762 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
763 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
764 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
765 glGenerateMipmap(GL_TEXTURE_2D);
766
767 // Supply a 3x3 (i.e. non-power-of-two) subimage to the texture.
768 // This should always work, even if GL_OES_texture_npot isn't active.
769 glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, 3, 3, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
770
771 EXPECT_GL_NO_ERROR();
772}
773
Jamie Madill2453dbc2015-07-14 11:35:42 -0400774// In the D3D11 renderer, we need to initialize some texture formats, to fill empty channels. EG RBA->RGBA8, with 1.0
775// in the alpha channel. This test covers a bug where redefining array textures with these formats does not work as
776// expected.
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200777TEST_P(Texture2DArrayTestES3, RedefineInittableArray)
Jamie Madill2453dbc2015-07-14 11:35:42 -0400778{
779 std::vector<GLubyte> pixelData;
780 for (size_t count = 0; count < 5000; count++)
781 {
782 pixelData.push_back(0u);
783 pixelData.push_back(255u);
784 pixelData.push_back(0u);
785 }
786
787 glBindTexture(GL_TEXTURE_2D_ARRAY, m2DArrayTexture);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200788 glUseProgram(mProgram);
Jamie Madill2453dbc2015-07-14 11:35:42 -0400789 glUniform1i(mTextureArrayLocation, 0);
790
791 // The first draw worked correctly.
792 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGB, 4, 4, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, &pixelData[0]);
793
794 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
795 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
796 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
797 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200798 drawQuad(mProgram, "position", 1.0f);
Jamie Madill2453dbc2015-07-14 11:35:42 -0400799 EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255);
800
801 // The dimension of the respecification must match the original exactly to trigger the bug.
802 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGB, 4, 4, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, &pixelData[0]);
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200803 drawQuad(mProgram, "position", 1.0f);
Jamie Madill2453dbc2015-07-14 11:35:42 -0400804 EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255);
805
806 ASSERT_GL_NO_ERROR();
807}
808
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400809class TextureLimitsTest : public ANGLETest
810{
811 protected:
812 struct RGBA8
813 {
814 uint8_t R, G, B, A;
815 };
816
817 TextureLimitsTest()
818 : mProgram(0), mMaxVertexTextures(0), mMaxFragmentTextures(0), mMaxCombinedTextures(0)
819 {
820 setWindowWidth(128);
821 setWindowHeight(128);
822 setConfigRedBits(8);
823 setConfigGreenBits(8);
824 setConfigBlueBits(8);
825 setConfigAlphaBits(8);
826 }
827
828 ~TextureLimitsTest()
829 {
830 if (mProgram != 0)
831 {
832 glDeleteProgram(mProgram);
833 mProgram = 0;
834
835 if (!mTextures.empty())
836 {
837 glDeleteTextures(static_cast<GLsizei>(mTextures.size()), &mTextures[0]);
838 }
839 }
840 }
841
842 void SetUp() override
843 {
844 ANGLETest::SetUp();
845
846 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mMaxVertexTextures);
847 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mMaxFragmentTextures);
848 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mMaxCombinedTextures);
849
850 ASSERT_GL_NO_ERROR();
851 }
852
853 void compileProgramWithTextureCounts(const std::string &vertexPrefix,
854 GLint vertexTextureCount,
855 GLint vertexActiveTextureCount,
856 const std::string &fragPrefix,
857 GLint fragmentTextureCount,
858 GLint fragmentActiveTextureCount)
859 {
860 std::stringstream vertexShaderStr;
861 vertexShaderStr << "attribute vec2 position;\n"
862 << "varying vec4 color;\n"
863 << "varying vec2 texCoord;\n";
864
865 for (GLint textureIndex = 0; textureIndex < vertexTextureCount; ++textureIndex)
866 {
867 vertexShaderStr << "uniform sampler2D " << vertexPrefix << textureIndex << ";\n";
868 }
869
870 vertexShaderStr << "void main() {\n"
871 << " gl_Position = vec4(position, 0, 1);\n"
872 << " texCoord = (position * 0.5) + 0.5;\n"
873 << " color = vec4(0);\n";
874
875 for (GLint textureIndex = 0; textureIndex < vertexActiveTextureCount; ++textureIndex)
876 {
877 vertexShaderStr << " color += texture2D(" << vertexPrefix << textureIndex
878 << ", texCoord);\n";
879 }
880
881 vertexShaderStr << "}";
882
883 std::stringstream fragmentShaderStr;
884 fragmentShaderStr << "varying mediump vec4 color;\n"
885 << "varying mediump vec2 texCoord;\n";
886
887 for (GLint textureIndex = 0; textureIndex < fragmentTextureCount; ++textureIndex)
888 {
889 fragmentShaderStr << "uniform sampler2D " << fragPrefix << textureIndex << ";\n";
890 }
891
892 fragmentShaderStr << "void main() {\n"
893 << " gl_FragColor = color;\n";
894
895 for (GLint textureIndex = 0; textureIndex < fragmentActiveTextureCount; ++textureIndex)
896 {
897 fragmentShaderStr << " gl_FragColor += texture2D(" << fragPrefix << textureIndex
898 << ", texCoord);\n";
899 }
900
901 fragmentShaderStr << "}";
902
903 const std::string &vertexShaderSource = vertexShaderStr.str();
904 const std::string &fragmentShaderSource = fragmentShaderStr.str();
905
906 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
907 }
908
909 RGBA8 getPixel(GLint texIndex)
910 {
911 RGBA8 pixel = {static_cast<uint8_t>(texIndex & 0x7u), static_cast<uint8_t>(texIndex >> 3),
912 0, 255u};
913 return pixel;
914 }
915
916 void initTextures(GLint tex2DCount, GLint texCubeCount)
917 {
918 GLint totalCount = tex2DCount + texCubeCount;
919 mTextures.assign(totalCount, 0);
920 glGenTextures(totalCount, &mTextures[0]);
921 ASSERT_GL_NO_ERROR();
922
923 std::vector<RGBA8> texData(16 * 16);
924
925 GLint texIndex = 0;
926 for (; texIndex < tex2DCount; ++texIndex)
927 {
928 texData.assign(texData.size(), getPixel(texIndex));
929 glActiveTexture(GL_TEXTURE0 + texIndex);
930 glBindTexture(GL_TEXTURE_2D, mTextures[texIndex]);
931 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE,
932 &texData[0]);
933 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
934 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
935 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
936 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
937 }
938
939 ASSERT_GL_NO_ERROR();
940
941 for (; texIndex < texCubeCount; ++texIndex)
942 {
943 texData.assign(texData.size(), getPixel(texIndex));
944 glActiveTexture(GL_TEXTURE0 + texIndex);
945 glBindTexture(GL_TEXTURE_CUBE_MAP, mTextures[texIndex]);
946 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
947 GL_UNSIGNED_BYTE, &texData[0]);
948 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
949 GL_UNSIGNED_BYTE, &texData[0]);
950 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
951 GL_UNSIGNED_BYTE, &texData[0]);
952 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
953 GL_UNSIGNED_BYTE, &texData[0]);
954 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
955 GL_UNSIGNED_BYTE, &texData[0]);
956 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
957 GL_UNSIGNED_BYTE, &texData[0]);
958 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
959 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
960 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
961 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
962 }
963
964 ASSERT_GL_NO_ERROR();
965 }
966
967 void testWithTextures(GLint vertexTextureCount,
968 const std::string &vertexTexturePrefix,
969 GLint fragmentTextureCount,
970 const std::string &fragmentTexturePrefix)
971 {
972 // Generate textures
973 initTextures(vertexTextureCount + fragmentTextureCount, 0);
974
975 glUseProgram(mProgram);
976 RGBA8 expectedSum = {0};
977 for (GLint texIndex = 0; texIndex < vertexTextureCount; ++texIndex)
978 {
979 std::stringstream uniformNameStr;
980 uniformNameStr << vertexTexturePrefix << texIndex;
981 const std::string &uniformName = uniformNameStr.str();
982 GLint location = glGetUniformLocation(mProgram, uniformName.c_str());
983 ASSERT_NE(-1, location);
984
985 glUniform1i(location, texIndex);
986 RGBA8 contribution = getPixel(texIndex);
987 expectedSum.R += contribution.R;
988 expectedSum.G += contribution.G;
989 }
990
991 for (GLint texIndex = 0; texIndex < fragmentTextureCount; ++texIndex)
992 {
993 std::stringstream uniformNameStr;
994 uniformNameStr << fragmentTexturePrefix << texIndex;
995 const std::string &uniformName = uniformNameStr.str();
996 GLint location = glGetUniformLocation(mProgram, uniformName.c_str());
997 ASSERT_NE(-1, location);
998
999 glUniform1i(location, texIndex + vertexTextureCount);
1000 RGBA8 contribution = getPixel(texIndex + vertexTextureCount);
1001 expectedSum.R += contribution.R;
1002 expectedSum.G += contribution.G;
1003 }
1004
1005 ASSERT_GE(256u, expectedSum.G);
1006
1007 drawQuad(mProgram, "position", 0.5f);
1008 ASSERT_GL_NO_ERROR();
1009 EXPECT_PIXEL_EQ(0, 0, expectedSum.R, expectedSum.G, 0, 255);
1010 }
1011
1012 GLuint mProgram;
1013 std::vector<GLuint> mTextures;
1014 GLint mMaxVertexTextures;
1015 GLint mMaxFragmentTextures;
1016 GLint mMaxCombinedTextures;
1017};
1018
1019// Test rendering with the maximum vertex texture units.
1020TEST_P(TextureLimitsTest, MaxVertexTextures)
1021{
Jamie Madill1ea9aaa2015-10-07 11:13:55 -04001022 // TODO(jmadill): Figure out why this fails on Intel.
1023 if (isIntel() && GetParam().getRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
1024 {
1025 std::cout << "Test skipped on Intel." << std::endl;
1026 return;
1027 }
1028
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001029 compileProgramWithTextureCounts("tex", mMaxVertexTextures, mMaxVertexTextures, "tex", 0, 0);
1030 ASSERT_NE(0u, mProgram);
1031 ASSERT_GL_NO_ERROR();
1032
1033 testWithTextures(mMaxVertexTextures, "tex", 0, "tex");
1034}
1035
1036// Test rendering with the maximum fragment texture units.
1037TEST_P(TextureLimitsTest, MaxFragmentTextures)
1038{
Jamie Madill1ea9aaa2015-10-07 11:13:55 -04001039 // TODO(jmadill): Figure out why this fails on Intel.
1040 if (isIntel() && GetParam().getRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
1041 {
1042 std::cout << "Test skipped on Intel." << std::endl;
1043 return;
1044 }
1045
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001046 compileProgramWithTextureCounts("tex", 0, 0, "tex", mMaxFragmentTextures, mMaxFragmentTextures);
1047 ASSERT_NE(0u, mProgram);
1048 ASSERT_GL_NO_ERROR();
1049
1050 testWithTextures(mMaxFragmentTextures, "tex", 0, "tex");
1051}
1052
1053// Test rendering with maximum combined texture units.
1054TEST_P(TextureLimitsTest, MaxCombinedTextures)
1055{
Jamie Madill412f17d2015-09-25 08:43:54 -04001056 // TODO(jmadill): Investigate workaround.
1057 if (isIntel() && GetParam() == ES2_OPENGL())
1058 {
1059 std::cout << "Test skipped on Intel." << std::endl;
1060 return;
1061 }
1062
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001063 GLint vertexTextures = mMaxVertexTextures;
1064
1065 if (vertexTextures + mMaxFragmentTextures > mMaxCombinedTextures)
1066 {
1067 vertexTextures = mMaxCombinedTextures - mMaxFragmentTextures;
1068 }
1069
1070 compileProgramWithTextureCounts("vtex", vertexTextures, vertexTextures, "ftex",
1071 mMaxFragmentTextures, mMaxFragmentTextures);
1072 ASSERT_NE(0u, mProgram);
1073 ASSERT_GL_NO_ERROR();
1074
1075 testWithTextures(vertexTextures, "vtex", mMaxFragmentTextures, "ftex");
1076}
1077
1078// Negative test for exceeding the number of vertex textures
1079TEST_P(TextureLimitsTest, ExcessiveVertexTextures)
1080{
1081 compileProgramWithTextureCounts("tex", mMaxVertexTextures + 1, mMaxVertexTextures + 1, "tex", 0,
1082 0);
1083 ASSERT_EQ(0u, mProgram);
1084}
1085
1086// Negative test for exceeding the number of fragment textures
1087TEST_P(TextureLimitsTest, ExcessiveFragmentTextures)
1088{
1089 compileProgramWithTextureCounts("tex", 0, 0, "tex", mMaxFragmentTextures + 1,
1090 mMaxFragmentTextures + 1);
1091 ASSERT_EQ(0u, mProgram);
1092}
1093
1094// Test active vertex textures under the limit, but excessive textures specified.
1095TEST_P(TextureLimitsTest, MaxActiveVertexTextures)
1096{
Jamie Madill1ea9aaa2015-10-07 11:13:55 -04001097 // TODO(jmadill): Figure out why this fails on Intel.
1098 if (isIntel() && GetParam().getRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
1099 {
1100 std::cout << "Test skipped on Intel." << std::endl;
1101 return;
1102 }
1103
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001104 compileProgramWithTextureCounts("tex", mMaxVertexTextures + 4, mMaxVertexTextures, "tex", 0, 0);
1105 ASSERT_NE(0u, mProgram);
1106 ASSERT_GL_NO_ERROR();
1107
1108 testWithTextures(mMaxVertexTextures, "tex", 0, "tex");
1109}
1110
1111// Test active fragment textures under the limit, but excessive textures specified.
1112TEST_P(TextureLimitsTest, MaxActiveFragmentTextures)
1113{
Jamie Madill1ea9aaa2015-10-07 11:13:55 -04001114 // TODO(jmadill): Figure out why this fails on Intel.
1115 if (isIntel() && GetParam().getRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
1116 {
1117 std::cout << "Test skipped on Intel." << std::endl;
1118 return;
1119 }
1120
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001121 compileProgramWithTextureCounts("tex", 0, 0, "tex", mMaxFragmentTextures + 4,
1122 mMaxFragmentTextures);
1123 ASSERT_NE(0u, mProgram);
1124 ASSERT_GL_NO_ERROR();
1125
1126 testWithTextures(0, "tex", mMaxFragmentTextures, "tex");
1127}
1128
1129// Negative test for pointing two sampler uniforms of different types to the same texture.
Olli Etuaho4a8329f2016-01-11 17:12:57 +02001130// GLES 2.0.25 section 2.10.4 page 39.
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001131TEST_P(TextureLimitsTest, TextureTypeConflict)
1132{
1133 const std::string &vertexShader =
1134 "attribute vec2 position;\n"
1135 "varying float color;\n"
1136 "uniform sampler2D tex2D;\n"
1137 "uniform samplerCube texCube;\n"
1138 "void main() {\n"
1139 " gl_Position = vec4(position, 0, 1);\n"
1140 " vec2 texCoord = (position * 0.5) + 0.5;\n"
1141 " color = texture2D(tex2D, texCoord).x;\n"
1142 " color += textureCube(texCube, vec3(texCoord, 0)).x;\n"
1143 "}";
1144 const std::string &fragmentShader =
1145 "varying mediump float color;\n"
1146 "void main() {\n"
1147 " gl_FragColor = vec4(color, 0, 0, 1);\n"
1148 "}";
1149
1150 mProgram = CompileProgram(vertexShader, fragmentShader);
1151 ASSERT_NE(0u, mProgram);
1152
1153 initTextures(1, 0);
1154
1155 glUseProgram(mProgram);
1156 GLint tex2DLocation = glGetUniformLocation(mProgram, "tex2D");
1157 ASSERT_NE(-1, tex2DLocation);
1158 GLint texCubeLocation = glGetUniformLocation(mProgram, "texCube");
1159 ASSERT_NE(-1, texCubeLocation);
1160
1161 glUniform1i(tex2DLocation, 0);
1162 glUniform1i(texCubeLocation, 0);
1163 ASSERT_GL_NO_ERROR();
1164
1165 drawQuad(mProgram, "position", 0.5f);
1166 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1167}
1168
1169// Negative test for rendering with texture outside the valid range.
Olli Etuaho4a8329f2016-01-11 17:12:57 +02001170// TODO(jmadill): Possibly adjust the test according to the spec:
1171// GLES 3.0.4 section 2.12.7 mentions that specifying an out-of-range sampler uniform value
1172// generates an INVALID_VALUE error - GLES 2.0 doesn't yet have this mention.
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001173TEST_P(TextureLimitsTest, DrawWithTexturePastMaximum)
1174{
1175 const std::string &vertexShader =
1176 "attribute vec2 position;\n"
1177 "varying float color;\n"
1178 "uniform sampler2D tex2D;\n"
1179 "void main() {\n"
1180 " gl_Position = vec4(position, 0, 1);\n"
1181 " vec2 texCoord = (position * 0.5) + 0.5;\n"
1182 " color = texture2D(tex2D, texCoord).x;\n"
1183 "}";
1184 const std::string &fragmentShader =
1185 "varying mediump float color;\n"
1186 "void main() {\n"
1187 " gl_FragColor = vec4(color, 0, 0, 1);\n"
1188 "}";
1189
1190 mProgram = CompileProgram(vertexShader, fragmentShader);
1191 ASSERT_NE(0u, mProgram);
1192
1193 glUseProgram(mProgram);
1194 GLint tex2DLocation = glGetUniformLocation(mProgram, "tex2D");
1195 ASSERT_NE(-1, tex2DLocation);
1196
1197 glUniform1i(tex2DLocation, mMaxCombinedTextures);
1198 ASSERT_GL_NO_ERROR();
1199
1200 drawQuad(mProgram, "position", 0.5f);
1201 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1202}
1203
Jamie Madillfa05f602015-05-07 13:47:11 -04001204// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
Olli Etuaho4a8329f2016-01-11 17:12:57 +02001205// TODO(geofflang): Figure out why tests below fail on Intel OpenGL:
1206ANGLE_INSTANTIATE_TEST(Texture2DTest, ES2_D3D9(), ES2_D3D11(), ES2_D3D11_FL9_3());
1207ANGLE_INSTANTIATE_TEST(TextureCubeTest, ES2_D3D9(), ES2_D3D11(), ES2_D3D11_FL9_3());
1208ANGLE_INSTANTIATE_TEST(Texture2DTestWithDrawScale, ES2_D3D9(), ES2_D3D11(), ES2_D3D11_FL9_3());
1209ANGLE_INSTANTIATE_TEST(Texture2DArrayTestES3, ES3_D3D11(), ES3_OPENGL());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001210ANGLE_INSTANTIATE_TEST(TextureLimitsTest, ES2_D3D11(), ES2_OPENGL());
Jamie Madillfa05f602015-05-07 13:47:11 -04001211
1212} // namespace