Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / drivers / dri / r300 / r500_fragprog.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #include "r500_fragprog.h"
29
30 #include "radeon_nqssadce.h"
31 #include "radeon_program_alu.h"
32
33
34 static void reset_srcreg(struct prog_src_register* reg)
35 {
36 _mesa_bzero(reg, sizeof(*reg));
37 reg->Swizzle = SWIZZLE_NOOP;
38 }
39
40 static struct prog_src_register shadow_ambient(struct gl_program *program, int tmu)
41 {
42 gl_state_index fail_value_tokens[STATE_LENGTH] = {
43 STATE_INTERNAL, STATE_SHADOW_AMBIENT, 0, 0, 0
44 };
45 struct prog_src_register reg = { 0, };
46
47 fail_value_tokens[2] = tmu;
48 reg.File = PROGRAM_STATE_VAR;
49 reg.Index = _mesa_add_state_reference(program->Parameters, fail_value_tokens);
50 reg.Swizzle = SWIZZLE_WWWW;
51 return reg;
52 }
53
54 /**
55 * Transform TEX, TXP, TXB, and KIL instructions in the following way:
56 * - premultiply texture coordinates for RECT
57 * - extract operand swizzles
58 * - introduce a temporary register when write masks are needed
59 *
60 */
61 static GLboolean transform_TEX(
62 struct radeon_transform_context *t,
63 struct prog_instruction* orig_inst, void* data)
64 {
65 struct r500_fragment_program_compiler *compiler =
66 (struct r500_fragment_program_compiler*)data;
67 struct prog_instruction inst = *orig_inst;
68 struct prog_instruction* tgt;
69 GLboolean destredirect = GL_FALSE;
70
71 if (inst.Opcode != OPCODE_TEX &&
72 inst.Opcode != OPCODE_TXB &&
73 inst.Opcode != OPCODE_TXP &&
74 inst.Opcode != OPCODE_KIL)
75 return GL_FALSE;
76
77 /* ARB_shadow & EXT_shadow_funcs */
78 if (inst.Opcode != OPCODE_KIL &&
79 t->Program->ShadowSamplers & (1 << inst.TexSrcUnit)) {
80 GLuint comparefunc = GL_NEVER + compiler->fp->state.unit[inst.TexSrcUnit].texture_compare_func;
81
82 if (comparefunc == GL_NEVER || comparefunc == GL_ALWAYS) {
83 tgt = radeonAppendInstructions(t->Program, 1);
84
85 tgt->Opcode = OPCODE_MOV;
86 tgt->DstReg = inst.DstReg;
87 if (comparefunc == GL_ALWAYS) {
88 tgt->SrcReg[0].File = PROGRAM_BUILTIN;
89 tgt->SrcReg[0].Swizzle = SWIZZLE_1111;
90 } else {
91 tgt->SrcReg[0] = shadow_ambient(t->Program, inst.TexSrcUnit);
92 }
93 return GL_TRUE;
94 }
95
96 inst.DstReg.File = PROGRAM_TEMPORARY;
97 inst.DstReg.Index = radeonFindFreeTemporary(t);
98 inst.DstReg.WriteMask = WRITEMASK_XYZW;
99 } else if (inst.Opcode != OPCODE_KIL && inst.DstReg.File != PROGRAM_TEMPORARY) {
100 int tempreg = radeonFindFreeTemporary(t);
101
102 inst.DstReg.File = PROGRAM_TEMPORARY;
103 inst.DstReg.Index = tempreg;
104 inst.DstReg.WriteMask = WRITEMASK_XYZW;
105 destredirect = GL_TRUE;
106 }
107
108 if (inst.SrcReg[0].File != PROGRAM_TEMPORARY && inst.SrcReg[0].File != PROGRAM_INPUT) {
109 int tmpreg = radeonFindFreeTemporary(t);
110 tgt = radeonAppendInstructions(t->Program, 1);
111 tgt->Opcode = OPCODE_MOV;
112 tgt->DstReg.File = PROGRAM_TEMPORARY;
113 tgt->DstReg.Index = tmpreg;
114 tgt->SrcReg[0] = inst.SrcReg[0];
115
116 reset_srcreg(&inst.SrcReg[0]);
117 inst.SrcReg[0].File = PROGRAM_TEMPORARY;
118 inst.SrcReg[0].Index = tmpreg;
119 }
120
121 tgt = radeonAppendInstructions(t->Program, 1);
122 _mesa_copy_instructions(tgt, &inst, 1);
123
124 if (inst.Opcode != OPCODE_KIL &&
125 t->Program->ShadowSamplers & (1 << inst.TexSrcUnit)) {
126 GLuint comparefunc = GL_NEVER + compiler->fp->state.unit[inst.TexSrcUnit].texture_compare_func;
127 GLuint depthmode = compiler->fp->state.unit[inst.TexSrcUnit].depth_texture_mode;
128 int rcptemp = radeonFindFreeTemporary(t);
129 int pass, fail;
130
131 tgt = radeonAppendInstructions(t->Program, 3);
132
133 tgt[0].Opcode = OPCODE_RCP;
134 tgt[0].DstReg.File = PROGRAM_TEMPORARY;
135 tgt[0].DstReg.Index = rcptemp;
136 tgt[0].DstReg.WriteMask = WRITEMASK_W;
137 tgt[0].SrcReg[0] = inst.SrcReg[0];
138 tgt[0].SrcReg[0].Swizzle = SWIZZLE_WWWW;
139
140 tgt[1].Opcode = OPCODE_MAD;
141 tgt[1].DstReg = inst.DstReg;
142 tgt[1].DstReg.WriteMask = orig_inst->DstReg.WriteMask;
143 tgt[1].SrcReg[0] = inst.SrcReg[0];
144 tgt[1].SrcReg[0].Swizzle = SWIZZLE_ZZZZ;
145 tgt[1].SrcReg[1].File = PROGRAM_TEMPORARY;
146 tgt[1].SrcReg[1].Index = rcptemp;
147 tgt[1].SrcReg[1].Swizzle = SWIZZLE_WWWW;
148 tgt[1].SrcReg[2].File = PROGRAM_TEMPORARY;
149 tgt[1].SrcReg[2].Index = inst.DstReg.Index;
150 if (depthmode == 0) /* GL_LUMINANCE */
151 tgt[1].SrcReg[2].Swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z);
152 else if (depthmode == 2) /* GL_ALPHA */
153 tgt[1].SrcReg[2].Swizzle = SWIZZLE_WWWW;
154
155 /* Recall that SrcReg[0] is tex, SrcReg[2] is r and:
156 * r < tex <=> -tex+r < 0
157 * r >= tex <=> not (-tex+r < 0 */
158 if (comparefunc == GL_LESS || comparefunc == GL_GEQUAL)
159 tgt[1].SrcReg[2].Negate = tgt[0].SrcReg[2].Negate ^ NEGATE_XYZW;
160 else
161 tgt[1].SrcReg[0].Negate = tgt[0].SrcReg[0].Negate ^ NEGATE_XYZW;
162
163 tgt[2].Opcode = OPCODE_CMP;
164 tgt[2].DstReg = orig_inst->DstReg;
165 tgt[2].SrcReg[0].File = PROGRAM_TEMPORARY;
166 tgt[2].SrcReg[0].Index = tgt[1].DstReg.Index;
167
168 if (comparefunc == GL_LESS || comparefunc == GL_GREATER) {
169 pass = 1;
170 fail = 2;
171 } else {
172 pass = 2;
173 fail = 1;
174 }
175
176 tgt[2].SrcReg[pass].File = PROGRAM_BUILTIN;
177 tgt[2].SrcReg[pass].Swizzle = SWIZZLE_1111;
178 tgt[2].SrcReg[fail] = shadow_ambient(t->Program, inst.TexSrcUnit);
179 } else if (destredirect) {
180 tgt = radeonAppendInstructions(t->Program, 1);
181
182 tgt->Opcode = OPCODE_MOV;
183 tgt->DstReg = orig_inst->DstReg;
184 tgt->SrcReg[0].File = PROGRAM_TEMPORARY;
185 tgt->SrcReg[0].Index = inst.DstReg.Index;
186 }
187
188 return GL_TRUE;
189 }
190
191
192 static void update_params(r300ContextPtr r300, struct r500_fragment_program *fp)
193 {
194 struct gl_fragment_program *mp = &fp->mesa_program;
195
196 /* Ask Mesa nicely to fill in ParameterValues for us */
197 if (mp->Base.Parameters)
198 _mesa_load_state_parameters(r300->radeon.glCtx, mp->Base.Parameters);
199 }
200
201
202 /**
203 * Transform the program to support fragment.position.
204 *
205 * Introduce a small fragment at the start of the program that will be
206 * the only code that directly reads the FRAG_ATTRIB_WPOS input.
207 * All other code pieces that reference that input will be rewritten
208 * to read from a newly allocated temporary.
209 *
210 * \todo if/when r5xx supports the radeon_program architecture, this is a
211 * likely candidate for code sharing.
212 */
213 static void insert_WPOS_trailer(struct r500_fragment_program_compiler *compiler)
214 {
215 GLuint InputsRead = compiler->fp->mesa_program.Base.InputsRead;
216
217 if (!(InputsRead & FRAG_BIT_WPOS))
218 return;
219
220 static gl_state_index tokens[STATE_LENGTH] = {
221 STATE_INTERNAL, STATE_R300_WINDOW_DIMENSION, 0, 0, 0
222 };
223 struct prog_instruction *fpi;
224 GLuint window_index;
225 int i = 0;
226 GLuint tempregi = _mesa_find_free_register(compiler->program, PROGRAM_TEMPORARY);
227
228 _mesa_insert_instructions(compiler->program, 0, 3);
229 fpi = compiler->program->Instructions;
230
231 /* perspective divide */
232 fpi[i].Opcode = OPCODE_RCP;
233
234 fpi[i].DstReg.File = PROGRAM_TEMPORARY;
235 fpi[i].DstReg.Index = tempregi;
236 fpi[i].DstReg.WriteMask = WRITEMASK_W;
237 fpi[i].DstReg.CondMask = COND_TR;
238
239 fpi[i].SrcReg[0].File = PROGRAM_INPUT;
240 fpi[i].SrcReg[0].Index = FRAG_ATTRIB_WPOS;
241 fpi[i].SrcReg[0].Swizzle = SWIZZLE_WWWW;
242 i++;
243
244 fpi[i].Opcode = OPCODE_MUL;
245
246 fpi[i].DstReg.File = PROGRAM_TEMPORARY;
247 fpi[i].DstReg.Index = tempregi;
248 fpi[i].DstReg.WriteMask = WRITEMASK_XYZ;
249 fpi[i].DstReg.CondMask = COND_TR;
250
251 fpi[i].SrcReg[0].File = PROGRAM_INPUT;
252 fpi[i].SrcReg[0].Index = FRAG_ATTRIB_WPOS;
253 fpi[i].SrcReg[0].Swizzle = SWIZZLE_XYZW;
254
255 fpi[i].SrcReg[1].File = PROGRAM_TEMPORARY;
256 fpi[i].SrcReg[1].Index = tempregi;
257 fpi[i].SrcReg[1].Swizzle = SWIZZLE_WWWW;
258 i++;
259
260 /* viewport transformation */
261 window_index = _mesa_add_state_reference(compiler->program->Parameters, tokens);
262
263 fpi[i].Opcode = OPCODE_MAD;
264
265 fpi[i].DstReg.File = PROGRAM_TEMPORARY;
266 fpi[i].DstReg.Index = tempregi;
267 fpi[i].DstReg.WriteMask = WRITEMASK_XYZ;
268 fpi[i].DstReg.CondMask = COND_TR;
269
270 fpi[i].SrcReg[0].File = PROGRAM_TEMPORARY;
271 fpi[i].SrcReg[0].Index = tempregi;
272 fpi[i].SrcReg[0].Swizzle =
273 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ZERO);
274
275 fpi[i].SrcReg[1].File = PROGRAM_STATE_VAR;
276 fpi[i].SrcReg[1].Index = window_index;
277 fpi[i].SrcReg[1].Swizzle =
278 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ZERO);
279
280 fpi[i].SrcReg[2].File = PROGRAM_STATE_VAR;
281 fpi[i].SrcReg[2].Index = window_index;
282 fpi[i].SrcReg[2].Swizzle =
283 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ZERO);
284 i++;
285
286 for (; i < compiler->program->NumInstructions; ++i) {
287 int reg;
288 for (reg = 0; reg < 3; reg++) {
289 if (fpi[i].SrcReg[reg].File == PROGRAM_INPUT &&
290 fpi[i].SrcReg[reg].Index == FRAG_ATTRIB_WPOS) {
291 fpi[i].SrcReg[reg].File = PROGRAM_TEMPORARY;
292 fpi[i].SrcReg[reg].Index = tempregi;
293 }
294 }
295 }
296 }
297
298
299 static void nqssadce_init(struct nqssadce_state* s)
300 {
301 s->Outputs[FRAG_RESULT_COLOR].Sourced = WRITEMASK_XYZW;
302 s->Outputs[FRAG_RESULT_DEPTH].Sourced = WRITEMASK_W;
303 }
304
305 static GLboolean is_native_swizzle(GLuint opcode, struct prog_src_register reg)
306 {
307 GLuint relevant;
308 int i;
309
310 if (opcode == OPCODE_TEX ||
311 opcode == OPCODE_TXB ||
312 opcode == OPCODE_TXP ||
313 opcode == OPCODE_KIL) {
314 if (reg.Abs)
315 return GL_FALSE;
316
317 if (reg.Negate)
318 reg.Negate ^= NEGATE_XYZW;
319
320 if (opcode == OPCODE_KIL) {
321 if (reg.Swizzle != SWIZZLE_NOOP)
322 return GL_FALSE;
323 } else {
324 for(i = 0; i < 4; ++i) {
325 GLuint swz = GET_SWZ(reg.Swizzle, i);
326 if (swz == SWIZZLE_NIL) {
327 reg.Negate &= ~(1 << i);
328 continue;
329 }
330 if (swz >= 4)
331 return GL_FALSE;
332 }
333 }
334
335 if (reg.Negate)
336 return GL_FALSE;
337
338 return GL_TRUE;
339 } else if (opcode == OPCODE_DDX || opcode == OPCODE_DDY) {
340 /* DDX/MDH and DDY/MDV explicitly ignore incoming swizzles;
341 * if it doesn't fit perfectly into a .xyzw case... */
342 if (reg.Swizzle == SWIZZLE_NOOP && !reg.Abs && !reg.Negate)
343 return GL_TRUE;
344
345 return GL_FALSE;
346 } else {
347 /* ALU instructions support almost everything */
348 if (reg.Abs)
349 return GL_TRUE;
350
351 relevant = 0;
352 for(i = 0; i < 3; ++i) {
353 GLuint swz = GET_SWZ(reg.Swizzle, i);
354 if (swz != SWIZZLE_NIL && swz != SWIZZLE_ZERO)
355 relevant |= 1 << i;
356 }
357 if ((reg.Negate & relevant) && ((reg.Negate & relevant) != relevant))
358 return GL_FALSE;
359
360 return GL_TRUE;
361 }
362 }
363
364 /**
365 * Implement a MOV with a potentially non-native swizzle.
366 *
367 * The only thing we *cannot* do in an ALU instruction is per-component
368 * negation. Therefore, we split the MOV into two instructions when necessary.
369 */
370 static void nqssadce_build_swizzle(struct nqssadce_state *s,
371 struct prog_dst_register dst, struct prog_src_register src)
372 {
373 struct prog_instruction *inst;
374 GLuint negatebase[2] = { 0, 0 };
375 int i;
376
377 for(i = 0; i < 4; ++i) {
378 GLuint swz = GET_SWZ(src.Swizzle, i);
379 if (swz == SWIZZLE_NIL)
380 continue;
381 negatebase[GET_BIT(src.Negate, i)] |= 1 << i;
382 }
383
384 _mesa_insert_instructions(s->Program, s->IP, (negatebase[0] ? 1 : 0) + (negatebase[1] ? 1 : 0));
385 inst = s->Program->Instructions + s->IP;
386
387 for(i = 0; i <= 1; ++i) {
388 if (!negatebase[i])
389 continue;
390
391 inst->Opcode = OPCODE_MOV;
392 inst->DstReg = dst;
393 inst->DstReg.WriteMask = negatebase[i];
394 inst->SrcReg[0] = src;
395 inst++;
396 s->IP++;
397 }
398 }
399
400 static GLuint build_dtm(GLuint depthmode)
401 {
402 switch(depthmode) {
403 default:
404 case GL_LUMINANCE: return 0;
405 case GL_INTENSITY: return 1;
406 case GL_ALPHA: return 2;
407 }
408 }
409
410 static GLuint build_func(GLuint comparefunc)
411 {
412 return comparefunc - GL_NEVER;
413 }
414
415
416 /**
417 * Collect all external state that is relevant for compiling the given
418 * fragment program.
419 */
420 static void build_state(
421 r300ContextPtr r300,
422 struct r500_fragment_program *fp,
423 struct r500_fragment_program_external_state *state)
424 {
425 int unit;
426
427 _mesa_bzero(state, sizeof(*state));
428
429 for(unit = 0; unit < 16; ++unit) {
430 if (fp->mesa_program.Base.ShadowSamplers & (1 << unit)) {
431 struct gl_texture_object* tex = r300->radeon.glCtx->Texture.Unit[unit]._Current;
432
433 state->unit[unit].depth_texture_mode = build_dtm(tex->DepthMode);
434 state->unit[unit].texture_compare_func = build_func(tex->CompareFunc);
435 }
436 }
437 }
438
439 static void dump_program(struct r500_fragment_program_code *code);
440
441 void r500TranslateFragmentShader(r300ContextPtr r300,
442 struct r500_fragment_program *fp)
443 {
444 struct r500_fragment_program_external_state state;
445
446 build_state(r300, fp, &state);
447 if (_mesa_memcmp(&fp->state, &state, sizeof(state))) {
448 /* TODO: cache compiled programs */
449 fp->translated = GL_FALSE;
450 _mesa_memcpy(&fp->state, &state, sizeof(state));
451 }
452
453 if (!fp->translated) {
454 struct r500_fragment_program_compiler compiler;
455
456 compiler.r300 = r300;
457 compiler.fp = fp;
458 compiler.code = &fp->code;
459 compiler.program = _mesa_clone_program(r300->radeon.glCtx, &fp->mesa_program.Base);
460
461 if (RADEON_DEBUG & DEBUG_PIXEL) {
462 _mesa_printf("Compiler: Initial program:\n");
463 _mesa_print_program(compiler.program);
464 }
465
466 insert_WPOS_trailer(&compiler);
467
468 struct radeon_program_transformation transformations[] = {
469 { &transform_TEX, &compiler },
470 { &radeonTransformALU, 0 },
471 { &radeonTransformDeriv, 0 },
472 { &radeonTransformTrigScale, 0 }
473 };
474 radeonLocalTransform(r300->radeon.glCtx, compiler.program,
475 4, transformations);
476
477 if (RADEON_DEBUG & DEBUG_PIXEL) {
478 _mesa_printf("Compiler: after native rewrite:\n");
479 _mesa_print_program(compiler.program);
480 }
481
482 struct radeon_nqssadce_descr nqssadce = {
483 .Init = &nqssadce_init,
484 .IsNativeSwizzle = &is_native_swizzle,
485 .BuildSwizzle = &nqssadce_build_swizzle,
486 .RewriteDepthOut = GL_TRUE
487 };
488 radeonNqssaDce(r300->radeon.glCtx, compiler.program, &nqssadce);
489
490 if (RADEON_DEBUG & DEBUG_PIXEL) {
491 _mesa_printf("Compiler: after NqSSA-DCE:\n");
492 _mesa_print_program(compiler.program);
493 }
494
495 fp->translated = r500FragmentProgramEmit(&compiler);
496
497 /* Subtle: Rescue any parameters that have been added during transformations */
498 _mesa_free_parameter_list(fp->mesa_program.Base.Parameters);
499 fp->mesa_program.Base.Parameters = compiler.program->Parameters;
500 compiler.program->Parameters = 0;
501
502 _mesa_reference_program(r300->radeon.glCtx, &compiler.program, 0);
503
504 r300UpdateStateParameters(r300->radeon.glCtx, _NEW_PROGRAM |
505 _NEW_PROGRAM_CONSTANTS);
506
507 if (RADEON_DEBUG & DEBUG_PIXEL) {
508 if (fp->translated) {
509 _mesa_printf("Machine-readable code:\n");
510 dump_program(&fp->code);
511 }
512 }
513
514 }
515
516 update_params(r300, fp);
517
518 }
519
520 static char *toswiz(int swiz_val) {
521 switch(swiz_val) {
522 case 0: return "R";
523 case 1: return "G";
524 case 2: return "B";
525 case 3: return "A";
526 case 4: return "0";
527 case 5: return "1/2";
528 case 6: return "1";
529 case 7: return "U";
530 }
531 return NULL;
532 }
533
534 static char *toop(int op_val)
535 {
536 char *str = NULL;
537 switch (op_val) {
538 case 0: str = "MAD"; break;
539 case 1: str = "DP3"; break;
540 case 2: str = "DP4"; break;
541 case 3: str = "D2A"; break;
542 case 4: str = "MIN"; break;
543 case 5: str = "MAX"; break;
544 case 6: str = "Reserved"; break;
545 case 7: str = "CND"; break;
546 case 8: str = "CMP"; break;
547 case 9: str = "FRC"; break;
548 case 10: str = "SOP"; break;
549 case 11: str = "MDH"; break;
550 case 12: str = "MDV"; break;
551 }
552 return str;
553 }
554
555 static char *to_alpha_op(int op_val)
556 {
557 char *str = NULL;
558 switch (op_val) {
559 case 0: str = "MAD"; break;
560 case 1: str = "DP"; break;
561 case 2: str = "MIN"; break;
562 case 3: str = "MAX"; break;
563 case 4: str = "Reserved"; break;
564 case 5: str = "CND"; break;
565 case 6: str = "CMP"; break;
566 case 7: str = "FRC"; break;
567 case 8: str = "EX2"; break;
568 case 9: str = "LN2"; break;
569 case 10: str = "RCP"; break;
570 case 11: str = "RSQ"; break;
571 case 12: str = "SIN"; break;
572 case 13: str = "COS"; break;
573 case 14: str = "MDH"; break;
574 case 15: str = "MDV"; break;
575 }
576 return str;
577 }
578
579 static char *to_mask(int val)
580 {
581 char *str = NULL;
582 switch(val) {
583 case 0: str = "NONE"; break;
584 case 1: str = "R"; break;
585 case 2: str = "G"; break;
586 case 3: str = "RG"; break;
587 case 4: str = "B"; break;
588 case 5: str = "RB"; break;
589 case 6: str = "GB"; break;
590 case 7: str = "RGB"; break;
591 case 8: str = "A"; break;
592 case 9: str = "AR"; break;
593 case 10: str = "AG"; break;
594 case 11: str = "ARG"; break;
595 case 12: str = "AB"; break;
596 case 13: str = "ARB"; break;
597 case 14: str = "AGB"; break;
598 case 15: str = "ARGB"; break;
599 }
600 return str;
601 }
602
603 static char *to_texop(int val)
604 {
605 switch(val) {
606 case 0: return "NOP";
607 case 1: return "LD";
608 case 2: return "TEXKILL";
609 case 3: return "PROJ";
610 case 4: return "LODBIAS";
611 case 5: return "LOD";
612 case 6: return "DXDY";
613 }
614 return NULL;
615 }
616
617 static void dump_program(struct r500_fragment_program_code *code)
618 {
619
620 fprintf(stderr, "R500 Fragment Program:\n--------\n");
621
622 int n;
623 uint32_t inst;
624 uint32_t inst0;
625 char *str = NULL;
626
627 if (code->const_nr) {
628 fprintf(stderr, "--------\nConstants:\n");
629 for (n = 0; n < code->const_nr; n++) {
630 fprintf(stderr, "Constant %d: %i[%i]\n", n,
631 code->constant[n].File, code->constant[n].Index);
632 }
633 fprintf(stderr, "--------\n");
634 }
635
636 for (n = 0; n < code->inst_end+1; n++) {
637 inst0 = inst = code->inst[n].inst0;
638 fprintf(stderr,"%d\t0:CMN_INST 0x%08x:", n, inst);
639 switch(inst & 0x3) {
640 case R500_INST_TYPE_ALU: str = "ALU"; break;
641 case R500_INST_TYPE_OUT: str = "OUT"; break;
642 case R500_INST_TYPE_FC: str = "FC"; break;
643 case R500_INST_TYPE_TEX: str = "TEX"; break;
644 };
645 fprintf(stderr,"%s %s %s %s %s ", str,
646 inst & R500_INST_TEX_SEM_WAIT ? "TEX_WAIT" : "",
647 inst & R500_INST_LAST ? "LAST" : "",
648 inst & R500_INST_NOP ? "NOP" : "",
649 inst & R500_INST_ALU_WAIT ? "ALU WAIT" : "");
650 fprintf(stderr,"wmask: %s omask: %s\n", to_mask((inst >> 11) & 0xf),
651 to_mask((inst >> 15) & 0xf));
652
653 switch(inst0 & 0x3) {
654 case 0:
655 case 1:
656 fprintf(stderr,"\t1:RGB_ADDR 0x%08x:", code->inst[n].inst1);
657 inst = code->inst[n].inst1;
658
659 fprintf(stderr,"Addr0: %d%c, Addr1: %d%c, Addr2: %d%c, srcp:%d\n",
660 inst & 0xff, (inst & (1<<8)) ? 'c' : 't',
661 (inst >> 10) & 0xff, (inst & (1<<18)) ? 'c' : 't',
662 (inst >> 20) & 0xff, (inst & (1<<28)) ? 'c' : 't',
663 (inst >> 30));
664
665 fprintf(stderr,"\t2:ALPHA_ADDR 0x%08x:", code->inst[n].inst2);
666 inst = code->inst[n].inst2;
667 fprintf(stderr,"Addr0: %d%c, Addr1: %d%c, Addr2: %d%c, srcp:%d\n",
668 inst & 0xff, (inst & (1<<8)) ? 'c' : 't',
669 (inst >> 10) & 0xff, (inst & (1<<18)) ? 'c' : 't',
670 (inst >> 20) & 0xff, (inst & (1<<28)) ? 'c' : 't',
671 (inst >> 30));
672 fprintf(stderr,"\t3 RGB_INST: 0x%08x:", code->inst[n].inst3);
673 inst = code->inst[n].inst3;
674 fprintf(stderr,"rgb_A_src:%d %s/%s/%s %d rgb_B_src:%d %s/%s/%s %d\n",
675 (inst) & 0x3, toswiz((inst >> 2) & 0x7), toswiz((inst >> 5) & 0x7), toswiz((inst >> 8) & 0x7),
676 (inst >> 11) & 0x3,
677 (inst >> 13) & 0x3, toswiz((inst >> 15) & 0x7), toswiz((inst >> 18) & 0x7), toswiz((inst >> 21) & 0x7),
678 (inst >> 24) & 0x3);
679
680
681 fprintf(stderr,"\t4 ALPHA_INST:0x%08x:", code->inst[n].inst4);
682 inst = code->inst[n].inst4;
683 fprintf(stderr,"%s dest:%d%s alp_A_src:%d %s %d alp_B_src:%d %s %d w:%d\n", to_alpha_op(inst & 0xf),
684 (inst >> 4) & 0x7f, inst & (1<<11) ? "(rel)":"",
685 (inst >> 12) & 0x3, toswiz((inst >> 14) & 0x7), (inst >> 17) & 0x3,
686 (inst >> 19) & 0x3, toswiz((inst >> 21) & 0x7), (inst >> 24) & 0x3,
687 (inst >> 31) & 0x1);
688
689 fprintf(stderr,"\t5 RGBA_INST: 0x%08x:", code->inst[n].inst5);
690 inst = code->inst[n].inst5;
691 fprintf(stderr,"%s dest:%d%s rgb_C_src:%d %s/%s/%s %d alp_C_src:%d %s %d\n", toop(inst & 0xf),
692 (inst >> 4) & 0x7f, inst & (1<<11) ? "(rel)":"",
693 (inst >> 12) & 0x3, toswiz((inst >> 14) & 0x7), toswiz((inst >> 17) & 0x7), toswiz((inst >> 20) & 0x7),
694 (inst >> 23) & 0x3,
695 (inst >> 25) & 0x3, toswiz((inst >> 27) & 0x7), (inst >> 30) & 0x3);
696 break;
697 case 2:
698 break;
699 case 3:
700 inst = code->inst[n].inst1;
701 fprintf(stderr,"\t1:TEX_INST: 0x%08x: id: %d op:%s, %s, %s %s\n", inst, (inst >> 16) & 0xf,
702 to_texop((inst >> 22) & 0x7), (inst & (1<<25)) ? "ACQ" : "",
703 (inst & (1<<26)) ? "IGNUNC" : "", (inst & (1<<27)) ? "UNSCALED" : "SCALED");
704 inst = code->inst[n].inst2;
705 fprintf(stderr,"\t2:TEX_ADDR: 0x%08x: src: %d%s %s/%s/%s/%s dst: %d%s %s/%s/%s/%s\n", inst,
706 inst & 127, inst & (1<<7) ? "(rel)" : "",
707 toswiz((inst >> 8) & 0x3), toswiz((inst >> 10) & 0x3),
708 toswiz((inst >> 12) & 0x3), toswiz((inst >> 14) & 0x3),
709 (inst >> 16) & 127, inst & (1<<23) ? "(rel)" : "",
710 toswiz((inst >> 24) & 0x3), toswiz((inst >> 26) & 0x3),
711 toswiz((inst >> 28) & 0x3), toswiz((inst >> 30) & 0x3));
712
713 fprintf(stderr,"\t3:TEX_DXDY: 0x%08x\n", code->inst[n].inst3);
714 break;
715 }
716 fprintf(stderr,"\n");
717 }
718
719 }