File: texsubimage-depth-formats.c

package info (click to toggle)
piglit 0~git20220119-124bca3c9-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 109,012 kB
  • sloc: ansic: 273,511; xml: 46,666; python: 33,098; lisp: 20,392; cpp: 12,480; sh: 22; makefile: 22; pascal: 5
file content (236 lines) | stat: -rw-r--r-- 7,899 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright © 2014 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 texsubimage-depth-formats.c
 *
 * Test glTexSubimage2D() with different depth formats and X, Y offsets.
 **/

#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
	config.khr_no_error_support = PIGLIT_NO_ERRORS;

PIGLIT_GL_TEST_CONFIG_END

static bool use_pbo = false;

static GLuint tex[4];
struct size {
	int width, height;
};

/* For ease of testing, use even dimensions. */
static const struct size tex_size[] = {
	{   6, 	12 },
	{   8, 	14 },
	{  12, 	22 },
	{  16,	32 },
	{ 130, 	64 }
};

struct format_info {
	GLenum internal_format, format, type;
	int size;
	const char *extension;
};
static const struct format_info formats[] = {
	{ GL_DEPTH_COMPONENT16,  GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT,                 sizeof(short), NULL },
	{ GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT,                          sizeof(float), "GL_ARB_depth_buffer_float" },
	{ GL_DEPTH24_STENCIL8,   GL_DEPTH_STENCIL,   GL_UNSIGNED_INT_24_8,              sizeof(int), NULL },
	{ GL_DEPTH32F_STENCIL8,  GL_DEPTH_STENCIL,   GL_FLOAT_32_UNSIGNED_INT_24_8_REV, sizeof(int) + sizeof(float), "GL_ARB_depth_buffer_float" }
};

static void
load_texture(int formats_idx, int tex_size_idx)
{
	unsigned i;
	unsigned w_by_2 = tex_size[tex_size_idx].width / 2;
	unsigned h_by_2 =  tex_size[tex_size_idx].height / 2;
	unsigned n_pixels = w_by_2 * h_by_2;
	unsigned buffer_size = n_pixels * formats[formats_idx].size;
	void* texDepthData[4];
	GLuint buffer;

	for (i = 0; i < 4; i++)
		texDepthData[i] = malloc(buffer_size);

	for (i = 0; i < n_pixels; i++) {
		switch (formats[formats_idx].type) {
		case GL_UNSIGNED_SHORT:
			((unsigned short *)texDepthData[0])[i] = 0x4000;
			((unsigned short *)texDepthData[1])[i] = 0x7F00;
			((unsigned short *)texDepthData[2])[i] = 0xC000;
			((unsigned short *)texDepthData[3])[i] = 0xFF00;
			break;
		case GL_UNSIGNED_INT_24_8:
			((unsigned *)texDepthData[0])[i] = 0x400000BB;
			((unsigned *)texDepthData[1])[i] = 0x7F0000BB;
			((unsigned *)texDepthData[2])[i] = 0xC00000BB;
			((unsigned *)texDepthData[3])[i] = 0xFF0000BB;
			break;
		case GL_FLOAT:
			((float *)texDepthData[0])[i] = 0.25;
			((float *)texDepthData[1])[i] = 0.50;
			((float *)texDepthData[2])[i] = 0.75;
			((float *)texDepthData[3])[i] = 1.00;
			break;
		case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
			((float *)texDepthData[0])[2 * i] = 0.25;
			((float *)texDepthData[1])[2 * i] = 0.50;
			((float *)texDepthData[2])[2 * i] = 0.75;
			((float *)texDepthData[3])[2 * i] = 1.00;

			((unsigned *)texDepthData[0])[2 * i + 1] = 0xBB;
			((unsigned *)texDepthData[1])[2 * i + 1] = 0xBB;
			((unsigned *)texDepthData[2])[2 * i + 1] = 0xBB;
			((unsigned *)texDepthData[3])[2 * i + 1] = 0xBB;
			break;
		}
	}

	glBindTexture(GL_TEXTURE_2D, tex[formats_idx]);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D, 0,
		     formats[formats_idx].internal_format,
		     tex_size[tex_size_idx].width,
		     tex_size[tex_size_idx].height, 0,
		     formats[formats_idx].format,
		     formats[formats_idx].type, NULL);

	/* Use glTexSubImage2D() to initialize each texture quadrant with
	 * different depth data.
	 */
	if (formats[formats_idx].type == GL_UNSIGNED_SHORT)
		glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
	else
		glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

	if (use_pbo) {
		glGenBuffers(1, &buffer);
		glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
	}

	if (use_pbo)
		glBufferData(GL_PIXEL_UNPACK_BUFFER, buffer_size, texDepthData[0], GL_STATIC_DRAW);
	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w_by_2, h_by_2,
			formats[formats_idx].format, formats[formats_idx].type,
			use_pbo ? NULL : texDepthData[0]);

	if (use_pbo)
		glBufferData(GL_PIXEL_UNPACK_BUFFER, buffer_size, texDepthData[1], GL_STATIC_DRAW);
	glTexSubImage2D(GL_TEXTURE_2D, 0, w_by_2, 0, w_by_2, h_by_2,
			formats[formats_idx].format, formats[formats_idx].type,
			use_pbo ? NULL : texDepthData[1]);

	if (use_pbo)
		glBufferData(GL_PIXEL_UNPACK_BUFFER, buffer_size, texDepthData[2], GL_STATIC_DRAW);
	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, h_by_2, w_by_2, h_by_2,
			formats[formats_idx].format, formats[formats_idx].type,
			use_pbo ? NULL : texDepthData[2]);

	if (use_pbo)
		glBufferData(GL_PIXEL_UNPACK_BUFFER, buffer_size, texDepthData[3], GL_STATIC_DRAW);
	glTexSubImage2D(GL_TEXTURE_2D, 0, w_by_2, h_by_2, w_by_2, h_by_2,
			formats[formats_idx].format, formats[formats_idx].type,
			use_pbo ? NULL : texDepthData[3]);

	if (use_pbo)
		glDeleteBuffers(1, &buffer);

	for (i = 0; i < 4; i++)
		free(texDepthData[i]);
}

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

	for (int i = 1; i < argc; ++i) {
		if (!strcmp(argv[i], "pbo")) {
			piglit_require_extension("GL_ARB_pixel_buffer_object");
			use_pbo = true;
		}
	}

	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
	glClearColor(0.0, 0.0, 0.0, 1.0);
}

