blob: 56a0877fb8e7f4ae30f0f9ca1cc92d37fc11cf08 [file] [log] [blame]
Geoff Lang496123f2014-02-12 11:33:51 -05001#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(BlendMinMaxTest, ES2_D3D9, ES2_D3D11);
Austin Kinross18b931d2014-09-29 12:58:31 -07005
6template<typename T>
Geoff Lang496123f2014-02-12 11:33:51 -05007class BlendMinMaxTest : public ANGLETest
8{
9protected:
Geoff Lang0d3683c2014-10-23 11:08:16 -040010 BlendMinMaxTest() : ANGLETest(T::GetGlesMajorVersion(), T::GetPlatform())
Geoff Lang496123f2014-02-12 11:33:51 -050011 {
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 Gong794e0002015-04-07 18:31:54 -070031 GLubyte curAsUbyte = static_cast<GLubyte>((curColor * std::numeric_limits<GLubyte>::max()) + 0.5f);
Geoff Lang496123f2014-02-12 11:33:51 -050032 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 Madill5599c8f2014-08-26 13:16:39 -040095 mProgram = CompileProgram(testVertexShaderSource, testFragmentShaderSource);
Geoff Lang496123f2014-02-12 11:33:51 -050096 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 Kinross18b931d2014-09-29 12:58:31 -0700143TYPED_TEST(BlendMinMaxTest, RGBA8)
Geoff Lang496123f2014-02-12 11:33:51 -0500144{
145 SetUpFramebuffer(GL_RGBA8);
146 runTest();
147}
148
Austin Kinross18b931d2014-09-29 12:58:31 -0700149TYPED_TEST(BlendMinMaxTest, RGBA32f)
Geoff Lang496123f2014-02-12 11:33:51 -0500150{
151 SetUpFramebuffer(GL_RGBA32F);
152 runTest();
153}
154
Austin Kinross18b931d2014-09-29 12:58:31 -0700155TYPED_TEST(BlendMinMaxTest, RGBA16F)
Geoff Lang496123f2014-02-12 11:33:51 -0500156{
157 SetUpFramebuffer(GL_RGBA16F);
158 runTest();
159}