Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / i915 / i830_texstate.c
1 /**************************************************************************
2  * 
3  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * All Rights Reserved.
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  * 
26  **************************************************************************/
27
28 #include "main/mtypes.h"
29 #include "main/enums.h"
30 #include "main/colormac.h"
31 #include "main/macros.h"
32 #include "main/samplerobj.h"
33
34 #include "intel_mipmap_tree.h"
35 #include "intel_tex.h"
36
37 #include "i830_context.h"
38 #include "i830_reg.h"
39 #include "intel_chipset.h"
40
41
42 static GLuint
43 translate_texture_format(GLuint mesa_format)
44 {
45    switch (mesa_format) {
46    case MESA_FORMAT_L8:
47       return MAPSURF_8BIT | MT_8BIT_L8;
48    case MESA_FORMAT_I8:
49       return MAPSURF_8BIT | MT_8BIT_I8;
50    case MESA_FORMAT_A8:
51       return MAPSURF_8BIT | MT_8BIT_I8; /* Kludge! */
52    case MESA_FORMAT_AL88:
53       return MAPSURF_16BIT | MT_16BIT_AY88;
54    case MESA_FORMAT_RGB565:
55       return MAPSURF_16BIT | MT_16BIT_RGB565;
56    case MESA_FORMAT_ARGB1555:
57       return MAPSURF_16BIT | MT_16BIT_ARGB1555;
58    case MESA_FORMAT_ARGB4444:
59       return MAPSURF_16BIT | MT_16BIT_ARGB4444;
60    case MESA_FORMAT_ARGB8888:
61       return MAPSURF_32BIT | MT_32BIT_ARGB8888;
62    case MESA_FORMAT_XRGB8888:
63       return MAPSURF_32BIT | MT_32BIT_XRGB8888;
64    case MESA_FORMAT_YCBCR_REV:
65       return (MAPSURF_422 | MT_422_YCRCB_NORMAL);
66    case MESA_FORMAT_YCBCR:
67       return (MAPSURF_422 | MT_422_YCRCB_SWAPY);
68    case MESA_FORMAT_RGB_FXT1:
69    case MESA_FORMAT_RGBA_FXT1:
70       return (MAPSURF_COMPRESSED | MT_COMPRESS_FXT1);
71    case MESA_FORMAT_RGBA_DXT1:
72    case MESA_FORMAT_RGB_DXT1:
73       return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1);
74    case MESA_FORMAT_RGBA_DXT3:
75       return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT2_3);
76    case MESA_FORMAT_RGBA_DXT5:
77       return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT4_5);
78    default:
79       fprintf(stderr, "%s: bad image format %x\n", __FUNCTION__, mesa_format);
80       abort();
81       return 0;
82    }
83 }
84
85
86
87
88 /* The i915 (and related graphics cores) do not support GL_CLAMP.  The
89  * Intel drivers for "other operating systems" implement GL_CLAMP as
90  * GL_CLAMP_TO_EDGE, so the same is done here.
91  */
92 static GLuint
93 translate_wrap_mode(GLenum wrap)
94 {
95    switch (wrap) {
96    case GL_REPEAT:
97       return TEXCOORDMODE_WRAP;
98    case GL_CLAMP:
99    case GL_CLAMP_TO_EDGE:
100       return TEXCOORDMODE_CLAMP;        /* not really correct */
101    case GL_CLAMP_TO_BORDER:
102       return TEXCOORDMODE_CLAMP_BORDER;
103    case GL_MIRRORED_REPEAT:
104       return TEXCOORDMODE_MIRROR;
105    default:
106       return TEXCOORDMODE_WRAP;
107    }
108 }
109
110
111 /* Recalculate all state from scratch.  Perhaps not the most
112  * efficient, but this has gotten complex enough that we need
113  * something which is understandable and reliable.
114  */
115 static GLboolean
116 i830_update_tex_unit(struct intel_context *intel, GLuint unit, GLuint ss3)
117 {
118    struct gl_context *ctx = &intel->ctx;
119    struct i830_context *i830 = i830_context(ctx);
120    struct gl_texture_unit *tUnit = &ctx->Texture.Unit[unit];
121    struct gl_texture_object *tObj = tUnit->_Current;
122    struct intel_texture_object *intelObj = intel_texture_object(tObj);
123    struct gl_texture_image *firstImage;
124    struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
125    GLuint *state = i830->state.Tex[unit], format, pitch;
126    GLint lodbias;
127    GLubyte border[4];
128    GLuint dst_x, dst_y;
129
130    memset(state, 0, sizeof(state));
131
132    /*We need to refcount these. */
133
134    if (i830->state.tex_buffer[unit] != NULL) {
135        drm_intel_bo_unreference(i830->state.tex_buffer[unit]);
136        i830->state.tex_buffer[unit] = NULL;
137    }
138
139    if (!intel_finalize_mipmap_tree(intel, unit))
140       return GL_FALSE;
141
142    /* Get first image here, since intelObj->firstLevel will get set in
143     * the intel_finalize_mipmap_tree() call above.
144     */
145    firstImage = tObj->Image[0][tObj->BaseLevel];
146
147    intel_miptree_get_image_offset(intelObj->mt, tObj->BaseLevel, 0, 0,
148                                   &dst_x, &dst_y);
149
150    drm_intel_bo_reference(intelObj->mt->region->buffer);
151    i830->state.tex_buffer[unit] = intelObj->mt->region->buffer;
152    pitch = intelObj->mt->region->pitch * intelObj->mt->cpp;
153
154    /* XXX: This calculation is probably broken for tiled images with
155     * a non-page-aligned offset.
156     */
157    i830->state.tex_offset[unit] = dst_x * intelObj->mt->cpp + dst_y * pitch;
158
159    format = translate_texture_format(firstImage->TexFormat);
160
161    state[I830_TEXREG_TM0LI] = (_3DSTATE_LOAD_STATE_IMMEDIATE_2 |
162                                (LOAD_TEXTURE_MAP0 << unit) | 4);
163
164    state[I830_TEXREG_TM0S1] =
165       (((firstImage->Height - 1) << TM0S1_HEIGHT_SHIFT) |
166        ((firstImage->Width - 1) << TM0S1_WIDTH_SHIFT) | format);
167
168    if (intelObj->mt->region->tiling != I915_TILING_NONE) {
169       state[I830_TEXREG_TM0S1] |= TM0S1_TILED_SURFACE;
170       if (intelObj->mt->region->tiling == I915_TILING_Y)
171          state[I830_TEXREG_TM0S1] |= TM0S1_TILE_WALK;
172    }
173
174    state[I830_TEXREG_TM0S2] =
175       ((((pitch / 4) - 1) << TM0S2_PITCH_SHIFT) | TM0S2_CUBE_FACE_ENA_MASK);
176
177    {
178       if (tObj->Target == GL_TEXTURE_CUBE_MAP)
179          state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit) |
180                                     CUBE_NEGX_ENABLE |
181                                     CUBE_POSX_ENABLE |
182                                     CUBE_NEGY_ENABLE |
183                                     CUBE_POSY_ENABLE |
184                                     CUBE_NEGZ_ENABLE | CUBE_POSZ_ENABLE);
185       else
186          state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit));
187    }
188
189
190
191
192    {
193       GLuint minFilt, mipFilt, magFilt;
194       float maxlod;
195       uint32_t minlod_fixed, maxlod_fixed;
196
197       switch (sampler->MinFilter) {
198       case GL_NEAREST:
199          minFilt = FILTER_NEAREST;
200          mipFilt = MIPFILTER_NONE;
201          break;
202       case GL_LINEAR:
203          minFilt = FILTER_LINEAR;
204          mipFilt = MIPFILTER_NONE;
205          break;
206       case GL_NEAREST_MIPMAP_NEAREST:
207          minFilt = FILTER_NEAREST;
208          mipFilt = MIPFILTER_NEAREST;
209          break;
210       case GL_LINEAR_MIPMAP_NEAREST:
211          minFilt = FILTER_LINEAR;
212          mipFilt = MIPFILTER_NEAREST;
213          break;
214       case GL_NEAREST_MIPMAP_LINEAR:
215          minFilt = FILTER_NEAREST;
216          mipFilt = MIPFILTER_LINEAR;
217          break;
218       case GL_LINEAR_MIPMAP_LINEAR:
219          minFilt = FILTER_LINEAR;
220          mipFilt = MIPFILTER_LINEAR;
221          break;
222       default:
223          return GL_FALSE;
224       }
225
226       if (sampler->MaxAnisotropy > 1.0) {
227          minFilt = FILTER_ANISOTROPIC;
228          magFilt = FILTER_ANISOTROPIC;
229       }
230       else {
231          switch (sampler->MagFilter) {
232          case GL_NEAREST:
233             magFilt = FILTER_NEAREST;
234             break;
235          case GL_LINEAR:
236             magFilt = FILTER_LINEAR;
237             break;
238          default:
239             return GL_FALSE;
240          }
241       }
242
243       lodbias = (int) ((tUnit->LodBias + sampler->LodBias) * 16.0);
244       if (lodbias < -64)
245           lodbias = -64;
246       if (lodbias > 63)
247           lodbias = 63;
248       
249       state[I830_TEXREG_TM0S3] = ((lodbias << TM0S3_LOD_BIAS_SHIFT) & 
250                                   TM0S3_LOD_BIAS_MASK);
251 #if 0
252       /* YUV conversion:
253        */
254       if (firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR ||
255           firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV)
256          state[I830_TEXREG_TM0S3] |= SS2_COLORSPACE_CONVERSION;
257 #endif
258
259       /* We get one field with fraction bits for the maximum
260        * addressable (smallest resolution) LOD.  Use it to cover both
261        * MAX_LEVEL and MAX_LOD.
262        */
263       minlod_fixed = U_FIXED(CLAMP(sampler->MinLod, 0.0, 11), 4);
264       maxlod = MIN2(sampler->MaxLod, tObj->_MaxLevel - tObj->BaseLevel);
265       if (intel->intelScreen->deviceID == PCI_CHIP_I855_GM ||
266           intel->intelScreen->deviceID == PCI_CHIP_I865_G) {
267          maxlod_fixed = U_FIXED(CLAMP(maxlod, 0.0, 11.75), 2);
268          maxlod_fixed = MAX2(maxlod_fixed, (minlod_fixed + 3) >> 2);
269          state[I830_TEXREG_TM0S3] |= maxlod_fixed << TM0S3_MIN_MIP_SHIFT;
270          state[I830_TEXREG_TM0S2] |= TM0S2_LOD_PRECLAMP;
271       } else {
272          maxlod_fixed = U_FIXED(CLAMP(maxlod, 0.0, 11), 0);
273          maxlod_fixed = MAX2(maxlod_fixed, (minlod_fixed + 15) >> 4);
274          state[I830_TEXREG_TM0S3] |= maxlod_fixed << TM0S3_MIN_MIP_SHIFT_830;
275       }
276       state[I830_TEXREG_TM0S3] |= minlod_fixed << TM0S3_MAX_MIP_SHIFT;
277       state[I830_TEXREG_TM0S3] |= ((minFilt << TM0S3_MIN_FILTER_SHIFT) |
278                                    (mipFilt << TM0S3_MIP_FILTER_SHIFT) |
279                                    (magFilt << TM0S3_MAG_FILTER_SHIFT));
280    }
281
282    {
283       GLenum ws = sampler->WrapS;
284       GLenum wt = sampler->WrapT;
285
286
287       /* 3D textures not available on i830
288        */
289       if (tObj->Target == GL_TEXTURE_3D)
290          return GL_FALSE;
291
292       state[I830_TEXREG_MCS] = (_3DSTATE_MAP_COORD_SET_CMD |
293                                 MAP_UNIT(unit) |
294                                 ENABLE_TEXCOORD_PARAMS |
295                                 ss3 |
296                                 ENABLE_ADDR_V_CNTL |
297                                 TEXCOORD_ADDR_V_MODE(translate_wrap_mode(wt))
298                                 | ENABLE_ADDR_U_CNTL |
299                                 TEXCOORD_ADDR_U_MODE(translate_wrap_mode
300                                                      (ws)));
301    }
302
303    /* convert border color from float to ubyte */
304    CLAMPED_FLOAT_TO_UBYTE(border[0], sampler->BorderColor.f[0]);
305    CLAMPED_FLOAT_TO_UBYTE(border[1], sampler->BorderColor.f[1]);
306    CLAMPED_FLOAT_TO_UBYTE(border[2], sampler->BorderColor.f[2]);
307    CLAMPED_FLOAT_TO_UBYTE(border[3], sampler->BorderColor.f[3]);
308
309    state[I830_TEXREG_TM0S4] = PACK_COLOR_8888(border[3],
310                                               border[0],
311                                               border[1],
312                                               border[2]);
313
314    I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(unit), GL_TRUE);
315    /* memcmp was already disabled, but definitely won't work as the
316     * region might now change and that wouldn't be detected:
317     */
318    I830_STATECHANGE(i830, I830_UPLOAD_TEX(unit));
319    return GL_TRUE;
320 }
321
322
323
324
325 void
326 i830UpdateTextureState(struct intel_context *intel)
327 {
328    struct i830_context *i830 = i830_context(&intel->ctx);
329    GLboolean ok = GL_TRUE;
330    GLuint i;
331
332    for (i = 0; i < I830_TEX_UNITS && ok; i++) {
333       switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
334       case TEXTURE_1D_BIT:
335       case TEXTURE_2D_BIT:
336       case TEXTURE_CUBE_BIT:
337          ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_NORMAL);
338          break;
339       case TEXTURE_RECT_BIT:
340          ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_IN_TEXELUNITS);
341          break;
342       case 0:{
343          struct i830_context *i830 = i830_context(&intel->ctx);
344          if (i830->state.active & I830_UPLOAD_TEX(i)) 
345             I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(i), GL_FALSE);
346
347          if (i830->state.tex_buffer[i] != NULL) {
348             drm_intel_bo_unreference(i830->state.tex_buffer[i]);
349             i830->state.tex_buffer[i] = NULL;
350          }
351          break;
352       }
353       case TEXTURE_3D_BIT:
354       default:
355          ok = GL_FALSE;
356          break;
357       }
358    }
359
360    FALLBACK(intel, I830_FALLBACK_TEXTURE, !ok);
361
362    if (ok)
363       i830EmitTextureBlend(i830);
364 }