Merge remote branch 'origin/gallium-0.2' into gallium-0.2
[mesa.git] / src / mesa / shader / prog_instruction.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul 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 "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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 /**
42 * Swizzle indexes.
43 * Do not change!
44 */
45 /*@{*/
46 #define SWIZZLE_X 0
47 #define SWIZZLE_Y 1
48 #define SWIZZLE_Z 2
49 #define SWIZZLE_W 3
50 #define SWIZZLE_ZERO 4 /**< For SWZ instruction only */
51 #define SWIZZLE_ONE 5 /**< For SWZ instruction only */
52 #define SWIZZLE_NIL 7 /**< used during shader code gen (undefined value) */
53 /*@}*/
54
55 #define MAKE_SWIZZLE4(a,b,c,d) (((a)<<0) | ((b)<<3) | ((c)<<6) | ((d)<<9))
56 #define SWIZZLE_NOOP MAKE_SWIZZLE4(0,1,2,3)
57 #define GET_SWZ(swz, idx) (((swz) >> ((idx)*3)) & 0x7)
58 #define GET_BIT(msk, idx) (((msk) >> (idx)) & 0x1)
59
60 #define SWIZZLE_XYZW MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W)
61 #define SWIZZLE_XXXX MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X)
62 #define SWIZZLE_YYYY MAKE_SWIZZLE4(SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y)
63 #define SWIZZLE_ZZZZ MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z)
64 #define SWIZZLE_WWWW MAKE_SWIZZLE4(SWIZZLE_W, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
65
66
67 /**
68 * Writemask values, 1 bit per component.
69 */
70 /*@{*/
71 #define WRITEMASK_X 0x1
72 #define WRITEMASK_Y 0x2
73 #define WRITEMASK_XY 0x3
74 #define WRITEMASK_Z 0x4
75 #define WRITEMASK_XZ 0x5
76 #define WRITEMASK_YZ 0x6
77 #define WRITEMASK_XYZ 0x7
78 #define WRITEMASK_W 0x8
79 #define WRITEMASK_XW 0x9
80 #define WRITEMASK_YW 0xa
81 #define WRITEMASK_XYW 0xb
82 #define WRITEMASK_ZW 0xc
83 #define WRITEMASK_XZW 0xd
84 #define WRITEMASK_YZW 0xe
85 #define WRITEMASK_XYZW 0xf
86 /*@}*/
87
88
89 /**
90 * Condition codes
91 */
92 /*@{*/
93 #define COND_GT 1 /**< greater than zero */
94 #define COND_EQ 2 /**< equal to zero */
95 #define COND_LT 3 /**< less than zero */
96 #define COND_UN 4 /**< unordered (NaN) */
97 #define COND_GE 5 /**< greater then or equal to zero */
98 #define COND_LE 6 /**< less then or equal to zero */
99 #define COND_NE 7 /**< not equal to zero */
100 #define COND_TR 8 /**< always true */
101 #define COND_FL 9 /**< always false */
102 /*@}*/
103
104
105 /**
106 * Instruction precision for GL_NV_fragment_program
107 */
108 /*@{*/
109 #define FLOAT32 0x1
110 #define FLOAT16 0x2
111 #define FIXED12 0x4
112 /*@}*/
113
114
115 /**
116 * Saturation modes when storing values.
117 */
118 /*@{*/
119 #define SATURATE_OFF 0
120 #define SATURATE_ZERO_ONE 1
121 #define SATURATE_PLUS_MINUS_ONE 2
122 /*@}*/
123
124
125 /**
126 * Per-component negation masks
127 */
128 /*@{*/
129 #define NEGATE_X 0x1
130 #define NEGATE_Y 0x2
131 #define NEGATE_Z 0x4
132 #define NEGATE_W 0x8
133 #define NEGATE_XYZW 0xf
134 #define NEGATE_NONE 0x0
135 /*@}*/
136
137
138 /**
139 * Program instruction opcodes, for both vertex and fragment programs.
140 * \note changes to this opcode list must be reflected in t_vb_arbprogram.c
141 */
142 typedef enum prog_opcode {
143 /* ARB_vp ARB_fp NV_vp NV_fp GLSL */
144 /*------------------------------------------*/
145 OPCODE_NOP = 0, /* X */
146 OPCODE_ABS, /* X X 1.1 X */
147 OPCODE_ADD, /* X X X X X */
148 OPCODE_AND, /* */
149 OPCODE_ARA, /* 2 */
150 OPCODE_ARL, /* X X */
151 OPCODE_ARL_NV, /* 2 */
152 OPCODE_ARR, /* 2 */
153 OPCODE_BGNLOOP, /* opt */
154 OPCODE_BGNSUB, /* opt */
155 OPCODE_BRA, /* 2 X */
156 OPCODE_BRK, /* 2 opt */
157 OPCODE_CAL, /* 2 2 */
158 OPCODE_CMP, /* X */
159 OPCODE_CONT, /* opt */
160 OPCODE_COS, /* X 2 X X */
161 OPCODE_DDX, /* X X */
162 OPCODE_DDY, /* X X */
163 OPCODE_DP2, /* 2 */
164 OPCODE_DP2A, /* 2 */
165 OPCODE_DP3, /* X X X X X */
166 OPCODE_DP4, /* X X X X X */
167 OPCODE_DPH, /* X X 1.1 */
168 OPCODE_DST, /* X X X X */
169 OPCODE_ELSE, /* X */
170 OPCODE_END, /* X X X X opt */
171 OPCODE_ENDIF, /* opt */
172 OPCODE_ENDLOOP, /* opt */
173 OPCODE_ENDSUB, /* opt */
174 OPCODE_EX2, /* X X 2 X X */
175 OPCODE_EXP, /* X X X */
176 OPCODE_FLR, /* X X 2 X X */
177 OPCODE_FRC, /* X X 2 X X */
178 OPCODE_IF, /* opt */
179 OPCODE_KIL, /* X */
180 OPCODE_KIL_NV, /* X X */
181 OPCODE_LG2, /* X X 2 X X */
182 OPCODE_LIT, /* X X X X */
183 OPCODE_LOG, /* X X X */
184 OPCODE_LRP, /* X X */
185 OPCODE_MAD, /* X X X X X */
186 OPCODE_MAX, /* X X X X X */
187 OPCODE_MIN, /* X X X X X */
188 OPCODE_MOV, /* X X X X X */
189 OPCODE_MUL, /* X X X X X */
190 OPCODE_NOISE1, /* X */
191 OPCODE_NOISE2, /* X */
192 OPCODE_NOISE3, /* X */
193 OPCODE_NOISE4, /* X */
194 OPCODE_NOT, /* */
195 OPCODE_NRM3, /* */
196 OPCODE_NRM4, /* */
197 OPCODE_OR, /* */
198 OPCODE_PK2H, /* X */
199 OPCODE_PK2US, /* X */
200 OPCODE_PK4B, /* X */
201 OPCODE_PK4UB, /* X */
202 OPCODE_POW, /* X X X X */
203 OPCODE_POPA, /* 3 */
204 OPCODE_PRINT, /* X X */
205 OPCODE_PUSHA, /* 3 */
206 OPCODE_RCC, /* 1.1 */
207 OPCODE_RCP, /* X X X X X */
208 OPCODE_RET, /* 2 2 */
209 OPCODE_RFL, /* X X */
210 OPCODE_RSQ, /* X X X X X */
211 OPCODE_SCS, /* X */
212 OPCODE_SEQ, /* 2 X X */
213 OPCODE_SFL, /* 2 X */
214 OPCODE_SGE, /* X X X X X */
215 OPCODE_SGT, /* 2 X X */
216 OPCODE_SIN, /* X 2 X X */
217 OPCODE_SLE, /* 2 X X */
218 OPCODE_SLT, /* X X X X X */
219 OPCODE_SNE, /* 2 X X */
220 OPCODE_SSG, /* 2 */
221 OPCODE_STR, /* 2 X */
222 OPCODE_SUB, /* X X 1.1 X X */
223 OPCODE_SWZ, /* X X */
224 OPCODE_TEX, /* X 3 X X */
225 OPCODE_TXB, /* X 3 X */
226 OPCODE_TXD, /* X X */
227 OPCODE_TXL, /* 3 2 X */
228 OPCODE_TXP, /* X X */
229 OPCODE_TXP_NV, /* 3 X */
230 OPCODE_TRUNC, /* X */
231 OPCODE_UP2H, /* X */
232 OPCODE_UP2US, /* X */
233 OPCODE_UP4B, /* X */
234 OPCODE_UP4UB, /* X */
235 OPCODE_X2D, /* X */
236 OPCODE_XOR, /* */
237 OPCODE_XPD, /* X X X */
238 MAX_OPCODE
239 } gl_inst_opcode;
240
241
242 /* temporary, just in case, remove soon */
243 #define OPCODE_INT OPCODE_TRUNC
244
245 /**
246 * Instruction source register.
247 */
248 struct prog_src_register
249 {
250 GLuint File:4; /**< One of the PROGRAM_* register file values. */
251 GLint Index:9; /**< May be negative for relative addressing. */
252 GLuint Swizzle:12;
253 GLuint RelAddr:1;
254
255 /**
256 * \name Source register "sign" control.
257 *
258 * The ARB and NV extensions allow varrying degrees of control over the
259 * sign of the source vector components. These values allow enough control
260 * for all flavors of the extensions.
261 */
262 /*@{*/
263 /**
264 * Per-component negation for the SWZ instruction. For non-SWZ
265 * instructions the only possible values are NEGATE_XYZW and NEGATE_NONE.
266 *
267 * \since
268 * ARB_vertex_program, ARB_fragment_program
269 */
270 GLuint NegateBase:4;
271
272 /**
273 * Take the component-wise absolute value.
274 *
275 * \since
276 * NV_fragment_program, NV_fragment_program_option, NV_vertex_program2,
277 * NV_vertex_program2_option.
278 */
279 GLuint Abs:1;
280
281 /**
282 * Post-absolute value negation (all components).
283 */
284 GLuint NegateAbs:1;
285 /*@}*/
286 };
287
288
289 /**
290 * Instruction destination register.
291 */
292 struct prog_dst_register
293 {
294 /**
295 * One of the PROGRAM_* register file values.
296 */
297 GLuint File:4;
298
299 GLuint Index:8;
300 GLuint WriteMask:4;
301
302 /**
303 * \name Conditional destination update control.
304 *
305 * \since
306 * NV_fragment_program, NV_fragment_program_option, NV_vertex_program2,
307 * NV_vertex_program2_option.
308 */
309 /*@{*/
310 /**
311 * Takes one of the 9 possible condition values (EQ, FL, GT, GE, LE, LT,
312 * NE, TR, or UN). Dest reg is only written to if the matching
313 * (swizzled) condition code value passes. When a conditional update mask
314 * is not specified, this will be \c COND_TR.
315 */
316 GLuint CondMask:4;
317
318 /**
319 * Condition code swizzle value.
320 */
321 GLuint CondSwizzle:12;
322
323 /**
324 * Selects the condition code register to use for conditional destination
325 * update masking. In NV_fragmnet_program or NV_vertex_program2 mode, only
326 * condition code register 0 is available. In NV_vertex_program3 mode,
327 * condition code registers 0 and 1 are available.
328 */
329 GLuint CondSrc:1;
330 /*@}*/
331
332 GLuint pad:31;
333 };
334
335
336 /**
337 * Vertex/fragment program instruction.
338 */
339 struct prog_instruction
340 {
341 gl_inst_opcode Opcode;
342 #if FEATURE_MESA_program_debug
343 GLshort StringPos;
344 #endif
345 /**
346 * Arbitrary data. Used for the PRINT, CAL, and BRA instructions.
347 */
348 void *Data;
349
350 struct prog_src_register SrcReg[3];
351 struct prog_dst_register DstReg;
352
353 /**
354 * Indicates that the instruction should update the condition code
355 * register.
356 *
357 * \since
358 * NV_fragment_program, NV_fragment_program_option, NV_vertex_program2,
359 * NV_vertex_program2_option.
360 */
361 GLuint CondUpdate:1;
362
363 /**
364 * If prog_instruction::CondUpdate is \c GL_TRUE, this value selects the
365 * condition code register that is to be updated.
366 *
367 * In GL_NV_fragment_program or GL_NV_vertex_program2 mode, only condition
368 * code register 0 is available. In GL_NV_vertex_program3 mode, condition
369 * code registers 0 and 1 are available.
370 *
371 * \since
372 * NV_fragment_program, NV_fragment_program_option, NV_vertex_program2,
373 * NV_vertex_program2_option.
374 */
375 GLuint CondDst:1;
376
377 /**
378 * Saturate each value of the vectored result to the range [0,1] or the
379 * range [-1,1]. \c SSAT mode (i.e., saturation to the range [-1,1]) is
380 * only available in NV_fragment_program2 mode.
381 * Value is one of the SATURATE_* tokens.
382 *
383 * \since
384 * NV_fragment_program, NV_fragment_program_option, NV_vertex_program3.
385 */
386 GLuint SaturateMode:2;
387
388 /**
389 * Per-instruction selectable precision.
390 *
391 * \since
392 * NV_fragment_program, NV_fragment_program_option.
393 */
394 GLuint Precision:3;
395
396 /**
397 * \name Texture source controls.
398 *
399 * The texture source controls are only used with the \c TEX, \c TXD,
400 * \c TXL, and \c TXP instructions.
401 *
402 * \since
403 * ARB_fragment_program, NV_fragment_program, NV_vertex_program3.
404 */
405 /*@{*/
406 /**
407 * Source texture unit. OpenGL supports a maximum of 32 texture
408 * units.
409 */
410 GLuint TexSrcUnit:5;
411
412 /**
413 * Source texture target, one of TEXTURE_{1D,2D,3D,CUBE,RECT}_INDEX.
414 */
415 GLuint TexSrcTarget:3;
416 /*@}*/
417
418 /**
419 * For BRA and CAL instructions, the location to jump to.
420 * For BGNLOOP, points to ENDLOOP (and vice-versa).
421 * For BRK, points to BGNLOOP (which points to ENDLOOP).
422 * For IF, points to else or endif.
423 * For ELSE, points to endif.
424 */
425 GLint BranchTarget;
426
427 #if 0
428 /**
429 * For TEX instructions in shaders, the sampler to use for the
430 * texture lookup.
431 */
432 GLint Sampler;
433 #endif
434
435 const char *Comment;
436 };
437
438
439 extern void
440 _mesa_init_instructions(struct prog_instruction *inst, GLuint count);
441
442 extern struct prog_instruction *
443 _mesa_alloc_instructions(GLuint numInst);
444
445 extern struct prog_instruction *
446 _mesa_realloc_instructions(struct prog_instruction *oldInst,
447 GLuint numOldInst, GLuint numNewInst);
448
449 extern struct prog_instruction *
450 _mesa_copy_instructions(struct prog_instruction *dest,
451 const struct prog_instruction *src, GLuint n);
452
453 extern void
454 _mesa_free_instructions(struct prog_instruction *inst, GLuint count);
455
456 extern GLuint
457 _mesa_num_inst_src_regs(gl_inst_opcode opcode);
458
459 extern GLuint
460 _mesa_num_inst_dst_regs(gl_inst_opcode opcode);
461
462 extern GLboolean
463 _mesa_is_tex_instruction(gl_inst_opcode opcode);
464
465 extern const char *
466 _mesa_opcode_string(gl_inst_opcode opcode);
467
468
469 #endif /* PROG_INSTRUCTION_H */