Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.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
31 #include "intel_mipmap_tree.h"
32 #include "intel_tex.h"
33
34 #include "i830_context.h"
35 #include "i830_reg.h"
36
37
38
39 static GLuint
40 translate_texture_format(GLuint mesa_format, GLuint internal_format)
41 {
42 switch (mesa_format) {
43 case MESA_FORMAT_L8:
44 return MAPSURF_8BIT | MT_8BIT_L8;
45 case MESA_FORMAT_I8:
46 return MAPSURF_8BIT | MT_8BIT_I8;
47 case MESA_FORMAT_A8:
48 return MAPSURF_8BIT | MT_8BIT_I8; /* Kludge! */
49 case MESA_FORMAT_AL88:
50 return MAPSURF_16BIT | MT_16BIT_AY88;
51 case MESA_FORMAT_RGB565:
52 return MAPSURF_16BIT | MT_16BIT_RGB565;
53 case MESA_FORMAT_ARGB1555:
54 return MAPSURF_16BIT | MT_16BIT_ARGB1555;
55 case MESA_FORMAT_ARGB4444:
56 return MAPSURF_16BIT | MT_16BIT_ARGB4444;
57 case MESA_FORMAT_ARGB8888:
58 if (internal_format == GL_RGB)
59 return MAPSURF_32BIT | MT_32BIT_XRGB8888;
60 else
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 GLcontext *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 GLuint *state = i830->state.Tex[unit], format, pitch;
125 GLint lodbias;
126 GLubyte border[4];
127
128 memset(state, 0, sizeof(state));
129
130 /*We need to refcount these. */
131
132 if (i830->state.tex_buffer[unit] != NULL) {
133 dri_bo_unreference(i830->state.tex_buffer[unit]);
134 i830->state.tex_buffer[unit] = NULL;
135 }
136
137 if (!intelObj->imageOverride && !intel_finalize_mipmap_tree(intel, unit))
138 return GL_FALSE;
139
140 /* Get first image here, since intelObj->firstLevel will get set in
141 * the intel_finalize_mipmap_tree() call above.
142 */
143 firstImage = tObj->Image[0][intelObj->firstLevel];
144
145 if (intelObj->imageOverride) {
146 i830->state.tex_buffer[unit] = NULL;
147 i830->state.tex_offset[unit] = intelObj->textureOffset;
148
149 switch (intelObj->depthOverride) {
150 case 32:
151 format = MAPSURF_32BIT | MT_32BIT_ARGB8888;
152 break;
153 case 24:
154 default:
155 format = MAPSURF_32BIT | MT_32BIT_XRGB8888;
156 break;
157 case 16:
158 format = MAPSURF_16BIT | MT_16BIT_RGB565;
159 break;
160 }
161
162 pitch = intelObj->pitchOverride;
163 } else {
164 GLuint dst_x, dst_y;
165
166 intel_miptree_get_image_offset(intelObj->mt, intelObj->firstLevel, 0, 0,
167 &dst_x, &dst_y);
168
169 dri_bo_reference(intelObj->mt->region->buffer);
170 i830->state.tex_buffer[unit] = intelObj->mt->region->buffer;
171 /* XXX: This calculation is probably broken for tiled images with
172 * a non-page-aligned offset.
173 */
174 i830->state.tex_offset[unit] = (dst_x + dst_y * intelObj->mt->pitch) *
175 intelObj->mt->cpp;
176
177 format = translate_texture_format(firstImage->TexFormat,
178 firstImage->InternalFormat);
179 pitch = intelObj->mt->pitch * intelObj->mt->cpp;
180 }
181
182 state[I830_TEXREG_TM0LI] = (_3DSTATE_LOAD_STATE_IMMEDIATE_2 |
183 (LOAD_TEXTURE_MAP0 << unit) | 4);
184
185 state[I830_TEXREG_TM0S1] =
186 (((firstImage->Height - 1) << TM0S1_HEIGHT_SHIFT) |
187 ((firstImage->Width - 1) << TM0S1_WIDTH_SHIFT) | format);
188
189 if (intelObj->mt->region->tiling != I915_TILING_NONE) {
190 state[I830_TEXREG_TM0S1] |= TM0S1_TILED_SURFACE;
191 if (intelObj->mt->region->tiling == I915_TILING_Y)
192 state[I830_TEXREG_TM0S1] |= TM0S1_TILE_WALK;
193 }
194
195 state[I830_TEXREG_TM0S2] =
196 ((((pitch / 4) - 1) << TM0S2_PITCH_SHIFT) | TM0S2_CUBE_FACE_ENA_MASK);
197
198 {
199 if (tObj->Target == GL_TEXTURE_CUBE_MAP)
200 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit) |
201 CUBE_NEGX_ENABLE |
202 CUBE_POSX_ENABLE |
203 CUBE_NEGY_ENABLE |
204 CUBE_POSY_ENABLE |
205 CUBE_NEGZ_ENABLE | CUBE_POSZ_ENABLE);
206 else
207 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit));
208 }
209
210
211
212
213 {
214 GLuint minFilt, mipFilt, magFilt;
215
216 switch (tObj->MinFilter) {
217 case GL_NEAREST:
218 minFilt = FILTER_NEAREST;
219 mipFilt = MIPFILTER_NONE;
220 break;
221 case GL_LINEAR:
222 minFilt = FILTER_LINEAR;
223 mipFilt = MIPFILTER_NONE;
224 break;
225 case GL_NEAREST_MIPMAP_NEAREST:
226 minFilt = FILTER_NEAREST;
227 mipFilt = MIPFILTER_NEAREST;
228 break;
229 case GL_LINEAR_MIPMAP_NEAREST:
230 minFilt = FILTER_LINEAR;
231 mipFilt = MIPFILTER_NEAREST;
232 break;
233 case GL_NEAREST_MIPMAP_LINEAR:
234 minFilt = FILTER_NEAREST;
235 mipFilt = MIPFILTER_LINEAR;
236 break;
237 case GL_LINEAR_MIPMAP_LINEAR:
238 minFilt = FILTER_LINEAR;
239 mipFilt = MIPFILTER_LINEAR;
240 break;
241 default:
242 return GL_FALSE;
243 }
244
245 if (tObj->MaxAnisotropy > 1.0) {
246 minFilt = FILTER_ANISOTROPIC;
247 magFilt = FILTER_ANISOTROPIC;
248 }
249 else {
250 switch (tObj->MagFilter) {
251 case GL_NEAREST:
252 magFilt = FILTER_NEAREST;
253 break;
254 case GL_LINEAR:
255 magFilt = FILTER_LINEAR;
256 break;
257 default:
258 return GL_FALSE;
259 }
260 }
261
262 lodbias = (int) ((tUnit->LodBias + tObj->LodBias) * 16.0);
263 if (lodbias < -64)
264 lodbias = -64;
265 if (lodbias > 63)
266 lodbias = 63;
267
268 state[I830_TEXREG_TM0S3] = ((lodbias << TM0S3_LOD_BIAS_SHIFT) &
269 TM0S3_LOD_BIAS_MASK);
270 #if 0
271 /* YUV conversion:
272 */
273 if (firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR ||
274 firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV)
275 state[I830_TEXREG_TM0S3] |= SS2_COLORSPACE_CONVERSION;
276 #endif
277
278 state[I830_TEXREG_TM0S3] |= ((intelObj->lastLevel -
279 intelObj->firstLevel) *
280 4) << TM0S3_MIN_MIP_SHIFT;
281
282 state[I830_TEXREG_TM0S3] |= ((minFilt << TM0S3_MIN_FILTER_SHIFT) |
283 (mipFilt << TM0S3_MIP_FILTER_SHIFT) |
284 (magFilt << TM0S3_MAG_FILTER_SHIFT));
285 }
286
287 {
288 GLenum ws = tObj->WrapS;
289 GLenum wt = tObj->WrapT;
290
291
292 /* 3D textures not available on i830
293 */
294 if (tObj->Target == GL_TEXTURE_3D)
295 return GL_FALSE;
296
297 state[I830_TEXREG_MCS] = (_3DSTATE_MAP_COORD_SET_CMD |
298 MAP_UNIT(unit) |
299 ENABLE_TEXCOORD_PARAMS |
300 ss3 |
301 ENABLE_ADDR_V_CNTL |
302 TEXCOORD_ADDR_V_MODE(translate_wrap_mode(wt))
303 | ENABLE_ADDR_U_CNTL |
304 TEXCOORD_ADDR_U_MODE(translate_wrap_mode
305 (ws)));
306 }
307
308 /* convert border color from float to ubyte */
309 CLAMPED_FLOAT_TO_UBYTE(border[0], tObj->BorderColor[0]);
310 CLAMPED_FLOAT_TO_UBYTE(border[1], tObj->BorderColor[1]);
311 CLAMPED_FLOAT_TO_UBYTE(border[2], tObj->BorderColor[2]);
312 CLAMPED_FLOAT_TO_UBYTE(border[3], tObj->BorderColor[3]);
313
314 state[I830_TEXREG_TM0S4] = INTEL_PACKCOLOR8888(border[0],
315 border[1],
316 border[2],
317 border[3]);
318
319
320 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(unit), GL_TRUE);
321 /* memcmp was already disabled, but definitely won't work as the
322 * region might now change and that wouldn't be detected:
323 */
324 I830_STATECHANGE(i830, I830_UPLOAD_TEX(unit));
325 return GL_TRUE;
326 }
327
328
329
330
331 void
332 i830UpdateTextureState(struct intel_context *intel)
333 {
334 struct i830_context *i830 = i830_context(&intel->ctx);
335 GLboolean ok = GL_TRUE;
336 GLuint i;
337
338 for (i = 0; i < I830_TEX_UNITS && ok; i++) {
339 switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
340 case TEXTURE_1D_BIT:
341 case TEXTURE_2D_BIT:
342 case TEXTURE_CUBE_BIT:
343 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_NORMAL);
344 break;
345 case TEXTURE_RECT_BIT:
346 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_IN_TEXELUNITS);
347 break;
348 case 0:{
349 struct i830_context *i830 = i830_context(&intel->ctx);
350 if (i830->state.active & I830_UPLOAD_TEX(i))
351 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(i), GL_FALSE);
352
353 if (i830->state.tex_buffer[i] != NULL) {
354 dri_bo_unreference(i830->state.tex_buffer[i]);
355 i830->state.tex_buffer[i] = NULL;
356 }
357 break;
358 }
359 case TEXTURE_3D_BIT:
360 default:
361 ok = GL_FALSE;
362 break;
363 }
364 }
365
366 FALLBACK(intel, I830_FALLBACK_TEXTURE, !ok);
367
368 if (ok)
369 i830EmitTextureBlend(i830);
370 }