1 /* 2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved. 3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved. 4 * Copyright 2010 Intel Corporation 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 * DEALINGS IN THE SOFTWARE. 24 */ 25 26 #include "ir.h" 27 #include "glsl_types.h" 28 #include "ir_visitor.h" 29 #include "../glsl/program.h" 30 #include "program/hash_table.h" 31 #include "ir_uniform.h" 32 33 extern "C" { 34 #include "main/compiler.h" 35 #include "main/mtypes.h" 36 #include "program/prog_parameter.h" 37 } 38 39 class get_sampler_name : public ir_hierarchical_visitor 40 { 41 public: 42 get_sampler_name(ir_dereference *last, 43 struct gl_shader_program *shader_program) 44 { 45 this->mem_ctx = ralloc_context(NULL); 46 this->shader_program = shader_program; 47 this->name = NULL; 48 this->offset = 0; 49 this->last = last; 50 } 51 52 ~get_sampler_name() 53 { 54 ralloc_free(this->mem_ctx); 55 } 56 57 virtual ir_visitor_status visit(ir_dereference_variable *ir) 58 { 59 this->name = ir->var->name; 60 return visit_continue; 61 } 62 63 virtual ir_visitor_status visit_leave(ir_dereference_record *ir) 64 { 65 this->name = ralloc_asprintf(mem_ctx, "%s.%s", name, ir->field); 66 return visit_continue; 67 } 68 69 virtual ir_visitor_status visit_leave(ir_dereference_array *ir) 70 { 71 ir_constant *index = ir->array_index->as_constant(); 72 int i; 73 74 if (index) { 75 i = index->value.i[0]; 76 } else { 77 /* GLSL 1.10 and 1.20 allowed variable sampler array indices, 78 * while GLSL 1.30 requires that the array indices be 79 * constant integer expressions. We don't expect any driver 80 * to actually work with a really variable array index, so 81 * all that would work would be an unrolled loop counter that ends 82 * up being constant above. 83 */ 84 ralloc_strcat(&shader_program->InfoLog, 85 "warning: Variable sampler array index unsupported.\n" 86 "This feature of the language was removed in GLSL 1.20 " 87 "and is unlikely to be supported for 1.10 in Mesa.\n"); 88 i = 0; 89 } 90 if (ir != last) { 91 this->name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i); 92 } else { 93 offset = i; 94 } 95 return visit_continue; 96 } 97 98 struct gl_shader_program *shader_program; 99 const char *name; 100 void *mem_ctx; 101 int offset; 102 ir_dereference *last; 103 }; 104 105 extern "C" { 106 int 107 _mesa_get_sampler_uniform_value(class ir_dereference *sampler, 108 struct gl_shader_program *shader_program, 109 const struct gl_program *prog) 110 { 111 get_sampler_name getname(sampler, shader_program); 112 113 sampler->accept(&getname); 114 115 unsigned location; 116 if (!shader_program->UniformHash->get(location, getname.name)) { 117 linker_error(shader_program, 118 "failed to find sampler named %s.\n", getname.name); 119 return 0; 120 } 121 122 return shader_program->UniformStorage[location].sampler + getname.offset; 123 } 124 } 125