glsl: don't unroll loops containing continue/break
[mesa.git] / src / mesa / shader / prog_print.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.3
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions 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 MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file prog_print.c
28 * Print vertex/fragment programs - for debugging.
29 * \author Brian Paul
30 */
31
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/imports.h"
35 #include "prog_instruction.h"
36 #include "prog_parameter.h"
37 #include "prog_print.h"
38 #include "prog_statevars.h"
39
40
41
42 /**
43 * Return string name for given program/register file.
44 */
45 static const char *
46 file_string(gl_register_file f, gl_prog_print_mode mode)
47 {
48 switch (f) {
49 case PROGRAM_TEMPORARY:
50 return "TEMP";
51 case PROGRAM_LOCAL_PARAM:
52 return "LOCAL";
53 case PROGRAM_ENV_PARAM:
54 return "ENV";
55 case PROGRAM_STATE_VAR:
56 return "STATE";
57 case PROGRAM_INPUT:
58 return "INPUT";
59 case PROGRAM_OUTPUT:
60 return "OUTPUT";
61 case PROGRAM_NAMED_PARAM:
62 return "NAMED";
63 case PROGRAM_CONSTANT:
64 return "CONST";
65 case PROGRAM_UNIFORM:
66 return "UNIFORM";
67 case PROGRAM_VARYING:
68 return "VARYING";
69 case PROGRAM_WRITE_ONLY:
70 return "WRITE_ONLY";
71 case PROGRAM_ADDRESS:
72 return "ADDR";
73 case PROGRAM_SAMPLER:
74 return "SAMPLER";
75 case PROGRAM_UNDEFINED:
76 return "UNDEFINED";
77 default:
78 return "Unknown program file!";
79 }
80 }
81
82
83 /**
84 * Return ARB_v/f_prog-style input attrib string.
85 */
86 static const char *
87 arb_input_attrib_string(GLint index, GLenum progType)
88 {
89 /*
90 * These strings should match the VERT_ATTRIB_x and FRAG_ATTRIB_x tokens.
91 */
92 const char *vertAttribs[] = {
93 "vertex.position",
94 "vertex.weight",
95 "vertex.normal",
96 "vertex.color.primary",
97 "vertex.color.secondary",
98 "vertex.fogcoord",
99 "vertex.(six)",
100 "vertex.(seven)",
101 "vertex.texcoord[0]",
102 "vertex.texcoord[1]",
103 "vertex.texcoord[2]",
104 "vertex.texcoord[3]",
105 "vertex.texcoord[4]",
106 "vertex.texcoord[5]",
107 "vertex.texcoord[6]",
108 "vertex.texcoord[7]",
109 "vertex.attrib[0]",
110 "vertex.attrib[1]",
111 "vertex.attrib[2]",
112 "vertex.attrib[3]",
113 "vertex.attrib[4]",
114 "vertex.attrib[5]",
115 "vertex.attrib[6]",
116 "vertex.attrib[7]",
117 "vertex.attrib[8]",
118 "vertex.attrib[9]",
119 "vertex.attrib[10]",
120 "vertex.attrib[11]",
121 "vertex.attrib[12]",
122 "vertex.attrib[13]",
123 "vertex.attrib[14]",
124 "vertex.attrib[15]"
125 };
126 const char *fragAttribs[] = {
127 "fragment.position",
128 "fragment.color.primary",
129 "fragment.color.secondary",
130 "fragment.fogcoord",
131 "fragment.texcoord[0]",
132 "fragment.texcoord[1]",
133 "fragment.texcoord[2]",
134 "fragment.texcoord[3]",
135 "fragment.texcoord[4]",
136 "fragment.texcoord[5]",
137 "fragment.texcoord[6]",
138 "fragment.texcoord[7]",
139 "fragment.varying[0]",
140 "fragment.varying[1]",
141 "fragment.varying[2]",
142 "fragment.varying[3]",
143 "fragment.varying[4]",
144 "fragment.varying[5]",
145 "fragment.varying[6]",
146 "fragment.varying[7]"
147 };
148
149 if (progType == GL_VERTEX_PROGRAM_ARB) {
150 assert(index < sizeof(vertAttribs) / sizeof(vertAttribs[0]));
151 return vertAttribs[index];
152 }
153 else {
154 assert(index < sizeof(fragAttribs) / sizeof(fragAttribs[0]));
155 return fragAttribs[index];
156 }
157 }
158
159
160 /**
161 * Return ARB_v/f_prog-style output attrib string.
162 */
163 static const char *
164 arb_output_attrib_string(GLint index, GLenum progType)
165 {
166 /*
167 * These strings should match the VERT_RESULT_x and FRAG_RESULT_x tokens.
168 */
169 const char *vertResults[] = {
170 "result.position",
171 "result.color.primary",
172 "result.color.secondary",
173 "result.fogcoord",
174 "result.texcoord[0]",
175 "result.texcoord[1]",
176 "result.texcoord[2]",
177 "result.texcoord[3]",
178 "result.texcoord[4]",
179 "result.texcoord[5]",
180 "result.texcoord[6]",
181 "result.texcoord[7]",
182 "result.varying[0]",
183 "result.varying[1]",
184 "result.varying[2]",
185 "result.varying[3]",
186 "result.varying[4]",
187 "result.varying[5]",
188 "result.varying[6]",
189 "result.varying[7]"
190 };
191 const char *fragResults[] = {
192 "result.color",
193 "result.color(half)",
194 "result.depth",
195 "result.color[0]",
196 "result.color[1]",
197 "result.color[2]",
198 "result.color[3]"
199 };
200
201 if (progType == GL_VERTEX_PROGRAM_ARB) {
202 assert(index < sizeof(vertResults) / sizeof(vertResults[0]));
203 return vertResults[index];
204 }
205 else {
206 assert(index < sizeof(fragResults) / sizeof(fragResults[0]));
207 return fragResults[index];
208 }
209 }
210
211
212 /**
213 * Return string representation of the given register.
214 * Note that some types of registers (like PROGRAM_UNIFORM) aren't defined
215 * by the ARB/NV program languages so we've taken some liberties here.
216 * \param f the register file (PROGRAM_INPUT, PROGRAM_TEMPORARY, etc)
217 * \param index number of the register in the register file
218 * \param mode the output format/mode/style
219 * \param prog pointer to containing program
220 */
221 static const char *
222 reg_string(gl_register_file f, GLint index, gl_prog_print_mode mode,
223 GLboolean relAddr, const struct gl_program *prog)
224 {
225 static char str[100];
226 const char *addr = relAddr ? "ADDR+" : "";
227
228 str[0] = 0;
229
230 switch (mode) {
231 case PROG_PRINT_DEBUG:
232 _mesa_sprintf(str, "%s[%s%d]", file_string(f, mode), addr, index);
233 break;
234
235 case PROG_PRINT_ARB:
236 switch (f) {
237 case PROGRAM_INPUT:
238 _mesa_sprintf(str, "%s", arb_input_attrib_string(index, prog->Target));
239 break;
240 case PROGRAM_OUTPUT:
241 _mesa_sprintf(str, "%s", arb_output_attrib_string(index, prog->Target));
242 break;
243 case PROGRAM_TEMPORARY:
244 _mesa_sprintf(str, "temp%d", index);
245 break;
246 case PROGRAM_ENV_PARAM:
247 _mesa_sprintf(str, "program.env[%s%d]", addr, index);
248 break;
249 case PROGRAM_LOCAL_PARAM:
250 _mesa_sprintf(str, "program.local[%s%d]", addr, index);
251 break;
252 case PROGRAM_VARYING: /* extension */
253 _mesa_sprintf(str, "varying[%s%d]", addr, index);
254 break;
255 case PROGRAM_CONSTANT: /* extension */
256 _mesa_sprintf(str, "constant[%s%d]", addr, index);
257 break;
258 case PROGRAM_UNIFORM: /* extension */
259 _mesa_sprintf(str, "uniform[%s%d]", addr, index);
260 break;
261 case PROGRAM_STATE_VAR:
262 {
263 struct gl_program_parameter *param
264 = prog->Parameters->Parameters + index;
265 char *state = _mesa_program_state_string(param->StateIndexes);
266 _mesa_sprintf(str, state);
267 _mesa_free(state);
268 }
269 break;
270 case PROGRAM_ADDRESS:
271 _mesa_sprintf(str, "A%d", index);
272 break;
273 default:
274 _mesa_problem(NULL, "bad file in reg_string()");
275 }
276 break;
277
278 case PROG_PRINT_NV:
279 switch (f) {
280 case PROGRAM_INPUT:
281 if (prog->Target == GL_VERTEX_PROGRAM_ARB)
282 _mesa_sprintf(str, "v[%d]", index);
283 else
284 _mesa_sprintf(str, "f[%d]", index);
285 break;
286 case PROGRAM_OUTPUT:
287 _mesa_sprintf(str, "o[%d]", index);
288 break;
289 case PROGRAM_TEMPORARY:
290 _mesa_sprintf(str, "R%d", index);
291 break;
292 case PROGRAM_ENV_PARAM:
293 _mesa_sprintf(str, "c[%d]", index);
294 break;
295 case PROGRAM_VARYING: /* extension */
296 _mesa_sprintf(str, "varying[%s%d]", addr, index);
297 break;
298 case PROGRAM_UNIFORM: /* extension */
299 _mesa_sprintf(str, "uniform[%s%d]", addr, index);
300 break;
301 case PROGRAM_CONSTANT: /* extension */
302 _mesa_sprintf(str, "constant[%s%d]", addr, index);
303 break;
304 case PROGRAM_STATE_VAR: /* extension */
305 _mesa_sprintf(str, "state[%s%d]", addr, index);
306 break;
307 default:
308 _mesa_problem(NULL, "bad file in reg_string()");
309 }
310 break;
311
312 default:
313 _mesa_problem(NULL, "bad mode in reg_string()");
314 }
315
316 return str;
317 }
318
319
320 /**
321 * Return a string representation of the given swizzle word.
322 * If extended is true, use extended (comma-separated) format.
323 * \param swizzle the swizzle field
324 * \param negateBase 4-bit negation vector
325 * \param extended if true, also allow 0, 1 values
326 */
327 const char *
328 _mesa_swizzle_string(GLuint swizzle, GLuint negateMask, GLboolean extended)
329 {
330 static const char swz[] = "xyzw01!?"; /* See SWIZZLE_x definitions */
331 static char s[20];
332 GLuint i = 0;
333
334 if (!extended && swizzle == SWIZZLE_NOOP && negateMask == 0)
335 return ""; /* no swizzle/negation */
336
337 if (!extended)
338 s[i++] = '.';
339
340 if (negateMask & NEGATE_X)
341 s[i++] = '-';
342 s[i++] = swz[GET_SWZ(swizzle, 0)];
343
344 if (extended) {
345 s[i++] = ',';
346 }
347
348 if (negateMask & NEGATE_Y)
349 s[i++] = '-';
350 s[i++] = swz[GET_SWZ(swizzle, 1)];
351
352 if (extended) {
353 s[i++] = ',';
354 }
355
356 if (negateMask & NEGATE_Z)
357 s[i++] = '-';
358 s[i++] = swz[GET_SWZ(swizzle, 2)];
359
360 if (extended) {
361 s[i++] = ',';
362 }
363
364 if (negateMask & NEGATE_W)
365 s[i++] = '-';
366 s[i++] = swz[GET_SWZ(swizzle, 3)];
367
368 s[i] = 0;
369 return s;
370 }
371
372
373 void
374 _mesa_print_swizzle(GLuint swizzle)
375 {
376 if (swizzle == SWIZZLE_XYZW) {
377 _mesa_printf(".xyzw\n");
378 }
379 else {
380 const char *s = _mesa_swizzle_string(swizzle, 0, 0);
381 _mesa_printf("%s\n", s);
382 }
383 }
384
385
386 const char *
387 _mesa_writemask_string(GLuint writeMask)
388 {
389 static char s[10];
390 GLuint i = 0;
391
392 if (writeMask == WRITEMASK_XYZW)
393 return "";
394
395 s[i++] = '.';
396 if (writeMask & WRITEMASK_X)
397 s[i++] = 'x';
398 if (writeMask & WRITEMASK_Y)
399 s[i++] = 'y';
400 if (writeMask & WRITEMASK_Z)
401 s[i++] = 'z';
402 if (writeMask & WRITEMASK_W)
403 s[i++] = 'w';
404
405 s[i] = 0;
406 return s;
407 }
408
409
410 const char *
411 _mesa_condcode_string(GLuint condcode)
412 {
413 switch (condcode) {
414 case COND_GT: return "GT";
415 case COND_EQ: return "EQ";
416 case COND_LT: return "LT";
417 case COND_UN: return "UN";
418 case COND_GE: return "GE";
419 case COND_LE: return "LE";
420 case COND_NE: return "NE";
421 case COND_TR: return "TR";
422 case COND_FL: return "FL";
423 default: return "cond???";
424 }
425 }
426
427
428 static void
429 fprint_dst_reg(FILE * f,
430 const struct prog_dst_register *dstReg,
431 gl_prog_print_mode mode,
432 const struct gl_program *prog)
433 {
434 _mesa_fprintf(f, "%s%s",
435 reg_string((gl_register_file) dstReg->File,
436 dstReg->Index, mode, dstReg->RelAddr, prog),
437 _mesa_writemask_string(dstReg->WriteMask));
438
439 if (dstReg->CondMask != COND_TR) {
440 _mesa_fprintf(f, " (%s.%s)",
441 _mesa_condcode_string(dstReg->CondMask),
442 _mesa_swizzle_string(dstReg->CondSwizzle,
443 GL_FALSE, GL_FALSE));
444 }
445
446 #if 0
447 _mesa_fprintf(f, "%s[%d]%s",
448 file_string((gl_register_file) dstReg->File, mode),
449 dstReg->Index,
450 _mesa_writemask_string(dstReg->WriteMask));
451 #endif
452 }
453
454
455 static void
456 fprint_src_reg(FILE *f,
457 const struct prog_src_register *srcReg,
458 gl_prog_print_mode mode,
459 const struct gl_program *prog)
460 {
461 const char *abs = srcReg->Abs ? "|" : "";
462
463 _mesa_fprintf(f, "%s%s%s%s",
464 abs,
465 reg_string((gl_register_file) srcReg->File,
466 srcReg->Index, mode, srcReg->RelAddr, prog),
467 _mesa_swizzle_string(srcReg->Swizzle,
468 srcReg->Negate, GL_FALSE),
469 abs);
470 #if 0
471 _mesa_fprintf(f, "%s[%d]%s",
472 file_string((gl_register_file) srcReg->File, mode),
473 srcReg->Index,
474 _mesa_swizzle_string(srcReg->Swizzle,
475 srcReg->Negate, GL_FALSE));
476 #endif
477 }
478
479
480 static void
481 fprint_comment(FILE *f, const struct prog_instruction *inst)
482 {
483 if (inst->Comment)
484 _mesa_fprintf(f, "; # %s\n", inst->Comment);
485 else
486 _mesa_fprintf(f, ";\n");
487 }
488
489
490 static void
491 fprint_alu_instruction(FILE *f,
492 const struct prog_instruction *inst,
493 const char *opcode_string, GLuint numRegs,
494 gl_prog_print_mode mode,
495 const struct gl_program *prog)
496 {
497 GLuint j;
498
499 _mesa_fprintf(f, "%s", opcode_string);
500 if (inst->CondUpdate)
501 _mesa_fprintf(f, ".C");
502
503 /* frag prog only */
504 if (inst->SaturateMode == SATURATE_ZERO_ONE)
505 _mesa_fprintf(f, "_SAT");
506
507 _mesa_fprintf(f, " ");
508 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
509 fprint_dst_reg(f, &inst->DstReg, mode, prog);
510 }
511 else {
512 _mesa_fprintf(f, " ???");
513 }
514
515 if (numRegs > 0)
516 _mesa_fprintf(f, ", ");
517
518 for (j = 0; j < numRegs; j++) {
519 fprint_src_reg(f, inst->SrcReg + j, mode, prog);
520 if (j + 1 < numRegs)
521 _mesa_fprintf(f, ", ");
522 }
523
524 fprint_comment(f, inst);
525 }
526
527
528 void
529 _mesa_print_alu_instruction(const struct prog_instruction *inst,
530 const char *opcode_string, GLuint numRegs)
531 {
532 fprint_alu_instruction(stdout, inst, opcode_string,
533 numRegs, PROG_PRINT_DEBUG, NULL);
534 }
535
536
537 /**
538 * Print a single vertex/fragment program instruction.
539 */
540 static GLint
541 _mesa_fprint_instruction_opt(FILE *f,
542 const struct prog_instruction *inst,
543 GLint indent,
544 gl_prog_print_mode mode,
545 const struct gl_program *prog)
546 {
547 GLint i;
548
549 if (inst->Opcode == OPCODE_ELSE ||
550 inst->Opcode == OPCODE_ENDIF ||
551 inst->Opcode == OPCODE_ENDLOOP ||
552 inst->Opcode == OPCODE_ENDSUB) {
553 indent -= 3;
554 }
555 for (i = 0; i < indent; i++) {
556 _mesa_fprintf(f, " ");
557 }
558
559 switch (inst->Opcode) {
560 case OPCODE_PRINT:
561 _mesa_fprintf(f, "PRINT '%s'", inst->Data);
562 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
563 _mesa_fprintf(f, ", ");
564 _mesa_fprintf(f, "%s[%d]%s",
565 file_string((gl_register_file) inst->SrcReg[0].File,
566 mode),
567 inst->SrcReg[0].Index,
568 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
569 inst->SrcReg[0].Negate, GL_FALSE));
570 }
571 if (inst->Comment)
572 _mesa_fprintf(f, " # %s", inst->Comment);
573 fprint_comment(f, inst);
574 break;
575 case OPCODE_SWZ:
576 _mesa_fprintf(f, "SWZ");
577 if (inst->SaturateMode == SATURATE_ZERO_ONE)
578 _mesa_fprintf(f, "_SAT");
579 _mesa_fprintf(f, " ");
580 fprint_dst_reg(f, &inst->DstReg, mode, prog);
581 _mesa_fprintf(f, ", %s[%d], %s",
582 file_string((gl_register_file) inst->SrcReg[0].File,
583 mode),
584 inst->SrcReg[0].Index,
585 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
586 inst->SrcReg[0].Negate, GL_TRUE));
587 fprint_comment(f, inst);
588 break;
589 case OPCODE_TEX:
590 case OPCODE_TXP:
591 case OPCODE_TXL:
592 case OPCODE_TXB:
593 _mesa_fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
594 if (inst->SaturateMode == SATURATE_ZERO_ONE)
595 _mesa_fprintf(f, "_SAT");
596 _mesa_fprintf(f, " ");
597 fprint_dst_reg(f, &inst->DstReg, mode, prog);
598 _mesa_fprintf(f, ", ");
599 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
600 _mesa_fprintf(f, ", texture[%d], ", inst->TexSrcUnit);
601 switch (inst->TexSrcTarget) {
602 case TEXTURE_1D_INDEX: _mesa_fprintf(f, "1D"); break;
603 case TEXTURE_2D_INDEX: _mesa_fprintf(f, "2D"); break;
604 case TEXTURE_3D_INDEX: _mesa_fprintf(f, "3D"); break;
605 case TEXTURE_CUBE_INDEX: _mesa_fprintf(f, "CUBE"); break;
606 case TEXTURE_RECT_INDEX: _mesa_fprintf(f, "RECT"); break;
607 default:
608 ;
609 }
610 if (inst->TexShadow)
611 _mesa_fprintf(f, " SHADOW");
612 fprint_comment(f, inst);
613 break;
614
615 case OPCODE_KIL:
616 _mesa_fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
617 _mesa_fprintf(f, " ");
618 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
619 fprint_comment(f, inst);
620 break;
621 case OPCODE_KIL_NV:
622 _mesa_fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
623 _mesa_fprintf(f, " ");
624 _mesa_fprintf(f, "%s.%s",
625 _mesa_condcode_string(inst->DstReg.CondMask),
626 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
627 GL_FALSE, GL_FALSE));
628 fprint_comment(f, inst);
629 break;
630
631 case OPCODE_ARL:
632 _mesa_fprintf(f, "ARL ");
633 fprint_dst_reg(f, &inst->DstReg, mode, prog);
634 _mesa_fprintf(f, ", ");
635 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
636 fprint_comment(f, inst);
637 break;
638 case OPCODE_BRA:
639 _mesa_fprintf(f, "BRA %d (%s%s)",
640 inst->BranchTarget,
641 _mesa_condcode_string(inst->DstReg.CondMask),
642 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
643 fprint_comment(f, inst);
644 break;
645 case OPCODE_IF:
646 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
647 /* Use ordinary register */
648 _mesa_fprintf(f, "IF ");
649 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
650 _mesa_fprintf(f, "; ");
651 }
652 else {
653 /* Use cond codes */
654 _mesa_fprintf(f, "IF (%s%s);",
655 _mesa_condcode_string(inst->DstReg.CondMask),
656 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
657 0, GL_FALSE));
658 }
659 _mesa_fprintf(f, " # (if false, goto %d)", inst->BranchTarget);
660 fprint_comment(f, inst);
661 return indent + 3;
662 case OPCODE_ELSE:
663 _mesa_fprintf(f, "ELSE; # (goto %d)\n", inst->BranchTarget);
664 return indent + 3;
665 case OPCODE_ENDIF:
666 _mesa_fprintf(f, "ENDIF;\n");
667 break;
668 case OPCODE_BGNLOOP:
669 _mesa_fprintf(f, "BGNLOOP; # (end at %d)\n", inst->BranchTarget);
670 return indent + 3;
671 case OPCODE_ENDLOOP:
672 _mesa_fprintf(f, "ENDLOOP; # (goto %d)\n", inst->BranchTarget);
673 break;
674 case OPCODE_BRK:
675 case OPCODE_CONT:
676 _mesa_fprintf(f, "%s (%s%s); # (goto %d)",
677 _mesa_opcode_string(inst->Opcode),
678 _mesa_condcode_string(inst->DstReg.CondMask),
679 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
680 inst->BranchTarget);
681 fprint_comment(f, inst);
682 break;
683
684 case OPCODE_BGNSUB:
685 if (mode == PROG_PRINT_NV) {
686 _mesa_fprintf(f, "%s:\n", inst->Comment); /* comment is label */
687 return indent;
688 }
689 else {
690 _mesa_fprintf(f, "BGNSUB");
691 fprint_comment(f, inst);
692 return indent + 3;
693 }
694 case OPCODE_ENDSUB:
695 if (mode == PROG_PRINT_DEBUG) {
696 _mesa_fprintf(f, "ENDSUB");
697 fprint_comment(f, inst);
698 }
699 break;
700 case OPCODE_CAL:
701 if (mode == PROG_PRINT_NV) {
702 _mesa_fprintf(f, "CAL %s; # (goto %d)\n", inst->Comment, inst->BranchTarget);
703 }
704 else {
705 _mesa_fprintf(f, "CAL %u", inst->BranchTarget);
706 fprint_comment(f, inst);
707 }
708 break;
709 case OPCODE_RET:
710 _mesa_fprintf(f, "RET (%s%s)",
711 _mesa_condcode_string(inst->DstReg.CondMask),
712 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
713 fprint_comment(f, inst);
714 break;
715
716 case OPCODE_END:
717 _mesa_fprintf(f, "END\n");
718 break;
719 case OPCODE_NOP:
720 if (mode == PROG_PRINT_DEBUG) {
721 _mesa_fprintf(f, "NOP");
722 fprint_comment(f, inst);
723 }
724 else if (inst->Comment) {
725 /* ARB/NV extensions don't have NOP instruction */
726 _mesa_fprintf(f, "# %s\n", inst->Comment);
727 }
728 break;
729 /* XXX may need other special-case instructions */
730 default:
731 if (inst->Opcode < MAX_OPCODE) {
732 /* typical alu instruction */
733 fprint_alu_instruction(f, inst,
734 _mesa_opcode_string(inst->Opcode),
735 _mesa_num_inst_src_regs(inst->Opcode),
736 mode, prog);
737 }
738 else {
739 _mesa_fprintf(f, "Other opcode %d\n", inst->Opcode);
740 }
741 break;
742 }
743 return indent;
744 }
745
746
747 GLint
748 _mesa_print_instruction_opt(const struct prog_instruction *inst,
749 GLint indent,
750 gl_prog_print_mode mode,
751 const struct gl_program *prog)
752 {
753 return _mesa_fprint_instruction_opt(stdout, inst, indent, mode, prog);
754 }
755
756
757 void
758 _mesa_print_instruction(const struct prog_instruction *inst)
759 {
760 /* note: 4th param should be ignored for PROG_PRINT_DEBUG */
761 _mesa_fprint_instruction_opt(stdout, inst, 0, PROG_PRINT_DEBUG, NULL);
762 }
763
764
765
766 /**
767 * Print program, with options.
768 */
769 void
770 _mesa_fprint_program_opt(FILE *f,
771 const struct gl_program *prog,
772 gl_prog_print_mode mode,
773 GLboolean lineNumbers)
774 {
775 GLuint i, indent = 0;
776
777 switch (prog->Target) {
778 case GL_VERTEX_PROGRAM_ARB:
779 if (mode == PROG_PRINT_ARB)
780 _mesa_fprintf(f, "!!ARBvp1.0\n");
781 else if (mode == PROG_PRINT_NV)
782 _mesa_fprintf(f, "!!VP1.0\n");
783 else
784 _mesa_fprintf(f, "# Vertex Program/Shader\n");
785 break;
786 case GL_FRAGMENT_PROGRAM_ARB:
787 case GL_FRAGMENT_PROGRAM_NV:
788 if (mode == PROG_PRINT_ARB)
789 _mesa_fprintf(f, "!!ARBfp1.0\n");
790 else if (mode == PROG_PRINT_NV)
791 _mesa_fprintf(f, "!!FP1.0\n");
792 else
793 _mesa_fprintf(f, "# Fragment Program/Shader\n");
794 break;
795 }
796
797 for (i = 0; i < prog->NumInstructions; i++) {
798 if (lineNumbers)
799 _mesa_fprintf(f, "%3d: ", i);
800 indent = _mesa_fprint_instruction_opt(f, prog->Instructions + i,
801 indent, mode, prog);
802 }
803 }
804
805
806 /**
807 * Print program to stdout, default options.
808 */
809 void
810 _mesa_print_program(const struct gl_program *prog)
811 {
812 _mesa_fprint_program_opt(stdout, prog, PROG_PRINT_DEBUG, GL_TRUE);
813 }
814
815
816 /**
817 * Print all of a program's parameters/fields to given file.
818 */
819 static void
820 _mesa_fprint_program_parameters(FILE *f,
821 GLcontext *ctx,
822 const struct gl_program *prog)
823 {
824 GLuint i;
825
826 _mesa_fprintf(f, "InputsRead: 0x%x\n", prog->InputsRead);
827 _mesa_fprintf(f, "OutputsWritten: 0x%x\n", prog->OutputsWritten);
828 _mesa_fprintf(f, "NumInstructions=%d\n", prog->NumInstructions);
829 _mesa_fprintf(f, "NumTemporaries=%d\n", prog->NumTemporaries);
830 _mesa_fprintf(f, "NumParameters=%d\n", prog->NumParameters);
831 _mesa_fprintf(f, "NumAttributes=%d\n", prog->NumAttributes);
832 _mesa_fprintf(f, "NumAddressRegs=%d\n", prog->NumAddressRegs);
833 _mesa_fprintf(f, "Samplers=[ ");
834 for (i = 0; i < MAX_SAMPLERS; i++) {
835 _mesa_fprintf(f, "%d ", prog->SamplerUnits[i]);
836 }
837 _mesa_fprintf(f, "]\n");
838
839 _mesa_load_state_parameters(ctx, prog->Parameters);
840
841 #if 0
842 _mesa_fprintf(f, "Local Params:\n");
843 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
844 const GLfloat *p = prog->LocalParams[i];
845 _mesa_fprintf(f, "%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
846 }
847 #endif
848 _mesa_print_parameter_list(prog->Parameters);
849 }
850
851
852 /**
853 * Print all of a program's parameters/fields to stdout.
854 */
855 void
856 _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
857 {
858 _mesa_fprint_program_parameters(stdout, ctx, prog);
859 }
860
861
862 /**
863 * Print a program parameter list to given file.
864 */
865 static void
866 _mesa_fprint_parameter_list(FILE *f,
867 const struct gl_program_parameter_list *list)
868 {
869 const gl_prog_print_mode mode = PROG_PRINT_DEBUG;
870 GLuint i;
871
872 if (!list)
873 return;
874
875 _mesa_fprintf(f, "param list %p\n", (void *) list);
876 _mesa_fprintf(f, "dirty state flags: 0x%x\n", list->StateFlags);
877 for (i = 0; i < list->NumParameters; i++){
878 struct gl_program_parameter *param = list->Parameters + i;
879 const GLfloat *v = list->ParameterValues[i];
880 _mesa_fprintf(f, "param[%d] sz=%d %s %s = {%.3g, %.3g, %.3g, %.3g}",
881 i, param->Size,
882 file_string(list->Parameters[i].Type, mode),
883 param->Name, v[0], v[1], v[2], v[3]);
884 if (param->Flags & PROG_PARAM_BIT_CENTROID)
885 _mesa_fprintf(f, " Centroid");
886 if (param->Flags & PROG_PARAM_BIT_INVARIANT)
887 _mesa_fprintf(f, " Invariant");
888 if (param->Flags & PROG_PARAM_BIT_FLAT)
889 _mesa_fprintf(f, " Flat");
890 if (param->Flags & PROG_PARAM_BIT_LINEAR)
891 _mesa_fprintf(f, " Linear");
892 _mesa_fprintf(f, "\n");
893 }
894 }
895
896
897 /**
898 * Print a program parameter list to stdout.
899 */
900 void
901 _mesa_print_parameter_list(const struct gl_program_parameter_list *list)
902 {
903 _mesa_fprint_parameter_list(stdout, list);
904 }
905
906
907 /**
908 * Write shader and associated info to a file.
909 */
910 void
911 _mesa_write_shader_to_file(const struct gl_shader *shader)
912 {
913 const char *type;
914 char filename[100];
915 FILE *f;
916
917 if (shader->Type == GL_FRAGMENT_SHADER)
918 type = "frag";
919 else
920 type = "vert";
921
922 _mesa_snprintf(filename, sizeof(filename), "shader_%u.%s", shader->Name, type);
923 f = fopen(filename, "w");
924 if (!f) {
925 fprintf(stderr, "Unable to open %s for writing\n", filename);
926 return;
927 }
928
929 fprintf(f, "/* Shader %u source */\n", shader->Name);
930 fputs(shader->Source, f);
931 fprintf(f, "\n");
932
933 fprintf(f, "/* Compile status: %s */\n",
934 shader->CompileStatus ? "ok" : "fail");
935 if (!shader->CompileStatus) {
936 fprintf(f, "/* Log Info: */\n");
937 fputs(shader->InfoLog, f);
938 }
939 else {
940 fprintf(f, "/* GPU code */\n");
941 fprintf(f, "/*\n");
942 _mesa_fprint_program_opt(f, shader->Program, PROG_PRINT_DEBUG, GL_TRUE);
943 fprintf(f, "*/\n");
944 }
945
946 fclose(f);
947 }
948
949