mesa: replace gl_texture_format with gl_format
[mesa.git] / src / mesa / drivers / dri / unichrome / via_tex.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
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:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
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.
23 */
24
25
26 #include <stdlib.h>
27 #include <stdio.h>
28
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"
43
44 #include "main/mm.h"
45 #include "via_context.h"
46 #include "via_fb.h"
47 #include "via_tex.h"
48 #include "via_state.h"
49 #include "via_ioctl.h"
50 #include "via_3d_reg.h"
51
52 static gl_format
53 viaChooseTexFormat( GLcontext *ctx, GLint internalFormat,
54 GLenum format, GLenum type )
55 {
56 struct via_context *vmesa = VIA_CONTEXT(ctx);
57 const GLboolean do32bpt = ( vmesa->viaScreen->bitsPerPixel == 32
58 /* && vmesa->viaScreen->textureSize > 4*1024*1024 */
59 );
60
61
62 switch ( internalFormat ) {
63 case 4:
64 case GL_RGBA:
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;
70 }
71 else if ( type == GL_UNSIGNED_SHORT_4_4_4_4_REV ) {
72 return MESA_FORMAT_ARGB4444;
73 }
74 else if ( type == GL_UNSIGNED_SHORT_1_5_5_5_REV ) {
75 return MESA_FORMAT_ARGB1555;
76 }
77 }
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;
82 }
83 return do32bpt ? MESA_FORMAT_ARGB8888 : MESA_FORMAT_ARGB4444;
84
85 case 3:
86 case GL_RGB:
87 case GL_COMPRESSED_RGB:
88 if ( format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5 ) {
89 return MESA_FORMAT_RGB565;
90 }
91 else if ( type == GL_UNSIGNED_BYTE ) {
92 return MESA_FORMAT_ARGB8888;
93 }
94 return do32bpt ? MESA_FORMAT_ARGB8888 : MESA_FORMAT_RGB565;
95
96 case GL_RGBA8:
97 case GL_RGB10_A2:
98 case GL_RGBA12:
99 case GL_RGBA16:
100 return MESA_FORMAT_ARGB8888;
101
102 case GL_RGBA4:
103 case GL_RGBA2:
104 return MESA_FORMAT_ARGB4444;
105
106 case GL_RGB5_A1:
107 return MESA_FORMAT_ARGB1555;
108
109 case GL_RGB8:
110 case GL_RGB10:
111 case GL_RGB12:
112 case GL_RGB16:
113 return MESA_FORMAT_ARGB8888;
114
115 case GL_RGB5:
116 case GL_RGB4:
117 case GL_R3_G3_B2:
118 return MESA_FORMAT_RGB565;
119
120 case GL_ALPHA:
121 case GL_ALPHA4:
122 case GL_ALPHA8:
123 case GL_ALPHA12:
124 case GL_ALPHA16:
125 case GL_COMPRESSED_ALPHA:
126 return MESA_FORMAT_A8;
127
128 case 1:
129 case GL_LUMINANCE:
130 case GL_LUMINANCE4:
131 case GL_LUMINANCE8:
132 case GL_LUMINANCE12:
133 case GL_LUMINANCE16:
134 case GL_COMPRESSED_LUMINANCE:
135 return MESA_FORMAT_L8;
136
137 case 2:
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;
147
148 case GL_INTENSITY:
149 case GL_INTENSITY4:
150 case GL_INTENSITY8:
151 case GL_INTENSITY12:
152 case GL_INTENSITY16:
153 case GL_COMPRESSED_INTENSITY:
154 return MESA_FORMAT_I8;
155
156 case GL_YCBCR_MESA:
157 if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
158 type == GL_UNSIGNED_BYTE)
159 return MESA_FORMAT_YCBCR;
160 else
161 return MESA_FORMAT_YCBCR_REV;
162
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;
167
168 case GL_RGB_S3TC:
169 case GL_RGB4_S3TC:
170 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
171 return MESA_FORMAT_RGB_DXT1;
172
173 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
174 return MESA_FORMAT_RGBA_DXT1;
175
176 case GL_RGBA_S3TC:
177 case GL_RGBA4_S3TC:
178 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
179 return MESA_FORMAT_RGBA_DXT3;
180
181 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
182 return MESA_FORMAT_RGBA_DXT5;
183
184 case GL_COLOR_INDEX:
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;
192
193 default:
194 fprintf(stderr, "unexpected texture format %s in %s\n",
195 _mesa_lookup_enum_by_nr(internalFormat),
196 __FUNCTION__);
197 return MESA_FORMAT_NONE;
198 }
199
200 return MESA_FORMAT_NONE; /* never get here */
201 }
202
203 static int logbase2(int n)
204 {
205 GLint i = 1;
206 GLint log2 = 0;
207
208 while (n > i) {
209 i *= 2;
210 log2++;
211 }
212
213 return log2;
214 }
215
216 static const char *get_memtype_name( GLint memType )
217 {
218 static const char *names[] = {
219 "VIA_MEM_VIDEO",
220 "VIA_MEM_AGP",
221 "VIA_MEM_SYSTEM",
222 "VIA_MEM_MIXED",
223 "VIA_MEM_UNKNOWN"
224 };
225
226 return names[memType];
227 }
228
229
230 static GLboolean viaMoveTexBuffers( struct via_context *vmesa,
231 struct via_tex_buffer **buffers,
232 GLuint nr,
233 GLint newMemType )
234 {
235 struct via_tex_buffer *newTexBuf[VIA_MAX_TEXLEVELS];
236 GLint i;
237
238 if (VIA_DEBUG & DEBUG_TEXTURE)
239 fprintf(stderr, "%s to %s\n",
240 __FUNCTION__,
241 get_memtype_name(newMemType));
242
243 memset(newTexBuf, 0, sizeof(newTexBuf));
244
245 /* First do all the allocations (or fail):
246 */
247 for (i = 0; i < nr; i++) {
248 if (buffers[i]->memType != newMemType) {
249
250 /* Don't allow uploads in a thrash state. Should try and
251 * catch this earlier.
252 */
253 if (vmesa->thrashing && newMemType != VIA_MEM_SYSTEM)
254 goto cleanup;
255
256 newTexBuf[i] = via_alloc_texture(vmesa,
257 buffers[i]->size,
258 newMemType);
259 if (!newTexBuf[i])
260 goto cleanup;
261 }
262 }
263
264
265 /* Now copy all the image data and free the old texture memory.
266 */
267 for (i = 0; i < nr; i++) {
268 if (newTexBuf[i]) {
269 memcpy(newTexBuf[i]->bufAddr,
270 buffers[i]->bufAddr,
271 buffers[i]->size);
272
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]);
277 }
278 }
279
280 if (VIA_DEBUG & DEBUG_TEXTURE)
281 fprintf(stderr, "%s - success\n", __FUNCTION__);
282
283 return GL_TRUE;
284
285 cleanup:
286 /* Release any allocations made prior to failure:
287 */
288 if (VIA_DEBUG & DEBUG_TEXTURE)
289 fprintf(stderr, "%s - failed\n", __FUNCTION__);
290
291 for (i = 0; i < nr; i++) {
292 if (newTexBuf[i]) {
293 via_free_texture(vmesa, newTexBuf[i]);
294 }
295 }
296
297 return GL_FALSE;
298 }
299
300
301 static GLboolean viaMoveTexObject( struct via_context *vmesa,
302 struct via_texture_object *viaObj,
303 GLint newMemType )
304 {
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];
308 GLuint i, nr = 0;
309
310 for (i = viaObj->firstLevel; i <= viaObj->lastLevel; i++)
311 buffers[nr++] = viaImage[i]->texMem;
312
313 if (viaMoveTexBuffers( vmesa, &buffers[0], nr, newMemType )) {
314 viaObj->memType = newMemType;
315 return GL_TRUE;
316 }
317
318 return GL_FALSE;
319 }
320
321
322
323 static GLboolean viaSwapInTexObject( struct via_context *vmesa,
324 struct via_texture_object *viaObj )
325 {
326 const struct via_texture_image *baseImage =
327 (struct via_texture_image *)viaObj->obj.Image[0][viaObj->obj.BaseLevel];
328
329 if (VIA_DEBUG & DEBUG_TEXTURE)
330 fprintf(stderr, "%s\n", __FUNCTION__);
331
332 if (baseImage->texMem->memType != VIA_MEM_SYSTEM)
333 return viaMoveTexObject( vmesa, viaObj, baseImage->texMem->memType );
334
335 return (viaMoveTexObject( vmesa, viaObj, VIA_MEM_AGP ) ||
336 viaMoveTexObject( vmesa, viaObj, VIA_MEM_VIDEO ));
337 }
338
339
340 /* This seems crude, but it asks a fairly pertinent question and gives
341 * an accurate answer:
342 */
343 static GLboolean viaIsTexMemLow( struct via_context *vmesa,
344 GLuint heap )
345 {
346 struct via_tex_buffer *buf = via_alloc_texture(vmesa, 512 * 1024, heap );
347 if (!buf)
348 return GL_TRUE;
349
350 via_free_texture(vmesa, buf);
351 return GL_FALSE;
352 }
353
354
355 /* Speculatively move texture images which haven't been used in a
356 * while back to system memory.
357 *
358 * TODO: only do this when texture memory is low.
359 *
360 * TODO: use dma.
361 *
362 * TODO: keep the fb/agp version hanging around and use the local
363 * version as backing store, so re-upload might be avoided.
364 *
365 * TODO: do this properly in the kernel...
366 */
367 GLboolean viaSwapOutWork( struct via_context *vmesa )
368 {
369 struct via_tex_buffer *s, *tmp;
370 GLuint done = 0;
371 GLuint heap, target;
372
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]);
378
379
380 for (heap = VIA_MEM_VIDEO; heap <= VIA_MEM_AGP; heap++) {
381 GLuint nr = 0, sz = 0;
382
383 if (vmesa->thrashing) {
384 if (VIA_DEBUG & DEBUG_TEXTURE)
385 fprintf(stderr, "Heap %d: trash flag\n", heap);
386 target = 1*1024*1024;
387 }
388 else if (viaIsTexMemLow(vmesa, heap)) {
389 if (VIA_DEBUG & DEBUG_TEXTURE)
390 fprintf(stderr, "Heap %d: low memory\n", heap);
391 target = 64*1024;
392 }
393 else {
394 if (VIA_DEBUG & DEBUG_TEXTURE)
395 fprintf(stderr, "Heap %d: nothing to do\n", heap);
396 continue;
397 }
398
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;
403
404 if (VIA_DEBUG & DEBUG_TEXTURE)
405 fprintf(stderr,
406 "back copy tex sz %d, lastUsed %d lastSwap %d\n",
407 s->size, s->lastUsed, vmesa->lastSwap[1]);
408
409 if (viaMoveTexBuffers( vmesa, &s, 1, VIA_MEM_SYSTEM )) {
410 viaObj->memType = VIA_MEM_MIXED;
411 done += s->size;
412 }
413 else {
414 if (VIA_DEBUG & DEBUG_TEXTURE)
415 fprintf(stderr, "Failed to back copy texture!\n");
416 sz += s->size;
417 }
418 }
419 else {
420 nr ++;
421 sz += s->size;
422 }
423
424 if (done > target) {
425 vmesa->thrashing = GL_FALSE; /* might not get set otherwise? */
426 return GL_TRUE;
427 }
428 }
429
430 assert(sz == vmesa->total_alloc[heap]);
431
432 if (VIA_DEBUG & DEBUG_TEXTURE)
433 fprintf(stderr, "Heap %d: nr %d tot sz %d\n", heap, nr, sz);
434 }
435
436
437 return done != 0;
438 }
439
440
441
442 /* Basically, just collect the image dimensions and addresses for each
443 * image and update the texture object state accordingly.
444 */
445 static GLboolean viaSetTexImages(GLcontext *ctx,
446 struct gl_texture_object *texObj)
447 {
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;
453 GLuint texFormat;
454 GLint w, h, p;
455 GLint i, j = 0, k = 0, l = 0, m = 0;
456 GLuint texBase;
457 GLuint basH = 0;
458 GLuint widthExp = 0;
459 GLuint heightExp = 0;
460
461 switch (baseImage->image.TexFormat) {
462 case MESA_FORMAT_ARGB8888:
463 texFormat = HC_HTXnFM_ARGB8888;
464 break;
465 case MESA_FORMAT_ARGB4444:
466 texFormat = HC_HTXnFM_ARGB4444;
467 break;
468 case MESA_FORMAT_RGB565:
469 texFormat = HC_HTXnFM_RGB565;
470 break;
471 case MESA_FORMAT_ARGB1555:
472 texFormat = HC_HTXnFM_ARGB1555;
473 break;
474 case MESA_FORMAT_RGB888:
475 texFormat = HC_HTXnFM_ARGB0888;
476 break;
477 case MESA_FORMAT_L8:
478 texFormat = HC_HTXnFM_L8;
479 break;
480 case MESA_FORMAT_I8:
481 texFormat = HC_HTXnFM_T8;
482 break;
483 case MESA_FORMAT_CI8:
484 texFormat = HC_HTXnFM_Index8;
485 break;
486 case MESA_FORMAT_AL88:
487 texFormat = HC_HTXnFM_AL88;
488 break;
489 case MESA_FORMAT_A8:
490 texFormat = HC_HTXnFM_A8;
491 break;
492 default:
493 _mesa_problem(vmesa->glCtx, "Bad texture format in viaSetTexImages");
494 return GL_FALSE;
495 }
496
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.
501 */
502 if (texObj->MinFilter == GL_LINEAR || texObj->MinFilter == GL_NEAREST) {
503 firstLevel = lastLevel = texObj->BaseLevel;
504 }
505 else {
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 */
513 }
514
515 numLevels = lastLevel - firstLevel + 1;
516
517 /* The hardware supports only 10 mipmap levels; ignore higher levels.
518 */
519 if ((numLevels > 10) && (ctx->Const.MaxTextureLevels > 10)) {
520 lastLevel -= numLevels - 10;
521 numLevels = 10;
522 }
523
524 /* save these values, check if they effect the residency of the
525 * texture:
526 */
527 if (viaObj->firstLevel != firstLevel ||
528 viaObj->lastLevel != lastLevel) {
529 viaObj->firstLevel = firstLevel;
530 viaObj->lastLevel = lastLevel;
531 viaObj->memType = VIA_MEM_MIXED;
532 }
533
534 if (VIA_DEBUG & DEBUG_TEXTURE & 0)
535 fprintf(stderr, "%s, current memType: %s\n",
536 __FUNCTION__,
537 get_memtype_name(viaObj->memType));
538
539
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",
546 vmesa->swap_count);
547 vmesa->thrashing = GL_TRUE;
548 return GL_FALSE;
549 }
550 }
551
552 if (viaObj->memType == VIA_MEM_AGP)
553 viaObj->regTexFM = (HC_SubA_HTXnFM << 24) | HC_HTXnLoc_AGP | texFormat;
554 else
555 viaObj->regTexFM = (HC_SubA_HTXnFM << 24) | HC_HTXnLoc_Local | texFormat;
556
557
558 for (i = 0; i < numLevels; i++) {
559 struct via_texture_image *viaImage =
560 (struct via_texture_image *)texObj->Image[0][firstLevel + i];
561
562 w = viaImage->image.WidthLog2;
563 h = viaImage->image.HeightLog2;
564 p = viaImage->pitchLog2;
565
566 assert(viaImage->texMem->memType == viaObj->memType);
567
568 texBase = viaImage->texMem->texBase;
569 if (!texBase) {
570 if (VIA_DEBUG & DEBUG_TEXTURE)
571 fprintf(stderr, "%s: no texBase[%d]\n", __FUNCTION__, i);
572 return GL_FALSE;
573 }
574
575 /* Image has to remain resident until the coming fence is retired.
576 */
577 move_to_head( &vmesa->tex_image_list[viaImage->texMem->memType],
578 viaImage->texMem );
579 viaImage->texMem->lastUsed = vmesa->lastBreadcrumbWrite;
580
581
582 viaObj->regTexBaseAndPitch[i].baseL =
583 ((HC_SubA_HTXnL0BasL + i) << 24) | (texBase & 0xFFFFFF);
584
585 viaObj->regTexBaseAndPitch[i].pitchLog2 =
586 ((HC_SubA_HTXnL0Pit + i) << 24) | (p << 20);
587
588
589 /* The base high bytes for each 3 levels are packed
590 * together into one register:
591 */
592 j = i / 3;
593 k = 3 - (i % 3);
594 basH |= ((texBase & 0xFF000000) >> (k << 3));
595 if (k == 1) {
596 viaObj->regTexBaseH[j] = ((j + HC_SubA_HTXnL012BasH) << 24) | basH;
597 basH = 0;
598 }
599
600 /* Likewise, sets of 6 log2width and log2height values are
601 * packed into individual registers:
602 */
603 l = i / 6;
604 m = i % 6;
605 widthExp |= (((GLuint)w & 0xF) << (m << 2));
606 heightExp |= (((GLuint)h & 0xF) << (m << 2));
607 if (m == 5) {
608 viaObj->regTexWidthLog2[l] =
609 (l + HC_SubA_HTXnL0_5WE) << 24 | widthExp;
610 viaObj->regTexHeightLog2[l] =
611 (l + HC_SubA_HTXnL0_5HE) << 24 | heightExp;
612 widthExp = 0;
613 heightExp = 0;
614 }
615 if (w) w--;
616 if (h) h--;
617 if (p) p--;
618 }
619
620 if (k != 1) {
621 viaObj->regTexBaseH[j] = ((j + HC_SubA_HTXnL012BasH) << 24) | basH;
622 }
623 if (m != 5) {
624 viaObj->regTexWidthLog2[l] = (l + HC_SubA_HTXnL0_5WE) << 24 | widthExp;
625 viaObj->regTexHeightLog2[l] = (l + HC_SubA_HTXnL0_5HE) << 24 | heightExp;
626 }
627
628 return GL_TRUE;
629 }
630
631
632 GLboolean viaUpdateTextureState( GLcontext *ctx )
633 {
634 struct gl_texture_unit *texUnit = ctx->Texture.Unit;
635 GLuint i;
636
637 for (i = 0; i < 2; i++) {
638 if (texUnit[i]._ReallyEnabled == TEXTURE_2D_BIT ||
639 texUnit[i]._ReallyEnabled == TEXTURE_1D_BIT) {
640
641 if (!viaSetTexImages(ctx, texUnit[i]._Current))
642 return GL_FALSE;
643 }
644 else if (texUnit[i]._ReallyEnabled) {
645 return GL_FALSE;
646 }
647 }
648
649 return GL_TRUE;
650 }
651
652
653
654
655
656
657
658
659 static void viaTexImage(GLcontext *ctx,
660 GLint dims,
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)
668 {
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
677 if (!is_empty_list(&vmesa->freed_tex_buffers)) {
678 viaCheckBreadcrumb(vmesa, 0);
679 via_release_pending_textures(vmesa);
680 }
681
682 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
683 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
684 &postConvHeight);
685 }
686
687 /* choose the texture format */
688 texImage->TexFormat = viaChooseTexFormat(ctx, internalFormat,
689 format, type);
690
691 assert(texImage->TexFormat);
692
693 _mesa_set_fetch_functions(texImage, dims);
694
695 texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
696
697 if (texelBytes == 0) {
698 /* compressed format */
699 texImage->IsCompressed = GL_TRUE;
700 texImage->CompressedSize =
701 ctx->Driver.CompressedTextureSize(ctx, texImage->Width,
702 texImage->Height, texImage->Depth,
703 texImage->TexFormat);
704 }
705
706 /* Minimum pitch of 32 bytes */
707 if (postConvWidth * texelBytes < 32) {
708 postConvWidth = 32 / texelBytes;
709 texImage->RowStride = postConvWidth;
710 }
711
712 assert(texImage->RowStride == postConvWidth);
713 viaImage->pitchLog2 = logbase2(postConvWidth * texelBytes);
714
715 /* allocate memory */
716 if (texImage->IsCompressed)
717 sizeInBytes = texImage->CompressedSize;
718 else
719 sizeInBytes = postConvWidth * postConvHeight * texelBytes;
720
721
722 /* Attempt to allocate texture memory directly, otherwise use main
723 * memory and this texture will always be a fallback. FIXME!
724 *
725 * TODO: make room in agp if this fails.
726 * TODO: use fb ram for textures as well.
727 */
728
729
730 switch (viaObj->memType) {
731 case VIA_MEM_UNKNOWN:
732 heaps[0] = VIA_MEM_AGP;
733 heaps[1] = VIA_MEM_VIDEO;
734 heaps[2] = VIA_MEM_SYSTEM;
735 nheaps = 3;
736 break;
737 case VIA_MEM_AGP:
738 case VIA_MEM_VIDEO:
739 heaps[0] = viaObj->memType;
740 heaps[1] = VIA_MEM_SYSTEM;
741 nheaps = 2;
742 break;
743 case VIA_MEM_MIXED:
744 case VIA_MEM_SYSTEM:
745 default:
746 heaps[0] = VIA_MEM_SYSTEM;
747 nheaps = 1;
748 break;
749 }
750
751 for (i = 0; i < nheaps && !viaImage->texMem; i++) {
752 if (VIA_DEBUG & DEBUG_TEXTURE)
753 fprintf(stderr, "try %s (obj %s)\n", get_memtype_name(heaps[i]),
754 get_memtype_name(viaObj->memType));
755 viaImage->texMem = via_alloc_texture(vmesa, sizeInBytes, heaps[i]);
756 }
757
758 if (!viaImage->texMem) {
759 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
760 return;
761 }
762
763 if (VIA_DEBUG & DEBUG_TEXTURE)
764 fprintf(stderr, "upload %d bytes to %s\n", sizeInBytes,
765 get_memtype_name(viaImage->texMem->memType));
766
767 viaImage->texMem->image = viaImage;
768 texImage->Data = viaImage->texMem->bufAddr;
769
770 if (viaObj->memType == VIA_MEM_UNKNOWN)
771 viaObj->memType = viaImage->texMem->memType;
772 else if (viaObj->memType != viaImage->texMem->memType)
773 viaObj->memType = VIA_MEM_MIXED;
774
775 if (VIA_DEBUG & DEBUG_TEXTURE)
776 fprintf(stderr, "%s, obj %s, image : %s\n",
777 __FUNCTION__,
778 get_memtype_name(viaObj->memType),
779 get_memtype_name(viaImage->texMem->memType));
780
781 vmesa->clearTexCache = 1;
782
783 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
784 format, type,
785 pixels, packing, "glTexImage");
786 if (!pixels) {
787 /* Note: we check for a NULL image pointer here, _after_ we allocated
788 * memory for the texture. That's what the GL spec calls for.
789 */
790 return;
791 }
792 else {
793 GLint dstRowStride;
794 GLboolean success;
795
796 if (texImage->IsCompressed) {
797 dstRowStride = _mesa_compressed_row_stride(texImage->TexFormat, width);
798 }
799 else {
800 dstRowStride = postConvWidth * _mesa_get_format_bytes(texImage->TexFormat);
801 }
802 success = _mesa_texstore(ctx, dims,
803 texImage->_BaseFormat,
804 texImage->TexFormat,
805 texImage->Data,
806 0, 0, 0, /* dstX/Y/Zoffset */
807 dstRowStride,
808 texImage->ImageOffsets,
809 width, height, 1,
810 format, type, pixels, packing);
811 if (!success) {
812 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
813 }
814 }
815
816 _mesa_unmap_teximage_pbo(ctx, packing);
817 }
818
819 static void viaTexImage2D(GLcontext *ctx,
820 GLenum target, GLint level,
821 GLint internalFormat,
822 GLint width, GLint height, GLint border,
823 GLenum format, GLenum type, const void *pixels,
824 const struct gl_pixelstore_attrib *packing,
825 struct gl_texture_object *texObj,
826 struct gl_texture_image *texImage)
827 {
828 viaTexImage( ctx, 2, target, level,
829 internalFormat, width, height, border,
830 format, type, pixels,
831 packing, texObj, texImage );
832 }
833
834 static void viaTexSubImage2D(GLcontext *ctx,
835 GLenum target,
836 GLint level,
837 GLint xoffset, GLint yoffset,
838 GLsizei width, GLsizei height,
839 GLenum format, GLenum type,
840 const GLvoid *pixels,
841 const struct gl_pixelstore_attrib *packing,
842 struct gl_texture_object *texObj,
843 struct gl_texture_image *texImage)
844 {
845 struct via_context *vmesa = VIA_CONTEXT(ctx);
846
847 viaWaitIdle(vmesa, GL_TRUE);
848 vmesa->clearTexCache = 1;
849
850 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset, width,
851 height, format, type, pixels, packing, texObj,
852 texImage);
853 }
854
855 static void viaTexImage1D(GLcontext *ctx,
856 GLenum target, GLint level,
857 GLint internalFormat,
858 GLint width, GLint border,
859 GLenum format, GLenum type, const void *pixels,
860 const struct gl_pixelstore_attrib *packing,
861 struct gl_texture_object *texObj,
862 struct gl_texture_image *texImage)
863 {
864 viaTexImage( ctx, 1, target, level,
865 internalFormat, width, 1, border,
866 format, type, pixels,
867 packing, texObj, texImage );
868 }
869
870 static void viaTexSubImage1D(GLcontext *ctx,
871 GLenum target,
872 GLint level,
873 GLint xoffset,
874 GLsizei width,
875 GLenum format, GLenum type,
876 const GLvoid *pixels,
877 const struct gl_pixelstore_attrib *packing,
878 struct gl_texture_object *texObj,
879 struct gl_texture_image *texImage)
880 {
881 struct via_context *vmesa = VIA_CONTEXT(ctx);
882
883 viaWaitIdle(vmesa, GL_TRUE);
884 vmesa->clearTexCache = 1;
885
886 _mesa_store_texsubimage1d(ctx, target, level, xoffset, width,
887 format, type, pixels, packing, texObj,
888 texImage);
889 }
890
891
892
893 static GLboolean viaIsTextureResident(GLcontext *ctx,
894 struct gl_texture_object *texObj)
895 {
896 struct via_texture_object *viaObj =
897 (struct via_texture_object *)texObj;
898
899 return (viaObj->memType == VIA_MEM_AGP ||
900 viaObj->memType == VIA_MEM_VIDEO);
901 }
902
903
904
905 static struct gl_texture_image *viaNewTextureImage( GLcontext *ctx )
906 {
907 (void) ctx;
908 return (struct gl_texture_image *)CALLOC_STRUCT(via_texture_image);
909 }
910
911
912 static struct gl_texture_object *viaNewTextureObject( GLcontext *ctx,
913 GLuint name,
914 GLenum target )
915 {
916 struct via_texture_object *obj = CALLOC_STRUCT(via_texture_object);
917
918 _mesa_initialize_texture_object(&obj->obj, name, target);
919 (void) ctx;
920
921 obj->memType = VIA_MEM_UNKNOWN;
922
923 return &obj->obj;
924 }
925
926
927 static void viaFreeTextureImageData( GLcontext *ctx,
928 struct gl_texture_image *texImage )
929 {
930 struct via_context *vmesa = VIA_CONTEXT(ctx);
931 struct via_texture_image *image = (struct via_texture_image *)texImage;
932
933 if (image->texMem) {
934 via_free_texture(vmesa, image->texMem);
935 image->texMem = NULL;
936 }
937
938 texImage->Data = NULL;
939 }
940
941
942
943
944 void viaInitTextureFuncs(struct dd_function_table * functions)
945 {
946 functions->ChooseTextureFormat = viaChooseTexFormat;
947 functions->TexImage1D = viaTexImage1D;
948 functions->TexImage2D = viaTexImage2D;
949 functions->TexSubImage1D = viaTexSubImage1D;
950 functions->TexSubImage2D = viaTexSubImage2D;
951
952 functions->NewTextureObject = viaNewTextureObject;
953 functions->NewTextureImage = viaNewTextureImage;
954 functions->DeleteTexture = _mesa_delete_texture_object;
955 functions->FreeTexImageData = viaFreeTextureImageData;
956
957 #if 0 && defined( USE_SSE_ASM )
958 /*
959 * XXX this code is disabled for now because the via_sse_memcpy()
960 * routine causes segfaults with flightgear.
961 * See Mesa3d-dev mail list messages from 7/15/2005 for details.
962 * Note that this function is currently disabled in via_tris.c too.
963 */
964 if (getenv("VIA_NO_SSE"))
965 functions->TextureMemCpy = _mesa_memcpy;
966 else
967 functions->TextureMemCpy = via_sse_memcpy;
968 #else
969 functions->TextureMemCpy = _mesa_memcpy;
970 #endif
971
972 functions->UpdateTexturePalette = 0;
973 functions->IsTextureResident = viaIsTextureResident;
974 }
975
976