0957bd9d77982a4756c59f2097271c65b57499f4
[mesa.git] / src / mesa / program / prog_instruction.h
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
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 than or equal to zero */
101 #define COND_LE 6 /**< less than 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 vertex, fragment and geometry programs.
143 */
144 typedef enum prog_opcode {
145 /* ARB_vp ARB_fp NV_vp NV_fp GLSL */
146 /*------------------------------------------*/
147 OPCODE_NOP = 0, /* X */
148 OPCODE_ABS, /* X X 1.1 X */
149 OPCODE_ADD, /* X X X X X */
150 OPCODE_ARL, /* X X X */
151 OPCODE_BGNLOOP, /* opt */
152 OPCODE_BGNSUB, /* opt */
153 OPCODE_BRK, /* 2 opt */
154 OPCODE_CAL, /* 2 2 opt */
155 OPCODE_CMP, /* X X */
156 OPCODE_CONT, /* opt */
157 OPCODE_COS, /* X 2 X X */
158 OPCODE_DDX, /* X X */
159 OPCODE_DDY, /* X X */
160 OPCODE_DP2, /* 2 X */
161 OPCODE_DP3, /* X X X X X */
162 OPCODE_DP4, /* X X X X X */
163 OPCODE_DPH, /* X X 1.1 */
164 OPCODE_DST, /* X X X X */
165 OPCODE_ELSE, /* opt */
166 OPCODE_END, /* X X X X opt */
167 OPCODE_ENDIF, /* opt */
168 OPCODE_ENDLOOP, /* opt */
169 OPCODE_ENDSUB, /* opt */
170 OPCODE_EX2, /* X X 2 X X */
171 OPCODE_EXP, /* X X */
172 OPCODE_FLR, /* X X 2 X X */
173 OPCODE_FRC, /* X X 2 X X */
174 OPCODE_IF, /* opt */
175 OPCODE_KIL, /* X X */
176 OPCODE_KIL_NV, /* X X */
177 OPCODE_LG2, /* X X 2 X X */
178 OPCODE_LIT, /* X X X X */
179 OPCODE_LOG, /* X X */
180 OPCODE_LRP, /* X X */
181 OPCODE_MAD, /* X X X X X */
182 OPCODE_MAX, /* X X X X X */
183 OPCODE_MIN, /* X X X X X */
184 OPCODE_MOV, /* X X X X X */
185 OPCODE_MUL, /* X X X X X */
186 OPCODE_NOISE1, /* X */
187 OPCODE_NOISE2, /* X */
188 OPCODE_NOISE3, /* X */
189 OPCODE_NOISE4, /* X */
190 OPCODE_POW, /* X X X X */
191 OPCODE_RCP, /* X X X X X */
192 OPCODE_RET, /* 2 2 opt */
193 OPCODE_RSQ, /* X X X X X */
194 OPCODE_SCS, /* X X */
195 OPCODE_SEQ, /* 2 X X */
196 OPCODE_SGE, /* X X X X X */
197 OPCODE_SGT, /* 2 X X */
198 OPCODE_SIN, /* X 2 X X */
199 OPCODE_SLE, /* 2 X X */
200 OPCODE_SLT, /* X X X X X */
201 OPCODE_SNE, /* 2 X X */
202 OPCODE_SSG, /* 2 X */
203 OPCODE_SUB, /* X X 1.1 X X */
204 OPCODE_SWZ, /* X X X */
205 OPCODE_TEX, /* X 3 X X */
206 OPCODE_TXB, /* X 3 X */
207 OPCODE_TXD, /* X X */
208 OPCODE_TXL, /* 3 2 X */
209 OPCODE_TXP, /* X X */
210 OPCODE_TXP_NV, /* 3 X */
211 OPCODE_TRUNC, /* X */
212 OPCODE_XPD, /* X X */
213 MAX_OPCODE
214 } gl_inst_opcode;
215
216
217 /**
218 * Number of bits for the src/dst register Index field.
219 * This limits the size of temp/uniform register files.
220 */
221 #define INST_INDEX_BITS 12
222
223
224 /**
225 * Instruction source register.
226 */
227 struct prog_src_register
228 {
229 GLuint File:4; /**< One of the PROGRAM_* register file values. */
230 GLint Index:(INST_INDEX_BITS+1); /**< Extra bit here for sign bit.
231 * May be negative for relative addressing.
232 */
233 GLuint Swizzle:12;
234 GLuint RelAddr:1;
235
236 /** Take the component-wise absolute value */
237 GLuint Abs:1;
238
239 /**
240 * Post-Abs negation.
241 * This will either be NEGATE_NONE or NEGATE_XYZW, except for the SWZ
242 * instruction which allows per-component negation.
243 */
244 GLuint Negate:4;
245
246 /**
247 * Is the register two-dimensional.
248 * Two dimensional registers are of the
249 * REGISTER[index][index2] format.
250 * They are used by the geometry shaders where
251 * the first index is the index within an array
252 * and the second index is the semantic of the
253 * array, e.g. gl_PositionIn[index] would become
254 * INPUT[index][gl_PositionIn]
255 */
256 GLuint HasIndex2:1;
257 GLuint RelAddr2:1;
258 GLint Index2:(INST_INDEX_BITS+1); /**< Extra bit here for sign bit.
259 * May be negative for relative
260 * addressing. */
261 };
262
263
264 /**
265 * Instruction destination register.
266 */
267 struct prog_dst_register
268 {
269 GLuint File:4; /**< One of the PROGRAM_* register file values */
270 GLuint Index:INST_INDEX_BITS; /**< Unsigned, never negative */
271 GLuint WriteMask:4;
272 GLuint RelAddr:1;
273
274 /**
275 * \name Conditional destination update control.
276 *
277 * \since
278 * NV_fragment_program_option, NV_vertex_program2, NV_vertex_program2_option.
279 */
280 /*@{*/
281 /**
282 * Takes one of the 9 possible condition values (EQ, FL, GT, GE, LE, LT,
283 * NE, TR, or UN). Dest reg is only written to if the matching
284 * (swizzled) condition code value passes. When a conditional update mask
285 * is not specified, this will be \c COND_TR.
286 */
287 GLuint CondMask:4;
288
289 /**
290 * Condition code swizzle value.
291 */
292 GLuint CondSwizzle:12;
293 };
294
295
296 /**
297 * Vertex/fragment program instruction.
298 */
299 struct prog_instruction
300 {
301 gl_inst_opcode Opcode;
302 struct prog_src_register SrcReg[3];
303 struct prog_dst_register DstReg;
304
305 /**
306 * Indicates that the instruction should update the condition code
307 * register.
308 *
309 * \since
310 * NV_fragment_program_option, NV_vertex_program2, NV_vertex_program2_option.
311 */
312 GLuint CondUpdate:1;
313
314 /**
315 * If prog_instruction::CondUpdate is \c GL_TRUE, this value selects the
316 * condition code register that is to be updated.
317 *
318 * In GL_NV_fragment_program or GL_NV_vertex_program2 mode, only condition
319 * code register 0 is available. In GL_NV_vertex_program3 mode, condition
320 * code registers 0 and 1 are available.
321 *
322 * \since
323 * NV_fragment_program_option, NV_vertex_program2, NV_vertex_program2_option.
324 */
325 GLuint CondDst:1;
326
327 /**
328 * Saturate each value of the vectored result to the range [0,1] or the
329 * range [-1,1]. \c SSAT mode (i.e., saturation to the range [-1,1]) is
330 * only available in NV_fragment_program2 mode.
331 * Value is one of the SATURATE_* tokens.
332 *
333 * \since
334 * NV_fragment_program_option, NV_vertex_program3.
335 */
336 GLuint SaturateMode:2;
337
338 /**
339 * Per-instruction selectable precision: FLOAT32, FLOAT16, FIXED12.
340 *
341 * \since
342 * NV_fragment_program_option.
343 */
344 GLuint Precision:3;
345
346 /**
347 * \name Extra fields for TEX, TXB, TXD, TXL, TXP instructions.
348 */
349 /*@{*/
350 /** Source texture unit. */
351 GLuint TexSrcUnit:5;
352
353 /** Source texture target, one of TEXTURE_{1D,2D,3D,CUBE,RECT}_INDEX */
354 GLuint TexSrcTarget:4;
355
356 /** True if tex instruction should do shadow comparison */
357 GLuint TexShadow:1;
358 /*@}*/
359
360 /**
361 * For BRA and CAL instructions, the location to jump to.
362 * For BGNLOOP, points to ENDLOOP (and vice-versa).
363 * For BRK, points to ENDLOOP
364 * For IF, points to ELSE or ENDIF.
365 * For ELSE, points to ENDIF.
366 */
367 GLint BranchTarget;
368
369 /** for debugging purposes */
370 const char *Comment;
371
372 /** for driver use (try to remove someday) */
373 GLint Aux;
374 };
375
376
377 #ifdef __cplusplus
378 extern "C" {
379 #endif
380
381 extern void
382 _mesa_init_instructions(struct prog_instruction *inst, GLuint count);
383
384 extern struct prog_instruction *
385 _mesa_alloc_instructions(GLuint numInst);
386
387 extern struct prog_instruction *
388 _mesa_copy_instructions(struct prog_instruction *dest,
389 const struct prog_instruction *src, GLuint n);
390
391 extern void
392 _mesa_free_instructions(struct prog_instruction *inst, GLuint count);
393
394 extern GLuint
395 _mesa_num_inst_src_regs(gl_inst_opcode opcode);
396
397 extern GLuint
398 _mesa_num_inst_dst_regs(gl_inst_opcode opcode);
399
400 extern GLboolean
401 _mesa_is_tex_instruction(gl_inst_opcode opcode);
402
403 extern GLboolean
404 _mesa_check_soa_dependencies(const struct prog_instruction *inst);
405
406 extern const char *
407 _mesa_opcode_string(gl_inst_opcode opcode);
408
409
410 #ifdef __cplusplus
411 } /* extern "C" */
412 #endif
413
414 #endif /* PROG_INSTRUCTION_H */