Merge branch 'texformat-rework'
[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
31 #include "intel_mipmap_tree.h"
32 #include "intel_tex.h"
33
34 #include "i830_context.h"
35 #include "i830_reg.h"
36
37
38
39 static GLuint
40 translate_texture_format(GLuint mesa_format, GLuint internal_format)
41 {
42 switch (mesa_format) {
43 case MESA_FORMAT_L8:
44 return MAPSURF_8BIT | MT_8BIT_L8;
45 case MESA_FORMAT_I8:
46 return MAPSURF_8BIT | MT_8BIT_I8;
47 case MESA_FORMAT_A8:
48 return MAPSURF_8BIT | MT_8BIT_I8; /* Kludge! */
49 case MESA_FORMAT_AL88:
50 return MAPSURF_16BIT | MT_16BIT_AY88;
51 case MESA_FORMAT_RGB565:
52 return MAPSURF_16BIT | MT_16BIT_RGB565;
53 case MESA_FORMAT_ARGB1555:
54 return MAPSURF_16BIT | MT_16BIT_ARGB1555;
55 case MESA_FORMAT_ARGB4444:
56 return MAPSURF_16BIT | MT_16BIT_ARGB4444;
57 case MESA_FORMAT_ARGB8888:
58 if (internal_format == GL_RGB)
59 return MAPSURF_32BIT | MT_32BIT_XRGB8888;
60 else
61 return MAPSURF_32BIT | MT_32BIT_ARGB8888;
62 case MESA_FORMAT_YCBCR_REV:
63 return (MAPSURF_422 | MT_422_YCRCB_NORMAL);
64 case MESA_FORMAT_YCBCR:
65 return (MAPSURF_422 | MT_422_YCRCB_SWAPY);
66 case MESA_FORMAT_RGB_FXT1:
67 case MESA_FORMAT_RGBA_FXT1:
68 return (MAPSURF_COMPRESSED | MT_COMPRESS_FXT1);
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 default:
77 fprintf(stderr, "%s: bad image format %x\n", __FUNCTION__, mesa_format);
78 abort();
79 return 0;
80 }
81 }
82
83
84
85
86 /* The i915 (and related graphics cores) do not support GL_CLAMP. The
87 * Intel drivers for "other operating systems" implement GL_CLAMP as
88 * GL_CLAMP_TO_EDGE, so the same is done here.
89 */
90 static GLuint
91 translate_wrap_mode(GLenum wrap)
92 {
93 switch (wrap) {
94 case GL_REPEAT:
95 return TEXCOORDMODE_WRAP;
96 case GL_CLAMP:
97 case GL_CLAMP_TO_EDGE:
98 return TEXCOORDMODE_CLAMP; /* not really correct */
99 case GL_CLAMP_TO_BORDER:
100 return TEXCOORDMODE_CLAMP_BORDER;
101 case GL_MIRRORED_REPEAT:
102 return TEXCOORDMODE_MIRROR;
103 default:
104 return TEXCOORDMODE_WRAP;
105 }
106 }
107
108
109 /* Recalculate all state from scratch. Perhaps not the most
110 * efficient, but this has gotten complex enough that we need
111 * something which is understandable and reliable.
112 */
113 static GLboolean
114 i830_update_tex_unit(struct intel_context *intel, GLuint unit, GLuint ss3)
115 {
116 GLcontext *ctx = &intel->ctx;
117 struct i830_context *i830 = i830_context(ctx);
118 struct gl_texture_unit *tUnit = &ctx->Texture.Unit[unit];
119 struct gl_texture_object *tObj = tUnit->_Current;
120 struct intel_texture_object *intelObj = intel_texture_object(tObj);
121 struct gl_texture_image *firstImage;
122 GLuint *state = i830->state.Tex[unit], format, pitch;
123 GLint lodbias;
124 GLubyte border[4];
125
126 memset(state, 0, sizeof(state));
127
128 /*We need to refcount these. */
129
130 if (i830->state.tex_buffer[unit] != NULL) {
131 dri_bo_unreference(i830->state.tex_buffer[unit]);
132 i830->state.tex_buffer[unit] = NULL;
133 }
134
135 if (!intelObj->imageOverride && !intel_finalize_mipmap_tree(intel, unit))
136 return GL_FALSE;
137
138 /* Get first image here, since intelObj->firstLevel will get set in
139 * the intel_finalize_mipmap_tree() call above.
140 */
141 firstImage = tObj->Image[0][intelObj->firstLevel];
142
143 if (intelObj->imageOverride) {
144 i830->state.tex_buffer[unit] = NULL;
145 i830->state.tex_offset[unit] = intelObj->textureOffset;
146
147 switch (intelObj->depthOverride) {
148 case 32:
149 format = MAPSURF_32BIT | MT_32BIT_ARGB8888;
150 break;
151 case 24:
152 default:
153 format = MAPSURF_32BIT | MT_32BIT_XRGB8888;
154 break;
155 case 16:
156 format = MAPSURF_16BIT | MT_16BIT_RGB565;
157 break;
158 }
159
160 pitch = intelObj->pitchOverride;
161 } else {
162 GLuint dst_x, dst_y;
163
164 intel_miptree_get_image_offset(intelObj->mt, intelObj->firstLevel, 0, 0,
165 &dst_x, &dst_y);
166
167 dri_bo_reference(intelObj->mt->region->buffer);
168 i830->state.tex_buffer[unit] = intelObj->mt->region->buffer;
169 /* XXX: This calculation is probably broken for tiled images with
170 * a non-page-aligned offset.
171 */
172 i830->state.tex_offset[unit] = (dst_x + dst_y * intelObj->mt->pitch) *
173 intelObj->mt->cpp;
174
175 format = translate_texture_format(firstImage->TexFormat,
176 firstImage->InternalFormat);
177 pitch = intelObj->mt->pitch * intelObj->mt->cpp;
178 }
179
180 state[I830_TEXREG_TM0LI] = (_3DSTATE_LOAD_STATE_IMMEDIATE_2 |
181 (LOAD_TEXTURE_MAP0 << unit) | 4);
182
183 state[I830_TEXREG_TM0S1] =
184 (((firstImage->Height - 1) << TM0S1_HEIGHT_SHIFT) |
185 ((firstImage->Width - 1) << TM0S1_WIDTH_SHIFT) | format);
186
187 if (intelObj->mt->region->tiling != I915_TILING_NONE) {
188 state[I830_TEXREG_TM0S1] |= TM0S1_TILED_SURFACE;
189 if (intelObj->mt->region->tiling == I915_TILING_Y)
190 state[I830_TEXREG_TM0S1] |= TM0S1_TILE_WALK;
191 }
192
193 state[I830_TEXREG_TM0S2] =
194 ((((pitch / 4) - 1) << TM0S2_PITCH_SHIFT) | TM0S2_CUBE_FACE_ENA_MASK);
195
196 {
197 if (tObj->Target == GL_TEXTURE_CUBE_MAP)
198 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit) |
199 CUBE_NEGX_ENABLE |
200 CUBE_POSX_ENABLE |
201 CUBE_NEGY_ENABLE |
202 CUBE_POSY_ENABLE |
203 CUBE_NEGZ_ENABLE | CUBE_POSZ_ENABLE);
204 else
205 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit));
206 }
207
208
209
210
211 {
212 GLuint minFilt, mipFilt, magFilt;
213
214 switch (tObj->MinFilter) {
215 case GL_NEAREST:
216 minFilt = FILTER_NEAREST;
217 mipFilt = MIPFILTER_NONE;
218 break;
219 case GL_LINEAR:
220 minFilt = FILTER_LINEAR;
221 mipFilt = MIPFILTER_NONE;
222 break;
223 case GL_NEAREST_MIPMAP_NEAREST:
224 minFilt = FILTER_NEAREST;
225 mipFilt = MIPFILTER_NEAREST;
226 break;
227 case GL_LINEAR_MIPMAP_NEAREST:
228 minFilt = FILTER_LINEAR;
229 mipFilt = MIPFILTER_NEAREST;
230 break;
231 case GL_NEAREST_MIPMAP_LINEAR:
232 minFilt = FILTER_NEAREST;
233 mipFilt = MIPFILTER_LINEAR;
234 break;
235 case GL_LINEAR_MIPMAP_LINEAR:
236 minFilt = FILTER_LINEAR;
237 mipFilt = MIPFILTER_LINEAR;
238 break;
239 default:
240 return GL_FALSE;
241 }
242
243 if (tObj->MaxAnisotropy > 1.0) {
244 minFilt = FILTER_ANISOTROPIC;
245 magFilt = FILTER_ANISOTROPIC;
246 }
247 else {
248 switch (tObj->MagFilter) {
249 case GL_NEAREST:
250 magFilt = FILTER_NEAREST;
251 break;
252 case GL_LINEAR:
253 magFilt = FILTER_LINEAR;
254 break;
255 default:
256 return GL_FALSE;
257 }
258 }
259
260 lodbias = (int) ((tUnit->LodBias + tObj->LodBias) * 16.0);
261 if (lodbias < -64)
262 lodbias = -64;
263 if (lodbias > 63)
264 lodbias = 63;
265
266 state[I830_TEXREG_TM0S3] = ((lodbias << TM0S3_LOD_BIAS_SHIFT) &
267 TM0S3_LOD_BIAS_MASK);
268 #if 0
269 /* YUV conversion:
270 */
271 if (firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR ||
272 firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV)
273 state[I830_TEXREG_TM0S3] |= SS2_COLORSPACE_CONVERSION;
274 #endif
275
276 state[I830_TEXREG_TM0S3] |= ((intelObj->lastLevel -
277 intelObj->firstLevel) *
278 4) << TM0S3_MIN_MIP_SHIFT;
279
280 state[I830_TEXREG_TM0S3] |= ((minFilt << TM0S3_MIN_FILTER_SHIFT) |
281 (mipFilt << TM0S3_MIP_FILTER_SHIFT) |
282 (magFilt << TM0S3_MAG_FILTER_SHIFT));
283 }
284
285 {
286 GLenum ws = tObj->WrapS;
287 GLenum wt = tObj->WrapT;
288
289
290 /* 3D textures not available on i830
291 */
292 if (tObj->Target == GL_TEXTURE_3D)
293 return GL_FALSE;
294
295 state[I830_TEXREG_MCS] = (_3DSTATE_MAP_COORD_SET_CMD |
296 MAP_UNIT(unit) |
297 ENABLE_TEXCOORD_PARAMS |
298 ss3 |
299 ENABLE_ADDR_V_CNTL |
300 TEXCOORD_ADDR_V_MODE(translate_wrap_mode(wt))
301 | ENABLE_ADDR_U_CNTL |
302 TEXCOORD_ADDR_U_MODE(translate_wrap_mode
303 (ws)));
304 }
305
306 /* convert border color from float to ubyte */
307 CLAMPED_FLOAT_TO_UBYTE(border[0], tObj->BorderColor[0]);
308 CLAMPED_FLOAT_TO_UBYTE(border[1], tObj->BorderColor[1]);
309 CLAMPED_FLOAT_TO_UBYTE(border[2], tObj->BorderColor[2]);
310 CLAMPED_FLOAT_TO_UBYTE(border[3], tObj->BorderColor[3]);
311
312 state[I830_TEXREG_TM0S4] = INTEL_PACKCOLOR8888(border[0],
313 border[1],
314 border[2],
315 border[3]);
316
317
318 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(unit), GL_TRUE);
319 /* memcmp was already disabled, but definitely won't work as the
320 * region might now change and that wouldn't be detected:
321 */
322 I830_STATECHANGE(i830, I830_UPLOAD_TEX(unit));
323 return GL_TRUE;
324 }
325
326
327
328
329 void
330 i830UpdateTextureState(struct intel_context *intel)
331 {
332 struct i830_context *i830 = i830_context(&intel->ctx);
333 GLboolean ok = GL_TRUE;
334 GLuint i;
335
336 for (i = 0; i < I830_TEX_UNITS && ok; i++) {
337 switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
338 case TEXTURE_1D_BIT:
339 case TEXTURE_2D_BIT:
340 case TEXTURE_CUBE_BIT:
341 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_NORMAL);
342 break;
343 case TEXTURE_RECT_BIT:
344 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_IN_TEXELUNITS);
345 break;
346 case 0:{
347 struct i830_context *i830 = i830_context(&intel->ctx);
348 if (i830->state.active & I830_UPLOAD_TEX(i))
349 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(i), GL_FALSE);
350
351 if (i830->state.tex_buffer[i] != NULL) {
352 dri_bo_unreference(i830->state.tex_buffer[i]);
353 i830->state.tex_buffer[i] = NULL;
354 }
355 break;
356 }
357 case TEXTURE_3D_BIT:
358 default:
359 ok = GL_FALSE;
360 break;
361 }
362 }
363
364 FALLBACK(intel, I830_FALLBACK_TEXTURE, !ok);
365
366 if (ok)
367 i830EmitTextureBlend(i830);
368 }