Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / tests / angle_tests / TextureTest.cpp
1 #include "ANGLETest.h"
2
3 // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
4 typedef ::testing::Types<TFT<Gles::Two, Rend::D3D11>, TFT<Gles::Two, Rend::D3D9>> TestFixtureTypes;
5 TYPED_TEST_CASE(TextureTest, TestFixtureTypes);
6
7 template<typename T>
8 class TextureTest : public ANGLETest
9 {
10 protected:
11     TextureTest() : ANGLETest(T::GetGlesMajorVersion(), T::GetRequestedRenderer())
12     {
13         setWindowWidth(128);
14         setWindowHeight(128);
15         setConfigRedBits(8);
16         setConfigGreenBits(8);
17         setConfigBlueBits(8);
18         setConfigAlphaBits(8);
19     }
20
21     virtual void SetUp()
22     {
23         ANGLETest::SetUp();
24         glGenTextures(1, &mTexture2D);
25         glGenTextures(1, &mTextureCube);
26
27         glBindTexture(GL_TEXTURE_2D, mTexture2D);
28         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
29         EXPECT_GL_NO_ERROR();
30
31         glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
32         glTexStorage2DEXT(GL_TEXTURE_CUBE_MAP, 1, GL_RGBA8, 1, 1);
33         EXPECT_GL_NO_ERROR();
34
35         ASSERT_GL_NO_ERROR();
36
37         const std::string vertexShaderSource = SHADER_SOURCE
38         (
39             precision highp float;
40             attribute vec4 position;
41             varying vec2 texcoord;
42
43             uniform vec2 textureScale;
44
45             void main()
46             {
47                 gl_Position = vec4(position.xy * textureScale, 0.0, 1.0);
48                 texcoord = (position.xy * 0.5) + 0.5;
49             }
50         );
51
52         const std::string fragmentShaderSource2D = SHADER_SOURCE
53         (
54             precision highp float;
55             uniform sampler2D tex;
56             varying vec2 texcoord;
57
58             void main()
59             {
60                 gl_FragColor = texture2D(tex, texcoord);
61             }
62         );
63
64         const std::string fragmentShaderSourceCube = SHADER_SOURCE
65         (
66             precision highp float;
67             uniform sampler2D tex2D;
68             uniform samplerCube texCube;
69             varying vec2 texcoord;
70
71             void main()
72             {
73                 gl_FragColor = texture2D(tex2D, texcoord);
74                 gl_FragColor += textureCube(texCube, vec3(texcoord, 0));
75             }
76         );
77
78         m2DProgram = CompileProgram(vertexShaderSource, fragmentShaderSource2D);
79         mCubeProgram = CompileProgram(vertexShaderSource, fragmentShaderSourceCube);
80         if (m2DProgram == 0 || mCubeProgram == 0)
81         {
82             FAIL() << "shader compilation failed.";
83         }
84
85         mTexture2DUniformLocation = glGetUniformLocation(m2DProgram, "tex");
86         ASSERT_NE(-1, mTexture2DUniformLocation);
87
88         mTextureScaleUniformLocation = glGetUniformLocation(m2DProgram, "textureScale");
89         ASSERT_NE(-1, mTextureScaleUniformLocation);
90
91         glUseProgram(m2DProgram);
92         glUniform2f(mTextureScaleUniformLocation, 1.0f, 1.0f);
93         glUseProgram(0);
94         ASSERT_GL_NO_ERROR();
95     }
96
97     virtual void TearDown()
98     {
99         glDeleteTextures(1, &mTexture2D);
100         glDeleteTextures(1, &mTextureCube);
101         glDeleteProgram(m2DProgram);
102         glDeleteProgram(mCubeProgram);
103
104         ANGLETest::TearDown();
105     }
106
107     GLuint mTexture2D;
108     GLuint mTextureCube;
109
110     GLuint m2DProgram;
111     GLuint mCubeProgram;
112     GLint mTexture2DUniformLocation;
113     GLint mTextureScaleUniformLocation;
114 };
115
116 TYPED_TEST(TextureTest, NegativeAPISubImage)
117 {
118     glBindTexture(GL_TEXTURE_2D, mTexture2D);
119     EXPECT_GL_ERROR(GL_NO_ERROR);
120
121     const GLubyte *pixels[20] = { 0 };
122     glTexSubImage2D(GL_TEXTURE_2D, 0, 1, 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
123     EXPECT_GL_ERROR(GL_INVALID_VALUE);
124 }
125
126 TYPED_TEST(TextureTest, ZeroSizedUploads)
127 {
128     glBindTexture(GL_TEXTURE_2D, mTexture2D);
129     EXPECT_GL_ERROR(GL_NO_ERROR);
130
131     // Use the texture first to make sure it's in video memory
132     glUseProgram(m2DProgram);
133     glUniform1i(mTexture2DUniformLocation, 0);
134     drawQuad(m2DProgram, "position", 0.5f);
135
136     const GLubyte *pixel[4] = { 0 };
137
138     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
139     EXPECT_GL_NO_ERROR();
140
141     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
142     EXPECT_GL_NO_ERROR();
143
144     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
145     EXPECT_GL_NO_ERROR();
146 }
147
148 // Test drawing with two texture types, to trigger an ANGLE bug in validation
149 TYPED_TEST(TextureTest, CubeMapBug)
150 {
151     glActiveTexture(GL_TEXTURE0);
152     glBindTexture(GL_TEXTURE_2D, mTexture2D);
153     glActiveTexture(GL_TEXTURE1);
154     glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureCube);
155     EXPECT_GL_ERROR(GL_NO_ERROR);
156
157     glUseProgram(mCubeProgram);
158     GLint tex2DUniformLocation = glGetUniformLocation(mCubeProgram, "tex2D");
159     GLint texCubeUniformLocation = glGetUniformLocation(mCubeProgram, "texCube");
160     EXPECT_NE(-1, tex2DUniformLocation);
161     EXPECT_NE(-1, texCubeUniformLocation);
162     glUniform1i(tex2DUniformLocation, 0);
163     glUniform1i(texCubeUniformLocation, 1);
164     drawQuad(mCubeProgram, "position", 0.5f);
165     EXPECT_GL_NO_ERROR();
166 }
167
168 // Copy of a test in conformance/textures/texture-mips, to test generate mipmaps
169 TYPED_TEST(TextureTest, MipmapsTwice)
170 {
171     int px = getWindowWidth() / 2;
172     int py = getWindowHeight() / 2;
173
174     glActiveTexture(GL_TEXTURE0);
175     glBindTexture(GL_TEXTURE_2D, mTexture2D);
176
177     // Fill with red
178     std::vector<GLubyte> pixels(4 * 16 * 16);
179     for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
180     {
181         pixels[pixelId * 4 + 0] = 255;
182         pixels[pixelId * 4 + 1] = 0;
183         pixels[pixelId * 4 + 2] = 0;
184         pixels[pixelId * 4 + 3] = 255;
185     }
186
187     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
188     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
189     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
190     glGenerateMipmap(GL_TEXTURE_2D);
191
192     glUseProgram(m2DProgram);
193     glUniform1i(mTexture2DUniformLocation, 0);
194     glUniform2f(mTextureScaleUniformLocation, 0.0625f, 0.0625f);
195     drawQuad(m2DProgram, "position", 0.5f);
196     EXPECT_GL_NO_ERROR();
197     EXPECT_PIXEL_EQ(px, py, 255, 0, 0, 255);
198
199     // Fill with blue
200     for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
201     {
202         pixels[pixelId * 4 + 0] = 0;
203         pixels[pixelId * 4 + 1] = 0;
204         pixels[pixelId * 4 + 2] = 255;
205         pixels[pixelId * 4 + 3] = 255;
206     }
207
208     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
209     glGenerateMipmap(GL_TEXTURE_2D);
210
211     // Fill with green
212     for (size_t pixelId = 0; pixelId < 16 * 16; ++pixelId)
213     {
214         pixels[pixelId * 4 + 0] = 0;
215         pixels[pixelId * 4 + 1] = 255;
216         pixels[pixelId * 4 + 2] = 0;
217         pixels[pixelId * 4 + 3] = 255;
218     }
219
220     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
221     glGenerateMipmap(GL_TEXTURE_2D);
222
223     drawQuad(m2DProgram, "position", 0.5f);
224
225     EXPECT_GL_NO_ERROR();
226     EXPECT_PIXEL_EQ(px, py, 0, 255, 0, 255);
227 }