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