blob: 78ce752f2a2331ec0f2383575b126200ade09903 [file] [log] [blame]
Brian00cdc0a2006-12-14 15:01:06 -07001/*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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{
89 static const char swz[] = "xyzw01";
90 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 */
241void
242_mesa_print_instruction(const struct prog_instruction *inst)
243{
244 switch (inst->Opcode) {
245 case OPCODE_PRINT:
246 _mesa_printf("PRINT '%s'", inst->Data);
247 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
248 _mesa_printf(", ");
249 _mesa_printf("%s[%d]%s",
250 program_file_string((enum register_file) inst->SrcReg[0].File),
251 inst->SrcReg[0].Index,
252 swizzle_string(inst->SrcReg[0].Swizzle,
253 inst->SrcReg[0].NegateBase, GL_FALSE));
254 }
255 if (inst->Comment)
256 _mesa_printf(" # %s", inst->Comment);
257 print_comment(inst);
258 break;
259 case OPCODE_SWZ:
260 _mesa_printf("SWZ");
261 if (inst->SaturateMode == SATURATE_ZERO_ONE)
262 _mesa_printf("_SAT");
263 print_dst_reg(&inst->DstReg);
264 _mesa_printf("%s[%d], %s",
265 program_file_string((enum register_file) inst->SrcReg[0].File),
266 inst->SrcReg[0].Index,
267 swizzle_string(inst->SrcReg[0].Swizzle,
268 inst->SrcReg[0].NegateBase, GL_TRUE));
269 print_comment(inst);
270 break;
271 case OPCODE_TEX:
272 case OPCODE_TXP:
273 case OPCODE_TXB:
274 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
275 if (inst->SaturateMode == SATURATE_ZERO_ONE)
276 _mesa_printf("_SAT");
Brian00cdc0a2006-12-14 15:01:06 -0700277 print_dst_reg(&inst->DstReg);
278 _mesa_printf(", ");
279 print_src_reg(&inst->SrcReg[0]);
280 _mesa_printf(", texture[%d], ", inst->TexSrcUnit);
281 switch (inst->TexSrcTarget) {
282 case TEXTURE_1D_INDEX: _mesa_printf("1D"); break;
283 case TEXTURE_2D_INDEX: _mesa_printf("2D"); break;
284 case TEXTURE_3D_INDEX: _mesa_printf("3D"); break;
285 case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break;
286 case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break;
287 default:
288 ;
289 }
290 print_comment(inst);
291 break;
292 case OPCODE_ARL:
293 _mesa_printf("ARL addr.x, ");
294 print_src_reg(&inst->SrcReg[0]);
295 print_comment(inst);
296 break;
297 case OPCODE_BRA:
Brian5ae49cf2007-01-20 09:27:40 -0700298 _mesa_printf("BRA %u (%s%s)",
Brian3a281532006-12-16 12:51:34 -0700299 inst->BranchTarget,
300 condcode_string(inst->DstReg.CondMask),
301 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
Brian00cdc0a2006-12-14 15:01:06 -0700302 print_comment(inst);
303 break;
304 case OPCODE_CAL:
305 _mesa_printf("CAL %u", inst->BranchTarget);
306 print_comment(inst);
307 break;
Brian5ae49cf2007-01-20 09:27:40 -0700308 case OPCODE_IF:
309 _mesa_printf(" IF (%s%s)",
310 condcode_string(inst->DstReg.CondMask),
311 swizzle_string(inst->DstReg.CondSwizzle, 0, GL_FALSE));
312 print_comment(inst);
313 break;
314 case OPCODE_ELSE:
315 _mesa_printf(" ELSE;\n");
316 break;
317 case OPCODE_ENDIF:
318 _mesa_printf(" ENDIF;\n");
319 break;
Brian00cdc0a2006-12-14 15:01:06 -0700320 case OPCODE_END:
321 _mesa_printf("END");
322 print_comment(inst);
323 break;
Brian3a281532006-12-16 12:51:34 -0700324 case OPCODE_NOP:
325 _mesa_printf("NOP");
326 print_comment(inst);
327 break;
Brian00cdc0a2006-12-14 15:01:06 -0700328 /* XXX may need other special-case instructions */
329 default:
330 /* typical alu instruction */
331 _mesa_print_alu_instruction(inst,
332 _mesa_opcode_string(inst->Opcode),
333 _mesa_num_inst_src_regs(inst->Opcode));
334 break;
335 }
336}
337
338
339/**
340 * Print a vertx/fragment program to stdout.
341 * XXX this function could be greatly improved.
342 */
343void
344_mesa_print_program(const struct gl_program *prog)
345{
346 GLuint i;
347 for (i = 0; i < prog->NumInstructions; i++) {
348 _mesa_printf("%3d: ", i);
349 _mesa_print_instruction(prog->Instructions + i);
350 }
351}
352
353
354/**
355 * Print all of a program's parameters.
356 */
357void
358_mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
359{
Brian00cdc0a2006-12-14 15:01:06 -0700360 _mesa_printf("InputsRead: 0x%x\n", prog->InputsRead);
361 _mesa_printf("OutputsWritten: 0x%x\n", prog->OutputsWritten);
362 _mesa_printf("NumInstructions=%d\n", prog->NumInstructions);
363 _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries);
364 _mesa_printf("NumParameters=%d\n", prog->NumParameters);
365 _mesa_printf("NumAttributes=%d\n", prog->NumAttributes);
366 _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs);
367
368 _mesa_load_state_parameters(ctx, prog->Parameters);
369
370#if 0
371 _mesa_printf("Local Params:\n");
372 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
373 const GLfloat *p = prog->LocalParams[i];
374 _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
375 }
376#endif
Brian83ca3ff2006-12-20 17:17:38 -0700377 _mesa_print_parameter_list(prog->Parameters);
378}
Brian00cdc0a2006-12-14 15:01:06 -0700379
Brian83ca3ff2006-12-20 17:17:38 -0700380
381void
382_mesa_print_parameter_list(const struct gl_program_parameter_list *list)
383{
384 GLuint i;
385 _mesa_printf("param list %p\n", (void *) list);
386 for (i = 0; i < list->NumParameters; i++){
387 struct gl_program_parameter *param = list->Parameters + i;
388 const GLfloat *v = list->ParameterValues[i];
389 _mesa_printf("param[%d] sz=%d %s %s = {%.3f, %.3f, %.3f, %.3f};\n",
390 i, param->Size,
391 program_file_string(list->Parameters[i].Type),
Brian00cdc0a2006-12-14 15:01:06 -0700392 param->Name, v[0], v[1], v[2], v[3]);
393 }
394}