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