Merge ../mesa into vulkan
[mesa.git] / src / glsl / nir / nir_lower_samplers.c
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 "nir_builder.h"
28 #include "../program.h"
29 #include "program/hash_table.h"
30 #include "ir_uniform.h"
31
32 #include "main/compiler.h"
33 #include "main/mtypes.h"
34 #include "program/prog_parameter.h"
35 #include "program/program.h"
36
37 static void
38 add_indirect_to_tex(nir_tex_instr *instr, nir_src indirect)
39 {
40 /* First, we have to resize the array of texture sources */
41 nir_tex_src *new_srcs = rzalloc_array(instr, nir_tex_src,
42 instr->num_srcs + 1);
43
44 for (unsigned i = 0; i < instr->num_srcs; i++) {
45 new_srcs[i].src_type = instr->src[i].src_type;
46 nir_instr_move_src(&instr->instr, &new_srcs[i].src, &instr->src[i].src);
47 }
48
49 ralloc_free(instr->src);
50 instr->src = new_srcs;
51
52 /* Now we can go ahead and move the source over to being a
53 * first-class texture source.
54 */
55 instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
56 instr->num_srcs++;
57 nir_instr_rewrite_src(&instr->instr, &instr->src[instr->num_srcs - 1].src,
58 indirect);
59 }
60
61 /* Calculate the sampler index based on array indicies and also
62 * calculate the base uniform location for struct members.
63 */
64 static void
65 calc_sampler_offsets(nir_deref *tail, nir_tex_instr *instr,
66 unsigned *array_elements, nir_ssa_def **indirect,
67 nir_builder *b, unsigned *location)
68 {
69 if (tail->child == NULL)
70 return;
71
72 switch (tail->child->deref_type) {
73 case nir_deref_type_array: {
74 nir_deref_array *deref_array = nir_deref_as_array(tail->child);
75
76 assert(deref_array->deref_array_type != nir_deref_array_type_wildcard);
77
78 calc_sampler_offsets(tail->child, instr, array_elements,
79 indirect, b, location);
80 instr->sampler_index += deref_array->base_offset * *array_elements;
81
82 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
83 nir_ssa_def *mul =
84 nir_imul(b, nir_imm_int(b, *array_elements),
85 nir_ssa_for_src(b, deref_array->indirect, 1));
86
87 nir_instr_rewrite_src(&instr->instr, &deref_array->indirect,
88 NIR_SRC_INIT);
89
90 if (*indirect) {
91 *indirect = nir_iadd(b, *indirect, mul);
92 } else {
93 *indirect = mul;
94 }
95 }
96
97 *array_elements *= glsl_get_length(tail->type);
98 break;
99 }
100
101 case nir_deref_type_struct: {
102 nir_deref_struct *deref_struct = nir_deref_as_struct(tail->child);
103 *location += glsl_get_record_location_offset(tail->type, deref_struct->index);
104 calc_sampler_offsets(tail->child, instr, array_elements,
105 indirect, b, location);
106 break;
107 }
108
109 default:
110 unreachable("Invalid deref type");
111 break;
112 }
113 }
114
115 static void
116 lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_program,
117 gl_shader_stage stage, nir_builder *builder)
118 {
119 if (instr->sampler == NULL)
120 return;
121
122 instr->sampler_index = 0;
123 unsigned location = instr->sampler->var->data.location;
124 unsigned array_elements = 1;
125 nir_ssa_def *indirect = NULL;
126
127 builder->cursor = nir_before_instr(&instr->instr);
128 calc_sampler_offsets(&instr->sampler->deref, instr, &array_elements,
129 &indirect, builder, &location);
130
131 if (indirect) {
132 /* First, we have to resize the array of texture sources */
133 nir_tex_src *new_srcs = rzalloc_array(instr, nir_tex_src,
134 instr->num_srcs + 1);
135
136 for (unsigned i = 0; i < instr->num_srcs; i++) {
137 new_srcs[i].src_type = instr->src[i].src_type;
138 nir_instr_move_src(&instr->instr, &new_srcs[i].src,
139 &instr->src[i].src);
140 }
141
142 ralloc_free(instr->src);
143 instr->src = new_srcs;
144
145 /* Now we can go ahead and move the source over to being a
146 * first-class texture source.
147 */
148 instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
149 instr->num_srcs++;
150 nir_instr_rewrite_src(&instr->instr,
151 &instr->src[instr->num_srcs - 1].src,
152 nir_src_for_ssa(indirect));
153
154 instr->sampler_array_size = array_elements;
155 }
156
157 if (location > shader_program->NumUniformStorage - 1 ||
158 !shader_program->UniformStorage[location].opaque[stage].active) {
159 assert(!"cannot return a sampler");
160 return;
161 }
162
163 instr->sampler_index +=
164 shader_program->UniformStorage[location].opaque[stage].index;
165
166 instr->sampler = NULL;
167 }
168
169 typedef struct {
170 nir_builder builder;
171 const struct gl_shader_program *shader_program;
172 gl_shader_stage stage;
173 } lower_state;
174
175 static bool
176 lower_block_cb(nir_block *block, void *_state)
177 {
178 lower_state *state = (lower_state *) _state;
179
180 nir_foreach_instr(block, instr) {
181 if (instr->type == nir_instr_type_tex) {
182 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
183 lower_sampler(tex_instr, state->shader_program, state->stage,
184 &state->builder);
185 }
186 }
187
188 return true;
189 }
190
191 static void
192 lower_impl(nir_function_impl *impl, const struct gl_shader_program *shader_program,
193 gl_shader_stage stage)
194 {
195 lower_state state;
196
197 nir_builder_init(&state.builder, impl);
198 state.shader_program = shader_program;
199 state.stage = stage;
200
201 nir_foreach_block(impl, lower_block_cb, &state);
202 }
203
204 void
205 nir_lower_samplers(nir_shader *shader,
206 const struct gl_shader_program *shader_program)
207 {
208 nir_foreach_overload(shader, overload) {
209 if (overload->impl)
210 lower_impl(overload->impl, shader_program, shader->stage);
211 }
212 }
213
214 static bool
215 lower_samplers_for_vk_block(nir_block *block, void *data)
216 {
217 nir_foreach_instr(block, instr) {
218 if (instr->type != nir_instr_type_tex)
219 continue;
220
221 nir_tex_instr *tex = nir_instr_as_tex(instr);
222
223 assert(tex->sampler);
224
225 tex->sampler_set = tex->sampler->var->data.descriptor_set;
226 tex->sampler_index = tex->sampler->var->data.binding;
227
228 if (tex->sampler->deref.child) {
229 assert(tex->sampler->deref.child->deref_type == nir_deref_type_array);
230 nir_deref_array *arr = nir_deref_as_array(tex->sampler->deref.child);
231
232 /* Only one-level arrays are allowed in vulkan */
233 assert(arr->deref.child == NULL);
234
235 tex->sampler_index += arr->base_offset;
236 if (arr->deref_array_type == nir_deref_array_type_indirect) {
237 add_indirect_to_tex(tex, arr->indirect);
238 nir_instr_rewrite_src(instr, &arr->indirect, NIR_SRC_INIT);
239
240 tex->sampler_array_size = glsl_get_length(tex->sampler->deref.type);
241 }
242 }
243
244 tex->sampler = NULL;
245 }
246
247 return true;
248 }
249
250 void
251 nir_lower_samplers_for_vk(nir_shader *shader)
252 {
253 nir_foreach_overload(shader, overload) {
254 if (overload->impl) {
255 nir_foreach_block(overload->impl, lower_samplers_for_vk_block, NULL);
256 }
257 }
258 }