blob: 79c01020eb24dffb52538cfeb5dfdc874d112ed2 [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 Rusinb4855282010-07-09 21:19:28 -0400268 GLboolean relAddr, const struct gl_program *prog,
Zack Rusin1491c6a2010-07-13 21:49:53 -0400269 GLboolean hasIndex2, GLboolean relAddr2, GLint index2)
Brian501ee872007-02-17 09:15:00 -0700270{
271 static char str[100];
Brian Paulf88a9012009-02-17 15:57:00 -0700272 const char *addr = relAddr ? "ADDR+" : "";
Brian501ee872007-02-17 09:15:00 -0700273
274 str[0] = 0;
275
276 switch (mode) {
277 case PROG_PRINT_DEBUG:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500278 sprintf(str, "%s[%s%d]", file_string(f, mode), addr, index);
Zack Rusin1491c6a2010-07-13 21:49:53 -0400279 if (hasIndex2) {
Zack Rusinb4855282010-07-09 21:19:28 -0400280 int offset = strlen(str);
Zack Rusin1491c6a2010-07-13 21:49:53 -0400281 const char *addr2 = relAddr2 ? "ADDR+" : "";
282 sprintf(str+offset, "[%s%d]", addr2, index2);
Zack Rusinb4855282010-07-09 21:19:28 -0400283 }
Brian501ee872007-02-17 09:15:00 -0700284 break;
285
286 case PROG_PRINT_ARB:
287 switch (f) {
288 case PROGRAM_INPUT:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500289 sprintf(str, "%s", arb_input_attrib_string(index, prog->Target));
Brian501ee872007-02-17 09:15:00 -0700290 break;
291 case PROGRAM_OUTPUT:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500292 sprintf(str, "%s", arb_output_attrib_string(index, prog->Target));
Brian501ee872007-02-17 09:15:00 -0700293 break;
294 case PROGRAM_TEMPORARY:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500295 sprintf(str, "temp%d", index);
Brian501ee872007-02-17 09:15:00 -0700296 break;
297 case PROGRAM_ENV_PARAM:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500298 sprintf(str, "program.env[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700299 break;
300 case PROGRAM_LOCAL_PARAM:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500301 sprintf(str, "program.local[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700302 break;
303 case PROGRAM_VARYING: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500304 sprintf(str, "varying[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700305 break;
306 case PROGRAM_CONSTANT: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500307 sprintf(str, "constant[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700308 break;
309 case PROGRAM_UNIFORM: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500310 sprintf(str, "uniform[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700311 break;
312 case PROGRAM_STATE_VAR:
313 {
314 struct gl_program_parameter *param
315 = prog->Parameters->Parameters + index;
Brian Paul9a644402008-09-04 15:05:03 -0600316 char *state = _mesa_program_state_string(param->StateIndexes);
Vinson Leea45f2ec2010-02-19 23:53:27 -0800317 sprintf(str, "%s", state);
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500318 free(state);
Brian501ee872007-02-17 09:15:00 -0700319 }
320 break;
321 case PROGRAM_ADDRESS:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500322 sprintf(str, "A%d", index);
Brian501ee872007-02-17 09:15:00 -0700323 break;
324 default:
325 _mesa_problem(NULL, "bad file in reg_string()");
326 }
327 break;
328
329 case PROG_PRINT_NV:
330 switch (f) {
331 case PROGRAM_INPUT:
332 if (prog->Target == GL_VERTEX_PROGRAM_ARB)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500333 sprintf(str, "v[%d]", index);
Brian501ee872007-02-17 09:15:00 -0700334 else
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500335 sprintf(str, "f[%d]", index);
Brian501ee872007-02-17 09:15:00 -0700336 break;
337 case PROGRAM_OUTPUT:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500338 sprintf(str, "o[%d]", index);
Brian501ee872007-02-17 09:15:00 -0700339 break;
340 case PROGRAM_TEMPORARY:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500341 sprintf(str, "R%d", index);
Brian501ee872007-02-17 09:15:00 -0700342 break;
343 case PROGRAM_ENV_PARAM:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500344 sprintf(str, "c[%d]", index);
Brian501ee872007-02-17 09:15:00 -0700345 break;
346 case PROGRAM_VARYING: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500347 sprintf(str, "varying[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700348 break;
349 case PROGRAM_UNIFORM: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500350 sprintf(str, "uniform[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700351 break;
352 case PROGRAM_CONSTANT: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500353 sprintf(str, "constant[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700354 break;
355 case PROGRAM_STATE_VAR: /* extension */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500356 sprintf(str, "state[%s%d]", addr, index);
Brian501ee872007-02-17 09:15:00 -0700357 break;
358 default:
359 _mesa_problem(NULL, "bad file in reg_string()");
360 }
361 break;
362
363 default:
364 _mesa_problem(NULL, "bad mode in reg_string()");
365 }
366
367 return str;
368}
369
370
371/**
Brian00cdc0a2006-12-14 15:01:06 -0700372 * Return a string representation of the given swizzle word.
373 * If extended is true, use extended (comma-separated) format.
Brian3a281532006-12-16 12:51:34 -0700374 * \param swizzle the swizzle field
375 * \param negateBase 4-bit negation vector
376 * \param extended if true, also allow 0, 1 values
Brian00cdc0a2006-12-14 15:01:06 -0700377 */
Brian059376c2007-02-22 17:45:32 -0700378const char *
Brian Paul7db7ff82009-04-14 22:14:30 -0600379_mesa_swizzle_string(GLuint swizzle, GLuint negateMask, GLboolean extended)
Brian00cdc0a2006-12-14 15:01:06 -0700380{
Brian Paul74a19b02008-07-18 19:46:19 -0600381 static const char swz[] = "xyzw01!?"; /* See SWIZZLE_x definitions */
Brian00cdc0a2006-12-14 15:01:06 -0700382 static char s[20];
383 GLuint i = 0;
384
Brian Paul7db7ff82009-04-14 22:14:30 -0600385 if (!extended && swizzle == SWIZZLE_NOOP && negateMask == 0)
Brian00cdc0a2006-12-14 15:01:06 -0700386 return ""; /* no swizzle/negation */
387
388 if (!extended)
389 s[i++] = '.';
390
Brian Paul7db7ff82009-04-14 22:14:30 -0600391 if (negateMask & NEGATE_X)
Brian00cdc0a2006-12-14 15:01:06 -0700392 s[i++] = '-';
393 s[i++] = swz[GET_SWZ(swizzle, 0)];
394
395 if (extended) {
396 s[i++] = ',';
397 }
398
Brian Paul7db7ff82009-04-14 22:14:30 -0600399 if (negateMask & NEGATE_Y)
Brian00cdc0a2006-12-14 15:01:06 -0700400 s[i++] = '-';
401 s[i++] = swz[GET_SWZ(swizzle, 1)];
402
403 if (extended) {
404 s[i++] = ',';
405 }
406
Brian Paul7db7ff82009-04-14 22:14:30 -0600407 if (negateMask & NEGATE_Z)
Brian00cdc0a2006-12-14 15:01:06 -0700408 s[i++] = '-';
409 s[i++] = swz[GET_SWZ(swizzle, 2)];
410
411 if (extended) {
412 s[i++] = ',';
413 }
414
Brian Paul7db7ff82009-04-14 22:14:30 -0600415 if (negateMask & NEGATE_W)
Brian00cdc0a2006-12-14 15:01:06 -0700416 s[i++] = '-';
417 s[i++] = swz[GET_SWZ(swizzle, 3)];
418
419 s[i] = 0;
420 return s;
421}
422
423
Brian Paul511733b2008-07-02 12:40:03 -0600424void
425_mesa_print_swizzle(GLuint swizzle)
426{
427 if (swizzle == SWIZZLE_XYZW) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500428 printf(".xyzw\n");
Brian Paul511733b2008-07-02 12:40:03 -0600429 }
430 else {
Michal Krolf9c574d2008-07-15 11:44:47 +0200431 const char *s = _mesa_swizzle_string(swizzle, 0, 0);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500432 printf("%s\n", s);
Brian Paul511733b2008-07-02 12:40:03 -0600433 }
434}
435
436
Brian Paul610c2462008-11-13 16:37:52 -0700437const char *
438_mesa_writemask_string(GLuint writeMask)
Brian00cdc0a2006-12-14 15:01:06 -0700439{
440 static char s[10];
441 GLuint i = 0;
442
443 if (writeMask == WRITEMASK_XYZW)
444 return "";
445
446 s[i++] = '.';
447 if (writeMask & WRITEMASK_X)
448 s[i++] = 'x';
449 if (writeMask & WRITEMASK_Y)
450 s[i++] = 'y';
451 if (writeMask & WRITEMASK_Z)
452 s[i++] = 'z';
453 if (writeMask & WRITEMASK_W)
454 s[i++] = 'w';
455
456 s[i] = 0;
457 return s;
458}
459
Brian3a281532006-12-16 12:51:34 -0700460
Briand7508612007-03-28 11:01:09 -0600461const char *
462_mesa_condcode_string(GLuint condcode)
Brian3a281532006-12-16 12:51:34 -0700463{
464 switch (condcode) {
465 case COND_GT: return "GT";
466 case COND_EQ: return "EQ";
467 case COND_LT: return "LT";
468 case COND_UN: return "UN";
469 case COND_GE: return "GE";
470 case COND_LE: return "LE";
471 case COND_NE: return "NE";
472 case COND_TR: return "TR";
473 case COND_FL: return "FL";
474 default: return "cond???";
475 }
476}
477
478
Brian00cdc0a2006-12-14 15:01:06 -0700479static void
Brian Pauld0038932009-01-22 10:34:43 -0700480fprint_dst_reg(FILE * f,
481 const struct prog_dst_register *dstReg,
482 gl_prog_print_mode mode,
483 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700484{
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500485 fprintf(f, "%s%s",
486 reg_string((gl_register_file) dstReg->File,
Zack Rusinb4855282010-07-09 21:19:28 -0400487 dstReg->Index, mode, dstReg->RelAddr, prog,
488 GL_FALSE, GL_FALSE, 0),
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500489 _mesa_writemask_string(dstReg->WriteMask));
490
Brian1936b252007-03-22 09:04:18 -0600491 if (dstReg->CondMask != COND_TR) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500492 fprintf(f, " (%s.%s)",
493 _mesa_condcode_string(dstReg->CondMask),
494 _mesa_swizzle_string(dstReg->CondSwizzle,
495 GL_FALSE, GL_FALSE));
Brian1936b252007-03-22 09:04:18 -0600496 }
497
Brian501ee872007-02-17 09:15:00 -0700498#if 0
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500499 fprintf(f, "%s[%d]%s",
500 file_string((gl_register_file) dstReg->File, mode),
501 dstReg->Index,
502 _mesa_writemask_string(dstReg->WriteMask));
Brian501ee872007-02-17 09:15:00 -0700503#endif
Brian00cdc0a2006-12-14 15:01:06 -0700504}
505
Brian Pauld0038932009-01-22 10:34:43 -0700506
Brian00cdc0a2006-12-14 15:01:06 -0700507static void
Brian Pauld0038932009-01-22 10:34:43 -0700508fprint_src_reg(FILE *f,
509 const struct prog_src_register *srcReg,
510 gl_prog_print_mode mode,
511 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700512{
Brian Paulf787baf2009-03-05 17:14:05 -0700513 const char *abs = srcReg->Abs ? "|" : "";
514
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500515 fprintf(f, "%s%s%s%s",
516 abs,
517 reg_string((gl_register_file) srcReg->File,
Zack Rusinb4855282010-07-09 21:19:28 -0400518 srcReg->Index, mode, srcReg->RelAddr, prog,
Zack Rusin1491c6a2010-07-13 21:49:53 -0400519 srcReg->HasIndex2, srcReg->RelAddr2, srcReg->Index2),
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500520 _mesa_swizzle_string(srcReg->Swizzle,
521 srcReg->Negate, GL_FALSE),
522 abs);
Brian501ee872007-02-17 09:15:00 -0700523#if 0
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500524 fprintf(f, "%s[%d]%s",
525 file_string((gl_register_file) srcReg->File, mode),
526 srcReg->Index,
527 _mesa_swizzle_string(srcReg->Swizzle,
528 srcReg->Negate, GL_FALSE));
Brian501ee872007-02-17 09:15:00 -0700529#endif
Brian00cdc0a2006-12-14 15:01:06 -0700530}
531
Brian Pauld0038932009-01-22 10:34:43 -0700532
Brian00cdc0a2006-12-14 15:01:06 -0700533static void
Brian Pauld0038932009-01-22 10:34:43 -0700534fprint_comment(FILE *f, const struct prog_instruction *inst)
Brian00cdc0a2006-12-14 15:01:06 -0700535{
536 if (inst->Comment)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500537 fprintf(f, "; # %s\n", inst->Comment);
Brian00cdc0a2006-12-14 15:01:06 -0700538 else
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500539 fprintf(f, ";\n");
Brian00cdc0a2006-12-14 15:01:06 -0700540}
541
542
Eric Anholt72fd0562010-08-04 19:55:52 -0700543void
544_mesa_fprint_alu_instruction(FILE *f,
545 const struct prog_instruction *inst,
546 const char *opcode_string, GLuint numRegs,
547 gl_prog_print_mode mode,
548 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700549{
550 GLuint j;
551
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500552 fprintf(f, "%s", opcode_string);
Brianb50280e2006-12-18 16:21:58 -0700553 if (inst->CondUpdate)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500554 fprintf(f, ".C");
Brian00cdc0a2006-12-14 15:01:06 -0700555
556 /* frag prog only */
557 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500558 fprintf(f, "_SAT");
Brian00cdc0a2006-12-14 15:01:06 -0700559
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500560 fprintf(f, " ");
Brian00cdc0a2006-12-14 15:01:06 -0700561 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
Brian Pauld0038932009-01-22 10:34:43 -0700562 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700563 }
564 else {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500565 fprintf(f, " ???");
Brian00cdc0a2006-12-14 15:01:06 -0700566 }
567
568 if (numRegs > 0)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500569 fprintf(f, ", ");
Brian00cdc0a2006-12-14 15:01:06 -0700570
571 for (j = 0; j < numRegs; j++) {
Brian Pauld0038932009-01-22 10:34:43 -0700572 fprint_src_reg(f, inst->SrcReg + j, mode, prog);
Brian00cdc0a2006-12-14 15:01:06 -0700573 if (j + 1 < numRegs)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500574 fprintf(f, ", ");
Brian00cdc0a2006-12-14 15:01:06 -0700575 }
576
Brian Pauld0038932009-01-22 10:34:43 -0700577 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700578}
579
580
Brian501ee872007-02-17 09:15:00 -0700581void
Brian0020d102007-02-23 11:43:44 -0700582_mesa_print_alu_instruction(const struct prog_instruction *inst,
583 const char *opcode_string, GLuint numRegs)
584{
Eric Anholt72fd0562010-08-04 19:55:52 -0700585 _mesa_fprint_alu_instruction(stderr, inst, opcode_string,
586 numRegs, PROG_PRINT_DEBUG, NULL);
Brian501ee872007-02-17 09:15:00 -0700587}
588
589
Brian00cdc0a2006-12-14 15:01:06 -0700590/**
591 * Print a single vertex/fragment program instruction.
592 */
Nicolai Hähnle2237d132009-07-23 22:09:11 +0200593GLint
Brian Pauld0038932009-01-22 10:34:43 -0700594_mesa_fprint_instruction_opt(FILE *f,
595 const struct prog_instruction *inst,
596 GLint indent,
Brian501ee872007-02-17 09:15:00 -0700597 gl_prog_print_mode mode,
598 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700599{
Brian7b300532007-02-22 09:08:36 -0700600 GLint i;
Brian5db088d2007-02-05 14:58:15 -0700601
602 if (inst->Opcode == OPCODE_ELSE ||
603 inst->Opcode == OPCODE_ENDIF ||
604 inst->Opcode == OPCODE_ENDLOOP ||
605 inst->Opcode == OPCODE_ENDSUB) {
606 indent -= 3;
607 }
Brian5db088d2007-02-05 14:58:15 -0700608 for (i = 0; i < indent; i++) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500609 fprintf(f, " ");
Brian5db088d2007-02-05 14:58:15 -0700610 }
611
Brian00cdc0a2006-12-14 15:01:06 -0700612 switch (inst->Opcode) {
613 case OPCODE_PRINT:
Brian Paul8de5a292010-02-19 12:45:49 -0700614 fprintf(f, "PRINT '%s'", (char *) inst->Data);
Brian00cdc0a2006-12-14 15:01:06 -0700615 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500616 fprintf(f, ", ");
617 fprintf(f, "%s[%d]%s",
618 file_string((gl_register_file) inst->SrcReg[0].File,
619 mode),
620 inst->SrcReg[0].Index,
621 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
622 inst->SrcReg[0].Negate, GL_FALSE));
Brian00cdc0a2006-12-14 15:01:06 -0700623 }
624 if (inst->Comment)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500625 fprintf(f, " # %s", inst->Comment);
Brian Pauld0038932009-01-22 10:34:43 -0700626 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700627 break;
628 case OPCODE_SWZ:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500629 fprintf(f, "SWZ");
Brian00cdc0a2006-12-14 15:01:06 -0700630 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500631 fprintf(f, "_SAT");
632 fprintf(f, " ");
Brian Pauld0038932009-01-22 10:34:43 -0700633 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500634 fprintf(f, ", %s[%d], %s",
635 file_string((gl_register_file) inst->SrcReg[0].File,
636 mode),
637 inst->SrcReg[0].Index,
638 _mesa_swizzle_string(inst->SrcReg[0].Swizzle,
639 inst->SrcReg[0].Negate, GL_TRUE));
Brian Pauld0038932009-01-22 10:34:43 -0700640 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700641 break;
642 case OPCODE_TEX:
643 case OPCODE_TXP:
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600644 case OPCODE_TXL:
Brian00cdc0a2006-12-14 15:01:06 -0700645 case OPCODE_TXB:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500646 fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
Brian00cdc0a2006-12-14 15:01:06 -0700647 if (inst->SaturateMode == SATURATE_ZERO_ONE)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500648 fprintf(f, "_SAT");
649 fprintf(f, " ");
Brian Pauld0038932009-01-22 10:34:43 -0700650 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500651 fprintf(f, ", ");
Brian Pauld0038932009-01-22 10:34:43 -0700652 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500653 fprintf(f, ", texture[%d], ", inst->TexSrcUnit);
Brian00cdc0a2006-12-14 15:01:06 -0700654 switch (inst->TexSrcTarget) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500655 case TEXTURE_1D_INDEX: fprintf(f, "1D"); break;
656 case TEXTURE_2D_INDEX: fprintf(f, "2D"); break;
657 case TEXTURE_3D_INDEX: fprintf(f, "3D"); break;
658 case TEXTURE_CUBE_INDEX: fprintf(f, "CUBE"); break;
659 case TEXTURE_RECT_INDEX: fprintf(f, "RECT"); break;
Brian Paul2c161982010-02-25 19:04:21 -0700660 case TEXTURE_1D_ARRAY_INDEX: fprintf(f, "1D_ARRAY"); break;
661 case TEXTURE_2D_ARRAY_INDEX: fprintf(f, "2D_ARRAY"); break;
Brian00cdc0a2006-12-14 15:01:06 -0700662 default:
663 ;
664 }
Brian Paul44e018c2009-02-20 13:42:08 -0700665 if (inst->TexShadow)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500666 fprintf(f, " SHADOW");
Brian Pauld0038932009-01-22 10:34:43 -0700667 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700668 break;
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600669
670 case OPCODE_KIL:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500671 fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
672 fprintf(f, " ");
Brian Pauld0038932009-01-22 10:34:43 -0700673 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
674 fprint_comment(f, inst);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600675 break;
676 case OPCODE_KIL_NV:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500677 fprintf(f, "%s", _mesa_opcode_string(inst->Opcode));
678 fprintf(f, " ");
679 fprintf(f, "%s.%s",
680 _mesa_condcode_string(inst->DstReg.CondMask),
681 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
682 GL_FALSE, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700683 fprint_comment(f, inst);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600684 break;
685
Brian00cdc0a2006-12-14 15:01:06 -0700686 case OPCODE_ARL:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500687 fprintf(f, "ARL ");
Brian Pauld0038932009-01-22 10:34:43 -0700688 fprint_dst_reg(f, &inst->DstReg, mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500689 fprintf(f, ", ");
Brian Pauld0038932009-01-22 10:34:43 -0700690 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
691 fprint_comment(f, inst);
Brian00cdc0a2006-12-14 15:01:06 -0700692 break;
693 case OPCODE_BRA:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500694 fprintf(f, "BRA %d (%s%s)",
695 inst->BranchTarget,
696 _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);
Brian00cdc0a2006-12-14 15:01:06 -0700699 break;
Brian5ae49cf2007-01-20 09:27:40 -0700700 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600701 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
702 /* Use ordinary register */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500703 fprintf(f, "IF ");
Brian Pauld0038932009-01-22 10:34:43 -0700704 fprint_src_reg(f, &inst->SrcReg[0], mode, prog);
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500705 fprintf(f, "; ");
Brian63556fa2007-03-23 14:47:46 -0600706 }
707 else {
708 /* Use cond codes */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500709 fprintf(f, "IF (%s%s);",
710 _mesa_condcode_string(inst->DstReg.CondMask),
711 _mesa_swizzle_string(inst->DstReg.CondSwizzle,
712 0, GL_FALSE));
Brian63556fa2007-03-23 14:47:46 -0600713 }
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500714 fprintf(f, " # (if false, goto %d)", inst->BranchTarget);
Brian Pauld0038932009-01-22 10:34:43 -0700715 fprint_comment(f, inst);
Brian5db088d2007-02-05 14:58:15 -0700716 return indent + 3;
717 case OPCODE_ELSE:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500718 fprintf(f, "ELSE; # (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700719 return indent + 3;
720 case OPCODE_ENDIF:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500721 fprintf(f, "ENDIF;\n");
Brian5db088d2007-02-05 14:58:15 -0700722 break;
723 case OPCODE_BGNLOOP:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500724 fprintf(f, "BGNLOOP; # (end at %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700725 return indent + 3;
726 case OPCODE_ENDLOOP:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500727 fprintf(f, "ENDLOOP; # (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700728 break;
729 case OPCODE_BRK:
Brianf22ed092007-02-06 22:31:19 -0700730 case OPCODE_CONT:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500731 fprintf(f, "%s (%s%s); # (goto %d)",
732 _mesa_opcode_string(inst->Opcode),
733 _mesa_condcode_string(inst->DstReg.CondMask),
734 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
735 inst->BranchTarget);
Brian Pauld0038932009-01-22 10:34:43 -0700736 fprint_comment(f, inst);
Brian5ae49cf2007-01-20 09:27:40 -0700737 break;
Brian63556fa2007-03-23 14:47:46 -0600738
Brian5db088d2007-02-05 14:58:15 -0700739 case OPCODE_BGNSUB:
Brianf407cad2007-03-27 14:53:17 -0600740 if (mode == PROG_PRINT_NV) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500741 fprintf(f, "%s:\n", inst->Comment); /* comment is label */
Brianf407cad2007-03-27 14:53:17 -0600742 return indent;
743 }
744 else {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500745 fprintf(f, "BGNSUB");
Brian Pauld0038932009-01-22 10:34:43 -0700746 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600747 return indent + 3;
748 }
Brian5db088d2007-02-05 14:58:15 -0700749 case OPCODE_ENDSUB:
Brianf407cad2007-03-27 14:53:17 -0600750 if (mode == PROG_PRINT_DEBUG) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500751 fprintf(f, "ENDSUB");
Brian Pauld0038932009-01-22 10:34:43 -0700752 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600753 }
754 break;
755 case OPCODE_CAL:
756 if (mode == PROG_PRINT_NV) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500757 fprintf(f, "CAL %s; # (goto %d)\n", inst->Comment, inst->BranchTarget);
Brianf407cad2007-03-27 14:53:17 -0600758 }
759 else {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500760 fprintf(f, "CAL %u", inst->BranchTarget);
Brian Pauld0038932009-01-22 10:34:43 -0700761 fprint_comment(f, inst);
Brianf407cad2007-03-27 14:53:17 -0600762 }
763 break;
764 case OPCODE_RET:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500765 fprintf(f, "RET (%s%s)",
766 _mesa_condcode_string(inst->DstReg.CondMask),
767 _mesa_swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
Brian Pauld0038932009-01-22 10:34:43 -0700768 fprint_comment(f, inst);
Brian5ae49cf2007-01-20 09:27:40 -0700769 break;
Brianf407cad2007-03-27 14:53:17 -0600770
Brian00cdc0a2006-12-14 15:01:06 -0700771 case OPCODE_END:
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500772 fprintf(f, "END\n");
Brian00cdc0a2006-12-14 15:01:06 -0700773 break;
Brian3a281532006-12-16 12:51:34 -0700774 case OPCODE_NOP:
Brian059376c2007-02-22 17:45:32 -0700775 if (mode == PROG_PRINT_DEBUG) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500776 fprintf(f, "NOP");
Brian Pauld0038932009-01-22 10:34:43 -0700777 fprint_comment(f, inst);
Brian059376c2007-02-22 17:45:32 -0700778 }
779 else if (inst->Comment) {
780 /* ARB/NV extensions don't have NOP instruction */
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500781 fprintf(f, "# %s\n", inst->Comment);
Brian059376c2007-02-22 17:45:32 -0700782 }
Brian3a281532006-12-16 12:51:34 -0700783 break;
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400784 case OPCODE_EMIT_VERTEX:
785 fprintf(f, "EMIT_VERTEX\n");
786 break;
787 case OPCODE_END_PRIMITIVE:
788 fprintf(f, "END_PRIMITIVE\n");
789 break;
Brian00cdc0a2006-12-14 15:01:06 -0700790 /* XXX may need other special-case instructions */
791 default:
Brian Paulf787baf2009-03-05 17:14:05 -0700792 if (inst->Opcode < MAX_OPCODE) {
793 /* typical alu instruction */
Eric Anholt72fd0562010-08-04 19:55:52 -0700794 _mesa_fprint_alu_instruction(f, inst,
795 _mesa_opcode_string(inst->Opcode),
796 _mesa_num_inst_src_regs(inst->Opcode),
797 mode, prog);
Brian Paulf787baf2009-03-05 17:14:05 -0700798 }
799 else {
Eric Anholt72fd0562010-08-04 19:55:52 -0700800 _mesa_fprint_alu_instruction(f, inst,
801 _mesa_opcode_string(inst->Opcode),
802 3/*_mesa_num_inst_src_regs(inst->Opcode)*/,
803 mode, prog);
Brian Paulf787baf2009-03-05 17:14:05 -0700804 }
Brian00cdc0a2006-12-14 15:01:06 -0700805 break;
806 }
Brian5db088d2007-02-05 14:58:15 -0700807 return indent;
Brian00cdc0a2006-12-14 15:01:06 -0700808}
809
810
Brian Pauld0038932009-01-22 10:34:43 -0700811GLint
812_mesa_print_instruction_opt(const struct prog_instruction *inst,
813 GLint indent,
814 gl_prog_print_mode mode,
815 const struct gl_program *prog)
816{
marvin2443a064e2009-08-27 09:22:51 -0600817 return _mesa_fprint_instruction_opt(stderr, inst, indent, mode, prog);
Brian Pauld0038932009-01-22 10:34:43 -0700818}
819
820
821void
822_mesa_print_instruction(const struct prog_instruction *inst)
823{
824 /* note: 4th param should be ignored for PROG_PRINT_DEBUG */
marvin2443a064e2009-08-27 09:22:51 -0600825 _mesa_fprint_instruction_opt(stderr, inst, 0, PROG_PRINT_DEBUG, NULL);
Brian Pauld0038932009-01-22 10:34:43 -0700826}
827
828
829
830/**
831 * Print program, with options.
832 */
Brian Pauld2eff332009-02-02 12:24:41 -0700833void
Brian Pauld0038932009-01-22 10:34:43 -0700834_mesa_fprint_program_opt(FILE *f,
835 const struct gl_program *prog,
836 gl_prog_print_mode mode,
837 GLboolean lineNumbers)
838{
839 GLuint i, indent = 0;
840
841 switch (prog->Target) {
842 case GL_VERTEX_PROGRAM_ARB:
843 if (mode == PROG_PRINT_ARB)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500844 fprintf(f, "!!ARBvp1.0\n");
Brian Pauld0038932009-01-22 10:34:43 -0700845 else if (mode == PROG_PRINT_NV)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500846 fprintf(f, "!!VP1.0\n");
Brian Pauld0038932009-01-22 10:34:43 -0700847 else
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500848 fprintf(f, "# Vertex Program/Shader %u\n", prog->Id);
Brian Pauld0038932009-01-22 10:34:43 -0700849 break;
850 case GL_FRAGMENT_PROGRAM_ARB:
851 case GL_FRAGMENT_PROGRAM_NV:
852 if (mode == PROG_PRINT_ARB)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500853 fprintf(f, "!!ARBfp1.0\n");
Brian Pauld0038932009-01-22 10:34:43 -0700854 else if (mode == PROG_PRINT_NV)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500855 fprintf(f, "!!FP1.0\n");
Brian Pauld0038932009-01-22 10:34:43 -0700856 else
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500857 fprintf(f, "# Fragment Program/Shader %u\n", prog->Id);
Brian Pauld0038932009-01-22 10:34:43 -0700858 break;
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400859 case MESA_GEOMETRY_PROGRAM:
860 fprintf(f, "# Geometry Shader\n");
Brian Pauld0038932009-01-22 10:34:43 -0700861 }
862
863 for (i = 0; i < prog->NumInstructions; i++) {
864 if (lineNumbers)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500865 fprintf(f, "%3d: ", i);
Brian Pauld0038932009-01-22 10:34:43 -0700866 indent = _mesa_fprint_instruction_opt(f, prog->Instructions + i,
867 indent, mode, prog);
868 }
869}
870
871
Brian00cdc0a2006-12-14 15:01:06 -0700872/**
marvin2443a064e2009-08-27 09:22:51 -0600873 * Print program to stderr, default options.
Brian00cdc0a2006-12-14 15:01:06 -0700874 */
875void
876_mesa_print_program(const struct gl_program *prog)
877{
Ian Romanicka7400e72009-07-24 18:03:02 -0700878 _mesa_fprint_program_opt(stderr, prog, PROG_PRINT_DEBUG, GL_TRUE);
Brian501ee872007-02-17 09:15:00 -0700879}
880
881
882/**
Brian Paulb5ddc782009-12-22 13:28:39 -0700883 * Return binary representation of 64-bit value (as a string).
Brian Paulf9ce0a92009-08-25 16:11:39 -0600884 * Insert a comma to separate each group of 8 bits.
Brian Paulb5ddc782009-12-22 13:28:39 -0700885 * Note we return a pointer to local static storage so this is not
886 * re-entrant, etc.
Brian Paulf9ce0a92009-08-25 16:11:39 -0600887 * XXX move to imports.[ch] if useful elsewhere.
888 */
889static const char *
Ian Romanick5606dfb2009-11-17 16:10:24 -0800890binary(GLbitfield64 val)
Brian Paulf9ce0a92009-08-25 16:11:39 -0600891{
Ian Romanick5606dfb2009-11-17 16:10:24 -0800892 static char buf[80];
Brian Paulf9ce0a92009-08-25 16:11:39 -0600893 GLint i, len = 0;
Ian Romanick5606dfb2009-11-17 16:10:24 -0800894 for (i = 63; i >= 0; --i) {
Brian Paule57689c2010-06-02 14:48:27 -0600895 if (val & (BITFIELD64_BIT(i)))
Brian Paulf9ce0a92009-08-25 16:11:39 -0600896 buf[len++] = '1';
897 else if (len > 0 || i == 0)
898 buf[len++] = '0';
899 if (len > 0 && ((i-1) % 8) == 7)
900 buf[len++] = ',';
901 }
902 buf[len] = '\0';
903 return buf;
904}
905
906
907/**
Brian Pauld0038932009-01-22 10:34:43 -0700908 * Print all of a program's parameters/fields to given file.
Brian501ee872007-02-17 09:15:00 -0700909 */
Brian Pauld0038932009-01-22 10:34:43 -0700910static void
911_mesa_fprint_program_parameters(FILE *f,
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400912 struct gl_context *ctx,
Brian Pauld0038932009-01-22 10:34:43 -0700913 const struct gl_program *prog)
Brian00cdc0a2006-12-14 15:01:06 -0700914{
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600915 GLuint i;
916
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500917 fprintf(f, "InputsRead: 0x%x (0b%s)\n",
Brian Paulf9ce0a92009-08-25 16:11:39 -0600918 prog->InputsRead, binary(prog->InputsRead));
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500919 fprintf(f, "OutputsWritten: 0x%llx (0b%s)\n",
José Fonseca53d3f0c2010-07-02 11:40:36 +0100920 (unsigned long long)prog->OutputsWritten,
921 binary(prog->OutputsWritten));
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500922 fprintf(f, "NumInstructions=%d\n", prog->NumInstructions);
923 fprintf(f, "NumTemporaries=%d\n", prog->NumTemporaries);
924 fprintf(f, "NumParameters=%d\n", prog->NumParameters);
925 fprintf(f, "NumAttributes=%d\n", prog->NumAttributes);
926 fprintf(f, "NumAddressRegs=%d\n", prog->NumAddressRegs);
Brian Paul56643092010-07-30 14:24:23 -0600927 fprintf(f, "IndirectRegisterFiles: 0x%x (0b%s)\n",
928 prog->IndirectRegisterFiles, binary(prog->IndirectRegisterFiles));
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500929 fprintf(f, "SamplersUsed: 0x%x (0b%s)\n",
Brian Paulf9ce0a92009-08-25 16:11:39 -0600930 prog->SamplersUsed, binary(prog->SamplersUsed));
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500931 fprintf(f, "Samplers=[ ");
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600932 for (i = 0; i < MAX_SAMPLERS; i++) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500933 fprintf(f, "%d ", prog->SamplerUnits[i]);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600934 }
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500935 fprintf(f, "]\n");
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600936
Brian00cdc0a2006-12-14 15:01:06 -0700937 _mesa_load_state_parameters(ctx, prog->Parameters);
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600938
Brian00cdc0a2006-12-14 15:01:06 -0700939#if 0
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500940 fprintf(f, "Local Params:\n");
Brian00cdc0a2006-12-14 15:01:06 -0700941 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
942 const GLfloat *p = prog->LocalParams[i];
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500943 fprintf(f, "%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
Brian00cdc0a2006-12-14 15:01:06 -0700944 }
945#endif
Brian83ca3ff2006-12-20 17:17:38 -0700946 _mesa_print_parameter_list(prog->Parameters);
947}
Brian00cdc0a2006-12-14 15:01:06 -0700948
Brian83ca3ff2006-12-20 17:17:38 -0700949
Brian Pauld0038932009-01-22 10:34:43 -0700950/**
marvin2443a064e2009-08-27 09:22:51 -0600951 * Print all of a program's parameters/fields to stderr.
Brian Pauld0038932009-01-22 10:34:43 -0700952 */
Brian83ca3ff2006-12-20 17:17:38 -0700953void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400954_mesa_print_program_parameters(struct gl_context *ctx, const struct gl_program *prog)
Brian Pauld0038932009-01-22 10:34:43 -0700955{
marvin2443a064e2009-08-27 09:22:51 -0600956 _mesa_fprint_program_parameters(stderr, ctx, prog);
Brian Pauld0038932009-01-22 10:34:43 -0700957}
958
959
960/**
961 * Print a program parameter list to given file.
962 */
963static void
964_mesa_fprint_parameter_list(FILE *f,
965 const struct gl_program_parameter_list *list)
Brian83ca3ff2006-12-20 17:17:38 -0700966{
Brian501ee872007-02-17 09:15:00 -0700967 const gl_prog_print_mode mode = PROG_PRINT_DEBUG;
Brian83ca3ff2006-12-20 17:17:38 -0700968 GLuint i;
Brian501ee872007-02-17 09:15:00 -0700969
Brian Paul4b6b0fd2008-05-18 15:41:01 -0600970 if (!list)
971 return;
972
Brian Pauldfefde32009-10-13 16:18:06 -0600973 if (0)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500974 fprintf(f, "param list %p\n", (void *) list);
975 fprintf(f, "dirty state flags: 0x%x\n", list->StateFlags);
Brian83ca3ff2006-12-20 17:17:38 -0700976 for (i = 0; i < list->NumParameters; i++){
977 struct gl_program_parameter *param = list->Parameters + i;
978 const GLfloat *v = list->ParameterValues[i];
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500979 fprintf(f, "param[%d] sz=%d %s %s = {%.3g, %.3g, %.3g, %.3g}",
980 i, param->Size,
981 file_string(list->Parameters[i].Type, mode),
982 param->Name, v[0], v[1], v[2], v[3]);
Brian Paulf490ec92008-11-24 09:04:52 -0700983 if (param->Flags & PROG_PARAM_BIT_CENTROID)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500984 fprintf(f, " Centroid");
Brian Paulf490ec92008-11-24 09:04:52 -0700985 if (param->Flags & PROG_PARAM_BIT_INVARIANT)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500986 fprintf(f, " Invariant");
Brian Paulf490ec92008-11-24 09:04:52 -0700987 if (param->Flags & PROG_PARAM_BIT_FLAT)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500988 fprintf(f, " Flat");
Brian Paulf490ec92008-11-24 09:04:52 -0700989 if (param->Flags & PROG_PARAM_BIT_LINEAR)
Kristian Høgsberg298be2b2010-02-19 12:32:24 -0500990 fprintf(f, " Linear");
991 fprintf(f, "\n");
Brian00cdc0a2006-12-14 15:01:06 -0700992 }
993}
Brian Pauld0038932009-01-22 10:34:43 -0700994
995
996/**
marvin2443a064e2009-08-27 09:22:51 -0600997 * Print a program parameter list to stderr.
Brian Pauld0038932009-01-22 10:34:43 -0700998 */
999void
1000_mesa_print_parameter_list(const struct gl_program_parameter_list *list)
1001{
marvin2443a064e2009-08-27 09:22:51 -06001002 _mesa_fprint_parameter_list(stderr, list);
Brian Pauld0038932009-01-22 10:34:43 -07001003}
1004
1005
1006/**
1007 * Write shader and associated info to a file.
1008 */
1009void
1010_mesa_write_shader_to_file(const struct gl_shader *shader)
1011{
1012 const char *type;
1013 char filename[100];
1014 FILE *f;
1015
1016 if (shader->Type == GL_FRAGMENT_SHADER)
1017 type = "frag";
Zack Rusinda7bd6a2010-06-28 17:31:21 -04001018 else if (shader->Type == GL_VERTEX_SHADER)
Brian Pauld0038932009-01-22 10:34:43 -07001019 type = "vert";
Zack Rusinda7bd6a2010-06-28 17:31:21 -04001020 else
1021 type = "geom";
Brian Pauld0038932009-01-22 10:34:43 -07001022
Brian Paul78a0c352010-02-19 12:56:49 -07001023 _mesa_snprintf(filename, sizeof(filename), "shader_%u.%s", shader->Name, type);
Brian Pauld0038932009-01-22 10:34:43 -07001024 f = fopen(filename, "w");
1025 if (!f) {
1026 fprintf(stderr, "Unable to open %s for writing\n", filename);
1027 return;
1028 }
1029
Brian Paula746ef22009-08-04 15:36:13 -06001030 fprintf(f, "/* Shader %u source, checksum %u */\n", shader->Name, shader->SourceChecksum);
Brian Pauld0038932009-01-22 10:34:43 -07001031 fputs(shader->Source, f);
1032 fprintf(f, "\n");
1033
1034 fprintf(f, "/* Compile status: %s */\n",
1035 shader->CompileStatus ? "ok" : "fail");
Eric Anholt8e181b62010-08-04 14:13:08 -07001036 fprintf(f, "/* Log Info: */\n");
Brian Paul2d83e3f2010-08-12 15:57:54 -06001037 if (shader->InfoLog) {
1038 fputs(shader->InfoLog, f);
1039 }
Eric Anholt8e181b62010-08-04 14:13:08 -07001040 if (shader->CompileStatus && shader->Program) {
Brian Pauld0038932009-01-22 10:34:43 -07001041 fprintf(f, "/* GPU code */\n");
Brian Paul6ce0c6e2009-02-06 10:20:33 -07001042 fprintf(f, "/*\n");
Brian Pauld0038932009-01-22 10:34:43 -07001043 _mesa_fprint_program_opt(f, shader->Program, PROG_PRINT_DEBUG, GL_TRUE);
Brian Paul6ce0c6e2009-02-06 10:20:33 -07001044 fprintf(f, "*/\n");
Brian Paulf95c0c02009-05-04 11:14:35 -06001045 fprintf(f, "/* Parameters / constants */\n");
1046 fprintf(f, "/*\n");
1047 _mesa_fprint_parameter_list(f, shader->Program->Parameters);
1048 fprintf(f, "*/\n");
Brian Pauld0038932009-01-22 10:34:43 -07001049 }
1050
1051 fclose(f);
1052}
1053
1054
Brian Pauldb598b82009-08-14 11:26:20 -06001055/**
1056 * Append the shader's uniform info/values to the shader log file.
1057 * The log file will typically have been created by the
1058 * _mesa_write_shader_to_file function.
1059 */
1060void
Brian Paul12199ed2009-08-14 12:57:39 -06001061_mesa_append_uniforms_to_file(const struct gl_shader *shader,
1062 const struct gl_program *prog)
Brian Pauldb598b82009-08-14 11:26:20 -06001063{
1064 const char *type;
1065 char filename[100];
1066 FILE *f;
1067
1068 if (shader->Type == GL_FRAGMENT_SHADER)
1069 type = "frag";
1070 else
1071 type = "vert";
1072
Brian Paul78a0c352010-02-19 12:56:49 -07001073 _mesa_snprintf(filename, sizeof(filename), "shader_%u.%s", shader->Name, type);
Brian Pauldb598b82009-08-14 11:26:20 -06001074 f = fopen(filename, "a"); /* append */
1075 if (!f) {
1076 fprintf(stderr, "Unable to open %s for appending\n", filename);
1077 return;
1078 }
1079
1080 fprintf(f, "/* First-draw parameters / constants */\n");
1081 fprintf(f, "/*\n");
Brian Paul12199ed2009-08-14 12:57:39 -06001082 _mesa_fprint_parameter_list(f, prog->Parameters);
Brian Pauldb598b82009-08-14 11:26:20 -06001083 fprintf(f, "*/\n");
1084
1085 fclose(f);
1086}