7c4f7fd96cd14308d297a87fec79754f5ae4cd0c
[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 struct exec_list;
28
29 typedef struct nir_builder {
30 struct exec_list *cf_node_list;
31 nir_shader *shader;
32 nir_function_impl *impl;
33 } nir_builder;
34
35 static inline void
36 nir_builder_init(nir_builder *build, nir_function_impl *impl)
37 {
38 memset(build, 0, sizeof(*build));
39 build->impl = impl;
40 build->shader = impl->overload->function->shader;
41 }
42
43 static inline void
44 nir_builder_insert_after_cf_list(nir_builder *build,
45 struct exec_list *cf_node_list)
46 {
47 build->cf_node_list = cf_node_list;
48 }
49
50
51 static inline nir_ssa_def *
52 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
53 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
54 {
55 const nir_op_info *op_info = &nir_op_infos[op];
56 nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
57 if (!instr)
58 return NULL;
59
60 instr->src[0].src = nir_src_for_ssa(src0);
61 if (src1)
62 instr->src[1].src = nir_src_for_ssa(src1);
63 if (src2)
64 instr->src[2].src = nir_src_for_ssa(src2);
65 if (src3)
66 instr->src[3].src = nir_src_for_ssa(src3);
67
68 /* Guess the number of components the destination temporary should have
69 * based on our input sizes, if it's not fixed for the op.
70 */
71 unsigned num_components = op_info->output_size;
72 if (num_components == 0) {
73 for (unsigned i = 0; i < op_info->num_inputs; i++) {
74 if (op_info->input_sizes[i] == 0)
75 num_components = MAX2(num_components,
76 instr->src[i].src.ssa->num_components);
77 }
78 }
79 assert(num_components != 0);
80
81 /* Make sure we don't swizzle from outside of our source vector (like if a
82 * scalar value was passed into a multiply with a vector).
83 */
84 for (unsigned i = 0; i < op_info->num_inputs; i++) {
85 for (unsigned j = instr->src[i].src.ssa->num_components; j < 4; j++) {
86 instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
87 }
88 }
89
90 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, NULL);
91 instr->dest.write_mask = (1 << num_components) - 1;
92
93 nir_instr_insert_after_cf_list(build->cf_node_list, &instr->instr);
94
95 return &instr->dest.dest.ssa;
96 }
97
98 #define ALU1(op) \
99 static inline nir_ssa_def * \
100 nir_##op(nir_builder *build, nir_ssa_def *src0) \
101 { \
102 return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL); \
103 }
104
105 #define ALU2(op) \
106 static inline nir_ssa_def * \
107 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1) \
108 { \
109 return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL); \
110 }
111
112 #define ALU3(op) \
113 static inline nir_ssa_def * \
114 nir_##op(nir_builder *build, nir_ssa_def *src0, \
115 nir_ssa_def *src1, nir_ssa_def *src2) \
116 { \
117 return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL); \
118 }
119
120 #define ALU4(op) \
121 static inline nir_ssa_def * \
122 nir_##op(nir_builder *build, nir_ssa_def *src0, \
123 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3) \
124 { \
125 return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3); \
126 }
127
128 #include "nir_builder_opcodes.h"
129
130 #endif /* NIR_BUILDER_H */