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