tgsi: add support for new SQRT opcode
[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 #include "pipe/p_shader_tokens.h"
35
36 #if defined __cplusplus
37 extern "C" {
38 #endif
39
40 #define TGSI_CHAN_X 0
41 #define TGSI_CHAN_Y 1
42 #define TGSI_CHAN_Z 2
43 #define TGSI_CHAN_W 3
44
45 #define TGSI_NUM_CHANNELS 4 /* R,G,B,A */
46 #define TGSI_QUAD_SIZE 4 /* 4 pixel/quad */
47
48 #define TGSI_FOR_EACH_CHANNEL( CHAN )\
49 for (CHAN = 0; CHAN < TGSI_NUM_CHANNELS; CHAN++)
50
51 #define TGSI_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
52 ((INST)->Dst[0].Register.WriteMask & (1 << (CHAN)))
53
54 #define TGSI_IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )\
55 if (TGSI_IS_DST0_CHANNEL_ENABLED( INST, CHAN ))
56
57 #define TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( INST, CHAN )\
58 TGSI_FOR_EACH_CHANNEL( CHAN )\
59 TGSI_IF_IS_DST0_CHANNEL_ENABLED( INST, CHAN )
60
61
62 /**
63 * Registers may be treated as float, signed int or unsigned int.
64 */
65 union tgsi_exec_channel
66 {
67 float f[TGSI_QUAD_SIZE];
68 int i[TGSI_QUAD_SIZE];
69 unsigned u[TGSI_QUAD_SIZE];
70 };
71
72 /**
73 * A vector[RGBA] of channels[4 pixels]
74 */
75 struct tgsi_exec_vector
76 {
77 union tgsi_exec_channel xyzw[TGSI_NUM_CHANNELS];
78 };
79
80 /**
81 * For fragment programs, information for computing fragment input
82 * values from plane equation of the triangle/line.
83 */
84 struct tgsi_interp_coef
85 {
86 float a0[TGSI_NUM_CHANNELS]; /* in an xyzw layout */
87 float dadx[TGSI_NUM_CHANNELS];
88 float dady[TGSI_NUM_CHANNELS];
89 };
90
91 enum tgsi_sampler_control {
92 tgsi_sampler_lod_bias,
93 tgsi_sampler_lod_explicit
94 };
95
96 /**
97 * Information for sampling textures, which must be implemented
98 * by code outside the TGSI executor.
99 */
100 struct tgsi_sampler
101 {
102 /** Get samples for four fragments in a quad */
103 /* this interface contains 5 sets of channels that vary
104 * depending on the sampler.
105 * s - the first texture coordinate for sampling.
106 * t - the second texture coordinate for sampling - unused for 1D,
107 layer for 1D arrays.
108 * p - the third coordinate for sampling for 3D, cube, cube arrays,
109 * layer for 2D arrays. Compare value for 1D/2D shadows.
110 * c0 - lod value for lod variants, compare value for shadow cube
111 * and shadow 2d arrays.
112 * c1 - cube array only - lod for cube map arrays
113 * compare for shadow cube map arrays.
114 */
115 void (*get_samples)(struct tgsi_sampler *sampler,
116 const float s[TGSI_QUAD_SIZE],
117 const float t[TGSI_QUAD_SIZE],
118 const float p[TGSI_QUAD_SIZE],
119 const float c0[TGSI_QUAD_SIZE],
120 const float c1[TGSI_QUAD_SIZE],
121 enum tgsi_sampler_control control,
122 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE]);
123 void (*get_dims)(struct tgsi_sampler *sampler, int level,
124 int dims[4]);
125 void (*get_texel)(struct tgsi_sampler *sampler, const int i[TGSI_QUAD_SIZE],
126 const int j[TGSI_QUAD_SIZE], const int k[TGSI_QUAD_SIZE],
127 const int lod[TGSI_QUAD_SIZE], const int8_t offset[3],
128 float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE]);
129 };
130
131 #define TGSI_EXEC_NUM_TEMPS 4096
132 #define TGSI_EXEC_NUM_IMMEDIATES 256
133 #define TGSI_EXEC_NUM_TEMP_ARRAYS 8
134
135 /*
136 * Locations of various utility registers (_I = Index, _C = Channel)
137 */
138 #define TGSI_EXEC_TEMP_00000000_I (TGSI_EXEC_NUM_TEMPS + 0)
139 #define TGSI_EXEC_TEMP_00000000_C 0
140
141 #define TGSI_EXEC_TEMP_7FFFFFFF_I (TGSI_EXEC_NUM_TEMPS + 0)
142 #define TGSI_EXEC_TEMP_7FFFFFFF_C 1
143
144 #define TGSI_EXEC_TEMP_80000000_I (TGSI_EXEC_NUM_TEMPS + 0)
145 #define TGSI_EXEC_TEMP_80000000_C 2
146
147 #define TGSI_EXEC_TEMP_FFFFFFFF_I (TGSI_EXEC_NUM_TEMPS + 0)
148 #define TGSI_EXEC_TEMP_FFFFFFFF_C 3
149
150 #define TGSI_EXEC_TEMP_ONE_I (TGSI_EXEC_NUM_TEMPS + 1)
151 #define TGSI_EXEC_TEMP_ONE_C 0
152
153 #define TGSI_EXEC_TEMP_TWO_I (TGSI_EXEC_NUM_TEMPS + 1)
154 #define TGSI_EXEC_TEMP_TWO_C 1
155
156 #define TGSI_EXEC_TEMP_128_I (TGSI_EXEC_NUM_TEMPS + 1)
157 #define TGSI_EXEC_TEMP_128_C 2
158
159 #define TGSI_EXEC_TEMP_MINUS_128_I (TGSI_EXEC_NUM_TEMPS + 1)
160 #define TGSI_EXEC_TEMP_MINUS_128_C 3
161
162 #define TGSI_EXEC_TEMP_KILMASK_I (TGSI_EXEC_NUM_TEMPS + 2)
163 #define TGSI_EXEC_TEMP_KILMASK_C 0
164
165 #define TGSI_EXEC_TEMP_OUTPUT_I (TGSI_EXEC_NUM_TEMPS + 2)
166 #define TGSI_EXEC_TEMP_OUTPUT_C 1
167
168 #define TGSI_EXEC_TEMP_PRIMITIVE_I (TGSI_EXEC_NUM_TEMPS + 2)
169 #define TGSI_EXEC_TEMP_PRIMITIVE_C 2
170
171 #define TGSI_EXEC_TEMP_THREE_I (TGSI_EXEC_NUM_TEMPS + 2)
172 #define TGSI_EXEC_TEMP_THREE_C 3
173
174 #define TGSI_EXEC_TEMP_HALF_I (TGSI_EXEC_NUM_TEMPS + 3)
175 #define TGSI_EXEC_TEMP_HALF_C 0
176
177 /* execution mask, each value is either 0 or ~0 */
178 #define TGSI_EXEC_MASK_I (TGSI_EXEC_NUM_TEMPS + 3)
179 #define TGSI_EXEC_MASK_C 1
180
181 /* 4 register buffer for various purposes */
182 #define TGSI_EXEC_TEMP_R0 (TGSI_EXEC_NUM_TEMPS + 4)
183 #define TGSI_EXEC_NUM_TEMP_R 4
184
185 #define TGSI_EXEC_TEMP_ADDR (TGSI_EXEC_NUM_TEMPS + 8)
186 #define TGSI_EXEC_NUM_ADDRS 1
187
188 /* predicate register */
189 #define TGSI_EXEC_TEMP_P0 (TGSI_EXEC_NUM_TEMPS + 9)
190 #define TGSI_EXEC_NUM_PREDS 1
191
192 #define TGSI_EXEC_NUM_TEMP_EXTRAS 10
193
194
195
196 #define TGSI_EXEC_MAX_NESTING 32
197 #define TGSI_EXEC_MAX_COND_NESTING TGSI_EXEC_MAX_NESTING
198 #define TGSI_EXEC_MAX_LOOP_NESTING TGSI_EXEC_MAX_NESTING
199 #define TGSI_EXEC_MAX_SWITCH_NESTING TGSI_EXEC_MAX_NESTING
200 #define TGSI_EXEC_MAX_CALL_NESTING TGSI_EXEC_MAX_NESTING
201
202 /* The maximum number of input attributes per vertex. For 2D
203 * input register files, this is the stride between two 1D
204 * arrays.
205 */
206 #define TGSI_EXEC_MAX_INPUT_ATTRIBS 17
207
208 /* The maximum number of constant vectors per constant buffer.
209 */
210 #define TGSI_EXEC_MAX_CONST_BUFFER 4096
211
212 /* The maximum number of vertices per primitive */
213 #define TGSI_MAX_PRIM_VERTICES 6
214
215 /* The maximum number of primitives to be generated */
216 #define TGSI_MAX_PRIMITIVES 64
217
218 /* The maximum total number of vertices */
219 #define TGSI_MAX_TOTAL_VERTICES (TGSI_MAX_PRIM_VERTICES * TGSI_MAX_PRIMITIVES * PIPE_MAX_ATTRIBS)
220
221 #define TGSI_MAX_MISC_INPUTS 8
222
223 /** function call/activation record */
224 struct tgsi_call_record
225 {
226 uint CondStackTop;
227 uint LoopStackTop;
228 uint ContStackTop;
229 int SwitchStackTop;
230 int BreakStackTop;
231 uint ReturnAddr;
232 };
233
234
235 /* Switch-case block state. */
236 struct tgsi_switch_record {
237 uint mask; /**< execution mask */
238 union tgsi_exec_channel selector; /**< a value case statements are compared to */
239 uint defaultMask; /**< non-execute mask for default case */
240 };
241
242
243 enum tgsi_break_type {
244 TGSI_EXEC_BREAK_INSIDE_LOOP,
245 TGSI_EXEC_BREAK_INSIDE_SWITCH
246 };
247
248
249 #define TGSI_EXEC_MAX_BREAK_STACK (TGSI_EXEC_MAX_LOOP_NESTING + TGSI_EXEC_MAX_SWITCH_NESTING)
250
251
252 /**
253 * Run-time virtual machine state for executing TGSI shader.
254 */
255 struct tgsi_exec_machine
256 {
257 /* Total = program temporaries + internal temporaries
258 */
259 struct tgsi_exec_vector Temps[TGSI_EXEC_NUM_TEMPS +
260 TGSI_EXEC_NUM_TEMP_EXTRAS];
261 struct tgsi_exec_vector TempArray[TGSI_EXEC_NUM_TEMP_ARRAYS][TGSI_EXEC_NUM_TEMPS];
262
263 float Imms[TGSI_EXEC_NUM_IMMEDIATES][4];
264
265 float ImmArray[TGSI_EXEC_NUM_IMMEDIATES][4];
266
267 struct tgsi_exec_vector *Inputs;
268 struct tgsi_exec_vector *Outputs;
269
270 /* System values */
271 unsigned SysSemanticToIndex[TGSI_SEMANTIC_COUNT];
272 union tgsi_exec_channel SystemValue[TGSI_MAX_MISC_INPUTS];
273
274 struct tgsi_exec_vector *Addrs;
275 struct tgsi_exec_vector *Predicates;
276
277 struct tgsi_sampler **Samplers;
278
279 unsigned ImmLimit;
280
281 const void *Consts[PIPE_MAX_CONSTANT_BUFFERS];
282 unsigned ConstsSize[PIPE_MAX_CONSTANT_BUFFERS];
283
284 const struct tgsi_token *Tokens; /**< Declarations, instructions */
285 unsigned Processor; /**< TGSI_PROCESSOR_x */
286
287 /* GEOMETRY processor only. */
288 unsigned *Primitives;
289 unsigned NumOutputs;
290 unsigned MaxGeometryShaderOutputs;
291
292 /* FRAGMENT processor only. */
293 const struct tgsi_interp_coef *InterpCoefs;
294 struct tgsi_exec_vector QuadPos;
295 float Face; /**< +1 if front facing, -1 if back facing */
296 bool flatshade_color;
297 /* Conditional execution masks */
298 uint CondMask; /**< For IF/ELSE/ENDIF */
299 uint LoopMask; /**< For BGNLOOP/ENDLOOP */
300 uint ContMask; /**< For loop CONT statements */
301 uint FuncMask; /**< For function calls */
302 uint ExecMask; /**< = CondMask & LoopMask */
303
304 /* Current switch-case state. */
305 struct tgsi_switch_record Switch;
306
307 /* Current break type. */
308 enum tgsi_break_type BreakType;
309
310 /** Condition mask stack (for nested conditionals) */
311 uint CondStack[TGSI_EXEC_MAX_COND_NESTING];
312 int CondStackTop;
313
314 /** Loop mask stack (for nested loops) */
315 uint LoopStack[TGSI_EXEC_MAX_LOOP_NESTING];
316 int LoopStackTop;
317
318 /** Loop label stack */
319 uint LoopLabelStack[TGSI_EXEC_MAX_LOOP_NESTING];
320 int LoopLabelStackTop;
321
322 /** Loop continue mask stack (see comments in tgsi_exec.c) */
323 uint ContStack[TGSI_EXEC_MAX_LOOP_NESTING];
324 int ContStackTop;
325
326 /** Switch case stack */
327 struct tgsi_switch_record SwitchStack[TGSI_EXEC_MAX_SWITCH_NESTING];
328 int SwitchStackTop;
329
330 enum tgsi_break_type BreakStack[TGSI_EXEC_MAX_BREAK_STACK];
331 int BreakStackTop;
332
333 /** Function execution mask stack (for executing subroutine code) */
334 uint FuncStack[TGSI_EXEC_MAX_CALL_NESTING];
335 int FuncStackTop;
336
337 /** Function call stack for saving/restoring the program counter */
338 struct tgsi_call_record CallStack[TGSI_EXEC_MAX_CALL_NESTING];
339 int CallStackTop;
340
341 struct tgsi_full_instruction *Instructions;
342 uint NumInstructions;
343
344 struct tgsi_full_declaration *Declarations;
345 uint NumDeclarations;
346
347 struct tgsi_declaration_sampler_view
348 SamplerViews[PIPE_MAX_SHADER_SAMPLER_VIEWS];
349
350 boolean UsedGeometryShader;
351 };
352
353 struct tgsi_exec_machine *
354 tgsi_exec_machine_create( void );
355
356 void
357 tgsi_exec_machine_destroy(struct tgsi_exec_machine *mach);
358
359
360 void
361 tgsi_exec_machine_bind_shader(
362 struct tgsi_exec_machine *mach,
363 const struct tgsi_token *tokens,
364 uint numSamplers,
365 struct tgsi_sampler **samplers);
366
367 uint
368 tgsi_exec_machine_run(
369 struct tgsi_exec_machine *mach );
370
371
372 void
373 tgsi_exec_machine_free_data(struct tgsi_exec_machine *mach);
374
375
376 boolean
377 tgsi_check_soa_dependencies(const struct tgsi_full_instruction *inst);
378
379
380 static INLINE void
381 tgsi_set_kill_mask(struct tgsi_exec_machine *mach, unsigned mask)
382 {
383 mach->Temps[TGSI_EXEC_TEMP_KILMASK_I].xyzw[TGSI_EXEC_TEMP_KILMASK_C].u[0] =
384 mask;
385 }
386
387
388 /** Set execution mask values prior to executing the shader */
389 static INLINE void
390 tgsi_set_exec_mask(struct tgsi_exec_machine *mach,
391 boolean ch0, boolean ch1, boolean ch2, boolean ch3)
392 {
393 int *mask = mach->Temps[TGSI_EXEC_MASK_I].xyzw[TGSI_EXEC_MASK_C].i;
394 mask[0] = ch0 ? ~0 : 0;
395 mask[1] = ch1 ? ~0 : 0;
396 mask[2] = ch2 ? ~0 : 0;
397 mask[3] = ch3 ? ~0 : 0;
398 }
399
400
401 extern void
402 tgsi_exec_set_constant_buffers(struct tgsi_exec_machine *mach,
403 unsigned num_bufs,
404 const void **bufs,
405 const unsigned *buf_sizes);
406
407
408 static INLINE int
409 tgsi_exec_get_shader_param(enum pipe_shader_cap param)
410 {
411 switch(param) {
412 case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
413 case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
414 case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
415 case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
416 return INT_MAX;
417 case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
418 return TGSI_EXEC_MAX_NESTING;
419 case PIPE_SHADER_CAP_MAX_INPUTS:
420 return TGSI_EXEC_MAX_INPUT_ATTRIBS;
421 case PIPE_SHADER_CAP_MAX_CONSTS:
422 return TGSI_EXEC_MAX_CONST_BUFFER;
423 case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
424 return PIPE_MAX_CONSTANT_BUFFERS;
425 case PIPE_SHADER_CAP_MAX_TEMPS:
426 return TGSI_EXEC_NUM_TEMPS;
427 case PIPE_SHADER_CAP_MAX_ADDRS:
428 return TGSI_EXEC_NUM_ADDRS;
429 case PIPE_SHADER_CAP_MAX_PREDS:
430 return TGSI_EXEC_NUM_PREDS;
431 case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
432 return 1;
433 case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
434 case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
435 case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
436 case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
437 return 1;
438 case PIPE_SHADER_CAP_SUBROUTINES:
439 return 1;
440 case PIPE_SHADER_CAP_INTEGERS:
441 return 1;
442 case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
443 return PIPE_MAX_SAMPLERS;
444 case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
445 return 1;
446 default:
447 return 0;
448 }
449 }
450
451 #if defined __cplusplus
452 } /* extern "C" */
453 #endif
454
455 #endif /* TGSI_EXEC_H */