summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/unichrome/via_texcombine.c
blob: b64689784866323f142adff95f106468f299ceb0 (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
/*
 * (C) Copyright IBM Corporation 2004
 * 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
 * on 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
 * IBM AND/OR THEIR 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.
 */

/**
 * \file via_texcombine.c
 * Calculate texture combine hardware state.
 *
 * \author Ian Romanick <idr@us.ibm.com>
 */

#include <stdio.h>

#include "main/glheader.h"
#include "main/context.h"
#include "main/macros.h"
#include "main/colormac.h"
#include "main/enums.h"

#include "via_context.h"
#include "via_state.h"
#include "via_tex.h"
#include "via_3d_reg.h"


#define VIA_USE_ALPHA (HC_XTC_Adif - HC_XTC_Dif)

#define INPUT_A_SHIFT     14
#define INPUT_B_SHIFT     7
#define INPUT_C_SHIFT     0
#define INPUT_CBias_SHIFT 14

#define CONST_ONE         (HC_XTC_0 | HC_XTC_InvTOPC)

static const unsigned color_operand_modifier[4] = {
   0,
   HC_XTC_InvTOPC,
   VIA_USE_ALPHA,
   VIA_USE_ALPHA | HC_XTC_InvTOPC,
};

static const unsigned alpha_operand_modifier[2] = {
   0, HC_XTA_InvTOPA
};

static const unsigned bias_alpha_operand_modifier[2] = {
   0, HC_HTXnTBLAbias_Inv
};


static const unsigned c_shift_table[3] = {
   HC_HTXnTBLCshift_No, HC_HTXnTBLCshift_1, HC_HTXnTBLCshift_2
};

static const unsigned  a_shift_table[3] = {
   HC_HTXnTBLAshift_No, HC_HTXnTBLAshift_1, HC_HTXnTBLAshift_2
};


/**
 * Calculate the hardware state for the specified texture combine mode
 *
 * \bug
 * All forms of DOT3 bumpmapping are completely untested, and are most
 * likely wrong.  KW: Looks like it will never be quite right as the
 * hardware seems to experience overflow in color calculation at the
 * 4x shift levels, which need to be programed for DOT3.  Maybe newer
 * hardware fixes these issues.
 *
 * \bug 
 * KW: needs attention to the case where texunit 1 is enabled but
 * texunit 0 is not.
 */
GLboolean
viaTexCombineState( struct via_context *vmesa,
		    const struct gl_tex_env_combine_state * combine,
		    unsigned unit )
{
   unsigned color_arg[3];
   unsigned alpha_arg[3];
   unsigned bias_alpha_arg[3];
   unsigned color = HC_HTXnTBLCsat_MASK;
   unsigned alpha = HC_HTXnTBLAsat_MASK;
   unsigned bias = 0;
   unsigned op = 0;
   unsigned a_shift = combine->ScaleShiftA;
   unsigned c_shift = combine->ScaleShiftRGB;
   unsigned i;
   unsigned constant_color[3];
   unsigned ordered_constant_color[4];
   unsigned constant_alpha[3];
   unsigned bias_alpha = 0;
   unsigned abc_alpha = 0;
   const struct gl_texture_unit * texUnit = 
      &vmesa->glCtx->Texture.Unit[unit];
   unsigned env_color[4];

   /* It seems that the color clamping can be overwhelmed at the 4x
    * scale settings, necessitating this fallback:
    */
   if (c_shift == 2 || a_shift == 2) {
      return GL_FALSE;
   }

   CLAMPED_FLOAT_TO_UBYTE(env_color[0], texUnit->EnvColor[0]);
   CLAMPED_FLOAT_TO_UBYTE(env_color[1], texUnit->EnvColor[1]);
   CLAMPED_FLOAT_TO_UBYTE(env_color[2], texUnit->EnvColor[2]);
   CLAMPED_FLOAT_TO_UBYTE(env_color[3], texUnit->EnvColor[3]);

   (void) memset( constant_color, 0, sizeof( constant_color ) );
   (void) memset( ordered_constant_color, 0, sizeof( ordered_constant_color ) );
   (void) memset( constant_alpha, 0, sizeof( constant_alpha ) );

   for ( i = 0 ; i < combine->_NumArgsRGB ; i++ ) {
      const GLint op = combine->OperandRGB[i] - GL_SRC_COLOR;

      switch ( combine->SourceRGB[i] ) {
      case GL_TEXTURE:
	 color_arg[i] = HC_XTC_Tex;
	 color_arg[i] += color_operand_modifier[op];
	 break;
      case GL_CONSTANT:
	 color_arg[i] = HC_XTC_HTXnTBLRC;

	 switch( op ) {
	 case 0:		/* GL_SRC_COLOR */
	    constant_color[i] = ((env_color[0] << 16) | 
				 (env_color[1] << 8) | 
				 env_color[2]);
	    break;
	 case 1:		/* GL_ONE_MINUS_SRC_COLOR */
	    constant_color[i] = ~((env_color[0] << 16) | 
				  (env_color[1] << 8) | 
				  env_color[2]) & 0x00ffffff;
	    break;
	 case 2:		/* GL_SRC_ALPHA */
	    constant_color[i] = ((env_color[3] << 16) | 
				 (env_color[3] << 8) | 
				 env_color[3]);
	    break;
	 case 3:		/* GL_ONE_MINUS_SRC_ALPHA */
	    constant_color[i] = ~((env_color[3] << 16) | 
				  (env_color[3] << 8) | 
				  env_color[3]) & 0x00ffffff;
	    break;
	 }
	 break;
      case GL_PRIMARY_COLOR:
	 color_arg[i] = HC_XTC_Dif;
	 color_arg[i] += color_operand_modifier[op];
	 break;
      case GL_PREVIOUS:
	 color_arg[i] = (unit == 0) ? HC_XTC_Dif : HC_XTC_Cur;
	 color_arg[i] += color_operand_modifier[op];
	 break;
      }
   }
	
   
   /* On the Unichrome, all combine operations take on some form of:
    *
    *     (xA * (xB op xC) + xBias) << xShift
    * 
    * 'op' can be selected as add, subtract, min, max, or mask.  The min, max
    * and mask modes are currently unused.  With the exception of DOT3, all
    * standard GL_COMBINE modes can be implemented simply by selecting the
    * correct inputs for A, B, C, and Bias and the correct operation for op.
    *
    * NOTE: xBias (when read from the constant registers) is signed,
    * and scaled to fit -255..255 in 8 bits, ie 0x1 == 2.
    */

   switch( combine->ModeRGB ) {
   /* Ca = 1.0, Cb = arg0, Cc = 0, Cbias = 0
    */
   case GL_REPLACE:
      color |= ((CONST_ONE << INPUT_A_SHIFT) |
		(color_arg[0] << INPUT_B_SHIFT));
		
      ordered_constant_color[1] = constant_color[0];
      break;
      
   /* Ca = arg[0], Cb = arg[1], Cc = 0, Cbias = 0
    */
   case GL_MODULATE:
      color |= ((color_arg[0] << INPUT_A_SHIFT) | 
		(color_arg[1] << INPUT_B_SHIFT));

      ordered_constant_color[0] = constant_color[0];
      ordered_constant_color[1] = constant_color[1];
      break;

   /* Ca = 1.0, Cb = arg[0], Cc = arg[1], Cbias = 0
    */
   case GL_ADD:
   case GL_SUBTRACT:
      if ( combine->ModeRGB == GL_SUBTRACT ) {
	 op |= HC_HTXnTBLCop_Sub;
      }

      color |= ((CONST_ONE << INPUT_A_SHIFT) |
		(color_arg[0] << INPUT_B_SHIFT) |
		(color_arg[1] << INPUT_C_SHIFT));

      ordered_constant_color[1] = constant_color[0];
      ordered_constant_color[2] = constant_color[1];
      break;

   /* Ca = 1.0, Cb = arg[0], Cc = arg[1], Cbias = -0.5
    */
   case GL_ADD_SIGNED:
      color |= ((CONST_ONE << INPUT_A_SHIFT) |
		(color_arg[0] << INPUT_B_SHIFT) | 
		(color_arg[1] << INPUT_C_SHIFT));

      bias |= HC_HTXnTBLCbias_HTXnTBLRC;

      ordered_constant_color[1] = constant_color[0];
      ordered_constant_color[2] = constant_color[1];
      ordered_constant_color[3] = 0x00bfbfbf; /* -.5 */
      break;

   /* Ca = arg[2], Cb = arg[0], Cc = arg[1], Cbias = arg[1]
    */
   case GL_INTERPOLATE:
      op |= HC_HTXnTBLCop_Sub;

      color |= ((color_arg[2] << INPUT_A_SHIFT) |
		(color_arg[0] << INPUT_B_SHIFT) |
		(color_arg[1] << INPUT_C_SHIFT));

      bias |= (color_arg[1] << INPUT_CBias_SHIFT);

      ordered_constant_color[0] = constant_color[2];
      ordered_constant_color[1] = constant_color[0];
      ordered_constant_color[2] = constant_color[1];
      ordered_constant_color[3] = (constant_color[1] >> 1) & 0x7f7f7f;
      break;

#if 0
   /* At this point this code is completely untested.  It appears that the
    * Unichrome has the same limitation as the Radeon R100.  The only
    * supported post-scale when doing DOT3 bumpmapping is 1x.
    */
   case GL_DOT3_RGB_EXT:
   case GL_DOT3_RGBA_EXT:
   case GL_DOT3_RGB:
   case GL_DOT3_RGBA:
      c_shift = 2;
      a_shift = 2;
      color |= ((color_arg[0] << INPUT_A_SHIFT) |
		(color_arg[1] << INPUT_B_SHIFT));
      op |= HC_HTXnTBLDOT4;
      break;
#endif

   default:
      assert(0);
      break;
   }




   /* The alpha blend stage has the annoying quirk of not having a
    * hard-wired 0 input, like the color stage.  As a result, we have
    * to program the constant register with 0 and use that as our
    * 0 input.
    *
    *     (xA * (xB op xC) + xBias) << xShift
    *
    */

   for ( i = 0 ; i < combine->_NumArgsA ; i++ ) {
      const GLint op = combine->OperandA[i] - GL_SRC_ALPHA;

      switch ( combine->SourceA[i] ) {
      case GL_TEXTURE:
	 alpha_arg[i] = HC_XTA_Atex;
	 alpha_arg[i] += alpha_operand_modifier[op];
	 bias_alpha_arg[i] = HC_HTXnTBLAbias_Atex;
	 bias_alpha_arg[i] += bias_alpha_operand_modifier[op];
	 break;
      case GL_CONSTANT:
	 alpha_arg[i] = HC_XTA_HTXnTBLRA;
	 bias_alpha_arg[i] = HC_HTXnTBLAbias_HTXnTBLRAbias;
	 constant_alpha[i] = (op == 0) ? env_color[3] : (~env_color[3] & 0xff);
	 break;
      case GL_PRIMARY_COLOR:
	 alpha_arg[i] = HC_XTA_Adif;
	 alpha_arg[i] += alpha_operand_modifier[op];
	 bias_alpha_arg[i] = HC_HTXnTBLAbias_Adif;
	 bias_alpha_arg[i] += bias_alpha_operand_modifier[op];
	 break;
      case GL_PREVIOUS:
	 alpha_arg[i] = (unit == 0) ? HC_XTA_Adif : HC_XTA_Acur;
	 alpha_arg[i] += alpha_operand_modifier[op];
	 bias_alpha_arg[i] = (unit == 0 ? 
			      HC_HTXnTBLAbias_Adif : 
			      HC_HTXnTBLAbias_Acur);
	 bias_alpha_arg[i] += bias_alpha_operand_modifier[op];
	 break;
      }
   }

   switch( combine->ModeA ) {
   /* Aa = 0, Ab = 0, Ac = 0, Abias = arg0
    */
   case GL_REPLACE:
      alpha |= ((HC_XTA_HTXnTBLRA << INPUT_A_SHIFT) |
		(HC_XTA_HTXnTBLRA << INPUT_B_SHIFT) |
		(HC_XTA_HTXnTBLRA << INPUT_C_SHIFT));
      abc_alpha = 0;

      bias |= bias_alpha_arg[0];
      bias_alpha = constant_alpha[0] >> 1;
      break;
      
   /* Aa = arg[0], Ab = arg[1], Ac = 0, Abias = 0
    */
   case GL_MODULATE:
      alpha |= ((alpha_arg[1] << INPUT_A_SHIFT) | 
		(alpha_arg[0] << INPUT_B_SHIFT) | 
		(HC_XTA_HTXnTBLRA << INPUT_C_SHIFT));

      abc_alpha = ((constant_alpha[1] << HC_HTXnTBLRAa_SHIFT) |
		   (constant_alpha[0] << HC_HTXnTBLRAb_SHIFT) |
		   (0 << HC_HTXnTBLRAc_SHIFT));

      bias |= HC_HTXnTBLAbias_HTXnTBLRAbias;
      bias_alpha = 0;
      break;

   /* Aa = 1.0, Ab = arg[0], Ac = arg[1], Abias = 0
    */
   case GL_ADD:
   case GL_SUBTRACT:
      if ( combine->ModeA == GL_SUBTRACT ) {
	 op |= HC_HTXnTBLAop_Sub;
      }

      alpha |= ((HC_XTA_HTXnTBLRA << INPUT_A_SHIFT) |
		(alpha_arg[0] << INPUT_B_SHIFT) |
		(alpha_arg[1] << INPUT_C_SHIFT));

      abc_alpha = ((0xff << HC_HTXnTBLRAa_SHIFT) |
		   (constant_alpha[0] << HC_HTXnTBLRAb_SHIFT) |
		   (constant_alpha[1] << HC_HTXnTBLRAc_SHIFT));

      bias |= HC_HTXnTBLAbias_HTXnTBLRAbias;
      bias_alpha = 0;
      break;

   /* Aa = 1.0, Ab = arg[0], Ac = arg[1], Abias = -0.5
    */
   case GL_ADD_SIGNED:
      alpha |= ((HC_XTA_HTXnTBLRA << INPUT_A_SHIFT) |
		(alpha_arg[0] << INPUT_B_SHIFT) | 
		(alpha_arg[1] << INPUT_C_SHIFT));
      abc_alpha = ((0xff << HC_HTXnTBLRAa_SHIFT) |
		   (constant_alpha[0] << HC_HTXnTBLRAb_SHIFT) |
		   (constant_alpha[1] << HC_HTXnTBLRAc_SHIFT));

      bias |= HC_HTXnTBLAbias_HTXnTBLRAbias;
      bias_alpha = 0xbf;
      break;

   /* Aa = arg[2], Ab = arg[0], Ac = arg[1], Abias = arg[1]
    */
   case GL_INTERPOLATE:
      op |= HC_HTXnTBLAop_Sub;

      alpha |= ((alpha_arg[2] << INPUT_A_SHIFT) |
		(alpha_arg[0] << INPUT_B_SHIFT) |
		(alpha_arg[1] << INPUT_C_SHIFT));
      abc_alpha = ((constant_alpha[2] << HC_HTXnTBLRAa_SHIFT) |
		   (constant_alpha[0] << HC_HTXnTBLRAb_SHIFT) |
		   (constant_alpha[1] << HC_HTXnTBLRAc_SHIFT));

      bias |= bias_alpha_arg[1];
      bias_alpha = constant_alpha[1] >> 1;
      break;
   }
   

   op |= c_shift_table[ c_shift ] | a_shift_table[ a_shift ];


   vmesa->regHTXnTBLMPfog[unit] = HC_HTXnTBLMPfog_Fog;

   vmesa->regHTXnTBLCsat[unit] = color;
   vmesa->regHTXnTBLAsat[unit] = alpha;
   vmesa->regHTXnTBLCop[unit] = op | bias;
   vmesa->regHTXnTBLRAa[unit] = abc_alpha;
   vmesa->regHTXnTBLRFog[unit] = bias_alpha;

   vmesa->regHTXnTBLRCa[unit] = ordered_constant_color[0];
   vmesa->regHTXnTBLRCb[unit] = ordered_constant_color[1];
   vmesa->regHTXnTBLRCc[unit] = ordered_constant_color[2];
   vmesa->regHTXnTBLRCbias[unit] = ordered_constant_color[3];

   return GL_TRUE;
}