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