ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_direct_state_access / gettextureimage-luminance.c
blobc9fbb2366e704e9ca3ccc854f6e9115873dced7d
1 /*
2 * Copyright (c) 2012 VMware, Inc.
3 * Copyright (c) 2014 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NON-INFRINGEMENT. IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
27 * Test glGetTexImage for luminance formats.
30 #include "piglit-util-gl.h"
32 PIGLIT_GL_TEST_CONFIG_BEGIN
34 config.supports_gl_compat_version = 10;
36 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
37 config.khr_no_error_support = PIGLIT_NO_ERRORS;
39 PIGLIT_GL_TEST_CONFIG_END
41 static const char *TestName = "gettextureimage-luminance";
42 static float tolerance = 3.0 / 255.0;
45 static bool
46 rgba_equal(const float *c1, const float *c2)
48 return ((fabs(c1[0] - c2[0]) < tolerance) &&
49 (fabs(c1[1] - c2[1]) < tolerance) &&
50 (fabs(c1[2] - c2[2]) < tolerance) &&
51 (fabs(c1[3] - c2[3]) < tolerance));
55 static bool
56 lum_equal(const float *l1, const float *l2)
58 return fabs(*l1 - *l2) < tolerance;
63 * Test reading back a luminance texture as luminance and RGBA.
65 static bool
66 test_luminance(void)
68 static const GLfloat lumImage[2*2] = { 0.25, 0.25, 0.25, 0.25 };
69 static const GLfloat rgbaImage[4] = { 0.25, 0.0, 0.0, 1.0 };
70 GLuint tex;
71 GLfloat test[2*2*4];
73 /* create 2x2 GL_LUMINANCE texture */
74 glGenTextures(1, &tex);
75 glBindTexture(GL_TEXTURE_2D, tex);
76 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 2, 0,
77 GL_LUMINANCE, GL_FLOAT, lumImage);
79 /* Get and check luminance image */
80 glGetTextureImage(tex, 0, GL_LUMINANCE, GL_FLOAT, sizeof(test), test);
81 if (!lum_equal(lumImage, test)) {
82 printf("%s: glGetTextureImage(GL_LUMINANCE as"
83 " GL_LUMINANCE) failed\n", TestName);
84 printf(" Expected %g Found %g\n", lumImage[0], test[0]);
85 return false;
88 /* Get and check rgba image */
89 glGetTextureImage(tex, 0, GL_RGBA, GL_FLOAT, sizeof(test), &test);
90 if (!rgba_equal(rgbaImage, test)) {
91 printf("%s: glGetTextureImage(GL_LUMINANCE as"
92 " GL_RGBA) failed\n", TestName);
93 printf(" Expected %g, %g, %g, %g Found %g, %g, %g, %g\n",
94 rgbaImage[0], rgbaImage[1], rgbaImage[2], rgbaImage[3],
95 test[0], test[1], test[2], test[3]);
96 return false;
99 return true;
104 * Test reading back an RGBA texture as luminance.
106 static bool
107 test_rgba(void)
109 static const GLfloat rgbaImage[4] = { 0.5, 0.25, 0.125, 1.0 };
110 static const GLfloat lumImage[1] = { 0.5 };
111 GLuint tex;
112 GLfloat test[2*2*4];
114 /* create 1x1 GL_RGBA texture */
115 glGenTextures(1, &tex);
116 glBindTexture(GL_TEXTURE_2D, tex);
117 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
118 GL_RGBA, GL_FLOAT, rgbaImage);
120 /* Get and check luminance image */
121 glGetTextureImage(tex, 0, GL_LUMINANCE, GL_FLOAT, sizeof(test), test);
122 if (!lum_equal(lumImage, test)) {
123 printf("%s: glGetTextureImage(GL_RGBA as"
124 " GL_LUMINANCE) failed\n", TestName);
125 printf(" Expected %g Found %g\n", lumImage[0], test[0]);
126 return false;
129 return true;
134 * Test reading back a luminance texture via FBO + glReadPixels as RGBA.
136 static bool
137 test_fbo_readpixels_lum_as_rgba(void)
139 static const GLfloat lumImage[2*2] = { 0.25, 0.25, 0.25, 0.25 };
140 static const GLfloat rgbaImage[4] = { 0.25, 0.0, 0.0, 1.0 };
141 GLuint tex, fbo;
142 GLfloat test[2*2*4];
143 GLenum status;
145 if (!piglit_is_extension_supported("GL_ARB_framebuffer_object"))
146 return true;
148 /* create 2x2 GL_LUMINANCE texture */
149 glGenTextures(1, &tex);
150 glBindTexture(GL_TEXTURE_2D, tex);
151 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 2, 0,
152 GL_LUMINANCE, GL_FLOAT, lumImage);
154 /* create an FBO to wrap the texture so we can read it back
155 * with glReadPixels
157 glGenFramebuffers(1, &fbo);
158 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
159 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT,
160 GL_TEXTURE_2D, tex, 0);
162 status = glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
163 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
164 /* can't test glReadPixels from a luminance fbo/texture */
165 if (!piglit_automatic) {
166 printf("Skipping FBO ReadPixels test\n");
168 return true;
171 /* get rgba image (only red should have the lum value) */
172 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, test);
173 if (!rgba_equal(rgbaImage, test)) {
174 printf("%s: glReadPixels(GL_LUMINANCE as GL_RGBA) failed\n",
175 TestName);
176 printf(" Expected %g, %g, %g, %g Found %g, %g, %g, %g\n",
177 rgbaImage[0], rgbaImage[1], rgbaImage[2], rgbaImage[3],
178 test[0], test[1], test[2], test[3]);
179 return false;
182 return true;
187 * Test reading back an RGBA texture via FBO + glReadPixels as luminance.
189 static bool
190 test_fbo_readpixels_rgba_as_lum(void)
192 static const GLfloat rgbaImage[4] = { 0.5, 0.25, 0.125, 1.0 };
193 static const GLfloat lumImage[1] = { 0.5 + 0.25 + 0.125 };
194 GLuint tex, fbo;
195 GLfloat test[1];
196 GLenum status;
198 if (!piglit_is_extension_supported("GL_ARB_framebuffer_object"))
199 return true;
201 /* create 1x1 GL_RGBA texture */
202 glGenTextures(1, &tex);
203 glBindTexture(GL_TEXTURE_2D, tex);
204 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
205 GL_RGBA, GL_FLOAT, rgbaImage);
207 /* create an FBO to wrap the texture so we can read it back
208 * with glReadPixels
210 glGenFramebuffers(1, &fbo);
211 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
212 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT,
213 GL_TEXTURE_2D, tex, 0);
215 status = glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
216 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
217 /* something failed with FBO setup, ignore it */
218 if (!piglit_automatic) {
219 printf("Skipping FBO ReadPixels test\n");
221 return true;
224 /* get luminance image, should be sum of RGB values */
225 glReadPixels(0, 0, 1, 1, GL_LUMINANCE, GL_FLOAT, test);
226 if (!lum_equal(lumImage, test)) {
227 printf("%s: glReadPixels(GL_RGBA as GL_LUMINANCE) failed\n",
228 TestName);
229 printf(" Expected %g Found %g\n", lumImage[0], test[0]);
230 return false;
233 return true;
237 enum piglit_result
238 piglit_display(void)
240 bool pass = true;
242 pass = test_luminance() && pass;
243 pass = test_rgba() && pass;
244 pass = test_fbo_readpixels_lum_as_rgba() && pass;
245 pass = test_fbo_readpixels_rgba_as_lum() && pass;
247 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
251 void
252 piglit_init(int argc, char **argv)
254 piglit_require_extension("GL_ARB_direct_state_access");