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