enum piglit_result
piglit_display(void)
{
	int i, j;
	bool result, pass = true;
	const int w = piglit_width / 2, h = piglit_height / 2;
	const float expected[4][4] = {{ 0.25, 0.25, 0.25, 1.00 },
				      { 0.50, 0.50, 0.50, 1.00 },
				      { 0.75, 0.75, 0.75, 1.00 },
				      { 1.00, 1.00, 1.00, 1.00 }};
	glClear(GL_COLOR_BUFFER_BIT);
	glEnable(GL_TEXTURE_2D);
	glGenTextures(ARRAY_SIZE(formats), tex);

	for (i = 0; i < ARRAY_SIZE(formats); i++) {
		if (formats[i].extension &&
		    !piglit_is_extension_supported(formats[i].extension)) {
			continue;
		}

		for (j = 0; j < ARRAY_SIZE(tex_size); j++) {
			result = true;
			load_texture(i, j);

			glBindTexture(GL_TEXTURE_2D, tex[i]);
			piglit_draw_rect_tex(0, 0, piglit_width, piglit_height,
					     0, 0, 1, 1);
			result = result && piglit_check_gl_error(GL_NO_ERROR);

			/* Probe four quadrants of the rectangle */
			result = piglit_probe_rect_rgba(0, 0, w, h, expected[0])
				 && result;
			result = piglit_probe_rect_rgba(w, 0, w, h, expected[1])
				 && result;
			result = piglit_probe_rect_rgba(0, h, w, h, expected[2])
				 && result;
			result = piglit_probe_rect_rgba(w, h, w, h, expected[3])
				 && result;
			pass = pass && result;

			printf("internal_format = %s, width = %d, height = %d,"
			       " result = %s\n",
			       piglit_get_gl_enum_name(formats[i].internal_format),
			       tex_size[j].width, tex_size[j].height,
			       result ? "pass" : "fail");
		}
	}
	piglit_present_results();
	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}