blob: d3f105fd76370ca75a51bcce59a3036a94e6dfb5 [file] [log] [blame]
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "common/mathutil.h"
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
using namespace angle;
namespace
{
constexpr GLuint kPixelTolerance = 1u;
constexpr GLfloat kPixelTolerance32F = 0.01f;
// Single compressed ETC2 block of source pixels all set red
constexpr uint8_t kCompressedImageETC2[] = {0x7E, 0x80, 0x04, 0x7F, 0x00, 0x07, 0xE0, 0x00};
// Take a pixel, and reset the components not covered by the format to default
// values. In particular, the default value for the alpha component is 255
// (1.0 as unsigned normalized fixed point value).
// For legacy formats, the components may be reordered to match the color that
// would be created if a pixel of that format was initialized from the given color
GLColor SliceFormatColor(GLenum format, GLColor full)
{
switch (format)
{
case GL_RED:
return GLColor(full.R, 0, 0, 255u);
case GL_RG:
return GLColor(full.R, full.G, 0, 255u);
case GL_RGB:
return GLColor(full.R, full.G, full.B, 255u);
case GL_RGBA:
return full;
case GL_LUMINANCE:
return GLColor(full.R, full.R, full.R, 255u);
case GL_ALPHA:
return GLColor(0, 0, 0, full.R);
case GL_LUMINANCE_ALPHA:
return GLColor(full.R, full.R, full.R, full.G);
default:
EXPECT_TRUE(false);
return GLColor::white;
}
}
GLColor16UI SliceFormatColor16UI(GLenum format, GLColor16UI full)
{
switch (format)
{
case GL_RED:
return GLColor16UI(full.R, 0, 0, 0xFFFF);
case GL_RG:
return GLColor16UI(full.R, full.G, 0, 0xFFFF);
case GL_RGB:
return GLColor16UI(full.R, full.G, full.B, 0xFFFF);
case GL_RGBA:
return full;
case GL_LUMINANCE:
return GLColor16UI(full.R, full.R, full.R, 0xFFFF);
case GL_ALPHA:
return GLColor16UI(0, 0, 0, full.R);
case GL_LUMINANCE_ALPHA:
return GLColor16UI(full.R, full.R, full.R, full.G);
default:
EXPECT_TRUE(false);
return GLColor16UI(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF);
}
}
// As above, for 32F colors
GLColor32F SliceFormatColor32F(GLenum format, GLColor32F full)
{
switch (format)
{
case GL_RED:
return GLColor32F(full.R, 0.0f, 0.0f, 1.0f);
case GL_RG:
return GLColor32F(full.R, full.G, 0.0f, 1.0f);
case GL_RGB:
return GLColor32F(full.R, full.G, full.B, 1.0f);
case GL_RGBA:
return full;
case GL_LUMINANCE:
return GLColor32F(full.R, full.R, full.R, 1.0f);
case GL_ALPHA:
return GLColor32F(0.0f, 0.0f, 0.0f, full.R);
case GL_LUMINANCE_ALPHA:
return GLColor32F(full.R, full.R, full.R, full.G);
default:
EXPECT_TRUE(false);
return GLColor32F(1.0f, 1.0f, 1.0f, 1.0f);
}
}
class TexCoordDrawTest : public ANGLETest
{
protected:
TexCoordDrawTest() : ANGLETest(), mProgram(0), mFramebuffer(0), mFramebufferColorTexture(0)
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
virtual const char *getVertexShaderSource()
{
return R"(precision highp float;
attribute vec4 position;
varying vec2 texcoord;
void main()
{
gl_Position = vec4(position.xy, 0.0, 1.0);
texcoord = (position.xy * 0.5) + 0.5;
})";
}
virtual const char *getFragmentShaderSource() = 0;
virtual void setUpProgram()
{
const char *vertexShaderSource = getVertexShaderSource();
const char *fragmentShaderSource = getFragmentShaderSource();
mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
ASSERT_NE(0u, mProgram);
ASSERT_GL_NO_ERROR();
}
void testSetUp() override { setUpFramebuffer(); }
void testTearDown() override
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &mFramebuffer);
glDeleteTextures(1, &mFramebufferColorTexture);
glDeleteProgram(mProgram);
}
void setUpFramebuffer()
{
// We use an FBO to work around an issue where the default framebuffer applies SRGB
// conversion (particularly known to happen incorrectly on Intel GL drivers). It's not
// clear whether this issue can even be fixed on all backends. For example GLES 3.0.4 spec
// section 4.4 says that the format of the default framebuffer is entirely up to the window
// system, so it might be SRGB, and GLES 3.0 doesn't have a "FRAMEBUFFER_SRGB" to turn off
// SRGB conversion like desktop GL does.
// TODO(oetuaho): Get rid of this if the underlying issue is fixed.
glGenFramebuffers(1, &mFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
glGenTextures(1, &mFramebufferColorTexture);
glBindTexture(GL_TEXTURE_2D, mFramebufferColorTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
mFramebufferColorTexture, 0);
ASSERT_GL_NO_ERROR();
ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
glBindTexture(GL_TEXTURE_2D, 0);
}
// Returns the created texture ID.
GLuint create2DTexture()
{
GLuint texture2D;
glGenTextures(1, &texture2D);
glBindTexture(GL_TEXTURE_2D, texture2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
EXPECT_GL_NO_ERROR();
return texture2D;
}
GLuint mProgram;
GLuint mFramebuffer;
private:
GLuint mFramebufferColorTexture;
};
class Texture2DTest : public TexCoordDrawTest
{
protected:
Texture2DTest() : TexCoordDrawTest(), mTexture2D(0), mTexture2DUniformLocation(-1) {}
const char *getFragmentShaderSource() override
{
return R"(precision highp float;
uniform sampler2D tex;
varying vec2 texcoord;
void main()
{
gl_FragColor = texture2D(tex, texcoord);
})";
}
virtual const char *getTextureUniformName() { return "tex"; }
void setUpProgram() override
{
TexCoordDrawTest::setUpProgram();
mTexture2DUniformLocation = glGetUniformLocation(mProgram, getTextureUniformName());
ASSERT_NE(-1, mTexture2DUniformLocation);
}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
mTexture2D = create2DTexture();
ASSERT_GL_NO_ERROR();
}
void testTearDown() override
{
glDeleteTextures(1, &mTexture2D);
TexCoordDrawTest::testTearDown();
}
// Tests CopyTexSubImage with floating point textures of various formats.
void testFloatCopySubImage(int sourceImageChannels, int destImageChannels)
{
setUpProgram();
if (getClientMajorVersion() < 3)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_texture_storage") ||
!IsGLExtensionEnabled("GL_OES_texture_float"));
ANGLE_SKIP_TEST_IF((sourceImageChannels < 3 || destImageChannels < 3) &&
!IsGLExtensionEnabled("GL_EXT_texture_rg"));
ANGLE_SKIP_TEST_IF(destImageChannels == 3 &&
!IsGLExtensionEnabled("GL_CHROMIUM_color_buffer_float_rgb"));
ANGLE_SKIP_TEST_IF(destImageChannels == 4 &&
!IsGLExtensionEnabled("GL_CHROMIUM_color_buffer_float_rgba"));
ANGLE_SKIP_TEST_IF(destImageChannels <= 2);
}
else
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_color_buffer_float"));
ANGLE_SKIP_TEST_IF(destImageChannels == 3 &&
!IsGLExtensionEnabled("GL_CHROMIUM_color_buffer_float_rgb"));
}
// clang-format off
GLfloat sourceImageData[4][16] =
{
{ // R
1.0f,
0.0f,
0.0f,
1.0f
},
{ // RG
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f
},
{ // RGB
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f
},
{ // RGBA
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f
},
};
// clang-format on
GLenum imageFormats[] = {
GL_R32F,
GL_RG32F,
GL_RGB32F,
GL_RGBA32F,
};
GLenum sourceUnsizedFormats[] = {
GL_RED,
GL_RG,
GL_RGB,
GL_RGBA,
};
GLuint textures[2];
glGenTextures(2, textures);
GLfloat *imageData = sourceImageData[sourceImageChannels - 1];
GLenum sourceImageFormat = imageFormats[sourceImageChannels - 1];
GLenum sourceUnsizedFormat = sourceUnsizedFormats[sourceImageChannels - 1];
GLenum destImageFormat = imageFormats[destImageChannels - 1];
glBindTexture(GL_TEXTURE_2D, textures[0]);
if (getClientMajorVersion() >= 3)
{
glTexStorage2D(GL_TEXTURE_2D, 1, sourceImageFormat, 2, 2);
}
else
{
glTexStorage2DEXT(GL_TEXTURE_2D, 1, sourceImageFormat, 2, 2);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, sourceUnsizedFormat, GL_FLOAT, imageData);
if (sourceImageChannels < 3 && !IsGLExtensionEnabled("GL_EXT_texture_rg"))
{
// This is not supported
ASSERT_GL_ERROR(GL_INVALID_OPERATION);
}
else
{
ASSERT_GL_NO_ERROR();
}
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
glBindTexture(GL_TEXTURE_2D, textures[1]);
if (getClientMajorVersion() >= 3)
{
glTexStorage2D(GL_TEXTURE_2D, 1, destImageFormat, 2, 2);
}
else
{
glTexStorage2DEXT(GL_TEXTURE_2D, 1, destImageFormat, 2, 2);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 2, 2);
ASSERT_GL_NO_ERROR();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
drawQuad(mProgram, "position", 0.5f);
int testImageChannels = std::min(sourceImageChannels, destImageChannels);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
if (testImageChannels > 1)
{
EXPECT_PIXEL_EQ(getWindowHeight() - 1, 0, 0, 255, 0, 255);
EXPECT_PIXEL_EQ(getWindowHeight() - 1, getWindowWidth() - 1, 255, 255, 0, 255);
if (testImageChannels > 2)
{
EXPECT_PIXEL_EQ(0, getWindowWidth() - 1, 0, 0, 255, 255);
}
}
glDeleteFramebuffers(1, &fbo);
glDeleteTextures(2, textures);
ASSERT_GL_NO_ERROR();
}
GLuint mTexture2D;
GLint mTexture2DUniformLocation;
};
class Texture2DTestES3 : public Texture2DTest
{
protected:
Texture2DTestES3() : Texture2DTest() {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp sampler2D tex;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = texture(tex, texcoord);\n"
"}\n";
}
void testSetUp() override
{
Texture2DTest::testSetUp();
setUpProgram();
}
};
class Texture2DIntegerAlpha1TestES3 : public Texture2DTest
{
protected:
Texture2DIntegerAlpha1TestES3() : Texture2DTest() {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp isampler2D tex;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" vec4 green = vec4(0, 1, 0, 1);\n"
" vec4 black = vec4(0, 0, 0, 0);\n"
" fragColor = (texture(tex, texcoord).a == 1) ? green : black;\n"
"}\n";
}
void testSetUp() override
{
Texture2DTest::testSetUp();
setUpProgram();
}
};
class Texture2DUnsignedIntegerAlpha1TestES3 : public Texture2DTest
{
protected:
Texture2DUnsignedIntegerAlpha1TestES3() : Texture2DTest() {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp usampler2D tex;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" vec4 green = vec4(0, 1, 0, 1);\n"
" vec4 black = vec4(0, 0, 0, 0);\n"
" fragColor = (texture(tex, texcoord).a == 1u) ? green : black;\n"
"}\n";
}
void testSetUp() override
{
Texture2DTest::testSetUp();
setUpProgram();
}
};
class Texture2DTestWithDrawScale : public Texture2DTest
{
protected:
Texture2DTestWithDrawScale() : Texture2DTest(), mDrawScaleUniformLocation(-1) {}
const char *getVertexShaderSource() override
{
return
R"(precision highp float;
attribute vec4 position;
varying vec2 texcoord;
uniform vec2 drawScale;
void main()
{
gl_Position = vec4(position.xy * drawScale, 0.0, 1.0);
texcoord = (position.xy * 0.5) + 0.5;
})";
}
void testSetUp() override
{
Texture2DTest::testSetUp();
setUpProgram();
mDrawScaleUniformLocation = glGetUniformLocation(mProgram, "drawScale");
ASSERT_NE(-1, mDrawScaleUniformLocation);
glUseProgram(mProgram);
glUniform2f(mDrawScaleUniformLocation, 1.0f, 1.0f);
glUseProgram(0);
ASSERT_GL_NO_ERROR();
}
GLint mDrawScaleUniformLocation;
};
class Sampler2DAsFunctionParameterTest : public Texture2DTest
{
protected:
Sampler2DAsFunctionParameterTest() : Texture2DTest() {}
const char *getFragmentShaderSource() override
{
return
R"(precision highp float;
uniform sampler2D tex;
varying vec2 texcoord;
vec4 computeFragColor(sampler2D aTex)
{
return texture2D(aTex, texcoord);
}
void main()
{
gl_FragColor = computeFragColor(tex);
})";
}
void testSetUp() override
{
Texture2DTest::testSetUp();
setUpProgram();
}
};
class TextureCubeTest : public TexCoordDrawTest
{
protected:
TextureCubeTest()
: TexCoordDrawTest(),
mTexture2D(0),
mTextureCube(0),
mTexture2DUniformLocation(-1),
mTextureCubeUniformLocation(-1)
{}
const char *getFragmentShaderSource() override
{
return
R"(precision highp float;
uniform sampler2D tex2D;
uniform samplerCube texCube;
varying vec2 texcoord;
void main()
{
gl_FragColor = texture2D(tex2D, texcoord);
gl_FragColor += textureCube(texCube, vec3(texcoord, 0));
})";
}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
glGenTextures(1, &mTextureCube);
glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
for (GLenum face = 0; face < 6; face++)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, 1, 1, 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
}
EXPECT_GL_NO_ERROR();
mTexture2D = create2DTexture();
setUpProgram();
mTexture2DUniformLocation = glGetUniformLocation(mProgram, "tex2D");
ASSERT_NE(-1, mTexture2DUniformLocation);
mTextureCubeUniformLocation = glGetUniformLocation(mProgram, "texCube");
ASSERT_NE(-1, mTextureCubeUniformLocation);
}
void testTearDown() override
{
glDeleteTextures(1, &mTextureCube);
TexCoordDrawTest::testTearDown();
}
GLuint mTexture2D;
GLuint mTextureCube;
GLint mTexture2DUniformLocation;
GLint mTextureCubeUniformLocation;
};
class TextureCubeTestES3 : public ANGLETest
{
protected:
TextureCubeTestES3() {}
};
class SamplerArrayTest : public TexCoordDrawTest
{
protected:
SamplerArrayTest()
: TexCoordDrawTest(),
mTexture2DA(0),
mTexture2DB(0),
mTexture0UniformLocation(-1),
mTexture1UniformLocation(-1)
{}
const char *getFragmentShaderSource() override
{
return
R"(precision mediump float;
uniform highp sampler2D tex2DArray[2];
varying vec2 texcoord;
void main()
{
gl_FragColor = texture2D(tex2DArray[0], texcoord);
gl_FragColor += texture2D(tex2DArray[1], texcoord);
})";
}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
setUpProgram();
mTexture0UniformLocation = glGetUniformLocation(mProgram, "tex2DArray[0]");
ASSERT_NE(-1, mTexture0UniformLocation);
mTexture1UniformLocation = glGetUniformLocation(mProgram, "tex2DArray[1]");
ASSERT_NE(-1, mTexture1UniformLocation);
mTexture2DA = create2DTexture();
mTexture2DB = create2DTexture();
ASSERT_GL_NO_ERROR();
}
void testTearDown() override
{
glDeleteTextures(1, &mTexture2DA);
glDeleteTextures(1, &mTexture2DB);
TexCoordDrawTest::testTearDown();
}
void testSamplerArrayDraw()
{
GLubyte texData[4];
texData[0] = 0;
texData[1] = 60;
texData[2] = 0;
texData[3] = 255;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2DA);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, texData);
texData[1] = 120;
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mTexture2DB);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, texData);
EXPECT_GL_ERROR(GL_NO_ERROR);
glUseProgram(mProgram);
glUniform1i(mTexture0UniformLocation, 0);
glUniform1i(mTexture1UniformLocation, 1);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_NEAR(0, 0, 0, 180, 0, 255, 2);
}
GLuint mTexture2DA;
GLuint mTexture2DB;
GLint mTexture0UniformLocation;
GLint mTexture1UniformLocation;
};
class SamplerArrayAsFunctionParameterTest : public SamplerArrayTest
{
protected:
SamplerArrayAsFunctionParameterTest() : SamplerArrayTest() {}
const char *getFragmentShaderSource() override
{
return
R"(precision mediump float;
uniform highp sampler2D tex2DArray[2];
varying vec2 texcoord;
vec4 computeFragColor(highp sampler2D aTex2DArray[2])
{
return texture2D(aTex2DArray[0], texcoord) + texture2D(aTex2DArray[1], texcoord);
}
void main()
{
gl_FragColor = computeFragColor(tex2DArray);
})";
}
};
class Texture2DArrayTestES3 : public TexCoordDrawTest
{
protected:
Texture2DArrayTestES3() : TexCoordDrawTest(), m2DArrayTexture(0), mTextureArrayLocation(-1) {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp sampler2DArray tex2DArray;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = texture(tex2DArray, vec3(texcoord.x, texcoord.y, 0.0));\n"
"}\n";
}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
setUpProgram();
mTextureArrayLocation = glGetUniformLocation(mProgram, "tex2DArray");
ASSERT_NE(-1, mTextureArrayLocation);
glGenTextures(1, &m2DArrayTexture);
ASSERT_GL_NO_ERROR();
}
void testTearDown() override
{
glDeleteTextures(1, &m2DArrayTexture);
TexCoordDrawTest::testTearDown();
}
GLuint m2DArrayTexture;
GLint mTextureArrayLocation;
};
class TextureSizeTextureArrayTest : public TexCoordDrawTest
{
protected:
TextureSizeTextureArrayTest()
: TexCoordDrawTest(),
mTexture2DA(0),
mTexture2DB(0),
mTexture0Location(-1),
mTexture1Location(-1)
{}
const char *getVertexShaderSource() override { return essl3_shaders::vs::Simple(); }
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp sampler2D tex2DArray[2];\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" float red = float(textureSize(tex2DArray[0], 0).x) / 255.0;\n"
" float green = float(textureSize(tex2DArray[1], 0).x) / 255.0;\n"
" fragColor = vec4(red, green, 0.0, 1.0);\n"
"}\n";
}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
setUpProgram();
mTexture0Location = glGetUniformLocation(mProgram, "tex2DArray[0]");
ASSERT_NE(-1, mTexture0Location);
mTexture1Location = glGetUniformLocation(mProgram, "tex2DArray[1]");
ASSERT_NE(-1, mTexture1Location);
mTexture2DA = create2DTexture();
mTexture2DB = create2DTexture();
ASSERT_GL_NO_ERROR();
}
void testTearDown() override
{
glDeleteTextures(1, &mTexture2DA);
glDeleteTextures(1, &mTexture2DB);
TexCoordDrawTest::testTearDown();
}
GLuint mTexture2DA;
GLuint mTexture2DB;
GLint mTexture0Location;
GLint mTexture1Location;
};
class Texture3DTestES3 : public TexCoordDrawTest
{
protected:
Texture3DTestES3() : TexCoordDrawTest(), mTexture3D(0), mTexture3DUniformLocation(-1) {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp sampler3D tex3D;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = texture(tex3D, vec3(texcoord, 0.0));\n"
"}\n";
}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
glGenTextures(1, &mTexture3D);
setUpProgram();
mTexture3DUniformLocation = glGetUniformLocation(mProgram, "tex3D");
ASSERT_NE(-1, mTexture3DUniformLocation);
}
void testTearDown() override
{
glDeleteTextures(1, &mTexture3D);
TexCoordDrawTest::testTearDown();
}
GLuint mTexture3D;
GLint mTexture3DUniformLocation;
};
class ShadowSamplerPlusSampler3DTestES3 : public TexCoordDrawTest
{
protected:
ShadowSamplerPlusSampler3DTestES3()
: TexCoordDrawTest(),
mTextureShadow(0),
mTexture3D(0),
mTextureShadowUniformLocation(-1),
mTexture3DUniformLocation(-1),
mDepthRefUniformLocation(-1)
{}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp sampler2DShadow tex2DShadow;\n"
"uniform highp sampler3D tex3D;\n"
"in vec2 texcoord;\n"
"uniform float depthRef;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = vec4(texture(tex2DShadow, vec3(texcoord, depthRef)) * 0.5);\n"
" fragColor += texture(tex3D, vec3(texcoord, 0.0));\n"
"}\n";
}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
glGenTextures(1, &mTexture3D);
glGenTextures(1, &mTextureShadow);
glBindTexture(GL_TEXTURE_2D, mTextureShadow);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
setUpProgram();
mTextureShadowUniformLocation = glGetUniformLocation(mProgram, "tex2DShadow");
ASSERT_NE(-1, mTextureShadowUniformLocation);
mTexture3DUniformLocation = glGetUniformLocation(mProgram, "tex3D");
ASSERT_NE(-1, mTexture3DUniformLocation);
mDepthRefUniformLocation = glGetUniformLocation(mProgram, "depthRef");
ASSERT_NE(-1, mDepthRefUniformLocation);
}
void testTearDown() override
{
glDeleteTextures(1, &mTextureShadow);
glDeleteTextures(1, &mTexture3D);
TexCoordDrawTest::testTearDown();
}
GLuint mTextureShadow;
GLuint mTexture3D;
GLint mTextureShadowUniformLocation;
GLint mTexture3DUniformLocation;
GLint mDepthRefUniformLocation;
};
class SamplerTypeMixTestES3 : public TexCoordDrawTest
{
protected:
SamplerTypeMixTestES3()
: TexCoordDrawTest(),
mTexture2D(0),
mTextureCube(0),
mTexture2DShadow(0),
mTextureCubeShadow(0),
mTexture2DUniformLocation(-1),
mTextureCubeUniformLocation(-1),
mTexture2DShadowUniformLocation(-1),
mTextureCubeShadowUniformLocation(-1),
mDepthRefUniformLocation(-1)
{}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp sampler2D tex2D;\n"
"uniform highp samplerCube texCube;\n"
"uniform highp sampler2DShadow tex2DShadow;\n"
"uniform highp samplerCubeShadow texCubeShadow;\n"
"in vec2 texcoord;\n"
"uniform float depthRef;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = texture(tex2D, texcoord);\n"
" fragColor += texture(texCube, vec3(1.0, 0.0, 0.0));\n"
" fragColor += vec4(texture(tex2DShadow, vec3(texcoord, depthRef)) * 0.25);\n"
" fragColor += vec4(texture(texCubeShadow, vec4(1.0, 0.0, 0.0, depthRef)) * "
"0.125);\n"
"}\n";
}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
glGenTextures(1, &mTexture2D);
glGenTextures(1, &mTextureCube);
glGenTextures(1, &mTexture2DShadow);
glBindTexture(GL_TEXTURE_2D, mTexture2DShadow);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glGenTextures(1, &mTextureCubeShadow);
glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCubeShadow);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
setUpProgram();
mTexture2DUniformLocation = glGetUniformLocation(mProgram, "tex2D");
ASSERT_NE(-1, mTexture2DUniformLocation);
mTextureCubeUniformLocation = glGetUniformLocation(mProgram, "texCube");
ASSERT_NE(-1, mTextureCubeUniformLocation);
mTexture2DShadowUniformLocation = glGetUniformLocation(mProgram, "tex2DShadow");
ASSERT_NE(-1, mTexture2DShadowUniformLocation);
mTextureCubeShadowUniformLocation = glGetUniformLocation(mProgram, "texCubeShadow");
ASSERT_NE(-1, mTextureCubeShadowUniformLocation);
mDepthRefUniformLocation = glGetUniformLocation(mProgram, "depthRef");
ASSERT_NE(-1, mDepthRefUniformLocation);
ASSERT_GL_NO_ERROR();
}
void testTearDown() override
{
glDeleteTextures(1, &mTexture2D);
glDeleteTextures(1, &mTextureCube);
glDeleteTextures(1, &mTexture2DShadow);
glDeleteTextures(1, &mTextureCubeShadow);
TexCoordDrawTest::testTearDown();
}
GLuint mTexture2D;
GLuint mTextureCube;
GLuint mTexture2DShadow;
GLuint mTextureCubeShadow;
GLint mTexture2DUniformLocation;
GLint mTextureCubeUniformLocation;
GLint mTexture2DShadowUniformLocation;
GLint mTextureCubeShadowUniformLocation;
GLint mDepthRefUniformLocation;
};
class SamplerInStructTest : public Texture2DTest
{
protected:
SamplerInStructTest() : Texture2DTest() {}
const char *getTextureUniformName() override { return "us.tex"; }
const char *getFragmentShaderSource() override
{
return "precision highp float;\n"
"struct S\n"
"{\n"
" vec4 a;\n"
" highp sampler2D tex;\n"
"};\n"
"uniform S us;\n"
"varying vec2 texcoord;\n"
"void main()\n"
"{\n"
" gl_FragColor = texture2D(us.tex, texcoord + us.a.x);\n"
"}\n";
}
void runSamplerInStructTest()
{
setUpProgram();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
&GLColor::green);
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
};
class SamplerInStructAsFunctionParameterTest : public SamplerInStructTest
{
protected:
SamplerInStructAsFunctionParameterTest() : SamplerInStructTest() {}
const char *getFragmentShaderSource() override
{
return "precision highp float;\n"
"struct S\n"
"{\n"
" vec4 a;\n"
" highp sampler2D tex;\n"
"};\n"
"uniform S us;\n"
"varying vec2 texcoord;\n"
"vec4 sampleFrom(S s) {\n"
" return texture2D(s.tex, texcoord + s.a.x);\n"
"}\n"
"void main()\n"
"{\n"
" gl_FragColor = sampleFrom(us);\n"
"}\n";
}
};
class SamplerInStructArrayAsFunctionParameterTest : public SamplerInStructTest
{
protected:
SamplerInStructArrayAsFunctionParameterTest() : SamplerInStructTest() {}
const char *getTextureUniformName() override { return "us[0].tex"; }
const char *getFragmentShaderSource() override
{
return "precision highp float;\n"
"struct S\n"
"{\n"
" vec4 a;\n"
" highp sampler2D tex;\n"
"};\n"
"uniform S us[1];\n"
"varying vec2 texcoord;\n"
"vec4 sampleFrom(S s) {\n"
" return texture2D(s.tex, texcoord + s.a.x);\n"
"}\n"
"void main()\n"
"{\n"
" gl_FragColor = sampleFrom(us[0]);\n"
"}\n";
}
};
class SamplerInNestedStructAsFunctionParameterTest : public SamplerInStructTest
{
protected:
SamplerInNestedStructAsFunctionParameterTest() : SamplerInStructTest() {}
const char *getTextureUniformName() override { return "us[0].sub.tex"; }
const char *getFragmentShaderSource() override
{
return "precision highp float;\n"
"struct SUB\n"
"{\n"
" vec4 a;\n"
" highp sampler2D tex;\n"
"};\n"
"struct S\n"
"{\n"
" SUB sub;\n"
"};\n"
"uniform S us[1];\n"
"varying vec2 texcoord;\n"
"vec4 sampleFrom(SUB s) {\n"
" return texture2D(s.tex, texcoord + s.a.x);\n"
"}\n"
"void main()\n"
"{\n"
" gl_FragColor = sampleFrom(us[0].sub);\n"
"}\n";
}
};
class SamplerInStructAndOtherVariableTest : public SamplerInStructTest
{
protected:
SamplerInStructAndOtherVariableTest() : SamplerInStructTest() {}
const char *getFragmentShaderSource() override
{
return "precision highp float;\n"
"struct S\n"
"{\n"
" vec4 a;\n"
" highp sampler2D tex;\n"
"};\n"
"uniform S us;\n"
"uniform float us_tex;\n"
"varying vec2 texcoord;\n"
"void main()\n"
"{\n"
" gl_FragColor = texture2D(us.tex, texcoord + us.a.x + us_tex);\n"
"}\n";
}
};
class Texture2DIntegerTestES3 : public Texture2DTest
{
protected:
Texture2DIntegerTestES3() : Texture2DTest() {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"precision highp usampler2D;\n"
"uniform usampler2D tex;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = vec4(texture(tex, texcoord))/255.0;\n"
"}\n";
}
};
class TextureCubeIntegerTestES3 : public TexCoordDrawTest
{
protected:
TextureCubeIntegerTestES3()
: TexCoordDrawTest(), mTextureCube(0), mTextureCubeUniformLocation(-1)
{}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = 0.5*position.xy;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"precision highp usamplerCube;\n"
"uniform usamplerCube texCube;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = vec4(texture(texCube, vec3(texcoord, 1)))/255.0;\n"
"}\n";
}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
glGenTextures(1, &mTextureCube);
setUpProgram();
mTextureCubeUniformLocation = glGetUniformLocation(mProgram, "texCube");
ASSERT_NE(-1, mTextureCubeUniformLocation);
}
void testTearDown() override
{
glDeleteTextures(1, &mTextureCube);
TexCoordDrawTest::testTearDown();
}
GLuint mTextureCube;
GLint mTextureCubeUniformLocation;
};
class TextureCubeIntegerEdgeTestES3 : public TextureCubeIntegerTestES3
{
protected:
TextureCubeIntegerEdgeTestES3() : TextureCubeIntegerTestES3() {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = position.xy;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"precision highp usamplerCube;\n"
"uniform usamplerCube texCube;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = vec4(texture(texCube, vec3(texcoord, 0)))/255.0;\n"
"}\n";
}
};
class Texture2DIntegerProjectiveOffsetTestES3 : public Texture2DTest
{
protected:
Texture2DIntegerProjectiveOffsetTestES3() : Texture2DTest() {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = 0.5*position.xy + vec2(0.5, 0.5);\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"precision highp usampler2D;\n"
"uniform usampler2D tex;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = vec4(textureProjOffset(tex, vec3(texcoord, 1), ivec2(0,0), "
"0.0))/255.0;\n"
"}\n";
}
};
class Texture2DArrayIntegerTestES3 : public Texture2DArrayTestES3
{
protected:
Texture2DArrayIntegerTestES3() : Texture2DArrayTestES3() {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp usampler2DArray tex2DArray;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = vec4(texture(tex2DArray, vec3(texcoord.x, texcoord.y, "
"0.0)))/255.0;\n"
"}\n";
}
};
class Texture3DIntegerTestES3 : public Texture3DTestES3
{
protected:
Texture3DIntegerTestES3() : Texture3DTestES3() {}
const char *getVertexShaderSource() override
{
return "#version 300 es\n"
"out vec2 texcoord;\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.xy, 0.0, 1.0);\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
}
const char *getFragmentShaderSource() override
{
return "#version 300 es\n"
"precision highp float;\n"
"uniform highp usampler3D tex3D;\n"
"in vec2 texcoord;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = vec4(texture(tex3D, vec3(texcoord, 0.0)))/255.0;\n"
"}\n";
}
};
class PBOCompressedTextureTest : public Texture2DTest
{
protected:
PBOCompressedTextureTest() : Texture2DTest() {}
void testSetUp() override
{
TexCoordDrawTest::testSetUp();
glGenTextures(1, &mTexture2D);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
EXPECT_GL_NO_ERROR();
setUpProgram();
glGenBuffers(1, &mPBO);
}
void testTearDown() override
{
glDeleteBuffers(1, &mPBO);
Texture2DTest::testTearDown();
}
GLuint mPBO;
};
TEST_P(Texture2DTest, NegativeAPISubImage)
{
glBindTexture(GL_TEXTURE_2D, mTexture2D);
EXPECT_GL_ERROR(GL_NO_ERROR);
setUpProgram();
const GLubyte *pixels[20] = {0};
glTexSubImage2D(GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
EXPECT_GL_ERROR(GL_INVALID_VALUE);
if (IsGLExtensionEnabled("GL_EXT_texture_storage"))
{
// Create a 1-level immutable texture.
glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, 2, 2);
// Try calling sub image on the second level.
glTexSubImage2D(GL_TEXTURE_2D, 1, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
}
// Test that querying GL_TEXTURE_BINDING* doesn't cause an unexpected error.
TEST_P(Texture2DTest, QueryBinding)
{
glBindTexture(GL_TEXTURE_2D, 0);
EXPECT_GL_ERROR(GL_NO_ERROR);
GLint textureBinding;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding);
EXPECT_GL_NO_ERROR();
EXPECT_EQ(0, textureBinding);
glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &textureBinding);
if (IsGLExtensionEnabled("GL_OES_EGL_image_external") ||
IsGLExtensionEnabled("GL_NV_EGL_stream_consumer_external"))
{
EXPECT_GL_NO_ERROR();
EXPECT_EQ(0, textureBinding);
}
else
{
EXPECT_GL_ERROR(GL_INVALID_ENUM);
}
}
TEST_P(Texture2DTest, ZeroSizedUploads)
{
glBindTexture(GL_TEXTURE_2D, mTexture2D);
EXPECT_GL_ERROR(GL_NO_ERROR);
setUpProgram();
// Use the texture first to make sure it's in video memory
glUseProgram(mProgram);
glUniform1i(mTexture2DUniformLocation, 0);
drawQuad(mProgram, "position", 0.5f);
const GLubyte *pixel[4] = {0};
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
EXPECT_GL_NO_ERROR();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
EXPECT_GL_NO_ERROR();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
EXPECT_GL_NO_ERROR();
}
// Test drawing with two texture types, to trigger an ANGLE bug in validation
TEST_P(TextureCubeTest, CubeMapBug)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
EXPECT_GL_ERROR(GL_NO_ERROR);
glUseProgram(mProgram);
glUniform1i(mTexture2DUniformLocation, 0);
glUniform1i(mTextureCubeUniformLocation, 1);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
}
// Test drawing with two texture types accessed from the same shader and check that the result of
// drawing is correct.
TEST_P(TextureCubeTest, CubeMapDraw)
{
GLubyte texData[4];
texData[0] = 0;
texData[1] = 60;
texData[2] = 0;
texData[3] = 255;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, texData);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
texData[1] = 120;
glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,
texData);
EXPECT_GL_ERROR(GL_NO_ERROR);
glUseProgram(mProgram);
glUniform1i(mTexture2DUniformLocation, 0);
glUniform1i(mTextureCubeUniformLocation, 1);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
int px = getWindowWidth() - 1;
int py = 0;
EXPECT_PIXEL_NEAR(px, py, 0, 180, 0, 255, 2);
}
TEST_P(Sampler2DAsFunctionParameterTest, Sampler2DAsFunctionParameter)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
GLubyte texData[4];
texData[0] = 0;
texData[1] = 128;
texData[2] = 0;
texData[3] = 255;
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, texData);
glUseProgram(mProgram);
glUniform1i(mTexture2DUniformLocation, 0);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_NEAR(0, 0, 0, 128, 0, 255, 2);
}
// Test drawing with two textures passed to the shader in a sampler array.
TEST_P(SamplerArrayTest, SamplerArrayDraw)
{
testSamplerArrayDraw();
}
// Test drawing with two textures passed to the shader in a sampler array which is passed to a
// user-defined function in the shader.
TEST_P(SamplerArrayAsFunctionParameterTest, SamplerArrayAsFunctionParameter)
{
// TODO: Diagnose and fix. http://anglebug.com/2955
ANGLE_SKIP_TEST_IF(IsVulkan() && IsAndroid());
testSamplerArrayDraw();
}
// Copy of a test in conformance/textures/texture-mips, to test generate mipmaps
TEST_P(Texture2DTestWithDrawScale, MipmapsTwice)
{
int px = getWindowWidth() / 2;
int py = getWindowHeight() / 2;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
std::vector<GLColor> pixelsRed(16u * 16u, GLColor::red);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelsRed.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glUseProgram(mProgram);
glUniform1i(mTexture2DUniformLocation, 0);
glUniform2f(mDrawScaleUniformLocation, 0.0625f, 0.0625f);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(px, py, GLColor::red);
std::vector<GLColor> pixelsBlue(16u * 16u, GLColor::blue);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE,
pixelsBlue.data());
glGenerateMipmap(GL_TEXTURE_2D);
std::vector<GLColor> pixelsGreen(16u * 16u, GLColor::green);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE,
pixelsGreen.data());
glGenerateMipmap(GL_TEXTURE_2D);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(px, py, GLColor::green);
}
// Test creating a FBO with a cube map render target, to test an ANGLE bug
// https://code.google.com/p/angleproject/issues/detail?id=849
TEST_P(TextureCubeTest, CubeMapFBO)
{
// http://anglebug.com/3145
ANGLE_SKIP_TEST_IF(IsFuchsia() && IsIntel() && IsVulkan());
// http://anglebug.com/2822
ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsVulkan());
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
mTextureCube, 0);
EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
EXPECT_GL_NO_ERROR();
// Test clearing the six mip faces individually.
std::array<GLColor, 6> faceColors = {{GLColor::red, GLColor::green, GLColor::blue,
GLColor::yellow, GLColor::cyan, GLColor::magenta}};
for (size_t faceIndex = 0; faceIndex < 6; ++faceIndex)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex, mTextureCube, 0);
Vector4 clearColorF = faceColors[faceIndex].toNormalizedVector();
glClearColor(clearColorF.x(), clearColorF.y(), clearColorF.z(), clearColorF.w());
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_COLOR_EQ(0, 0, faceColors[faceIndex]);
}
// Iterate the faces again to make sure the colors haven't changed.
for (size_t faceIndex = 0; faceIndex < 6; ++faceIndex)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex, mTextureCube, 0);
EXPECT_PIXEL_COLOR_EQ(0, 0, faceColors[faceIndex])
<< "face color " << faceIndex << " shouldn't change";
}
}
// Tests clearing a cube map with a scissor enabled.
TEST_P(TextureCubeTest, CubeMapFBOScissoredClear)
{
// TODO(jie.a.chen): Diagnose and fix. http://anglebug.com/2822
ANGLE_SKIP_TEST_IF(IsVulkan() && IsIntel() && IsWindows());
// http://anglebug.com/3145
ANGLE_SKIP_TEST_IF(IsFuchsia() && IsIntel() && IsVulkan());
constexpr size_t kSize = 16;
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0, 0, kSize, kSize);
GLTexture texcube;
glBindTexture(GL_TEXTURE_CUBE_MAP, texcube);
for (GLenum face = 0; face < 6; face++)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
}
ASSERT_GL_NO_ERROR();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
texcube, 0);
EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
ASSERT_GL_NO_ERROR();
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
glEnable(GL_SCISSOR_TEST);
glScissor(kSize / 2, 0, kSize / 2, kSize);
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(kSize / 2 + 1, 0, GLColor::green);
ASSERT_GL_NO_ERROR();
}
// Test that glTexSubImage2D works properly when glTexStorage2DEXT has initialized the image with a
// default color.
TEST_P(Texture2DTest, TexStorage)
{
ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 &&
!IsGLExtensionEnabled("GL_EXT_texture_storage"));
int width = getWindowWidth();
int height = getWindowHeight();
GLuint tex2D;
glGenTextures(1, &tex2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex2D);
// Fill with red
std::vector<GLubyte> pixels(3 * 16 * 16);
for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
{
pixels[pixelId * 3 + 0] = 255;
pixels[pixelId * 3 + 1] = 0;
pixels[pixelId * 3 + 2] = 0;
}
// ANGLE internally uses RGBA as the DirectX format for RGB images
// therefore glTexStorage2DEXT initializes the image to a default color to get a consistent
// alpha color. The data is kept in a CPU-side image and the image is marked as dirty.
if (getClientMajorVersion() >= 3)
{
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, 16, 16);
}
else
{
glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGB8, 16, 16);
}
// Initializes the color of the upper-left 8x8 pixels, leaves the other pixels untouched.
// glTexSubImage2D should take into account that the image is dirty.
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 8, 8, GL_RGB, GL_UNSIGNED_BYTE, pixels.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
setUpProgram();
glUseProgram(mProgram);
glUniform1i(mTexture2DUniformLocation, 0);
drawQuad(mProgram, "position", 0.5f);
glDeleteTextures(1, &tex2D);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(width / 4, height / 4, 255, 0, 0, 255);
// Validate that the region of the texture without data has an alpha of 1.0
angle::GLColor pixel = ReadColor(3 * width / 4, 3 * height / 4);
EXPECT_EQ(255, pixel.A);
}
// Test that glTexSubImage2D combined with a PBO works properly when glTexStorage2DEXT has
// initialized the image with a default color.
TEST_P(Texture2DTest, TexStorageWithPBO)
{
// http://anglebug.com/4126
ANGLE_SKIP_TEST_IF(IsOSX() && IsOpenGL());
if (getClientMajorVersion() < 3)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_texture_storage"));
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_NV_pixel_buffer_object"));
}
const int width = getWindowWidth();
const int height = getWindowHeight();
const size_t pixelCount = width * height;
const int componentCount = 3;
GLuint tex2D;
glGenTextures(1, &tex2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex2D);
// Fill with red
std::vector<GLubyte> pixels(componentCount * pixelCount);
for (size_t pixelId = 0; pixelId < pixelCount; ++pixelId)
{
pixels[pixelId * componentCount + 0] = 255;
pixels[pixelId * componentCount + 1] = 0;
pixels[pixelId * componentCount + 2] = 0;
}
// Read 16x16 region from red backbuffer to PBO
GLuint pbo;
glGenBuffers(1, &pbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
glBufferData(GL_PIXEL_UNPACK_BUFFER, componentCount * pixelCount, pixels.data(),
GL_STATIC_DRAW);
// ANGLE internally uses RGBA as the DirectX format for RGB images
// therefore glTexStorage2DEXT initializes the image to a default color to get a consistent
// alpha color. The data is kept in a CPU-side image and the image is marked as dirty.
glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGB8, width, height);
// Initializes the color of the upper-left quadrant of pixels, leaves the other pixels
// untouched. glTexSubImage2D should take into account that the image is dirty.
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RGB, GL_UNSIGNED_BYTE,
nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
setUpProgram();
glUseProgram(mProgram);
glUniform1i(mTexture2DUniformLocation, 0);
drawQuad(mProgram, "position", 0.5f);
glDeleteTextures(1, &tex2D);
glDeleteBuffers(1, &pbo);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(3 * width / 4, 3 * height / 4, 0, 0, 0, 255);
EXPECT_PIXEL_EQ(width / 4, height / 4, 255, 0, 0, 255);
}
// Test that glTexSubImage2D combined with a PBO works properly after deleting the PBO
// and drawing with the texture
// Pseudo code for the follow test:
// 1. Upload PBO to mTexture2D
// 2. Delete PBO
// 3. Draw with otherTexture (x5)
// 4. Draw with mTexture2D
// 5. Validate color output
TEST_P(Texture2DTest, PBOWithMultipleDraws)
{
if (getClientMajorVersion() < 3)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_texture_storage"));
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_NV_pixel_buffer_object"));
}
const GLuint width = getWindowWidth();
const GLuint height = getWindowHeight();
const GLuint windowPixelCount = width * height;
std::vector<GLColor> pixelsRed(windowPixelCount, GLColor::red);
std::vector<GLColor> pixelsGreen(windowPixelCount, GLColor::green);
// Create secondary draw that does not use mTexture
const char *vertexShaderSource = getVertexShaderSource();
const char *fragmentShaderSource = getFragmentShaderSource();
ANGLE_GL_PROGRAM(otherProgram, vertexShaderSource, fragmentShaderSource);
GLint uniformLoc = glGetUniformLocation(otherProgram, getTextureUniformName());
ASSERT_NE(-1, uniformLoc);
glUseProgram(0);
// Create secondary Texture to draw with
GLTexture otherTexture;
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, otherTexture);
glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, width, height);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
pixelsRed.data());
ASSERT_GL_NO_ERROR();
// Setup primary Texture
glBindTexture(GL_TEXTURE_2D, mTexture2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, width, height);
ASSERT_GL_NO_ERROR();
// Setup PBO
GLuint pbo = 0;
glGenBuffers(1, &pbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
glBufferData(GL_PIXEL_UNPACK_BUFFER, pixelsGreen.size() * 4u, pixelsGreen.data(),
GL_STATIC_DRAW);
ASSERT_GL_NO_ERROR();
// Write PBO to mTexture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0);
ASSERT_GL_NO_ERROR();
// Delete PBO as ANGLE should be properly handling refcount of this buffer
glDeleteBuffers(1, &pbo);
pixelsGreen.clear();
// Do 5 draws not involving primary texture that the PBO updated
glUseProgram(otherProgram);
glUniform1i(uniformLoc, 0);
glBindTexture(GL_TEXTURE_2D, otherTexture);
drawQuad(otherProgram, "position", 0.5f);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
glUseProgram(otherProgram);
glUniform1i(uniformLoc, 0);
glBindTexture(GL_TEXTURE_2D, otherTexture);
drawQuad(otherProgram, "position", 0.5f);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
glUseProgram(otherProgram);
glUniform1i(uniformLoc, 0);
glBindTexture(GL_TEXTURE_2D, otherTexture);
drawQuad(otherProgram, "position", 0.5f);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
glUseProgram(otherProgram);
glUniform1i(uniformLoc, 0);
glBindTexture(GL_TEXTURE_2D, otherTexture);
drawQuad(otherProgram, "position", 0.5f);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
ASSERT_GL_NO_ERROR();
std::vector<GLColor> output(windowPixelCount, GLColor::black);
glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RGBA, GL_UNSIGNED_BYTE,
output.data());
EXPECT_EQ(pixelsRed, output);
setUpProgram();
// Draw using PBO updated texture
glUseProgram(mProgram);
glUniform1i(mTexture2DUniformLocation, 0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
drawQuad(mProgram, "position", 0.5f);
ASSERT_GL_NO_ERROR();
std::vector<GLColor> actual(windowPixelCount, GLColor::black);
glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RGBA, GL_UNSIGNED_BYTE,
actual.data());
// Value should be green as it was updated during PBO transfer to mTexture
std::vector<GLColor> expected(windowPixelCount, GLColor::green);
EXPECT_EQ(expected, actual);
}
// Tests CopySubImage for float formats
TEST_P(Texture2DTest, CopySubImageFloat_R_R)
{
testFloatCopySubImage(1, 1);
}
TEST_P(Texture2DTest, CopySubImageFloat_RG_R)
{
testFloatCopySubImage(2, 1);
}
TEST_P(Texture2DTest, CopySubImageFloat_RG_RG)
{
testFloatCopySubImage(2, 2);
}
TEST_P(Texture2DTest, CopySubImageFloat_RGB_R)
{
testFloatCopySubImage(3, 1);
}
TEST_P(Texture2DTest, CopySubImageFloat_RGB_RG)
{
testFloatCopySubImage(3, 2);
}
TEST_P(Texture2DTest, CopySubImageFloat_RGB_RGB)
{
// TODO(cwallez): Fix on Linux Intel drivers (http://anglebug.com/1346)
ANGLE_SKIP_TEST_IF(IsIntel() && IsLinux() && IsOpenGL());
// Ignore SDK layers messages on D3D11 FL 9.3 (http://anglebug.com/1284)
ANGLE_SKIP_TEST_IF(IsD3D11_FL93());
testFloatCopySubImage(3, 3);
}
TEST_P(Texture2DTest, CopySubImageFloat_RGBA_R)
{
testFloatCopySubImage(4, 1);
}
TEST_P(Texture2DTest, CopySubImageFloat_RGBA_RG)
{
testFloatCopySubImage(4, 2);
}
TEST_P(Texture2DTest, CopySubImageFloat_RGBA_RGB)
{
// Ignore SDK layers messages on D3D11 FL 9.3 (http://anglebug.com/1284)
ANGLE_SKIP_TEST_IF(IsD3D11_FL93());
testFloatCopySubImage(4, 3);
}
TEST_P(Texture2DTest, CopySubImageFloat_RGBA_RGBA)
{
// Ignore SDK layers messages on D3D11 FL 9.3 (http://anglebug.com/1284)
ANGLE_SKIP_TEST_IF(IsD3D11_FL93());
testFloatCopySubImage(4, 4);
}
// Port of
// https://www.khronos.org/registry/webgl/conformance-suites/1.0.3/conformance/textures/texture-npot.html
// Run against GL_ALPHA/UNSIGNED_BYTE format, to ensure that D3D11 Feature Level 9_3 correctly
// handles GL_ALPHA
TEST_P(Texture2DTest, TextureNPOT_GL_ALPHA_UBYTE)
{
const int npotTexSize = 5;
const int potTexSize = 4; // Should be less than npotTexSize
GLuint tex2D;
if (IsGLExtensionEnabled("GL_OES_texture_npot"))
{
// This test isn't applicable if texture_npot is enabled
return;
}
setUpProgram();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Default unpack alignment is 4. The values of 'pixels' below needs it to be 1.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex2D);
glBindTexture(GL_TEXTURE_2D, tex2D);
const std::vector<GLubyte> pixels(1 * npotTexSize * npotTexSize, 64);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Check that an NPOT texture not on level 0 generates INVALID_VALUE
glTexImage2D(GL_TEXTURE_2D, 1, GL_ALPHA, npotTexSize, npotTexSize, 0, GL_ALPHA,
GL_UNSIGNED_BYTE, pixels.data());
EXPECT_GL_ERROR(GL_INVALID_VALUE);
// Check that an NPOT texture on level 0 succeeds
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, npotTexSize, npotTexSize, 0, GL_ALPHA,
GL_UNSIGNED_BYTE, pixels.data());
EXPECT_GL_NO_ERROR();
// Check that generateMipmap fails on NPOT
glGenerateMipmap(GL_TEXTURE_2D);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
// Check that nothing is drawn if filtering is not correct for NPOT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(mProgram, "position", 1.0f);
EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 0, 0, 0, 255);
// NPOT texture with TEXTURE_MIN_FILTER not NEAREST or LINEAR should draw with 0,0,0,255
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(mProgram, "position", 1.0f);
EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 0, 0, 0, 255);
// NPOT texture with TEXTURE_MIN_FILTER set to LINEAR should draw
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(mProgram, "position", 1.0f);
EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 0, 0, 0, 64);
// Check that glTexImage2D for POT texture succeeds
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, potTexSize, potTexSize, 0, GL_ALPHA, GL_UNSIGNED_BYTE,
pixels.data());
EXPECT_GL_NO_ERROR();
// Check that generateMipmap for an POT texture succeeds
glGenerateMipmap(GL_TEXTURE_2D);
EXPECT_GL_NO_ERROR();
// POT texture with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(mProgram, "position", 1.0f);
EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 0, 0, 0, 64);
EXPECT_GL_NO_ERROR();
}
// Test to ensure that glTexSubImage2D always accepts data for non-power-of-two subregions.
// ANGLE previously rejected this if GL_OES_texture_npot wasn't active, which is incorrect.
TEST_P(Texture2DTest, NPOTSubImageParameters)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
// Create an 8x8 (i.e. power-of-two) texture.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
// Supply a 3x3 (i.e. non-power-of-two) subimage to the texture.
// This should always work, even if GL_OES_texture_npot isn't active.
std::array<GLColor, 3 * 3> data;
glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, 3, 3, GL_RGBA, GL_UNSIGNED_BYTE, data.data());
EXPECT_GL_NO_ERROR();
}
// Regression test for http://crbug.com/949985 to make sure dirty bits are propagated up from
// TextureImpl and the texture is synced before being used in a draw call.
TEST_P(Texture2DTestES3, TextureImplPropogatesDirtyBits)
{
ANGLE_SKIP_TEST_IF(IsIntel() && IsOpenGL());
// Flaky hangs on Win10 AMD RX 550 GL. http://anglebug.com/3371
ANGLE_SKIP_TEST_IF(IsWindows() && IsAMD() && IsOpenGL());
// D3D Debug device reports an error. http://anglebug.com/3501
ANGLE_SKIP_TEST_IF(IsWindows() && IsD3D11());
// TODO(cnorthrop): Needs triage on Vulkan backend. http://anglebug.com/3950
ANGLE_SKIP_TEST_IF(IsVulkan());
// The workaround in the GL backend required to trigger this bug generates driver warning
// messages.
ScopedIgnorePlatformMessages ignoreMessages;
setUpProgram();
glUseProgram(mProgram);
glActiveTexture(GL_TEXTURE0 + mTexture2DUniformLocation);
GLTexture dest;
glBindTexture(GL_TEXTURE_2D, dest);
GLTexture source;
glBindTexture(GL_TEXTURE_2D, source);
// Put data in mip 0 and 1
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
GLColor::red.data());
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
GLColor::green.data());
// Disable mipmapping so source is complete
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Force the dirty bits to be synchronized in source
drawQuad(mProgram, "position", 1.0f);
// Copy from mip 1 of the source. In the GL backend this internally sets the base level to mip
// 1 and sets a dirty bit.
glCopyTextureCHROMIUM(source, 1, GL_TEXTURE_2D, dest, 0, GL_RGBA, GL_UNSIGNED_BYTE, GL_FALSE,
GL_FALSE, GL_FALSE);
// Draw again, assertions are generated if the texture has internal dirty bits at draw time
drawQuad(mProgram, "position", 1.0f);
}
// This test case changes the base level of a texture that's attached to a framebuffer, clears every
// level to green, and then samples the texture when rendering. Test is taken from
// https://www.khronos.org/registry/webgl/sdk/tests/conformance2/rendering/framebuffer-texture-changing-base-level.html
TEST_P(Texture2DTestES3, FramebufferTextureChangingBaselevel)
{
// TODO(geofflang): Investigate on D3D11. http://anglebug.com/2291
ANGLE_SKIP_TEST_IF(IsD3D11());
// TODO(cnorthrop): Failing on Vulkan/Windows/AMD. http://anglebug.com/3996
ANGLE_SKIP_TEST_IF(IsVulkan() && IsWindows() && IsAMD());
setUpProgram();
constexpr GLint width = 8;
constexpr GLint height = 4;
GLTexture texture;
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Create all mipmap levels for the texture from level 0 to the 1x1 pixel level.
GLint level = 0;
GLint levelW = width;
GLint levelH = height;
glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, levelW, levelH, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr);
while (levelW > 1 || levelH > 1)
{
++level;
levelW = static_cast<GLint>(std::max(1.0, std::floor(width / std::pow(2, level))));
levelH = static_cast<GLint>(std::max(1.0, std::floor(height / std::pow(2, level))));
glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, levelW, levelH, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr);
}
// Clear each level of the texture using an FBO. Change the base level to match the level used
// for the FBO on each iteration.
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
level = 0;
levelW = width;
levelH = height;
while (levelW > 1 || levelH > 1)
{
levelW = static_cast<GLint>(std::floor(width / std::pow(2, level)));
levelH = static_cast<GLint>(std::floor(height / std::pow(2, level)));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, level);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
EXPECT_GL_NO_ERROR();
glClearColor(0, 1, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
++level;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, 16, 16);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Test to check that texture completeness is determined correctly when the texture base level is
// greater than 0, and also that level 0 is not sampled when base level is greater than 0.
TEST_P(Texture2DTestES3, DrawWithBaseLevel1)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
std::vector<GLColor> texDataRed(4u * 4u, GLColor::red);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, texDataRed.data());
std::vector<GLColor> texDataGreen(2u * 2u, GLColor::green);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexImage2D(GL_TEXTURE_2D, 2, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Test that drawing works correctly when levels outside the BASE_LEVEL/MAX_LEVEL range do not
// have images defined.
TEST_P(Texture2DTestES3, DrawWithLevelsOutsideRangeUndefined)
{
// Observed crashing on AMD. Oddly the crash only happens with 2D textures, not 3D or array.
ANGLE_SKIP_TEST_IF(IsAMD() && IsOpenGL());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
std::vector<GLColor> texDataGreen(2u * 2u, GLColor::green);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Test that drawing works correctly when level 0 is undefined and base level is 1.
TEST_P(Texture2DTestES3, DrawWithLevelZeroUndefined)
{
// Observed crashing on AMD. Oddly the crash only happens with 2D textures, not 3D or array.
ANGLE_SKIP_TEST_IF(IsAMD() && IsOpenGL());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
std::vector<GLColor> texDataGreen(2u * 2u, GLColor::green);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
EXPECT_GL_NO_ERROR();
// Texture is incomplete.
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
glTexImage2D(GL_TEXTURE_2D, 2, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
// Texture is now complete.
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Test that drawing works correctly when levels outside the BASE_LEVEL/MAX_LEVEL range have
// dimensions that don't fit the images inside the range.
// GLES 3.0.4 section 3.8.13 Texture completeness
TEST_P(Texture2DTestES3, DrawWithLevelsOutsideRangeWithInconsistentDimensions)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
std::vector<GLColor> texDataRed(8u * 8u, GLColor::red);
std::vector<GLColor> texDataGreen(2u * 2u, GLColor::green);
std::vector<GLColor> texDataCyan(2u * 2u, GLColor::cyan);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// Two levels that are initially unused.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, texDataRed.data());
glTexImage2D(GL_TEXTURE_2D, 2, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataCyan.data());
// One level that is used - only this level should affect completeness.
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
ANGLE_SKIP_TEST_IF(IsIntel() && IsWindows() && IsOpenGL());
// Switch the level that is being used to the cyan level 2.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::cyan);
}
// Test that drawing works correctly when levels outside the BASE_LEVEL/MAX_LEVEL range do not
// have images defined.
TEST_P(Texture3DTestES3, DrawWithLevelsOutsideRangeUndefined)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, mTexture3D);
std::vector<GLColor> texDataGreen(2u * 2u * 2u, GLColor::green);
glTexImage3D(GL_TEXTURE_3D, 1, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 1);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Test that drawing works correctly when levels outside the BASE_LEVEL/MAX_LEVEL range have
// dimensions that don't fit the images inside the range.
// GLES 3.0.4 section 3.8.13 Texture completeness
TEST_P(Texture3DTestES3, DrawWithLevelsOutsideRangeWithInconsistentDimensions)
{
// Crashes on Intel Ubuntu 19.04 Mesa 19.0.2 GL. http://anglebug.com/2782
ANGLE_SKIP_TEST_IF(IsLinux() && IsIntel() && IsDesktopOpenGL());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, mTexture3D);
std::vector<GLColor> texDataRed(8u * 8u * 8u, GLColor::red);
std::vector<GLColor> texDataGreen(2u * 2u * 2u, GLColor::green);
std::vector<GLColor> texDataCyan(2u * 2u * 2u, GLColor::cyan);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// Two levels that are initially unused.
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataRed.data());
glTexImage3D(GL_TEXTURE_3D, 2, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataCyan.data());
// One level that is used - only this level should affect completeness.
glTexImage3D(GL_TEXTURE_3D, 1, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 1);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
ANGLE_SKIP_TEST_IF(IsIntel() && IsWindows() && IsOpenGL());
// Switch the level that is being used to the cyan level 2.
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 2);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 2);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::cyan);
}
// Test that drawing works correctly when levels outside the BASE_LEVEL/MAX_LEVEL range do not
// have images defined.
TEST_P(Texture2DArrayTestES3, DrawWithLevelsOutsideRangeUndefined)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, m2DArrayTexture);
std::vector<GLColor> texDataGreen(2u * 2u * 2u, GLColor::green);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Test that drawing works correctly when levels outside the BASE_LEVEL/MAX_LEVEL range have
// dimensions that don't fit the images inside the range.
// GLES 3.0.4 section 3.8.13 Texture completeness
TEST_P(Texture2DArrayTestES3, DrawWithLevelsOutsideRangeWithInconsistentDimensions)
{
// TODO(crbug.com/998505): Test failing on Android FYI Release (NVIDIA Shield TV)
ANGLE_SKIP_TEST_IF(IsNVIDIAShield());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, m2DArrayTexture);
std::vector<GLColor> texDataRed(8u * 8u * 8u, GLColor::red);
std::vector<GLColor> texDataGreen(2u * 2u * 2u, GLColor::green);
std::vector<GLColor> texDataCyan(2u * 2u * 2u, GLColor::cyan);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// Two levels that are initially unused.
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataRed.data());
glTexImage3D(GL_TEXTURE_2D_ARRAY, 2, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataCyan.data());
// One level that is used - only this level should affect completeness.
glTexImage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
ANGLE_SKIP_TEST_IF(IsIntel() && IsWindows() && IsOpenGL());
// Switch the level that is being used to the cyan level 2.
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 2);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 2);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::cyan);
}
// Test that texture completeness is updated if texture max level changes.
// GLES 3.0.4 section 3.8.13 Texture completeness
TEST_P(Texture2DTestES3, TextureCompletenessChangesWithMaxLevel)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
std::vector<GLColor> texDataGreen(8u * 8u, GLColor::green);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// A level that is initially unused.
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
// One level that is initially used - only this level should affect completeness.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
// Switch the max level to level 1. The levels within the used range now have inconsistent
// dimensions and the texture should be incomplete.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
}
// Test that 3D texture completeness is updated if texture max level changes.
// GLES 3.0.4 section 3.8.13 Texture completeness
TEST_P(Texture3DTestES3, Texture3DCompletenessChangesWithMaxLevel)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, mTexture3D);
std::vector<GLColor> texDataGreen(2u * 2u * 2u, GLColor::green);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// A level that is initially unused.
glTexImage3D(GL_TEXTURE_3D, 1, GL_RGBA8, 1, 1, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
// One level that is initially used - only this level should affect completeness.
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
// Switch the max level to level 1. The levels within the used range now have inconsistent
// dimensions and the texture should be incomplete.
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
}
// Test that texture completeness is updated if texture base level changes.
// GLES 3.0.4 section 3.8.13 Texture completeness
TEST_P(Texture2DTestES3, TextureCompletenessChangesWithBaseLevel)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
std::vector<GLColor> texDataGreen(8u * 8u, GLColor::green);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// Two levels that are initially unused.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
// One level that is initially used - only this level should affect completeness.
glTexImage2D(GL_TEXTURE_2D, 2, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataGreen.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 2);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
// Switch the base level to level 1. The levels within the used range now have inconsistent
// dimensions and the texture should be incomplete.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 1);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
}
// Test that texture is not complete if base level is greater than max level.
// GLES 3.0.4 section 3.8.13 Texture completeness
TEST_P(Texture2DTestES3, TextureBaseLevelGreaterThanMaxLevel)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GLColor::green);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 10000);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
// Texture should be incomplete.
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
}
// Test that immutable texture base level and max level are clamped.
// GLES 3.0.4 section 3.8.10 subsection Mipmapping
TEST_P(Texture2DTestES3, ImmutableTextureBaseLevelOutOfRange)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &GLColor::green);
// For immutable-format textures, base level should be clamped to [0, levels - 1], and max level
// should be clamped to [base_level, levels - 1].
// GLES 3.0.4 section 3.8.10 subsection Mipmapping
// In the case of this test, those rules make the effective base level and max level 0.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 10000);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 10000);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
// Texture should be complete.
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Test that changing base level works when it affects the format of the texture.
TEST_P(Texture2DTestES3, TextureFormatChangesWithBaseLevel)
{
// TODO(crbug.com/998505): Test failing on Android FYI Release (NVIDIA Shield TV)
ANGLE_SKIP_TEST_IF(IsNVIDIAShield());
ANGLE_SKIP_TEST_IF(IsIntel() && IsWindows() && IsDesktopOpenGL());
// Observed incorrect rendering on AMD OpenGL.
ANGLE_SKIP_TEST_IF(IsAMD() && IsDesktopOpenGL());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
std::vector<GLColor> texDataCyan(4u * 4u, GLColor::cyan);
std::vector<GLColor> texDataGreen(4u * 4u, GLColor::green);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// RGBA8 level that's initially unused.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texDataCyan.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
// RG8 level that's initially used, with consistent dimensions with level 0 but a different
// format. It reads green channel data from the green and alpha channels of texDataGreen
// (this is a bit hacky but works).
glTexImage2D(GL_TEXTURE_2D, 1, GL_RG8, 2, 2, 0, GL_RG, GL_UNSIGNED_BYTE, texDataGreen.data());
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
// Switch the texture to use the cyan level 0 with the RGBA format.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::cyan);
}
// Test that setting a texture image works when base level is out of range.
TEST_P(Texture2DTestES3, SetImageWhenBaseLevelOutOfRange)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 10000);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 10000);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GLColor::green);
EXPECT_GL_NO_ERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);