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