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