Move some code from prog_print.c to prog_instruction.c
[mesa.git] / src / mesa / shader / prog_print.c
1 /*
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"
34 #include "macros.h"
35 #include "program.h"
36 #include "prog_instruction.h"
37 #include "prog_parameter.h"
38 #include "prog_print.h"
39 #include "prog_statevars.h"
40
41
42 /**
43 * Return string name for given program/register file.
44 */
45 static const char *
46 program_file_string(enum register_file f)
47 {
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";
73 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.
82 */
83 static const char *
84 swizzle_string(GLuint swizzle, GLuint negateBase, GLboolean extended)
85 {
86 static const char swz[] = "xyzw01";
87 static char s[20];
88 GLuint i = 0;
89
90 if (!extended && swizzle == SWIZZLE_NOOP && negateBase == 0)
91 return ""; /* no swizzle/negation */
92
93 if (!extended)
94 s[i++] = '.';
95
96 if (negateBase & 0x1)
97 s[i++] = '-';
98 s[i++] = swz[GET_SWZ(swizzle, 0)];
99
100 if (extended) {
101 s[i++] = ',';
102 }
103
104 if (negateBase & 0x2)
105 s[i++] = '-';
106 s[i++] = swz[GET_SWZ(swizzle, 1)];
107
108 if (extended) {
109 s[i++] = ',';
110 }
111
112 if (negateBase & 0x4)
113 s[i++] = '-';
114 s[i++] = swz[GET_SWZ(swizzle, 2)];
115
116 if (extended) {
117 s[i++] = ',';
118 }
119
120 if (negateBase & 0x8)
121 s[i++] = '-';
122 s[i++] = swz[GET_SWZ(swizzle, 3)];
123
124 s[i] = 0;
125 return s;
126 }
127
128
129 static const char *
130 writemask_string(GLuint writeMask)
131 {
132 static char s[10];
133 GLuint i = 0;
134
135 if (writeMask == WRITEMASK_XYZW)
136 return "";
137
138 s[i++] = '.';
139 if (writeMask & WRITEMASK_X)
140 s[i++] = 'x';
141 if (writeMask & WRITEMASK_Y)
142 s[i++] = 'y';
143 if (writeMask & WRITEMASK_Z)
144 s[i++] = 'z';
145 if (writeMask & WRITEMASK_W)
146 s[i++] = 'w';
147
148 s[i] = 0;
149 return s;
150 }
151
152 static void
153 print_dst_reg(const struct prog_dst_register *dstReg)
154 {
155 _mesa_printf(" %s[%d]%s",
156 program_file_string((enum register_file) dstReg->File),
157 dstReg->Index,
158 writemask_string(dstReg->WriteMask));
159 }
160
161 static void
162 print_src_reg(const struct prog_src_register *srcReg)
163 {
164 _mesa_printf("%s[%d]%s",
165 program_file_string((enum register_file) srcReg->File),
166 srcReg->Index,
167 swizzle_string(srcReg->Swizzle,
168 srcReg->NegateBase, GL_FALSE));
169 }
170
171 static void
172 print_comment(const struct prog_instruction *inst)
173 {
174 if (inst->Comment)
175 _mesa_printf("; # %s\n", inst->Comment);
176 else
177 _mesa_printf(";\n");
178 }
179
180
181 void
182 _mesa_print_alu_instruction(const struct prog_instruction *inst,
183 const char *opcode_string,
184 GLuint numRegs)
185 {
186 GLuint j;
187
188 _mesa_printf("%s", opcode_string);
189
190 /* frag prog only */
191 if (inst->SaturateMode == SATURATE_ZERO_ONE)
192 _mesa_printf("_SAT");
193
194 if (inst->DstReg.File != PROGRAM_UNDEFINED) {
195 _mesa_printf(" %s[%d]%s",
196 program_file_string((enum register_file) inst->DstReg.File),
197 inst->DstReg.Index,
198 writemask_string(inst->DstReg.WriteMask));
199 }
200 else {
201 _mesa_printf(" ???");
202 }
203
204 if (numRegs > 0)
205 _mesa_printf(", ");
206
207 for (j = 0; j < numRegs; j++) {
208 print_src_reg(inst->SrcReg + j);
209 if (j + 1 < numRegs)
210 _mesa_printf(", ");
211 }
212
213 if (inst->Comment)
214 _mesa_printf(" # %s", inst->Comment);
215
216 print_comment(inst);
217 }
218
219
220 /**
221 * Print a single vertex/fragment program instruction.
222 */
223 void
224 _mesa_print_instruction(const struct prog_instruction *inst)
225 {
226 switch (inst->Opcode) {
227 case OPCODE_PRINT:
228 _mesa_printf("PRINT '%s'", inst->Data);
229 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
230 _mesa_printf(", ");
231 _mesa_printf("%s[%d]%s",
232 program_file_string((enum register_file) inst->SrcReg[0].File),
233 inst->SrcReg[0].Index,
234 swizzle_string(inst->SrcReg[0].Swizzle,
235 inst->SrcReg[0].NegateBase, GL_FALSE));
236 }
237 if (inst->Comment)
238 _mesa_printf(" # %s", inst->Comment);
239 print_comment(inst);
240 break;
241 case OPCODE_SWZ:
242 _mesa_printf("SWZ");
243 if (inst->SaturateMode == SATURATE_ZERO_ONE)
244 _mesa_printf("_SAT");
245 print_dst_reg(&inst->DstReg);
246 _mesa_printf("%s[%d], %s",
247 program_file_string((enum register_file) inst->SrcReg[0].File),
248 inst->SrcReg[0].Index,
249 swizzle_string(inst->SrcReg[0].Swizzle,
250 inst->SrcReg[0].NegateBase, GL_TRUE));
251 print_comment(inst);
252 break;
253 case OPCODE_TEX:
254 case OPCODE_TXP:
255 case OPCODE_TXB:
256 _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
257 if (inst->SaturateMode == SATURATE_ZERO_ONE)
258 _mesa_printf("_SAT");
259 _mesa_printf(" ");
260 print_dst_reg(&inst->DstReg);
261 _mesa_printf(", ");
262 print_src_reg(&inst->SrcReg[0]);
263 _mesa_printf(", texture[%d], ", inst->TexSrcUnit);
264 switch (inst->TexSrcTarget) {
265 case TEXTURE_1D_INDEX: _mesa_printf("1D"); break;
266 case TEXTURE_2D_INDEX: _mesa_printf("2D"); break;
267 case TEXTURE_3D_INDEX: _mesa_printf("3D"); break;
268 case TEXTURE_CUBE_INDEX: _mesa_printf("CUBE"); break;
269 case TEXTURE_RECT_INDEX: _mesa_printf("RECT"); break;
270 default:
271 ;
272 }
273 print_comment(inst);
274 break;
275 case OPCODE_ARL:
276 _mesa_printf("ARL addr.x, ");
277 print_src_reg(&inst->SrcReg[0]);
278 print_comment(inst);
279 break;
280 case OPCODE_BRA:
281 _mesa_printf("BRA %u", inst->BranchTarget);
282 print_comment(inst);
283 break;
284 case OPCODE_CAL:
285 _mesa_printf("CAL %u", inst->BranchTarget);
286 print_comment(inst);
287 break;
288 case OPCODE_END:
289 _mesa_printf("END");
290 print_comment(inst);
291 break;
292 /* XXX may need other special-case instructions */
293 default:
294 /* typical alu instruction */
295 _mesa_print_alu_instruction(inst,
296 _mesa_opcode_string(inst->Opcode),
297 _mesa_num_inst_src_regs(inst->Opcode));
298 break;
299 }
300 }
301
302
303 /**
304 * Print a vertx/fragment program to stdout.
305 * XXX this function could be greatly improved.
306 */
307 void
308 _mesa_print_program(const struct gl_program *prog)
309 {
310 GLuint i;
311 for (i = 0; i < prog->NumInstructions; i++) {
312 _mesa_printf("%3d: ", i);
313 _mesa_print_instruction(prog->Instructions + i);
314 }
315 }
316
317
318 /**
319 * Print all of a program's parameters.
320 */
321 void
322 _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
323 {
324 GLint i;
325
326 _mesa_printf("InputsRead: 0x%x\n", prog->InputsRead);
327 _mesa_printf("OutputsWritten: 0x%x\n", prog->OutputsWritten);
328 _mesa_printf("NumInstructions=%d\n", prog->NumInstructions);
329 _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries);
330 _mesa_printf("NumParameters=%d\n", prog->NumParameters);
331 _mesa_printf("NumAttributes=%d\n", prog->NumAttributes);
332 _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs);
333
334 _mesa_load_state_parameters(ctx, prog->Parameters);
335
336 #if 0
337 _mesa_printf("Local Params:\n");
338 for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){
339 const GLfloat *p = prog->LocalParams[i];
340 _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]);
341 }
342 #endif
343
344 for (i = 0; i < prog->Parameters->NumParameters; i++){
345 struct gl_program_parameter *param = prog->Parameters->Parameters + i;
346 const GLfloat *v = prog->Parameters->ParameterValues[i];
347 _mesa_printf("param[%d] %s %s = {%.3f, %.3f, %.3f, %.3f};\n",
348 i,
349 program_file_string(prog->Parameters->Parameters[i].Type),
350 param->Name, v[0], v[1], v[2], v[3]);
351 }
352 }