summaryrefslogtreecommitdiff
path: root/tests/fbo/fbo-maxsize.c
blob: ae4f6564c48069b690b5adb5451b73c41be06a28 (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
/*
 * Copyright © 2010 Marek Olšák <maraeo@gmail.com>
 *
 * 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 fbo-maxsize.c
 *
 * Tests that rendering to a texture of maximum size works.
 */

#include "piglit-util-gl-common.h"

PIGLIT_GL_TEST_MAIN(
    256 /*window_width*/,
    256 /*window_height*/,
    GLUT_DOUBLE | GLUT_RGB)

static int
find_max_texture_size(void)
{
	GLint maxsize;

	glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxsize);

	while (maxsize > 1) {
		GLint w, h;
		glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA,
			     maxsize, maxsize, 0,
			     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
		glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
					 GL_TEXTURE_WIDTH, &w);
		glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
					 GL_TEXTURE_WIDTH, &h);
		if (w == maxsize && h == maxsize) {
			return maxsize;
		}
		else {
			maxsize /= 2;
		}
	}
	return maxsize;
}


static void
draw_color_sub_rect(int x, int y, int texsize)
{
	int k = texsize / 64;
	int x0 = x - k;
	int x1 = x + k;
	int y1 = y - k;
	int y2 = y + k;

	float r0 = (float) x0 / texsize;
	float r1 = (float) x1 / texsize;

	float g0 = (float) y1 / texsize;
	float g1 = (float) y2 / texsize;

	float b0 = (float) y1 / texsize;
	float b1 = (float) y2 / texsize;

	glBegin(GL_POLYGON);
	glColor3f(r0, g0, b0);  glVertex2i(x0, y1);
	glColor3f(r1, g0, b0);  glVertex2i(x1, y1);
	glColor3f(r1, g1, b1);  glVertex2i(x1, y2);
	glColor3f(r0, g1, b1);  glVertex2i(x0, y2);
	glEnd();
}


static int create_fbo(void)
{
	GLuint tex, fb;
	GLint maxsize;
	GLenum status;
	int x0, x1, y0, y1;
	GLenum glerror;

	maxsize = find_max_texture_size();
	printf("max 2D texture size: %d x %d\n", maxsize, maxsize);

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, maxsize, maxsize,
		     0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

	glerror = glGetError();

	switch (glerror) {
	case GL_NO_ERROR:
		break;
	case GL_OUT_OF_MEMORY:
		puts("Got GL_OUT_OF_MEMORY.");
		piglit_report_result(PIGLIT_PASS);
	default:
		printf("Unexpected error: 0x%x\n", glerror);
		piglit_report_result(PIGLIT_FAIL);
	}

	glGenFramebuffersEXT(1, &fb);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
				  GL_COLOR_ATTACHMENT0_EXT,
				  GL_TEXTURE_2D,
				  tex, 0);
	assert(glGetError() == 0);

	status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
	if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
		printf("FBO incomplete\n");
		piglit_report_result(PIGLIT_SKIP);
	}

	glViewport(0, 0, maxsize, maxsize);
	piglit_ortho_projection(maxsize, maxsize, GL_FALSE);

	x0 = maxsize / 4;
	x1 = maxsize * 3 / 4;
	y0 = maxsize / 4;
	y1 = maxsize * 3 / 4;
	draw_color_sub_rect(x0, y0, maxsize);
	draw_color_sub_rect(x1, y0, maxsize);
	draw_color_sub_rect(x1, y1, maxsize);
	draw_color_sub_rect(x0, y1, maxsize);

	glDeleteFramebuffersEXT(1, &fb);

	return tex;
}


/** draw textured rect centered on the given pixel.
 */
static void
draw_tex_sub_rect(int x, int y)
{
	int x0 = x - 16;
	int x1 = x + 16;
	int y1 = y - 16;
	int y2 = y + 16;

	float s0 = (float) x0 / piglit_width;
	float s1 = (float) x1 / piglit_width;
	float t0 = (float) y1 / piglit_height;
	float t1 = (float) y2 / piglit_height;

	glBegin(GL_POLYGON);
	glTexCoord2f(s0, t0);  glVertex2i(x0, y1);
	glTexCoord2f(s1, t0);  glVertex2i(x1, y1);
	glTexCoord2f(s1, t1);  glVertex2i(x1, y2);
	glTexCoord2f(s0, t1);  glVertex2i(x0, y2);
	glEnd();
}


enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;
	GLuint tex;
	int x1 = piglit_width / 4;
	int x2 = (piglit_width / 4) * 3;
	int y1 = piglit_height / 4;
	int y2 = (piglit_height / 4) * 3;
	float c1[3] = {0.25, 0.25, 0.25};
	float c2[3] = {0.75, 0.25, 0.25};
	float c3[3] = {0.25, 0.75, 0.75};
	float c4[3] = {0.75, 0.75, 0.75};

	glClearColor(0.5, 0.5, 0.5, 0.5);
	glClear(GL_COLOR_BUFFER_BIT);

	tex = create_fbo();
	glViewport(0, 0, piglit_width, piglit_height);
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

	glEnable(GL_TEXTURE_2D);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	draw_tex_sub_rect(x1, y1);
	draw_tex_sub_rect(x2, y1);
	draw_tex_sub_rect(x1, y2);
	draw_tex_sub_rect(x2, y2);

	pass &= piglit_probe_pixel_rgb(x1, y1, c1);
	pass &= piglit_probe_pixel_rgb(x2, y1, c2);
	pass &= piglit_probe_pixel_rgb(x1, y2, c3);
	pass &= piglit_probe_pixel_rgb(x2, y2, c4);

	glDeleteTextures(1, &tex);
	glDisable(GL_TEXTURE_2D);

	glutSwapBuffers();

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}

void piglit_init(int argc, char **argv)
{
	piglit_require_extension("GL_EXT_framebuffer_object");
}