hiz-util.c 18.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Copyright © 2011 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *     Chad Versace <chad.versace@intel.com>
 */

/**
 * \file hiz-util.c
 * \copybrief hiz-util.h
 * \author Chad Versace <chad.versace@intel.com>
 */

#include <assert.h>
#include "piglit-util.h"
#include "hiz/hiz-util.h"

Chad Versace's avatar
Chad Versace committed
37
38
39
40
41
42
43
44
45
46
47
#define hiz_probe_common(probe_func, expect) \
	bool pass = true; \
	const float dx = piglit_width / 9.0; \
	const float dy = piglit_height / 9.0; \
	\
	int ix; \
	int iy; \
	\
	for (iy = 0; iy < 3; ++iy) for (ix = 0; ix < 3; ++ix) {	\
		int x = (3 * ix + 1) * dx; \
		int y = (3 * iy + 1) * dy; \
48
		int i = 3 * (2 - iy) + ix;		     \
Chad Versace's avatar
Chad Versace committed
49
50
51
52
53
54
		pass &= probe_func(x, y, dx, dy, expect[i]); \
	} \
	\
	return pass;


55
bool
Chad Versace's avatar
Chad Versace committed
56
hiz_probe_color_buffer(const float *expected_colors[])
57
{
Chad Versace's avatar
Chad Versace committed
58
59
	hiz_probe_common(piglit_probe_rect_rgb, expected_colors);
}
60

Chad Versace's avatar
Chad Versace committed
61
62
63
64
bool
hiz_probe_depth_buffer(const float expected_depths[])
{
	hiz_probe_common(piglit_probe_rect_depth, expected_depths);
65
66
}

67
68
69
70
71
72
bool
hiz_probe_stencil_buffer(const unsigned expected_stencil[])
{
	hiz_probe_common(piglit_probe_rect_stencil, expected_stencil);
}

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

GLuint
hiz_make_fbo(const struct hiz_fbo_options *options)
{
	GLuint fb = 0;
	GLenum fb_status;
	GLuint color_rb = 0;
	GLuint depth_rb = 0;
	GLuint stencil_rb = 0;
	GLuint depth_stencil_rb = 0;

	glGenFramebuffers(1, &fb);
	glBindFramebuffer(GL_FRAMEBUFFER, fb);

	/* Bind color attachment. */
	if (options->color_format != 0) {
		glGenRenderbuffers(1, &color_rb);
		glBindRenderbuffer(GL_RENDERBUFFER, color_rb);
		glRenderbufferStorage(GL_RENDERBUFFER,
		                      options->color_format,
		                      piglit_width, piglit_height);
		glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
		                          GL_COLOR_ATTACHMENT0,
		                          GL_RENDERBUFFER,
		                          color_rb);
98
         	piglit_check_gl_error(0, PIGLIT_FAIL);
99
100
101
102
103
104
105
106
107
108
109
110
111
	}

	/* Bind depth attachment. */
	if (options->depth_format != 0) {
		glGenRenderbuffers(1, &depth_rb);
		glBindRenderbuffer(GL_RENDERBUFFER, depth_rb);
		glRenderbufferStorage(GL_RENDERBUFFER,
		                      options->depth_format,
		                      piglit_width, piglit_height);
		glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
		                          GL_DEPTH_ATTACHMENT,
		                          GL_RENDERBUFFER,
		                          depth_rb);
112
         	piglit_check_gl_error(0, PIGLIT_FAIL);
113
114
115
116
117
118
119
120
121
122
123
124
125
	}

	/* Bind stencil attachment. */
	if (options->stencil_format != 0) {
		glGenRenderbuffers(1, &stencil_rb);
		glBindRenderbuffer(GL_RENDERBUFFER, stencil_rb);
		glRenderbufferStorage(GL_RENDERBUFFER,
		                      options->stencil_format,
		                      piglit_width, piglit_height);
		glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
		                          GL_STENCIL_ATTACHMENT,
		                          GL_RENDERBUFFER,
		                          stencil_rb);
