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