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"
35 #include "swrast/swrast.h"
40 #include "i810screen.h"
43 #include "i810context.h"
45 #include "i810state.h"
46 #include "i810ioctl.h"
50 * Compute the 'S2.4' lod bias factor from the floating point OpenGL bias.
52 static GLuint
i810ComputeLodBias(GLfloat bias
)
54 int b
= (int) (bias
* 16.0) + 12;
59 return (GLuint
) (b
& MLC_LOD_BIAS_MASK
);
63 static void i810SetTexWrapping(i810TextureObjectPtr tex
,
64 GLenum swrap
, GLenum twrap
)
66 tex
->Setup
[I810_TEXREG_MCS
] &= ~(MCS_U_STATE_MASK
| MCS_V_STATE_MASK
);
70 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_U_WRAP
;
73 case GL_CLAMP_TO_EDGE
:
74 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_U_CLAMP
;
76 case GL_MIRRORED_REPEAT
:
77 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_U_MIRROR
;
80 _mesa_problem(NULL
, "bad S wrap mode in %s", __FUNCTION__
);
85 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_V_WRAP
;
88 case GL_CLAMP_TO_EDGE
:
89 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_V_CLAMP
;
91 case GL_MIRRORED_REPEAT
:
92 tex
->Setup
[I810_TEXREG_MCS
] |= MCS_V_MIRROR
;
95 _mesa_problem(NULL
, "bad T wrap mode in %s", __FUNCTION__
);
100 static void i810SetTexFilter(i810ContextPtr imesa
,
101 i810TextureObjectPtr t
,
102 GLenum minf
, GLenum magf
,
105 t
->Setup
[I810_TEXREG_MF
] &= ~(MF_MIN_MASK
|
108 t
->Setup
[I810_TEXREG_MLC
] &= ~(MLC_LOD_BIAS_MASK
);
112 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_NEAREST
| MF_MIP_NONE
;
115 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_LINEAR
| MF_MIP_NONE
;
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 */
123 case GL_LINEAR_MIPMAP_NEAREST
:
124 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_LINEAR
| MF_MIP_NEAREST
;
126 case GL_NEAREST_MIPMAP_LINEAR
:
128 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_NEAREST
| MF_MIP_LINEAR
;
130 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_NEAREST
| MF_MIP_DITHER
;
132 if (magf == GL_LINEAR) {
136 bias
-= 0.5; /* always biasing here looks better */
138 case GL_LINEAR_MIPMAP_LINEAR
:
140 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_LINEAR
| MF_MIP_LINEAR
;
142 t
->Setup
[I810_TEXREG_MF
] |= MF_MIN_LINEAR
| MF_MIP_DITHER
;
150 t
->Setup
[I810_TEXREG_MF
] |= MF_MAG_NEAREST
;
153 t
->Setup
[I810_TEXREG_MF
] |= MF_MAG_LINEAR
;
159 t
->Setup
[I810_TEXREG_MLC
] |= i810ComputeLodBias(bias
);
164 i810SetTexBorderColor( i810TextureObjectPtr t
, GLubyte color
[4] )
171 static i810TextureObjectPtr
172 i810AllocTexObj( GLcontext
*ctx
, struct gl_texture_object
*texObj
)
174 i810TextureObjectPtr t
;
175 i810ContextPtr imesa
= I810_CONTEXT(ctx
);
177 t
= CALLOC_STRUCT( i810_texture_object_t
);
178 texObj
->DriverData
= t
;
180 GLfloat bias
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].LodBias
;
181 /* Initialize non-image-dependent parts of the state:
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
|
189 /*MLC_DITHER_WEIGHT_FULL |*/
190 MLC_DITHER_WEIGHT_12
|
191 MLC_UPDATE_LOD_BIAS
|
193 t
->Setup
[I810_TEXREG_MCS
] = (GFX_OP_MAP_COORD_SETS
|
195 MCS_UPDATE_NORMALIZED
|
196 MCS_NORMALIZED_COORDS
|
201 t
->Setup
[I810_TEXREG_MF
] = (GFX_OP_MAP_FILTER
|
203 MF_UPDATE_ANISOTROPIC
|
204 MF_UPDATE_MIP_FILTER
|
205 MF_UPDATE_MAG_FILTER
|
206 MF_UPDATE_MIN_FILTER
);
208 make_empty_list( & t
->base
);
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
);
220 static void i810TexParameter( GLcontext
*ctx
, GLenum target
,
221 struct gl_texture_object
*tObj
,
222 GLenum pname
, const GLfloat
*params
)
224 i810ContextPtr imesa
= I810_CONTEXT(ctx
);
225 i810TextureObjectPtr t
= (i810TextureObjectPtr
) tObj
->DriverData
;
229 if ( target
!= GL_TEXTURE_2D
)
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.
239 case GL_TEXTURE_MIN_FILTER
:
240 case GL_TEXTURE_MAG_FILTER
:
242 GLfloat bias
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].LodBias
;
243 i810SetTexFilter( imesa
, t
, tObj
->MinFilter
, tObj
->MagFilter
, bias
);
247 case GL_TEXTURE_WRAP_S
:
248 case GL_TEXTURE_WRAP_T
:
249 i810SetTexWrapping( t
, tObj
->WrapS
, tObj
->WrapT
);
252 case GL_TEXTURE_BORDER_COLOR
:
253 i810SetTexBorderColor( t
, tObj
->_BorderChan
);
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.
265 I810_FIREVERTICES( I810_CONTEXT(ctx
) );
266 driSwapOutTextureObject( (driTextureObject
*) t
);
273 if (t
== imesa
->CurrentTexObj
[0]) {
274 I810_STATECHANGE( imesa
, I810_UPLOAD_TEX0
);
277 if (t
== imesa
->CurrentTexObj
[1]) {
278 I810_STATECHANGE( imesa
, I810_UPLOAD_TEX1
);
283 static void i810TexEnv( GLcontext
*ctx
, GLenum target
,
284 GLenum pname
, const GLfloat
*param
)
286 i810ContextPtr imesa
= I810_CONTEXT( ctx
);
287 GLuint unit
= ctx
->Texture
.CurrentUnit
;
289 /* Only one env color. Need a fallback if env colors are different
290 * and texture setup references env color in both units.
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]);
307 if (imesa
->Setup
[I810_CTXREG_CF1
] != col
) {
308 I810_STATECHANGE(imesa
, I810_UPLOAD_CTX
);
309 imesa
->Setup
[I810_CTXREG_CF1
] = col
;
313 case GL_TEXTURE_ENV_MODE
:
314 imesa
->TexEnvImageFmt
[unit
] = 0; /* force recalc of env state */
317 case GL_TEXTURE_LOD_BIAS_EXT
:
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
);
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
)
343 i810TextureObjectPtr t
= (i810TextureObjectPtr
) texObj
->DriverData
;
346 i810SwapOutTexObj( imesa
, t
);
350 static void i810TexSubImage1D( GLcontext
*ctx
,
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
)
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
)
373 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
;
405 I810_FIREVERTICES( I810_CONTEXT(ctx
) );
406 driSwapOutTextureObject( t
);
408 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
, width
,
409 height
, format
, type
, pixels
, packing
, texObj
,
415 /* not needed anymore */
416 static void i810BindTexture( GLcontext
*ctx
, GLenum target
,
417 struct gl_texture_object
*tObj
)
420 if (!tObj
->DriverData
) {
421 i810AllocTexObj( ctx
, tObj
);
427 static void i810DeleteTexture( GLcontext
*ctx
, struct gl_texture_object
*tObj
)
429 driTextureObject
* t
= (driTextureObject
*) tObj
->DriverData
;
432 i810ContextPtr imesa
= I810_CONTEXT( ctx
);
434 I810_FIREVERTICES( imesa
);
435 driDestroyTextureObject( t
);
437 /* Free mipmap images and the texture object itself */
438 _mesa_delete_texture_object(ctx
, tObj
);
441 static const struct gl_texture_format
*
442 i810ChooseTextureFormat( GLcontext
*ctx
, GLint internalFormat
,
443 GLenum format
, GLenum type
)
445 switch ( internalFormat
) {
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
;
454 return &_mesa_texformat_argb4444
;
458 case GL_COMPRESSED_RGB
:
466 return &_mesa_texformat_rgb565
;
474 return &_mesa_texformat_argb4444
;
477 return &_mesa_texformat_argb1555
;
484 case GL_COMPRESSED_ALPHA
:
485 return &_mesa_texformat_al88
;
493 case GL_COMPRESSED_LUMINANCE
:
494 return &_mesa_texformat_rgb565
;
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
:
510 case GL_COMPRESSED_INTENSITY
:
511 return &_mesa_texformat_argb4444
;
514 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
515 type
== GL_UNSIGNED_BYTE
)
516 return &_mesa_texformat_ycbcr
;
518 return &_mesa_texformat_ycbcr_rev
;
521 fprintf(stderr
, "unexpected texture format in %s\n", __FUNCTION__
);
525 return NULL
; /* never get here */
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.
534 static struct gl_texture_object
*
535 i810NewTextureObject( GLcontext
*ctx
, GLuint name
, GLenum target
)
537 struct gl_texture_object
*obj
;
539 obj
= _mesa_new_texture_object(ctx
, name
, target
);
542 t
= (driTextureObject
*) i810AllocTexObj( ctx
, obj
);
544 _mesa_delete_texture_object(ctx
, obj
);
550 void i810InitTextureFuncs( struct dd_function_table
*functions
)
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
;