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