Merge commit 'origin/gallium-0.1'
[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)
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 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_RGBA_DXT1:
68 case MESA_FORMAT_RGB_DXT1:
69 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT1);
70 case MESA_FORMAT_RGBA_DXT3:
71 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT2_3);
72 case MESA_FORMAT_RGBA_DXT5:
73 return (MAPSURF_COMPRESSED | MT_COMPRESS_DXT4_5);
74 default:
75 fprintf(stderr, "%s: bad image format %x\n", __FUNCTION__, mesa_format);
76 abort();
77 return 0;
78 }
79 }
80
81
82
83
84 /* The i915 (and related graphics cores) do not support GL_CLAMP. The
85 * Intel drivers for "other operating systems" implement GL_CLAMP as
86 * GL_CLAMP_TO_EDGE, so the same is done here.
87 */
88 static GLuint
89 translate_wrap_mode(GLenum wrap)
90 {
91 switch (wrap) {
92 case GL_REPEAT:
93 return TEXCOORDMODE_WRAP;
94 case GL_CLAMP:
95 case GL_CLAMP_TO_EDGE:
96 return TEXCOORDMODE_CLAMP; /* not really correct */
97 case GL_CLAMP_TO_BORDER:
98 return TEXCOORDMODE_CLAMP_BORDER;
99 case GL_MIRRORED_REPEAT:
100 return TEXCOORDMODE_MIRROR;
101 default:
102 return TEXCOORDMODE_WRAP;
103 }
104 }
105
106
107 /* Recalculate all state from scratch. Perhaps not the most
108 * efficient, but this has gotten complex enough that we need
109 * something which is understandable and reliable.
110 */
111 static GLboolean
112 i830_update_tex_unit(struct intel_context *intel, GLuint unit, GLuint ss3)
113 {
114 GLcontext *ctx = &intel->ctx;
115 struct i830_context *i830 = i830_context(ctx);
116 struct gl_texture_unit *tUnit = &ctx->Texture.Unit[unit];
117 struct gl_texture_object *tObj = tUnit->_Current;
118 struct intel_texture_object *intelObj = intel_texture_object(tObj);
119 struct gl_texture_image *firstImage;
120 GLuint *state = i830->state.Tex[unit], format, pitch;
121 GLint lodbias;
122
123 memset(state, 0, sizeof(state));
124
125 /*We need to refcount these. */
126
127 if (i830->state.tex_buffer[unit] != NULL) {
128 dri_bo_unreference(i830->state.tex_buffer[unit]);
129 i830->state.tex_buffer[unit] = NULL;
130 }
131
132 if (!intelObj->imageOverride && !intel_finalize_mipmap_tree(intel, unit))
133 return GL_FALSE;
134
135 /* Get first image here, since intelObj->firstLevel will get set in
136 * the intel_finalize_mipmap_tree() call above.
137 */
138 firstImage = tObj->Image[0][intelObj->firstLevel];
139
140 if (intelObj->imageOverride) {
141 i830->state.tex_buffer[unit] = NULL;
142 i830->state.tex_offset[unit] = intelObj->textureOffset;
143
144 switch (intelObj->depthOverride) {
145 case 32:
146 format = MAPSURF_32BIT | MT_32BIT_ARGB8888;
147 break;
148 case 24:
149 default:
150 format = MAPSURF_32BIT | MT_32BIT_XRGB8888;
151 break;
152 case 16:
153 format = MAPSURF_16BIT | MT_16BIT_RGB565;
154 break;
155 }
156
157 pitch = intelObj->pitchOverride;
158 } else {
159 dri_bo_reference(intelObj->mt->region->buffer);
160 i830->state.tex_buffer[unit] = intelObj->mt->region->buffer;
161 i830->state.tex_offset[unit] = intel_miptree_image_offset(intelObj->mt,
162 0, intelObj->
163 firstLevel);
164
165 format = translate_texture_format(firstImage->TexFormat->MesaFormat);
166 pitch = intelObj->mt->pitch * intelObj->mt->cpp;
167 }
168
169 state[I830_TEXREG_TM0LI] = (_3DSTATE_LOAD_STATE_IMMEDIATE_2 |
170 (LOAD_TEXTURE_MAP0 << unit) | 4);
171
172 /* state[I830_TEXREG_TM0S0] = (TM0S0_USE_FENCE | */
173 /* t->intel.TextureOffset); */
174
175
176 state[I830_TEXREG_TM0S1] =
177 (((firstImage->Height - 1) << TM0S1_HEIGHT_SHIFT) |
178 ((firstImage->Width - 1) << TM0S1_WIDTH_SHIFT) | format);
179
180 state[I830_TEXREG_TM0S2] =
181 ((((pitch / 4) - 1) << TM0S2_PITCH_SHIFT) | TM0S2_CUBE_FACE_ENA_MASK);
182
183 {
184 if (tObj->Target == GL_TEXTURE_CUBE_MAP)
185 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit) |
186 CUBE_NEGX_ENABLE |
187 CUBE_POSX_ENABLE |
188 CUBE_NEGY_ENABLE |
189 CUBE_POSY_ENABLE |
190 CUBE_NEGZ_ENABLE | CUBE_POSZ_ENABLE);
191 else
192 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit));
193 }
194
195
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 }
234 else {
235 switch (tObj->MagFilter) {
236 case GL_NEAREST:
237 magFilt = FILTER_NEAREST;
238 break;
239 case GL_LINEAR:
240 magFilt = FILTER_LINEAR;
241 break;
242 default:
243 return GL_FALSE;
244 }
245 }
246
247 lodbias = (int) ((tUnit->LodBias + tObj->LodBias) * 16.0);
248 if (lodbias < -64)
249 lodbias = -64;
250 if (lodbias > 63)
251 lodbias = 63;
252
253 state[I830_TEXREG_TM0S3] = ((lodbias << TM0S3_LOD_BIAS_SHIFT) &
254 TM0S3_LOD_BIAS_MASK);
255 #if 0
256 /* YUV conversion:
257 */
258 if (firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR ||
259 firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV)
260 state[I830_TEXREG_TM0S3] |= SS2_COLORSPACE_CONVERSION;
261 #endif
262
263 state[I830_TEXREG_TM0S3] |= ((intelObj->lastLevel -
264 intelObj->firstLevel) *
265 4) << TM0S3_MIN_MIP_SHIFT;
266
267 state[I830_TEXREG_TM0S3] |= ((minFilt << TM0S3_MIN_FILTER_SHIFT) |
268 (mipFilt << TM0S3_MIP_FILTER_SHIFT) |
269 (magFilt << TM0S3_MAG_FILTER_SHIFT));
270 }
271
272 {
273 GLenum ws = tObj->WrapS;
274 GLenum wt = tObj->WrapT;
275
276
277 /* 3D textures not available on i830
278 */
279 if (tObj->Target == GL_TEXTURE_3D)
280 return GL_FALSE;
281
282 state[I830_TEXREG_MCS] = (_3DSTATE_MAP_COORD_SET_CMD |
283 MAP_UNIT(unit) |
284 ENABLE_TEXCOORD_PARAMS |
285 ss3 |
286 ENABLE_ADDR_V_CNTL |
287 TEXCOORD_ADDR_V_MODE(translate_wrap_mode(wt))
288 | ENABLE_ADDR_U_CNTL |
289 TEXCOORD_ADDR_U_MODE(translate_wrap_mode
290 (ws)));
291 }
292
293
294 state[I830_TEXREG_TM0S4] = INTEL_PACKCOLOR8888(tObj->_BorderChan[0],
295 tObj->_BorderChan[1],
296 tObj->_BorderChan[2],
297 tObj->_BorderChan[3]);
298
299
300 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(unit), GL_TRUE);
301 /* memcmp was already disabled, but definitely won't work as the
302 * region might now change and that wouldn't be detected:
303 */
304 I830_STATECHANGE(i830, I830_UPLOAD_TEX(unit));
305 return GL_TRUE;
306 }
307
308
309
310
311 void
312 i830UpdateTextureState(struct intel_context *intel)
313 {
314 struct i830_context *i830 = i830_context(&intel->ctx);
315 GLboolean ok = GL_TRUE;
316 GLuint i;
317
318 for (i = 0; i < I830_TEX_UNITS && ok; i++) {
319 switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
320 case TEXTURE_1D_BIT:
321 case TEXTURE_2D_BIT:
322 case TEXTURE_CUBE_BIT:
323 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_NORMAL);
324 break;
325 case TEXTURE_RECT_BIT:
326 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_IN_TEXELUNITS);
327 break;
328 case 0:{
329 struct i830_context *i830 = i830_context(&intel->ctx);
330 if (i830->state.active & I830_UPLOAD_TEX(i))
331 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(i), GL_FALSE);
332
333 if (i830->state.tex_buffer[i] != NULL) {
334 dri_bo_unreference(i830->state.tex_buffer[i]);
335 i830->state.tex_buffer[i] = NULL;
336 }
337 break;
338 }
339 case TEXTURE_3D_BIT:
340 default:
341 ok = GL_FALSE;
342 break;
343 }
344 }
345
346 FALLBACK(intel, I830_FALLBACK_TEXTURE, !ok);
347
348 if (ok)
349 i830EmitTextureBlend(i830);
350 }