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