Merge branch 'wip/i965-separate-sampler-tex' into vulkan
[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 void
39 add_indirect_to_tex(nir_tex_instr *instr, nir_src indirect)
40 {
41 /* First, we have to resize the array of texture sources */
42 nir_tex_src *new_srcs = rzalloc_array(instr, nir_tex_src,
43 instr->num_srcs + 1);
44
45 for (unsigned i = 0; i < instr->num_srcs; i++) {
46 new_srcs[i].src_type = instr->src[i].src_type;
47 nir_instr_move_src(&instr->instr, &new_srcs[i].src, &instr->src[i].src);
48 }
49
50 ralloc_free(instr->src);
51 instr->src = new_srcs;
52
53 /* Now we can go ahead and move the source over to being a
54 * first-class texture source.
55 */
56 instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
57 instr->num_srcs++;
58 nir_instr_rewrite_src(&instr->instr, &instr->src[instr->num_srcs - 1].src,
59 indirect);
60 }
61
62 static unsigned
63 get_sampler_index(const struct gl_shader_program *shader_program,
64 gl_shader_stage stage, const char *name)
65 {
66 unsigned location;
67 if (!shader_program->UniformHash->get(location, name)) {
68 assert(!"failed to find sampler");
69 return 0;
70 }
71
72 if (!shader_program->UniformStorage[location].sampler[stage].active) {
73 assert(!"cannot return a sampler");
74 return 0;
75 }
76
77 return shader_program->UniformStorage[location].sampler[stage].index;
78 }
79
80 static void
81 lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_program,
82 gl_shader_stage stage, void *mem_ctx)
83 {
84 if (instr->sampler == NULL)
85 return;
86
87 /* Get the name and the offset */
88 instr->sampler_index = 0;
89 char *name = ralloc_strdup(mem_ctx, instr->sampler->var->name);
90
91 for (nir_deref *deref = &instr->sampler->deref;
92 deref->child; deref = deref->child) {
93 switch (deref->child->deref_type) {
94 case nir_deref_type_array: {
95 nir_deref_array *deref_array = nir_deref_as_array(deref->child);
96
97 assert(deref_array->deref_array_type != nir_deref_array_type_wildcard);
98
99 if (deref_array->deref.child) {
100 ralloc_asprintf_append(&name, "[%u]",
101 deref_array->deref_array_type == nir_deref_array_type_direct ?
102 deref_array->base_offset : 0);
103 } else {
104 assert(deref->child->type->base_type == GLSL_TYPE_SAMPLER);
105 instr->sampler_index = deref_array->base_offset;
106 }
107
108 /* XXX: We're assuming here that the indirect is the last array
109 * thing we have. This should be ok for now as we don't support
110 * arrays_of_arrays yet.
111 */
112 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
113 /* First, we have to resize the array of texture sources */
114 nir_tex_src *new_srcs = rzalloc_array(instr, nir_tex_src,
115 instr->num_srcs + 1);
116
117 for (unsigned i = 0; i < instr->num_srcs; i++) {
118 new_srcs[i].src_type = instr->src[i].src_type;
119 nir_instr_move_src(&instr->instr, &new_srcs[i].src,
120 &instr->src[i].src);
121 }
122
123 ralloc_free(instr->src);
124 instr->src = new_srcs;
125
126 /* Now we can go ahead and move the source over to being a
127 * first-class texture source.
128 */
129 instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
130 instr->num_srcs++;
131 nir_instr_move_src(&instr->instr,
132 &instr->src[instr->num_srcs - 1].src,
133 &deref_array->indirect);
134
135 instr->sampler_array_size = glsl_get_length(deref->type);
136 }
137 break;
138 }
139
140 case nir_deref_type_struct: {
141 nir_deref_struct *deref_struct = nir_deref_as_struct(deref->child);
142 const char *field = glsl_get_struct_elem_name(deref->type,
143 deref_struct->index);
144 ralloc_asprintf_append(&name, ".%s", field);
145 break;
146 }
147
148 default:
149 unreachable("Invalid deref type");
150 break;
151 }
152 }
153
154 instr->sampler_index += get_sampler_index(shader_program, stage, name);
155
156 instr->sampler = NULL;
157 }
158
159 typedef struct {
160 void *mem_ctx;
161 const struct gl_shader_program *shader_program;
162 gl_shader_stage stage;
163 } lower_state;
164
165 static bool
166 lower_block_cb(nir_block *block, void *_state)
167 {
168 lower_state *state = (lower_state *) _state;
169
170 nir_foreach_instr(block, instr) {
171 if (instr->type == nir_instr_type_tex) {
172 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
173 lower_sampler(tex_instr, state->shader_program, state->stage,
174 state->mem_ctx);
175 }
176 }
177
178 return true;
179 }
180
181 static void
182 lower_impl(nir_function_impl *impl, const struct gl_shader_program *shader_program,
183 gl_shader_stage stage)
184 {
185 lower_state state;
186
187 state.mem_ctx = ralloc_parent(impl);
188 state.shader_program = shader_program;
189 state.stage = stage;
190
191 nir_foreach_block(impl, lower_block_cb, &state);
192 }
193
194 extern "C" void
195 nir_lower_samplers(nir_shader *shader,
196 const struct gl_shader_program *shader_program)
197 {
198 nir_foreach_overload(shader, overload) {
199 if (overload->impl)
200 lower_impl(overload->impl, shader_program, shader->stage);
201 }
202 }
203
204 static bool
205 lower_samplers_for_vk_block(nir_block *block, void *data)
206 {
207 nir_foreach_instr(block, instr) {
208 if (instr->type != nir_instr_type_tex)
209 continue;
210
211 nir_tex_instr *tex = nir_instr_as_tex(instr);
212
213 assert(tex->sampler);
214
215 tex->sampler_set = tex->sampler->var->data.descriptor_set;
216 tex->sampler_index = tex->sampler->var->data.binding;
217
218 if (tex->sampler->deref.child) {
219 assert(tex->sampler->deref.child->deref_type == nir_deref_type_array);
220 nir_deref_array *arr = nir_deref_as_array(tex->sampler->deref.child);
221
222 /* Only one-level arrays are allowed in vulkan */
223 assert(arr->deref.child == NULL);
224
225 tex->sampler_index += arr->base_offset;
226 if (arr->deref_array_type == nir_deref_array_type_indirect) {
227 add_indirect_to_tex(tex, arr->indirect);
228 nir_instr_rewrite_src(instr, &arr->indirect, NIR_SRC_INIT);
229
230 tex->sampler_array_size = glsl_get_length(tex->sampler->deref.type);
231 }
232 }
233
234 tex->sampler = NULL;
235 }
236
237 return true;
238 }
239
240 extern "C" void
241 nir_lower_samplers_for_vk(nir_shader *shader)
242 {
243 nir_foreach_overload(shader, overload) {
244 if (overload->impl) {
245 nir_foreach_block(overload->impl, lower_samplers_for_vk_block, NULL);
246 }
247 }
248 }