mesa: Remove build infrastructure for r300c and r600c.
[profile/ivi/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/mfeatures.h"
42 #include "main/mipmap.h"
43 #include "main/simple_list.h"
44 #include "main/texobj.h"
45
46 #include "texmem.h"
47
48 #include "r300_context.h"
49 #include "radeon_mipmap_tree.h"
50 #include "r300_tex.h"
51
52
53 static unsigned int translate_wrap_mode(GLenum wrapmode)
54 {
55         switch(wrapmode) {
56         case GL_REPEAT: return R300_TX_REPEAT;
57         case GL_CLAMP: return R300_TX_CLAMP;
58         case GL_CLAMP_TO_EDGE: return R300_TX_CLAMP_TO_EDGE;
59         case GL_CLAMP_TO_BORDER: return R300_TX_CLAMP_TO_BORDER;
60         case GL_MIRRORED_REPEAT: return R300_TX_REPEAT | R300_TX_MIRRORED;
61         case GL_MIRROR_CLAMP_EXT: return R300_TX_CLAMP | R300_TX_MIRRORED;
62         case GL_MIRROR_CLAMP_TO_EDGE_EXT: return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
63         case GL_MIRROR_CLAMP_TO_BORDER_EXT: return R300_TX_CLAMP_TO_BORDER | R300_TX_MIRRORED;
64         default:
65                 _mesa_problem(NULL, "bad wrap mode in %s", __FUNCTION__);
66                 return 0;
67         }
68 }
69
70
71 /**
72  * Update the cached hardware registers based on the current texture wrap modes.
73  *
74  * \param t Texture object whose wrap modes are to be set
75  */
76 static void r300UpdateTexWrap(radeonTexObjPtr t)
77 {
78         struct gl_texture_object *tObj = &t->base;
79
80         t->pp_txfilter &=
81             ~(R300_TX_WRAP_S_MASK | R300_TX_WRAP_T_MASK | R300_TX_WRAP_R_MASK);
82
83         t->pp_txfilter |= translate_wrap_mode(tObj->Sampler.WrapS) << R300_TX_WRAP_S_SHIFT;
84
85         if (tObj->Target != GL_TEXTURE_1D) {
86                 t->pp_txfilter |= translate_wrap_mode(tObj->Sampler.WrapT) << R300_TX_WRAP_T_SHIFT;
87
88                 if (tObj->Target == GL_TEXTURE_3D)
89                         t->pp_txfilter |= translate_wrap_mode(tObj->Sampler.WrapR) << R300_TX_WRAP_R_SHIFT;
90         }
91 }
92
93 static GLuint aniso_filter(GLfloat anisotropy)
94 {
95         if (anisotropy >= 16.0) {
96                 return R300_TX_MAX_ANISO_16_TO_1;
97         } else if (anisotropy >= 8.0) {
98                 return R300_TX_MAX_ANISO_8_TO_1;
99         } else if (anisotropy >= 4.0) {
100                 return R300_TX_MAX_ANISO_4_TO_1;
101         } else if (anisotropy >= 2.0) {
102                 return R300_TX_MAX_ANISO_2_TO_1;
103         } else {
104                 return R300_TX_MAX_ANISO_1_TO_1;
105         }
106 }
107
108 /**
109  * Set the texture magnification and minification modes.
110  *
111  * \param t Texture whose filter modes are to be set
112  * \param minf Texture minification mode
113  * \param magf Texture magnification mode
114  * \param anisotropy Maximum anisotropy level
115  */
116 static void r300SetTexFilter(radeonTexObjPtr t, GLenum minf, GLenum magf, GLfloat anisotropy)
117 {
118         /* Force revalidation to account for switches from/to mipmapping. */
119         t->validated = GL_FALSE;
120
121         t->pp_txfilter &= ~(R300_TX_MIN_FILTER_MASK | R300_TX_MIN_FILTER_MIP_MASK | R300_TX_MAG_FILTER_MASK | R300_TX_MAX_ANISO_MASK);
122         t->pp_txfilter_1 &= ~R300_EDGE_ANISO_EDGE_ONLY;
123
124         /* Note that EXT_texture_filter_anisotropic is extremely vague about
125          * how anisotropic filtering interacts with the "normal" filter modes.
126          * When anisotropic filtering is enabled, we override min and mag
127          * filter settings completely. This includes driconf's settings.
128          */
129         if (anisotropy >= 2.0 && (minf != GL_NEAREST) && (magf != GL_NEAREST)) {
130                 t->pp_txfilter |= R300_TX_MAG_FILTER_ANISO
131                         | R300_TX_MIN_FILTER_ANISO
132                         | R300_TX_MIN_FILTER_MIP_LINEAR
133                         | aniso_filter(anisotropy);
134                 if (RADEON_DEBUG & RADEON_TEXTURE)
135                         fprintf(stderr, "Using maximum anisotropy of %f\n", anisotropy);
136                 return;
137         }
138
139         switch (minf) {
140         case GL_NEAREST:
141                 t->pp_txfilter |= R300_TX_MIN_FILTER_NEAREST;
142                 break;
143         case GL_LINEAR:
144                 t->pp_txfilter |= R300_TX_MIN_FILTER_LINEAR;
145                 break;
146         case GL_NEAREST_MIPMAP_NEAREST:
147                 t->pp_txfilter |= R300_TX_MIN_FILTER_NEAREST|R300_TX_MIN_FILTER_MIP_NEAREST;
148                 break;
149         case GL_NEAREST_MIPMAP_LINEAR:
150                 t->pp_txfilter |= R300_TX_MIN_FILTER_NEAREST|R300_TX_MIN_FILTER_MIP_LINEAR;
151                 break;
152         case GL_LINEAR_MIPMAP_NEAREST:
153                 t->pp_txfilter |= R300_TX_MIN_FILTER_LINEAR|R300_TX_MIN_FILTER_MIP_NEAREST;
154                 break;
155         case GL_LINEAR_MIPMAP_LINEAR:
156                 t->pp_txfilter |= R300_TX_MIN_FILTER_LINEAR|R300_TX_MIN_FILTER_MIP_LINEAR;
157                 break;
158         }
159
160         /* Note we don't have 3D mipmaps so only use the mag filter setting
161          * to set the 3D texture filter mode.
162          */
163         switch (magf) {
164         case GL_NEAREST:
165                 t->pp_txfilter |= R300_TX_MAG_FILTER_NEAREST;
166                 break;
167         case GL_LINEAR:
168                 t->pp_txfilter |= R300_TX_MAG_FILTER_LINEAR;
169                 break;
170         }
171 }
172
173 static void r300SetTexBorderColor(radeonTexObjPtr t, const GLfloat color[4])
174 {
175         GLubyte c[4];
176         CLAMPED_FLOAT_TO_UBYTE(c[0], color[0]);
177         CLAMPED_FLOAT_TO_UBYTE(c[1], color[1]);
178         CLAMPED_FLOAT_TO_UBYTE(c[2], color[2]);
179         CLAMPED_FLOAT_TO_UBYTE(c[3], color[3]);
180         t->pp_border_color = PACK_COLOR_8888(c[3], c[0], c[1], c[2]);
181 }
182
183 /**
184  * Changes variables and flags for a state update, which will happen at the
185  * next UpdateTextureState
186  */
187
188 static void r300TexParameter(struct gl_context * ctx, GLenum target,
189                              struct gl_texture_object *texObj,
190                              GLenum pname, const GLfloat * params)
191 {
192         radeonTexObj* t = radeon_tex_obj(texObj);
193         GLenum texBaseFormat;
194
195         if (RADEON_DEBUG & (RADEON_STATE | RADEON_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->Sampler.MinFilter, texObj->Sampler.MagFilter, texObj->Sampler.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->Sampler.BorderColor.f);
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                 t->validated = GL_FALSE;
222                 break;
223
224         case GL_DEPTH_TEXTURE_MODE:
225                 if (!texObj->Image[0][texObj->BaseLevel])
226                         return;
227                 texBaseFormat = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
228
229                 if (texBaseFormat == GL_DEPTH_COMPONENT ||
230                         texBaseFormat == GL_DEPTH_STENCIL) {
231                         r300SetDepthTexMode(texObj);
232                         break;
233                 } else {
234                         /* If the texture isn't a depth texture, changing this
235                          * state won't cause any changes to the hardware.
236                          * Don't force a flush of texture state.
237                          */
238                         return;
239                 }
240
241         default:
242                 return;
243         }
244 }
245
246 static void r300DeleteTexture(struct gl_context * ctx, struct gl_texture_object *texObj)
247 {
248         r300ContextPtr rmesa = R300_CONTEXT(ctx);
249         radeonTexObj* t = radeon_tex_obj(texObj);
250
251         if (RADEON_DEBUG & (RADEON_STATE | RADEON_TEXTURE)) {
252                 fprintf(stderr, "%s( %p (target = %s) )\n", __FUNCTION__,
253                         (void *)texObj,
254                         _mesa_lookup_enum_by_nr(texObj->Target));
255         }
256
257         if (rmesa) {
258                 int i;
259                 struct radeon_bo *bo;
260                 bo = !t->mt ? t->bo : t->mt->bo;
261                 if (bo && radeon_bo_is_referenced_by_cs(bo, rmesa->radeon.cmdbuf.cs)) {
262                         radeon_firevertices(&rmesa->radeon);
263                 }
264
265                 for(i = 0; i < R300_MAX_TEXTURE_UNITS; ++i)
266                         if (rmesa->hw.textures[i] == t)
267                                 rmesa->hw.textures[i] = 0;
268         }
269
270         if (t->bo) {
271                 radeon_bo_unref(t->bo);
272                 t->bo = NULL;
273         }
274
275         radeon_miptree_unreference(&t->mt);
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(struct gl_context * 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 & (RADEON_STATE | RADEON_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.Sampler.MaxAnisotropy = rmesa->radeon.initialMaxAnisotropy;
302
303         /* Initialize hardware state */
304         r300UpdateTexWrap(t);
305         r300SetTexFilter(t, t->base.Sampler.MinFilter,
306                          t->base.Sampler.MagFilter,
307                          t->base.Sampler.MaxAnisotropy);
308         r300SetTexBorderColor(t, t->base.Sampler.BorderColor.f);
309
310         return &t->base;
311 }
312
313 unsigned r300IsFormatRenderable(gl_format mesa_format)
314 {
315         switch (mesa_format)
316         {
317                 case MESA_FORMAT_RGB565:
318                 case MESA_FORMAT_RGBA5551:
319                 case MESA_FORMAT_RGBA8888:
320                 case MESA_FORMAT_RGB565_REV:
321                 case MESA_FORMAT_RGBA8888_REV:
322                 case MESA_FORMAT_ARGB4444:
323                 case MESA_FORMAT_ARGB1555:
324                 case MESA_FORMAT_XRGB8888:
325                 case MESA_FORMAT_ARGB8888:
326                 case MESA_FORMAT_ARGB4444_REV:
327                 case MESA_FORMAT_ARGB1555_REV:
328                 case MESA_FORMAT_XRGB8888_REV:
329                 case MESA_FORMAT_ARGB8888_REV:
330                 case MESA_FORMAT_SRGBA8:
331                 case MESA_FORMAT_SARGB8:
332                 case MESA_FORMAT_SL8:
333                 case MESA_FORMAT_A8:
334                 case MESA_FORMAT_L8:
335                 case MESA_FORMAT_I8:
336                 case MESA_FORMAT_Z16:
337                         return 1;
338                 default:
339                         return 0;
340         }
341 }
342
343 unsigned r500IsFormatRenderable(gl_format mesa_format)
344 {
345         if (mesa_format == MESA_FORMAT_S8_Z24) {
346                 return 1;
347         } else {
348                 return r300IsFormatRenderable(mesa_format);
349         }
350 }
351
352 void r300InitTextureFuncs(radeonContextPtr radeon, struct dd_function_table *functions)
353 {
354         /* Note: we only plug in the functions we implement in the driver
355          * since _mesa_init_driver_functions() was already called.
356          */
357
358         radeon_init_common_texture_funcs(radeon, functions);
359
360         functions->NewTextureObject = r300NewTextureObject;
361         functions->DeleteTexture = r300DeleteTexture;
362         functions->IsTextureResident = driIsTextureResident;
363
364         functions->TexParameter = r300TexParameter;
365
366 #if FEATURE_OES_EGL_image
367         functions->EGLImageTargetTexture2D = radeon_image_target_texture_2d;
368 #endif
369 }