i915g: Reduce state emission by using a index bias
[mesa.git] / src / gallium / drivers / r600 / r600_compiler.h
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #ifndef R600_COMPILER_H
24 #define R600_COMPILER_H
25
26 struct c_vector;
27
28 /* operand are the basic source/destination of each operation */
29 struct c_channel {
30 struct c_channel *next;
31 struct c_channel *prev;
32 unsigned vindex; /**< index in vector X,Y,Z,W (0,1,2,3) */
33 unsigned value; /**< immediate value 32bits */
34 struct c_vector *vector; /**< vector to which it belongs */
35 };
36
37 /* in GPU world most of the time operand are grouped into vector
38 * of 4 component this structure is mostly and handler to group
39 * operand into a same vector
40 */
41 struct c_vector {
42 struct c_vector *next;
43 struct c_vector *prev;
44 unsigned id; /**< vector uniq id */
45 unsigned name; /**< semantic name */
46 unsigned file; /**< operand file C_FILE_* */
47 int sid; /**< semantic id */
48 struct c_channel *channel[4]; /**< operands */
49 };
50
51 #define c_list_init(e) do { (e)->next = e; (e)->prev = e; } while(0)
52 #define c_list_add(e, h) do { (e)->next = (h)->next; (e)->prev = h; (h)->next = e; (e)->next->prev = e; } while(0)
53 #define c_list_add_tail(e, h) do { (e)->next = h; (e)->prev = (h)->prev; (h)->prev = e; (e)->prev->next = e; } while(0)
54 #define c_list_del(e) do { (e)->next->prev = (e)->prev; (e)->prev->next = (e)->next; c_list_init(e); } while(0)
55 #define c_list_for_each(p, h) for (p = (h)->next; p != (h); p = p->next)
56 #define c_list_for_each_from(p, s, h) for (p = s; p != (h); p = p->next)
57 #define c_list_for_each_safe(p, n, h) for (p = (h)->next, n = p->next; p != (h); p = n, n = p->next)
58 #define c_list_empty(h) ((h)->next == h)
59
60
61 #define C_PROGRAM_TYPE_VS 0
62 #define C_PROGRAM_TYPE_FS 1
63 #define C_PROGRAM_TYPE_COUNT 2
64
65 #define C_NODE_FLAG_ALU 1
66 #define C_NODE_FLAG_FETCH 2
67
68 #define C_SWIZZLE_X 0
69 #define C_SWIZZLE_Y 1
70 #define C_SWIZZLE_Z 2
71 #define C_SWIZZLE_W 3
72 #define C_SWIZZLE_0 4
73 #define C_SWIZZLE_1 5
74 #define C_SWIZZLE_D 6
75
76 #define C_FILE_NULL 0
77 #define C_FILE_CONSTANT 1
78 #define C_FILE_INPUT 2
79 #define C_FILE_OUTPUT 3
80 #define C_FILE_TEMPORARY 4
81 #define C_FILE_SAMPLER 5
82 #define C_FILE_ADDRESS 6
83 #define C_FILE_IMMEDIATE 7
84 #define C_FILE_LOOP 8
85 #define C_FILE_PREDICATE 9
86 #define C_FILE_SYSTEM_VALUE 10
87 #define C_FILE_RESOURCE 11
88 #define C_FILE_COUNT 12
89
90 #define C_SEMANTIC_POSITION 0
91 #define C_SEMANTIC_COLOR 1
92 #define C_SEMANTIC_BCOLOR 2 /**< back-face color */
93 #define C_SEMANTIC_FOG 3
94 #define C_SEMANTIC_PSIZE 4
95 #define C_SEMANTIC_GENERIC 5
96 #define C_SEMANTIC_NORMAL 6
97 #define C_SEMANTIC_FACE 7
98 #define C_SEMANTIC_EDGEFLAG 8
99 #define C_SEMANTIC_PRIMID 9
100 #define C_SEMANTIC_INSTANCEID 10
101 #define C_SEMANTIC_VERTEXID 11
102 #define C_SEMANTIC_COUNT 12 /**< number of semantic values */
103
104 #define C_OPCODE_NOP 0
105 #define C_OPCODE_MOV 1
106 #define C_OPCODE_LIT 2
107 #define C_OPCODE_RCP 3
108 #define C_OPCODE_RSQ 4
109 #define C_OPCODE_EXP 5
110 #define C_OPCODE_LOG 6
111 #define C_OPCODE_MUL 7
112 #define C_OPCODE_ADD 8
113 #define C_OPCODE_DP3 9
114 #define C_OPCODE_DP4 10
115 #define C_OPCODE_DST 11
116 #define C_OPCODE_MIN 12
117 #define C_OPCODE_MAX 13
118 #define C_OPCODE_SLT 14
119 #define C_OPCODE_SGE 15
120 #define C_OPCODE_MAD 16
121 #define C_OPCODE_SUB 17
122 #define C_OPCODE_LRP 18
123 #define C_OPCODE_CND 19
124 /* gap */
125 #define C_OPCODE_DP2A 21
126 /* gap */
127 #define C_OPCODE_FRC 24
128 #define C_OPCODE_CLAMP 25
129 #define C_OPCODE_FLR 26
130 #define C_OPCODE_ROUND 27
131 #define C_OPCODE_EX2 28
132 #define C_OPCODE_LG2 29
133 #define C_OPCODE_POW 30
134 #define C_OPCODE_XPD 31
135 /* gap */
136 #define C_OPCODE_ABS 33
137 #define C_OPCODE_RCC 34
138 #define C_OPCODE_DPH 35
139 #define C_OPCODE_COS 36
140 #define C_OPCODE_DDX 37
141 #define C_OPCODE_DDY 38
142 #define C_OPCODE_KILP 39 /* predicated kill */
143 #define C_OPCODE_PK2H 40
144 #define C_OPCODE_PK2US 41
145 #define C_OPCODE_PK4B 42
146 #define C_OPCODE_PK4UB 43
147 #define C_OPCODE_RFL 44
148 #define C_OPCODE_SEQ 45
149 #define C_OPCODE_SFL 46
150 #define C_OPCODE_SGT 47
151 #define C_OPCODE_SIN 48
152 #define C_OPCODE_SLE 49
153 #define C_OPCODE_SNE 50
154 #define C_OPCODE_STR 51
155 #define C_OPCODE_TEX 52
156 #define C_OPCODE_TXD 53
157 #define C_OPCODE_TXP 54
158 #define C_OPCODE_UP2H 55
159 #define C_OPCODE_UP2US 56
160 #define C_OPCODE_UP4B 57
161 #define C_OPCODE_UP4UB 58
162 #define C_OPCODE_X2D 59
163 #define C_OPCODE_ARA 60
164 #define C_OPCODE_ARR 61
165 #define C_OPCODE_BRA 62
166 #define C_OPCODE_CAL 63
167 #define C_OPCODE_RET 64
168 #define C_OPCODE_SSG 65 /* SGN */
169 #define C_OPCODE_CMP 66
170 #define C_OPCODE_SCS 67
171 #define C_OPCODE_TXB 68
172 #define C_OPCODE_NRM 69
173 #define C_OPCODE_DIV 70
174 #define C_OPCODE_DP2 71
175 #define C_OPCODE_TXL 72
176 #define C_OPCODE_BRK 73
177 #define C_OPCODE_IF 74
178 #define C_OPCODE_BGNFOR 75
179 #define C_OPCODE_REP 76
180 #define C_OPCODE_ELSE 77
181 #define C_OPCODE_ENDIF 78
182 #define C_OPCODE_ENDFOR 79
183 #define C_OPCODE_ENDREP 80
184 #define C_OPCODE_PUSHA 81
185 #define C_OPCODE_POPA 82
186 #define C_OPCODE_CEIL 83
187 #define C_OPCODE_I2F 84
188 #define C_OPCODE_NOT 85
189 #define C_OPCODE_TRUNC 86
190 #define C_OPCODE_SHL 87
191 /* gap */
192 #define C_OPCODE_AND 89
193 #define C_OPCODE_OR 90
194 #define C_OPCODE_MOD 91
195 #define C_OPCODE_XOR 92
196 #define C_OPCODE_SAD 93
197 #define C_OPCODE_TXF 94
198 #define C_OPCODE_TXQ 95
199 #define C_OPCODE_CONT 96
200 #define C_OPCODE_EMIT 97
201 #define C_OPCODE_ENDPRIM 98
202 #define C_OPCODE_BGNLOOP 99
203 #define C_OPCODE_BGNSUB 100
204 #define C_OPCODE_ENDLOOP 101
205 #define C_OPCODE_ENDSUB 102
206 /* gap */
207 #define C_OPCODE_NRM4 112
208 #define C_OPCODE_CALLNZ 113
209 #define C_OPCODE_IFC 114
210 #define C_OPCODE_BREAKC 115
211 #define C_OPCODE_KIL 116 /* conditional kill */
212 #define C_OPCODE_END 117 /* aka HALT */
213 /* gap */
214 #define C_OPCODE_F2I 119
215 #define C_OPCODE_IDIV 120
216 #define C_OPCODE_IMAX 121
217 #define C_OPCODE_IMIN 122
218 #define C_OPCODE_INEG 123
219 #define C_OPCODE_ISGE 124
220 #define C_OPCODE_ISHR 125
221 #define C_OPCODE_ISLT 126
222 #define C_OPCODE_F2U 127
223 #define C_OPCODE_U2F 128
224 #define C_OPCODE_UADD 129
225 #define C_OPCODE_UDIV 130
226 #define C_OPCODE_UMAD 131
227 #define C_OPCODE_UMAX 132
228 #define C_OPCODE_UMIN 133
229 #define C_OPCODE_UMOD 134
230 #define C_OPCODE_UMUL 135
231 #define C_OPCODE_USEQ 136
232 #define C_OPCODE_USGE 137
233 #define C_OPCODE_USHR 138
234 #define C_OPCODE_USLT 139
235 #define C_OPCODE_USNE 140
236 #define C_OPCODE_SWITCH 141
237 #define C_OPCODE_CASE 142
238 #define C_OPCODE_DEFAULT 143
239 #define C_OPCODE_ENDSWITCH 144
240 #define C_OPCODE_VFETCH 145
241 #define C_OPCODE_ENTRY 146
242 #define C_OPCODE_ARL 147
243 #define C_OPCODE_LAST 148
244
245 #define C_OPERAND_FLAG_ABS (1 << 0)
246 #define C_OPERAND_FLAG_NEG (1 << 1)
247
248 struct c_operand {
249 struct c_vector *vector;
250 unsigned swizzle;
251 unsigned flag;
252 };
253
254 struct c_op {
255 unsigned ninput;
256 struct c_operand input[3];
257 struct c_operand output;
258 unsigned opcode;
259 };
260
261 struct c_instruction {
262 struct c_instruction *next, *prev;
263 unsigned nop;
264 struct c_op op[5];
265 };
266
267 struct c_node;
268
269 struct c_node_link {
270 struct c_node_link *next;
271 struct c_node_link *prev;
272 struct c_node *node;
273 };
274
275 /**
276 * struct c_node
277 *
278 * @next: all node are in a double linked list, this point to
279 * next node
280 * @next: all node are in a double linked list, this point to
281 * previous node
282 * @predecessors: list of all predecessor nodes in the flow graph
283 * @successors: list of all sucessor nodes in the flow graph
284 * @parent: parent node in the depth first walk tree
285 * @childs: child nodes in the depth first walk tree
286 */
287 struct c_node {
288 struct c_node *next, *prev;
289 struct c_node_link predecessors;
290 struct c_node_link successors;
291 struct c_node *parent;
292 struct c_node_link childs;
293 struct c_instruction insts;
294 unsigned opcode;
295 unsigned visited;
296 unsigned done;
297 void *backend;
298 };
299
300 struct c_file {
301 unsigned nvectors;
302 struct c_vector vectors;
303 };
304
305 struct c_shader {
306 unsigned nvectors;
307 struct c_file files[C_FILE_COUNT];
308 struct c_node nodes;
309 struct c_node entry;
310 struct c_node end;
311 unsigned type;
312 };
313
314 int c_shader_init(struct c_shader *shader, unsigned type);
315 void c_shader_destroy(struct c_shader *shader);
316 struct c_vector *c_shader_vector_new(struct c_shader *shader, unsigned file, unsigned name, int sid);
317 int c_shader_build_dominator_tree(struct c_shader *shader);
318 void c_shader_dump(struct c_shader *shader);
319
320 void c_node_init(struct c_node *node);
321 int c_node_add_new_instruction(struct c_node *node, struct c_instruction *instruction);
322 int c_node_add_new_instruction_head(struct c_node *node, struct c_instruction *instruction);
323
324 /* control flow graph functions */
325 int c_node_cfg_link(struct c_node *predecessor, struct c_node *successor);
326 struct c_node *c_node_cfg_new_after(struct c_node *predecessor);
327 struct c_node *c_shader_cfg_new_node_after(struct c_shader *shader, struct c_node *predecessor);
328
329 struct c_vector *c_vector_new(void);
330
331 #endif