OSDN Git Service

R600: initial copy of r300 code
[android-x86/external-mesa.git] / src / mesa / drivers / dri / r600 / r700_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 "r700_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].NegateBase = tgt[0].SrcReg[2].NegateBase ^ NEGATE_XYZW;
160                 else
161                         tgt[1].SrcReg[0].NegateBase = tgt[0].SrcReg[0].NegateBase ^ 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.NegateAbs)
318                         reg.NegateBase ^= 15;
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.NegateBase &= ~(1 << i);
328                                         continue;
329                                 }
330                                 if (swz >= 4)
331                                         return GL_FALSE;
332                         }
333                 }
334
335                 if (reg.NegateBase)
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
343                                 && !reg.NegateBase && !reg.NegateAbs)
344                         return GL_TRUE;
345
346                 return GL_FALSE;
347         } else {
348                 /* ALU instructions support almost everything */
349                 if (reg.Abs)
350                         return GL_TRUE;
351
352                 relevant = 0;
353                 for(i = 0; i < 3; ++i) {
354                         GLuint swz = GET_SWZ(reg.Swizzle, i);
355                         if (swz != SWIZZLE_NIL && swz != SWIZZLE_ZERO)
356                                 relevant |= 1 << i;
357                 }
358                 if ((reg.NegateBase & relevant) && ((reg.NegateBase & relevant) != relevant))
359                         return GL_FALSE;
360
361                 return GL_TRUE;
362         }
363 }
364
365 /**
366  * Implement a MOV with a potentially non-native swizzle.
367  *
368  * The only thing we *cannot* do in an ALU instruction is per-component
369  * negation. Therefore, we split the MOV into two instructions when necessary.
370  */
371 static void nqssadce_build_swizzle(struct nqssadce_state *s,
372         struct prog_dst_register dst, struct prog_src_register src)
373 {
374         struct prog_instruction *inst;
375         GLuint negatebase[2] = { 0, 0 };
376         int i;
377
378         for(i = 0; i < 4; ++i) {
379                 GLuint swz = GET_SWZ(src.Swizzle, i);
380                 if (swz == SWIZZLE_NIL)
381                         continue;
382                 negatebase[GET_BIT(src.NegateBase, i)] |= 1 << i;
383         }
384
385         _mesa_insert_instructions(s->Program, s->IP, (negatebase[0] ? 1 : 0) + (negatebase[1] ? 1 : 0));
386         inst = s->Program->Instructions + s->IP;
387
388         for(i = 0; i <= 1; ++i) {
389                 if (!negatebase[i])
390                         continue;
391
392                 inst->Opcode = OPCODE_MOV;
393                 inst->DstReg = dst;
394                 inst->DstReg.WriteMask = negatebase[i];
395                 inst->SrcReg[0] = src;
396                 inst++;
397                 s->IP++;
398         }
399 }
400
401 static GLuint build_dtm(GLuint depthmode)
402 {
403         switch(depthmode) {
404         default:
405         case GL_LUMINANCE: return 0;
406         case GL_INTENSITY: return 1;
407         case GL_ALPHA: return 2;
408         }
409 }
410
411 static GLuint build_func(GLuint comparefunc)
412 {
413         return comparefunc - GL_NEVER;
414 }
415
416
417 /**
418  * Collect all external state that is relevant for compiling the given
419  * fragment program.
420  */
421 static void build_state(
422         r300ContextPtr r300,
423         struct r500_fragment_program *fp,
424         struct r500_fragment_program_external_state *state)
425 {
426         int unit;
427
428         _mesa_bzero(state, sizeof(*state));
429
430         for(unit = 0; unit < 16; ++unit) {
431                 if (fp->mesa_program.Base.ShadowSamplers & (1 << unit)) {
432                         struct gl_texture_object* tex = r300->radeon.glCtx->Texture.Unit[unit]._Current;
433
434                         state->unit[unit].depth_texture_mode = build_dtm(tex->DepthMode);
435                         state->unit[unit].texture_compare_func = build_func(tex->CompareFunc);
436                 }
437         }
438 }
439
440 static void dump_program(struct r500_fragment_program_code *code);
441
442 void r500TranslateFragmentShader(r300ContextPtr r300,
443                                  struct r500_fragment_program *fp)
444 {
445         struct r500_fragment_program_external_state state;
446
447         build_state(r300, fp, &state);
448         if (_mesa_memcmp(&fp->state, &state, sizeof(state))) {
449                 /* TODO: cache compiled programs */
450                 fp->translated = GL_FALSE;
451                 _mesa_memcpy(&fp->state, &state, sizeof(state));
452         }
453
454         if (!fp->translated) {
455                 struct r500_fragment_program_compiler compiler;
456
457                 compiler.r300 = r300;
458                 compiler.fp = fp;
459                 compiler.code = &fp->code;
460                 compiler.program = _mesa_clone_program(r300->radeon.glCtx, &fp->mesa_program.Base);
461
462                 if (RADEON_DEBUG & DEBUG_PIXEL) {
463                         _mesa_printf("Compiler: Initial program:\n");
464                         _mesa_print_program(compiler.program);
465                 }
466
467                 insert_WPOS_trailer(&compiler);
468
469                 struct radeon_program_transformation transformations[] = {
470                         { &transform_TEX, &compiler },
471                         { &radeonTransformALU, 0 },
472                         { &radeonTransformDeriv, 0 },
473                         { &radeonTransformTrigScale, 0 }
474                 };
475                 radeonLocalTransform(r300->radeon.glCtx, compiler.program,
476                         4, transformations);
477
478                 if (RADEON_DEBUG & DEBUG_PIXEL) {
479                         _mesa_printf("Compiler: after native rewrite:\n");
480                         _mesa_print_program(compiler.program);
481                 }
482
483                 struct radeon_nqssadce_descr nqssadce = {
484                         .Init = &nqssadce_init,
485                         .IsNativeSwizzle = &is_native_swizzle,
486                         .BuildSwizzle = &nqssadce_build_swizzle,
487                         .RewriteDepthOut = GL_TRUE
488                 };
489                 radeonNqssaDce(r300->radeon.glCtx, compiler.program, &nqssadce);
490
491                 if (RADEON_DEBUG & DEBUG_PIXEL) {
492                         _mesa_printf("Compiler: after NqSSA-DCE:\n");
493                         _mesa_print_program(compiler.program);
494                 }
495
496                 fp->translated = r500FragmentProgramEmit(&compiler);
497
498                 /* Subtle: Rescue any parameters that have been added during transformations */
499                 _mesa_free_parameter_list(fp->mesa_program.Base.Parameters);
500                 fp->mesa_program.Base.Parameters = compiler.program->Parameters;
501                 compiler.program->Parameters = 0;
502
503                 _mesa_reference_program(r300->radeon.glCtx, &compiler.program, 0);
504
505                 r300UpdateStateParameters(r300->radeon.glCtx, _NEW_PROGRAM);
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 }