blob: fa1d3614bfa7079e33909004dda5c5c04f694353 [file] [log] [blame]
Brian00cdc0a2006-12-14 15:01:06 -07001/*
2 * Mesa 3-D graphics library
Brian Paulf4361542008-11-11 10:47:10 -07003 * Version: 7.3
Brian00cdc0a2006-12-14 15:01:06 -07004 *
Brian Paulf4361542008-11-11 10:47:10 -07005 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
Brian Pauld0038932009-01-22 10:34:43 -07006 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
Brian00cdc0a2006-12-14 15:01:06 -07007 *
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
Brian Paulbbd28712008-09-18 12:26:54 -060032#include "main/glheader.h"
33#include "main/context.h"
34#include "main/imports.h"
Brian00cdc0a2006-12-14 15:01:06 -070035#include "prog_instruction.h"
36#include "prog_parameter.h"
37#include "prog_print.h"
38#include "prog_statevars.h"
39
40
Brian501ee872007-02-17 09:15:00 -070041
Brian00cdc0a2006-12-14 15:01:06 -070042/**
43 * Return string name for given program/register file.
44 */
45static const char *
Brian501ee872007-02-17 09:15:00 -070046file_string(enum register_file f, gl_prog_print_mode mode)
Brian00cdc0a2006-12-14 15:01:06 -070047{
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";
Brianb2ab6932007-01-05 16:01:43 -070073 case PROGRAM_SAMPLER:
74 return "SAMPLER";
Brian Paulbc7d2c42009-01-07 18:44:41 -070075 case PROGRAM_UNDEFINED:
76 return "UNDEFINED";
Brian00cdc0a2006-12-14 15:01:06 -070077 default:
78 return "Unknown program file!";
79 }
80}
81
82
83/**
Brian501ee872007-02-17 09:15:00 -070084 * Return ARB_v/f_prog-style input attrib string.
85 */
86static const char *
87arb_input_attrib_string(GLint index, GLenum progType)
88{
89 const char *vertAttribs[] = {
90 "vertex.position",
91 "vertex.weight",
92 "vertex.normal",
93 "vertex.color.primary",
94 "vertex.color.secondary",
95 "vertex.fogcoord",
96 "vertex.(six)",
97 "vertex.(seven)",
98 "vertex.texcoord[0]",
99 "vertex.texcoord[1]",
100 "vertex.texcoord[2]",
101 "vertex.texcoord[3]",
102 "vertex.texcoord[4]",
103 "vertex.texcoord[5]",
104 "vertex.texcoord[6]",
Markus Amslerd7878502008-03-17 12:11:11 +0100105 "vertex.texcoord[7]",
106 "vertex.attrib[0]",
107 "vertex.attrib[1]",
108 "vertex.attrib[2]",
109 "vertex.attrib[3]",
110 "vertex.attrib[4]",
111 "vertex.attrib[5]",
112 "vertex.attrib[6]",
113 "vertex.attrib[7]",
114 "vertex.attrib[8]",
115 "vertex.attrib[9]",
116 "vertex.attrib[10]",
117 "vertex.attrib[11]",
118 "vertex.attrib[12]",
119 "vertex.attrib[13]",
120 "vertex.attrib[14]",
121 "vertex.attrib[15]"
Brian501ee872007-02-17 09:15:00 -0700122 };
123 const char *fragAttribs[] = {
124 "fragment.position",
125 "fragment.color.primary",
126 "fragment.color.secondary",
127 "fragment.fogcoord",
128 "fragment.texcoord[0]",
129 "fragment.texcoord[1]",
130 "fragment.texcoord[2]",
131 "fragment.texcoord[3]",
132 "fragment.texcoord[4]",
133 "fragment.texcoord[5]",
134 "fragment.texcoord[6]",
135 "fragment.texcoord[7]",
136 "fragment.varying[0]",
137 "fragment.varying[1]",
138 "fragment.varying[2]",
139 "fragment.varying[3]",
140 "fragment.varying[4]",
141 "fragment.varying[5]",
142 "fragment.varying[6]",
143 "fragment.varying[7]"
144 };
145
146 if (progType == GL_VERTEX_PROGRAM_ARB) {
147 assert(index < sizeof(vertAttribs) / sizeof(vertAttribs[0]));
148 return vertAttribs[index];
149 }
150 else {
151 assert(index < sizeof(fragAttribs) / sizeof(fragAttribs[0]));
152 return fragAttribs[index];
153 }
154}
155
156
157/**
158 * Return ARB_v/f_prog-style output attrib string.
159 */
160static const char *
161arb_output_attrib_string(GLint index, GLenum progType)
162{
163 const char *vertResults[] = {
164 "result.position",
165 "result.color.primary",
166 "result.color.secondary",
167 "result.fogcoord",
168 "result.texcoord[0]",
169 "result.texcoord[1]",
170 "result.texcoord[2]",
171 "result.texcoord[3]",
172 "result.texcoord[4]",
173 "result.texcoord[5]",
174 "result.texcoord[6]",
175 "result.texcoord[7]",
176 "result.varying[0]",
177 "result.varying[1]",
178 "result.varying[2]",
179 "result.varying[3]",
180 "result.varying[4]",
181 "result.varying[5]",
182 "result.varying[6]",
183 "result.varying[7]"
184 };
185 const char *fragResults[] = {
186 "result.color",
187 "result.depth"
188 };
189
190 if (progType == GL_VERTEX_PROGRAM_ARB) {
191 assert(index < sizeof(vertResults) / sizeof(vertResults[0]));
192 return vertResults[index];
193 }
194 else {
195 assert(index < sizeof(fragResults) / sizeof(fragResults[0]));
196 return fragResults[index];
197 }
198}
199
200
201/**
202 * Return string representation of the given register.
203 * Note that some types of registers (like PROGRAM_UNIFORM) aren't defined
204 * by the ARB/NV program languages so we've taken some liberties here.
205 * \param file the register file (PROGRAM_INPUT, PROGRAM_TEMPORARY, etc)
206 * \param index number of the register in the register file
207 * \param mode the output format/mode/style
208 * \param prog pointer to containing program
209 */
210static const char *
211reg_string(enum register_file f, GLint index, gl_prog_print_mode mode,
Zack Rusin19659a52008-06-12 14:19:10 -0400212 GLboolean relAddr, const struct gl_program *prog)
Brian501ee872007-02-17 09:15:00 -0700213{
214 static char str[100];
215
216 str[0] = 0;
217
218 switch (mode) {
219 case PROG_PRINT_DEBUG:
Zack Rusin74964ff2008-06-10 16:59:44 -0400220 if (relAddr)
Brian Paul557fde92008-11-12 11:12:10 -0700221 sprintf(str, "%s[ADDR+%d]", file_string(f, mode), index);
Zack Rusin74964ff2008-06-10 16:59:44 -0400222 else
223 sprintf(str, "%s[%d]", file_string(f, mode), index);
Brian501ee872007-02-17 09:15:00 -0700224 break;
225
226 case PROG_PRINT_ARB:
227 switch (f) {
228 case PROGRAM_INPUT:
229 sprintf(str, "%s", arb_input_attrib_string(index, prog->Target));
230 break;
231 case PROGRAM_OUTPUT:
232 sprintf(str, "%s", arb_output_attrib_string(index, prog->Target));
233 break;
234 case PROGRAM_TEMPORARY:
235 sprintf(str, "temp%d", index);
236 break;
237 case PROGRAM_ENV_PARAM:
238 sprintf(str, "program.env[%d]", index);
239 break;
240 case PROGRAM_LOCAL_PARAM:
241 sprintf(str, "program.local[%d]", index);
242 break;
243 case PROGRAM_VARYING: /* extension */
244 sprintf(str, "varying[%d]", index);
245 break;
246 case PROGRAM_CONSTANT: /* extension */
247 sprintf(str, "constant[%d]", index);
248 break;
249 case PROGRAM_UNIFORM: /* extension */
250 sprintf(str, "uniform[%d]", index);
251 break;
252 case PROGRAM_STATE_VAR:
253 {
254 struct gl_program_parameter *param
255 = prog->Parameters->Parameters + index;
256 sprintf(str, _mesa_program_state_string(param->StateIndexes));
257 }
258 break;
259 case PROGRAM_ADDRESS:
260 sprintf(str, "A%d", index);
261 break;
262 default:
263 _mesa_problem(NULL, "bad file in reg_string()");
264 }
265 break;
266
267 case PROG_PRINT_NV:
268 switch (f) {
269 case PROGRAM_INPUT:
270 if (prog->Target == GL_VERTEX_PROGRAM_ARB)
271 sprintf(str, "v[%d]", index);
272 else
273 sprintf(str, "f[%d]", index);
274 break;
275 case PROGRAM_OUTPUT:
276 sprintf(str, "o[%d]", index);
277 break;
278 case PROGRAM_TEMPORARY:
279 sprintf(str, "R%d", index);
280 break;
281 case PROGRAM_ENV_PARAM:
282 sprintf(str, "c[%d]", index);
283 break;
284 case PROGRAM_VARYING: /* extension */
285 sprintf(str, "varying[%d]", index);
286 break;
287 case PROGRAM_UNIFORM: /* extension */
288 sprintf(str, "uniform[%d]", index);
289 break;
290 case PROGRAM_CONSTANT: /* extension */
291 sprintf(str, "constant[%d]", index);
292 break;
293 case PROGRAM_STATE_VAR: /* extension */
294 sprintf(str, "state[%d]", index);
295 break;
296 default:
297 _mesa_problem(NULL, "bad file in reg_string()");
298 }
299 break;
300
301 default:
302 _mesa_problem(NULL, "bad mode in reg_string()");
303 }
304
305 return str;
306}
307
308
309/**
Brian00cdc0a2006-12-14 15:01:06 -0700310 * Return a string representation of the given swizzle word.
311 * If extended is true, use extended (comma-separated) format.
Brian3a281532006-12-16 12:51:34 -0700312 * \param swizzle the swizzle field
313 * \param negateBase 4-bit negation vector
314 * \param extended if true, also allow 0, 1 values
Brian00cdc0a2006-12-14 15:01:06 -0700315 */
Brian059376c2007-02-22 17:45:32 -0700316const char *
317_mesa_swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
Brian00cdc0a2006-12-14 15:01:06 -0700318{
Brian Paul74a19b02008-07-18 19:46:19 -0600319 static const char swz[] = "xyzw01!?"; /* See SWIZZLE_x definitions */
Brian00cdc0a2006-12-14 15:01:06 -0700320 static char s[20];
321 GLuint i = 0;
322
323 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
324 return ""; /* no swizzle/negation */
325
326 if (!extended)
327 s[i++] = '.';
328
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600329 if (negateBase & NEGATE_X)
Brian00cdc0a2006-12-14 15:01:06 -0700330 s[i++] = '-';
331 s[i++] = swz[GET_SWZ(swizzle, 0)];
332
333 if (extended) {
334 s[i++] = ',';
335 }
336
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600337 if (negateBase & NEGATE_Y)
Brian00cdc0a2006-12-14 15:01:06 -0700338 s[i++] = '-';
339 s[i++] = swz[GET_SWZ(swizzle, 1)];
340
341 if (extended) {
342 s[i++] = ',';
343 }
344
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600345 if (negateBase & NEGATE_Z)
Brian00cdc0a2006-12-14 15:01:06 -0700346 s[i++] = '-';
347 s[i++] = swz[GET_SWZ(swizzle, 2)];
348
349 if (extended) {
350 s[i++] = ',';
351 }
352
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600353 if (negateBase & NEGATE_W)
Brian00cdc0a2006-12-14 15:01:06 -0700354 s[i++] = '-';
355 s[i++] = swz[GET_SWZ(swizzle, 3)];
356
357 s[i] = 0;
358 return s;
359}
360
361
Brian Paul610c2462008-11-13 16:37:52 -0700362const char *
363_mesa_writemask_string(GLuint writeMask)
Brian00cdc0a2006-12-14 15:01:06 -0700364{
365 static char s[10];
366 GLuint i = 0;
367
368 if (writeMask == WRITEMASK_XYZW)
369 return "";
370
371 s[i++] = '.';
372 if (writeMask & WRITEMASK_X)
373 s[i++] = 'x';
374 if (writeMask & WRITEMASK_Y)
375 s[i++] = 'y';
376 if (writeMask & WRITEMASK_Z)
377 s[i++] = 'z';
378 if (writeMask & WRITEMASK_W)
379 s[i++] = 'w';
380
381 s[i] = 0;
382 return s;
383}
384
Brian3a281532006-12-16 12:51:34 -0700385
Briand7508612007-03-28 11:01:09 -0600386const char *
387_mesa_condcode_string(GLuint condcode)
Brian3a281532006-12-16 12:51:34 -0700388{
389 switch (condcode) {
390 case COND_GT: return "GT";
391 case COND_EQ: return "EQ";
392 case COND_LT: return "LT";
393 case COND_UN: return "UN";
394 case COND_GE: return "GE";
395 case COND_LE: return "LE";
396 case COND_NE: return "NE";
397 case COND_TR: return "TR";
398 case COND_FL: return "FL";
399 default: return "cond???";
400 }
401}
402
403
Brian00cdc0a2006-12-14 15:01:06 -0700404static void
Brian Pauld0038932009-01-22 10:34:43 -0700405fprint_dst_reg(FILE * f,
406 const struct prog_dst_register *dstReg,
407 gl_prog_print_mode mode,
408 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700409{
Brian Pauld0038932009-01-22 10:34:43 -0700410 _mesa_fprintf(f, "%s%s",
411 reg_string((enum register_file) dstReg->File,
412 dstReg->Index, mode, dstReg->RelAddr, prog),
413 _mesa_writemask_string(dstReg->WriteMask));
Brian501ee872007-02-17 09:15:00 -0700414
Brian1936b252007-03-22 09:04:18 -0600415 if (dstReg->CondMask != COND_TR) {
Brian Pauld0038932009-01-22 10:34:43 -0700416 _mesa_fprintf(f, " (%s.%s)",
417 _mesa_condcode_string(dstReg->CondMask),
418 _mesa_swizzle_string(dstReg->CondSwizzle,
419 GL_FALSE, GL_FALSE));
Brian1936b252007-03-22 09:04:18 -0600420 }
421
Brian501ee872007-02-17 09:15:00 -0700422#if 0
Brian Pauld0038932009-01-22 10:34:43 -0700423 _mesa_fprintf(f, "%s[%d]%s",
Brian501ee872007-02-17 09:15:00 -0700424 file_string((enum register_file) dstReg->File, mode),
Brian00cdc0a2006-12-14 15:01:06 -0700425 dstReg->Index,
Brian Paul610c2462008-11-13 16:37:52 -0700426 _mesa_writemask_string(dstReg->WriteMask));
Brian501ee872007-02-17 09:15:00 -0700427#endif
Brian00cdc0a2006-12-14 15:01:06 -0700428}
429
Brian Pauld0038932009-01-22 10:34:43 -0700430
Brian00cdc0a2006-12-14 15:01:06 -0700431static void
Brian Pauld0038932009-01-22 10:34:43 -0700432fprint_src_reg(FILE *f,
433 const struct prog_src_register *srcReg,
434 gl_prog_print_mode mode,
435 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700436{
Brian Pauld0038932009-01-22 10:34:43 -0700437 _mesa_fprintf(f, "%s%s",
438 reg_string((enum register_file) srcReg->File,
439 srcReg->Index, mode, srcReg->RelAddr, prog),
440 _mesa_swizzle_string(srcReg->Swizzle,
441 srcReg->NegateBase, GL_FALSE));
Brian501ee872007-02-17 09:15:00 -0700442#if 0
Brian Pauld0038932009-01-22 10:34:43 -0700443 _mesa_fprintf(f, "%s[%d]%s",
444 file_string((enum register_file) srcReg->File, mode),
445 srcReg->Index,
446 _mesa_swizzle_string(srcReg->Swizzle,
447 srcReg->NegateBase, GL_FALSE));
Brian501ee872007-02-17 09:15:00 -0700448#endif
Brian00cdc0a2006-12-14 15:01:06 -0700449}
450
Brian Pauld0038932009-01-22 10:34:43 -0700451
Brian00cdc0a2006-12-14 15:01:06 -0700452static void
Brian Pauld0038932009-01-22 10:34:43 -0700453fprint_comment(FILE *f, const struct prog_instruction *inst)
Brian00cdc0a2006-12-14 15:01:06 -0700454{
455 if (inst->Comment)
Brian Pauld0038932009-01-22 10:34:43 -0700456 _mesa_fprintf(f, "; # %s\n", inst->Comment);
Brian00cdc0a2006-12-14 15:01:06 -0700457 else
Brian Pauld0038932009-01-22 10:34:43 -0700458 _mesa_fprintf(f, ";\n");
Brian00cdc0a2006-12-14 15:01:06 -0700459}
460
461
Brian501ee872007-02-17 09:15:00 -0700462static void
Brian Pauld0038932009-01-22 10:34:43 -0700463fprint_alu_instruction(FILE *f,
464 const struct prog_instruction *inst,
465 const char *opcode_string, GLuint numRegs,
466 gl_prog_print_mode mode,
467 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700468{
469 GLuint j;
470
Brian Pauld0038932009-01-22 10:34:43 -0700471 _mesa_fprintf(f, "%s", opcode_string);
Brianb50280e2006-12-18 16:21:58 -0700472 if (inst->CondUpdate)
Brian Pauld0038932009-01-22 10:34:43 -0700473 _mesa_fprintf(f, ".C");
Brian00cdc0a2006-12-14 15:01:06 -0700474
475 /* frag prog only */
476 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Pauld0038932009-01-22 10:34:43 -0700477 _mesa_fprintf(f, "_SAT");
Brian00cdc0a2006-12-14 15:01:06 -0700478
Brian Pauld0038932009-01-22 10:34:43 -0700479 _mesa_fprintf(f, " ");
Brian00cdc0a2006-12-14 15:01:06 -0700480 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
Brian Pauld0038932009-01-22 10:34:43 -0700481 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700482 }
483 else {
Brian Pauld0038932009-01-22 10:34:43 -0700484 _mesa_fprintf(f, " ???");
Brian00cdc0a2006-12-14 15:01:06 -0700485 }
486
487 if (numRegs > 0)
Brian Pauld0038932009-01-22 10:34:43 -0700488 _mesa_fprintf(f, ", ");
Brian00cdc0a2006-12-14 15:01:06 -0700489
490 for (j = 0; j < numRegs; j++) {
Brian Pauld0038932009-01-22 10:34:43 -0700491 fprint_src_reg(f, inst->SrcReg + j, mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700492 if (j + 1 < numRegs)
Brian Pauld0038932009-01-22 10:34:43 -0700493 _mesa_fprintf(f, ", ");
Brian00cdc0a2006-12-14 15:01:06 -0700494 }
495
Brian Pauld0038932009-01-22 10:34:43 -0700496 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700497}
498
499
Brian501ee872007-02-17 09:15:00 -0700500void
Brian0020d102007-02-23 11:43:44 -0700501_mesa_print_alu_instruction(const struct prog_instruction *inst,
502 const char *opcode_string, GLuint numRegs)
503{
Brian Pauld0038932009-01-22 10:34:43 -0700504 fprint_alu_instruction(stdout, inst, opcode_string,
505 numRegs, PROG_PRINT_DEBUG, NULL);
Brian501ee872007-02-17 09:15:00 -0700506}
507
508
Brian00cdc0a2006-12-14 15:01:06 -0700509/**
510 * Print a single vertex/fragment program instruction.
511 */
Brian Pauld0038932009-01-22 10:34:43 -0700512static GLint
513_mesa_fprint_instruction_opt(FILE *f,
514 const struct prog_instruction *inst,
515 GLint indent,
Brian501ee872007-02-17 09:15:00 -0700516 gl_prog_print_mode mode,
517 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700518{
Brian7b300532007-02-22 09:08:36 -0700519 GLint i;
Brian5db088d2007-02-05 14:58:15 -0700520
521 if (inst->Opcode == OPCODE_ELSE ||
522 inst->Opcode == OPCODE_ENDIF ||
523 inst->Opcode == OPCODE_ENDLOOP ||
524 inst->Opcode == OPCODE_ENDSUB) {
525 indent -= 3;
526 }
Brian5db088d2007-02-05 14:58:15 -0700527 for (i = 0; i < indent; i++) {
Brian Pauld0038932009-01-22 10:34:43 -0700528 _mesa_fprintf(f, " ");
Brian5db088d2007-02-05 14:58:15 -0700529 }
530
Brian00cdc0a2006-12-14 15:01:06 -0700531 switch (inst->Opcode) {
532 case OPCODE_PRINT:
Brian Pauld0038932009-01-22 10:34:43 -0700533 _mesa_fprintf(f, "PRINT '%s'", inst->Data);
Brian00cdc0a2006-12-14 15:01:06 -0700534 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
Brian Pauld0038932009-01-22 10:34:43 -0700535 _mesa_fprintf(f, ", ");
536 _mesa_fprintf(f, "%s[%d]%s",
Brian501ee872007-02-17 09:15:00 -0700537 file_string((enum register_file) inst->SrcReg[0].File,
538 mode),
Brian00cdc0a2006-12-14 15:01:06 -0700539 inst->SrcReg[0].Index,
Brian059376c2007-02-22 17:45:32 -0700540 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
541 inst->SrcReg[0].NegateBase, GL_FALSE));
Brian00cdc0a2006-12-14 15:01:06 -0700542 }
543 if (inst->Comment)
Brian Pauld0038932009-01-22 10:34:43 -0700544 _mesa_fprintf(f, " # %s", inst->Comment);
545 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700546 break;
547 case OPCODE_SWZ:
Brian Pauld0038932009-01-22 10:34:43 -0700548 _mesa_fprintf(f, "SWZ");
Brian00cdc0a2006-12-14 15:01:06 -0700549 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Pauld0038932009-01-22 10:34:43 -0700550 _mesa_fprintf(f, "_SAT");
551 _mesa_fprintf(f, " ");
552 fprint_dst_reg(f, &inst->DstReg, mode, prog);
553 _mesa_fprintf(f, ", %s[%d], %s",
Brian501ee872007-02-17 09:15:00 -0700554 file_string((enum register_file) inst->SrcReg[0].File,
555 mode),
Brian00cdc0a2006-12-14 15:01:06 -0700556 inst->SrcReg[0].Index,
Brian059376c2007-02-22 17:45:32 -0700557 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
558 inst->SrcReg[0].NegateBase, GL_TRUE));
Brian Pauld0038932009-01-22 10:34:43 -0700559 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700560 break;
561 case OPCODE_TEX:
562 case OPCODE_TXP:
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600563 case OPCODE_TXL:
Brian00cdc0a2006-12-14 15:01:06 -0700564 case OPCODE_TXB:
Brian Pauld0038932009-01-22 10:34:43 -0700565 _mesa_fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
Brian00cdc0a2006-12-14 15:01:06 -0700566 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Pauld0038932009-01-22 10:34:43 -0700567 _mesa_fprintf(f, "_SAT");
568 _mesa_fprintf(f, " ");
569 fprint_dst_reg(f, &inst->DstReg, mode, prog);
570 _mesa_fprintf(f, ", ");
571 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
572 _mesa_fprintf(f, ", texture[%d], ", inst->TexSrcUnit);
Brian00cdc0a2006-12-14 15:01:06 -0700573 switch (inst->TexSrcTarget) {
Brian Pauld0038932009-01-22 10:34:43 -0700574 case TEXTURE_1D_INDEX: _mesa_fprintf(f, "1D"); break;
575 case TEXTURE_2D_INDEX: _mesa_fprintf(f, "2D"); break;
576 case TEXTURE_3D_INDEX: _mesa_fprintf(f, "3D"); break;
577 case TEXTURE_CUBE_INDEX: _mesa_fprintf(f, "CUBE"); break;
578 case TEXTURE_RECT_INDEX: _mesa_fprintf(f, "RECT"); break;
Brian00cdc0a2006-12-14 15:01:06 -0700579 default:
580 ;
581 }
Brian Pauld0038932009-01-22 10:34:43 -0700582 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700583 break;
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600584
585 case OPCODE_KIL:
Brian Pauld0038932009-01-22 10:34:43 -0700586 _mesa_fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
587 _mesa_fprintf(f, " ");
588 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
589 fprint_comment(f, inst);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600590 break;
591 case OPCODE_KIL_NV:
Brian Pauld0038932009-01-22 10:34:43 -0700592 _mesa_fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
593 _mesa_fprintf(f, " ");
594 _mesa_fprintf(f, "%s.%s",
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600595 _mesa_condcode_string(inst->DstReg.CondMask),
596 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
597 GL_FALSE, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700598 fprint_comment(f, inst);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600599 break;
600
Brian00cdc0a2006-12-14 15:01:06 -0700601 case OPCODE_ARL:
Brian Pauld0038932009-01-22 10:34:43 -0700602 _mesa_fprintf(f, "ARL ");
603 fprint_dst_reg(f, &inst->DstReg, mode, prog);
604 _mesa_fprintf(f, ", ");
605 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
606 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700607 break;
608 case OPCODE_BRA:
Brian Pauld0038932009-01-22 10:34:43 -0700609 _mesa_fprintf(f, "BRA %d (%s%s)",
Brian3a281532006-12-16 12:51:34 -0700610 inst->BranchTarget,
Briand7508612007-03-28 11:01:09 -0600611 _mesa_condcode_string(inst->DstReg.CondMask),
Brian059376c2007-02-22 17:45:32 -0700612 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700613 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700614 break;
Brian5ae49cf2007-01-20 09:27:40 -0700615 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600616 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
617 /* Use ordinary register */
Brian Pauld0038932009-01-22 10:34:43 -0700618 _mesa_fprintf(f, "IF ");
619 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
620 _mesa_fprintf(f, "; ");
Brian63556fa2007-03-23 14:47:46 -0600621 }
622 else {
623 /* Use cond codes */
Brian Pauld0038932009-01-22 10:34:43 -0700624 _mesa_fprintf(f, "IF (%s%s);",
Briand7508612007-03-28 11:01:09 -0600625 _mesa_condcode_string(inst->DstReg.CondMask),
Brian63556fa2007-03-23 14:47:46 -0600626 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
627 0, GL_FALSE));
628 }
Brian Pauld0038932009-01-22 10:34:43 -0700629 _mesa_fprintf(f, " # (if false, goto %d)", inst->BranchTarget);
630 fprint_comment(f, inst);
Brian5db088d2007-02-05 14:58:15 -0700631 return indent + 3;
632 case OPCODE_ELSE:
Brian Pauld0038932009-01-22 10:34:43 -0700633 _mesa_fprintf(f, "ELSE; # (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700634 return indent + 3;
635 case OPCODE_ENDIF:
Brian Pauld0038932009-01-22 10:34:43 -0700636 _mesa_fprintf(f, "ENDIF;\n");
Brian5db088d2007-02-05 14:58:15 -0700637 break;
638 case OPCODE_BGNLOOP:
Brian Pauld0038932009-01-22 10:34:43 -0700639 _mesa_fprintf(f, "BGNLOOP; # (end at %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700640 return indent + 3;
641 case OPCODE_ENDLOOP:
Brian Pauld0038932009-01-22 10:34:43 -0700642 _mesa_fprintf(f, "ENDLOOP; # (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700643 break;
644 case OPCODE_BRK:
Brianf22ed092007-02-06 22:31:19 -0700645 case OPCODE_CONT:
Brian Pauld0038932009-01-22 10:34:43 -0700646 _mesa_fprintf(f, "%s (%s%s); # (goto %d)",
Brian81767ee2007-03-23 17:45:53 -0600647 _mesa_opcode_string(inst->Opcode),
Briand7508612007-03-28 11:01:09 -0600648 _mesa_condcode_string(inst->DstReg.CondMask),
Brian059376c2007-02-22 17:45:32 -0700649 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
Brian2755c792007-02-05 18:01:02 -0700650 inst->BranchTarget);
Brian Pauld0038932009-01-22 10:34:43 -0700651 fprint_comment(f, inst);
Brian5ae49cf2007-01-20 09:27:40 -0700652 break;
Brian63556fa2007-03-23 14:47:46 -0600653
Brian5db088d2007-02-05 14:58:15 -0700654 case OPCODE_BGNSUB:
Brianf407cad2007-03-27 14:53:17 -0600655 if (mode == PROG_PRINT_NV) {
Brian Pauld0038932009-01-22 10:34:43 -0700656 _mesa_fprintf(f, "%s:\n", inst->Comment); /* comment is label */
Brianf407cad2007-03-27 14:53:17 -0600657 return indent;
658 }
659 else {
Brian Pauld0038932009-01-22 10:34:43 -0700660 _mesa_fprintf(f, "BGNSUB");
661 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600662 return indent + 3;
663 }
Brian5db088d2007-02-05 14:58:15 -0700664 case OPCODE_ENDSUB:
Brianf407cad2007-03-27 14:53:17 -0600665 if (mode == PROG_PRINT_DEBUG) {
Brian Pauld0038932009-01-22 10:34:43 -0700666 _mesa_fprintf(f, "ENDSUB");
667 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600668 }
669 break;
670 case OPCODE_CAL:
671 if (mode == PROG_PRINT_NV) {
Brian Pauld0038932009-01-22 10:34:43 -0700672 _mesa_fprintf(f, "CAL %s; # (goto %d)\n", inst->Comment, inst->BranchTarget);
Brianf407cad2007-03-27 14:53:17 -0600673 }
674 else {
Brian Pauld0038932009-01-22 10:34:43 -0700675 _mesa_fprintf(f, "CAL %u", inst->BranchTarget);
676 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600677 }
678 break;
679 case OPCODE_RET:
Brian Pauld0038932009-01-22 10:34:43 -0700680 _mesa_fprintf(f, "RET (%s%s)",
Briand7508612007-03-28 11:01:09 -0600681 _mesa_condcode_string(inst->DstReg.CondMask),
682 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700683 fprint_comment(f, inst);
Brian5ae49cf2007-01-20 09:27:40 -0700684 break;
Brianf407cad2007-03-27 14:53:17 -0600685
Brian00cdc0a2006-12-14 15:01:06 -0700686 case OPCODE_END:
Brian Pauld0038932009-01-22 10:34:43 -0700687 _mesa_fprintf(f, "END\n");
Brian00cdc0a2006-12-14 15:01:06 -0700688 break;
Brian3a281532006-12-16 12:51:34 -0700689 case OPCODE_NOP:
Brian059376c2007-02-22 17:45:32 -0700690 if (mode == PROG_PRINT_DEBUG) {
Brian Pauld0038932009-01-22 10:34:43 -0700691 _mesa_fprintf(f, "NOP");
692 fprint_comment(f, inst);
Brian059376c2007-02-22 17:45:32 -0700693 }
694 else if (inst->Comment) {
695 /* ARB/NV extensions don't have NOP instruction */
Brian Pauld0038932009-01-22 10:34:43 -0700696 _mesa_fprintf(f, "# %s\n", inst->Comment);
Brian059376c2007-02-22 17:45:32 -0700697 }
Brian3a281532006-12-16 12:51:34 -0700698 break;
Brian00cdc0a2006-12-14 15:01:06 -0700699 /* XXX may need other special-case instructions */
700 default:
701 /* typical alu instruction */
Brian Pauld0038932009-01-22 10:34:43 -0700702 fprint_alu_instruction(f, inst,
703 _mesa_opcode_string(inst->Opcode),
704 _mesa_num_inst_src_regs(inst->Opcode),
705 mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700706 break;
707 }
Brian5db088d2007-02-05 14:58:15 -0700708 return indent;
Brian00cdc0a2006-12-14 15:01:06 -0700709}
710
711
Brian Pauld0038932009-01-22 10:34:43 -0700712GLint
713_mesa_print_instruction_opt(const struct prog_instruction *inst,
714 GLint indent,
715 gl_prog_print_mode mode,
716 const struct gl_program *prog)
717{
718 return _mesa_fprint_instruction_opt(stdout, inst, indent, mode, prog);
719}
720
721
722void
723_mesa_print_instruction(const struct prog_instruction *inst)
724{
725 /* note: 4th param should be ignored for PROG_PRINT_DEBUG */
726 _mesa_fprint_instruction_opt(stdout, inst, 0, PROG_PRINT_DEBUG, NULL);
727}
728
729
730
731/**
732 * Print program, with options.
733 */
Brian Pauld2eff332009-02-02 12:24:41 -0700734void
Brian Pauld0038932009-01-22 10:34:43 -0700735_mesa_fprint_program_opt(FILE *f,
736 const struct gl_program *prog,
737 gl_prog_print_mode mode,
738 GLboolean lineNumbers)
739{
740 GLuint i, indent = 0;
741
742 switch (prog->Target) {
743 case GL_VERTEX_PROGRAM_ARB:
744 if (mode == PROG_PRINT_ARB)
745 _mesa_fprintf(f, "!!ARBvp1.0\n");
746 else if (mode == PROG_PRINT_NV)
747 _mesa_fprintf(f, "!!VP1.0\n");
748 else
749 _mesa_fprintf(f, "# Vertex Program/Shader\n");
750 break;
751 case GL_FRAGMENT_PROGRAM_ARB:
752 case GL_FRAGMENT_PROGRAM_NV:
753 if (mode == PROG_PRINT_ARB)
754 _mesa_fprintf(f, "!!ARBfp1.0\n");
755 else if (mode == PROG_PRINT_NV)
756 _mesa_fprintf(f, "!!FP1.0\n");
757 else
758 _mesa_fprintf(f, "# Fragment Program/Shader\n");
759 break;
760 }
761
762 for (i = 0; i < prog->NumInstructions; i++) {
763 if (lineNumbers)
764 _mesa_fprintf(f, "%3d: ", i);
765 indent = _mesa_fprint_instruction_opt(f, prog->Instructions + i,
766 indent, mode, prog);
767 }
768}
769
770
Brian00cdc0a2006-12-14 15:01:06 -0700771/**
Brian501ee872007-02-17 09:15:00 -0700772 * Print program to stdout, default options.
Brian00cdc0a2006-12-14 15:01:06 -0700773 */
774void
775_mesa_print_program(const struct gl_program *prog)
776{
Brian Pauld0038932009-01-22 10:34:43 -0700777 _mesa_fprint_program_opt(stdout, prog, PROG_PRINT_DEBUG, GL_TRUE);
Brian501ee872007-02-17 09:15:00 -0700778}
779
780
781/**
Brian Pauld0038932009-01-22 10:34:43 -0700782 * Print all of a program's parameters/fields to given file.
Brian501ee872007-02-17 09:15:00 -0700783 */
Brian Pauld0038932009-01-22 10:34:43 -0700784static void
785_mesa_fprint_program_parameters(FILE *f,
786 GLcontext *ctx,
787 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700788{
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600789 GLuint i;
790
Brian Pauld0038932009-01-22 10:34:43 -0700791 _mesa_fprintf(f, "InputsRead: 0x%x\n", prog->InputsRead);
792 _mesa_fprintf(f, "OutputsWritten: 0x%x\n", prog->OutputsWritten);
793 _mesa_fprintf(f, "NumInstructions=%d\n", prog->NumInstructions);
794 _mesa_fprintf(f, "NumTemporaries=%d\n", prog->NumTemporaries);
795 _mesa_fprintf(f, "NumParameters=%d\n", prog->NumParameters);
796 _mesa_fprintf(f, "NumAttributes=%d\n", prog->NumAttributes);
797 _mesa_fprintf(f, "NumAddressRegs=%d\n", prog->NumAddressRegs);
798 _mesa_fprintf(f, "Samplers=[ ");
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600799 for (i = 0; i < MAX_SAMPLERS; i++) {
Brian Pauld0038932009-01-22 10:34:43 -0700800 _mesa_fprintf(f, "%d ", prog->SamplerUnits[i]);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600801 }
Brian Pauld0038932009-01-22 10:34:43 -0700802 _mesa_fprintf(f, "]\n");
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600803
Brian00cdc0a2006-12-14 15:01:06 -0700804 _mesa_load_state_parameters(ctx, prog->Parameters);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600805
Brian00cdc0a2006-12-14 15:01:06 -0700806#if 0
Brian Pauld0038932009-01-22 10:34:43 -0700807 _mesa_fprintf(f, "Local Params:\n");
Brian00cdc0a2006-12-14 15:01:06 -0700808 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
809 const GLfloat *p = prog->LocalParams[i];
Brian Pauld0038932009-01-22 10:34:43 -0700810 _mesa_fprintf(f, "%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
Brian00cdc0a2006-12-14 15:01:06 -0700811 }
812#endif
Brian83ca3ff2006-12-20 17:17:38 -0700813 _mesa_print_parameter_list(prog->Parameters);
814}
Brian00cdc0a2006-12-14 15:01:06 -0700815
Brian83ca3ff2006-12-20 17:17:38 -0700816
Brian Pauld0038932009-01-22 10:34:43 -0700817/**
818 * Print all of a program's parameters/fields to stdout.
819 */
Brian83ca3ff2006-12-20 17:17:38 -0700820void
Brian Pauld0038932009-01-22 10:34:43 -0700821_mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
822{
823 _mesa_fprint_program_parameters(stdout, ctx, prog);
824}
825
826
827/**
828 * Print a program parameter list to given file.
829 */
830static void
831_mesa_fprint_parameter_list(FILE *f,
832 const struct gl_program_parameter_list *list)
Brian83ca3ff2006-12-20 17:17:38 -0700833{
Brian501ee872007-02-17 09:15:00 -0700834 const gl_prog_print_mode mode = PROG_PRINT_DEBUG;
Brian83ca3ff2006-12-20 17:17:38 -0700835 GLuint i;
Brian501ee872007-02-17 09:15:00 -0700836
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600837 if (!list)
838 return;
839
Brian Pauld0038932009-01-22 10:34:43 -0700840 _mesa_fprintf(f, "param list %p\n", (void *) list);
Brian83ca3ff2006-12-20 17:17:38 -0700841 for (i = 0; i < list->NumParameters; i++){
842 struct gl_program_parameter *param = list->Parameters + i;
843 const GLfloat *v = list->ParameterValues[i];
Brian Pauld0038932009-01-22 10:34:43 -0700844 _mesa_fprintf(f, "param[%d] sz=%d %s %s = {%.3g, %.3g, %.3g, %.3g}",
Brian83ca3ff2006-12-20 17:17:38 -0700845 i, param->Size,
Brian501ee872007-02-17 09:15:00 -0700846 file_string(list->Parameters[i].Type, mode),
Brian00cdc0a2006-12-14 15:01:06 -0700847 param->Name, v[0], v[1], v[2], v[3]);
Brian Paulf490ec92008-11-24 09:04:52 -0700848 if (param->Flags & PROG_PARAM_BIT_CENTROID)
Brian Pauld0038932009-01-22 10:34:43 -0700849 _mesa_fprintf(f, " Centroid");
Brian Paulf490ec92008-11-24 09:04:52 -0700850 if (param->Flags & PROG_PARAM_BIT_INVARIANT)
Brian Pauld0038932009-01-22 10:34:43 -0700851 _mesa_fprintf(f, " Invariant");
Brian Paulf490ec92008-11-24 09:04:52 -0700852 if (param->Flags & PROG_PARAM_BIT_FLAT)
Brian Pauld0038932009-01-22 10:34:43 -0700853 _mesa_fprintf(f, " Flat");
Brian Paulf490ec92008-11-24 09:04:52 -0700854 if (param->Flags & PROG_PARAM_BIT_LINEAR)
Brian Pauld0038932009-01-22 10:34:43 -0700855 _mesa_fprintf(f, " Linear");
856 _mesa_fprintf(f, "\n");
Brian00cdc0a2006-12-14 15:01:06 -0700857 }
858}
Brian Pauld0038932009-01-22 10:34:43 -0700859
860
861/**
862 * Print a program parameter list to stdout.
863 */
864void
865_mesa_print_parameter_list(const struct gl_program_parameter_list *list)
866{
867 _mesa_fprint_parameter_list(stdout, list);
868}
869
870
871/**
872 * Write shader and associated info to a file.
873 */
874void
875_mesa_write_shader_to_file(const struct gl_shader *shader)
876{
877 const char *type;
878 char filename[100];
879 FILE *f;
880
881 if (shader->Type == GL_FRAGMENT_SHADER)
882 type = "frag";
883 else
884 type = "vert";
885
886 snprintf(filename, strlen(filename), "shader_%u.%s", shader->Name, type);
887 f = fopen(filename, "w");
888 if (!f) {
889 fprintf(stderr, "Unable to open %s for writing\n", filename);
890 return;
891 }
892
893 fprintf(f, "/* Shader %u source */\n", shader->Name);
894 fputs(shader->Source, f);
895 fprintf(f, "\n");
896
897 fprintf(f, "/* Compile status: %s */\n",
898 shader->CompileStatus ? "ok" : "fail");
899 if (!shader->CompileStatus) {
900 fprintf(f, "/* Log Info: */\n");
901 fputs(shader->InfoLog, f);
902 }
903 else {
904 fprintf(f, "/* GPU code */\n");
905 _mesa_fprint_program_opt(f, shader->Program, PROG_PRINT_DEBUG, GL_TRUE);
906 }
907
908 fclose(f);
909}
910
911