Merge remote branch 'origin/7.8'
[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/macros.h"
31 #include "main/colormac.h"
32
33 #include "intel_mipmap_tree.h"
34 #include "intel_tex.h"
35
36 #include "i915_context.h"
37 #include "i915_reg.h"
38
39
40 static GLuint
41 translate_texture_format(gl_format mesa_format, GLuint internal_format,
42 GLenum DepthMode)
43 {
44 switch (mesa_format) {
45 case MESA_FORMAT_L8:
46 return MAPSURF_8BIT | MT_8BIT_L8;
47 case MESA_FORMAT_I8:
48 return MAPSURF_8BIT | MT_8BIT_I8;
49 case MESA_FORMAT_A8:
50 return MAPSURF_8BIT | MT_8BIT_A8;
51 case MESA_FORMAT_AL88:
52 return MAPSURF_16BIT | MT_16BIT_AY88;
53 case MESA_FORMAT_RGB565:
54 return MAPSURF_16BIT | MT_16BIT_RGB565;
55 case MESA_FORMAT_ARGB1555:
56 return MAPSURF_16BIT | MT_16BIT_ARGB1555;
57 case MESA_FORMAT_ARGB4444:
58 return MAPSURF_16BIT | MT_16BIT_ARGB4444;
59 case MESA_FORMAT_ARGB8888:
60 return MAPSURF_32BIT | MT_32BIT_ARGB8888;
61 case MESA_FORMAT_XRGB8888:
62 return MAPSURF_32BIT | MT_32BIT_XRGB8888;
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 if (DepthMode == GL_ALPHA)
86 return (MAPSURF_32BIT | MT_32BIT_x8A24);
87 else if (DepthMode == GL_INTENSITY)
88 return (MAPSURF_32BIT | MT_32BIT_x8I24);
89 else
90 return (MAPSURF_32BIT | MT_32BIT_x8L24);
91 default:
92 fprintf(stderr, "%s: bad image format %x\n", __FUNCTION__, mesa_format);
93 abort();
94 return 0;
95 }
96 }
97
98
99
100
101 /* The i915 (and related graphics cores) do not support GL_CLAMP. The
102 * Intel drivers for "other operating systems" implement GL_CLAMP as
103 * GL_CLAMP_TO_EDGE, so the same is done here.
104 */
105 static GLuint
106 translate_wrap_mode(GLenum wrap)
107 {
108 switch (wrap) {
109 case GL_REPEAT:
110 return TEXCOORDMODE_WRAP;
111 case GL_CLAMP:
112 return TEXCOORDMODE_CLAMP_EDGE; /* not quite correct */
113 case GL_CLAMP_TO_EDGE:
114 return TEXCOORDMODE_CLAMP_EDGE;
115 case GL_CLAMP_TO_BORDER:
116 return TEXCOORDMODE_CLAMP_BORDER;
117 case GL_MIRRORED_REPEAT:
118 return TEXCOORDMODE_MIRROR;
119 default:
120 return TEXCOORDMODE_WRAP;
121 }
122 }
123
124
125
126 /* Recalculate all state from scratch. Perhaps not the most
127 * efficient, but this has gotten complex enough that we need
128 * something which is understandable and reliable.
129 */
130 static GLboolean
131 i915_update_tex_unit(struct intel_context *intel, GLuint unit, GLuint ss3)
132 {
133 GLcontext *ctx = &intel->ctx;
134 struct i915_context *i915 = i915_context(ctx);
135 struct gl_texture_unit *tUnit = &ctx->Texture.Unit[unit];
136 struct gl_texture_object *tObj = tUnit->_Current;
137 struct intel_texture_object *intelObj = intel_texture_object(tObj);
138 struct gl_texture_image *firstImage;
139 GLuint *state = i915->state.Tex[unit], format, pitch;
140 GLint lodbias, aniso = 0;
141 GLubyte border[4];
142 GLfloat maxlod;
143
144 memset(state, 0, sizeof(state));
145
146 /*We need to refcount these. */
147
148 if (i915->state.tex_buffer[unit] != NULL) {
149 dri_bo_unreference(i915->state.tex_buffer[unit]);
150 i915->state.tex_buffer[unit] = NULL;
151 }
152
153 if (!intel_finalize_mipmap_tree(intel, unit))
154 return GL_FALSE;
155
156 /* Get first image here, since intelObj->firstLevel will get set in
157 * the intel_finalize_mipmap_tree() call above.
158 */
159 firstImage = tObj->Image[0][intelObj->firstLevel];
160
161 dri_bo_reference(intelObj->mt->region->buffer);
162 i915->state.tex_buffer[unit] = intelObj->mt->region->buffer;
163 i915->state.tex_offset[unit] = 0; /* Always the origin of the miptree */
164
165 format = translate_texture_format(firstImage->TexFormat,
166 firstImage->InternalFormat,
167 tObj->DepthMode);
168 pitch = intelObj->mt->region->pitch * intelObj->mt->cpp;
169
170 state[I915_TEXREG_MS3] =
171 (((firstImage->Height - 1) << MS3_HEIGHT_SHIFT) |
172 ((firstImage->Width - 1) << MS3_WIDTH_SHIFT) | format);
173
174 if (intelObj->mt->region->tiling != I915_TILING_NONE) {
175 state[I915_TEXREG_MS3] |= MS3_TILED_SURFACE;
176 if (intelObj->mt->region->tiling == I915_TILING_Y)
177 state[I915_TEXREG_MS3] |= MS3_TILE_WALK;
178 }
179
180 /* We get one field with fraction bits for the maximum addressable
181 * (lowest resolution) LOD. Use it to cover both MAX_LEVEL and
182 * MAX_LOD.
183 */
184 maxlod = MIN2(tObj->MaxLod, tObj->_MaxLevel - tObj->BaseLevel);
185 state[I915_TEXREG_MS4] =
186 ((((pitch / 4) - 1) << MS4_PITCH_SHIFT) |
187 MS4_CUBE_FACE_ENA_MASK |
188 (U_FIXED(CLAMP(maxlod, 0.0, 11.0), 2) << MS4_MAX_LOD_SHIFT) |
189 ((firstImage->Depth - 1) << 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 if (tObj->MaxAnisotropy > 2.0)
228 aniso = SS2_MAX_ANISO_4;
229 else
230 aniso = SS2_MAX_ANISO_2;
231 }
232 else {
233 switch (tObj->MagFilter) {
234 case GL_NEAREST:
235 magFilt = FILTER_NEAREST;
236 break;
237 case GL_LINEAR:
238 magFilt = FILTER_LINEAR;
239 break;
240 default:
241 return GL_FALSE;
242 }
243 }
244
245 lodbias = (int) ((tUnit->LodBias + tObj->LodBias) * 16.0);
246 if (lodbias < -256)
247 lodbias = -256;
248 if (lodbias > 255)
249 lodbias = 255;
250 state[I915_TEXREG_SS2] = ((lodbias << SS2_LOD_BIAS_SHIFT) &
251 SS2_LOD_BIAS_MASK);
252
253 /* YUV conversion:
254 */
255 if (firstImage->TexFormat == MESA_FORMAT_YCBCR ||
256 firstImage->TexFormat == MESA_FORMAT_YCBCR_REV)
257 state[I915_TEXREG_SS2] |= SS2_COLORSPACE_CONVERSION;
258
259 /* Shadow:
260 */
261 if (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB &&
262 tObj->Target != GL_TEXTURE_3D) {
263 if (tObj->Target == GL_TEXTURE_1D)
264 return GL_FALSE;
265
266 state[I915_TEXREG_SS2] |=
267 (SS2_SHADOW_ENABLE |
268 intel_translate_shadow_compare_func(tObj->CompareFunc));
269
270 minFilt = FILTER_4X4_FLAT;
271 magFilt = FILTER_4X4_FLAT;
272 }
273
274 state[I915_TEXREG_SS2] |= ((minFilt << SS2_MIN_FILTER_SHIFT) |
275 (mipFilt << SS2_MIP_FILTER_SHIFT) |
276 (magFilt << SS2_MAG_FILTER_SHIFT) |
277 aniso);
278 }
279
280 {
281 GLenum ws = tObj->WrapS;
282 GLenum wt = tObj->WrapT;
283 GLenum wr = tObj->WrapR;
284
285 /* We program 1D textures as 2D textures, so the 2D texcoord could
286 * result in sampling border values if we don't set the T wrap to
287 * repeat.
288 */
289 if (tObj->Target == GL_TEXTURE_1D)
290 wt = GL_REPEAT;
291
292 /* 3D textures don't seem to respect the border color.
293 * Fallback if there's ever a danger that they might refer to
294 * it.
295 *
296 * Effectively this means fallback on 3D clamp or
297 * clamp_to_border.
298 */
299 if (tObj->Target == GL_TEXTURE_3D &&
300 (tObj->MinFilter != GL_NEAREST ||
301 tObj->MagFilter != GL_NEAREST) &&
302 (ws == GL_CLAMP ||
303 wt == GL_CLAMP ||
304 wr == GL_CLAMP ||
305 ws == GL_CLAMP_TO_BORDER ||
306 wt == GL_CLAMP_TO_BORDER || wr == GL_CLAMP_TO_BORDER))
307 return GL_FALSE;
308
309 /* Only support TEXCOORDMODE_CLAMP_EDGE and TEXCOORDMODE_CUBE (not
310 * used) when using cube map texture coordinates
311 */
312 if (tObj->Target == GL_TEXTURE_CUBE_MAP_ARB &&
313 (((ws != GL_CLAMP) && (ws != GL_CLAMP_TO_EDGE)) ||
314 ((wt != GL_CLAMP) && (wt != GL_CLAMP_TO_EDGE))))
315 return GL_FALSE;
316
317 state[I915_TEXREG_SS3] = ss3; /* SS3_NORMALIZED_COORDS */
318
319 state[I915_TEXREG_SS3] |=
320 ((translate_wrap_mode(ws) << SS3_TCX_ADDR_MODE_SHIFT) |
321 (translate_wrap_mode(wt) << SS3_TCY_ADDR_MODE_SHIFT) |
322 (translate_wrap_mode(wr) << SS3_TCZ_ADDR_MODE_SHIFT));
323
324 state[I915_TEXREG_SS3] |= (unit << SS3_TEXTUREMAP_INDEX_SHIFT);
325 state[I915_TEXREG_SS3] |= (U_FIXED(CLAMP(tObj->MinLod, 0.0, 11.0), 4) <<
326 SS3_MIN_LOD_SHIFT);
327
328 }
329
330 /* convert border color from float to ubyte */
331 CLAMPED_FLOAT_TO_UBYTE(border[0], tObj->BorderColor.f[0]);
332 CLAMPED_FLOAT_TO_UBYTE(border[1], tObj->BorderColor.f[1]);
333 CLAMPED_FLOAT_TO_UBYTE(border[2], tObj->BorderColor.f[2]);
334 CLAMPED_FLOAT_TO_UBYTE(border[3], tObj->BorderColor.f[3]);
335
336 if (firstImage->_BaseFormat == GL_DEPTH_COMPONENT) {
337 /* GL specs that border color for depth textures is taken from the
338 * R channel, while the hardware uses A. Spam R into all the channels
339 * for safety.
340 */
341 state[I915_TEXREG_SS4] = PACK_COLOR_8888(border[0],
342 border[0],
343 border[0],
344 border[0]);
345 } else {
346 state[I915_TEXREG_SS4] = PACK_COLOR_8888(border[3],
347 border[0],
348 border[1],
349 border[2]);
350 }
351
352
353 I915_ACTIVESTATE(i915, I915_UPLOAD_TEX(unit), GL_TRUE);
354 /* memcmp was already disabled, but definitely won't work as the
355 * region might now change and that wouldn't be detected:
356 */
357 I915_STATECHANGE(i915, I915_UPLOAD_TEX(unit));
358
359
360 #if 0
361 DBG(TEXTURE, "state[I915_TEXREG_SS2] = 0x%x\n", state[I915_TEXREG_SS2]);
362 DBG(TEXTURE, "state[I915_TEXREG_SS3] = 0x%x\n", state[I915_TEXREG_SS3]);
363 DBG(TEXTURE, "state[I915_TEXREG_SS4] = 0x%x\n", state[I915_TEXREG_SS4]);
364 DBG(TEXTURE, "state[I915_TEXREG_MS2] = 0x%x\n", state[I915_TEXREG_MS2]);
365 DBG(TEXTURE, "state[I915_TEXREG_MS3] = 0x%x\n", state[I915_TEXREG_MS3]);
366 DBG(TEXTURE, "state[I915_TEXREG_MS4] = 0x%x\n", state[I915_TEXREG_MS4]);
367 #endif
368
369 return GL_TRUE;
370 }
371
372
373
374
375 void
376 i915UpdateTextureState(struct intel_context *intel)
377 {
378 GLboolean ok = GL_TRUE;
379 GLuint i;
380
381 for (i = 0; i < I915_TEX_UNITS && ok; i++) {
382 switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
383 case TEXTURE_1D_BIT:
384 case TEXTURE_2D_BIT:
385 case TEXTURE_CUBE_BIT:
386 case TEXTURE_3D_BIT:
387 ok = i915_update_tex_unit(intel, i, SS3_NORMALIZED_COORDS);
388 break;
389 case TEXTURE_RECT_BIT:
390 ok = i915_update_tex_unit(intel, i, 0);
391 break;
392 case 0:{
393 struct i915_context *i915 = i915_context(&intel->ctx);
394 if (i915->state.active & I915_UPLOAD_TEX(i))
395 I915_ACTIVESTATE(i915, I915_UPLOAD_TEX(i), GL_FALSE);
396
397 if (i915->state.tex_buffer[i] != NULL) {
398 dri_bo_unreference(i915->state.tex_buffer[i]);
399 i915->state.tex_buffer[i] = NULL;
400 }
401
402 break;
403 }
404 default:
405 ok = GL_FALSE;
406 break;
407 }
408 }
409
410 FALLBACK(intel, I915_FALLBACK_TEXTURE, !ok);
411 }