File: getteximage-simple.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 (114 lines) | stat: -rw-r--r-- 3,152 bytes parent folder | download | duplicates (3)
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
/**
 * @file getteximage-simple.c
 *
 * Extremely basic test to check whether image data can be retrieved.
 *
 * Note that the texture is used in a full frame of rendering before
 * the readback, to ensure that buffer manager related code for uploading
 * texture images is executed before the readback.
 *
 * This used to crash for R300+bufmgr.
 *
 * This also used to stress test the blit methods in i965. The BLT engine only
 * supports pitch sizes up to but not including 32768 dwords. BLORP supports
 * even larger sizes.
 */

#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

#define MAX_TYPE_VAL 1.0
#define PIX_TYPE GLfloat
#define TEX_TYPE GL_FLOAT
#define TEX_INT_FMT GL_RGBA32F
#define TEX_FMT GL_RGBA
#define CHANNELS_PER_PIXEL 4

static bool test_getteximage(PIX_TYPE *data, size_t data_size, GLint w, GLint h)
{
	PIX_TYPE *compare = (PIX_TYPE *)malloc(data_size);

	glGetTexImage(GL_TEXTURE_2D, 0, TEX_FMT, TEX_TYPE, compare);

	bool match = true;
	const unsigned data_channels = w * h / CHANNELS_PER_PIXEL;
	for (unsigned i = 0; i < data_channels; ++i) {
		if (data[i] != compare[i]) {
			const unsigned pixel = i / CHANNELS_PER_PIXEL;
			const unsigned pixel_channel = i % CHANNELS_PER_PIXEL;
			printf("GetTexImage() returns incorrect data in element %i\n", i);
			printf("    corresponding to (%i,%i) channel %i\n", pixel % w, pixel / w, pixel_channel);
			printf("    expected: %f\n", data[i]);
			printf("    got: %f\n", compare[i]);
			match = false;
			break;
		}
	}

	free(compare);
	return match;
}


enum piglit_result
piglit_display(void)
{
	GLsizei height = 2;
	GLsizei width;
	glGetIntegerv(GL_MAX_TEXTURE_SIZE, &width);

	/* Upload random data to a texture with the given dimensions */
	const unsigned data_channels = width * height * CHANNELS_PER_PIXEL;
	const size_t data_size = data_channels * sizeof(PIX_TYPE);
	PIX_TYPE *data = (PIX_TYPE *)malloc(data_size);
	for (unsigned i = 0; i < data_channels; ++i)
		data[i] = ((float)rand() / RAND_MAX) * MAX_TYPE_VAL;
	glTexImage2D(GL_TEXTURE_2D, 0, TEX_INT_FMT, width, height, 0, TEX_FMT,
		     TEX_TYPE, data);

	glClearColor(0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glEnable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
	glTexCoord2f(0, 0);
	glVertex2f(0, 0);
	glTexCoord2f(1, 0);
	glVertex2f(1, 0);
	glTexCoord2f(1, 1);
	glVertex2f(1, 1);
	glTexCoord2f(0, 1);
	glVertex2f(0, 1);
	glEnd();

	piglit_present_results();

	bool pass = test_getteximage(data, data_size, width, height);

	free(data);

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}

void piglit_init(int argc, char **argv)
{
	if (TEX_TYPE == GL_FLOAT)
		piglit_require_extension("GL_ARB_texture_float");

	GLuint tex;

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	piglit_gen_ortho_projection(0.0, 1.0, 0.0, 1.0, -2.0, 6.0, GL_FALSE);
}