<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    On 29/04/18 12:09, Timothy Arceri wrote:<br>
    <blockquote type="cite"
      cite="mid:25e50d25-f4b7-9eaa-b614-9ae790537d18@itsqueeze.com">On
      29/04/18 19:05, Alejandro Piñeiro wrote:
      <br>
      <blockquote type="cite">On 29/04/18 10:13, Timothy Arceri wrote:
        <br>
        <blockquote type="cite">On 29/04/18 16:48, Alejandro Piñeiro
          wrote:
          <br>
          <blockquote type="cite">From: Eduardo Lima Mitev
            <a class="moz-txt-link-rfc2396E" href="mailto:elima@igalia.com"><elima@igalia.com></a>
            <br>
            <br>
            This function will be the entry point for linking the
            uniforms from
            <br>
            the nir_shader objects associated with the gl_linked_shaders
            of a
            <br>
            program.
            <br>
            <br>
            This patch includes initial support for linking uniforms
            from NIR
            <br>
            shaders. It is tailored for the ARB_gl_spirv needs, and it
            is far from
            <br>
            complete, but it should handle most cases of uniforms, array
            <br>
            uniforms, structs, samplers and images.
            <br>
            <br>
            There are some FIXMEs related to specific features that will
            be
            <br>
            implemented in following patches, like atomic counters, UBOs
            and
            <br>
            SSBOs.
            <br>
            <br>
            Also, note that ARB_gl_spirv makes mandatory explicit
            location for
            <br>
            normal uniforms, so this code only handles uniforms with
            explicit
            <br>
            location. But there are cases, like uniform atomic counters,
            that
            <br>
            doesn't have a location from the OpenGL point of view (they
            have a
            <br>
            binding), but that Mesa assign internally a location. That
            will be
            <br>
            handled on following patches.
            <br>
            <br>
            A nir_linker.h file is also added. More NIR-linking related
            API will
            <br>
            be added in subsequent patches and those will include stuff
            from Mesa,
            <br>
            so reusing nir.h didn't seem a good idea.
            <br>
          </blockquote>
          <br>
          These files should actually be
          src/compiler/glsl/nir_link_uniforms.c
          <br>
          etc these are not general purpose nir helpers they are GLSL
          specific.
          <br>
        </blockquote>
        <br>
        Yes, it is true that are not general purpose nir helpers, but
        they are
        <br>
        not GLSL specific either. As mentioned on the commit message and
        on the
        <br>
        introductory comment, it is heavily tailored for ARB_gl_spirv,
        so they
        <br>
        are SPIR-V specific.
        <br>
      </blockquote>
      <br>
      But why? Why not try to make it reusable as a linker for GLSL?
      What exactly is tailored for ARB_gl_spirv? And does this really
      block us reusing it for GLSL?
      <br>
      <br>
      I've expressed my opinion on this long ago, we are very close to
      being able to implement a NIR linker for GLSL, spending a little
      effort designing this linker code as share-able seems like a no
      brainer to me.
      <br>
    </blockquote>
    <br>
    Yes, it is true that we didn't explain in detail that decision on
    the mailing list. We briefly mentioned that on the patches that we
    were sending to the list, and explained on my presentation on FOSDEM
    [1], where Nicolai mentioned during the Q&A section that they
    agreed to us (at least with going for a nir-based linking). In fact,
    at the beginning we also hoped that this work would be more aligned
    with a more general nir-linker, and more easily reused for a
    nir-based GLSL linker, but our opinion changed as we started to code
    the needs for this extension.<br>
    <br>
    In summary the main reason are the names. Right now GLSL linking is
    based on the names. Uniform name, ubo name, and so on. So for
    example, from link_uniform_blocks.cpp:<br>
    <br>
       /* This hash table will track all of the uniform blocks that have
    been<br>
        * encountered.  Since blocks with the same block-name must be
    the same,<br>
        * the hash is organized by block-name.<br>
        */<br>
    <br>
    And most the validation rules on GLSL are based, or include somehow,
    the name of the variables.<br>
    <br>
    But on ARB_gl_spirv, everything should work without names. Read as
    example issues 12, 21 and 22 of the spec [2] as a reference. In fact
    names are optional even if the SPIR-V include them (see issue 19).
    So the linking for nir shaders coming from ARB_gl_spirv should work
    based on the location, binding, offsets, etc. With the previous
    example, ubos are linked using the binding. So explicit bindings are
    now mandatory (so the validation rule here change, as not having a
    explicit binding can be raised as an error).<br>
    <br>
    So in order to work for ARB_gl_spirv it should work without names.
    In order to reuse it for GLSL it should work with names, in fact
    based on them. Validation rules for both are different. And getting
    both working at the same time would just add a complexity that IMHO
    we don't need right now. We already have a GLSL linker, so it is not
    really worth right now to get this new NIR linker to work there too.
    Right now we are focusing on getting ARB_gl_spirv linkage working.<br>
    <br>
    Another reason is that ARB_gl_spirv GLSL supported features are not
    exactly any existing GLSL. It is based on GL_KHR_vulkan_glsl, but at
    the same time, slightly tweaked. See "Differences Relative to Other
    Specifications" on the ARB_gl_spirv spec [2]. <br>
    <br>
    So perhaps in the future some of this work could be reused for
    nir-base linker for GLSL. But, IMHO, that shouldn't be one of the
    features driving the development. I think that we should work first
    on what it is not supported.<br>
    <br>
    <blockquote type="cite"
      cite="mid:25e50d25-f4b7-9eaa-b614-9ae790537d18@itsqueeze.com">
      <br>
      Ignoring the above issue I'm fairly certain adding a dependency on
      core mesa GL to NIR is a no no so either way these files don't
      belong in src/compiler/nir
      <br>
    </blockquote>
    <br>
    But we already have that dependency, right? I mean gl_program have a
    reference to nir_shader for some time now.<br>
    <br>
    And in any case, the implementation of ARB_gl_spirv is based on NIR,
    as right now we don't have an alternative to process SPIR-V. So for
    example, the following patch (already merged on master) [3]:<br>
    <div class="commit-subject">  mesa/glspirv: Add a
      _mesa_spirv_to_nir() function</div>
    <div class="commit-msg">  This is basically a wrapper around
      spirv_to_nir() that includes
      arguments setup and post-conversion validation.<br>
      <br>
      Is at main/glspirv.c, and calls directly spirv_to_nir.<br>
      <br>
      Although it is true that code would not be called unless the
      extension is enabled, it depends on NIR and it is included on core
      mesa (main/gl_spirv.c).<br>
      <br>
      Well, unless I'm misunderstanding what do you mean for core mesa.
      I'm including mesa/main as core mesa too.<br>
      <br>
      In any case, I'm not against moving those files to a different
      directory if needed, just trying to understand why doesn't belong
      there.<br>
      <br>
      BR<br>
    </div>
    <br>
    <br>
    [1] <a class="moz-txt-link-freetext" href="https://fosdem.org/2018/schedule/event/spirv/">https://fosdem.org/2018/schedule/event/spirv/</a><br>
    [2]
    <a class="moz-txt-link-freetext" href="https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_gl_spirv.txt">https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_gl_spirv.txt</a><br>
    [3]
