[Piglit] [PATCH 07/25] arb_shader_image_load_store: Import atomicity image built-in tests.

Francisco Jerez currojerez at riseup.net
Sun Oct 5 14:00:37 PDT 2014


Import a number of tests intended to check if the read-modify-write
built-in functions defined by the spec are carried out atomically.
---
 tests/all.py                                       |   1 +
 .../arb_shader_image_load_store/CMakeLists.gl.txt  |   2 +
 tests/spec/arb_shader_image_load_store/atomicity.c | 326 +++++++++++++++++++++
 3 files changed, 329 insertions(+)
 create mode 100644 tests/spec/arb_shader_image_load_store/atomicity.c

diff --git a/tests/all.py b/tests/all.py
index f1bf710..7ec539e 100644
--- a/tests/all.py
+++ b/tests/all.py
@@ -4246,6 +4246,7 @@ spec['ARB_shader_image_load_store'] = arb_shader_image_load_store
 import_glsl_parser_tests(spec['ARB_shader_image_load_store'],
                          os.path.join(testsDir, 'spec', 'arb_shader_image_load_store'),
                          [''])
+arb_shader_image_load_store['atomicity'] = concurrent_test('arb_shader_image_load_store-atomicity')
 
 profile.tests['hiz'] = hiz
 profile.tests['fast_color_clear'] = fast_color_clear
