summaryrefslogtreecommitdiff
path: root/tests/spec/ext_texture_norm16/render.c
blob: 19b9f98160664e946f79016b72081918dbaa6b88 (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
/*
 * Copyright © 2018 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
 * Basic tests for formats added by GL_EXT_texture_norm16 extension
 *
 * https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_texture_norm16.txt
 *
 * Test includes:
 * 	- texture uploads
 * 	- mipmap generation
 * 	- framebuffer creation
 * 	- rendering to
 * 	- reading from
 *	- interaction with GL_EXT_copy_image
 *	- interaction with GL_OES_texture_buffer
 */

#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN
	config.supports_gl_es_version = 31;
	config.window_visual = PIGLIT_GL_VISUAL_RGBA;
PIGLIT_GL_TEST_CONFIG_END

#define PIGLIT_RESULT(x) x ? PIGLIT_PASS : PIGLIT_FAIL

static const char vs_source[] =
	"#version 310 es\n"
	"layout(location = 0) in highp vec4 vertex;\n"
	"layout(location = 1) in highp vec4 uv;\n"
	"out highp vec2 tex_coord;\n"
	"\n"
	"void main()\n"
	"{\n"
	"	gl_Position = vertex;\n"
	"	tex_coord = uv.st;\n"
	"}\n";

static const char fs_source[] =
	"#version 310 es\n"
	"layout(location = 0) uniform sampler2D texture;\n"
	"in highp vec2 tex_coord;\n"
	"out highp vec4 color;\n"
	"void main()\n"
	"{\n"
	"	color = texture2D(texture, tex_coord);\n"
	"}\n";

static const char fs_buf_source[] =
	"#version 310 es\n"
	"#extension GL_OES_texture_buffer : require\n"
	"layout(location = 0) uniform highp samplerBuffer buf;\n"
	"in highp vec2 tex_coord;\n"
	"out highp vec4 color;\n"
	"void main()\n"
	"{\n"
	"	color = texelFetch(buf, 0);\n"
	"}\n";

/* trianglestrip, interleaved vertices + texcoords */
static const GLfloat vertex_data[] = {
	-1.0f,  1.0f,
	0.0f,  1.0f,
	1.0f,  1.0f,
	1.0f,  1.0f,
	-1.0f, -1.0f,
	0.0f,  0.0f,
	1.0f, -1.0f,
	1.0f,  0.0f
};

static struct fmt_test {
	GLenum iformat;
	GLenum base_format;
	unsigned bpp;
	GLenum type;
	bool req_render;
	bool can_read;
	bool can_texbuf;
} tests[] = {
	{ GL_R16_EXT,		GL_RED,		2,	GL_UNSIGNED_SHORT,	true,	true,	true	},
	{ GL_RG16_EXT,		GL_RG,		4,	GL_UNSIGNED_SHORT,	true,	true,	true	},
	{ GL_RGB16_EXT,		GL_RGB,		6,	GL_UNSIGNED_SHORT,	false,	true,	false	},
	{ GL_RGBA16_EXT,	GL_RGBA,	8,	GL_UNSIGNED_SHORT,	true,	true,	true	},
	{ GL_R16_SNORM_EXT,	GL_RED,		2,	GL_SHORT,		false,	false,	false	},
	{ GL_RG16_SNORM_EXT,	GL_RG,		4,	GL_SHORT,		false,	false,	false	},
	{ GL_RGB16_SNORM_EXT,	GL_RGB,		6,	GL_SHORT,		false,	false,	false	},
	{ GL_RGBA16_SNORM_EXT,	GL_RGBA,	8,	GL_SHORT,		false,	false,	false	},
};

static GLuint prog;
static GLuint buf_prog;

static void
upload(const struct fmt_test *test, void *data)
{
	/* glGenerateMipmap only for color renderable formats. */
	if (test->req_render) {
		glTexStorage2D(GL_TEXTURE_2D, 4, test->iformat, piglit_width,
			       piglit_height);
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, piglit_width,
				piglit_height, test->base_format, test->type,
				data);
		glGenerateMipmap(GL_TEXTURE_2D);
		return;
	}
	glTexImage2D(GL_TEXTURE_2D, 0, test->iformat, piglit_width,
		     piglit_height, 0, test->base_format, test->type, data);
}

static unsigned
get_max_value(GLenum type)
{
	return type == GL_SHORT ? SHRT_MAX : USHRT_MAX;
}

static void
value_for_format(const struct fmt_test *test, unsigned short *value)
{
	unsigned short val = get_max_value(test->type);

	/* red */
	value[0] =  val;
	/* yellow */
	if (test->bpp > 2)
		value[1] = val;
	/* pink */
	if (test->bpp > 4) {
		value[2] = val;
		value[1] = 0;
	}
	/* blue */
	if (test->bpp > 6) {
		value[3] = val;
		value[0] = 0;
	}
}

