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