<a class="moz-txt-link-freetext" href="https://cgit.freedesktop.org/mesa/mesa/commit/?id=abb6d0797c8a0c32f45d38d7a41e96b2db47a47d">https://cgit.freedesktop.org/mesa/mesa/commit/?id=abb6d0797c8a0c32f45d38d7a41e96b2db47a47d</a><br>
    <blockquote type="cite"
      cite="mid:25e50d25-f4b7-9eaa-b614-9ae790537d18@itsqueeze.com">
      <br>
      <blockquote type="cite">In fact, they fit better at
        src/compiler/spirv than
        <br>
        on src/compiler/glsl. But as that directory is even more general
        <br>
        purpose, so we placed them at src/compiler/nir as it using the
        <br>
        nir-shader to do the linkage.
        <br>
        <br>
        The alternative would be place them on a new directory,
        something like
        <br>
        src/compiler/gl_spirv, but that seems somewhat an overkill.
        <br>
        <br>
        <blockquote type="cite">
          <br>
          <blockquote type="cite">
            <br>
            v2: update var->data.location with UniformStorage index
            when the
            <br>
                  storage was set up on a previous stage (Neil)
            <br>
            <br>
            Signed-off-by: Eduardo Lima <a class="moz-txt-link-rfc2396E" href="mailto:elima@igalia.com"><elima@igalia.com></a>
            <br>
            Signed-off-by: Neil Roberts <<a class="moz-txt-link-abbreviated" href="mailto:nroberts@igalia.com">nroberts@igalia.com</a>
            <br>
            Signed-off-by: Alejandro Piñeiro
            <a class="moz-txt-link-rfc2396E" href="mailto:apinheiro@igalia.com"><apinheiro@igalia.com></a>
            <br>
            ---
            <br>
               src/compiler/Makefile.sources        |   2 +
            <br>
               src/compiler/nir/meson.build         |   2 +
            <br>
               src/compiler/nir/nir_link_uniforms.c | 461
            <br>
            +++++++++++++++++++++++++++++++++++
            <br>
               src/compiler/nir/nir_linker.h        |  41 ++++
            <br>
               4 files changed, 506 insertions(+)
            <br>
               create mode 100644 src/compiler/nir/nir_link_uniforms.c
            <br>
               create mode 100644 src/compiler/nir/nir_linker.h
            <br>
            <br>
            diff --git a/src/compiler/Makefile.sources
            <br>
            b/src/compiler/Makefile.sources
            <br>
            index bb86972ea1a..dba70a5e5a1 100644
            <br>
            --- a/src/compiler/Makefile.sources
            <br>
            +++ b/src/compiler/Makefile.sources
            <br>
            @@ -206,6 +206,8 @@ NIR_FILES = \
            <br>
                   nir/nir_inline_functions.c \
            <br>
                   nir/nir_instr_set.c \
            <br>
                   nir/nir_instr_set.h \
            <br>
            +    nir/nir_link_uniforms.c \
            <br>
            +    nir/nir_linker.h \
            <br>
                   nir/nir_linking_helpers.c \
            <br>
                   nir/nir_liveness.c \
            <br>
                   nir/nir_loop_analyze.c \
            <br>
            diff --git a/src/compiler/nir/meson.build
            b/src/compiler/nir/meson.build
            <br>
            index b28a565d0c6..baf5682d388 100644
            <br>
            --- a/src/compiler/nir/meson.build
            <br>
            +++ b/src/compiler/nir/meson.build
            <br>
            @@ -99,6 +99,8 @@ files_libnir = files(
            <br>
                 'nir_inline_functions.c',
            <br>
                 'nir_instr_set.c',
            <br>
                 'nir_instr_set.h',
            <br>
            +  'nir_link_uniforms.c',
            <br>
            +  'nir_linker.h',
            <br>
                 'nir_linking_helpers.c',
            <br>
                 'nir_liveness.c',
            <br>
                 'nir_loop_analyze.c',
            <br>
            diff --git a/src/compiler/nir/nir_link_uniforms.c
            <br>
            b/src/compiler/nir/nir_link_uniforms.c
            <br>
            new file mode 100644
            <br>
            index 00000000000..450a5ce2e21
            <br>
            --- /dev/null
            <br>
            +++ b/src/compiler/nir/nir_link_uniforms.c
            <br>
            @@ -0,0 +1,461 @@
            <br>
            +/*
            <br>
            + * Copyright © 2018 Intel Corporation
            <br>
            + *
            <br>
            + * Permission is hereby granted, free of charge, to any
            person
            <br>
            obtaining a
            <br>
            + * copy of this software and associated documentation files
            (the
            <br>
            "Software"),
            <br>
            + * to deal in the Software without restriction, including
            without
            <br>
            limitation
            <br>
            + * the rights to use, copy, modify, merge, publish,
            distribute,
            <br>
            sublicense,
            <br>
            + * and/or sell copies of the Software, and to permit
            persons to whom
            <br>
            the
            <br>
            + * Software is furnished to do so, subject to the following
            conditions:
            <br>
            + *
            <br>
            + * The above copyright notice and this permission notice
            (including
            <br>
            the next
            <br>
            + * paragraph) shall be included in all copies or
            substantial
            <br>
            portions of the
            <br>
            + * Software.
            <br>
            + *
            <br>
            + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
            ANY KIND,
            <br>
            EXPRESS OR
            <br>
            + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
            <br>
            MERCHANTABILITY,
            <br>
            + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
            IN NO
            <br>
            EVENT SHALL
            <br>
            + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
            CLAIM, DAMAGES
            <br>
            OR OTHER
            <br>
            + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
            OTHERWISE,
            <br>
            ARISING
            <br>
            + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
            USE OR
            <br>
            OTHER DEALINGS
            <br>
            + * IN THE SOFTWARE.
            <br>
            + */
            <br>
            +
            <br>
            +#include "nir.h"
            <br>
            +#include "nir_linker.h"
            <br>
            +#include "compiler/glsl/ir_uniform.h" /* for
            gl_uniform_storage */
            <br>
            +#include "compiler/linker_util.h"
            <br>
            +#include "main/context.h"
            <br>
            +#include "main/mtypes.h"
            <br>
            +
            <br>
            +/* This file do the common link for GLSL uniforms, using
            NIR,
            <br>
            instead of IR as
            <br>
            + * the counter-part glsl/link_uniforms.cpp
            <br>
            + *
            <br>
            + * Also note that this is tailored for ARB_gl_spirv needs
            and
            <br>
            particularities
            <br>
            + * (like need to work/link without name available, explicit
            location
            <br>
            for
            <br>
            + * normal uniforms as mandatory, and so on).
            <br>
            + */
            <br>
            +
            <br>
            +static void
            <br>
            +nir_setup_uniform_remap_tables(struct gl_context *ctx,
            <br>
            +                               struct gl_shader_program
            *prog)
            <br>
            +{
            <br>
            +   prog->UniformRemapTable = rzalloc_array(prog,
            <br>
            +                                           struct
            gl_uniform_storage *,
            <br>
            +                                          
            prog->NumUniformRemapTable);
            <br>
            +   union gl_constant_value *data =
            <br>
            +      rzalloc_array(prog->data,
            <br>
            +                    union gl_constant_value,
            <br>
            prog->data->NumUniformDataSlots);
            <br>
            +   if (!prog->UniformRemapTable || !data) {
            <br>
            +      linker_error(prog, "Out of memory during
            linking.\n");
            <br>
            +      return;
            <br>
            +   }
            <br>
            +   prog->data->UniformDataSlots = data;
            <br>
            +
            <br>
            +   unsigned data_pos = 0;
            <br>
            +
            <br>
            +   /* Reserve all the explicit locations of the active
            uniforms. */
            <br>
            +   for (unsigned i = 0; i <
            prog->data->NumUniformStorage; i++) {
            <br>
            +      struct gl_uniform_storage *uniform =
            <br>
            &prog->data->UniformStorage[i];
            <br>
            +
            <br>
            +      /* How many new entries for this uniform? */
            <br>
            +      const unsigned entries = MAX2(1,
            uniform->array_elements);
            <br>
            +      unsigned num_slots =
            glsl_get_component_slots(uniform->type);
            <br>
            +
            <br>
            +      uniform->storage = &data[data_pos];
            <br>
            +
            <br>
            +      /* Set remap table entries point to correct
            <br>
            gl_uniform_storage. */
            <br>
            +      for (unsigned j = 0; j < entries; j++) {
            <br>
            +         unsigned element_loc = uniform->remap_location
            + j;
            <br>
            +         prog->UniformRemapTable[element_loc] = uniform;
            <br>
            +
            <br>
            +         data_pos += num_slots;
            <br>
            +      }
            <br>
            +   }
            <br>
            +}
            <br>
            +
            <br>
            +static struct gl_uniform_storage *
            <br>
            +find_previous_uniform_storage(struct gl_shader_program
            *prog,
            <br>
            +                              int location)
            <br>
            +{
            <br>
            +   /* This would only work for uniform with explicit
            location, as
            <br>
            all the
            <br>
            +    * uniforms without location (ie: atomic counters) would
            have a
            <br>
            initial
            <br>
            +    * location equal to -1. We early return in that case.
            <br>
            +    */
            <br>
            +   if (location == -1)
            <br>
            +      return NULL;
            <br>
            +
            <br>
            +   for (unsigned i = 0; i <
            prog->data->NumUniformStorage; i++)
            <br>
            +      if
            (prog->data->UniformStorage[i].remap_location ==
            location)
            <br>
            +         return &prog->data->UniformStorage[i];
            <br>
            +
            <br>
            +   return NULL;
            <br>
            +}
            <br>
            +
            <br>
            +/* Used to build a tree representing the glsl_type so that
            we can
            <br>
            have a place
            <br>
            + * to store the next index for opaque types. Array types
            are
            <br>
            expanded so that
            <br>
            + * they have a single child which is used for all elements
            of the
            <br>
            array.
            <br>
            + * Struct types have a child for each member. The tree is
            walked while
            <br>
            + * processing a uniform so that we can recognise when an
            opaque type is
            <br>
            + * encountered a second time in order to reuse the same
            range of
            <br>
            indices that
            <br>
            + * was reserved the first time. That way the sampler
            indices can be
            <br>
            arranged
            <br>
            + * so that members of an array are placed sequentially even
            if the
            <br>
            array is an
            <br>
            + * array of structs containing other opaque members.
            <br>
            + */
            <br>
            +struct type_tree_entry {
            <br>
            +   /* For opaque types, this will be the next index to use.
            If we
            <br>
            haven’t
            <br>
            +    * encountered this member yet, it will be UINT_MAX.
            <br>
            +    */
            <br>
            +   unsigned next_index;
            <br>
            +   unsigned array_size;
            <br>
            +   struct type_tree_entry *parent;
            <br>
            +   struct type_tree_entry *next_sibling;
            <br>
            +   struct type_tree_entry *children;
            <br>
            +};
            <br>
            +
            <br>
            +struct nir_link_uniforms_state {
            <br>
            +   /* per-whole program */
            <br>
            +   unsigned num_hidden_uniforms;
            <br>
            +   unsigned num_values;
            <br>
            +   unsigned max_uniform_location;
            <br>
            +   unsigned next_sampler_index;
            <br>
            +   unsigned next_image_index;
            <br>
            +
            <br>
            +   /* per-shader stage */
            <br>
            +   unsigned num_shader_samplers;
            <br>
            +   unsigned num_shader_images;
            <br>
            +   unsigned num_shader_uniform_components;
            <br>
            +   unsigned shader_samplers_used;
            <br>
            +   unsigned shader_shadow_samplers;
            <br>
            +
            <br>
            +   nir_variable *current_var;
            <br>
            +
            <br>
            +   struct type_tree_entry *current_type;
            <br>
            +};
            <br>
            +
            <br>
            +static struct type_tree_entry *
            <br>
            +build_type_tree_for_type(const struct glsl_type *type)
            <br>
            +{
            <br>
            +   struct type_tree_entry *entry = malloc(sizeof *entry);
            <br>
            +
            <br>
            +   entry->array_size = 1;
            <br>
            +   entry->next_index = UINT_MAX;
            <br>
            +   entry->children = NULL;
            <br>
            +   entry->next_sibling = NULL;
            <br>
            +   entry->parent = NULL;
            <br>
            +
            <br>
            +   if (glsl_type_is_array(type)) {
            <br>
            +      entry->array_size = glsl_get_length(type);
            <br>
            +      entry->children =
            <br>
            build_type_tree_for_type(glsl_get_array_element(type));
            <br>
            +      entry->children->parent = entry;
            <br>
            +   } else if (glsl_type_is_struct(type)) {
            <br>
            +      struct type_tree_entry *last = NULL;
            <br>
            +
            <br>
            +      for (unsigned i = 0; i < glsl_get_length(type);
            i++) {
            <br>
            +         const struct glsl_type *field_type =
            <br>
            glsl_get_struct_field(type, i);
            <br>
            +         struct type_tree_entry *field_entry =
            <br>
            +            build_type_tree_for_type(field_type);
            <br>
            +
            <br>
            +         if (last == NULL)
            <br>
            +            entry->children = field_entry;
            <br>
            +         else
            <br>
            +            last->next_sibling = field_entry;
            <br>
            +
            <br>
            +         field_entry->parent = entry;
            <br>
            +
            <br>
            +         last = field_entry;
            <br>
            +      }
            <br>
            +   }
            <br>
            +
            <br>
            +   return entry;
            <br>
            +}
            <br>
            +
            <br>
            +static void
            <br>
            +free_type_tree(struct type_tree_entry *entry)
            <br>
            +{
            <br>
            +   struct type_tree_entry *p, *next;
            <br>
            +
            <br>
            +   for (p = entry->children; p; p = next) {
            <br>
            +      next = p->next_sibling;
            <br>
            +      free_type_tree(p);
            <br>
            +   }
            <br>
            +
            <br>
            +   free(entry);
            <br>
            +}
            <br>
            +
            <br>
            +static unsigned
            <br>
            +get_next_index(struct nir_link_uniforms_state *state,
            <br>
            +               const struct gl_uniform_storage *uniform,
            <br>
            +               unsigned *next_index)
            <br>
            +{
            <br>
            +   /* If we’ve already calculated an index for this member
            then we
            <br>
            can just
            <br>
            +    * offset from there.
            <br>
            +    */
            <br>
            +   if (state->current_type->next_index == UINT_MAX) {
            <br>
            +      /* Otherwise we need to reserve enough indices for
            all of the
            <br>
            arrays
            <br>
            +       * enclosing this member.
            <br>
            +       */
            <br>
            +
            <br>
            +      unsigned array_size = 1;
            <br>
            +
            <br>
            +      for (const struct type_tree_entry *p =
            state->current_type;
            <br>
            +           p;
            <br>
            +           p = p->parent) {
            <br>
            +         array_size *= p->array_size;
            <br>
            +      }
            <br>
            +
            <br>
            +      state->current_type->next_index = *next_index;
            <br>
            +      *next_index += array_size;
            <br>
            +   }
            <br>
            +
            <br>
            +   unsigned index = state->current_type->next_index;
            <br>
            +
            <br>
            +   state->current_type->next_index += MAX2(1,
            uniform->array_elements);
            <br>
            +
            <br>
            +   return index;
            <br>
            +}
            <br>
            +
            <br>
            +
            <br>
            +/**
            <br>
            + * Creates the neccessary entries in UniformStorage for the
            uniform.
            <br>
            Returns
            <br>
            + * the number of locations used or -1 on failure.
            <br>
            + */
            <br>
            +static int
            <br>
            +nir_link_uniform(struct gl_context *ctx,
            <br>
            +                 struct gl_shader_program *prog,
            <br>
            +                 struct gl_program *stage_program,
            <br>
            +                 gl_shader_stage stage,
            <br>
            +                 const struct glsl_type *type,
            <br>
            +                 int location,
            <br>
            +                 struct nir_link_uniforms_state *state)
            <br>
            +{
            <br>
            +   struct gl_uniform_storage *uniform = NULL;
            <br>
            +
            <br>
            +   /* gl_uniform_storage can cope with one level of array,
            so if the
            <br>
            type is a
            <br>
            +    * composite type or an array where each element
            occupies more
            <br>
            than one
            <br>
            +    * location than we need to recursively process it.
            <br>
            +    */
            <br>
            +   if (glsl_type_is_struct(type) ||
            <br>
            +       (glsl_type_is_array(type) &&
            <br>
            +        (glsl_type_is_array(glsl_get_array_element(type))
            ||
            <br>
            +        
            glsl_type_is_struct(glsl_get_array_element(type))))) {
            <br>
            +      int location_count = 0;
            <br>
            +      struct type_tree_entry *old_type =
            state->current_type;
            <br>
            +
            <br>
            +      state->current_type = old_type->children;
            <br>
            +
            <br>
            +      for (unsigned i = 0; i < glsl_get_length(type);
            i++) {
            <br>
            +         const struct glsl_type *field_type;
            <br>
            +
            <br>
            +         if (glsl_type_is_struct(type))
            <br>
            +            field_type = glsl_get_struct_field(type, i);
            <br>
            +         else
            <br>
            +            field_type = glsl_get_array_element(type);
            <br>
            +
            <br>
            +         int entries = nir_link_uniform(ctx, prog,
            stage_program,
            <br>
            stage,
            <br>
            +                                        field_type,
            location,
            <br>
            +                                        state);
            <br>
            +         if (entries == -1)
            <br>
            +            return -1;
            <br>
            +
            <br>
            +         if (location != -1)
            <br>
            +            location += entries;
            <br>
            +         location_count += entries;
            <br>
            +
            <br>
            +         if (glsl_type_is_struct(type))
            <br>
            +            state->current_type =
            state->current_type->next_sibling;
            <br>
            +      }
            <br>
            +
            <br>
            +      state->current_type = old_type;
            <br>
            +
            <br>
            +      return location_count;
            <br>
            +   } else {
            <br>
            +      /* Create a new uniform storage entry */
            <br>
            +      prog->data->UniformStorage =
            <br>
            +         reralloc(prog->data,
            <br>
            +                  prog->data->UniformStorage,
            <br>
            +                  struct gl_uniform_storage,
            <br>
            +                  prog->data->NumUniformStorage + 1);
            <br>
            +      if (!prog->data->UniformStorage) {
            <br>
            +         linker_error(prog, "Out of memory during
            linking.\n");
            <br>
            +         return -1;
            <br>
            +      }
            <br>
            +
            <br>
            +      uniform =
            <br>
&prog->data->UniformStorage[prog->data->NumUniformStorage];
            <br>
            +      prog->data->NumUniformStorage++;
            <br>
            +
            <br>
            +      /* Initialize its members */
            <br>
            +      memset(uniform, 0x00, sizeof(struct
            gl_uniform_storage));
            <br>
            +      /* ARB_gl_spirv: names are considered optional debug
            info, so
            <br>
            the linker
            <br>
            +       * needs to work without them, and returning them is
            optional.
            <br>
            For
            <br>
            +       * simplicity we ignore names.
            <br>
            +       */
            <br>
            +      uniform->name = NULL;
            <br>
            +
            <br>
            +      const struct glsl_type *type_no_array =
            glsl_without_array(type);
            <br>
            +      if (glsl_type_is_array(type)) {
            <br>
            +         uniform->type = type_no_array;
            <br>
            +         uniform->array_elements =
            glsl_get_length(type);
            <br>
            +      } else {
            <br>
            +         uniform->type = type;
            <br>
            +         uniform->array_elements = 0;
            <br>
            +      }
            <br>
            +      uniform->active_shader_mask |= 1 << stage;
            <br>
            +
            <br>
            +      /* Uniform has an explicit location */
            <br>
            +      uniform->remap_location = location;
            <br>
            +
            <br>
            +      /* @FIXME: the initialization of the following will
            be done as we
            <br>
            +       * implement support for their specific features,
            like SSBO,
            <br>
            atomics,
            <br>
            +       * etc.
            <br>
            +       */
            <br>
            +      uniform->block_index = -1;
            <br>
            +      uniform->offset = -1;
            <br>
            +      uniform->matrix_stride = -1;
            <br>
            +      uniform->array_stride = -1;
            <br>
            +      uniform->row_major = false;
            <br>
            +      uniform->hidden = false;
            <br>
            +      uniform->builtin = false;
            <br>
            +      uniform->is_shader_storage = false;
            <br>
            +      uniform->atomic_buffer_index = -1;
            <br>
            +      uniform->top_level_array_size = 0;
            <br>
            +      uniform->top_level_array_stride = 0;
            <br>
            +      uniform->is_bindless = false;
            <br>
            +
            <br>
            +      /* The following are not for features not supported
            by
            <br>
            ARB_gl_spirv */
            <br>
            +      uniform->num_compatible_subroutines = 0;
            <br>
            +
            <br>
            +      unsigned entries = MAX2(1,
            uniform->array_elements);
            <br>
            +
            <br>
            +      if (glsl_type_is_sampler(type_no_array)) {
            <br>
            +         int sampler_index =
            <br>
            +            get_next_index(state, uniform,
            &state->next_sampler_index);
            <br>
            +
            <br>
            +         state->num_shader_samplers++;
            <br>
            +
            <br>
            +         uniform->opaque[stage].active = true;
            <br>
            +         uniform->opaque[stage].index = sampler_index;
            <br>
            +
            <br>
            +         const unsigned shadow =
            <br>
            glsl_sampler_type_is_shadow(type_no_array);
            <br>
            +
            <br>
            +         for (unsigned i = sampler_index;
            <br>
            +              i < MIN2(state->next_sampler_index,
            MAX_SAMPLERS);
            <br>
            +              i++) {
            <br>
            +            stage_program->sh.SamplerTargets[i] =
            <br>
            +               glsl_get_sampler_target(type_no_array);
            <br>
            +            state->shader_samplers_used |= 1U <<
            i;
            <br>
            +            state->shader_shadow_samplers |= shadow
            << i;
            <br>
            +         }
            <br>
            +      } else if (glsl_type_is_image(type_no_array)) {
            <br>
            +         /* @FIXME: image_index should match that of the
            same image
            <br>
            +          * uniform in other shaders. This means we need to
            match image
            <br>
            +          * uniforms by location (GLSL does it by variable
            name, but we
            <br>
            +          * want to avoid that).
            <br>
            +          */
            <br>
            +         int image_index = state->next_image_index;
            <br>
            +         state->next_image_index += entries;
            <br>
            +
            <br>
            +         state->num_shader_images++;
            <br>
            +
            <br>
            +         uniform->opaque[stage].active = true;
            <br>
            +         uniform->opaque[stage].index = image_index;
            <br>
            +
            <br>
            +         /* Set image access qualifiers */
            <br>
            +         const GLenum access =
            <br>
            +            (state->current_var->data.image.read_only
            ? GL_READ_ONLY :
            <br>
            +            
            state->current_var->data.image.write_only ?
            <br>
            GL_WRITE_ONLY :
            <br>
            +             GL_READ_WRITE);
            <br>
            +         for (unsigned i = image_index;
            <br>
            +              i < MIN2(state->next_image_index,
            MAX_IMAGE_UNIFORMS);
            <br>
            +              i++) {
            <br>
            +            stage_program->sh.ImageAccess[i] = access;
            <br>
            +         }
            <br>
            +      }
            <br>
            +
            <br>
            +      unsigned values = glsl_get_component_slots(type);
            <br>
            +      state->num_shader_uniform_components += values;
            <br>
            +      state->num_values += values;
            <br>
            +
            <br>
            +      if (state->max_uniform_location <
            uniform->remap_location +
            <br>
            entries)
            <br>
            +         state->max_uniform_location =
            uniform->remap_location +
            <br>
            entries;
            <br>
            +
            <br>
            +      return MAX2(uniform->array_elements, 1);
            <br>
            +   }
            <br>
            +}
            <br>
            +
            <br>
            +bool
            <br>
            +nir_link_uniforms(struct gl_context *ctx,
            <br>
            +                  struct gl_shader_program *prog)
            <br>
            +{
            <br>
            +   /* First free up any previous UniformStorage items */
            <br>
            +   ralloc_free(prog->data->UniformStorage);
            <br>
            +   prog->data->UniformStorage = NULL;
            <br>
            +   prog->data->NumUniformStorage = 0;
            <br>
            +
            <br>
            +   /* Iterate through all linked shaders */
            <br>
            +   struct nir_link_uniforms_state state = {0,};
            <br>
            +
            <br>
            +   for (unsigned shader_type = 0; shader_type <
            MESA_SHADER_STAGES;
            <br>
            shader_type++) {
            <br>
            +      struct gl_linked_shader *sh =
            prog->_LinkedShaders[shader_type];
            <br>
            +      if (!sh)
            <br>
            +         continue;
            <br>
            +
            <br>
            +      nir_shader *nir = sh->Program->nir;
            <br>
            +      assert(nir);
            <br>
            +
            <br>
            +      state.num_shader_samplers = 0;
            <br>
            +      state.num_shader_images = 0;
            <br>
            +      state.num_shader_uniform_components = 0;
            <br>
            +      state.shader_samplers_used = 0;
            <br>
            +      state.shader_shadow_samplers = 0;
            <br>
            +
            <br>
            +      nir_foreach_variable(var, &nir->uniforms) {
            <br>
            +         struct gl_uniform_storage *uniform = NULL;
            <br>
            +
            <br>
            +         /* Check if the uniform has been processed already
            for
            <br>
            +          * other stage. If so, validate they are
            compatible and update
            <br>
            +          * the active stage mask.
            <br>
            +          */
            <br>
            +         uniform = find_previous_uniform_storage(prog,
            <br>
            var->data.location);
            <br>
            +         if (uniform) {
            <br>
            +            uniform->active_shader_mask |= 1 <<
            shader_type;
            <br>
            +            var->data.location = uniform -
            prog->data->UniformStorage;
            <br>
            +
            <br>
            +            continue;
            <br>
            +         }
            <br>
            +
            <br>
            +         int location = var->data.location;
            <br>
            +         /* From now on the variable’s location will be its
            uniform
            <br>
            index */
            <br>
            +         var->data.location =
            prog->data->NumUniformStorage;
            <br>
            +
            <br>
            +         state.current_var = var;
            <br>
            +
            <br>
            +         struct type_tree_entry *type_tree =
            <br>
            +            build_type_tree_for_type(var->type);
            <br>
            +         state.current_type = type_tree;
            <br>
            +
            <br>
            +         int res = nir_link_uniform(ctx, prog,
            sh->Program,
            <br>
            shader_type, var->type,
            <br>
            +                                    location, &state);
            <br>
            +
            <br>
            +         free_type_tree(type_tree);
            <br>
            +
            <br>
            +         if (res == -1)
            <br>
            +            return false;
            <br>
            +      }
            <br>
            +
            <br>
            +      sh->Program->SamplersUsed =
            state.shader_samplers_used;
            <br>
            +      sh->shadow_samplers =
            state.shader_shadow_samplers;
            <br>
            +      sh->Program->info.num_textures =
            state.num_shader_samplers;
            <br>
            +      sh->Program->info.num_images =
            state.num_shader_images;
            <br>
            +      sh->num_uniform_components =
            state.num_shader_uniform_components;
            <br>
            +      sh->num_combined_uniform_components =
            sh->num_uniform_components;
            <br>
            +   }
            <br>
            +
            <br>
            +   prog->data->NumHiddenUniforms =
            state.num_hidden_uniforms;
            <br>
            +   prog->NumUniformRemapTable =
            state.max_uniform_location;
            <br>
            +   prog->data->NumUniformDataSlots =
            state.num_values;
            <br>
            +
            <br>
            +   nir_setup_uniform_remap_tables(ctx, prog);
            <br>
            +
            <br>
            +   return true;
            <br>
            +}
            <br>
            diff --git a/src/compiler/nir/nir_linker.h
            <br>
            b/src/compiler/nir/nir_linker.h
            <br>
            new file mode 100644
            <br>
            index 00000000000..d57bed64f97
            <br>
            --- /dev/null
            <br>
            +++ b/src/compiler/nir/nir_linker.h
            <br>
            @@ -0,0 +1,41 @@
            <br>
            +/*
            <br>
            + * Copyright © 2017 Intel Corporation
            <br>
            + *
            <br>
            + * Permission is hereby granted, free of charge, to any
            person
            <br>
            obtaining a
            <br>
            + * copy of this software and associated documentation files
            (the
            <br>
            "Software"),
            <br>
            + * to deal in the Software without restriction, including
            without
            <br>
            limitation
            <br>
            + * the rights to use, copy, modify, merge, publish,
            distribute,
            <br>
            sublicense,
            <br>
            + * and/or sell copies of the Software, and to permit
            persons to whom
            <br>
            the
            <br>
            + * Software is furnished to do so, subject to the following
            conditions:
            <br>
            + *
            <br>
            + * The above copyright notice and this permission notice
            (including
            <br>
            the next
            <br>
            + * paragraph) shall be included in all copies or
            substantial
            <br>
            portions of the
            <br>
            + * Software.
            <br>
            + *
            <br>
            + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
            ANY KIND,
            <br>
            EXPRESS OR
            <br>
            + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
            <br>
            MERCHANTABILITY,
            <br>
            + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
            IN NO
            <br>
            EVENT SHALL
            <br>
            + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
            CLAIM, DAMAGES
            <br>
            OR OTHER
            <br>
            + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
            OTHERWISE,
            <br>
            ARISING
            <br>
            + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
            USE OR
            <br>
            OTHER DEALINGS
            <br>
            + * IN THE SOFTWARE.
            <br>
            + */
            <br>
            +
            <br>
            +#ifndef NIR_LINKER_H
            <br>
            +#define NIR_LINKER_H
            <br>
            +
            <br>
            +#ifdef __cplusplus
            <br>
            +extern "C" {
            <br>
            +#endif
            <br>
            +
            <br>
            +struct gl_context;
            <br>
            +struct gl_shader_program;
            <br>
            +
            <br>
            +bool nir_link_uniforms(struct gl_context *ctx,
            <br>
            +                       struct gl_shader_program *prog);
            <br>
            +
            <br>
            +#ifdef __cplusplus
            <br>
            +} /* extern "C" */
            <br>
            +#endif
            <br>
            +
            <br>
            +#endif /* NIR_LINKER_H */
            <br>
            <br>
          </blockquote>
          <br>
        </blockquote>
        <br>
      </blockquote>
      <br>
    </blockquote>
    <br>
  </body>
</html>