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
;
108 if (VIA_DEBUG
) fprintf(stderr
, "viaTexImage1D - in\n");
111 viaSwapOutTexObj(VIA_CONTEXT(ctx
), t
);
118 t
= viaAllocTextureObject(texObj
);
120 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "viaTexImage1D");
123 texObj
->DriverData
= t
;
125 _mesa_store_teximage1d(ctx
, target
, level
, internalFormat
,
126 width
, border
, format
, type
,
127 pixels
, packing
, texObj
, texImage
);
128 if (VIA_DEBUG
) fprintf(stderr
, "viaTexImage1D - out\n");
130 static void viaTexSubImage1D(GLcontext
*ctx
,
135 GLenum format
, GLenum type
,
136 const GLvoid
*pixels
,
137 const struct gl_pixelstore_attrib
*packing
,
138 struct gl_texture_object
*texObj
,
139 struct gl_texture_image
*texImage
)
142 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
145 viaSwapOutTexObj(VIA_CONTEXT(ctx
), t
);
147 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
, width
,
148 format
, type
, pixels
, packing
, texObj
,
154 static void viaTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
155 GLint internalFormat
,
156 GLint width
, GLint height
, GLint border
,
157 GLenum format
, GLenum type
, const GLvoid
*pixels
,
158 const struct gl_pixelstore_attrib
*packing
,
159 struct gl_texture_object
*texObj
,
160 struct gl_texture_image
*texImage
)
162 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
163 if (VIA_DEBUG
) fprintf(stderr
, "viaTexImage2D - in\n");
166 viaSwapOutTexObj(VIA_CONTEXT(ctx
), t
);
173 t
= viaAllocTextureObject(texObj
);
175 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "viaTexImage2D");
178 texObj
->DriverData
= t
;
180 _mesa_store_teximage2d(ctx
, target
, level
, internalFormat
,
181 width
, height
, border
, format
, type
,
182 pixels
, packing
, texObj
, texImage
);
183 if (VIA_DEBUG
) fprintf(stderr
, "viaTexImage2D - out\n");
186 static void viaTexSubImage2D(GLcontext
*ctx
,
189 GLint xoffset
, GLint yoffset
,
190 GLsizei width
, GLsizei height
,
191 GLenum format
, GLenum type
,
192 const GLvoid
*pixels
,
193 const struct gl_pixelstore_attrib
*packing
,
194 struct gl_texture_object
*texObj
,
195 struct gl_texture_image
*texImage
)
197 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
199 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
202 viaSwapOutTexObj(VIA_CONTEXT(ctx
), t
);
204 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
, width
,
205 height
, format
, type
, pixels
, packing
, texObj
,
209 vmesa
->shareCtx
->NewState
|= _NEW_TEXTURE
;
213 static void viaBindTexture(GLcontext
*ctx
, GLenum target
,
214 struct gl_texture_object
*texObj
)
216 if (VIA_DEBUG
) fprintf(stderr
, "viaBindTexture - in\n");
217 if (target
== GL_TEXTURE_2D
) {
218 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
222 t
= viaAllocTextureObject(texObj
);
224 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "viaBindTexture");
227 texObj
->DriverData
= t
;
230 if (VIA_DEBUG
) fprintf(stderr
, "viaBindTexture - out\n");
233 static void viaDeleteTexture(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
235 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
236 if (VIA_DEBUG
) fprintf(stderr
, "viaDeleteTexture - in\n");
238 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
240 if (vmesa
->dma
) { /* imply vmesa is not under destroying */
241 VIA_FLUSH_DMA(vmesa
);
243 viaDestroyTexObj(vmesa
, t
);
245 texObj
->DriverData
= 0;
247 if (VIA_DEBUG
) fprintf(stderr
, "viaDeleteTexture - out\n");
249 /* Free mipmap images and the texture object itself */
250 _mesa_delete_texture_object(ctx
, texObj
);
253 static GLboolean
viaIsTextureResident(GLcontext
*ctx
,
254 struct gl_texture_object
*texObj
)
256 viaTextureObjectPtr t
= (viaTextureObjectPtr
)texObj
->DriverData
;
258 return t
&& t
->bufAddr
;
261 static const struct gl_texture_format
*
262 viaChooseTexFormat(GLcontext
*ctx
, GLint internalFormat
,
263 GLenum format
, GLenum type
)
265 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
268 if (VIA_DEBUG
) fprintf(stderr
, "%s in\n", __FUNCTION__
);
269 if (VIA_DEBUG
) fprintf(stderr
, "internalFormat:%d format:%d\n", internalFormat
, format
);
270 switch (internalFormat
) {
277 return &_mesa_texformat_l8
;
279 case GL_LUMINANCE_ALPHA
:
280 case GL_LUMINANCE4_ALPHA4
:
281 case GL_LUMINANCE6_ALPHA2
:
282 case GL_LUMINANCE8_ALPHA8
:
283 case GL_LUMINANCE12_ALPHA4
:
284 case GL_LUMINANCE12_ALPHA12
:
285 case GL_LUMINANCE16_ALPHA16
:
286 return &_mesa_texformat_al88
;
290 if (VIA_DEBUG
) fprintf(stderr
, "2 &_mesa_texformat_arg565\n");
291 return &_mesa_texformat_rgb565
;
298 if (vmesa
->viaScreen
->bitsPerPixel
== 0x20) {
299 if (VIA_DEBUG
) fprintf(stderr
,"3 argb8888\n");
300 return &_mesa_texformat_argb8888
;
303 if (VIA_DEBUG
) fprintf(stderr
,"3 rgb565\n");
304 return &_mesa_texformat_rgb565
;
307 if (vmesa
->viaScreen
->bitsPerPixel
== 0x20) {
308 if (VIA_DEBUG
) fprintf(stderr
, "4 &_mesa_texformat_argb8888\n");
309 return &_mesa_texformat_argb8888
;
312 if (VIA_DEBUG
) fprintf(stderr
, "4 &_mesa_texformat_argb4444\n");
313 return &_mesa_texformat_argb4444
;
317 if (VIA_DEBUG
) fprintf(stderr
, "GL_RGBA4 &_mesa_texformat_argb4444\n");
318 return &_mesa_texformat_argb4444
;
321 if (VIA_DEBUG
) fprintf(stderr
, "GL_RGB5_A1 &_mesa_texformat_argb1555\n");
322 return &_mesa_texformat_argb1555
;
328 if (VIA_DEBUG
) fprintf(stderr
, "GL_RGBA &_mesa_texformat_argb8888\n");
329 return &_mesa_texformat_argb8888
;
335 return &_mesa_texformat_a8
;
341 return &_mesa_texformat_i8
;
343 case GL_COLOR_INDEX1_EXT
:
344 case GL_COLOR_INDEX2_EXT
:
345 case GL_COLOR_INDEX4_EXT
:
346 case GL_COLOR_INDEX8_EXT
:
347 case GL_COLOR_INDEX12_EXT
:
348 case GL_COLOR_INDEX16_EXT
:
349 return &_mesa_texformat_ci8
;
351 _mesa_problem(ctx
, "unexpected format in viaChooseTextureFormat");
356 void viaInitTextureFuncs(struct dd_function_table
* functions
)
358 if (VIA_DEBUG
) fprintf(stderr
, "viaInitTextureFuncs - in\n");
359 functions
->TexEnv
= viaTexEnv
;
360 functions
->ChooseTextureFormat
= viaChooseTexFormat
;
361 functions
->TexImage1D
= viaTexImage1D
;
362 functions
->TexImage2D
= viaTexImage2D
;
363 functions
->TexImage3D
= _mesa_store_teximage3d
;
364 functions
->TexSubImage1D
= viaTexSubImage1D
;
365 functions
->TexSubImage2D
= viaTexSubImage2D
;
366 functions
->TexSubImage3D
= _mesa_store_texsubimage3d
;
367 functions
->CopyTexImage1D
= _swrast_copy_teximage1d
;
368 functions
->CopyTexImage2D
= _swrast_copy_teximage2d
;
369 functions
->CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
370 functions
->CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
371 functions
->CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
373 functions
->NewTextureObject
= _mesa_new_texture_object
;
374 functions
->BindTexture
= viaBindTexture
;
375 functions
->DeleteTexture
= viaDeleteTexture
;
376 functions
->TexParameter
= viaTexParameter
;
377 functions
->UpdateTexturePalette
= 0;
378 functions
->IsTextureResident
= viaIsTextureResident
;
379 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;
381 if (VIA_DEBUG
) fprintf(stderr
, "viaInitTextureFuncs - out\n");
384 void viaInitTextures(GLcontext
*ctx
)
386 GLuint tmp
= ctx
->Texture
.CurrentUnit
;
387 ctx
->Texture
.CurrentUnit
= 0;
388 viaBindTexture(ctx
, GL_TEXTURE_1D
, ctx
->Texture
.Unit
[0].Current1D
);
389 viaBindTexture(ctx
, GL_TEXTURE_2D
, ctx
->Texture
.Unit
[0].Current2D
);
390 ctx
->Texture
.CurrentUnit
= 1;
391 viaBindTexture(ctx
, GL_TEXTURE_1D
, ctx
->Texture
.Unit
[1].Current1D
);
392 viaBindTexture(ctx
, GL_TEXTURE_2D
, ctx
->Texture
.Unit
[1].Current2D
);
393 ctx
->Texture
.CurrentUnit
= tmp
;