ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_texture_view / rendering_target.c
blob44a6a3f92d79b7f994cd8b5a347dcf48a042d679
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 texture targets.
28 * Creates texture maps with different solid colors for each level or layer
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-target";
46 static int prog3D, prog2Darray, prog2D, prog1D;
48 /**
49 * Simple views of textures; test rendering with various texture view targets
51 static bool
52 test_render_with_targets(GLenum target)
54 GLuint tex, newTex;
55 GLint width = 128, height = 64, depth = 4, levels = 8;
56 GLint l;
57 bool pass = true;
59 glGenTextures(1, &tex);
60 glBindTexture(target, tex);
62 switch (target) {
63 case GL_TEXTURE_1D:
64 glTexStorage1D(target, levels, GL_RGBA8, width);
65 height = 1;
66 depth = 1;
67 break;
68 case GL_TEXTURE_2D:
69 glTexStorage2D(target, levels, GL_RGBA8, width, height);
70 depth = 1;
71 break;
72 case GL_TEXTURE_3D:
73 case GL_TEXTURE_2D_ARRAY:
74 glTexStorage3D(target, levels, GL_RGBA8, width, height, depth);
75 break;
76 default:
77 /* only handle subset of legal targets */
78 piglit_report_result(PIGLIT_FAIL);
79 assert(!"Illegal target for test_render_with_targets()\n");
80 break;
83 /* load each mipmap with a different color texture */
84 for (l = 0; l < levels; l++) {
85 GLubyte *buf = create_solid_image(width, height, depth, 4, l);
87 if (buf != NULL) {
88 switch(target) {
89 case GL_TEXTURE_1D:
90 glTexSubImage1D(GL_TEXTURE_1D, l, 0, width,
91 GL_RGBA, GL_UNSIGNED_BYTE, buf);
92 break;
93 case GL_TEXTURE_2D:
94 glTexSubImage2D(GL_TEXTURE_2D, l, 0, 0, width,
95 height, GL_RGBA,
96 GL_UNSIGNED_BYTE, buf);
97 break;
98 case GL_TEXTURE_3D:
99 case GL_TEXTURE_2D_ARRAY:
100 glTexSubImage3D(target, l, 0, 0, 0, width,
101 height, depth, GL_RGBA,
102 GL_UNSIGNED_BYTE, buf);
103 break;
105 free(buf);
106 } else {
107 piglit_report_result(PIGLIT_FAIL);
108 assert(!"create_solid_image() failed\n");
111 if (width > 1)
112 width /= 2;
113 if (height > 1)
114 height /= 2;
115 if (depth > 1 && target == GL_TEXTURE_3D)
116 depth /= 2;
119 if (piglit_check_gl_error(GL_NO_ERROR) == GL_FALSE) {
120 printf("%s: Found gl errors prior to testing glTextureView\n",
121 TestName);
122 glDeleteTextures(1, &tex);
124 return false;
127 /* create view of texture and bind it to target */
128 glGenTextures(1, &newTex);
129 glTextureView(newTex, target, tex, GL_RGBA8, 0, levels, 0, 1);
130 glDeleteTextures(1, &tex);
131 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
132 glActiveTexture(GL_TEXTURE0);
133 glBindTexture(target, newTex);
135 /* draw a quad/line using each texture mipmap level */
136 glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
137 GL_NEAREST_MIPMAP_NEAREST);
138 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
139 for (l = 0; l < levels; l++) {
140 GLfloat expected[4];
141 int p;
143 glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, l);
144 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, l);
146 glClear(GL_COLOR_BUFFER_BIT);
148 switch (target) {
149 case GL_TEXTURE_1D:
150 glUseProgram(prog1D);
151 piglit_draw_rect_tex(-1.0, -1.0, 2.0, 2.0, 0.0, 0.0,
152 1.0, 1.0);
153 break;
154 case GL_TEXTURE_2D:
155 glUseProgram(prog2D);
156 piglit_draw_rect_tex(-1.0, -1.0, 2.0, 2.0, 0.0, 0.0,
157 1.0, 1.0);
158 break;
159 case GL_TEXTURE_2D_ARRAY:
160 glUseProgram(prog2Darray);
161 draw_3d_depth(-1.0, -1.0, 2.0, 2.0, l);
162 break;
163 case GL_TEXTURE_3D:
164 glUseProgram(prog3D);
165 draw_3d_depth(-1.0, -1.0, 2.0, 2.0, l);
166 break;
169 expected[0] = Colors[l][0] / 255.0;
170 expected[1] = Colors[l][1] / 255.0;
171 expected[2] = Colors[l][2] / 255.0;
172 expected[3] = 1.0;
174 p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2,
175 expected);
177 piglit_present_results();
179 #if 0 /* debug */
180 printf("for level=%d, target=%s, expected color=%f %f %f\n",
181 l, piglit_get_gl_enum_name(target), expected[0],
182 expected[1], expected[2]);
183 sleep(1);
184 #endif
186 if (!p) {
187 printf("%s: wrong color for mipmap level %d\n",
188 TestName, l);
189 pass = false;
192 glDeleteTextures(1, &newTex);
194 return pass;
197 #define X(f, desc) \
198 do { \
199 const bool subtest_pass = (f); \
200 piglit_report_subtest_result(subtest_pass \
201 ? PIGLIT_PASS : PIGLIT_FAIL, \
202 (desc)); \
203 pass = pass && subtest_pass; \
204 } while (0)
206 enum piglit_result
207 piglit_display(void)
209 bool pass = true;
210 #ifdef PIGLIT_USE_OPENGL
211 X(test_render_with_targets(GL_TEXTURE_1D), "1D view rendering");
212 #endif
213 X(test_render_with_targets(GL_TEXTURE_2D), "2D view rendering");
214 X(test_render_with_targets(GL_TEXTURE_3D), "3D view rendering");
215 X(test_render_with_targets(GL_TEXTURE_2D_ARRAY),
216 "2D Array view rendering");
217 #undef X
218 pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
219 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
222 #ifdef PIGLIT_USE_OPENGL
223 #define GLSL_VERSION "130"
224 #else
225 #define GLSL_VERSION "310 es"
226 #endif
228 static const char *vs =
229 "#version " GLSL_VERSION "\n"
230 "in vec4 piglit_vertex;\n"
231 "in vec2 piglit_texcoord;\n"
232 "out vec3 texcoord;\n"
233 "void main() { \n"
234 " gl_Position = vec4(piglit_vertex.xy, 0.0, 1.0);\n"
235 " texcoord = vec3(piglit_texcoord, piglit_vertex.z);\n"
236 "}\n";
238 static const char *fs_3d =
239 "#version " GLSL_VERSION "\n"
240 "#ifdef GL_ES\n"
241 "precision highp float;\n"
242 "precision highp sampler3D;\n"
243 "#endif\n"
244 "in vec3 texcoord;\n"
245 "uniform sampler3D tex;\n"
246 "out vec4 color;\n"
247 "void main() { \n"
248 " color = vec4(texture(tex, texcoord).xyz, 1.0);\n"
249 "}\n";
251 static const char *fs_2darray =
252 "#version " GLSL_VERSION "\n"
253 "#ifdef GL_ES\n"
254 "precision highp float;\n"
255 "precision highp sampler2DArray;\n"
256 "#endif\n"
257 "in vec3 texcoord;\n"
258 "uniform sampler2DArray tex;\n"
259 "out vec4 color;\n"
260 "void main() { \n"
261 " color = vec4(texture(tex, texcoord).xyz, 1.0);\n"
262 "}\n";
264 static const char *fs_2d =
265 "#version " GLSL_VERSION "\n"
266 "#ifdef GL_ES\n"
267 "precision highp float;\n"
268 "precision highp sampler2D;\n"
269 "#endif\n"
270 "in vec3 texcoord;\n"
271 "uniform sampler2D tex;\n"
272 "out vec4 color;\n"
273 "void main() { \n"
274 " color = vec4(texture(tex, texcoord.xy).xyz, 1.0);\n"
275 "}\n";
277 #ifdef PIGLIT_USE_OPENGL
278 static const char *fs_1d =
279 "#version " GLSL_VERSION "\n"
280 "in vec3 texcoord;\n"
281 "uniform sampler1D tex;\n"
282 "out vec4 color;\n"
283 "void main() { \n"
284 " color = vec4(texture(tex, texcoord.x).xyz, 1.0);\n"
285 "}\n";
286 #endif
288 void
289 piglit_init(int argc, char **argv)
291 #ifdef PIGLIT_USE_OPENGL
292 piglit_require_extension("GL_ARB_texture_storage");
293 piglit_require_extension("GL_ARB_texture_view");
294 piglit_require_extension("GL_EXT_texture_array");
295 #else
296 piglit_require_extension("GL_OES_texture_view");
297 #endif
299 prog3D = piglit_build_simple_program(vs, fs_3d);
300 prog2Darray = piglit_build_simple_program(vs, fs_2darray);
301 prog2D = piglit_build_simple_program(vs, fs_2d);
303 #ifdef PIGLIT_USE_OPENGL
304 prog1D = piglit_build_simple_program(vs, fs_1d);
305 #endif