From: Neil Roberts
This is mostly just a straight-forward conversion of
link_assign_atomic_counter_resources to C directly using nir variables
instead of GLSL IR variables.
It is based on the version of link_assign_atomic_counter_resources in
6b8909f2d1906. I’m noting this here to make it easier to track changes
and keep the NIR version up-to-date.
---
src/compiler/Makefile.sources | 1 +
src/compiler/glsl/gl_nir_link_atomics.c | 282
src/compiler/glsl/gl_nir_linker.h | 3 +
src/compiler/glsl/meson.build | 1 +
4 files changed, 287 insertions(+)
create mode 100644 src/compiler/glsl/gl_nir_link_atomics.c
diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index 0fcbc5c5c5b..dca4d6966ee 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -28,6 +28,7 @@ LIBGLSL_FILES = \
glsl/gl_nir_lower_atomics.c \
glsl/gl_nir_lower_samplers.c \
glsl/gl_nir_lower_samplers_as_deref.c \
+ glsl/gl_nir_link_atomics.c \
glsl/gl_nir_link_uniform_initializers.c \
glsl/gl_nir_link_uniforms.c \
glsl/gl_nir_linker.c \
diff --git a/src/compiler/glsl/gl_nir_link_atomics.c
b/src/compiler/glsl/gl_nir_link_atomics.c
new file mode 100644
index 000..da6f5107c9a
--- /dev/null
+++ b/src/compiler/glsl/gl_nir_link_atomics.c
@@ -0,0 +1,282 @@
+/*
+ * Copyright © 2018 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.
+ */
+
+#include "nir.h"
+#include "linker_util.h"
+#include "gl_nir_linker.h"
+#include "compiler/glsl/ir_uniform.h" /* for gl_uniform_storage */
+#include "main/context.h"
+
+/* This file do the common link for GLSL atomic counter uniforms, using NIR,
+ * instead of IR as the counter-part glsl/link_uniforms.cpp
+ *
+ * Also note that this is tailored for ARB_gl_spirv needs and particularities
+ */
+
+struct active_atomic_counter_uniform {
+ unsigned loc;
+ nir_variable *var;
+};
+
+struct active_atomic_buffer {
+ struct active_atomic_counter_uniform *uniforms;
+ unsigned num_uniforms;
+ unsigned uniform_buffer_size;
+ unsigned stage_counter_references[MESA_SHADER_STAGES];
+ unsigned size;
+};
+
+static void
+add_atomic_counter(const void *ctx,
+ struct active_atomic_buffer *buffer,
+ unsigned uniform_loc,
+ nir_variable *var)
+{
+ if (buffer->num_uniforms >= buffer->uniform_buffer_size) {
+ if (buffer->uniform_buffer_size == 0)
+ buffer->uniform_buffer_size = 1;
+ else
+ buffer->uniform_buffer_size *= 2;
+ buffer->uniforms = reralloc(ctx,
+ buffer->uniforms,
+ struct active_atomic_counter_uniform,
+ buffer->uniform_buffer_size);
+ }
+
+ struct active_atomic_counter_uniform *uniform =
+ buffer->uniforms + buffer->num_uniforms;
+ uniform->loc = uniform_loc;
+ uniform->var = var;
+ buffer->num_uniforms++;
+}
+
+static void
+process_atomic_variable(const struct glsl_type *t,
+struct gl_shader_program *prog,
+unsigned *uniform_loc,
+nir_variable *var,
+struct active_atomic_buffer *buffers,
+unsigned *num_buffers,
+int *offset,
+unsigned shader_stage)
+{
+ /* FIXME: Arrays of arrays get counted separately. For example:
+* x1[3][3][2] = 9 uniforms, 18 atomic counters
+* x2[3][2]= 3 uniforms, 6 atomic counters
+* x3[2] = 1 uniform, 2 atomic counters
+*
+* However this code marks all the counters as active even when they
+* might not be used.
+*/
+ if (glsl_type_is_array(t) &&
+ glsl_type_is_array(glsl_get_array_element(t))) {
+ for (unsigned i = 0; i <