add missing license texts
[mesa.git] / src / mesa / drivers / dri / i915 / i915_debug.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "i915_reg.h"
29 #include "i915_context.h"
30 #include <stdio.h>
31
32
33 static const char *opcodes[0x20] = {
34 "NOP",
35 "ADD",
36 "MOV",
37 "MUL",
38 "MAD",
39 "DP2ADD",
40 "DP3",
41 "DP4",
42 "FRC",
43 "RCP",
44 "RSQ",
45 "EXP",
46 "LOG",
47 "CMP",
48 "MIN",
49 "MAX",
50 "FLR",
51 "MOD",
52 "TRC",
53 "SGE",
54 "SLT",
55 "TEXLD",
56 "TEXLDP",
57 "TEXLDB",
58 "TEXKILL",
59 "DCL",
60 "0x1a",
61 "0x1b",
62 "0x1c",
63 "0x1d",
64 "0x1e",
65 "0x1f",
66 };
67
68
69 static const int args[0x20] = {
70 0, /* 0 nop */
71 2, /* 1 add */
72 1, /* 2 mov */
73 2, /* 3 m ul */
74 3, /* 4 mad */
75 3, /* 5 dp2add */
76 2, /* 6 dp3 */
77 2, /* 7 dp4 */
78 1, /* 8 frc */
79 1, /* 9 rcp */
80 1, /* a rsq */
81 1, /* b exp */
82 1, /* c log */
83 3, /* d cmp */
84 2, /* e min */
85 2, /* f max */
86 1, /* 10 flr */
87 1, /* 11 mod */
88 1, /* 12 trc */
89 2, /* 13 sge */
90 2, /* 14 slt */
91 1,
92 1,
93 1,
94 1,
95 0,
96 0,
97 0,
98 0,
99 0,
100 0,
101 0,
102 };
103
104
105 static const char *regname[0x8] = {
106 "R",
107 "T",
108 "CONST",
109 "S",
110 "OC",
111 "OD",
112 "U",
113 "UNKNOWN",
114 };
115
116 static void print_reg_type_nr( GLuint type, GLuint nr )
117 {
118 switch (type) {
119 case REG_TYPE_T:
120 switch (nr) {
121 case T_DIFFUSE: fprintf(stderr, "T_DIFFUSE"); return;
122 case T_SPECULAR: fprintf(stderr, "T_SPECULAR"); return;
123 case T_FOG_W: fprintf(stderr, "T_FOG_W"); return;
124 default: fprintf(stderr, "T_TEX%d", nr); return;
125 }
126 case REG_TYPE_OC:
127 if (nr == 0) {
128 fprintf(stderr, "oC");
129 return;
130 }
131 break;
132 case REG_TYPE_OD:
133 if (nr == 0) {
134 fprintf(stderr, "oD");
135 return;
136 }
137 break;
138 default:
139 break;
140 }
141
142 fprintf(stderr, "%s[%d]", regname[type], nr);
143 }
144
145 #define REG_SWIZZLE_MASK 0x7777
146 #define REG_NEGATE_MASK 0x8888
147
148 #define REG_SWIZZLE_XYZW ((SRC_X << A2_SRC2_CHANNEL_X_SHIFT) | \
149 (SRC_Y << A2_SRC2_CHANNEL_Y_SHIFT) | \
150 (SRC_Z << A2_SRC2_CHANNEL_Z_SHIFT) | \
151 (SRC_W << A2_SRC2_CHANNEL_W_SHIFT))
152
153
154 static void print_reg_neg_swizzle( GLuint reg )
155 {
156 int i;
157
158 if ((reg & REG_SWIZZLE_MASK) == REG_SWIZZLE_XYZW &&
159 (reg & REG_NEGATE_MASK) == 0)
160 return;
161
162 fprintf(stderr, ".");
163
164 for (i = 3 ; i >= 0; i--) {
165 if (reg & (1<<((i*4)+3)))
166 fprintf(stderr, "-");
167
168 switch ((reg>>(i*4)) & 0x7) {
169 case 0: fprintf(stderr, "x"); break;
170 case 1: fprintf(stderr, "y"); break;
171 case 2: fprintf(stderr, "z"); break;
172 case 3: fprintf(stderr, "w"); break;
173 case 4: fprintf(stderr, "0"); break;
174 case 5: fprintf(stderr, "1"); break;
175 default: fprintf(stderr, "?"); break;
176 }
177 }
178 }
179
180
181 static void print_src_reg( GLuint dword )
182 {
183 GLuint nr = (dword >> A2_SRC2_NR_SHIFT) & REG_NR_MASK;
184 GLuint type = (dword >> A2_SRC2_TYPE_SHIFT) & REG_TYPE_MASK;
185 print_reg_type_nr( type, nr );
186 print_reg_neg_swizzle( dword );
187 }
188
189 void i915_print_ureg( const char *msg, GLuint ureg )
190 {
191 fprintf(stderr, "%s: ", msg);
192 print_src_reg( ureg >> 8 );
193 fprintf(stderr, "\n");
194 }
195
196 static void print_dest_reg( GLuint dword )
197 {
198 GLuint nr = (dword >> A0_DEST_NR_SHIFT) & REG_NR_MASK;
199 GLuint type = (dword >> A0_DEST_TYPE_SHIFT) & REG_TYPE_MASK;
200 print_reg_type_nr( type, nr );
201 if ((dword & A0_DEST_CHANNEL_ALL) == A0_DEST_CHANNEL_ALL)
202 return;
203 fprintf(stderr, ".");
204 if (dword & A0_DEST_CHANNEL_X) fprintf(stderr, "x");
205 if (dword & A0_DEST_CHANNEL_Y) fprintf(stderr, "y");
206 if (dword & A0_DEST_CHANNEL_Z) fprintf(stderr, "z");
207 if (dword & A0_DEST_CHANNEL_W) fprintf(stderr, "w");
208 }
209
210
211 #define GET_SRC0_REG(r0, r1) ((r0<<14)|(r1>>A1_SRC0_CHANNEL_W_SHIFT))
212 #define GET_SRC1_REG(r0, r1) ((r0<<8)|(r1>>A2_SRC1_CHANNEL_W_SHIFT))
213 #define GET_SRC2_REG(r) (r)
214
215
216 static void print_arith_op( GLuint opcode, const GLuint *program )
217 {
218 if (opcode != A0_NOP) {
219 print_dest_reg(program[0]);
220 if (program[0] & A0_DEST_SATURATE)
221 fprintf(stderr, " = SATURATE ");
222 else
223 fprintf(stderr, " = ");
224 }
225
226 fprintf(stderr, "%s ", opcodes[opcode]);
227
228 print_src_reg(GET_SRC0_REG(program[0], program[1]));
229 if (args[opcode] == 1) {
230 fprintf(stderr, "\n");
231 return;
232 }
233
234 fprintf(stderr, ", ");
235 print_src_reg(GET_SRC1_REG(program[1], program[2]));
236 if (args[opcode] == 2) {
237 fprintf(stderr, "\n");
238 return;
239 }
240
241 fprintf(stderr, ", ");
242 print_src_reg(GET_SRC2_REG(program[2]));
243 fprintf(stderr, "\n");
244 return;
245 }
246
247
248 static void print_tex_op( GLuint opcode, const GLuint *program )
249 {
250 print_dest_reg(program[0] | A0_DEST_CHANNEL_ALL);
251 fprintf(stderr, " = ");
252
253 fprintf(stderr, "%s ", opcodes[opcode]);
254
255 fprintf(stderr, "S[%d],",
256 program[0] & T0_SAMPLER_NR_MASK);
257
258 print_reg_type_nr( (program[1]>>T1_ADDRESS_REG_TYPE_SHIFT) & REG_TYPE_MASK,
259 (program[1]>>T1_ADDRESS_REG_NR_SHIFT) & REG_NR_MASK );
260 fprintf(stderr, "\n");
261 }
262
263 static void print_dcl_op( GLuint opcode, const GLuint *program )
264 {
265 fprintf(stderr, "%s ", opcodes[opcode]);
266 print_dest_reg(program[0] | A0_DEST_CHANNEL_ALL);
267 fprintf(stderr, "\n");
268 }
269
270
271 void i915_disassemble_program( const GLuint *program, GLuint sz )
272 {
273 GLuint size = program[0] & 0x1ff;
274 GLint i;
275
276 fprintf(stderr, "BEGIN\n");
277
278 if (size+2 != sz) {
279 fprintf(stderr, "%s: program size mismatch %d/%d\n", __FUNCTION__,
280 size+2, sz);
281 exit(1);
282 }
283
284 program ++;
285 for (i = 1 ; i < sz ; i+=3, program+=3) {
286 GLuint opcode = program[0] & (0x1f<<24);
287
288 if (opcode >= A0_NOP && opcode <= A0_SLT)
289 print_arith_op(opcode >> 24, program);
290 else if (opcode >= T0_TEXLD && opcode <= T0_TEXKILL)
291 print_tex_op(opcode >> 24, program);
292 else if (opcode == D0_DCL)
293 print_dcl_op(opcode >> 24, program);
294 else
295 fprintf(stderr, "Unknown opcode 0x%x\n", opcode);
296 }
297
298 fprintf(stderr, "END\n\n");
299 }