126
         	piglit_check_gl_error(0, PIGLIT_FAIL);
127
128
129
130
131
132
133
134
135
136
137
138
139
	}

	/* Bind depth/stencil attachment. */
	if (options->depth_stencil_format != 0) {
		glGenRenderbuffers(1, &depth_stencil_rb);
		glBindRenderbuffer(GL_RENDERBUFFER, depth_stencil_rb);
		glRenderbufferStorage(GL_RENDERBUFFER,
		                      options->depth_stencil_format,
		                      piglit_width, piglit_height);
		glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
		                          GL_DEPTH_STENCIL_ATTACHMENT,
		                          GL_RENDERBUFFER,
		                          depth_stencil_rb);
140
         	piglit_check_gl_error(0, PIGLIT_FAIL);
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
	}

	fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
		printf("error: FBO incomplete (status = 0x%04x)\n", fb_status);
		piglit_report_result(PIGLIT_SKIP);
	}

	return fb;
}

void
hiz_delete_fbo(GLuint fbo)
{
	const GLenum *i;
	const GLenum attachments[] = {
		GL_COLOR_ATTACHMENT0,
		GL_DEPTH_STENCIL_ATTACHMENT,
		GL_DEPTH_ATTACHMENT,
		GL_STENCIL_ATTACHMENT,
		0
	};

	for (i = attachments; *i != 0; ++i) {
		GLuint name;
		glGetFramebufferAttachmentParameteriv(
			GL_DRAW_FRAMEBUFFER,
			GL_COLOR_ATTACHMENT0,
			GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
			(GLint*) &name);
		if (name != 0)
			glDeleteRenderbuffers(1, &name);
	}

	glDeleteFramebuffers(1, &fbo);

	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
	glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);

180
 	piglit_check_gl_error(0, PIGLIT_FAIL);
181
182
}

Chad Versace's avatar
Chad Versace committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* ------------------------------------------------------------------------ */

/**
 * \name hiz_run_depth_test utilties
 *
 * Utilities for testing depth testing.
 *
 * \{
 */

/**
 * Common functionality needed by hiz_run_test_depth_test_fbo() and
 * hiz_run_test_depth_test_window().
 */
static bool
hiz_run_test_depth_test_common()
{
Chad Versace's avatar
Chad Versace committed
200
	static const float *expect_color[9] = {
201
202
203
		hiz_grey,  hiz_blue,  hiz_blue,
		hiz_green, hiz_green, hiz_blue,
		hiz_green, hiz_green, hiz_grey,
Chad Versace's avatar
Chad Versace committed
204
205
	};

Chad Versace's avatar
Chad Versace committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	const float width_3 = piglit_width / 3.0;
	const float height_3 = piglit_height / 3.0;

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);
	glClearDepth(hiz_clear_z);
	glClearColor(hiz_grey[0], hiz_grey[1], hiz_grey[2], hiz_grey[3]);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glViewport(0, 0, piglit_width, piglit_height);
	piglit_ortho_projection(piglit_width, piglit_height, false);

	glColor4fv(hiz_green);
	glDepthRange(hiz_green_z, hiz_green_z);
	piglit_draw_rect(0 * width_3, 0 * width_3,   /* x, y */
		         2 * width_3, 2 * height_3); /* width, height */

	glColor4fv(hiz_blue);
	glDepthRange(hiz_blue_z, hiz_blue_z);
	piglit_draw_rect(1 * width_3, 1 * height_3,   /* x, y */
		         2 * width_3, 2 * height_3); /* width, height */

	glClearDepth(1.0);
	glDepthRange(0, 1);

231
	return hiz_probe_color_buffer(expect_color);
Chad Versace's avatar
Chad Versace committed
232
233
}

