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