Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 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 | |
Chad Versace | 223568f | 2010-11-01 14:23:53 -0700 | [diff] [blame] | 26 | #include "ir.h" |
| 27 | #include "glsl_types.h" |
| 28 | #include "ir_visitor.h" |
Ian Romanick | a9f2516 | 2011-09-22 16:56:58 -0700 | [diff] [blame] | 29 | #include "../glsl/program.h" |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 30 | |
| 31 | extern "C" { |
| 32 | #include "main/compiler.h" |
| 33 | #include "main/mtypes.h" |
| 34 | #include "program/prog_parameter.h" |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 37 | class get_sampler_name : public ir_hierarchical_visitor |
| 38 | { |
| 39 | public: |
| 40 | get_sampler_name(ir_dereference *last, |
| 41 | struct gl_shader_program *shader_program) |
| 42 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 43 | this->mem_ctx = ralloc_context(NULL); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 44 | this->shader_program = shader_program; |
| 45 | this->name = NULL; |
| 46 | this->offset = 0; |
| 47 | this->last = last; |
| 48 | } |
| 49 | |
| 50 | ~get_sampler_name() |
| 51 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 52 | ralloc_free(this->mem_ctx); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 56 | { |
| 57 | this->name = ir->var->name; |
| 58 | return visit_continue; |
| 59 | } |
| 60 | |
| 61 | virtual ir_visitor_status visit_leave(ir_dereference_record *ir) |
| 62 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 63 | this->name = ralloc_asprintf(mem_ctx, "%s.%s", name, ir->field); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 64 | return visit_continue; |
| 65 | } |
| 66 | |
| 67 | virtual ir_visitor_status visit_leave(ir_dereference_array *ir) |
| 68 | { |
| 69 | ir_constant *index = ir->array_index->as_constant(); |
| 70 | int i; |
| 71 | |
| 72 | if (index) { |
| 73 | i = index->value.i[0]; |
| 74 | } else { |
| 75 | /* GLSL 1.10 and 1.20 allowed variable sampler array indices, |
| 76 | * while GLSL 1.30 requires that the array indices be |
| 77 | * constant integer expressions. We don't expect any driver |
| 78 | * to actually work with a really variable array index, so |
| 79 | * all that would work would be an unrolled loop counter that ends |
| 80 | * up being constant above. |
| 81 | */ |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 82 | ralloc_strcat(&shader_program->InfoLog, |
| 83 | "warning: Variable sampler array index unsupported.\n" |
| 84 | "This feature of the language was removed in GLSL 1.20 " |
| 85 | "and is unlikely to be supported for 1.10 in Mesa.\n"); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 86 | i = 0; |
| 87 | } |
| 88 | if (ir != last) { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 89 | this->name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 90 | } else { |
| 91 | offset = i; |
| 92 | } |
| 93 | return visit_continue; |
| 94 | } |
| 95 | |
| 96 | struct gl_shader_program *shader_program; |
| 97 | const char *name; |
| 98 | void *mem_ctx; |
| 99 | int offset; |
| 100 | ir_dereference *last; |
| 101 | }; |
| 102 | |
| 103 | extern "C" { |
| 104 | int |
| 105 | _mesa_get_sampler_uniform_value(class ir_dereference *sampler, |
| 106 | struct gl_shader_program *shader_program, |
| 107 | const struct gl_program *prog) |
| 108 | { |
| 109 | get_sampler_name getname(sampler, shader_program); |
| 110 | |
| 111 | sampler->accept(&getname); |
| 112 | |
| 113 | GLint index = _mesa_lookup_parameter_index(prog->Parameters, -1, |
| 114 | getname.name); |
| 115 | |
| 116 | if (index < 0) { |
Ian Romanick | a9f2516 | 2011-09-22 16:56:58 -0700 | [diff] [blame] | 117 | linker_error(shader_program, |
| 118 | "failed to find sampler named %s.\n", getname.name); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | index += getname.offset; |
| 123 | |
Bryan Cain | 6d89aba | 2011-05-17 17:13:20 -0500 | [diff] [blame] | 124 | return prog->Parameters->ParameterValues[index][0].f; |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 125 | } |
| 126 | } |