234
bool
235
hiz_run_test_depth_test_fbo(const struct hiz_fbo_options *fbo_options)
236
237
238
239
{
	bool pass = true;
	GLuint fbo = 0;

240
	piglit_require_extension("GL_ARB_framebuffer_object");
241
242
243
244
245
246
247

	/* Create and bind FBO. */
	fbo = hiz_make_fbo(fbo_options);
	assert(fbo != 0);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);

Chad Versace's avatar
Chad Versace committed
248
	pass = hiz_run_test_depth_test_common();
249
250
251
252
253
254
255
256
257
258
259
260
261

	if (!piglit_automatic) {
		/* Blit the FBO to the window FB so we can see the results. */
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
		glBlitFramebuffer(0, 0, piglit_width, piglit_height,
			          0, 0, piglit_width, piglit_height,
			          GL_COLOR_BUFFER_BIT, GL_NEAREST);
		glutSwapBuffers();
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	}

	hiz_delete_fbo(fbo);

262
	return pass;
263
}
264
265

bool
Chad Versace's avatar
Chad Versace committed
266
267
hiz_run_test_depth_test_window()
{
Chad Versace's avatar
Chad Versace committed
268
	bool pass = hiz_run_test_depth_test_common();
269
270
271
272
	if (!piglit_automatic)
		glutSwapBuffers();
	return pass;
}
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

/** \} */

/* ------------------------------------------------------------------------ */

/**
 * \name hiz_run_depth_read utilties
 *
 * Utilities for testing depth reads.
 *
 * \{
 */

/**
 * Common functionality needed by hiz_run_test_depth_read_fbo() and
 * hiz_run_test_depth_read_window().
 */
static bool
hiz_run_test_depth_read_common()
{
	static const float *expect_color[9] = {
294
295
296
		hiz_grey,  hiz_blue,  hiz_blue,
		hiz_green, hiz_green, hiz_blue,
		hiz_green, hiz_green, hiz_grey,
297
298
299
	};

	static const float expect_depth[9] = {
300
301
302
		hiz_clear_z, hiz_blue_z,  hiz_blue_z,
		hiz_green_z, hiz_green_z, hiz_blue_z,
		hiz_green_z, hiz_green_z, hiz_clear_z,
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
	};


	const float width_3 = piglit_width / 3.0;
	const float height_3 = piglit_height / 3.0;

	glDisable(GL_STENCIL_TEST);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);
	glClearDepth(hiz_clear_z);
	glClearColor(hiz_grey[0], hiz_grey[1], hiz_grey[2], hiz_grey[3]);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glViewport(0, 0, piglit_width, piglit_height);
	piglit_ortho_projection(piglit_width, piglit_height, false);

	glColor4fv(hiz_green);
	glDepthRange(hiz_green_z, hiz_green_z);
	piglit_draw_rect(0 * width_3, 0 * width_3,   /* x, y */
		         2 * width_3, 2 * height_3); /* width, height */

	glColor4fv(hiz_blue);
	glDepthRange(hiz_blue_z, hiz_blue_z);
	piglit_draw_rect(1 * width_3, 1 * height_3,   /* x, y */
		         2 * width_3, 2 * height_3); /* width, height */

	glClearDepth(1.0);
	glDepthRange(0, 1);

	if (!hiz_probe_color_buffer(expect_color))
		return false;
	return hiz_probe_depth_buffer(expect_depth);
}

bool
hiz_run_test_depth_read_fbo(const struct hiz_fbo_options *fbo_options)
{
	bool pass = true;
	GLuint fbo = 0;

	piglit_require_extension("GL_ARB_framebuffer_object");

	/* Create and bind FBO. */
	fbo = hiz_make_fbo(fbo_options);
	assert(fbo != 0);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);

	pass = hiz_run_test_depth_read_common();

	if (!piglit_automatic) {
		/* Blit the FBO to the window FB so we can see the results. */
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
		glBlitFramebuffer(0, 0, piglit_width, piglit_height,
			          0, 0, piglit_width, piglit_height,
			          GL_COLOR_BUFFER_BIT, GL_NEAREST);
		glutSwapBuffers();
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	}

	hiz_delete_fbo(fbo);

	return pass;
}

