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 "glsl_symbol_table.h"
  29. #include "program/hash_table.h"
  30.  
  31. /* These functions are put in a "private" namespace instead of being marked
  32.  * static so that the unit tests can access them.  See
  33.  * http://code.google.com/p/googletest/wiki/AdvancedGuide#Testing_Private_Code
  34.  */
  35. namespace linker {
  36.  
  37. gl_uniform_storage *
  38. get_storage(gl_uniform_storage *storage, unsigned num_storage,
  39.             const char *name)
  40. {
  41.    for (unsigned int i = 0; i < num_storage; i++) {
  42.       if (strcmp(name, storage[i].name) == 0)
  43.          return &storage[i];
  44.    }
  45.  
  46.    return NULL;
  47. }
  48.  
  49. void
  50. copy_constant_to_storage(union gl_constant_value *storage,
  51.                          const ir_constant *val,
  52.                          const enum glsl_base_type base_type,
  53.                          const unsigned int elements)
  54. {
  55.    for (unsigned int i = 0; i < elements; i++) {
  56.       switch (base_type) {
  57.       case GLSL_TYPE_UINT:
  58.          storage[i].u = val->value.u[i];
  59.          break;
  60.       case GLSL_TYPE_INT:
  61.       case GLSL_TYPE_SAMPLER:
  62.          storage[i].i = val->value.i[i];
  63.          break;
  64.       case GLSL_TYPE_FLOAT:
  65.          storage[i].f = val->value.f[i];
  66.          break;
  67.       case GLSL_TYPE_BOOL:
  68.          storage[i].b = int(val->value.b[i]);
  69.          break;
  70.       case GLSL_TYPE_ARRAY:
  71.       case GLSL_TYPE_STRUCT:
  72.       case GLSL_TYPE_INTERFACE:
  73.       case GLSL_TYPE_VOID:
  74.       case GLSL_TYPE_ERROR:
  75.          /* All other types should have already been filtered by other
  76.           * paths in the caller.
  77.           */
  78.          assert(!"Should not get here.");
  79.          break;
  80.       }
  81.    }
  82. }
  83.  
  84. void
  85. set_uniform_binding(void *mem_ctx, gl_shader_program *prog,
  86.                     const char *name, const glsl_type *type, int binding)
  87. {
  88.    struct gl_uniform_storage *const storage =
  89.       get_storage(prog->UniformStorage, prog->NumUserUniformStorage, name);
  90.  
  91.    if (storage == NULL) {
  92.       assert(storage != NULL);
  93.       return;
  94.    }
  95.  
  96.    if (storage->type->is_sampler()) {
  97.       unsigned elements = MAX2(storage->array_elements, 1);
  98.  
  99.       /* From section 4.4.4 of the GLSL 4.20 specification:
  100.        * "If the binding identifier is used with an array, the first element
  101.        *  of the array takes the specified unit and each subsequent element
  102.        *  takes the next consecutive unit."
  103.        */
  104.       for (unsigned int i = 0; i < elements; i++) {
  105.          storage->storage[i].i = binding + i;
  106.       }
  107.  
  108.       for (int sh = 0; sh < MESA_SHADER_TYPES; sh++) {
  109.          gl_shader *shader = prog->_LinkedShaders[sh];
  110.  
  111.          if (shader && storage->sampler[sh].active) {
  112.             for (unsigned i = 0; i < elements; i++) {
  113.                unsigned index = storage->sampler[sh].index + i;
  114.  
  115.                shader->SamplerUnits[index] = storage->storage[i].i;
  116.             }
  117.          }
  118.       }
  119.    } else if (storage->block_index != -1) {
  120.       /* This is a field of a UBO.  val is the binding index. */
  121.       for (int i = 0; i < MESA_SHADER_TYPES; i++) {
  122.          int stage_index = prog->UniformBlockStageIndex[i][storage->block_index];
  123.  
  124.          if (stage_index != -1) {
  125.             struct gl_shader *sh = prog->_LinkedShaders[i];
  126.             sh->UniformBlocks[stage_index].Binding = binding;
  127.          }
  128.       }
  129.    }
  130.  
  131.    storage->initialized = true;
  132. }
  133.  
  134. void
  135. set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
  136.                         const char *name, const glsl_type *type,
  137.                         ir_constant *val)
  138. {
  139.    if (type->is_record()) {
  140.       ir_constant *field_constant;
  141.  
  142.       field_constant = (ir_constant *)val->components.get_head();
  143.  
  144.       for (unsigned int i = 0; i < type->length; i++) {
  145.          const glsl_type *field_type = type->fields.structure[i].type;
  146.          const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
  147.                                             type->fields.structure[i].name);
  148.          set_uniform_initializer(mem_ctx, prog, field_name,
  149.                                  field_type, field_constant);
  150.          field_constant = (ir_constant *)field_constant->next;
  151.       }
  152.       return;
  153.    } else if (type->is_array() && type->fields.array->is_record()) {
  154.       const glsl_type *const element_type = type->fields.array;
  155.  
  156.       for (unsigned int i = 0; i < type->length; i++) {
  157.          const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
  158.  
  159.          set_uniform_initializer(mem_ctx, prog, element_name,
  160.                                  element_type, val->array_elements[i]);
  161.       }
  162.       return;
  163.    }
  164.  
  165.    struct gl_uniform_storage *const storage =
  166.       get_storage(prog->UniformStorage,
  167.                   prog->NumUserUniformStorage,
  168.                   name);
  169.    if (storage == NULL) {
  170.       assert(storage != NULL);
  171.       return;
  172.    }
  173.  
  174.    if (val->type->is_array()) {
  175.       const enum glsl_base_type base_type =
  176.          val->array_elements[0]->type->base_type;
  177.       const unsigned int elements = val->array_elements[0]->type->components();
  178.       unsigned int idx = 0;
  179.  
  180.       assert(val->type->length >= storage->array_elements);
  181.       for (unsigned int i = 0; i < storage->array_elements; i++) {
  182.          copy_constant_to_storage(& storage->storage[idx],
  183.                                   val->array_elements[i],
  184.                                   base_type,
  185.                                   elements);
  186.  
  187.          idx += elements;
  188.       }
  189.    } else {
  190.       copy_constant_to_storage(storage->storage,
  191.                                val,
  192.                                val->type->base_type,
  193.                                val->type->components());
  194.  
  195.       if (storage->type->is_sampler()) {
  196.          for (int sh = 0; sh < MESA_SHADER_TYPES; sh++) {
  197.             gl_shader *shader = prog->_LinkedShaders[sh];
  198.  
  199.             if (shader && storage->sampler[sh].active) {
  200.                unsigned index = storage->sampler[sh].index;
  201.  
  202.                shader->SamplerUnits[index] = storage->storage[0].i;
  203.             }
  204.          }
  205.       }
  206.    }
  207.  
  208.    storage->initialized = true;
  209. }
  210. }
  211.  
  212. void
  213. link_set_uniform_initializers(struct gl_shader_program *prog)
  214. {
  215.    void *mem_ctx = NULL;
  216.  
  217.    for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
  218.       struct gl_shader *shader = prog->_LinkedShaders[i];
  219.  
  220.       if (shader == NULL)
  221.          continue;
  222.  
  223.       foreach_list(node, shader->ir) {
  224.          ir_variable *const var = ((ir_instruction *) node)->as_variable();
  225.  
  226.          if (!var || var->mode != ir_var_uniform)
  227.             continue;
  228.  
  229.          if (!mem_ctx)
  230.             mem_ctx = ralloc_context(NULL);
  231.  
  232.          if (var->explicit_binding) {
  233.             linker::set_uniform_binding(mem_ctx, prog, var->name,
  234.                                         var->type, var->binding);
  235.          } else if (var->constant_value) {
  236.             linker::set_uniform_initializer(mem_ctx, prog, var->name,
  237.                                             var->type, var->constant_value);
  238.          }
  239.       }
  240.    }
  241.  
  242.    ralloc_free(mem_ctx);
  243. }
  244.