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