2 * Copyright © 2009 Intel Corporation
3 * Copyright © 2011 Red Hat Inc.
4 * Copyright © 2014 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * Marek Olšák <maraeo@gmail.com>
31 #include "piglit-util-gl.h"
36 PIGLIT_GL_TEST_CONFIG_BEGIN
38 config
.supports_gl_compat_version
= 20;
40 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGB
;
41 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
43 PIGLIT_GL_TEST_CONFIG_END
45 static const char *fs_1d
=
46 "uniform sampler1D tex; \n"
49 " gl_FragColor = texture1D(tex, gl_TexCoord[0].x); \n"
54 static float colors
[4][4] = {
66 float p
[TEX_SIZE
* 4];
69 for (x
= 0; x
< TEX_SIZE
; x
++) {
70 int c
= x
/ (TEX_SIZE
/4);
71 memcpy(&p
[x
*4], colors
[c
], sizeof(float) * 4);
74 glTexSubImage1D(GL_TEXTURE_1D
, 0, 0, TEX_SIZE
, GL_RGBA
, GL_FLOAT
, p
);
78 create_texture_1d(void)
84 glGenTextures(1, &tex
);
85 glBindTexture(GL_TEXTURE_1D
, tex
);
86 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST_MIPMAP_NEAREST
);
87 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
89 for (i
= 0, dim
= TEX_SIZE
; dim
>0; i
++, dim
/= 2) {
90 glTexImage1D(GL_TEXTURE_1D
, i
, format
, dim
, 0,
91 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
94 glGenFramebuffers(1, &fb
);
95 glBindFramebuffer(GL_FRAMEBUFFER
, fb
);
97 glFramebufferTexture1D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
98 GL_TEXTURE_1D
, tex
, 0);
100 status
= glCheckFramebufferStatus(GL_FRAMEBUFFER
);
101 if (status
!= GL_FRAMEBUFFER_COMPLETE
) {
106 glViewport(0, 0, TEX_SIZE
, 1);
107 piglit_ortho_projection(TEX_SIZE
, 1, GL_FALSE
);
109 glColor4fv(colors
[0]);
110 piglit_draw_rect(0, 0, TEX_SIZE
/ 4, 1);
111 glColor4fv(colors
[1]);
112 piglit_draw_rect(TEX_SIZE
/ 4, 0, TEX_SIZE
/ 4, 1);
113 glColor4fv(colors
[2]);
114 piglit_draw_rect(TEX_SIZE
/ 2, 0, TEX_SIZE
/ 4, 1);
115 glColor4fv(colors
[3]);
116 piglit_draw_rect(TEX_SIZE
/4 * 3, 0, TEX_SIZE
/ 4, 1);
119 glDeleteFramebuffers(1, &fb
);
120 glGenerateMipmap(GL_TEXTURE_1D
);
125 draw_level(int x
, int y
, int level
)
129 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_LOD
, level
);
130 glTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAX_LOD
, level
);
133 loc
= glGetUniformLocation(prog
, "tex");
134 glUniform1i(loc
, 0); /* texture unit p */
136 glViewport(0, 0, piglit_width
, piglit_height
);
137 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
139 glBindFramebuffer(GL_FRAMEBUFFER
, piglit_winsys_fbo
);
141 piglit_draw_rect_tex(x
, y
, TEX_SIZE
, 5, 0, 0, 1, 1);
146 test_level(int level
)
148 int size
= TEX_SIZE
>> level
;
150 float e
[TEX_SIZE
* 4], observed
[TEX_SIZE
* 4];
152 glGetTexImage(GL_TEXTURE_1D
, level
, GL_RGBA
, GL_FLOAT
, observed
);
155 memset(e
, 0, 4 * sizeof(float));
157 /* Average the colors for the last level. */
158 for (i
= 0; i
< 4; i
++)
159 for (c
= 0; c
< 4; c
++)
160 e
[c
] += colors
[i
][c
] * 0.25;
162 else if (size
== 2) {
163 memset(e
, 0, 8 * sizeof(float));
165 for (i
= 0; i
< 2; i
++)
166 for (c
= 0; c
< 4; c
++)
167 e
[c
] += colors
[i
][c
] * 0.5;
168 for (i
= 2; i
< 4; i
++)
169 for (c
= 0; c
< 4; c
++)
170 e
[4+c
] += colors
[i
][c
] * 0.5;
173 for (x
= 0; x
< size
; x
++) {
174 int col
= x
/ (size
/4);
175 memcpy(&e
[x
*4], colors
[col
], sizeof(float) * 4);
179 for (x
= 0; x
< size
; x
++) {
180 float *probe
= &observed
[x
*4];
181 float *expected
= &e
[x
*4];
183 for (i
= 0; i
< 4; ++i
) {
184 if (fabs(probe
[i
] - expected
[i
]) >= piglit_tolerance
[i
]) {
185 printf("Probe color at (%i)\n", x
);
186 printf(" Expected: %f %f %f %f\n",
187 expected
[0], expected
[1], expected
[2], expected
[3]);
188 printf(" Observed: %f %f %f %f\n",
189 probe
[0], probe
[1], probe
[2], probe
[3]);
190 printf(" when testing level %i\n", level
);
201 GLboolean pass
= GL_TRUE
;
205 glClearColor(0.1, 0.1, 0.1, 0.1);
206 glClear(GL_COLOR_BUFFER_BIT
);
208 tex1d
= create_texture_1d();
210 for (level
= 0; level
< TEX_LEVELS
; level
++) {
211 draw_level(5, 5 + level
* 10, level
);
212 pass
= pass
&& test_level(level
);
215 glDeleteTextures(1, &tex1d
);
216 piglit_present_results();
217 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
220 void piglit_init(int argc
, char **argv
)
224 piglit_require_extension("GL_EXT_framebuffer_object");
228 for (i
= 1; i
< argc
; i
++) {
229 if (strcmp(argv
[i
], "RGB9_E5") == 0) {
230 /* Test a non-renderable format. */
231 piglit_require_extension("GL_EXT_texture_shared_exponent");
239 prog
= piglit_build_simple_program(NULL
, fs_1d
);