bool
hiz_run_test_depth_read_window()
{
	bool pass = hiz_run_test_depth_read_common();
	if (!piglit_automatic)
		glutSwapBuffers();
	return pass;
}
Chad Versace's avatar
Chad Versace committed
377
378

/** \} */
Chad Versace's avatar
Chad Versace committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397

/* ------------------------------------------------------------------------ */

/**
 * \name hiz_run_stencil_test utilties
 *
 * Utilities for testing stencil testing.
 *
 * \{
 */

/**
 * Common functionality needed by hiz_run_test_stencil_test_fbo() and
 * hiz_run_test_stencil_test_window().
 */
static bool
hiz_run_test_stencil_test_common()
{
	static const float *expected_colors[9] = {
398
399
400
		hiz_grey,  hiz_blue,  hiz_grey,
		hiz_green, hiz_blue,  hiz_grey,
		hiz_green, hiz_green, hiz_grey,
Chad Versace's avatar
Chad Versace committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
	};

	const float dx = piglit_width / 3.0;
	const float dy = piglit_height / 3.0;

	/* Set up depth state. */
	glDisable(GL_DEPTH_TEST);
	glClearDepth(hiz_clear_z);

	/* Set up stencil state. */
	glEnable(GL_STENCIL_TEST);
	glClearStencil(3); /* 3 is a good canary. */
	glStencilFunc(GL_LESS, 3, ~0);
	glStencilOp(GL_INCR, GL_INCR, GL_INCR);

	glClearColor(hiz_grey[0], hiz_grey[1], hiz_grey[2], hiz_grey[3]);
	glClear(GL_COLOR_BUFFER_BIT
		| GL_DEPTH_BUFFER_BIT
		| GL_STENCIL_BUFFER_BIT);

	glViewport(0, 0, piglit_width, piglit_height);
	piglit_ortho_projection(piglit_width, piglit_height, false);

	/* Draw rect 1. */
	glColor4fv(hiz_grey);
	piglit_draw_rect(0 * dx, 0 * dy, /* x, y */
			 2 * dx, 3 * dy); /* w, h */

	/* Draw rect 2. */
	glColor4fv(hiz_green);
	piglit_draw_rect(0 * dx, 0 * dy,  /* x, y */
			 2 * dx, 2 * dy); /* w, h */

	/* Draw rect 3. */
	glColor4fv(hiz_blue);
	piglit_draw_rect(1 * dx, 1 * dy,   /* x, y */
		         2 * dx, 2 * dy);  /* w, h */

439
	piglit_check_gl_error(0, PIGLIT_FAIL);
Chad Versace's avatar
Chad Versace committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484

	return hiz_probe_color_buffer(expected_colors);
}

bool
hiz_run_test_stencil_test_fbo(const struct hiz_fbo_options *fbo_options)
{
	bool pass = true;
	GLuint fbo = 0;

	piglit_require_extension("GL_ARB_framebuffer_object");

	/* Create and bind FBO. */
	fbo = hiz_make_fbo(fbo_options);
	assert(fbo != 0);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);

	pass = hiz_run_test_stencil_test_common();

	if (!piglit_automatic) {
		/* Blit the FBO to the window FB so we can see the results. */
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
		glBlitFramebuffer(0, 0, piglit_width, piglit_height,
			          0, 0, piglit_width, piglit_height,
			          GL_COLOR_BUFFER_BIT, GL_NEAREST);
		glutSwapBuffers();
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	}

	hiz_delete_fbo(fbo);

	return pass;
}

bool
hiz_run_test_stencil_test_window()
{
	bool pass = hiz_run_test_stencil_test_common();
	if (!piglit_automatic)
		glutSwapBuffers();
	return pass;
}

/** \} */
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503

/* ------------------------------------------------------------------------ */

/**
 * \name hiz_run_stencil_read utilties
 *
 * Utilities for testing the reading of the stencil buffer.
 *
 * \{
 */

