Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 1 | #include "ANGLETest.h" |
| 2 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 3 | // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against. |
Geoff Lang | 0d3683c | 2014-10-23 11:08:16 -0400 | [diff] [blame] | 4 | ANGLE_TYPED_TEST_CASE(BlendMinMaxTest, ES2_D3D9, ES2_D3D11); |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 5 | |
| 6 | template<typename T> |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 7 | class BlendMinMaxTest : public ANGLETest |
| 8 | { |
| 9 | protected: |
Geoff Lang | 0d3683c | 2014-10-23 11:08:16 -0400 | [diff] [blame] | 10 | BlendMinMaxTest() : ANGLETest(T::GetGlesMajorVersion(), T::GetPlatform()) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 11 | { |
| 12 | setWindowWidth(128); |
| 13 | setWindowHeight(128); |
| 14 | setConfigRedBits(8); |
| 15 | setConfigGreenBits(8); |
| 16 | setConfigBlueBits(8); |
| 17 | setConfigAlphaBits(8); |
| 18 | setConfigDepthBits(24); |
| 19 | |
| 20 | mProgram = 0; |
| 21 | mColorLocation = -1; |
| 22 | } |
| 23 | |
| 24 | struct Color |
| 25 | { |
| 26 | float values[4]; |
| 27 | }; |
| 28 | |
| 29 | static GLubyte getExpected(bool blendMin, float curColor, GLubyte prevColor) |
| 30 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame^] | 31 | GLubyte curAsUbyte = static_cast<GLubyte>((curColor * std::numeric_limits<GLubyte>::max()) + 0.5f); |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 32 | return blendMin ? std::min<GLubyte>(curAsUbyte, prevColor) : std::max<GLubyte>(curAsUbyte, prevColor); |
| 33 | } |
| 34 | |
| 35 | void runTest() |
| 36 | { |
| 37 | const size_t colorCount = 1024; |
| 38 | Color colors[colorCount]; |
| 39 | for (size_t i = 0; i < colorCount; i++) |
| 40 | { |
| 41 | for (size_t j = 0; j < 4; j++) |
| 42 | { |
| 43 | colors[i].values[j] = (rand() % 255) / 255.0f; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | GLubyte prevColor[4]; |
| 48 | for (size_t i = 0; i < colorCount; i++) |
| 49 | { |
| 50 | const Color &color = colors[i]; |
| 51 | glUseProgram(mProgram); |
| 52 | glUniform4f(mColorLocation, color.values[0], color.values[1], color.values[2], color.values[3]); |
| 53 | |
| 54 | bool blendMin = (rand() % 2 == 0); |
| 55 | glBlendEquation(blendMin ? GL_MIN : GL_MAX); |
| 56 | |
| 57 | drawQuad(mProgram, "aPosition", 0.5f); |
| 58 | |
| 59 | if (i > 0) |
| 60 | { |
| 61 | EXPECT_PIXEL_EQ(0, 0, |
| 62 | getExpected(blendMin, color.values[0], prevColor[0]), |
| 63 | getExpected(blendMin, color.values[1], prevColor[1]), |
| 64 | getExpected(blendMin, color.values[2], prevColor[2]), |
| 65 | getExpected(blendMin, color.values[3], prevColor[3])); |
| 66 | } |
| 67 | |
| 68 | glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, prevColor); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | virtual void SetUp() |
| 73 | { |
| 74 | ANGLETest::SetUp(); |
| 75 | |
| 76 | const std::string testVertexShaderSource = SHADER_SOURCE |
| 77 | ( |
| 78 | attribute highp vec4 aPosition; |
| 79 | |
| 80 | void main(void) |
| 81 | { |
| 82 | gl_Position = aPosition; |
| 83 | } |
| 84 | ); |
| 85 | |
| 86 | const std::string testFragmentShaderSource = SHADER_SOURCE |
| 87 | ( |
| 88 | uniform highp vec4 color; |
| 89 | void main(void) |
| 90 | { |
| 91 | gl_FragColor = color; |
| 92 | } |
| 93 | ); |
| 94 | |
Jamie Madill | 5599c8f | 2014-08-26 13:16:39 -0400 | [diff] [blame] | 95 | mProgram = CompileProgram(testVertexShaderSource, testFragmentShaderSource); |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 96 | if (mProgram == 0) |
| 97 | { |
| 98 | FAIL() << "shader compilation failed."; |
| 99 | } |
| 100 | |
| 101 | mColorLocation = glGetUniformLocation(mProgram, "color"); |
| 102 | |
| 103 | glUseProgram(mProgram); |
| 104 | |
| 105 | glClearColor(0, 0, 0, 0); |
| 106 | glClearDepthf(0.0); |
| 107 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 108 | |
| 109 | glEnable(GL_BLEND); |
| 110 | glDisable(GL_DEPTH_TEST); |
| 111 | } |
| 112 | |
| 113 | void SetUpFramebuffer(GLenum colorFormat) |
| 114 | { |
| 115 | glGenFramebuffers(1, &mFramebuffer); |
| 116 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebuffer); |
| 117 | glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebuffer); |
| 118 | |
| 119 | glGenRenderbuffers(1, &mColorRenderbuffer); |
| 120 | glBindRenderbuffer(GL_RENDERBUFFER, mColorRenderbuffer); |
| 121 | glRenderbufferStorage(GL_RENDERBUFFER, colorFormat, getWindowWidth(), getWindowHeight()); |
| 122 | glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mColorRenderbuffer); |
| 123 | |
| 124 | ASSERT_GL_NO_ERROR(); |
| 125 | } |
| 126 | |
| 127 | virtual void TearDown() |
| 128 | { |
| 129 | glDeleteProgram(mProgram); |
| 130 | glDeleteFramebuffers(1, &mFramebuffer); |
| 131 | glDeleteRenderbuffers(1, &mColorRenderbuffer); |
| 132 | |
| 133 | ANGLETest::TearDown(); |
| 134 | } |
| 135 | |
| 136 | GLuint mProgram; |
| 137 | GLint mColorLocation; |
| 138 | |
| 139 | GLuint mFramebuffer; |
| 140 | GLuint mColorRenderbuffer; |
| 141 | }; |
| 142 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 143 | TYPED_TEST(BlendMinMaxTest, RGBA8) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 144 | { |
| 145 | SetUpFramebuffer(GL_RGBA8); |
| 146 | runTest(); |
| 147 | } |
| 148 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 149 | TYPED_TEST(BlendMinMaxTest, RGBA32f) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 150 | { |
| 151 | SetUpFramebuffer(GL_RGBA32F); |
| 152 | runTest(); |
| 153 | } |
| 154 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 155 | TYPED_TEST(BlendMinMaxTest, RGBA16F) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 156 | { |
| 157 | SetUpFramebuffer(GL_RGBA16F); |
| 158 | runTest(); |
| 159 | } |