Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / i915 / i915_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 "mtypes.h"
29 #include "enums.h"
30 #include "texformat.h"
31
32 #include "intel_mipmap_tree.h"
33 #include "intel_tex.h"
34
35 #include "i915_context.h"
36 #include "i915_reg.h"
37
38
39 static GLuint
40 translate_texture_format(GLuint mesa_format, GLenum DepthMode)
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_A8;
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 return MAPSURF_32BIT | MT_32BIT_ARGB8888;
59 case MESA_FORMAT_YCBCR_REV:
60 return (MAPSURF_422 | MT_422_YCRCB_NORMAL);
61 case MESA_FORMAT_YCBCR:
62 return (MAPSURF_422 | MT_422_YCRCB_SWAPY);
63 case MESA_FORMAT_RGB_FXT1:
64 case MESA_FORMAT_RGBA_FXT1:
65 return (MAPSURF_COMPRESSED | MT_COMPRESS_FXT1);
66 case MESA_FORMAT_Z16:
67 if (DepthMode == GL_ALPHA)
68 return (MAPSURF_16BIT | MT_16BIT_A16);
69 else if (DepthMode == GL_INTENSITY)
70 return (MAPSURF_16BIT | MT_16BIT_I16);
71 else
72 return (MAPSURF_16BIT | MT_16BIT_L16);
73 case MESA_FORMAT_RGBA_DXT1:
74 case MESA_FORMAT_RGB_DXT1:
75 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1);
76 case MESA_FORMAT_RGBA_DXT3:
77 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT2_3);
78 case MESA_FORMAT_RGBA_DXT5:
79 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT4_5);
80 case MESA_FORMAT_S8_Z24:
81 return (MAPSURF_32BIT | MT_32BIT_xI824);
82 default:
83 fprintf(stderr, "%s: bad image format %x\n", __FUNCTION__, mesa_format);
84 abort();
85 return 0;
86 }
87 }
88
89
90
91
92 /* The i915 (and related graphics cores) do not support GL_CLAMP. The
93 * Intel drivers for "other operating systems" implement GL_CLAMP as
94 * GL_CLAMP_TO_EDGE, so the same is done here.
95 */
96 static GLuint
97 translate_wrap_mode(GLenum wrap)
98 {
99 switch (wrap) {
100 case GL_REPEAT:
101 return TEXCOORDMODE_WRAP;
102 case GL_CLAMP:
103 return TEXCOORDMODE_CLAMP_EDGE; /* not quite correct */
104 case GL_CLAMP_TO_EDGE:
105 return TEXCOORDMODE_CLAMP_EDGE;
106 case GL_CLAMP_TO_BORDER:
107 return TEXCOORDMODE_CLAMP_BORDER;
108 case GL_MIRRORED_REPEAT:
109 return TEXCOORDMODE_MIRROR;
110 default:
111 return TEXCOORDMODE_WRAP;
112 }
113 }
114
115
116
117 /* Recalculate all state from scratch. Perhaps not the most
118 * efficient, but this has gotten complex enough that we need
119 * something which is understandable and reliable.
120 */
121 static GLboolean
122 i915_update_tex_unit(struct intel_context *intel, GLuint unit, GLuint ss3)
123 {
124 GLcontext *ctx = &intel->ctx;
125 struct i915_context *i915 = i915_context(ctx);
126 struct gl_texture_unit *tUnit = &ctx->Texture.Unit[unit];
127 struct gl_texture_object *tObj = tUnit->_Current;
128 struct intel_texture_object *intelObj = intel_texture_object(tObj);
129 struct gl_texture_image *firstImage;
130 GLuint *state = i915->state.Tex[unit], format, pitch;
131 GLint lodbias;
132
133 memset(state, 0, sizeof(state));
134
135 /*We need to refcount these. */
136
137 if (i915->state.tex_buffer[unit] != NULL) {
138 dri_bo_unreference(i915->state.tex_buffer[unit]);
139 i915->state.tex_buffer[unit] = NULL;
140 }
141
142 if (!intelObj->imageOverride && !intel_finalize_mipmap_tree(intel, unit))
143 return GL_FALSE;
144
145 /* Get first image here, since intelObj->firstLevel will get set in
146 * the intel_finalize_mipmap_tree() call above.
147 */
148 firstImage = tObj->Image[0][intelObj->firstLevel];
149
150 if (intelObj->imageOverride) {
151 i915->state.tex_buffer[unit] = NULL;
152 i915->state.tex_offset[unit] = intelObj->textureOffset;
153
154 switch (intelObj->depthOverride) {
155 case 32:
156 format = MAPSURF_32BIT | MT_32BIT_ARGB8888;
157 break;
158 case 24:
159 default:
160 format = MAPSURF_32BIT | MT_32BIT_XRGB8888;
161 break;
162 case 16:
163 format = MAPSURF_16BIT | MT_16BIT_RGB565;
164 break;
165 }
166
167 pitch = intelObj->pitchOverride;
168 } else {
169 dri_bo_reference(intelObj->mt->region->buffer);
170 i915->state.tex_buffer[unit] = intelObj->mt->region->buffer;
171 i915->state.tex_offset[unit] = intel_miptree_image_offset(intelObj->mt,
172 0, intelObj->
173 firstLevel);
174
175 format = translate_texture_format(firstImage->TexFormat->MesaFormat,
176 tObj->DepthMode);
177 pitch = intelObj->mt->pitch * intelObj->mt->cpp;
178 }
179
180 state[I915_TEXREG_MS3] =
181 (((firstImage->Height - 1) << MS3_HEIGHT_SHIFT) |
182 ((firstImage->Width - 1) << MS3_WIDTH_SHIFT) | format |
183 MS3_USE_FENCE_REGS);
184
185 state[I915_TEXREG_MS4] =
186 ((((pitch / 4) - 1) << MS4_PITCH_SHIFT) | MS4_CUBE_FACE_ENA_MASK |
187 ((((intelObj->lastLevel - intelObj->firstLevel) * 4)) <<
188 MS4_MAX_LOD_SHIFT) | ((firstImage->Depth - 1) <<
189 MS4_VOLUME_DEPTH_SHIFT));
190
191
192 {
193 GLuint minFilt, mipFilt, magFilt;
194
195 switch (tObj->MinFilter) {
196 case GL_NEAREST:
197 minFilt = FILTER_NEAREST;
198 mipFilt = MIPFILTER_NONE;
199 break;
200 case GL_LINEAR:
201 minFilt = FILTER_LINEAR;
202 mipFilt = MIPFILTER_NONE;
203 break;
204 case GL_NEAREST_MIPMAP_NEAREST:
205 minFilt = FILTER_NEAREST;
206 mipFilt = MIPFILTER_NEAREST;
207 break;
208 case GL_LINEAR_MIPMAP_NEAREST:
209 minFilt = FILTER_LINEAR;
210 mipFilt = MIPFILTER_NEAREST;
211 break;
212 case GL_NEAREST_MIPMAP_LINEAR:
213 minFilt = FILTER_NEAREST;
214 mipFilt = MIPFILTER_LINEAR;
215 break;
216 case GL_LINEAR_MIPMAP_LINEAR:
217 minFilt = FILTER_LINEAR;
218 mipFilt = MIPFILTER_LINEAR;
219 break;
220 default:
221 return GL_FALSE;
222 }
223
224 if (tObj->MaxAnisotropy > 1.0) {
225 minFilt = FILTER_ANISOTROPIC;
226 magFilt = FILTER_ANISOTROPIC;
227 }
228 else {
229 switch (tObj->MagFilter) {
230 case GL_NEAREST:
231 magFilt = FILTER_NEAREST;
232 break;
233 case GL_LINEAR:
234 magFilt = FILTER_LINEAR;
235 break;
236 default:
237 return GL_FALSE;
238 }
239 }
240
241 lodbias = (int) ((tUnit->LodBias + tObj->LodBias) * 16.0);
242 if (lodbias < -256)
243 lodbias = -256;
244 if (lodbias > 255)
245 lodbias = 255;
246 state[I915_TEXREG_SS2] = ((lodbias << SS2_LOD_BIAS_SHIFT) &
247 SS2_LOD_BIAS_MASK);
248
249 /* YUV conversion:
250 */
251 if (firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR ||
252 firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV)
253 state[I915_TEXREG_SS2] |= SS2_COLORSPACE_CONVERSION;
254
255 /* Shadow:
256 */
257 if (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB &&
258 tObj->Target != GL_TEXTURE_3D) {
259 if (tObj->Target == GL_TEXTURE_1D)
260 return GL_FALSE;
261
262 state[I915_TEXREG_SS2] |=
263 (SS2_SHADOW_ENABLE |
264 intel_translate_shadow_compare_func(tObj->CompareFunc));
265
266 minFilt = FILTER_4X4_FLAT;
267 magFilt = FILTER_4X4_FLAT;
268 }
269
270 state[I915_TEXREG_SS2] |= ((minFilt << SS2_MIN_FILTER_SHIFT) |
271 (mipFilt << SS2_MIP_FILTER_SHIFT) |
272 (magFilt << SS2_MAG_FILTER_SHIFT));
273 }
274
275 {
276 GLenum ws = tObj->WrapS;
277 GLenum wt = tObj->WrapT;
278 GLenum wr = tObj->WrapR;
279
280
281 /* 3D textures don't seem to respect the border color.
282 * Fallback if there's ever a danger that they might refer to
283 * it.
284 *
285 * Effectively this means fallback on 3D clamp or
286 * clamp_to_border.
287 */
288 if (tObj->Target == GL_TEXTURE_3D &&
289 (tObj->MinFilter != GL_NEAREST ||
290 tObj->MagFilter != GL_NEAREST) &&
291 (ws == GL_CLAMP ||
292 wt == GL_CLAMP ||
293 wr == GL_CLAMP ||
294 ws == GL_CLAMP_TO_BORDER ||
295 wt == GL_CLAMP_TO_BORDER || wr == GL_CLAMP_TO_BORDER))
296 return GL_FALSE;
297
298
299 state[I915_TEXREG_SS3] = ss3; /* SS3_NORMALIZED_COORDS */
300
301 state[I915_TEXREG_SS3] |=
302 ((translate_wrap_mode(ws) << SS3_TCX_ADDR_MODE_SHIFT) |
303 (translate_wrap_mode(wt) << SS3_TCY_ADDR_MODE_SHIFT) |
304 (translate_wrap_mode(wr) << SS3_TCZ_ADDR_MODE_SHIFT));
305
306 state[I915_TEXREG_SS3] |= (unit << SS3_TEXTUREMAP_INDEX_SHIFT);
307 }
308
309
310 state[I915_TEXREG_SS4] = INTEL_PACKCOLOR8888(tObj->_BorderChan[0],
311 tObj->_BorderChan[1],
312 tObj->_BorderChan[2],
313 tObj->_BorderChan[3]);
314
315
316 I915_ACTIVESTATE(i915, I915_UPLOAD_TEX(unit), GL_TRUE);
317 /* memcmp was already disabled, but definitely won't work as the
318 * region might now change and that wouldn't be detected:
319 */
320 I915_STATECHANGE(i915, I915_UPLOAD_TEX(unit));
321
322
323 #if 0
324 DBG(TEXTURE, "state[I915_TEXREG_SS2] = 0x%x\n", state[I915_TEXREG_SS2]);
325 DBG(TEXTURE, "state[I915_TEXREG_SS3] = 0x%x\n", state[I915_TEXREG_SS3]);
326 DBG(TEXTURE, "state[I915_TEXREG_SS4] = 0x%x\n", state[I915_TEXREG_SS4]);
327 DBG(TEXTURE, "state[I915_TEXREG_MS2] = 0x%x\n", state[I915_TEXREG_MS2]);
328 DBG(TEXTURE, "state[I915_TEXREG_MS3] = 0x%x\n", state[I915_TEXREG_MS3]);
329 DBG(TEXTURE, "state[I915_TEXREG_MS4] = 0x%x\n", state[I915_TEXREG_MS4]);
330 #endif
331
332 return GL_TRUE;
333 }
334
335
336
337
338 void
339 i915UpdateTextureState(struct intel_context *intel)
340 {
341 GLboolean ok = GL_TRUE;
342 GLuint i;
343
344 for (i = 0; i < I915_TEX_UNITS && ok; i++) {
345 switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
346 case TEXTURE_1D_BIT:
347 case TEXTURE_2D_BIT:
348 case TEXTURE_CUBE_BIT:
349 case TEXTURE_3D_BIT:
350 ok = i915_update_tex_unit(intel, i, SS3_NORMALIZED_COORDS);
351 break;
352 case TEXTURE_RECT_BIT:
353 ok = i915_update_tex_unit(intel, i, 0);
354 break;
355 case 0:{
356 struct i915_context *i915 = i915_context(&intel->ctx);
357 if (i915->state.active & I915_UPLOAD_TEX(i))
358 I915_ACTIVESTATE(i915, I915_UPLOAD_TEX(i), GL_FALSE);
359
360 if (i915->state.tex_buffer[i] != NULL) {
361 dri_bo_unreference(i915->state.tex_buffer[i]);
362 i915->state.tex_buffer[i] = NULL;
363 }
364
365 break;
366 }
367 default:
368 ok = GL_FALSE;
369 break;
370 }
371 }
372
373 FALLBACK(intel, I915_FALLBACK_TEXTURE, !ok);
374 }