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/macros.h"
31 #include "main/colormac.h"
33 #include "intel_mipmap_tree.h"
34 #include "intel_tex.h"
36 #include "i915_context.h"
41 translate_texture_format(gl_format mesa_format
, GLuint internal_format
,
44 switch (mesa_format
) {
46 return MAPSURF_8BIT
| MT_8BIT_L8
;
48 return MAPSURF_8BIT
| MT_8BIT_I8
;
50 return MAPSURF_8BIT
| MT_8BIT_A8
;
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_XRGB8888
:
62 return MAPSURF_32BIT
| MT_32BIT_XRGB8888
;
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
);
71 if (DepthMode
== GL_ALPHA
)
72 return (MAPSURF_16BIT
| MT_16BIT_A16
);
73 else if (DepthMode
== GL_INTENSITY
)
74 return (MAPSURF_16BIT
| MT_16BIT_I16
);
76 return (MAPSURF_16BIT
| MT_16BIT_L16
);
77 case MESA_FORMAT_RGBA_DXT1
:
78 case MESA_FORMAT_RGB_DXT1
:
79 return (MAPSURF_COMPRESSED
| MT_COMPRESS_DXT1
);
80 case MESA_FORMAT_RGBA_DXT3
:
81 return (MAPSURF_COMPRESSED
| MT_COMPRESS_DXT2_3
);
82 case MESA_FORMAT_RGBA_DXT5
:
83 return (MAPSURF_COMPRESSED
| MT_COMPRESS_DXT4_5
);
84 case MESA_FORMAT_S8_Z24
:
85 if (DepthMode
== GL_ALPHA
)
86 return (MAPSURF_32BIT
| MT_32BIT_x8A24
);
87 else if (DepthMode
== GL_INTENSITY
)
88 return (MAPSURF_32BIT
| MT_32BIT_x8I24
);
90 return (MAPSURF_32BIT
| MT_32BIT_x8L24
);
92 fprintf(stderr
, "%s: bad image format %x\n", __FUNCTION__
, mesa_format
);
101 /* The i915 (and related graphics cores) do not support GL_CLAMP. The
102 * Intel drivers for "other operating systems" implement GL_CLAMP as
103 * GL_CLAMP_TO_EDGE, so the same is done here.
106 translate_wrap_mode(GLenum wrap
)
110 return TEXCOORDMODE_WRAP
;
112 return TEXCOORDMODE_CLAMP_EDGE
; /* not quite correct */
113 case GL_CLAMP_TO_EDGE
:
114 return TEXCOORDMODE_CLAMP_EDGE
;
115 case GL_CLAMP_TO_BORDER
:
116 return TEXCOORDMODE_CLAMP_BORDER
;
117 case GL_MIRRORED_REPEAT
:
118 return TEXCOORDMODE_MIRROR
;
120 return TEXCOORDMODE_WRAP
;
126 /* Recalculate all state from scratch. Perhaps not the most
127 * efficient, but this has gotten complex enough that we need
128 * something which is understandable and reliable.
131 i915_update_tex_unit(struct intel_context
*intel
, GLuint unit
, GLuint ss3
)
133 GLcontext
*ctx
= &intel
->ctx
;
134 struct i915_context
*i915
= i915_context(ctx
);
135 struct gl_texture_unit
*tUnit
= &ctx
->Texture
.Unit
[unit
];
136 struct gl_texture_object
*tObj
= tUnit
->_Current
;
137 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
138 struct gl_texture_image
*firstImage
;
139 GLuint
*state
= i915
->state
.Tex
[unit
], format
, pitch
;
140 GLint lodbias
, aniso
= 0;
144 memset(state
, 0, sizeof(state
));
146 /*We need to refcount these. */
148 if (i915
->state
.tex_buffer
[unit
] != NULL
) {
149 dri_bo_unreference(i915
->state
.tex_buffer
[unit
]);
150 i915
->state
.tex_buffer
[unit
] = NULL
;
153 if (!intelObj
->imageOverride
&& !intel_finalize_mipmap_tree(intel
, unit
))
156 /* Get first image here, since intelObj->firstLevel will get set in
157 * the intel_finalize_mipmap_tree() call above.
159 firstImage
= tObj
->Image
[0][intelObj
->firstLevel
];
161 if (intelObj
->imageOverride
) {
162 i915
->state
.tex_buffer
[unit
] = NULL
;
163 i915
->state
.tex_offset
[unit
] = intelObj
->textureOffset
;
165 switch (intelObj
->depthOverride
) {
167 format
= MAPSURF_32BIT
| MT_32BIT_ARGB8888
;
171 format
= MAPSURF_32BIT
| MT_32BIT_XRGB8888
;
174 format
= MAPSURF_16BIT
| MT_16BIT_RGB565
;
178 pitch
= intelObj
->pitchOverride
;
180 dri_bo_reference(intelObj
->mt
->region
->buffer
);
181 i915
->state
.tex_buffer
[unit
] = intelObj
->mt
->region
->buffer
;
182 i915
->state
.tex_offset
[unit
] = 0; /* Always the origin of the miptree */
184 format
= translate_texture_format(firstImage
->TexFormat
,
185 firstImage
->InternalFormat
,
187 pitch
= intelObj
->mt
->pitch
* intelObj
->mt
->cpp
;
190 state
[I915_TEXREG_MS3
] =
191 (((firstImage
->Height
- 1) << MS3_HEIGHT_SHIFT
) |
192 ((firstImage
->Width
- 1) << MS3_WIDTH_SHIFT
) | format
);
194 if (intelObj
->mt
->region
->tiling
!= I915_TILING_NONE
) {
195 state
[I915_TEXREG_MS3
] |= MS3_TILED_SURFACE
;
196 if (intelObj
->mt
->region
->tiling
== I915_TILING_Y
)
197 state
[I915_TEXREG_MS3
] |= MS3_TILE_WALK
;
200 /* We get one field with fraction bits for the maximum addressable
201 * (lowest resolution) LOD. Use it to cover both MAX_LEVEL and
204 maxlod
= MIN2(tObj
->MaxLod
, tObj
->_MaxLevel
- tObj
->BaseLevel
);
205 state
[I915_TEXREG_MS4
] =
206 ((((pitch
/ 4) - 1) << MS4_PITCH_SHIFT
) |
207 MS4_CUBE_FACE_ENA_MASK
|
208 (U_FIXED(CLAMP(maxlod
, 0.0, 11.0), 2) << MS4_MAX_LOD_SHIFT
) |
209 ((firstImage
->Depth
- 1) << MS4_VOLUME_DEPTH_SHIFT
));
213 GLuint minFilt
, mipFilt
, magFilt
;
215 switch (tObj
->MinFilter
) {
217 minFilt
= FILTER_NEAREST
;
218 mipFilt
= MIPFILTER_NONE
;
221 minFilt
= FILTER_LINEAR
;
222 mipFilt
= MIPFILTER_NONE
;
224 case GL_NEAREST_MIPMAP_NEAREST
:
225 minFilt
= FILTER_NEAREST
;
226 mipFilt
= MIPFILTER_NEAREST
;
228 case GL_LINEAR_MIPMAP_NEAREST
:
229 minFilt
= FILTER_LINEAR
;
230 mipFilt
= MIPFILTER_NEAREST
;
232 case GL_NEAREST_MIPMAP_LINEAR
:
233 minFilt
= FILTER_NEAREST
;
234 mipFilt
= MIPFILTER_LINEAR
;
236 case GL_LINEAR_MIPMAP_LINEAR
:
237 minFilt
= FILTER_LINEAR
;
238 mipFilt
= MIPFILTER_LINEAR
;
244 if (tObj
->MaxAnisotropy
> 1.0) {
245 minFilt
= FILTER_ANISOTROPIC
;
246 magFilt
= FILTER_ANISOTROPIC
;
247 if (tObj
->MaxAnisotropy
> 2.0)
248 aniso
= SS2_MAX_ANISO_4
;
250 aniso
= SS2_MAX_ANISO_2
;
253 switch (tObj
->MagFilter
) {
255 magFilt
= FILTER_NEAREST
;
258 magFilt
= FILTER_LINEAR
;
265 lodbias
= (int) ((tUnit
->LodBias
+ tObj
->LodBias
) * 16.0);
270 state
[I915_TEXREG_SS2
] = ((lodbias
<< SS2_LOD_BIAS_SHIFT
) &
275 if (firstImage
->TexFormat
== MESA_FORMAT_YCBCR
||
276 firstImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
)
277 state
[I915_TEXREG_SS2
] |= SS2_COLORSPACE_CONVERSION
;
281 if (tObj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE_ARB
&&
282 tObj
->Target
!= GL_TEXTURE_3D
) {
283 if (tObj
->Target
== GL_TEXTURE_1D
)
286 state
[I915_TEXREG_SS2
] |=
288 intel_translate_shadow_compare_func(tObj
->CompareFunc
));
290 minFilt
= FILTER_4X4_FLAT
;
291 magFilt
= FILTER_4X4_FLAT
;
294 state
[I915_TEXREG_SS2
] |= ((minFilt
<< SS2_MIN_FILTER_SHIFT
) |
295 (mipFilt
<< SS2_MIP_FILTER_SHIFT
) |
296 (magFilt
<< SS2_MAG_FILTER_SHIFT
) |
301 GLenum ws
= tObj
->WrapS
;
302 GLenum wt
= tObj
->WrapT
;
303 GLenum wr
= tObj
->WrapR
;
305 /* We program 1D textures as 2D textures, so the 2D texcoord could
306 * result in sampling border values if we don't set the T wrap to
309 if (tObj
->Target
== GL_TEXTURE_1D
)
312 /* 3D textures don't seem to respect the border color.
313 * Fallback if there's ever a danger that they might refer to
316 * Effectively this means fallback on 3D clamp or
319 if (tObj
->Target
== GL_TEXTURE_3D
&&
320 (tObj
->MinFilter
!= GL_NEAREST
||
321 tObj
->MagFilter
!= GL_NEAREST
) &&
325 ws
== GL_CLAMP_TO_BORDER
||
326 wt
== GL_CLAMP_TO_BORDER
|| wr
== GL_CLAMP_TO_BORDER
))
329 /* Only support TEXCOORDMODE_CLAMP_EDGE and TEXCOORDMODE_CUBE (not
330 * used) when using cube map texture coordinates
332 if (tObj
->Target
== GL_TEXTURE_CUBE_MAP_ARB
&&
333 (((ws
!= GL_CLAMP
) && (ws
!= GL_CLAMP_TO_EDGE
)) ||
334 ((wt
!= GL_CLAMP
) && (wt
!= GL_CLAMP_TO_EDGE
))))
337 state
[I915_TEXREG_SS3
] = ss3
; /* SS3_NORMALIZED_COORDS */
339 state
[I915_TEXREG_SS3
] |=
340 ((translate_wrap_mode(ws
) << SS3_TCX_ADDR_MODE_SHIFT
) |
341 (translate_wrap_mode(wt
) << SS3_TCY_ADDR_MODE_SHIFT
) |
342 (translate_wrap_mode(wr
) << SS3_TCZ_ADDR_MODE_SHIFT
));
344 state
[I915_TEXREG_SS3
] |= (unit
<< SS3_TEXTUREMAP_INDEX_SHIFT
);
345 state
[I915_TEXREG_SS3
] |= (U_FIXED(CLAMP(tObj
->MinLod
, 0.0, 11.0), 4) <<
350 /* convert border color from float to ubyte */
351 CLAMPED_FLOAT_TO_UBYTE(border
[0], tObj
->BorderColor
.f
[0]);
352 CLAMPED_FLOAT_TO_UBYTE(border
[1], tObj
->BorderColor
.f
[1]);
353 CLAMPED_FLOAT_TO_UBYTE(border
[2], tObj
->BorderColor
.f
[2]);
354 CLAMPED_FLOAT_TO_UBYTE(border
[3], tObj
->BorderColor
.f
[3]);
356 if (firstImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
357 /* GL specs that border color for depth textures is taken from the
358 * R channel, while the hardware uses A. Spam R into all the channels
361 state
[I915_TEXREG_SS4
] = PACK_COLOR_8888(border
[0],
366 state
[I915_TEXREG_SS4
] = PACK_COLOR_8888(border
[3],
373 I915_ACTIVESTATE(i915
, I915_UPLOAD_TEX(unit
), GL_TRUE
);
374 /* memcmp was already disabled, but definitely won't work as the
375 * region might now change and that wouldn't be detected:
377 I915_STATECHANGE(i915
, I915_UPLOAD_TEX(unit
));
381 DBG(TEXTURE
, "state[I915_TEXREG_SS2] = 0x%x\n", state
[I915_TEXREG_SS2
]);
382 DBG(TEXTURE
, "state[I915_TEXREG_SS3] = 0x%x\n", state
[I915_TEXREG_SS3
]);
383 DBG(TEXTURE
, "state[I915_TEXREG_SS4] = 0x%x\n", state
[I915_TEXREG_SS4
]);
384 DBG(TEXTURE
, "state[I915_TEXREG_MS2] = 0x%x\n", state
[I915_TEXREG_MS2
]);
385 DBG(TEXTURE
, "state[I915_TEXREG_MS3] = 0x%x\n", state
[I915_TEXREG_MS3
]);
386 DBG(TEXTURE
, "state[I915_TEXREG_MS4] = 0x%x\n", state
[I915_TEXREG_MS4
]);
396 i915UpdateTextureState(struct intel_context
*intel
)
398 GLboolean ok
= GL_TRUE
;
401 for (i
= 0; i
< I915_TEX_UNITS
&& ok
; i
++) {
402 switch (intel
->ctx
.Texture
.Unit
[i
]._ReallyEnabled
) {
405 case TEXTURE_CUBE_BIT
:
407 ok
= i915_update_tex_unit(intel
, i
, SS3_NORMALIZED_COORDS
);
409 case TEXTURE_RECT_BIT
:
410 ok
= i915_update_tex_unit(intel
, i
, 0);
413 struct i915_context
*i915
= i915_context(&intel
->ctx
);
414 if (i915
->state
.active
& I915_UPLOAD_TEX(i
))
415 I915_ACTIVESTATE(i915
, I915_UPLOAD_TEX(i
), GL_FALSE
);
417 if (i915
->state
.tex_buffer
[i
] != NULL
) {
418 dri_bo_unreference(i915
->state
.tex_buffer
[i
]);
419 i915
->state
.tex_buffer
[i
] = NULL
;
430 FALLBACK(intel
, I915_FALLBACK_TEXTURE
, !ok
);