1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "main/mtypes.h"
29 #include "main/enums.h"
30 #include "main/texformat.h"
32 #include "intel_mipmap_tree.h"
33 #include "intel_tex.h"
35 #include "i915_context.h"
40 translate_texture_format(GLuint mesa_format
, GLenum DepthMode
)
42 switch (mesa_format
) {
44 return MAPSURF_8BIT
| MT_8BIT_L8
;
46 return MAPSURF_8BIT
| MT_8BIT_I8
;
48 return MAPSURF_8BIT
| MT_8BIT_A8
;
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 return MAPSURF_32BIT
| MT_32BIT_ARGB8888
;
59 case MESA_FORMAT_YCBCR_REV
:
60 return (MAPSURF_422
| MT_422_YCRCB_NORMAL
);
61 case MESA_FORMAT_YCBCR
:
62 return (MAPSURF_422
| MT_422_YCRCB_SWAPY
);
63 case MESA_FORMAT_RGB_FXT1
:
64 case MESA_FORMAT_RGBA_FXT1
:
65 return (MAPSURF_COMPRESSED
| MT_COMPRESS_FXT1
);
67 if (DepthMode
== GL_ALPHA
)
68 return (MAPSURF_16BIT
| MT_16BIT_A16
);
69 else if (DepthMode
== GL_INTENSITY
)
70 return (MAPSURF_16BIT
| MT_16BIT_I16
);
72 return (MAPSURF_16BIT
| MT_16BIT_L16
);
73 case MESA_FORMAT_RGBA_DXT1
:
74 case MESA_FORMAT_RGB_DXT1
:
75 return (MAPSURF_COMPRESSED
| MT_COMPRESS_DXT1
);
76 case MESA_FORMAT_RGBA_DXT3
:
77 return (MAPSURF_COMPRESSED
| MT_COMPRESS_DXT2_3
);
78 case MESA_FORMAT_RGBA_DXT5
:
79 return (MAPSURF_COMPRESSED
| MT_COMPRESS_DXT4_5
);
80 case MESA_FORMAT_S8_Z24
:
81 return (MAPSURF_32BIT
| MT_32BIT_xI824
);
83 fprintf(stderr
, "%s: bad image format %x\n", __FUNCTION__
, mesa_format
);
92 /* The i915 (and related graphics cores) do not support GL_CLAMP. The
93 * Intel drivers for "other operating systems" implement GL_CLAMP as
94 * GL_CLAMP_TO_EDGE, so the same is done here.
97 translate_wrap_mode(GLenum wrap
)
101 return TEXCOORDMODE_WRAP
;
103 return TEXCOORDMODE_CLAMP_EDGE
; /* not quite correct */
104 case GL_CLAMP_TO_EDGE
:
105 return TEXCOORDMODE_CLAMP_EDGE
;
106 case GL_CLAMP_TO_BORDER
:
107 return TEXCOORDMODE_CLAMP_BORDER
;
108 case GL_MIRRORED_REPEAT
:
109 return TEXCOORDMODE_MIRROR
;
111 return TEXCOORDMODE_WRAP
;
117 /* Recalculate all state from scratch. Perhaps not the most
118 * efficient, but this has gotten complex enough that we need
119 * something which is understandable and reliable.
122 i915_update_tex_unit(struct intel_context
*intel
, GLuint unit
, GLuint ss3
)
124 GLcontext
*ctx
= &intel
->ctx
;
125 struct i915_context
*i915
= i915_context(ctx
);
126 struct gl_texture_unit
*tUnit
= &ctx
->Texture
.Unit
[unit
];
127 struct gl_texture_object
*tObj
= tUnit
->_Current
;
128 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
129 struct gl_texture_image
*firstImage
;
130 GLuint
*state
= i915
->state
.Tex
[unit
], format
, pitch
;
133 memset(state
, 0, sizeof(state
));
135 /*We need to refcount these. */
137 if (i915
->state
.tex_buffer
[unit
] != NULL
) {
138 dri_bo_unreference(i915
->state
.tex_buffer
[unit
]);
139 i915
->state
.tex_buffer
[unit
] = NULL
;
142 if (!intelObj
->imageOverride
&& !intel_finalize_mipmap_tree(intel
, unit
))
145 /* Get first image here, since intelObj->firstLevel will get set in
146 * the intel_finalize_mipmap_tree() call above.
148 firstImage
= tObj
->Image
[0][intelObj
->firstLevel
];
150 if (intelObj
->imageOverride
) {
151 i915
->state
.tex_buffer
[unit
] = NULL
;
152 i915
->state
.tex_offset
[unit
] = intelObj
->textureOffset
;
154 switch (intelObj
->depthOverride
) {
156 format
= MAPSURF_32BIT
| MT_32BIT_ARGB8888
;
160 format
= MAPSURF_32BIT
| MT_32BIT_XRGB8888
;
163 format
= MAPSURF_16BIT
| MT_16BIT_RGB565
;
167 pitch
= intelObj
->pitchOverride
;
169 dri_bo_reference(intelObj
->mt
->region
->buffer
);
170 i915
->state
.tex_buffer
[unit
] = intelObj
->mt
->region
->buffer
;
171 i915
->state
.tex_offset
[unit
] = intel_miptree_image_offset(intelObj
->mt
,
175 format
= translate_texture_format(firstImage
->TexFormat
->MesaFormat
,
177 pitch
= intelObj
->mt
->pitch
* intelObj
->mt
->cpp
;
180 state
[I915_TEXREG_MS3
] =
181 (((firstImage
->Height
- 1) << MS3_HEIGHT_SHIFT
) |
182 ((firstImage
->Width
- 1) << MS3_WIDTH_SHIFT
) | format
|
185 state
[I915_TEXREG_MS4
] =
186 ((((pitch
/ 4) - 1) << MS4_PITCH_SHIFT
) | MS4_CUBE_FACE_ENA_MASK
|
187 ((((intelObj
->lastLevel
- intelObj
->firstLevel
) * 4)) <<
188 MS4_MAX_LOD_SHIFT
) | ((firstImage
->Depth
- 1) <<
189 MS4_VOLUME_DEPTH_SHIFT
));
193 GLuint minFilt
, mipFilt
, magFilt
;
195 switch (tObj
->MinFilter
) {
197 minFilt
= FILTER_NEAREST
;
198 mipFilt
= MIPFILTER_NONE
;
201 minFilt
= FILTER_LINEAR
;
202 mipFilt
= MIPFILTER_NONE
;
204 case GL_NEAREST_MIPMAP_NEAREST
:
205 minFilt
= FILTER_NEAREST
;
206 mipFilt
= MIPFILTER_NEAREST
;
208 case GL_LINEAR_MIPMAP_NEAREST
:
209 minFilt
= FILTER_LINEAR
;
210 mipFilt
= MIPFILTER_NEAREST
;
212 case GL_NEAREST_MIPMAP_LINEAR
:
213 minFilt
= FILTER_NEAREST
;
214 mipFilt
= MIPFILTER_LINEAR
;
216 case GL_LINEAR_MIPMAP_LINEAR
:
217 minFilt
= FILTER_LINEAR
;
218 mipFilt
= MIPFILTER_LINEAR
;
224 if (tObj
->MaxAnisotropy
> 1.0) {
225 minFilt
= FILTER_ANISOTROPIC
;
226 magFilt
= FILTER_ANISOTROPIC
;
229 switch (tObj
->MagFilter
) {
231 magFilt
= FILTER_NEAREST
;
234 magFilt
= FILTER_LINEAR
;
241 lodbias
= (int) ((tUnit
->LodBias
+ tObj
->LodBias
) * 16.0);
246 state
[I915_TEXREG_SS2
] = ((lodbias
<< SS2_LOD_BIAS_SHIFT
) &
251 if (firstImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR
||
252 firstImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR_REV
)
253 state
[I915_TEXREG_SS2
] |= SS2_COLORSPACE_CONVERSION
;
257 if (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
&&
258 tObj
->Target
!= GL_TEXTURE_3D
) {
259 if (tObj
->Target
== GL_TEXTURE_1D
)
262 state
[I915_TEXREG_SS2
] |=
264 intel_translate_shadow_compare_func(tObj
->CompareFunc
));
266 minFilt
= FILTER_4X4_FLAT
;
267 magFilt
= FILTER_4X4_FLAT
;
270 state
[I915_TEXREG_SS2
] |= ((minFilt
<< SS2_MIN_FILTER_SHIFT
) |
271 (mipFilt
<< SS2_MIP_FILTER_SHIFT
) |
272 (magFilt
<< SS2_MAG_FILTER_SHIFT
));
276 GLenum ws
= tObj
->WrapS
;
277 GLenum wt
= tObj
->WrapT
;
278 GLenum wr
= tObj
->WrapR
;
281 /* 3D textures don't seem to respect the border color.
282 * Fallback if there's ever a danger that they might refer to
285 * Effectively this means fallback on 3D clamp or
288 if (tObj
->Target
== GL_TEXTURE_3D
&&
289 (tObj
->MinFilter
!= GL_NEAREST
||
290 tObj
->MagFilter
!= GL_NEAREST
) &&
294 ws
== GL_CLAMP_TO_BORDER
||
295 wt
== GL_CLAMP_TO_BORDER
|| wr
== GL_CLAMP_TO_BORDER
))
298 /* Only support TEXCOORDMODE_CLAMP_EDGE and TEXCOORDMODE_CUBE (not
299 * used) when using cube map texture coordinates
301 if (tObj
->Target
== GL_TEXTURE_CUBE_MAP_ARB
&&
302 (((ws
!= GL_CLAMP
) && (ws
!= GL_CLAMP_TO_EDGE
)) ||
303 ((wr
!= GL_CLAMP
) && (wr
!= GL_CLAMP_TO_EDGE
))))
306 state
[I915_TEXREG_SS3
] = ss3
; /* SS3_NORMALIZED_COORDS */
308 state
[I915_TEXREG_SS3
] |=
309 ((translate_wrap_mode(ws
) << SS3_TCX_ADDR_MODE_SHIFT
) |
310 (translate_wrap_mode(wt
) << SS3_TCY_ADDR_MODE_SHIFT
) |
311 (translate_wrap_mode(wr
) << SS3_TCZ_ADDR_MODE_SHIFT
));
313 state
[I915_TEXREG_SS3
] |= (unit
<< SS3_TEXTUREMAP_INDEX_SHIFT
);
317 if (firstImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
318 /* GL specs that border color for depth textures is taken from the
319 * R channel, while the hardware uses A. Spam R into all the channels
322 state
[I915_TEXREG_SS4
] = INTEL_PACKCOLOR8888(tObj
->_BorderChan
[0],
323 tObj
->_BorderChan
[0],
324 tObj
->_BorderChan
[0],
325 tObj
->_BorderChan
[0]);
327 state
[I915_TEXREG_SS4
] = INTEL_PACKCOLOR8888(tObj
->_BorderChan
[0],
328 tObj
->_BorderChan
[1],
329 tObj
->_BorderChan
[2],
330 tObj
->_BorderChan
[3]);
334 I915_ACTIVESTATE(i915
, I915_UPLOAD_TEX(unit
), GL_TRUE
);
335 /* memcmp was already disabled, but definitely won't work as the
336 * region might now change and that wouldn't be detected:
338 I915_STATECHANGE(i915
, I915_UPLOAD_TEX(unit
));
342 DBG(TEXTURE
, "state[I915_TEXREG_SS2] = 0x%x\n", state
[I915_TEXREG_SS2
]);
343 DBG(TEXTURE
, "state[I915_TEXREG_SS3] = 0x%x\n", state
[I915_TEXREG_SS3
]);
344 DBG(TEXTURE
, "state[I915_TEXREG_SS4] = 0x%x\n", state
[I915_TEXREG_SS4
]);
345 DBG(TEXTURE
, "state[I915_TEXREG_MS2] = 0x%x\n", state
[I915_TEXREG_MS2
]);
346 DBG(TEXTURE
, "state[I915_TEXREG_MS3] = 0x%x\n", state
[I915_TEXREG_MS3
]);
347 DBG(TEXTURE
, "state[I915_TEXREG_MS4] = 0x%x\n", state
[I915_TEXREG_MS4
]);
357 i915UpdateTextureState(struct intel_context
*intel
)
359 GLboolean ok
= GL_TRUE
;
362 for (i
= 0; i
< I915_TEX_UNITS
&& ok
; i
++) {
363 switch (intel
->ctx
.Texture
.Unit
[i
]._ReallyEnabled
) {
366 case TEXTURE_CUBE_BIT
:
368 ok
= i915_update_tex_unit(intel
, i
, SS3_NORMALIZED_COORDS
);
370 case TEXTURE_RECT_BIT
:
371 ok
= i915_update_tex_unit(intel
, i
, 0);
374 struct i915_context
*i915
= i915_context(&intel
->ctx
);
375 if (i915
->state
.active
& I915_UPLOAD_TEX(i
))
376 I915_ACTIVESTATE(i915
, I915_UPLOAD_TEX(i
), GL_FALSE
);
378 if (i915
->state
.tex_buffer
[i
] != NULL
) {
379 dri_bo_unreference(i915
->state
.tex_buffer
[i
]);
380 i915
->state
.tex_buffer
[i
] = NULL
;
391 FALLBACK(intel
, I915_FALLBACK_TEXTURE
, !ok
);