33db3ea0ff8330ed82aa8152dc6a55a913c3c8a2
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_program.h
1 /*
2 * Copyright (C) 2008 Nicolai Haehnle.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #ifndef __RADEON_PROGRAM_H_
29 #define __RADEON_PROGRAM_H_
30
31 #include <stdint.h>
32 #include <string.h>
33
34 #include "radeon_opcodes.h"
35 #include "radeon_code.h"
36 #include "radeon_program_constants.h"
37 #include "radeon_program_pair.h"
38
39 struct radeon_compiler;
40
41 struct rc_src_register {
42 rc_register_file File:3;
43
44 /** Negative values may be used for relative addressing. */
45 signed int Index:(RC_REGISTER_INDEX_BITS+1);
46 unsigned int RelAddr:1;
47
48 unsigned int Swizzle:12;
49
50 /** Take the component-wise absolute value */
51 unsigned int Abs:1;
52
53 /** Post-Abs negation. */
54 unsigned int Negate:4;
55 };
56
57 struct rc_dst_register {
58 rc_register_file File:3;
59
60 /** Negative values may be used for relative addressing. */
61 signed int Index:(RC_REGISTER_INDEX_BITS+1);
62 unsigned int RelAddr:1;
63
64 unsigned int WriteMask:4;
65 };
66
67 /**
68 * Instructions are maintained by the compiler in a doubly linked list
69 * of these structures.
70 *
71 * This instruction format is intended to be expanded for hardware-specific
72 * trickery. At different stages of compilation, a different set of
73 * instruction types may be valid.
74 */
75 struct rc_sub_instruction {
76 struct rc_src_register SrcReg[3];
77 struct rc_dst_register DstReg;
78
79 /**
80 * Opcode of this instruction, according to \ref rc_opcode enums.
81 */
82 rc_opcode Opcode:8;
83
84 /**
85 * Saturate each value of the result to the range [0,1] or [-1,1],
86 * according to \ref rc_saturate_mode enums.
87 */
88 rc_saturate_mode SaturateMode:2;
89
90 /**
91 * Writing to the special register RC_SPECIAL_ALU_RESULT
92 */
93 /*@{*/
94 rc_write_aluresult WriteALUResult:2;
95 rc_compare_func ALUResultCompare:3;
96 /*@}*/
97
98 /**
99 * \name Extra fields for TEX, TXB, TXD, TXL, TXP instructions.
100 */
101 /*@{*/
102 /** Source texture unit. */
103 unsigned int TexSrcUnit:5;
104
105 /** Source texture target, one of the \ref rc_texture_target enums */
106 rc_texture_target TexSrcTarget:3;
107
108 /** True if tex instruction should do shadow comparison */
109 unsigned int TexShadow:1;
110 /*@}*/
111 };
112
113 typedef enum {
114 RC_INSTRUCTION_NORMAL = 0,
115 RC_INSTRUCTION_PAIR
116 } rc_instruction_type;
117
118 struct rc_instruction {
119 struct rc_instruction * Prev;
120 struct rc_instruction * Next;
121
122 rc_instruction_type Type;
123 union {
124 struct rc_sub_instruction I;
125 struct rc_pair_instruction P;
126 } U;
127
128 /**
129 * Warning: IPs are not stable. If you want to use them,
130 * you need to recompute them at the beginning of each pass
131 * using \ref rc_recompute_ips
132 */
133 unsigned int IP;
134 };
135
136 struct rc_program {
137 /**
138 * Instructions.Next points to the first instruction,
139 * Instructions.Prev points to the last instruction.
140 */
141 struct rc_instruction Instructions;
142
143 /* Long term, we should probably remove InputsRead & OutputsWritten,
144 * since updating dependent state can be fragile, and they aren't
145 * actually used very often. */
146 uint32_t InputsRead;
147 uint32_t OutputsWritten;
148 uint32_t ShadowSamplers; /**< Texture units used for shadow sampling. */
149
150 struct rc_constant_list Constants;
151 };
152
153 enum {
154 OPCODE_REPL_ALPHA = MAX_RC_OPCODE /**< used in paired instructions */
155 };
156
157
158 static inline rc_swizzle get_swz(unsigned int swz, rc_swizzle idx)
159 {
160 if (idx & 0x4)
161 return idx;
162 return GET_SWZ(swz, idx);
163 }
164
165 static inline unsigned int combine_swizzles4(unsigned int src,
166 rc_swizzle swz_x, rc_swizzle swz_y, rc_swizzle swz_z, rc_swizzle swz_w)
167 {
168 unsigned int ret = 0;
169
170 ret |= get_swz(src, swz_x);
171 ret |= get_swz(src, swz_y) << 3;
172 ret |= get_swz(src, swz_z) << 6;
173 ret |= get_swz(src, swz_w) << 9;
174
175 return ret;
176 }
177
178 static inline unsigned int combine_swizzles(unsigned int src, unsigned int swz)
179 {
180 unsigned int ret = 0;
181
182 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_X));
183 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_Y)) << 3;
184 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_Z)) << 6;
185 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_W)) << 9;
186
187 return ret;
188 }
189
190 struct rc_src_register lmul_swizzle(unsigned int swizzle, struct rc_src_register srcreg);
191
192 static inline void reset_srcreg(struct rc_src_register* reg)
193 {
194 memset(reg, 0, sizeof(reg));
195 reg->Swizzle = RC_SWIZZLE_XYZW;
196 }
197
198
199 /**
200 * A transformation that can be passed to \ref radeonLocalTransform.
201 *
202 * The function will be called once for each instruction.
203 * It has to either emit the appropriate transformed code for the instruction
204 * and return true, or return false if it doesn't understand the
205 * instruction.
206 *
207 * The function gets passed the userData as last parameter.
208 */
209 struct radeon_program_transformation {
210 int (*function)(
211 struct radeon_compiler*,
212 struct rc_instruction*,
213 void*);
214 void *userData;
215 };
216
217 void radeonLocalTransform(
218 struct radeon_compiler *c,
219 int num_transformations,
220 struct radeon_program_transformation* transformations);
221
222 unsigned int rc_find_free_temporary(struct radeon_compiler * c);
223
224 struct rc_instruction *rc_alloc_instruction(struct radeon_compiler * c);
225 struct rc_instruction *rc_insert_new_instruction(struct radeon_compiler * c, struct rc_instruction * after);
226 void rc_insert_instruction(struct rc_instruction * after, struct rc_instruction * inst);
227 void rc_remove_instruction(struct rc_instruction * inst);
228
229 unsigned int rc_recompute_ips(struct radeon_compiler * c);
230
231 void rc_print_program(const struct rc_program *prog);
232
233 #endif