Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / drivers / dri / i915 / i830_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 "i830_context.h"
36 #include "i830_reg.h"
37
38
39
40 static GLuint
41 translate_texture_format(GLuint mesa_format, GLuint internal_format)
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_I8; /* Kludge! */
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_RGBA_DXT1:
71 case MESA_FORMAT_RGB_DXT1:
72 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1);
73 case MESA_FORMAT_RGBA_DXT3:
74 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT2_3);
75 case MESA_FORMAT_RGBA_DXT5:
76 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT4_5);
77 default:
78 fprintf(stderr, "%s: bad image format %x\n", __FUNCTION__, mesa_format);
79 abort();
80 return 0;
81 }
82 }
83
84
85
86
87 /* The i915 (and related graphics cores) do not support GL_CLAMP. The
88 * Intel drivers for "other operating systems" implement GL_CLAMP as
89 * GL_CLAMP_TO_EDGE, so the same is done here.
90 */
91 static GLuint
92 translate_wrap_mode(GLenum wrap)
93 {
94 switch (wrap) {
95 case GL_REPEAT:
96 return TEXCOORDMODE_WRAP;
97 case GL_CLAMP:
98 case GL_CLAMP_TO_EDGE:
99 return TEXCOORDMODE_CLAMP; /* not really correct */
100 case GL_CLAMP_TO_BORDER:
101 return TEXCOORDMODE_CLAMP_BORDER;
102 case GL_MIRRORED_REPEAT:
103 return TEXCOORDMODE_MIRROR;
104 default:
105 return TEXCOORDMODE_WRAP;
106 }
107 }
108
109
110 /* Recalculate all state from scratch. Perhaps not the most
111 * efficient, but this has gotten complex enough that we need
112 * something which is understandable and reliable.
113 */
114 static GLboolean
115 i830_update_tex_unit(struct intel_context *intel, GLuint unit, GLuint ss3)
116 {
117 GLcontext *ctx = &intel->ctx;
118 struct i830_context *i830 = i830_context(ctx);
119 struct gl_texture_unit *tUnit = &ctx->Texture.Unit[unit];
120 struct gl_texture_object *tObj = tUnit->_Current;
121 struct intel_texture_object *intelObj = intel_texture_object(tObj);
122 struct gl_texture_image *firstImage;
123 GLuint *state = i830->state.Tex[unit], format, pitch;
124 GLint lodbias;
125 GLubyte border[4];
126
127 memset(state, 0, sizeof(state));
128
129 /*We need to refcount these. */
130
131 if (i830->state.tex_buffer[unit] != NULL) {
132 dri_bo_unreference(i830->state.tex_buffer[unit]);
133 i830->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 i830->state.tex_buffer[unit] = NULL;
146 i830->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 i830->state.tex_buffer[unit] = intelObj->mt->region->buffer;
165 i830->state.tex_offset[unit] =
166 intel_miptree_image_offset(intelObj->mt, 0, intelObj->firstLevel, 0);
167
168 format = translate_texture_format(firstImage->TexFormat->MesaFormat,
169 firstImage->InternalFormat);
170 pitch = intelObj->mt->pitch * intelObj->mt->cpp;
171 }
172
173 state[I830_TEXREG_TM0LI] = (_3DSTATE_LOAD_STATE_IMMEDIATE_2 |
174 (LOAD_TEXTURE_MAP0 << unit) | 4);
175
176 state[I830_TEXREG_TM0S1] =
177 (((firstImage->Height - 1) << TM0S1_HEIGHT_SHIFT) |
178 ((firstImage->Width - 1) << TM0S1_WIDTH_SHIFT) | format);
179
180 if (intelObj->mt->region->tiling != I915_TILING_NONE) {
181 state[I830_TEXREG_TM0S1] |= TM0S1_TILED_SURFACE;
182 if (intelObj->mt->region->tiling == I915_TILING_Y)
183 state[I830_TEXREG_TM0S1] |= TM0S1_TILE_WALK;
184 }
185
186 state[I830_TEXREG_TM0S2] =
187 ((((pitch / 4) - 1) << TM0S2_PITCH_SHIFT) | TM0S2_CUBE_FACE_ENA_MASK);
188
189 {
190 if (tObj->Target == GL_TEXTURE_CUBE_MAP)
191 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit) |
192 CUBE_NEGX_ENABLE |
193 CUBE_POSX_ENABLE |
194 CUBE_NEGY_ENABLE |
195 CUBE_POSY_ENABLE |
196 CUBE_NEGZ_ENABLE | CUBE_POSZ_ENABLE);
197 else
198 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit));
199 }
200
201
202
203
204 {
205 GLuint minFilt, mipFilt, magFilt;
206
207 switch (tObj->MinFilter) {
208 case GL_NEAREST:
209 minFilt = FILTER_NEAREST;
210 mipFilt = MIPFILTER_NONE;
211 break;
212 case GL_LINEAR:
213 minFilt = FILTER_LINEAR;
214 mipFilt = MIPFILTER_NONE;
215 break;
216 case GL_NEAREST_MIPMAP_NEAREST:
217 minFilt = FILTER_NEAREST;
218 mipFilt = MIPFILTER_NEAREST;
219 break;
220 case GL_LINEAR_MIPMAP_NEAREST:
221 minFilt = FILTER_LINEAR;
222 mipFilt = MIPFILTER_NEAREST;
223 break;
224 case GL_NEAREST_MIPMAP_LINEAR:
225 minFilt = FILTER_NEAREST;
226 mipFilt = MIPFILTER_LINEAR;
227 break;
228 case GL_LINEAR_MIPMAP_LINEAR:
229 minFilt = FILTER_LINEAR;
230 mipFilt = MIPFILTER_LINEAR;
231 break;
232 default:
233 return GL_FALSE;
234 }
235
236 if (tObj->MaxAnisotropy > 1.0) {
237 minFilt = FILTER_ANISOTROPIC;
238 magFilt = FILTER_ANISOTROPIC;
239 }
240 else {
241 switch (tObj->MagFilter) {
242 case GL_NEAREST:
243 magFilt = FILTER_NEAREST;
244 break;
245 case GL_LINEAR:
246 magFilt = FILTER_LINEAR;
247 break;
248 default:
249 return GL_FALSE;
250 }
251 }
252
253 lodbias = (int) ((tUnit->LodBias + tObj->LodBias) * 16.0);
254 if (lodbias < -64)
255 lodbias = -64;
256 if (lodbias > 63)
257 lodbias = 63;
258
259 state[I830_TEXREG_TM0S3] = ((lodbias << TM0S3_LOD_BIAS_SHIFT) &
260 TM0S3_LOD_BIAS_MASK);
261 #if 0
262 /* YUV conversion:
263 */
264 if (firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR ||
265 firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV)
266 state[I830_TEXREG_TM0S3] |= SS2_COLORSPACE_CONVERSION;
267 #endif
268
269 state[I830_TEXREG_TM0S3] |= ((intelObj->lastLevel -
270 intelObj->firstLevel) *
271 4) << TM0S3_MIN_MIP_SHIFT;
272
273 state[I830_TEXREG_TM0S3] |= ((minFilt << TM0S3_MIN_FILTER_SHIFT) |
274 (mipFilt << TM0S3_MIP_FILTER_SHIFT) |
275 (magFilt << TM0S3_MAG_FILTER_SHIFT));
276 }
277
278 {
279 GLenum ws = tObj->WrapS;
280 GLenum wt = tObj->WrapT;
281
282
283 /* 3D textures not available on i830
284 */
285 if (tObj->Target == GL_TEXTURE_3D)
286 return GL_FALSE;
287
288 state[I830_TEXREG_MCS] = (_3DSTATE_MAP_COORD_SET_CMD |
289 MAP_UNIT(unit) |
290 ENABLE_TEXCOORD_PARAMS |
291 ss3 |
292 ENABLE_ADDR_V_CNTL |
293 TEXCOORD_ADDR_V_MODE(translate_wrap_mode(wt))
294 | ENABLE_ADDR_U_CNTL |
295 TEXCOORD_ADDR_U_MODE(translate_wrap_mode
296 (ws)));
297 }
298
299 /* convert border color from float to ubyte */
300 CLAMPED_FLOAT_TO_UBYTE(border[0], tObj->BorderColor[0]);
301 CLAMPED_FLOAT_TO_UBYTE(border[1], tObj->BorderColor[1]);
302 CLAMPED_FLOAT_TO_UBYTE(border[2], tObj->BorderColor[2]);
303 CLAMPED_FLOAT_TO_UBYTE(border[3], tObj->BorderColor[3]);
304
305 state[I830_TEXREG_TM0S4] = INTEL_PACKCOLOR8888(border[0],
306 border[1],
307 border[2],
308 border[3]);
309
310
311 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(unit), GL_TRUE);
312 /* memcmp was already disabled, but definitely won't work as the
313 * region might now change and that wouldn't be detected:
314 */
315 I830_STATECHANGE(i830, I830_UPLOAD_TEX(unit));
316 return GL_TRUE;
317 }
318
319
320
321
322 void
323 i830UpdateTextureState(struct intel_context *intel)
324 {
325 struct i830_context *i830 = i830_context(&intel->ctx);
326 GLboolean ok = GL_TRUE;
327 GLuint i;
328
329 for (i = 0; i < I830_TEX_UNITS && ok; i++) {
330 switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
331 case TEXTURE_1D_BIT:
332 case TEXTURE_2D_BIT:
333 case TEXTURE_CUBE_BIT:
334 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_NORMAL);
335 break;
336 case TEXTURE_RECT_BIT:
337 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_IN_TEXELUNITS);
338 break;
339 case 0:{
340 struct i830_context *i830 = i830_context(&intel->ctx);
341 if (i830->state.active & I830_UPLOAD_TEX(i))
342 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(i), GL_FALSE);
343
344 if (i830->state.tex_buffer[i] != NULL) {
345 dri_bo_unreference(i830->state.tex_buffer[i]);
346 i830->state.tex_buffer[i] = NULL;
347 }
348 break;
349 }
350 case TEXTURE_3D_BIT:
351 default:
352 ok = GL_FALSE;
353 break;
354 }
355 }
356
357 FALLBACK(intel, I830_FALLBACK_TEXTURE, !ok);
358
359 if (ok)
360 i830EmitTextureBlend(i830);
361 }