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