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