static void
generate_data(const struct fmt_test *test)
{
	unsigned pixels = piglit_width * piglit_height;
	char *data = malloc (pixels * test->bpp);
	unsigned short *p = (unsigned short *) data;

	for (unsigned i = 0; i < pixels; i++, p += test->bpp / 2)
		value_for_format(test, p);

	upload(test, data);
	free(data);
}

static GLuint
create_empty_texture()
{
	GLuint tex;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	return tex;
}

static GLuint
create_texture(const struct fmt_test *test)
{
	GLuint tex = create_empty_texture();
	generate_data(test);
	return tex;
}

static GLuint
create_rbo(const struct fmt_test *test)
{
	GLuint rbo;
	glGenRenderbuffers(1, &rbo);
	glBindRenderbuffer(GL_RENDERBUFFER, rbo);
	glRenderbufferStorage(GL_RENDERBUFFER, test->iformat, piglit_width,
			      piglit_height);
	return rbo;
}

static GLuint
create_fbo(const struct fmt_test *test, GLuint *tex)
{
	GLuint fbo;
	GLuint fbo_tex = create_texture(test);

	*tex = fbo_tex;

	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_2D, fbo_tex, 0);
	return fbo;
}

static void
render_texture(GLuint texture, GLenum target, GLuint fbo_target)
{
	glBindTexture(target, texture);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo_target);

	glViewport(0, 0, piglit_width, piglit_height);

	glClear(GL_COLOR_BUFFER_BIT);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

static bool
verify_contents(const struct fmt_test *test)
{
	bool result = true;
	unsigned amount = piglit_width * piglit_height;
	unsigned short *pix = malloc (amount * 8);
	glReadPixels(0, 0, piglit_width, piglit_height, GL_RGBA, test->type,
		     pix);

	/* Setup expected value, alpha is always max in the test. */
	unsigned short value[4] = { 0 };
	value_for_format(test, value);
	value[3] = get_max_value(test->type);

	unsigned short *p = pix;
	for (unsigned i = 0; i < amount; i++, p += 4) {
		if (memcmp(p, value, sizeof(value)) == 0)
			continue;

		piglit_report_subtest_result(PIGLIT_FAIL,
					     "format %s read fail",
					     piglit_get_gl_enum_name(test->iformat));
		result = false;
		break;
	}

	free(pix);
	return result;
}

static bool
verify_contents_float(const struct fmt_test *test)
{
	/* Setup expected value, alpha is always max in the test. */
	unsigned short value[4] = { 0 };
	unsigned short max = get_max_value(test->type);
	value_for_format(test, value);
	value[3] = max;

	const float expected[4] = {
		value[0] / max,
		value[1] / max,
		value[2] / max,
		value[3] / max,
	};

	bool res = piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height,
					  expected);
	return res;
}

static bool
test_copy_image(const struct fmt_test *test, GLuint src, GLuint *texture)
{
	bool result = true;
	GLuint tex = create_texture(test);
	*texture = tex;
	glCopyImageSubData(src, GL_TEXTURE_2D, 0, 0, 0, 0, tex, GL_TEXTURE_2D,
			   0, 0, 0, 0, piglit_width, piglit_height, 0);

	if (!piglit_check_gl_error(GL_NO_ERROR)) {
		piglit_report_subtest_result(PIGLIT_FAIL,
					     "format %s copyimage fail",
					     piglit_get_gl_enum_name(test->iformat));
		result = false;
	}
	return result;
}

static bool
buffer_test(const struct fmt_test *test)
{
	GLuint tex, tbo;

	/* Setup expected value, alpha is always max in the test. */
	unsigned short tbo_data[4] = { 0 };
	value_for_format(test, tbo_data);
	tbo_data[3] = get_max_value(test->type);

	glGenBuffers(1, &tbo);
	glBindBuffer(GL_TEXTURE_BUFFER, tbo);
	glBufferData(GL_TEXTURE_BUFFER, sizeof(tbo_data), tbo_data,
		     GL_STATIC_DRAW);

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

	glTexBuffer(GL_TEXTURE_BUFFER, test->iformat, tbo);

	if (!piglit_check_gl_error(GL_NO_ERROR))
		return false;

	glUseProgram(buf_prog);
	glUniform1i(0 /* explicit loc */, 0);

	render_texture(tex, GL_TEXTURE_BUFFER, 0);

	if (!verify_contents_float(test))
		return false;

	piglit_present_results();

	glDeleteTextures(1, &tex);

	glBindBuffer(GL_TEXTURE_BUFFER, 0);

	return true;
}


