tgsi_exec.h revision 9ee1bcf7a5442ccb517a5cfbaf024755bd4d2738
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
37extern "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 NUM_CHANNELS 4  /* R,G,B,A */
46#define QUAD_SIZE    4  /* 4 pixel/quad */
47
48
49/**
50  * Registers may be treated as float, signed int or unsigned int.
51  */
52union tgsi_exec_channel
53{
54   float    f[QUAD_SIZE];
55   int      i[QUAD_SIZE];
56   unsigned u[QUAD_SIZE];
57};
58
59/**
60  * A vector[RGBA] of channels[4 pixels]
61  */
62struct tgsi_exec_vector
63{
64   union tgsi_exec_channel xyzw[NUM_CHANNELS];
65};
66
67/**
68 * For fragment programs, information for computing fragment input
69 * values from plane equation of the triangle/line.
70 */
71struct tgsi_interp_coef
72{
73   float a0[NUM_CHANNELS];	/* in an xyzw layout */
74   float dadx[NUM_CHANNELS];
75   float dady[NUM_CHANNELS];
76};
77
78enum tgsi_sampler_control {
79   tgsi_sampler_lod_bias,
80   tgsi_sampler_lod_explicit
81};
82
83/**
84 * Information for sampling textures, which must be implemented
85 * by code outside the TGSI executor.
86 */
87struct tgsi_sampler
88{
89   /** Get samples for four fragments in a quad */
90   void (*get_samples)(struct tgsi_sampler *sampler,
91                       const float s[QUAD_SIZE],
92                       const float t[QUAD_SIZE],
93                       const float p[QUAD_SIZE],
94                       const float c0[QUAD_SIZE],
95                       enum tgsi_sampler_control control,
96                       float rgba[NUM_CHANNELS][QUAD_SIZE]);
97   void (*get_dims)(struct tgsi_sampler *sampler, int level,
98		    int dims[4]);
99   void (*get_texel)(struct tgsi_sampler *sampler, const int i[QUAD_SIZE],
100		     const int j[QUAD_SIZE], const int k[QUAD_SIZE],
101		     const int lod[QUAD_SIZE], const int8_t offset[3],
102		     float rgba[NUM_CHANNELS][QUAD_SIZE]);
103};
104
105#define TGSI_EXEC_NUM_TEMPS       128
106#define TGSI_EXEC_NUM_IMMEDIATES  256
107#define TGSI_EXEC_NUM_TEMP_ARRAYS 8
108
109/*
110 * Locations of various utility registers (_I = Index, _C = Channel)
111 */
112#define TGSI_EXEC_TEMP_00000000_I   (TGSI_EXEC_NUM_TEMPS + 0)
113#define TGSI_EXEC_TEMP_00000000_C   0
114
115#define TGSI_EXEC_TEMP_7FFFFFFF_I   (TGSI_EXEC_NUM_TEMPS + 0)
116#define TGSI_EXEC_TEMP_7FFFFFFF_C   1
117
118#define TGSI_EXEC_TEMP_80000000_I   (TGSI_EXEC_NUM_TEMPS + 0)
119#define TGSI_EXEC_TEMP_80000000_C   2
120
121#define TGSI_EXEC_TEMP_FFFFFFFF_I   (TGSI_EXEC_NUM_TEMPS + 0)
122#define TGSI_EXEC_TEMP_FFFFFFFF_C   3
123
124#define TGSI_EXEC_TEMP_ONE_I        (TGSI_EXEC_NUM_TEMPS + 1)
125#define TGSI_EXEC_TEMP_ONE_C        0
126
127#define TGSI_EXEC_TEMP_TWO_I        (TGSI_EXEC_NUM_TEMPS + 1)
128#define TGSI_EXEC_TEMP_TWO_C        1
129
130#define TGSI_EXEC_TEMP_128_I        (TGSI_EXEC_NUM_TEMPS + 1)
131#define TGSI_EXEC_TEMP_128_C        2
132
133#define TGSI_EXEC_TEMP_MINUS_128_I  (TGSI_EXEC_NUM_TEMPS + 1)
134#define TGSI_EXEC_TEMP_MINUS_128_C  3
135
136#define TGSI_EXEC_TEMP_KILMASK_I    (TGSI_EXEC_NUM_TEMPS + 2)
137#define TGSI_EXEC_TEMP_KILMASK_C    0
138
139#define TGSI_EXEC_TEMP_OUTPUT_I     (TGSI_EXEC_NUM_TEMPS + 2)
140#define TGSI_EXEC_TEMP_OUTPUT_C     1
141
142#define TGSI_EXEC_TEMP_PRIMITIVE_I  (TGSI_EXEC_NUM_TEMPS + 2)
143#define TGSI_EXEC_TEMP_PRIMITIVE_C  2
144
145#define TGSI_EXEC_TEMP_THREE_I      (TGSI_EXEC_NUM_TEMPS + 2)
146#define TGSI_EXEC_TEMP_THREE_C      3
147
148#define TGSI_EXEC_TEMP_HALF_I       (TGSI_EXEC_NUM_TEMPS + 3)
149#define TGSI_EXEC_TEMP_HALF_C       0
150
151/* execution mask, each value is either 0 or ~0 */
152#define TGSI_EXEC_MASK_I            (TGSI_EXEC_NUM_TEMPS + 3)
153#define TGSI_EXEC_MASK_C            1
154
155/* 4 register buffer for various purposes */
156#define TGSI_EXEC_TEMP_R0           (TGSI_EXEC_NUM_TEMPS + 4)
157#define TGSI_EXEC_NUM_TEMP_R        4
158
159#define TGSI_EXEC_TEMP_ADDR         (TGSI_EXEC_NUM_TEMPS + 8)
160#define TGSI_EXEC_NUM_ADDRS         1
161
162/* predicate register */
163#define TGSI_EXEC_TEMP_P0           (TGSI_EXEC_NUM_TEMPS + 9)
164#define TGSI_EXEC_NUM_PREDS         1
165
166#define TGSI_EXEC_NUM_TEMP_EXTRAS   10
167
168
169
170#define TGSI_EXEC_MAX_NESTING  32
171#define TGSI_EXEC_MAX_COND_NESTING  TGSI_EXEC_MAX_NESTING
172#define TGSI_EXEC_MAX_LOOP_NESTING  TGSI_EXEC_MAX_NESTING
173#define TGSI_EXEC_MAX_SWITCH_NESTING TGSI_EXEC_MAX_NESTING
174#define TGSI_EXEC_MAX_CALL_NESTING  TGSI_EXEC_MAX_NESTING
175
176/* The maximum number of input attributes per vertex. For 2D
177 * input register files, this is the stride between two 1D
178 * arrays.
179 */
180#define TGSI_EXEC_MAX_INPUT_ATTRIBS 17
181
182/* The maximum number of constant vectors per constant buffer.
183 */
184#define TGSI_EXEC_MAX_CONST_BUFFER  4096
185
186/* The maximum number of vertices per primitive */
187#define TGSI_MAX_PRIM_VERTICES 6
188
189/* The maximum number of primitives to be generated */
190#define TGSI_MAX_PRIMITIVES 64
191
192/* The maximum total number of vertices */
193#define TGSI_MAX_TOTAL_VERTICES (TGSI_MAX_PRIM_VERTICES * TGSI_MAX_PRIMITIVES * PIPE_MAX_ATTRIBS)
194
195#define TGSI_MAX_MISC_INPUTS 8
196
197/** function call/activation record */
198struct tgsi_call_record
199{
200   uint CondStackTop;
201   uint LoopStackTop;
202   uint ContStackTop;
203   int SwitchStackTop;
204   int BreakStackTop;
205   uint ReturnAddr;
206};
207
208
209/* Switch-case block state. */
210struct tgsi_switch_record {
211   uint mask;                          /**< execution mask */
212   union tgsi_exec_channel selector;   /**< a value case statements are compared to */
213   uint defaultMask;                   /**< non-execute mask for default case */
214};
215
216
217enum tgsi_break_type {
218   TGSI_EXEC_BREAK_INSIDE_LOOP,
219   TGSI_EXEC_BREAK_INSIDE_SWITCH
220};
221
222
223#define TGSI_EXEC_MAX_BREAK_STACK (TGSI_EXEC_MAX_LOOP_NESTING + TGSI_EXEC_MAX_SWITCH_NESTING)
224
225
226/**
227 * Run-time virtual machine state for executing TGSI shader.
228 */
229struct tgsi_exec_machine
230{
231   /* Total = program temporaries + internal temporaries
232    */
233   struct tgsi_exec_vector       Temps[TGSI_EXEC_NUM_TEMPS +
234                                       TGSI_EXEC_NUM_TEMP_EXTRAS];
235   struct tgsi_exec_vector       TempArray[TGSI_EXEC_NUM_TEMP_ARRAYS][TGSI_EXEC_NUM_TEMPS];
236
237   float                         Imms[TGSI_EXEC_NUM_IMMEDIATES][4];
238
239   float                         ImmArray[TGSI_EXEC_NUM_IMMEDIATES][4];
240
241   struct tgsi_exec_vector       *Inputs;
242   struct tgsi_exec_vector       *Outputs;
243
244   /* System values */
245   unsigned                      SysSemanticToIndex[TGSI_SEMANTIC_COUNT];
246   union tgsi_exec_channel       SystemValue[TGSI_MAX_MISC_INPUTS];
247
248   struct tgsi_exec_vector       *Addrs;
249   struct tgsi_exec_vector       *Predicates;
250
251   struct tgsi_sampler           **Samplers;
252
253   unsigned                      ImmLimit;
254
255   const void *Consts[PIPE_MAX_CONSTANT_BUFFERS];
256   unsigned ConstsSize[PIPE_MAX_CONSTANT_BUFFERS];
257
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   bool                          flatshade_color;
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 continue mask stack (see comments in tgsi_exec.c) */
297   uint ContStack[TGSI_EXEC_MAX_LOOP_NESTING];
298   int ContStackTop;
299
300   /** Switch case stack */
301   struct tgsi_switch_record SwitchStack[TGSI_EXEC_MAX_SWITCH_NESTING];
302   int SwitchStackTop;
303
304   enum tgsi_break_type BreakStack[TGSI_EXEC_MAX_BREAK_STACK];
305   int BreakStackTop;
306
307   /** Function execution mask stack (for executing subroutine code) */
308   uint FuncStack[TGSI_EXEC_MAX_CALL_NESTING];
309   int FuncStackTop;
310
311   /** Function call stack for saving/restoring the program counter */
312   struct tgsi_call_record CallStack[TGSI_EXEC_MAX_CALL_NESTING];
313   int CallStackTop;
314
315   struct tgsi_full_instruction *Instructions;
316   uint NumInstructions;
317
318   struct tgsi_full_declaration *Declarations;
319   uint NumDeclarations;
320
321   struct tgsi_declaration_resource Resources[PIPE_MAX_SHADER_RESOURCES];
322
323   boolean UsedGeometryShader;
324};
325
326struct tgsi_exec_machine *
327tgsi_exec_machine_create( void );
328
329void
330tgsi_exec_machine_destroy(struct tgsi_exec_machine *mach);
331
332
333void
334tgsi_exec_machine_bind_shader(
335   struct tgsi_exec_machine *mach,
336   const struct tgsi_token *tokens,
337   uint numSamplers,
338   struct tgsi_sampler **samplers);
339
340uint
341tgsi_exec_machine_run(
342   struct tgsi_exec_machine *mach );
343
344
345void
346tgsi_exec_machine_free_data(struct tgsi_exec_machine *mach);
347
348
349boolean
350tgsi_check_soa_dependencies(const struct tgsi_full_instruction *inst);
351
352
353static INLINE void
354tgsi_set_kill_mask(struct tgsi_exec_machine *mach, unsigned mask)
355{
356   mach->Temps[TGSI_EXEC_TEMP_KILMASK_I].xyzw[TGSI_EXEC_TEMP_KILMASK_C].u[0] =
357      mask;
358}
359
360
361/** Set execution mask values prior to executing the shader */
362static INLINE void
363tgsi_set_exec_mask(struct tgsi_exec_machine *mach,
364                   boolean ch0, boolean ch1, boolean ch2, boolean ch3)
365{
366   int *mask = mach->Temps[TGSI_EXEC_MASK_I].xyzw[TGSI_EXEC_MASK_C].i;
367   mask[0] = ch0 ? ~0 : 0;
368   mask[1] = ch1 ? ~0 : 0;
369   mask[2] = ch2 ? ~0 : 0;
370   mask[3] = ch3 ? ~0 : 0;
371}
372
373
374extern void
375tgsi_exec_set_constant_buffers(struct tgsi_exec_machine *mach,
376                               unsigned num_bufs,
377                               const void **bufs,
378                               const unsigned *buf_sizes);
379
380
381static INLINE int
382tgsi_exec_get_shader_param(enum pipe_shader_cap param)
383{
384   switch(param) {
385   case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
386   case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
387   case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
388   case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
389      return INT_MAX;
390   case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
391      return TGSI_EXEC_MAX_NESTING;
392   case PIPE_SHADER_CAP_MAX_INPUTS:
393      return TGSI_EXEC_MAX_INPUT_ATTRIBS;
394   case PIPE_SHADER_CAP_MAX_CONSTS:
395      return TGSI_EXEC_MAX_CONST_BUFFER;
396   case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
397      return PIPE_MAX_CONSTANT_BUFFERS;
398   case PIPE_SHADER_CAP_MAX_TEMPS:
399      return TGSI_EXEC_NUM_TEMPS;
400   case PIPE_SHADER_CAP_MAX_ADDRS:
401      return TGSI_EXEC_NUM_ADDRS;
402   case PIPE_SHADER_CAP_MAX_PREDS:
403      return TGSI_EXEC_NUM_PREDS;
404   case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
405      return 1;
406   case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
407   case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
408   case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
409   case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
410      return 1;
411   case PIPE_SHADER_CAP_SUBROUTINES:
412      return 1;
413   case PIPE_SHADER_CAP_INTEGERS:
414      return 1;
415   case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
416      return PIPE_MAX_SAMPLERS;
417   default:
418      return 0;
419   }
420}
421
422#if defined __cplusplus
423} /* extern "C" */
424#endif
425
426#endif /* TGSI_EXEC_H */
427