diff --git a/tests/spec/arb_shader_image_load_store/CMakeLists.gl.txt b/tests/spec/arb_shader_image_load_store/CMakeLists.gl.txt
index cc5586d..8c67bb6 100644
--- a/tests/spec/arb_shader_image_load_store/CMakeLists.gl.txt
+++ b/tests/spec/arb_shader_image_load_store/CMakeLists.gl.txt
@@ -12,4 +12,6 @@ link_libraries (
 
 set(depends image.c grid.c common.c)
 
+piglit_add_executable(arb_shader_image_load_store-atomicity atomicity.c ${depends})
+
 # vim: ft=cmake:
diff --git a/tests/spec/arb_shader_image_load_store/atomicity.c b/tests/spec/arb_shader_image_load_store/atomicity.c
new file mode 100644
index 0000000..569c041
--- /dev/null
+++ b/tests/spec/arb_shader_image_load_store/atomicity.c
@@ -0,0 +1,326 @@
+/*
+ * 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 atomicity.c
+ *
+ * Test the atomicity of the read-modify-write image operations
+ * defined by the spec.  The subtests can be classified in two groups:
+ *
+ * The ones that test bitwise operations (imageAtomicAnd(),
+ * imageAtomicOr(), imageAtomicXor()) work by using an image as bitmap
+ * which is written to by a large number of shader invocations in
+ * parallel, each of them will use a bitwise built-in to flip an
+ * individual bit on the image.  If the read-modify-write operation is
+ * implemented atomically no write will overwrite any concurrent write
+ * supposed to flip a different bit in the same dword, so the whole
+ * bitmap will be inverted when the rendering completes.
+ *
+ * The remaining subtests (imageAtomicAdd(), imageAtomicMin(),
+ * imageAtomicMax(), imageAtomicExchange(), imageAtomicCompSwap())
+ * operate on a single 32-bit location of the image which is accessed
+ * concurrently from all shader invocations.  In each case a function
+ * written in terms of one of the built-ins is guaranteed to return a
+ * unique 32-bit value for each concurrent invocation as long as the
+ * read-modify-write operation is implemented atomically.  The way in
+ * which this is achieved differs for each built-in and is described
+ * in more detail below.
+ */
+
+#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)
+
+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, uint32_t v)
+{
+        uint32_t pixels[N];
+
+        return init_pixels(img, pixels, v, 0, 0, 0) &&
+                upload_image(img, 0, pixels);
+}
+
+static bool
+check_fb_unique(const struct grid_info grid)
+{
+        uint32_t pixels[H][W];
+        int frequency[N] = { 0 };
+        int i, j;
+
+        if (!download_result(grid, pixels[0]))
+                return false;
+
+        for (i = 0; i < W; ++i) {
+                for (j = 0; j < H; ++j) {
+                        if (frequency[pixels[j][i] % N]++) {
+                                printf("Probe value at (%d, %d)\n", i, j);
+                                printf("  Observed: 0x%08x\n", pixels[j][i]);
+                                printf("  Value not unique.\n");
+                                return false;
+                        }
+                }
+        }
+
+        return true;
+}
+
+static bool
+check_image_const(const struct image_info img, unsigned n, uint32_t v)
+{
+        uint32_t pixels[N];
+
+        return download_image(img, 0, pixels) &&
+                check_pixels(set_image_size(img, n, 1, 1, 1),
+                             pixels, v, 0, 0, 0);
+}
+
+/**
+ * Test skeleton: Init image to \a init_value, run the provided shader
+ * \a op, check that the first \a check_sz pixels of the image equal
+ * \a check_value and optionally check that the resulting fragment
+ * values on the framebuffer are unique.
+ */
+static bool
+run_test(uint32_t init_value, unsigned check_sz, uint32_t check_value,
+         bool check_unique, const char *op)
+{
+        const struct grid_info grid =
+                grid_info(GL_FRAGMENT_SHADER, GL_R32UI, W, H);
+        const struct image_info img =
+                image_info(GL_TEXTURE_1D, GL_R32UI, W, H);
+        GLuint prog = generate_program(
+                grid, GL_FRAGMENT_SHADER,
+                concat(image_hunk(img, ""),
+                       hunk("volatile uniform IMAGE_T img;\n"),
+                       hunk(op), NULL));
+        bool ret = prog &&
+                init_fb(grid) &&
+                init_image(img, init_value) &&
+                set_uniform_int(prog, "img", 0) &&
+                draw_grid(grid, prog) &&
+                check_image_const(img, check_sz, check_value) &&
+                (!check_unique || check_fb_unique(grid));
+
+        glDeleteProgram(prog);
+        return ret;
+}
+
+void
+piglit_init(int argc, char **argv)
+{
+        enum piglit_result status = PIGLIT_PASS;
+
+        piglit_require_extension("GL_ARB_shader_image_load_store");
+
+        /*
+         * If imageAtomicAdd() is atomic the return values obtained
+         * from each call are guaranteed to be unique.
+         */
+        subtest(&status, true,
+                run_test(0, 1, N, true,
+                         "GRID_T op(ivec2 idx, GRID_T x) {\n"
+                         "       return GRID_T("
+                         "          imageAtomicAdd(img, IMAGE_ADDR(ivec2(0)), 1u),"
+                         "          0, 0, 1);\n"
+                         "}\n"),
+                "imageAtomicAdd");
+
+        /*
+         * Call imageAtomicMin() on a fixed location from within a
+         * loop passing the most recent guess of the counter value
+         * decremented by one.
+         *
+         * If no race occurs the counter will be decremented by one
+         * and we're done, if another thread updates the counter in
+         * parallel imageAtomicMin() has no effect since
+         * min(x-n, x-1) = x-n for n >= 1, so we update our guess and
+         * repeat.  In the end we obtain a unique counter value for
+         * each fragment if the read-modify-write operation is atomic.
+         */
+        subtest(&status, true,
+                run_test(0xffffffff, 1, 0xffffffff - N, true,
+                        "GRID_T op(ivec2 idx, GRID_T x) {\n"
+                        "       uint old, v = 0xffffffff;"
+                        "\n"
+                        "       do {\n"
+                        "               old = v;\n"
+                        "               v = imageAtomicMin(img, IMAGE_ADDR(ivec2(0)),"
+                        "                                  v - 1u);\n"
+                        "       } while (v != old);\n"
+                        "\n"
+                        "       return GRID_T(v, 0, 0, 1);\n"
+                        "}\n"),
+                "imageAtomicMin");
+
+        /*
+         * Use imageAtomicMax() on a fixed location to increment a
+         * counter as explained above for imageAtomicMin().  The
+         * atomicity of the built-in guarantees that the obtained
+         * values will be unique for each fragment.
+         */
+        subtest(&status, true,
+                run_test(0, 1, N, true,
+                        "GRID_T op(ivec2 idx, GRID_T x) {\n"
+                        "       uint old, v = 0u;"
+                        "\n"
+                        "       do {\n"
+                        "               old = v;\n"
+                        "               v = imageAtomicMax(img, IMAGE_ADDR(ivec2(0)),"
+                        "                                  v + 1u);\n"
+                        "       } while (v != old);\n"
+                        "\n"
+                        "       return GRID_T(v, 0, 0, 1);\n"
+                        "}\n"),
+                "imageAtomicMax");
+
+        /*
+         * Use imageAtomicAnd() to flip individual bits of a bitmap
+         * atomically.  The atomicity of the built-in guarantees that
+         * all bits will be clear on termination.
+         */
+        subtest(&status, true,
+                run_test(0xffffffff, N / 32, 0, false,
+                        "GRID_T op(ivec2 idx, GRID_T x) {\n"
+                        "       int i = IMAGE_ADDR(idx);\n"
+                        "       uint m = ~(1u << (i % 32));\n"
+                        "\n"
+                        "       imageAtomicAnd(img, i / 32, m);\n"
+                        "\n"
+                        "       return GRID_T(0, 0, 0, 1);\n"
+                        "}\n"),
+                "imageAtomicAnd");
+
+        /*
+         * Use imageAtomicOr() to flip individual bits of a bitmap
+         * atomically.  The atomicity of the built-in guarantees that
+         * all bits will be set on termination.
+         */
+        subtest(&status, true,
+                run_test(0, N / 32, 0xffffffff, false,
+                        "GRID_T op(ivec2 idx, GRID_T x) {\n"
+                        "       int i = IMAGE_ADDR(idx);\n"
+                        "       uint m = (1u << (i % 32));\n"
+                        "\n"
+                        "       imageAtomicOr(img, i / 32, m);\n"
+                        "\n"
+                        "       return GRID_T(0, 0, 0, 1);\n"
+                        "}\n"),
+                "imageAtomicOr");
+
+        /*
+         * Use imageAtomicXor() to flip individual bits of a bitmap
+         * atomically.  The atomicity of the built-in guarantees that
+         * all bits will have been inverted on termination.
+         */
+        subtest(&status, true,
+                run_test(0x55555555, N / 32, 0xaaaaaaaa, false,
+                        "GRID_T op(ivec2 idx, GRID_T x) {\n"
+                        "       int i = IMAGE_ADDR(idx);\n"
+                        "       uint m = (1u << (i % 32));\n"
+                        "\n"
+                        "       imageAtomicXor(img, i / 32, m);\n"
+                        "\n"
+                        "       return GRID_T(0, 0, 0, 1);\n"
+                        "}\n"),
+                "imageAtomicXor");
+
+        /*
+         * Use imageAtomicExchange() on a fixed location to increment
+         * a counter, implementing a sort of spin-lock.
+         *
+         * The counter has two states: locked (0xffffffff) and
+         * unlocked (any other value).  While locked a single thread
+         * owns the value of the counter, increments its value and
+         * puts it back to the fixed location, atomically releasing
+         * the counter.  The atomicity of the built-in guarantees that
+         * the obtained values will be unique for each fragment.
+         *
+         * Unlike the classic spin-lock implementation, we use the
+         * same built-in call to perform either a lock or an unlock
+         * operation depending on the current thread state.  This is
+         * critical to avoid a dead-lock situation on machines where
+         * neighboring threads have limited parallelism (e.g. share
+         * the same instruction pointer).
+         */
+        subtest(&status, true,
+                run_test(0, 1, N, true,
+                        "GRID_T op(ivec2 idx, GRID_T x) {\n"
+                        "       uint p = 0xffffffff, v = 0xffffffff;\n"
+                        "\n"
+                        "       do {\n"
+                        "               if (p != 0xffffffff)\n"
+                        "                       v = p++;\n"
+                        "               p = imageAtomicExchange("
+                        "                  img, IMAGE_ADDR(ivec2(0)), p);\n"
+                        "       } while (v == 0xffffffff);\n"
+                        "\n"
+                        "       return GRID_T(v, 0, 0, 1);\n"
+                        "}\n"),
+                "imageAtomicExchange");
+
+        /*
+         * Use imageAtomicCompSwap() on a fixed location from within a
+         * loop passing the most recent guess of the counter value as
+         * comparison value and the same value incremented by one as
+         * argument.  The atomicity of the built-in guarantees that
+         * the obtained values will be unique for each fragment.
+         */
+        subtest(&status, true,
+                run_test(0, 1, N, true,
+                        "GRID_T op(ivec2 idx, GRID_T x) {\n"
+                        "       uint old, v = 0u;"
+                        "\n"
+                        "       do {\n"
+                        "               old = v;\n"
+                        "               v = imageAtomicCompSwap("
+                        "                  img, IMAGE_ADDR(ivec2(0)), v, v + 1u);\n"
+                        "       } while (v != old);\n"
+                        "\n"
+                        "       return GRID_T(v, 0, 0, 1);\n"
+                        "}\n"),
+                "imageAtomicCompSwap");
+
+        piglit_report_result(status);
+}
+
+enum piglit_result
+piglit_display(void)
+{
+        return PIGLIT_FAIL;
+}
-- 
2.1.1



More information about the Piglit mailing list