glsl: add SYSTEM_VALUE_VERTEX_CNT
[mesa.git] / src / glsl / link_atomics.cpp
index 2466bbd79ed9636d8632b39c6e3a1f3c1b48648e..100d03c4e8f719826e6aa0a340ad87a4cdf66ea6 100644 (file)
@@ -21,6 +21,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include "glsl_parser_extras.h"
 #include "ir.h"
 #include "ir_uniform.h"
 #include "linker.h"
@@ -53,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++;
@@ -63,7 +73,7 @@ namespace {
 
       active_atomic_counter *counters;
       unsigned num_counters;
-      unsigned stage_references[MESA_SHADER_TYPES];
+      unsigned stage_references[MESA_SHADER_STAGES];
       unsigned size;
    };
 
@@ -73,16 +83,16 @@ namespace {
       const active_atomic_counter *const first = (active_atomic_counter *) a;
       const active_atomic_counter *const second = (active_atomic_counter *) b;
 
-      return int(first->var->atomic.offset) - int(second->var->atomic.offset);
+      return int(first->var->data.atomic.offset) - int(second->var->data.atomic.offset);
    }
 
    bool
    check_atomic_counters_overlap(const ir_variable *x, const ir_variable *y)
    {
-      return ((x->atomic.offset >= y->atomic.offset &&
-               x->atomic.offset < y->atomic.offset + y->type->atomic_size()) ||
-              (y->atomic.offset >= x->atomic.offset &&
-               y->atomic.offset < x->atomic.offset + x->type->atomic_size()));
+      return ((x->data.atomic.offset >= y->data.atomic.offset &&
+               x->data.atomic.offset < y->data.atomic.offset + y->type->atomic_size()) ||
+              (y->data.atomic.offset >= x->data.atomic.offset &&
+               y->data.atomic.offset < x->data.atomic.offset + x->type->atomic_size()));
    }
 
    active_atomic_buffer *
@@ -95,19 +105,20 @@ namespace {
 
       *num_buffers = 0;
 
-      for (unsigned i = 0; i < MESA_SHADER_TYPES; ++i) {
+      for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
          struct gl_shader *sh = prog->_LinkedShaders[i];
          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);
-               active_atomic_buffer *buf = &buffers[var->binding];
+               (void) found;
+               active_atomic_buffer *buf = &buffers[var->data.binding];
 
                /* If this is the first time the buffer is used, increment
                 * the counter of buffers used.
@@ -118,7 +129,7 @@ namespace {
                buf->push_back(id, var);
 
                buf->stage_references[i]++;
-               buf->size = MAX2(buf->size, var->atomic.offset +
+               buf->size = MAX2(buf->size, var->data.atomic.offset +
                                 var->type->atomic_size());
             }
          }
@@ -143,7 +154,7 @@ namespace {
                linker_error(prog, "Atomic counter %s declared at offset %d "
                             "which is already in use.",
                             buffers[i].counters[j].var->name,
-                            buffers[i].counters[j].var->atomic.offset);
+                            buffers[i].counters[j].var->data.atomic.offset);
             }
          }
       }
@@ -190,15 +201,17 @@ link_assign_atomic_counter_resources(struct gl_context *ctx,
          gl_uniform_storage *const storage = &prog->UniformStorage[id];
 
          mab.Uniforms[j] = id;
-         var->atomic.buffer_index = i;
+         if (!var->data.explicit_binding)
+            var->data.binding = i;
+
          storage->atomic_buffer_index = i;
-         storage->offset = var->atomic.offset;
+         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. */
-      for (unsigned j = 0; j < MESA_SHADER_TYPES; ++j)
+      for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j)
          mab.StageReferences[j] =
             (ab.stage_references[j] ? GL_TRUE : GL_FALSE);
 
@@ -213,25 +226,11 @@ void
 link_check_atomic_counter_resources(struct gl_context *ctx,
                                     struct gl_shader_program *prog)
 {
-   STATIC_ASSERT(MESA_SHADER_TYPES == 3);
-   static const char *shader_names[MESA_SHADER_TYPES] = {
-      "vertex", "geometry", "fragment"
-   };
-   const unsigned max_atomic_counters[MESA_SHADER_TYPES] = {
-      ctx->Const.VertexProgram.MaxAtomicCounters,
-      ctx->Const.GeometryProgram.MaxAtomicCounters,
-      ctx->Const.FragmentProgram.MaxAtomicCounters
-   };
-   const unsigned max_atomic_buffers[MESA_SHADER_TYPES] = {
-      ctx->Const.VertexProgram.MaxAtomicBuffers,
-      ctx->Const.GeometryProgram.MaxAtomicBuffers,
-      ctx->Const.FragmentProgram.MaxAtomicBuffers
-   };
    unsigned num_buffers;
    active_atomic_buffer *const abs =
       find_active_atomic_counters(ctx, prog, &num_buffers);
-   unsigned atomic_counters[MESA_SHADER_TYPES] = {};
-   unsigned atomic_buffers[MESA_SHADER_TYPES] = {};
+   unsigned atomic_counters[MESA_SHADER_STAGES] = {};
+   unsigned atomic_buffers[MESA_SHADER_STAGES] = {};
    unsigned total_atomic_counters = 0;
    unsigned total_atomic_buffers = 0;
 
@@ -244,7 +243,7 @@ link_check_atomic_counter_resources(struct gl_context *ctx,
       if (abs[i].size == 0)
          continue;
 
-      for (unsigned j = 0; j < MESA_SHADER_TYPES; ++j) {
+      for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j) {
          const unsigned n = abs[i].stage_references[j];
 
          if (n) {
@@ -257,14 +256,14 @@ link_check_atomic_counter_resources(struct gl_context *ctx,
    }
 
    /* Check that they are within the supported limits. */
-   for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
-      if (atomic_counters[i] > max_atomic_counters[i])
+   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
+      if (atomic_counters[i] > ctx->Const.Program[i].MaxAtomicCounters)
          linker_error(prog, "Too many %s shader atomic counters",
-                      shader_names[i]);
+                      _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",
-                      shader_names[i]);
+                      _mesa_shader_stage_to_string(i));
    }
 
    if (total_atomic_counters > ctx->Const.MaxCombinedAtomicCounters)