• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2018 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 DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "linker_util.h"
26 #include "gl_nir_linker.h"
27 #include "main/consts_exts.h"
28 #include "main/shader_types.h"
29 #include "util/glheader.h"
30 
31 /**
32  * This file do the common link for GLSL atomic counter uniforms, using NIR,
33  * instead of IR as the counter-part glsl/link_uniforms.cpp
34  */
35 
36 struct active_atomic_counter_uniform {
37    unsigned loc;
38    nir_variable *var;
39 };
40 
41 struct active_atomic_buffer {
42    struct active_atomic_counter_uniform *uniforms;
43    unsigned num_uniforms;
44    unsigned uniform_buffer_size;
45    unsigned stage_counter_references[MESA_SHADER_STAGES];
46    unsigned size;
47 };
48 
49 static void
add_atomic_counter(const void * ctx,struct active_atomic_buffer * buffer,unsigned uniform_loc,nir_variable * var)50 add_atomic_counter(const void *ctx,
51                    struct active_atomic_buffer *buffer,
52                    unsigned uniform_loc,
53                    nir_variable *var)
54 {
55    if (buffer->num_uniforms >= buffer->uniform_buffer_size) {
56       if (buffer->uniform_buffer_size == 0)
57          buffer->uniform_buffer_size = 1;
58       else
59          buffer->uniform_buffer_size *= 2;
60       buffer->uniforms = reralloc(ctx,
61                                   buffer->uniforms,
62                                   struct active_atomic_counter_uniform,
63                                   buffer->uniform_buffer_size);
64    }
65 
66    struct active_atomic_counter_uniform *uniform =
67       buffer->uniforms + buffer->num_uniforms;
68    uniform->loc = uniform_loc;
69    uniform->var = var;
70    buffer->num_uniforms++;
71 }
72 
73 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)74 process_atomic_variable(const struct glsl_type *t,
75                         struct gl_shader_program *prog,
76                         unsigned *uniform_loc,
77                         nir_variable *var,
78                         struct active_atomic_buffer *buffers,
79                         unsigned *num_buffers,
80                         int *offset,
81                         unsigned shader_stage)
82 {
83    /* FIXME: Arrays of arrays get counted separately. For example:
84     * x1[3][3][2] = 9 uniforms, 18 atomic counters
85     * x2[3][2]    = 3 uniforms, 6 atomic counters
86     * x3[2]       = 1 uniform, 2 atomic counters
87     *
88     * However this code marks all the counters as active even when they
89     * might not be used.
90     */
91    if (glsl_type_is_array(t) &&
92        glsl_type_is_array(glsl_get_array_element(t))) {
93       for (unsigned i = 0; i < glsl_get_length(t); i++) {
94          process_atomic_variable(glsl_get_array_element(t),
95                                  prog,
96                                  uniform_loc,
97                                  var,
98                                  buffers, num_buffers,
99                                  offset,
100                                  shader_stage);
101       }
102    } else {
103       struct active_atomic_buffer *buf = buffers + var->data.binding;
104       struct gl_uniform_storage *const storage =
105          &prog->data->UniformStorage[*uniform_loc];
106 
107       /* If this is the first time the buffer is used, increment
108        * the counter of buffers used.
109        */
110       if (buf->size == 0)
111          (*num_buffers)++;
112 
113       add_atomic_counter(buffers, /* ctx */
114                          buf,
115                          *uniform_loc,
116                          var);
117 
118       /* When checking for atomic counters we should count every member in
119        * an array as an atomic counter reference.
120        */
121       if (glsl_type_is_array(t))
122          buf->stage_counter_references[shader_stage] += glsl_get_length(t);
123       else
124          buf->stage_counter_references[shader_stage]++;
125       buf->size = MAX2(buf->size, *offset + glsl_atomic_size(t));
126 
127       storage->offset = *offset;
128       *offset += glsl_atomic_size(t);
129 
130       (*uniform_loc)++;
131    }
132 }
133 
134 static struct active_atomic_buffer *
find_active_atomic_counters(const struct gl_constants * consts,struct gl_shader_program * prog,unsigned * num_buffers)135 find_active_atomic_counters(const struct gl_constants *consts,
136                             struct gl_shader_program *prog,
137                             unsigned *num_buffers)
138 {
139    struct active_atomic_buffer *buffers =
140       rzalloc_array(NULL, /* ctx */
141                     struct active_atomic_buffer,
142                     consts->MaxAtomicBufferBindings);
143    *num_buffers = 0;
144 
145    for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
146       struct gl_linked_shader *sh = prog->_LinkedShaders[i];
147       if (sh == NULL)
148          continue;
149 
150       nir_shader *nir = sh->Program->nir;
151 
152       nir_foreach_uniform_variable(var, nir) {
153          if (!glsl_contains_atomic(var->type))
154             continue;
155 
156          int offset = var->data.offset;
157          unsigned uniform_loc = var->data.location;
158 
159          process_atomic_variable(var->type,
160                                  prog,
161                                  &uniform_loc,
162                                  var,
163                                  buffers,
164                                  num_buffers,
165                                  &offset,
166                                  i);
167       }
168    }
169 
170    return buffers;
171 }
172 
173 static bool
check_atomic_counters_overlap(const nir_variable * x,const nir_variable * y)174 check_atomic_counters_overlap(const nir_variable *x, const nir_variable *y)
175 {
176    return ((x->data.offset >= y->data.offset &&
177             x->data.offset < y->data.offset + glsl_atomic_size(y->type)) ||
178            (y->data.offset >= x->data.offset &&
179             y->data.offset < x->data.offset + glsl_atomic_size(x->type)));
180 }
181 
182 static int
cmp_active_counter_offsets(const void * a,const void * b)183 cmp_active_counter_offsets(const void *a, const void *b)
184 {
185    const struct active_atomic_counter_uniform *const first =
186       (struct active_atomic_counter_uniform *) a;
187    const struct active_atomic_counter_uniform *const second =
188       (struct active_atomic_counter_uniform *) b;
189 
190    return first->var->data.offset - second->var->data.offset;
191 }
192 
193 void
gl_nir_link_assign_atomic_counter_resources(const struct gl_constants * consts,struct gl_shader_program * prog)194 gl_nir_link_assign_atomic_counter_resources(const struct gl_constants *consts,
195                                             struct gl_shader_program *prog)
196 {
197    unsigned num_buffers;
198    unsigned num_atomic_buffers[MESA_SHADER_STAGES] = {0};
199    struct active_atomic_buffer *abs =
200       find_active_atomic_counters(consts, prog, &num_buffers);
201 
202    prog->data->AtomicBuffers =
203       rzalloc_array(prog->data, struct gl_active_atomic_buffer, num_buffers);
204    prog->data->NumAtomicBuffers = num_buffers;
205 
206    unsigned buffer_idx = 0;
207    for (unsigned binding = 0;
208         binding < consts->MaxAtomicBufferBindings;
209         binding++) {
210 
211       /* If the binding was not used, skip.
212        */
213       if (abs[binding].size == 0)
214          continue;
215 
216       struct active_atomic_buffer *ab = abs + binding;
217       struct gl_active_atomic_buffer *mab =
218          prog->data->AtomicBuffers + buffer_idx;
219 
220       /* Assign buffer-specific fields. */
221       mab->Binding = binding;
222       mab->MinimumSize = ab->size;
223       mab->Uniforms = rzalloc_array(prog->data->AtomicBuffers, GLuint,
224                                     ab->num_uniforms);
225       mab->NumUniforms = ab->num_uniforms;
226 
227       /* Assign counter-specific fields. */
228       for (unsigned j = 0; j < ab->num_uniforms; j++) {
229          nir_variable *var = ab->uniforms[j].var;
230          struct gl_uniform_storage *storage =
231             &prog->data->UniformStorage[ab->uniforms[j].loc];
232 
233          mab->Uniforms[j] = ab->uniforms[j].loc;
234 
235          storage->atomic_buffer_index = buffer_idx;
236          storage->offset = var->data.offset;
237          if (glsl_type_is_array(var->type)) {
238             const struct glsl_type *without_array =
239                glsl_without_array(var->type);
240             storage->array_stride = glsl_atomic_size(without_array);
241          } else {
242             storage->array_stride = 0;
243          }
244          if (!glsl_type_is_matrix(var->type))
245             storage->matrix_stride = 0;
246       }
247 
248       /* Assign stage-specific fields. */
249       for (unsigned stage = 0; stage < MESA_SHADER_STAGES; ++stage) {
250          if (ab->stage_counter_references[stage]) {
251             mab->StageReferences[stage] = GL_TRUE;
252             num_atomic_buffers[stage]++;
253          } else {
254             mab->StageReferences[stage] = GL_FALSE;
255          }
256       }
257 
258       buffer_idx++;
259    }
260 
261    /* Store a list pointers to atomic buffers per stage and store the index
262     * to the intra-stage buffer list in uniform storage.
263     */
264    for (unsigned stage = 0; stage < MESA_SHADER_STAGES; ++stage) {
265       if (prog->_LinkedShaders[stage] == NULL ||
266           num_atomic_buffers[stage] <= 0)
267          continue;
268 
269       struct gl_program *gl_prog = prog->_LinkedShaders[stage]->Program;
270       gl_prog->info.num_abos = num_atomic_buffers[stage];
271       gl_prog->sh.AtomicBuffers =
272          rzalloc_array(gl_prog,
273                        struct gl_active_atomic_buffer *,
274                        num_atomic_buffers[stage]);
275 
276       gl_prog->nir->info.num_abos = num_atomic_buffers[stage];
277 
278       unsigned intra_stage_idx = 0;
279       for (unsigned i = 0; i < num_buffers; i++) {
280          struct gl_active_atomic_buffer *atomic_buffer =
281             &prog->data->AtomicBuffers[i];
282          if (!atomic_buffer->StageReferences[stage])
283             continue;
284 
285          gl_prog->sh.AtomicBuffers[intra_stage_idx] = atomic_buffer;
286 
287          for (unsigned u = 0; u < atomic_buffer->NumUniforms; u++) {
288             GLuint uniform_loc = atomic_buffer->Uniforms[u];
289             struct gl_opaque_uniform_index *opaque =
290                prog->data->UniformStorage[uniform_loc].opaque + stage;
291             opaque->index = intra_stage_idx;
292             opaque->active = true;
293          }
294 
295          intra_stage_idx++;
296       }
297    }
298 
299    assert(buffer_idx == num_buffers);
300 
301    ralloc_free(abs);
302 }
303 
304 void
gl_nir_link_check_atomic_counter_resources(const struct gl_constants * consts,struct gl_shader_program * prog)305 gl_nir_link_check_atomic_counter_resources(const struct gl_constants *consts,
306                                            struct gl_shader_program *prog)
307 {
308    unsigned num_buffers;
309    struct active_atomic_buffer *abs =
310       find_active_atomic_counters(consts, prog, &num_buffers);
311    unsigned atomic_counters[MESA_SHADER_STAGES] = {0};
312    unsigned atomic_buffers[MESA_SHADER_STAGES] = {0};
313    unsigned total_atomic_counters = 0;
314    unsigned total_atomic_buffers = 0;
315 
316    /* Sum the required resources.  Note that this counts buffers and
317     * counters referenced by several shader stages multiple times
318     * against the combined limit -- That's the behavior the spec
319     * requires.
320     */
321    for (unsigned i = 0; i < consts->MaxAtomicBufferBindings; i++) {
322       if (abs[i].size == 0)
323          continue;
324 
325       qsort(abs[i].uniforms, abs[i].num_uniforms,
326             sizeof(struct active_atomic_counter_uniform),
327             cmp_active_counter_offsets);
328 
329       for (unsigned j = 1; j < abs[i].num_uniforms; j++) {
330          /* If an overlapping counter found, it must be a reference to the
331           * same counter from a different shader stage.
332           */
333          if (check_atomic_counters_overlap(abs[i].uniforms[j-1].var,
334                                            abs[i].uniforms[j].var)
335              && strcmp(abs[i].uniforms[j-1].var->name,
336                        abs[i].uniforms[j].var->name) != 0) {
337             linker_error(prog, "Atomic counter %s declared at offset %d "
338                          "which is already in use.",
339                          abs[i].uniforms[j].var->name,
340                          abs[i].uniforms[j].var->data.offset);
341          }
342       }
343 
344       for (unsigned j = 0; j < MESA_SHADER_STAGES; ++j) {
345          const unsigned n = abs[i].stage_counter_references[j];
346 
347          if (n) {
348             atomic_counters[j] += n;
349             total_atomic_counters += n;
350             atomic_buffers[j]++;
351             total_atomic_buffers++;
352          }
353       }
354    }
355 
356    /* Check that they are within the supported limits. */
357    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
358       if (atomic_counters[i] > consts->Program[i].MaxAtomicCounters)
359          linker_error(prog, "Too many %s shader atomic counters",
360                       _mesa_shader_stage_to_string(i));
361 
362       if (atomic_buffers[i] > consts->Program[i].MaxAtomicBuffers)
363          linker_error(prog, "Too many %s shader atomic counter buffers",
364                       _mesa_shader_stage_to_string(i));
365    }
366 
367    if (total_atomic_counters > consts->MaxCombinedAtomicCounters)
368       linker_error(prog, "Too many combined atomic counters");
369 
370    if (total_atomic_buffers > consts->MaxCombinedAtomicBuffers)
371       linker_error(prog, "Too many combined atomic buffers");
372 
373    ralloc_free(abs);
374 }
375