blob: d0ed22e98e1cf3ba2aaf4e8567fdf924198678cf [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;
Brian Paul9a644402008-09-04 15:05:03 -0600256 char *state = _mesa_program_state_string(param->StateIndexes);
257 sprintf(str, state);
258 _mesa_free(state);
Brian501ee872007-02-17 09:15:00 -0700259 }
260 break;
261 case PROGRAM_ADDRESS:
262 sprintf(str, "A%d", index);
263 break;
264 default:
265 _mesa_problem(NULL, "bad file in reg_string()");
266 }
267 break;
268
269 case PROG_PRINT_NV:
270 switch (f) {
271 case PROGRAM_INPUT:
272 if (prog->Target == GL_VERTEX_PROGRAM_ARB)
273 sprintf(str, "v[%d]", index);
274 else
275 sprintf(str, "f[%d]", index);
276 break;
277 case PROGRAM_OUTPUT:
278 sprintf(str, "o[%d]", index);
279 break;
280 case PROGRAM_TEMPORARY:
281 sprintf(str, "R%d", index);
282 break;
283 case PROGRAM_ENV_PARAM:
284 sprintf(str, "c[%d]", index);
285 break;
286 case PROGRAM_VARYING: /* extension */
287 sprintf(str, "varying[%d]", index);
288 break;
289 case PROGRAM_UNIFORM: /* extension */
290 sprintf(str, "uniform[%d]", index);
291 break;
292 case PROGRAM_CONSTANT: /* extension */
293 sprintf(str, "constant[%d]", index);
294 break;
295 case PROGRAM_STATE_VAR: /* extension */
296 sprintf(str, "state[%d]", index);
297 break;
298 default:
299 _mesa_problem(NULL, "bad file in reg_string()");
300 }
301 break;
302
303 default:
304 _mesa_problem(NULL, "bad mode in reg_string()");
305 }
306
307 return str;
308}
309
310
311/**
Brian00cdc0a2006-12-14 15:01:06 -0700312 * Return a string representation of the given swizzle word.
313 * If extended is true, use extended (comma-separated) format.
Brian3a281532006-12-16 12:51:34 -0700314 * \param swizzle the swizzle field
315 * \param negateBase 4-bit negation vector
316 * \param extended if true, also allow 0, 1 values
Brian00cdc0a2006-12-14 15:01:06 -0700317 */
Brian059376c2007-02-22 17:45:32 -0700318const char *
319_mesa_swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
Brian00cdc0a2006-12-14 15:01:06 -0700320{
Brian Paul74a19b02008-07-18 19:46:19 -0600321 static const char swz[] = "xyzw01!?"; /* See SWIZZLE_x definitions */
Brian00cdc0a2006-12-14 15:01:06 -0700322 static char s[20];
323 GLuint i = 0;
324
325 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
326 return ""; /* no swizzle/negation */
327
328 if (!extended)
329 s[i++] = '.';
330
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600331 if (negateBase & NEGATE_X)
Brian00cdc0a2006-12-14 15:01:06 -0700332 s[i++] = '-';
333 s[i++] = swz[GET_SWZ(swizzle, 0)];
334
335 if (extended) {
336 s[i++] = ',';
337 }
338
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600339 if (negateBase & NEGATE_Y)
Brian00cdc0a2006-12-14 15:01:06 -0700340 s[i++] = '-';
341 s[i++] = swz[GET_SWZ(swizzle, 1)];
342
343 if (extended) {
344 s[i++] = ',';
345 }
346
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600347 if (negateBase & NEGATE_Z)
Brian00cdc0a2006-12-14 15:01:06 -0700348 s[i++] = '-';
349 s[i++] = swz[GET_SWZ(swizzle, 2)];
350
351 if (extended) {
352 s[i++] = ',';
353 }
354
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600355 if (negateBase & NEGATE_W)
Brian00cdc0a2006-12-14 15:01:06 -0700356 s[i++] = '-';
357 s[i++] = swz[GET_SWZ(swizzle, 3)];
358
359 s[i] = 0;
360 return s;
361}
362
363
Brian Paul511733b2008-07-02 12:40:03 -0600364void
365_mesa_print_swizzle(GLuint swizzle)
366{
367 if (swizzle == SWIZZLE_XYZW) {
368 _mesa_printf(".xyzw\n");
369 }
370 else {
Michal Krolf9c574d2008-07-15 11:44:47 +0200371 const char *s = _mesa_swizzle_string(swizzle, 0, 0);
Brian Paul511733b2008-07-02 12:40:03 -0600372 _mesa_printf("%s\n", s);
373 }
374}
375
376
Brian Paul610c2462008-11-13 16:37:52 -0700377const char *
378_mesa_writemask_string(GLuint writeMask)
Brian00cdc0a2006-12-14 15:01:06 -0700379{
380 static char s[10];
381 GLuint i = 0;
382
383 if (writeMask == WRITEMASK_XYZW)
384 return "";
385
386 s[i++] = '.';
387 if (writeMask & WRITEMASK_X)
388 s[i++] = 'x';
389 if (writeMask & WRITEMASK_Y)
390 s[i++] = 'y';
391 if (writeMask & WRITEMASK_Z)
392 s[i++] = 'z';
393 if (writeMask & WRITEMASK_W)
394 s[i++] = 'w';
395
396 s[i] = 0;
397 return s;
398}
399
Brian3a281532006-12-16 12:51:34 -0700400
Briand7508612007-03-28 11:01:09 -0600401const char *
402_mesa_condcode_string(GLuint condcode)
Brian3a281532006-12-16 12:51:34 -0700403{
404 switch (condcode) {
405 case COND_GT: return "GT";
406 case COND_EQ: return "EQ";
407 case COND_LT: return "LT";
408 case COND_UN: return "UN";
409 case COND_GE: return "GE";
410 case COND_LE: return "LE";
411 case COND_NE: return "NE";
412 case COND_TR: return "TR";
413 case COND_FL: return "FL";
414 default: return "cond???";
415 }
416}
417
418
Brian00cdc0a2006-12-14 15:01:06 -0700419static void
Brian Pauld0038932009-01-22 10:34:43 -0700420fprint_dst_reg(FILE * f,
421 const struct prog_dst_register *dstReg,
422 gl_prog_print_mode mode,
423 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700424{
Brian Pauld0038932009-01-22 10:34:43 -0700425 _mesa_fprintf(f, "%s%s",
426 reg_string((enum register_file) dstReg->File,
427 dstReg->Index, mode, dstReg->RelAddr, prog),
428 _mesa_writemask_string(dstReg->WriteMask));
Brian501ee872007-02-17 09:15:00 -0700429
Brian1936b252007-03-22 09:04:18 -0600430 if (dstReg->CondMask != COND_TR) {
Brian Pauld0038932009-01-22 10:34:43 -0700431 _mesa_fprintf(f, " (%s.%s)",
432 _mesa_condcode_string(dstReg->CondMask),
433 _mesa_swizzle_string(dstReg->CondSwizzle,
434 GL_FALSE, GL_FALSE));
Brian1936b252007-03-22 09:04:18 -0600435 }
436
Brian501ee872007-02-17 09:15:00 -0700437#if 0
Brian Pauld0038932009-01-22 10:34:43 -0700438 _mesa_fprintf(f, "%s[%d]%s",
Brian501ee872007-02-17 09:15:00 -0700439 file_string((enum register_file) dstReg->File, mode),
Brian00cdc0a2006-12-14 15:01:06 -0700440 dstReg->Index,
Brian Paul610c2462008-11-13 16:37:52 -0700441 _mesa_writemask_string(dstReg->WriteMask));
Brian501ee872007-02-17 09:15:00 -0700442#endif
Brian00cdc0a2006-12-14 15:01:06 -0700443}
444
Brian Pauld0038932009-01-22 10:34:43 -0700445
Brian00cdc0a2006-12-14 15:01:06 -0700446static void
Brian Pauld0038932009-01-22 10:34:43 -0700447fprint_src_reg(FILE *f,
448 const struct prog_src_register *srcReg,
449 gl_prog_print_mode mode,
450 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700451{
Brian Pauld0038932009-01-22 10:34:43 -0700452 _mesa_fprintf(f, "%s%s",
453 reg_string((enum register_file) srcReg->File,
454 srcReg->Index, mode, srcReg->RelAddr, prog),
455 _mesa_swizzle_string(srcReg->Swizzle,
456 srcReg->NegateBase, GL_FALSE));
Brian501ee872007-02-17 09:15:00 -0700457#if 0
Brian Pauld0038932009-01-22 10:34:43 -0700458 _mesa_fprintf(f, "%s[%d]%s",
459 file_string((enum register_file) srcReg->File, mode),
460 srcReg->Index,
461 _mesa_swizzle_string(srcReg->Swizzle,
462 srcReg->NegateBase, GL_FALSE));
Brian501ee872007-02-17 09:15:00 -0700463#endif
Brian00cdc0a2006-12-14 15:01:06 -0700464}
465
Brian Pauld0038932009-01-22 10:34:43 -0700466
Brian00cdc0a2006-12-14 15:01:06 -0700467static void
Brian Pauld0038932009-01-22 10:34:43 -0700468fprint_comment(FILE *f, const struct prog_instruction *inst)
Brian00cdc0a2006-12-14 15:01:06 -0700469{
470 if (inst->Comment)
Brian Pauld0038932009-01-22 10:34:43 -0700471 _mesa_fprintf(f, "; # %s\n", inst->Comment);
Brian00cdc0a2006-12-14 15:01:06 -0700472 else
Brian Pauld0038932009-01-22 10:34:43 -0700473 _mesa_fprintf(f, ";\n");
Brian00cdc0a2006-12-14 15:01:06 -0700474}
475
476
Brian501ee872007-02-17 09:15:00 -0700477static void
Brian Pauld0038932009-01-22 10:34:43 -0700478fprint_alu_instruction(FILE *f,
479 const struct prog_instruction *inst,
480 const char *opcode_string, GLuint numRegs,
481 gl_prog_print_mode mode,
482 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700483{
484 GLuint j;
485
Brian Pauld0038932009-01-22 10:34:43 -0700486 _mesa_fprintf(f, "%s", opcode_string);
Brianb50280e2006-12-18 16:21:58 -0700487 if (inst->CondUpdate)
Brian Pauld0038932009-01-22 10:34:43 -0700488 _mesa_fprintf(f, ".C");
Brian00cdc0a2006-12-14 15:01:06 -0700489
490 /* frag prog only */
491 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Pauld0038932009-01-22 10:34:43 -0700492 _mesa_fprintf(f, "_SAT");
Brian00cdc0a2006-12-14 15:01:06 -0700493
Brian Pauld0038932009-01-22 10:34:43 -0700494 _mesa_fprintf(f, " ");
Brian00cdc0a2006-12-14 15:01:06 -0700495 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
Brian Pauld0038932009-01-22 10:34:43 -0700496 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700497 }
498 else {
Brian Pauld0038932009-01-22 10:34:43 -0700499 _mesa_fprintf(f, " ???");
Brian00cdc0a2006-12-14 15:01:06 -0700500 }
501
502 if (numRegs > 0)
Brian Pauld0038932009-01-22 10:34:43 -0700503 _mesa_fprintf(f, ", ");
Brian00cdc0a2006-12-14 15:01:06 -0700504
505 for (j = 0; j < numRegs; j++) {
Brian Pauld0038932009-01-22 10:34:43 -0700506 fprint_src_reg(f, inst->SrcReg + j, mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700507 if (j + 1 < numRegs)
Brian Pauld0038932009-01-22 10:34:43 -0700508 _mesa_fprintf(f, ", ");
Brian00cdc0a2006-12-14 15:01:06 -0700509 }
510
Brian Pauld0038932009-01-22 10:34:43 -0700511 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700512}
513
514
Brian501ee872007-02-17 09:15:00 -0700515void
Brian0020d102007-02-23 11:43:44 -0700516_mesa_print_alu_instruction(const struct prog_instruction *inst,
517 const char *opcode_string, GLuint numRegs)
518{
Brian Pauld0038932009-01-22 10:34:43 -0700519 fprint_alu_instruction(stdout, inst, opcode_string,
520 numRegs, PROG_PRINT_DEBUG, NULL);
Brian501ee872007-02-17 09:15:00 -0700521}
522
523
Brian00cdc0a2006-12-14 15:01:06 -0700524/**
525 * Print a single vertex/fragment program instruction.
526 */
Brian Pauld0038932009-01-22 10:34:43 -0700527static GLint
528_mesa_fprint_instruction_opt(FILE *f,
529 const struct prog_instruction *inst,
530 GLint indent,
Brian501ee872007-02-17 09:15:00 -0700531 gl_prog_print_mode mode,
532 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700533{
Brian7b300532007-02-22 09:08:36 -0700534 GLint i;
Brian5db088d2007-02-05 14:58:15 -0700535
536 if (inst->Opcode == OPCODE_ELSE ||
537 inst->Opcode == OPCODE_ENDIF ||
538 inst->Opcode == OPCODE_ENDLOOP ||
539 inst->Opcode == OPCODE_ENDSUB) {
540 indent -= 3;
541 }
Brian5db088d2007-02-05 14:58:15 -0700542 for (i = 0; i < indent; i++) {
Brian Pauld0038932009-01-22 10:34:43 -0700543 _mesa_fprintf(f, " ");
Brian5db088d2007-02-05 14:58:15 -0700544 }
545
Brian00cdc0a2006-12-14 15:01:06 -0700546 switch (inst->Opcode) {
547 case OPCODE_PRINT:
Brian Pauld0038932009-01-22 10:34:43 -0700548 _mesa_fprintf(f, "PRINT '%s'", inst->Data);
Brian00cdc0a2006-12-14 15:01:06 -0700549 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
Brian Pauld0038932009-01-22 10:34:43 -0700550 _mesa_fprintf(f, ", ");
551 _mesa_fprintf(f, "%s[%d]%s",
Brian501ee872007-02-17 09:15:00 -0700552 file_string((enum register_file) inst->SrcReg[0].File,
553 mode),
Brian00cdc0a2006-12-14 15:01:06 -0700554 inst->SrcReg[0].Index,
Brian059376c2007-02-22 17:45:32 -0700555 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
556 inst->SrcReg[0].NegateBase, GL_FALSE));
Brian00cdc0a2006-12-14 15:01:06 -0700557 }
558 if (inst->Comment)
Brian Pauld0038932009-01-22 10:34:43 -0700559 _mesa_fprintf(f, " # %s", inst->Comment);
560 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700561 break;
562 case OPCODE_SWZ:
Brian Pauld0038932009-01-22 10:34:43 -0700563 _mesa_fprintf(f, "SWZ");
Brian00cdc0a2006-12-14 15:01:06 -0700564 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Pauld0038932009-01-22 10:34:43 -0700565 _mesa_fprintf(f, "_SAT");
566 _mesa_fprintf(f, " ");
567 fprint_dst_reg(f, &inst->DstReg, mode, prog);
568 _mesa_fprintf(f, ", %s[%d], %s",
Brian501ee872007-02-17 09:15:00 -0700569 file_string((enum register_file) inst->SrcReg[0].File,
570 mode),
Brian00cdc0a2006-12-14 15:01:06 -0700571 inst->SrcReg[0].Index,
Brian059376c2007-02-22 17:45:32 -0700572 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
573 inst->SrcReg[0].NegateBase, GL_TRUE));
Brian Pauld0038932009-01-22 10:34:43 -0700574 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700575 break;
576 case OPCODE_TEX:
577 case OPCODE_TXP:
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600578 case OPCODE_TXL:
Brian00cdc0a2006-12-14 15:01:06 -0700579 case OPCODE_TXB:
Brian Pauld0038932009-01-22 10:34:43 -0700580 _mesa_fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
Brian00cdc0a2006-12-14 15:01:06 -0700581 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Brian Pauld0038932009-01-22 10:34:43 -0700582 _mesa_fprintf(f, "_SAT");
583 _mesa_fprintf(f, " ");
584 fprint_dst_reg(f, &inst->DstReg, mode, prog);
585 _mesa_fprintf(f, ", ");
586 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
587 _mesa_fprintf(f, ", texture[%d], ", inst->TexSrcUnit);
Brian00cdc0a2006-12-14 15:01:06 -0700588 switch (inst->TexSrcTarget) {
Brian Pauld0038932009-01-22 10:34:43 -0700589 case TEXTURE_1D_INDEX: _mesa_fprintf(f, "1D"); break;
590 case TEXTURE_2D_INDEX: _mesa_fprintf(f, "2D"); break;
591 case TEXTURE_3D_INDEX: _mesa_fprintf(f, "3D"); break;
592 case TEXTURE_CUBE_INDEX: _mesa_fprintf(f, "CUBE"); break;
593 case TEXTURE_RECT_INDEX: _mesa_fprintf(f, "RECT"); break;
Brian00cdc0a2006-12-14 15:01:06 -0700594 default:
595 ;
596 }
Brian Pauld0038932009-01-22 10:34:43 -0700597 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700598 break;
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600599
600 case OPCODE_KIL:
Brian Pauld0038932009-01-22 10:34:43 -0700601 _mesa_fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
602 _mesa_fprintf(f, " ");
603 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
604 fprint_comment(f, inst);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600605 break;
606 case OPCODE_KIL_NV:
Brian Pauld0038932009-01-22 10:34:43 -0700607 _mesa_fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
608 _mesa_fprintf(f, " ");
609 _mesa_fprintf(f, "%s.%s",
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600610 _mesa_condcode_string(inst->DstReg.CondMask),
611 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
612 GL_FALSE, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700613 fprint_comment(f, inst);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600614 break;
615
Brian00cdc0a2006-12-14 15:01:06 -0700616 case OPCODE_ARL:
Brian Pauld0038932009-01-22 10:34:43 -0700617 _mesa_fprintf(f, "ARL ");
618 fprint_dst_reg(f, &inst->DstReg, mode, prog);
619 _mesa_fprintf(f, ", ");
620 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
621 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700622 break;
623 case OPCODE_BRA:
Brian Pauld0038932009-01-22 10:34:43 -0700624 _mesa_fprintf(f, "BRA %d (%s%s)",
Brian3a281532006-12-16 12:51:34 -0700625 inst->BranchTarget,
Briand7508612007-03-28 11:01:09 -0600626 _mesa_condcode_string(inst->DstReg.CondMask),
Brian059376c2007-02-22 17:45:32 -0700627 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700628 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700629 break;
Brian5ae49cf2007-01-20 09:27:40 -0700630 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600631 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
632 /* Use ordinary register */
Brian Pauld0038932009-01-22 10:34:43 -0700633 _mesa_fprintf(f, "IF ");
634 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
635 _mesa_fprintf(f, "; ");
Brian63556fa2007-03-23 14:47:46 -0600636 }
637 else {
638 /* Use cond codes */
Brian Pauld0038932009-01-22 10:34:43 -0700639 _mesa_fprintf(f, "IF (%s%s);",
Briand7508612007-03-28 11:01:09 -0600640 _mesa_condcode_string(inst->DstReg.CondMask),
Brian63556fa2007-03-23 14:47:46 -0600641 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
642 0, GL_FALSE));
643 }
Brian Pauld0038932009-01-22 10:34:43 -0700644 _mesa_fprintf(f, " # (if false, goto %d)", inst->BranchTarget);
645 fprint_comment(f, inst);
Brian5db088d2007-02-05 14:58:15 -0700646 return indent + 3;
647 case OPCODE_ELSE:
Brian Pauld0038932009-01-22 10:34:43 -0700648 _mesa_fprintf(f, "ELSE; # (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700649 return indent + 3;
650 case OPCODE_ENDIF:
Brian Pauld0038932009-01-22 10:34:43 -0700651 _mesa_fprintf(f, "ENDIF;\n");
Brian5db088d2007-02-05 14:58:15 -0700652 break;
653 case OPCODE_BGNLOOP:
Brian Pauld0038932009-01-22 10:34:43 -0700654 _mesa_fprintf(f, "BGNLOOP; # (end at %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700655 return indent + 3;
656 case OPCODE_ENDLOOP:
Brian Pauld0038932009-01-22 10:34:43 -0700657 _mesa_fprintf(f, "ENDLOOP; # (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700658 break;
659 case OPCODE_BRK:
Brianf22ed092007-02-06 22:31:19 -0700660 case OPCODE_CONT:
Brian Pauld0038932009-01-22 10:34:43 -0700661 _mesa_fprintf(f, "%s (%s%s); # (goto %d)",
Brian81767ee2007-03-23 17:45:53 -0600662 _mesa_opcode_string(inst->Opcode),
Briand7508612007-03-28 11:01:09 -0600663 _mesa_condcode_string(inst->DstReg.CondMask),
Brian059376c2007-02-22 17:45:32 -0700664 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
Brian2755c792007-02-05 18:01:02 -0700665 inst->BranchTarget);
Brian Pauld0038932009-01-22 10:34:43 -0700666 fprint_comment(f, inst);
Brian5ae49cf2007-01-20 09:27:40 -0700667 break;
Brian63556fa2007-03-23 14:47:46 -0600668
Brian5db088d2007-02-05 14:58:15 -0700669 case OPCODE_BGNSUB:
Brianf407cad2007-03-27 14:53:17 -0600670 if (mode == PROG_PRINT_NV) {
Brian Pauld0038932009-01-22 10:34:43 -0700671 _mesa_fprintf(f, "%s:\n", inst->Comment); /* comment is label */
Brianf407cad2007-03-27 14:53:17 -0600672 return indent;
673 }
674 else {
Brian Pauld0038932009-01-22 10:34:43 -0700675 _mesa_fprintf(f, "BGNSUB");
676 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600677 return indent + 3;
678 }
Brian5db088d2007-02-05 14:58:15 -0700679 case OPCODE_ENDSUB:
Brianf407cad2007-03-27 14:53:17 -0600680 if (mode == PROG_PRINT_DEBUG) {
Brian Pauld0038932009-01-22 10:34:43 -0700681 _mesa_fprintf(f, "ENDSUB");
682 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600683 }
684 break;
685 case OPCODE_CAL:
686 if (mode == PROG_PRINT_NV) {
Brian Pauld0038932009-01-22 10:34:43 -0700687 _mesa_fprintf(f, "CAL %s; # (goto %d)\n", inst->Comment, inst->BranchTarget);
Brianf407cad2007-03-27 14:53:17 -0600688 }
689 else {
Brian Pauld0038932009-01-22 10:34:43 -0700690 _mesa_fprintf(f, "CAL %u", inst->BranchTarget);
691 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600692 }
693 break;
694 case OPCODE_RET:
Brian Pauld0038932009-01-22 10:34:43 -0700695 _mesa_fprintf(f, "RET (%s%s)",
Briand7508612007-03-28 11:01:09 -0600696 _mesa_condcode_string(inst->DstReg.CondMask),
697 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700698 fprint_comment(f, inst);
Brian5ae49cf2007-01-20 09:27:40 -0700699 break;
Brianf407cad2007-03-27 14:53:17 -0600700
Brian00cdc0a2006-12-14 15:01:06 -0700701 case OPCODE_END:
Brian Pauld0038932009-01-22 10:34:43 -0700702 _mesa_fprintf(f, "END\n");
Brian00cdc0a2006-12-14 15:01:06 -0700703 break;
Brian3a281532006-12-16 12:51:34 -0700704 case OPCODE_NOP:
Brian059376c2007-02-22 17:45:32 -0700705 if (mode == PROG_PRINT_DEBUG) {
Brian Pauld0038932009-01-22 10:34:43 -0700706 _mesa_fprintf(f, "NOP");
707 fprint_comment(f, inst);
Brian059376c2007-02-22 17:45:32 -0700708 }
709 else if (inst->Comment) {
710 /* ARB/NV extensions don't have NOP instruction */
Brian Pauld0038932009-01-22 10:34:43 -0700711 _mesa_fprintf(f, "# %s\n", inst->Comment);
Brian059376c2007-02-22 17:45:32 -0700712 }
Brian3a281532006-12-16 12:51:34 -0700713 break;
Brian00cdc0a2006-12-14 15:01:06 -0700714 /* XXX may need other special-case instructions */
715 default:
716 /* typical alu instruction */
Brian Pauld0038932009-01-22 10:34:43 -0700717 fprint_alu_instruction(f, inst,
718 _mesa_opcode_string(inst->Opcode),
719 _mesa_num_inst_src_regs(inst->Opcode),
720 mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700721 break;
722 }
Brian5db088d2007-02-05 14:58:15 -0700723 return indent;
Brian00cdc0a2006-12-14 15:01:06 -0700724}
725
726
Brian Pauld0038932009-01-22 10:34:43 -0700727GLint
728_mesa_print_instruction_opt(const struct prog_instruction *inst,
729 GLint indent,
730 gl_prog_print_mode mode,
731 const struct gl_program *prog)
732{
733 return _mesa_fprint_instruction_opt(stdout, inst, indent, mode, prog);
734}
735
736
737void
738_mesa_print_instruction(const struct prog_instruction *inst)
739{
740 /* note: 4th param should be ignored for PROG_PRINT_DEBUG */
741 _mesa_fprint_instruction_opt(stdout, inst, 0, PROG_PRINT_DEBUG, NULL);
742}
743
744
745
746/**
747 * Print program, with options.
748 */
Brian Pauld2eff332009-02-02 12:24:41 -0700749void
Brian Pauld0038932009-01-22 10:34:43 -0700750_mesa_fprint_program_opt(FILE *f,
751 const struct gl_program *prog,
752 gl_prog_print_mode mode,
753 GLboolean lineNumbers)
754{
755 GLuint i, indent = 0;
756
757 switch (prog->Target) {
758 case GL_VERTEX_PROGRAM_ARB:
759 if (mode == PROG_PRINT_ARB)
760 _mesa_fprintf(f, "!!ARBvp1.0\n");
761 else if (mode == PROG_PRINT_NV)
762 _mesa_fprintf(f, "!!VP1.0\n");
763 else
764 _mesa_fprintf(f, "# Vertex Program/Shader\n");
765 break;
766 case GL_FRAGMENT_PROGRAM_ARB:
767 case GL_FRAGMENT_PROGRAM_NV:
768 if (mode == PROG_PRINT_ARB)
769 _mesa_fprintf(f, "!!ARBfp1.0\n");
770 else if (mode == PROG_PRINT_NV)
771 _mesa_fprintf(f, "!!FP1.0\n");
772 else
773 _mesa_fprintf(f, "# Fragment Program/Shader\n");
774 break;
775 }
776
777 for (i = 0; i < prog->NumInstructions; i++) {
778 if (lineNumbers)
779 _mesa_fprintf(f, "%3d: ", i);
780 indent = _mesa_fprint_instruction_opt(f, prog->Instructions + i,
781 indent, mode, prog);
782 }
783}
784
785
Brian00cdc0a2006-12-14 15:01:06 -0700786/**
Brian501ee872007-02-17 09:15:00 -0700787 * Print program to stdout, default options.
Brian00cdc0a2006-12-14 15:01:06 -0700788 */
789void
790_mesa_print_program(const struct gl_program *prog)
791{
Brian Pauld0038932009-01-22 10:34:43 -0700792 _mesa_fprint_program_opt(stdout, prog, PROG_PRINT_DEBUG, GL_TRUE);
Brian501ee872007-02-17 09:15:00 -0700793}
794
795
796/**
Brian Pauld0038932009-01-22 10:34:43 -0700797 * Print all of a program's parameters/fields to given file.
Brian501ee872007-02-17 09:15:00 -0700798 */
Brian Pauld0038932009-01-22 10:34:43 -0700799static void
800_mesa_fprint_program_parameters(FILE *f,
801 GLcontext *ctx,
802 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700803{
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600804 GLuint i;
805
Brian Pauld0038932009-01-22 10:34:43 -0700806 _mesa_fprintf(f, "InputsRead: 0x%x\n", prog->InputsRead);
807 _mesa_fprintf(f, "OutputsWritten: 0x%x\n", prog->OutputsWritten);
808 _mesa_fprintf(f, "NumInstructions=%d\n", prog->NumInstructions);
809 _mesa_fprintf(f, "NumTemporaries=%d\n", prog->NumTemporaries);
810 _mesa_fprintf(f, "NumParameters=%d\n", prog->NumParameters);
811 _mesa_fprintf(f, "NumAttributes=%d\n", prog->NumAttributes);
812 _mesa_fprintf(f, "NumAddressRegs=%d\n", prog->NumAddressRegs);
813 _mesa_fprintf(f, "Samplers=[ ");
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600814 for (i = 0; i < MAX_SAMPLERS; i++) {
Brian Pauld0038932009-01-22 10:34:43 -0700815 _mesa_fprintf(f, "%d ", prog->SamplerUnits[i]);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600816 }
Brian Pauld0038932009-01-22 10:34:43 -0700817 _mesa_fprintf(f, "]\n");
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600818
Brian00cdc0a2006-12-14 15:01:06 -0700819 _mesa_load_state_parameters(ctx, prog->Parameters);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600820
Brian00cdc0a2006-12-14 15:01:06 -0700821#if 0
Brian Pauld0038932009-01-22 10:34:43 -0700822 _mesa_fprintf(f, "Local Params:\n");
Brian00cdc0a2006-12-14 15:01:06 -0700823 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
824 const GLfloat *p = prog->LocalParams[i];
Brian Pauld0038932009-01-22 10:34:43 -0700825 _mesa_fprintf(f, "%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
Brian00cdc0a2006-12-14 15:01:06 -0700826 }
827#endif
Brian83ca3ff2006-12-20 17:17:38 -0700828 _mesa_print_parameter_list(prog->Parameters);
829}
Brian00cdc0a2006-12-14 15:01:06 -0700830
Brian83ca3ff2006-12-20 17:17:38 -0700831
Brian Pauld0038932009-01-22 10:34:43 -0700832/**
833 * Print all of a program's parameters/fields to stdout.
834 */
Brian83ca3ff2006-12-20 17:17:38 -0700835void
Brian Pauld0038932009-01-22 10:34:43 -0700836_mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
837{
838 _mesa_fprint_program_parameters(stdout, ctx, prog);
839}
840
841
842/**
843 * Print a program parameter list to given file.
844 */
845static void
846_mesa_fprint_parameter_list(FILE *f,
847 const struct gl_program_parameter_list *list)
Brian83ca3ff2006-12-20 17:17:38 -0700848{
Brian501ee872007-02-17 09:15:00 -0700849 const gl_prog_print_mode mode = PROG_PRINT_DEBUG;
Brian83ca3ff2006-12-20 17:17:38 -0700850 GLuint i;
Brian501ee872007-02-17 09:15:00 -0700851
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600852 if (!list)
853 return;
854
Brian Pauld0038932009-01-22 10:34:43 -0700855 _mesa_fprintf(f, "param list %p\n", (void *) list);
Brian83ca3ff2006-12-20 17:17:38 -0700856 for (i = 0; i < list->NumParameters; i++){
857 struct gl_program_parameter *param = list->Parameters + i;
858 const GLfloat *v = list->ParameterValues[i];
Brian Pauld0038932009-01-22 10:34:43 -0700859 _mesa_fprintf(f, "param[%d] sz=%d %s %s = {%.3g, %.3g, %.3g, %.3g}",
Brian83ca3ff2006-12-20 17:17:38 -0700860 i, param->Size,
Brian501ee872007-02-17 09:15:00 -0700861 file_string(list->Parameters[i].Type, mode),
Brian00cdc0a2006-12-14 15:01:06 -0700862 param->Name, v[0], v[1], v[2], v[3]);
Brian Paulf490ec92008-11-24 09:04:52 -0700863 if (param->Flags & PROG_PARAM_BIT_CENTROID)
Brian Pauld0038932009-01-22 10:34:43 -0700864 _mesa_fprintf(f, " Centroid");
Brian Paulf490ec92008-11-24 09:04:52 -0700865 if (param->Flags & PROG_PARAM_BIT_INVARIANT)
Brian Pauld0038932009-01-22 10:34:43 -0700866 _mesa_fprintf(f, " Invariant");
Brian Paulf490ec92008-11-24 09:04:52 -0700867 if (param->Flags & PROG_PARAM_BIT_FLAT)
Brian Pauld0038932009-01-22 10:34:43 -0700868 _mesa_fprintf(f, " Flat");
Brian Paulf490ec92008-11-24 09:04:52 -0700869 if (param->Flags & PROG_PARAM_BIT_LINEAR)
Brian Pauld0038932009-01-22 10:34:43 -0700870 _mesa_fprintf(f, " Linear");
871 _mesa_fprintf(f, "\n");
Brian00cdc0a2006-12-14 15:01:06 -0700872 }
873}
Brian Pauld0038932009-01-22 10:34:43 -0700874
875
876/**
877 * Print a program parameter list to stdout.
878 */
879void
880_mesa_print_parameter_list(const struct gl_program_parameter_list *list)
881{
882 _mesa_fprint_parameter_list(stdout, list);
883}
884
885
886/**
887 * Write shader and associated info to a file.
888 */
889void
890_mesa_write_shader_to_file(const struct gl_shader *shader)
891{
892 const char *type;
893 char filename[100];
894 FILE *f;
895
896 if (shader->Type == GL_FRAGMENT_SHADER)
897 type = "frag";
898 else
899 type = "vert";
900
901 snprintf(filename, strlen(filename), "shader_%u.%s", shader->Name, type);
902 f = fopen(filename, "w");
903 if (!f) {
904 fprintf(stderr, "Unable to open %s for writing\n", filename);
905 return;
906 }
907
908 fprintf(f, "/* Shader %u source */\n", shader->Name);
909 fputs(shader->Source, f);
910 fprintf(f, "\n");
911
912 fprintf(f, "/* Compile status: %s */\n",
913 shader->CompileStatus ? "ok" : "fail");
914 if (!shader->CompileStatus) {
915 fprintf(f, "/* Log Info: */\n");
916 fputs(shader->InfoLog, f);
917 }
918 else {
919 fprintf(f, "/* GPU code */\n");
Brian Paul6ce0c6e2009-02-06 10:20:33 -0700920 fprintf(f, "/*\n");
Brian Pauld0038932009-01-22 10:34:43 -0700921 _mesa_fprint_program_opt(f, shader->Program, PROG_PRINT_DEBUG, GL_TRUE);
Brian Paul6ce0c6e2009-02-06 10:20:33 -0700922 fprintf(f, "*/\n");
Brian Pauld0038932009-01-22 10:34:43 -0700923 }
924
925 fclose(f);
926}
927
928