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/mfeatures.h"
41 #include "main/simple_list.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/samplerobj.h"
46 #include "radeon_mipmap_tree.h"
47 #include "r200_context.h"
48 #include "r200_ioctl.h"
56 * Set the texture wrap modes.
58 * \param t Texture object whose wrap modes are to be set
59 * \param swrap Wrap mode for the \a s texture coordinate
60 * \param twrap Wrap mode for the \a t texture coordinate
63 static void r200SetTexWrap( radeonTexObjPtr t
, GLenum swrap
, GLenum twrap
, GLenum rwrap
)
65 GLboolean is_clamp
= GL_FALSE
;
66 GLboolean is_clamp_to_border
= GL_FALSE
;
67 struct gl_texture_object
*tObj
= &t
->base
;
69 radeon_print(RADEON_TEXTURE
, RADEON_TRACE
,
70 "%s(tex %p) sw %s, tw %s, rw %s\n",
72 _mesa_lookup_enum_by_nr(swrap
),
73 _mesa_lookup_enum_by_nr(twrap
),
74 _mesa_lookup_enum_by_nr(rwrap
));
76 t
->pp_txfilter
&= ~(R200_CLAMP_S_MASK
| R200_CLAMP_T_MASK
| R200_BORDER_MODE_D3D
);
80 t
->pp_txfilter
|= R200_CLAMP_S_WRAP
;
83 t
->pp_txfilter
|= R200_CLAMP_S_CLAMP_GL
;
86 case GL_CLAMP_TO_EDGE
:
87 t
->pp_txfilter
|= R200_CLAMP_S_CLAMP_LAST
;
89 case GL_CLAMP_TO_BORDER
:
90 t
->pp_txfilter
|= R200_CLAMP_S_CLAMP_GL
;
91 is_clamp_to_border
= GL_TRUE
;
93 case GL_MIRRORED_REPEAT
:
94 t
->pp_txfilter
|= R200_CLAMP_S_MIRROR
;
96 case GL_MIRROR_CLAMP_EXT
:
97 t
->pp_txfilter
|= R200_CLAMP_S_MIRROR_CLAMP_GL
;
100 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
101 t
->pp_txfilter
|= R200_CLAMP_S_MIRROR_CLAMP_LAST
;
103 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
104 t
->pp_txfilter
|= R200_CLAMP_S_MIRROR_CLAMP_GL
;
105 is_clamp_to_border
= GL_TRUE
;
108 _mesa_problem(NULL
, "bad S wrap mode in %s", __FUNCTION__
);
111 if (tObj
->Target
!= GL_TEXTURE_1D
) {
114 t
->pp_txfilter
|= R200_CLAMP_T_WRAP
;
117 t
->pp_txfilter
|= R200_CLAMP_T_CLAMP_GL
;
120 case GL_CLAMP_TO_EDGE
:
121 t
->pp_txfilter
|= R200_CLAMP_T_CLAMP_LAST
;
123 case GL_CLAMP_TO_BORDER
:
124 t
->pp_txfilter
|= R200_CLAMP_T_CLAMP_GL
;
125 is_clamp_to_border
= GL_TRUE
;
127 case GL_MIRRORED_REPEAT
:
128 t
->pp_txfilter
|= R200_CLAMP_T_MIRROR
;
130 case GL_MIRROR_CLAMP_EXT
:
131 t
->pp_txfilter
|= R200_CLAMP_T_MIRROR_CLAMP_GL
;
134 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
135 t
->pp_txfilter
|= R200_CLAMP_T_MIRROR_CLAMP_LAST
;
137 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
138 t
->pp_txfilter
|= R200_CLAMP_T_MIRROR_CLAMP_GL
;
139 is_clamp_to_border
= GL_TRUE
;
142 _mesa_problem(NULL
, "bad T wrap mode in %s", __FUNCTION__
);
146 t
->pp_txformat_x
&= ~R200_CLAMP_Q_MASK
;
150 t
->pp_txformat_x
|= R200_CLAMP_Q_WRAP
;
153 t
->pp_txformat_x
|= R200_CLAMP_Q_CLAMP_GL
;
156 case GL_CLAMP_TO_EDGE
:
157 t
->pp_txformat_x
|= R200_CLAMP_Q_CLAMP_LAST
;
159 case GL_CLAMP_TO_BORDER
:
160 t
->pp_txformat_x
|= R200_CLAMP_Q_CLAMP_GL
;
161 is_clamp_to_border
= GL_TRUE
;
163 case GL_MIRRORED_REPEAT
:
164 t
->pp_txformat_x
|= R200_CLAMP_Q_MIRROR
;
166 case GL_MIRROR_CLAMP_EXT
:
167 t
->pp_txformat_x
|= R200_CLAMP_Q_MIRROR_CLAMP_GL
;
170 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
171 t
->pp_txformat_x
|= R200_CLAMP_Q_MIRROR_CLAMP_LAST
;
173 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
174 t
->pp_txformat_x
|= R200_CLAMP_Q_MIRROR_CLAMP_GL
;
175 is_clamp_to_border
= GL_TRUE
;
178 _mesa_problem(NULL
, "bad R wrap mode in %s", __FUNCTION__
);
181 if ( is_clamp_to_border
) {
182 t
->pp_txfilter
|= R200_BORDER_MODE_D3D
;
185 t
->border_fallback
= (is_clamp
&& is_clamp_to_border
);
188 static void r200SetTexMaxAnisotropy( radeonTexObjPtr t
, GLfloat max
)
190 t
->pp_txfilter
&= ~R200_MAX_ANISO_MASK
;
191 radeon_print(RADEON_TEXTURE
, RADEON_TRACE
,
192 "%s(tex %p) max %f.\n",
196 t
->pp_txfilter
|= R200_MAX_ANISO_1_TO_1
;
197 } else if ( max
<= 2.0 ) {
198 t
->pp_txfilter
|= R200_MAX_ANISO_2_TO_1
;
199 } else if ( max
<= 4.0 ) {
200 t
->pp_txfilter
|= R200_MAX_ANISO_4_TO_1
;
201 } else if ( max
<= 8.0 ) {
202 t
->pp_txfilter
|= R200_MAX_ANISO_8_TO_1
;
204 t
->pp_txfilter
|= R200_MAX_ANISO_16_TO_1
;
209 * Set the texture magnification and minification modes.
211 * \param t Texture whose filter modes are to be set
212 * \param minf Texture minification mode
213 * \param magf Texture magnification mode
216 static void r200SetTexFilter( radeonTexObjPtr t
, GLenum minf
, GLenum magf
)
218 GLuint anisotropy
= (t
->pp_txfilter
& R200_MAX_ANISO_MASK
);
220 /* Force revalidation to account for switches from/to mipmapping. */
221 t
->validated
= GL_FALSE
;
223 t
->pp_txfilter
&= ~(R200_MIN_FILTER_MASK
| R200_MAG_FILTER_MASK
);
224 t
->pp_txformat_x
&= ~R200_VOLUME_FILTER_MASK
;
226 radeon_print(RADEON_TEXTURE
, RADEON_TRACE
,
227 "%s(tex %p) minf %s, maxf %s, anisotropy %d.\n",
229 _mesa_lookup_enum_by_nr(minf
),
230 _mesa_lookup_enum_by_nr(magf
),
233 if ( anisotropy
== R200_MAX_ANISO_1_TO_1
) {
236 t
->pp_txfilter
|= R200_MIN_FILTER_NEAREST
;
239 t
->pp_txfilter
|= R200_MIN_FILTER_LINEAR
;
241 case GL_NEAREST_MIPMAP_NEAREST
:
242 t
->pp_txfilter
|= R200_MIN_FILTER_NEAREST_MIP_NEAREST
;
244 case GL_NEAREST_MIPMAP_LINEAR
:
245 t
->pp_txfilter
|= R200_MIN_FILTER_LINEAR_MIP_NEAREST
;
247 case GL_LINEAR_MIPMAP_NEAREST
:
248 t
->pp_txfilter
|= R200_MIN_FILTER_NEAREST_MIP_LINEAR
;
250 case GL_LINEAR_MIPMAP_LINEAR
:
251 t
->pp_txfilter
|= R200_MIN_FILTER_LINEAR_MIP_LINEAR
;
257 t
->pp_txfilter
|= R200_MIN_FILTER_ANISO_NEAREST
;
260 t
->pp_txfilter
|= R200_MIN_FILTER_ANISO_LINEAR
;
262 case GL_NEAREST_MIPMAP_NEAREST
:
263 case GL_LINEAR_MIPMAP_NEAREST
:
264 t
->pp_txfilter
|= R200_MIN_FILTER_ANISO_NEAREST_MIP_NEAREST
;
266 case GL_NEAREST_MIPMAP_LINEAR
:
267 case GL_LINEAR_MIPMAP_LINEAR
:
268 t
->pp_txfilter
|= R200_MIN_FILTER_ANISO_NEAREST_MIP_LINEAR
;
273 /* Note we don't have 3D mipmaps so only use the mag filter setting
274 * to set the 3D texture filter mode.
278 t
->pp_txfilter
|= R200_MAG_FILTER_NEAREST
;
279 t
->pp_txformat_x
|= R200_VOLUME_FILTER_NEAREST
;
282 t
->pp_txfilter
|= R200_MAG_FILTER_LINEAR
;
283 t
->pp_txformat_x
|= R200_VOLUME_FILTER_LINEAR
;
288 static void r200SetTexBorderColor( radeonTexObjPtr t
, const GLfloat color
[4] )
291 CLAMPED_FLOAT_TO_UBYTE(c
[0], color
[0]);
292 CLAMPED_FLOAT_TO_UBYTE(c
[1], color
[1]);
293 CLAMPED_FLOAT_TO_UBYTE(c
[2], color
[2]);
294 CLAMPED_FLOAT_TO_UBYTE(c
[3], color
[3]);
295 t
->pp_border_color
= radeonPackColor( 4, c
[0], c
[1], c
[2], c
[3] );
298 static void r200TexEnv( struct gl_context
*ctx
, GLenum target
,
299 GLenum pname
, const GLfloat
*param
)
301 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
302 GLuint unit
= ctx
->Texture
.CurrentUnit
;
303 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
305 radeon_print(RADEON_TEXTURE
| RADEON_STATE
, RADEON_VERBOSE
, "%s( %s )\n",
306 __FUNCTION__
, _mesa_lookup_enum_by_nr( pname
) );
308 /* This is incorrect: Need to maintain this data for each of
309 * GL_TEXTURE_{123}D, GL_TEXTURE_RECTANGLE_NV, etc, and switch
310 * between them according to _ReallyEnabled.
313 case GL_TEXTURE_ENV_COLOR
: {
316 _mesa_unclamped_float_rgba_to_ubyte(c
, texUnit
->EnvColor
);
317 envColor
= radeonPackColor( 4, c
[0], c
[1], c
[2], c
[3] );
318 if ( rmesa
->hw
.tf
.cmd
[TF_TFACTOR_0
+ unit
] != envColor
) {
319 R200_STATECHANGE( rmesa
, tf
);
320 rmesa
->hw
.tf
.cmd
[TF_TFACTOR_0
+ unit
] = envColor
;
325 case GL_TEXTURE_LOD_BIAS_EXT
: {
328 const int fixed_one
= R200_LOD_BIAS_FIXED_ONE
;
330 /* The R200's LOD bias is a signed 2's complement value with a
331 * range of -16.0 <= bias < 16.0.
333 * NOTE: Add a small bias to the bias for conform mipsel.c test.
336 min
= driQueryOptionb (&rmesa
->radeon
.optionCache
, "no_neg_lod_bias") ?
338 bias
= CLAMP( bias
, min
, 16.0 );
339 b
= ((int)(bias
* fixed_one
)
340 + R200_LOD_BIAS_CORRECTION
) & R200_LOD_BIAS_MASK
;
342 if ( (rmesa
->hw
.tex
[unit
].cmd
[TEX_PP_TXFORMAT_X
] & R200_LOD_BIAS_MASK
) != b
) {
343 R200_STATECHANGE( rmesa
, tex
[unit
] );
344 rmesa
->hw
.tex
[unit
].cmd
[TEX_PP_TXFORMAT_X
] &= ~R200_LOD_BIAS_MASK
;
345 rmesa
->hw
.tex
[unit
].cmd
[TEX_PP_TXFORMAT_X
] |= b
;
349 case GL_COORD_REPLACE_ARB
:
350 if (ctx
->Point
.PointSprite
) {
351 R200_STATECHANGE( rmesa
, spr
);
352 if ((GLenum
)param
[0]) {
353 rmesa
->hw
.spr
.cmd
[SPR_POINT_SPRITE_CNTL
] |= R200_PS_GEN_TEX_0
<< unit
;
355 rmesa
->hw
.spr
.cmd
[SPR_POINT_SPRITE_CNTL
] &= ~(R200_PS_GEN_TEX_0
<< unit
);
364 void r200TexUpdateParameters(struct gl_context
*ctx
, GLuint unit
)
366 struct gl_sampler_object
*samp
= _mesa_get_samplerobj(ctx
, unit
);
367 radeonTexObj
* t
= radeon_tex_obj(ctx
->Texture
.Unit
[unit
]._Current
);
369 r200SetTexMaxAnisotropy(t
, samp
->MaxAnisotropy
);
370 r200SetTexFilter(t
, samp
->MinFilter
, samp
->MagFilter
);
371 r200SetTexWrap(t
, samp
->WrapS
, samp
->WrapT
, samp
->WrapR
);
372 r200SetTexBorderColor(t
, samp
->BorderColor
.f
);
376 * Changes variables and flags for a state update, which will happen at the
377 * next UpdateTextureState
379 static void r200TexParameter( struct gl_context
*ctx
, GLenum target
,
380 struct gl_texture_object
*texObj
,
381 GLenum pname
, const GLfloat
*params
)
383 radeonTexObj
* t
= radeon_tex_obj(texObj
);
385 radeon_print(RADEON_TEXTURE
| RADEON_STATE
, RADEON_VERBOSE
,
386 "%s(%p, tex %p) target %s, pname %s\n",
387 __FUNCTION__
, ctx
, texObj
,
388 _mesa_lookup_enum_by_nr( target
),
389 _mesa_lookup_enum_by_nr( pname
) );
392 case GL_TEXTURE_MIN_FILTER
:
393 case GL_TEXTURE_MAG_FILTER
:
394 case GL_TEXTURE_MAX_ANISOTROPY_EXT
:
395 case GL_TEXTURE_WRAP_S
:
396 case GL_TEXTURE_WRAP_T
:
397 case GL_TEXTURE_WRAP_R
:
398 case GL_TEXTURE_BORDER_COLOR
:
399 case GL_TEXTURE_BASE_LEVEL
:
400 case GL_TEXTURE_MAX_LEVEL
:
401 case GL_TEXTURE_MIN_LOD
:
402 case GL_TEXTURE_MAX_LOD
:
403 t
->validated
= GL_FALSE
;
412 static void r200DeleteTexture(struct gl_context
* ctx
, struct gl_texture_object
*texObj
)
414 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
415 radeonTexObj
* t
= radeon_tex_obj(texObj
);
417 radeon_print(RADEON_TEXTURE
| RADEON_STATE
, RADEON_NORMAL
,
418 "%s( %p (target = %s) )\n", __FUNCTION__
,
420 _mesa_lookup_enum_by_nr(texObj
->Target
));
424 radeon_firevertices(&rmesa
->radeon
);
425 for ( i
= 0 ; i
< rmesa
->radeon
.glCtx
.Const
.MaxTextureUnits
; i
++ ) {
426 if ( t
== rmesa
->state
.texture
.unit
[i
].texobj
) {
427 rmesa
->state
.texture
.unit
[i
].texobj
= NULL
;
428 rmesa
->hw
.tex
[i
].dirty
= GL_FALSE
;
429 rmesa
->hw
.cube
[i
].dirty
= GL_FALSE
;
434 radeon_miptree_unreference(&t
->mt
);
436 _mesa_delete_texture_object(ctx
, texObj
);
440 * - Same GEN_MODE for all active bits
441 * - Same EyePlane/ObjPlane for all active bits when using Eye/Obj
442 * - STRQ presumably all supported (matrix means incoming R values
443 * can end up in STQ, this has implications for vertex support,
444 * presumably ok if maos is used, though?)
446 * Basically impossible to do this on the fly - just collect some
447 * basic info & do the checks from ValidateState().
449 static void r200TexGen( struct gl_context
*ctx
,
452 const GLfloat
*params
)
454 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
455 GLuint unit
= ctx
->Texture
.CurrentUnit
;
456 rmesa
->recheck_texgen
[unit
] = GL_TRUE
;
461 * Allocate a new texture object.
462 * Called via ctx->Driver.NewTextureObject.
463 * Note: this function will be called during context creation to
464 * allocate the default texture objects.
465 * Fixup MaxAnisotropy according to user preference.
467 static struct gl_texture_object
*r200NewTextureObject(struct gl_context
* ctx
,
471 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
472 radeonTexObj
* t
= CALLOC_STRUCT(radeon_tex_obj
);
475 radeon_print(RADEON_STATE
| RADEON_TEXTURE
, RADEON_NORMAL
,
476 "%s(%p) target %s, new texture %p.\n",
478 _mesa_lookup_enum_by_nr(target
), t
);
480 _mesa_initialize_texture_object(&t
->base
, name
, target
);
481 t
->base
.Sampler
.MaxAnisotropy
= rmesa
->radeon
.initialMaxAnisotropy
;
483 /* Initialize hardware state */
484 r200SetTexWrap( t
, t
->base
.Sampler
.WrapS
, t
->base
.Sampler
.WrapT
, t
->base
.Sampler
.WrapR
);
485 r200SetTexMaxAnisotropy( t
, t
->base
.Sampler
.MaxAnisotropy
);
486 r200SetTexFilter(t
, t
->base
.Sampler
.MinFilter
, t
->base
.Sampler
.MagFilter
);
487 r200SetTexBorderColor(t
, t
->base
.Sampler
.BorderColor
.f
);
492 static struct gl_sampler_object
*
493 r200NewSamplerObject(struct gl_context
*ctx
, GLuint name
)
495 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
496 struct gl_sampler_object
*samp
= _mesa_new_sampler_object(ctx
, name
);
498 samp
->MaxAnisotropy
= rmesa
->radeon
.initialMaxAnisotropy
;
504 void r200InitTextureFuncs( radeonContextPtr radeon
, struct dd_function_table
*functions
)
506 /* Note: we only plug in the functions we implement in the driver
507 * since _mesa_init_driver_functions() was already called.
510 radeon_init_common_texture_funcs(radeon
, functions
);
512 functions
->NewTextureObject
= r200NewTextureObject
;
513 // functions->BindTexture = r200BindTexture;
514 functions
->DeleteTexture
= r200DeleteTexture
;
516 functions
->TexEnv
= r200TexEnv
;
517 functions
->TexParameter
= r200TexParameter
;
518 functions
->TexGen
= r200TexGen
;
519 functions
->NewSamplerObject
= r200NewSamplerObject
;