/**
 * Common functionality needed by hiz_run_test_stencil_read_fbo() and
 * hiz_run_test_stencil_read_window().
 */
static bool
hiz_run_test_stencil_read_common()
{
	static const float *expected_colors[9] = {
504
505
506
		hiz_grey,  hiz_blue,  hiz_grey,
		hiz_green, hiz_blue,  hiz_grey,
		hiz_green, hiz_green, hiz_grey,
507
508
509
	};

	static const unsigned expected_stencil[9] = {
510
511
512
		4, 5, 4,
		5, 6, 4,
		5, 5, 3,
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
	};

	const float dx = piglit_width / 3.0;
	const float dy = piglit_height / 3.0;

	/* Set up depth state. */
	glDisable(GL_DEPTH_TEST);
	glClearDepth(hiz_clear_z);

	/* Set up stencil state. */
	glEnable(GL_STENCIL_TEST);
	glClearStencil(3); /* 3 is a good canary. */
	glStencilFunc(GL_LESS, 3, ~0);
	glStencilOp(GL_INCR, GL_INCR, GL_INCR);

	glClearColor(hiz_grey[0], hiz_grey[1], hiz_grey[2], hiz_grey[3]);
	glClear(GL_COLOR_BUFFER_BIT
		| GL_DEPTH_BUFFER_BIT
		| GL_STENCIL_BUFFER_BIT);

	glViewport(0, 0, piglit_width, piglit_height);
	piglit_ortho_projection(piglit_width, piglit_height, false);

	/* Draw rect 1. */
	glColor4fv(hiz_grey);
	piglit_draw_rect(0 * dx, 0 * dy, /* x, y */
			 2 * dx, 3 * dy); /* w, h */

	/* Draw rect 2. */
	glColor4fv(hiz_green);
	piglit_draw_rect(0 * dx, 0 * dy,  /* x, y */
			 2 * dx, 2 * dy); /* w, h */

	/* Draw rect 3. */
	glColor4fv(hiz_blue);
	piglit_draw_rect(1 * dx, 1 * dy,   /* x, y */
		         2 * dx, 2 * dy);  /* w, h */

551
	piglit_check_gl_error(0, PIGLIT_FAIL);
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598

	if (!hiz_probe_color_buffer(expected_colors))
		return false;
	return hiz_probe_stencil_buffer(expected_stencil);
}

bool
hiz_run_test_stencil_read_fbo(const struct hiz_fbo_options *fbo_options)
{
	bool pass = true;
	GLuint fbo = 0;

	piglit_require_extension("GL_ARB_framebuffer_object");

	/* Create and bind FBO. */
	fbo = hiz_make_fbo(fbo_options);
	assert(fbo != 0);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);

	pass = hiz_run_test_stencil_read_common();

	if (!piglit_automatic) {
		/* Blit the FBO to the window FB so we can see the results. */
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
		glBlitFramebuffer(0, 0, piglit_width, piglit_height,
			          0, 0, piglit_width, piglit_height,
			          GL_COLOR_BUFFER_BIT, GL_NEAREST);
		glutSwapBuffers();
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	}

	hiz_delete_fbo(fbo);

	return pass;
}

bool
hiz_run_test_stencil_read_window()
{
	bool pass = hiz_run_test_stencil_read_common();
	if (!piglit_automatic)
		glutSwapBuffers();
	return pass;
}

/** \} */
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613

