blob: 765aa003b769f5f435dd8c37a899c697ce6a9dee [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 *
Brian Paulb4026d92009-03-07 12:02:52 -070046file_string(gl_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:
Brian Paul7c2fe422009-05-11 09:38:32 -060078 {
79 static char s[20];
Brian Paul78a0c352010-02-19 12:56:49 -070080 _mesa_snprintf(s, sizeof(s), "FILE%u", f);
Brian Paul7c2fe422009-05-11 09:38:32 -060081 return s;
82 }
Brian00cdc0a2006-12-14 15:01:06 -070083 }
84}
85
86
87/**
Brian501ee872007-02-17 09:15:00 -070088 * Return ARB_v/f_prog-style input attrib string.
89 */
90static const char *
91arb_input_attrib_string(GLint index, GLenum progType)
92{
Brian Paula0709372009-02-27 13:44:42 -070093 /*
94 * These strings should match the VERT_ATTRIB_x and FRAG_ATTRIB_x tokens.
95 */
Brian501ee872007-02-17 09:15:00 -070096 const char *vertAttribs[] = {
97 "vertex.position",
98 "vertex.weight",
99 "vertex.normal",
100 "vertex.color.primary",
101 "vertex.color.secondary",
102 "vertex.fogcoord",
103 "vertex.(six)",
104 "vertex.(seven)",
105 "vertex.texcoord[0]",
106 "vertex.texcoord[1]",
107 "vertex.texcoord[2]",
108 "vertex.texcoord[3]",
109 "vertex.texcoord[4]",
110 "vertex.texcoord[5]",
111 "vertex.texcoord[6]",
Markus Amslerd7878502008-03-17 12:11:11 +0100112 "vertex.texcoord[7]",
113 "vertex.attrib[0]",
114 "vertex.attrib[1]",
115 "vertex.attrib[2]",
116 "vertex.attrib[3]",
117 "vertex.attrib[4]",
118 "vertex.attrib[5]",
119 "vertex.attrib[6]",
120 "vertex.attrib[7]",
121 "vertex.attrib[8]",
122 "vertex.attrib[9]",
123 "vertex.attrib[10]",
124 "vertex.attrib[11]",
125 "vertex.attrib[12]",
126 "vertex.attrib[13]",
127 "vertex.attrib[14]",
128 "vertex.attrib[15]"
Brian501ee872007-02-17 09:15:00 -0700129 };
130 const char *fragAttribs[] = {
131 "fragment.position",
132 "fragment.color.primary",
133 "fragment.color.secondary",
134 "fragment.fogcoord",
135 "fragment.texcoord[0]",
136 "fragment.texcoord[1]",
137 "fragment.texcoord[2]",
138 "fragment.texcoord[3]",
139 "fragment.texcoord[4]",
140 "fragment.texcoord[5]",
141 "fragment.texcoord[6]",
142 "fragment.texcoord[7]",
143 "fragment.varying[0]",
144 "fragment.varying[1]",
145 "fragment.varying[2]",
146 "fragment.varying[3]",
147 "fragment.varying[4]",
148 "fragment.varying[5]",
149 "fragment.varying[6]",
150 "fragment.varying[7]"
151 };
152
Brian Paul12ffee52010-02-01 13:03:25 -0700153 /* sanity checks */
154 assert(strcmp(vertAttribs[VERT_ATTRIB_TEX0], "vertex.texcoord[0]") == 0);
155 assert(strcmp(vertAttribs[VERT_ATTRIB_GENERIC15], "vertex.attrib[15]") == 0);
156
Brian501ee872007-02-17 09:15:00 -0700157 if (progType == GL_VERTEX_PROGRAM_ARB) {
158 assert(index < sizeof(vertAttribs) / sizeof(vertAttribs[0]));
159 return vertAttribs[index];
160 }
161 else {
162 assert(index < sizeof(fragAttribs) / sizeof(fragAttribs[0]));
163 return fragAttribs[index];
164 }
165}
166
167
168/**
Brian Paul12ffee52010-02-01 13:03:25 -0700169 * Print a vertex program's InputsRead field in human-readable format.
170 * For debugging.
171 */
172void
173_mesa_print_vp_inputs(GLbitfield inputs)
174{
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500175 printf("VP Inputs 0x%x: \n", inputs);
Brian Paul12ffee52010-02-01 13:03:25 -0700176 while (inputs) {
177 GLint attr = _mesa_ffs(inputs) - 1;
178 const char *name = arb_input_attrib_string(attr,
179 GL_VERTEX_PROGRAM_ARB);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500180 printf(" %d: %s\n", attr, name);
Brian Paul12ffee52010-02-01 13:03:25 -0700181 inputs &= ~(1 << attr);
182 }
183}
184
185
186/**
187 * Print a fragment program's InputsRead field in human-readable format.
188 * For debugging.
189 */
190void
191_mesa_print_fp_inputs(GLbitfield inputs)
192{
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500193 printf("FP Inputs 0x%x: \n", inputs);
Brian Paul12ffee52010-02-01 13:03:25 -0700194 while (inputs) {
195 GLint attr = _mesa_ffs(inputs) - 1;
196 const char *name = arb_input_attrib_string(attr,
197 GL_FRAGMENT_PROGRAM_ARB);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500198 printf(" %d: %s\n", attr, name);
Brian Paul12ffee52010-02-01 13:03:25 -0700199 inputs &= ~(1 << attr);
200 }
201}
202
203
204
205/**
Brian501ee872007-02-17 09:15:00 -0700206 * Return ARB_v/f_prog-style output attrib string.
207 */
208static const char *
209arb_output_attrib_string(GLint index, GLenum progType)
210{
Brian Paula0709372009-02-27 13:44:42 -0700211 /*
212 * These strings should match the VERT_RESULT_x and FRAG_RESULT_x tokens.
213 */
Brian501ee872007-02-17 09:15:00 -0700214 const char *vertResults[] = {
215 "result.position",
216 "result.color.primary",
217 "result.color.secondary",
218 "result.fogcoord",
219 "result.texcoord[0]",
220 "result.texcoord[1]",
221 "result.texcoord[2]",
222 "result.texcoord[3]",
223 "result.texcoord[4]",
224 "result.texcoord[5]",
225 "result.texcoord[6]",
226 "result.texcoord[7]",
227 "result.varying[0]",
228 "result.varying[1]",
229 "result.varying[2]",
230 "result.varying[3]",
231 "result.varying[4]",
232 "result.varying[5]",
233 "result.varying[6]",
234 "result.varying[7]"
235 };
236 const char *fragResults[] = {
237 "result.color",
Brian Paula0709372009-02-27 13:44:42 -0700238 "result.color(half)",
239 "result.depth",
240 "result.color[0]",
241 "result.color[1]",
242 "result.color[2]",
243 "result.color[3]"
Brian501ee872007-02-17 09:15:00 -0700244 };
245
246 if (progType == GL_VERTEX_PROGRAM_ARB) {
247 assert(index < sizeof(vertResults) / sizeof(vertResults[0]));
248 return vertResults[index];
249 }
250 else {
251 assert(index < sizeof(fragResults) / sizeof(fragResults[0]));
252 return fragResults[index];
253 }
254}
255
256
257/**
258 * Return string representation of the given register.
259 * Note that some types of registers (like PROGRAM_UNIFORM) aren't defined
260 * by the ARB/NV program languages so we've taken some liberties here.
Vinson Lee1eee1ba2009-03-17 09:34:30 -0600261 * \param f the register file (PROGRAM_INPUT, PROGRAM_TEMPORARY, etc)
Brian501ee872007-02-17 09:15:00 -0700262 * \param index number of the register in the register file
263 * \param mode the output format/mode/style
264 * \param prog pointer to containing program
265 */
266static const char *
Brian Paulb4026d92009-03-07 12:02:52 -0700267reg_string(gl_register_file f, GLint index, gl_prog_print_mode mode,
Zack Rusin19659a52008-06-12 14:19:10 -0400268 GLboolean relAddr, const struct gl_program *prog)
Brian501ee872007-02-17 09:15:00 -0700269{
270 static char str[100];
Brian Paulf88a9012009-02-17 15:57:00 -0700271 const char *addr = relAddr ? "ADDR+" : "";
Brian501ee872007-02-17 09:15:00 -0700272
273 str[0] = 0;
274
275 switch (mode) {
276 case PROG_PRINT_DEBUG:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500277 sprintf(str, "%s[%s%d]", file_string(f, mode), addr, index);
Brian501ee872007-02-17 09:15:00 -0700278 break;
279
280 case PROG_PRINT_ARB:
281 switch (f) {
282 case PROGRAM_INPUT:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500283 sprintf(str, "%s", arb_input_attrib_string(index, prog->Target));
Brian501ee872007-02-17 09:15:00 -0700284 break;
285 case PROGRAM_OUTPUT:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500286 sprintf(str, "%s", arb_output_attrib_string(index, prog->Target));
Brian501ee872007-02-17 09:15:00 -0700287 break;
288 case PROGRAM_TEMPORARY:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500289 sprintf(str, "temp%d", index);
Brian501ee872007-02-17 09:15:00 -0700290 break;
291 case PROGRAM_ENV_PARAM:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500292 sprintf(str, "program.env[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700293 break;
294 case PROGRAM_LOCAL_PARAM:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500295 sprintf(str, "program.local[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700296 break;
297 case PROGRAM_VARYING: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500298 sprintf(str, "varying[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700299 break;
300 case PROGRAM_CONSTANT: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500301 sprintf(str, "constant[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700302 break;
303 case PROGRAM_UNIFORM: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500304 sprintf(str, "uniform[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700305 break;
306 case PROGRAM_STATE_VAR:
307 {
308 struct gl_program_parameter *param
309 = prog->Parameters->Parameters + index;
Brian Paul9a644402008-09-04 15:05:03 -0600310 char *state = _mesa_program_state_string(param->StateIndexes);
Vinson Leea45f2ec2010-02-19 23:53:27 -0800311 sprintf(str, "%s", state);
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500312 free(state);
Brian501ee872007-02-17 09:15:00 -0700313 }
314 break;
315 case PROGRAM_ADDRESS:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500316 sprintf(str, "A%d", index);
Brian501ee872007-02-17 09:15:00 -0700317 break;
318 default:
319 _mesa_problem(NULL, "bad file in reg_string()");
320 }
321 break;
322
323 case PROG_PRINT_NV:
324 switch (f) {
325 case PROGRAM_INPUT:
326 if (prog->Target == GL_VERTEX_PROGRAM_ARB)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500327 sprintf(str, "v[%d]", index);
Brian501ee872007-02-17 09:15:00 -0700328 else
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500329 sprintf(str, "f[%d]", index);
Brian501ee872007-02-17 09:15:00 -0700330 break;
331 case PROGRAM_OUTPUT:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500332 sprintf(str, "o[%d]", index);
Brian501ee872007-02-17 09:15:00 -0700333 break;
334 case PROGRAM_TEMPORARY:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500335 sprintf(str, "R%d", index);
Brian501ee872007-02-17 09:15:00 -0700336 break;
337 case PROGRAM_ENV_PARAM:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500338 sprintf(str, "c[%d]", index);
Brian501ee872007-02-17 09:15:00 -0700339 break;
340 case PROGRAM_VARYING: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500341 sprintf(str, "varying[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700342 break;
343 case PROGRAM_UNIFORM: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500344 sprintf(str, "uniform[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700345 break;
346 case PROGRAM_CONSTANT: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500347 sprintf(str, "constant[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700348 break;
349 case PROGRAM_STATE_VAR: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500350 sprintf(str, "state[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700351 break;
352 default:
353 _mesa_problem(NULL, "bad file in reg_string()");
354 }
355 break;
356
357 default:
358 _mesa_problem(NULL, "bad mode in reg_string()");
359 }
360
361 return str;
362}
363
364
365/**
Brian00cdc0a2006-12-14 15:01:06 -0700366 * Return a string representation of the given swizzle word.
367 * If extended is true, use extended (comma-separated) format.
Brian3a281532006-12-16 12:51:34 -0700368 * \param swizzle the swizzle field
369 * \param negateBase 4-bit negation vector
370 * \param extended if true, also allow 0, 1 values
Brian00cdc0a2006-12-14 15:01:06 -0700371 */
Brian059376c2007-02-22 17:45:32 -0700372const char *
Brian Paul7db7ff82009-04-14 22:14:30 -0600373_mesa_swizzle_string(GLuint swizzle, GLuint negateMask, GLboolean extended)
Brian00cdc0a2006-12-14 15:01:06 -0700374{
Brian Paul74a19b02008-07-18 19:46:19 -0600375 static const char swz[] = "xyzw01!?"; /* See SWIZZLE_x definitions */
Brian00cdc0a2006-12-14 15:01:06 -0700376 static char s[20];
377 GLuint i = 0;
378
Brian Paul7db7ff82009-04-14 22:14:30 -0600379 if (!extended && swizzle == SWIZZLE_NOOP && negateMask == 0)
Brian00cdc0a2006-12-14 15:01:06 -0700380 return ""; /* no swizzle/negation */
381
382 if (!extended)
383 s[i++] = '.';
384
Brian Paul7db7ff82009-04-14 22:14:30 -0600385 if (negateMask & NEGATE_X)
Brian00cdc0a2006-12-14 15:01:06 -0700386 s[i++] = '-';
387 s[i++] = swz[GET_SWZ(swizzle, 0)];
388
389 if (extended) {
390 s[i++] = ',';
391 }
392
Brian Paul7db7ff82009-04-14 22:14:30 -0600393 if (negateMask & NEGATE_Y)
Brian00cdc0a2006-12-14 15:01:06 -0700394 s[i++] = '-';
395 s[i++] = swz[GET_SWZ(swizzle, 1)];
396
397 if (extended) {
398 s[i++] = ',';
399 }
400
Brian Paul7db7ff82009-04-14 22:14:30 -0600401 if (negateMask & NEGATE_Z)
Brian00cdc0a2006-12-14 15:01:06 -0700402 s[i++] = '-';
403 s[i++] = swz[GET_SWZ(swizzle, 2)];
404
405 if (extended) {
406 s[i++] = ',';
407 }
408
Brian Paul7db7ff82009-04-14 22:14:30 -0600409 if (negateMask & NEGATE_W)
Brian00cdc0a2006-12-14 15:01:06 -0700410 s[i++] = '-';
411 s[i++] = swz[GET_SWZ(swizzle, 3)];
412
413 s[i] = 0;
414 return s;
415}
416
417
Brian Paul511733b2008-07-02 12:40:03 -0600418void
419_mesa_print_swizzle(GLuint swizzle)
420{
421 if (swizzle == SWIZZLE_XYZW) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500422 printf(".xyzw\n");
Brian Paul511733b2008-07-02 12:40:03 -0600423 }
424 else {
Michal Krolf9c574d2008-07-15 11:44:47 +0200425 const char *s = _mesa_swizzle_string(swizzle, 0, 0);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500426 printf("%s\n", s);
Brian Paul511733b2008-07-02 12:40:03 -0600427 }
428}
429
430
Brian Paul610c2462008-11-13 16:37:52 -0700431const char *
432_mesa_writemask_string(GLuint writeMask)
Brian00cdc0a2006-12-14 15:01:06 -0700433{
434 static char s[10];
435 GLuint i = 0;
436
437 if (writeMask == WRITEMASK_XYZW)
438 return "";
439
440 s[i++] = '.';
441 if (writeMask & WRITEMASK_X)
442 s[i++] = 'x';
443 if (writeMask & WRITEMASK_Y)
444 s[i++] = 'y';
445 if (writeMask & WRITEMASK_Z)
446 s[i++] = 'z';
447 if (writeMask & WRITEMASK_W)
448 s[i++] = 'w';
449
450 s[i] = 0;
451 return s;
452}
453
Brian3a281532006-12-16 12:51:34 -0700454
Briand7508612007-03-28 11:01:09 -0600455const char *
456_mesa_condcode_string(GLuint condcode)
Brian3a281532006-12-16 12:51:34 -0700457{
458 switch (condcode) {
459 case COND_GT: return "GT";
460 case COND_EQ: return "EQ";
461 case COND_LT: return "LT";
462 case COND_UN: return "UN";
463 case COND_GE: return "GE";
464 case COND_LE: return "LE";
465 case COND_NE: return "NE";
466 case COND_TR: return "TR";
467 case COND_FL: return "FL";
468 default: return "cond???";
469 }
470}
471
472
Brian00cdc0a2006-12-14 15:01:06 -0700473static void
Brian Pauld0038932009-01-22 10:34:43 -0700474fprint_dst_reg(FILE * f,
475 const struct prog_dst_register *dstReg,
476 gl_prog_print_mode mode,
477 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700478{
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500479 fprintf(f, "%s%s",
480 reg_string((gl_register_file) dstReg->File,
481 dstReg->Index, mode, dstReg->RelAddr, prog),
482 _mesa_writemask_string(dstReg->WriteMask));
483
Brian1936b252007-03-22 09:04:18 -0600484 if (dstReg->CondMask != COND_TR) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500485 fprintf(f, " (%s.%s)",
486 _mesa_condcode_string(dstReg->CondMask),
487 _mesa_swizzle_string(dstReg->CondSwizzle,
488 GL_FALSE, GL_FALSE));
Brian1936b252007-03-22 09:04:18 -0600489 }
490
Brian501ee872007-02-17 09:15:00 -0700491#if 0
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500492 fprintf(f, "%s[%d]%s",
493 file_string((gl_register_file) dstReg->File, mode),
494 dstReg->Index,
495 _mesa_writemask_string(dstReg->WriteMask));
Brian501ee872007-02-17 09:15:00 -0700496#endif
Brian00cdc0a2006-12-14 15:01:06 -0700497}
498
Brian Pauld0038932009-01-22 10:34:43 -0700499
Brian00cdc0a2006-12-14 15:01:06 -0700500static void
Brian Pauld0038932009-01-22 10:34:43 -0700501fprint_src_reg(FILE *f,
502 const struct prog_src_register *srcReg,
503 gl_prog_print_mode mode,
504 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700505{
Brian Paulf787baf2009-03-05 17:14:05 -0700506 const char *abs = srcReg->Abs ? "|" : "";
507
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500508 fprintf(f, "%s%s%s%s",
509 abs,
510 reg_string((gl_register_file) srcReg->File,
511 srcReg->Index, mode, srcReg->RelAddr, prog),
512 _mesa_swizzle_string(srcReg->Swizzle,
513 srcReg->Negate, GL_FALSE),
514 abs);
Brian501ee872007-02-17 09:15:00 -0700515#if 0
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500516 fprintf(f, "%s[%d]%s",
517 file_string((gl_register_file) srcReg->File, mode),
518 srcReg->Index,
519 _mesa_swizzle_string(srcReg->Swizzle,
520 srcReg->Negate, GL_FALSE));
Brian501ee872007-02-17 09:15:00 -0700521#endif
Brian00cdc0a2006-12-14 15:01:06 -0700522}
523
Brian Pauld0038932009-01-22 10:34:43 -0700524
Brian00cdc0a2006-12-14 15:01:06 -0700525static void
Brian Pauld0038932009-01-22 10:34:43 -0700526fprint_comment(FILE *f, const struct prog_instruction *inst)
Brian00cdc0a2006-12-14 15:01:06 -0700527{
528 if (inst->Comment)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500529 fprintf(f, "; # %s\n", inst->Comment);
Brian00cdc0a2006-12-14 15:01:06 -0700530 else
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500531 fprintf(f, ";\n");
Brian00cdc0a2006-12-14 15:01:06 -0700532}
533
534
Brian501ee872007-02-17 09:15:00 -0700535static void
Brian Pauld0038932009-01-22 10:34:43 -0700536fprint_alu_instruction(FILE *f,
537 const struct prog_instruction *inst,
538 const char *opcode_string, GLuint numRegs,
539 gl_prog_print_mode mode,
540 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700541{
542 GLuint j;
543
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500544 fprintf(f, "%s", opcode_string);
Brianb50280e2006-12-18 16:21:58 -0700545 if (inst->CondUpdate)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500546 fprintf(f, ".C");
Brian00cdc0a2006-12-14 15:01:06 -0700547
548 /* frag prog only */
549 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500550 fprintf(f, "_SAT");
Brian00cdc0a2006-12-14 15:01:06 -0700551
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500552 fprintf(f, " ");
Brian00cdc0a2006-12-14 15:01:06 -0700553 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
Brian Pauld0038932009-01-22 10:34:43 -0700554 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700555 }
556 else {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500557 fprintf(f, " ???");
Brian00cdc0a2006-12-14 15:01:06 -0700558 }
559
560 if (numRegs > 0)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500561 fprintf(f, ", ");
Brian00cdc0a2006-12-14 15:01:06 -0700562
563 for (j = 0; j < numRegs; j++) {
Brian Pauld0038932009-01-22 10:34:43 -0700564 fprint_src_reg(f, inst->SrcReg + j, mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700565 if (j + 1 < numRegs)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500566 fprintf(f, ", ");
Brian00cdc0a2006-12-14 15:01:06 -0700567 }
568
Brian Pauld0038932009-01-22 10:34:43 -0700569 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700570}
571
572
Brian501ee872007-02-17 09:15:00 -0700573void
Brian0020d102007-02-23 11:43:44 -0700574_mesa_print_alu_instruction(const struct prog_instruction *inst,
575 const char *opcode_string, GLuint numRegs)
576{
marvin2443a064e2009-08-27 09:22:51 -0600577 fprint_alu_instruction(stderr, inst, opcode_string,
Brian Pauld0038932009-01-22 10:34:43 -0700578 numRegs, PROG_PRINT_DEBUG, NULL);
Brian501ee872007-02-17 09:15:00 -0700579}
580
581
Brian00cdc0a2006-12-14 15:01:06 -0700582/**
583 * Print a single vertex/fragment program instruction.
584 */
Nicolai Hähnle2237d132009-07-23 22:09:11 +0200585GLint
Brian Pauld0038932009-01-22 10:34:43 -0700586_mesa_fprint_instruction_opt(FILE *f,
587 const struct prog_instruction *inst,
588 GLint indent,
Brian501ee872007-02-17 09:15:00 -0700589 gl_prog_print_mode mode,
590 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700591{
Brian7b300532007-02-22 09:08:36 -0700592 GLint i;
Brian5db088d2007-02-05 14:58:15 -0700593
594 if (inst->Opcode == OPCODE_ELSE ||
595 inst->Opcode == OPCODE_ENDIF ||
596 inst->Opcode == OPCODE_ENDLOOP ||
597 inst->Opcode == OPCODE_ENDSUB) {
598 indent -= 3;
599 }
Brian5db088d2007-02-05 14:58:15 -0700600 for (i = 0; i < indent; i++) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500601 fprintf(f, " ");
Brian5db088d2007-02-05 14:58:15 -0700602 }
603
Brian00cdc0a2006-12-14 15:01:06 -0700604 switch (inst->Opcode) {
605 case OPCODE_PRINT:
Brian Paul8de5a292010-02-19 12:45:49 -0700606 fprintf(f, "PRINT '%s'", (char *) inst->Data);
Brian00cdc0a2006-12-14 15:01:06 -0700607 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500608 fprintf(f, ", ");
609 fprintf(f, "%s[%d]%s",
610 file_string((gl_register_file) inst->SrcReg[0].File,
611 mode),
612 inst->SrcReg[0].Index,
613 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
614 inst->SrcReg[0].Negate, GL_FALSE));
Brian00cdc0a2006-12-14 15:01:06 -0700615 }
616 if (inst->Comment)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500617 fprintf(f, " # %s", inst->Comment);
Brian Pauld0038932009-01-22 10:34:43 -0700618 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700619 break;
620 case OPCODE_SWZ:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500621 fprintf(f, "SWZ");
Brian00cdc0a2006-12-14 15:01:06 -0700622 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500623 fprintf(f, "_SAT");
624 fprintf(f, " ");
Brian Pauld0038932009-01-22 10:34:43 -0700625 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500626 fprintf(f, ", %s[%d], %s",
627 file_string((gl_register_file) inst->SrcReg[0].File,
628 mode),
629 inst->SrcReg[0].Index,
630 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
631 inst->SrcReg[0].Negate, GL_TRUE));
Brian Pauld0038932009-01-22 10:34:43 -0700632 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700633 break;
634 case OPCODE_TEX:
635 case OPCODE_TXP:
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600636 case OPCODE_TXL:
Brian00cdc0a2006-12-14 15:01:06 -0700637 case OPCODE_TXB:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500638 fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
Brian00cdc0a2006-12-14 15:01:06 -0700639 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500640 fprintf(f, "_SAT");
641 fprintf(f, " ");
Brian Pauld0038932009-01-22 10:34:43 -0700642 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500643 fprintf(f, ", ");
Brian Pauld0038932009-01-22 10:34:43 -0700644 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500645 fprintf(f, ", texture[%d], ", inst->TexSrcUnit);
Brian00cdc0a2006-12-14 15:01:06 -0700646 switch (inst->TexSrcTarget) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500647 case TEXTURE_1D_INDEX: fprintf(f, "1D"); break;
648 case TEXTURE_2D_INDEX: fprintf(f, "2D"); break;
649 case TEXTURE_3D_INDEX: fprintf(f, "3D"); break;
650 case TEXTURE_CUBE_INDEX: fprintf(f, "CUBE"); break;
651 case TEXTURE_RECT_INDEX: fprintf(f, "RECT"); break;
Brian00cdc0a2006-12-14 15:01:06 -0700652 default:
653 ;
654 }
Brian Paul44e018c2009-02-20 13:42:08 -0700655 if (inst->TexShadow)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500656 fprintf(f, " SHADOW");
Brian Pauld0038932009-01-22 10:34:43 -0700657 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700658 break;
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600659
660 case OPCODE_KIL:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500661 fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
662 fprintf(f, " ");
Brian Pauld0038932009-01-22 10:34:43 -0700663 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
664 fprint_comment(f, inst);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600665 break;
666 case OPCODE_KIL_NV:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500667 fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
668 fprintf(f, " ");
669 fprintf(f, "%s.%s",
670 _mesa_condcode_string(inst->DstReg.CondMask),
671 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
672 GL_FALSE, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700673 fprint_comment(f, inst);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600674 break;
675
Brian00cdc0a2006-12-14 15:01:06 -0700676 case OPCODE_ARL:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500677 fprintf(f, "ARL ");
Brian Pauld0038932009-01-22 10:34:43 -0700678 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500679 fprintf(f, ", ");
Brian Pauld0038932009-01-22 10:34:43 -0700680 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
681 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700682 break;
683 case OPCODE_BRA:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500684 fprintf(f, "BRA %d (%s%s)",
685 inst->BranchTarget,
686 _mesa_condcode_string(inst->DstReg.CondMask),
687 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700688 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700689 break;
Brian5ae49cf2007-01-20 09:27:40 -0700690 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600691 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
692 /* Use ordinary register */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500693 fprintf(f, "IF ");
Brian Pauld0038932009-01-22 10:34:43 -0700694 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500695 fprintf(f, "; ");
Brian63556fa2007-03-23 14:47:46 -0600696 }
697 else {
698 /* Use cond codes */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500699 fprintf(f, "IF (%s%s);",
700 _mesa_condcode_string(inst->DstReg.CondMask),
701 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
702 0, GL_FALSE));
Brian63556fa2007-03-23 14:47:46 -0600703 }
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500704 fprintf(f, " # (if false, goto %d)", inst->BranchTarget);
Brian Pauld0038932009-01-22 10:34:43 -0700705 fprint_comment(f, inst);
Brian5db088d2007-02-05 14:58:15 -0700706 return indent + 3;
707 case OPCODE_ELSE:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500708 fprintf(f, "ELSE; # (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700709 return indent + 3;
710 case OPCODE_ENDIF:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500711 fprintf(f, "ENDIF;\n");
Brian5db088d2007-02-05 14:58:15 -0700712 break;
713 case OPCODE_BGNLOOP:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500714 fprintf(f, "BGNLOOP; # (end at %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700715 return indent + 3;
716 case OPCODE_ENDLOOP:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500717 fprintf(f, "ENDLOOP; # (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700718 break;
719 case OPCODE_BRK:
Brianf22ed092007-02-06 22:31:19 -0700720 case OPCODE_CONT:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500721 fprintf(f, "%s (%s%s); # (goto %d)",
722 _mesa_opcode_string(inst->Opcode),
723 _mesa_condcode_string(inst->DstReg.CondMask),
724 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
725 inst->BranchTarget);
Brian Pauld0038932009-01-22 10:34:43 -0700726 fprint_comment(f, inst);
Brian5ae49cf2007-01-20 09:27:40 -0700727 break;
Brian63556fa2007-03-23 14:47:46 -0600728
Brian5db088d2007-02-05 14:58:15 -0700729 case OPCODE_BGNSUB:
Brianf407cad2007-03-27 14:53:17 -0600730 if (mode == PROG_PRINT_NV) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500731 fprintf(f, "%s:\n", inst->Comment); /* comment is label */
Brianf407cad2007-03-27 14:53:17 -0600732 return indent;
733 }
734 else {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500735 fprintf(f, "BGNSUB");
Brian Pauld0038932009-01-22 10:34:43 -0700736 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600737 return indent + 3;
738 }
Brian5db088d2007-02-05 14:58:15 -0700739 case OPCODE_ENDSUB:
Brianf407cad2007-03-27 14:53:17 -0600740 if (mode == PROG_PRINT_DEBUG) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500741 fprintf(f, "ENDSUB");
Brian Pauld0038932009-01-22 10:34:43 -0700742 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600743 }
744 break;
745 case OPCODE_CAL:
746 if (mode == PROG_PRINT_NV) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500747 fprintf(f, "CAL %s; # (goto %d)\n", inst->Comment, inst->BranchTarget);
Brianf407cad2007-03-27 14:53:17 -0600748 }
749 else {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500750 fprintf(f, "CAL %u", inst->BranchTarget);
Brian Pauld0038932009-01-22 10:34:43 -0700751 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600752 }
753 break;
754 case OPCODE_RET:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500755 fprintf(f, "RET (%s%s)",
756 _mesa_condcode_string(inst->DstReg.CondMask),
757 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700758 fprint_comment(f, inst);
Brian5ae49cf2007-01-20 09:27:40 -0700759 break;
Brianf407cad2007-03-27 14:53:17 -0600760
Brian00cdc0a2006-12-14 15:01:06 -0700761 case OPCODE_END:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500762 fprintf(f, "END\n");
Brian00cdc0a2006-12-14 15:01:06 -0700763 break;
Brian3a281532006-12-16 12:51:34 -0700764 case OPCODE_NOP:
Brian059376c2007-02-22 17:45:32 -0700765 if (mode == PROG_PRINT_DEBUG) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500766 fprintf(f, "NOP");
Brian Pauld0038932009-01-22 10:34:43 -0700767 fprint_comment(f, inst);
Brian059376c2007-02-22 17:45:32 -0700768 }
769 else if (inst->Comment) {
770 /* ARB/NV extensions don't have NOP instruction */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500771 fprintf(f, "# %s\n", inst->Comment);
Brian059376c2007-02-22 17:45:32 -0700772 }
Brian3a281532006-12-16 12:51:34 -0700773 break;
Brian00cdc0a2006-12-14 15:01:06 -0700774 /* XXX may need other special-case instructions */
775 default:
Brian Paulf787baf2009-03-05 17:14:05 -0700776 if (inst->Opcode < MAX_OPCODE) {
777 /* typical alu instruction */
778 fprint_alu_instruction(f, inst,
779 _mesa_opcode_string(inst->Opcode),
780 _mesa_num_inst_src_regs(inst->Opcode),
781 mode, prog);
782 }
783 else {
Brian Paul7c2fe422009-05-11 09:38:32 -0600784 fprint_alu_instruction(f, inst,
785 _mesa_opcode_string(inst->Opcode),
786 3/*_mesa_num_inst_src_regs(inst->Opcode)*/,
787 mode, prog);
Brian Paulf787baf2009-03-05 17:14:05 -0700788 }
Brian00cdc0a2006-12-14 15:01:06 -0700789 break;
790 }
Brian5db088d2007-02-05 14:58:15 -0700791 return indent;
Brian00cdc0a2006-12-14 15:01:06 -0700792}
793
794
Brian Pauld0038932009-01-22 10:34:43 -0700795GLint
796_mesa_print_instruction_opt(const struct prog_instruction *inst,
797 GLint indent,
798 gl_prog_print_mode mode,
799 const struct gl_program *prog)
800{
marvin2443a064e2009-08-27 09:22:51 -0600801 return _mesa_fprint_instruction_opt(stderr, inst, indent, mode, prog);
Brian Pauld0038932009-01-22 10:34:43 -0700802}
803
804
805void
806_mesa_print_instruction(const struct prog_instruction *inst)
807{
808 /* note: 4th param should be ignored for PROG_PRINT_DEBUG */
marvin2443a064e2009-08-27 09:22:51 -0600809 _mesa_fprint_instruction_opt(stderr, inst, 0, PROG_PRINT_DEBUG, NULL);
Brian Pauld0038932009-01-22 10:34:43 -0700810}
811
812
813
814/**
815 * Print program, with options.
816 */
Brian Pauld2eff332009-02-02 12:24:41 -0700817void
Brian Pauld0038932009-01-22 10:34:43 -0700818_mesa_fprint_program_opt(FILE *f,
819 const struct gl_program *prog,
820 gl_prog_print_mode mode,
821 GLboolean lineNumbers)
822{
823 GLuint i, indent = 0;
824
825 switch (prog->Target) {
826 case GL_VERTEX_PROGRAM_ARB:
827 if (mode == PROG_PRINT_ARB)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500828 fprintf(f, "!!ARBvp1.0\n");
Brian Pauld0038932009-01-22 10:34:43 -0700829 else if (mode == PROG_PRINT_NV)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500830 fprintf(f, "!!VP1.0\n");
Brian Pauld0038932009-01-22 10:34:43 -0700831 else
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500832 fprintf(f, "# Vertex Program/Shader %u\n", prog->Id);
Brian Pauld0038932009-01-22 10:34:43 -0700833 break;
834 case GL_FRAGMENT_PROGRAM_ARB:
835 case GL_FRAGMENT_PROGRAM_NV:
836 if (mode == PROG_PRINT_ARB)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500837 fprintf(f, "!!ARBfp1.0\n");
Brian Pauld0038932009-01-22 10:34:43 -0700838 else if (mode == PROG_PRINT_NV)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500839 fprintf(f, "!!FP1.0\n");
Brian Pauld0038932009-01-22 10:34:43 -0700840 else
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500841 fprintf(f, "# Fragment Program/Shader %u\n", prog->Id);
Brian Pauld0038932009-01-22 10:34:43 -0700842 break;
843 }
844
845 for (i = 0; i < prog->NumInstructions; i++) {
846 if (lineNumbers)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500847 fprintf(f, "%3d: ", i);
Brian Pauld0038932009-01-22 10:34:43 -0700848 indent = _mesa_fprint_instruction_opt(f, prog->Instructions + i,
849 indent, mode, prog);
850 }
851}
852
853
Brian00cdc0a2006-12-14 15:01:06 -0700854/**
marvin2443a064e2009-08-27 09:22:51 -0600855 * Print program to stderr, default options.
Brian00cdc0a2006-12-14 15:01:06 -0700856 */
857void
858_mesa_print_program(const struct gl_program *prog)
859{
Ian Romanicka7400e72009-07-24 18:03:02 -0700860 _mesa_fprint_program_opt(stderr, prog, PROG_PRINT_DEBUG, GL_TRUE);
Brian501ee872007-02-17 09:15:00 -0700861}
862
863
864/**
Brian Paulb5ddc782009-12-22 13:28:39 -0700865 * Return binary representation of 64-bit value (as a string).
Brian Paulf9ce0a92009-08-25 16:11:39 -0600866 * Insert a comma to separate each group of 8 bits.
Brian Paulb5ddc782009-12-22 13:28:39 -0700867 * Note we return a pointer to local static storage so this is not
868 * re-entrant, etc.
Brian Paulf9ce0a92009-08-25 16:11:39 -0600869 * XXX move to imports.[ch] if useful elsewhere.
870 */
871static const char *
Ian Romanick5606dfb2009-11-17 16:10:24 -0800872binary(GLbitfield64 val)
Brian Paulf9ce0a92009-08-25 16:11:39 -0600873{
Ian Romanick5606dfb2009-11-17 16:10:24 -0800874 static char buf[80];
Brian Paulf9ce0a92009-08-25 16:11:39 -0600875 GLint i, len = 0;
Ian Romanick5606dfb2009-11-17 16:10:24 -0800876 for (i = 63; i >= 0; --i) {
Brian Paulb5ddc782009-12-22 13:28:39 -0700877 if (val & (1ULL << i))
Brian Paulf9ce0a92009-08-25 16:11:39 -0600878 buf[len++] = '1';
879 else if (len > 0 || i == 0)
880 buf[len++] = '0';
881 if (len > 0 && ((i-1) % 8) == 7)
882 buf[len++] = ',';
883 }
884 buf[len] = '\0';
885 return buf;
886}
887
888
889/**
Brian Pauld0038932009-01-22 10:34:43 -0700890 * Print all of a program's parameters/fields to given file.
Brian501ee872007-02-17 09:15:00 -0700891 */
Brian Pauld0038932009-01-22 10:34:43 -0700892static void
893_mesa_fprint_program_parameters(FILE *f,
894 GLcontext *ctx,
895 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700896{
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600897 GLuint i;
898
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500899 fprintf(f, "InputsRead: 0x%x (0b%s)\n",
Brian Paulf9ce0a92009-08-25 16:11:39 -0600900 prog->InputsRead, binary(prog->InputsRead));
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500901 fprintf(f, "OutputsWritten: 0x%llx (0b%s)\n",
Brian Paulf9ce0a92009-08-25 16:11:39 -0600902 prog->OutputsWritten, binary(prog->OutputsWritten));
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500903 fprintf(f, "NumInstructions=%d\n", prog->NumInstructions);
904 fprintf(f, "NumTemporaries=%d\n", prog->NumTemporaries);
905 fprintf(f, "NumParameters=%d\n", prog->NumParameters);
906 fprintf(f, "NumAttributes=%d\n", prog->NumAttributes);
907 fprintf(f, "NumAddressRegs=%d\n", prog->NumAddressRegs);
908 fprintf(f, "SamplersUsed: 0x%x (0b%s)\n",
Brian Paulf9ce0a92009-08-25 16:11:39 -0600909 prog->SamplersUsed, binary(prog->SamplersUsed));
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500910 fprintf(f, "Samplers=[ ");
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600911 for (i = 0; i < MAX_SAMPLERS; i++) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500912 fprintf(f, "%d ", prog->SamplerUnits[i]);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600913 }
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500914 fprintf(f, "]\n");
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600915
Brian00cdc0a2006-12-14 15:01:06 -0700916 _mesa_load_state_parameters(ctx, prog->Parameters);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600917
Brian00cdc0a2006-12-14 15:01:06 -0700918#if 0
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500919 fprintf(f, "Local Params:\n");
Brian00cdc0a2006-12-14 15:01:06 -0700920 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
921 const GLfloat *p = prog->LocalParams[i];
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500922 fprintf(f, "%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
Brian00cdc0a2006-12-14 15:01:06 -0700923 }
924#endif
Brian83ca3ff2006-12-20 17:17:38 -0700925 _mesa_print_parameter_list(prog->Parameters);
926}
Brian00cdc0a2006-12-14 15:01:06 -0700927
Brian83ca3ff2006-12-20 17:17:38 -0700928
Brian Pauld0038932009-01-22 10:34:43 -0700929/**
marvin2443a064e2009-08-27 09:22:51 -0600930 * Print all of a program's parameters/fields to stderr.
Brian Pauld0038932009-01-22 10:34:43 -0700931 */
Brian83ca3ff2006-12-20 17:17:38 -0700932void
Brian Pauld0038932009-01-22 10:34:43 -0700933_mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
934{
marvin2443a064e2009-08-27 09:22:51 -0600935 _mesa_fprint_program_parameters(stderr, ctx, prog);
Brian Pauld0038932009-01-22 10:34:43 -0700936}
937
938
939/**
940 * Print a program parameter list to given file.
941 */
942static void
943_mesa_fprint_parameter_list(FILE *f,
944 const struct gl_program_parameter_list *list)
Brian83ca3ff2006-12-20 17:17:38 -0700945{
Brian501ee872007-02-17 09:15:00 -0700946 const gl_prog_print_mode mode = PROG_PRINT_DEBUG;
Brian83ca3ff2006-12-20 17:17:38 -0700947 GLuint i;
Brian501ee872007-02-17 09:15:00 -0700948
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600949 if (!list)
950 return;
951
Brian Pauldfefde32009-10-13 16:18:06 -0600952 if (0)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500953 fprintf(f, "param list %p\n", (void *) list);
954 fprintf(f, "dirty state flags: 0x%x\n", list->StateFlags);
Brian83ca3ff2006-12-20 17:17:38 -0700955 for (i = 0; i < list->NumParameters; i++){
956 struct gl_program_parameter *param = list->Parameters + i;
957 const GLfloat *v = list->ParameterValues[i];
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500958 fprintf(f, "param[%d] sz=%d %s %s = {%.3g, %.3g, %.3g, %.3g}",
959 i, param->Size,
960 file_string(list->Parameters[i].Type, mode),
961 param->Name, v[0], v[1], v[2], v[3]);
Brian Paulf490ec92008-11-24 09:04:52 -0700962 if (param->Flags & PROG_PARAM_BIT_CENTROID)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500963 fprintf(f, " Centroid");
Brian Paulf490ec92008-11-24 09:04:52 -0700964 if (param->Flags & PROG_PARAM_BIT_INVARIANT)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500965 fprintf(f, " Invariant");
Brian Paulf490ec92008-11-24 09:04:52 -0700966 if (param->Flags & PROG_PARAM_BIT_FLAT)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500967 fprintf(f, " Flat");
Brian Paulf490ec92008-11-24 09:04:52 -0700968 if (param->Flags & PROG_PARAM_BIT_LINEAR)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500969 fprintf(f, " Linear");
970 fprintf(f, "\n");
Brian00cdc0a2006-12-14 15:01:06 -0700971 }
972}
Brian Pauld0038932009-01-22 10:34:43 -0700973
974
975/**
marvin2443a064e2009-08-27 09:22:51 -0600976 * Print a program parameter list to stderr.
Brian Pauld0038932009-01-22 10:34:43 -0700977 */
978void
979_mesa_print_parameter_list(const struct gl_program_parameter_list *list)
980{
marvin2443a064e2009-08-27 09:22:51 -0600981 _mesa_fprint_parameter_list(stderr, list);
Brian Pauld0038932009-01-22 10:34:43 -0700982}
983
984
985/**
986 * Write shader and associated info to a file.
987 */
988void
989_mesa_write_shader_to_file(const struct gl_shader *shader)
990{
991 const char *type;
992 char filename[100];
993 FILE *f;
994
995 if (shader->Type == GL_FRAGMENT_SHADER)
996 type = "frag";
997 else
998 type = "vert";
999
Brian Paul78a0c352010-02-19 12:56:49 -07001000 _mesa_snprintf(filename, sizeof(filename), "shader_%u.%s", shader->Name, type);
Brian Pauld0038932009-01-22 10:34:43 -07001001 f = fopen(filename, "w");
1002 if (!f) {
1003 fprintf(stderr, "Unable to open %s for writing\n", filename);
1004 return;
1005 }
1006
Brian Paula746ef22009-08-04 15:36:13 -06001007 fprintf(f, "/* Shader %u source, checksum %u */\n", shader->Name, shader->SourceChecksum);
Brian Pauld0038932009-01-22 10:34:43 -07001008 fputs(shader->Source, f);
1009 fprintf(f, "\n");
1010
1011 fprintf(f, "/* Compile status: %s */\n",
1012 shader->CompileStatus ? "ok" : "fail");
1013 if (!shader->CompileStatus) {
1014 fprintf(f, "/* Log Info: */\n");
1015 fputs(shader->InfoLog, f);
1016 }
1017 else {
1018 fprintf(f, "/* GPU code */\n");
Brian Paul6ce0c6e2009-02-06 10:20:33 -07001019 fprintf(f, "/*\n");
Brian Pauld0038932009-01-22 10:34:43 -07001020 _mesa_fprint_program_opt(f, shader->Program, PROG_PRINT_DEBUG, GL_TRUE);
Brian Paul6ce0c6e2009-02-06 10:20:33 -07001021 fprintf(f, "*/\n");
Brian Paulf95c0c02009-05-04 11:14:35 -06001022 fprintf(f, "/* Parameters / constants */\n");
1023 fprintf(f, "/*\n");
1024 _mesa_fprint_parameter_list(f, shader->Program->Parameters);
1025 fprintf(f, "*/\n");
Brian Pauld0038932009-01-22 10:34:43 -07001026 }
1027
1028 fclose(f);
1029}
1030
1031
Brian Pauldb598b82009-08-14 11:26:20 -06001032/**
1033 * Append the shader's uniform info/values to the shader log file.
1034 * The log file will typically have been created by the
1035 * _mesa_write_shader_to_file function.
1036 */
1037void
Brian Paul12199ed2009-08-14 12:57:39 -06001038_mesa_append_uniforms_to_file(const struct gl_shader *shader,
1039 const struct gl_program *prog)
Brian Pauldb598b82009-08-14 11:26:20 -06001040{
1041 const char *type;
1042 char filename[100];
1043 FILE *f;
1044
1045 if (shader->Type == GL_FRAGMENT_SHADER)
1046 type = "frag";
1047 else
1048 type = "vert";
1049
Brian Paul78a0c352010-02-19 12:56:49 -07001050 _mesa_snprintf(filename, sizeof(filename), "shader_%u.%s", shader->Name, type);
Brian Pauldb598b82009-08-14 11:26:20 -06001051 f = fopen(filename, "a"); /* append */
1052 if (!f) {
1053 fprintf(stderr, "Unable to open %s for appending\n", filename);
1054 return;
1055 }
1056
1057 fprintf(f, "/* First-draw parameters / constants */\n");
1058 fprintf(f, "/*\n");
Brian Paul12199ed2009-08-14 12:57:39 -06001059 _mesa_fprint_parameter_list(f, prog->Parameters);
Brian Pauldb598b82009-08-14 11:26:20 -06001060 fprintf(f, "*/\n");
1061
1062 fclose(f);
1063}