2 * Mesa 3-D graphics library
4 * Copyright (C) 2005-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Mesa GLSL Intermediate Representation tree types and constants.
36 #include "main/imports.h"
37 #include "slang_compile.h"
38 #include "slang_label.h"
39 #include "main/mtypes.h"
40 #include "program/prog_instruction.h"
44 * Intermediate Representation opcodes
49 IR_SEQ
, /* sequence (eval left, then right) */
50 IR_SCOPE
, /* new variable scope (one child) */
52 IR_LABEL
, /* target of a jump or cjump */
54 IR_COND
, /* conditional expression/predicate */
56 IR_IF
, /* high-level IF/then/else */
57 /* Children[0] = conditional expression */
58 /* Children[1] = if-true part */
59 /* Children[2] = if-else part, or NULL */
61 IR_BEGIN_SUB
, /* begin subroutine */
62 IR_END_SUB
, /* end subroutine */
63 IR_RETURN
, /* return from subroutine */
64 IR_CALL
, /* call subroutine */
66 IR_LOOP
, /* high-level loop-begin / loop-end */
67 /* Children[0] = loop body */
68 /* Children[1] = loop tail code, or NULL */
70 IR_CONT
, /* continue loop */
71 /* n->Parent = ptr to parent IR_LOOP Node */
72 IR_BREAK
, /* break loop */
74 IR_BREAK_IF_TRUE
, /**< Children[0] = the condition expression */
77 IR_COPY
, /**< assignment/copy */
78 IR_MOVE
, /**< assembly MOV instruction */
81 IR_ADD
, /**< assembly ADD instruction */
90 IR_CROSS
, /* vec3 cross product */
95 IR_CMP
, /* = (op0 < 0) ? op1 : op2 */
96 IR_SEQUAL
, /* Set if args are equal (vector) */
97 IR_SNEQUAL
, /* Set if args are not equal (vector) */
98 IR_SGE
, /* Set if greater or equal (vector) */
99 IR_SGT
, /* Set if greater than (vector) */
100 IR_SLE
, /* Set if less or equal (vector) */
101 IR_SLT
, /* Set if less than (vector) */
105 IR_LOG2
, /* log base 2 */
106 IR_RSQ
, /* 1/sqrt() */
107 IR_RCP
, /* reciprocol */
110 IR_ABS
, /* absolute value */
112 IR_DDX
, /* derivative w.r.t. X */
113 IR_DDY
, /* derivative w.r.t. Y */
116 IR_NOISE1
, /* noise(x) */
117 IR_NOISE2
, /* noise(x, y) */
118 IR_NOISE3
, /* noise(x, y, z) */
119 IR_NOISE4
, /* noise(x, y, z, w) */
121 IR_EQUAL
, /* boolean equality */
122 IR_NOTEQUAL
,/* boolean inequality */
123 IR_NOT
, /* boolean not */
125 IR_VAR
, /* variable reference */
126 IR_VAR_DECL
,/* var declaration */
128 IR_ELEMENT
, /* array element */
129 IR_FIELD
, /* struct field */
130 IR_SWIZZLE
, /* swizzled storage access */
132 IR_TEX
, /* texture lookup */
133 IR_TEXB
, /* texture lookup with LOD bias */
134 IR_TEXP
, /* texture lookup with projection */
136 IR_TEX_SH
, /* texture lookup, shadow compare */
137 IR_TEXB_SH
, /* texture lookup with LOD bias, shadow compare */
138 IR_TEXP_SH
, /* texture lookup with projection, shadow compare */
141 IR_I_TO_F
, /* int[4] to float[4] conversion */
142 IR_F_TO_I
, /* float[4] to int[4] conversion */
144 IR_KILL
, /* fragment kill/discard */
146 IR_EMIT_VERTEX
, /* geometry shader: emit vertex */
147 IR_END_PRIMITIVE
/* geometry shader: end primitive */
152 * Describes where data/variables are stored in the various register files.
154 * In the simple case, the File, Index and Size fields indicate where
155 * a variable is stored. For example, a vec3 variable may be stored
156 * as (File=PROGRAM_TEMPORARY, Index=6, Size=3). Or, File[Index].
157 * Or, a program input like color may be stored as
158 * (File=PROGRAM_INPUT,Index=3,Size=4);
160 * For single-float values, the Swizzle field indicates which component
161 * of the vector contains the float.
163 * If IsIndirect is set, the storage is accessed through an indirect
164 * register lookup. The value in question will be located at:
165 * File[Index + IndirectFile[IndirectIndex]]
167 * This is primary used for indexing arrays. For example, consider this
173 * here, storage for a[i] would be described by (File=PROGRAM_TEMPORAY,
174 * Index=aPos, IndirectFile=PROGRAM_UNIFORM, IndirectIndex=iPos), which
175 * would mean TEMP[aPos + UNIFORM[iPos]]
177 struct slang_ir_storage_
179 gl_register_file File
; /**< PROGRAM_TEMPORARY, PROGRAM_INPUT, etc */
180 GLint Index
; /**< -1 means unallocated */
181 GLint Size
; /**< number of floats or ints */
182 GLuint Swizzle
; /**< Swizzle AND writemask info */
183 GLint RefCount
; /**< Used during IR tree delete */
185 GLboolean RelAddr
; /* we'll remove this eventually */
187 GLboolean IsIndirect
;
188 gl_register_file IndirectFile
;
190 GLuint IndirectSwizzle
;
191 GLuint TexTarget
; /**< If File==PROGRAM_SAMPLER, one of TEXTURE_x_INDEX */
193 /* Is the register two-dimensional and
194 * if so what's the second index */
198 /** If Parent is non-null, Index is relative to parent.
199 * The other fields are ignored.
201 struct slang_ir_storage_
*Parent
;
204 typedef struct slang_ir_storage_ slang_ir_storage
;
208 * Intermediate Representation (IR) tree node
209 * Basically a binary tree, but IR_LRP and IR_CLAMP have three children.
211 typedef struct slang_ir_node_
213 slang_ir_opcode Opcode
;
214 struct slang_ir_node_
*Children
[3];
215 slang_ir_storage
*Store
; /**< location of result of this operation */
216 GLint InstLocation
; /**< Location of instruction emitted for this node */
218 /** special fields depending on Opcode: */
219 const char *Field
; /**< If Opcode == IR_FIELD */
220 GLfloat Value
[4]; /**< If Opcode == IR_FLOAT */
221 slang_variable
*Var
; /**< If Opcode == IR_VAR or IR_VAR_DECL */
222 struct slang_ir_node_
*List
; /**< For various linked lists */
223 struct slang_ir_node_
*Parent
; /**< Pointer to logical parent (ie. loop) */
224 slang_label
*Label
; /**< Used for branches */
225 const char *Comment
; /**< If Opcode == IR_COMMENT */
231 * Assembly and IR info
235 slang_ir_opcode IrOpcode
;
237 gl_inst_opcode InstOpcode
;
238 GLuint ResultSize
, NumParams
;
243 extern const slang_ir_info
*
244 _slang_ir_info(slang_ir_opcode opcode
);
248 _slang_init_ir_storage(slang_ir_storage
*st
,
249 gl_register_file file
, GLint index
, GLint size
,
252 extern slang_ir_storage
*
253 _slang_new_ir_storage(gl_register_file file
, GLint index
, GLint size
);
256 extern slang_ir_storage
*
257 _slang_new_ir_storage_swz(gl_register_file file
, GLint index
, GLint size
,
260 extern slang_ir_storage
*
261 _slang_new_ir_storage_2d(gl_register_file file
, GLint index
, GLint index2d
,
262 GLint size
, GLuint swizzle
);
264 extern slang_ir_storage
*
265 _slang_new_ir_storage_relative(GLint index
, GLint size
,
266 slang_ir_storage
*parent
);
269 extern slang_ir_storage
*
270 _slang_new_ir_storage_indirect(gl_register_file file
,
273 gl_register_file indirectFile
,
275 GLuint indirectSwizzle
);
277 extern slang_ir_storage
*
278 _slang_new_ir_storage_sampler(GLint sampNum
, GLuint texTarget
, GLint size
);
282 _slang_copy_ir_storage(slang_ir_storage
*dst
, const slang_ir_storage
*src
);
286 _slang_free_ir_tree(slang_ir_node
*n
);
290 _slang_print_ir_tree(const slang_ir_node
*n
, int indent
);
293 #endif /* SLANG_IR_H */