1/*
2 * Copyright © 2012 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 "ir.h"
25#include "linker.h"
26#include "ir_uniform.h"
27#include "link_uniform_block_active_visitor.h"
28#include "util/hash_table.h"
29#include "program.h"
30#include "main/errors.h"
31#include "main/shader_types.h"
32#include "main/consts_exts.h"
33
34namespace {
35
36class ubo_visitor : public program_resource_visitor {
37public:
38   ubo_visitor(void *mem_ctx, gl_uniform_buffer_variable *variables,
39               unsigned num_variables, struct gl_shader_program *prog,
40               bool use_std430_as_default)
41      : index(0), offset(0), buffer_size(0), variables(variables),
42        num_variables(num_variables), mem_ctx(mem_ctx),
43        is_array_instance(false), prog(prog),
44        use_std430_as_default(use_std430_as_default)
45   {
46      /* empty */
47   }
48
49   void process(const glsl_type *type, const char *name)
50   {
51      this->offset = 0;
52      this->buffer_size = 0;
53      this->is_array_instance = strchr(name, ']') != NULL;
54      this->program_resource_visitor::process(type, name,
55                                              use_std430_as_default);
56   }
57
58   unsigned index;
59   unsigned offset;
60   unsigned buffer_size;
61   gl_uniform_buffer_variable *variables;
62   unsigned num_variables;
63   void *mem_ctx;
64   bool is_array_instance;
65   struct gl_shader_program *prog;
66
67private:
68   virtual void enter_record(const glsl_type *type, const char *,
69                             bool row_major,
70                             const enum glsl_interface_packing packing)
71   {
72      assert(type->is_struct());
73      if (packing == GLSL_INTERFACE_PACKING_STD430)
74         this->offset = glsl_align(
75            this->offset, type->std430_base_alignment(row_major));
76      else
77         this->offset = glsl_align(
78            this->offset, type->std140_base_alignment(row_major));
79   }
80
81   virtual void leave_record(const glsl_type *type, const char *,
82                             bool row_major,
83                             const enum glsl_interface_packing packing)
84   {
85      assert(type->is_struct());
86
87      /* If this is the last field of a structure, apply rule #9.  The
88       * ARB_uniform_buffer_object spec says:
89       *
90       *    The structure may have padding at the end; the base offset of the
91       *    member following the sub-structure is rounded up to the next
92       *    multiple of the base alignment of the structure.
93       */
94      if (packing == GLSL_INTERFACE_PACKING_STD430)
95         this->offset = glsl_align(
96            this->offset, type->std430_base_alignment(row_major));
97      else
98         this->offset = glsl_align(
99            this->offset, type->std140_base_alignment(row_major));
100   }
101
102   virtual void set_buffer_offset(unsigned offset)
103   {
104      this->offset = offset;
105   }
106
107   virtual void visit_field(const glsl_type *type, const char *name,
108                            bool row_major, const glsl_type *,
109                            const enum glsl_interface_packing packing,
110                            bool last_field)
111   {
112      assert(this->index < this->num_variables);
113
114      gl_uniform_buffer_variable *v = &this->variables[this->index++];
115
116      v->Name = ralloc_strdup(mem_ctx, name);
117      v->Type = type;
118      v->RowMajor = type->without_array()->is_matrix() && row_major;
119
120      if (this->is_array_instance) {
121         v->IndexName = ralloc_strdup(mem_ctx, name);
122
123         char *open_bracket = strchr(v->IndexName, '[');
124         assert(open_bracket != NULL);
125
126         char *close_bracket = strchr(open_bracket, '.') - 1;
127         assert(close_bracket != NULL);
128
129         /* Length of the tail without the ']' but with the NUL.
130          */
131         unsigned len = strlen(close_bracket + 1) + 1;
132
133         memmove(open_bracket, close_bracket + 1, len);
134      } else {
135         v->IndexName = v->Name;
136      }
137
138      unsigned alignment = 0;
139      unsigned size = 0;
140
141      /* The ARB_program_interface_query spec says:
142       *
143       *    If the final member of an active shader storage block is array
144       *    with no declared size, the minimum buffer size is computed
145       *    assuming the array was declared as an array with one element.
146       *
147       * For that reason, we use the base type of the unsized array to
148       * calculate its size. We don't need to check if the unsized array is
149       * the last member of a shader storage block (that check was already
150       * done by the parser).
151       */
152      const glsl_type *type_for_size = type;
153      if (type->is_unsized_array()) {
154         if (!last_field) {
155            linker_error(prog, "unsized array `%s' definition: "
156                         "only last member of a shader storage block "
157                         "can be defined as unsized array",
158                         name);
159         }
160
161         type_for_size = type->without_array();
162      }
163
164      if (packing == GLSL_INTERFACE_PACKING_STD430) {
165         alignment = type->std430_base_alignment(v->RowMajor);
166         size = type_for_size->std430_size(v->RowMajor);
167      } else {
168         alignment = type->std140_base_alignment(v->RowMajor);
169         size = type_for_size->std140_size(v->RowMajor);
170      }
171
172      this->offset = glsl_align(this->offset, alignment);
173      v->Offset = this->offset;
174
175      this->offset += size;
176
177      /* The ARB_uniform_buffer_object spec says:
178       *
179       *    For uniform blocks laid out according to [std140] rules, the
180       *    minimum buffer object size returned by the UNIFORM_BLOCK_DATA_SIZE
181       *    query is derived by taking the offset of the last basic machine
182       *    unit consumed by the last uniform of the uniform block (including
183       *    any end-of-array or end-of-structure padding), adding one, and
184       *    rounding up to the next multiple of the base alignment required
185       *    for a vec4.
186       */
187      this->buffer_size = glsl_align(this->offset, 16);
188   }
189
190   bool use_std430_as_default;
191};
192
193class count_block_size : public program_resource_visitor {
194public:
195   count_block_size() : num_active_uniforms(0)
196   {
197      /* empty */
198   }
199
200   unsigned num_active_uniforms;
201
202private:
203   virtual void visit_field(const glsl_type * /* type */,
204                            const char * /* name */,
205                            bool /* row_major */,
206                            const glsl_type * /* record_type */,
207                            const enum glsl_interface_packing,
208                            bool /* last_field */)
209   {
210      this->num_active_uniforms++;
211   }
212};
213
214} /* anonymous namespace */
215
216struct block {
217   const glsl_type *type;
218   bool has_instance_name;
219};
220
221static void process_block_array_leaf(const char *name, gl_uniform_block *blocks,
222                                     ubo_visitor *parcel,
223                                     gl_uniform_buffer_variable *variables,
224                                     const struct link_uniform_block_active *const b,
225                                     unsigned *block_index,
226                                     unsigned binding_offset,
227                                     unsigned linearized_index,
228                                     const struct gl_constants *consts,
229                                     struct gl_shader_program *prog);
230
231/**
232 *
233 * \param first_index Value of \c block_index for the first element of the
234 *                    array.
235 */
236static void
237process_block_array(struct uniform_block_array_elements *ub_array, char **name,
238                    size_t name_length, gl_uniform_block *blocks,
239                    ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
240                    const struct link_uniform_block_active *const b,
241                    unsigned *block_index, unsigned binding_offset,
242                    const struct gl_constants *consts,
243                    struct gl_shader_program *prog,
244                    unsigned first_index)
245{
246   for (unsigned j = 0; j < ub_array->num_array_elements; j++) {
247      size_t new_length = name_length;
248
249      unsigned int element_idx = ub_array->array_elements[j];
250      /* Append the subscript to the current variable name */
251      ralloc_asprintf_rewrite_tail(name, &new_length, "[%u]", element_idx);
252
253      if (ub_array->array) {
254         unsigned binding_stride = binding_offset + (element_idx *
255                                   ub_array->array->aoa_size);
256         process_block_array(ub_array->array, name, new_length, blocks,
257                             parcel, variables, b, block_index,
258                             binding_stride, consts, prog, first_index);
259      } else {
260         process_block_array_leaf(*name, blocks,
261                                  parcel, variables, b, block_index,
262                                  binding_offset + element_idx,
263                                  *block_index - first_index, consts, prog);
264      }
265   }
266}
267
268static void
269process_block_array_leaf(const char *name,
270                         gl_uniform_block *blocks,
271                         ubo_visitor *parcel, gl_uniform_buffer_variable *variables,
272                         const struct link_uniform_block_active *const b,
273                         unsigned *block_index, unsigned binding_offset,
274                         unsigned linearized_index,
275                         const struct gl_constants *consts,
276                         struct gl_shader_program *prog)
277{
278   unsigned i = *block_index;
279   const glsl_type *type =  b->type->without_array();
280
281   blocks[i].name.string = ralloc_strdup(blocks, name);
282   resource_name_updated(&blocks[i].name);
283   blocks[i].Uniforms = &variables[(*parcel).index];
284
285   /* The ARB_shading_language_420pack spec says:
286    *
287    *    If the binding identifier is used with a uniform block instanced as
288    *    an array then the first element of the array takes the specified
289    *    block binding and each subsequent element takes the next consecutive
290    *    uniform block binding point.
291    */
292   blocks[i].Binding = (b->has_binding) ? b->binding + binding_offset : 0;
293
294   blocks[i].UniformBufferSize = 0;
295   blocks[i]._Packing = glsl_interface_packing(type->interface_packing);
296   blocks[i]._RowMajor = type->get_interface_row_major();
297   blocks[i].linearized_array_index = linearized_index;
298
299   parcel->process(type, b->has_instance_name ? blocks[i].name.string : "");
300
301   blocks[i].UniformBufferSize = parcel->buffer_size;
302
303   /* Check SSBO size is lower than maximum supported size for SSBO */
304   if (b->is_shader_storage &&
305       parcel->buffer_size > consts->MaxShaderStorageBlockSize) {
306      linker_error(prog, "shader storage block `%s' has size %d, "
307                   "which is larger than the maximum allowed (%d)",
308                   b->type->name,
309                   parcel->buffer_size,
310                   consts->MaxShaderStorageBlockSize);
311   }
312   blocks[i].NumUniforms =
313      (unsigned)(ptrdiff_t)(&variables[parcel->index] - blocks[i].Uniforms);
314
315   *block_index = *block_index + 1;
316}
317
318/* This function resizes the array types of the block so that later we can use
319 * this new size to correctly calculate the offest for indirect indexing.
320 */
321static const glsl_type *
322resize_block_array(const glsl_type *type,
323                   struct uniform_block_array_elements *ub_array)
324{
325   if (type->is_array()) {
326      struct uniform_block_array_elements *child_array =
327         type->fields.array->is_array() ? ub_array->array : NULL;
328      const glsl_type *new_child_type =
329         resize_block_array(type->fields.array, child_array);
330
331      const glsl_type *new_type =
332         glsl_type::get_array_instance(new_child_type,
333                                       ub_array->num_array_elements);
334      ub_array->ir->array->type = new_type;
335      return new_type;
336   } else {
337      return type;
338   }
339}
340
341static void
342create_buffer_blocks(void *mem_ctx, const struct gl_constants *consts,
343                     struct gl_shader_program *prog,
344                     struct gl_uniform_block **out_blks, unsigned num_blocks,
345                     struct hash_table *block_hash, unsigned num_variables,
346                     bool create_ubo_blocks)
347{
348   if (num_blocks == 0) {
349      assert(num_variables == 0);
350      return;
351   }
352
353   assert(num_variables != 0);
354
355   /* Allocate storage to hold all of the information related to uniform
356    * blocks that can be queried through the API.
357    */
358   struct gl_uniform_block *blocks =
359      rzalloc_array(mem_ctx, gl_uniform_block, num_blocks);
360   gl_uniform_buffer_variable *variables =
361      ralloc_array(blocks, gl_uniform_buffer_variable, num_variables);
362
363   /* Add each variable from each uniform block to the API tracking
364    * structures.
365    */
366   ubo_visitor parcel(blocks, variables, num_variables, prog,
367                      consts->UseSTD430AsDefaultPacking);
368
369   unsigned i = 0;
370   hash_table_foreach (block_hash, entry) {
371      const struct link_uniform_block_active *const b =
372         (const struct link_uniform_block_active *) entry->data;
373      const glsl_type *block_type = b->type;
374
375      if ((create_ubo_blocks && !b->is_shader_storage) ||
376          (!create_ubo_blocks && b->is_shader_storage)) {
377
378         if (b->array != NULL) {
379            char *name = ralloc_strdup(NULL,
380                                       block_type->without_array()->name);
381            size_t name_length = strlen(name);
382
383            assert(b->has_instance_name);
384            process_block_array(b->array, &name, name_length, blocks, &parcel,
385                                variables, b, &i, 0, consts, prog,
386                                i);
387            ralloc_free(name);
388         } else {
389            process_block_array_leaf(block_type->name, blocks, &parcel,
390                                     variables, b, &i, 0,
391                                     0, consts, prog);
392         }
393      }
394   }
395
396   *out_blks = blocks;
397
398   assert(parcel.index == num_variables);
399}
400
401void
402link_uniform_blocks(void *mem_ctx,
403                    const struct gl_constants *consts,
404                    struct gl_shader_program *prog,
405                    struct gl_linked_shader *shader,
406                    struct gl_uniform_block **ubo_blocks,
407                    unsigned *num_ubo_blocks,
408                    struct gl_uniform_block **ssbo_blocks,
409                    unsigned *num_ssbo_blocks)
410{
411   /* This hash table will track all of the uniform blocks that have been
412    * encountered.  Since blocks with the same block-name must be the same,
413    * the hash is organized by block-name.
414    */
415   struct hash_table *block_hash =
416      _mesa_hash_table_create(mem_ctx, _mesa_hash_string,
417                              _mesa_key_string_equal);
418
419   if (block_hash == NULL) {
420      _mesa_error_no_memory(__func__);
421      linker_error(prog, "out of memory\n");
422      return;
423   }
424
425   /* Determine which uniform blocks are active. */
426   link_uniform_block_active_visitor v(mem_ctx, block_hash, prog);
427   visit_list_elements(&v, shader->ir);
428
429   /* Count the number of active uniform blocks.  Count the total number of
430    * active slots in those uniform blocks.
431    */
432   unsigned num_ubo_variables = 0;
433   unsigned num_ssbo_variables = 0;
434   count_block_size block_size;
435
436   hash_table_foreach (block_hash, entry) {
437      struct link_uniform_block_active *const b =
438         (struct link_uniform_block_active *) entry->data;
439
440      assert((b->array != NULL) == b->type->is_array());
441
442      if (b->array != NULL &&
443          (b->type->without_array()->interface_packing ==
444           GLSL_INTERFACE_PACKING_PACKED)) {
445         b->type = resize_block_array(b->type, b->array);
446         b->var->type = b->type;
447         b->var->data.max_array_access = b->type->length - 1;
448      }
449
450      block_size.num_active_uniforms = 0;
451      block_size.process(b->type->without_array(), "",
452                         consts->UseSTD430AsDefaultPacking);
453
454      if (b->array != NULL) {
455         unsigned aoa_size = b->type->arrays_of_arrays_size();
456         if (b->is_shader_storage) {
457            *num_ssbo_blocks += aoa_size;
458            num_ssbo_variables += aoa_size * block_size.num_active_uniforms;
459         } else {
460            *num_ubo_blocks += aoa_size;
461            num_ubo_variables += aoa_size * block_size.num_active_uniforms;
462         }
463      } else {
464         if (b->is_shader_storage) {
465            (*num_ssbo_blocks)++;
466            num_ssbo_variables += block_size.num_active_uniforms;
467         } else {
468            (*num_ubo_blocks)++;
469            num_ubo_variables += block_size.num_active_uniforms;
470         }
471      }
472
473   }
474
475   create_buffer_blocks(mem_ctx, consts, prog, ubo_blocks, *num_ubo_blocks,
476                        block_hash, num_ubo_variables, true);
477   create_buffer_blocks(mem_ctx, consts, prog, ssbo_blocks, *num_ssbo_blocks,
478                        block_hash, num_ssbo_variables, false);
479
480   _mesa_hash_table_destroy(block_hash, NULL);
481}
482
483static bool
484link_uniform_blocks_are_compatible(const gl_uniform_block *a,
485                                   const gl_uniform_block *b)
486{
487   assert(strcmp(a->name.string, b->name.string) == 0);
488
489   /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
490    *
491    *    Matched block names within an interface (as defined above) must match
492    *    in terms of having the same number of declarations with the same
493    *    sequence of types and the same sequence of member names, as well as
494    *    having the same member-wise layout qualification....if a matching
495    *    block is declared as an array, then the array sizes must also
496    *    match... Any mismatch will generate a link error.
497    *
498    * Arrays are not yet supported, so there is no check for that.
499    */
500   if (a->NumUniforms != b->NumUniforms)
501      return false;
502
503   if (a->_Packing != b->_Packing)
504      return false;
505
506   if (a->_RowMajor != b->_RowMajor)
507      return false;
508
509   if (a->Binding != b->Binding)
510      return false;
511
512   for (unsigned i = 0; i < a->NumUniforms; i++) {
513      if (strcmp(a->Uniforms[i].Name, b->Uniforms[i].Name) != 0)
514         return false;
515
516      if (a->Uniforms[i].Type != b->Uniforms[i].Type)
517         return false;
518
519      if (a->Uniforms[i].RowMajor != b->Uniforms[i].RowMajor)
520         return false;
521   }
522
523   return true;
524}
525
526/**
527 * Merges a uniform block into an array of uniform blocks that may or
528 * may not already contain a copy of it.
529 *
530 * Returns the index of the new block in the array.
531 */
532int
533link_cross_validate_uniform_block(void *mem_ctx,
534                                  struct gl_uniform_block **linked_blocks,
535                                  unsigned int *num_linked_blocks,
536                                  struct gl_uniform_block *new_block)
537{
538   for (unsigned int i = 0; i < *num_linked_blocks; i++) {
539      struct gl_uniform_block *old_block = &(*linked_blocks)[i];
540
541      if (strcmp(old_block->name.string, new_block->name.string) == 0)
542         return link_uniform_blocks_are_compatible(old_block, new_block)
543            ? i : -1;
544   }
545
546   *linked_blocks = reralloc(mem_ctx, *linked_blocks,
547                             struct gl_uniform_block,
548                             *num_linked_blocks + 1);
549   int linked_block_index = (*num_linked_blocks)++;
550   struct gl_uniform_block *linked_block = &(*linked_blocks)[linked_block_index];
551
552   memcpy(linked_block, new_block, sizeof(*new_block));
553   linked_block->Uniforms = ralloc_array(*linked_blocks,
554                                         struct gl_uniform_buffer_variable,
555                                         linked_block->NumUniforms);
556
557   memcpy(linked_block->Uniforms,
558          new_block->Uniforms,
559          sizeof(*linked_block->Uniforms) * linked_block->NumUniforms);
560
561   linked_block->name.string = ralloc_strdup(*linked_blocks, linked_block->name.string);
562   resource_name_updated(&linked_block->name);
563
564   for (unsigned int i = 0; i < linked_block->NumUniforms; i++) {
565      struct gl_uniform_buffer_variable *ubo_var =
566         &linked_block->Uniforms[i];
567
568      if (ubo_var->Name == ubo_var->IndexName) {
569         ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
570         ubo_var->IndexName = ubo_var->Name;
571      } else {
572         ubo_var->Name = ralloc_strdup(*linked_blocks, ubo_var->Name);
573         ubo_var->IndexName = ralloc_strdup(*linked_blocks, ubo_var->IndexName);
574      }
575   }
576
577   return linked_block_index;
578}
579