Merge branch '7.8'
[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/colormac.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 return MAPSURF_32BIT | MT_32BIT_ARGB8888;
60 case MESA_FORMAT_XRGB8888:
61 return MAPSURF_32BIT | MT_32BIT_XRGB8888;
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 GLuint dst_x, dst_y;
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 (!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 intel_miptree_get_image_offset(intelObj->mt, intelObj->firstLevel, 0, 0,
145 &dst_x, &dst_y);
146
147 dri_bo_reference(intelObj->mt->region->buffer);
148 i830->state.tex_buffer[unit] = intelObj->mt->region->buffer;
149 pitch = intelObj->mt->region->pitch * intelObj->mt->cpp;
150
151 /* XXX: This calculation is probably broken for tiled images with
152 * a non-page-aligned offset.
153 */
154 i830->state.tex_offset[unit] = dst_x * intelObj->mt->cpp + dst_y * pitch;
155
156 format = translate_texture_format(firstImage->TexFormat,
157 firstImage->InternalFormat);
158
159 state[I830_TEXREG_TM0LI] = (_3DSTATE_LOAD_STATE_IMMEDIATE_2 |
160 (LOAD_TEXTURE_MAP0 << unit) | 4);
161
162 state[I830_TEXREG_TM0S1] =
163 (((firstImage->Height - 1) << TM0S1_HEIGHT_SHIFT) |
164 ((firstImage->Width - 1) << TM0S1_WIDTH_SHIFT) | format);
165
166 if (intelObj->mt->region->tiling != I915_TILING_NONE) {
167 state[I830_TEXREG_TM0S1] |= TM0S1_TILED_SURFACE;
168 if (intelObj->mt->region->tiling == I915_TILING_Y)
169 state[I830_TEXREG_TM0S1] |= TM0S1_TILE_WALK;
170 }
171
172 state[I830_TEXREG_TM0S2] =
173 ((((pitch / 4) - 1) << TM0S2_PITCH_SHIFT) | TM0S2_CUBE_FACE_ENA_MASK);
174
175 {
176 if (tObj->Target == GL_TEXTURE_CUBE_MAP)
177 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit) |
178 CUBE_NEGX_ENABLE |
179 CUBE_POSX_ENABLE |
180 CUBE_NEGY_ENABLE |
181 CUBE_POSY_ENABLE |
182 CUBE_NEGZ_ENABLE | CUBE_POSZ_ENABLE);
183 else
184 state[I830_TEXREG_CUBE] = (_3DSTATE_MAP_CUBE | MAP_UNIT(unit));
185 }
186
187
188
189
190 {
191 GLuint minFilt, mipFilt, magFilt;
192
193 switch (tObj->MinFilter) {
194 case GL_NEAREST:
195 minFilt = FILTER_NEAREST;
196 mipFilt = MIPFILTER_NONE;
197 break;
198 case GL_LINEAR:
199 minFilt = FILTER_LINEAR;
200 mipFilt = MIPFILTER_NONE;
201 break;
202 case GL_NEAREST_MIPMAP_NEAREST:
203 minFilt = FILTER_NEAREST;
204 mipFilt = MIPFILTER_NEAREST;
205 break;
206 case GL_LINEAR_MIPMAP_NEAREST:
207 minFilt = FILTER_LINEAR;
208 mipFilt = MIPFILTER_NEAREST;
209 break;
210 case GL_NEAREST_MIPMAP_LINEAR:
211 minFilt = FILTER_NEAREST;
212 mipFilt = MIPFILTER_LINEAR;
213 break;
214 case GL_LINEAR_MIPMAP_LINEAR:
215 minFilt = FILTER_LINEAR;
216 mipFilt = MIPFILTER_LINEAR;
217 break;
218 default:
219 return GL_FALSE;
220 }
221
222 if (tObj->MaxAnisotropy > 1.0) {
223 minFilt = FILTER_ANISOTROPIC;
224 magFilt = FILTER_ANISOTROPIC;
225 }
226 else {
227 switch (tObj->MagFilter) {
228 case GL_NEAREST:
229 magFilt = FILTER_NEAREST;
230 break;
231 case GL_LINEAR:
232 magFilt = FILTER_LINEAR;
233 break;
234 default:
235 return GL_FALSE;
236 }
237 }
238
239 lodbias = (int) ((tUnit->LodBias + tObj->LodBias) * 16.0);
240 if (lodbias < -64)
241 lodbias = -64;
242 if (lodbias > 63)
243 lodbias = 63;
244
245 state[I830_TEXREG_TM0S3] = ((lodbias << TM0S3_LOD_BIAS_SHIFT) &
246 TM0S3_LOD_BIAS_MASK);
247 #if 0
248 /* YUV conversion:
249 */
250 if (firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR ||
251 firstImage->TexFormat->MesaFormat == MESA_FORMAT_YCBCR_REV)
252 state[I830_TEXREG_TM0S3] |= SS2_COLORSPACE_CONVERSION;
253 #endif
254
255 state[I830_TEXREG_TM0S3] |= ((intelObj->lastLevel -
256 intelObj->firstLevel) *
257 4) << TM0S3_MIN_MIP_SHIFT;
258
259 state[I830_TEXREG_TM0S3] |= ((minFilt << TM0S3_MIN_FILTER_SHIFT) |
260 (mipFilt << TM0S3_MIP_FILTER_SHIFT) |
261 (magFilt << TM0S3_MAG_FILTER_SHIFT));
262 }
263
264 {
265 GLenum ws = tObj->WrapS;
266 GLenum wt = tObj->WrapT;
267
268
269 /* 3D textures not available on i830
270 */
271 if (tObj->Target == GL_TEXTURE_3D)
272 return GL_FALSE;
273
274 state[I830_TEXREG_MCS] = (_3DSTATE_MAP_COORD_SET_CMD |
275 MAP_UNIT(unit) |
276 ENABLE_TEXCOORD_PARAMS |
277 ss3 |
278 ENABLE_ADDR_V_CNTL |
279 TEXCOORD_ADDR_V_MODE(translate_wrap_mode(wt))
280 | ENABLE_ADDR_U_CNTL |
281 TEXCOORD_ADDR_U_MODE(translate_wrap_mode
282 (ws)));
283 }
284
285 /* convert border color from float to ubyte */
286 CLAMPED_FLOAT_TO_UBYTE(border[0], tObj->BorderColor.f[0]);
287 CLAMPED_FLOAT_TO_UBYTE(border[1], tObj->BorderColor.f[1]);
288 CLAMPED_FLOAT_TO_UBYTE(border[2], tObj->BorderColor.f[2]);
289 CLAMPED_FLOAT_TO_UBYTE(border[3], tObj->BorderColor.f[3]);
290
291 state[I830_TEXREG_TM0S4] = PACK_COLOR_8888(border[3],
292 border[0],
293 border[1],
294 border[2]);
295
296 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(unit), GL_TRUE);
297 /* memcmp was already disabled, but definitely won't work as the
298 * region might now change and that wouldn't be detected:
299 */
300 I830_STATECHANGE(i830, I830_UPLOAD_TEX(unit));
301 return GL_TRUE;
302 }
303
304
305
306
307 void
308 i830UpdateTextureState(struct intel_context *intel)
309 {
310 struct i830_context *i830 = i830_context(&intel->ctx);
311 GLboolean ok = GL_TRUE;
312 GLuint i;
313
314 for (i = 0; i < I830_TEX_UNITS && ok; i++) {
315 switch (intel->ctx.Texture.Unit[i]._ReallyEnabled) {
316 case TEXTURE_1D_BIT:
317 case TEXTURE_2D_BIT:
318 case TEXTURE_CUBE_BIT:
319 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_NORMAL);
320 break;
321 case TEXTURE_RECT_BIT:
322 ok = i830_update_tex_unit(intel, i, TEXCOORDS_ARE_IN_TEXELUNITS);
323 break;
324 case 0:{
325 struct i830_context *i830 = i830_context(&intel->ctx);
326 if (i830->state.active & I830_UPLOAD_TEX(i))
327 I830_ACTIVESTATE(i830, I830_UPLOAD_TEX(i), GL_FALSE);
328
329 if (i830->state.tex_buffer[i] != NULL) {
330 dri_bo_unreference(i830->state.tex_buffer[i]);
331 i830->state.tex_buffer[i] = NULL;
332 }
333 break;
334 }
335 case TEXTURE_3D_BIT:
336 default:
337 ok = GL_FALSE;
338 break;
339 }
340 }
341
342 FALLBACK(intel, I830_FALLBACK_TEXTURE, !ok);
343
344 if (ok)
345 i830EmitTextureBlend(i830);
346 }