blob: c8356763519abcbabc190f85ff2c27116f7e9959 [file] [log] [blame]
Jamie Madillfa05f602015-05-07 13:47:11 -04001//
2// Copyright 2015 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Corentin Wallezd3970de2015-05-14 11:07:48 -04007#include "test_utils/ANGLETest.h"
Geoff Lang0ddab0c2013-10-23 13:24:41 -04008
9#include <vector>
Jamie Madill81c2e252017-09-09 23:32:46 -040010#include "test_utils/gl_raii.h"
Geoff Lang0ddab0c2013-10-23 13:24:41 -040011
Jamie Madillfa05f602015-05-07 13:47:11 -040012using namespace angle;
Austin Kinross18b931d2014-09-29 12:58:31 -070013
Geoff Lang0ddab0c2013-10-23 13:24:41 -040014class IncompleteTextureTest : public ANGLETest
15{
Jamie Madillfa05f602015-05-07 13:47:11 -040016 protected:
17 IncompleteTextureTest()
Geoff Lang0ddab0c2013-10-23 13:24:41 -040018 {
19 setWindowWidth(128);
20 setWindowHeight(128);
21 setConfigRedBits(8);
22 setConfigGreenBits(8);
23 setConfigBlueBits(8);
24 setConfigAlphaBits(8);
25 }
26
27 virtual void SetUp()
28 {
29 ANGLETest::SetUp();
30
31 const std::string vertexShaderSource = SHADER_SOURCE
32 (
33 precision highp float;
34 attribute vec4 position;
35 varying vec2 texcoord;
36
37 void main()
38 {
39 gl_Position = position;
40 texcoord = (position.xy * 0.5) + 0.5;
41 }
42 );
43
44 const std::string fragmentShaderSource = SHADER_SOURCE
45 (
46 precision highp float;
47 uniform sampler2D tex;
48 varying vec2 texcoord;
49
50 void main()
51 {
52 gl_FragColor = texture2D(tex, texcoord);
53 }
54 );
55
Jamie Madill5599c8f2014-08-26 13:16:39 -040056 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
Geoff Lang0ddab0c2013-10-23 13:24:41 -040057 if (mProgram == 0)
58 {
59 FAIL() << "shader compilation failed.";
60 }
61
62 mTextureUniformLocation = glGetUniformLocation(mProgram, "tex");
63 }
64
65 virtual void TearDown()
66 {
67 glDeleteProgram(mProgram);
68
69 ANGLETest::TearDown();
70 }
71
72 void fillTextureData(std::vector<GLubyte> &buffer, GLubyte r, GLubyte g, GLubyte b, GLubyte a)
73 {
74 size_t count = buffer.size() / 4;
75 for (size_t i = 0; i < count; i++)
76 {
77 buffer[i * 4 + 0] = r;
78 buffer[i * 4 + 1] = g;
79 buffer[i * 4 + 2] = b;
80 buffer[i * 4 + 3] = a;
81 }
82 }
83
84 GLuint mProgram;
85 GLint mTextureUniformLocation;
86};
87
Jamie Madillfa05f602015-05-07 13:47:11 -040088TEST_P(IncompleteTextureTest, IncompleteTexture2D)
Geoff Lang0ddab0c2013-10-23 13:24:41 -040089{
Jamie Madill81c2e252017-09-09 23:32:46 -040090 GLTexture tex;
Geoff Lang0ddab0c2013-10-23 13:24:41 -040091 glActiveTexture(GL_TEXTURE0);
92 glBindTexture(GL_TEXTURE_2D, tex);
93
94 glUseProgram(mProgram);
95 glUniform1i(mTextureUniformLocation, 0);
96
Jamie Madill81c2e252017-09-09 23:32:46 -040097 constexpr GLsizei kTextureSize = 2;
98 std::vector<GLColor> textureData(kTextureSize * kTextureSize, GLColor::red);
Geoff Lang0ddab0c2013-10-23 13:24:41 -040099
Jamie Madill81c2e252017-09-09 23:32:46 -0400100 // Make a complete texture.
101 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTextureSize, kTextureSize, 0, GL_RGBA,
102 GL_UNSIGNED_BYTE, textureData.data());
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
104
Jamie Madill81c2e252017-09-09 23:32:46 -0400105 // Should be complete - expect red.
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400106 drawQuad(mProgram, "position", 0.5f);
Jamie Madill81c2e252017-09-09 23:32:46 -0400107 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400108
Jamie Madill81c2e252017-09-09 23:32:46 -0400109 // Make texture incomplete.
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
111
Jamie Madill81c2e252017-09-09 23:32:46 -0400112 // Should be incomplete - expect black.
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400113 drawQuad(mProgram, "position", 0.5f);
Jamie Madill81c2e252017-09-09 23:32:46 -0400114 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400115
Jamie Madill81c2e252017-09-09 23:32:46 -0400116 // Make texture complete by defining the second mip.
117 glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, kTextureSize >> 1, kTextureSize >> 1, 0, GL_RGBA,
118 GL_UNSIGNED_BYTE, textureData.data());
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400119
Jamie Madill81c2e252017-09-09 23:32:46 -0400120 // Should be complete - expect red.
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400121 drawQuad(mProgram, "position", 0.5f);
Jamie Madill81c2e252017-09-09 23:32:46 -0400122 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400123}
124
Jamie Madillfa05f602015-05-07 13:47:11 -0400125TEST_P(IncompleteTextureTest, UpdateTexture)
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400126{
Jamie Madill81c2e252017-09-09 23:32:46 -0400127 GLTexture tex;
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400128 glActiveTexture(GL_TEXTURE0);
129 glBindTexture(GL_TEXTURE_2D, tex);
130
131 glUseProgram(mProgram);
132 glUniform1i(mTextureUniformLocation, 0);
133
134 const GLsizei redTextureWidth = 64;
135 const GLsizei redTextureHeight = 64;
136 std::vector<GLubyte> redTextureData(redTextureWidth * redTextureHeight * 4);
137 fillTextureData(redTextureData, 255, 0, 0, 255);
138 for (size_t i = 0; i < 7; i++)
139 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700140 glTexImage2D(GL_TEXTURE_2D, static_cast<GLint>(i), GL_RGBA, redTextureWidth >> i,
141 redTextureHeight >> i, 0, GL_RGBA, GL_UNSIGNED_BYTE, &redTextureData[0]);
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400142 }
143
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
146
147 drawQuad(mProgram, "position", 0.5f);
148 EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
149
150 const GLsizei greenTextureWidth = 32;
151 const GLsizei greenTextureHeight = 32;
152 std::vector<GLubyte> greenTextureData(greenTextureWidth * greenTextureHeight * 4);
153 fillTextureData(greenTextureData, 0, 255, 0, 255);
154
155 for (size_t i = 0; i < 6; i++)
156 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700157 glTexSubImage2D(GL_TEXTURE_2D, static_cast<GLint>(i), greenTextureWidth >> i,
158 greenTextureHeight >> i, greenTextureWidth >> i, greenTextureHeight >> i,
159 GL_RGBA, GL_UNSIGNED_BYTE, &greenTextureData[0]);
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400160 }
161
162 drawQuad(mProgram, "position", 0.5f);
163 EXPECT_PIXEL_EQ(getWindowWidth() - greenTextureWidth, getWindowHeight() - greenTextureWidth, 0, 255, 0, 255);
Geoff Lang0ddab0c2013-10-23 13:24:41 -0400164}
Jamie Madillfa05f602015-05-07 13:47:11 -0400165
166// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
Geoff Lange0cc2a42016-01-20 10:58:17 -0500167ANGLE_INSTANTIATE_TEST(IncompleteTextureTest,
168 ES2_D3D9(),
169 ES2_D3D11(),
170 ES2_OPENGL(),
171 ES2_OPENGLES());