radeon: add support for new ttm
[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;
136
137 memset(state, 0, sizeof(state));
138
139 /*We need to refcount these. */
140
141 if (i915->state.tex_buffer[unit] != NULL) {
142 dri_bo_unreference(i915->state.tex_buffer[unit]);
143 i915->state.tex_buffer[unit] = NULL;
144 }
145
146 if (!intelObj->imageOverride && !intel_finalize_mipmap_tree(intel, unit))
147 return GL_FALSE;
148
149 /* Get first image here, since intelObj->firstLevel will get set in
150 * the intel_finalize_mipmap_tree() call above.
151 */
152 firstImage = tObj->Image[0][intelObj->firstLevel];
153
154 if (intelObj->imageOverride) {
155 i915->state.tex_buffer[unit] = NULL;
156 i915->state.tex_offset[unit] = intelObj->textureOffset;
157
158 switch (intelObj->depthOverride) {
159 case 32:
160 format = MAPSURF_32BIT | MT_32BIT_ARGB8888;
161 break;
162 case 24:
163 default:
164 format = MAPSURF_32BIT | MT_32BIT_XRGB8888;
165 break;
166 case 16:
167 format = MAPSURF_16BIT | MT_16BIT_RGB565;
168 break;
169 }
170
171 pitch = intelObj->pitchOverride;
172 } else {
173 dri_bo_reference(intelObj->mt->region->buffer);
174 i915->state.tex_buffer[unit] = intelObj->mt->region->buffer;
175 i915->state.tex_offset[unit] = intel_miptree_image_offset(intelObj->mt,
176 0, intelObj->
177 firstLevel);
178
179 format = translate_texture_format(firstImage->TexFormat->MesaFormat,
180 firstImage->InternalFormat,
181 tObj->DepthMode);
182 pitch = intelObj->mt->pitch * intelObj->mt->cpp;
183 }
184
185 state[I915_TEXREG_MS3] =
186 (((firstImage->Height - 1) << MS3_HEIGHT_SHIFT) |
187 ((firstImage->Width - 1) << MS3_WIDTH_SHIFT) | format |
188 MS3_USE_FENCE_REGS);
189
190 state[I915_TEXREG_MS4] =
191 ((((pitch / 4) - 1) << MS4_PITCH_SHIFT) | MS4_CUBE_FACE_ENA_MASK |
192 ((((intelObj->lastLevel - intelObj->firstLevel) * 4)) <<
193 MS4_MAX_LOD_SHIFT) | ((firstImage->Depth - 1) <<
194 MS4_VOLUME_DEPTH_SHIFT));
195
196
197 {
198 GLuint minFilt, mipFilt, magFilt;
199
200 switch (tObj->MinFilter) {
201 case GL_NEAREST:
202 minFilt = FILTER_NEAREST;
203 mipFilt = MIPFILTER_NONE;
204 break;
205 case GL_LINEAR:
206 minFilt = FILTER_LINEAR;
207 mipFilt = MIPFILTER_NONE;
208 break;
209 case GL_NEAREST_MIPMAP_NEAREST:
210 minFilt = FILTER_NEAREST;
211 mipFilt = MIPFILTER_NEAREST;
212 break;
213 case GL_LINEAR_MIPMAP_NEAREST:
214 minFilt = FILTER_LINEAR;
215 mipFilt = MIPFILTER_NEAREST;
216 break;
217 case GL_NEAREST_MIPMAP_LINEAR:
218 minFilt = FILTER_NEAREST;
219 mipFilt = MIPFILTER_LINEAR;
220 break;
221 case GL_LINEAR_MIPMAP_LINEAR:
222 minFilt = FILTER_LINEAR;
223 mipFilt = MIPFILTER_LINEAR;
224 break;
225 default:
226 return GL_FALSE;
227 }
228
229 if (tObj->MaxAnisotropy > 1.0) {
230 minFilt = FILTER_ANISOTROPIC;
231 magFilt = FILTER_ANISOTROPIC;
232 }
233 else {
234 switch (tObj->MagFilter) {
235 case GL_NEAREST:
236 magFilt = FILTER_NEAREST;
237 break;
238 case GL_LINEAR:
239 magFilt = FILTER_LINEAR;
240 break;
241 default:
242 return GL_FALSE;
243 }
244 }
245
246 lodbias = (int) ((tUnit->LodBias + tObj->LodBias) * 16.0);
247 if (lodbias < -256)
248 lodbias = -256;
249 if (lodbias > 255)
250 lodbias = 255;
251 state[I915_TEXREG_SS2] = ((lodbias << SS2_LOD_BIAS_SHIFT) &
252 SS2_LOD_BIAS_MASK);
253
254 /* YUV conversion:
255 */
256 if (firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR ||
257 firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV)
258 state[I915_TEXREG_SS2] |= SS2_COLORSPACE_CONVERSION;
259
260 /* Shadow:
261 */
262 if (tObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB &&
263 tObj->Target != GL_TEXTURE_3D) {
264 if (tObj->Target == GL_TEXTURE_1D)
265 return GL_FALSE;
266
267 state[I915_TEXREG_SS2] |=
268 (SS2_SHADOW_ENABLE |
269 intel_translate_shadow_compare_func(tObj->CompareFunc));
270
271 minFilt = FILTER_4X4_FLAT;
272 magFilt = FILTER_4X4_FLAT;
273 }
274
275 state[I915_TEXREG_SS2] |= ((minFilt << SS2_MIN_FILTER_SHIFT) |
276 (mipFilt << SS2_MIP_FILTER_SHIFT) |
277 (magFilt << SS2_MAG_FILTER_SHIFT));
278 }
279
280 {
281 GLenum ws = tObj->WrapS;
282 GLenum wt = tObj->WrapT;
283 GLenum wr = tObj->WrapR;
284
285
286 /* 3D textures don't seem to respect the border color.
287 * Fallback if there's ever a danger that they might refer to
288 * it.
289 *
290 * Effectively this means fallback on 3D clamp or
291 * clamp_to_border.
292 */
293 if (tObj->Target == GL_TEXTURE_3D &&
294 (tObj->MinFilter != GL_NEAREST ||
295 tObj->MagFilter != GL_NEAREST) &&
296 (ws == GL_CLAMP ||
297 wt == GL_CLAMP ||
298 wr == GL_CLAMP ||
299 ws == GL_CLAMP_TO_BORDER ||
300 wt == GL_CLAMP_TO_BORDER || wr == GL_CLAMP_TO_BORDER))
301 return GL_FALSE;
302
303 /* Only support TEXCOORDMODE_CLAMP_EDGE and TEXCOORDMODE_CUBE (not
304 * used) when using cube map texture coordinates
305 */
306 if (tObj->Target == GL_TEXTURE_CUBE_MAP_ARB &&
307 (((ws != GL_CLAMP) && (ws != GL_CLAMP_TO_EDGE)) ||
308 ((wt != GL_CLAMP) && (wt != GL_CLAMP_TO_EDGE))))
309 return GL_FALSE;
310
311 state[I915_TEXREG_SS3] = ss3; /* SS3_NORMALIZED_COORDS */
312
313 state[I915_TEXREG_SS3] |=
314 ((translate_wrap_mode(ws) << SS3_TCX_ADDR_MODE_SHIFT) |
315 (translate_wrap_mode(wt) << SS3_TCY_ADDR_MODE_SHIFT) |
316 (translate_wrap_mode(wr) << SS3_TCZ_ADDR_MODE_SHIFT));
317
318 state[I915_TEXREG_SS3] |= (unit << SS3_TEXTUREMAP_INDEX_SHIFT);
319 }
320
321
322 if (firstImage->_BaseFormat == GL_DEPTH_COMPONENT) {
323 /* GL specs that border color for depth textures is taken from the
324 * R channel, while the hardware uses A. Spam R into all the channels
325 * for safety.
326 */
327 state[I915_TEXREG_SS4] = INTEL_PACKCOLOR8888(tObj->_BorderChan[0],
328 tObj->_BorderChan[0],
329 tObj->_BorderChan[0],
330 tObj->_BorderChan[0]);
331 } else {
332 state[I915_TEXREG_SS4] = INTEL_PACKCOLOR8888(tObj->_BorderChan[0],
333 tObj->_BorderChan[1],
334 tObj->_BorderChan[2],
335 tObj->_BorderChan[3]);
336 }
337
338
339 I915_ACTIVESTATE(i915, I915_UPLOAD_TEX(unit), GL_TRUE);
340 /* memcmp was already disabled, but definitely won't work as the
341 * region might now change and that wouldn't be detected:
342 */
343 I915_STATECHANGE(i915, I915_UPLOAD_TEX(unit));
344
345
346 #if 0
347 DBG(TEXTURE, "state[I915_TEXREG_SS2] = 0x%x\n", state[I915_TEXREG_SS2]);
348 DBG(TEXTURE, "state[I915_TEXREG_SS3] = 0x%x\n", state[I915_TEXREG_SS3]);
349 DBG(TEXTURE, "state[I915_TEXREG_SS4] = 0x%x\n", state[I915_TEXREG_SS4]);
350 DBG(TEXTURE, "state[I915_TEXREG_MS2] = 0x%x\n", state[I915_TEXREG_MS2]);
351 DBG(TEXTURE, "state[I915_TEXREG_MS3] = 0x%x\n", state[I915_TEXREG_MS3]);
352 DBG(TEXTURE, "state[I915_TEXREG_MS4] = 0x%x\n", state[I915_TEXREG_MS4]);
353 #endif
354
355 return GL_TRUE;
356 }
357
358
359
360
361 void
362 i915UpdateTextureState(struct intel_context *intel)
363 {
364 GLboolean ok = GL_TRUE;
365 GLuint i;
366
367 for (i = 0; i < I915_TEX_UNITS && ok; i++) {
368 switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
369 case TEXTURE_1D_BIT:
370 case TEXTURE_2D_BIT:
371 case TEXTURE_CUBE_BIT:
372 case TEXTURE_3D_BIT:
373 ok = i915_update_tex_unit(intel, i, SS3_NORMALIZED_COORDS);
374 break;
375 case TEXTURE_RECT_BIT:
376 ok = i915_update_tex_unit(intel, i, 0);
377 break;
378 case 0:{
379 struct i915_context *i915 = i915_context(&intel->ctx);
380 if (i915->state.active & I915_UPLOAD_TEX(i))
381 I915_ACTIVESTATE(i915, I915_UPLOAD_TEX(i), GL_FALSE);
382
383 if (i915->state.tex_buffer[i] != NULL) {
384 dri_bo_unreference(i915->state.tex_buffer[i]);
385 i915->state.tex_buffer[i] = NULL;
386 }
387
388 break;
389 }
390 default:
391 ok = GL_FALSE;
392 break;
393 }
394 }
395
396 FALLBACK(intel, I915_FALLBACK_TEXTURE, !ok);
397 }