summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/gamma/gamma_tex.c
blob: 97797d47882d8fef532b2528e6025b6bb844c1dc (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

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

#include "main/glheader.h"
#include "main/mtypes.h"
#include "main/imports.h"
#include "main/simple_list.h"
#include "main/enums.h"
#include "main/texstore.h"
#include "teximage.h"
#include "main/texformat.h"
#include "texobj.h"
#include "swrast/swrast.h"

#include "main/mm.h"
#include "gammacontext.h"
#include "colormac.h"


/*
 * Compute the 'S2.4' lod bias factor from the floating point OpenGL bias.
 */
#if 0
static GLuint gammaComputeLodBias(GLfloat bias)
{
   return bias;
}
#endif

static void gammaSetTexWrapping(gammaTextureObjectPtr t, 
			       GLenum wraps, GLenum wrapt)
{
   uint32_t t1 = t->TextureAddressMode;
   uint32_t t2 = t->TextureReadMode;

   t1 &= ~(TAM_SWrap_Mask | TAM_TWrap_Mask);
   t2 &= ~(TRM_UWrap_Mask | TRM_VWrap_Mask);
   
   if (wraps != GL_CLAMP) {
      t1 |= TAM_SWrap_Repeat;
      t2 |= TRM_UWrap_Repeat;
   }

   if (wrapt != GL_CLAMP) {
      t1 |= TAM_TWrap_Repeat;
      t2 |= TRM_VWrap_Repeat;
   }

   t->TextureAddressMode = t1;
   t->TextureReadMode = t2;
}


static void gammaSetTexFilter(gammaContextPtr gmesa, 
			     gammaTextureObjectPtr t, 
			     GLenum minf, GLenum magf,
                             GLfloat bias)
{
   uint32_t t1 = t->TextureAddressMode;
   uint32_t t2 = t->TextureReadMode;

   t2 &= ~(TRM_Mag_Mask | TRM_Min_Mask);

   switch (minf) {
   case GL_NEAREST:
      t1 &= ~TAM_LODEnable;
      t2 &= ~TRM_MipMapEnable;
      t2 |= TRM_Min_Nearest;
      break;
   case GL_LINEAR:
      t1 &= ~TAM_LODEnable;
      t2 &= ~TRM_MipMapEnable;
      t2 |= TRM_Min_Linear;
      break;
   case GL_NEAREST_MIPMAP_NEAREST:
      t2 |= TRM_Min_NearestMMNearest;
      break;
   case GL_LINEAR_MIPMAP_NEAREST:
      t2 |= TRM_Min_LinearMMNearest;
      break;
   case GL_NEAREST_MIPMAP_LINEAR:
      t2 |= TRM_Min_NearestMMLinear;
      break;
   case GL_LINEAR_MIPMAP_LINEAR:
      t2 |= TRM_Min_LinearMMLinear;
      break;
   default:
      break;
   }

   switch (magf) {
   case GL_NEAREST:
      t2 |= TRM_Mag_Nearest;
      break;
   case GL_LINEAR:
      t2 |= TRM_Mag_Linear;
      break;
   default:
      break;
   }  

   t->TextureAddressMode = t1;
   t->TextureReadMode = t2;
}


static void gammaSetTexBorderColor(gammaContextPtr gmesa,
				  gammaTextureObjectPtr t, 
                                  const GLfloat color[4])
{
   GLubyte c[4];
   CLAMPED_FLOAT_TO_UBYTE(c[0], color[0]);
   CLAMPED_FLOAT_TO_UBYTE(c[1], color[1]);
   CLAMPED_FLOAT_TO_UBYTE(c[2], color[2]);
   CLAMPED_FLOAT_TO_UBYTE(c[3], color[3]);
   t->TextureBorderColor = PACK_COLOR_8888(c[0], c[1], c[2], c[3]);
}


static void gammaTexParameter( GLcontext *ctx, GLenum target,
			      struct gl_texture_object *tObj,
			      GLenum pname, const GLfloat *params )
{
   gammaContextPtr gmesa = GAMMA_CONTEXT(ctx);
   gammaTextureObjectPtr t = (gammaTextureObjectPtr) tObj->DriverData;
   if (!t)
      return;

   /* Can't do the update now as we don't know whether to flush
    * vertices or not.  Setting gmesa->new_state means that
    * gammaUpdateTextureState() will be called before any triangles are
    * rendered.  If a statechange has occurred, it will be detected at
    * that point, and buffered vertices flushed.  
    */
   switch (pname) {
   case GL_TEXTURE_MIN_FILTER:
   case GL_TEXTURE_MAG_FILTER:
      {
         GLfloat bias = ctx->Texture.Unit[ctx->Texture.CurrentUnit].LodBias;
         gammaSetTexFilter( gmesa, t, tObj->MinFilter, tObj->MagFilter, bias );
      }
      break;

   case GL_TEXTURE_WRAP_S:
   case GL_TEXTURE_WRAP_T:
      gammaSetTexWrapping( t, tObj->WrapS, tObj->WrapT );
      break;
  
   case GL_TEXTURE_BORDER_COLOR:
      gammaSetTexBorderColor( gmesa, t, tObj->BorderColor );
      break;

   case GL_TEXTURE_BASE_LEVEL:
   case GL_TEXTURE_MAX_LEVEL:
   case GL_TEXTURE_MIN_LOD:
   case GL_TEXTURE_MAX_LOD:
      /* This isn't the most efficient solution but there doesn't appear to
       * be a nice alternative for Radeon.  Since there's no LOD clamping,
       * we just have to rely on loading the right subset of mipmap levels
       * to simulate a clamped LOD.
       */
      gammaSwapOutTexObj( gmesa, t );
      break;

   default:
      return;
   }

   if (t == gmesa->CurrentTexObj[0])
      gmesa->dirty |= GAMMA_UPLOAD_TEX0;

#if 0
   if (t == gmesa->CurrentTexObj[1]) {
      gmesa->dirty |= GAMMA_UPLOAD_TEX1;
   }
#endif
}


static void gammaTexEnv( GLcontext *ctx, GLenum target, 
			GLenum pname, const GLfloat *param )
{
   gammaContextPtr gmesa = GAMMA_CONTEXT( ctx );
   GLuint unit = ctx->Texture.CurrentUnit;

   /* Only one env color.  Need a fallback if env colors are different
    * and texture setup references env color in both units.  
    */
   switch (pname) {
   case GL_TEXTURE_ENV_COLOR: {
      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
      GLfloat *fc = texUnit->EnvColor;
      GLuint r, g, b, a, col;
      CLAMPED_FLOAT_TO_UBYTE(r, fc[0]);
      CLAMPED_FLOAT_TO_UBYTE(g, fc[1]);
      CLAMPED_FLOAT_TO_UBYTE(b, fc[2]);
      CLAMPED_FLOAT_TO_UBYTE(a, fc[3]);

      col = ((a << 24) | 
	     (r << 16) | 
	     (g <<  8) | 
	     (b <<  0));

      break;
   }
   case GL_TEXTURE_ENV_MODE:
      gmesa->TexEnvImageFmt[unit] = 0; /* force recalc of env state */
      break;

   case GL_TEXTURE_LOD_BIAS_EXT:
#if 0  /* ?!?!?! */
      {
         struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
         gammaTextureObjectPtr t = (gammaTextureObjectPtr) tObj->DriverData;
         (void) t;
	 /* XXX Looks like there's something missing here */
      }
#endif
      break;

   default:
      break;
   }
} 

#if 0
static void gammaTexImage1D( GLcontext *ctx, GLenum target, GLint level,
			    GLint internalFormat,
			    GLint width, GLint border,
			    GLenum format, GLenum type, 
			    const GLvoid *pixels,
			    const struct gl_pixelstore_attrib *pack,
			    struct gl_texture_object *texObj,
			    struct gl_texture_image *texImage )
{
   gammaTextureObjectPtr t = (gammaTextureObjectPtr) texObj->DriverData;
   if (t) {
      gammaSwapOutTexObj( GAMMA_CONTEXT(ctx), t );
   }
   _mesa_store_teximage1d( ctx, target, level, internalFormat,
			   width, border, format, type,
			   pixels, pack, texObj, texImage );
}
#endif

#if 0
static void gammaTexSubImage1D( GLcontext *ctx, 
			       GLenum target,
			       GLint level,	
			       GLint xoffset,
			       GLsizei width,
			       GLenum format, GLenum type,
			       const GLvoid *pixels,
			       const struct gl_pixelstore_attrib *pack,
			       struct gl_texture_object *texObj,
			       struct gl_texture_image *texImage )
{
   gammaTextureObjectPtr t = (gammaTextureObjectPtr) texObj->DriverData;
   if (t) {
      gammaSwapOutTexObj( GAMMA_CONTEXT(ctx), t );
   }
   _mesa_store_texsubimage1d(ctx, target, level, xoffset, width, 
			     format, type, pixels, pack, texObj,
			     texImage);
}
#endif

static void gammaTexImage2D( GLcontext *ctx, GLenum target, GLint level,
			    GLint internalFormat,
			    GLint width, GLint height, GLint border,
			    GLenum format, GLenum type, const GLvoid *pixels,
			    const struct gl_pixelstore_attrib *packing,
			    struct gl_texture_object *texObj,
			    struct gl_texture_image *texImage )
{
   gammaTextureObjectPtr t = (gammaTextureObjectPtr) texObj->DriverData;
   if (t) {
      gammaSwapOutTexObj( GAMMA_CONTEXT(ctx), t );
   }
   _mesa_store_teximage2d( ctx, target, level, internalFormat,
			   width, height, border, format, type,
			   pixels, packing, texObj, texImage );
}

static void gammaTexSubImage2D( GLcontext *ctx, 
			       GLenum target,
			       GLint level,	
			       GLint xoffset, GLint yoffset,
			       GLsizei width, GLsizei height,
			       GLenum format, GLenum type,
			       const GLvoid *pixels,
			       const struct gl_pixelstore_attrib *packing,
			       struct gl_texture_object *texObj,
			       struct gl_texture_image *texImage )
{
   gammaTextureObjectPtr t = (gammaTextureObjectPtr) texObj->DriverData;
   if (t) {
      gammaSwapOutTexObj( GAMMA_CONTEXT(ctx), t );
   }
   _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset, width, 
			     height, format, type, pixels, packing, texObj,
			     texImage);
}

