aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/gl-3.0/api/clearbuffer-common.c
blob: b41a99f6891705fd8657be9435012fb201165fb3 (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
/* 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.
 */

/**
 * \file clearbuffer-common.c
 * Common code and data for the basic glClearBuffer tests
 */
#include "piglit-util-gl-common.h"

const float default_color[4] = { 0.2, 0.4, 0.6, 1.0 };
const float default_depth = 0.2;
const int default_stencil = 0x7a;

PIGLIT_GL_TEST_MAIN(
    100 /*window_width*/,
    100 /*window_height*/,
    PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE)

GLuint
generate_simple_fbo(bool color, bool stencil, bool depth, bool packed)
{
	GLuint fb;
	GLuint rb[3];
	GLenum status;

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

	glGenRenderbuffers(3, rb);

	if (color) {
		glBindRenderbuffer(GL_RENDERBUFFER, rb[0]);
		glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8,
				      piglit_width, piglit_height);

		glFramebufferRenderbuffer(GL_FRAMEBUFFER,
					  GL_COLOR_ATTACHMENT0,
					  GL_RENDERBUFFER,
					  rb[0]);
	} else {
		/* If GL_ARB_ES2_compatibility is not supported, the GL
		 * expects the draw buffer and read buffer be disabled if
		 * there is no color buffer (to read or draw).
		 */
		glDrawBuffer(GL_NONE);
		glReadBuffer(GL_NONE);
	}

	if (stencil) {
		const GLenum format = (packed)
			? GL_DEPTH24_STENCIL8
			: GL_STENCIL_INDEX8;
		const GLenum attachment = (packed)
			? GL_DEPTH_STENCIL_ATTACHMENT
			: GL_STENCIL_ATTACHMENT;

		glBindRenderbuffer(GL_RENDERBUFFER, rb[1]);
		glRenderbufferStorage(GL_RENDERBUFFER, format,
				      piglit_width, piglit_height);

		glFramebufferRenderbuffer(GL_FRAMEBUFFER,
					  attachment,
					  GL_RENDERBUFFER,
					  rb[1]);
	}

	if (!packed && depth) {
		glBindRenderbuffer(GL_RENDERBUFFER, rb[2]);
		glRenderbufferStorage(GL_RENDERBUFFER,
				      GL_DEPTH_COMPONENT24,
				      piglit_width, piglit_height);

		glFramebufferRenderbuffer(GL_FRAMEBUFFER,
					  GL_DEPTH_ATTACHMENT,
					  GL_RENDERBUFFER,
					  rb[2]);
	}

	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	/* All of the possible combinations that we can generate are required
	 * to be supported by all OpenGL 3.0 implementations, with one
	 * exception.  As far as I can tell, implementations are not required
	 * to support separate depth and stencil.  That one option is handled
	 * specially.
	 */
	status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (status != GL_FRAMEBUFFER_COMPLETE
	    && !(status == GL_FRAMEBUFFER_UNSUPPORTED && stencil && !packed)) {
		fprintf(stderr,
			"Framebuffer %s color, %s stencil (%s) was not "
			"complete: 0x%04x\n",
			color ? "with" : "without",
			stencil ? "with" : "without",
			packed ? "packed" : "separate",
			status);
		piglit_report_result(PIGLIT_FAIL);
	}

	if (status == GL_FRAMEBUFFER_UNSUPPORTED) {
		glDeleteRenderbuffers(3, rb);
		glDeleteFramebuffers(1, &fb);
		return 0;
	}

	glClearColor(default_color[0],
		     default_color[1],
		     default_color[2],
		     default_color[3]);
	glClearDepth(default_depth);
	glClearStencil(default_stencil);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
		| GL_STENCIL_BUFFER_BIT);
	glFinish();

	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	return fb;
}

bool
simple_probe(bool color, const float *color_value,
	     bool stencil, int stencil_value,
	     bool depth, float depth_value)
{
	bool pass = true;

	if (color) {
		if (!piglit_probe_rect_rgba(0, 0,
					    piglit_width, piglit_height,
					    color_value)) {
			fprintf(stderr, "Bad color value.\n");
			pass = false;
		}
	}

	if (stencil) {
		if (!piglit_probe_rect_stencil(0, 0,
					       piglit_width, piglit_height,
					       stencil_value)) {
			fprintf(stderr, "Bad stencil value.\n");
			pass = false;
		}
	}

	if (depth) {
		if (!piglit_probe_rect_depth(0, 0,
					     piglit_width, piglit_height,
					     depth_value)) {
			fprintf(stderr, "Bad depth value.\n");
			pass = false;
		}
	}

	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
	return pass;
}

/* Read pixel color values from float, integer or unsigned integer color
 * buffer types
 */
bool
probe_rect_color(int x, int y, int w, int h, GLenum type,
		 const GLvoid *refcolor)
{
	if (type == GL_FLOAT)
		return piglit_probe_rect_rgba(x, y,
					      w, h,
					      (const float*)refcolor);
	else if (type == GL_UNSIGNED_INT)
		return piglit_probe_rect_rgba_uint(x, y,
						   w, h,
						   (const unsigned int*)
						   refcolor);
	else if (type == GL_INT)
		return piglit_probe_rect_rgba_int(x, y,
					          w, h,
					          (const int*)refcolor);
	else
		assert(type == GL_INT ||
		       type == GL_UNSIGNED_INT ||
		       type == GL_FLOAT);
	return true;
}

enum piglit_result
piglit_display(void)
{
	return PIGLIT_FAIL;
}