blob: 6c303de9b5a40bbe64227d10aecbcd68d6dd66a5 [file] [log] [blame]
Brian00cdc0a2006-12-14 15:01:06 -07001/*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
Brian5db088d2007-02-05 14:58:15 -07005 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Brian00cdc0a2006-12-14 15:01:06 -07006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/**
26 * \file prog_print.c
27 * Print vertex/fragment programs - for debugging.
28 * \author Brian Paul
29 */
30
31#include "glheader.h"
32#include "context.h"
33#include "imports.h"
Brian00cdc0a2006-12-14 15:01:06 -070034#include "prog_instruction.h"
35#include "prog_parameter.h"
36#include "prog_print.h"
37#include "prog_statevars.h"
38
39
Brian00cdc0a2006-12-14 15:01:06 -070040/**
41 * Return string name for given program/register file.
42 */
43static const char *
44program_file_string(enum register_file f)
45{
46 switch (f) {
47 case PROGRAM_TEMPORARY:
48 return "TEMP";
49 case PROGRAM_LOCAL_PARAM:
50 return "LOCAL";
51 case PROGRAM_ENV_PARAM:
52 return "ENV";
53 case PROGRAM_STATE_VAR:
54 return "STATE";
55 case PROGRAM_INPUT:
56 return "INPUT";
57 case PROGRAM_OUTPUT:
58 return "OUTPUT";
59 case PROGRAM_NAMED_PARAM:
60 return "NAMED";
61 case PROGRAM_CONSTANT:
62 return "CONST";
63 case PROGRAM_UNIFORM:
64 return "UNIFORM";
65 case PROGRAM_VARYING:
66 return "VARYING";
67 case PROGRAM_WRITE_ONLY:
68 return "WRITE_ONLY";
69 case PROGRAM_ADDRESS:
70 return "ADDR";
Brianb2ab6932007-01-05 16:01:43 -070071 case PROGRAM_SAMPLER:
72 return "SAMPLER";
Brian00cdc0a2006-12-14 15:01:06 -070073 default:
74 return "Unknown program file!";
75 }
76}
77
78
79/**
80 * Return a string representation of the given swizzle word.
81 * If extended is true, use extended (comma-separated) format.
Brian3a281532006-12-16 12:51:34 -070082 * \param swizzle the swizzle field
83 * \param negateBase 4-bit negation vector
84 * \param extended if true, also allow 0, 1 values
Brian00cdc0a2006-12-14 15:01:06 -070085 */
86static const char *
87swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
88{
Brianb63c1002007-01-31 16:34:54 -070089 static const char swz[] = "xyzw01?!";
Brian00cdc0a2006-12-14 15:01:06 -070090 static char s[20];
91 GLuint i = 0;
92
93 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
94 return ""; /* no swizzle/negation */
95
96 if (!extended)
97 s[i++] = '.';
98
99 if (negateBase & 0x1)
100 s[i++] = '-';
101 s[i++] = swz[GET_SWZ(swizzle, 0)];
102
103 if (extended) {
104 s[i++] = ',';
105 }
106
107 if (negateBase & 0x2)
108 s[i++] = '-';
109 s[i++] = swz[GET_SWZ(swizzle, 1)];
110
111 if (extended) {
112 s[i++] = ',';
113 }
114
115 if (negateBase & 0x4)
116 s[i++] = '-';
117 s[i++] = swz[GET_SWZ(swizzle, 2)];
118
119 if (extended) {
120 s[i++] = ',';
121 }
122
123 if (negateBase & 0x8)
124 s[i++] = '-';
125 s[i++] = swz[GET_SWZ(swizzle, 3)];
126
127 s[i] = 0;
128 return s;
129}
130
131
132static const char *
133writemask_string(GLuint writeMask)
134{
135 static char s[10];
136 GLuint i = 0;
137
138 if (writeMask == WRITEMASK_XYZW)
139 return "";
140
141 s[i++] = '.';
142 if (writeMask & WRITEMASK_X)
143 s[i++] = 'x';
144 if (writeMask & WRITEMASK_Y)
145 s[i++] = 'y';
146 if (writeMask & WRITEMASK_Z)
147 s[i++] = 'z';
148 if (writeMask & WRITEMASK_W)
149 s[i++] = 'w';
150
151 s[i] = 0;
152 return s;
153}
154
Brian3a281532006-12-16 12:51:34 -0700155
156static const char *
157condcode_string(GLuint condcode)
158{
159 switch (condcode) {
160 case COND_GT: return "GT";
161 case COND_EQ: return "EQ";
162 case COND_LT: return "LT";
163 case COND_UN: return "UN";
164 case COND_GE: return "GE";
165 case COND_LE: return "LE";
166 case COND_NE: return "NE";
167 case COND_TR: return "TR";
168 case COND_FL: return "FL";
169 default: return "cond???";
170 }
171}
172
173
Brian00cdc0a2006-12-14 15:01:06 -0700174static void
175print_dst_reg(const struct prog_dst_register *dstReg)
176{
177 _mesa_printf(" %s[%d]%s",
178 program_file_string((enum register_file) dstReg->File),
179 dstReg->Index,
180 writemask_string(dstReg->WriteMask));
181}
182
183static void
184print_src_reg(const struct prog_src_register *srcReg)
185{
186 _mesa_printf("%s[%d]%s",
187 program_file_string((enum register_file) srcReg->File),
188 srcReg->Index,
189 swizzle_string(srcReg->Swizzle,
190 srcReg->NegateBase, GL_FALSE));
191}
192
193static void
194print_comment(const struct prog_instruction *inst)
195{
196 if (inst->Comment)
197 _mesa_printf("; # %s\n", inst->Comment);
198 else
199 _mesa_printf(";\n");
200}
201
202
203void
204_mesa_print_alu_instruction(const struct prog_instruction *inst,
205 const char *opcode_string,
206 GLuint numRegs)
207{
208 GLuint j;
209
210 _mesa_printf("%s", opcode_string);
Brianb50280e2006-12-18 16:21:58 -0700211 if (inst->CondUpdate)
212 _mesa_printf(".C");
Brian00cdc0a2006-12-14 15:01:06 -0700213
214 /* frag prog only */
215 if (inst->SaturateMode == SATURATE_ZERO_ONE)
216 _mesa_printf("_SAT");
217
218 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
Brianb50280e2006-12-18 16:21:58 -0700219 print_dst_reg(&inst->DstReg);
Brian00cdc0a2006-12-14 15:01:06 -0700220 }
221 else {
222 _mesa_printf(" ???");
223 }
224
225 if (numRegs > 0)
226 _mesa_printf(", ");
227
228 for (j = 0; j < numRegs; j++) {
229 print_src_reg(inst->SrcReg + j);
230 if (j + 1 < numRegs)
231 _mesa_printf(", ");
232 }
233
Brian00cdc0a2006-12-14 15:01:06 -0700234 print_comment(inst);
235}
236
237
238/**
239 * Print a single vertex/fragment program instruction.
240 */
Brian5db088d2007-02-05 14:58:15 -0700241GLint
242_mesa_print_instruction(const struct prog_instruction *inst, GLint indent)
Brian00cdc0a2006-12-14 15:01:06 -0700243{
Brian5db088d2007-02-05 14:58:15 -0700244 GLuint i;
245
246 if (inst->Opcode == OPCODE_ELSE ||
247 inst->Opcode == OPCODE_ENDIF ||
248 inst->Opcode == OPCODE_ENDLOOP ||
249 inst->Opcode == OPCODE_ENDSUB) {
250 indent -= 3;
251 }
252 assert(indent >= 0);
253 for (i = 0; i < indent; i++) {
254 _mesa_printf(" ");
255 }
256
Brian00cdc0a2006-12-14 15:01:06 -0700257 switch (inst->Opcode) {
258 case OPCODE_PRINT:
259 _mesa_printf("PRINT '%s'", inst->Data);
260 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
261 _mesa_printf(", ");
262 _mesa_printf("%s[%d]%s",
263 program_file_string((enum register_file) inst->SrcReg[0].File),
264 inst->SrcReg[0].Index,
265 swizzle_string(inst->SrcReg[0].Swizzle,
266 inst->SrcReg[0].NegateBase, GL_FALSE));
267 }
268 if (inst->Comment)
269 _mesa_printf(" # %s", inst->Comment);
270 print_comment(inst);
271 break;
272 case OPCODE_SWZ:
273 _mesa_printf("SWZ");
274 if (inst->SaturateMode == SATURATE_ZERO_ONE)
275 _mesa_printf("_SAT");
276 print_dst_reg(&inst->DstReg);
277 _mesa_printf("%s[%d], %s",
278 program_file_string((enum register_file) inst->SrcReg[0].File),
279 inst->SrcReg[0].Index,
280 swizzle_string(inst->SrcReg[0].Swizzle,
281 inst->SrcReg[0].NegateBase, GL_TRUE));
282 print_comment(inst);
283 break;
284 case OPCODE_TEX:
285 case OPCODE_TXP:
286 case OPCODE_TXB:
287 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
288 if (inst->SaturateMode == SATURATE_ZERO_ONE)
289 _mesa_printf("_SAT");
Brian00cdc0a2006-12-14 15:01:06 -0700290 print_dst_reg(&inst->DstReg);
291 _mesa_printf(", ");
292 print_src_reg(&inst->SrcReg[0]);
293 _mesa_printf(", texture[%d], ", inst->TexSrcUnit);
294 switch (inst->TexSrcTarget) {
295 case TEXTURE_1D_INDEX: _mesa_printf("1D"); break;
296 case TEXTURE_2D_INDEX: _mesa_printf("2D"); break;
297 case TEXTURE_3D_INDEX: _mesa_printf("3D"); break;
298 case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break;
299 case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break;
300 default:
301 ;
302 }
303 print_comment(inst);
304 break;
305 case OPCODE_ARL:
306 _mesa_printf("ARL addr.x, ");
307 print_src_reg(&inst->SrcReg[0]);
308 print_comment(inst);
309 break;
310 case OPCODE_BRA:
Brian5ae49cf2007-01-20 09:27:40 -0700311 _mesa_printf("BRA %u (%s%s)",
Brian3a281532006-12-16 12:51:34 -0700312 inst->BranchTarget,
313 condcode_string(inst->DstReg.CondMask),
314 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
Brian00cdc0a2006-12-14 15:01:06 -0700315 print_comment(inst);
316 break;
317 case OPCODE_CAL:
318 _mesa_printf("CAL %u", inst->BranchTarget);
319 print_comment(inst);
320 break;
Brian5ae49cf2007-01-20 09:27:40 -0700321 case OPCODE_IF:
Brian86080792007-02-05 17:18:10 -0700322 _mesa_printf("IF (%s%s) (if false, goto %d)",
Brian5db088d2007-02-05 14:58:15 -0700323 condcode_string(inst->DstReg.CondMask),
Brian86080792007-02-05 17:18:10 -0700324 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
325 inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700326 print_comment(inst);
327 return indent + 3;
328 case OPCODE_ELSE:
Brian86080792007-02-05 17:18:10 -0700329 _mesa_printf("ELSE (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700330 return indent + 3;
331 case OPCODE_ENDIF:
Briand9731b22007-02-05 15:17:06 -0700332 _mesa_printf("ENDIF\n");
Brian5db088d2007-02-05 14:58:15 -0700333 break;
334 case OPCODE_BGNLOOP:
Brian2755c792007-02-05 18:01:02 -0700335 _mesa_printf("BGNLOOP (end at %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700336 return indent + 3;
337 case OPCODE_ENDLOOP:
Briand9731b22007-02-05 15:17:06 -0700338 _mesa_printf("ENDLOOP (goto %d)\n", inst->BranchTarget);
Brian5db088d2007-02-05 14:58:15 -0700339 break;
340 case OPCODE_BRK:
Brianf22ed092007-02-06 22:31:19 -0700341 _mesa_printf("BRK (%s%s) (goto %d)",
342 condcode_string(inst->DstReg.CondMask),
343 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
344 inst->BranchTarget);
345 print_comment(inst);
346 break;
347 case OPCODE_CONT:
348 _mesa_printf("CONT (%s%s) (goto %d)",
Brian5ae49cf2007-01-20 09:27:40 -0700349 condcode_string(inst->DstReg.CondMask),
Brian2755c792007-02-05 18:01:02 -0700350 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE),
351 inst->BranchTarget);
Brian5ae49cf2007-01-20 09:27:40 -0700352 print_comment(inst);
353 break;
Brian5db088d2007-02-05 14:58:15 -0700354 case OPCODE_BGNSUB:
355 _mesa_printf("SUB;\n");
356 print_comment(inst);
357 return indent + 3;
358 case OPCODE_ENDSUB:
359 _mesa_printf("ENDSUB;\n");
360 print_comment(inst);
Brian5ae49cf2007-01-20 09:27:40 -0700361 break;
Brian00cdc0a2006-12-14 15:01:06 -0700362 case OPCODE_END:
363 _mesa_printf("END");
364 print_comment(inst);
365 break;
Brian3a281532006-12-16 12:51:34 -0700366 case OPCODE_NOP:
367 _mesa_printf("NOP");
368 print_comment(inst);
369 break;
Brian00cdc0a2006-12-14 15:01:06 -0700370 /* XXX may need other special-case instructions */
371 default:
372 /* typical alu instruction */
373 _mesa_print_alu_instruction(inst,
374 _mesa_opcode_string(inst->Opcode),
375 _mesa_num_inst_src_regs(inst->Opcode));
376 break;
377 }
Brian5db088d2007-02-05 14:58:15 -0700378 return indent;
Brian00cdc0a2006-12-14 15:01:06 -0700379}
380
381
382/**
383 * Print a vertx/fragment program to stdout.
384 * XXX this function could be greatly improved.
385 */
386void
387_mesa_print_program(const struct gl_program *prog)
388{
Brian5db088d2007-02-05 14:58:15 -0700389 GLuint i, indent = 0;
Brian00cdc0a2006-12-14 15:01:06 -0700390 for (i = 0; i < prog->NumInstructions; i++) {
391 _mesa_printf("%3d: ", i);
Brian5db088d2007-02-05 14:58:15 -0700392 indent = _mesa_print_instruction(prog->Instructions + i, indent);
Brian00cdc0a2006-12-14 15:01:06 -0700393 }
394}
395
396
397/**
398 * Print all of a program's parameters.
399 */
400void
401_mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
402{
Brian00cdc0a2006-12-14 15:01:06 -0700403 _mesa_printf("InputsRead: 0x%x\n", prog->InputsRead);
404 _mesa_printf("OutputsWritten: 0x%x\n", prog->OutputsWritten);
405 _mesa_printf("NumInstructions=%d\n", prog->NumInstructions);
406 _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries);
407 _mesa_printf("NumParameters=%d\n", prog->NumParameters);
408 _mesa_printf("NumAttributes=%d\n", prog->NumAttributes);
409 _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs);
410
411 _mesa_load_state_parameters(ctx, prog->Parameters);
412
413#if 0
414 _mesa_printf("Local Params:\n");
415 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
416 const GLfloat *p = prog->LocalParams[i];
417 _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
418 }
419#endif
Brian83ca3ff2006-12-20 17:17:38 -0700420 _mesa_print_parameter_list(prog->Parameters);
421}
Brian00cdc0a2006-12-14 15:01:06 -0700422
Brian83ca3ff2006-12-20 17:17:38 -0700423
424void
425_mesa_print_parameter_list(const struct gl_program_parameter_list *list)
426{
427 GLuint i;
428 _mesa_printf("param list %p\n", (void *) list);
429 for (i = 0; i < list->NumParameters; i++){
430 struct gl_program_parameter *param = list->Parameters + i;
431 const GLfloat *v = list->ParameterValues[i];
432 _mesa_printf("param[%d] sz=%d %s %s = {%.3f, %.3f, %.3f, %.3f};\n",
433 i, param->Size,
434 program_file_string(list->Parameters[i].Type),
Brian00cdc0a2006-12-14 15:01:06 -0700435 param->Name, v[0], v[1], v[2], v[3]);
436 }
437}