summaryrefslogtreecommitdiff
path: root/tests/hiz/hiz-util.c
blob: 253df913937f9cac87dc480ca8bcb74408f56641 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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
439
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
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
551
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
 * 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-gl.h"
#include "hiz/hiz-util.h"

#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; \
		int i = 3 * (2 - iy) + ix;		     \
		pass &= probe_func(x, y, dx, dy, expect[i]); \
	} \
	\
	return pass;


bool
hiz_probe_color_buffer(const float *expected_colors[])
{
	hiz_probe_common(piglit_probe_rect_rgb, expected_colors);
}

bool
hiz_probe_depth_buffer(const float expected_depths[])
{
	hiz_probe_common(piglit_probe_rect_depth, expected_depths);
}

bool
hiz_probe_stencil_buffer(const unsigned expected_stencil[])
{
	hiz_probe_common(piglit_probe_rect_stencil, expected_stencil);
}


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);
         	if (!piglit_check_gl_error(0))
			piglit_report_result(PIGLIT_FAIL);
	}

	/* 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);
         	if (!piglit_check_gl_error(0))
			piglit_report_result(PIGLIT_FAIL);
	}

	/* 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);
         	if (!piglit_check_gl_error(0))
			piglit_report_result(PIGLIT_FAIL);
	}

	/* 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);
         	if (!piglit_check_gl_error(0))
			piglit_report_result(PIGLIT_FAIL);
	}

	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, piglit_winsys_fbo);
	glBindFramebuffer(GL_READ_FRAMEBUFFER, piglit_winsys_fbo);

 	if (!piglit_check_gl_error(0))
		piglit_report_result(PIGLIT_FAIL);
}

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

/**
 * \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()
{
	static const float *expect_color[9] = {
		hiz_grey,  hiz_blue,  hiz_blue,
		hiz_green, hiz_green, hiz_blue,
		hiz_green, hiz_green, hiz_grey,
	};

	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);

	return hiz_probe_color_buffer(expect_color);
}

bool
hiz_run_test_depth_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_depth_test_common();

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

	hiz_delete_fbo(fbo);

	return pass;
}

bool
hiz_run_test_depth_test_window()
{
	bool pass = hiz_run_test_depth_test_common();
	if (!piglit_automatic)
		piglit_present_results();
	return pass;
}

/** \} */

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

/**
 * \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] = {
		hiz_grey,  hiz_blue,  hiz_blue,
		hiz_green, hiz_green, hiz_blue,
		hiz_green, hiz_green, hiz_grey,
	};

	static const float expect_depth[9] = {
		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,
	};


	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, piglit_winsys_fbo);
		glBlitFramebuffer(0, 0, piglit_width, piglit_height,
			          0, 0, piglit_width, piglit_height,
			          GL_COLOR_BUFFER_BIT, GL_NEAREST);
		piglit_present_results();
		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)
		piglit_present_results();
	return pass;
}

/** \} */

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

/**
 * \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] = {
		hiz_grey,  hiz_blue,  hiz_grey,
		hiz_green, hiz_blue,  hiz_grey,
		hiz_green, hiz_green, hiz_grey,
	};

	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 */

	if (!piglit_check_gl_error(0))
		return false;

	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, piglit_winsys_fbo);
		glBlitFramebuffer(0, 0, piglit_width, piglit_height,
			          0, 0, piglit_width, piglit_height,
			          GL_COLOR_BUFFER_BIT, GL_NEAREST);
		piglit_present_results();
		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)
		piglit_present_results();
	return pass;
}

/** \} */

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

/**
 * \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] = {
		hiz_grey,  hiz_blue,  hiz_grey,
		hiz_green, hiz_blue,  hiz_grey,
		hiz_green, hiz_green, hiz_grey,
	};

	static const unsigned expected_stencil[9] = {
		4, 5, 4,
		5, 6, 4,
		5, 5, 3,
	};

	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 */

	if (!piglit_check_gl_error(0))
		return false;

	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, piglit_winsys_fbo);
		glBlitFramebuffer(0, 0, piglit_width, piglit_height,
			          0, 0, piglit_width, piglit_height,
			          GL_COLOR_BUFFER_BIT, GL_NEAREST);
		piglit_present_results();
		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)
		piglit_present_results();
	return pass;
}

/** \} */

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;

	static const float **expected_colors = NULL;

	static const float *expected_colors_d1s0[9] = {
		hiz_grey,  hiz_blue,  hiz_blue,
		hiz_green, hiz_green, hiz_blue,
		hiz_green, hiz_green, hiz_grey,
	};

	static const float *expected_colors_d0s1[9] = {
		hiz_grey,  hiz_blue,  hiz_grey,
		hiz_green, hiz_blue,  hiz_grey,
		hiz_green, hiz_green, hiz_grey,
	};

	static const float *expected_colors_d1s1[9] = {
		hiz_grey,  hiz_blue,  hiz_grey,
		hiz_green, hiz_green, hiz_grey,
		hiz_green, hiz_green, hiz_grey,
	};

	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);

	/* Set up stencil state. The test for 3 < stencil with the
	 * buffer cleared to 3 means the first primitive drawn will be
	 * stenciled out.
	 */
	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 a rect 1 on left 2/3 of the screen with clear color,
	 * letting the next drawing there pass stencil.
	 */
	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 */

	/* Draw rect 2. This should pass with or without stencil. */
	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 */

	/* 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.
	 */
	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 */

 	pass = piglit_check_gl_error(0);

	pass = hiz_probe_color_buffer(expected_colors) && pass;

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

	hiz_delete_fbo(fbo);

	return pass;
}