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.
29 #include "main/glheader.h"
30 #include "main/macros.h"
31 #include "main/mtypes.h"
32 #include "main/enums.h"
33 #include "main/formats.h"
34 #include "main/colortab.h"
35 #include "main/convolve.h"
36 #include "main/context.h"
37 #include "main/mipmap.h"
38 #include "main/simple_list.h"
39 #include "main/texcompress.h"
40 #include "main/texformat.h"
41 #include "main/texobj.h"
42 #include "main/texstore.h"
45 #include "via_context.h"
48 #include "via_state.h"
49 #include "via_ioctl.h"
50 #include "via_3d_reg.h"
53 viaChooseTexFormat( GLcontext
*ctx
, GLint internalFormat
,
54 GLenum format
, GLenum type
)
56 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
57 const GLboolean do32bpt
= ( vmesa
->viaScreen
->bitsPerPixel
== 32
58 /* && vmesa->viaScreen->textureSize > 4*1024*1024 */
62 switch ( internalFormat
) {
65 case GL_COMPRESSED_RGBA
:
66 if ( format
== GL_BGRA
) {
67 if ( type
== GL_UNSIGNED_INT_8_8_8_8_REV
||
68 type
== GL_UNSIGNED_BYTE
) {
69 return MESA_FORMAT_ARGB8888
;
71 else if ( type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
72 return MESA_FORMAT_ARGB4444
;
74 else if ( type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
75 return MESA_FORMAT_ARGB1555
;
78 else if ( type
== GL_UNSIGNED_BYTE
||
79 type
== GL_UNSIGNED_INT_8_8_8_8_REV
||
80 type
== GL_UNSIGNED_INT_8_8_8_8
) {
81 return MESA_FORMAT_ARGB8888
;
83 return do32bpt
? MESA_FORMAT_ARGB8888
: MESA_FORMAT_ARGB4444
;
87 case GL_COMPRESSED_RGB
:
88 if ( format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
89 return MESA_FORMAT_RGB565
;
91 else if ( type
== GL_UNSIGNED_BYTE
) {
92 return MESA_FORMAT_ARGB8888
;
94 return do32bpt
? MESA_FORMAT_ARGB8888
: MESA_FORMAT_RGB565
;
100 return MESA_FORMAT_ARGB8888
;
104 return MESA_FORMAT_ARGB4444
;
107 return MESA_FORMAT_ARGB1555
;
113 return MESA_FORMAT_ARGB8888
;
118 return MESA_FORMAT_RGB565
;
125 case GL_COMPRESSED_ALPHA
:
126 return MESA_FORMAT_A8
;
134 case GL_COMPRESSED_LUMINANCE
:
135 return MESA_FORMAT_L8
;
138 case GL_LUMINANCE_ALPHA
:
139 case GL_LUMINANCE4_ALPHA4
:
140 case GL_LUMINANCE6_ALPHA2
:
141 case GL_LUMINANCE8_ALPHA8
:
142 case GL_LUMINANCE12_ALPHA4
:
143 case GL_LUMINANCE12_ALPHA12
:
144 case GL_LUMINANCE16_ALPHA16
:
145 case GL_COMPRESSED_LUMINANCE_ALPHA
:
146 return MESA_FORMAT_AL88
;
153 case GL_COMPRESSED_INTENSITY
:
154 return MESA_FORMAT_I8
;
157 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
158 type
== GL_UNSIGNED_BYTE
)
159 return MESA_FORMAT_YCBCR
;
161 return MESA_FORMAT_YCBCR_REV
;
163 case GL_COMPRESSED_RGB_FXT1_3DFX
:
164 return MESA_FORMAT_RGB_FXT1
;
165 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
166 return MESA_FORMAT_RGBA_FXT1
;
170 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
171 return MESA_FORMAT_RGB_DXT1
;
173 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
174 return MESA_FORMAT_RGBA_DXT1
;
178 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
179 return MESA_FORMAT_RGBA_DXT3
;
181 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
182 return MESA_FORMAT_RGBA_DXT5
;
185 case GL_COLOR_INDEX1_EXT
:
186 case GL_COLOR_INDEX2_EXT
:
187 case GL_COLOR_INDEX4_EXT
:
188 case GL_COLOR_INDEX8_EXT
:
189 case GL_COLOR_INDEX12_EXT
:
190 case GL_COLOR_INDEX16_EXT
:
191 return MESA_FORMAT_CI8
;
194 fprintf(stderr
, "unexpected texture format %s in %s\n",
195 _mesa_lookup_enum_by_nr(internalFormat
),
197 return MESA_FORMAT_NONE
;
200 return MESA_FORMAT_NONE
; /* never get here */
203 static int logbase2(int n
)
216 static const char *get_memtype_name( GLint memType
)
218 static const char *names
[] = {
226 return names
[memType
];
230 static GLboolean
viaMoveTexBuffers( struct via_context
*vmesa
,
231 struct via_tex_buffer
**buffers
,
235 struct via_tex_buffer
*newTexBuf
[VIA_MAX_TEXLEVELS
];
238 if (VIA_DEBUG
& DEBUG_TEXTURE
)
239 fprintf(stderr
, "%s to %s\n",
241 get_memtype_name(newMemType
));
243 memset(newTexBuf
, 0, sizeof(newTexBuf
));
245 /* First do all the allocations (or fail):
247 for (i
= 0; i
< nr
; i
++) {
248 if (buffers
[i
]->memType
!= newMemType
) {
250 /* Don't allow uploads in a thrash state. Should try and
251 * catch this earlier.
253 if (vmesa
->thrashing
&& newMemType
!= VIA_MEM_SYSTEM
)
256 newTexBuf
[i
] = via_alloc_texture(vmesa
,
265 /* Now copy all the image data and free the old texture memory.
267 for (i
= 0; i
< nr
; i
++) {
269 memcpy(newTexBuf
[i
]->bufAddr
,
273 newTexBuf
[i
]->image
= buffers
[i
]->image
;
274 newTexBuf
[i
]->image
->texMem
= newTexBuf
[i
];
275 newTexBuf
[i
]->image
->image
.Data
= newTexBuf
[i
]->bufAddr
;
276 via_free_texture(vmesa
, buffers
[i
]);
280 if (VIA_DEBUG
& DEBUG_TEXTURE
)
281 fprintf(stderr
, "%s - success\n", __FUNCTION__
);
286 /* Release any allocations made prior to failure:
288 if (VIA_DEBUG
& DEBUG_TEXTURE
)
289 fprintf(stderr
, "%s - failed\n", __FUNCTION__
);
291 for (i
= 0; i
< nr
; i
++) {
293 via_free_texture(vmesa
, newTexBuf
[i
]);
301 static GLboolean
viaMoveTexObject( struct via_context
*vmesa
,
302 struct via_texture_object
*viaObj
,
305 struct via_texture_image
**viaImage
=
306 (struct via_texture_image
**)&viaObj
->obj
.Image
[0][0];
307 struct via_tex_buffer
*buffers
[VIA_MAX_TEXLEVELS
];
310 for (i
= viaObj
->firstLevel
; i
<= viaObj
->lastLevel
; i
++)
311 buffers
[nr
++] = viaImage
[i
]->texMem
;
313 if (viaMoveTexBuffers( vmesa
, &buffers
[0], nr
, newMemType
)) {
314 viaObj
->memType
= newMemType
;
323 static GLboolean
viaSwapInTexObject( struct via_context
*vmesa
,
324 struct via_texture_object
*viaObj
)
326 const struct via_texture_image
*baseImage
=
327 (struct via_texture_image
*)viaObj
->obj
.Image
[0][viaObj
->obj
.BaseLevel
];
329 if (VIA_DEBUG
& DEBUG_TEXTURE
)
330 fprintf(stderr
, "%s\n", __FUNCTION__
);
332 if (baseImage
->texMem
->memType
!= VIA_MEM_SYSTEM
)
333 return viaMoveTexObject( vmesa
, viaObj
, baseImage
->texMem
->memType
);
335 return (viaMoveTexObject( vmesa
, viaObj
, VIA_MEM_AGP
) ||
336 viaMoveTexObject( vmesa
, viaObj
, VIA_MEM_VIDEO
));
340 /* This seems crude, but it asks a fairly pertinent question and gives
341 * an accurate answer:
343 static GLboolean
viaIsTexMemLow( struct via_context
*vmesa
,
346 struct via_tex_buffer
*buf
= via_alloc_texture(vmesa
, 512 * 1024, heap
);
350 via_free_texture(vmesa
, buf
);
355 /* Speculatively move texture images which haven't been used in a
356 * while back to system memory.
358 * TODO: only do this when texture memory is low.
362 * TODO: keep the fb/agp version hanging around and use the local
363 * version as backing store, so re-upload might be avoided.
365 * TODO: do this properly in the kernel...
367 GLboolean
viaSwapOutWork( struct via_context
*vmesa
)
369 struct via_tex_buffer
*s
, *tmp
;
373 if (VIA_DEBUG
& DEBUG_TEXTURE
)
374 fprintf(stderr
, "%s VID %d AGP %d SYS %d\n", __FUNCTION__
,
375 vmesa
->total_alloc
[VIA_MEM_VIDEO
],
376 vmesa
->total_alloc
[VIA_MEM_AGP
],
377 vmesa
->total_alloc
[VIA_MEM_SYSTEM
]);
380 for (heap
= VIA_MEM_VIDEO
; heap
<= VIA_MEM_AGP
; heap
++) {
381 GLuint nr
= 0, sz
= 0;
383 if (vmesa
->thrashing
) {
384 if (VIA_DEBUG
& DEBUG_TEXTURE
)
385 fprintf(stderr
, "Heap %d: trash flag\n", heap
);
386 target
= 1*1024*1024;
388 else if (viaIsTexMemLow(vmesa
, heap
)) {
389 if (VIA_DEBUG
& DEBUG_TEXTURE
)
390 fprintf(stderr
, "Heap %d: low memory\n", heap
);
394 if (VIA_DEBUG
& DEBUG_TEXTURE
)
395 fprintf(stderr
, "Heap %d: nothing to do\n", heap
);
399 foreach_s( s
, tmp
, &vmesa
->tex_image_list
[heap
] ) {
400 if (s
->lastUsed
< vmesa
->lastSwap
[1]) {
401 struct via_texture_object
*viaObj
=
402 (struct via_texture_object
*) s
->image
->image
.TexObject
;
404 if (VIA_DEBUG
& DEBUG_TEXTURE
)
406 "back copy tex sz %d, lastUsed %d lastSwap %d\n",
407 s
->size
, s
->lastUsed
, vmesa
->lastSwap
[1]);
409 if (viaMoveTexBuffers( vmesa
, &s
, 1, VIA_MEM_SYSTEM
)) {
410 viaObj
->memType
= VIA_MEM_MIXED
;
414 if (VIA_DEBUG
& DEBUG_TEXTURE
)
415 fprintf(stderr
, "Failed to back copy texture!\n");
425 vmesa
->thrashing
= GL_FALSE
; /* might not get set otherwise? */
430 assert(sz
== vmesa
->total_alloc
[heap
]);
432 if (VIA_DEBUG
& DEBUG_TEXTURE
)
433 fprintf(stderr
, "Heap %d: nr %d tot sz %d\n", heap
, nr
, sz
);
442 /* Basically, just collect the image dimensions and addresses for each
443 * image and update the texture object state accordingly.
445 static GLboolean
viaSetTexImages(GLcontext
*ctx
,
446 struct gl_texture_object
*texObj
)
448 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
449 struct via_texture_object
*viaObj
= (struct via_texture_object
*)texObj
;
450 const struct via_texture_image
*baseImage
=
451 (struct via_texture_image
*)texObj
->Image
[0][texObj
->BaseLevel
];
452 GLint firstLevel
, lastLevel
, numLevels
;
455 GLint i
, j
= 0, k
= 0, l
= 0, m
= 0;
459 GLuint heightExp
= 0;
461 switch (baseImage
->image
.TexFormat
) {
462 case MESA_FORMAT_ARGB8888
:
463 texFormat
= HC_HTXnFM_ARGB8888
;
465 case MESA_FORMAT_ARGB4444
:
466 texFormat
= HC_HTXnFM_ARGB4444
;
468 case MESA_FORMAT_RGB565
:
469 texFormat
= HC_HTXnFM_RGB565
;
471 case MESA_FORMAT_ARGB1555
:
472 texFormat
= HC_HTXnFM_ARGB1555
;
474 case MESA_FORMAT_RGB888
:
475 texFormat
= HC_HTXnFM_ARGB0888
;
478 texFormat
= HC_HTXnFM_L8
;
481 texFormat
= HC_HTXnFM_T8
;
483 case MESA_FORMAT_CI8
:
484 texFormat
= HC_HTXnFM_Index8
;
486 case MESA_FORMAT_AL88
:
487 texFormat
= HC_HTXnFM_AL88
;
490 texFormat
= HC_HTXnFM_A8
;
493 _mesa_problem(vmesa
->glCtx
, "Bad texture format in viaSetTexImages");
497 /* Compute which mipmap levels we really want to send to the hardware.
498 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
499 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
500 * Yes, this looks overly complicated, but it's all needed.
502 if (texObj
->MinFilter
== GL_LINEAR
|| texObj
->MinFilter
== GL_NEAREST
) {
503 firstLevel
= lastLevel
= texObj
->BaseLevel
;
506 firstLevel
= texObj
->BaseLevel
+ (GLint
)(texObj
->MinLod
+ 0.5);
507 firstLevel
= MAX2(firstLevel
, texObj
->BaseLevel
);
508 lastLevel
= texObj
->BaseLevel
+ (GLint
)(texObj
->MaxLod
+ 0.5);
509 lastLevel
= MAX2(lastLevel
, texObj
->BaseLevel
);
510 lastLevel
= MIN2(lastLevel
, texObj
->BaseLevel
+ baseImage
->image
.MaxLog2
);
511 lastLevel
= MIN2(lastLevel
, texObj
->MaxLevel
);
512 lastLevel
= MAX2(firstLevel
, lastLevel
); /* need at least one level */
515 numLevels
= lastLevel
- firstLevel
+ 1;
517 /* The hardware supports only 10 mipmap levels; ignore higher levels.
519 if ((numLevels
> 10) && (ctx
->Const
.MaxTextureLevels
> 10)) {
520 lastLevel
-= numLevels
- 10;
524 /* save these values, check if they effect the residency of the
527 if (viaObj
->firstLevel
!= firstLevel
||
528 viaObj
->lastLevel
!= lastLevel
) {
529 viaObj
->firstLevel
= firstLevel
;
530 viaObj
->lastLevel
= lastLevel
;
531 viaObj
->memType
= VIA_MEM_MIXED
;
534 if (VIA_DEBUG
& DEBUG_TEXTURE
& 0)
535 fprintf(stderr
, "%s, current memType: %s\n",
537 get_memtype_name(viaObj
->memType
));
540 if (viaObj
->memType
== VIA_MEM_MIXED
||
541 viaObj
->memType
== VIA_MEM_SYSTEM
) {
542 if (!viaSwapInTexObject(vmesa
, viaObj
)) {
543 if (VIA_DEBUG
& DEBUG_TEXTURE
)
544 if (!vmesa
->thrashing
)
545 fprintf(stderr
, "Thrashing flag set for frame %d\n",
547 vmesa
->thrashing
= GL_TRUE
;
552 if (viaObj
->memType
== VIA_MEM_AGP
)
553 viaObj
->regTexFM
= (HC_SubA_HTXnFM
<< 24) | HC_HTXnLoc_AGP
| texFormat
;
555 viaObj
->regTexFM
= (HC_SubA_HTXnFM
<< 24) | HC_HTXnLoc_Local
| texFormat
;
558 for (i
= 0; i
< numLevels
; i
++) {
559 struct via_texture_image
*viaImage
=
560 (struct via_texture_image
*)texObj
->Image
[0][firstLevel
+ i
];
562 w
= viaImage
->image
.WidthLog2
;
563 h
= viaImage
->image
.HeightLog2
;
564 p
= viaImage
->pitchLog2
;
566 assert(viaImage
->texMem
->memType
== viaObj
->memType
);
568 texBase
= viaImage
->texMem
->texBase
;
570 if (VIA_DEBUG
& DEBUG_TEXTURE
)
571 fprintf(stderr
, "%s: no texBase[%d]\n", __FUNCTION__
, i
);
575 /* Image has to remain resident until the coming fence is retired.
577 move_to_head( &vmesa
->tex_image_list
[viaImage
->texMem
->memType
],
579 viaImage
->texMem
->lastUsed
= vmesa
->lastBreadcrumbWrite
;
582 viaObj
->regTexBaseAndPitch
[i
].baseL
=
583 ((HC_SubA_HTXnL0BasL
+ i
) << 24) | (texBase
& 0xFFFFFF);
585 viaObj
->regTexBaseAndPitch
[i
].pitchLog2
=
586 ((HC_SubA_HTXnL0Pit
+ i
) << 24) | (p
<< 20);
589 /* The base high bytes for each 3 levels are packed
590 * together into one register:
594 basH
|= ((texBase
& 0xFF000000) >> (k
<< 3));
596 viaObj
->regTexBaseH
[j
] = ((j
+ HC_SubA_HTXnL012BasH
) << 24) | basH
;
600 /* Likewise, sets of 6 log2width and log2height values are
601 * packed into individual registers:
605 widthExp
|= (((GLuint
)w
& 0xF) << (m
<< 2));
606 heightExp
|= (((GLuint
)h
& 0xF) << (m
<< 2));
608 viaObj
->regTexWidthLog2
[l
] =
609 (l
+ HC_SubA_HTXnL0_5WE
) << 24 | widthExp
;
610 viaObj
->regTexHeightLog2
[l
] =
611 (l
+ HC_SubA_HTXnL0_5HE
) << 24 | heightExp
;
621 viaObj
->regTexBaseH
[j
] = ((j
+ HC_SubA_HTXnL012BasH
) << 24) | basH
;
624 viaObj
->regTexWidthLog2
[l
] = (l
+ HC_SubA_HTXnL0_5WE
) << 24 | widthExp
;
625 viaObj
->regTexHeightLog2
[l
] = (l
+ HC_SubA_HTXnL0_5HE
) << 24 | heightExp
;
632 GLboolean
viaUpdateTextureState( GLcontext
*ctx
)
634 struct gl_texture_unit
*texUnit
= ctx
->Texture
.Unit
;
637 for (i
= 0; i
< 2; i
++) {
638 if (texUnit
[i
]._ReallyEnabled
== TEXTURE_2D_BIT
||
639 texUnit
[i
]._ReallyEnabled
== TEXTURE_1D_BIT
) {
641 if (!viaSetTexImages(ctx
, texUnit
[i
]._Current
))
644 else if (texUnit
[i
]._ReallyEnabled
) {
659 static void viaTexImage(GLcontext
*ctx
,
661 GLenum target
, GLint level
,
662 GLint internalFormat
,
663 GLint width
, GLint height
, GLint border
,
664 GLenum format
, GLenum type
, const void *pixels
,
665 const struct gl_pixelstore_attrib
*packing
,
666 struct gl_texture_object
*texObj
,
667 struct gl_texture_image
*texImage
)
669 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
670 GLint postConvWidth
= width
;
671 GLint postConvHeight
= height
;
672 GLint texelBytes
, sizeInBytes
;
673 struct via_texture_object
*viaObj
= (struct via_texture_object
*)texObj
;
674 struct via_texture_image
*viaImage
= (struct via_texture_image
*)texImage
;
675 int heaps
[3], nheaps
, i
;
676 GLuint compressedSize
;
678 if (!is_empty_list(&vmesa
->freed_tex_buffers
)) {
679 viaCheckBreadcrumb(vmesa
, 0);
680 via_release_pending_textures(vmesa
);
683 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
684 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
688 /* choose the texture format */
689 texImage
->TexFormat
= viaChooseTexFormat(ctx
, internalFormat
,
692 assert(texImage
->TexFormat
);
694 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
696 if (texelBytes
== 0) {
697 /* compressed format */
699 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
700 texImage
->Height
, texImage
->Depth
,
701 texImage
->TexFormat
);
704 /* Minimum pitch of 32 bytes */
705 if (postConvWidth
* texelBytes
< 32) {
706 postConvWidth
= 32 / texelBytes
;
707 texImage
->RowStride
= postConvWidth
;
710 assert(texImage
->RowStride
== postConvWidth
);
711 viaImage
->pitchLog2
= logbase2(postConvWidth
* texelBytes
);
713 /* allocate memory */
714 if (_mesa_is_format_compressed(texImage
->TexFormat
))
715 sizeInBytes
= compressedSize
;
717 sizeInBytes
= postConvWidth
* postConvHeight
* texelBytes
;
720 /* Attempt to allocate texture memory directly, otherwise use main
721 * memory and this texture will always be a fallback. FIXME!
723 * TODO: make room in agp if this fails.
724 * TODO: use fb ram for textures as well.
728 switch (viaObj
->memType
) {
729 case VIA_MEM_UNKNOWN
:
730 heaps
[0] = VIA_MEM_AGP
;
731 heaps
[1] = VIA_MEM_VIDEO
;
732 heaps
[2] = VIA_MEM_SYSTEM
;
737 heaps
[0] = viaObj
->memType
;
738 heaps
[1] = VIA_MEM_SYSTEM
;
744 heaps
[0] = VIA_MEM_SYSTEM
;
749 for (i
= 0; i
< nheaps
&& !viaImage
->texMem
; i
++) {
750 if (VIA_DEBUG
& DEBUG_TEXTURE
)
751 fprintf(stderr
, "try %s (obj %s)\n", get_memtype_name(heaps
[i
]),
752 get_memtype_name(viaObj
->memType
));
753 viaImage
->texMem
= via_alloc_texture(vmesa
, sizeInBytes
, heaps
[i
]);
756 if (!viaImage
->texMem
) {
757 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
761 if (VIA_DEBUG
& DEBUG_TEXTURE
)
762 fprintf(stderr
, "upload %d bytes to %s\n", sizeInBytes
,
763 get_memtype_name(viaImage
->texMem
->memType
));
765 viaImage
->texMem
->image
= viaImage
;
766 texImage
->Data
= viaImage
->texMem
->bufAddr
;
768 if (viaObj
->memType
== VIA_MEM_UNKNOWN
)
769 viaObj
->memType
= viaImage
->texMem
->memType
;
770 else if (viaObj
->memType
!= viaImage
->texMem
->memType
)
771 viaObj
->memType
= VIA_MEM_MIXED
;
773 if (VIA_DEBUG
& DEBUG_TEXTURE
)
774 fprintf(stderr
, "%s, obj %s, image : %s\n",
776 get_memtype_name(viaObj
->memType
),
777 get_memtype_name(viaImage
->texMem
->memType
));
779 vmesa
->clearTexCache
= 1;
781 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
783 pixels
, packing
, "glTexImage");
785 /* Note: we check for a NULL image pointer here, _after_ we allocated
786 * memory for the texture. That's what the GL spec calls for.
794 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
795 dstRowStride
= _mesa_compressed_row_stride(texImage
->TexFormat
, width
);
798 dstRowStride
= postConvWidth
* _mesa_get_format_bytes(texImage
->TexFormat
);
800 success
= _mesa_texstore(ctx
, dims
,
801 texImage
->_BaseFormat
,
804 0, 0, 0, /* dstX/Y/Zoffset */
806 texImage
->ImageOffsets
,
808 format
, type
, pixels
, packing
);
810 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
814 _mesa_unmap_teximage_pbo(ctx
, packing
);
817 static void viaTexImage2D(GLcontext
*ctx
,
818 GLenum target
, GLint level
,
819 GLint internalFormat
,
820 GLint width
, GLint height
, GLint border
,
821 GLenum format
, GLenum type
, const void *pixels
,
822 const struct gl_pixelstore_attrib
*packing
,
823 struct gl_texture_object
*texObj
,
824 struct gl_texture_image
*texImage
)
826 viaTexImage( ctx
, 2, target
, level
,
827 internalFormat
, width
, height
, border
,
828 format
, type
, pixels
,
829 packing
, texObj
, texImage
);
832 static void viaTexSubImage2D(GLcontext
*ctx
,
835 GLint xoffset
, GLint yoffset
,
836 GLsizei width
, GLsizei height
,
837 GLenum format
, GLenum type
,
838 const GLvoid
*pixels
,
839 const struct gl_pixelstore_attrib
*packing
,
840 struct gl_texture_object
*texObj
,
841 struct gl_texture_image
*texImage
)
843 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
845 viaWaitIdle(vmesa
, GL_TRUE
);
846 vmesa
->clearTexCache
= 1;
848 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
, width
,
849 height
, format
, type
, pixels
, packing
, texObj
,
853 static void viaTexImage1D(GLcontext
*ctx
,
854 GLenum target
, GLint level
,
855 GLint internalFormat
,
856 GLint width
, GLint border
,
857 GLenum format
, GLenum type
, const void *pixels
,
858 const struct gl_pixelstore_attrib
*packing
,
859 struct gl_texture_object
*texObj
,
860 struct gl_texture_image
*texImage
)
862 viaTexImage( ctx
, 1, target
, level
,
863 internalFormat
, width
, 1, border
,
864 format
, type
, pixels
,
865 packing
, texObj
, texImage
);
868 static void viaTexSubImage1D(GLcontext
*ctx
,
873 GLenum format
, GLenum type
,
874 const GLvoid
*pixels
,
875 const struct gl_pixelstore_attrib
*packing
,
876 struct gl_texture_object
*texObj
,
877 struct gl_texture_image
*texImage
)
879 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
881 viaWaitIdle(vmesa
, GL_TRUE
);
882 vmesa
->clearTexCache
= 1;
884 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
, width
,
885 format
, type
, pixels
, packing
, texObj
,
891 static GLboolean
viaIsTextureResident(GLcontext
*ctx
,
892 struct gl_texture_object
*texObj
)
894 struct via_texture_object
*viaObj
=
895 (struct via_texture_object
*)texObj
;
897 return (viaObj
->memType
== VIA_MEM_AGP
||
898 viaObj
->memType
== VIA_MEM_VIDEO
);
903 static struct gl_texture_image
*viaNewTextureImage( GLcontext
*ctx
)
906 return (struct gl_texture_image
*)CALLOC_STRUCT(via_texture_image
);
910 static struct gl_texture_object
*viaNewTextureObject( GLcontext
*ctx
,
914 struct via_texture_object
*obj
= CALLOC_STRUCT(via_texture_object
);
916 _mesa_initialize_texture_object(&obj
->obj
, name
, target
);
919 obj
->memType
= VIA_MEM_UNKNOWN
;
925 static void viaFreeTextureImageData( GLcontext
*ctx
,
926 struct gl_texture_image
*texImage
)
928 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
929 struct via_texture_image
*image
= (struct via_texture_image
*)texImage
;
932 via_free_texture(vmesa
, image
->texMem
);
933 image
->texMem
= NULL
;
936 texImage
->Data
= NULL
;
942 void viaInitTextureFuncs(struct dd_function_table
* functions
)
944 functions
->ChooseTextureFormat
= viaChooseTexFormat
;
945 functions
->TexImage1D
= viaTexImage1D
;
946 functions
->TexImage2D
= viaTexImage2D
;
947 functions
->TexSubImage1D
= viaTexSubImage1D
;
948 functions
->TexSubImage2D
= viaTexSubImage2D
;
950 functions
->NewTextureObject
= viaNewTextureObject
;
951 functions
->NewTextureImage
= viaNewTextureImage
;
952 functions
->DeleteTexture
= _mesa_delete_texture_object
;
953 functions
->FreeTexImageData
= viaFreeTextureImageData
;
955 #if 0 && defined( USE_SSE_ASM )
957 * XXX this code is disabled for now because the via_sse_memcpy()
958 * routine causes segfaults with flightgear.
959 * See Mesa3d-dev mail list messages from 7/15/2005 for details.
960 * Note that this function is currently disabled in via_tris.c too.
962 if (getenv("VIA_NO_SSE"))
963 functions
->TextureMemCpy
= _mesa_memcpy
;
965 functions
->TextureMemCpy
= via_sse_memcpy
;
967 functions
->TextureMemCpy
= _mesa_memcpy
;
970 functions
->UpdateTexturePalette
= 0;
971 functions
->IsTextureResident
= viaIsTextureResident
;