aboutsummaryrefslogtreecommitdiff
path: root/tests/texturing/getteximage-luminance.c
blob: b7f0841428f5da5ee6138a8e5dcd11becfdb6991 (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
/*
 * Copyright (c) 2012 VMware, Inc.
 *
 * 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
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, 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
 * NON-INFRINGEMENT.  IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
 * 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.
 */

/*
 * Test glGetTexImage for luminance formats.
 * Brian Paul
 * 8 Mar 2012
 */

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

PIGLIT_GL_TEST_MAIN(
    100 /*window_width*/,
    100 /*window_height*/,
    GLUT_RGBA | GLUT_DOUBLE)

static const char *TestName = "getteximage-luminance";
static float tolerance = 2.0 / 255.0;


static bool
rgba_equal(const float *c1, const float *c2)
{
	return ((fabs(c1[0] - c2[0]) < tolerance) &&
		(fabs(c1[1] - c2[1]) < tolerance) &&
		(fabs(c1[2] - c2[2]) < tolerance) &&
		(fabs(c1[3] - c2[3]) < tolerance));
}


static bool
lum_equal(const float *l1, const float *l2)
{
	return fabs(*l1 - *l2) < tolerance;
}


/*
 * Test reading back a luminance texture as luminance and RGBA.
 */
static bool
test_luminance(void)
{
	static const GLfloat lumImage[2*2] = { 0.25, 0.25, 0.25, 0.25 };
	static const GLfloat rgbaImage[4] = { 0.25, 0.0, 0.0, 1.0 };
	GLuint tex;
	GLfloat test[2*2*4];

	/* create 2x2 GL_LUMINANCE texture */
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 2, 0,
		     GL_LUMINANCE, GL_FLOAT, lumImage);

	/* Get and check luminance image */
	glGetTexImage(GL_TEXTURE_2D, 0, GL_LUMINANCE, GL_FLOAT, test);
	if (!lum_equal(lumImage, test)) {
		printf("%s: glGetTexImage(GL_LUMINANCE as"
		       " GL_LUMINANCE) failed\n", TestName);
		printf("  Expected %g  Found %g\n", lumImage[0], test[0]);
		return false;
	}

	/* Get and check rgba image */
	glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, &test);
	if (!rgba_equal(rgbaImage, test)) {
		printf("%s: glGetTexImage(GL_LUMINANCE as GL_RGBA) failed\n",
		       TestName);
		printf("  Expected %g, %g, %g, %g  Found %g, %g, %g, %g\n",
		       rgbaImage[0], rgbaImage[1], rgbaImage[2], rgbaImage[3],
		       test[0], test[1], test[2], test[3]);
		return false;
	}

	return true;
}


/*
 * Test reading back an RGBA texture as luminance.
 */
static bool
test_rgba(void)
{
	static const GLfloat rgbaImage[4] = { 0.5, 0.25, 0.125, 1.0 };
	static const GLfloat lumImage[1] = { 0.5 };
	GLuint tex;
	GLfloat test[2*2*4];

	/* create 1x1 GL_RGBA texture */
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
		     GL_RGBA, GL_FLOAT, rgbaImage);

	/* Get and check luminance image */
	glGetTexImage(GL_TEXTURE_2D, 0, GL_LUMINANCE, GL_FLOAT, test);
	if (!lum_equal(lumImage, test)) {
		printf("%s: glGetTexImage(GL_RGBA as GL_LUMINANCE) failed\n",
		       TestName);
		printf("  Expected %g  Found %g\n", lumImage[0], test[0]);
		return false;
	}

	return true;
}


/*
 * Test reading back a luminance texture via FBO + glReadPixels as RGBA.
 */
static bool
test_fbo_readpixels_lum_as_rgba(void)
{
	static const GLfloat lumImage[2*2] = { 0.25, 0.25, 0.25, 0.25 };
	static const GLfloat rgbaImage[4] = { 0.25, 0.0, 0.0, 1.0 };
	GLuint tex, fbo;
	GLfloat test[2*2*4];
	GLenum status;

	if (!piglit_is_extension_supported("GL_ARB_framebuffer_object"))
		return true;

	/* create 2x2 GL_LUMINANCE texture */
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 2, 0,
		     GL_LUMINANCE, GL_FLOAT, lumImage);

	/* create an FBO to wrap the texture so we can read it back
	 * with glReadPixels
	 */
	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT,
			       GL_TEXTURE_2D, tex, 0);

	status = glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
	if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
		/* can't test glReadPixels from a luminance fbo/texture */
		if (!piglit_automatic) {
			printf("Skipping FBO ReadPixels test\n");
		}
		return true;
	}

	/* get rgba image (only red should have the lum value) */
	glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, test);
	if (!rgba_equal(rgbaImage, test)) {
		printf("%s: glReadPixels(GL_LUMINANCE as GL_RGBA) failed\n",
		       TestName);
		printf("  Expected %g, %g, %g, %g  Found %g, %g, %g, %g\n",
		       rgbaImage[0], rgbaImage[1], rgbaImage[2], rgbaImage[3],
		       test[0], test[1], test[2], test[3]);
		return false;
	}

	return true;
}


/*
 * Test reading back an RGBA texture via FBO + glReadPixels as luminance.
 */
static bool
test_fbo_readpixels_rgba_as_lum(void)
{
	static const GLfloat rgbaImage[4] = { 0.5, 0.25, 0.125, 1.0 };
	static const GLfloat lumImage[1] = { 0.5 + 0.25 + 0.125 };
	GLuint tex, fbo;
	GLfloat test[1];
	GLenum status;

	if (!piglit_is_extension_supported("GL_ARB_framebuffer_object"))
		return true;

	/* create 1x1 GL_RGBA texture */
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
		     GL_RGBA, GL_FLOAT, rgbaImage);

	/* create an FBO to wrap the texture so we can read it back
	 * with glReadPixels
	 */
	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT,
			       GL_TEXTURE_2D, tex, 0);

	status = glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
	if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
		/* something failed with FBO setup, ignore it */
		if (!piglit_automatic) {
			printf("Skipping FBO ReadPixels test\n");
		}
		return true;
	}

	/* get luminance image, should be sum of RGB values */
	glReadPixels(0, 0, 1, 1, GL_LUMINANCE, GL_FLOAT, test);
	if (!lum_equal(lumImage, test)) {
		printf("%s: glReadPixels(GL_RGBA as GL_LUMINANCE) failed\n",
		       TestName);
		printf("  Expected %g  Found %g\n", lumImage[0], test[0]);
		return false;
	}

	return true;
}


enum piglit_result
piglit_display(void)
{
	bool pass = true;

	pass = test_luminance() && pass;
	pass = test_rgba() && pass;
	pass = test_fbo_readpixels_lum_as_rgba() && pass;
	pass = test_fbo_readpixels_rgba_as_lum() && pass;

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}


void
piglit_init(int argc, char **argv)
{
	glewInit();
}