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