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