Subversion Repositories Kolibri OS

Rev

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