static bool
test_format(const struct fmt_test *test)
{
	bool pass = true;

	if (piglit_is_extension_supported("GL_OES_texture_buffer") &&
	    test->can_texbuf) {
		bool buf_test = buffer_test(test);
		piglit_report_subtest_result(PIGLIT_RESULT(buf_test),
					     "format %s TBO test",
					     piglit_get_gl_enum_name(test->iformat));
		pass &= buf_test;
	}

	glUseProgram(prog);
	glUniform1i(0 /* explicit loc */, 0);

	/* Create a texture, upload data */
	const GLuint texture = create_texture(test);

	glBindTexture(GL_TEXTURE_2D, texture);

	/* Can only texture from. */
	if (!test->req_render) {
		/* Render texture to window and verify contents. */
		render_texture(texture, GL_TEXTURE_2D, 0);
		bool render_test = verify_contents_float(test);
		piglit_present_results();
		piglit_report_subtest_result(PIGLIT_RESULT(render_test),
					     "format %s",
					     piglit_get_gl_enum_name(test->iformat));
		glDeleteTextures(1, &texture);
		pass &= render_test;
		return pass;
	}

	/* Test glRenderbufferStorage. */
	GLuint rbo = create_rbo(test);
	if (!rbo || !piglit_check_gl_error(GL_NO_ERROR)) {
		piglit_report_subtest_result(PIGLIT_FAIL,
					     "format %s RBO test",
					     piglit_get_gl_enum_name(test->iformat));
		pass &= false;
	} else {
		piglit_report_subtest_result(PIGLIT_PASS,
					     "format %s RBO test",
					     piglit_get_gl_enum_name(test->iformat));
	}
	glDeleteRenderbuffers(1, &rbo);

	/* Create framebuffer object. */
	GLuint fbo_tex;
	const GLuint fbo = create_fbo(test, &fbo_tex);

	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) !=
		GL_FRAMEBUFFER_COMPLETE) {
		piglit_report_subtest_result(PIGLIT_FAIL,
					     "format %s fbo fail",
					     piglit_get_gl_enum_name(test->iformat));
		pass &= false;
	}

	render_texture(texture, GL_TEXTURE_2D, fbo);

	/* Test glCopyTexImage2D by copying current fbo content to
	 * a texture, rendering copy back to fbo and verifying fbo contents.
	 */
	GLuint tmp_tex = create_empty_texture();
	glCopyTexImage2D(GL_TEXTURE_2D, 0, test->iformat, 0, 0, piglit_width,
			 piglit_height, 0);

	render_texture(tmp_tex, GL_TEXTURE_2D, fbo);

	/* If format can be read, verify contents. */
	if (test->can_read)
		pass &= verify_contents(test);

	glDeleteTextures(1, &tmp_tex);

	/* If GL_EXT_copy_image is supported then create another
	 * texture, copy contents and render result to fbo.
	 */
	GLuint texture_copy = 0;
	if (piglit_is_extension_supported("GL_EXT_copy_image")) {
		bool copy_pass =
			test_copy_image(test, texture, &texture_copy);
		pass &= copy_pass;
		piglit_report_subtest_result(PIGLIT_RESULT(copy_pass),
					     "copy image format %s",
					     piglit_get_gl_enum_name(test->iformat));
		render_texture(texture_copy, GL_TEXTURE_2D, fbo);
	}

	/* If format can be read, verify contents. */
	if (test->can_read)
		pass &= verify_contents(test);

	/* Render fbo contents to window. */
	render_texture(fbo_tex, GL_TEXTURE_2D, 0);

	piglit_present_results();

	glDeleteFramebuffers(1, &fbo);
	glDeleteTextures(1, &texture);
	glDeleteTextures(1, &texture_copy);

	return pass;
}

enum piglit_result
piglit_display(void)
{
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);

	glActiveTexture(GL_TEXTURE0);

	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
			      vertex_data);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float),
			      (void*) (vertex_data + (2 * sizeof(float))));

	bool pass = true;

	struct fmt_test *test = tests;
	/* Toggle 'req_rend' for EXT_render_snorm compatible formats. */
	if (piglit_is_extension_supported("GL_EXT_render_snorm")) {
		for (unsigned i = 0; i < ARRAY_SIZE(tests); i++, test++) {
			switch (test->iformat) {
			case GL_R16_SNORM_EXT:
			case GL_RG16_SNORM_EXT:
			case GL_RGBA16_SNORM_EXT:
				test->req_render = true;
				test->can_read = true;
			}
		}
	}

	/* Loop over each format. */
	test = tests;
	for (unsigned i = 0; i < ARRAY_SIZE(tests); i++, test++) {
		bool fmt_pass = test_format(test);
		piglit_report_subtest_result(PIGLIT_RESULT(fmt_pass),
					     "format %s",
					     piglit_get_gl_enum_name(test->iformat));
		pass &= fmt_pass;
	}

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

	return PIGLIT_RESULT(pass);
}

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

	prog = piglit_build_simple_program(vs_source, fs_source);

	if (piglit_is_extension_supported("GL_OES_texture_buffer"))
		buf_prog = piglit_build_simple_program(vs_source,
						       fs_buf_source);
}