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/texfetch.h"
41 #include "main/texformat.h"
42 #include "main/texobj.h"
43 #include "main/texstore.h"
46 #include "via_context.h"
49 #include "via_state.h"
50 #include "via_ioctl.h"
51 #include "via_3d_reg.h"
54 viaChooseTexFormat( GLcontext
*ctx
, GLint internalFormat
,
55 GLenum format
, GLenum type
)
57 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
58 const GLboolean do32bpt
= ( vmesa
->viaScreen
->bitsPerPixel
== 32
59 /* && vmesa->viaScreen->textureSize > 4*1024*1024 */
63 switch ( internalFormat
) {
66 case GL_COMPRESSED_RGBA
:
67 if ( format
== GL_BGRA
) {
68 if ( type
== GL_UNSIGNED_INT_8_8_8_8_REV
||
69 type
== GL_UNSIGNED_BYTE
) {
70 return MESA_FORMAT_ARGB8888
;
72 else if ( type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
) {
73 return MESA_FORMAT_ARGB4444
;
75 else if ( type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
) {
76 return MESA_FORMAT_ARGB1555
;
79 else if ( type
== GL_UNSIGNED_BYTE
||
80 type
== GL_UNSIGNED_INT_8_8_8_8_REV
||
81 type
== GL_UNSIGNED_INT_8_8_8_8
) {
82 return MESA_FORMAT_ARGB8888
;
84 return do32bpt
? MESA_FORMAT_ARGB8888
: MESA_FORMAT_ARGB4444
;
88 case GL_COMPRESSED_RGB
:
89 if ( format
== GL_RGB
&& type
== GL_UNSIGNED_SHORT_5_6_5
) {
90 return MESA_FORMAT_RGB565
;
92 else if ( type
== GL_UNSIGNED_BYTE
) {
93 return MESA_FORMAT_ARGB8888
;
95 return do32bpt
? MESA_FORMAT_ARGB8888
: MESA_FORMAT_RGB565
;
101 return MESA_FORMAT_ARGB8888
;
105 return MESA_FORMAT_ARGB4444
;
108 return MESA_FORMAT_ARGB1555
;
114 return MESA_FORMAT_ARGB8888
;
119 return MESA_FORMAT_RGB565
;
126 case GL_COMPRESSED_ALPHA
:
127 return MESA_FORMAT_A8
;
135 case GL_COMPRESSED_LUMINANCE
:
136 return MESA_FORMAT_L8
;
139 case GL_LUMINANCE_ALPHA
:
140 case GL_LUMINANCE4_ALPHA4
:
141 case GL_LUMINANCE6_ALPHA2
:
142 case GL_LUMINANCE8_ALPHA8
:
143 case GL_LUMINANCE12_ALPHA4
:
144 case GL_LUMINANCE12_ALPHA12
:
145 case GL_LUMINANCE16_ALPHA16
:
146 case GL_COMPRESSED_LUMINANCE_ALPHA
:
147 return MESA_FORMAT_AL88
;
154 case GL_COMPRESSED_INTENSITY
:
155 return MESA_FORMAT_I8
;
158 if (type
== GL_UNSIGNED_SHORT_8_8_MESA
||
159 type
== GL_UNSIGNED_BYTE
)
160 return MESA_FORMAT_YCBCR
;
162 return MESA_FORMAT_YCBCR_REV
;
164 case GL_COMPRESSED_RGB_FXT1_3DFX
:
165 return MESA_FORMAT_RGB_FXT1
;
166 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
167 return MESA_FORMAT_RGBA_FXT1
;
171 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
172 return MESA_FORMAT_RGB_DXT1
;
174 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
175 return MESA_FORMAT_RGBA_DXT1
;
179 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
180 return MESA_FORMAT_RGBA_DXT3
;
182 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
183 return MESA_FORMAT_RGBA_DXT5
;
186 case GL_COLOR_INDEX1_EXT
:
187 case GL_COLOR_INDEX2_EXT
:
188 case GL_COLOR_INDEX4_EXT
:
189 case GL_COLOR_INDEX8_EXT
:
190 case GL_COLOR_INDEX12_EXT
:
191 case GL_COLOR_INDEX16_EXT
:
192 return MESA_FORMAT_CI8
;
195 fprintf(stderr
, "unexpected texture format %s in %s\n",
196 _mesa_lookup_enum_by_nr(internalFormat
),
198 return MESA_FORMAT_NONE
;
201 return MESA_FORMAT_NONE
; /* never get here */
204 static int logbase2(int n
)
217 static const char *get_memtype_name( GLint memType
)
219 static const char *names
[] = {
227 return names
[memType
];
231 static GLboolean
viaMoveTexBuffers( struct via_context
*vmesa
,
232 struct via_tex_buffer
**buffers
,
236 struct via_tex_buffer
*newTexBuf
[VIA_MAX_TEXLEVELS
];
239 if (VIA_DEBUG
& DEBUG_TEXTURE
)
240 fprintf(stderr
, "%s to %s\n",
242 get_memtype_name(newMemType
));
244 memset(newTexBuf
, 0, sizeof(newTexBuf
));
246 /* First do all the allocations (or fail):
248 for (i
= 0; i
< nr
; i
++) {
249 if (buffers
[i
]->memType
!= newMemType
) {
251 /* Don't allow uploads in a thrash state. Should try and
252 * catch this earlier.
254 if (vmesa
->thrashing
&& newMemType
!= VIA_MEM_SYSTEM
)
257 newTexBuf
[i
] = via_alloc_texture(vmesa
,
266 /* Now copy all the image data and free the old texture memory.
268 for (i
= 0; i
< nr
; i
++) {
270 memcpy(newTexBuf
[i
]->bufAddr
,
274 newTexBuf
[i
]->image
= buffers
[i
]->image
;
275 newTexBuf
[i
]->image
->texMem
= newTexBuf
[i
];
276 newTexBuf
[i
]->image
->image
.Data
= newTexBuf
[i
]->bufAddr
;
277 via_free_texture(vmesa
, buffers
[i
]);
281 if (VIA_DEBUG
& DEBUG_TEXTURE
)
282 fprintf(stderr
, "%s - success\n", __FUNCTION__
);
287 /* Release any allocations made prior to failure:
289 if (VIA_DEBUG
& DEBUG_TEXTURE
)
290 fprintf(stderr
, "%s - failed\n", __FUNCTION__
);
292 for (i
= 0; i
< nr
; i
++) {
294 via_free_texture(vmesa
, newTexBuf
[i
]);
302 static GLboolean
viaMoveTexObject( struct via_context
*vmesa
,
303 struct via_texture_object
*viaObj
,
306 struct via_texture_image
**viaImage
=
307 (struct via_texture_image
**)&viaObj
->obj
.Image
[0][0];
308 struct via_tex_buffer
*buffers
[VIA_MAX_TEXLEVELS
];
311 for (i
= viaObj
->firstLevel
; i
<= viaObj
->lastLevel
; i
++)
312 buffers
[nr
++] = viaImage
[i
]->texMem
;
314 if (viaMoveTexBuffers( vmesa
, &buffers
[0], nr
, newMemType
)) {
315 viaObj
->memType
= newMemType
;
324 static GLboolean
viaSwapInTexObject( struct via_context
*vmesa
,
325 struct via_texture_object
*viaObj
)
327 const struct via_texture_image
*baseImage
=
328 (struct via_texture_image
*)viaObj
->obj
.Image
[0][viaObj
->obj
.BaseLevel
];
330 if (VIA_DEBUG
& DEBUG_TEXTURE
)
331 fprintf(stderr
, "%s\n", __FUNCTION__
);
333 if (baseImage
->texMem
->memType
!= VIA_MEM_SYSTEM
)
334 return viaMoveTexObject( vmesa
, viaObj
, baseImage
->texMem
->memType
);
336 return (viaMoveTexObject( vmesa
, viaObj
, VIA_MEM_AGP
) ||
337 viaMoveTexObject( vmesa
, viaObj
, VIA_MEM_VIDEO
));
341 /* This seems crude, but it asks a fairly pertinent question and gives
342 * an accurate answer:
344 static GLboolean
viaIsTexMemLow( struct via_context
*vmesa
,
347 struct via_tex_buffer
*buf
= via_alloc_texture(vmesa
, 512 * 1024, heap
);
351 via_free_texture(vmesa
, buf
);
356 /* Speculatively move texture images which haven't been used in a
357 * while back to system memory.
359 * TODO: only do this when texture memory is low.
363 * TODO: keep the fb/agp version hanging around and use the local
364 * version as backing store, so re-upload might be avoided.
366 * TODO: do this properly in the kernel...
368 GLboolean
viaSwapOutWork( struct via_context
*vmesa
)
370 struct via_tex_buffer
*s
, *tmp
;
374 if (VIA_DEBUG
& DEBUG_TEXTURE
)
375 fprintf(stderr
, "%s VID %d AGP %d SYS %d\n", __FUNCTION__
,
376 vmesa
->total_alloc
[VIA_MEM_VIDEO
],
377 vmesa
->total_alloc
[VIA_MEM_AGP
],
378 vmesa
->total_alloc
[VIA_MEM_SYSTEM
]);
381 for (heap
= VIA_MEM_VIDEO
; heap
<= VIA_MEM_AGP
; heap
++) {
382 GLuint nr
= 0, sz
= 0;
384 if (vmesa
->thrashing
) {
385 if (VIA_DEBUG
& DEBUG_TEXTURE
)
386 fprintf(stderr
, "Heap %d: trash flag\n", heap
);
387 target
= 1*1024*1024;
389 else if (viaIsTexMemLow(vmesa
, heap
)) {
390 if (VIA_DEBUG
& DEBUG_TEXTURE
)
391 fprintf(stderr
, "Heap %d: low memory\n", heap
);
395 if (VIA_DEBUG
& DEBUG_TEXTURE
)
396 fprintf(stderr
, "Heap %d: nothing to do\n", heap
);
400 foreach_s( s
, tmp
, &vmesa
->tex_image_list
[heap
] ) {
401 if (s
->lastUsed
< vmesa
->lastSwap
[1]) {
402 struct via_texture_object
*viaObj
=
403 (struct via_texture_object
*) s
->image
->image
.TexObject
;
405 if (VIA_DEBUG
& DEBUG_TEXTURE
)
407 "back copy tex sz %d, lastUsed %d lastSwap %d\n",
408 s
->size
, s
->lastUsed
, vmesa
->lastSwap
[1]);
410 if (viaMoveTexBuffers( vmesa
, &s
, 1, VIA_MEM_SYSTEM
)) {
411 viaObj
->memType
= VIA_MEM_MIXED
;
415 if (VIA_DEBUG
& DEBUG_TEXTURE
)
416 fprintf(stderr
, "Failed to back copy texture!\n");
426 vmesa
->thrashing
= GL_FALSE
; /* might not get set otherwise? */
431 assert(sz
== vmesa
->total_alloc
[heap
]);
433 if (VIA_DEBUG
& DEBUG_TEXTURE
)
434 fprintf(stderr
, "Heap %d: nr %d tot sz %d\n", heap
, nr
, sz
);
443 /* Basically, just collect the image dimensions and addresses for each
444 * image and update the texture object state accordingly.
446 static GLboolean
viaSetTexImages(GLcontext
*ctx
,
447 struct gl_texture_object
*texObj
)
449 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
450 struct via_texture_object
*viaObj
= (struct via_texture_object
*)texObj
;
451 const struct via_texture_image
*baseImage
=
452 (struct via_texture_image
*)texObj
->Image
[0][texObj
->BaseLevel
];
453 GLint firstLevel
, lastLevel
, numLevels
;
456 GLint i
, j
= 0, k
= 0, l
= 0, m
= 0;
460 GLuint heightExp
= 0;
462 switch (baseImage
->image
.TexFormat
) {
463 case MESA_FORMAT_ARGB8888
:
464 texFormat
= HC_HTXnFM_ARGB8888
;
466 case MESA_FORMAT_ARGB4444
:
467 texFormat
= HC_HTXnFM_ARGB4444
;
469 case MESA_FORMAT_RGB565
:
470 texFormat
= HC_HTXnFM_RGB565
;
472 case MESA_FORMAT_ARGB1555
:
473 texFormat
= HC_HTXnFM_ARGB1555
;
475 case MESA_FORMAT_RGB888
:
476 texFormat
= HC_HTXnFM_ARGB0888
;
479 texFormat
= HC_HTXnFM_L8
;
482 texFormat
= HC_HTXnFM_T8
;
484 case MESA_FORMAT_CI8
:
485 texFormat
= HC_HTXnFM_Index8
;
487 case MESA_FORMAT_AL88
:
488 texFormat
= HC_HTXnFM_AL88
;
491 texFormat
= HC_HTXnFM_A8
;
494 _mesa_problem(vmesa
->glCtx
, "Bad texture format in viaSetTexImages");
498 /* Compute which mipmap levels we really want to send to the hardware.
499 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
500 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
501 * Yes, this looks overly complicated, but it's all needed.
503 if (texObj
->MinFilter
== GL_LINEAR
|| texObj
->MinFilter
== GL_NEAREST
) {
504 firstLevel
= lastLevel
= texObj
->BaseLevel
;
507 firstLevel
= texObj
->BaseLevel
+ (GLint
)(texObj
->MinLod
+ 0.5);
508 firstLevel
= MAX2(firstLevel
, texObj
->BaseLevel
);
509 lastLevel
= texObj
->BaseLevel
+ (GLint
)(texObj
->MaxLod
+ 0.5);
510 lastLevel
= MAX2(lastLevel
, texObj
->BaseLevel
);
511 lastLevel
= MIN2(lastLevel
, texObj
->BaseLevel
+ baseImage
->image
.MaxLog2
);
512 lastLevel
= MIN2(lastLevel
, texObj
->MaxLevel
);
513 lastLevel
= MAX2(firstLevel
, lastLevel
); /* need at least one level */
516 numLevels
= lastLevel
- firstLevel
+ 1;
518 /* The hardware supports only 10 mipmap levels; ignore higher levels.
520 if ((numLevels
> 10) && (ctx
->Const
.MaxTextureLevels
> 10)) {
521 lastLevel
-= numLevels
- 10;
525 /* save these values, check if they effect the residency of the
528 if (viaObj
->firstLevel
!= firstLevel
||
529 viaObj
->lastLevel
!= lastLevel
) {
530 viaObj
->firstLevel
= firstLevel
;
531 viaObj
->lastLevel
= lastLevel
;
532 viaObj
->memType
= VIA_MEM_MIXED
;
535 if (VIA_DEBUG
& DEBUG_TEXTURE
& 0)
536 fprintf(stderr
, "%s, current memType: %s\n",
538 get_memtype_name(viaObj
->memType
));
541 if (viaObj
->memType
== VIA_MEM_MIXED
||
542 viaObj
->memType
== VIA_MEM_SYSTEM
) {
543 if (!viaSwapInTexObject(vmesa
, viaObj
)) {
544 if (VIA_DEBUG
& DEBUG_TEXTURE
)
545 if (!vmesa
->thrashing
)
546 fprintf(stderr
, "Thrashing flag set for frame %d\n",
548 vmesa
->thrashing
= GL_TRUE
;
553 if (viaObj
->memType
== VIA_MEM_AGP
)
554 viaObj
->regTexFM
= (HC_SubA_HTXnFM
<< 24) | HC_HTXnLoc_AGP
| texFormat
;
556 viaObj
->regTexFM
= (HC_SubA_HTXnFM
<< 24) | HC_HTXnLoc_Local
| texFormat
;
559 for (i
= 0; i
< numLevels
; i
++) {
560 struct via_texture_image
*viaImage
=
561 (struct via_texture_image
*)texObj
->Image
[0][firstLevel
+ i
];
563 w
= viaImage
->image
.WidthLog2
;
564 h
= viaImage
->image
.HeightLog2
;
565 p
= viaImage
->pitchLog2
;
567 assert(viaImage
->texMem
->memType
== viaObj
->memType
);
569 texBase
= viaImage
->texMem
->texBase
;
571 if (VIA_DEBUG
& DEBUG_TEXTURE
)
572 fprintf(stderr
, "%s: no texBase[%d]\n", __FUNCTION__
, i
);
576 /* Image has to remain resident until the coming fence is retired.
578 move_to_head( &vmesa
->tex_image_list
[viaImage
->texMem
->memType
],
580 viaImage
->texMem
->lastUsed
= vmesa
->lastBreadcrumbWrite
;
583 viaObj
->regTexBaseAndPitch
[i
].baseL
=
584 ((HC_SubA_HTXnL0BasL
+ i
) << 24) | (texBase
& 0xFFFFFF);
586 viaObj
->regTexBaseAndPitch
[i
].pitchLog2
=
587 ((HC_SubA_HTXnL0Pit
+ i
) << 24) | (p
<< 20);
590 /* The base high bytes for each 3 levels are packed
591 * together into one register:
595 basH
|= ((texBase
& 0xFF000000) >> (k
<< 3));
597 viaObj
->regTexBaseH
[j
] = ((j
+ HC_SubA_HTXnL012BasH
) << 24) | basH
;
601 /* Likewise, sets of 6 log2width and log2height values are
602 * packed into individual registers:
606 widthExp
|= (((GLuint
)w
& 0xF) << (m
<< 2));
607 heightExp
|= (((GLuint
)h
& 0xF) << (m
<< 2));
609 viaObj
->regTexWidthLog2
[l
] =
610 (l
+ HC_SubA_HTXnL0_5WE
) << 24 | widthExp
;
611 viaObj
->regTexHeightLog2
[l
] =
612 (l
+ HC_SubA_HTXnL0_5HE
) << 24 | heightExp
;
622 viaObj
->regTexBaseH
[j
] = ((j
+ HC_SubA_HTXnL012BasH
) << 24) | basH
;
625 viaObj
->regTexWidthLog2
[l
] = (l
+ HC_SubA_HTXnL0_5WE
) << 24 | widthExp
;
626 viaObj
->regTexHeightLog2
[l
] = (l
+ HC_SubA_HTXnL0_5HE
) << 24 | heightExp
;
633 GLboolean
viaUpdateTextureState( GLcontext
*ctx
)
635 struct gl_texture_unit
*texUnit
= ctx
->Texture
.Unit
;
638 for (i
= 0; i
< 2; i
++) {
639 if (texUnit
[i
]._ReallyEnabled
== TEXTURE_2D_BIT
||
640 texUnit
[i
]._ReallyEnabled
== TEXTURE_1D_BIT
) {
642 if (!viaSetTexImages(ctx
, texUnit
[i
]._Current
))
645 else if (texUnit
[i
]._ReallyEnabled
) {
660 static void viaTexImage(GLcontext
*ctx
,
662 GLenum target
, GLint level
,
663 GLint internalFormat
,
664 GLint width
, GLint height
, GLint border
,
665 GLenum format
, GLenum type
, const void *pixels
,
666 const struct gl_pixelstore_attrib
*packing
,
667 struct gl_texture_object
*texObj
,
668 struct gl_texture_image
*texImage
)
670 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
671 GLint postConvWidth
= width
;
672 GLint postConvHeight
= height
;
673 GLint texelBytes
, sizeInBytes
;
674 struct via_texture_object
*viaObj
= (struct via_texture_object
*)texObj
;
675 struct via_texture_image
*viaImage
= (struct via_texture_image
*)texImage
;
676 int heaps
[3], nheaps
, i
;
677 GLuint compressedSize
;
679 if (!is_empty_list(&vmesa
->freed_tex_buffers
)) {
680 viaCheckBreadcrumb(vmesa
, 0);
681 via_release_pending_textures(vmesa
);
684 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
685 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
689 /* choose the texture format */
690 texImage
->TexFormat
= viaChooseTexFormat(ctx
, internalFormat
,
693 assert(texImage
->TexFormat
);
695 _mesa_set_fetch_functions(texImage
, dims
);
697 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
699 if (texelBytes
== 0) {
700 /* compressed format */
702 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
703 texImage
->Height
, texImage
->Depth
,
704 texImage
->TexFormat
);
707 /* Minimum pitch of 32 bytes */
708 if (postConvWidth
* texelBytes
< 32) {
709 postConvWidth
= 32 / texelBytes
;
710 texImage
->RowStride
= postConvWidth
;
713 assert(texImage
->RowStride
== postConvWidth
);
714 viaImage
->pitchLog2
= logbase2(postConvWidth
* texelBytes
);
716 /* allocate memory */
717 if (_mesa_is_format_compressed(texImage
->TexFormat
))
718 sizeInBytes
= compressedSize
;
720 sizeInBytes
= postConvWidth
* postConvHeight
* texelBytes
;
723 /* Attempt to allocate texture memory directly, otherwise use main
724 * memory and this texture will always be a fallback. FIXME!
726 * TODO: make room in agp if this fails.
727 * TODO: use fb ram for textures as well.
731 switch (viaObj
->memType
) {
732 case VIA_MEM_UNKNOWN
:
733 heaps
[0] = VIA_MEM_AGP
;
734 heaps
[1] = VIA_MEM_VIDEO
;
735 heaps
[2] = VIA_MEM_SYSTEM
;
740 heaps
[0] = viaObj
->memType
;
741 heaps
[1] = VIA_MEM_SYSTEM
;
747 heaps
[0] = VIA_MEM_SYSTEM
;
752 for (i
= 0; i
< nheaps
&& !viaImage
->texMem
; i
++) {
753 if (VIA_DEBUG
& DEBUG_TEXTURE
)
754 fprintf(stderr
, "try %s (obj %s)\n", get_memtype_name(heaps
[i
]),
755 get_memtype_name(viaObj
->memType
));
756 viaImage
->texMem
= via_alloc_texture(vmesa
, sizeInBytes
, heaps
[i
]);
759 if (!viaImage
->texMem
) {
760 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
764 if (VIA_DEBUG
& DEBUG_TEXTURE
)
765 fprintf(stderr
, "upload %d bytes to %s\n", sizeInBytes
,
766 get_memtype_name(viaImage
->texMem
->memType
));
768 viaImage
->texMem
->image
= viaImage
;
769 texImage
->Data
= viaImage
->texMem
->bufAddr
;
771 if (viaObj
->memType
== VIA_MEM_UNKNOWN
)
772 viaObj
->memType
= viaImage
->texMem
->memType
;
773 else if (viaObj
->memType
!= viaImage
->texMem
->memType
)
774 viaObj
->memType
= VIA_MEM_MIXED
;
776 if (VIA_DEBUG
& DEBUG_TEXTURE
)
777 fprintf(stderr
, "%s, obj %s, image : %s\n",
779 get_memtype_name(viaObj
->memType
),
780 get_memtype_name(viaImage
->texMem
->memType
));
782 vmesa
->clearTexCache
= 1;
784 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
786 pixels
, packing
, "glTexImage");
788 /* Note: we check for a NULL image pointer here, _after_ we allocated
789 * memory for the texture. That's what the GL spec calls for.
797 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
798 dstRowStride
= _mesa_compressed_row_stride(texImage
->TexFormat
, width
);
801 dstRowStride
= postConvWidth
* _mesa_get_format_bytes(texImage
->TexFormat
);
803 success
= _mesa_texstore(ctx
, dims
,
804 texImage
->_BaseFormat
,
807 0, 0, 0, /* dstX/Y/Zoffset */
809 texImage
->ImageOffsets
,
811 format
, type
, pixels
, packing
);
813 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
817 _mesa_unmap_teximage_pbo(ctx
, packing
);
820 static void viaTexImage2D(GLcontext
*ctx
,
821 GLenum target
, GLint level
,
822 GLint internalFormat
,
823 GLint width
, GLint height
, GLint border
,
824 GLenum format
, GLenum type
, const void *pixels
,
825 const struct gl_pixelstore_attrib
*packing
,
826 struct gl_texture_object
*texObj
,
827 struct gl_texture_image
*texImage
)
829 viaTexImage( ctx
, 2, target
, level
,
830 internalFormat
, width
, height
, border
,
831 format
, type
, pixels
,
832 packing
, texObj
, texImage
);
835 static void viaTexSubImage2D(GLcontext
*ctx
,
838 GLint xoffset
, GLint yoffset
,
839 GLsizei width
, GLsizei height
,
840 GLenum format
, GLenum type
,
841 const GLvoid
*pixels
,
842 const struct gl_pixelstore_attrib
*packing
,
843 struct gl_texture_object
*texObj
,
844 struct gl_texture_image
*texImage
)
846 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
848 viaWaitIdle(vmesa
, GL_TRUE
);
849 vmesa
->clearTexCache
= 1;
851 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
, width
,
852 height
, format
, type
, pixels
, packing
, texObj
,
856 static void viaTexImage1D(GLcontext
*ctx
,
857 GLenum target
, GLint level
,
858 GLint internalFormat
,
859 GLint width
, GLint border
,
860 GLenum format
, GLenum type
, const void *pixels
,
861 const struct gl_pixelstore_attrib
*packing
,
862 struct gl_texture_object
*texObj
,
863 struct gl_texture_image
*texImage
)
865 viaTexImage( ctx
, 1, target
, level
,
866 internalFormat
, width
, 1, border
,
867 format
, type
, pixels
,
868 packing
, texObj
, texImage
);
871 static void viaTexSubImage1D(GLcontext
*ctx
,
876 GLenum format
, GLenum type
,
877 const GLvoid
*pixels
,
878 const struct gl_pixelstore_attrib
*packing
,
879 struct gl_texture_object
*texObj
,
880 struct gl_texture_image
*texImage
)
882 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
884 viaWaitIdle(vmesa
, GL_TRUE
);
885 vmesa
->clearTexCache
= 1;
887 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
, width
,
888 format
, type
, pixels
, packing
, texObj
,
894 static GLboolean
viaIsTextureResident(GLcontext
*ctx
,
895 struct gl_texture_object
*texObj
)
897 struct via_texture_object
*viaObj
=
898 (struct via_texture_object
*)texObj
;
900 return (viaObj
->memType
== VIA_MEM_AGP
||
901 viaObj
->memType
== VIA_MEM_VIDEO
);
906 static struct gl_texture_image
*viaNewTextureImage( GLcontext
*ctx
)
909 return (struct gl_texture_image
*)CALLOC_STRUCT(via_texture_image
);
913 static struct gl_texture_object
*viaNewTextureObject( GLcontext
*ctx
,
917 struct via_texture_object
*obj
= CALLOC_STRUCT(via_texture_object
);
919 _mesa_initialize_texture_object(&obj
->obj
, name
, target
);
922 obj
->memType
= VIA_MEM_UNKNOWN
;
928 static void viaFreeTextureImageData( GLcontext
*ctx
,
929 struct gl_texture_image
*texImage
)
931 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
932 struct via_texture_image
*image
= (struct via_texture_image
*)texImage
;
935 via_free_texture(vmesa
, image
->texMem
);
936 image
->texMem
= NULL
;
939 texImage
->Data
= NULL
;
945 void viaInitTextureFuncs(struct dd_function_table
* functions
)
947 functions
->ChooseTextureFormat
= viaChooseTexFormat
;
948 functions
->TexImage1D
= viaTexImage1D
;
949 functions
->TexImage2D
= viaTexImage2D
;
950 functions
->TexSubImage1D
= viaTexSubImage1D
;
951 functions
->TexSubImage2D
= viaTexSubImage2D
;
953 functions
->NewTextureObject
= viaNewTextureObject
;
954 functions
->NewTextureImage
= viaNewTextureImage
;
955 functions
->DeleteTexture
= _mesa_delete_texture_object
;
956 functions
->FreeTexImageData
= viaFreeTextureImageData
;
958 #if 0 && defined( USE_SSE_ASM )
960 * XXX this code is disabled for now because the via_sse_memcpy()
961 * routine causes segfaults with flightgear.
962 * See Mesa3d-dev mail list messages from 7/15/2005 for details.
963 * Note that this function is currently disabled in via_tris.c too.
965 if (getenv("VIA_NO_SSE"))
966 functions
->TextureMemCpy
= _mesa_memcpy
;
968 functions
->TextureMemCpy
= via_sse_memcpy
;
970 functions
->TextureMemCpy
= _mesa_memcpy
;
973 functions
->UpdateTexturePalette
= 0;
974 functions
->IsTextureResident
= viaIsTextureResident
;