2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
31 #include "simple_list.h"
36 #include "texformat.h"
37 #include "swrast/swrast.h"
39 #include "via_context.h"
41 #include "via_state.h"
42 #include "via_ioctl.h"
46 * Compute the 'S2.4' lod bias factor from the floating point OpenGL bias.
49 static GLuint viaComputeLodBias(GLfloat bias)
51 int b = (int)(bias * 16.0) + 12;
56 return (GLuint)(b & MLC_LOD_BIAS_MASK);
60 viaTextureObjectPtr
viaAllocTextureObject(struct gl_texture_object
*texObj
)
62 viaTextureObjectPtr t
;
64 t
= (viaTextureObjectPtr
)CALLOC_STRUCT(via_texture_object_t
);
68 /* Initialize non-image-dependent parts of the state:
79 static void viaTexParameter(GLcontext
*ctx
, GLenum target
,
80 struct gl_texture_object
*texObj
,
81 GLenum pname
, const GLfloat
*params
)
83 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
87 if (target
!= GL_TEXTURE_2D
)
91 static void viaTexEnv(GLcontext
*ctx
, GLenum target
,
92 GLenum pname
, const GLfloat
*param
)
94 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
98 static void viaTexImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
100 GLint width
, GLint border
,
101 GLenum format
, GLenum type
,
102 const GLvoid
*pixels
,
103 const struct gl_pixelstore_attrib
*packing
,
104 struct gl_texture_object
*texObj
,
105 struct gl_texture_image
*texImage
)
107 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
109 if (VIA_DEBUG
) fprintf(stderr
, "viaTexImage1D - in\n");
113 viaSwapOutTexObj(VIA_CONTEXT(ctx
), t
);
120 t
= viaAllocTextureObject(texObj
);
122 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "viaTexImage1D");
125 texObj
->DriverData
= t
;
127 _mesa_store_teximage1d(ctx
, target
, level
, internalFormat
,
128 width
, border
, format
, type
,
129 pixels
, packing
, texObj
, texImage
);
131 if (VIA_DEBUG
) fprintf(stderr
, "viaTexImage1D - out\n");
134 static void viaTexSubImage1D(GLcontext
*ctx
,
139 GLenum format
, GLenum type
,
140 const GLvoid
*pixels
,
141 const struct gl_pixelstore_attrib
*packing
,
142 struct gl_texture_object
*texObj
,
143 struct gl_texture_image
*texImage
)
146 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
149 viaSwapOutTexObj(VIA_CONTEXT(ctx
), t
);
151 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
, width
,
152 format
, type
, pixels
, packing
, texObj
,
158 static void viaTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
159 GLint internalFormat
,
160 GLint width
, GLint height
, GLint border
,
161 GLenum format
, GLenum type
, const GLvoid
*pixels
,
162 const struct gl_pixelstore_attrib
*packing
,
163 struct gl_texture_object
*texObj
,
164 struct gl_texture_image
*texImage
)
166 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
168 if (VIA_DEBUG
) fprintf(stderr
, "viaTexImage2D - in\n");
172 viaSwapOutTexObj(VIA_CONTEXT(ctx
), t
);
179 t
= viaAllocTextureObject(texObj
);
181 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "viaTexImage2D");
184 texObj
->DriverData
= t
;
186 _mesa_store_teximage2d(ctx
, target
, level
, internalFormat
,
187 width
, height
, border
, format
, type
,
188 pixels
, packing
, texObj
, texImage
);
190 if (VIA_DEBUG
) fprintf(stderr
, "viaTexImage2D - out\n");
194 static void viaTexSubImage2D(GLcontext
*ctx
,
197 GLint xoffset
, GLint yoffset
,
198 GLsizei width
, GLsizei height
,
199 GLenum format
, GLenum type
,
200 const GLvoid
*pixels
,
201 const struct gl_pixelstore_attrib
*packing
,
202 struct gl_texture_object
*texObj
,
203 struct gl_texture_image
*texImage
)
205 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
207 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
210 viaSwapOutTexObj(VIA_CONTEXT(ctx
), t
);
212 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
, width
,
213 height
, format
, type
, pixels
, packing
, texObj
,
217 vmesa
->shareCtx
->NewState
|= _NEW_TEXTURE
;
221 static void viaBindTexture(GLcontext
*ctx
, GLenum target
,
222 struct gl_texture_object
*texObj
)
225 if (VIA_DEBUG
) fprintf(stderr
, "viaBindTexture - in\n");
227 if (target
== GL_TEXTURE_2D
) {
228 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
232 t
= viaAllocTextureObject(texObj
);
234 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "viaBindTexture");
237 texObj
->DriverData
= t
;
241 if (VIA_DEBUG
) fprintf(stderr
, "viaBindTexture - out\n");
245 static void viaDeleteTexture(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
247 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
249 if (VIA_DEBUG
) fprintf(stderr
, "viaDeleteTexture - in\n");
252 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
254 /*=* John Sheng [2003.7.18] viewperf frames/sec *=*/
255 /*VIA_FIREVERTICES(vmesa);*/
256 if (vmesa
->dma
[0].map
) { /* imply vmesa is not under destroying */
257 VIA_FIREVERTICES(vmesa
);
259 viaDestroyTexObj(vmesa
, t
);
261 texObj
->DriverData
= 0;
264 if (VIA_DEBUG
) fprintf(stderr
, "viaDeleteTexture - out\n");
268 static GLboolean
viaIsTextureResident(GLcontext
*ctx
,
269 struct gl_texture_object
*texObj
)
271 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
273 return t
&& t
->bufAddr
;
276 static const struct gl_texture_format
*
277 viaChooseTexFormat(GLcontext
*ctx
, GLint internalFormat
,
278 GLenum format
, GLenum type
)
280 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
284 if (VIA_DEBUG
) fprintf(stderr
, "%s in\n", __FUNCTION__
);
285 if (VIA_DEBUG
) fprintf(stderr
, "internalFormat:%d format:%d\n", internalFormat
, format
);
287 switch (internalFormat
) {
294 return &_mesa_texformat_l8
;
296 case GL_LUMINANCE_ALPHA
:
297 case GL_LUMINANCE4_ALPHA4
:
298 case GL_LUMINANCE6_ALPHA2
:
299 case GL_LUMINANCE8_ALPHA8
:
300 case GL_LUMINANCE12_ALPHA4
:
301 case GL_LUMINANCE12_ALPHA12
:
302 case GL_LUMINANCE16_ALPHA16
:
303 return &_mesa_texformat_al88
;
308 if (VIA_DEBUG
) fprintf(stderr
, "2 &_mesa_texformat_arg565\n");
310 return &_mesa_texformat_rgb565
;
317 if (vmesa
->viaScreen
->bitsPerPixel
== 0x20) {
319 if (VIA_DEBUG
) fprintf(stderr
,"3 argb8888\n");
321 return &_mesa_texformat_argb8888
;
325 if (VIA_DEBUG
) fprintf(stderr
,"3 rgb565\n");
327 return &_mesa_texformat_rgb565
;
330 if (vmesa
->viaScreen
->bitsPerPixel
== 0x20) {
332 if (VIA_DEBUG
) fprintf(stderr
, "4 &_mesa_texformat_argb8888\n");
334 return &_mesa_texformat_argb8888
;
338 if (VIA_DEBUG
) fprintf(stderr
, "4 &_mesa_texformat_argb4444\n");
340 return &_mesa_texformat_argb4444
;
345 if (VIA_DEBUG
) fprintf(stderr
, "GL_RGBA4 &_mesa_texformat_argb4444\n");
347 return &_mesa_texformat_argb4444
;
351 if (VIA_DEBUG
) fprintf(stderr
, "GL_RGB5_A1 &_mesa_texformat_argb1555\n");
353 return &_mesa_texformat_argb1555
;
360 if (VIA_DEBUG
) fprintf(stderr
, "GL_RGBA &_mesa_texformat_argb8888\n");
362 return &_mesa_texformat_argb8888
;
368 return &_mesa_texformat_a8
;
374 return &_mesa_texformat_i8
;
376 case GL_COLOR_INDEX1_EXT
:
377 case GL_COLOR_INDEX2_EXT
:
378 case GL_COLOR_INDEX4_EXT
:
379 case GL_COLOR_INDEX8_EXT
:
380 case GL_COLOR_INDEX12_EXT
:
381 case GL_COLOR_INDEX16_EXT
:
382 return &_mesa_texformat_ci8
;
384 _mesa_problem(ctx
, "unexpected format in viaChooseTextureFormat");
389 void viaInitTextureFuncs(struct dd_function_table
* functions
)
392 if (VIA_DEBUG
) fprintf(stderr
, "viaInitTextureFuncs - in\n");
394 functions
->TexEnv
= viaTexEnv
;
395 functions
->ChooseTextureFormat
= viaChooseTexFormat
;
396 functions
->TexImage1D
= viaTexImage1D
;
397 functions
->TexImage2D
= viaTexImage2D
;
398 functions
->TexImage3D
= _mesa_store_teximage3d
;
399 functions
->TexSubImage1D
= viaTexSubImage1D
;
400 functions
->TexSubImage2D
= viaTexSubImage2D
;
401 functions
->TexSubImage3D
= _mesa_store_texsubimage3d
;
402 functions
->CopyTexImage1D
= _swrast_copy_teximage1d
;
403 functions
->CopyTexImage2D
= _swrast_copy_teximage2d
;
404 functions
->CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
405 functions
->CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
406 functions
->CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
408 functions
->NewTextureObject
= _mesa_new_texture_object
;
409 functions
->BindTexture
= viaBindTexture
;
410 functions
->DeleteTexture
= viaDeleteTexture
;
411 functions
->TexParameter
= viaTexParameter
;
412 functions
->UpdateTexturePalette
= 0;
413 functions
->IsTextureResident
= viaIsTextureResident
;
414 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;
417 if (VIA_DEBUG
) fprintf(stderr
, "viaInitTextureFuncs - out\n");
421 void viaInitTextures(GLcontext
*ctx
)
423 GLuint tmp
= ctx
->Texture
.CurrentUnit
;
424 ctx
->Texture
.CurrentUnit
= 0;
425 viaBindTexture(ctx
, GL_TEXTURE_1D
, ctx
->Texture
.Unit
[0].Current1D
);
426 viaBindTexture(ctx
, GL_TEXTURE_2D
, ctx
->Texture
.Unit
[0].Current2D
);
427 ctx
->Texture
.CurrentUnit
= 1;
428 viaBindTexture(ctx
, GL_TEXTURE_1D
, ctx
->Texture
.Unit
[1].Current1D
);
429 viaBindTexture(ctx
, GL_TEXTURE_2D
, ctx
->Texture
.Unit
[1].Current2D
);
430 ctx
->Texture
.CurrentUnit
= tmp
;