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