Merge branch 'wip/i965-separate-sampler-tex' into vulkan
[mesa.git] / src / glsl / nir / nir_builder.h
1 /*
2 * Copyright © 2014-2015 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef NIR_BUILDER_H
25 #define NIR_BUILDER_H
26
27 #include "nir_control_flow.h"
28
29 struct exec_list;
30
31 typedef struct nir_builder {
32 nir_cursor cursor;
33
34 nir_shader *shader;
35 nir_function_impl *impl;
36 } nir_builder;
37
38 static inline void
39 nir_builder_init(nir_builder *build, nir_function_impl *impl)
40 {
41 memset(build, 0, sizeof(*build));
42 build->impl = impl;
43 build->shader = impl->overload->function->shader;
44 }
45
46 static inline void
47 nir_builder_instr_insert(nir_builder *build, nir_instr *instr)
48 {
49 nir_instr_insert(build->cursor, instr);
50
51 /* Move the cursor forward. */
52 build->cursor = nir_after_instr(instr);
53 }
54
55 static inline void
56 nir_builder_cf_insert(nir_builder *build, nir_cf_node *cf)
57 {
58 nir_cf_node_insert(build->cursor, cf);
59 }
60
61 static inline nir_ssa_def *
62 nir_ssa_undef(nir_builder *build, unsigned num_components)
63 {
64 nir_ssa_undef_instr *undef =
65 nir_ssa_undef_instr_create(build->shader, num_components);
66 if (!undef)
67 return NULL;
68
69 nir_instr_insert(nir_before_block(nir_start_block(build->impl)),
70 &undef->instr);
71
72 return &undef->def;
73 }
74
75 static inline nir_ssa_def *
76 nir_build_imm(nir_builder *build, unsigned num_components, nir_const_value value)
77 {
78 nir_load_const_instr *load_const =
79 nir_load_const_instr_create(build->shader, num_components);
80 if (!load_const)
81 return NULL;
82
83 load_const->value = value;
84
85 nir_builder_instr_insert(build, &load_const->instr);
86
87 return &load_const->def;
88 }
89
90 static inline nir_ssa_def *
91 nir_imm_float(nir_builder *build, float x)
92 {
93 nir_const_value v;
94
95 memset(&v, 0, sizeof(v));
96 v.f[0] = x;
97
98 return nir_build_imm(build, 1, v);
99 }
100
101 static inline nir_ssa_def *
102 nir_imm_vec4(nir_builder *build, float x, float y, float z, float w)
103 {
104 nir_const_value v;
105
106 memset(&v, 0, sizeof(v));
107 v.f[0] = x;
108 v.f[1] = y;
109 v.f[2] = z;
110 v.f[3] = w;
111
112 return nir_build_imm(build, 4, v);
113 }
114
115 static inline nir_ssa_def *
116 nir_imm_int(nir_builder *build, int x)
117 {
118 nir_const_value v;
119
120 memset(&v, 0, sizeof(v));
121 v.i[0] = x;
122
123 return nir_build_imm(build, 1, v);
124 }
125
126 static inline nir_ssa_def *
127 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
128 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
129 {
130 const nir_op_info *op_info = &nir_op_infos[op];
131 nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
132 if (!instr)
133 return NULL;
134
135 instr->src[0].src = nir_src_for_ssa(src0);
136 if (src1)
137 instr->src[1].src = nir_src_for_ssa(src1);
138 if (src2)
139 instr->src[2].src = nir_src_for_ssa(src2);
140 if (src3)
141 instr->src[3].src = nir_src_for_ssa(src3);
142
143 /* Guess the number of components the destination temporary should have
144 * based on our input sizes, if it's not fixed for the op.
145 */
146 unsigned num_components = op_info->output_size;
147 if (num_components == 0) {
148 for (unsigned i = 0; i < op_info->num_inputs; i++) {
149 if (op_info->input_sizes[i] == 0)
150 num_components = MAX2(num_components,
151 instr->src[i].src.ssa->num_components);
152 }
153 }
154 assert(num_components != 0);
155
156 /* Make sure we don't swizzle from outside of our source vector (like if a
157 * scalar value was passed into a multiply with a vector).
158 */
159 for (unsigned i = 0; i < op_info->num_inputs; i++) {
160 for (unsigned j = instr->src[i].src.ssa->num_components; j < 4; j++) {
161 instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
162 }
163 }
164
165 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, NULL);
166 instr->dest.write_mask = (1 << num_components) - 1;
167
168 nir_builder_instr_insert(build, &instr->instr);
169
170 return &instr->dest.dest.ssa;
171 }
172
173 #define ALU1(op) \
174 static inline nir_ssa_def * \
175 nir_##op(nir_builder *build, nir_ssa_def *src0) \
176 { \
177 return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL); \
178 }
179
180 #define ALU2(op) \
181 static inline nir_ssa_def * \
182 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1) \
183 { \
184 return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL); \
185 }
186
187 #define ALU3(op) \
188 static inline nir_ssa_def * \
189 nir_##op(nir_builder *build, nir_ssa_def *src0, \
190 nir_ssa_def *src1, nir_ssa_def *src2) \
191 { \
192 return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL); \
193 }
194
195 #define ALU4(op) \
196 static inline nir_ssa_def * \
197 nir_##op(nir_builder *build, nir_ssa_def *src0, \
198 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3) \
199 { \
200 return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3); \
201 }
202
203 #include "nir_builder_opcodes.h"
204
205 static inline nir_ssa_def *
206 nir_vec(nir_builder *build, nir_ssa_def **comp, unsigned num_components)
207 {
208 switch (num_components) {
209 case 4:
210 return nir_vec4(build, comp[0], comp[1], comp[2], comp[3]);
211 case 3:
212 return nir_vec3(build, comp[0], comp[1], comp[2]);
213 case 2:
214 return nir_vec2(build, comp[0], comp[1]);
215 case 1:
216 return comp[0];
217 default:
218 unreachable("bad component count");
219 return NULL;
220 }
221 }
222
223 /**
224 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
225 */
226 static inline nir_ssa_def *
227 nir_fmov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
228 {
229 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_fmov);
230 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
231 mov->dest.write_mask = (1 << num_components) - 1;
232 mov->src[0] = src;
233 nir_builder_instr_insert(build, &mov->instr);
234
235 return &mov->dest.dest.ssa;
236 }
237
238 static inline nir_ssa_def *
239 nir_imov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
240 {
241 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_imov);
242 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
243 mov->dest.write_mask = (1 << num_components) - 1;
244 mov->src[0] = src;
245 nir_builder_instr_insert(build, &mov->instr);
246
247 return &mov->dest.dest.ssa;
248 }
249
250 /**
251 * Construct an fmov or imov that reswizzles the source's components.
252 */
253 static inline nir_ssa_def *
254 nir_swizzle(nir_builder *build, nir_ssa_def *src, unsigned swiz[4],
255 unsigned num_components, bool use_fmov)
256 {
257 nir_alu_src alu_src = { NIR_SRC_INIT };
258 alu_src.src = nir_src_for_ssa(src);
259 for (int i = 0; i < 4; i++)
260 alu_src.swizzle[i] = swiz[i];
261
262 return use_fmov ? nir_fmov_alu(build, alu_src, num_components) :
263 nir_imov_alu(build, alu_src, num_components);
264 }
265
266 /* Selects the right fdot given the number of components in each source. */
267 static inline nir_ssa_def *
268 nir_fdot(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1)
269 {
270 assert(src0->num_components == src1->num_components);
271 switch (src0->num_components) {
272 case 1: return nir_fmul(build, src0, src1);
273 case 2: return nir_fdot2(build, src0, src1);
274 case 3: return nir_fdot3(build, src0, src1);
275 case 4: return nir_fdot4(build, src0, src1);
276 default:
277 unreachable("bad component size");
278 }
279
280 return NULL;
281 }
282
283 static inline nir_ssa_def *
284 nir_channel(nir_builder *b, nir_ssa_def *def, unsigned c)
285 {
286 unsigned swizzle[4] = {c, c, c, c};
287 return nir_swizzle(b, def, swizzle, 1, false);
288 }
289
290 /**
291 * Turns a nir_src into a nir_ssa_def * so it can be passed to
292 * nir_build_alu()-based builder calls.
293 */
294 static inline nir_ssa_def *
295 nir_ssa_for_src(nir_builder *build, nir_src src, int num_components)
296 {
297 if (src.is_ssa && src.ssa->num_components == num_components)
298 return src.ssa;
299
300 nir_alu_src alu = { NIR_SRC_INIT };
301 alu.src = src;
302 for (int j = 0; j < 4; j++)
303 alu.swizzle[j] = j;
304
305 return nir_imov_alu(build, alu, num_components);
306 }
307
308 static inline nir_ssa_def *
309 nir_load_var(nir_builder *build, nir_variable *var)
310 {
311 const unsigned num_components = glsl_get_vector_elements(var->type);
312
313 nir_intrinsic_instr *load =
314 nir_intrinsic_instr_create(build->shader, nir_intrinsic_load_var);
315 load->num_components = num_components;
316 load->variables[0] = nir_deref_var_create(load, var);
317 nir_ssa_dest_init(&load->instr, &load->dest, num_components, NULL);
318 nir_builder_instr_insert(build, &load->instr);
319 return &load->dest.ssa;
320 }
321
322 static inline void
323 nir_store_var(nir_builder *build, nir_variable *var, nir_ssa_def *value)
324 {
325 const unsigned num_components = glsl_get_vector_elements(var->type);
326
327 nir_intrinsic_instr *store =
328 nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_var);
329 store->num_components = num_components;
330 store->variables[0] = nir_deref_var_create(store, var);
331 store->src[0] = nir_src_for_ssa(value);
332 nir_builder_instr_insert(build, &store->instr);
333 }
334
335 #endif /* NIR_BUILDER_H */