ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_texture_view / rendering_levels.c
blob5f2208eb45a87fb2cbaf5dc196bbb7882e79c926
1 /*
2 * Copyright © 2013 LunarG, 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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
23 * Author: Jon Ashburn <jon@lunarg.com>
26 /**
27 * Tests GL_ARB_texture_view rendering with various levels.
28 * Creates texture maps with different solid colors for each level,
29 * reads the framebuffer to ensure the rendered color is correct.
32 #include "piglit-util-gl.h"
33 #include "common.h"
35 PIGLIT_GL_TEST_CONFIG_BEGIN
37 config.supports_gl_compat_version = 30;
38 config.supports_gl_es_version = 31;
40 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE;
41 config.khr_no_error_support = PIGLIT_NO_ERRORS;
43 PIGLIT_GL_TEST_CONFIG_END
45 static const char *TestName = "arb_texture_view-rendering-levels";
47 #ifdef PIGLIT_USE_OPENGL
48 #define GLSL_VERSION "130"
49 #else
50 #define GLSL_VERSION "310 es"
51 #endif
53 static const char *vs =
54 "#version " GLSL_VERSION "\n"
55 "in vec4 piglit_vertex;\n"
56 "in vec2 piglit_texcoord;\n"
57 "out vec2 texcoord;\n"
58 "void main() { \n"
59 " gl_Position = piglit_vertex;\n"
60 " texcoord = piglit_texcoord;\n"
61 "}\n";
63 static const char *fs =
64 "#version " GLSL_VERSION "\n"
65 "#ifdef GL_ES\n"
66 "precision highp float;\n"
67 "precision highp sampler2D;\n"
68 "#endif\n"
69 "in vec2 texcoord;\n"
70 "uniform sampler2D tex;\n"
71 "out vec4 color;\n"
72 "void main() { \n"
73 " color = texture(tex, texcoord);\n"
74 "}\n";
77 /**
78 * Texture views with varying minimum and number of levels, 2D only
80 static bool
81 test_render_levels(void)
83 GLuint tex, new_tex;
84 GLint width = 4096, height = 4096, levels =13;
85 GLuint numLevels[] = {3,2,2,1};
86 GLint l;
87 int expectedLevel;
88 GLfloat expected[4];
89 int p;
90 bool pass = true;
92 glGenTextures(1, &tex);
93 glBindTexture(GL_TEXTURE_2D, tex);
95 glTexStorage2D(GL_TEXTURE_2D, levels, GL_RGBA8, width, height);
96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
99 /* load each mipmap with a different color texture */
100 for (l = 0; l < levels; l++) {
101 GLubyte *buf = create_solid_image(width, height, 1, 4, l);
103 if (buf != NULL) {
104 glTexSubImage2D(GL_TEXTURE_2D, l, 0, 0, width, height,
105 GL_RGBA, GL_UNSIGNED_BYTE, buf);
106 free(buf);
109 if (width > 1)
110 width /= 2;
111 if (height > 1)
112 height /= 2;
115 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
117 /* create view of texture with restricted levels and draw quad */
118 /* using smallest mip level in the view range which varies every loop */
119 for (l = 0; l < ARRAY_SIZE(numLevels); l++) {
120 glGenTextures(1, &new_tex);
121 glTextureView(new_tex, GL_TEXTURE_2D, tex, GL_RGBA8, l,
122 numLevels[l], 0, 1);
123 glBindTexture(GL_TEXTURE_2D, new_tex);
124 glActiveTexture(GL_TEXTURE0);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, levels-1);
127 glClear(GL_COLOR_BUFFER_BIT);
129 piglit_draw_rect_tex(-1.0, -1.0, 2.0/(float) (l+2),
130 2.0/ (float) (l+2), 0.0, 0.0, 1.0, 1.0);
132 expectedLevel = l + numLevels[l] - 1;
133 expected[0] = Colors[expectedLevel][0] / 255.0;
134 expected[1] = Colors[expectedLevel][1] / 255.0;
135 expected[2] = Colors[expectedLevel][2] / 255.0;
136 expected[3] = 1.0;
138 p = piglit_probe_pixel_rgba(piglit_width/(2*(l+3)),
139 piglit_height/(2*(l+3)), expected);
141 piglit_present_results();
143 #if 0
144 { /* debug */
145 GLint param;
146 glGetTexParameteriv(GL_TEXTURE_2D,
147 GL_TEXTURE_BASE_LEVEL, &param);
148 printf("for view min level=%d base_level=%d exp color=%f %f %f\n",
149 l, param, expected[0], expected[1], expected[2]);
150 glGetTexParameteriv(GL_TEXTURE_2D,
151 GL_TEXTURE_MAX_LEVEL, &param);
152 printf("max_level=%d\n", param);
153 glGetTexParameteriv(GL_TEXTURE_2D,
154 GL_TEXTURE_VIEW_MIN_LEVEL, &param);
155 printf("view min_level=%d\n", param);
156 glGetTexParameteriv(GL_TEXTURE_2D,
157 GL_TEXTURE_VIEW_NUM_LEVELS, &param);
158 printf("view num_level=%d\n", param);
159 glGetTexParameteriv(GL_TEXTURE_2D,
160 GL_TEXTURE_IMMUTABLE_LEVELS,
161 &param);
162 printf("immutable levels=%d\n", param);
163 sleep(1);
165 #endif
167 if (!p) {
168 printf("%s: wrong color for view min level %d, expectedLevel %d\n",
169 TestName, l, expectedLevel);
170 pass = false;
172 glDeleteTextures(1, &new_tex);
175 glDeleteTextures(1, &tex);
176 return pass;
179 #define X(f, desc) \
180 do { \
181 const bool subtest_pass = (f); \
182 piglit_report_subtest_result(subtest_pass \
183 ? PIGLIT_PASS : PIGLIT_FAIL, \
184 (desc)); \
185 pass = pass && subtest_pass; \
186 } while (0)
188 enum piglit_result
189 piglit_display(void)
191 bool pass = true;
192 X(test_render_levels(), "2D levels rendering");
193 #undef X
194 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
195 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
198 void
199 piglit_init(int argc, char **argv)
201 GLuint prog;
202 #ifdef PIGLIT_USE_OPENGL
203 piglit_require_extension("GL_ARB_texture_storage");
204 piglit_require_extension("GL_ARB_texture_view");
205 #else
206 piglit_require_extension("GL_OES_texture_view");
207 #endif
209 prog = piglit_build_simple_program(vs, fs);
210 glUseProgram(prog);