i965: relAddr local var (to make debug/test a little easier)
[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 MS3_USE_FENCE_REGS);
190
191 state[I915_TEXREG_MS4] =
192 ((((pitch / 4) - 1) << MS4_PITCH_SHIFT) | MS4_CUBE_FACE_ENA_MASK |
193 ((((intelObj->lastLevel - intelObj->firstLevel) * 4)) <<
194 MS4_MAX_LOD_SHIFT) | ((firstImage->Depth - 1) <<
195 MS4_VOLUME_DEPTH_SHIFT));
196
197
198 {
199 GLuint minFilt, mipFilt, magFilt;
200
201 switch (tObj->MinFilter) {
202 case GL_NEAREST:
203 minFilt = FILTER_NEAREST;
204 mipFilt = MIPFILTER_NONE;
205 break;
206 case GL_LINEAR:
207 minFilt = FILTER_LINEAR;
208 mipFilt = MIPFILTER_NONE;
209 break;
210 case GL_NEAREST_MIPMAP_NEAREST:
211 minFilt = FILTER_NEAREST;
212 mipFilt = MIPFILTER_NEAREST;
213 break;
214 case GL_LINEAR_MIPMAP_NEAREST:
215 minFilt = FILTER_LINEAR;
216 mipFilt = MIPFILTER_NEAREST;
217 break;
218 case GL_NEAREST_MIPMAP_LINEAR:
219 minFilt = FILTER_NEAREST;
220 mipFilt = MIPFILTER_LINEAR;
221 break;
222 case GL_LINEAR_MIPMAP_LINEAR:
223 minFilt = FILTER_LINEAR;
224 mipFilt = MIPFILTER_LINEAR;
225 break;
226 default:
227 return GL_FALSE;
228 }
229
230 if (tObj->MaxAnisotropy > 1.0) {
231 minFilt = FILTER_ANISOTROPIC;
232 magFilt = FILTER_ANISOTROPIC;
233 if (tObj->MaxAnisotropy > 2.0)
234 aniso = SS2_MAX_ANISO_4;
235 else
236 aniso = SS2_MAX_ANISO_2;
237 }
238 else {
239 switch (tObj->MagFilter) {
240 case GL_NEAREST:
241 magFilt = FILTER_NEAREST;
242 break;
243 case GL_LINEAR:
244 magFilt = FILTER_LINEAR;
245 break;
246 default:
247 return GL_FALSE;
248 }
249 }
250
251 lodbias = (int) ((tUnit->LodBias + tObj->LodBias) * 16.0);
252 if (lodbias < -256)
253 lodbias = -256;
254 if (lodbias > 255)
255 lodbias = 255;
256 state[I915_TEXREG_SS2] = ((lodbias << SS2_LOD_BIAS_SHIFT) &
257 SS2_LOD_BIAS_MASK);
258
259 /* YUV conversion:
260 */
261 if (firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR ||
262 firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV)
263 state[I915_TEXREG_SS2] |= SS2_COLORSPACE_CONVERSION;
264
265 /* Shadow:
266 */
267 if (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB &&
268 tObj->Target != GL_TEXTURE_3D) {
269 if (tObj->Target == GL_TEXTURE_1D)
270 return GL_FALSE;
271
272 state[I915_TEXREG_SS2] |=
273 (SS2_SHADOW_ENABLE |
274 intel_translate_shadow_compare_func(tObj->CompareFunc));
275
276 minFilt = FILTER_4X4_FLAT;
277 magFilt = FILTER_4X4_FLAT;
278 }
279
280 state[I915_TEXREG_SS2] |= ((minFilt << SS2_MIN_FILTER_SHIFT) |
281 (mipFilt << SS2_MIP_FILTER_SHIFT) |
282 (magFilt << SS2_MAG_FILTER_SHIFT) |
283 aniso);
284 }
285
286 {
287 GLenum ws = tObj->WrapS;
288 GLenum wt = tObj->WrapT;
289 GLenum wr = tObj->WrapR;
290
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 }
326
327 /* convert border color from float to ubyte */
328 CLAMPED_FLOAT_TO_UBYTE(border[0], tObj->BorderColor[0]);
329 CLAMPED_FLOAT_TO_UBYTE(border[1], tObj->BorderColor[1]);
330 CLAMPED_FLOAT_TO_UBYTE(border[2], tObj->BorderColor[2]);
331 CLAMPED_FLOAT_TO_UBYTE(border[3], tObj->BorderColor[3]);
332
333 if (firstImage->_BaseFormat == GL_DEPTH_COMPONENT) {
334 /* GL specs that border color for depth textures is taken from the
335 * R channel, while the hardware uses A. Spam R into all the channels
336 * for safety.
337 */
338 state[I915_TEXREG_SS4] = INTEL_PACKCOLOR8888(border[0],
339 border[0],
340 border[0],
341 border[0]);
342 } else {
343 state[I915_TEXREG_SS4] = INTEL_PACKCOLOR8888(border[0],
344 border[1],
345 border[2],
346 border[3]);
347 }
348
349
350 I915_ACTIVESTATE(i915, I915_UPLOAD_TEX(unit), GL_TRUE);
351 /* memcmp was already disabled, but definitely won't work as the
352 * region might now change and that wouldn't be detected:
353 */
354 I915_STATECHANGE(i915, I915_UPLOAD_TEX(unit));
355
356
357 #if 0
358 DBG(TEXTURE, "state[I915_TEXREG_SS2] = 0x%x\n", state[I915_TEXREG_SS2]);
359 DBG(TEXTURE, "state[I915_TEXREG_SS3] = 0x%x\n", state[I915_TEXREG_SS3]);
360 DBG(TEXTURE, "state[I915_TEXREG_SS4] = 0x%x\n", state[I915_TEXREG_SS4]);
361 DBG(TEXTURE, "state[I915_TEXREG_MS2] = 0x%x\n", state[I915_TEXREG_MS2]);
362 DBG(TEXTURE, "state[I915_TEXREG_MS3] = 0x%x\n", state[I915_TEXREG_MS3]);
363 DBG(TEXTURE, "state[I915_TEXREG_MS4] = 0x%x\n", state[I915_TEXREG_MS4]);
364 #endif
365
366 return GL_TRUE;
367 }
368
369
370
371
372 void
373 i915UpdateTextureState(struct intel_context *intel)
374 {
375 GLboolean ok = GL_TRUE;
376 GLuint i;
377
378 for (i = 0; i < I915_TEX_UNITS && ok; i++) {
379 switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
380 case TEXTURE_1D_BIT:
381 case TEXTURE_2D_BIT:
382 case TEXTURE_CUBE_BIT:
383 case TEXTURE_3D_BIT:
384 ok = i915_update_tex_unit(intel, i, SS3_NORMALIZED_COORDS);
385 break;
386 case TEXTURE_RECT_BIT:
387 ok = i915_update_tex_unit(intel, i, 0);
388 break;
389 case 0:{
390 struct i915_context *i915 = i915_context(&intel->ctx);
391 if (i915->state.active & I915_UPLOAD_TEX(i))
392 I915_ACTIVESTATE(i915, I915_UPLOAD_TEX(i), GL_FALSE);
393
394 if (i915->state.tex_buffer[i] != NULL) {
395 dri_bo_unreference(i915->state.tex_buffer[i]);
396 i915->state.tex_buffer[i] = NULL;
397 }
398
399 break;
400 }
401 default:
402 ok = GL_FALSE;
403 break;
404 }
405 }
406
407 FALLBACK(intel, I915_FALLBACK_TEXTURE, !ok);
408 }