Merge remote branch 'origin/lp-binning'
[mesa.git] / src / mesa / drivers / dri / r200 / r200_tex.c
1 /*
2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3
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.
7
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:
15
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.
19
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.
27 */
28
29 /*
30 * Authors:
31 * Keith Whitwell <keith@tungstengraphics.com>
32 */
33
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"
44
45 #include "radeon_mipmap_tree.h"
46 #include "r200_context.h"
47 #include "r200_ioctl.h"
48 #include "r200_tex.h"
49
50 #include "xmlpool.h"
51
52
53
54 /**
55 * Set the texture wrap modes.
56 *
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
60 */
61
62 static void r200SetTexWrap( radeonTexObjPtr t, GLenum swrap, GLenum twrap, GLenum rwrap )
63 {
64 GLboolean is_clamp = GL_FALSE;
65 GLboolean is_clamp_to_border = GL_FALSE;
66 struct gl_texture_object *tObj = &t->base;
67
68 t->pp_txfilter &= ~(R200_CLAMP_S_MASK | R200_CLAMP_T_MASK | R200_BORDER_MODE_D3D);
69
70 switch ( swrap ) {
71 case GL_REPEAT:
72 t->pp_txfilter |= R200_CLAMP_S_WRAP;
73 break;
74 case GL_CLAMP:
75 t->pp_txfilter |= R200_CLAMP_S_CLAMP_GL;
76 is_clamp = GL_TRUE;
77 break;
78 case GL_CLAMP_TO_EDGE:
79 t->pp_txfilter |= R200_CLAMP_S_CLAMP_LAST;
80 break;
81 case GL_CLAMP_TO_BORDER:
82 t->pp_txfilter |= R200_CLAMP_S_CLAMP_GL;
83 is_clamp_to_border = GL_TRUE;
84 break;
85 case GL_MIRRORED_REPEAT:
86 t->pp_txfilter |= R200_CLAMP_S_MIRROR;
87 break;
88 case GL_MIRROR_CLAMP_EXT:
89 t->pp_txfilter |= R200_CLAMP_S_MIRROR_CLAMP_GL;
90 is_clamp = GL_TRUE;
91 break;
92 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
93 t->pp_txfilter |= R200_CLAMP_S_MIRROR_CLAMP_LAST;
94 break;
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;
98 break;
99 default:
100 _mesa_problem(NULL, "bad S wrap mode in %s", __FUNCTION__);
101 }
102
103 if (tObj->Target != GL_TEXTURE_1D) {
104 switch ( twrap ) {
105 case GL_REPEAT:
106 t->pp_txfilter |= R200_CLAMP_T_WRAP;
107 break;
108 case GL_CLAMP:
109 t->pp_txfilter |= R200_CLAMP_T_CLAMP_GL;
110 is_clamp = GL_TRUE;
111 break;
112 case GL_CLAMP_TO_EDGE:
113 t->pp_txfilter |= R200_CLAMP_T_CLAMP_LAST;
114 break;
115 case GL_CLAMP_TO_BORDER:
116 t->pp_txfilter |= R200_CLAMP_T_CLAMP_GL;
117 is_clamp_to_border = GL_TRUE;
118 break;
119 case GL_MIRRORED_REPEAT:
120 t->pp_txfilter |= R200_CLAMP_T_MIRROR;
121 break;
122 case GL_MIRROR_CLAMP_EXT:
123 t->pp_txfilter |= R200_CLAMP_T_MIRROR_CLAMP_GL;
124 is_clamp = GL_TRUE;
125 break;
126 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
127 t->pp_txfilter |= R200_CLAMP_T_MIRROR_CLAMP_LAST;
128 break;
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;
132 break;
133 default:
134 _mesa_problem(NULL, "bad T wrap mode in %s", __FUNCTION__);
135 }
136 }
137
138 t->pp_txformat_x &= ~R200_CLAMP_Q_MASK;
139
140 switch ( rwrap ) {
141 case GL_REPEAT:
142 t->pp_txformat_x |= R200_CLAMP_Q_WRAP;
143 break;
144 case GL_CLAMP:
145 t->pp_txformat_x |= R200_CLAMP_Q_CLAMP_GL;
146 is_clamp = GL_TRUE;
147 break;
148 case GL_CLAMP_TO_EDGE:
149 t->pp_txformat_x |= R200_CLAMP_Q_CLAMP_LAST;
150 break;
151 case GL_CLAMP_TO_BORDER:
152 t->pp_txformat_x |= R200_CLAMP_Q_CLAMP_GL;
153 is_clamp_to_border = GL_TRUE;
154 break;
155 case GL_MIRRORED_REPEAT:
156 t->pp_txformat_x |= R200_CLAMP_Q_MIRROR;
157 break;
158 case GL_MIRROR_CLAMP_EXT:
159 t->pp_txformat_x |= R200_CLAMP_Q_MIRROR_CLAMP_GL;
160 is_clamp = GL_TRUE;
161 break;
162 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
163 t->pp_txformat_x |= R200_CLAMP_Q_MIRROR_CLAMP_LAST;
164 break;
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;
168 break;
169 default:
170 _mesa_problem(NULL, "bad R wrap mode in %s", __FUNCTION__);
171 }
172
173 if ( is_clamp_to_border ) {
174 t->pp_txfilter |= R200_BORDER_MODE_D3D;
175 }
176
177 t->border_fallback = (is_clamp && is_clamp_to_border);
178 }
179
180 static void r200SetTexMaxAnisotropy( radeonTexObjPtr t, GLfloat max )
181 {
182 t->pp_txfilter &= ~R200_MAX_ANISO_MASK;
183
184 if ( max <= 1.0 ) {
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;
192 } else {
193 t->pp_txfilter |= R200_MAX_ANISO_16_TO_1;
194 }
195 }
196
197 /**
198 * Set the texture magnification and minification modes.
199 *
200 * \param t Texture whose filter modes are to be set
201 * \param minf Texture minification mode
202 * \param magf Texture magnification mode
203 */
204
205 static void r200SetTexFilter( radeonTexObjPtr t, GLenum minf, GLenum magf )
206 {
207 GLuint anisotropy = (t->pp_txfilter & R200_MAX_ANISO_MASK);
208
209 /* Force revalidation to account for switches from/to mipmapping. */
210 t->validated = GL_FALSE;
211
212 t->pp_txfilter &= ~(R200_MIN_FILTER_MASK | R200_MAG_FILTER_MASK);
213 t->pp_txformat_x &= ~R200_VOLUME_FILTER_MASK;
214
215 if ( anisotropy == R200_MAX_ANISO_1_TO_1 ) {
216 switch ( minf ) {
217 case GL_NEAREST:
218 t->pp_txfilter |= R200_MIN_FILTER_NEAREST;
219 break;
220 case GL_LINEAR:
221 t->pp_txfilter |= R200_MIN_FILTER_LINEAR;
222 break;
223 case GL_NEAREST_MIPMAP_NEAREST:
224 t->pp_txfilter |= R200_MIN_FILTER_NEAREST_MIP_NEAREST;
225 break;
226 case GL_NEAREST_MIPMAP_LINEAR:
227 t->pp_txfilter |= R200_MIN_FILTER_LINEAR_MIP_NEAREST;
228 break;
229 case GL_LINEAR_MIPMAP_NEAREST:
230 t->pp_txfilter |= R200_MIN_FILTER_NEAREST_MIP_LINEAR;
231 break;
232 case GL_LINEAR_MIPMAP_LINEAR:
233 t->pp_txfilter |= R200_MIN_FILTER_LINEAR_MIP_LINEAR;
234 break;
235 }
236 } else {
237 switch ( minf ) {
238 case GL_NEAREST:
239 t->pp_txfilter |= R200_MIN_FILTER_ANISO_NEAREST;
240 break;
241 case GL_LINEAR:
242 t->pp_txfilter |= R200_MIN_FILTER_ANISO_LINEAR;
243 break;
244 case GL_NEAREST_MIPMAP_NEAREST:
245 case GL_LINEAR_MIPMAP_NEAREST:
246 t->pp_txfilter |= R200_MIN_FILTER_ANISO_NEAREST_MIP_NEAREST;
247 break;
248 case GL_NEAREST_MIPMAP_LINEAR:
249 case GL_LINEAR_MIPMAP_LINEAR:
250 t->pp_txfilter |= R200_MIN_FILTER_ANISO_NEAREST_MIP_LINEAR;
251 break;
252 }
253 }
254
255 /* Note we don't have 3D mipmaps so only use the mag filter setting
256 * to set the 3D texture filter mode.
257 */
258 switch ( magf ) {
259 case GL_NEAREST:
260 t->pp_txfilter |= R200_MAG_FILTER_NEAREST;
261 t->pp_txformat_x |= R200_VOLUME_FILTER_NEAREST;
262 break;
263 case GL_LINEAR:
264 t->pp_txfilter |= R200_MAG_FILTER_LINEAR;
265 t->pp_txformat_x |= R200_VOLUME_FILTER_LINEAR;
266 break;
267 }
268 }
269
270 static void r200SetTexBorderColor( radeonTexObjPtr t, const GLfloat color[4] )
271 {
272 GLubyte c[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] );
278 }
279
280 static void r200TexEnv( GLcontext *ctx, GLenum target,
281 GLenum pname, const GLfloat *param )
282 {
283 r200ContextPtr rmesa = R200_CONTEXT(ctx);
284 GLuint unit = ctx->Texture.CurrentUnit;
285 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
286
287 if ( R200_DEBUG & RADEON_STATE ) {
288 fprintf( stderr, "%s( %s )\n",
289 __FUNCTION__, _mesa_lookup_enum_by_nr( pname ) );
290 }
291
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.
295 */
296 switch ( pname ) {
297 case GL_TEXTURE_ENV_COLOR: {
298 GLubyte c[4];
299 GLuint envColor;
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;
305 }
306 break;
307 }
308
309 case GL_TEXTURE_LOD_BIAS_EXT: {
310 GLfloat bias, min;
311 GLuint b;
312 const int fixed_one = 0x8000000;
313
314 /* The R200's LOD bias is a signed 2's complement value with a
315 * range of -16.0 <= bias < 16.0.
316 *
317 * NOTE: Add a small bias to the bias for conform mipsel.c test.
318 */
319 bias = *param + .01;
320 min = driQueryOptionb (&rmesa->radeon.optionCache, "no_neg_lod_bias") ?
321 0.0 : -16.0;
322 bias = CLAMP( bias, min, 16.0 );
323 b = (int)(bias * fixed_one) & R200_LOD_BIAS_MASK;
324
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;
329 }
330 break;
331 }
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;
337 } else {
338 rmesa->hw.spr.cmd[SPR_POINT_SPRITE_CNTL] &= ~(R200_PS_GEN_TEX_0 << unit);
339 }
340 }
341 break;
342 default:
343 return;
344 }
345 }
346
347
348 /**
349 * Changes variables and flags for a state update, which will happen at the
350 * next UpdateTextureState
351 */
352
353 static void r200TexParameter( GLcontext *ctx, GLenum target,
354 struct gl_texture_object *texObj,
355 GLenum pname, const GLfloat *params )
356 {
357 radeonTexObj* t = radeon_tex_obj(texObj);
358
359 if ( R200_DEBUG & (RADEON_STATE|RADEON_TEXTURE) ) {
360 fprintf( stderr, "%s( %s )\n", __FUNCTION__,
361 _mesa_lookup_enum_by_nr( pname ) );
362 }
363
364 switch ( 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 );
370 break;
371
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 );
376 break;
377
378 case GL_TEXTURE_BORDER_COLOR:
379 r200SetTexBorderColor( t, texObj->BorderColor.f );
380 break;
381
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;
387 break;
388
389 default:
390 return;
391 }
392 }
393
394
395 static void r200DeleteTexture(GLcontext * ctx, struct gl_texture_object *texObj)
396 {
397 r200ContextPtr rmesa = R200_CONTEXT(ctx);
398 radeonTexObj* t = radeon_tex_obj(texObj);
399
400 if (RADEON_DEBUG & (RADEON_STATE | RADEON_TEXTURE)) {
401 fprintf(stderr, "%s( %p (target = %s) )\n", __FUNCTION__,
402 (void *)texObj,
403 _mesa_lookup_enum_by_nr(texObj->Target));
404 }
405
406 if (rmesa) {
407 int i;
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;
414 }
415 }
416 }
417
418 radeon_miptree_unreference(&t->mt);
419
420 _mesa_delete_texture_object(ctx, texObj);
421 }
422
423 /* Need:
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?)
429 *
430 * Basically impossible to do this on the fly - just collect some
431 * basic info & do the checks from ValidateState().
432 */
433 static void r200TexGen( GLcontext *ctx,
434 GLenum coord,
435 GLenum pname,
436 const GLfloat *params )
437 {
438 r200ContextPtr rmesa = R200_CONTEXT(ctx);
439 GLuint unit = ctx->Texture.CurrentUnit;
440 rmesa->recheck_texgen[unit] = GL_TRUE;
441 }
442
443
444 /**
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.
450 */
451 static struct gl_texture_object *r200NewTextureObject(GLcontext * ctx,
452 GLuint name,
453 GLenum target)
454 {
455 r200ContextPtr rmesa = R200_CONTEXT(ctx);
456 radeonTexObj* t = CALLOC_STRUCT(radeon_tex_obj);
457
458
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));
462 }
463
464 _mesa_initialize_texture_object(&t->base, name, target);
465 t->base.MaxAnisotropy = rmesa->radeon.initialMaxAnisotropy;
466
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);
472
473 return &t->base;
474 }
475
476
477
478 void r200InitTextureFuncs( radeonContextPtr radeon, struct dd_function_table *functions )
479 {
480 /* Note: we only plug in the functions we implement in the driver
481 * since _mesa_init_driver_functions() was already called.
482 */
483 functions->ChooseTextureFormat = radeonChooseTextureFormat_mesa;
484 functions->TexImage1D = radeonTexImage1D;
485 functions->TexImage2D = radeonTexImage2D;
486 #if ENABLE_HW_3D_TEXTURE
487 functions->TexImage3D = radeonTexImage3D;
488 #else
489 functions->TexImage3D = _mesa_store_teximage3d;
490 #endif
491 functions->TexSubImage1D = radeonTexSubImage1D;
492 functions->TexSubImage2D = radeonTexSubImage2D;
493 #if ENABLE_HW_3D_TEXTURE
494 functions->TexSubImage3D = radeonTexSubImage3D;
495 #else
496 functions->TexSubImage3D = _mesa_store_texsubimage3d;
497 #endif
498 functions->GetTexImage = radeonGetTexImage;
499 functions->GetCompressedTexImage = radeonGetCompressedTexImage;
500 functions->NewTextureObject = r200NewTextureObject;
501 // functions->BindTexture = r200BindTexture;
502 functions->DeleteTexture = r200DeleteTexture;
503 functions->IsTextureResident = driIsTextureResident;
504
505 functions->TexEnv = r200TexEnv;
506 functions->TexParameter = r200TexParameter;
507 functions->TexGen = r200TexGen;
508
509 functions->CompressedTexImage2D = radeonCompressedTexImage2D;
510 functions->CompressedTexSubImage2D = radeonCompressedTexSubImage2D;
511
512 if (radeon->radeonScreen->kernel_mm) {
513 functions->CopyTexImage2D = radeonCopyTexImage2D;
514 functions->CopyTexSubImage2D = radeonCopyTexSubImage2D;
515 }
516
517 functions->GenerateMipmap = radeonGenerateMipmap;
518
519 functions->NewTextureImage = radeonNewTextureImage;
520 functions->FreeTexImageData = radeonFreeTexImageData;
521 functions->MapTexture = radeonMapTexture;
522 functions->UnmapTexture = radeonUnmapTexture;
523
524 driInitTextureFormats();
525
526 }