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