summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/cell/spu/spu_per_fragment_op.c
blob: 3b9566042a1d1369f19c6f819b6dedecd7666251 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/**************************************************************************
 * 
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

/**
 * \author Brian Paul
 */


#include <transpose_matrix4x4.h>
#include "pipe/p_format.h"
#include "spu_main.h"
#include "spu_colorpack.h"
#include "spu_per_fragment_op.h"


#define LINEAR_QUAD_LAYOUT 1


static INLINE vector float
spu_min(vector float a, vector float b)
{
   vector unsigned int m;
   m = spu_cmpgt(a, b);    /* m = a > b ? ~0 : 0 */
   return spu_sel(a, b, m);
}


static INLINE vector float
spu_max(vector float a, vector float b)
{
   vector unsigned int m;
   m = spu_cmpgt(a, b);    /* m = a > b ? ~0 : 0 */
   return spu_sel(b, a, m);
}


/**
 * Called by rasterizer for each quad after the shader has run.  Do
 * all the per-fragment operations including alpha test, z test,
 * stencil test, blend, colormask and logicops.  This is a
 * fallback/debug function.  In reality we'll use a generated function
 * produced by the PPU.  But this function is useful for
 * debug/validation.
 */
void
spu_fallback_fragment_ops(uint x, uint y,
                          tile_t *colorTile,
                          tile_t *depthStencilTile,
                          vector float fragZ,
                          vector float fragR,
                          vector float fragG,
                          vector float fragB,
                          vector float fragA,
                          vector unsigned int mask)
{
   vector float frag_aos[4];
   unsigned int fbc0, fbc1, fbc2, fbc3 ; /* framebuffer/tile colors */
   unsigned int fragc0, fragc1, fragc2, fragc3;  /* fragment colors */

   /*
    * Do alpha test
    */
   if (spu.depth_stencil_alpha.alpha.enabled) {
      vector float ref = spu_splats(spu.depth_stencil_alpha.alpha.ref_value);
      vector unsigned int amask;

      switch (spu.depth_stencil_alpha.alpha.func) {
      case PIPE_FUNC_LESS:
         amask = spu_cmpgt(ref, fragA);  /* mask = (fragA < ref) */
         break;
      case PIPE_FUNC_GREATER:
         amask = spu_cmpgt(fragA, ref);  /* mask = (fragA > ref) */
         break;
      case PIPE_FUNC_GEQUAL:
         amask = spu_cmpgt(ref, fragA);
         amask = spu_nor(amask, amask);
         break;
      case PIPE_FUNC_LEQUAL:
         amask = spu_cmpgt(fragA, ref);
         amask = spu_nor(amask, amask);
         break;
      case PIPE_FUNC_EQUAL:
         amask = spu_cmpeq(ref, fragA);
         break;
      case PIPE_FUNC_NOTEQUAL:
         amask = spu_cmpeq(ref, fragA);
         amask = spu_nor(amask, amask);
         break;
      case PIPE_FUNC_ALWAYS:
         amask = spu_splats(0xffffffffU);
         break;
      case PIPE_FUNC_NEVER:
         amask = spu_splats( 0x0U);
         break;
      default:
         ;
      }

      mask = spu_and(mask, amask);
   }


   /*
    * Z and/or stencil testing...
    */
   if (spu.depth_stencil_alpha.depth.enabled ||
       spu.depth_stencil_alpha.stencil[0].enabled) {

      /* get four Z/Stencil values from tile */
      vector unsigned int mask24 = spu_splats((unsigned int)0x00ffffffU);
      vector unsigned int ifbZS = depthStencilTile->ui4[y/2][x/2];
      vector unsigned int ifbZ = spu_and(ifbZS, mask24);
      vector unsigned int ifbS = spu_andc(ifbZS, mask24);

      if (spu.depth_stencil_alpha.stencil[0].enabled) {
         /* do stencil test */
         ASSERT(spu.fb.depth_format == PIPE_FORMAT_Z24_UNORM_S8_USCALED);

      }
      else if (spu.depth_stencil_alpha.depth.enabled) {
         /* do depth test */

         ASSERT(spu.fb.depth_format == PIPE_FORMAT_Z24_UNORM_S8_USCALED ||
                spu.fb.depth_format == PIPE_FORMAT_Z24X8_UNORM);

         vector unsigned int ifragZ;
         vector unsigned int zmask;

         /* convert four fragZ from float to uint */
         fragZ = spu_mul(fragZ, spu_splats((float) 0xffffff));
         ifragZ = spu_convtu(fragZ, 0);

         /* do depth comparison, setting zmask with results */
         switch (spu.depth_stencil_alpha.depth.func) {
         case PIPE_FUNC_LESS:
            zmask = spu_cmpgt(ifbZ, ifragZ);  /* mask = (ifragZ < ifbZ) */
            break;
         case PIPE_FUNC_GREATER:
            zmask = spu_cmpgt(ifragZ, ifbZ);  /* mask = (ifbZ > ifragZ) */
            break;
         case PIPE_FUNC_GEQUAL:
            zmask = spu_cmpgt(ifbZ, ifragZ);
            zmask = spu_nor(zmask, zmask);
            break;
         case PIPE_FUNC_LEQUAL:
            zmask = spu_cmpgt(ifragZ, ifbZ);
            zmask = spu_nor(zmask, zmask);
            break;
         case PIPE_FUNC_EQUAL:
            zmask = spu_cmpeq(ifbZ, ifragZ);
            break;
         case PIPE_FUNC_NOTEQUAL:
            zmask = spu_cmpeq(ifbZ, ifragZ);
            zmask = spu_nor(zmask, zmask);
            break;
         case PIPE_FUNC_ALWAYS:
            zmask = spu_splats(0xffffffffU);
            break;
         case PIPE_FUNC_NEVER:
            zmask = spu_splats( 0x0U);
            break;
         default:
            ;
         }

         mask = spu_and(mask, zmask);

         /* merge framebuffer Z and fragment Z according to the mask */
         ifbZ = spu_or(spu_and(ifragZ, mask),
                       spu_andc(ifbZ, mask));
      }

      if (spu_extract(spu_orx(mask), 0)) {
         /* put new fragment Z/Stencil values back into Z/Stencil tile */
         depthStencilTile->ui4[y/2][x/2] = spu_or(ifbZ, ifbS);

         spu.cur_ztile_status = TILE_STATUS_DIRTY;
      }
   }


   /*
    * If we'll need the current framebuffer/tile colors for blending
    * or logicop or colormask, fetch them now.
    */
   if (spu.blend.rt[0].blend_enable ||
       spu.blend.logicop_enable ||
       spu.blend.rt[0].colormask != 0xf) {

#if LINEAR_QUAD_LAYOUT /* See comments/diagram below */
      fbc0 = colorTile->ui[y][x*2+0];
      fbc1 = colorTile->ui[y][x*2+1];
      fbc2 = colorTile->ui[y][x*2+2];
      fbc3 = colorTile->ui[y][x*2+3];
#else
      fbc0 = colorTile->ui[y+0][x+0];
      fbc1 = colorTile->ui[y+0][x+1];
      fbc2 = colorTile->ui[y+1][x+0];
      fbc3 = colorTile->ui[y+1][x+1];
#endif
   }


   /*
    * Do blending
    */
   if (spu.blend.rt[0].blend_enable) {
      /* blending terms, misc regs */
      vector float term1r, term1g, term1b, term1a;
      vector float term2r, term2g, term2b, term2a;
      vector float one, tmp;

      vector float fbRGBA[4];  /* current framebuffer colors */

      /* convert framebuffer colors from packed int to vector float */
      {
         vector float temp[4]; /* float colors in AOS form */
         switch (spu.fb.color_format) {
         case PIPE_FORMAT_A8R8G8B8_UNORM:
            temp[0] = spu_unpack_B8G8R8A8(fbc0);
            temp[1] = spu_unpack_B8G8R8A8(fbc1);
            temp[2] = spu_unpack_B8G8R8A8(fbc2);
            temp[3] = spu_unpack_B8G8R8A8(fbc3);
            break;
         case PIPE_FORMAT_B8G8R8A8_UNORM:
            temp[0] = spu_unpack_A8R8G8B8(fbc0);
            temp[1] = spu_unpack_A8R8G8B8(fbc1);
            temp[2] = spu_unpack_A8R8G8B8(fbc2);
            temp[3] = spu_unpack_A8R8G8B8(fbc3);
            break;
         default:
            ASSERT(0);
         }
         _transpose_matrix4x4(fbRGBA, temp); /* fbRGBA = transpose(temp) */
      }

      /*
       * Compute Src RGB terms (fragment color * factor)
       */
      switch (spu.blend.rt[0].rgb_src_factor) {
      case PIPE_BLENDFACTOR_ONE:
         term1r = fragR;
         term1g = fragG;
         term1b = fragB;
         break;
      case PIPE_BLENDFACTOR_ZERO:
         term1r =
         term1g =
         term1b = spu_splats(0.0f);
         break;
      case PIPE_BLENDFACTOR_SRC_COLOR:
         term1r = spu_mul(fragR, fragR);
         term1g = spu_mul(fragG, fragG);
         term1b = spu_mul(fragB, fragB);
         break;
      case PIPE_BLENDFACTOR_SRC_ALPHA:
         term1r = spu_mul(fragR, fragA);
         term1g = spu_mul(fragG, fragA);
         term1b = spu_mul(fragB, fragA);
         break;
      case PIPE_BLENDFACTOR_DST_COLOR:
         term1r = spu_mul(fragR, fbRGBA[0]);
         term1g = spu_mul(fragG, fbRGBA[1]);
         term1b = spu_mul(fragB, fbRGBA[1]);
         break;
      case PIPE_BLENDFACTOR_DST_ALPHA:
         term1r = spu_mul(fragR, fbRGBA[3]);
         term1g = spu_mul(fragG, fbRGBA[3]);
         term1b = spu_mul(fragB, fbRGBA[3]);
         break;
      case PIPE_BLENDFACTOR_CONST_COLOR:
         term1r = spu_mul(fragR, spu_splats(spu.blend_color.color[0]));
         term1g = spu_mul(fragG, spu_splats(spu.blend_color.color[1]));
         term1b = spu_mul(fragB, spu_splats(spu.blend_color.color[2]));
         break;
      case PIPE_BLENDFACTOR_CONST_ALPHA:
         term1r = spu_mul(fragR, spu_splats(spu.blend_color.color[3]));
         term1g = spu_mul(fragG, spu_splats(spu.blend_color.color[3]));
         term1b = spu_mul(fragB, spu_splats(spu.blend_color.color[3]));
         break;
      /* XXX more cases */
      default:
         ASSERT(0);
      }

      /*
       * Compute Src Alpha term (fragment alpha * factor)
       */
      switch (spu.blend.rt[0].alpha_src_factor) {
      case PIPE_BLENDFACTOR_ONE:
         term1a = fragA;
         break;
      case PIPE_BLENDFACTOR_SRC_COLOR:
         term1a = spu_splats(0.0f);
         break;
      case PIPE_BLENDFACTOR_SRC_ALPHA:
         term1a = spu_mul(fragA, fragA);
         break;
      case PIPE_BLENDFACTOR_DST_COLOR:
         /* fall-through */
      case PIPE_BLENDFACTOR_DST_ALPHA:
         term1a = spu_mul(fragA, fbRGBA[3]);
         break;
      case PIPE_BLENDFACTOR_CONST_COLOR:
         /* fall-through */
      case PIPE_BLENDFACTOR_CONST_ALPHA:
         term1a = spu_mul(fragR, spu_splats(spu.blend_color.color[3]));
         break;
      /* XXX more cases */
      default:
         ASSERT(0);
      }

      /*
       * Compute Dest RGB terms (framebuffer color * factor)
       */
      switch (spu.blend.rt[0].rgb_dst_factor) {
      case PIPE_BLENDFACTOR_ONE:
         term2r = fbRGBA[0];
         term2g = fbRGBA[1];
         term2b = fbRGBA[2];
         break;
      case PIPE_BLENDFACTOR_ZERO:
         term2r =
         term2g =
         term2b = spu_splats(0.0f);
         break;
      case PIPE_BLENDFACTOR_SRC_COLOR:
         term2r = spu_mul(fbRGBA[0], fragR);
         term2g = spu_mul(fbRGBA[1], fragG);
         term2b = spu_mul(fbRGBA[2], fragB);
         break;
      case PIPE_BLENDFACTOR_SRC_ALPHA:
         term2r = spu_mul(fbRGBA[0], fragA);
         term2g = spu_mul(fbRGBA[1], fragA);
         term2b = spu_mul(fbRGBA[2], fragA);
         break;
      case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
         one = spu_splats(1.0f);
         tmp = spu_sub(one, fragA);
         term2r = spu_mul(fbRGBA[0], tmp);
         term2g = spu_mul(fbRGBA[1], tmp);
         term2b = spu_mul(fbRGBA[2], tmp);
         break;
      case PIPE_BLENDFACTOR_DST_COLOR:
         term2r = spu_mul(fbRGBA[0], fbRGBA[0]);
         term2g = spu_mul(fbRGBA[1], fbRGBA[1]);
         term2b = spu_mul(fbRGBA[2], fbRGBA[2]);
         break;
      case PIPE_BLENDFACTOR_DST_ALPHA:
         term2r = spu_mul(fbRGBA[0], fbRGBA[3]);
         term2g = spu_mul(fbRGBA[1], fbRGBA[3]);
         term2b = spu_mul(fbRGBA[2], fbRGBA[3]);
         break;
      case PIPE_BLENDFACTOR_CONST_COLOR:
         term2r = spu_mul(fbRGBA[0], spu_splats(spu.blend_color.color[0]));
         term2g = spu_mul(fbRGBA[1], spu_splats(spu.blend_color.color[1]));
         term2b = spu_mul(fbRGBA[2], spu_splats(spu.blend_color.color[2]));
         break;
      case PIPE_BLENDFACTOR_CONST_ALPHA:
         term2r = spu_mul(fbRGBA[0], spu_splats(spu.blend_color.color[3]));
         term2g = spu_mul(fbRGBA[1], spu_splats(spu.blend_color.color[3]));
         term2b = spu_mul(fbRGBA[2], spu_splats(spu.blend_color.color[3]));
         break;
       /* XXX more cases */
      default:
         ASSERT(0);
      }

      /*
       * Compute Dest Alpha term (framebuffer alpha * factor)
       */
      switch (spu.blend.rt[0].alpha_dst_factor) {
      case PIPE_BLENDFACTOR_ONE:
         term2a = fbRGBA[3];
         break;
      case PIPE_BLENDFACTOR_SRC_COLOR:
         term2a = spu_splats(0.0f);
         break;
      case PIPE_BLENDFACTOR_SRC_ALPHA:
         term2a = spu_mul(fbRGBA[3], fragA);
         break;
      case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
         one = spu_splats(1.0f);
         tmp = spu_sub(one, fragA);
         term2a = spu_mul(fbRGBA[3], tmp);
         break;
      case PIPE_BLENDFACTOR_DST_COLOR:
         /* fall-through */
      case PIPE_BLENDFACTOR_DST_ALPHA:
         term2a = spu_mul(fbRGBA[3], fbRGBA[3]);
         break;
      case PIPE_BLENDFACTOR_CONST_COLOR:
         /* fall-through */
      case PIPE_BLENDFACTOR_CONST_ALPHA:
         term2a = spu_mul(fbRGBA[3], spu_splats(spu.blend_color.color[3]));
         break;
      /* XXX more cases */
      default:
         ASSERT(0);
      }

      /*
       * Combine Src/Dest RGB terms
       */
      switch (spu.blend.rt[0].rgb_func) {
      case PIPE_BLEND_ADD:
         fragR = spu_add(term1r, term2r);
         fragG = spu_add(term1g, term2g);
         fragB = spu_add(term1b, term2b);
         break;
      case PIPE_BLEND_SUBTRACT:
         fragR = spu_sub(term1r, term2r);
         fragG = spu_sub(term1g, term2g);
         fragB = spu_sub(term1b, term2b);
         break;
      case PIPE_BLEND_REVERSE_SUBTRACT:
         fragR = spu_sub(term2r, term1r);
         fragG = spu_sub(term2g, term1g);
         fragB = spu_sub(term2b, term1b);
         break;
      case PIPE_BLEND_MIN:
         fragR = spu_min(term1r, term2r);
         fragG = spu_min(term1g, term2g);
         fragB = spu_min(term1b, term2b);
         break;
      case PIPE_BLEND_MAX:
         fragR = spu_max(term1r, term2r);
         fragG = spu_max(term1g, term2g);
         fragB = spu_max(term1b, term2b);
         break;
      default:
         ASSERT(0);
      }

      /*
       * Combine Src/Dest A term
       */
      switch (spu.blend.rt[0].alpha_func) {
      case PIPE_BLEND_ADD:
         fragA = spu_add(term1a, term2a);
         break;
      case PIPE_BLEND_SUBTRACT:
         fragA = spu_sub(term1a, term2a);
         break;
      case PIPE_BLEND_REVERSE_SUBTRACT:
         fragA = spu_sub(term2a, term1a);
         break;
      case PIPE_BLEND_MIN:
         fragA = spu_min(term1a, term2a);
         break;
      case PIPE_BLEND_MAX:
         fragA = spu_max(term1a, term2a);
         break;
      default:
         ASSERT(0);
      }
   }


   /*
    * Convert RRRR,GGGG,BBBB,AAAA to RGBA,RGBA,RGBA,RGBA.
    */
#if 0
   /* original code */
   {
      vector float frag_soa[4];
      frag_soa[0] = fragR;
      frag_soa[1] = fragG;
      frag_soa[2] = fragB;
      frag_soa[3] = fragA;
      _transpose_matrix4x4(frag_aos, frag_soa);
   }
#else
   /* short-cut relying on function parameter layout: */
   _transpose_matrix4x4(frag_aos, &fragR);
   (void) fragG;
   (void) fragB;
#endif

   /*
    * Pack fragment float colors into 32-bit RGBA words.
    */
   switch (spu.fb.color_format) {
   case PIPE_FORMAT_B8G8R8A8_UNORM:
      fragc0 = spu_pack_A8R8G8B8(frag_aos[0]);
      fragc1 = spu_pack_A8R8G8B8(frag_aos[1]);
      fragc2 = spu_pack_A8R8G8B8(frag_aos[2]);
      fragc3 = spu_pack_A8R8G8B8(frag_aos[3]);
      break;
   case PIPE_FORMAT_A8R8G8B8_UNORM:
      fragc0 = spu_pack_B8G8R8A8(frag_aos[0]);
      fragc1 = spu_pack_B8G8R8A8(frag_aos[1]);
      fragc2 = spu_pack_B8G8R8A8(frag_aos[2]);
      fragc3 = spu_pack_B8G8R8A8(frag_aos[3]);
      break;
   default:
      fprintf(stderr, "SPU: Bad pixel format in spu_default_fragment_ops\n");
      ASSERT(0);
   }


   /*
    * Do color masking
    */
   if (spu.blend.rt[0].colormask != 0xf) {
      uint cmask = 0x0; /* each byte corresponds to a color channel */

      /* Form bitmask depending on color buffer format and colormask bits */
      switch (spu.fb.color_format) {
      case PIPE_FORMAT_B8G8R8A8_UNORM:
         if (spu.blend.rt[0].colormask & PIPE_MASK_R)
            cmask |= 0x00ff0000; /* red */
         if (spu.blend.rt[0].colormask & PIPE_MASK_G)
            cmask |= 0x0000ff00; /* green */
         if (spu.blend.rt[0].colormask & PIPE_MASK_B)
            cmask |= 0x000000ff; /* blue */
         if (spu.blend.rt[0].colormask & PIPE_MASK_A)
            cmask |= 0xff000000; /* alpha */
         break;
      case PIPE_FORMAT_A8R8G8B8_UNORM:
         if (spu.blend.rt[0].colormask & PIPE_MASK_R)
            cmask |= 0x0000ff00; /* red */
         if (spu.blend.rt[0].colormask & PIPE_MASK_G)
            cmask |= 0x00ff0000; /* green */
         if (spu.blend.rt[0].colormask & PIPE_MASK_B)
            cmask |= 0xff000000; /* blue */
         if (spu.blend.rt[0].colormask & PIPE_MASK_A)
            cmask |= 0x000000ff; /* alpha */
         break;
      default:
         ASSERT(0);
      }

      /*
       * Apply color mask to the 32-bit packed colors.
       * if (cmask[i])
       *    frag color[i] = frag color[i];
       * else
       *    frag color[i] = framebuffer color[i];
       */
      fragc0 = (fragc0 & cmask) | (fbc0 & ~cmask);
      fragc1 = (fragc1 & cmask) | (fbc1 & ~cmask);
      fragc2 = (fragc2 & cmask) | (fbc2 & ~cmask);
      fragc3 = (fragc3 & cmask) | (fbc3 & ~cmask);
   }


   /*
    * Do logic ops
    */
   if (spu.blend.logicop_enable) {
      /* XXX to do */
      /* apply logicop to 32-bit packed colors (fragcx and fbcx) */
   }


   /*
    * If mask is non-zero, mark tile as dirty.
    */
   if (spu_extract(spu_orx(mask), 0)) {
      spu.cur_ctile_status = TILE_STATUS_DIRTY;
   }
   else {
      /* write no fragments */
      return;
   }


   /*
    * Write new fragment/quad colors to the framebuffer/tile.
    * Only write pixels where the corresponding mask word is set.
    */
#if LINEAR_QUAD_LAYOUT
   /*
    * Quad layout:
    *  +--+--+--+--+
    *  |p0|p1|p2|p3|...
    *  +--+--+--+--+
    */
   if (spu_extract(mask, 0))
      colorTile->ui[y][x*2] = fragc0;
   if (spu_extract(mask, 1))
      colorTile->ui[y][x*2+1] = fragc1;
   if (spu_extract(mask, 2))
      colorTile->ui[y][x*2+2] = fragc2;
   if (spu_extract(mask, 3))
      colorTile->ui[y][x*2+3] = fragc3;
#else
   /*
    * Quad layout:
    *  +--+--+
    *  |p0|p1|...
    *  +--+--+
    *  |p2|p3|...
    *  +--+--+
    */
   if (spu_extract(mask, 0))
      colorTile->ui[y+0][x+0] = fragc0;
   if (spu_extract(mask, 1))
      colorTile->ui[y+0][x+1] = fragc1;
   if (spu_extract(mask, 2))
      colorTile->ui[y+1][x+0] = fragc2;
   if (spu_extract(mask, 3))
      colorTile->ui[y+1][x+1] = fragc3;
#endif
}