Before calling _mesa_create_context(), initialize a dd_function_table struct
[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 "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 assert(t);
228
229 if ( target != GL_TEXTURE_2D )
230 return;
231
232 /* Can't do the update now as we don't know whether to flush
233 * vertices or not. Setting imesa->new_state means that
234 * i810UpdateTextureState() will be called before any triangles are
235 * rendered. If a statechange has occurred, it will be detected at
236 * that point, and buffered vertices flushed.
237 */
238 switch (pname) {
239 case GL_TEXTURE_MIN_FILTER:
240 case GL_TEXTURE_MAG_FILTER:
241 {
242 GLfloat bias = ctx->Texture.Unit[ctx->Texture.CurrentUnit].LodBias;
243 i810SetTexFilter( imesa, t, tObj->MinFilter, tObj->MagFilter, bias );
244 }
245 break;
246
247 case GL_TEXTURE_WRAP_S:
248 case GL_TEXTURE_WRAP_T:
249 i810SetTexWrapping( t, tObj->WrapS, tObj->WrapT );
250 break;
251
252 case GL_TEXTURE_BORDER_COLOR:
253 i810SetTexBorderColor( t, tObj->_BorderChan );
254 break;
255
256 case GL_TEXTURE_BASE_LEVEL:
257 case GL_TEXTURE_MAX_LEVEL:
258 case GL_TEXTURE_MIN_LOD:
259 case GL_TEXTURE_MAX_LOD:
260 /* This isn't the most efficient solution but there doesn't appear to
261 * be a nice alternative for Radeon. Since there's no LOD clamping,
262 * we just have to rely on loading the right subset of mipmap levels
263 * to simulate a clamped LOD.
264 */
265 I810_FIREVERTICES( I810_CONTEXT(ctx) );
266 driSwapOutTextureObject( (driTextureObject *) t );
267 break;
268
269 default:
270 return;
271 }
272
273 if (t == imesa->CurrentTexObj[0]) {
274 I810_STATECHANGE( imesa, I810_UPLOAD_TEX0 );
275 }
276
277 if (t == imesa->CurrentTexObj[1]) {
278 I810_STATECHANGE( imesa, I810_UPLOAD_TEX1 );
279 }
280 }
281
282
283 static void i810TexEnv( GLcontext *ctx, GLenum target,
284 GLenum pname, const GLfloat *param )
285 {
286 i810ContextPtr imesa = I810_CONTEXT( ctx );
287 GLuint unit = ctx->Texture.CurrentUnit;
288
289 /* Only one env color. Need a fallback if env colors are different
290 * and texture setup references env color in both units.
291 */
292 switch (pname) {
293 case GL_TEXTURE_ENV_COLOR: {
294 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
295 GLfloat *fc = texUnit->EnvColor;
296 GLuint r, g, b, a, col;
297 CLAMPED_FLOAT_TO_UBYTE(r, fc[0]);
298 CLAMPED_FLOAT_TO_UBYTE(g, fc[1]);
299 CLAMPED_FLOAT_TO_UBYTE(b, fc[2]);
300 CLAMPED_FLOAT_TO_UBYTE(a, fc[3]);
301
302 col = ((a << 24) |
303 (r << 16) |
304 (g << 8) |
305 (b << 0));
306
307 if (imesa->Setup[I810_CTXREG_CF1] != col) {
308 I810_STATECHANGE(imesa, I810_UPLOAD_CTX);
309 imesa->Setup[I810_CTXREG_CF1] = col;
310 }
311 break;
312 }
313 case GL_TEXTURE_ENV_MODE:
314 imesa->TexEnvImageFmt[unit] = 0; /* force recalc of env state */
315 break;
316
317 case GL_TEXTURE_LOD_BIAS_EXT:
318 {
319 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
320 i810TextureObjectPtr t = (i810TextureObjectPtr) tObj->DriverData;
321 t->Setup[I810_TEXREG_MLC] &= ~(MLC_LOD_BIAS_MASK);
322 t->Setup[I810_TEXREG_MLC] |= i810ComputeLodBias(*param);
323 }
324 break;
325
326 default:
327 break;
328 }
329 }
330
331
332
333 #if 0
334 static void i810TexImage1D( GLcontext *ctx, GLenum target, GLint level,
335 GLint internalFormat,
336 GLint width, GLint border,
337 GLenum format, GLenum type,
338 const GLvoid *pixels,
339 const struct gl_pixelstore_attrib *pack,
340 struct gl_texture_object *texObj,
341 struct gl_texture_image *texImage )
342 {
343 i810TextureObjectPtr t = (i810TextureObjectPtr) texObj->DriverData;
344 assert(t);
345 if (t) {
346 i810SwapOutTexObj( imesa, t );
347 }
348 }
349
350 static void i810TexSubImage1D( GLcontext *ctx,
351 GLenum target,
352 GLint level,
353 GLint xoffset,
354 GLsizei width,
355 GLenum format, GLenum type,
356 const GLvoid *pixels,
357 const struct gl_pixelstore_attrib *pack,
358 struct gl_texture_object *texObj,
359 struct gl_texture_image *texImage )
360 {
361 }
362 #endif
363
364
365 static void i810TexImage2D( GLcontext *ctx, GLenum target, GLint level,
366 GLint internalFormat,
367 GLint width, GLint height, GLint border,
368 GLenum format, GLenum type, const GLvoid *pixels,
369 const struct gl_pixelstore_attrib *packing,
370 struct gl_texture_object *texObj,
371 struct gl_texture_image *texImage )
372 {
373 driTextureObject *t = (driTextureObject *) texObj->DriverData;
374 assert(t);
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 assert(t);
404 if (t) {
405 I810_FIREVERTICES( I810_CONTEXT(ctx) );
406 driSwapOutTextureObject( t );
407 }
408 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset, width,
409 height, format, type, pixels, packing, texObj,
410 texImage);
411 }
412
413
414 #if 0
415 /* not needed anymore */
416 static void i810BindTexture( GLcontext *ctx, GLenum target,
417 struct gl_texture_object *tObj )
418 {
419 assert(t);
420 if (!tObj->DriverData) {
421 i810AllocTexObj( ctx, tObj );
422 }
423 }
424 #endif
425
426
427 static void i810DeleteTexture( GLcontext *ctx, struct gl_texture_object *tObj )
428 {
429 driTextureObject * t = (driTextureObject *) tObj->DriverData;
430 assert(t);
431 if (t) {
432 i810ContextPtr imesa = I810_CONTEXT( ctx );
433 if (imesa)
434 I810_FIREVERTICES( imesa );
435 driDestroyTextureObject( t );
436 }
437 /* Free mipmap images and the texture object itself */
438 _mesa_delete_texture_object(ctx, tObj);
439 }
440
441 static const struct gl_texture_format *
442 i810ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
443 GLenum format, GLenum type )
444 {
445 switch ( internalFormat ) {
446 case 4:
447 case GL_RGBA:
448 case GL_COMPRESSED_RGBA:
449 if ( format == GL_BGRA ) {
450 if ( type == GL_UNSIGNED_SHORT_1_5_5_5_REV ) {
451 return &_mesa_texformat_argb1555;
452 }
453 }
454 return &_mesa_texformat_argb4444;
455
456 case 3:
457 case GL_RGB:
458 case GL_COMPRESSED_RGB:
459 case GL_R3_G3_B2:
460 case GL_RGB4:
461 case GL_RGB5:
462 case GL_RGB8:
463 case GL_RGB10:
464 case GL_RGB12:
465 case GL_RGB16:
466 return &_mesa_texformat_rgb565;
467
468 case GL_RGBA2:
469 case GL_RGBA4:
470 case GL_RGBA8:
471 case GL_RGB10_A2:
472 case GL_RGBA12:
473 case GL_RGBA16:
474 return &_mesa_texformat_argb4444;
475
476 case GL_RGB5_A1:
477 return &_mesa_texformat_argb1555;
478
479 case GL_ALPHA:
480 case GL_ALPHA4:
481 case GL_ALPHA8:
482 case GL_ALPHA12:
483 case GL_ALPHA16:
484 case GL_COMPRESSED_ALPHA:
485 return &_mesa_texformat_al88;
486
487 case 1:
488 case GL_LUMINANCE:
489 case GL_LUMINANCE4:
490 case GL_LUMINANCE8:
491 case GL_LUMINANCE12:
492 case GL_LUMINANCE16:
493 case GL_COMPRESSED_LUMINANCE:
494 return &_mesa_texformat_rgb565;
495
496 case 2:
497 case GL_LUMINANCE_ALPHA:
498 case GL_LUMINANCE4_ALPHA4:
499 case GL_LUMINANCE6_ALPHA2:
500 case GL_LUMINANCE8_ALPHA8:
501 case GL_LUMINANCE12_ALPHA4:
502 case GL_LUMINANCE12_ALPHA12:
503 case GL_LUMINANCE16_ALPHA16:
504 case GL_COMPRESSED_LUMINANCE_ALPHA:
505 case GL_INTENSITY:
506 case GL_INTENSITY4:
507 case GL_INTENSITY8:
508 case GL_INTENSITY12:
509 case GL_INTENSITY16:
510 case GL_COMPRESSED_INTENSITY:
511 return &_mesa_texformat_argb4444;
512
513 case GL_YCBCR_MESA:
514 if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
515 type == GL_UNSIGNED_BYTE)
516 return &_mesa_texformat_ycbcr;
517 else
518 return &_mesa_texformat_ycbcr_rev;
519
520 default:
521 fprintf(stderr, "unexpected texture format in %s\n", __FUNCTION__);
522 return NULL;
523 }
524
525 return NULL; /* never get here */
526 }
527
528 /**
529 * Allocate a new texture object.
530 * Called via ctx->Driver.NewTextureObject.
531 * Note: this function will be called during context creation to
532 * allocate the default texture objects.
533 */
534 static struct gl_texture_object *
535 i810NewTextureObject( GLcontext *ctx, GLuint name, GLenum target )
536 {
537 struct gl_texture_object *obj;
538 driTextureObject *t;
539 obj = _mesa_new_texture_object(ctx, name, target);
540 if (!obj)
541 return NULL;
542 t = (driTextureObject *) i810AllocTexObj( ctx, obj );
543 if (!t) {
544 _mesa_delete_texture_object(ctx, obj);
545 return NULL;
546 }
547 return obj;
548 }
549
550 void i810InitTextureFuncs( struct dd_function_table *functions )
551 {
552 functions->ChooseTextureFormat = i810ChooseTextureFormat;
553 functions->TexImage2D = i810TexImage2D;
554 functions->TexSubImage2D = i810TexSubImage2D;
555 /*functions->BindTexture = i810BindTexture;*/
556 functions->NewTextureObject = i810NewTextureObject;
557 functions->DeleteTexture = i810DeleteTexture;
558 functions->TexParameter = i810TexParameter;
559 functions->TexEnv = i810TexEnv;
560 functions->IsTextureResident = driIsTextureResident;
561 }