nir/spirv: pull out logic for getting builtin locations
[mesa.git] / src / glsl / link_atomics.cpp
index bfa09a3bca6b625dafc1b27102ed9f98f53cc8c9..100d03c4e8f719826e6aa0a340ad87a4cdf66ea6 100644 (file)
@@ -54,9 +54,18 @@ namespace {
 
       void push_back(unsigned id, ir_variable *var)
       {
-         counters = (active_atomic_counter *)
-            realloc(counters, sizeof(active_atomic_counter) * (num_counters + 1));
+         active_atomic_counter *new_counters;
 
+         new_counters = (active_atomic_counter *)
+            realloc(counters, sizeof(active_atomic_counter) *
+                    (num_counters + 1));
+
+         if (new_counters == NULL) {
+            _mesa_error_no_memory(__func__);
+            return;
+         }
+
+         counters = new_counters;
          counters[num_counters].id = id;
          counters[num_counters].var = var;
          num_counters++;
@@ -101,13 +110,14 @@ namespace {
          if (sh == NULL)
             continue;
 
-         foreach_list(node, sh->ir) {
-            ir_variable *var = ((ir_instruction *)node)->as_variable();
+         foreach_in_list(ir_instruction, node, sh->ir) {
+            ir_variable *var = node->as_variable();
 
             if (var && var->type->contains_atomic()) {
-               unsigned id;
+               unsigned id = 0;
                bool found = prog->UniformHash->get(id, var->name);
                assert(found);
+               (void) found;
                active_atomic_buffer *buf = &buffers[var->data.binding];
 
                /* If this is the first time the buffer is used, increment
@@ -191,11 +201,13 @@ link_assign_atomic_counter_resources(struct gl_context *ctx,
          gl_uniform_storage *const storage = &prog->UniformStorage[id];
 
          mab.Uniforms[j] = id;
-         var->data.atomic.buffer_index = i;
+         if (!var->data.explicit_binding)
+            var->data.binding = i;
+
          storage->atomic_buffer_index = i;
          storage->offset = var->data.atomic.offset;
          storage->array_stride = (var->type->is_array() ?
-                                  var->type->element_type()->atomic_size() : 0);
+                                  var->type->without_array()->atomic_size() : 0);
       }
 
       /* Assign stage-specific fields. */
@@ -214,18 +226,6 @@ void
 link_check_atomic_counter_resources(struct gl_context *ctx,
                                     struct gl_shader_program *prog)
 {
-   const unsigned max_atomic_counters[] = {
-      ctx->Const.VertexProgram.MaxAtomicCounters,
-      ctx->Const.GeometryProgram.MaxAtomicCounters,
-      ctx->Const.FragmentProgram.MaxAtomicCounters
-   };
-   STATIC_ASSERT(Elements(max_atomic_counters) == MESA_SHADER_STAGES);
-   const unsigned max_atomic_buffers[] = {
-      ctx->Const.VertexProgram.MaxAtomicBuffers,
-      ctx->Const.GeometryProgram.MaxAtomicBuffers,
-      ctx->Const.FragmentProgram.MaxAtomicBuffers
-   };
-   STATIC_ASSERT(Elements(max_atomic_buffers) == MESA_SHADER_STAGES);
    unsigned num_buffers;
    active_atomic_buffer *const abs =
       find_active_atomic_counters(ctx, prog, &num_buffers);
@@ -257,11 +257,11 @@ link_check_atomic_counter_resources(struct gl_context *ctx,
 
    /* Check that they are within the supported limits. */
    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
-      if (atomic_counters[i] > max_atomic_counters[i])
+      if (atomic_counters[i] > ctx->Const.Program[i].MaxAtomicCounters)
          linker_error(prog, "Too many %s shader atomic counters",
                       _mesa_shader_stage_to_string(i));
 
-      if (atomic_buffers[i] > max_atomic_buffers[i])
+      if (atomic_buffers[i] > ctx->Const.Program[i].MaxAtomicBuffers)
          linker_error(prog, "Too many %s shader atomic counter buffers",
                       _mesa_shader_stage_to_string(i));
    }