summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/s3v/s3v_texstate.c
blob: b136c9d378cca045e0143b3350a5484ea7a4a51d (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
/*
 * Author: Max Lingua <sunmax@libero.it>
 */

#include <stdlib.h>
#include <stdio.h>

#include "glheader.h"
#include "macros.h"
#include "mtypes.h"
#include "simple_list.h"
#include "enums.h"

#include "mm.h"
#include "s3v_context.h"
#include "s3v_tex.h"


static void s3vSetTexImages( s3vContextPtr vmesa, 
			      struct gl_texture_object *tObj )
{
   GLuint height, width, pitch, i, /*textureFormat,*/ log_pitch;
   s3vTextureObjectPtr t = (s3vTextureObjectPtr) tObj->DriverData;
   const struct gl_texture_image *baseImage = tObj->Image[0][tObj->BaseLevel];
   GLint firstLevel, lastLevel, numLevels;
   GLint log2Width, log2Height;
#if TEX_DEBUG_ON
   static unsigned int times=0;
   DEBUG_TEX(("*** s3vSetTexImages: #%i ***\n", ++times));
#endif

   t->texelBytes = 2; /* FIXME: always 2 ? */

   /* Compute which mipmap levels we really want to send to the hardware.
    * This depends on the base image size, GL_TEXTURE_MIN_LOD,
    * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
    * Yes, this looks overly complicated, but it's all needed.
    */
   if (tObj->MinFilter == GL_LINEAR || tObj->MinFilter == GL_NEAREST) {
      firstLevel = lastLevel = tObj->BaseLevel;
   }
   else {
      firstLevel = tObj->BaseLevel + (GLint) (tObj->MinLod + 0.5);
      firstLevel = MAX2(firstLevel, tObj->BaseLevel);
      lastLevel = tObj->BaseLevel + (GLint) (tObj->MaxLod + 0.5);
	  lastLevel = MAX2(lastLevel, tObj->BaseLevel);
      lastLevel = MIN2(lastLevel, tObj->BaseLevel + baseImage->MaxLog2);
      lastLevel = MIN2(lastLevel, tObj->MaxLevel);
      lastLevel = MAX2(firstLevel, lastLevel); /* need at least one level */
   }

   /* save these values */
   t->firstLevel = firstLevel;
   t->lastLevel = lastLevel;

   numLevels = lastLevel - firstLevel + 1;

   log2Width = tObj->Image[0][firstLevel]->WidthLog2;
   log2Height = tObj->Image[0][firstLevel]->HeightLog2;


   /* Figure out the amount of memory required to hold all the mipmap
    * levels.  Choose the smallest pitch to accomodate the largest
    * mipmap:
    */
   width = tObj->Image[0][firstLevel]->Width * t->texelBytes;
   for (pitch = 32, log_pitch=2 ; pitch < width ; pitch *= 2 )
      log_pitch++;
   
   /* All images must be loaded at this pitch.  Count the number of
    * lines required:
    */
   for ( height = i = 0 ; i < numLevels ; i++ ) {
      t->image[i].image = tObj->Image[0][firstLevel + i];
      t->image[i].offset = height * pitch;
      t->image[i].internalFormat = baseImage->Format;
      height += t->image[i].image->Height;
      t->TextureBaseAddr[i] = (t->BufAddr + t->image[i].offset +
         _TEXALIGN) & (GLuint)(~_TEXALIGN);
   }

   t->Pitch = pitch;
   t->WidthLog2 = log2Width;
   t->totalSize = height*pitch;
   t->max_level = i-1;
   vmesa->dirty |= S3V_UPLOAD_TEX0 /* | S3V_UPLOAD_TEX1*/;   
   vmesa->restore_primitive = -1;
   DEBUG(("<><>pitch = TexStride = %i\n", pitch));
   DEBUG(("log2Width = %i\n", log2Width));

   s3vUploadTexImages( vmesa, t );
}

static void s3vUpdateTexEnv( GLcontext *ctx, GLuint unit )
{
   s3vContextPtr vmesa = S3V_CONTEXT(ctx);
   const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
   const struct gl_texture_object *tObj = texUnit->_Current;
   const GLuint format = tObj->Image[0][tObj->BaseLevel]->Format;
/*
   s3vTextureObjectPtr t = (s3vTextureObjectPtr)tObj->DriverData;
   GLuint tc;
*/
   GLuint alpha = 0;
   GLuint cmd = vmesa->CMD;
#if TEX_DEBUG_ON
   static unsigned int times=0;
   DEBUG_TEX(("*** s3vUpdateTexEnv: %i ***\n", ++times));
#endif

   cmd &= ~TEX_COL_MASK;
   cmd &= ~TEX_BLEND_MAKS;
/*   cmd &= ~ALPHA_BLEND_MASK; */

   DEBUG(("format = "));

   switch (format) {
   case GL_RGB:
      DEBUG_TEX(("GL_RGB\n"));
      cmd |= TEX_COL_ARGB1555;
      break;
   case GL_LUMINANCE:
      DEBUG_TEX(("GL_LUMINANCE\n"));
      cmd |= TEX_COL_ARGB4444;
      alpha = 1; /* FIXME: check */
      break;
   case GL_ALPHA:
      DEBUG_TEX(("GL_ALPHA\n"));
      cmd |= TEX_COL_ARGB4444;
      alpha = 1;
      break;
   case GL_LUMINANCE_ALPHA:
      DEBUG_TEX(("GL_LUMINANCE_ALPHA\n"));
      cmd |= TEX_COL_ARGB4444;
      alpha = 1;
      break;
   case GL_INTENSITY:
      DEBUG_TEX(("GL_INTENSITY\n"));
      cmd |= TEX_COL_ARGB4444;
      alpha = 1;
      break;
   case GL_RGBA:
      DEBUG_TEX(("GL_RGBA\n"));
      cmd |= TEX_COL_ARGB4444;
      alpha = 1;
      break;
   case GL_COLOR_INDEX:
      DEBUG_TEX(("GL_COLOR_INDEX\n"));
      cmd |= TEX_COL_PAL;
      break;
   }

   DEBUG_TEX(("EnvMode = "));

   switch (texUnit->EnvMode) {
   case GL_REPLACE:
      DEBUG_TEX(("GL_REPLACE\n"));
      cmd |= TEX_REFLECT; /* FIXME */
      vmesa->_tri[1] = DO_TEX_UNLIT_TRI; /* FIXME: white tri hack */
      vmesa->_alpha_tex = ALPHA_TEX /* * alpha */;
      break;
   case GL_MODULATE:
      DEBUG_TEX(("GL_MODULATE\n"));
      cmd |= TEX_MODULATE;
      vmesa->_tri[1] = DO_TEX_LIT_TRI;
#if 0
	if (alpha)
		vmesa->_alpha_tex = ALPHA_TEX /* * alpha */;
	else
		vmesa->_alpha_tex = ALPHA_SRC /* * alpha */;
#else
	vmesa->_alpha_tex = ALPHA_TEX ;
#endif
      break;
   case GL_ADD:
      DEBUG_TEX(("DEBUG_TEX\n"));
      /* do nothing ???*/
      break;
   case GL_DECAL:
      DEBUG_TEX(("GL_DECAL\n"));
      cmd |= TEX_DECAL;
      vmesa->_tri[1] = DO_TEX_LIT_TRI;
      vmesa->_alpha_tex = ALPHA_OFF;
      break;
   case GL_BLEND:
      DEBUG_TEX(("GL_BLEND\n"));
      cmd |= TEX_DECAL;
      vmesa->_tri[1] = DO_TEX_LIT_TRI;
      vmesa->_alpha_tex = ALPHA_OFF; /* FIXME: sure? */
      break;
   default:
      fprintf(stderr, "unknown tex env mode");
      return;
   }
  
   DEBUG_TEX(("\n\n    vmesa->CMD was 0x%x\n", vmesa->CMD));   
   DEBUG_TEX((	  "    vmesa->CMD is 0x%x\n\n", cmd ));

   vmesa->_alpha[1] = vmesa->_alpha_tex;
   vmesa->CMD = cmd; /* | MIPMAP_LEVEL(8); */
   vmesa->restore_primitive = -1; 
}

static void s3vUpdateTexUnit( GLcontext *ctx, GLuint unit )
{
   s3vContextPtr vmesa = S3V_CONTEXT(ctx);
   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
   GLuint cmd = vmesa->CMD;
#if TEX_DEBUG_ON
   static unsigned int times=0;
   DEBUG_TEX(("*** s3vUpdateTexUnit: %i ***\n", ++times));
   DEBUG_TEX(("and vmesa->CMD was 0x%x\n", vmesa->CMD));
#endif

   if (texUnit->_ReallyEnabled == TEXTURE_2D_BIT) 
   {
      struct gl_texture_object *tObj = texUnit->_Current;
      s3vTextureObjectPtr t = (s3vTextureObjectPtr)tObj->DriverData;

      /* Upload teximages (not pipelined)
       */
      if (t->dirty_images) {
#if _TEXFLUSH
         DMAFLUSH();
#endif
         s3vSetTexImages( vmesa, tObj ); 
         if (!t->MemBlock) {
#if _TEXFALLBACK
            FALLBACK( vmesa, S3V_FALLBACK_TEXTURE, GL_TRUE );
#endif
            return;
         }
      }

      /* Update state if this is a different texture object to last
       * time.
       */
#if 1
      if (vmesa->CurrentTexObj[unit] != t) {
         vmesa->dirty |= S3V_UPLOAD_TEX0 /* << unit */;
         vmesa->CurrentTexObj[unit] = t;
         s3vUpdateTexLRU( vmesa, t ); /* done too often */
      }
#endif
      
      /* Update texture environment if texture object image format or 
       * texture environment state has changed.
       */
      if (tObj->Image[0][tObj->BaseLevel]->Format !=
          vmesa->TexEnvImageFmt[unit]) {
         vmesa->TexEnvImageFmt[unit] = tObj->Image[0][tObj->BaseLevel]->Format;
         s3vUpdateTexEnv( ctx, unit );
      }
#if 1
      cmd = vmesa->CMD & ~MIP_MASK;
      vmesa->dirty |= S3V_UPLOAD_TEX0 /* << unit */;
      vmesa->CurrentTexObj[unit] = t;
      vmesa->TexOffset = t->TextureBaseAddr[tObj->BaseLevel];
      vmesa->TexStride = t->Pitch;
      cmd |= MIPMAP_LEVEL(t->WidthLog2);
	
      DEBUG_TEX(("\n\n>>  vmesa->CMD was 0x%x\n", vmesa->CMD));
      DEBUG_TEX((    ">>  vmesa->CMD is 0x%x\n\n", cmd ));
      DEBUG_TEX(("t->WidthLog2 = %i\n", t->WidthLog2));
      DEBUG_TEX(("MIPMAP_LEVEL(t->WidthLog2) = 0x%x\n", MIPMAP_LEVEL(t->WidthLog2)));

      vmesa->CMD = cmd;
      vmesa->restore_primitive = -1;
#endif
   }
   else if (texUnit->_ReallyEnabled) { /* _ReallyEnabled but != TEXTURE0_2D */
#if _TEXFALLBACK
      FALLBACK( vmesa, S3V_FALLBACK_TEXTURE, GL_TRUE );
#endif
   }
   else /*if (vmesa->CurrentTexObj[unit])*/ { /* !_ReallyEnabled */
      vmesa->CurrentTexObj[unit] = 0;
      vmesa->TexEnvImageFmt[unit] = 0;	
      vmesa->dirty &= ~(S3V_UPLOAD_TEX0<<unit); 
   }
}


void s3vUpdateTextureState( GLcontext *ctx )
{
   s3vContextPtr vmesa = S3V_CONTEXT(ctx);
   (void) vmesa;
#if TEX_DEBUG_ON
   static unsigned int times=0;
   DEBUG_TEX(("*** s3vUpdateTextureState: #%i ***\n", ++times));
#endif

#if _TEXFALLBACK   
   FALLBACK( vmesa, S3V_FALLBACK_TEXTURE, GL_FALSE );
#endif
   s3vUpdateTexUnit( ctx, 0 );
#if 0
   s3vUpdateTexUnit( ctx, 1 );
#endif
}