tgsi: make function call code in tgsi_sse.c less opaque
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_exec.h
1 /**************************************************************************
2 *
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef TGSI_EXEC_H
29 #define TGSI_EXEC_H
30
31 #include "pipe/p_compiler.h"
32
33 #if defined __cplusplus
34 extern "C" {
35 #endif
36
37 #define MAX_LABELS (4 * 1024) /**< basically, max instructions */
38
39 #define NUM_CHANNELS 4 /* R,G,B,A */
40 #define QUAD_SIZE 4 /* 4 pixel/quad */
41
42 /**
43 * Registers may be treated as float, signed int or unsigned int.
44 */
45 union tgsi_exec_channel
46 {
47 float f[QUAD_SIZE];
48 int i[QUAD_SIZE];
49 unsigned u[QUAD_SIZE];
50 };
51
52 /**
53 * A vector[RGBA] of channels[4 pixels]
54 */
55 struct tgsi_exec_vector
56 {
57 union tgsi_exec_channel xyzw[NUM_CHANNELS];
58 };
59
60 /**
61 * For fragment programs, information for computing fragment input
62 * values from plane equation of the triangle/line.
63 */
64 struct tgsi_interp_coef
65 {
66 float a0[NUM_CHANNELS]; /* in an xyzw layout */
67 float dadx[NUM_CHANNELS];
68 float dady[NUM_CHANNELS];
69 };
70
71 /**
72 * Information for sampling textures, which must be implemented
73 * by code outside the TGSI executor.
74 */
75 struct tgsi_sampler
76 {
77 /** Get samples for four fragments in a quad */
78 void (*get_samples)(struct tgsi_sampler *sampler,
79 const float s[QUAD_SIZE],
80 const float t[QUAD_SIZE],
81 const float p[QUAD_SIZE],
82 float lodbias,
83 float rgba[NUM_CHANNELS][QUAD_SIZE]);
84 };
85
86 /**
87 * For branching/calling subroutines.
88 */
89 struct tgsi_exec_labels
90 {
91 unsigned labels[MAX_LABELS][2];
92 unsigned count;
93 };
94
95
96 #define TGSI_EXEC_NUM_TEMPS 128
97 #define TGSI_EXEC_NUM_IMMEDIATES 256
98
99 /*
100 * Locations of various utility registers (_I = Index, _C = Channel)
101 */
102 #define TGSI_EXEC_TEMP_00000000_I (TGSI_EXEC_NUM_TEMPS + 0)
103 #define TGSI_EXEC_TEMP_00000000_C 0
104
105 #define TGSI_EXEC_TEMP_7FFFFFFF_I (TGSI_EXEC_NUM_TEMPS + 0)
106 #define TGSI_EXEC_TEMP_7FFFFFFF_C 1
107
108 #define TGSI_EXEC_TEMP_80000000_I (TGSI_EXEC_NUM_TEMPS + 0)
109 #define TGSI_EXEC_TEMP_80000000_C 2
110
111 #define TGSI_EXEC_TEMP_FFFFFFFF_I (TGSI_EXEC_NUM_TEMPS + 0)
112 #define TGSI_EXEC_TEMP_FFFFFFFF_C 3
113
114 #define TGSI_EXEC_TEMP_ONE_I (TGSI_EXEC_NUM_TEMPS + 1)
115 #define TGSI_EXEC_TEMP_ONE_C 0
116
117 #define TGSI_EXEC_TEMP_TWO_I (TGSI_EXEC_NUM_TEMPS + 1)
118 #define TGSI_EXEC_TEMP_TWO_C 1
119
120 #define TGSI_EXEC_TEMP_128_I (TGSI_EXEC_NUM_TEMPS + 1)
121 #define TGSI_EXEC_TEMP_128_C 2
122
123 #define TGSI_EXEC_TEMP_MINUS_128_I (TGSI_EXEC_NUM_TEMPS + 1)
124 #define TGSI_EXEC_TEMP_MINUS_128_C 3
125
126 #define TGSI_EXEC_TEMP_KILMASK_I (TGSI_EXEC_NUM_TEMPS + 2)
127 #define TGSI_EXEC_TEMP_KILMASK_C 0
128
129 #define TGSI_EXEC_TEMP_OUTPUT_I (TGSI_EXEC_NUM_TEMPS + 2)
130 #define TGSI_EXEC_TEMP_OUTPUT_C 1
131
132 #define TGSI_EXEC_TEMP_PRIMITIVE_I (TGSI_EXEC_NUM_TEMPS + 2)
133 #define TGSI_EXEC_TEMP_PRIMITIVE_C 2
134
135 /* NVIDIA condition code (CC) vector
136 */
137 #define TGSI_EXEC_CC_GT 0x01
138 #define TGSI_EXEC_CC_EQ 0x02
139 #define TGSI_EXEC_CC_LT 0x04
140 #define TGSI_EXEC_CC_UN 0x08
141
142 #define TGSI_EXEC_CC_X_MASK 0x000000ff
143 #define TGSI_EXEC_CC_X_SHIFT 0
144 #define TGSI_EXEC_CC_Y_MASK 0x0000ff00
145 #define TGSI_EXEC_CC_Y_SHIFT 8
146 #define TGSI_EXEC_CC_Z_MASK 0x00ff0000
147 #define TGSI_EXEC_CC_Z_SHIFT 16
148 #define TGSI_EXEC_CC_W_MASK 0xff000000
149 #define TGSI_EXEC_CC_W_SHIFT 24
150
151 #define TGSI_EXEC_TEMP_CC_I (TGSI_EXEC_NUM_TEMPS + 2)
152 #define TGSI_EXEC_TEMP_CC_C 3
153
154 #define TGSI_EXEC_TEMP_THREE_I (TGSI_EXEC_NUM_TEMPS + 3)
155 #define TGSI_EXEC_TEMP_THREE_C 0
156
157 #define TGSI_EXEC_TEMP_HALF_I (TGSI_EXEC_NUM_TEMPS + 3)
158 #define TGSI_EXEC_TEMP_HALF_C 1
159
160 /* execution mask, each value is either 0 or ~0 */
161 #define TGSI_EXEC_MASK_I (TGSI_EXEC_NUM_TEMPS + 3)
162 #define TGSI_EXEC_MASK_C 2
163
164 /* 4 register buffer for various purposes */
165 #define TGSI_EXEC_TEMP_R0 (TGSI_EXEC_NUM_TEMPS + 4)
166 #define TGSI_EXEC_NUM_TEMP_R 4
167
168 #define TGSI_EXEC_TEMP_ADDR (TGSI_EXEC_NUM_TEMPS + 8)
169 #define TGSI_EXEC_NUM_ADDRS 1
170 #define TGSI_EXEC_NUM_TEMP_EXTRAS 9
171
172
173
174 #define TGSI_EXEC_MAX_COND_NESTING 20
175 #define TGSI_EXEC_MAX_LOOP_NESTING 20
176 #define TGSI_EXEC_MAX_CALL_NESTING 20
177
178 /* The maximum number of input attributes per vertex. For 2D
179 * input register files, this is the stride between two 1D
180 * arrays.
181 */
182 #define TGSI_EXEC_MAX_INPUT_ATTRIBS 17
183
184 /* The maximum number of constant vectors per constant buffer.
185 */
186 #define TGSI_EXEC_MAX_CONST_BUFFER 4096
187
188 /**
189 * Run-time virtual machine state for executing TGSI shader.
190 */
191 struct tgsi_exec_machine
192 {
193 /* Total = program temporaries + internal temporaries
194 */
195 struct tgsi_exec_vector Temps[TGSI_EXEC_NUM_TEMPS +
196 TGSI_EXEC_NUM_TEMP_EXTRAS];
197
198 float Imms[TGSI_EXEC_NUM_IMMEDIATES][4];
199
200 struct tgsi_exec_vector *Addrs;
201
202 struct tgsi_sampler **Samplers;
203
204 unsigned ImmLimit;
205 const float (*Consts)[4];
206 struct tgsi_exec_vector *Inputs;
207 struct tgsi_exec_vector *Outputs;
208 const struct tgsi_token *Tokens; /**< Declarations, instructions */
209 unsigned Processor; /**< TGSI_PROCESSOR_x */
210
211 /* GEOMETRY processor only. */
212 unsigned *Primitives;
213
214 /* FRAGMENT processor only. */
215 const struct tgsi_interp_coef *InterpCoefs;
216 struct tgsi_exec_vector QuadPos;
217
218 /* Conditional execution masks */
219 uint CondMask; /**< For IF/ELSE/ENDIF */
220 uint LoopMask; /**< For BGNLOOP/ENDLOOP */
221 uint ContMask; /**< For loop CONT statements */
222 uint FuncMask; /**< For function calls */
223 uint ExecMask; /**< = CondMask & LoopMask */
224
225 /** Condition mask stack (for nested conditionals) */
226 uint CondStack[TGSI_EXEC_MAX_COND_NESTING];
227 int CondStackTop;
228
229 /** Loop mask stack (for nested loops) */
230 uint LoopStack[TGSI_EXEC_MAX_LOOP_NESTING];
231 int LoopStackTop;
232
233 /** Loop continue mask stack (see comments in tgsi_exec.c) */
234 uint ContStack[TGSI_EXEC_MAX_LOOP_NESTING];
235 int ContStackTop;
236
237 /** Function execution mask stack (for executing subroutine code) */
238 uint FuncStack[TGSI_EXEC_MAX_CALL_NESTING];
239 int FuncStackTop;
240
241 /** Function call stack for saving/restoring the program counter */
242 uint CallStack[TGSI_EXEC_MAX_CALL_NESTING];
243 int CallStackTop;
244
245 struct tgsi_full_instruction *Instructions;
246 uint NumInstructions;
247
248 struct tgsi_full_declaration *Declarations;
249 uint NumDeclarations;
250
251 struct tgsi_exec_labels Labels;
252 };
253
254 struct tgsi_exec_machine *
255 tgsi_exec_machine_create( void );
256
257 void
258 tgsi_exec_machine_destroy(struct tgsi_exec_machine *mach);
259
260
261 void
262 tgsi_exec_machine_bind_shader(
263 struct tgsi_exec_machine *mach,
264 const struct tgsi_token *tokens,
265 uint numSamplers,
266 struct tgsi_sampler **samplers);
267
268 uint
269 tgsi_exec_machine_run(
270 struct tgsi_exec_machine *mach );
271
272
273 static INLINE void
274 tgsi_set_kill_mask(struct tgsi_exec_machine *mach, unsigned mask)
275 {
276 mach->Temps[TGSI_EXEC_TEMP_KILMASK_I].xyzw[TGSI_EXEC_TEMP_KILMASK_C].u[0] =
277 mask;
278 }
279
280
281 /** Set execution mask values prior to executing the shader */
282 static INLINE void
283 tgsi_set_exec_mask(struct tgsi_exec_machine *mach,
284 boolean ch0, boolean ch1, boolean ch2, boolean ch3)
285 {
286 int *mask = mach->Temps[TGSI_EXEC_MASK_I].xyzw[TGSI_EXEC_MASK_C].i;
287 mask[0] = ch0 ? ~0 : 0;
288 mask[1] = ch1 ? ~0 : 0;
289 mask[2] = ch2 ? ~0 : 0;
290 mask[3] = ch3 ? ~0 : 0;
291 }
292
293
294 #if defined __cplusplus
295 } /* extern "C" */
296 #endif
297
298 #endif /* TGSI_EXEC_H */