tests/graw: add fp-test
[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 * Copyright 2009-2010 VMware, Inc. All rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #ifndef TGSI_EXEC_H
30 #define TGSI_EXEC_H
31
32 #include "pipe/p_compiler.h"
33 #include "pipe/p_state.h"
34
35 #if defined __cplusplus
36 extern "C" {
37 #endif
38
39
40 #define NUM_CHANNELS 4 /* R,G,B,A */
41 #define QUAD_SIZE 4 /* 4 pixel/quad */
42
43
44 /**
45 * Registers may be treated as float, signed int or unsigned int.
46 */
47 union tgsi_exec_channel
48 {
49 float f[QUAD_SIZE];
50 int i[QUAD_SIZE];
51 unsigned u[QUAD_SIZE];
52 };
53
54 /**
55 * A vector[RGBA] of channels[4 pixels]
56 */
57 struct tgsi_exec_vector
58 {
59 union tgsi_exec_channel xyzw[NUM_CHANNELS];
60 };
61
62 /**
63 * For fragment programs, information for computing fragment input
64 * values from plane equation of the triangle/line.
65 */
66 struct tgsi_interp_coef
67 {
68 float a0[NUM_CHANNELS]; /* in an xyzw layout */
69 float dadx[NUM_CHANNELS];
70 float dady[NUM_CHANNELS];
71 };
72
73 enum tgsi_sampler_control {
74 tgsi_sampler_lod_bias,
75 tgsi_sampler_lod_explicit
76 };
77
78 /**
79 * Information for sampling textures, which must be implemented
80 * by code outside the TGSI executor.
81 */
82 struct tgsi_sampler
83 {
84 /** Get samples for four fragments in a quad */
85 void (*get_samples)(struct tgsi_sampler *sampler,
86 const float s[QUAD_SIZE],
87 const float t[QUAD_SIZE],
88 const float p[QUAD_SIZE],
89 const float c0[QUAD_SIZE],
90 enum tgsi_sampler_control control,
91 float rgba[NUM_CHANNELS][QUAD_SIZE]);
92 };
93
94 #define TGSI_EXEC_NUM_TEMPS 128
95 #define TGSI_EXEC_NUM_IMMEDIATES 256
96
97 /*
98 * Locations of various utility registers (_I = Index, _C = Channel)
99 */
100 #define TGSI_EXEC_TEMP_00000000_I (TGSI_EXEC_NUM_TEMPS + 0)
101 #define TGSI_EXEC_TEMP_00000000_C 0
102
103 #define TGSI_EXEC_TEMP_7FFFFFFF_I (TGSI_EXEC_NUM_TEMPS + 0)
104 #define TGSI_EXEC_TEMP_7FFFFFFF_C 1
105
106 #define TGSI_EXEC_TEMP_80000000_I (TGSI_EXEC_NUM_TEMPS + 0)
107 #define TGSI_EXEC_TEMP_80000000_C 2
108
109 #define TGSI_EXEC_TEMP_FFFFFFFF_I (TGSI_EXEC_NUM_TEMPS + 0)
110 #define TGSI_EXEC_TEMP_FFFFFFFF_C 3
111
112 #define TGSI_EXEC_TEMP_ONE_I (TGSI_EXEC_NUM_TEMPS + 1)
113 #define TGSI_EXEC_TEMP_ONE_C 0
114
115 #define TGSI_EXEC_TEMP_TWO_I (TGSI_EXEC_NUM_TEMPS + 1)
116 #define TGSI_EXEC_TEMP_TWO_C 1
117
118 #define TGSI_EXEC_TEMP_128_I (TGSI_EXEC_NUM_TEMPS + 1)
119 #define TGSI_EXEC_TEMP_128_C 2
120
121 #define TGSI_EXEC_TEMP_MINUS_128_I (TGSI_EXEC_NUM_TEMPS + 1)
122 #define TGSI_EXEC_TEMP_MINUS_128_C 3
123
124 #define TGSI_EXEC_TEMP_KILMASK_I (TGSI_EXEC_NUM_TEMPS + 2)
125 #define TGSI_EXEC_TEMP_KILMASK_C 0
126
127 #define TGSI_EXEC_TEMP_OUTPUT_I (TGSI_EXEC_NUM_TEMPS + 2)
128 #define TGSI_EXEC_TEMP_OUTPUT_C 1
129
130 #define TGSI_EXEC_TEMP_PRIMITIVE_I (TGSI_EXEC_NUM_TEMPS + 2)
131 #define TGSI_EXEC_TEMP_PRIMITIVE_C 2
132
133 /* NVIDIA condition code (CC) vector
134 */
135 #define TGSI_EXEC_CC_GT 0x01
136 #define TGSI_EXEC_CC_EQ 0x02
137 #define TGSI_EXEC_CC_LT 0x04
138 #define TGSI_EXEC_CC_UN 0x08
139
140 #define TGSI_EXEC_CC_X_MASK 0x000000ff
141 #define TGSI_EXEC_CC_X_SHIFT 0
142 #define TGSI_EXEC_CC_Y_MASK 0x0000ff00
143 #define TGSI_EXEC_CC_Y_SHIFT 8
144 #define TGSI_EXEC_CC_Z_MASK 0x00ff0000
145 #define TGSI_EXEC_CC_Z_SHIFT 16
146 #define TGSI_EXEC_CC_W_MASK 0xff000000
147 #define TGSI_EXEC_CC_W_SHIFT 24
148
149 #define TGSI_EXEC_TEMP_CC_I (TGSI_EXEC_NUM_TEMPS + 2)
150 #define TGSI_EXEC_TEMP_CC_C 3
151
152 #define TGSI_EXEC_TEMP_THREE_I (TGSI_EXEC_NUM_TEMPS + 3)
153 #define TGSI_EXEC_TEMP_THREE_C 0
154
155 #define TGSI_EXEC_TEMP_HALF_I (TGSI_EXEC_NUM_TEMPS + 3)
156 #define TGSI_EXEC_TEMP_HALF_C 1
157
158 /* execution mask, each value is either 0 or ~0 */
159 #define TGSI_EXEC_MASK_I (TGSI_EXEC_NUM_TEMPS + 3)
160 #define TGSI_EXEC_MASK_C 2
161
162 /* 4 register buffer for various purposes */
163 #define TGSI_EXEC_TEMP_R0 (TGSI_EXEC_NUM_TEMPS + 4)
164 #define TGSI_EXEC_NUM_TEMP_R 4
165
166 #define TGSI_EXEC_TEMP_ADDR (TGSI_EXEC_NUM_TEMPS + 8)
167 #define TGSI_EXEC_NUM_ADDRS 1
168
169 /* predicate register */
170 #define TGSI_EXEC_TEMP_P0 (TGSI_EXEC_NUM_TEMPS + 9)
171 #define TGSI_EXEC_NUM_PREDS 1
172
173 #define TGSI_EXEC_NUM_TEMP_EXTRAS 10
174
175
176
177 #define TGSI_EXEC_MAX_NESTING 32
178 #define TGSI_EXEC_MAX_COND_NESTING TGSI_EXEC_MAX_NESTING
179 #define TGSI_EXEC_MAX_LOOP_NESTING TGSI_EXEC_MAX_NESTING
180 #define TGSI_EXEC_MAX_SWITCH_NESTING TGSI_EXEC_MAX_NESTING
181 #define TGSI_EXEC_MAX_CALL_NESTING TGSI_EXEC_MAX_NESTING
182
183 /* The maximum number of input attributes per vertex. For 2D
184 * input register files, this is the stride between two 1D
185 * arrays.
186 */
187 #define TGSI_EXEC_MAX_INPUT_ATTRIBS 17
188
189 /* The maximum number of constant vectors per constant buffer.
190 */
191 #define TGSI_EXEC_MAX_CONST_BUFFER 4096
192
193 /* The maximum number of vertices per primitive */
194 #define TGSI_MAX_PRIM_VERTICES 6
195
196 /* The maximum number of primitives to be generated */
197 #define TGSI_MAX_PRIMITIVES 64
198
199 /* The maximum total number of vertices */
200 #define TGSI_MAX_TOTAL_VERTICES (TGSI_MAX_PRIM_VERTICES * TGSI_MAX_PRIMITIVES * PIPE_MAX_ATTRIBS)
201
202 /** function call/activation record */
203 struct tgsi_call_record
204 {
205 uint CondStackTop;
206 uint LoopStackTop;
207 uint ContStackTop;
208 int SwitchStackTop;
209 int BreakStackTop;
210 uint ReturnAddr;
211 };
212
213
214 /* Switch-case block state. */
215 struct tgsi_switch_record {
216 uint mask; /**< execution mask */
217 union tgsi_exec_channel selector; /**< a value case statements are compared to */
218 uint defaultMask; /**< non-execute mask for default case */
219 };
220
221
222 enum tgsi_break_type {
223 TGSI_EXEC_BREAK_INSIDE_LOOP,
224 TGSI_EXEC_BREAK_INSIDE_SWITCH
225 };
226
227
228 #define TGSI_EXEC_MAX_BREAK_STACK (TGSI_EXEC_MAX_LOOP_NESTING + TGSI_EXEC_MAX_SWITCH_NESTING)
229
230
231 /**
232 * Run-time virtual machine state for executing TGSI shader.
233 */
234 struct tgsi_exec_machine
235 {
236 /* Total = program temporaries + internal temporaries
237 */
238 struct tgsi_exec_vector Temps[TGSI_EXEC_NUM_TEMPS +
239 TGSI_EXEC_NUM_TEMP_EXTRAS];
240
241 float Imms[TGSI_EXEC_NUM_IMMEDIATES][4];
242
243 struct tgsi_exec_vector Inputs[TGSI_MAX_PRIM_VERTICES * PIPE_MAX_ATTRIBS];
244 struct tgsi_exec_vector Outputs[TGSI_MAX_TOTAL_VERTICES];
245
246 struct tgsi_exec_vector *Addrs;
247 struct tgsi_exec_vector *Predicates;
248
249 struct tgsi_sampler **Samplers;
250
251 unsigned ImmLimit;
252 const void *Consts[PIPE_MAX_CONSTANT_BUFFERS];
253 const struct tgsi_token *Tokens; /**< Declarations, instructions */
254 unsigned Processor; /**< TGSI_PROCESSOR_x */
255
256 /* GEOMETRY processor only. */
257 unsigned *Primitives;
258 unsigned NumOutputs;
259 unsigned MaxGeometryShaderOutputs;
260
261 /* FRAGMENT processor only. */
262 const struct tgsi_interp_coef *InterpCoefs;
263 struct tgsi_exec_vector QuadPos;
264 float Face; /**< +1 if front facing, -1 if back facing */
265
266 /* Conditional execution masks */
267 uint CondMask; /**< For IF/ELSE/ENDIF */
268 uint LoopMask; /**< For BGNLOOP/ENDLOOP */
269 uint ContMask; /**< For loop CONT statements */
270 uint FuncMask; /**< For function calls */
271 uint ExecMask; /**< = CondMask & LoopMask */
272
273 /* Current switch-case state. */
274 struct tgsi_switch_record Switch;
275
276 /* Current break type. */
277 enum tgsi_break_type BreakType;
278
279 /** Condition mask stack (for nested conditionals) */
280 uint CondStack[TGSI_EXEC_MAX_COND_NESTING];
281 int CondStackTop;
282
283 /** Loop mask stack (for nested loops) */
284 uint LoopStack[TGSI_EXEC_MAX_LOOP_NESTING];
285 int LoopStackTop;
286
287 /** Loop label stack */
288 uint LoopLabelStack[TGSI_EXEC_MAX_LOOP_NESTING];
289 int LoopLabelStackTop;
290
291 /** Loop continue mask stack (see comments in tgsi_exec.c) */
292 uint ContStack[TGSI_EXEC_MAX_LOOP_NESTING];
293 int ContStackTop;
294
295 /** Switch case stack */
296 struct tgsi_switch_record SwitchStack[TGSI_EXEC_MAX_SWITCH_NESTING];
297 int SwitchStackTop;
298
299 enum tgsi_break_type BreakStack[TGSI_EXEC_MAX_BREAK_STACK];
300 int BreakStackTop;
301
302 /** Function execution mask stack (for executing subroutine code) */
303 uint FuncStack[TGSI_EXEC_MAX_CALL_NESTING];
304 int FuncStackTop;
305
306 /** Function call stack for saving/restoring the program counter */
307 struct tgsi_call_record CallStack[TGSI_EXEC_MAX_CALL_NESTING];
308 int CallStackTop;
309
310 struct tgsi_full_instruction *Instructions;
311 uint NumInstructions;
312
313 struct tgsi_full_declaration *Declarations;
314 uint NumDeclarations;
315
316 };
317
318 struct tgsi_exec_machine *
319 tgsi_exec_machine_create( void );
320
321 void
322 tgsi_exec_machine_destroy(struct tgsi_exec_machine *mach);
323
324
325 void
326 tgsi_exec_machine_bind_shader(
327 struct tgsi_exec_machine *mach,
328 const struct tgsi_token *tokens,
329 uint numSamplers,
330 struct tgsi_sampler **samplers);
331
332 uint
333 tgsi_exec_machine_run(
334 struct tgsi_exec_machine *mach );
335
336
337 void
338 tgsi_exec_machine_free_data(struct tgsi_exec_machine *mach);
339
340
341 boolean
342 tgsi_check_soa_dependencies(const struct tgsi_full_instruction *inst);
343
344
345 static INLINE void
346 tgsi_set_kill_mask(struct tgsi_exec_machine *mach, unsigned mask)
347 {
348 mach->Temps[TGSI_EXEC_TEMP_KILMASK_I].xyzw[TGSI_EXEC_TEMP_KILMASK_C].u[0] =
349 mask;
350 }
351
352
353 /** Set execution mask values prior to executing the shader */
354 static INLINE void
355 tgsi_set_exec_mask(struct tgsi_exec_machine *mach,
356 boolean ch0, boolean ch1, boolean ch2, boolean ch3)
357 {
358 int *mask = mach->Temps[TGSI_EXEC_MASK_I].xyzw[TGSI_EXEC_MASK_C].i;
359 mask[0] = ch0 ? ~0 : 0;
360 mask[1] = ch1 ? ~0 : 0;
361 mask[2] = ch2 ? ~0 : 0;
362 mask[3] = ch3 ? ~0 : 0;
363 }
364
365
366 #if defined __cplusplus
367 } /* extern "C" */
368 #endif
369
370 #endif /* TGSI_EXEC_H */