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