Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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 "main/core.h"
  25. #include "ir.h"
  26. #include "linker.h"
  27. #include "ir_uniform.h"
  28. #include "link_uniform_block_active_visitor.h"
  29. #include "util/hash_table.h"
  30. #include "program.h"
  31.  
  32. namespace {
  33.  
  34. class ubo_visitor : public program_resource_visitor {
  35. public:
  36.    ubo_visitor(void *mem_ctx, gl_uniform_buffer_variable *variables,
  37.                unsigned num_variables)
  38.       : index(0), offset(0), buffer_size(0), variables(variables),
  39.         num_variables(num_variables), mem_ctx(mem_ctx), is_array_instance(false)
  40.    {
  41.       /* empty */
  42.    }
  43.  
  44.    void process(const glsl_type *type, const char *name)
  45.    {
  46.       this->offset = 0;
  47.       this->buffer_size = 0;
  48.       this->is_array_instance = strchr(name, ']') != NULL;
  49.       this->program_resource_visitor::process(type, name);
  50.    }
  51.  
  52.    unsigned index;
  53.    unsigned offset;
  54.    unsigned buffer_size;
  55.    gl_uniform_buffer_variable *variables;
  56.    unsigned num_variables;
  57.    void *mem_ctx;
  58.    bool is_array_instance;
  59.  
  60. private:
  61.    virtual void visit_field(const glsl_type *type, const char *name,
  62.                             bool row_major)
  63.    {
  64.       (void) type;
  65.       (void) name;
  66.       (void) row_major;
  67.       assert(!"Should not get here.");
  68.    }
  69.  
  70.    virtual void enter_record(const glsl_type *type, const char *,
  71.                              bool row_major) {
  72.       assert(type->is_record());
  73.       this->offset = glsl_align(
  74.             this->offset, type->std140_base_alignment(row_major));
  75.    }
  76.  
  77.    virtual void leave_record(const glsl_type *type, const char *,
  78.                              bool row_major) {
  79.       assert(type->is_record());
  80.  
  81.       /* If this is the last field of a structure, apply rule #9.  The
  82.        * GL_ARB_uniform_buffer_object spec says:
  83.        *
  84.        *     "The structure may have padding at the end; the base offset of
  85.        *     the member following the sub-structure is rounded up to the next
  86.        *     multiple of the base alignment of the structure."
  87.        */
  88.       this->offset = glsl_align(
  89.             this->offset, type->std140_base_alignment(row_major));
  90.    }
  91.  
  92.    virtual void visit_field(const glsl_type *type, const char *name,
  93.                             bool row_major, const glsl_type *,
  94.                             bool /* last_field */)
  95.    {
  96.       assert(this->index < this->num_variables);
  97.  
  98.       gl_uniform_buffer_variable *v = &this->variables[this->index++];
  99.  
  100.       v->Name = ralloc_strdup(mem_ctx, name);
  101.       v->Type = type;
  102.       v->RowMajor = type->without_array()->is_matrix() && row_major;
  103.  
  104.       if (this->is_array_instance) {
  105.          v->IndexName = ralloc_strdup(mem_ctx, name);
  106.  
  107.          char *open_bracket = strchr(v->IndexName, '[');
  108.          assert(open_bracket != NULL);
  109.  
  110.          char *close_bracket = strchr(open_bracket, ']');
  111.          assert(close_bracket != NULL);
  112.  
  113.          /* Length of the tail without the ']' but with the NUL.
  114.           */
  115.          unsigned len = strlen(close_bracket + 1) + 1;
  116.  
  117.          memmove(open_bracket, close_bracket + 1, len);
  118.       } else {
  119.          v->IndexName = v->Name;
  120.       }
  121.  
  122.       const unsigned alignment = type->std140_base_alignment(v->RowMajor);
  123.       unsigned size = type->std140_size(v->RowMajor);
  124.  
  125.       this->offset = glsl_align(this->offset, alignment);
  126.       v->Offset = this->offset;
  127.  
  128.       this->offset += size;
  129.  
  130.       /* From the GL_ARB_uniform_buffer_object spec:
  131.        *
  132.        *     "For uniform blocks laid out according to [std140] rules, the
  133.        *      minimum buffer object size returned by the
  134.        *      UNIFORM_BLOCK_DATA_SIZE query is derived by taking the offset of
  135.        *      the last basic machine unit consumed by the last uniform of the
  136.        *      uniform block (including any end-of-array or end-of-structure
  137.        *      padding), adding one, and rounding up to the next multiple of
  138.        *      the base alignment required for a vec4."
  139.        */
  140.       this->buffer_size = glsl_align(this->offset, 16);
  141.    }
  142. };
  143.  
  144. class count_block_size : public program_resource_visitor {
  145. public:
  146.    count_block_size() : num_active_uniforms(0)
  147.    {
  148.       /* empty */
  149.    }
  150.  
  151.    unsigned num_active_uniforms;
  152.  
  153. private:
  154.    virtual void visit_field(const glsl_type *type, const char *name,
  155.                             bool row_major)
  156.    {
  157.       (void) type;
  158.       (void) name;
  159.       (void) row_major;
  160.       this->num_active_uniforms++;
  161.    }
  162. };
  163.  
  164. } /* anonymous namespace */
  165.  
  166. struct block {
  167.    const glsl_type *type;
  168.    bool has_instance_name;
  169. };
  170.  
  171. unsigned
  172. link_uniform_blocks(void *mem_ctx,
  173.                     struct gl_shader_program *prog,
  174.                     struct gl_shader **shader_list,
  175.                     unsigned num_shaders,
  176.                     struct gl_uniform_block **blocks_ret)
  177. {
  178.    /* This hash table will track all of the uniform blocks that have been
  179.     * encountered.  Since blocks with the same block-name must be the same,
  180.     * the hash is organized by block-name.
  181.     */
  182.    struct hash_table *block_hash =
  183.       _mesa_hash_table_create(mem_ctx, _mesa_key_hash_string,
  184.                               _mesa_key_string_equal);
  185.  
  186.    if (block_hash == NULL) {
  187.       _mesa_error_no_memory(__func__);
  188.       linker_error(prog, "out of memory\n");
  189.       return 0;
  190.    }
  191.  
  192.    /* Determine which uniform blocks are active.
  193.     */
  194.    link_uniform_block_active_visitor v(mem_ctx, block_hash, prog);
  195.    for (unsigned i = 0; i < num_shaders; i++) {
  196.       visit_list_elements(&v, shader_list[i]->ir);
  197.    }
  198.  
  199.    /* Count the number of active uniform blocks.  Count the total number of
  200.     * active slots in those uniform blocks.
  201.     */
  202.    unsigned num_blocks = 0;
  203.    unsigned num_variables = 0;
  204.    count_block_size block_size;
  205.    struct hash_entry *entry;
  206.  
  207.    hash_table_foreach (block_hash, entry) {
  208.       const struct link_uniform_block_active *const b =
  209.          (const struct link_uniform_block_active *) entry->data;
  210.  
  211.       const glsl_type *const block_type =
  212.          b->type->is_array() ? b->type->fields.array : b->type;
  213.  
  214.       assert((b->num_array_elements > 0) == b->type->is_array());
  215.  
  216.       block_size.num_active_uniforms = 0;
  217.       block_size.process(block_type, "");
  218.  
  219.       if (b->num_array_elements > 0) {
  220.          num_blocks += b->num_array_elements;
  221.          num_variables += b->num_array_elements
  222.             * block_size.num_active_uniforms;
  223.       } else {
  224.          num_blocks++;
  225.          num_variables += block_size.num_active_uniforms;
  226.       }
  227.  
  228.    }
  229.  
  230.    if (num_blocks == 0) {
  231.       assert(num_variables == 0);
  232.       _mesa_hash_table_destroy(block_hash, NULL);
  233.       return 0;
  234.    }
  235.  
  236.    assert(num_variables != 0);
  237.  
  238.    /* Allocate storage to hold all of the informatation related to uniform
  239.     * blocks that can be queried through the API.
  240.     */
  241.    gl_uniform_block *blocks =
  242.       ralloc_array(mem_ctx, gl_uniform_block, num_blocks);
  243.    gl_uniform_buffer_variable *variables =
  244.       ralloc_array(blocks, gl_uniform_buffer_variable, num_variables);
  245.  
  246.    /* Add each variable from each uniform block to the API tracking
  247.     * structures.
  248.     */
  249.    unsigned i = 0;
  250.    ubo_visitor parcel(blocks, variables, num_variables);
  251.  
  252.    STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_STD140)
  253.                  == unsigned(ubo_packing_std140));
  254.    STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_SHARED)
  255.                  == unsigned(ubo_packing_shared));
  256.    STATIC_ASSERT(unsigned(GLSL_INTERFACE_PACKING_PACKED)
  257.                  == unsigned(ubo_packing_packed));
  258.  
  259.  
  260.    hash_table_foreach (block_hash, entry) {
  261.       const struct link_uniform_block_active *const b =
  262.          (const struct link_uniform_block_active *) entry->data;
  263.       const glsl_type *block_type = b->type;
  264.  
  265.       if (b->num_array_elements > 0) {
  266.          const char *const name = block_type->fields.array->name;
  267.  
  268.          assert(b->has_instance_name);
  269.          for (unsigned j = 0; j < b->num_array_elements; j++) {
  270.             blocks[i].Name = ralloc_asprintf(blocks, "%s[%u]", name,
  271.                                              b->array_elements[j]);
  272.             blocks[i].Uniforms = &variables[parcel.index];
  273.  
  274.             /* The GL_ARB_shading_language_420pack spec says:
  275.              *
  276.              *     "If the binding identifier is used with a uniform block
  277.              *     instanced as an array then the first element of the array
  278.              *     takes the specified block binding and each subsequent
  279.              *     element takes the next consecutive uniform block binding
  280.              *     point."
  281.              */
  282.             blocks[i].Binding = (b->has_binding) ? b->binding + j : 0;
  283.  
  284.             blocks[i].UniformBufferSize = 0;
  285.             blocks[i]._Packing =
  286.                gl_uniform_block_packing(block_type->interface_packing);
  287.  
  288.             parcel.process(block_type->fields.array,
  289.                            blocks[i].Name);
  290.  
  291.             blocks[i].UniformBufferSize = parcel.buffer_size;
  292.  
  293.             blocks[i].NumUniforms =
  294.                (unsigned)(ptrdiff_t)(&variables[parcel.index] - blocks[i].Uniforms);
  295.  
  296.             i++;
  297.          }
  298.       } else {
  299.          blocks[i].Name = ralloc_strdup(blocks, block_type->name);
  300.          blocks[i].Uniforms = &variables[parcel.index];
  301.          blocks[i].Binding = (b->has_binding) ? b->binding : 0;
  302.          blocks[i].UniformBufferSize = 0;
  303.          blocks[i]._Packing =
  304.             gl_uniform_block_packing(block_type->interface_packing);
  305.  
  306.          parcel.process(block_type,
  307.                         b->has_instance_name ? block_type->name : "");
  308.  
  309.          blocks[i].UniformBufferSize = parcel.buffer_size;
  310.  
  311.          blocks[i].NumUniforms =
  312.             (unsigned)(ptrdiff_t)(&variables[parcel.index] - blocks[i].Uniforms);
  313.  
  314.          i++;
  315.       }
  316.    }
  317.  
  318.    assert(parcel.index == num_variables);
  319.  
  320.    _mesa_hash_table_destroy(block_hash, NULL);
  321.  
  322.    *blocks_ret = blocks;
  323.    return num_blocks;
  324. }
  325.  
  326. bool
  327. link_uniform_blocks_are_compatible(const gl_uniform_block *a,
  328.                                    const gl_uniform_block *b)
  329. {
  330.    assert(strcmp(a->Name, b->Name) == 0);
  331.  
  332.    /* Page 35 (page 42 of the PDF) in section 4.3.7 of the GLSL 1.50 spec says:
  333.     *
  334.     *     "Matched block names within an interface (as defined above) must
  335.     *     match in terms of having the same number of declarations with the
  336.     *     same sequence of types and the same sequence of member names, as
  337.     *     well as having the same member-wise layout qualification....if a
  338.     *     matching block is declared as an array, then the array sizes must
  339.     *     also match... Any mismatch will generate a link error."
  340.     *
  341.     * Arrays are not yet supported, so there is no check for that.
  342.     */
  343.    if (a->NumUniforms != b->NumUniforms)
  344.       return false;
  345.  
  346.    if (a->_Packing != b->_Packing)
  347.       return false;
  348.  
  349.    for (unsigned i = 0; i < a->NumUniforms; i++) {
  350.       if (strcmp(a->Uniforms[i].Name, b->Uniforms[i].Name) != 0)
  351.          return false;
  352.  
  353.       if (a->Uniforms[i].Type != b->Uniforms[i].Type)
  354.          return false;
  355.  
  356.       if (a->Uniforms[i].RowMajor != b->Uniforms[i].RowMajor)
  357.          return false;
  358.    }
  359.  
  360.    return true;
  361. }
  362.