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