2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Print vertex/fragment programs - for debugging.
31 #include "main/glheader.h"
32 #include "main/context.h"
33 #include "main/imports.h"
34 #include "prog_instruction.h"
35 #include "prog_parameter.h"
36 #include "prog_print.h"
37 #include "prog_statevars.h"
42 * Return string name for given program/register file.
45 file_string(enum register_file f
, gl_prog_print_mode mode
)
48 case PROGRAM_TEMPORARY
:
50 case PROGRAM_LOCAL_PARAM
:
52 case PROGRAM_ENV_PARAM
:
54 case PROGRAM_STATE_VAR
:
60 case PROGRAM_NAMED_PARAM
:
62 case PROGRAM_CONSTANT
:
68 case PROGRAM_WRITE_ONLY
:
75 return "Unknown program file!";
81 * Return ARB_v/f_prog-style input attrib string.
84 arb_input_attrib_string(GLint index
, GLenum progType
)
86 const char *vertAttribs
[] = {
90 "vertex.color.primary",
91 "vertex.color.secondary",
100 "vertex.texcoord[5]",
101 "vertex.texcoord[6]",
102 "vertex.texcoord[7]",
120 const char *fragAttribs
[] = {
122 "fragment.color.primary",
123 "fragment.color.secondary",
125 "fragment.texcoord[0]",
126 "fragment.texcoord[1]",
127 "fragment.texcoord[2]",
128 "fragment.texcoord[3]",
129 "fragment.texcoord[4]",
130 "fragment.texcoord[5]",
131 "fragment.texcoord[6]",
132 "fragment.texcoord[7]",
133 "fragment.varying[0]",
134 "fragment.varying[1]",
135 "fragment.varying[2]",
136 "fragment.varying[3]",
137 "fragment.varying[4]",
138 "fragment.varying[5]",
139 "fragment.varying[6]",
140 "fragment.varying[7]"
143 if (progType
== GL_VERTEX_PROGRAM_ARB
) {
144 assert(index
< sizeof(vertAttribs
) / sizeof(vertAttribs
[0]));
145 return vertAttribs
[index
];
148 assert(index
< sizeof(fragAttribs
) / sizeof(fragAttribs
[0]));
149 return fragAttribs
[index
];
155 * Return ARB_v/f_prog-style output attrib string.
158 arb_output_attrib_string(GLint index
, GLenum progType
)
160 const char *vertResults
[] = {
162 "result.color.primary",
163 "result.color.secondary",
165 "result.texcoord[0]",
166 "result.texcoord[1]",
167 "result.texcoord[2]",
168 "result.texcoord[3]",
169 "result.texcoord[4]",
170 "result.texcoord[5]",
171 "result.texcoord[6]",
172 "result.texcoord[7]",
182 const char *fragResults
[] = {
187 if (progType
== GL_VERTEX_PROGRAM_ARB
) {
188 assert(index
< sizeof(vertResults
) / sizeof(vertResults
[0]));
189 return vertResults
[index
];
192 assert(index
< sizeof(fragResults
) / sizeof(fragResults
[0]));
193 return fragResults
[index
];
199 * Return string representation of the given register.
200 * Note that some types of registers (like PROGRAM_UNIFORM) aren't defined
201 * by the ARB/NV program languages so we've taken some liberties here.
202 * \param file the register file (PROGRAM_INPUT, PROGRAM_TEMPORARY, etc)
203 * \param index number of the register in the register file
204 * \param mode the output format/mode/style
205 * \param prog pointer to containing program
208 reg_string(enum register_file f
, GLint index
, gl_prog_print_mode mode
,
209 GLboolean relAddr
, const struct gl_program
*prog
)
211 static char str
[100];
216 case PROG_PRINT_DEBUG
:
218 sprintf(str
, "%s[ADDR%s%d]", file_string(f
, mode
), (index
> 0) ? "+" : "", index
);
220 sprintf(str
, "%s[%d]", file_string(f
, mode
), index
);
226 sprintf(str
, "%s", arb_input_attrib_string(index
, prog
->Target
));
229 sprintf(str
, "%s", arb_output_attrib_string(index
, prog
->Target
));
231 case PROGRAM_TEMPORARY
:
232 sprintf(str
, "temp%d", index
);
234 case PROGRAM_ENV_PARAM
:
235 sprintf(str
, "program.env[%d]", index
);
237 case PROGRAM_LOCAL_PARAM
:
238 sprintf(str
, "program.local[%d]", index
);
240 case PROGRAM_VARYING
: /* extension */
241 sprintf(str
, "varying[%d]", index
);
243 case PROGRAM_CONSTANT
: /* extension */
244 sprintf(str
, "constant[%d]", index
);
246 case PROGRAM_UNIFORM
: /* extension */
247 sprintf(str
, "uniform[%d]", index
);
249 case PROGRAM_STATE_VAR
:
251 struct gl_program_parameter
*param
252 = prog
->Parameters
->Parameters
+ index
;
253 char *state
= _mesa_program_state_string(param
->StateIndexes
);
258 case PROGRAM_ADDRESS
:
259 sprintf(str
, "A%d", index
);
262 _mesa_problem(NULL
, "bad file in reg_string()");
269 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
)
270 sprintf(str
, "v[%d]", index
);
272 sprintf(str
, "f[%d]", index
);
275 sprintf(str
, "o[%d]", index
);
277 case PROGRAM_TEMPORARY
:
278 sprintf(str
, "R%d", index
);
280 case PROGRAM_ENV_PARAM
:
281 sprintf(str
, "c[%d]", index
);
283 case PROGRAM_VARYING
: /* extension */
284 sprintf(str
, "varying[%d]", index
);
286 case PROGRAM_UNIFORM
: /* extension */
287 sprintf(str
, "uniform[%d]", index
);
289 case PROGRAM_CONSTANT
: /* extension */
290 sprintf(str
, "constant[%d]", index
);
292 case PROGRAM_STATE_VAR
: /* extension */
293 sprintf(str
, "state[%d]", index
);
296 _mesa_problem(NULL
, "bad file in reg_string()");
301 _mesa_problem(NULL
, "bad mode in reg_string()");
309 * Return a string representation of the given swizzle word.
310 * If extended is true, use extended (comma-separated) format.
311 * \param swizzle the swizzle field
312 * \param negateBase 4-bit negation vector
313 * \param extended if true, also allow 0, 1 values
316 _mesa_swizzle_string(GLuint swizzle
, GLuint negateBase
, GLboolean extended
)
318 static const char swz
[] = "xyzw01!?"; /* See SWIZZLE_x definitions */
322 if (!extended
&& swizzle
== SWIZZLE_NOOP
&& negateBase
== 0)
323 return ""; /* no swizzle/negation */
328 if (negateBase
& NEGATE_X
)
330 s
[i
++] = swz
[GET_SWZ(swizzle
, 0)];
336 if (negateBase
& NEGATE_Y
)
338 s
[i
++] = swz
[GET_SWZ(swizzle
, 1)];
344 if (negateBase
& NEGATE_Z
)
346 s
[i
++] = swz
[GET_SWZ(swizzle
, 2)];
352 if (negateBase
& NEGATE_W
)
354 s
[i
++] = swz
[GET_SWZ(swizzle
, 3)];
362 _mesa_print_swizzle(GLuint swizzle
)
364 if (swizzle
== SWIZZLE_XYZW
) {
365 _mesa_printf(".xyzw\n");
368 const char *s
= _mesa_swizzle_string(swizzle
, 0, 0);
369 _mesa_printf("%s\n", s
);
375 writemask_string(GLuint writeMask
)
380 if (writeMask
== WRITEMASK_XYZW
)
384 if (writeMask
& WRITEMASK_X
)
386 if (writeMask
& WRITEMASK_Y
)
388 if (writeMask
& WRITEMASK_Z
)
390 if (writeMask
& WRITEMASK_W
)
399 _mesa_condcode_string(GLuint condcode
)
402 case COND_GT
: return "GT";
403 case COND_EQ
: return "EQ";
404 case COND_LT
: return "LT";
405 case COND_UN
: return "UN";
406 case COND_GE
: return "GE";
407 case COND_LE
: return "LE";
408 case COND_NE
: return "NE";
409 case COND_TR
: return "TR";
410 case COND_FL
: return "FL";
411 default: return "cond???";
417 print_dst_reg(const struct prog_dst_register
*dstReg
, gl_prog_print_mode mode
,
418 const struct gl_program
*prog
)
421 reg_string((enum register_file
) dstReg
->File
,
422 dstReg
->Index
, mode
, GL_FALSE
, prog
),
423 writemask_string(dstReg
->WriteMask
));
425 if (dstReg
->CondMask
!= COND_TR
) {
426 _mesa_printf(" (%s.%s)",
427 _mesa_condcode_string(dstReg
->CondMask
),
428 _mesa_swizzle_string(dstReg
->CondSwizzle
, GL_FALSE
, GL_FALSE
));
432 _mesa_printf("%s[%d]%s",
433 file_string((enum register_file
) dstReg
->File
, mode
),
435 writemask_string(dstReg
->WriteMask
));
440 print_src_reg(const struct prog_src_register
*srcReg
, gl_prog_print_mode mode
,
441 const struct gl_program
*prog
)
444 reg_string((enum register_file
) srcReg
->File
,
445 srcReg
->Index
, mode
, srcReg
->RelAddr
, prog
),
446 _mesa_swizzle_string(srcReg
->Swizzle
,
447 srcReg
->NegateBase
, GL_FALSE
));
449 _mesa_printf("%s[%d]%s",
450 file_string((enum register_file
) srcReg
->File
, mode
),
452 _mesa_swizzle_string(srcReg
->Swizzle
,
453 srcReg
->NegateBase
, GL_FALSE
));
458 print_comment(const struct prog_instruction
*inst
)
461 _mesa_printf("; # %s\n", inst
->Comment
);
468 print_alu_instruction(const struct prog_instruction
*inst
,
469 const char *opcode_string
, GLuint numRegs
,
470 gl_prog_print_mode mode
,
471 const struct gl_program
*prog
)
475 _mesa_printf("%s", opcode_string
);
476 if (inst
->CondUpdate
)
480 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
481 _mesa_printf("_SAT");
484 if (inst
->DstReg
.File
!= PROGRAM_UNDEFINED
) {
485 print_dst_reg(&inst
->DstReg
, mode
, prog
);
488 _mesa_printf(" ???");
494 for (j
= 0; j
< numRegs
; j
++) {
495 print_src_reg(inst
->SrcReg
+ j
, mode
, prog
);
505 _mesa_print_alu_instruction(const struct prog_instruction
*inst
,
506 const char *opcode_string
, GLuint numRegs
)
508 print_alu_instruction(inst
, opcode_string
, numRegs
, PROG_PRINT_DEBUG
, NULL
);
513 _mesa_print_instruction(const struct prog_instruction
*inst
)
515 /* note: 4th param should be ignored for PROG_PRINT_DEBUG */
516 _mesa_print_instruction_opt(inst
, 0, PROG_PRINT_DEBUG
, NULL
);
521 * Print a single vertex/fragment program instruction.
524 _mesa_print_instruction_opt(const struct prog_instruction
*inst
, GLint indent
,
525 gl_prog_print_mode mode
,
526 const struct gl_program
*prog
)
530 if (inst
->Opcode
== OPCODE_ELSE
||
531 inst
->Opcode
== OPCODE_ENDIF
||
532 inst
->Opcode
== OPCODE_ENDLOOP
||
533 inst
->Opcode
== OPCODE_ENDSUB
) {
536 for (i
= 0; i
< indent
; i
++) {
540 switch (inst
->Opcode
) {
542 _mesa_printf("PRINT '%s'", inst
->Data
);
543 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
545 _mesa_printf("%s[%d]%s",
546 file_string((enum register_file
) inst
->SrcReg
[0].File
,
548 inst
->SrcReg
[0].Index
,
549 _mesa_swizzle_string(inst
->SrcReg
[0].Swizzle
,
550 inst
->SrcReg
[0].NegateBase
, GL_FALSE
));
553 _mesa_printf(" # %s", inst
->Comment
);
558 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
559 _mesa_printf("_SAT");
561 print_dst_reg(&inst
->DstReg
, mode
, prog
);
562 _mesa_printf(", %s[%d], %s",
563 file_string((enum register_file
) inst
->SrcReg
[0].File
,
565 inst
->SrcReg
[0].Index
,
566 _mesa_swizzle_string(inst
->SrcReg
[0].Swizzle
,
567 inst
->SrcReg
[0].NegateBase
, GL_TRUE
));
574 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
575 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
576 _mesa_printf("_SAT");
578 print_dst_reg(&inst
->DstReg
, mode
, prog
);
580 print_src_reg(&inst
->SrcReg
[0], mode
, prog
);
581 _mesa_printf(", texture[%d], ", inst
->TexSrcUnit
);
582 switch (inst
->TexSrcTarget
) {
583 case TEXTURE_1D_INDEX
: _mesa_printf("1D"); break;
584 case TEXTURE_2D_INDEX
: _mesa_printf("2D"); break;
585 case TEXTURE_3D_INDEX
: _mesa_printf("3D"); break;
586 case TEXTURE_CUBE_INDEX
: _mesa_printf("CUBE"); break;
587 case TEXTURE_RECT_INDEX
: _mesa_printf("RECT"); break;
595 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
597 print_src_reg(&inst
->SrcReg
[0], mode
, prog
);
601 _mesa_printf("%s", _mesa_opcode_string(inst
->Opcode
));
603 _mesa_printf("%s.%s",
604 _mesa_condcode_string(inst
->DstReg
.CondMask
),
605 _mesa_swizzle_string(inst
->DstReg
.CondSwizzle
,
606 GL_FALSE
, GL_FALSE
));
611 _mesa_printf("ARL ");
612 print_dst_reg(&inst
->DstReg
, mode
, prog
);
614 print_src_reg(&inst
->SrcReg
[0], mode
, prog
);
618 _mesa_printf("BRA %d (%s%s)",
620 _mesa_condcode_string(inst
->DstReg
.CondMask
),
621 _mesa_swizzle_string(inst
->DstReg
.CondSwizzle
, 0, GL_FALSE
));
625 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
626 /* Use ordinary register */
628 print_src_reg(&inst
->SrcReg
[0], mode
, prog
);
633 _mesa_printf("IF (%s%s);",
634 _mesa_condcode_string(inst
->DstReg
.CondMask
),
635 _mesa_swizzle_string(inst
->DstReg
.CondSwizzle
,
638 _mesa_printf(" # (if false, goto %d)", inst
->BranchTarget
);
642 _mesa_printf("ELSE; # (goto %d)\n", inst
->BranchTarget
);
645 _mesa_printf("ENDIF;\n");
648 _mesa_printf("BGNLOOP; # (end at %d)\n", inst
->BranchTarget
);
651 _mesa_printf("ENDLOOP; # (goto %d)\n", inst
->BranchTarget
);
655 _mesa_printf("%s (%s%s); # (goto %d)",
656 _mesa_opcode_string(inst
->Opcode
),
657 _mesa_condcode_string(inst
->DstReg
.CondMask
),
658 _mesa_swizzle_string(inst
->DstReg
.CondSwizzle
, 0, GL_FALSE
),
664 if (mode
== PROG_PRINT_NV
) {
665 _mesa_printf("%s:\n", inst
->Comment
); /* comment is label */
669 _mesa_printf("BGNSUB");
674 if (mode
== PROG_PRINT_DEBUG
) {
675 _mesa_printf("ENDSUB");
680 if (mode
== PROG_PRINT_NV
) {
681 _mesa_printf("CAL %s; # (goto %d)\n", inst
->Comment
, inst
->BranchTarget
);
684 _mesa_printf("CAL %u", inst
->BranchTarget
);
689 _mesa_printf("RET (%s%s)",
690 _mesa_condcode_string(inst
->DstReg
.CondMask
),
691 _mesa_swizzle_string(inst
->DstReg
.CondSwizzle
, 0, GL_FALSE
));
696 _mesa_printf("END\n");
699 if (mode
== PROG_PRINT_DEBUG
) {
703 else if (inst
->Comment
) {
704 /* ARB/NV extensions don't have NOP instruction */
705 _mesa_printf("# %s\n", inst
->Comment
);
708 /* XXX may need other special-case instructions */
710 /* typical alu instruction */
711 print_alu_instruction(inst
,
712 _mesa_opcode_string(inst
->Opcode
),
713 _mesa_num_inst_src_regs(inst
->Opcode
),
722 * Print program to stdout, default options.
725 _mesa_print_program(const struct gl_program
*prog
)
727 _mesa_print_program_opt(prog
, PROG_PRINT_DEBUG
, GL_TRUE
);
732 * Print program, with options.
735 _mesa_print_program_opt(const struct gl_program
*prog
,
736 gl_prog_print_mode mode
,
737 GLboolean lineNumbers
)
739 GLuint i
, indent
= 0;
741 switch (prog
->Target
) {
742 case GL_VERTEX_PROGRAM_ARB
:
743 if (mode
== PROG_PRINT_ARB
)
744 _mesa_printf("!!ARBvp1.0\n");
745 else if (mode
== PROG_PRINT_NV
)
746 _mesa_printf("!!VP1.0\n");
748 _mesa_printf("# Vertex Program/Shader\n");
750 case GL_FRAGMENT_PROGRAM_ARB
:
751 case GL_FRAGMENT_PROGRAM_NV
:
752 if (mode
== PROG_PRINT_ARB
)
753 _mesa_printf("!!ARBfp1.0\n");
754 else if (mode
== PROG_PRINT_NV
)
755 _mesa_printf("!!FP1.0\n");
757 _mesa_printf("# Fragment Program/Shader\n");
761 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
763 _mesa_printf("%3d: ", i
);
764 indent
= _mesa_print_instruction_opt(prog
->Instructions
+ i
,
771 * Print all of a program's parameters.
774 _mesa_print_program_parameters(GLcontext
*ctx
, const struct gl_program
*prog
)
778 _mesa_printf("InputsRead: 0x%x\n", prog
->InputsRead
);
779 _mesa_printf("OutputsWritten: 0x%x\n", prog
->OutputsWritten
);
780 _mesa_printf("NumInstructions=%d\n", prog
->NumInstructions
);
781 _mesa_printf("NumTemporaries=%d\n", prog
->NumTemporaries
);
782 _mesa_printf("NumParameters=%d\n", prog
->NumParameters
);
783 _mesa_printf("NumAttributes=%d\n", prog
->NumAttributes
);
784 _mesa_printf("NumAddressRegs=%d\n", prog
->NumAddressRegs
);
785 _mesa_printf("Samplers=[ ");
786 for (i
= 0; i
< MAX_SAMPLERS
; i
++) {
787 _mesa_printf("%d ", prog
->SamplerUnits
[i
]);
791 _mesa_load_state_parameters(ctx
, prog
->Parameters
);
794 _mesa_printf("Local Params:\n");
795 for (i
= 0; i
< MAX_PROGRAM_LOCAL_PARAMS
; i
++){
796 const GLfloat
*p
= prog
->LocalParams
[i
];
797 _mesa_printf("%2d: %f, %f, %f, %f\n", i
, p
[0], p
[1], p
[2], p
[3]);
800 _mesa_print_parameter_list(prog
->Parameters
);
805 _mesa_print_parameter_list(const struct gl_program_parameter_list
*list
)
807 const gl_prog_print_mode mode
= PROG_PRINT_DEBUG
;
813 _mesa_printf("param list %p\n", (void *) list
);
814 for (i
= 0; i
< list
->NumParameters
; i
++){
815 struct gl_program_parameter
*param
= list
->Parameters
+ i
;
816 const GLfloat
*v
= list
->ParameterValues
[i
];
817 _mesa_printf("param[%d] sz=%d %s %s = {%.3g, %.3g, %.3g, %.3g};\n",
819 file_string(list
->Parameters
[i
].Type
, mode
),
820 param
->Name
, v
[0], v
[1], v
[2], v
[3]);