Small optimization for big-endian (e.g., PowerPC) systems.
[mesa.git] / src / mesa / drivers / dri / i810 / i810tex.c
1 /*
2 * GLX Hardware Device Driver for Intel i810
3 * Copyright (C) 1999 Keith Whitwell
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24 /* $XFree86: xc/lib/GL/mesa/src/drv/i810/i810tex.c,v 1.9 2002/10/30 12:51:33 alanh Exp $ */
25
26 #include "glheader.h"
27 #include "mtypes.h"
28 #include "imports.h"
29 #include "simple_list.h"
30 #include "enums.h"
31 #include "texstore.h"
32 #include "texformat.h"
33 #include "teximage.h"
34 #include "texmem.h"
35 #include "texobj.h"
36 #include "swrast/swrast.h"
37 #include "colormac.h"
38 #include "texobj.h"
39 #include "mm.h"
40
41 #include "i810screen.h"
42 #include "i810_dri.h"
43
44 #include "i810context.h"
45 #include "i810tex.h"
46 #include "i810state.h"
47 #include "i810ioctl.h"
48
49
50 /*
51 * Compute the 'S2.4' lod bias factor from the floating point OpenGL bias.
52 */
53 static GLuint i810ComputeLodBias(GLfloat bias)
54 {
55 int b = (int) (bias * 16.0) + 12;
56 if (b > 63)
57 b = 63;
58 else if (b < -64)
59 b = -64;
60 return (GLuint) (b & MLC_LOD_BIAS_MASK);
61 }
62
63
64 static void i810SetTexWrapping(i810TextureObjectPtr tex,
65 GLenum swrap, GLenum twrap)
66 {
67 tex->Setup[I810_TEXREG_MCS] &= ~(MCS_U_STATE_MASK| MCS_V_STATE_MASK);
68
69 switch( swrap ) {
70 case GL_REPEAT:
71 tex->Setup[I810_TEXREG_MCS] |= MCS_U_WRAP;
72 break;
73 case GL_CLAMP:
74 case GL_CLAMP_TO_EDGE:
75 tex->Setup[I810_TEXREG_MCS] |= MCS_U_CLAMP;
76 break;
77 case GL_MIRRORED_REPEAT:
78 tex->Setup[I810_TEXREG_MCS] |= MCS_U_MIRROR;
79 break;
80 default:
81 _mesa_problem(NULL, "bad S wrap mode in %s", __FUNCTION__);
82 }
83
84 switch( twrap ) {
85 case GL_REPEAT:
86 tex->Setup[I810_TEXREG_MCS] |= MCS_V_WRAP;
87 break;
88 case GL_CLAMP:
89 case GL_CLAMP_TO_EDGE:
90 tex->Setup[I810_TEXREG_MCS] |= MCS_V_CLAMP;
91 break;
92 case GL_MIRRORED_REPEAT:
93 tex->Setup[I810_TEXREG_MCS] |= MCS_V_MIRROR;
94 break;
95 default:
96 _mesa_problem(NULL, "bad T wrap mode in %s", __FUNCTION__);
97 }
98 }
99
100
101 static void i810SetTexFilter(i810ContextPtr imesa,
102 i810TextureObjectPtr t,
103 GLenum minf, GLenum magf,
104 GLfloat bias)
105 {
106 t->Setup[I810_TEXREG_MF] &= ~(MF_MIN_MASK|
107 MF_MAG_MASK|
108 MF_MIP_MASK);
109 t->Setup[I810_TEXREG_MLC] &= ~(MLC_LOD_BIAS_MASK);
110
111 switch (minf) {
112 case GL_NEAREST:
113 t->Setup[I810_TEXREG_MF] |= MF_MIN_NEAREST | MF_MIP_NONE;
114 break;
115 case GL_LINEAR:
116 t->Setup[I810_TEXREG_MF] |= MF_MIN_LINEAR | MF_MIP_NONE;
117 break;
118 case GL_NEAREST_MIPMAP_NEAREST:
119 t->Setup[I810_TEXREG_MF] |= MF_MIN_NEAREST | MF_MIP_NEAREST;
120 if (magf == GL_LINEAR) {
121 /*bias -= 0.5;*/ /* this doesn't work too good */
122 }
123 break;
124 case GL_LINEAR_MIPMAP_NEAREST:
125 t->Setup[I810_TEXREG_MF] |= MF_MIN_LINEAR | MF_MIP_NEAREST;
126 break;
127 case GL_NEAREST_MIPMAP_LINEAR:
128 if (IS_I815(imesa))
129 t->Setup[I810_TEXREG_MF] |= MF_MIN_NEAREST | MF_MIP_LINEAR;
130 else
131 t->Setup[I810_TEXREG_MF] |= MF_MIN_NEAREST | MF_MIP_DITHER;
132 /*
133 if (magf == GL_LINEAR) {
134 bias -= 0.5;
135 }
136 */
137 bias -= 0.5; /* always biasing here looks better */
138 break;
139 case GL_LINEAR_MIPMAP_LINEAR:
140 if (IS_I815(imesa))
141 t->Setup[I810_TEXREG_MF] |= MF_MIN_LINEAR | MF_MIP_LINEAR;
142 else
143 t->Setup[I810_TEXREG_MF] |= MF_MIN_LINEAR | MF_MIP_DITHER;
144 break;
145 default:
146 return;
147 }
148
149 switch (magf) {
150 case GL_NEAREST:
151 t->Setup[I810_TEXREG_MF] |= MF_MAG_NEAREST;
152 break;
153 case GL_LINEAR:
154 t->Setup[I810_TEXREG_MF] |= MF_MAG_LINEAR;
155 break;
156 default:
157 return;
158 }
159
160 t->Setup[I810_TEXREG_MLC] |= i810ComputeLodBias(bias);
161 }
162
163
164 static void
165 i810SetTexBorderColor( i810TextureObjectPtr t, GLubyte color[4] )
166 {
167 /* Need a fallback.
168 */
169 }
170
171
172 static i810TextureObjectPtr
173 i810AllocTexObj( GLcontext *ctx, struct gl_texture_object *texObj )
174 {
175 i810TextureObjectPtr t;
176 i810ContextPtr imesa = I810_CONTEXT(ctx);
177
178 t = CALLOC_STRUCT( i810_texture_object_t );
179 texObj->DriverData = t;
180 if ( t != NULL ) {
181 GLfloat bias = ctx->Texture.Unit[ctx->Texture.CurrentUnit].LodBias;
182 /* Initialize non-image-dependent parts of the state:
183 */
184 t->base.tObj = texObj;
185 t->Setup[I810_TEXREG_MI0] = GFX_OP_MAP_INFO;
186 t->Setup[I810_TEXREG_MI1] = MI1_MAP_0;
187 t->Setup[I810_TEXREG_MI2] = MI2_DIMENSIONS_ARE_LOG2;
188 t->Setup[I810_TEXREG_MLC] = (GFX_OP_MAP_LOD_CTL |
189 MLC_MAP_0 |
190 /*MLC_DITHER_WEIGHT_FULL |*/
191 MLC_DITHER_WEIGHT_12 |
192 MLC_UPDATE_LOD_BIAS |
193 0x0);
194 t->Setup[I810_TEXREG_MCS] = (GFX_OP_MAP_COORD_SETS |
195 MCS_COORD_0 |
196 MCS_UPDATE_NORMALIZED |
197 MCS_NORMALIZED_COORDS |
198 MCS_UPDATE_V_STATE |
199 MCS_V_WRAP |
200 MCS_UPDATE_U_STATE |
201 MCS_U_WRAP);
202 t->Setup[I810_TEXREG_MF] = (GFX_OP_MAP_FILTER |
203 MF_MAP_0 |
204 MF_UPDATE_ANISOTROPIC |
205 MF_UPDATE_MIP_FILTER |
206 MF_UPDATE_MAG_FILTER |
207 MF_UPDATE_MIN_FILTER);
208
209 make_empty_list( & t->base );
210
211 i810SetTexWrapping( t, texObj->WrapS, texObj->WrapT );
212 /*i830SetTexMaxAnisotropy( t, texObj->MaxAnisotropy );*/
213 i810SetTexFilter( imesa, t, texObj->MinFilter, texObj->MagFilter, bias );
214 i810SetTexBorderColor( t, texObj->_BorderChan );
215 }
216
217 return t;
218 }
219
220
221 static void i810TexParameter( GLcontext *ctx, GLenum target,
222 struct gl_texture_object *tObj,
223 GLenum pname, const GLfloat *params )
224 {
225 i810ContextPtr imesa = I810_CONTEXT(ctx);
226 i810TextureObjectPtr t = (i810TextureObjectPtr) tObj->DriverData;
227
228 if (!t)
229 return;
230
231 if ( target != GL_TEXTURE_2D )
232 return;
233
234 /* Can't do the update now as we don't know whether to flush
235 * vertices or not. Setting imesa->new_state means that
236 * i810UpdateTextureState() will be called before any triangles are
237 * rendered. If a statechange has occurred, it will be detected at
238 * that point, and buffered vertices flushed.
239 */
240 switch (pname) {
241 case GL_TEXTURE_MIN_FILTER:
242 case GL_TEXTURE_MAG_FILTER:
243 {
244 GLfloat bias = ctx->Texture.Unit[ctx->Texture.CurrentUnit].LodBias;
245 i810SetTexFilter( imesa, t, tObj->MinFilter, tObj->MagFilter, bias );
246 }
247 break;
248
249 case GL_TEXTURE_WRAP_S:
250 case GL_TEXTURE_WRAP_T:
251 i810SetTexWrapping( t, tObj->WrapS, tObj->WrapT );
252 break;
253
254 case GL_TEXTURE_BORDER_COLOR:
255 i810SetTexBorderColor( t, tObj->_BorderChan );
256 break;
257
258 case GL_TEXTURE_BASE_LEVEL:
259 case GL_TEXTURE_MAX_LEVEL:
260 case GL_TEXTURE_MIN_LOD:
261 case GL_TEXTURE_MAX_LOD:
262 /* This isn't the most efficient solution but there doesn't appear to
263 * be a nice alternative for Radeon. Since there's no LOD clamping,
264 * we just have to rely on loading the right subset of mipmap levels
265 * to simulate a clamped LOD.
266 */
267 I810_FIREVERTICES( I810_CONTEXT(ctx) );
268 driSwapOutTextureObject( (driTextureObject *) t );
269 break;
270
271 default:
272 return;
273 }
274
275 if (t == imesa->CurrentTexObj[0]) {
276 I810_STATECHANGE( imesa, I810_UPLOAD_TEX0 );
277 }
278
279 if (t == imesa->CurrentTexObj[1]) {
280 I810_STATECHANGE( imesa, I810_UPLOAD_TEX1 );
281 }
282 }
283
284
285 static void i810TexEnv( GLcontext *ctx, GLenum target,
286 GLenum pname, const GLfloat *param )
287 {
288 i810ContextPtr imesa = I810_CONTEXT( ctx );
289 GLuint unit = ctx->Texture.CurrentUnit;
290
291 /* Only one env color. Need a fallback if env colors are different
292 * and texture setup references env color in both units.
293 */
294 switch (pname) {
295 case GL_TEXTURE_ENV_COLOR: {
296 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
297 GLfloat *fc = texUnit->EnvColor;
298 GLuint r, g, b, a, col;
299 CLAMPED_FLOAT_TO_UBYTE(r, fc[0]);
300 CLAMPED_FLOAT_TO_UBYTE(g, fc[1]);
301 CLAMPED_FLOAT_TO_UBYTE(b, fc[2]);
302 CLAMPED_FLOAT_TO_UBYTE(a, fc[3]);
303
304 col = ((a << 24) |
305 (r << 16) |
306 (g << 8) |
307 (b << 0));
308
309 if (imesa->Setup[I810_CTXREG_CF1] != col) {
310 I810_STATECHANGE(imesa, I810_UPLOAD_CTX);
311 imesa->Setup[I810_CTXREG_CF1] = col;
312 }
313 break;
314 }
315 case GL_TEXTURE_ENV_MODE:
316 imesa->TexEnvImageFmt[unit] = 0; /* force recalc of env state */
317 break;
318
319 case GL_TEXTURE_LOD_BIAS_EXT:
320 {
321 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
322 i810TextureObjectPtr t = (i810TextureObjectPtr) tObj->DriverData;
323 t->Setup[I810_TEXREG_MLC] &= ~(MLC_LOD_BIAS_MASK);
324 t->Setup[I810_TEXREG_MLC] |= i810ComputeLodBias(*param);
325 }
326 break;
327
328 default:
329 break;
330 }
331 }
332
333
334
335 #if 0
336 static void i810TexImage1D( GLcontext *ctx, GLenum target, GLint level,
337 GLint internalFormat,
338 GLint width, GLint border,
339 GLenum format, GLenum type,
340 const GLvoid *pixels,
341 const struct gl_pixelstore_attrib *pack,
342 struct gl_texture_object *texObj,
343 struct gl_texture_image *texImage )
344 {
345 i810TextureObjectPtr t = (i810TextureObjectPtr) texObj->DriverData;
346 if (t) {
347 i810SwapOutTexObj( imesa, t );
348 }
349 }
350
351 static void i810TexSubImage1D( GLcontext *ctx,
352 GLenum target,
353 GLint level,
354 GLint xoffset,
355 GLsizei width,
356 GLenum format, GLenum type,
357 const GLvoid *pixels,
358 const struct gl_pixelstore_attrib *pack,
359 struct gl_texture_object *texObj,
360 struct gl_texture_image *texImage )
361 {
362 }
363 #endif
364
365
366 static void i810TexImage2D( GLcontext *ctx, GLenum target, GLint level,
367 GLint internalFormat,
368 GLint width, GLint height, GLint border,
369 GLenum format, GLenum type, const GLvoid *pixels,
370 const struct gl_pixelstore_attrib *packing,
371 struct gl_texture_object *texObj,
372 struct gl_texture_image *texImage )
373 {
374 driTextureObject *t = (driTextureObject *) texObj->DriverData;
375 if (t) {
376 I810_FIREVERTICES( I810_CONTEXT(ctx) );
377 driSwapOutTextureObject( t );
378 }
379 else {
380 t = (driTextureObject *) i810AllocTexObj( ctx, texObj );
381 if (!t) {
382 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
383 return;
384 }
385 }
386 _mesa_store_teximage2d( ctx, target, level, internalFormat,
387 width, height, border, format, type,
388 pixels, packing, texObj, texImage );
389 }
390
391 static void i810TexSubImage2D( GLcontext *ctx,
392 GLenum target,
393 GLint level,
394 GLint xoffset, GLint yoffset,
395 GLsizei width, GLsizei height,
396 GLenum format, GLenum type,
397 const GLvoid *pixels,
398 const struct gl_pixelstore_attrib *packing,
399 struct gl_texture_object *texObj,
400 struct gl_texture_image *texImage )
401 {
402 driTextureObject *t = (driTextureObject *)texObj->DriverData;
403 if (t) {
404 I810_FIREVERTICES( I810_CONTEXT(ctx) );
405 driSwapOutTextureObject( t );
406 }
407 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset, width,
408 height, format, type, pixels, packing, texObj,
409 texImage);
410 }
411
412
413 static void i810BindTexture( GLcontext *ctx, GLenum target,
414 struct gl_texture_object *tObj )
415 {
416 assert( (target != GL_TEXTURE_2D) || (tObj->DriverData != NULL) );
417 }
418
419
420 static void i810DeleteTexture( GLcontext *ctx, struct gl_texture_object *tObj )
421 {
422 driTextureObject * t = (driTextureObject *) tObj->DriverData;
423 if (t) {
424 i810ContextPtr imesa = I810_CONTEXT( ctx );
425 if (imesa)
426 I810_FIREVERTICES( imesa );
427 driDestroyTextureObject( t );
428 }
429 /* Free mipmap images and the texture object itself */
430 _mesa_delete_texture_object(ctx, tObj);
431 }
432
433 static const struct gl_texture_format *
434 i810ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
435 GLenum format, GLenum type )
436 {
437 switch ( internalFormat ) {
438 case 4:
439 case GL_RGBA:
440 case GL_COMPRESSED_RGBA:
441 if ( format == GL_BGRA ) {
442 if ( type == GL_UNSIGNED_SHORT_1_5_5_5_REV ) {
443 return &_mesa_texformat_argb1555;
444 }
445 }
446 return &_mesa_texformat_argb4444;
447
448 case 3:
449 case GL_RGB:
450 case GL_COMPRESSED_RGB:
451 case GL_R3_G3_B2:
452 case GL_RGB4:
453 case GL_RGB5:
454 case GL_RGB8:
455 case GL_RGB10:
456 case GL_RGB12:
457 case GL_RGB16:
458 return &_mesa_texformat_rgb565;
459
460 case GL_RGBA2:
461 case GL_RGBA4:
462 case GL_RGBA8:
463 case GL_RGB10_A2:
464 case GL_RGBA12:
465 case GL_RGBA16:
466 return &_mesa_texformat_argb4444;
467
468 case GL_RGB5_A1:
469 return &_mesa_texformat_argb1555;
470
471 case GL_ALPHA:
472 case GL_ALPHA4:
473 case GL_ALPHA8:
474 case GL_ALPHA12:
475 case GL_ALPHA16:
476 case GL_COMPRESSED_ALPHA:
477 return &_mesa_texformat_al88;
478
479 case 1:
480 case GL_LUMINANCE:
481 case GL_LUMINANCE4:
482 case GL_LUMINANCE8:
483 case GL_LUMINANCE12:
484 case GL_LUMINANCE16:
485 case GL_COMPRESSED_LUMINANCE:
486 return &_mesa_texformat_rgb565;
487
488 case 2:
489 case GL_LUMINANCE_ALPHA:
490 case GL_LUMINANCE4_ALPHA4:
491 case GL_LUMINANCE6_ALPHA2:
492 case GL_LUMINANCE8_ALPHA8:
493 case GL_LUMINANCE12_ALPHA4:
494 case GL_LUMINANCE12_ALPHA12:
495 case GL_LUMINANCE16_ALPHA16:
496 case GL_COMPRESSED_LUMINANCE_ALPHA:
497 case GL_INTENSITY:
498 case GL_INTENSITY4:
499 case GL_INTENSITY8:
500 case GL_INTENSITY12:
501 case GL_INTENSITY16:
502 case GL_COMPRESSED_INTENSITY:
503 return &_mesa_texformat_argb4444;
504
505 case GL_YCBCR_MESA:
506 if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
507 type == GL_UNSIGNED_BYTE)
508 return &_mesa_texformat_ycbcr;
509 else
510 return &_mesa_texformat_ycbcr_rev;
511
512 default:
513 fprintf(stderr, "unexpected texture format in %s\n", __FUNCTION__);
514 return NULL;
515 }
516
517 return NULL; /* never get here */
518 }
519
520 /**
521 * Allocate a new texture object.
522 * Called via ctx->Driver.NewTextureObject.
523 * Note: this function will be called during context creation to
524 * allocate the default texture objects.
525 * Note: we could use containment here to 'derive' the driver-specific
526 * texture object from the core mesa gl_texture_object. Not done at this time.
527 */
528 static struct gl_texture_object *
529 i810NewTextureObject( GLcontext *ctx, GLuint name, GLenum target )
530 {
531 struct gl_texture_object *obj;
532 obj = _mesa_new_texture_object(ctx, name, target);
533 i810AllocTexObj( ctx, obj );
534 return obj;
535 }
536
537 void i810InitTextureFuncs( struct dd_function_table *functions )
538 {
539 functions->ChooseTextureFormat = i810ChooseTextureFormat;
540 functions->TexImage2D = i810TexImage2D;
541 functions->TexSubImage2D = i810TexSubImage2D;
542 functions->BindTexture = i810BindTexture;
543 functions->NewTextureObject = i810NewTextureObject;
544 functions->DeleteTexture = i810DeleteTexture;
545 functions->TexParameter = i810TexParameter;
546 functions->TexEnv = i810TexEnv;
547 functions->IsTextureResident = driIsTextureResident;
548 }