ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_shader_image_load_store / indexing.c
blobacef2db17d0668f10d31f00604ed05a40d1661b5
1 /*
2 * Copyright (C) 2014 Intel Corporation
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.
24 /** @file indexing.c
26 * Test that image array indexing gives the expected results. The
27 * original ARB_shader_image_load_store is rather vague in this
28 * regard, but the GLSL 4.2 specification states that:
30 * "When aggregated into arrays within a shader, images can only be
31 * indexed with a dynamically uniform integral expression, otherwise
32 * results are undefined."
34 * Which means that we can only check indexing with dynamically
35 * uniform expressions, i.e. expressions that are invariant for all
36 * shader invocations in which they are evaluated.
39 #include "common.h"
41 /** Window width. */
42 #define W 16
44 /** Window height. */
45 #define H 96
47 /** Total number of pixels in the window and images. */
48 #define N (W * H)
50 PIGLIT_GL_TEST_CONFIG_BEGIN
52 config.supports_gl_core_version = 32;
54 config.window_width = W;
55 config.window_height = H;
56 config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;
57 config.khr_no_error_support = PIGLIT_NO_ERRORS;
59 PIGLIT_GL_TEST_CONFIG_END
61 static bool
62 init_images(const struct image_info img, GLuint prog)
64 uint32_t pixels[H][W];
65 int i, j, unit;
67 for (unit = 0; unit < 8; ++unit) {
68 char *name = NULL;
70 for (i = 0; i < W; ++i)
71 for (j = 0; j < H; ++j)
72 pixels[j][i] = (i + j + unit) % 16;
74 if (!upload_image(img, unit, pixels[0]))
75 return false;
77 (void)!asprintf(&name, "imgs[%d]", unit);
78 set_uniform_int(prog, name, unit);
79 free(name);
82 return true;
85 static bool
86 check(const struct grid_info grid, unsigned u)
88 uint32_t pixels[H][W];
89 uint32_t expect[H][W];
90 int i, j, unit;
92 for (i = 0; i < W; ++i) {
93 for (j = 0; j < H; ++j) {
94 if (i % 2 == j % 3) {
95 /* Skipped invocation. */
96 expect[j][i] = 0xdeadcafe;
97 } else {
98 /* Active invocation. */
99 unsigned x = 0;
101 for (unit = 0; unit < 8; ++unit)
102 x = (x << 4 |
103 ((i + j + (unit + u) % 8) % 16));
105 expect[j][i] = x;
110 return download_result(grid, pixels[0]) &&
111 check_pixels_v(image_info_for_grid(grid),
112 pixels[0], expect[0]);
116 * Discard a number of fragments and then load elements from an array
117 * of images using dynamically uniform indices.
119 static bool
120 run_test(const struct image_stage_info *stage)
122 const struct grid_info grid =
123 grid_info(stage->stage, GL_R32UI, W, H);
124 const struct image_info img = image_info_for_grid(grid);
125 GLuint prog = generate_program(
126 grid, stage->stage ,
127 concat(image_hunk(img, ""),
128 hunk("uniform int u;\n"
129 "IMAGE_UNIFORM_T imgs[8];\n"
130 "\n"
131 "GRID_T op(ivec2 idx, GRID_T x) {\n"
132 " int i;\n"
133 "\n"
134 " if (idx.x % 2 == idx.y % 3)\n"
135 " return GRID_T(0xdeadcafeu);\n"
136 "\n"
137 " for (i = 0; i < 8; ++i) {\n"
138 " x.x = (x.x << 4 |"
139 " imageLoad(imgs[(i + u) % 8],"
140 " IMAGE_ADDR(idx)).x);\n"
141 " }\n"
142 "\n"
143 " return x;\n"
144 "}\n"), NULL));
145 bool ret = prog &&
146 init_fb(grid) &&
147 init_images(img, prog) &&
148 set_uniform_int(prog, "u", 5) &&
149 draw_grid(grid, prog) &&
150 check(grid, 5);
152 glDeleteProgram(prog);
153 return ret;
156 void
157 piglit_init(int argc, char **argv)
159 enum piglit_result status = PIGLIT_PASS;
160 const struct image_stage_info *stage;
162 piglit_require_extension("GL_ARB_shader_image_load_store");
164 for (stage = image_stages(); stage->name; ++stage)
165 subtest(&status, true, run_test(stage),
166 "%s shader/dynamically uniform indexing test",
167 stage->name);
169 piglit_report_result(status);
172 enum piglit_result
173 piglit_display(void)
175 return PIGLIT_FAIL;