slang: Fix order of parameters to sl_pp_tokenise().
[mesa.git] / src / mesa / shader / slang / slang_ir.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2005-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. 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 * \file slang_ir.h
27 * Mesa GLSL Intermediate Representation tree types and constants.
28 * \author Brian Paul
29 */
30
31
32 #ifndef SLANG_IR_H
33 #define SLANG_IR_H
34
35
36 #include "main/imports.h"
37 #include "slang_compile.h"
38 #include "slang_label.h"
39 #include "main/mtypes.h"
40
41
42 /**
43 * Intermediate Representation opcodes
44 */
45 typedef enum
46 {
47 IR_NOP = 0,
48 IR_SEQ, /* sequence (eval left, then right) */
49 IR_SCOPE, /* new variable scope (one child) */
50
51 IR_LABEL, /* target of a jump or cjump */
52
53 IR_COND, /* conditional expression/predicate */
54
55 IR_IF, /* high-level IF/then/else */
56 /* Children[0] = conditional expression */
57 /* Children[1] = if-true part */
58 /* Children[2] = if-else part, or NULL */
59
60 IR_BEGIN_SUB, /* begin subroutine */
61 IR_END_SUB, /* end subroutine */
62 IR_RETURN, /* return from subroutine */
63 IR_CALL, /* call subroutine */
64
65 IR_LOOP, /* high-level loop-begin / loop-end */
66 /* Children[0] = loop body */
67 /* Children[1] = loop tail code, or NULL */
68
69 IR_CONT, /* continue loop */
70 /* n->Parent = ptr to parent IR_LOOP Node */
71 IR_BREAK, /* break loop */
72
73 IR_BREAK_IF_TRUE, /**< Children[0] = the condition expression */
74 IR_CONT_IF_TRUE,
75
76 IR_COPY, /**< assignment/copy */
77 IR_MOVE, /**< assembly MOV instruction */
78
79 /* vector ops: */
80 IR_ADD, /**< assembly ADD instruction */
81 IR_SUB,
82 IR_MUL,
83 IR_DIV,
84 IR_DOT4,
85 IR_DOT3,
86 IR_DOT2,
87 IR_NRM4,
88 IR_NRM3,
89 IR_CROSS, /* vec3 cross product */
90 IR_LRP,
91 IR_CLAMP,
92 IR_MIN,
93 IR_MAX,
94 IR_SEQUAL, /* Set if args are equal (vector) */
95 IR_SNEQUAL, /* Set if args are not equal (vector) */
96 IR_SGE, /* Set if greater or equal (vector) */
97 IR_SGT, /* Set if greater than (vector) */
98 IR_SLE, /* Set if less or equal (vector) */
99 IR_SLT, /* Set if less than (vector) */
100 IR_POW, /* x^y */
101 IR_EXP, /* e^x */
102 IR_EXP2, /* 2^x */
103 IR_LOG2, /* log base 2 */
104 IR_RSQ, /* 1/sqrt() */
105 IR_RCP, /* reciprocol */
106 IR_FLOOR,
107 IR_FRAC,
108 IR_ABS, /* absolute value */
109 IR_NEG, /* negate */
110 IR_DDX, /* derivative w.r.t. X */
111 IR_DDY, /* derivative w.r.t. Y */
112 IR_SIN, /* sine */
113 IR_COS, /* cosine */
114 IR_NOISE1, /* noise(x) */
115 IR_NOISE2, /* noise(x, y) */
116 IR_NOISE3, /* noise(x, y, z) */
117 IR_NOISE4, /* noise(x, y, z, w) */
118
119 IR_EQUAL, /* boolean equality */
120 IR_NOTEQUAL,/* boolean inequality */
121 IR_NOT, /* boolean not */
122
123 IR_VAR, /* variable reference */
124 IR_VAR_DECL,/* var declaration */
125
126 IR_ELEMENT, /* array element */
127 IR_FIELD, /* struct field */
128 IR_SWIZZLE, /* swizzled storage access */
129
130 IR_TEX, /* texture lookup */
131 IR_TEXB, /* texture lookup with LOD bias */
132 IR_TEXP, /* texture lookup with projection */
133
134 IR_TEX_SH, /* texture lookup, shadow compare */
135 IR_TEXB_SH, /* texture lookup with LOD bias, shadow compare */
136 IR_TEXP_SH, /* texture lookup with projection, shadow compare */
137
138 IR_FLOAT,
139 IR_I_TO_F, /* int[4] to float[4] conversion */
140 IR_F_TO_I, /* float[4] to int[4] conversion */
141
142 IR_KILL /* fragment kill/discard */
143 } slang_ir_opcode;
144
145
146 /**
147 * Describes where data/variables are stored in the various register files.
148 *
149 * In the simple case, the File, Index and Size fields indicate where
150 * a variable is stored. For example, a vec3 variable may be stored
151 * as (File=PROGRAM_TEMPORARY, Index=6, Size=3). Or, File[Index].
152 * Or, a program input like color may be stored as
153 * (File=PROGRAM_INPUT,Index=3,Size=4);
154 *
155 * For single-float values, the Swizzle field indicates which component
156 * of the vector contains the float.
157 *
158 * If IsIndirect is set, the storage is accessed through an indirect
159 * register lookup. The value in question will be located at:
160 * File[Index + IndirectFile[IndirectIndex]]
161 *
162 * This is primary used for indexing arrays. For example, consider this
163 * GLSL code:
164 * uniform int i;
165 * float a[10];
166 * float x = a[i];
167 *
168 * here, storage for a[i] would be described by (File=PROGRAM_TEMPORAY,
169 * Index=aPos, IndirectFile=PROGRAM_UNIFORM, IndirectIndex=iPos), which
170 * would mean TEMP[aPos + UNIFORM[iPos]]
171 */
172 struct slang_ir_storage_
173 {
174 gl_register_file File; /**< PROGRAM_TEMPORARY, PROGRAM_INPUT, etc */
175 GLint Index; /**< -1 means unallocated */
176 GLint Size; /**< number of floats or ints */
177 GLuint Swizzle; /**< Swizzle AND writemask info */
178 GLint RefCount; /**< Used during IR tree delete */
179
180 GLboolean RelAddr; /* we'll remove this eventually */
181
182 GLboolean IsIndirect;
183 gl_register_file IndirectFile;
184 GLint IndirectIndex;
185 GLuint IndirectSwizzle;
186 GLuint TexTarget; /**< If File==PROGRAM_SAMPLER, one of TEXTURE_x_INDEX */
187
188 /** If Parent is non-null, Index is relative to parent.
189 * The other fields are ignored.
190 */
191 struct slang_ir_storage_ *Parent;
192 };
193
194 typedef struct slang_ir_storage_ slang_ir_storage;
195
196
197 /**
198 * Intermediate Representation (IR) tree node
199 * Basically a binary tree, but IR_LRP and IR_CLAMP have three children.
200 */
201 typedef struct slang_ir_node_
202 {
203 slang_ir_opcode Opcode;
204 struct slang_ir_node_ *Children[3];
205 slang_ir_storage *Store; /**< location of result of this operation */
206 GLint InstLocation; /**< Location of instruction emitted for this node */
207
208 /** special fields depending on Opcode: */
209 const char *Field; /**< If Opcode == IR_FIELD */
210 GLfloat Value[4]; /**< If Opcode == IR_FLOAT */
211 slang_variable *Var; /**< If Opcode == IR_VAR or IR_VAR_DECL */
212 struct slang_ir_node_ *List; /**< For various linked lists */
213 struct slang_ir_node_ *Parent; /**< Pointer to logical parent (ie. loop) */
214 slang_label *Label; /**< Used for branches */
215 const char *Comment; /**< If Opcode == IR_COMMENT */
216 } slang_ir_node;
217
218
219
220 /**
221 * Assembly and IR info
222 */
223 typedef struct
224 {
225 slang_ir_opcode IrOpcode;
226 const char *IrName;
227 gl_inst_opcode InstOpcode;
228 GLuint ResultSize, NumParams;
229 } slang_ir_info;
230
231
232
233 extern const slang_ir_info *
234 _slang_ir_info(slang_ir_opcode opcode);
235
236
237 extern void
238 _slang_init_ir_storage(slang_ir_storage *st,
239 gl_register_file file, GLint index, GLint size,
240 GLuint swizzle);
241
242 extern slang_ir_storage *
243 _slang_new_ir_storage(gl_register_file file, GLint index, GLint size);
244
245
246 extern slang_ir_storage *
247 _slang_new_ir_storage_swz(gl_register_file file, GLint index, GLint size,
248 GLuint swizzle);
249
250 extern slang_ir_storage *
251 _slang_new_ir_storage_relative(GLint index, GLint size,
252 slang_ir_storage *parent);
253
254
255 extern slang_ir_storage *
256 _slang_new_ir_storage_indirect(gl_register_file file,
257 GLint index,
258 GLint size,
259 gl_register_file indirectFile,
260 GLint indirectIndex,
261 GLuint indirectSwizzle);
262
263 extern slang_ir_storage *
264 _slang_new_ir_storage_sampler(GLint sampNum, GLuint texTarget, GLint size);
265
266
267 extern void
268 _slang_copy_ir_storage(slang_ir_storage *dst, const slang_ir_storage *src);
269
270
271 extern void
272 _slang_free_ir_tree(slang_ir_node *n);
273
274
275 extern void
276 _slang_print_ir_tree(const slang_ir_node *n, int indent);
277
278
279 #endif /* SLANG_IR_H */