nir: Make texture instruction names more consistent
[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 switch (deref->child->deref_type) {
51 case nir_deref_type_array:
52 deref_array = nir_deref_as_array(deref->child);
53 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
54 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
55 * while GLSL 1.30 requires that the array indices be
56 * constant integer expressions. We don't expect any driver
57 * to actually work with a really variable array index, so
58 * all that would work would be an unrolled loop counter that
59 * ends up being constant.
60 */
61 ralloc_strcat(&shader_program->InfoLog,
62 "warning: Variable sampler array index unsupported.\n"
63 "This feature of the language was removed in GLSL 1.20 "
64 "and is unlikely to be supported for 1.10 in Mesa.\n");
65 }
66 if (deref_array->deref.child == NULL) {
67 return deref_array->base_offset;
68 }
69 ralloc_asprintf_append(name, "[%u]", deref_array->base_offset);
70 break;
71
72 case nir_deref_type_struct: {
73 deref_struct = nir_deref_as_struct(deref->child);
74 const char *field = glsl_get_struct_elem_name(deref->type,
75 deref_struct->index);
76 ralloc_asprintf_append(name, ".%s", field);
77 break;
78 }
79
80 default:
81 assert(0);
82 break;
83 }
84 deref = deref->child;
85 }
86
87 return 0;
88 }
89
90 static unsigned
91 get_sampler_index(nir_deref_var *sampler,
92 struct gl_shader_program *shader_program,
93 const struct gl_program *prog)
94 {
95 void *mem_ctx = ralloc_context(NULL);
96 char *name;
97 unsigned offset = get_deref_name_offset(sampler, shader_program, &name,
98 mem_ctx);
99
100 GLuint shader = _mesa_program_enum_to_shader_stage(prog->Target);
101
102 unsigned location;
103 if (!shader_program->UniformHash->get(location, name)) {
104 linker_error(shader_program,
105 "failed to find sampler named %s.\n", name);
106 return 0;
107 }
108
109 if (!shader_program->UniformStorage[location].sampler[shader].active) {
110 assert(0 && "cannot return a sampler");
111 linker_error(shader_program,
112 "cannot return a sampler named %s, because it is not "
113 "used in this shader stage. This is a driver bug.\n",
114 name);
115 return 0;
116 }
117
118 ralloc_free(mem_ctx);
119
120 return shader_program->UniformStorage[location].sampler[shader].index +
121 offset;
122 }
123
124 static void
125 lower_sampler(nir_tex_instr *instr, struct gl_shader_program *shader_program,
126 const struct gl_program *prog)
127 {
128 if (instr->sampler) {
129 instr->sampler_index = get_sampler_index(instr->sampler, shader_program,
130 prog);
131 nir_src empty_src;
132 memset(&empty_src, 0, sizeof empty_src);
133 for (nir_deref *deref = &instr->sampler->deref; deref; deref = deref->child) {
134 if (deref->deref_type == nir_deref_type_array) {
135 nir_deref_array *arr = nir_deref_as_array(deref);
136 nir_instr_rewrite_src(&instr->instr, &arr->indirect, empty_src);
137 }
138 }
139
140 instr->sampler = NULL;
141 }
142 }
143
144 typedef struct {
145 struct gl_shader_program *shader_program;
146 struct gl_program *prog;
147 } lower_state;
148
149 static bool
150 lower_block_cb(nir_block *block, void *_state)
151 {
152 lower_state *state = (lower_state *) _state;
153
154 nir_foreach_instr(block, instr) {
155 if (instr->type == nir_instr_type_tex) {
156 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
157 lower_sampler(tex_instr, state->shader_program, state->prog);
158 }
159 }
160
161 return true;
162 }
163
164 static void
165 lower_impl(nir_function_impl *impl, struct gl_shader_program *shader_program,
166 struct gl_program *prog)
167 {
168 lower_state state;
169 state.shader_program = shader_program;
170 state.prog = prog;
171 nir_foreach_block(impl, lower_block_cb, &state);
172 }
173
174 extern "C" void
175 nir_lower_samplers(nir_shader *shader, struct gl_shader_program *shader_program,
176 struct gl_program *prog)
177 {
178 nir_foreach_overload(shader, overload) {
179 if (overload->impl)
180 lower_impl(overload->impl, shader_program, prog);
181 }
182 }