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" |
Ian Romanick | 7199096 | 2011-10-18 16:01:49 -0700 | [diff] [blame] | 30 | #include "ir_uniform.h" |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 31 | |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 32 | #include "main/mtypes.h" |
Brian Paul | 6dac455 | 2014-12-15 17:05:13 -0700 | [diff] [blame] | 33 | #include "program/hash_table.h" |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 34 | #include "program/prog_parameter.h" |
Marek Olšák | d4a06d7 | 2013-05-13 15:46:49 +0200 | [diff] [blame] | 35 | #include "program/program.h" |
Brian Paul | 6dac455 | 2014-12-15 17:05:13 -0700 | [diff] [blame] | 36 | |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 37 | |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 38 | class get_sampler_name : public ir_hierarchical_visitor |
| 39 | { |
| 40 | public: |
| 41 | get_sampler_name(ir_dereference *last, |
| 42 | struct gl_shader_program *shader_program) |
| 43 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 44 | this->mem_ctx = ralloc_context(NULL); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 45 | this->shader_program = shader_program; |
| 46 | this->name = NULL; |
| 47 | this->offset = 0; |
| 48 | this->last = last; |
| 49 | } |
| 50 | |
| 51 | ~get_sampler_name() |
| 52 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 53 | ralloc_free(this->mem_ctx); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 57 | { |
| 58 | this->name = ir->var->name; |
| 59 | return visit_continue; |
| 60 | } |
| 61 | |
| 62 | virtual ir_visitor_status visit_leave(ir_dereference_record *ir) |
| 63 | { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 64 | this->name = ralloc_asprintf(mem_ctx, "%s.%s", name, ir->field); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 65 | return visit_continue; |
| 66 | } |
| 67 | |
| 68 | virtual ir_visitor_status visit_leave(ir_dereference_array *ir) |
| 69 | { |
| 70 | ir_constant *index = ir->array_index->as_constant(); |
| 71 | int i; |
| 72 | |
| 73 | if (index) { |
| 74 | i = index->value.i[0]; |
| 75 | } else { |
| 76 | /* GLSL 1.10 and 1.20 allowed variable sampler array indices, |
| 77 | * while GLSL 1.30 requires that the array indices be |
| 78 | * constant integer expressions. We don't expect any driver |
| 79 | * to actually work with a really variable array index, so |
| 80 | * all that would work would be an unrolled loop counter that ends |
| 81 | * up being constant above. |
| 82 | */ |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 83 | ralloc_strcat(&shader_program->InfoLog, |
| 84 | "warning: Variable sampler array index unsupported.\n" |
| 85 | "This feature of the language was removed in GLSL 1.20 " |
| 86 | "and is unlikely to be supported for 1.10 in Mesa.\n"); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 87 | i = 0; |
| 88 | } |
| 89 | if (ir != last) { |
Kenneth Graunke | d3073f5 | 2011-01-21 14:32:31 -0800 | [diff] [blame] | 90 | this->name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 91 | } else { |
| 92 | offset = i; |
| 93 | } |
| 94 | return visit_continue; |
| 95 | } |
| 96 | |
| 97 | struct gl_shader_program *shader_program; |
| 98 | const char *name; |
| 99 | void *mem_ctx; |
| 100 | int offset; |
| 101 | ir_dereference *last; |
| 102 | }; |
| 103 | |
Marek Olšák | d4a06d7 | 2013-05-13 15:46:49 +0200 | [diff] [blame] | 104 | |
Brian Paul | 6dac455 | 2014-12-15 17:05:13 -0700 | [diff] [blame] | 105 | int |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 106 | _mesa_get_sampler_uniform_value(class ir_dereference *sampler, |
| 107 | struct gl_shader_program *shader_program, |
| 108 | const struct gl_program *prog) |
| 109 | { |
| 110 | get_sampler_name getname(sampler, shader_program); |
| 111 | |
Paul Berry | 665b8d7 | 2014-01-07 10:11:39 -0800 | [diff] [blame] | 112 | GLuint shader = _mesa_program_enum_to_shader_stage(prog->Target); |
Marek Olšák | d4a06d7 | 2013-05-13 15:46:49 +0200 | [diff] [blame] | 113 | |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 114 | sampler->accept(&getname); |
| 115 | |
Ian Romanick | 7199096 | 2011-10-18 16:01:49 -0700 | [diff] [blame] | 116 | unsigned location; |
| 117 | if (!shader_program->UniformHash->get(location, getname.name)) { |
Ian Romanick | a9f2516 | 2011-09-22 16:56:58 -0700 | [diff] [blame] | 118 | linker_error(shader_program, |
| 119 | "failed to find sampler named %s.\n", getname.name); |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Marek Olšák | d4a06d7 | 2013-05-13 15:46:49 +0200 | [diff] [blame] | 123 | if (!shader_program->UniformStorage[location].sampler[shader].active) { |
| 124 | assert(0 && "cannot return a sampler"); |
| 125 | linker_error(shader_program, |
| 126 | "cannot return a sampler named %s, because it is not " |
| 127 | "used in this shader stage. This is a driver bug.\n", |
| 128 | getname.name); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | return shader_program->UniformStorage[location].sampler[shader].index + |
| 133 | getname.offset; |
Eric Anholt | a328932 | 2010-09-28 10:43:56 -0700 | [diff] [blame] | 134 | } |
Chris Forbes | 3b48f6a | 2014-08-03 19:55:55 +1200 | [diff] [blame] | 135 | |
| 136 | |
Brian Paul | 6dac455 | 2014-12-15 17:05:13 -0700 | [diff] [blame] | 137 | class ir_rvalue * |
Chris Forbes | 3b48f6a | 2014-08-03 19:55:55 +1200 | [diff] [blame] | 138 | _mesa_get_sampler_array_nonconst_index(class ir_dereference *sampler) |
| 139 | { |
| 140 | ir_dereference_array *deref_arr = sampler->as_dereference_array(); |
| 141 | if (!deref_arr || deref_arr->array_index->as_constant()) |
| 142 | return NULL; |
| 143 | |
| 144 | return deref_arr->array_index; |
| 145 | } |