2 * Copyright (c) 2011 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
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
26 * Tests GL_ARB_texture_float floating point formats
29 #include "piglit-util-gl.h"
31 PIGLIT_GL_TEST_CONFIG_BEGIN
33 config
.supports_gl_compat_version
= 10;
35 config
.window_visual
= PIGLIT_GL_VISUAL_RGBA
| PIGLIT_GL_VISUAL_DOUBLE
;
37 PIGLIT_GL_TEST_CONFIG_END
39 static const char *TestName
= "texture-float-formats";
40 static GLint TexWidth
= 16, TexHeight
= 16;
41 static GLint BiasUniform
= -1, ScaleUniform
= -1, TexUniform
= -1;
42 static const float Scale
= 1.0 / 2000.0, Bias
= 0.5;
43 static GLboolean HaveRG
;
48 GLenum IntFormat
, BaseFormat
;
49 GLuint BitsPerChannel
;
53 static const struct format_info Formats
[] = {
54 { GL_RGBA32F_ARB
, GL_RGBA
, 32 },
55 { GL_RGB32F_ARB
, GL_RGB
, 32 },
56 { GL_ALPHA32F_ARB
, GL_ALPHA
, 32 },
57 { GL_INTENSITY32F_ARB
, GL_INTENSITY
, 32 },
58 { GL_LUMINANCE32F_ARB
, GL_LUMINANCE
, 32 },
59 { GL_LUMINANCE_ALPHA32F_ARB
, GL_LUMINANCE
, 32 },
61 { GL_RGBA16F_ARB
, GL_RGBA
, 16 },
62 { GL_RGB16F_ARB
, GL_RGB
, 16 },
63 { GL_ALPHA16F_ARB
, GL_ALPHA
, 16 },
64 { GL_INTENSITY16F_ARB
, GL_INTENSITY
, 16 },
65 { GL_LUMINANCE16F_ARB
, GL_LUMINANCE
, 16 },
66 { GL_LUMINANCE_ALPHA16F_ARB
, GL_LUMINANCE
, 16 },
68 /* These require GL_ARB_texture_rg */
69 { GL_R32F
, GL_RED
, 32 },
70 { GL_RG32F
, GL_RG
, 32 },
71 { GL_R16F
, GL_RED
, 16 },
72 { GL_RG16F
, GL_RG
, 16 },
76 static const char *FragShaderText
=
77 "uniform float bias, scale; \n"
78 "uniform sampler2D tex; \n"
81 " vec4 t = vec4(texture2D(tex, gl_TexCoord[0].xy)); \n"
82 " gl_FragColor = t * scale + bias; \n"
86 static GLuint Program
;
91 num_components(GLenum format
)
104 case GL_LUMINANCE_ALPHA
:
118 fill_array(int comps
, int texels
, void *buf
, const float val
[4])
120 GLfloat
*f
= (GLfloat
*) buf
;
123 for (i
= 0; i
< texels
; i
++) {
124 for (j
= 0; j
< comps
; j
++) {
125 f
[i
* comps
+ j
] = val
[j
];
132 check_error(const char *file
, int line
)
134 GLenum err
= glGetError();
136 printf("%s: error 0x%x at %s:%d\n", TestName
, err
, file
, line
);
143 /** Scale a float in [-1000, 1000] to [0, 1] */
145 scale_and_bias(float val
)
147 return val
* Scale
+ Bias
;
152 * Get a color to use for filling the texture image.
153 * Range of values is [-1000, 1000]
156 get_texture_color(GLfloat value
[4])
158 static const GLfloat colors
[12][4] = {
159 { 690.0, 633.0, -649.0, -975.0 },
160 { 409.0, -678.0, 624.0, -976.0 },
161 { -460.0, -102.0, -983.0, 120.0 },
162 { 202.0, 75.0, 826.0, -339.0 },
163 { -709.0, 620.0, 204.0, -666.0 },
164 { 718.0, -299.0, 290.0, 383.0 },
165 { 634.0, 235.0, 571.0, -651.0 },
166 { -984.0, -99.0, 448.0, 263.0 },
167 { -466.0, 356.0, -155.0, 500.0 },
168 { 678.0, -531.0, 81.0, -783.0 },
169 { -76.0, 98.0, -106.0, -875.0 },
170 { 730.0, -723.0, -656.0, -980.0 }
173 value
[0] = colors
[i
][0];
174 value
[1] = colors
[i
][1];
175 value
[2] = colors
[i
][2];
176 value
[3] = colors
[i
][3];
181 /** \return GL_TRUE for pass, GL_FALSE for fail */
183 test_format(const struct format_info
*info
)
185 const int comps
= num_components(info
->BaseFormat
);
186 const int texels
= TexWidth
* TexHeight
;
187 const int w
= piglit_width
/ 10;
188 const int h
= piglit_height
/ 10;
195 const char *name
= piglit_get_gl_enum_name(info
->IntFormat
);
197 if ((info
->BaseFormat
== GL_RED
||
198 info
->BaseFormat
== GL_RG
) && !HaveRG
) {
203 /*printf("Testing %s\n", name);*/
205 get_texture_color(value
);
207 /* alloc, fill texture image */
208 image
= malloc(comps
* texels
* sizeof(GLfloat
));
209 fill_array(comps
, texels
, image
, value
);
211 /* GL_INTENSITY is not a legal src format */
212 userFormat
= info
->BaseFormat
== GL_INTENSITY
? GL_LUMINANCE
: info
->BaseFormat
;
214 glTexImage2D(GL_TEXTURE_2D
, 0, info
->IntFormat
, TexWidth
, TexHeight
, 0,
215 userFormat
, GL_FLOAT
, image
);
218 if (check_error(__FILE__
, __LINE__
))
221 /* check internal format */
222 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_INTERNAL_FORMAT
, &f
);
223 if (f
!= info
->IntFormat
) {
224 printf("%s: GL_TEXTURE_INTERNAL_FORMAT query failed for 0x%x\n",
230 /* check texture datatype info */
232 static const GLenum queries
[] = {
233 GL_TEXTURE_RED_TYPE_ARB
,
234 GL_TEXTURE_GREEN_TYPE_ARB
,
235 GL_TEXTURE_BLUE_TYPE_ARB
,
236 GL_TEXTURE_ALPHA_TYPE_ARB
,
237 GL_TEXTURE_LUMINANCE_TYPE_ARB
,
238 GL_TEXTURE_INTENSITY_TYPE_ARB
,
239 GL_TEXTURE_DEPTH_TYPE_ARB
242 for (i
= 0; i
< ARRAY_SIZE(queries
); i
++) {
244 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, queries
[i
], &type
);
245 if (check_error(__FILE__
, __LINE__
))
247 if (type
!= GL_NONE
&& type
!= GL_FLOAT
) {
248 printf("%s: %s query failed (returned 0x%x)\n",
249 TestName
, piglit_get_gl_enum_name(queries
[i
]), type
);
255 /* compute expected color */
256 switch (info
->BaseFormat
) {
258 expected
[0] = scale_and_bias(value
[0]);
259 expected
[1] = scale_and_bias(value
[1]);
260 expected
[2] = scale_and_bias(value
[2]);
261 expected
[3] = scale_and_bias(value
[3]);
264 expected
[0] = scale_and_bias(value
[0]);
265 expected
[1] = scale_and_bias(value
[1]);
266 expected
[2] = scale_and_bias(value
[2]);
267 expected
[3] = scale_and_bias(1.0);
272 expected
[2] = scale_and_bias(0.0);
273 expected
[3] = scale_and_bias(value
[0]);
278 expected
[2] = scale_and_bias(value
[0]);
279 expected
[3] = scale_and_bias(1.0);
285 expected
[3] = scale_and_bias(value
[0]);
287 case GL_LUMINANCE_ALPHA
:
290 expected
[2] = scale_and_bias(value
[0]);
291 expected
[3] = scale_and_bias(value
[1]);
294 expected
[0] = scale_and_bias(value
[0]);
295 expected
[1] = scale_and_bias(0.0);
296 expected
[2] = scale_and_bias(0.0);
297 expected
[3] = scale_and_bias(1.0);
300 expected
[0] = scale_and_bias(value
[0]);
301 expected
[1] = scale_and_bias(value
[1]);
302 expected
[2] = scale_and_bias(0.0);
303 expected
[3] = scale_and_bias(1.0);
310 glClearColor(0.5, 0.5, 0.5, 0.0);
311 glClear(GL_COLOR_BUFFER_BIT
);
313 glTexCoord2f(0, 0); glVertex2f(0, 0);
314 glTexCoord2f(1, 0); glVertex2f(w
, 0);
315 glTexCoord2f(1, 1); glVertex2f(w
, h
);
316 glTexCoord2f(0, 1); glVertex2f(0, h
);
320 p
= piglit_probe_pixel_rgba(w
/2, h
/2, expected
);
324 printf(" Failed with format %s:\n", name
);
325 printf(" Texture color = ");
326 for (i
= 0; i
< comps
; i
++) {
327 printf("%f", value
[i
]);
334 piglit_present_results();
344 GLboolean pass
= GL_TRUE
;
346 for (f
= 0; f
< ARRAY_SIZE(Formats
); f
++)
347 if (!test_format(&Formats
[f
]))
350 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
355 piglit_init(int argc
, char **argv
)
359 piglit_require_extension("GL_ARB_texture_float");
360 piglit_require_extension("GL_ARB_fragment_shader");
362 HaveRG
= piglit_is_extension_supported("GL_ARB_texture_rg");
364 Program
= piglit_build_simple_program(NULL
, FragShaderText
);
366 glUseProgram(Program
);
368 BiasUniform
= glGetUniformLocation(Program
, "bias");
369 ScaleUniform
= glGetUniformLocation(Program
, "scale");
370 TexUniform
= glGetUniformLocation(Program
, "tex");
372 glUniform1f(BiasUniform
, Bias
);
373 glUniform1f(ScaleUniform
, Scale
);
374 glUniform1i(TexUniform
, 0); /* tex unit zero */
376 (void) check_error(__FILE__
, __LINE__
);
378 glGenTextures(1, &Texture
);
379 glBindTexture(GL_TEXTURE_2D
, Texture
);
380 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
381 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
383 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);