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