bool
hiz_run_test_depth_stencil_test_fbo(const struct hiz_fbo_options *fbo_options)
{
	bool pass = true;

	GLuint fbo = 0;
	bool has_depth_buffer = fbo_options->depth_format
				|| fbo_options->depth_stencil_format;
	bool has_stencil_buffer = fbo_options->stencil_format
				  || fbo_options->depth_stencil_format;

	const float dx = piglit_width / 3.0;
	const float dy = piglit_height / 3.0;

614
	static const float **expected_colors = NULL;
615
616

	static const float *expected_colors_d1s0[9] = {
617
618
619
		hiz_grey,  hiz_blue,  hiz_blue,
		hiz_green, hiz_green, hiz_blue,
		hiz_green, hiz_green, hiz_grey,
620
621
622
	};

	static const float *expected_colors_d0s1[9] = {
623
624
625
		hiz_grey,  hiz_blue,  hiz_grey,
		hiz_green, hiz_blue,  hiz_grey,
		hiz_green, hiz_green, hiz_grey,
626
627
628
	};

	static const float *expected_colors_d1s1[9] = {
629
630
631
		hiz_grey,  hiz_blue,  hiz_grey,
		hiz_green, hiz_green, hiz_grey,
		hiz_green, hiz_green, hiz_grey,
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
	};

	if (has_depth_buffer && !has_stencil_buffer)
	   expected_colors = expected_colors_d1s0;
	else if (!has_depth_buffer && has_stencil_buffer)
	   expected_colors = expected_colors_d0s1;
	else if (has_depth_buffer && has_stencil_buffer)
	   expected_colors = expected_colors_d1s1;

	piglit_require_extension("GL_ARB_framebuffer_object");

	/* Create and bind FBO. */
	fbo = hiz_make_fbo(fbo_options);
	assert(fbo != 0);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);

	/* Set up depth state. */
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);
	glClearDepth(hiz_clear_z);

654
655
656
657
	/* Set up stencil state. The test for 3 < stencil with the
	 * buffer cleared to 3 means the first primitive drawn will be
	 * stenciled out.
	 */
658
659
660
661
662
663
664
665
666
667
668
669
670
	glEnable(GL_STENCIL_TEST);
	glClearStencil(3); /* 3 is a good canary. */
	glStencilFunc(GL_LESS, 3, ~0);
	glStencilOp(GL_INCR, GL_INCR, GL_INCR);

	glClearColor(hiz_grey[0], hiz_grey[1], hiz_grey[2], hiz_grey[3]);
	glClear(GL_COLOR_BUFFER_BIT
		| GL_DEPTH_BUFFER_BIT
		| GL_STENCIL_BUFFER_BIT);

	glViewport(0, 0, piglit_width, piglit_height);
	piglit_ortho_projection(piglit_width, piglit_height, false);

671
672
673
	/* Draw a rect 1 on left 2/3 of the screen with clear color,
	 * letting the next drawing there pass stencil.
	 */
674
675
676
677
678
	glColor4fv(hiz_grey);
	glDepthRange(hiz_clear_z, hiz_clear_z);
	piglit_draw_rect(0 * dx, 0 * dy, /* x, y */
			 2 * dx, 3 * dy); /* w, h */

679
	/* Draw rect 2. This should pass with or without stencil. */
680
681
682
683
684
	glColor4fv(hiz_green);
	glDepthRange(hiz_green_z, hiz_green_z);
	piglit_draw_rect(0 * dx, 0 * dy,  /* x, y */
			 2 * dx, 2 * dy); /* w, h */

685
686
687
688
	/* Draw rect 3. This should draw only the left half if stencil
	 * is present (due to rect 1 covering only that much), and
	 * should draw over rect 2 only if depth is not present.
	 */
689
690
691
692
693
	glColor4fv(hiz_blue);
	glDepthRange(hiz_blue_z, hiz_blue_z);
	piglit_draw_rect(1 * dx, 1 * dy,   /* x, y */
		         2 * dx, 2 * dy);  /* w, h */

694
 	piglit_check_gl_error(0, PIGLIT_FAIL);
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711

	pass = hiz_probe_color_buffer(expected_colors);

	if (!piglit_automatic) {
		/* Blit the FBO to the window FB so we can see the results. */
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
		glBlitFramebuffer(0, 0, piglit_width, piglit_height,
			          0, 0, piglit_width, piglit_height,
			          GL_COLOR_BUFFER_BIT, GL_NEAREST);
		glutSwapBuffers();
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
	}

	hiz_delete_fbo(fbo);

	return pass;
}