Merge branch 'mesa_7_7_branch'
[mesa.git] / src / mesa / shader / prog_instruction.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.3
4 *
5 * Copyright (C) 1999-2008 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 #include "main/mfeatures.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
63 #define SWIZZLE_XYZW MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W)
64 #define SWIZZLE_XXXX MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X)
65 #define SWIZZLE_YYYY MAKE_SWIZZLE4(SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y)
66 #define SWIZZLE_ZZZZ MAKE_SWIZZLE4(SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Z)
67 #define SWIZZLE_WWWW MAKE_SWIZZLE4(SWIZZLE_W, SWIZZLE_W, SWIZZLE_W, SWIZZLE_W)
68
69
70 /**
71 * Writemask values, 1 bit per component.
72 */
73 /*@{*/
74 #define WRITEMASK_X 0x1
75 #define WRITEMASK_Y 0x2
76 #define WRITEMASK_XY 0x3
77 #define WRITEMASK_Z 0x4
78 #define WRITEMASK_XZ 0x5
79 #define WRITEMASK_YZ 0x6
80 #define WRITEMASK_XYZ 0x7
81 #define WRITEMASK_W 0x8
82 #define WRITEMASK_XW 0x9
83 #define WRITEMASK_YW 0xa
84 #define WRITEMASK_XYW 0xb
85 #define WRITEMASK_ZW 0xc
86 #define WRITEMASK_XZW 0xd
87 #define WRITEMASK_YZW 0xe
88 #define WRITEMASK_XYZW 0xf
89 /*@}*/
90
91
92 /**
93 * Condition codes
94 */
95 /*@{*/
96 #define COND_GT 1 /**< greater than zero */
97 #define COND_EQ 2 /**< equal to zero */
98 #define COND_LT 3 /**< less than zero */
99 #define COND_UN 4 /**< unordered (NaN) */
100 #define COND_GE 5 /**< greater then or equal to zero */
101 #define COND_LE 6 /**< less then or equal to zero */
102 #define COND_NE 7 /**< not equal to zero */
103 #define COND_TR 8 /**< always true */
104 #define COND_FL 9 /**< always false */
105 /*@}*/
106
107
108 /**
109 * Instruction precision for GL_NV_fragment_program
110 */
111 /*@{*/
112 #define FLOAT32 0x1
113 #define FLOAT16 0x2
114 #define FIXED12 0x4
115 /*@}*/
116
117
118 /**
119 * Saturation modes when storing values.
120 */
121 /*@{*/
122 #define SATURATE_OFF 0
123 #define SATURATE_ZERO_ONE 1
124 /*@}*/
125
126
127 /**
128 * Per-component negation masks
129 */
130 /*@{*/
131 #define NEGATE_X 0x1
132 #define NEGATE_Y 0x2
133 #define NEGATE_Z 0x4
134 #define NEGATE_W 0x8
135 #define NEGATE_XYZ 0x7
136 #define NEGATE_XYZW 0xf
137 #define NEGATE_NONE 0x0
138 /*@}*/
139
140
141 /**
142 * Program instruction opcodes, for both vertex and fragment programs.
143 * \note changes to this opcode list must be reflected in t_vb_arbprogram.c
144 */
145 typedef enum prog_opcode {
146 /* ARB_vp ARB_fp NV_vp NV_fp GLSL */
147 /*------------------------------------------*/
148 OPCODE_NOP = 0, /* X */
149 OPCODE_ABS, /* X X 1.1 X */
150 OPCODE_ADD, /* X X X X X */
151 OPCODE_AND, /* */
152 OPCODE_ARA, /* 2 */
153 OPCODE_ARL, /* X X */
154 OPCODE_ARL_NV, /* 2 */
155 OPCODE_ARR, /* 2 */
156 OPCODE_BGNLOOP, /* opt */
157 OPCODE_BGNSUB, /* opt */
158 OPCODE_BRA, /* 2 X */
159 OPCODE_BRK, /* 2 opt */
160 OPCODE_CAL, /* 2 2 */
161 OPCODE_CMP, /* X */
162 OPCODE_CONT, /* opt */
163 OPCODE_COS, /* X 2 X X */
164 OPCODE_DDX, /* X X */
165 OPCODE_DDY, /* X X */
166 OPCODE_DP2, /* 2 */
167 OPCODE_DP2A, /* 2 */
168 OPCODE_DP3, /* X X X X X */
169 OPCODE_DP4, /* X X X X X */
170 OPCODE_DPH, /* X X 1.1 */
171 OPCODE_DST, /* X X X X */
172 OPCODE_ELSE, /* X */
173 OPCODE_END, /* X X X X opt */
174 OPCODE_ENDIF, /* opt */
175 OPCODE_ENDLOOP, /* opt */
176 OPCODE_ENDSUB, /* opt */
177 OPCODE_EX2, /* X X 2 X X */
178 OPCODE_EXP, /* X X X */
179 OPCODE_FLR, /* X X 2 X X */
180 OPCODE_FRC, /* X X 2 X X */
181 OPCODE_IF, /* opt */
182 OPCODE_KIL, /* X */
183 OPCODE_KIL_NV, /* X X */
184 OPCODE_LG2, /* X X 2 X X */
185 OPCODE_LIT, /* X X X X */
186 OPCODE_LOG, /* X X X */
187 OPCODE_LRP, /* X X */
188 OPCODE_MAD, /* X X X X X */
189 OPCODE_MAX, /* X X X X X */
190 OPCODE_MIN, /* X X X X X */
191 OPCODE_MOV, /* X X X X X */
192 OPCODE_MUL, /* X X X X X */
193 OPCODE_NOISE1, /* X */
194 OPCODE_NOISE2, /* X */
195 OPCODE_NOISE3, /* X */
196 OPCODE_NOISE4, /* X */
197 OPCODE_NOT, /* */
198 OPCODE_NRM3, /* */
199 OPCODE_NRM4, /* */
200 OPCODE_OR, /* */
201 OPCODE_PK2H, /* X */
202 OPCODE_PK2US, /* X */
203 OPCODE_PK4B, /* X */
204 OPCODE_PK4UB, /* X */
205 OPCODE_POW, /* X X X X */
206 OPCODE_POPA, /* 3 */
207 OPCODE_PRINT, /* X X */
208 OPCODE_PUSHA, /* 3 */
209 OPCODE_RCC, /* 1.1 */
210 OPCODE_RCP, /* X X X X X */
211 OPCODE_RET, /* 2 2 */
212 OPCODE_RFL, /* X X */
213 OPCODE_RSQ, /* X X X X X */
214 OPCODE_SCS, /* X */
215 OPCODE_SEQ, /* 2 X X */
216 OPCODE_SFL, /* 2 X */
217 OPCODE_SGE, /* X X X X X */
218 OPCODE_SGT, /* 2 X X */
219 OPCODE_SIN, /* X 2 X X */
220 OPCODE_SLE, /* 2 X X */
221 OPCODE_SLT, /* X X X X X */
222 OPCODE_SNE, /* 2 X X */
223 OPCODE_SSG, /* 2 */
224 OPCODE_STR, /* 2 X */
225 OPCODE_SUB, /* X X 1.1 X X */
226 OPCODE_SWZ, /* X X */
227 OPCODE_TEX, /* X 3 X X */
228 OPCODE_TXB, /* X 3 X */
229 OPCODE_TXD, /* X X */
230 OPCODE_TXL, /* 3 2 X */
231 OPCODE_TXP, /* X X */
232 OPCODE_TXP_NV, /* 3 X */
233 OPCODE_TRUNC, /* X */
234 OPCODE_UP2H, /* X */
235 OPCODE_UP2US, /* X */
236 OPCODE_UP4B, /* X */
237 OPCODE_UP4UB, /* X */
238 OPCODE_X2D, /* X */
239 OPCODE_XOR, /* */
240 OPCODE_XPD, /* X X X */
241 MAX_OPCODE
242 } gl_inst_opcode;
243
244
245 /**
246 * Number of bits for the src/dst register Index field.
247 * This limits the size of temp/uniform register files.
248 */
249 #define INST_INDEX_BITS 10
250
251
252 /**
253 * Instruction source register.
254 */
255 struct prog_src_register
256 {
257 GLuint File:4; /**< One of the PROGRAM_* register file values. */
258 GLint Index:(INST_INDEX_BITS+1); /**< Extra bit here for sign bit.
259 * May be negative for relative addressing.
260 */
261 GLuint Swizzle:12;
262 GLuint RelAddr:1;
263
264 /** Take the component-wise absolute value */
265 GLuint Abs:1;
266
267 /**
268 * Post-Abs negation.
269 * This will either be NEGATE_NONE or NEGATE_XYZW, except for the SWZ
270 * instruction which allows per-component negation.
271 */
272 GLuint Negate:4;
273 };
274
275
276 /**
277 * Instruction destination register.
278 */
279 struct prog_dst_register
280 {
281 GLuint File:4; /**< One of the PROGRAM_* register file values */
282 GLuint Index:INST_INDEX_BITS; /**< Unsigned, never negative */
283 GLuint WriteMask:4;
284 GLuint RelAddr:1;
285
286 /**
287 * \name Conditional destination update control.
288 *
289 * \since
290 * NV_fragment_program, NV_fragment_program_option, NV_vertex_program2,
291 * NV_vertex_program2_option.
292 */
293 /*@{*/
294 /**
295 * Takes one of the 9 possible condition values (EQ, FL, GT, GE, LE, LT,
296 * NE, TR, or UN). Dest reg is only written to if the matching
297 * (swizzled) condition code value passes. When a conditional update mask
298 * is not specified, this will be \c COND_TR.
299 */
300 GLuint CondMask:4;
301
302 /**
303 * Condition code swizzle value.
304 */
305 GLuint CondSwizzle:12;
306
307 /**
308 * Selects the condition code register to use for conditional destination
309 * update masking. In NV_fragmnet_program or NV_vertex_program2 mode, only
310 * condition code register 0 is available. In NV_vertex_program3 mode,
311 * condition code registers 0 and 1 are available.
312 */
313 GLuint CondSrc:1;
314 /*@}*/
315 };
316
317
318 /**
319 * Vertex/fragment program instruction.
320 */
321 struct prog_instruction
322 {
323 gl_inst_opcode Opcode;
324 struct prog_src_register SrcReg[3];
325 struct prog_dst_register DstReg;
326
327 /**
328 * Indicates that the instruction should update the condition code
329 * register.
330 *
331 * \since
332 * NV_fragment_program, NV_fragment_program_option, NV_vertex_program2,
333 * NV_vertex_program2_option.
334 */
335 GLuint CondUpdate:1;
336
337 /**
338 * If prog_instruction::CondUpdate is \c GL_TRUE, this value selects the
339 * condition code register that is to be updated.
340 *
341 * In GL_NV_fragment_program or GL_NV_vertex_program2 mode, only condition
342 * code register 0 is available. In GL_NV_vertex_program3 mode, condition
343 * code registers 0 and 1 are available.
344 *
345 * \since
346 * NV_fragment_program, NV_fragment_program_option, NV_vertex_program2,
347 * NV_vertex_program2_option.
348 */
349 GLuint CondDst:1;
350
351 /**
352 * Saturate each value of the vectored result to the range [0,1] or the
353 * range [-1,1]. \c SSAT mode (i.e., saturation to the range [-1,1]) is
354 * only available in NV_fragment_program2 mode.
355 * Value is one of the SATURATE_* tokens.
356 *
357 * \since
358 * NV_fragment_program, NV_fragment_program_option, NV_vertex_program3.
359 */
360 GLuint SaturateMode:2;
361
362 /**
363 * Per-instruction selectable precision: FLOAT32, FLOAT16, FIXED12.
364 *
365 * \since
366 * NV_fragment_program, NV_fragment_program_option.
367 */
368 GLuint Precision:3;
369
370 /**
371 * \name Extra fields for TEX, TXB, TXD, TXL, TXP instructions.
372 */
373 /*@{*/
374 /** Source texture unit. */
375 GLuint TexSrcUnit:5;
376
377 /** Source texture target, one of TEXTURE_{1D,2D,3D,CUBE,RECT}_INDEX */
378 GLuint TexSrcTarget:3;
379
380 /** True if tex instruction should do shadow comparison */
381 GLuint TexShadow:1;
382 /*@}*/
383
384 /**
385 * For BRA and CAL instructions, the location to jump to.
386 * For BGNLOOP, points to ENDLOOP (and vice-versa).
387 * For BRK, points to BGNLOOP (which points to ENDLOOP).
388 * For IF, points to ELSE or ENDIF.
389 * For ELSE, points to ENDIF.
390 */
391 GLint BranchTarget;
392
393 /** for debugging purposes */
394 const char *Comment;
395
396 /** Arbitrary data. Used for OPCODE_PRINT and some drivers */
397 void *Data;
398
399 /** for driver use (try to remove someday) */
400 GLint Aux;
401 };
402
403
404 extern void
405 _mesa_init_instructions(struct prog_instruction *inst, GLuint count);
406
407 extern struct prog_instruction *
408 _mesa_alloc_instructions(GLuint numInst);
409
410 extern struct prog_instruction *
411 _mesa_realloc_instructions(struct prog_instruction *oldInst,
412 GLuint numOldInst, GLuint numNewInst);
413
414 extern struct prog_instruction *
415 _mesa_copy_instructions(struct prog_instruction *dest,
416 const struct prog_instruction *src, GLuint n);
417
418 extern void
419 _mesa_free_instructions(struct prog_instruction *inst, GLuint count);
420
421 extern GLuint
422 _mesa_num_inst_src_regs(gl_inst_opcode opcode);
423
424 extern GLuint
425 _mesa_num_inst_dst_regs(gl_inst_opcode opcode);
426
427 extern GLboolean
428 _mesa_is_tex_instruction(gl_inst_opcode opcode);
429
430 extern GLboolean
431 _mesa_check_soa_dependencies(const struct prog_instruction *inst);
432
433 extern const char *
434 _mesa_opcode_string(gl_inst_opcode opcode);
435
436
437 #endif /* PROG_INSTRUCTION_H */