[MESA]
[reactos.git] / reactos / dll / opengl / mesa / 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 = _mesa_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 = _mesa_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_1D_ARRAY_INDEX: fprintf(f, "1D_ARRAY"); break;
716 case TEXTURE_2D_ARRAY_INDEX: fprintf(f, "2D_ARRAY"); break;
717 default:
718 ;
719 }
720 if (inst->TexShadow)
721 fprintf(f, " SHADOW");
722 fprint_comment(f, inst);
723 break;
724
725 case OPCODE_KIL:
726 fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
727 fprintf(f, " ");
728 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
729 fprint_comment(f, inst);
730 break;
731 case OPCODE_KIL_NV:
732 fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
733 fprintf(f, " ");
734 fprintf(f, "%s.%s",
735 _mesa_condcode_string(inst->DstReg.CondMask),
736 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
737 GL_FALSE, GL_FALSE));
738 fprint_comment(f, inst);
739 break;
740
741 case OPCODE_ARL:
742 fprintf(f, "ARL ");
743 fprint_dst_reg(f, &inst->DstReg, mode, prog);
744 fprintf(f, ", ");
745 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
746 fprint_comment(f, inst);
747 break;
748 case OPCODE_BRA:
749 fprintf(f, "BRA %d (%s%s)",
750 inst->BranchTarget,
751 _mesa_condcode_string(inst->DstReg.CondMask),
752 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
753 fprint_comment(f, inst);
754 break;
755 case OPCODE_IF:
756 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
757 /* Use ordinary register */
758 fprintf(f, "IF ");
759 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
760 fprintf(f, "; ");
761 }
762 else {
763 /* Use cond codes */
764 fprintf(f, "IF (%s%s);",
765 _mesa_condcode_string(inst->DstReg.CondMask),
766 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
767 0, GL_FALSE));
768 }
769 fprintf(f, " # (if false, goto %d)", inst->BranchTarget);
770 fprint_comment(f, inst);
771 return indent + 3;
772 case OPCODE_ELSE:
773 fprintf(f, "ELSE; # (goto %d)\n", inst->BranchTarget);
774 return indent + 3;
775 case OPCODE_ENDIF:
776 fprintf(f, "ENDIF;\n");
777 break;
778 case OPCODE_BGNLOOP:
779 fprintf(f, "BGNLOOP; # (end at %d)\n", inst->BranchTarget);
780 return indent + 3;
781 case OPCODE_ENDLOOP:
782 fprintf(f, "ENDLOOP; # (goto %d)\n", inst->BranchTarget);
783 break;
784 case OPCODE_BRK:
785 case OPCODE_CONT:
786 fprintf(f, "%s (%s%s); # (goto %d)",
787 _mesa_opcode_string(inst->Opcode),
788 _mesa_condcode_string(inst->DstReg.CondMask),
789 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
790 inst->BranchTarget);
791 fprint_comment(f, inst);
792 break;
793
794 case OPCODE_BGNSUB:
795 if (mode == PROG_PRINT_NV) {
796 fprintf(f, "%s:\n", inst->Comment); /* comment is label */
797 return indent;
798 }
799 else {
800 fprintf(f, "BGNSUB");
801 fprint_comment(f, inst);
802 return indent + 3;
803 }
804 case OPCODE_ENDSUB:
805 if (mode == PROG_PRINT_DEBUG) {
806 fprintf(f, "ENDSUB");
807 fprint_comment(f, inst);
808 }
809 break;
810 case OPCODE_CAL:
811 if (mode == PROG_PRINT_NV) {
812 fprintf(f, "CAL %s; # (goto %d)\n", inst->Comment, inst->BranchTarget);
813 }
814 else {
815 fprintf(f, "CAL %u", inst->BranchTarget);
816 fprint_comment(f, inst);
817 }
818 break;
819 case OPCODE_RET:
820 fprintf(f, "RET (%s%s)",
821 _mesa_condcode_string(inst->DstReg.CondMask),
822 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
823 fprint_comment(f, inst);
824 break;
825
826 case OPCODE_END:
827 fprintf(f, "END\n");
828 break;
829 case OPCODE_NOP:
830 if (mode == PROG_PRINT_DEBUG) {
831 fprintf(f, "NOP");
832 fprint_comment(f, inst);
833 }
834 else if (inst->Comment) {
835 /* ARB/NV extensions don't have NOP instruction */
836 fprintf(f, "# %s\n", inst->Comment);
837 }
838 break;
839 case OPCODE_EMIT_VERTEX:
840 fprintf(f, "EMIT_VERTEX\n");
841 break;
842 case OPCODE_END_PRIMITIVE:
843 fprintf(f, "END_PRIMITIVE\n");
844 break;
845 /* XXX may need other special-case instructions */
846 default:
847 if (inst->Opcode < MAX_OPCODE) {
848 /* typical alu instruction */
849 _mesa_fprint_alu_instruction(f, inst,
850 _mesa_opcode_string(inst->Opcode),
851 _mesa_num_inst_src_regs(inst->Opcode),
852 mode, prog);
853 }
854 else {
855 _mesa_fprint_alu_instruction(f, inst,
856 _mesa_opcode_string(inst->Opcode),
857 3/*_mesa_num_inst_src_regs(inst->Opcode)*/,
858 mode, prog);
859 }
860 break;
861 }
862 return indent;
863 }
864
865
866 GLint
867 _mesa_print_instruction_opt(const struct prog_instruction *inst,
868 GLint indent,
869 gl_prog_print_mode mode,
870 const struct gl_program *prog)
871 {
872 return _mesa_fprint_instruction_opt(stderr, inst, indent, mode, prog);
873 }
874
875
876 void
877 _mesa_print_instruction(const struct prog_instruction *inst)
878 {
879 /* note: 4th param should be ignored for PROG_PRINT_DEBUG */
880 _mesa_fprint_instruction_opt(stderr, inst, 0, PROG_PRINT_DEBUG, NULL);
881 }
882
883
884
885 /**
886 * Print program, with options.
887 */
888 void
889 _mesa_fprint_program_opt(FILE *f,
890 const struct gl_program *prog,
891 gl_prog_print_mode mode,
892 GLboolean lineNumbers)
893 {
894 GLuint i, indent = 0;
895
896 switch (prog->Target) {
897 case GL_VERTEX_PROGRAM_ARB:
898 if (mode == PROG_PRINT_ARB)
899 fprintf(f, "!!ARBvp1.0\n");
900 else if (mode == PROG_PRINT_NV)
901 fprintf(f, "!!VP1.0\n");
902 else
903 fprintf(f, "# Vertex Program/Shader %u\n", prog->Id);
904 break;
905 case GL_FRAGMENT_PROGRAM_ARB:
906 case GL_FRAGMENT_PROGRAM_NV:
907 if (mode == PROG_PRINT_ARB)
908 fprintf(f, "!!ARBfp1.0\n");
909 else if (mode == PROG_PRINT_NV)
910 fprintf(f, "!!FP1.0\n");
911 else
912 fprintf(f, "# Fragment Program/Shader %u\n", prog->Id);
913 break;
914 }
915
916 for (i = 0; i < prog->NumInstructions; i++) {
917 if (lineNumbers)
918 fprintf(f, "%3d: ", i);
919 indent = _mesa_fprint_instruction_opt(f, prog->Instructions + i,
920 indent, mode, prog);
921 }
922 }
923
924
925 /**
926 * Print program to stderr, default options.
927 */
928 void
929 _mesa_print_program(const struct gl_program *prog)
930 {
931 _mesa_fprint_program_opt(stderr, prog, PROG_PRINT_DEBUG, GL_TRUE);
932 }
933
934
935 /**
936 * Return binary representation of 64-bit value (as a string).
937 * Insert a comma to separate each group of 8 bits.
938 * Note we return a pointer to local static storage so this is not
939 * re-entrant, etc.
940 * XXX move to imports.[ch] if useful elsewhere.
941 */
942 static const char *
943 binary(GLbitfield64 val)
944 {
945 static char buf[80];
946 GLint i, len = 0;
947 for (i = 63; i >= 0; --i) {
948 if (val & (BITFIELD64_BIT(i)))
949 buf[len++] = '1';
950 else if (len > 0 || i == 0)
951 buf[len++] = '0';
952 if (len > 0 && ((i-1) % 8) == 7)
953 buf[len++] = ',';
954 }
955 buf[len] = '\0';
956 return buf;
957 }
958
959
960 /**
961 * Print all of a program's parameters/fields to given file.
962 */
963 static void
964 _mesa_fprint_program_parameters(FILE *f,
965 struct gl_context *ctx,
966 const struct gl_program *prog)
967 {
968 GLuint i;
969
970 fprintf(f, "InputsRead: 0x%llx (0b%s)\n",
971 (unsigned long long) prog->InputsRead, binary(prog->InputsRead));
972 fprintf(f, "OutputsWritten: 0x%llx (0b%s)\n",
973 (unsigned long long)prog->OutputsWritten,
974 binary(prog->OutputsWritten));
975 fprintf(f, "NumInstructions=%d\n", prog->NumInstructions);
976 fprintf(f, "NumTemporaries=%d\n", prog->NumTemporaries);
977 fprintf(f, "NumParameters=%d\n", prog->NumParameters);
978 fprintf(f, "NumAttributes=%d\n", prog->NumAttributes);
979 fprintf(f, "NumAddressRegs=%d\n", prog->NumAddressRegs);
980 fprintf(f, "IndirectRegisterFiles: 0x%x (0b%s)\n",
981 prog->IndirectRegisterFiles, binary(prog->IndirectRegisterFiles));
982 fprintf(f, "SamplersUsed: 0x%x (0b%s)\n",
983 prog->SamplersUsed, binary(prog->SamplersUsed));
984 fprintf(f, "Samplers=[ ");
985 for (i = 0; i < MAX_SAMPLERS; i++) {
986 fprintf(f, "%d ", prog->SamplerUnits[i]);
987 }
988 fprintf(f, "]\n");
989
990 _mesa_load_state_parameters(ctx, prog->Parameters);
991
992 #if 0
993 fprintf(f, "Local Params:\n");
994 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
995 const GLfloat *p = prog->LocalParams[i];
996 fprintf(f, "%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
997 }
998 #endif
999 _mesa_print_parameter_list(prog->Parameters);
1000 }
1001
1002
1003 /**
1004 * Print all of a program's parameters/fields to stderr.
1005 */
1006 void
1007 _mesa_print_program_parameters(struct gl_context *ctx, const struct gl_program *prog)
1008 {
1009 _mesa_fprint_program_parameters(stderr, ctx, prog);
1010 }
1011
1012
1013 /**
1014 * Print a program parameter list to given file.
1015 */
1016 static void
1017 _mesa_fprint_parameter_list(FILE *f,
1018 const struct gl_program_parameter_list *list)
1019 {
1020 GLuint i;
1021
1022 if (!list)
1023 return;
1024
1025 if (0)
1026 fprintf(f, "param list %p\n", (void *) list);
1027 fprintf(f, "dirty state flags: 0x%x\n", list->StateFlags);
1028 for (i = 0; i < list->NumParameters; i++){
1029 struct gl_program_parameter *param = list->Parameters + i;
1030 const GLfloat *v = (GLfloat *) list->ParameterValues[i];
1031 fprintf(f, "param[%d] sz=%d %s %s = {%.3g, %.3g, %.3g, %.3g}",
1032 i, param->Size,
1033 _mesa_register_file_name(list->Parameters[i].Type),
1034 param->Name, v[0], v[1], v[2], v[3]);
1035 if (param->Flags & PROG_PARAM_BIT_CENTROID)
1036 fprintf(f, " Centroid");
1037 if (param->Flags & PROG_PARAM_BIT_INVARIANT)
1038 fprintf(f, " Invariant");
1039 if (param->Flags & PROG_PARAM_BIT_FLAT)
1040 fprintf(f, " Flat");
1041 if (param->Flags & PROG_PARAM_BIT_LINEAR)
1042 fprintf(f, " Linear");
1043 fprintf(f, "\n");
1044 }
1045 }
1046
1047
1048 /**
1049 * Print a program parameter list to stderr.
1050 */
1051 void
1052 _mesa_print_parameter_list(const struct gl_program_parameter_list *list)
1053 {
1054 _mesa_fprint_parameter_list(stderr, list);
1055 }
1056
1057
1058 /**
1059 * Write shader and associated info to a file.
1060 */
1061 void
1062 _mesa_write_shader_to_file(const struct gl_shader *shader)
1063 {
1064 const char *type;
1065 char filename[100];
1066 FILE *f;
1067
1068 if (shader->Type == GL_FRAGMENT_SHADER)
1069 type = "frag";
1070 else if (shader->Type == GL_VERTEX_SHADER)
1071 type = "vert";
1072
1073 _mesa_snprintf(filename, sizeof(filename), "shader_%u.%s", shader->Name, type);
1074 f = fopen(filename, "w");
1075 if (!f) {
1076 fprintf(stderr, "Unable to open %s for writing\n", filename);
1077 return;
1078 }
1079
1080 fprintf(f, "/* Shader %u source, checksum %u */\n", shader->Name, shader->SourceChecksum);
1081 fputs(shader->Source, f);
1082 fprintf(f, "\n");
1083
1084 fprintf(f, "/* Compile status: %s */\n",
1085 shader->CompileStatus ? "ok" : "fail");
1086 fprintf(f, "/* Log Info: */\n");
1087 if (shader->InfoLog) {
1088 fputs(shader->InfoLog, f);
1089 }
1090 if (shader->CompileStatus && shader->Program) {
1091 fprintf(f, "/* GPU code */\n");
1092 fprintf(f, "/*\n");
1093 _mesa_fprint_program_opt(f, shader->Program, PROG_PRINT_DEBUG, GL_TRUE);
1094 fprintf(f, "*/\n");
1095 fprintf(f, "/* Parameters / constants */\n");
1096 fprintf(f, "/*\n");
1097 _mesa_fprint_parameter_list(f, shader->Program->Parameters);
1098 fprintf(f, "*/\n");
1099 }
1100
1101 fclose(f);
1102 }
1103
1104
1105 /**
1106 * Append the shader's uniform info/values to the shader log file.
1107 * The log file will typically have been created by the
1108 * _mesa_write_shader_to_file function.
1109 */
1110 void
1111 _mesa_append_uniforms_to_file(const struct gl_shader *shader)
1112 {
1113 const struct gl_program *const prog = shader->Program;
1114 const char *type;
1115 char filename[100];
1116 FILE *f;
1117
1118 if (shader->Type == GL_FRAGMENT_SHADER)
1119 type = "frag";
1120 else
1121 type = "vert";
1122
1123 _mesa_snprintf(filename, sizeof(filename), "shader_%u.%s", shader->Name, type);
1124 f = fopen(filename, "a"); /* append */
1125 if (!f) {
1126 fprintf(stderr, "Unable to open %s for appending\n", filename);
1127 return;
1128 }
1129
1130 fprintf(f, "/* First-draw parameters / constants */\n");
1131 fprintf(f, "/*\n");
1132 _mesa_fprint_parameter_list(f, prog->Parameters);
1133 fprintf(f, "*/\n");
1134
1135 fclose(f);
1136 }