gallium: Pass per-element (not per-quad) LOD bias values down to texture sampler.
[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 MAX_LABELS (4 * 1024) /**< basically, max instructions */
41
42 #define NUM_CHANNELS 4 /* R,G,B,A */
43 #define QUAD_SIZE 4 /* 4 pixel/quad */
44
45
46 /**
47 * Registers may be treated as float, signed int or unsigned int.
48 */
49 union tgsi_exec_channel
50 {
51 float f[QUAD_SIZE];
52 int i[QUAD_SIZE];
53 unsigned u[QUAD_SIZE];
54 };
55
56 /**
57 * A vector[RGBA] of channels[4 pixels]
58 */
59 struct tgsi_exec_vector
60 {
61 union tgsi_exec_channel xyzw[NUM_CHANNELS];
62 };
63
64 /**
65 * For fragment programs, information for computing fragment input
66 * values from plane equation of the triangle/line.
67 */
68 struct tgsi_interp_coef
69 {
70 float a0[NUM_CHANNELS]; /* in an xyzw layout */
71 float dadx[NUM_CHANNELS];
72 float dady[NUM_CHANNELS];
73 };
74
75 /**
76 * Information for sampling textures, which must be implemented
77 * by code outside the TGSI executor.
78 */
79 struct tgsi_sampler
80 {
81 /** Get samples for four fragments in a quad */
82 void (*get_samples)(struct tgsi_sampler *sampler,
83 const float s[QUAD_SIZE],
84 const float t[QUAD_SIZE],
85 const float p[QUAD_SIZE],
86 const float lodbias[QUAD_SIZE],
87 float rgba[NUM_CHANNELS][QUAD_SIZE]);
88 };
89
90 /**
91 * For branching/calling subroutines.
92 */
93 struct tgsi_exec_labels
94 {
95 unsigned labels[MAX_LABELS][2];
96 unsigned count;
97 };
98
99
100 #define TGSI_EXEC_NUM_TEMPS 128
101 #define TGSI_EXEC_NUM_IMMEDIATES 256
102
103 /*
104 * Locations of various utility registers (_I = Index, _C = Channel)
105 */
106 #define TGSI_EXEC_TEMP_00000000_I (TGSI_EXEC_NUM_TEMPS + 0)
107 #define TGSI_EXEC_TEMP_00000000_C 0
108
109 #define TGSI_EXEC_TEMP_7FFFFFFF_I (TGSI_EXEC_NUM_TEMPS + 0)
110 #define TGSI_EXEC_TEMP_7FFFFFFF_C 1
111
112 #define TGSI_EXEC_TEMP_80000000_I (TGSI_EXEC_NUM_TEMPS + 0)
113 #define TGSI_EXEC_TEMP_80000000_C 2
114
115 #define TGSI_EXEC_TEMP_FFFFFFFF_I (TGSI_EXEC_NUM_TEMPS + 0)
116 #define TGSI_EXEC_TEMP_FFFFFFFF_C 3
117
118 #define TGSI_EXEC_TEMP_ONE_I (TGSI_EXEC_NUM_TEMPS + 1)
119 #define TGSI_EXEC_TEMP_ONE_C 0
120
121 #define TGSI_EXEC_TEMP_TWO_I (TGSI_EXEC_NUM_TEMPS + 1)
122 #define TGSI_EXEC_TEMP_TWO_C 1
123
124 #define TGSI_EXEC_TEMP_128_I (TGSI_EXEC_NUM_TEMPS + 1)
125 #define TGSI_EXEC_TEMP_128_C 2
126
127 #define TGSI_EXEC_TEMP_MINUS_128_I (TGSI_EXEC_NUM_TEMPS + 1)
128 #define TGSI_EXEC_TEMP_MINUS_128_C 3
129
130 #define TGSI_EXEC_TEMP_KILMASK_I (TGSI_EXEC_NUM_TEMPS + 2)
131 #define TGSI_EXEC_TEMP_KILMASK_C 0
132
133 #define TGSI_EXEC_TEMP_OUTPUT_I (TGSI_EXEC_NUM_TEMPS + 2)
134 #define TGSI_EXEC_TEMP_OUTPUT_C 1
135
136 #define TGSI_EXEC_TEMP_PRIMITIVE_I (TGSI_EXEC_NUM_TEMPS + 2)
137 #define TGSI_EXEC_TEMP_PRIMITIVE_C 2
138
139 /* NVIDIA condition code (CC) vector
140 */
141 #define TGSI_EXEC_CC_GT 0x01
142 #define TGSI_EXEC_CC_EQ 0x02
143 #define TGSI_EXEC_CC_LT 0x04
144 #define TGSI_EXEC_CC_UN 0x08
145
146 #define TGSI_EXEC_CC_X_MASK 0x000000ff
147 #define TGSI_EXEC_CC_X_SHIFT 0
148 #define TGSI_EXEC_CC_Y_MASK 0x0000ff00
149 #define TGSI_EXEC_CC_Y_SHIFT 8
150 #define TGSI_EXEC_CC_Z_MASK 0x00ff0000
151 #define TGSI_EXEC_CC_Z_SHIFT 16
152 #define TGSI_EXEC_CC_W_MASK 0xff000000
153 #define TGSI_EXEC_CC_W_SHIFT 24
154
155 #define TGSI_EXEC_TEMP_CC_I (TGSI_EXEC_NUM_TEMPS + 2)
156 #define TGSI_EXEC_TEMP_CC_C 3
157
158 #define TGSI_EXEC_TEMP_THREE_I (TGSI_EXEC_NUM_TEMPS + 3)
159 #define TGSI_EXEC_TEMP_THREE_C 0
160
161 #define TGSI_EXEC_TEMP_HALF_I (TGSI_EXEC_NUM_TEMPS + 3)
162 #define TGSI_EXEC_TEMP_HALF_C 1
163
164 /* execution mask, each value is either 0 or ~0 */
165 #define TGSI_EXEC_MASK_I (TGSI_EXEC_NUM_TEMPS + 3)
166 #define TGSI_EXEC_MASK_C 2
167
168 /* 4 register buffer for various purposes */
169 #define TGSI_EXEC_TEMP_R0 (TGSI_EXEC_NUM_TEMPS + 4)
170 #define TGSI_EXEC_NUM_TEMP_R 4
171
172 #define TGSI_EXEC_TEMP_ADDR (TGSI_EXEC_NUM_TEMPS + 8)
173 #define TGSI_EXEC_NUM_ADDRS 1
174
175 /* predicate register */
176 #define TGSI_EXEC_TEMP_P0 (TGSI_EXEC_NUM_TEMPS + 9)
177 #define TGSI_EXEC_NUM_PREDS 1
178
179 #define TGSI_EXEC_NUM_TEMP_EXTRAS 10
180
181
182
183 #define TGSI_EXEC_MAX_COND_NESTING 32
184 #define TGSI_EXEC_MAX_LOOP_NESTING 32
185 #define TGSI_EXEC_MAX_SWITCH_NESTING 32
186 #define TGSI_EXEC_MAX_CALL_NESTING 32
187
188 /* The maximum number of input attributes per vertex. For 2D
189 * input register files, this is the stride between two 1D
190 * arrays.
191 */
192 #define TGSI_EXEC_MAX_INPUT_ATTRIBS 17
193
194 /* The maximum number of constant vectors per constant buffer.
195 */
196 #define TGSI_EXEC_MAX_CONST_BUFFER 4096
197
198 /* The maximum number of vertices per primitive */
199 #define TGSI_MAX_PRIM_VERTICES 6
200
201 /* The maximum number of primitives to be generated */
202 #define TGSI_MAX_PRIMITIVES 64
203
204 /* The maximum total number of vertices */
205 #define TGSI_MAX_TOTAL_VERTICES (TGSI_MAX_PRIM_VERTICES * TGSI_MAX_PRIMITIVES * PIPE_MAX_ATTRIBS)
206
207 /** function call/activation record */
208 struct tgsi_call_record
209 {
210 uint CondStackTop;
211 uint LoopStackTop;
212 uint ContStackTop;
213 int SwitchStackTop;
214 int BreakStackTop;
215 uint ReturnAddr;
216 };
217
218
219 /* Switch-case block state. */
220 struct tgsi_switch_record {
221 uint mask; /**< execution mask */
222 union tgsi_exec_channel selector; /**< a value case statements are compared to */
223 uint defaultMask; /**< non-execute mask for default case */
224 };
225
226
227 enum tgsi_break_type {
228 TGSI_EXEC_BREAK_INSIDE_LOOP,
229 TGSI_EXEC_BREAK_INSIDE_SWITCH
230 };
231
232
233 #define TGSI_EXEC_MAX_BREAK_STACK (TGSI_EXEC_MAX_LOOP_NESTING + TGSI_EXEC_MAX_SWITCH_NESTING)
234
235
236 /**
237 * Run-time virtual machine state for executing TGSI shader.
238 */
239 struct tgsi_exec_machine
240 {
241 /* Total = program temporaries + internal temporaries
242 */
243 struct tgsi_exec_vector Temps[TGSI_EXEC_NUM_TEMPS +
244 TGSI_EXEC_NUM_TEMP_EXTRAS];
245
246 float Imms[TGSI_EXEC_NUM_IMMEDIATES][4];
247
248 struct tgsi_exec_vector Inputs[TGSI_MAX_PRIM_VERTICES * PIPE_MAX_ATTRIBS];
249 struct tgsi_exec_vector Outputs[TGSI_MAX_TOTAL_VERTICES];
250
251 struct tgsi_exec_vector *Addrs;
252 struct tgsi_exec_vector *Predicates;
253
254 struct tgsi_sampler **Samplers;
255
256 unsigned ImmLimit;
257 const float (*Consts)[4];
258 const struct tgsi_token *Tokens; /**< Declarations, instructions */
259 unsigned Processor; /**< TGSI_PROCESSOR_x */
260
261 /* GEOMETRY processor only. */
262 unsigned *Primitives;
263 unsigned NumOutputs;
264 unsigned MaxGeometryShaderOutputs;
265
266 /* FRAGMENT processor only. */
267 const struct tgsi_interp_coef *InterpCoefs;
268 struct tgsi_exec_vector QuadPos;
269 float Face; /**< +1 if front facing, -1 if back facing */
270
271 /* Conditional execution masks */
272 uint CondMask; /**< For IF/ELSE/ENDIF */
273 uint LoopMask; /**< For BGNLOOP/ENDLOOP */
274 uint ContMask; /**< For loop CONT statements */
275 uint FuncMask; /**< For function calls */
276 uint ExecMask; /**< = CondMask & LoopMask */
277
278 /* Current switch-case state. */
279 struct tgsi_switch_record Switch;
280
281 /* Current break type. */
282 enum tgsi_break_type BreakType;
283
284 /** Condition mask stack (for nested conditionals) */
285 uint CondStack[TGSI_EXEC_MAX_COND_NESTING];
286 int CondStackTop;
287
288 /** Loop mask stack (for nested loops) */
289 uint LoopStack[TGSI_EXEC_MAX_LOOP_NESTING];
290 int LoopStackTop;
291
292 /** Loop label stack */
293 uint LoopLabelStack[TGSI_EXEC_MAX_LOOP_NESTING];
294 int LoopLabelStackTop;
295
296 /** Loop counter stack (x = index, y = counter, z = step) */
297 struct tgsi_exec_vector LoopCounterStack[TGSI_EXEC_MAX_LOOP_NESTING];
298 int LoopCounterStackTop;
299
300 /** Loop continue mask stack (see comments in tgsi_exec.c) */
301 uint ContStack[TGSI_EXEC_MAX_LOOP_NESTING];
302 int ContStackTop;
303
304 /** Switch case stack */
305 struct tgsi_switch_record SwitchStack[TGSI_EXEC_MAX_SWITCH_NESTING];
306 int SwitchStackTop;
307
308 enum tgsi_break_type BreakStack[TGSI_EXEC_MAX_BREAK_STACK];
309 int BreakStackTop;
310
311 /** Function execution mask stack (for executing subroutine code) */
312 uint FuncStack[TGSI_EXEC_MAX_CALL_NESTING];
313 int FuncStackTop;
314
315 /** Function call stack for saving/restoring the program counter */
316 struct tgsi_call_record CallStack[TGSI_EXEC_MAX_CALL_NESTING];
317 int CallStackTop;
318
319 struct tgsi_full_instruction *Instructions;
320 uint NumInstructions;
321
322 struct tgsi_full_declaration *Declarations;
323 uint NumDeclarations;
324
325 struct tgsi_exec_labels Labels;
326 };
327
328 struct tgsi_exec_machine *
329 tgsi_exec_machine_create( void );
330
331 void
332 tgsi_exec_machine_destroy(struct tgsi_exec_machine *mach);
333
334
335 void
336 tgsi_exec_machine_bind_shader(
337 struct tgsi_exec_machine *mach,
338 const struct tgsi_token *tokens,
339 uint numSamplers,
340 struct tgsi_sampler **samplers);
341
342 uint
343 tgsi_exec_machine_run(
344 struct tgsi_exec_machine *mach );
345
346
347 void
348 tgsi_exec_machine_free_data(struct tgsi_exec_machine *mach);
349
350
351 boolean
352 tgsi_check_soa_dependencies(const struct tgsi_full_instruction *inst);
353
354
355 static INLINE void
356 tgsi_set_kill_mask(struct tgsi_exec_machine *mach, unsigned mask)
357 {
358 mach->Temps[TGSI_EXEC_TEMP_KILMASK_I].xyzw[TGSI_EXEC_TEMP_KILMASK_C].u[0] =
359 mask;
360 }
361
362
363 /** Set execution mask values prior to executing the shader */
364 static INLINE void
365 tgsi_set_exec_mask(struct tgsi_exec_machine *mach,
366 boolean ch0, boolean ch1, boolean ch2, boolean ch3)
367 {
368 int *mask = mach->Temps[TGSI_EXEC_MASK_I].xyzw[TGSI_EXEC_MASK_C].i;
369 mask[0] = ch0 ? ~0 : 0;
370 mask[1] = ch1 ? ~0 : 0;
371 mask[2] = ch2 ? ~0 : 0;
372 mask[3] = ch3 ? ~0 : 0;
373 }
374
375
376 #if defined __cplusplus
377 } /* extern "C" */
378 #endif
379
380 #endif /* TGSI_EXEC_H */