Home | History | Annotate | Download | only in glsl
      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       default:
     71 	 /* All other types should have already been filtered by other
     72 	  * paths in the caller.
     73 	  */
     74 	 assert(!"Should not get here.");
     75 	 break;
     76       }
     77    }
     78 }
     79 
     80 void
     81 set_uniform_initializer(void *mem_ctx, gl_shader_program *prog,
     82 			const char *name, const glsl_type *type,
     83 			ir_constant *val)
     84 {
     85    if (type->is_record()) {
     86       ir_constant *field_constant;
     87 
     88       field_constant = (ir_constant *)val->components.get_head();
     89 
     90       for (unsigned int i = 0; i < type->length; i++) {
     91 	 const glsl_type *field_type = type->fields.structure[i].type;
     92 	 const char *field_name = ralloc_asprintf(mem_ctx, "%s.%s", name,
     93 					    type->fields.structure[i].name);
     94 	 set_uniform_initializer(mem_ctx, prog, field_name,
     95 				 field_type, field_constant);
     96 	 field_constant = (ir_constant *)field_constant->next;
     97       }
     98       return;
     99    } else if (type->is_array() && type->fields.array->is_record()) {
    100       const glsl_type *const element_type = type->fields.array;
    101 
    102       for (unsigned int i = 0; i < type->length; i++) {
    103 	 const char *element_name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
    104 
    105 	 set_uniform_initializer(mem_ctx, prog, element_name,
    106 				 element_type, val->array_elements[i]);
    107       }
    108       return;
    109    }
    110 
    111    struct gl_uniform_storage *const storage =
    112       get_storage(prog->UniformStorage,
    113 		  prog->NumUserUniformStorage,
    114 		  name);
    115    if (storage == NULL) {
    116       assert(storage != NULL);
    117       return;
    118    }
    119 
    120    if (val->type->is_array()) {
    121       const enum glsl_base_type base_type =
    122 	 val->array_elements[0]->type->base_type;
    123       const unsigned int elements = val->array_elements[0]->type->components();
    124       unsigned int idx = 0;
    125 
    126       assert(val->type->length >= storage->array_elements);
    127       for (unsigned int i = 0; i < storage->array_elements; i++) {
    128 	 copy_constant_to_storage(& storage->storage[idx],
    129 				  val->array_elements[i],
    130 				  base_type,
    131 				  elements);
    132 
    133 	 idx += elements;
    134       }
    135 
    136       if (base_type == GLSL_TYPE_SAMPLER) {
    137 	 for (unsigned int i = 0; i < storage->array_elements; i++) {
    138 	    prog->SamplerUnits[storage->sampler + i] = storage->storage[i].i;
    139 	 }
    140       }
    141    } else {
    142       copy_constant_to_storage(storage->storage,
    143 			       val,
    144 			       val->type->base_type,
    145 			       val->type->components());
    146 
    147       if (storage->type->is_sampler())
    148 	 prog->SamplerUnits[storage->sampler] = storage->storage[0].i;
    149    }
    150 
    151    storage->initialized = true;
    152 }
    153 }
    154 
    155 void
    156 link_set_uniform_initializers(struct gl_shader_program *prog)
    157 {
    158    void *mem_ctx = NULL;
    159 
    160    for (unsigned int i = 0; i < MESA_SHADER_TYPES; i++) {
    161       struct gl_shader *shader = prog->_LinkedShaders[i];
    162 
    163       if (shader == NULL)
    164 	 continue;
    165 
    166       foreach_list(node, shader->ir) {
    167 	 ir_variable *const var = ((ir_instruction *) node)->as_variable();
    168 
    169 	 if (!var || var->mode != ir_var_uniform || !var->constant_value)
    170 	    continue;
    171 
    172 	 if (!mem_ctx)
    173 	    mem_ctx = ralloc_context(NULL);
    174 
    175 	 linker::set_uniform_initializer(mem_ctx, prog, var->name,
    176 					 var->type, var->constant_value);
    177       }
    178    }
    179 
    180    ralloc_free(mem_ctx);
    181 }
    182