Implement CONT, improve BRK.
[mesa.git] / src / mesa / shader / prog_print.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 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 * \file prog_print.c
27 * Print vertex/fragment programs - for debugging.
28 * \author Brian Paul
29 */
30
31 #include "glheader.h"
32 #include "context.h"
33 #include "imports.h"
34 #include "prog_instruction.h"
35 #include "prog_parameter.h"
36 #include "prog_print.h"
37 #include "prog_statevars.h"
38
39
40 /**
41 * Return string name for given program/register file.
42 */
43 static const char *
44 program_file_string(enum register_file f)
45 {
46 switch (f) {
47 case PROGRAM_TEMPORARY:
48 return "TEMP";
49 case PROGRAM_LOCAL_PARAM:
50 return "LOCAL";
51 case PROGRAM_ENV_PARAM:
52 return "ENV";
53 case PROGRAM_STATE_VAR:
54 return "STATE";
55 case PROGRAM_INPUT:
56 return "INPUT";
57 case PROGRAM_OUTPUT:
58 return "OUTPUT";
59 case PROGRAM_NAMED_PARAM:
60 return "NAMED";
61 case PROGRAM_CONSTANT:
62 return "CONST";
63 case PROGRAM_UNIFORM:
64 return "UNIFORM";
65 case PROGRAM_VARYING:
66 return "VARYING";
67 case PROGRAM_WRITE_ONLY:
68 return "WRITE_ONLY";
69 case PROGRAM_ADDRESS:
70 return "ADDR";
71 case PROGRAM_SAMPLER:
72 return "SAMPLER";
73 default:
74 return "Unknown program file!";
75 }
76 }
77
78
79 /**
80 * Return a string representation of the given swizzle word.
81 * If extended is true, use extended (comma-separated) format.
82 * \param swizzle the swizzle field
83 * \param negateBase 4-bit negation vector
84 * \param extended if true, also allow 0, 1 values
85 */
86 static const char *
87 swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
88 {
89 static const char swz[] = "xyzw01?!";
90 static char s[20];
91 GLuint i = 0;
92
93 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
94 return ""; /* no swizzle/negation */
95
96 if (!extended)
97 s[i++] = '.';
98
99 if (negateBase & 0x1)
100 s[i++] = '-';
101 s[i++] = swz[GET_SWZ(swizzle, 0)];
102
103 if (extended) {
104 s[i++] = ',';
105 }
106
107 if (negateBase & 0x2)
108 s[i++] = '-';
109 s[i++] = swz[GET_SWZ(swizzle, 1)];
110
111 if (extended) {
112 s[i++] = ',';
113 }
114
115 if (negateBase & 0x4)
116 s[i++] = '-';
117 s[i++] = swz[GET_SWZ(swizzle, 2)];
118
119 if (extended) {
120 s[i++] = ',';
121 }
122
123 if (negateBase & 0x8)
124 s[i++] = '-';
125 s[i++] = swz[GET_SWZ(swizzle, 3)];
126
127 s[i] = 0;
128 return s;
129 }
130
131
132 static const char *
133 writemask_string(GLuint writeMask)
134 {
135 static char s[10];
136 GLuint i = 0;
137
138 if (writeMask == WRITEMASK_XYZW)
139 return "";
140
141 s[i++] = '.';
142 if (writeMask & WRITEMASK_X)
143 s[i++] = 'x';
144 if (writeMask & WRITEMASK_Y)
145 s[i++] = 'y';
146 if (writeMask & WRITEMASK_Z)
147 s[i++] = 'z';
148 if (writeMask & WRITEMASK_W)
149 s[i++] = 'w';
150
151 s[i] = 0;
152 return s;
153 }
154
155
156 static const char *
157 condcode_string(GLuint condcode)
158 {
159 switch (condcode) {
160 case COND_GT: return "GT";
161 case COND_EQ: return "EQ";
162 case COND_LT: return "LT";
163 case COND_UN: return "UN";
164 case COND_GE: return "GE";
165 case COND_LE: return "LE";
166 case COND_NE: return "NE";
167 case COND_TR: return "TR";
168 case COND_FL: return "FL";
169 default: return "cond???";
170 }
171 }
172
173
174 static void
175 print_dst_reg(const struct prog_dst_register *dstReg)
176 {
177 _mesa_printf(" %s[%d]%s",
178 program_file_string((enum register_file) dstReg->File),
179 dstReg->Index,
180 writemask_string(dstReg->WriteMask));
181 }
182
183 static void
184 print_src_reg(const struct prog_src_register *srcReg)
185 {
186 _mesa_printf("%s[%d]%s",
187 program_file_string((enum register_file) srcReg->File),
188 srcReg->Index,
189 swizzle_string(srcReg->Swizzle,
190 srcReg->NegateBase, GL_FALSE));
191 }
192
193 static void
194 print_comment(const struct prog_instruction *inst)
195 {
196 if (inst->Comment)
197 _mesa_printf("; # %s\n", inst->Comment);
198 else
199 _mesa_printf(";\n");
200 }
201
202
203 void
204 _mesa_print_alu_instruction(const struct prog_instruction *inst,
205 const char *opcode_string,
206 GLuint numRegs)
207 {
208 GLuint j;
209
210 _mesa_printf("%s", opcode_string);
211 if (inst->CondUpdate)
212 _mesa_printf(".C");
213
214 /* frag prog only */
215 if (inst->SaturateMode == SATURATE_ZERO_ONE)
216 _mesa_printf("_SAT");
217
218 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
219 print_dst_reg(&inst->DstReg);
220 }
221 else {
222 _mesa_printf(" ???");
223 }
224
225 if (numRegs > 0)
226 _mesa_printf(", ");
227
228 for (j = 0; j < numRegs; j++) {
229 print_src_reg(inst->SrcReg + j);
230 if (j + 1 < numRegs)
231 _mesa_printf(", ");
232 }
233
234 print_comment(inst);
235 }
236
237
238 /**
239 * Print a single vertex/fragment program instruction.
240 */
241 GLint
242 _mesa_print_instruction(const struct prog_instruction *inst, GLint indent)
243 {
244 GLuint i;
245
246 if (inst->Opcode == OPCODE_ELSE ||
247 inst->Opcode == OPCODE_ENDIF ||
248 inst->Opcode == OPCODE_ENDLOOP ||
249 inst->Opcode == OPCODE_ENDSUB) {
250 indent -= 3;
251 }
252 assert(indent >= 0);
253 for (i = 0; i < indent; i++) {
254 _mesa_printf(" ");
255 }
256
257 switch (inst->Opcode) {
258 case OPCODE_PRINT:
259 _mesa_printf("PRINT '%s'", inst->Data);
260 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
261 _mesa_printf(", ");
262 _mesa_printf("%s[%d]%s",
263 program_file_string((enum register_file) inst->SrcReg[0].File),
264 inst->SrcReg[0].Index,
265 swizzle_string(inst->SrcReg[0].Swizzle,
266 inst->SrcReg[0].NegateBase, GL_FALSE));
267 }
268 if (inst->Comment)
269 _mesa_printf(" # %s", inst->Comment);
270 print_comment(inst);
271 break;
272 case OPCODE_SWZ:
273 _mesa_printf("SWZ");
274 if (inst->SaturateMode == SATURATE_ZERO_ONE)
275 _mesa_printf("_SAT");
276 print_dst_reg(&inst->DstReg);
277 _mesa_printf("%s[%d], %s",
278 program_file_string((enum register_file) inst->SrcReg[0].File),
279 inst->SrcReg[0].Index,
280 swizzle_string(inst->SrcReg[0].Swizzle,
281 inst->SrcReg[0].NegateBase, GL_TRUE));
282 print_comment(inst);
283 break;
284 case OPCODE_TEX:
285 case OPCODE_TXP:
286 case OPCODE_TXB:
287 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
288 if (inst->SaturateMode == SATURATE_ZERO_ONE)
289 _mesa_printf("_SAT");
290 print_dst_reg(&inst->DstReg);
291 _mesa_printf(", ");
292 print_src_reg(&inst->SrcReg[0]);
293 _mesa_printf(", texture[%d], ", inst->TexSrcUnit);
294 switch (inst->TexSrcTarget) {
295 case TEXTURE_1D_INDEX: _mesa_printf("1D"); break;
296 case TEXTURE_2D_INDEX: _mesa_printf("2D"); break;
297 case TEXTURE_3D_INDEX: _mesa_printf("3D"); break;
298 case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break;
299 case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break;
300 default:
301 ;
302 }
303 print_comment(inst);
304 break;
305 case OPCODE_ARL:
306 _mesa_printf("ARL addr.x, ");
307 print_src_reg(&inst->SrcReg[0]);
308 print_comment(inst);
309 break;
310 case OPCODE_BRA:
311 _mesa_printf("BRA %u (%s%s)",
312 inst->BranchTarget,
313 condcode_string(inst->DstReg.CondMask),
314 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
315 print_comment(inst);
316 break;
317 case OPCODE_CAL:
318 _mesa_printf("CAL %u", inst->BranchTarget);
319 print_comment(inst);
320 break;
321 case OPCODE_IF:
322 _mesa_printf("IF (%s%s) (if false, goto %d)",
323 condcode_string(inst->DstReg.CondMask),
324 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
325 inst->BranchTarget);
326 print_comment(inst);
327 return indent + 3;
328 case OPCODE_ELSE:
329 _mesa_printf("ELSE (goto %d)\n", inst->BranchTarget);
330 return indent + 3;
331 case OPCODE_ENDIF:
332 _mesa_printf("ENDIF\n");
333 break;
334 case OPCODE_BGNLOOP:
335 _mesa_printf("BGNLOOP (end at %d)\n", inst->BranchTarget);
336 return indent + 3;
337 case OPCODE_ENDLOOP:
338 _mesa_printf("ENDLOOP (goto %d)\n", inst->BranchTarget);
339 break;
340 case OPCODE_BRK:
341 _mesa_printf("BRK (%s%s) (goto %d)",
342 condcode_string(inst->DstReg.CondMask),
343 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
344 inst->BranchTarget);
345 print_comment(inst);
346 break;
347 case OPCODE_CONT:
348 _mesa_printf("CONT (%s%s) (goto %d)",
349 condcode_string(inst->DstReg.CondMask),
350 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
351 inst->BranchTarget);
352 print_comment(inst);
353 break;
354 case OPCODE_BGNSUB:
355 _mesa_printf("SUB;\n");
356 print_comment(inst);
357 return indent + 3;
358 case OPCODE_ENDSUB:
359 _mesa_printf("ENDSUB;\n");
360 print_comment(inst);
361 break;
362 case OPCODE_END:
363 _mesa_printf("END");
364 print_comment(inst);
365 break;
366 case OPCODE_NOP:
367 _mesa_printf("NOP");
368 print_comment(inst);
369 break;
370 /* XXX may need other special-case instructions */
371 default:
372 /* typical alu instruction */
373 _mesa_print_alu_instruction(inst,
374 _mesa_opcode_string(inst->Opcode),
375 _mesa_num_inst_src_regs(inst->Opcode));
376 break;
377 }
378 return indent;
379 }
380
381
382 /**
383 * Print a vertx/fragment program to stdout.
384 * XXX this function could be greatly improved.
385 */
386 void
387 _mesa_print_program(const struct gl_program *prog)
388 {
389 GLuint i, indent = 0;
390 for (i = 0; i < prog->NumInstructions; i++) {
391 _mesa_printf("%3d: ", i);
392 indent = _mesa_print_instruction(prog->Instructions + i, indent);
393 }
394 }
395
396
397 /**
398 * Print all of a program's parameters.
399 */
400 void
401 _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
402 {
403 _mesa_printf("InputsRead: 0x%x\n", prog->InputsRead);
404 _mesa_printf("OutputsWritten: 0x%x\n", prog->OutputsWritten);
405 _mesa_printf("NumInstructions=%d\n", prog->NumInstructions);
406 _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries);
407 _mesa_printf("NumParameters=%d\n", prog->NumParameters);
408 _mesa_printf("NumAttributes=%d\n", prog->NumAttributes);
409 _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs);
410
411 _mesa_load_state_parameters(ctx, prog->Parameters);
412
413 #if 0
414 _mesa_printf("Local Params:\n");
415 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
416 const GLfloat *p = prog->LocalParams[i];
417 _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
418 }
419 #endif
420 _mesa_print_parameter_list(prog->Parameters);
421 }
422
423
424 void
425 _mesa_print_parameter_list(const struct gl_program_parameter_list *list)
426 {
427 GLuint i;
428 _mesa_printf("param list %p\n", (void *) list);
429 for (i = 0; i < list->NumParameters; i++){
430 struct gl_program_parameter *param = list->Parameters + i;
431 const GLfloat *v = list->ParameterValues[i];
432 _mesa_printf("param[%d] sz=%d %s %s = {%.3f, %.3f, %.3f, %.3f};\n",
433 i, param->Size,
434 program_file_string(list->Parameters[i].Type),
435 param->Name, v[0], v[1], v[2], v[3]);
436 }
437 }