2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "main/glheader.h"
35 #include "main/imports.h"
36 #include "main/colormac.h"
37 #include "main/context.h"
38 #include "main/enums.h"
39 #include "main/image.h"
40 #include "main/simple_list.h"
41 #include "main/texstore.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
45 #include "radeon_mipmap_tree.h"
46 #include "r200_context.h"
47 #include "r200_ioctl.h"
55 * Set the texture wrap modes.
57 * \param t Texture object whose wrap modes are to be set
58 * \param swrap Wrap mode for the \a s texture coordinate
59 * \param twrap Wrap mode for the \a t texture coordinate
62 static void r200SetTexWrap( radeonTexObjPtr t
, GLenum swrap
, GLenum twrap
, GLenum rwrap
)
64 GLboolean is_clamp
= GL_FALSE
;
65 GLboolean is_clamp_to_border
= GL_FALSE
;
66 struct gl_texture_object
*tObj
= &t
->base
;
68 t
->pp_txfilter
&= ~(R200_CLAMP_S_MASK
| R200_CLAMP_T_MASK
| R200_BORDER_MODE_D3D
);
72 t
->pp_txfilter
|= R200_CLAMP_S_WRAP
;
75 t
->pp_txfilter
|= R200_CLAMP_S_CLAMP_GL
;
78 case GL_CLAMP_TO_EDGE
:
79 t
->pp_txfilter
|= R200_CLAMP_S_CLAMP_LAST
;
81 case GL_CLAMP_TO_BORDER
:
82 t
->pp_txfilter
|= R200_CLAMP_S_CLAMP_GL
;
83 is_clamp_to_border
= GL_TRUE
;
85 case GL_MIRRORED_REPEAT
:
86 t
->pp_txfilter
|= R200_CLAMP_S_MIRROR
;
88 case GL_MIRROR_CLAMP_EXT
:
89 t
->pp_txfilter
|= R200_CLAMP_S_MIRROR_CLAMP_GL
;
92 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
93 t
->pp_txfilter
|= R200_CLAMP_S_MIRROR_CLAMP_LAST
;
95 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
96 t
->pp_txfilter
|= R200_CLAMP_S_MIRROR_CLAMP_GL
;
97 is_clamp_to_border
= GL_TRUE
;
100 _mesa_problem(NULL
, "bad S wrap mode in %s", __FUNCTION__
);
103 if (tObj
->Target
!= GL_TEXTURE_1D
) {
106 t
->pp_txfilter
|= R200_CLAMP_T_WRAP
;
109 t
->pp_txfilter
|= R200_CLAMP_T_CLAMP_GL
;
112 case GL_CLAMP_TO_EDGE
:
113 t
->pp_txfilter
|= R200_CLAMP_T_CLAMP_LAST
;
115 case GL_CLAMP_TO_BORDER
:
116 t
->pp_txfilter
|= R200_CLAMP_T_CLAMP_GL
;
117 is_clamp_to_border
= GL_TRUE
;
119 case GL_MIRRORED_REPEAT
:
120 t
->pp_txfilter
|= R200_CLAMP_T_MIRROR
;
122 case GL_MIRROR_CLAMP_EXT
:
123 t
->pp_txfilter
|= R200_CLAMP_T_MIRROR_CLAMP_GL
;
126 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
127 t
->pp_txfilter
|= R200_CLAMP_T_MIRROR_CLAMP_LAST
;
129 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
130 t
->pp_txfilter
|= R200_CLAMP_T_MIRROR_CLAMP_GL
;
131 is_clamp_to_border
= GL_TRUE
;
134 _mesa_problem(NULL
, "bad T wrap mode in %s", __FUNCTION__
);
138 t
->pp_txformat_x
&= ~R200_CLAMP_Q_MASK
;
142 t
->pp_txformat_x
|= R200_CLAMP_Q_WRAP
;
145 t
->pp_txformat_x
|= R200_CLAMP_Q_CLAMP_GL
;
148 case GL_CLAMP_TO_EDGE
:
149 t
->pp_txformat_x
|= R200_CLAMP_Q_CLAMP_LAST
;
151 case GL_CLAMP_TO_BORDER
:
152 t
->pp_txformat_x
|= R200_CLAMP_Q_CLAMP_GL
;
153 is_clamp_to_border
= GL_TRUE
;
155 case GL_MIRRORED_REPEAT
:
156 t
->pp_txformat_x
|= R200_CLAMP_Q_MIRROR
;
158 case GL_MIRROR_CLAMP_EXT
:
159 t
->pp_txformat_x
|= R200_CLAMP_Q_MIRROR_CLAMP_GL
;
162 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
163 t
->pp_txformat_x
|= R200_CLAMP_Q_MIRROR_CLAMP_LAST
;
165 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
166 t
->pp_txformat_x
|= R200_CLAMP_Q_MIRROR_CLAMP_GL
;
167 is_clamp_to_border
= GL_TRUE
;
170 _mesa_problem(NULL
, "bad R wrap mode in %s", __FUNCTION__
);
173 if ( is_clamp_to_border
) {
174 t
->pp_txfilter
|= R200_BORDER_MODE_D3D
;
177 t
->border_fallback
= (is_clamp
&& is_clamp_to_border
);
180 static void r200SetTexMaxAnisotropy( radeonTexObjPtr t
, GLfloat max
)
182 t
->pp_txfilter
&= ~R200_MAX_ANISO_MASK
;
185 t
->pp_txfilter
|= R200_MAX_ANISO_1_TO_1
;
186 } else if ( max
<= 2.0 ) {
187 t
->pp_txfilter
|= R200_MAX_ANISO_2_TO_1
;
188 } else if ( max
<= 4.0 ) {
189 t
->pp_txfilter
|= R200_MAX_ANISO_4_TO_1
;
190 } else if ( max
<= 8.0 ) {
191 t
->pp_txfilter
|= R200_MAX_ANISO_8_TO_1
;
193 t
->pp_txfilter
|= R200_MAX_ANISO_16_TO_1
;
198 * Set the texture magnification and minification modes.
200 * \param t Texture whose filter modes are to be set
201 * \param minf Texture minification mode
202 * \param magf Texture magnification mode
205 static void r200SetTexFilter( radeonTexObjPtr t
, GLenum minf
, GLenum magf
)
207 GLuint anisotropy
= (t
->pp_txfilter
& R200_MAX_ANISO_MASK
);
209 /* Force revalidation to account for switches from/to mipmapping. */
210 t
->validated
= GL_FALSE
;
212 t
->pp_txfilter
&= ~(R200_MIN_FILTER_MASK
| R200_MAG_FILTER_MASK
);
213 t
->pp_txformat_x
&= ~R200_VOLUME_FILTER_MASK
;
215 if ( anisotropy
== R200_MAX_ANISO_1_TO_1
) {
218 t
->pp_txfilter
|= R200_MIN_FILTER_NEAREST
;
221 t
->pp_txfilter
|= R200_MIN_FILTER_LINEAR
;
223 case GL_NEAREST_MIPMAP_NEAREST
:
224 t
->pp_txfilter
|= R200_MIN_FILTER_NEAREST_MIP_NEAREST
;
226 case GL_NEAREST_MIPMAP_LINEAR
:
227 t
->pp_txfilter
|= R200_MIN_FILTER_LINEAR_MIP_NEAREST
;
229 case GL_LINEAR_MIPMAP_NEAREST
:
230 t
->pp_txfilter
|= R200_MIN_FILTER_NEAREST_MIP_LINEAR
;
232 case GL_LINEAR_MIPMAP_LINEAR
:
233 t
->pp_txfilter
|= R200_MIN_FILTER_LINEAR_MIP_LINEAR
;
239 t
->pp_txfilter
|= R200_MIN_FILTER_ANISO_NEAREST
;
242 t
->pp_txfilter
|= R200_MIN_FILTER_ANISO_LINEAR
;
244 case GL_NEAREST_MIPMAP_NEAREST
:
245 case GL_LINEAR_MIPMAP_NEAREST
:
246 t
->pp_txfilter
|= R200_MIN_FILTER_ANISO_NEAREST_MIP_NEAREST
;
248 case GL_NEAREST_MIPMAP_LINEAR
:
249 case GL_LINEAR_MIPMAP_LINEAR
:
250 t
->pp_txfilter
|= R200_MIN_FILTER_ANISO_NEAREST_MIP_LINEAR
;
255 /* Note we don't have 3D mipmaps so only use the mag filter setting
256 * to set the 3D texture filter mode.
260 t
->pp_txfilter
|= R200_MAG_FILTER_NEAREST
;
261 t
->pp_txformat_x
|= R200_VOLUME_FILTER_NEAREST
;
264 t
->pp_txfilter
|= R200_MAG_FILTER_LINEAR
;
265 t
->pp_txformat_x
|= R200_VOLUME_FILTER_LINEAR
;
270 static void r200SetTexBorderColor( radeonTexObjPtr t
, const GLfloat color
[4] )
273 CLAMPED_FLOAT_TO_UBYTE(c
[0], color
[0]);
274 CLAMPED_FLOAT_TO_UBYTE(c
[1], color
[1]);
275 CLAMPED_FLOAT_TO_UBYTE(c
[2], color
[2]);
276 CLAMPED_FLOAT_TO_UBYTE(c
[3], color
[3]);
277 t
->pp_border_color
= radeonPackColor( 4, c
[0], c
[1], c
[2], c
[3] );
280 static void r200TexEnv( GLcontext
*ctx
, GLenum target
,
281 GLenum pname
, const GLfloat
*param
)
283 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
284 GLuint unit
= ctx
->Texture
.CurrentUnit
;
285 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
287 if ( R200_DEBUG
& RADEON_STATE
) {
288 fprintf( stderr
, "%s( %s )\n",
289 __FUNCTION__
, _mesa_lookup_enum_by_nr( pname
) );
292 /* This is incorrect: Need to maintain this data for each of
293 * GL_TEXTURE_{123}D, GL_TEXTURE_RECTANGLE_NV, etc, and switch
294 * between them according to _ReallyEnabled.
297 case GL_TEXTURE_ENV_COLOR
: {
300 UNCLAMPED_FLOAT_TO_RGBA_CHAN( c
, texUnit
->EnvColor
);
301 envColor
= radeonPackColor( 4, c
[0], c
[1], c
[2], c
[3] );
302 if ( rmesa
->hw
.tf
.cmd
[TF_TFACTOR_0
+ unit
] != envColor
) {
303 R200_STATECHANGE( rmesa
, tf
);
304 rmesa
->hw
.tf
.cmd
[TF_TFACTOR_0
+ unit
] = envColor
;
309 case GL_TEXTURE_LOD_BIAS_EXT
: {
312 const int fixed_one
= 0x8000000;
314 /* The R200's LOD bias is a signed 2's complement value with a
315 * range of -16.0 <= bias < 16.0.
317 * NOTE: Add a small bias to the bias for conform mipsel.c test.
320 min
= driQueryOptionb (&rmesa
->radeon
.optionCache
, "no_neg_lod_bias") ?
322 bias
= CLAMP( bias
, min
, 16.0 );
323 b
= (int)(bias
* fixed_one
) & R200_LOD_BIAS_MASK
;
325 if ( (rmesa
->hw
.tex
[unit
].cmd
[TEX_PP_TXFORMAT_X
] & R200_LOD_BIAS_MASK
) != b
) {
326 R200_STATECHANGE( rmesa
, tex
[unit
] );
327 rmesa
->hw
.tex
[unit
].cmd
[TEX_PP_TXFORMAT_X
] &= ~R200_LOD_BIAS_MASK
;
328 rmesa
->hw
.tex
[unit
].cmd
[TEX_PP_TXFORMAT_X
] |= b
;
332 case GL_COORD_REPLACE_ARB
:
333 if (ctx
->Point
.PointSprite
) {
334 R200_STATECHANGE( rmesa
, spr
);
335 if ((GLenum
)param
[0]) {
336 rmesa
->hw
.spr
.cmd
[SPR_POINT_SPRITE_CNTL
] |= R200_PS_GEN_TEX_0
<< unit
;
338 rmesa
->hw
.spr
.cmd
[SPR_POINT_SPRITE_CNTL
] &= ~(R200_PS_GEN_TEX_0
<< unit
);
349 * Changes variables and flags for a state update, which will happen at the
350 * next UpdateTextureState
353 static void r200TexParameter( GLcontext
*ctx
, GLenum target
,
354 struct gl_texture_object
*texObj
,
355 GLenum pname
, const GLfloat
*params
)
357 radeonTexObj
* t
= radeon_tex_obj(texObj
);
359 if ( R200_DEBUG
& (RADEON_STATE
|RADEON_TEXTURE
) ) {
360 fprintf( stderr
, "%s( %s )\n", __FUNCTION__
,
361 _mesa_lookup_enum_by_nr( pname
) );
365 case GL_TEXTURE_MIN_FILTER
:
366 case GL_TEXTURE_MAG_FILTER
:
367 case GL_TEXTURE_MAX_ANISOTROPY_EXT
:
368 r200SetTexMaxAnisotropy( t
, texObj
->MaxAnisotropy
);
369 r200SetTexFilter( t
, texObj
->MinFilter
, texObj
->MagFilter
);
372 case GL_TEXTURE_WRAP_S
:
373 case GL_TEXTURE_WRAP_T
:
374 case GL_TEXTURE_WRAP_R
:
375 r200SetTexWrap( t
, texObj
->WrapS
, texObj
->WrapT
, texObj
->WrapR
);
378 case GL_TEXTURE_BORDER_COLOR
:
379 r200SetTexBorderColor( t
, texObj
->BorderColor
.f
);
382 case GL_TEXTURE_BASE_LEVEL
:
383 case GL_TEXTURE_MAX_LEVEL
:
384 case GL_TEXTURE_MIN_LOD
:
385 case GL_TEXTURE_MAX_LOD
:
386 t
->validated
= GL_FALSE
;
395 static void r200DeleteTexture(GLcontext
* ctx
, struct gl_texture_object
*texObj
)
397 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
398 radeonTexObj
* t
= radeon_tex_obj(texObj
);
400 if (RADEON_DEBUG
& (RADEON_STATE
| RADEON_TEXTURE
)) {
401 fprintf(stderr
, "%s( %p (target = %s) )\n", __FUNCTION__
,
403 _mesa_lookup_enum_by_nr(texObj
->Target
));
408 radeon_firevertices(&rmesa
->radeon
);
409 for ( i
= 0 ; i
< rmesa
->radeon
.glCtx
->Const
.MaxTextureUnits
; i
++ ) {
410 if ( t
== rmesa
->state
.texture
.unit
[i
].texobj
) {
411 rmesa
->state
.texture
.unit
[i
].texobj
= NULL
;
412 rmesa
->hw
.tex
[i
].dirty
= GL_FALSE
;
413 rmesa
->hw
.cube
[i
].dirty
= GL_FALSE
;
418 radeon_miptree_unreference(&t
->mt
);
420 _mesa_delete_texture_object(ctx
, texObj
);
424 * - Same GEN_MODE for all active bits
425 * - Same EyePlane/ObjPlane for all active bits when using Eye/Obj
426 * - STRQ presumably all supported (matrix means incoming R values
427 * can end up in STQ, this has implications for vertex support,
428 * presumably ok if maos is used, though?)
430 * Basically impossible to do this on the fly - just collect some
431 * basic info & do the checks from ValidateState().
433 static void r200TexGen( GLcontext
*ctx
,
436 const GLfloat
*params
)
438 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
439 GLuint unit
= ctx
->Texture
.CurrentUnit
;
440 rmesa
->recheck_texgen
[unit
] = GL_TRUE
;
445 * Allocate a new texture object.
446 * Called via ctx->Driver.NewTextureObject.
447 * Note: this function will be called during context creation to
448 * allocate the default texture objects.
449 * Fixup MaxAnisotropy according to user preference.
451 static struct gl_texture_object
*r200NewTextureObject(GLcontext
* ctx
,
455 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
456 radeonTexObj
* t
= CALLOC_STRUCT(radeon_tex_obj
);
459 if (RADEON_DEBUG
& (RADEON_STATE
| RADEON_TEXTURE
)) {
460 fprintf(stderr
, "%s( %p (target = %s) )\n", __FUNCTION__
,
461 t
, _mesa_lookup_enum_by_nr(target
));
464 _mesa_initialize_texture_object(&t
->base
, name
, target
);
465 t
->base
.MaxAnisotropy
= rmesa
->radeon
.initialMaxAnisotropy
;
467 /* Initialize hardware state */
468 r200SetTexWrap( t
, t
->base
.WrapS
, t
->base
.WrapT
, t
->base
.WrapR
);
469 r200SetTexMaxAnisotropy( t
, t
->base
.MaxAnisotropy
);
470 r200SetTexFilter(t
, t
->base
.MinFilter
, t
->base
.MagFilter
);
471 r200SetTexBorderColor(t
, t
->base
.BorderColor
.f
);
478 void r200InitTextureFuncs( radeonContextPtr radeon
, struct dd_function_table
*functions
)
480 /* Note: we only plug in the functions we implement in the driver
481 * since _mesa_init_driver_functions() was already called.
483 functions
->ChooseTextureFormat
= radeonChooseTextureFormat_mesa
;
484 functions
->TexImage1D
= radeonTexImage1D
;
485 functions
->TexImage2D
= radeonTexImage2D
;
486 #if ENABLE_HW_3D_TEXTURE
487 functions
->TexImage3D
= radeonTexImage3D
;
489 functions
->TexImage3D
= _mesa_store_teximage3d
;
491 functions
->TexSubImage1D
= radeonTexSubImage1D
;
492 functions
->TexSubImage2D
= radeonTexSubImage2D
;
493 #if ENABLE_HW_3D_TEXTURE
494 functions
->TexSubImage3D
= radeonTexSubImage3D
;
496 functions
->TexSubImage3D
= _mesa_store_texsubimage3d
;
498 functions
->GetTexImage
= radeonGetTexImage
;
499 functions
->GetCompressedTexImage
= radeonGetCompressedTexImage
;
500 functions
->NewTextureObject
= r200NewTextureObject
;
501 // functions->BindTexture = r200BindTexture;
502 functions
->DeleteTexture
= r200DeleteTexture
;
503 functions
->IsTextureResident
= driIsTextureResident
;
505 functions
->TexEnv
= r200TexEnv
;
506 functions
->TexParameter
= r200TexParameter
;
507 functions
->TexGen
= r200TexGen
;
509 functions
->CompressedTexImage2D
= radeonCompressedTexImage2D
;
510 functions
->CompressedTexSubImage2D
= radeonCompressedTexSubImage2D
;
512 if (radeon
->radeonScreen
->kernel_mm
) {
513 functions
->CopyTexImage2D
= radeonCopyTexImage2D
;
514 functions
->CopyTexSubImage2D
= radeonCopyTexSubImage2D
;
517 functions
->GenerateMipmap
= radeonGenerateMipmap
;
519 functions
->NewTextureImage
= radeonNewTextureImage
;
520 functions
->FreeTexImageData
= radeonFreeTexImageData
;
521 functions
->MapTexture
= radeonMapTexture
;
522 functions
->UnmapTexture
= radeonUnmapTexture
;
524 driInitTextureFormats();