2 * GLX Hardware Device Driver for Intel i810
3 * Copyright (C) 1999 Keith Whitwell
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:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
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.
24 /* $XFree86: xc/lib/GL/mesa/src/drv/i810/i810tex.c,v 1.9 2002/10/30 12:51:33 alanh Exp $ */
29 #include "simple_list.h"
32 #include "texformat.h"
36 #include "swrast/swrast.h"
41 #include "i810screen.h"
44 #include "i810context.h"
46 #include "i810state.h"
47 #include "i810ioctl.h"
51 * Compute the 'S2.4' lod bias factor from the floating point OpenGL bias.
53 static GLuint
i810ComputeLodBias(GLfloat bias
)
55 int b
= (int) (bias
* 16.0) + 12;
60 return (GLuint
) (b
& MLC_LOD_BIAS_MASK
);
64 static void i810SetTexWrapping(i810TextureObjectPtr tex
,
65 GLenum swrap
, GLenum twrap
)
67 tex
->Setup
[I810_TEXREG_MCS
] &= ~(MCS_U_STATE_MASK
| MCS_V_STATE_MASK
);
71 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_U_WRAP
;
74 case GL_CLAMP_TO_EDGE
:
75 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_U_CLAMP
;
77 case GL_MIRRORED_REPEAT
:
78 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_U_MIRROR
;
81 _mesa_problem(NULL
, "bad S wrap mode in %s", __FUNCTION__
);
86 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_V_WRAP
;
89 case GL_CLAMP_TO_EDGE
:
90 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_V_CLAMP
;
92 case GL_MIRRORED_REPEAT
:
93 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_V_MIRROR
;
96 _mesa_problem(NULL
, "bad T wrap mode in %s", __FUNCTION__
);
101 static void i810SetTexFilter(i810ContextPtr imesa
,
102 i810TextureObjectPtr t
,
103 GLenum minf
, GLenum magf
,
106 t
->Setup
[I810_TEXREG_MF
] &= ~(MF_MIN_MASK
|
109 t
->Setup
[I810_TEXREG_MLC
] &= ~(MLC_LOD_BIAS_MASK
);
113 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_NEAREST
| MF_MIP_NONE
;
116 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_LINEAR
| MF_MIP_NONE
;
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 */
124 case GL_LINEAR_MIPMAP_NEAREST
:
125 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_LINEAR
| MF_MIP_NEAREST
;
127 case GL_NEAREST_MIPMAP_LINEAR
:
129 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_NEAREST
| MF_MIP_LINEAR
;
131 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_NEAREST
| MF_MIP_DITHER
;
133 if (magf == GL_LINEAR) {
137 bias
-= 0.5; /* always biasing here looks better */
139 case GL_LINEAR_MIPMAP_LINEAR
:
141 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_LINEAR
| MF_MIP_LINEAR
;
143 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_LINEAR
| MF_MIP_DITHER
;
151 t
->Setup
[I810_TEXREG_MF
] |= MF_MAG_NEAREST
;
154 t
->Setup
[I810_TEXREG_MF
] |= MF_MAG_LINEAR
;
160 t
->Setup
[I810_TEXREG_MLC
] |= i810ComputeLodBias(bias
);
165 i810SetTexBorderColor( i810TextureObjectPtr t
, GLubyte color
[4] )
172 static i810TextureObjectPtr
173 i810AllocTexObj( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
175 i810TextureObjectPtr t
;
176 i810ContextPtr imesa
= I810_CONTEXT(ctx
);
178 t
= CALLOC_STRUCT( i810_texture_object_t
);
179 texObj
->DriverData
= t
;
181 GLfloat bias
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].LodBias
;
182 /* Initialize non-image-dependent parts of the state:
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
|
190 /*MLC_DITHER_WEIGHT_FULL |*/
191 MLC_DITHER_WEIGHT_12
|
192 MLC_UPDATE_LOD_BIAS
|
194 t
->Setup
[I810_TEXREG_MCS
] = (GFX_OP_MAP_COORD_SETS
|
196 MCS_UPDATE_NORMALIZED
|
197 MCS_NORMALIZED_COORDS
|
202 t
->Setup
[I810_TEXREG_MF
] = (GFX_OP_MAP_FILTER
|
204 MF_UPDATE_ANISOTROPIC
|
205 MF_UPDATE_MIP_FILTER
|
206 MF_UPDATE_MAG_FILTER
|
207 MF_UPDATE_MIN_FILTER
);
209 make_empty_list( & t
->base
);
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
);
221 static void i810TexParameter( GLcontext
*ctx
, GLenum target
,
222 struct gl_texture_object
*tObj
,
223 GLenum pname
, const GLfloat
*params
)
225 i810ContextPtr imesa
= I810_CONTEXT(ctx
);
226 i810TextureObjectPtr t
= (i810TextureObjectPtr
) tObj
->DriverData
;
231 if ( target
!= GL_TEXTURE_2D
)
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.
241 case GL_TEXTURE_MIN_FILTER
:
242 case GL_TEXTURE_MAG_FILTER
:
244 GLfloat bias
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].LodBias
;
245 i810SetTexFilter( imesa
, t
, tObj
->MinFilter
, tObj
->MagFilter
, bias
);
249 case GL_TEXTURE_WRAP_S
:
250 case GL_TEXTURE_WRAP_T
:
251 i810SetTexWrapping( t
, tObj
->WrapS
, tObj
->WrapT
);
254 case GL_TEXTURE_BORDER_COLOR
:
255 i810SetTexBorderColor( t
, tObj
->_BorderChan
);
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.
267 I810_FIREVERTICES( I810_CONTEXT(ctx
) );
268 driSwapOutTextureObject( (driTextureObject
*) t
);
275 if (t
== imesa
->CurrentTexObj
[0]) {
276 I810_STATECHANGE( imesa
, I810_UPLOAD_TEX0
);
279 if (t
== imesa
->CurrentTexObj
[1]) {
280 I810_STATECHANGE( imesa
, I810_UPLOAD_TEX1
);
285 static void i810TexEnv( GLcontext
*ctx
, GLenum target
,
286 GLenum pname
, const GLfloat
*param
)
288 i810ContextPtr imesa
= I810_CONTEXT( ctx
);
289 GLuint unit
= ctx
->Texture
.CurrentUnit
;
291 /* Only one env color. Need a fallback if env colors are different
292 * and texture setup references env color in both units.
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]);
309 if (imesa
->Setup
[I810_CTXREG_CF1
] != col
) {
310 I810_STATECHANGE(imesa
, I810_UPLOAD_CTX
);
311 imesa
->Setup
[I810_CTXREG_CF1
] = col
;
315 case GL_TEXTURE_ENV_MODE
:
316 imesa
->TexEnvImageFmt
[unit
] = 0; /* force recalc of env state */
319 case GL_TEXTURE_LOD_BIAS_EXT
:
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
);
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
)
345 i810TextureObjectPtr t
= (i810TextureObjectPtr
) texObj
->DriverData
;
347 i810SwapOutTexObj( imesa
, t
);
351 static void i810TexSubImage1D( GLcontext
*ctx
,
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
)
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
)
374 driTextureObject
*t
= (driTextureObject
*) texObj
->DriverData
;
376 I810_FIREVERTICES( I810_CONTEXT(ctx
) );
377 driSwapOutTextureObject( t
);
380 t
= (driTextureObject
*) i810AllocTexObj( ctx
, texObj
);
382 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage2D");
386 _mesa_store_teximage2d( ctx
, target
, level
, internalFormat
,
387 width
, height
, border
, format
, type
,
388 pixels
, packing
, texObj
, texImage
);
391 static void i810TexSubImage2D( GLcontext
*ctx
,
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
)
402 driTextureObject
*t
= (driTextureObject
*)texObj
->DriverData
;
404 I810_FIREVERTICES( I810_CONTEXT(ctx
) );
405 driSwapOutTextureObject( t
);
407 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
, width
,
408 height
, format
, type
, pixels
, packing
, texObj
,
413 static void i810BindTexture( GLcontext
*ctx
, GLenum target
,
414 struct gl_texture_object
*tObj
)
416 assert( (target
!= GL_TEXTURE_2D
) || (tObj
->DriverData
!= NULL
) );
420 static void i810DeleteTexture( GLcontext
*ctx
, struct gl_texture_object
*tObj
)
422 driTextureObject
* t
= (driTextureObject
*) tObj
->DriverData
;
424 i810ContextPtr imesa
= I810_CONTEXT( ctx
);
426 I810_FIREVERTICES( imesa
);
427 driDestroyTextureObject( t
);
429 /* Free mipmap images and the texture object itself */
430 _mesa_delete_texture_object(ctx
, tObj
);
433 static const struct gl_texture_format
*
434 i810ChooseTextureFormat( GLcontext
*ctx
, GLint internalFormat
,
435 GLenum format
, GLenum type
)
437 switch ( internalFormat
) {
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
;
446 return &_mesa_texformat_argb4444
;
450 case GL_COMPRESSED_RGB
:
458 return &_mesa_texformat_rgb565
;
466 return &_mesa_texformat_argb4444
;
469 return &_mesa_texformat_argb1555
;
476 case GL_COMPRESSED_ALPHA
:
477 return &_mesa_texformat_al88
;
485 case GL_COMPRESSED_LUMINANCE
:
486 return &_mesa_texformat_rgb565
;
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
:
502 case GL_COMPRESSED_INTENSITY
:
503 return &_mesa_texformat_argb4444
;
506 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
507 type
== GL_UNSIGNED_BYTE
)
508 return &_mesa_texformat_ycbcr
;
510 return &_mesa_texformat_ycbcr_rev
;
513 fprintf(stderr
, "unexpected texture format in %s\n", __FUNCTION__
);
517 return NULL
; /* never get here */
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.
528 static struct gl_texture_object
*
529 i810NewTextureObject( GLcontext
*ctx
, GLuint name
, GLenum target
)
531 struct gl_texture_object
*obj
;
532 obj
= _mesa_new_texture_object(ctx
, name
, target
);
533 i810AllocTexObj( ctx
, obj
);
537 void i810InitTextureFuncs( struct dd_function_table
*functions
)
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
;