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 | |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | extern "C" { |
| 29 | #include "main/compiler.h" |
| 30 | #include "main/mtypes.h" |
| 31 | #include "program/prog_parameter.h" |
| 32 | #include "ir.h" |
| 33 | #include "ir_visitor.h" |
| 34 | #include "glsl_types.h" |
| 35 | } |
| 36 | |
| 37 | static void fail_link(struct gl_shader_program *prog, const char *fmt, ...) PRINTFLIKE(2, 3); |
| 38 | |
| 39 | static void fail_link(struct gl_shader_program *prog, const char *fmt, ...) |
| 40 | { |
| 41 | va_list args; |
| 42 | va_start(args, fmt); |
| 43 | prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, args); |
| 44 | va_end(args); |
| 45 | |
| 46 | prog->LinkStatus = GL_FALSE; |
| 47 | } |
| 48 | |
| 49 | class get_sampler_name : public ir_hierarchical_visitor |
| 50 | { |
| 51 | public: |
| 52 | get_sampler_name(ir_dereference *last, |
| 53 | struct gl_shader_program *shader_program) |
| 54 | { |
| 55 | this->mem_ctx = talloc_new(NULL); |
| 56 | this->shader_program = shader_program; |
| 57 | this->name = NULL; |
| 58 | this->offset = 0; |
| 59 | this->last = last; |
| 60 | } |
| 61 | |
| 62 | ~get_sampler_name() |
| 63 | { |
| 64 | talloc_free(this->mem_ctx); |
| 65 | } |
| 66 | |
| 67 | virtual ir_visitor_status visit(ir_dereference_variable *ir) |
| 68 | { |
| 69 | this->name = ir->var->name; |
| 70 | return visit_continue; |
| 71 | } |
| 72 | |
| 73 | virtual ir_visitor_status visit_leave(ir_dereference_record *ir) |
| 74 | { |
| 75 | this->name = talloc_asprintf(mem_ctx, "%s.%s", name, ir->field); |
| 76 | return visit_continue; |
| 77 | } |
| 78 | |
| 79 | virtual ir_visitor_status visit_leave(ir_dereference_array *ir) |
| 80 | { |
| 81 | ir_constant *index = ir->array_index->as_constant(); |
| 82 | int i; |
| 83 | |
| 84 | if (index) { |
| 85 | i = index->value.i[0]; |
| 86 | } else { |
| 87 | /* GLSL 1.10 and 1.20 allowed variable sampler array indices, |
| 88 | * while GLSL 1.30 requires that the array indices be |
| 89 | * constant integer expressions. We don't expect any driver |
| 90 | * to actually work with a really variable array index, so |
| 91 | * all that would work would be an unrolled loop counter that ends |
| 92 | * up being constant above. |
| 93 | */ |
| 94 | shader_program->InfoLog = |
| 95 | talloc_asprintf_append(shader_program->InfoLog, |
| 96 | "warning: Variable sampler array index " |
| 97 | "unsupported.\nThis feature of the language " |
| 98 | "was removed in GLSL 1.20 and is unlikely " |
| 99 | "to be supported for 1.10 in Mesa.\n"); |
| 100 | i = 0; |
| 101 | } |
| 102 | if (ir != last) { |
| 103 | this->name = talloc_asprintf(mem_ctx, "%s[%d]", name, i); |
| 104 | } else { |
| 105 | offset = i; |
| 106 | } |
| 107 | return visit_continue; |
| 108 | } |
| 109 | |
| 110 | struct gl_shader_program *shader_program; |
| 111 | const char *name; |
| 112 | void *mem_ctx; |
| 113 | int offset; |
| 114 | ir_dereference *last; |
| 115 | }; |
| 116 | |
| 117 | extern "C" { |
| 118 | int |
| 119 | _mesa_get_sampler_uniform_value(class ir_dereference *sampler, |
| 120 | struct gl_shader_program *shader_program, |
| 121 | const struct gl_program *prog) |
| 122 | { |
| 123 | get_sampler_name getname(sampler, shader_program); |
| 124 | |
| 125 | sampler->accept(&getname); |
| 126 | |
| 127 | GLint index = _mesa_lookup_parameter_index(prog->Parameters, -1, |
| 128 | getname.name); |
| 129 | |
| 130 | if (index < 0) { |
| 131 | fail_link(shader_program, |
| 132 | "failed to find sampler named %s.\n", getname.name); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | index += getname.offset; |
| 137 | |
| 138 | return prog->Parameters->ParameterValues[index][0]; |
| 139 | } |
| 140 | } |