nir/lower_variables_scalar: Silence a compiler warning
[mesa.git] / src / glsl / nir / nir_lower_samplers.cpp
1 /*
2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2014 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 "nir.h"
27 #include "../program.h"
28 #include "program/hash_table.h"
29 #include "ir_uniform.h"
30
31 extern "C" {
32 #include "main/compiler.h"
33 #include "main/mtypes.h"
34 #include "program/prog_parameter.h"
35 #include "program/program.h"
36 }
37
38 static unsigned
39 get_deref_name_offset(nir_deref_var *deref_var,
40 gl_shader_program *shader_program, char **name,
41 void *mem_ctx)
42 {
43 nir_deref *deref = &deref_var->deref;
44 nir_deref_array *deref_array;
45 nir_deref_struct *deref_struct;
46
47 *name = ralloc_strdup(mem_ctx, deref_var->var->name);
48
49 while (deref->child != NULL) {
50 deref = deref->child;
51 switch (deref->deref_type) {
52 case nir_deref_type_array:
53 deref_array = nir_deref_as_array(deref);
54 if (deref_array->has_indirect) {
55 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
56 * while GLSL 1.30 requires that the array indices be
57 * constant integer expressions. We don't expect any driver
58 * to actually work with a really variable array index, so
59 * all that would work would be an unrolled loop counter that
60 * ends up being constant.
61 */
62 ralloc_strcat(&shader_program->InfoLog,
63 "warning: Variable sampler array index unsupported.\n"
64 "This feature of the language was removed in GLSL 1.20 "
65 "and is unlikely to be supported for 1.10 in Mesa.\n");
66 }
67 if (deref->child == NULL) {
68 return deref_array->base_offset;
69 }
70 ralloc_asprintf_append(name, "[%u]", deref_array->base_offset);
71 break;
72
73 case nir_deref_type_struct:
74 deref_struct = nir_deref_as_struct(deref);
75 ralloc_asprintf_append(name, ".%s", deref_struct->elem);
76 break;
77
78 default:
79 assert(0);
80 break;
81 }
82 }
83
84 return 0;
85 }
86
87 static unsigned
88 get_sampler_index(nir_deref_var *sampler,
89 struct gl_shader_program *shader_program,
90 const struct gl_program *prog)
91 {
92 void *mem_ctx = ralloc_context(NULL);
93 char *name;
94 unsigned offset = get_deref_name_offset(sampler, shader_program, &name,
95 mem_ctx);
96
97 GLuint shader = _mesa_program_enum_to_shader_stage(prog->Target);
98
99 unsigned location;
100 if (!shader_program->UniformHash->get(location, name)) {
101 linker_error(shader_program,
102 "failed to find sampler named %s.\n", name);
103 return 0;
104 }
105
106 if (!shader_program->UniformStorage[location].sampler[shader].active) {
107 assert(0 && "cannot return a sampler");
108 linker_error(shader_program,
109 "cannot return a sampler named %s, because it is not "
110 "used in this shader stage. This is a driver bug.\n",
111 name);
112 return 0;
113 }
114
115 ralloc_free(mem_ctx);
116
117 return shader_program->UniformStorage[location].sampler[shader].index +
118 offset;
119 }
120
121 static void
122 lower_sampler(nir_tex_instr *instr, struct gl_shader_program *shader_program,
123 const struct gl_program *prog)
124 {
125 if (instr->sampler) {
126 instr->sampler_index = get_sampler_index(instr->sampler, shader_program,
127 prog);
128 instr->sampler = NULL;
129 }
130 }
131
132 typedef struct {
133 struct gl_shader_program *shader_program;
134 struct gl_program *prog;
135 } lower_state;
136
137 static bool
138 lower_block_cb(nir_block *block, void *_state)
139 {
140 lower_state *state = (lower_state *) _state;
141
142 nir_foreach_instr(block, instr) {
143 if (instr->type == nir_instr_type_texture) {
144 nir_tex_instr *tex_instr = nir_instr_as_texture(instr);
145 lower_sampler(tex_instr, state->shader_program, state->prog);
146 }
147 }
148
149 return true;
150 }
151
152 static void
153 lower_impl(nir_function_impl *impl, struct gl_shader_program *shader_program,
154 struct gl_program *prog)
155 {
156 lower_state state;
157 state.shader_program = shader_program;
158 state.prog = prog;
159 nir_foreach_block(impl, lower_block_cb, &state);
160 }
161
162 extern "C" void
163 nir_lower_samplers(nir_shader *shader, struct gl_shader_program *shader_program,
164 struct gl_program *prog)
165 {
166 nir_foreach_overload(shader, overload) {
167 if (overload->impl)
168 lower_impl(overload->impl, shader_program, prog);
169 }
170 }