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