static void gammaBindTexture( GLcontext *ctx, GLenum target,
			     struct gl_texture_object *tObj )
{
      gammaContextPtr gmesa = GAMMA_CONTEXT( ctx );
      gammaTextureObjectPtr t = (gammaTextureObjectPtr) tObj->DriverData;

      if (!t) {
         GLfloat bias = ctx->Texture.Unit[ctx->Texture.CurrentUnit].LodBias;
	 t = CALLOC_STRUCT(gamma_texture_object_t);

	 /* Initialize non-image-dependent parts of the state:
	  */
	 t->globj = tObj;

   	 t->TextureAddressMode = TextureAddressModeEnable | TAM_Operation_3D |
			   TAM_DY_Enable | TAM_LODEnable;
         t->TextureReadMode = TextureReadModeEnable | TRM_PrimaryCacheEnable |
			TRM_MipMapEnable | TRM_BorderClamp | TRM_Border;
         t->TextureColorMode = TextureColorModeEnable;
         t->TextureFilterMode = TextureFilterModeEnable;

         if (target == GL_TEXTURE_2D) {
            t->TextureAddressMode |= TAM_TexMapType_2D;
            t->TextureReadMode |= TRM_TexMapType_2D;
         }
         else if (target == GL_TEXTURE_1D) {
            t->TextureAddressMode |= TAM_TexMapType_1D;
            t->TextureReadMode |= TRM_TexMapType_1D;
         }

         t->TextureColorMode = TextureColorModeEnable;

         t->TextureFilterMode = TextureFilterModeEnable;

#ifdef MESA_LITTLE_ENDIAN
         t->TextureFormat = (TF_LittleEndian |
#else
         t->TextureFormat = (TF_BigEndian |
#endif
			TF_ColorOrder_RGB |
			TF_OutputFmt_Texel);

	 t->dirty_images = ~0;

	 tObj->DriverData = t;
	 make_empty_list( t );

	 gammaSetTexWrapping( t, tObj->WrapS, tObj->WrapT );
	 gammaSetTexFilter( gmesa, t, tObj->MinFilter, tObj->MagFilter, bias );
	 gammaSetTexBorderColor( gmesa, t, tObj->BorderColor );
      }
}

static void gammaDeleteTexture( GLcontext *ctx, struct gl_texture_object *tObj )
{
   gammaTextureObjectPtr t = (gammaTextureObjectPtr)tObj->DriverData;

   if (t) {
      gammaContextPtr gmesa = GAMMA_CONTEXT( ctx );
#if 0
      if (gmesa)
         GAMMA_FIREVERTICES( gmesa );
#endif
      gammaDestroyTexObj( gmesa, t );
      tObj->DriverData = 0;
   }
   /* Free mipmap images and the texture object itself */
   _mesa_delete_texture_object(ctx, tObj);
}

static GLboolean gammaIsTextureResident( GLcontext *ctx, 
					struct gl_texture_object *tObj )
{
   gammaTextureObjectPtr t = (gammaTextureObjectPtr)tObj->DriverData;
   return t && t->MemBlock;
}

#ifdef UNUSED
/**
 * Allocate a new texture object.
 * Called via ctx->Driver.NewTextureObject.
 * Note: this function will be called during context creation to
 * allocate the default texture objects.
 * Note: we could use containment here to 'derive' the driver-specific
 * texture object from the core mesa gl_texture_object.  Not done at this time.
 */
static struct gl_texture_object *
gammaNewTextureObject( GLcontext *ctx, GLuint name, GLenum target )
{
   struct gl_texture_object *obj;
   obj = _mesa_new_texture_object(ctx, name, target);
   return obj;
}
#endif

void gammaInitTextureObjects( GLcontext *ctx )
{
   struct gl_texture_object *texObj;
   GLuint tmp = ctx->Texture.CurrentUnit;

   ctx->Texture.CurrentUnit = 0;

   texObj = ctx->Texture.Unit[0].CurrentTex[TEXTURE_1D_INDEX];
   gammaBindTexture( ctx, GL_TEXTURE_1D, texObj );

   texObj = ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];
   gammaBindTexture( ctx, GL_TEXTURE_2D, texObj );

#if 0
   ctx->Texture.CurrentUnit = 1;

   texObj = ctx->Texture.Unit[1].CurrentTex[TEXTURE_1D_INDEX];
   gammaBindTexture( ctx, GL_TEXTURE_1D, texObj );

   texObj = ctx->Texture.Unit[1].CurrentTex[TEXTURE_2D_INDEX];
   gammaBindTexture( ctx, GL_TEXTURE_2D, texObj );
#endif

   ctx->Texture.CurrentUnit = tmp;
}


void gammaDDInitTextureFuncs( struct dd_function_table *functions )
{
   functions->TexEnv = gammaTexEnv;
   functions->TexImage2D = gammaTexImage2D;
   functions->TexSubImage2D = gammaTexSubImage2D;
   functions->BindTexture = gammaBindTexture;
   functions->DeleteTexture = gammaDeleteTexture;
   functions->TexParameter = gammaTexParameter;
   functions->IsTextureResident = gammaIsTextureResident;
}