blob: f227457b113ec9a3bc12ed37dd654e8d8d53d214 [file] [log] [blame]
Jamie Madillf67115c2014-04-22 13:14:05 -04001#include "ANGLETest.h"
2
Austin Kinross18b931d2014-09-29 12:58:31 -07003// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
Geoff Lang0d3683c2014-10-23 11:08:16 -04004ANGLE_TYPED_TEST_CASE(TextureTest, ES2_D3D9, ES2_D3D11);
Austin Kinross18b931d2014-09-29 12:58:31 -07005
6template<typename T>
Jamie Madillf67115c2014-04-22 13:14:05 -04007class TextureTest : public ANGLETest
8{
9protected:
Geoff Lang0d3683c2014-10-23 11:08:16 -040010 TextureTest() : ANGLETest(T::GetGlesMajorVersion(), T::GetPlatform())
Jamie Madillf67115c2014-04-22 13:14:05 -040011 {
12 setWindowWidth(128);
13 setWindowHeight(128);
14 setConfigRedBits(8);
15 setConfigGreenBits(8);
16 setConfigBlueBits(8);
17 setConfigAlphaBits(8);
18 }
19
20 virtual void SetUp()
21 {
22 ANGLETest::SetUp();
Jamie Madilld4cfa572014-07-08 10:00:32 -040023 glGenTextures(1, &mTexture2D);
24 glGenTextures(1, &mTextureCube);
Jamie Madillf67115c2014-04-22 13:14:05 -040025
Jamie Madilld4cfa572014-07-08 10:00:32 -040026 glBindTexture(GL_TEXTURE_2D, mTexture2D);
Jamie Madillf67115c2014-04-22 13:14:05 -040027 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
28 EXPECT_GL_NO_ERROR();
29
Jamie Madilld4cfa572014-07-08 10:00:32 -040030 glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
31 glTexStorage2DEXT(GL_TEXTURE_CUBE_MAP, 1, GL_RGBA8, 1, 1);
32 EXPECT_GL_NO_ERROR();
33
Jamie Madillf67115c2014-04-22 13:14:05 -040034 ASSERT_GL_NO_ERROR();
Geoff Langc41e42d2014-04-28 10:58:16 -040035
36 const std::string vertexShaderSource = SHADER_SOURCE
37 (
38 precision highp float;
39 attribute vec4 position;
40 varying vec2 texcoord;
41
Jamie Madill9aca0592014-10-06 16:26:59 -040042 uniform vec2 textureScale;
43
Geoff Langc41e42d2014-04-28 10:58:16 -040044 void main()
45 {
Jamie Madill9aca0592014-10-06 16:26:59 -040046 gl_Position = vec4(position.xy * textureScale, 0.0, 1.0);
Geoff Langc41e42d2014-04-28 10:58:16 -040047 texcoord = (position.xy * 0.5) + 0.5;
48 }
49 );
50
Jamie Madilld4cfa572014-07-08 10:00:32 -040051 const std::string fragmentShaderSource2D = SHADER_SOURCE
Geoff Langc41e42d2014-04-28 10:58:16 -040052 (
53 precision highp float;
54 uniform sampler2D tex;
55 varying vec2 texcoord;
56
57 void main()
58 {
59 gl_FragColor = texture2D(tex, texcoord);
60 }
61 );
62
Jamie Madilld4cfa572014-07-08 10:00:32 -040063 const std::string fragmentShaderSourceCube = SHADER_SOURCE
64 (
65 precision highp float;
66 uniform sampler2D tex2D;
67 uniform samplerCube texCube;
68 varying vec2 texcoord;
69
70 void main()
71 {
72 gl_FragColor = texture2D(tex2D, texcoord);
73 gl_FragColor += textureCube(texCube, vec3(texcoord, 0));
74 }
75 );
76
Jamie Madill5599c8f2014-08-26 13:16:39 -040077 m2DProgram = CompileProgram(vertexShaderSource, fragmentShaderSource2D);
78 mCubeProgram = CompileProgram(vertexShaderSource, fragmentShaderSourceCube);
Jamie Madilld4cfa572014-07-08 10:00:32 -040079 if (m2DProgram == 0 || mCubeProgram == 0)
Geoff Langc41e42d2014-04-28 10:58:16 -040080 {
81 FAIL() << "shader compilation failed.";
82 }
83
Jamie Madilld4cfa572014-07-08 10:00:32 -040084 mTexture2DUniformLocation = glGetUniformLocation(m2DProgram, "tex");
Jamie Madill9aca0592014-10-06 16:26:59 -040085 ASSERT_NE(-1, mTexture2DUniformLocation);
86
87 mTextureScaleUniformLocation = glGetUniformLocation(m2DProgram, "textureScale");
88 ASSERT_NE(-1, mTextureScaleUniformLocation);
89
90 glUseProgram(m2DProgram);
91 glUniform2f(mTextureScaleUniformLocation, 1.0f, 1.0f);
92 glUseProgram(0);
93 ASSERT_GL_NO_ERROR();
Jamie Madillf67115c2014-04-22 13:14:05 -040094 }
95
96 virtual void TearDown()
97 {
Jamie Madilld4cfa572014-07-08 10:00:32 -040098 glDeleteTextures(1, &mTexture2D);
99 glDeleteTextures(1, &mTextureCube);
100 glDeleteProgram(m2DProgram);
101 glDeleteProgram(mCubeProgram);
Jamie Madillf67115c2014-04-22 13:14:05 -0400102
103 ANGLETest::TearDown();
104 }
105
Jamie Madilld4cfa572014-07-08 10:00:32 -0400106 GLuint mTexture2D;
107 GLuint mTextureCube;
Geoff Langc41e42d2014-04-28 10:58:16 -0400108
Jamie Madilld4cfa572014-07-08 10:00:32 -0400109 GLuint m2DProgram;
110 GLuint mCubeProgram;
111 GLint mTexture2DUniformLocation;
Jamie Madill9aca0592014-10-06 16:26:59 -0400112 GLint mTextureScaleUniformLocation;
Jamie Madillf67115c2014-04-22 13:14:05 -0400113};
114
Austin Kinross18b931d2014-09-29 12:58:31 -0700115TYPED_TEST(TextureTest, NegativeAPISubImage)
Jamie Madillf67115c2014-04-22 13:14:05 -0400116{
Jamie Madilld4cfa572014-07-08 10:00:32 -0400117 glBindTexture(GL_TEXTURE_2D, mTexture2D);
Jamie Madillf67115c2014-04-22 13:14:05 -0400118 EXPECT_GL_ERROR(GL_NO_ERROR);
119
120 const GLubyte *pixels[20] = { 0 };
121 glTexSubImage2D(GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
122 EXPECT_GL_ERROR(GL_INVALID_VALUE);
123}
Geoff Langc41e42d2014-04-28 10:58:16 -0400124
Austin Kinross18b931d2014-09-29 12:58:31 -0700125TYPED_TEST(TextureTest, ZeroSizedUploads)
Geoff Langc41e42d2014-04-28 10:58:16 -0400126{
Jamie Madilld4cfa572014-07-08 10:00:32 -0400127 glBindTexture(GL_TEXTURE_2D, mTexture2D);
Geoff Langc41e42d2014-04-28 10:58:16 -0400128 EXPECT_GL_ERROR(GL_NO_ERROR);
129
130 // Use the texture first to make sure it's in video memory
Jamie Madilld4cfa572014-07-08 10:00:32 -0400131 glUseProgram(m2DProgram);
132 glUniform1i(mTexture2DUniformLocation, 0);
133 drawQuad(m2DProgram, "position", 0.5f);
Geoff Langc41e42d2014-04-28 10:58:16 -0400134
135 const GLubyte *pixel[4] = { 0 };
136
137 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
138 EXPECT_GL_NO_ERROR();
139
140 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
141 EXPECT_GL_NO_ERROR();
142
143 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
144 EXPECT_GL_NO_ERROR();
145}
Jamie Madilld4cfa572014-07-08 10:00:32 -0400146
147// Test drawing with two texture types, to trigger an ANGLE bug in validation
Austin Kinross18b931d2014-09-29 12:58:31 -0700148TYPED_TEST(TextureTest, CubeMapBug)
Jamie Madilld4cfa572014-07-08 10:00:32 -0400149{
150 glActiveTexture(GL_TEXTURE0);
151 glBindTexture(GL_TEXTURE_2D, mTexture2D);
152 glActiveTexture(GL_TEXTURE1);
153 glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
154 EXPECT_GL_ERROR(GL_NO_ERROR);
155
156 glUseProgram(mCubeProgram);
157 GLint tex2DUniformLocation = glGetUniformLocation(mCubeProgram, "tex2D");
158 GLint texCubeUniformLocation = glGetUniformLocation(mCubeProgram, "texCube");
159 EXPECT_NE(-1, tex2DUniformLocation);
160 EXPECT_NE(-1, texCubeUniformLocation);
161 glUniform1i(tex2DUniformLocation, 0);
162 glUniform1i(texCubeUniformLocation, 1);
163 drawQuad(mCubeProgram, "position", 0.5f);
164 EXPECT_GL_NO_ERROR();
165}
Jamie Madill9aca0592014-10-06 16:26:59 -0400166
167// Copy of a test in conformance/textures/texture-mips, to test generate mipmaps
168TYPED_TEST(TextureTest, MipmapsTwice)
169{
170 int px = getWindowWidth() / 2;
171 int py = getWindowHeight() / 2;
172
173 glActiveTexture(GL_TEXTURE0);
174 glBindTexture(GL_TEXTURE_2D, mTexture2D);
175
176 // Fill with red
177 std::vector<GLubyte> pixels(4 * 16 * 16);
178 for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
179 {
180 pixels[pixelId * 4 + 0] = 255;
181 pixels[pixelId * 4 + 1] = 0;
182 pixels[pixelId * 4 + 2] = 0;
183 pixels[pixelId * 4 + 3] = 255;
184 }
185
186 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
189 glGenerateMipmap(GL_TEXTURE_2D);
190
191 glUseProgram(m2DProgram);
192 glUniform1i(mTexture2DUniformLocation, 0);
193 glUniform2f(mTextureScaleUniformLocation, 0.0625f, 0.0625f);
194 drawQuad(m2DProgram, "position", 0.5f);
195 EXPECT_GL_NO_ERROR();
196 EXPECT_PIXEL_EQ(px, py, 255, 0, 0, 255);
197
198 // Fill with blue
199 for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
200 {
201 pixels[pixelId * 4 + 0] = 0;
202 pixels[pixelId * 4 + 1] = 0;
203 pixels[pixelId * 4 + 2] = 255;
204 pixels[pixelId * 4 + 3] = 255;
205 }
206
207 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
208 glGenerateMipmap(GL_TEXTURE_2D);
209
210 // Fill with green
211 for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
212 {
213 pixels[pixelId * 4 + 0] = 0;
214 pixels[pixelId * 4 + 1] = 255;
215 pixels[pixelId * 4 + 2] = 0;
216 pixels[pixelId * 4 + 3] = 255;
217 }
218
219 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
220 glGenerateMipmap(GL_TEXTURE_2D);
221
222 drawQuad(m2DProgram, "position", 0.5f);
223
224 EXPECT_GL_NO_ERROR();
225 EXPECT_PIXEL_EQ(px, py, 0, 255, 0, 255);
226}