summaryrefslogtreecommitdiff
path: root/tests/spec/arb_shader_image_load_store/level.c
blob: 5fbdbaf5c000c8510ecc1ba6b193771c92f2eee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (C) 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/** @file level.c
 *
 * Test the binding of individual mipmap levels to an image unit by
 * dumping the whole accessible contents of an image to the
 * framebuffer and then checking that the observed values match the
 * bound mipmap level.  The same mipmap level is then overwritten by
 * the shader program after its contents have been read.
 */

#include "common.h"

/** Window width. */
#define W 16

/** Window height. */
#define H 96

/** Total number of pixels in the window and image. */
#define N (W * H)

/** Maximum number of mipmap levels. */
#define M 11

PIGLIT_GL_TEST_CONFIG_BEGIN

config.supports_gl_core_version = 32;

config.window_width = W;
config.window_height = H;
config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA;

PIGLIT_GL_TEST_CONFIG_END

static bool
init_image(const struct image_info img, unsigned l)
{
        const unsigned num_levels = image_num_levels(img);
        uint32_t pixels[4 * N * M];
        unsigned i;

        for (i = 0; i < 4 * N * num_levels; ++i)
                pixels[i] = encode(img.format, i);

        return upload_image_levels(img, num_levels, l, 0, pixels);
}

static bool
check_fb(const struct grid_info grid, const struct image_info img, unsigned l)
{
        const unsigned offset = 4 * image_level_offset(img, l);
        const unsigned n = 4 * product(grid.size);
        uint32_t pixels_fb[4 * N], expect_fb[4 * N];
        unsigned i;

        if (!download_result(grid, pixels_fb))
                return false;

        for (i = 0; i < n; ++i) {
                /*
                 * The framebuffer contents should reflect level l of
                 * the image which is read by the shader program.
                 */
                expect_fb[i] = encode(grid.format, offset + i);
        }

        if (!check_pixels_v(image_info_for_grid(grid), pixels_fb, expect_fb)) {
                printf("  Source: framebuffer\n");
                return false;
        }

        return true;
}

static bool
check_img(const struct image_info img, unsigned l)
{
        const unsigned num_levels = image_num_levels(img);
        uint32_t pixels_img[4 * N * M], expect_img[4 * N];
        unsigned i, j;

        if (!download_image_levels(img, num_levels, 0, pixels_img))
                return false;

        for (j = 0; j < num_levels; ++j) {
                const struct image_info level_img = image_info_for_level(img, j);
                const unsigned offset = 4 * image_level_offset(img, j);
                const unsigned n = 4 * product(level_img.size);

                for (i = 0; i < n; ++i) {
                        if (j == l) {
                                /*
                                 * Level l should have been modified
                                 * by the shader.
                                 */
                                expect_img[i] = encode(img.format, 33);
                        } else {
                                /*
                                 * Other levels should have remained
                                 * unchanged.
                                 */
                                expect_img[i] = encode(img.format, offset + i);
                        }
                }

                if (!check_pixels_v(level_img, &pixels_img[offset],
                                    expect_img)) {
                        printf("  Source: image level %d\n", j);
                        return false;
                }
        }

        return true;
}

/**
 * Bind an individual level of a texture mipmap to an image unit, read
 * its contents and write back a different value to the same location.
 */
static bool
run_test(const struct image_target_info *target)
{
        const unsigned level = 3;
        const struct image_info img = image_info(
                target->target, GL_RGBA32F, W, H);
        const struct image_info level_img = image_info_for_level(img, level);
        const struct grid_info grid = {
                GL_FRAGMENT_SHADER_BIT, img.format,
                image_optimal_extent(level_img.size)
        };
        GLuint prog = generate_program(
                grid, GL_FRAGMENT_SHADER,
                concat(image_hunk(level_img, ""),
                       hunk("uniform IMAGE_T img;\n"
                            "\n"
                            "GRID_T op(ivec2 idx, GRID_T x) {\n"
                            "        GRID_T v = imageLoad(img, IMAGE_ADDR(idx));\n"
                            "        imageStore(img, IMAGE_ADDR(idx), DATA_T(33));\n"
                            "        return v;\n"
                            "}\n"), NULL));
        bool ret = prog && init_fb(grid) &&
                init_image(img, level) &&
                set_uniform_int(prog, "img", 0) &&
                draw_grid(grid, prog) &&
                check_fb(grid, img, level) &&
                check_img(img, level);

        glDeleteProgram(prog);
        return ret;
}

void
piglit_init(int argc, char **argv)
{
        enum piglit_result status = PIGLIT_PASS;
        const struct image_target_info *target;

        piglit_require_extension("GL_ARB_shader_image_load_store");

        for (target = image_targets(); target->name; ++target) {
                if (image_target_mipmapping_dimensions(target))
                        subtest(&status, true, run_test(target),
                                "%s level binding test", target->name);
        }

        piglit_report_result(status);
}

enum piglit_result
piglit_display(void)
{
        return PIGLIT_FAIL;
}