summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/mga/mga_texcombine.c
blob: bbfa29be5fe9aeee19681196ebe5dc7eef6c35d8 (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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
 * Copyright (c) 2003 Ville Syrjala
 *
 * 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, sublicense,
 * 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 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 NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 * Authors:
 *    Ville Syrjala <syrjala@sci.fi>
 */

#include "glheader.h"

#include "mgacontext.h"
#include "mgatex.h"
#include "mgaregs.h"

/*
 * GL_ARB_texture_env_combine
 * GL_EXT_texture_env_combine
 * GL_ARB_texture_env_crossbar
 * GL_ATI_texture_env_combine3
 */

#define ARG_DISABLE 0xffffffff
#define MGA_ARG1  0
#define MGA_ARG2  1
#define MGA_ALPHA 2

GLboolean mgaUpdateTextureEnvCombine( GLcontext *ctx, int unit )
{
   mgaContextPtr mmesa = MGA_CONTEXT(ctx);
   const int source = mmesa->tmu_source[unit];
   const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[source];
   GLuint *reg = ((GLuint *)&mmesa->setup.tdualstage0 + unit);
   GLuint numColorArgs = 0, numAlphaArgs = 0;
   GLuint arg1[3], arg2[3], alpha[3];
   int args[3];
   int i;

   switch (texUnit->Combine.ModeRGB) {
   case GL_REPLACE:
      numColorArgs = 1;
      break;
   case GL_MODULATE:
   case GL_ADD:
   case GL_ADD_SIGNED:
   case GL_SUBTRACT:
      numColorArgs = 2;
      break;
   case GL_INTERPOLATE:
   case GL_MODULATE_ADD_ATI:
   case GL_MODULATE_SIGNED_ADD_ATI:
   case GL_MODULATE_SUBTRACT_ATI:
      numColorArgs = 3;
      break;
   default:
      return GL_FALSE;
   }

   switch (texUnit->Combine.ModeA) {
   case GL_REPLACE:
      numAlphaArgs = 1;
      break;
   case GL_MODULATE:
   case GL_ADD:
   case GL_ADD_SIGNED:
   case GL_SUBTRACT:
      numAlphaArgs = 2;
      break;
   default:
      return GL_FALSE;
   }

   /* Start fresh :) */
   *reg = 0;

   /* COLOR */
   for (i = 0; i < 3; i++) {
      arg1[i] = 0;
      arg2[i] = 0;
      alpha[i] = 0;
   }

   for (i = 0;i < numColorArgs; i++) {
      switch (texUnit->Combine.SourceRGB[i]) {
      case GL_TEXTURE:
         arg1[i] |= 0;
         arg2[i] |= ARG_DISABLE;
         alpha[i] |= TD0_color_alpha_currtex;
         break;
      case GL_TEXTURE0:
         if (source == 0) {
            arg1[i] |= 0;
            arg2[i] |= ARG_DISABLE;
            alpha[i] |= TD0_color_alpha_currtex;
         } else {
            if (ctx->Texture._EnabledUnits != 0x03) {
               /* disable texturing */
               mmesa->setup.dwgctl &= DC_opcod_MASK;
               mmesa->setup.dwgctl |= DC_opcod_trap;
               mmesa->hw.alpha_sel = AC_alphasel_diffused;
               /* return GL_TRUE since we don't need a fallback */
               return GL_TRUE;
            }
            arg1[i] |= ARG_DISABLE;
            arg2[i] |= ARG_DISABLE;
            alpha[i] |= TD0_color_alpha_prevtex;
         }
         break;
      case GL_TEXTURE1:
         if (source == 0) {
            if (ctx->Texture._EnabledUnits != 0x03) {
               /* disable texturing */
               mmesa->setup.dwgctl &= DC_opcod_MASK;
               mmesa->setup.dwgctl |= DC_opcod_trap;
               mmesa->hw.alpha_sel = AC_alphasel_diffused;
               /* return GL_TRUE since we don't need a fallback */
               return GL_TRUE;
            }
            arg1[i] |= ARG_DISABLE;
            /* G400 specs (TDUALSTAGE0) */
            arg2[i] |= TD0_color_arg2_prevstage;
            alpha[i] |= TD0_color_alpha_prevstage;
         } else {
            arg1[i] |= 0;
            arg2[i] |= ARG_DISABLE;
            alpha[i] |= TD0_color_alpha_currtex;
         }
         break;
      case GL_CONSTANT:
         if (mmesa->fcol_used &&
             mmesa->envcolor[source] != mmesa->envcolor[!source])
            return GL_FALSE;

         arg1[i] |= ARG_DISABLE;
         arg2[i] |= TD0_color_arg2_fcol;
         alpha[i] |= TD0_color_alpha_fcol;

         mmesa->setup.fcol = mmesa->envcolor[source];
         mmesa->fcol_used = GL_TRUE;
         break;
      case GL_PRIMARY_COLOR:
         arg1[i] |= ARG_DISABLE;
         /* G400 specs (TDUALSTAGE1) */
         if (unit == 0 || (mmesa->setup.tdualstage0 &
                           ((TD0_color_sel_mul & TD0_color_sel_add) |
                            (TD0_alpha_sel_mul & TD0_alpha_sel_add)))) {
            arg2[i] |= TD0_color_arg2_diffuse;
            alpha[i] |= TD0_color_alpha_diffuse;
         } else {
            arg2[i] |= ARG_DISABLE;
            alpha[i] |= ARG_DISABLE;
         }
         break;
      case GL_PREVIOUS:
         arg1[i] |= ARG_DISABLE;
         if (unit == 0) {
            arg2[i] |= TD0_color_arg2_diffuse;
            alpha[i] |= TD0_color_alpha_diffuse;
         } else {
            arg2[i] |= TD0_color_arg2_prevstage;
            alpha[i] |= TD0_color_alpha_prevstage;
         }
         break;
      default:
         return GL_FALSE;
      }

      switch (texUnit->Combine.OperandRGB[i]) {
      case GL_SRC_COLOR:
         arg1[i] |= 0;
         arg2[i] |= 0;
         if (texUnit->Combine.SourceRGB[i] == GL_CONSTANT &&
             RGBA_EQUAL( mmesa->envcolor[source] )) {
            alpha[i] |= 0;
         } else {
            alpha[i] |= ARG_DISABLE;
         }
         break;
      case GL_ONE_MINUS_SRC_COLOR:
         arg1[i] |= TD0_color_arg1_inv_enable;
         arg2[i] |= TD0_color_arg2_inv_enable;
         if (texUnit->Combine.SourceRGB[i] == GL_CONSTANT &&
             RGBA_EQUAL( mmesa->envcolor[source] )) {
            alpha[i] |= (TD0_color_alpha1inv_enable |
                         TD0_color_alpha2inv_enable);
         } else {
            alpha[i] |= ARG_DISABLE;
         }
         break;
      case GL_SRC_ALPHA:
         arg1[i] |= TD0_color_arg1_replicatealpha_enable;
         arg2[i] |= TD0_color_arg2_replicatealpha_enable;
         alpha[i] |= 0;
         break;
      case GL_ONE_MINUS_SRC_ALPHA:
         arg1[i] |= (TD0_color_arg1_replicatealpha_enable |
                     TD0_color_arg1_inv_enable);
         arg2[i] |= (TD0_color_arg2_replicatealpha_enable |
                     TD0_color_arg2_inv_enable);
         alpha[i] |= (TD0_color_alpha1inv_enable |
                      TD0_color_alpha2inv_enable);
         break;
      }
   }

   switch (texUnit->Combine.ModeRGB) {
   case GL_MODULATE_ADD_ATI:
   case GL_MODULATE_SIGNED_ADD_ATI:
      /* Special handling for ATI_texture_env_combine3.
       * If Arg1 == Arg0 or Arg1 == Arg2 we can use arg1 or arg2 as input for
       * both multiplier and adder.
       */
      /* Arg1 == arg1 */
      if (arg1[1] == arg1[0]) {
         if ((arg1[1] | arg2[2]) != ARG_DISABLE) {
            *reg |= arg1[1] | arg2[2];
            args[0] = MGA_ARG1; args[1] = MGA_ARG1; args[2] = MGA_ARG2;
            break;
         } else
         if ((arg1[1] | alpha[2]) != ARG_DISABLE) {
            *reg |= arg1[1] | alpha[2];
            args[0] = MGA_ARG1; args[1] = MGA_ARG1; args[2] = MGA_ALPHA;
            break;
         }
      }
      if (arg1[1] == arg1[2]) {
         if ((arg1[1] | arg2[0]) != ARG_DISABLE) {
            *reg |= arg1[1] | arg2[0];
            args[0] = MGA_ARG2; args[1] = MGA_ARG1; args[2] = MGA_ARG1;
            break;
         } else
         if ((arg1[1] | alpha[0]) != ARG_DISABLE) {
            *reg |= arg1[1] | alpha[0];
            args[0] = MGA_ALPHA; args[1] = MGA_ARG1; args[2] = MGA_ARG1;
            break;
         }
      }
      /* fallthrough */
   case GL_MODULATE_SUBTRACT_ATI:
      /* Arg1 == arg2 */
      if (arg2[1] == arg2[0]) {
         if ((arg2[1] | arg1[2]) != ARG_DISABLE) {
            *reg |= arg2[1] | arg1[2];
            args[0] = MGA_ARG2; args[1] = MGA_ARG2; args[2] = MGA_ARG1;
            break;
         } else
         if ((arg2[1] | alpha[2]) != ARG_DISABLE) {
            *reg |= arg2[1] | alpha[2];
            args[0] = MGA_ARG2; args[1] = MGA_ARG2; args[2] = MGA_ALPHA;
            break;
         }
      }
      if (arg2[1] == arg2[2]) {
         if ((arg2[1] | arg1[0]) != ARG_DISABLE) {
            *reg |= arg2[1] | arg1[0];
            args[0] = MGA_ARG1; args[1] = MGA_ARG2; args[2] = MGA_ARG2;
            break;
         } else
         if ((arg2[1] | alpha[0]) != ARG_DISABLE) {
            *reg |= arg2[1] | alpha[0];
            args[0] = MGA_ALPHA; args[1] = MGA_ARG2; args[2] = MGA_ARG2;
            break;
         }
      }
      /* fallthrough */
   default:
      /* Find working combo of arg1, arg2 and alpha.
       *
       * Keep the Arg0 != alpha cases first since there's
       * no way to get alpha out by itself (GL_REPLACE).
       *
       * Keep the Arg2 == alpha cases first because only alpha has the
       * capabilities to function as Arg2 (GL_INTERPOLATE). Also good for 
       * GL_ADD, GL_ADD_SIGNED, GL_SUBTRACT since we can't get alpha to the
       * adder.
       *
       * Keep the Arg1 == alpha cases last for GL_MODULATE_ADD_ATI,
       * GL_MODULATE_SIGNED_ADD_ATI. Again because we can't get alpha to the
       * adder.
       *
       * GL_MODULATE_SUBTRACT_ATI needs special treatment since it requires
       * that Arg1 == arg2. This requirement clashes with those of other modes.
       */
      if ((arg1[0] | arg2[1] | alpha[2]) != ARG_DISABLE) {
         *reg |= arg1[0] | arg2[1] | alpha[2];
         args[0] = MGA_ARG1; args[1] = MGA_ARG2; args[2] = MGA_ALPHA;
      } else
      if ((arg1[1] | arg2[0] | alpha[2]) != ARG_DISABLE &&
          texUnit->Combine.ModeRGB != GL_MODULATE_SUBTRACT_ATI) {
         *reg |= arg1[1] | arg2[0] | alpha[2];
         args[0] = MGA_ARG2; args[1] = MGA_ARG1; args[2] = MGA_ALPHA;
      } else
      if ((arg1[1] | arg2[2] | alpha[0]) != ARG_DISABLE &&
          texUnit->Combine.ModeRGB != GL_MODULATE_SUBTRACT_ATI) {
         *reg |= arg1[1] | arg2[2] | alpha[0];
         args[0] = MGA_ALPHA; args[1] = MGA_ARG1; args[2] = MGA_ARG2;
      } else
      if ((arg1[2] | arg2[1] | alpha[0]) != ARG_DISABLE) {
         *reg |= arg1[2] | arg2[1] | alpha[0];
         args[0] = MGA_ALPHA; args[1] = MGA_ARG2; args[2] = MGA_ARG1;
      } else
      if ((arg1[0] | arg2[2] | alpha[1]) != ARG_DISABLE) {
         *reg |= arg1[0] | arg2[2] | alpha[1];
         args[0] = MGA_ARG1; args[1] = MGA_ALPHA; args[2] = MGA_ARG2;
      } else
      if ((arg1[2] | arg2[0] | alpha[1]) != ARG_DISABLE) {
         *reg |= arg1[2] | arg2[0] | alpha[1];
         args[0] = MGA_ARG2; args[1] = MGA_ALPHA; args[2] = MGA_ARG1;
      } else {
         /* nothing suitable */
         return GL_FALSE;
      }
   }

   switch (texUnit->Combine.ModeRGB) {
   case GL_REPLACE:
      if (texUnit->Combine.ScaleShiftRGB) {
         return GL_FALSE;
      }

      if (args[0] == MGA_ARG1) {
         *reg |= TD0_color_sel_arg1;
      } else if (args[0] == MGA_ARG2) {
         *reg |= TD0_color_sel_arg2;
      } else if (args[0] == MGA_ALPHA) {
         /* Can't get alpha out by itself */
         return GL_FALSE;
      }
      break;
   case GL_MODULATE:
      if (texUnit->Combine.ScaleShiftRGB == 1) {
         *reg |= TD0_color_modbright_2x;
      } else if (texUnit->Combine.ScaleShiftRGB == 2) {
         *reg |= TD0_color_modbright_4x;
      }

      *reg |= TD0_color_sel_mul;

      if (args[0] == MGA_ALPHA || args[1] == MGA_ALPHA) {
         if (args[0] == MGA_ARG1 || args[1] == MGA_ARG1) {
            *reg |= TD0_color_arg2mul_alpha2;
         } else if (args[0] == MGA_ARG2 || args[1] == MGA_ARG2) {
            *reg |= TD0_color_arg1mul_alpha1;
         }
      }
      break;
   case GL_ADD_SIGNED:
      *reg |= TD0_color_addbias_enable;
      /* fallthrough */
   case GL_ADD:
      if (args[0] == MGA_ALPHA || args[1] == MGA_ALPHA) {
         /* Can't get alpha to the adder */
         return GL_FALSE;
      }
      if (texUnit->Combine.ScaleShiftRGB == 1) {
         *reg |= TD0_color_add2x_enable;
      } else if (texUnit->Combine.ScaleShiftRGB == 2) {
         return GL_FALSE;
      }

      *reg |= (TD0_color_add_add |
               TD0_color_sel_add);
      break;
   case GL_INTERPOLATE:
      if (args[2] != MGA_ALPHA) {
         /* Only alpha can function as Arg2 */
         return GL_FALSE;
      }
      if (texUnit->Combine.ScaleShiftRGB == 1) {
         *reg |= TD0_color_add2x_enable;
      } else if (texUnit->Combine.ScaleShiftRGB == 2) {
         return GL_FALSE;
      }

      *reg |= (TD0_color_arg1mul_alpha1 |
               TD0_color_blend_enable |
               TD0_color_arg1add_mulout |
               TD0_color_arg2add_mulout |
               TD0_color_add_add |
               TD0_color_sel_add);

      /* Have to do this with xor since GL_ONE_MINUS_SRC_ALPHA may have
       * already touched this bit.
       */
      *reg ^= TD0_color_alpha1inv_enable;

      if (args[0] == MGA_ARG2) {
         /* Swap arguments */
         *reg ^= (TD0_color_arg1mul_alpha1 |
                  TD0_color_arg2mul_alpha2 |
                  TD0_color_alpha1inv_enable |
                  TD0_color_alpha2inv_enable);
      }

      if (ctx->Texture._EnabledUnits != 0x03) {
         /* Linear blending mode needs dualtex enabled */
         *(reg+1) = (TD0_color_arg2_prevstage |
                     TD0_color_sel_arg2 |
                     TD0_alpha_arg2_prevstage |
                     TD0_alpha_sel_arg2);
         mmesa->force_dualtex = GL_TRUE;
      }
      break;
   case GL_SUBTRACT:
      if (args[0] == MGA_ALPHA || args[1] == MGA_ALPHA) {
         /* Can't get alpha to the adder */
         return GL_FALSE;
      }
      if (texUnit->Combine.ScaleShiftRGB == 1) {
         *reg |= TD0_color_add2x_enable;
      } else if (texUnit->Combine.ScaleShiftRGB == 2) {
         return GL_FALSE;
      }

      *reg |= (TD0_color_add_sub |
               TD0_color_sel_add);

      if (args[0] == MGA_ARG2) {
         /* Swap arguments */
         *reg ^= (TD0_color_arg1_inv_enable |
                  TD0_color_arg2_inv_enable);
      }
      break;
   case GL_MODULATE_SIGNED_ADD_ATI:
      *reg |= TD0_color_addbias_enable;
      /* fallthrough */
   case GL_MODULATE_ADD_ATI:
      if (args[1] == MGA_ALPHA) {
         /* Can't get alpha to the adder */
         return GL_FALSE;
      }
      if (texUnit->Combine.ScaleShiftRGB == 1) {
         *reg |= TD0_color_add2x_enable;
      } else if (texUnit->Combine.ScaleShiftRGB == 2) {
         return GL_FALSE;
      }

      *reg |= (TD0_color_add_add |
               TD0_color_sel_add);

      if (args[1] == args[0] || args[1] == args[2]) {
         *reg |= TD0_color_arg1add_mulout;
         if (args[0] == MGA_ALPHA || args[2] == MGA_ALPHA)
            *reg |= TD0_color_arg1mul_alpha1;

         if (args[1] == MGA_ARG1) {
            /* Swap adder arguments */
            *reg ^= (TD0_color_arg1add_mulout |
                     TD0_color_arg2add_mulout);
            if (args[0] == MGA_ALPHA || args[2] == MGA_ALPHA) {
               /* Swap multiplier arguments */
               *reg ^= (TD0_color_arg1mul_alpha1 |
                        TD0_color_arg2mul_alpha2);
            }
         }
      } else {
         *reg |= (TD0_color_arg2mul_alpha2 |
                  TD0_color_arg1add_mulout);

         if (args[1] == MGA_ARG1) {
            /* Swap arguments */
            *reg ^= (TD0_color_arg1mul_alpha1 |
                     TD0_color_arg2mul_alpha2 |
                     TD0_color_arg1add_mulout |
                     TD0_color_arg2add_mulout);
         }
      }
      break;
   case GL_MODULATE_SUBTRACT_ATI:
      if (args[1] != MGA_ARG2) {
         /* Can't swap arguments */
         return GL_FALSE;
      }
      if (texUnit->Combine.ScaleShiftRGB == 1) {
         *reg |= TD0_color_add2x_enable;
      } else if (texUnit->Combine.ScaleShiftRGB == 2) {
         return GL_FALSE;
      }

      *reg |= (TD0_color_add_sub |
               TD0_color_sel_add);

      if (args[1] == args[0] || args[1] == args[2]) {
         *reg |= TD0_color_arg1add_mulout;
         if (args[0] == MGA_ALPHA || args[2] == MGA_ALPHA)
            *reg |= TD0_color_arg1mul_alpha1;
      } else {
         *reg |= (TD0_color_arg2mul_alpha2 |
                  TD0_color_arg1add_mulout);
      }
      break;
   }


   /* ALPHA */
   for (i = 0; i < 2; i++) {
      arg1[i] = 0;
      arg2[i] = 0;
   }

   for (i = 0; i < numAlphaArgs; i++) {
      switch (texUnit->Combine.SourceA[i]) {
      case GL_TEXTURE:
         arg1[i] |= 0;
         arg2[i] |= ARG_DISABLE;
         break;
      case GL_TEXTURE0:
         if (source == 0) {
            arg1[i] |= 0;
            arg2[i] |= ARG_DISABLE;
         } else {
            if (ctx->Texture._EnabledUnits != 0x03) {
               /* disable texturing */
               mmesa->setup.dwgctl &= DC_opcod_MASK;
               mmesa->setup.dwgctl |= DC_opcod_trap;
               mmesa->hw.alpha_sel = AC_alphasel_diffused;
               /* return GL_TRUE since we don't need a fallback */
               return GL_TRUE;
            }
            arg1[i] |= ARG_DISABLE;
            arg2[i] |= TD0_alpha_arg2_prevtex;
         }
         break;
      case GL_TEXTURE1:
         if (source == 0) {
            if (ctx->Texture._EnabledUnits != 0x03) {
               /* disable texturing */
               mmesa->setup.dwgctl &= DC_opcod_MASK;
               mmesa->setup.dwgctl |= DC_opcod_trap;
               mmesa->hw.alpha_sel = AC_alphasel_diffused;
               /* return GL_TRUE since we don't need a fallback */
               return GL_TRUE;
            }
            arg1[i] |= ARG_DISABLE;
            /* G400 specs (TDUALSTAGE0) */
            arg2[i] |= TD0_alpha_arg2_prevstage;
         } else {
            arg1[i] |= 0;
            arg2[i] |= ARG_DISABLE;
         }
         break;
      case GL_CONSTANT:
         if (mmesa->fcol_used &&
             mmesa->envcolor[source] != mmesa->envcolor[!source])
            return GL_FALSE;

         arg1[i] |= ARG_DISABLE;
         arg2[i] |= TD0_alpha_arg2_fcol;

         mmesa->setup.fcol = mmesa->envcolor[source];
         mmesa->fcol_used = GL_TRUE;
         break;
      case GL_PRIMARY_COLOR:
         arg1[i] |= ARG_DISABLE;
         /* G400 specs (TDUALSTAGE1) */
         if (unit == 0 || (mmesa->setup.tdualstage0 &
                           ((TD0_color_sel_mul & TD0_color_sel_add) |
                            (TD0_alpha_sel_mul & TD0_alpha_sel_add)))) {
            arg2[i] |= TD0_alpha_arg2_diffuse;
         } else {
            arg2[i] |= ARG_DISABLE;
         }
         break;
      case GL_PREVIOUS:
         arg1[i] |= ARG_DISABLE;
         if (unit == 0) {
            arg2[i] |= TD0_alpha_arg2_diffuse;
         } else {
            arg2[i] |= TD0_alpha_arg2_prevstage;
         }
         break;
      default:
         return GL_FALSE;
      }

      switch (texUnit->Combine.OperandA[i]) {
      case GL_SRC_ALPHA:
         arg1[i] |= 0;
         arg2[i] |= 0;
         break;
      case GL_ONE_MINUS_SRC_ALPHA:
         arg1[i] |= TD0_alpha_arg1_inv_enable;
         arg2[i] |= TD0_alpha_arg2_inv_enable;
         break;
      }
   }

   /* Find a working combo of arg1 and arg2 */
   if ((arg1[0] | arg2[1]) != ARG_DISABLE) {
      *reg |= arg1[0] | arg2[1];
      args[0] = MGA_ARG1; args[1] = MGA_ARG2;
   } else
   if ((arg1[1] | arg2[0]) != ARG_DISABLE) {
      *reg |= arg1[1] | arg2[0];
      args[0] = MGA_ARG2; args[1] = MGA_ARG1;
   } else {
      /* nothing suitable */
      return GL_FALSE;
   }

   switch (texUnit->Combine.ModeA) {
   case GL_REPLACE:
      if (texUnit->Combine.ScaleShiftA) {
         return GL_FALSE;
      }

      if (args[0] == MGA_ARG1) {
         *reg |= TD0_alpha_sel_arg1;
      } else if (args[0] == MGA_ARG2) {
         *reg |= TD0_alpha_sel_arg2;
      }
      break;
   case GL_MODULATE:
      if (texUnit->Combine.ScaleShiftA == 1) {
         *reg |= TD0_alpha_modbright_2x;
      } else if (texUnit->Combine.ScaleShiftA == 2) {
         *reg |= TD0_alpha_modbright_4x;
      }

      *reg |= TD0_alpha_sel_mul;
      break;
   case GL_ADD_SIGNED:
      *reg |= TD0_alpha_addbias_enable;
      /* fallthrough */
   case GL_ADD:
      if (texUnit->Combine.ScaleShiftA == 1) {
         *reg |= TD0_alpha_add2x_enable;
      } else if (texUnit->Combine.ScaleShiftA == 2) {
         return GL_FALSE;
      }

      *reg |= (TD0_alpha_add_enable |
               TD0_alpha_sel_add);
      break;
   case GL_SUBTRACT:
      if (texUnit->Combine.ScaleShiftA == 1) {
         *reg |= TD0_alpha_add2x_enable;
      } else if (texUnit->Combine.ScaleShiftA == 2) {
         return GL_FALSE;
      }

      *reg |= (TD0_alpha_add_disable |
               TD0_alpha_sel_add);

      if (args[0] == MGA_ARG2) {
         /* Swap arguments */
         *reg ^= (TD0_alpha_arg1_inv_enable |
                  TD0_alpha_arg2_inv_enable);
      }
      break;
   }

   return GL_TRUE;
}