Merge remote branch 'origin/mesa_7_7_branch'
[mesa.git] / src / mesa / drivers / dri / r300 / r300_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 * \file
31 *
32 * \author Keith Whitwell <keith@tungstengraphics.com>
33 */
34
35 #include "main/glheader.h"
36 #include "main/imports.h"
37 #include "main/colormac.h"
38 #include "main/context.h"
39 #include "main/enums.h"
40 #include "main/image.h"
41 #include "main/mipmap.h"
42 #include "main/simple_list.h"
43 #include "main/texstore.h"
44 #include "main/teximage.h"
45 #include "main/texobj.h"
46
47 #include "texmem.h"
48
49 #include "r300_context.h"
50 #include "r300_state.h"
51 #include "radeon_mipmap_tree.h"
52 #include "r300_tex.h"
53
54 #include "xmlpool.h"
55
56
57 static unsigned int translate_wrap_mode(GLenum wrapmode)
58 {
59 switch(wrapmode) {
60 case GL_REPEAT: return R300_TX_REPEAT;
61 case GL_CLAMP: return R300_TX_CLAMP;
62 case GL_CLAMP_TO_EDGE: return R300_TX_CLAMP_TO_EDGE;
63 case GL_CLAMP_TO_BORDER: return R300_TX_CLAMP_TO_BORDER;
64 case GL_MIRRORED_REPEAT: return R300_TX_REPEAT | R300_TX_MIRRORED;
65 case GL_MIRROR_CLAMP_EXT: return R300_TX_CLAMP | R300_TX_MIRRORED;
66 case GL_MIRROR_CLAMP_TO_EDGE_EXT: return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
67 case GL_MIRROR_CLAMP_TO_BORDER_EXT: return R300_TX_CLAMP_TO_BORDER | R300_TX_MIRRORED;
68 default:
69 _mesa_problem(NULL, "bad wrap mode in %s", __FUNCTION__);
70 return 0;
71 }
72 }
73
74
75 /**
76 * Update the cached hardware registers based on the current texture wrap modes.
77 *
78 * \param t Texture object whose wrap modes are to be set
79 */
80 static void r300UpdateTexWrap(radeonTexObjPtr t)
81 {
82 struct gl_texture_object *tObj = &t->base;
83
84 t->pp_txfilter &=
85 ~(R300_TX_WRAP_S_MASK | R300_TX_WRAP_T_MASK | R300_TX_WRAP_R_MASK);
86
87 t->pp_txfilter |= translate_wrap_mode(tObj->WrapS) << R300_TX_WRAP_S_SHIFT;
88
89 if (tObj->Target != GL_TEXTURE_1D) {
90 t->pp_txfilter |= translate_wrap_mode(tObj->WrapT) << R300_TX_WRAP_T_SHIFT;
91
92 if (tObj->Target == GL_TEXTURE_3D)
93 t->pp_txfilter |= translate_wrap_mode(tObj->WrapR) << R300_TX_WRAP_R_SHIFT;
94 }
95 }
96
97 static GLuint aniso_filter(GLfloat anisotropy)
98 {
99 if (anisotropy >= 16.0) {
100 return R300_TX_MAX_ANISO_16_TO_1;
101 } else if (anisotropy >= 8.0) {
102 return R300_TX_MAX_ANISO_8_TO_1;
103 } else if (anisotropy >= 4.0) {
104 return R300_TX_MAX_ANISO_4_TO_1;
105 } else if (anisotropy >= 2.0) {
106 return R300_TX_MAX_ANISO_2_TO_1;
107 } else {
108 return R300_TX_MAX_ANISO_1_TO_1;
109 }
110 }
111
112 /**
113 * Set the texture magnification and minification modes.
114 *
115 * \param t Texture whose filter modes are to be set
116 * \param minf Texture minification mode
117 * \param magf Texture magnification mode
118 * \param anisotropy Maximum anisotropy level
119 */
120 static void r300SetTexFilter(radeonTexObjPtr t, GLenum minf, GLenum magf, GLfloat anisotropy)
121 {
122 /* Force revalidation to account for switches from/to mipmapping. */
123 t->validated = GL_FALSE;
124
125 t->pp_txfilter &= ~(R300_TX_MIN_FILTER_MASK | R300_TX_MIN_FILTER_MIP_MASK | R300_TX_MAG_FILTER_MASK | R300_TX_MAX_ANISO_MASK);
126 t->pp_txfilter_1 &= ~R300_EDGE_ANISO_EDGE_ONLY;
127
128 /* Note that EXT_texture_filter_anisotropic is extremely vague about
129 * how anisotropic filtering interacts with the "normal" filter modes.
130 * When anisotropic filtering is enabled, we override min and mag
131 * filter settings completely. This includes driconf's settings.
132 */
133 if (anisotropy >= 2.0 && (minf != GL_NEAREST) && (magf != GL_NEAREST)) {
134 t->pp_txfilter |= R300_TX_MAG_FILTER_ANISO
135 | R300_TX_MIN_FILTER_ANISO
136 | R300_TX_MIN_FILTER_MIP_LINEAR
137 | aniso_filter(anisotropy);
138 if (RADEON_DEBUG & RADEON_TEXTURE)
139 fprintf(stderr, "Using maximum anisotropy of %f\n", anisotropy);
140 return;
141 }
142
143 switch (minf) {
144 case GL_NEAREST:
145 t->pp_txfilter |= R300_TX_MIN_FILTER_NEAREST;
146 break;
147 case GL_LINEAR:
148 t->pp_txfilter |= R300_TX_MIN_FILTER_LINEAR;
149 break;
150 case GL_NEAREST_MIPMAP_NEAREST:
151 t->pp_txfilter |= R300_TX_MIN_FILTER_NEAREST|R300_TX_MIN_FILTER_MIP_NEAREST;
152 break;
153 case GL_NEAREST_MIPMAP_LINEAR:
154 t->pp_txfilter |= R300_TX_MIN_FILTER_NEAREST|R300_TX_MIN_FILTER_MIP_LINEAR;
155 break;
156 case GL_LINEAR_MIPMAP_NEAREST:
157 t->pp_txfilter |= R300_TX_MIN_FILTER_LINEAR|R300_TX_MIN_FILTER_MIP_NEAREST;
158 break;
159 case GL_LINEAR_MIPMAP_LINEAR:
160 t->pp_txfilter |= R300_TX_MIN_FILTER_LINEAR|R300_TX_MIN_FILTER_MIP_LINEAR;
161 break;
162 }
163
164 /* Note we don't have 3D mipmaps so only use the mag filter setting
165 * to set the 3D texture filter mode.
166 */
167 switch (magf) {
168 case GL_NEAREST:
169 t->pp_txfilter |= R300_TX_MAG_FILTER_NEAREST;
170 break;
171 case GL_LINEAR:
172 t->pp_txfilter |= R300_TX_MAG_FILTER_LINEAR;
173 break;
174 }
175 }
176
177 static void r300SetTexBorderColor(radeonTexObjPtr t, const GLfloat color[4])
178 {
179 GLubyte c[4];
180 CLAMPED_FLOAT_TO_UBYTE(c[0], color[0]);
181 CLAMPED_FLOAT_TO_UBYTE(c[1], color[1]);
182 CLAMPED_FLOAT_TO_UBYTE(c[2], color[2]);
183 CLAMPED_FLOAT_TO_UBYTE(c[3], color[3]);
184 t->pp_border_color = PACK_COLOR_8888(c[3], c[0], c[1], c[2]);
185 }
186
187 /**
188 * Changes variables and flags for a state update, which will happen at the
189 * next UpdateTextureState
190 */
191
192 static void r300TexParameter(GLcontext * ctx, GLenum target,
193 struct gl_texture_object *texObj,
194 GLenum pname, const GLfloat * params)
195 {
196 radeonTexObj* t = radeon_tex_obj(texObj);
197 GLenum texBaseFormat;
198
199 if (RADEON_DEBUG & (RADEON_STATE | RADEON_TEXTURE)) {
200 fprintf(stderr, "%s( %s )\n", __FUNCTION__,
201 _mesa_lookup_enum_by_nr(pname));
202 }
203
204 switch (pname) {
205 case GL_TEXTURE_MIN_FILTER:
206 case GL_TEXTURE_MAG_FILTER:
207 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
208 r300SetTexFilter(t, texObj->MinFilter, texObj->MagFilter, texObj->MaxAnisotropy);
209 break;
210
211 case GL_TEXTURE_WRAP_S:
212 case GL_TEXTURE_WRAP_T:
213 case GL_TEXTURE_WRAP_R:
214 r300UpdateTexWrap(t);
215 break;
216
217 case GL_TEXTURE_BORDER_COLOR:
218 r300SetTexBorderColor(t, texObj->BorderColor.f);
219 break;
220
221 case GL_TEXTURE_BASE_LEVEL:
222 case GL_TEXTURE_MAX_LEVEL:
223 case GL_TEXTURE_MIN_LOD:
224 case GL_TEXTURE_MAX_LOD:
225 t->validated = GL_FALSE;
226 break;
227
228 case GL_DEPTH_TEXTURE_MODE:
229 if (!texObj->Image[0][texObj->BaseLevel])
230 return;
231 texBaseFormat = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
232
233 if (texBaseFormat == GL_DEPTH_COMPONENT ||
234 texBaseFormat == GL_DEPTH_STENCIL) {
235 r300SetDepthTexMode(texObj);
236 break;
237 } else {
238 /* If the texture isn't a depth texture, changing this
239 * state won't cause any changes to the hardware.
240 * Don't force a flush of texture state.
241 */
242 return;
243 }
244
245 default:
246 return;
247 }
248 }
249
250 static void r300DeleteTexture(GLcontext * ctx, struct gl_texture_object *texObj)
251 {
252 r300ContextPtr rmesa = R300_CONTEXT(ctx);
253 radeonTexObj* t = radeon_tex_obj(texObj);
254
255 if (RADEON_DEBUG & (RADEON_STATE | RADEON_TEXTURE)) {
256 fprintf(stderr, "%s( %p (target = %s) )\n", __FUNCTION__,
257 (void *)texObj,
258 _mesa_lookup_enum_by_nr(texObj->Target));
259 }
260
261 if (rmesa) {
262 int i;
263 struct radeon_bo *bo;
264 bo = !t->mt ? t->bo : t->mt->bo;
265 if (bo && radeon_bo_is_referenced_by_cs(bo, rmesa->radeon.cmdbuf.cs)) {
266 radeon_firevertices(&rmesa->radeon);
267 }
268
269 for(i = 0; i < R300_MAX_TEXTURE_UNITS; ++i)
270 if (rmesa->hw.textures[i] == t)
271 rmesa->hw.textures[i] = 0;
272 }
273
274 if (t->bo) {
275 radeon_bo_unref(t->bo);
276 t->bo = NULL;
277 }
278
279 radeon_miptree_unreference(&t->mt);
280
281 _mesa_delete_texture_object(ctx, texObj);
282 }
283
284 /**
285 * Allocate a new texture object.
286 * Called via ctx->Driver.NewTextureObject.
287 * Note: this function will be called during context creation to
288 * allocate the default texture objects.
289 * Fixup MaxAnisotropy according to user preference.
290 */
291 static struct gl_texture_object *r300NewTextureObject(GLcontext * ctx,
292 GLuint name,
293 GLenum target)
294 {
295 r300ContextPtr rmesa = R300_CONTEXT(ctx);
296 radeonTexObj* t = CALLOC_STRUCT(radeon_tex_obj);
297
298
299 if (RADEON_DEBUG & (RADEON_STATE | RADEON_TEXTURE)) {
300 fprintf(stderr, "%s( %p (target = %s) )\n", __FUNCTION__,
301 t, _mesa_lookup_enum_by_nr(target));
302 }
303
304 _mesa_initialize_texture_object(&t->base, name, target);
305 t->base.MaxAnisotropy = rmesa->radeon.initialMaxAnisotropy;
306
307 /* Initialize hardware state */
308 r300UpdateTexWrap(t);
309 r300SetTexFilter(t, t->base.MinFilter, t->base.MagFilter, t->base.MaxAnisotropy);
310 r300SetTexBorderColor(t, t->base.BorderColor.f);
311
312 return &t->base;
313 }
314
315 void r300InitTextureFuncs(struct dd_function_table *functions)
316 {
317 /* Note: we only plug in the functions we implement in the driver
318 * since _mesa_init_driver_functions() was already called.
319 */
320 functions->NewTextureImage = radeonNewTextureImage;
321 functions->FreeTexImageData = radeonFreeTexImageData;
322 functions->MapTexture = radeonMapTexture;
323 functions->UnmapTexture = radeonUnmapTexture;
324
325 functions->ChooseTextureFormat = radeonChooseTextureFormat_mesa;
326 functions->TexImage1D = radeonTexImage1D;
327 functions->TexImage2D = radeonTexImage2D;
328 functions->TexImage3D = radeonTexImage3D;
329 functions->TexSubImage1D = radeonTexSubImage1D;
330 functions->TexSubImage2D = radeonTexSubImage2D;
331 functions->TexSubImage3D = radeonTexSubImage3D;
332 functions->GetTexImage = radeonGetTexImage;
333 functions->GetCompressedTexImage = radeonGetCompressedTexImage;
334 functions->NewTextureObject = r300NewTextureObject;
335 functions->DeleteTexture = r300DeleteTexture;
336 functions->IsTextureResident = driIsTextureResident;
337
338 functions->TexParameter = r300TexParameter;
339
340 functions->CompressedTexImage2D = radeonCompressedTexImage2D;
341 functions->CompressedTexSubImage2D = radeonCompressedTexSubImage2D;
342
343 functions->GenerateMipmap = radeonGenerateMipmap;
344
345 driInitTextureFormats();
346 }