1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   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 "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 
26 /**
27  * \file prog_instruction.h
28  *
29  * Vertex/fragment program instruction datatypes and constants.
30  *
31  * \author Brian Paul
32  * \author Keith Whitwell
33  * \author Ian Romanick <idr@us.ibm.com>
34  */
35 
36 
37 #ifndef PROG_INSTRUCTION_H
38 #define PROG_INSTRUCTION_H
39 
40 
41 #include "main/glheader.h"
42 
43 
44 /**
45  * Swizzle indexes.
46  * Do not change!
47  */
48 /*@{*/
49 #define SWIZZLE_X    0
50 #define SWIZZLE_Y    1
51 #define SWIZZLE_Z    2
52 #define SWIZZLE_W    3
53 #define SWIZZLE_ZERO 4   /**< For SWZ instruction only */
54 #define SWIZZLE_ONE  5   /**< For SWZ instruction only */
55 #define SWIZZLE_NIL  7   /**< used during shader code gen (undefined value) */
56 /*@}*/
57 
58 #define MAKE_SWIZZLE4(a,b,c,d) (((a)<<0) | ((b)<<3) | ((c)<<6) | ((d)<<9))
59 #define SWIZZLE_NOOP           MAKE_SWIZZLE4(0,1,2,3)
60 #define GET_SWZ(swz, idx)      (((swz) >> ((idx)*3)) & 0x7)
61 #define GET_BIT(msk, idx)      (((msk) >> (idx)) & 0x1)
62 /** Determine if swz contains SWIZZLE_ZERO/ONE/NIL for any components. */
63 #define HAS_EXTENDED_SWIZZLE(swz) (swz & 0x924)
64 
65 #define SWIZZLE_XYZW MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W)
66 #define SWIZZLE_XXXX MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X)
67 #define SWIZZLE_YYYY MAKE_SWIZZLE4(SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y)
68 #define SWIZZLE_ZZZZ MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z)
69 #define SWIZZLE_WWWW MAKE_SWIZZLE4(SWIZZLE_W, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
70 
71 
72 /**
73  * Writemask values, 1 bit per component.
74  */
75 /*@{*/
76 #define WRITEMASK_X     0x1
77 #define WRITEMASK_Y     0x2
78 #define WRITEMASK_XY    0x3
79 #define WRITEMASK_Z     0x4
80 #define WRITEMASK_XZ    0x5
81 #define WRITEMASK_YZ    0x6
82 #define WRITEMASK_XYZ   0x7
83 #define WRITEMASK_W     0x8
84 #define WRITEMASK_XW    0x9
85 #define WRITEMASK_YW    0xa
86 #define WRITEMASK_XYW   0xb
87 #define WRITEMASK_ZW    0xc
88 #define WRITEMASK_XZW   0xd
89 #define WRITEMASK_YZW   0xe
90 #define WRITEMASK_XYZW  0xf
91 /*@}*/
92 
93 
94 /**
95  * Per-component negation masks
96  */
97 /*@{*/
98 #define NEGATE_X    0x1
99 #define NEGATE_Y    0x2
100 #define NEGATE_Z    0x4
101 #define NEGATE_W    0x8
102 #define NEGATE_XYZ  0x7
103 #define NEGATE_XYZW 0xf
104 #define NEGATE_NONE 0x0
105 /*@}*/
106 
107 
108 /**
109  * Program instruction opcodes for vertex, fragment and geometry programs.
110  */
111 enum prog_opcode {
112                      /* ARB_vp   ARB_fp   NV_vp   NV_fp     GLSL */
113                      /*------------------------------------------*/
114    OPCODE_NOP = 0,   /*                                      X   */
115    OPCODE_ABS,       /*   X        X       1.1               X   */
116    OPCODE_ADD,       /*   X        X       X       X         X   */
117    OPCODE_ARL,       /*   X                X                 X   */
118    OPCODE_BGNLOOP,   /*                                     opt  */
119    OPCODE_BGNSUB,    /*                                     opt  */
120    OPCODE_BRK,       /*                    2                opt  */
121    OPCODE_CAL,       /*                    2       2        opt  */
122    OPCODE_CMP,       /*            X                         X   */
123    OPCODE_CONT,      /*                                     opt  */
124    OPCODE_COS,       /*            X       2       X         X   */
125    OPCODE_DDX,       /*                            X         X   */
126    OPCODE_DDY,       /*                            X         X   */
127    OPCODE_DP2,       /*                            2         X   */
128    OPCODE_DP3,       /*   X        X       X       X         X   */
129    OPCODE_DP4,       /*   X        X       X       X         X   */
130    OPCODE_DPH,       /*   X        X       1.1                   */
131    OPCODE_DST,       /*   X        X       X       X             */
132    OPCODE_ELSE,      /*                                     opt  */
133    OPCODE_END,       /*   X        X       X       X        opt  */
134    OPCODE_ENDIF,     /*                                     opt  */
135    OPCODE_ENDLOOP,   /*                                     opt  */
136    OPCODE_ENDSUB,    /*                                     opt  */
137    OPCODE_EX2,       /*   X        X       2       X         X   */
138    OPCODE_EXP,       /*   X                X                     */
139    OPCODE_FLR,       /*   X        X       2       X         X   */
140    OPCODE_FRC,       /*   X        X       2       X         X   */
141    OPCODE_IF,        /*                                     opt  */
142    OPCODE_KIL,       /*            X                         X   */
143    OPCODE_LG2,       /*   X        X       2       X         X   */
144    OPCODE_LIT,       /*   X        X       X       X             */
145    OPCODE_LOG,       /*   X                X                     */
146    OPCODE_LRP,       /*            X               X             */
147    OPCODE_MAD,       /*   X        X       X       X         X   */
148    OPCODE_MAX,       /*   X        X       X       X         X   */
149    OPCODE_MIN,       /*   X        X       X       X         X   */
150    OPCODE_MOV,       /*   X        X       X       X         X   */
151    OPCODE_MUL,       /*   X        X       X       X         X   */
152    OPCODE_NOISE1,    /*                                      X   */
153    OPCODE_NOISE2,    /*                                      X   */
154    OPCODE_NOISE3,    /*                                      X   */
155    OPCODE_NOISE4,    /*                                      X   */
156    OPCODE_POW,       /*   X        X               X         X   */
157    OPCODE_RCP,       /*   X        X       X       X         X   */
158    OPCODE_RET,       /*                    2       2        opt  */
159    OPCODE_RSQ,       /*   X        X       X       X         X   */
160    OPCODE_SCS,       /*            X                         X   */
161    OPCODE_SGE,       /*   X        X       X       X         X   */
162    OPCODE_SIN,       /*            X       2       X         X   */
163    OPCODE_SLT,       /*   X        X       X       X         X   */
164    OPCODE_SSG,       /*                    2                 X   */
165    OPCODE_SUB,       /*   X        X       1.1     X         X   */
166    OPCODE_SWZ,       /*   X        X                         X   */
167    OPCODE_TEX,       /*            X       3       X         X   */
168    OPCODE_TXB,       /*            X       3                 X   */
169    OPCODE_TXD,       /*                            X         X   */
170    OPCODE_TXL,       /*                    3       2         X   */
171    OPCODE_TXP,       /*            X                         X   */
172    OPCODE_TRUNC,     /*                                      X   */
173    OPCODE_XPD,       /*   X        X                             */
174    MAX_OPCODE
175 };
176 
177 
178 /**
179  * Number of bits for the src/dst register Index field.
180  * This limits the size of temp/uniform register files.
181  */
182 #define INST_INDEX_BITS 12
183 
184 
185 /**
186  * Instruction source register.
187  */
188 struct prog_src_register
189 {
190    GLuint File:4;	/**< One of the PROGRAM_* register file values. */
191    GLint Index:(INST_INDEX_BITS+1); /**< Extra bit here for sign bit.
192                                      * May be negative for relative addressing.
193                                      */
194    GLuint Swizzle:12;
195    GLuint RelAddr:1;
196 
197    /**
198     * Negation.
199     * This will either be NEGATE_NONE or NEGATE_XYZW, except for the SWZ
200     * instruction which allows per-component negation.
201     */
202    GLuint Negate:4;
203 };
204 
205 
206 /**
207  * Instruction destination register.
208  */
209 struct prog_dst_register
210 {
211    GLuint File:4;      /**< One of the PROGRAM_* register file values */
212    GLuint Index:INST_INDEX_BITS;  /**< Unsigned, never negative */
213    GLuint WriteMask:4;
214    GLuint RelAddr:1;
215 };
216 
217 
218 /**
219  * Vertex/fragment program instruction.
220  */
221 struct prog_instruction
222 {
223    enum prog_opcode Opcode;
224    struct prog_src_register SrcReg[3];
225    struct prog_dst_register DstReg;
226 
227    /**
228     * Saturate each value of the vectored result to the range [0,1].
229     *
230     * \since
231     * ARB_fragment_program
232     */
233    GLuint Saturate:1;
234 
235    /**
236     * \name Extra fields for TEX, TXB, TXD, TXL, TXP instructions.
237     */
238    /*@{*/
239    /** Source texture unit. */
240    GLuint TexSrcUnit:5;
241 
242    /** Source texture target, one of TEXTURE_{1D,2D,3D,CUBE,RECT}_INDEX */
243    GLuint TexSrcTarget:4;
244 
245    /** True if tex instruction should do shadow comparison */
246    GLuint TexShadow:1;
247    /*@}*/
248 
249    /**
250     * For BRA and CAL instructions, the location to jump to.
251     * For BGNLOOP, points to ENDLOOP (and vice-versa).
252     * For BRK, points to ENDLOOP
253     * For IF, points to ELSE or ENDIF.
254     * For ELSE, points to ENDIF.
255     */
256    GLint BranchTarget;
257 };
258 
259 
260 #ifdef __cplusplus
261 extern "C" {
262 #endif
263 
264 struct gl_program;
265 
266 extern void
267 _mesa_init_instructions(struct prog_instruction *inst, GLuint count);
268 
269 extern struct prog_instruction *
270 _mesa_copy_instructions(struct prog_instruction *dest,
271                         const struct prog_instruction *src, GLuint n);
272 
273 extern GLuint
274 _mesa_num_inst_src_regs(enum prog_opcode opcode);
275 
276 extern GLuint
277 _mesa_num_inst_dst_regs(enum prog_opcode opcode);
278 
279 extern GLboolean
280 _mesa_is_tex_instruction(enum prog_opcode opcode);
281 
282 extern GLboolean
283 _mesa_check_soa_dependencies(const struct prog_instruction *inst);
284 
285 extern const char *
286 _mesa_opcode_string(enum prog_opcode opcode);
287 
288 
289 #ifdef __cplusplus
290 } /* extern "C" */
291 #endif
292 
293 #endif /* PROG_INSTRUCTION_H */
294