1/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include "glsl_parser_extras.h"
25#include "ir.h"
26#include "ir_uniform.h"
27#include "linker.h"
28#include "main/macros.h"
29
30namespace {
31   /*
32    * Atomic counter uniform as seen by the program.
33    */
34   struct active_atomic_counter_uniform {
35      unsigned uniform_loc;
36      ir_variable *var;
37   };
38
39   /*
40    * Atomic counter buffer referenced by the program.  There is a one
41    * to one correspondence between these and the objects that can be
42    * queried using glGetActiveAtomicCounterBufferiv().
43    */
44   struct active_atomic_buffer {
45      active_atomic_buffer()
46         : uniforms(0), num_uniforms(0), stage_counter_references(), size(0)
47      {}
48
49      ~active_atomic_buffer()
50      {
51         free(uniforms);
52      }
53
54      void push_back(unsigned uniform_loc, ir_variable *var)
55      {
56         active_atomic_counter_uniform *new_uniforms;
57
58         new_uniforms = (active_atomic_counter_uniform *)
59            realloc(uniforms, sizeof(active_atomic_counter_uniform) *
60                    (num_uniforms + 1));
61
62         if (new_uniforms == NULL) {
63            _mesa_error_no_memory(__func__);
64            return;
65         }
66
67         uniforms = new_uniforms;
68         uniforms[num_uniforms].uniform_loc = uniform_loc;
69         uniforms[num_uniforms].var = var;
70         num_uniforms++;
71      }
72
73      active_atomic_counter_uniform *uniforms;
74      unsigned num_uniforms;
75      unsigned stage_counter_references[MESA_SHADER_STAGES];
76      unsigned size;
77   };
78
79   int
80   cmp_actives(const void *a, const void *b)
81   {
82      const active_atomic_counter_uniform *const first = (active_atomic_counter_uniform *) a;
83      const active_atomic_counter_uniform *const second = (active_atomic_counter_uniform *) b;
84
85      return int(first->var->data.offset) - int(second->var->data.offset);
86   }
87
88   bool
89   check_atomic_counters_overlap(const ir_variable *x, const ir_variable *y)
90   {
91      return ((x->data.offset >= y->data.offset &&
92               x->data.offset < y->data.offset + y->type->atomic_size()) ||
93              (y->data.offset >= x->data.offset &&
94               y->data.offset < x->data.offset + x->type->atomic_size()));
95   }
96
97   void
98   process_atomic_variable(const glsl_type *t, struct gl_shader_program *prog,
99                           unsigned *uniform_loc, ir_variable *var,
100                           active_atomic_buffer *const buffers,
101                           unsigned *num_buffers, int *offset,
102                           const unsigned shader_stage)
103   {
104      /* FIXME: Arrays of arrays get counted separately. For example:
105       * x1[3][3][2] = 9 uniforms, 18 atomic counters
106       * x2[3][2]    = 3 uniforms, 6 atomic counters
107       * x3[2]       = 1 uniform, 2 atomic counters
108       *
109       * However this code marks all the counters as active even when they
110       * might not be used.
111       */
112      if (t->is_array() && t->fields.array->is_array()) {
113         for (unsigned i = 0; i < t->length; i++) {
114            process_atomic_variable(t->fields.array, prog, uniform_loc,
115                                    var, buffers, num_buffers, offset,
116                                    shader_stage);
117         }
118      } else {
119         active_atomic_buffer *buf = &buffers[var->data.binding];
120         gl_uniform_storage *const storage =
121            &prog->data->UniformStorage[*uniform_loc];
122
123         /* If this is the first time the buffer is used, increment
124          * the counter of buffers used.
125          */
126         if (buf->size == 0)
127            (*num_buffers)++;
128
129         buf->push_back(*uniform_loc, var);
130
131         /* When checking for atomic counters we should count every member in
132          * an array as an atomic counter reference.
133          */
134         if (t->is_array())
135            buf->stage_counter_references[shader_stage] += t->length;
136         else
137            buf->stage_counter_references[shader_stage]++;
138         buf->size = MAX2(buf->size, *offset + t->atomic_size());
139
140         storage->offset = *offset;
141         *offset += t->atomic_size();
142
143         (*uniform_loc)++;
144      }
145   }
146
147   active_atomic_buffer *
148   find_active_atomic_counters(struct gl_context *ctx,
149                               struct gl_shader_program *prog,
150                               unsigned *num_buffers)
151   {
152      active_atomic_buffer *const buffers =
153         new active_atomic_buffer[ctx->Const.MaxAtomicBufferBindings];
154
155      *num_buffers = 0;
156
157      for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
158         struct gl_linked_shader *sh = prog->_LinkedShaders[i];
159         if (sh == NULL)
160            continue;
161
162         foreach_in_list(ir_instruction, node, sh->ir) {
163            ir_variable *var = node->as_variable();
164
165            if (var && var->type->contains_atomic()) {
166               int offset = var->data.offset;
167               unsigned uniform_loc = var->data.location;
168               process_atomic_variable(var->type, prog, &uniform_loc,
169                                       var, buffers, num_buffers, &offset, i);
170            }
171         }
172      }
173
174      for (unsigned i = 0; i < ctx->Const.MaxAtomicBufferBindings; i++) {
175         if (buffers[i].size == 0)
176            continue;
177
178         qsort(buffers[i].uniforms, buffers[i].num_uniforms,
179               sizeof(active_atomic_counter_uniform),
180               cmp_actives);
181
182         for (unsigned j = 1; j < buffers[i].num_uniforms; j++) {
183            /* If an overlapping counter found, it must be a reference to the
184             * same counter from a different shader stage.
185             */
186            if (check_atomic_counters_overlap(buffers[i].uniforms[j-1].var,
187                                              buffers[i].uniforms[j].var)
188                && strcmp(buffers[i].uniforms[j-1].var->name,
189                          buffers[i].uniforms[j].var->name) != 0) {
190               linker_error(prog, "Atomic counter %s declared at offset %d "
191                            "which is already in use.",
192                            buffers[i].uniforms[j].var->name,
193                            buffers[i].uniforms[j].var->data.offset);
194            }
195         }
196      }
197      return buffers;
198   }
199}
200
201void
202link_assign_atomic_counter_resources(struct gl_context *ctx,
203                                     struct gl_shader_program *prog)
204{
205   unsigned num_buffers;
206   unsigned num_atomic_buffers[MESA_SHADER_STAGES] = {};
207   active_atomic_buffer *abs =
208      find_active_atomic_counters(ctx, prog, &num_buffers);
209
210   prog->data->AtomicBuffers = rzalloc_array(prog, gl_active_atomic_buffer,
211                                             num_buffers);
212   prog->data->NumAtomicBuffers = num_buffers;
213
214   unsigned i = 0;
215   for (unsigned binding = 0;
216        binding < ctx->Const.MaxAtomicBufferBindings;
217        binding++) {
218
219      /* If the binding was not used, skip.
220       */
221      if (abs[binding].size == 0)
222         continue;
223
224      active_atomic_buffer &ab = abs[binding];
225      gl_active_atomic_buffer &mab = prog->data->AtomicBuffers[i];
226
227      /* Assign buffer-specific fields. */
228      mab.Binding = binding;
229      mab.MinimumSize = ab.size;
230      mab.Uniforms = rzalloc_array(prog->data->AtomicBuffers, GLuint,
231                                   ab.num_uniforms);
232      mab.NumUniforms = ab.num_uniforms;
233
234      /* Assign counter-specific fields. */
235      for (unsigned j = 0; j < ab.num_uniforms; j++) {
236         ir_variable *const var = ab.uniforms[j].var;
237         gl_uniform_storage *const storage =
238            &prog->data->UniformStorage[ab.uniforms[j].uniform_loc];
239
240         mab.Uniforms[j] = ab.uniforms[j].uniform_loc;
241         if (!var->data.explicit_binding)
242            var->data.binding = i;
243
244         storage->atomic_buffer_index = i;
245         storage->offset = var->data.offset;
246         storage->array_stride = (var->type->is_array() ?
247                                  var->type->without_array()->atomic_size() : 0);
248         if (!var->type->is_matrix())
249            storage->matrix_stride = 0;
250      }
251
252      /* Assign stage-specific fields. */
253      for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j) {
254         if (ab.stage_counter_references[j]) {
255            mab.StageReferences[j] = GL_TRUE;
256            num_atomic_buffers[j]++;
257         } else {
258            mab.StageReferences[j] = GL_FALSE;
259         }
260      }
261
262      i++;
263   }
264
265   /* Store a list pointers to atomic buffers per stage and store the index
266    * to the intra-stage buffer list in uniform storage.
267    */
268   for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j) {
269      if (prog->_LinkedShaders[j] && num_atomic_buffers[j] > 0) {
270         struct gl_program *gl_prog = prog->_LinkedShaders[j]->Program;
271         gl_prog->info.num_abos = num_atomic_buffers[j];
272         gl_prog->sh.AtomicBuffers =
273            rzalloc_array(prog, gl_active_atomic_buffer *,
274                          num_atomic_buffers[j]);
275
276         unsigned intra_stage_idx = 0;
277         for (unsigned i = 0; i < num_buffers; i++) {
278            struct gl_active_atomic_buffer *atomic_buffer =
279               &prog->data->AtomicBuffers[i];
280            if (atomic_buffer->StageReferences[j]) {
281               gl_prog->sh.AtomicBuffers[intra_stage_idx] = atomic_buffer;
282
283               for (unsigned u = 0; u < atomic_buffer->NumUniforms; u++) {
284                  prog->data->UniformStorage[atomic_buffer->Uniforms[u]].opaque[j].index =
285                     intra_stage_idx;
286                  prog->data->UniformStorage[atomic_buffer->Uniforms[u]].opaque[j].active =
287                     true;
288               }
289
290               intra_stage_idx++;
291            }
292         }
293      }
294   }
295
296   delete [] abs;
297   assert(i == num_buffers);
298}
299
300void
301link_check_atomic_counter_resources(struct gl_context *ctx,
302                                    struct gl_shader_program *prog)
303{
304   unsigned num_buffers;
305   active_atomic_buffer *const abs =
306      find_active_atomic_counters(ctx, prog, &num_buffers);
307   unsigned atomic_counters[MESA_SHADER_STAGES] = {};
308   unsigned atomic_buffers[MESA_SHADER_STAGES] = {};
309   unsigned total_atomic_counters = 0;
310   unsigned total_atomic_buffers = 0;
311
312   /* Sum the required resources.  Note that this counts buffers and
313    * counters referenced by several shader stages multiple times
314    * against the combined limit -- That's the behavior the spec
315    * requires.
316    */
317   for (unsigned i = 0; i < ctx->Const.MaxAtomicBufferBindings; i++) {
318      if (abs[i].size == 0)
319         continue;
320
321      for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j) {
322         const unsigned n = abs[i].stage_counter_references[j];
323
324         if (n) {
325            atomic_counters[j] += n;
326            total_atomic_counters += n;
327            atomic_buffers[j]++;
328            total_atomic_buffers++;
329         }
330      }
331   }
332
333   /* Check that they are within the supported limits. */
334   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
335      if (atomic_counters[i] > ctx->Const.Program[i].MaxAtomicCounters)
336         linker_error(prog, "Too many %s shader atomic counters",
337                      _mesa_shader_stage_to_string(i));
338
339      if (atomic_buffers[i] > ctx->Const.Program[i].MaxAtomicBuffers)
340         linker_error(prog, "Too many %s shader atomic counter buffers",
341                      _mesa_shader_stage_to_string(i));
342   }
343
344   if (total_atomic_counters > ctx->Const.MaxCombinedAtomicCounters)
345      linker_error(prog, "Too many combined atomic counters");
346
347   if (total_atomic_buffers > ctx->Const.MaxCombinedAtomicBuffers)
348      linker_error(prog, "Too many combined atomic buffers");
349
350   delete [] abs;
351}
352