Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / tmaskedclear.cpp
blobb8fd6f9ecae53c3c2aa91e1bc9c6dfc818a593c1
1 // BEGIN_COPYRIGHT -*- glean -*-
2 //
3 // Copyright (C) 1999 Allen Akin All Rights Reserved.
4 //
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
12 // conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the
16 // Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
26 //
27 // END_COPYRIGHT
29 // tmaskedclear.cpp: Test color/index masking with glClear.
31 #include "tmaskedclear.h"
32 #include "rand.h"
33 #include "image.h"
35 namespace GLEAN {
37 void
38 MaskedClearTest::failRGB(BasicResult &r, GLint chan, GLfloat expected,
39 GLfloat actual, GLint buffer)
41 static const char *chanNames[] = { "Red", "Green", "Blue", "Alpha" };
42 static const char *bufferNames[] = { "GL_FRONT", "GL_BACK" };
43 GLboolean mask[4];
44 glGetBooleanv(GL_COLOR_WRITEMASK, mask);
45 env->log << name << ": FAIL "
46 << r.config->conciseDescription() << '\n'
47 << "\t" << chanNames[chan] << " is " << actual
48 << ", expected " << expected
49 << " in " << bufferNames[buffer] << " buffer\n";
50 env->log << "\tGL_COLOR_WRITEMASK = ("
51 << (mask[0] ? "GL_TRUE" : "GL_FALSE") << ", "
52 << (mask[1] ? "GL_TRUE" : "GL_FALSE") << ", "
53 << (mask[2] ? "GL_TRUE" : "GL_FALSE") << ", "
54 << (mask[3] ? "GL_TRUE" : "GL_FALSE") << ")\n";
57 void
58 MaskedClearTest::failCI(BasicResult& r, GLuint expected, GLuint actual,
59 GLint buffer)
61 static const char *bufferNames[] = { "GL_FRONT", "GL_BACK" };
62 GLint mask;
63 glGetIntegerv(GL_INDEX_WRITEMASK, &mask);
64 env->log << name << ": FAIL "
65 << r.config->conciseDescription() << '\n'
66 << "\tcolor index is " << actual
67 << ", expected " << expected
68 << " in " << bufferNames[buffer] << " buffer\n";
69 env->log << "\tGL_INDEX_WRITEMASK = " << mask << "\n";
72 void
73 MaskedClearTest::failZ(BasicResult& r, GLfloat expected, GLfloat actual)
75 GLboolean mask;
76 glGetBooleanv(GL_DEPTH_WRITEMASK, &mask);
77 env->log << name << ": FAIL "
78 << r.config->conciseDescription() << '\n'
79 << "\tdepth buffer value is " << actual
80 << ", expected " << expected << "\n";
81 env->log << "\tGL_DEPTH_WRITEMASK = "
82 << (mask ? "GL_TRUE" : "GL_FALSE") << "\n";
85 void
86 MaskedClearTest::failStencil(BasicResult& r, GLuint expected, GLuint actual)
88 GLint mask;
89 glGetIntegerv(GL_STENCIL_WRITEMASK, &mask);
90 env->log << name << ": FAIL "
91 << r.config->conciseDescription() << '\n'
92 << "\tstencil buffer value is " << actual
93 << ", expected " << expected << "\n";
94 env->log << "\tGL_STENCIL_WRITEMASK = " << mask << "\n";
97 ///////////////////////////////////////////////////////////////////////////////
98 // runOne: Run a single test case
99 ///////////////////////////////////////////////////////////////////////////////
100 void
101 MaskedClearTest::runOne(BasicResult& r, Window&) {
103 bool passed = true;
105 // GL init, just to be safe
106 glDisable(GL_SCISSOR_TEST);
108 // only test front/back-left buffers, quad-buffer stereo in the future
109 const GLint numBuffers = r.config->db ? 2 : 1;
110 for (GLint buffer = 0; buffer < numBuffers && passed; buffer++) {
112 if (buffer == 0) {
113 glReadBuffer(GL_FRONT);
114 glDrawBuffer(GL_FRONT);
115 } else {
116 glReadBuffer(GL_BACK);
117 glDrawBuffer(GL_BACK);
120 if (r.config->canRGBA) {
121 const GLint numChannels = (r.config->a > 0) ? 4 : 3;
122 for (GLint chan = 0;
123 chan < numChannels && passed; chan++) {
124 // clear to black
125 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
126 glClearColor(0.0, 0.0, 0.0, 0.0);
127 glClear(GL_COLOR_BUFFER_BIT);
129 // select one channel to "clear" to 1.0
130 glColorMask(chan == 0, chan == 1,
131 chan == 2, chan == 3);
133 // try to clear surface to white
134 glClearColor(1.0, 1.0, 1.0, 1.0);
135 glClear(GL_COLOR_BUFFER_BIT);
137 // read 1x1 image at (x,y)=(4,4)
138 GLfloat pixel[4];
139 glReadPixels(4, 4, 1, 1,
140 GL_RGBA, GL_FLOAT, pixel);
142 // test results
143 for (GLint comp = 0;
144 comp < numChannels && passed; comp++) {
145 if (comp == chan) {
146 // component should be 1.0
147 if (pixel[comp] < 0.5) {
148 passed = false;
149 failRGB(r, comp, 1.0,
150 pixel[comp], buffer);
152 } else {
153 // component should be 0.0
154 if (pixel[comp] > 0.5) {
155 passed = false;
156 failRGB(r, comp, 0.0,
157 pixel[comp], buffer);
163 else {
164 const GLint indexBits = r.config->bufSize;
165 // We just run <indexBits> tests rather than 2^indexBits
166 for (GLint bit = 0; bit < indexBits && passed; bit++) {
167 // clear to 0
168 glIndexMask(~0);
169 glClearIndex(0);
170 glClear(GL_COLOR_BUFFER_BIT);
172 // select one bit to "clear" to 1
173 glIndexMask(1 << bit);
175 // try to clear surface to ~0
176 glClearIndex(~0);
177 glClear(GL_COLOR_BUFFER_BIT);
179 // read 1x1 image at (x,y)=(4,4)
180 GLuint pixel;
181 glReadPixels(4, 4, 1, 1,
182 GL_COLOR_INDEX, GL_UNSIGNED_INT, &pixel);
184 // test results
185 if (pixel != (1U << bit)) {
186 passed = false;
187 failCI(r, 1 << bit, pixel, buffer);
193 if (passed && r.config->z > 0) {
194 // clear depth buffer to zero
195 glDepthMask(GL_TRUE);
196 glClearDepth(0.0);
197 glClear(GL_DEPTH_BUFFER_BIT);
199 // disable Z writes, try to clear to one
200 glDepthMask(GL_FALSE);
201 glClearDepth(1.0);
202 glClear(GL_DEPTH_BUFFER_BIT);
204 // read 1x1 image at (x,y)=(4,4);
205 GLfloat depth;
206 glReadPixels(4, 4, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
208 // test result
209 if (depth != 0.0) {
210 passed = false;
211 failZ(r, 0.0, depth);
215 if (passed && r.config->s > 0) {
216 const GLint stencilBits = r.config->s;
217 // We just run <stencilBits> tests rather than 2^stencilBits
218 for (GLint bit = 0; bit < stencilBits && passed; bit++) {
219 // clear to 0
220 glStencilMask(~0);
221 glClearStencil(0);
222 glClear(GL_STENCIL_BUFFER_BIT);
224 // select one bit to "clear" to 1
225 glStencilMask(1 << bit);
227 // try to clear stencil buffer to ~0
228 glClearStencil(~0);
229 glClear(GL_STENCIL_BUFFER_BIT);
231 // read 1x1 image at (x,y)=(4,4)
232 GLuint stencil;
233 glReadPixels(4, 4, 1, 1,
234 GL_STENCIL_INDEX, GL_UNSIGNED_INT, &stencil);
236 // test results
237 if (stencil != (1U << bit)) {
238 passed = false;
239 failStencil(r, 1 << bit, stencil);
243 r.pass = passed;
244 } // MaskedClearTest::runOne
247 ///////////////////////////////////////////////////////////////////////////////
248 // logOne: Log a single test case
249 ///////////////////////////////////////////////////////////////////////////////
250 void
251 MaskedClearTest::logOne(BasicResult& r) {
252 if (r.pass) {
253 logPassFail(r);
254 logConcise(r);
256 } // MaskedClearTest::logOne
258 ///////////////////////////////////////////////////////////////////////////////
259 // The test object itself:
260 ///////////////////////////////////////////////////////////////////////////////
261 MaskedClearTest maskedClearTest("maskedClear", "window",
262 "This test checks that glClear works correctly with glColorMask,\n"
263 "glIndexMask, glDepthMask and glStencilMask.\n");
266 } // namespace GLEAN