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