OSDN Git Service

nir/builder: Add a nir_build_ivec4() convenience helper.
[android-x86/external-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->function->shader;
44 }
45
46 static inline void
47 nir_builder_init_simple_shader(nir_builder *build, void *mem_ctx,
48                                gl_shader_stage stage,
49                                const nir_shader_compiler_options *options)
50 {
51    build->shader = nir_shader_create(mem_ctx, stage, options);
52    nir_function *func = nir_function_create(build->shader, "main");
53    build->impl = nir_function_impl_create(func);
54    build->cursor = nir_after_cf_list(&build->impl->body);
55 }
56
57 static inline void
58 nir_builder_instr_insert(nir_builder *build, nir_instr *instr)
59 {
60    nir_instr_insert(build->cursor, instr);
61
62    /* Move the cursor forward. */
63    build->cursor = nir_after_instr(instr);
64 }
65
66 static inline void
67 nir_builder_cf_insert(nir_builder *build, nir_cf_node *cf)
68 {
69    nir_cf_node_insert(build->cursor, cf);
70 }
71
72 static inline nir_ssa_def *
73 nir_build_imm(nir_builder *build, unsigned num_components, nir_const_value value)
74 {
75    nir_load_const_instr *load_const =
76       nir_load_const_instr_create(build->shader, num_components);
77    if (!load_const)
78       return NULL;
79
80    load_const->value = value;
81
82    nir_builder_instr_insert(build, &load_const->instr);
83
84    return &load_const->def;
85 }
86
87 static inline nir_ssa_def *
88 nir_imm_float(nir_builder *build, float x)
89 {
90    nir_const_value v;
91
92    memset(&v, 0, sizeof(v));
93    v.f[0] = x;
94
95    return nir_build_imm(build, 1, v);
96 }
97
98 static inline nir_ssa_def *
99 nir_imm_vec4(nir_builder *build, float x, float y, float z, float w)
100 {
101    nir_const_value v;
102
103    memset(&v, 0, sizeof(v));
104    v.f[0] = x;
105    v.f[1] = y;
106    v.f[2] = z;
107    v.f[3] = w;
108
109    return nir_build_imm(build, 4, v);
110 }
111
112 static inline nir_ssa_def *
113 nir_imm_int(nir_builder *build, int x)
114 {
115    nir_const_value v;
116
117    memset(&v, 0, sizeof(v));
118    v.i[0] = x;
119
120    return nir_build_imm(build, 1, v);
121 }
122
123 static inline nir_ssa_def *
124 nir_imm_ivec4(nir_builder *build, int x, int y, int z, int w)
125 {
126    nir_const_value v;
127
128    memset(&v, 0, sizeof(v));
129    v.i[0] = x;
130    v.i[1] = y;
131    v.i[2] = z;
132    v.i[3] = w;
133
134    return nir_build_imm(build, 4, v);
135 }
136
137 static inline nir_ssa_def *
138 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
139               nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
140 {
141    const nir_op_info *op_info = &nir_op_infos[op];
142    nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
143    if (!instr)
144       return NULL;
145
146    instr->src[0].src = nir_src_for_ssa(src0);
147    if (src1)
148       instr->src[1].src = nir_src_for_ssa(src1);
149    if (src2)
150       instr->src[2].src = nir_src_for_ssa(src2);
151    if (src3)
152       instr->src[3].src = nir_src_for_ssa(src3);
153
154    /* Guess the number of components the destination temporary should have
155     * based on our input sizes, if it's not fixed for the op.
156     */
157    unsigned num_components = op_info->output_size;
158    if (num_components == 0) {
159       for (unsigned i = 0; i < op_info->num_inputs; i++) {
160          if (op_info->input_sizes[i] == 0)
161             num_components = MAX2(num_components,
162                                   instr->src[i].src.ssa->num_components);
163       }
164    }
165    assert(num_components != 0);
166
167    /* Make sure we don't swizzle from outside of our source vector (like if a
168     * scalar value was passed into a multiply with a vector).
169     */
170    for (unsigned i = 0; i < op_info->num_inputs; i++) {
171       for (unsigned j = instr->src[i].src.ssa->num_components; j < 4; j++) {
172          instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
173       }
174    }
175
176    nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, NULL);
177    instr->dest.write_mask = (1 << num_components) - 1;
178
179    nir_builder_instr_insert(build, &instr->instr);
180
181    return &instr->dest.dest.ssa;
182 }
183
184 #define ALU1(op)                                                          \
185 static inline nir_ssa_def *                                               \
186 nir_##op(nir_builder *build, nir_ssa_def *src0)                           \
187 {                                                                         \
188    return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL);      \
189 }
190
191 #define ALU2(op)                                                          \
192 static inline nir_ssa_def *                                               \
193 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1)        \
194 {                                                                         \
195    return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL);      \
196 }
197
198 #define ALU3(op)                                                          \
199 static inline nir_ssa_def *                                               \
200 nir_##op(nir_builder *build, nir_ssa_def *src0,                           \
201          nir_ssa_def *src1, nir_ssa_def *src2)                            \
202 {                                                                         \
203    return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL);      \
204 }
205
206 #define ALU4(op)                                                          \
207 static inline nir_ssa_def *                                               \
208 nir_##op(nir_builder *build, nir_ssa_def *src0,                           \
209          nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)         \
210 {                                                                         \
211    return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3);      \
212 }
213
214 #include "nir_builder_opcodes.h"
215
216 static inline nir_ssa_def *
217 nir_vec(nir_builder *build, nir_ssa_def **comp, unsigned num_components)
218 {
219    switch (num_components) {
220    case 4:
221       return nir_vec4(build, comp[0], comp[1], comp[2], comp[3]);
222    case 3:
223       return nir_vec3(build, comp[0], comp[1], comp[2]);
224    case 2:
225       return nir_vec2(build, comp[0], comp[1]);
226    case 1:
227       return comp[0];
228    default:
229       unreachable("bad component count");
230       return NULL;
231    }
232 }
233
234 /**
235  * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
236  */
237 static inline nir_ssa_def *
238 nir_fmov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
239 {
240    nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_fmov);
241    nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
242    mov->dest.write_mask = (1 << num_components) - 1;
243    mov->src[0] = src;
244    nir_builder_instr_insert(build, &mov->instr);
245
246    return &mov->dest.dest.ssa;
247 }
248
249 static inline nir_ssa_def *
250 nir_imov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
251 {
252    nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_imov);
253    nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
254    mov->dest.write_mask = (1 << num_components) - 1;
255    mov->src[0] = src;
256    nir_builder_instr_insert(build, &mov->instr);
257
258    return &mov->dest.dest.ssa;
259 }
260
261 /**
262  * Construct an fmov or imov that reswizzles the source's components.
263  */
264 static inline nir_ssa_def *
265 nir_swizzle(nir_builder *build, nir_ssa_def *src, unsigned swiz[4],
266             unsigned num_components, bool use_fmov)
267 {
268    nir_alu_src alu_src = { NIR_SRC_INIT };
269    alu_src.src = nir_src_for_ssa(src);
270    for (unsigned i = 0; i < num_components; i++)
271       alu_src.swizzle[i] = swiz[i];
272
273    return use_fmov ? nir_fmov_alu(build, alu_src, num_components) :
274                      nir_imov_alu(build, alu_src, num_components);
275 }
276
277 static inline nir_ssa_def *
278 nir_channel(nir_builder *b, nir_ssa_def *def, unsigned c)
279 {
280    unsigned swizzle[4] = {c, c, c, c};
281    return nir_swizzle(b, def, swizzle, 1, false);
282 }
283
284 /**
285  * Turns a nir_src into a nir_ssa_def * so it can be passed to
286  * nir_build_alu()-based builder calls.
287  *
288  * See nir_ssa_for_alu_src() for alu instructions.
289  */
290 static inline nir_ssa_def *
291 nir_ssa_for_src(nir_builder *build, nir_src src, int num_components)
292 {
293    if (src.is_ssa && src.ssa->num_components == num_components)
294       return src.ssa;
295
296    nir_alu_src alu = { NIR_SRC_INIT };
297    alu.src = src;
298    for (int j = 0; j < 4; j++)
299       alu.swizzle[j] = j;
300
301    return nir_imov_alu(build, alu, num_components);
302 }
303
304 /**
305  * Similar to nir_ssa_for_src(), but for alu src's, respecting the
306  * nir_alu_src's swizzle.
307  */
308 static inline nir_ssa_def *
309 nir_ssa_for_alu_src(nir_builder *build, nir_alu_instr *instr, unsigned srcn)
310 {
311    static uint8_t trivial_swizzle[4] = { 0, 1, 2, 3 };
312    nir_alu_src *src = &instr->src[srcn];
313    unsigned num_components = nir_ssa_alu_instr_src_components(instr, srcn);
314
315    if (src->src.is_ssa && (src->src.ssa->num_components == num_components) &&
316        !src->abs && !src->negate &&
317        (memcmp(src->swizzle, trivial_swizzle, num_components) == 0))
318       return src->src.ssa;
319
320    return nir_imov_alu(build, *src, num_components);
321 }
322
323 static inline nir_ssa_def *
324 nir_load_var(nir_builder *build, nir_variable *var)
325 {
326    const unsigned num_components = glsl_get_vector_elements(var->type);
327
328    nir_intrinsic_instr *load =
329       nir_intrinsic_instr_create(build->shader, nir_intrinsic_load_var);
330    load->num_components = num_components;
331    load->variables[0] = nir_deref_var_create(load, var);
332    nir_ssa_dest_init(&load->instr, &load->dest, num_components, NULL);
333    nir_builder_instr_insert(build, &load->instr);
334    return &load->dest.ssa;
335 }
336
337 static inline void
338 nir_store_var(nir_builder *build, nir_variable *var, nir_ssa_def *value,
339               unsigned writemask)
340 {
341    const unsigned num_components = glsl_get_vector_elements(var->type);
342
343    nir_intrinsic_instr *store =
344       nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_var);
345    store->num_components = num_components;
346    store->const_index[0] = writemask;
347    store->variables[0] = nir_deref_var_create(store, var);
348    store->src[0] = nir_src_for_ssa(value);
349    nir_builder_instr_insert(build, &store->instr);
350 }
351
352 static inline nir_ssa_def *
353 nir_load_system_value(nir_builder *build, nir_intrinsic_op op, int index)
354 {
355    nir_intrinsic_instr *load = nir_intrinsic_instr_create(build->shader, op);
356    load->num_components = nir_intrinsic_infos[op].dest_components;
357    load->const_index[0] = index;
358    nir_ssa_dest_init(&load->instr, &load->dest,
359                      nir_intrinsic_infos[op].dest_components, NULL);
360    nir_builder_instr_insert(build, &load->instr);
361    return &load->dest.ssa;
362 }
363
364 #endif /* NIR_BUILDER_H */