1 /* $XFree86: xc/lib/GL/mesa/src/drv/gamma/gamma_tex.c,v 1.4 2002/11/05 17:46:07 tsi Exp $ */
9 #include "simple_list.h"
13 #include "texformat.h"
15 #include "swrast/swrast.h"
18 #include "gamma_context.h"
23 * Compute the 'S2.4' lod bias factor from the floating point OpenGL bias.
26 static GLuint
gammaComputeLodBias(GLfloat bias
)
32 static void gammaSetTexWrapping(gammaTextureObjectPtr t
,
33 GLenum wraps
, GLenum wrapt
)
35 u_int32_t t1
= t
->TextureAddressMode
;
36 u_int32_t t2
= t
->TextureReadMode
;
38 t1
&= ~(TAM_SWrap_Mask
| TAM_TWrap_Mask
);
39 t2
&= ~(TRM_UWrap_Mask
| TRM_VWrap_Mask
);
41 if (wraps
!= GL_CLAMP
) {
42 t1
|= TAM_SWrap_Repeat
;
43 t2
|= TRM_UWrap_Repeat
;
46 if (wrapt
!= GL_CLAMP
) {
47 t1
|= TAM_TWrap_Repeat
;
48 t2
|= TRM_VWrap_Repeat
;
51 t
->TextureAddressMode
= t1
;
52 t
->TextureReadMode
= t2
;
56 static void gammaSetTexFilter(gammaContextPtr gmesa
,
57 gammaTextureObjectPtr t
,
58 GLenum minf
, GLenum magf
,
61 u_int32_t t1
= t
->TextureAddressMode
;
62 u_int32_t t2
= t
->TextureReadMode
;
64 t2
&= ~(TRM_Mag_Mask
| TRM_Min_Mask
);
69 t2
&= ~TRM_MipMapEnable
;
70 t2
|= TRM_Min_Nearest
;
74 t2
&= ~TRM_MipMapEnable
;
77 case GL_NEAREST_MIPMAP_NEAREST
:
78 t2
|= TRM_Min_NearestMMNearest
;
80 case GL_LINEAR_MIPMAP_NEAREST
:
81 t2
|= TRM_Min_LinearMMNearest
;
83 case GL_NEAREST_MIPMAP_LINEAR
:
84 t2
|= TRM_Min_NearestMMLinear
;
86 case GL_LINEAR_MIPMAP_LINEAR
:
87 t2
|= TRM_Min_LinearMMLinear
;
95 t2
|= TRM_Mag_Nearest
;
104 t
->TextureAddressMode
= t1
;
105 t
->TextureReadMode
= t2
;
109 static void gammaSetTexBorderColor(gammaContextPtr gmesa
,
110 gammaTextureObjectPtr t
,
113 t
->TextureBorderColor
= PACK_COLOR_8888(color
[0], color
[1], color
[2], color
[3]);
117 static void gammaTexParameter( GLcontext
*ctx
, GLenum target
,
118 struct gl_texture_object
*tObj
,
119 GLenum pname
, const GLfloat
*params
)
121 gammaContextPtr gmesa
= GAMMA_CONTEXT(ctx
);
122 gammaTextureObjectPtr t
= (gammaTextureObjectPtr
) tObj
->DriverData
;
126 /* Can't do the update now as we don't know whether to flush
127 * vertices or not. Setting gmesa->new_state means that
128 * gammaUpdateTextureState() will be called before any triangles are
129 * rendered. If a statechange has occurred, it will be detected at
130 * that point, and buffered vertices flushed.
133 case GL_TEXTURE_MIN_FILTER
:
134 case GL_TEXTURE_MAG_FILTER
:
136 GLfloat bias
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].LodBias
;
137 gammaSetTexFilter( gmesa
, t
, tObj
->MinFilter
, tObj
->MagFilter
, bias
);
141 case GL_TEXTURE_WRAP_S
:
142 case GL_TEXTURE_WRAP_T
:
143 gammaSetTexWrapping( t
, tObj
->WrapS
, tObj
->WrapT
);
146 case GL_TEXTURE_BORDER_COLOR
:
147 gammaSetTexBorderColor( gmesa
, t
, tObj
->_BorderChan
);
150 case GL_TEXTURE_BASE_LEVEL
:
151 case GL_TEXTURE_MAX_LEVEL
:
152 case GL_TEXTURE_MIN_LOD
:
153 case GL_TEXTURE_MAX_LOD
:
154 /* This isn't the most efficient solution but there doesn't appear to
155 * be a nice alternative for Radeon. Since there's no LOD clamping,
156 * we just have to rely on loading the right subset of mipmap levels
157 * to simulate a clamped LOD.
159 gammaSwapOutTexObj( gmesa
, t
);
166 if (t
== gmesa
->CurrentTexObj
[0])
167 gmesa
->dirty
|= GAMMA_UPLOAD_TEX0
;
170 if (t
== gmesa
->CurrentTexObj
[1]) {
171 gmesa
->dirty
|= GAMMA_UPLOAD_TEX1
;
177 static void gammaTexEnv( GLcontext
*ctx
, GLenum target
,
178 GLenum pname
, const GLfloat
*param
)
180 gammaContextPtr gmesa
= GAMMA_CONTEXT( ctx
);
181 GLuint unit
= ctx
->Texture
.CurrentUnit
;
183 /* Only one env color. Need a fallback if env colors are different
184 * and texture setup references env color in both units.
187 case GL_TEXTURE_ENV_COLOR
: {
188 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
189 GLfloat
*fc
= texUnit
->EnvColor
;
190 GLuint r
, g
, b
, a
, col
;
191 CLAMPED_FLOAT_TO_UBYTE(r
, fc
[0]);
192 CLAMPED_FLOAT_TO_UBYTE(g
, fc
[1]);
193 CLAMPED_FLOAT_TO_UBYTE(b
, fc
[2]);
194 CLAMPED_FLOAT_TO_UBYTE(a
, fc
[3]);
203 case GL_TEXTURE_ENV_MODE
:
204 gmesa
->TexEnvImageFmt
[unit
] = 0; /* force recalc of env state */
207 case GL_TEXTURE_LOD_BIAS_EXT
:
210 struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[unit
]._Current
;
211 gammaTextureObjectPtr t
= (gammaTextureObjectPtr
) tObj
->DriverData
;
213 /* XXX Looks like there's something missing here */
224 static void gammaTexImage1D( GLcontext
*ctx
, GLenum target
, GLint level
,
225 GLint internalFormat
,
226 GLint width
, GLint border
,
227 GLenum format
, GLenum type
,
228 const GLvoid
*pixels
,
229 const struct gl_pixelstore_attrib
*pack
,
230 struct gl_texture_object
*texObj
,
231 struct gl_texture_image
*texImage
)
233 gammaTextureObjectPtr t
= (gammaTextureObjectPtr
) texObj
->DriverData
;
235 gammaSwapOutTexObj( GAMMA_CONTEXT(ctx
), t
);
237 _mesa_store_teximage1d( ctx
, target
, level
, internalFormat
,
238 width
, border
, format
, type
,
239 pixels
, pack
, texObj
, texImage
);
244 static void gammaTexSubImage1D( GLcontext
*ctx
,
249 GLenum format
, GLenum type
,
250 const GLvoid
*pixels
,
251 const struct gl_pixelstore_attrib
*pack
,
252 struct gl_texture_object
*texObj
,
253 struct gl_texture_image
*texImage
)
255 gammaTextureObjectPtr t
= (gammaTextureObjectPtr
) texObj
->DriverData
;
257 gammaSwapOutTexObj( GAMMA_CONTEXT(ctx
), t
);
259 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
, width
,
260 format
, type
, pixels
, pack
, texObj
,
265 static void gammaTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
266 GLint internalFormat
,
267 GLint width
, GLint height
, GLint border
,
268 GLenum format
, GLenum type
, const GLvoid
*pixels
,
269 const struct gl_pixelstore_attrib
*packing
,
270 struct gl_texture_object
*texObj
,
271 struct gl_texture_image
*texImage
)
273 gammaTextureObjectPtr t
= (gammaTextureObjectPtr
) texObj
->DriverData
;
275 gammaSwapOutTexObj( GAMMA_CONTEXT(ctx
), t
);
277 _mesa_store_teximage2d( ctx
, target
, level
, internalFormat
,
278 width
, height
, border
, format
, type
,
279 pixels
, packing
, texObj
, texImage
);
282 static void gammaTexSubImage2D( GLcontext
*ctx
,
285 GLint xoffset
, GLint yoffset
,
286 GLsizei width
, GLsizei height
,
287 GLenum format
, GLenum type
,
288 const GLvoid
*pixels
,
289 const struct gl_pixelstore_attrib
*packing
,
290 struct gl_texture_object
*texObj
,
291 struct gl_texture_image
*texImage
)
293 gammaTextureObjectPtr t
= (gammaTextureObjectPtr
) texObj
->DriverData
;
295 gammaSwapOutTexObj( GAMMA_CONTEXT(ctx
), t
);
297 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
, width
,
298 height
, format
, type
, pixels
, packing
, texObj
,
302 static void gammaBindTexture( GLcontext
*ctx
, GLenum target
,
303 struct gl_texture_object
*tObj
)
305 gammaContextPtr gmesa
= GAMMA_CONTEXT( ctx
);
306 gammaTextureObjectPtr t
= (gammaTextureObjectPtr
) tObj
->DriverData
;
309 GLfloat bias
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].LodBias
;
310 t
= CALLOC_STRUCT(gamma_texture_object_t
);
312 /* Initialize non-image-dependent parts of the state:
316 t
->TextureAddressMode
= TextureAddressModeEnable
| TAM_Operation_3D
|
317 TAM_DY_Enable
| TAM_LODEnable
;
318 t
->TextureReadMode
= TextureReadModeEnable
| TRM_PrimaryCacheEnable
|
319 TRM_MipMapEnable
| TRM_BorderClamp
| TRM_Border
;
320 t
->TextureColorMode
= TextureColorModeEnable
;
321 t
->TextureFilterMode
= TextureFilterModeEnable
;
323 if (target
== GL_TEXTURE_2D
) {
324 t
->TextureAddressMode
|= TAM_TexMapType_2D
;
325 t
->TextureReadMode
|= TRM_TexMapType_2D
;
327 else if (target
== GL_TEXTURE_1D
) {
328 t
->TextureAddressMode
|= TAM_TexMapType_1D
;
329 t
->TextureReadMode
|= TRM_TexMapType_1D
;
332 t
->TextureColorMode
= TextureColorModeEnable
;
334 t
->TextureFilterMode
= TextureFilterModeEnable
;
336 #ifdef MESA_LITTLE_ENDIAN
337 t
->TextureFormat
= (TF_LittleEndian
|
339 t
->TextureFormat
= (TF_BigEndian
|
344 t
->dirty_images
= ~0;
346 tObj
->DriverData
= t
;
347 make_empty_list( t
);
349 gammaSetTexWrapping( t
, tObj
->WrapS
, tObj
->WrapT
);
350 gammaSetTexFilter( gmesa
, t
, tObj
->MinFilter
, tObj
->MagFilter
, bias
);
351 gammaSetTexBorderColor( gmesa
, t
, tObj
->_BorderChan
);
355 static void gammaDeleteTexture( GLcontext
*ctx
, struct gl_texture_object
*tObj
)
357 gammaTextureObjectPtr t
= (gammaTextureObjectPtr
)tObj
->DriverData
;
360 gammaContextPtr gmesa
= GAMMA_CONTEXT( ctx
);
363 GAMMA_FIREVERTICES( gmesa
);
365 gammaDestroyTexObj( gmesa
, t
);
366 tObj
->DriverData
= 0;
368 /* Free mipmap images and the texture object itself */
369 _mesa_delete_texture_object(ctx
, tObj
);
372 static GLboolean
gammaIsTextureResident( GLcontext
*ctx
,
373 struct gl_texture_object
*tObj
)
375 gammaTextureObjectPtr t
= (gammaTextureObjectPtr
)tObj
->DriverData
;
376 return t
&& t
->MemBlock
;
381 * Allocate a new texture object.
382 * Called via ctx->Driver.NewTextureObject.
383 * Note: this function will be called during context creation to
384 * allocate the default texture objects.
385 * Note: we could use containment here to 'derive' the driver-specific
386 * texture object from the core mesa gl_texture_object. Not done at this time.
388 static struct gl_texture_object
*
389 gammaNewTextureObject( GLcontext
*ctx
, GLuint name
, GLenum target
)
391 struct gl_texture_object
*obj
;
392 obj
= _mesa_new_texture_object(ctx
, name
, target
);
397 void gammaInitTextureObjects( GLcontext
*ctx
)
399 struct gl_texture_object
*texObj
;
400 GLuint tmp
= ctx
->Texture
.CurrentUnit
;
402 ctx
->Texture
.CurrentUnit
= 0;
404 texObj
= ctx
->Texture
.Unit
[0].Current1D
;
405 gammaBindTexture( ctx
, GL_TEXTURE_1D
, texObj
);
407 texObj
= ctx
->Texture
.Unit
[0].Current2D
;
408 gammaBindTexture( ctx
, GL_TEXTURE_2D
, texObj
);
411 ctx
->Texture
.CurrentUnit
= 1;
413 texObj
= ctx
->Texture
.Unit
[1].Current1D
;
414 gammaBindTexture( ctx
, GL_TEXTURE_1D
, texObj
);
416 texObj
= ctx
->Texture
.Unit
[1].Current2D
;
417 gammaBindTexture( ctx
, GL_TEXTURE_2D
, texObj
);
420 ctx
->Texture
.CurrentUnit
= tmp
;
424 void gammaDDInitTextureFuncs( struct dd_function_table
*functions
)
426 functions
->TexEnv
= gammaTexEnv
;
427 functions
->TexImage2D
= gammaTexImage2D
;
428 functions
->TexSubImage2D
= gammaTexSubImage2D
;
429 functions
->BindTexture
= gammaBindTexture
;
430 functions
->DeleteTexture
= gammaDeleteTexture
;
431 functions
->TexParameter
= gammaTexParameter
;
432 functions
->IsTextureResident
= gammaIsTextureResident
;