Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
| 3 | * Version: 6.5.3 |
| 4 | * |
| 5 | * Copyright (C) 1999-2006 Brian Paul All Rights Reserved. |
| 6 | * |
| 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: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included |
| 15 | * in all copies or substantial portions of the Software. |
| 16 | * |
| 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. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * \file prog_print.c |
| 27 | * Print vertex/fragment programs - for debugging. |
| 28 | * \author Brian Paul |
| 29 | */ |
| 30 | |
| 31 | #include "glheader.h" |
| 32 | #include "context.h" |
| 33 | #include "imports.h" |
| 34 | #include "macros.h" |
| 35 | #include "program.h" |
| 36 | #include "prog_instruction.h" |
| 37 | #include "prog_parameter.h" |
| 38 | #include "prog_print.h" |
| 39 | #include "prog_statevars.h" |
| 40 | |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Basic info about each instruction |
| 45 | */ |
| 46 | struct instruction_info |
| 47 | { |
| 48 | gl_inst_opcode Opcode; |
| 49 | const char *Name; |
| 50 | GLuint NumSrcRegs; |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * Instruction info |
| 55 | * \note Opcode should equal array index! |
| 56 | */ |
| 57 | static const struct instruction_info InstInfo[MAX_OPCODE] = { |
| 58 | { OPCODE_NOP, "NOP", 0 }, |
| 59 | { OPCODE_ABS, "ABS", 1 }, |
| 60 | { OPCODE_ADD, "ADD", 2 }, |
| 61 | { OPCODE_ARA, "ARA", 1 }, |
| 62 | { OPCODE_ARL, "ARL", 1 }, |
| 63 | { OPCODE_ARL_NV, "ARL", 1 }, |
| 64 | { OPCODE_ARR, "ARL", 1 }, |
| 65 | { OPCODE_BRA, "BRA", 0 }, |
| 66 | { OPCODE_CAL, "CAL", 0 }, |
| 67 | { OPCODE_CMP, "CMP", 3 }, |
| 68 | { OPCODE_COS, "COS", 1 }, |
| 69 | { OPCODE_DDX, "DDX", 1 }, |
| 70 | { OPCODE_DDY, "DDY", 1 }, |
| 71 | { OPCODE_DP3, "DP3", 2 }, |
| 72 | { OPCODE_DP4, "DP4", 2 }, |
| 73 | { OPCODE_DPH, "DPH", 2 }, |
| 74 | { OPCODE_DST, "DST", 2 }, |
| 75 | { OPCODE_END, "END", 0 }, |
| 76 | { OPCODE_EX2, "EX2", 1 }, |
| 77 | { OPCODE_EXP, "EXP", 1 }, |
| 78 | { OPCODE_FLR, "FLR", 1 }, |
| 79 | { OPCODE_FRC, "FRC", 1 }, |
| 80 | { OPCODE_KIL, "KIL", 1 }, |
| 81 | { OPCODE_KIL_NV, "KIL", 0 }, |
| 82 | { OPCODE_LG2, "LG2", 1 }, |
| 83 | { OPCODE_LIT, "LIT", 1 }, |
| 84 | { OPCODE_LOG, "LOG", 1 }, |
| 85 | { OPCODE_LRP, "LRP", 3 }, |
| 86 | { OPCODE_MAD, "MAD", 3 }, |
| 87 | { OPCODE_MAX, "MAX", 2 }, |
| 88 | { OPCODE_MIN, "MIN", 2 }, |
| 89 | { OPCODE_MOV, "MOV", 1 }, |
| 90 | { OPCODE_MUL, "MUL", 2 }, |
| 91 | { OPCODE_PK2H, "PK2H", 1 }, |
| 92 | { OPCODE_PK2US, "PK2US", 1 }, |
| 93 | { OPCODE_PK4B, "PK4B", 1 }, |
| 94 | { OPCODE_PK4UB, "PK4UB", 1 }, |
| 95 | { OPCODE_POW, "POW", 2 }, |
| 96 | { OPCODE_POPA, "POPA", 0 }, |
| 97 | { OPCODE_PRINT, "PRINT", 1 }, |
| 98 | { OPCODE_PUSHA, "PUSHA", 0 }, |
| 99 | { OPCODE_RCC, "RCC", 1 }, |
| 100 | { OPCODE_RCP, "RCP", 1 }, |
| 101 | { OPCODE_RET, "RET", 0 }, |
| 102 | { OPCODE_RFL, "RFL", 1 }, |
| 103 | { OPCODE_RSQ, "RSQ", 1 }, |
| 104 | { OPCODE_SCS, "SCS", 1 }, |
| 105 | { OPCODE_SEQ, "SEQ", 2 }, |
| 106 | { OPCODE_SFL, "SFL", 0 }, |
| 107 | { OPCODE_SGE, "SGE", 2 }, |
| 108 | { OPCODE_SGT, "SGT", 2 }, |
| 109 | { OPCODE_SIN, "SIN", 1 }, |
| 110 | { OPCODE_SLE, "SLE", 2 }, |
| 111 | { OPCODE_SLT, "SLT", 2 }, |
| 112 | { OPCODE_SNE, "SNE", 2 }, |
| 113 | { OPCODE_SSG, "SSG", 1 }, |
| 114 | { OPCODE_STR, "STR", 0 }, |
| 115 | { OPCODE_SUB, "SUB", 2 }, |
| 116 | { OPCODE_SWZ, "SWZ", 1 }, |
| 117 | { OPCODE_TEX, "TEX", 1 }, |
| 118 | { OPCODE_TXB, "TXB", 1 }, |
| 119 | { OPCODE_TXD, "TXD", 3 }, |
| 120 | { OPCODE_TXL, "TXL", 1 }, |
| 121 | { OPCODE_TXP, "TXP", 1 }, |
| 122 | { OPCODE_TXP_NV, "TXP", 1 }, |
| 123 | { OPCODE_UP2H, "UP2H", 1 }, |
| 124 | { OPCODE_UP2US, "UP2US", 1 }, |
| 125 | { OPCODE_UP4B, "UP4B", 1 }, |
| 126 | { OPCODE_UP4UB, "UP4UB", 1 }, |
| 127 | { OPCODE_X2D, "X2D", 3 }, |
| 128 | { OPCODE_XPD, "XPD", 2 } |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Return the number of src registers for the given instruction/opcode. |
| 134 | */ |
| 135 | GLuint |
| 136 | _mesa_num_inst_src_regs(gl_inst_opcode opcode) |
| 137 | { |
| 138 | ASSERT(opcode == InstInfo[opcode].Opcode); |
| 139 | ASSERT(OPCODE_XPD == InstInfo[OPCODE_XPD].Opcode); |
| 140 | return InstInfo[opcode].NumSrcRegs; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Return string name for given program opcode. |
| 146 | */ |
| 147 | const char * |
| 148 | _mesa_opcode_string(gl_inst_opcode opcode) |
| 149 | { |
| 150 | ASSERT(opcode < MAX_OPCODE); |
| 151 | return InstInfo[opcode].Name; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Return string name for given program/register file. |
| 156 | */ |
| 157 | static const char * |
| 158 | program_file_string(enum register_file f) |
| 159 | { |
| 160 | switch (f) { |
| 161 | case PROGRAM_TEMPORARY: |
| 162 | return "TEMP"; |
| 163 | case PROGRAM_LOCAL_PARAM: |
| 164 | return "LOCAL"; |
| 165 | case PROGRAM_ENV_PARAM: |
| 166 | return "ENV"; |
| 167 | case PROGRAM_STATE_VAR: |
| 168 | return "STATE"; |
| 169 | case PROGRAM_INPUT: |
| 170 | return "INPUT"; |
| 171 | case PROGRAM_OUTPUT: |
| 172 | return "OUTPUT"; |
| 173 | case PROGRAM_NAMED_PARAM: |
| 174 | return "NAMED"; |
| 175 | case PROGRAM_CONSTANT: |
| 176 | return "CONST"; |
| 177 | case PROGRAM_UNIFORM: |
| 178 | return "UNIFORM"; |
| 179 | case PROGRAM_VARYING: |
| 180 | return "VARYING"; |
| 181 | case PROGRAM_WRITE_ONLY: |
| 182 | return "WRITE_ONLY"; |
| 183 | case PROGRAM_ADDRESS: |
| 184 | return "ADDR"; |
| 185 | default: |
| 186 | return "Unknown program file!"; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Return a string representation of the given swizzle word. |
| 193 | * If extended is true, use extended (comma-separated) format. |
| 194 | */ |
| 195 | static const char * |
| 196 | swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended) |
| 197 | { |
| 198 | static const char swz[] = "xyzw01"; |
| 199 | static char s[20]; |
| 200 | GLuint i = 0; |
| 201 | |
| 202 | if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0) |
| 203 | return ""; /* no swizzle/negation */ |
| 204 | |
| 205 | if (!extended) |
| 206 | s[i++] = '.'; |
| 207 | |
| 208 | if (negateBase & 0x1) |
| 209 | s[i++] = '-'; |
| 210 | s[i++] = swz[GET_SWZ(swizzle, 0)]; |
| 211 | |
| 212 | if (extended) { |
| 213 | s[i++] = ','; |
| 214 | } |
| 215 | |
| 216 | if (negateBase & 0x2) |
| 217 | s[i++] = '-'; |
| 218 | s[i++] = swz[GET_SWZ(swizzle, 1)]; |
| 219 | |
| 220 | if (extended) { |
| 221 | s[i++] = ','; |
| 222 | } |
| 223 | |
| 224 | if (negateBase & 0x4) |
| 225 | s[i++] = '-'; |
| 226 | s[i++] = swz[GET_SWZ(swizzle, 2)]; |
| 227 | |
| 228 | if (extended) { |
| 229 | s[i++] = ','; |
| 230 | } |
| 231 | |
| 232 | if (negateBase & 0x8) |
| 233 | s[i++] = '-'; |
| 234 | s[i++] = swz[GET_SWZ(swizzle, 3)]; |
| 235 | |
| 236 | s[i] = 0; |
| 237 | return s; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | static const char * |
| 242 | writemask_string(GLuint writeMask) |
| 243 | { |
| 244 | static char s[10]; |
| 245 | GLuint i = 0; |
| 246 | |
| 247 | if (writeMask == WRITEMASK_XYZW) |
| 248 | return ""; |
| 249 | |
| 250 | s[i++] = '.'; |
| 251 | if (writeMask & WRITEMASK_X) |
| 252 | s[i++] = 'x'; |
| 253 | if (writeMask & WRITEMASK_Y) |
| 254 | s[i++] = 'y'; |
| 255 | if (writeMask & WRITEMASK_Z) |
| 256 | s[i++] = 'z'; |
| 257 | if (writeMask & WRITEMASK_W) |
| 258 | s[i++] = 'w'; |
| 259 | |
| 260 | s[i] = 0; |
| 261 | return s; |
| 262 | } |
| 263 | |
| 264 | static void |
| 265 | print_dst_reg(const struct prog_dst_register *dstReg) |
| 266 | { |
| 267 | _mesa_printf(" %s[%d]%s", |
| 268 | program_file_string((enum register_file) dstReg->File), |
| 269 | dstReg->Index, |
| 270 | writemask_string(dstReg->WriteMask)); |
| 271 | } |
| 272 | |
| 273 | static void |
| 274 | print_src_reg(const struct prog_src_register *srcReg) |
| 275 | { |
| 276 | _mesa_printf("%s[%d]%s", |
| 277 | program_file_string((enum register_file) srcReg->File), |
| 278 | srcReg->Index, |
| 279 | swizzle_string(srcReg->Swizzle, |
| 280 | srcReg->NegateBase, GL_FALSE)); |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | print_comment(const struct prog_instruction *inst) |
| 285 | { |
| 286 | if (inst->Comment) |
| 287 | _mesa_printf("; # %s\n", inst->Comment); |
| 288 | else |
| 289 | _mesa_printf(";\n"); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | void |
| 294 | _mesa_print_alu_instruction(const struct prog_instruction *inst, |
| 295 | const char *opcode_string, |
| 296 | GLuint numRegs) |
| 297 | { |
| 298 | GLuint j; |
| 299 | |
| 300 | _mesa_printf("%s", opcode_string); |
| 301 | |
| 302 | /* frag prog only */ |
| 303 | if (inst->SaturateMode == SATURATE_ZERO_ONE) |
| 304 | _mesa_printf("_SAT"); |
| 305 | |
| 306 | if (inst->DstReg.File != PROGRAM_UNDEFINED) { |
| 307 | _mesa_printf(" %s[%d]%s", |
| 308 | program_file_string((enum register_file) inst->DstReg.File), |
| 309 | inst->DstReg.Index, |
| 310 | writemask_string(inst->DstReg.WriteMask)); |
| 311 | } |
| 312 | else { |
| 313 | _mesa_printf(" ???"); |
| 314 | } |
| 315 | |
| 316 | if (numRegs > 0) |
| 317 | _mesa_printf(", "); |
| 318 | |
| 319 | for (j = 0; j < numRegs; j++) { |
| 320 | print_src_reg(inst->SrcReg + j); |
| 321 | if (j + 1 < numRegs) |
| 322 | _mesa_printf(", "); |
| 323 | } |
| 324 | |
| 325 | if (inst->Comment) |
| 326 | _mesa_printf(" # %s", inst->Comment); |
| 327 | |
| 328 | print_comment(inst); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /** |
| 333 | * Print a single vertex/fragment program instruction. |
| 334 | */ |
| 335 | void |
| 336 | _mesa_print_instruction(const struct prog_instruction *inst) |
| 337 | { |
| 338 | switch (inst->Opcode) { |
| 339 | case OPCODE_PRINT: |
| 340 | _mesa_printf("PRINT '%s'", inst->Data); |
| 341 | if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) { |
| 342 | _mesa_printf(", "); |
| 343 | _mesa_printf("%s[%d]%s", |
| 344 | program_file_string((enum register_file) inst->SrcReg[0].File), |
| 345 | inst->SrcReg[0].Index, |
| 346 | swizzle_string(inst->SrcReg[0].Swizzle, |
| 347 | inst->SrcReg[0].NegateBase, GL_FALSE)); |
| 348 | } |
| 349 | if (inst->Comment) |
| 350 | _mesa_printf(" # %s", inst->Comment); |
| 351 | print_comment(inst); |
| 352 | break; |
| 353 | case OPCODE_SWZ: |
| 354 | _mesa_printf("SWZ"); |
| 355 | if (inst->SaturateMode == SATURATE_ZERO_ONE) |
| 356 | _mesa_printf("_SAT"); |
| 357 | print_dst_reg(&inst->DstReg); |
| 358 | _mesa_printf("%s[%d], %s", |
| 359 | program_file_string((enum register_file) inst->SrcReg[0].File), |
| 360 | inst->SrcReg[0].Index, |
| 361 | swizzle_string(inst->SrcReg[0].Swizzle, |
| 362 | inst->SrcReg[0].NegateBase, GL_TRUE)); |
| 363 | print_comment(inst); |
| 364 | break; |
| 365 | case OPCODE_TEX: |
| 366 | case OPCODE_TXP: |
| 367 | case OPCODE_TXB: |
| 368 | _mesa_printf("%s", _mesa_opcode_string(inst->Opcode)); |
| 369 | if (inst->SaturateMode == SATURATE_ZERO_ONE) |
| 370 | _mesa_printf("_SAT"); |
| 371 | _mesa_printf(" "); |
| 372 | print_dst_reg(&inst->DstReg); |
| 373 | _mesa_printf(", "); |
| 374 | print_src_reg(&inst->SrcReg[0]); |
| 375 | _mesa_printf(", texture[%d], ", inst->TexSrcUnit); |
| 376 | switch (inst->TexSrcTarget) { |
| 377 | case TEXTURE_1D_INDEX: _mesa_printf("1D"); break; |
| 378 | case TEXTURE_2D_INDEX: _mesa_printf("2D"); break; |
| 379 | case TEXTURE_3D_INDEX: _mesa_printf("3D"); break; |
| 380 | case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break; |
| 381 | case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break; |
| 382 | default: |
| 383 | ; |
| 384 | } |
| 385 | print_comment(inst); |
| 386 | break; |
| 387 | case OPCODE_ARL: |
| 388 | _mesa_printf("ARL addr.x, "); |
| 389 | print_src_reg(&inst->SrcReg[0]); |
| 390 | print_comment(inst); |
| 391 | break; |
| 392 | case OPCODE_BRA: |
| 393 | _mesa_printf("BRA %u", inst->BranchTarget); |
| 394 | print_comment(inst); |
| 395 | break; |
| 396 | case OPCODE_CAL: |
| 397 | _mesa_printf("CAL %u", inst->BranchTarget); |
| 398 | print_comment(inst); |
| 399 | break; |
| 400 | case OPCODE_END: |
| 401 | _mesa_printf("END"); |
| 402 | print_comment(inst); |
| 403 | break; |
| 404 | /* XXX may need other special-case instructions */ |
| 405 | default: |
| 406 | /* typical alu instruction */ |
| 407 | _mesa_print_alu_instruction(inst, |
| 408 | _mesa_opcode_string(inst->Opcode), |
| 409 | _mesa_num_inst_src_regs(inst->Opcode)); |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | |
| 415 | /** |
| 416 | * Print a vertx/fragment program to stdout. |
| 417 | * XXX this function could be greatly improved. |
| 418 | */ |
| 419 | void |
| 420 | _mesa_print_program(const struct gl_program *prog) |
| 421 | { |
| 422 | GLuint i; |
| 423 | for (i = 0; i < prog->NumInstructions; i++) { |
| 424 | _mesa_printf("%3d: ", i); |
| 425 | _mesa_print_instruction(prog->Instructions + i); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | |
| 430 | /** |
| 431 | * Print all of a program's parameters. |
| 432 | */ |
| 433 | void |
| 434 | _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog) |
| 435 | { |
| 436 | GLint i; |
| 437 | |
| 438 | _mesa_printf("InputsRead: 0x%x\n", prog->InputsRead); |
| 439 | _mesa_printf("OutputsWritten: 0x%x\n", prog->OutputsWritten); |
| 440 | _mesa_printf("NumInstructions=%d\n", prog->NumInstructions); |
| 441 | _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries); |
| 442 | _mesa_printf("NumParameters=%d\n", prog->NumParameters); |
| 443 | _mesa_printf("NumAttributes=%d\n", prog->NumAttributes); |
| 444 | _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs); |
| 445 | |
| 446 | _mesa_load_state_parameters(ctx, prog->Parameters); |
| 447 | |
| 448 | #if 0 |
| 449 | _mesa_printf("Local Params:\n"); |
| 450 | for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){ |
| 451 | const GLfloat *p = prog->LocalParams[i]; |
| 452 | _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]); |
| 453 | } |
| 454 | #endif |
| 455 | |
| 456 | for (i = 0; i < prog->Parameters->NumParameters; i++){ |
| 457 | struct gl_program_parameter *param = prog->Parameters->Parameters + i; |
| 458 | const GLfloat *v = prog->Parameters->ParameterValues[i]; |
| 459 | _mesa_printf("param[%d] %s %s = {%.3f, %.3f, %.3f, %.3f};\n", |
| 460 | i, |
| 461 | program_file_string(prog->Parameters->Parameters[i].Type), |
| 462 | param->Name, v[0], v[1], v[2], v[3]); |
| 463 | } |
| 464 | } |