README: recommend Ninja by default and switch to cmake --build
[piglit.git] / tests / fbo / fbo-generatemipmap-1d.c
blob8aab39003d506f0cf9c3b959385d5d60ae8d44e8
1 /*
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
15 * Software.
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
23 * IN THE SOFTWARE.
25 * Authors:
26 * Dave Airlie
27 * Marek Olšák <maraeo@gmail.com>
31 #include "piglit-util-gl.h"
33 #define TEX_SIZE 128
34 #define TEX_LEVELS 8
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"
47 "void main() \n"
48 "{ \n"
49 " gl_FragColor = texture1D(tex, gl_TexCoord[0].x); \n"
50 "} \n";
52 static GLuint prog;
54 static float colors[4][4] = {
55 {1.0, 0.0, 0.0, 1.0},
56 {0.0, 1.0, 0.0, 1.0},
57 {0.0, 0.0, 1.0, 1.0},
58 {1.0, 0.0, 1.0, 1.0},
61 static GLenum format;
63 static void
64 load_texture_1d(void)
66 float p[TEX_SIZE * 4];
67 int x;
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);
77 static GLuint
78 create_texture_1d(void)
80 GLuint tex, fb;
81 GLenum status;
82 int i, dim;
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) {
102 load_texture_1d();
103 goto done;
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);
118 done:
119 glDeleteFramebuffers(1, &fb);
120 glGenerateMipmap(GL_TEXTURE_1D);
121 return tex;
124 static void
125 draw_level(int x, int y, int level)
127 int loc;
129 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_LOD, level);
130 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LOD, level);
132 glUseProgram(prog);
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);
142 glUseProgram(0);
145 static bool
146 test_level(int level)
148 int size = TEX_SIZE >> level;
149 int x,i,c;
150 float e[TEX_SIZE * 4], observed[TEX_SIZE * 4];
152 glGetTexImage(GL_TEXTURE_1D, level, GL_RGBA, GL_FLOAT, observed);
154 if (size == 1) {
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;
172 else {
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);
191 return false;
195 return true;
198 enum piglit_result
199 piglit_display(void)
201 GLboolean pass = GL_TRUE;
202 GLuint tex1d;
203 int level;
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)
222 int i;
224 piglit_require_extension("GL_EXT_framebuffer_object");
226 format = GL_RGBA8;
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");
232 format = GL_RGB9_E5;
234 else {
235 assert(0);
239 prog = piglit_build_simple_program(NULL, fs_1d);