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