add pbobench: a benchmark for pbo functions
[piglit.git] / tests / texturing / getteximage-luminance.c
blob79a2fbdf44c5698eb59d8811c78e3b7e26b186fb
1 /*
2 * Copyright (c) 2012 VMware, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NON-INFRINGEMENT. IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
26 * Test glGetTexImage for luminance formats.
27 * Brian Paul
28 * 8 Mar 2012
31 #include "piglit-util-gl.h"
33 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config.supports_gl_compat_version = 10;
37 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
38 config.khr_no_error_support = PIGLIT_NO_ERRORS;
40 PIGLIT_GL_TEST_CONFIG_END
42 static const char *TestName = "getteximage-luminance";
43 static float tolerance = 3.0 / 255.0;
46 static bool
47 rgba_equal(const float *c1, const float *c2)
49 return ((fabs(c1[0] - c2[0]) < tolerance) &&
50 (fabs(c1[1] - c2[1]) < tolerance) &&
51 (fabs(c1[2] - c2[2]) < tolerance) &&
52 (fabs(c1[3] - c2[3]) < tolerance));
56 static bool
57 lum_equal(const float *l1, const float *l2)
59 return fabs(*l1 - *l2) < tolerance;
64 * Test reading back a luminance texture as luminance and RGBA.
66 static bool
67 test_luminance(void)
69 static const GLfloat lumImage[2*2] = { 0.25, 0.25, 0.25, 0.25 };
70 static const GLfloat rgbaImage[4] = { 0.25, 0.0, 0.0, 1.0 };
71 GLuint tex, pbo;
72 GLfloat *test_pbo = NULL;
73 bool pass = true;
74 GLfloat test[2*2*4];
76 /* create 2x2 GL_LUMINANCE texture */
77 glGenTextures(1, &tex);
78 glBindTexture(GL_TEXTURE_2D, tex);
79 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 2, 0,
80 GL_LUMINANCE, GL_FLOAT, lumImage);
82 /* Get and check luminance image */
83 glGetTexImage(GL_TEXTURE_2D, 0, GL_LUMINANCE, GL_FLOAT, test);
84 if (!lum_equal(lumImage, test)) {
85 printf("%s: glGetTexImage(GL_LUMINANCE as"
86 " GL_LUMINANCE) failed\n", TestName);
87 printf(" Expected %g Found %g\n", lumImage[0], test[0]);
88 return false;
91 /* Get and check rgba image */
92 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, &test);
93 if (!rgba_equal(rgbaImage, test)) {
94 printf("%s: glGetTexImage(GL_LUMINANCE as GL_RGBA) failed\n",
95 TestName);
96 printf(" Expected %g, %g, %g, %g Found %g, %g, %g, %g\n",
97 rgbaImage[0], rgbaImage[1], rgbaImage[2], rgbaImage[3],
98 test[0], test[1], test[2], test[3]);
99 pass = false;
102 /* Test reading in to a PBO. */
103 if (!piglit_is_extension_supported("GL_ARB_pixel_buffer_object"))
104 return pass;
106 glGenBuffersARB(1, &pbo);
107 glBindBufferARB(GL_PIXEL_PACK_BUFFER, pbo);
108 glBufferDataARB(GL_PIXEL_PACK_BUFFER, 2*2*4*4, NULL, GL_STREAM_DRAW_ARB);
109 glPixelStorei(GL_PACK_ALIGNMENT, 1);
111 /* Get in to a PBO and check rgba image */
112 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, NULL);
114 test_pbo = glMapBufferARB(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY_ARB);
116 if (test_pbo && !rgba_equal(rgbaImage, test_pbo)) {
117 printf("%s: glGetTexImage(GL_LUMINANCE as GL_RGBA) in pbo failed\n",
118 TestName);
119 printf(" Expected %g, %g, %g, %g Found %g, %g, %g, %g\n",
120 rgbaImage[0], rgbaImage[1], rgbaImage[2], rgbaImage[3],
121 test_pbo[0], test_pbo[1], test_pbo[2], test_pbo[3]);
122 pass = false;
125 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER);
126 glBindBufferARB(GL_PIXEL_PACK_BUFFER, 0);
127 glDeleteBuffersARB(1, &pbo);
128 return pass;
133 * Test reading back an RGBA texture as luminance.
135 static bool
136 test_rgba(void)
138 static const GLfloat rgbaImage[4] = { 0.5, 0.25, 0.125, 1.0 };
139 static const GLfloat lumImage[1] = { 0.5 };
140 GLuint tex, pbo;
141 GLfloat *test_pbo = NULL;
142 bool pass = true;
143 GLfloat test[2*2*4];
145 /* create 1x1 GL_RGBA texture */
146 glGenTextures(1, &tex);
147 glBindTexture(GL_TEXTURE_2D, tex);
148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
149 GL_RGBA, GL_FLOAT, rgbaImage);
151 /* Get and check luminance image */
152 glGetTexImage(GL_TEXTURE_2D, 0, GL_LUMINANCE, GL_FLOAT, test);
153 if (!lum_equal(lumImage, test)) {
154 printf("%s: glGetTexImage(GL_RGBA as GL_LUMINANCE) failed\n",
155 TestName);
156 printf(" Expected %g Found %g\n", lumImage[0], test[0]);
157 pass = false;
160 /* Test reading in to a PBO. */
161 if (!piglit_is_extension_supported("GL_ARB_pixel_buffer_object"))
162 return pass;
164 glGenBuffersARB(1, &pbo);
165 glBindBufferARB(GL_PIXEL_PACK_BUFFER, pbo);
166 glBufferDataARB(GL_PIXEL_PACK_BUFFER, 2*2*4*4, NULL, GL_STREAM_DRAW_ARB);
167 glPixelStorei(GL_PACK_ALIGNMENT, 1);
169 /* Get in to a PBO and check luminance image */
170 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, NULL);
172 test_pbo = glMapBufferARB(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY_ARB);
174 if (test_pbo && !rgba_equal(rgbaImage, test_pbo)) {
175 printf("%s: glGetTexImage(GL_RGBA as GL_LUMINANCE) in pbo failed\n",
176 TestName);
177 printf(" Expected %g Found %g\n", lumImage[0], test_pbo[0]);
178 pass = false;
181 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER);
182 glBindBufferARB(GL_PIXEL_PACK_BUFFER, 0);
183 glDeleteBuffersARB(1, &pbo);
184 return pass;
189 * Test reading back a luminance texture via FBO + glReadPixels as RGBA.
191 static bool
192 test_fbo_readpixels_lum_as_rgba(void)
194 static const GLfloat lumImage[2*2] = { 0.25, 0.25, 0.25, 0.25 };
195 static const GLfloat rgbaImage[4] = { 0.25, 0.0, 0.0, 1.0 };
196 GLuint tex, fbo, pbo;
197 GLfloat *test_pbo = NULL;
198 bool pass = true;
199 GLfloat test[2*2*4];
200 GLenum status;
202 if (!piglit_is_extension_supported("GL_ARB_framebuffer_object"))
203 return true;
205 /* create 2x2 GL_LUMINANCE texture */
206 glGenTextures(1, &tex);
207 glBindTexture(GL_TEXTURE_2D, tex);
208 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 2, 0,
209 GL_LUMINANCE, GL_FLOAT, lumImage);
211 /* create an FBO to wrap the texture so we can read it back
212 * with glReadPixels
214 glGenFramebuffers(1, &fbo);
215 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
216 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT,
217 GL_TEXTURE_2D, tex, 0);
219 status = glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
220 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
221 /* can't test glReadPixels from a luminance fbo/texture */
222 if (!piglit_automatic) {
223 printf("Skipping FBO ReadPixels test\n");
225 return true;
228 /* get rgba image (only red should have the lum value) */
229 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, test);
230 if (!rgba_equal(rgbaImage, test)) {
231 printf("%s: glReadPixels(GL_LUMINANCE as GL_RGBA) failed\n",
232 TestName);
233 printf(" Expected %g, %g, %g, %g Found %g, %g, %g, %g\n",
234 rgbaImage[0], rgbaImage[1], rgbaImage[2], rgbaImage[3],
235 test[0], test[1], test[2], test[3]);
236 pass = false;
239 /* Test reading in to a PBO. */
240 if (!piglit_is_extension_supported("GL_ARB_pixel_buffer_object"))
241 return pass;
243 glGenBuffersARB(1, &pbo);
244 glBindBufferARB(GL_PIXEL_PACK_BUFFER, pbo);
245 glBufferDataARB(GL_PIXEL_PACK_BUFFER, 2*2*4, NULL, GL_STREAM_DRAW_ARB);
246 glPixelStorei(GL_PACK_ALIGNMENT, 1);
248 /* get rgba image in a pbo (only red should have the lum value) */
249 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, NULL);
251 test_pbo = glMapBufferARB(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY_ARB);
253 if (test_pbo && !rgba_equal(rgbaImage, test_pbo)) {
254 printf("%s: glReadPixels(GL_LUMINANCE as GL_RGBA) in pbo failed\n",
255 TestName);
256 printf(" Expected %g, %g, %g, %g Found %g, %g, %g, %g\n",
257 rgbaImage[0], rgbaImage[1], rgbaImage[2], rgbaImage[3],
258 test_pbo[0], test_pbo[1], test_pbo[2], test_pbo[3]);
259 pass = false;
262 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER);
263 glBindBufferARB(GL_PIXEL_PACK_BUFFER, 0);
264 glDeleteBuffersARB(1, &pbo);
265 return pass;
270 * Test reading back an RGBA texture via FBO + glReadPixels as luminance.
272 static bool
273 test_fbo_readpixels_rgba_as_lum(void)
275 static const GLfloat rgbaImage[4] = { 0.5, 0.25, 0.125, 1.0 };
276 static const GLfloat lumImage[1] = { 0.5 + 0.25 + 0.125 };
277 GLuint tex, fbo, pbo;
278 GLfloat *test_pbo = NULL;
279 bool pass = true;
280 GLfloat test[1];
281 GLenum status;
283 if (!piglit_is_extension_supported("GL_ARB_framebuffer_object"))
284 return true;
286 /* create 1x1 GL_RGBA texture */
287 glGenTextures(1, &tex);
288 glBindTexture(GL_TEXTURE_2D, tex);
289 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
290 GL_RGBA, GL_FLOAT, rgbaImage);
292 /* create an FBO to wrap the texture so we can read it back
293 * with glReadPixels
295 glGenFramebuffers(1, &fbo);
296 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
297 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT,
298 GL_TEXTURE_2D, tex, 0);
300 status = glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
301 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
302 /* something failed with FBO setup, ignore it */
303 if (!piglit_automatic) {
304 printf("Skipping FBO ReadPixels test\n");
306 return true;
309 /* get luminance image, should be sum of RGB values */
310 glReadPixels(0, 0, 1, 1, GL_LUMINANCE, GL_FLOAT, test);
312 if (!lum_equal(lumImage, test)) {
313 printf("%s: glReadPixels(GL_RGBA as GL_LUMINANCE) failed\n",
314 TestName);
315 printf(" Expected %g Found %g\n", lumImage[0], test[0]);
316 pass = false;
319 /* Test reading in to a PBO. */
320 if (!piglit_is_extension_supported("GL_ARB_pixel_buffer_object"))
321 return pass;
323 glGenBuffersARB(1, &pbo);
324 glBindBufferARB(GL_PIXEL_PACK_BUFFER, pbo);
325 glBufferDataARB(GL_PIXEL_PACK_BUFFER, 4, NULL, GL_STREAM_DRAW_ARB);
326 glPixelStorei(GL_PACK_ALIGNMENT, 1);
328 /* get luminance image in to pbo, should be sum of RGB values */
329 glReadPixels(0, 0, 1, 1, GL_LUMINANCE, GL_FLOAT, NULL);
331 test_pbo = glMapBufferARB(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY_ARB);
333 if (test_pbo && !lum_equal(lumImage, test_pbo)) {
334 printf("%s: glReadPixels(GL_RGBA as GL_LUMINANCE) in pbo failed\n",
335 TestName);
336 printf(" Expected %g Found %g\n", lumImage[0], test_pbo[0]);
337 pass = false;
340 glUnmapBufferARB(GL_PIXEL_PACK_BUFFER);
341 glBindBufferARB(GL_PIXEL_PACK_BUFFER, 0);
342 glDeleteBuffersARB(1, &pbo);
343 return pass;
347 enum piglit_result
348 piglit_display(void)
350 bool pass = true;
352 pass = test_luminance() && pass;
353 pass = test_rgba() && pass;
354 pass = test_fbo_readpixels_lum_as_rgba() && pass;
355 pass = test_fbo_readpixels_rgba_as_lum() && pass;
357 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
361 void
362 piglit_init(int argc, char **argv)
364 /* empty */