2 * Copyright (C) 2008 Nicolai Haehnle.
3 * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
5 * The Weather Channel (TM) funded Tungsten Graphics to develop the
6 * initial release of the Radeon 8500 driver under the XFree86 license.
7 * This notice must be preserved.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial
19 * portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
25 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include "main/glheader.h"
32 #include "main/imports.h"
33 #include "main/context.h"
34 #include "main/convolve.h"
35 #include "main/mipmap.h"
36 #include "main/texcompress.h"
37 #include "main/texformat.h"
38 #include "main/texstore.h"
39 #include "main/teximage.h"
40 #include "main/texobj.h"
41 #include "main/texgetimage.h"
43 #include "xmlpool.h" /* for symbolic values of enum-type options */
45 #include "radeon_common.h"
47 #include "radeon_mipmap_tree.h"
50 static void copy_rows(void* dst, GLuint dststride, const void* src, GLuint srcstride,
51 GLuint numrows, GLuint rowsize)
53 assert(rowsize <= dststride);
54 assert(rowsize <= srcstride);
56 if (rowsize == srcstride && rowsize == dststride) {
57 memcpy(dst, src, numrows*rowsize);
60 for(i = 0; i < numrows; ++i) {
61 memcpy(dst, src, rowsize);
70 * Allocate an empty texture image object.
72 struct gl_texture_image *radeonNewTextureImage(GLcontext *ctx)
74 return CALLOC(sizeof(radeon_texture_image));
78 * Free memory associated with this texture image.
80 void radeonFreeTexImageData(GLcontext *ctx, struct gl_texture_image *timage)
82 radeon_texture_image* image = get_radeon_texture_image(timage);
85 radeon_miptree_unreference(image->mt);
87 assert(!image->base.Data);
89 _mesa_free_texture_image_data(ctx, timage);
92 radeon_bo_unref(image->bo);
96 _mesa_free_texmemory(timage->Data);
101 /* Set Data pointer and additional data for mapped texture image */
102 static void teximage_set_map_data(radeon_texture_image *image)
104 radeon_mipmap_level *lvl = &image->mt->levels[image->mtlevel];
106 image->base.Data = image->mt->bo->ptr + lvl->faces[image->mtface].offset;
107 image->base.RowStride = lvl->rowstride / image->mt->bpp;
112 * Map a single texture image for glTexImage and friends.
114 void radeon_teximage_map(radeon_texture_image *image, GLboolean write_enable)
117 assert(!image->base.Data);
119 radeon_bo_map(image->mt->bo, write_enable);
120 teximage_set_map_data(image);
125 void radeon_teximage_unmap(radeon_texture_image *image)
128 assert(image->base.Data);
130 image->base.Data = 0;
131 radeon_bo_unmap(image->mt->bo);
135 static void map_override(GLcontext *ctx, radeonTexObj *t)
137 radeon_texture_image *img = get_radeon_texture_image(t->base.Image[0][0]);
139 radeon_bo_map(t->bo, GL_FALSE);
141 img->base.Data = t->bo->ptr;
142 _mesa_set_fetch_functions(&img->base, 2);
145 static void unmap_override(GLcontext *ctx, radeonTexObj *t)
147 radeon_texture_image *img = get_radeon_texture_image(t->base.Image[0][0]);
149 radeon_bo_unmap(t->bo);
151 img->base.Data = NULL;
155 * Map a validated texture for reading during software rendering.
157 void radeonMapTexture(GLcontext *ctx, struct gl_texture_object *texObj)
159 radeonTexObj* t = radeon_tex_obj(texObj);
162 if (!radeon_validate_texture_miptree(ctx, texObj))
165 /* for r100 3D sw fallbacks don't have mt */
166 if (t->image_override && t->bo)
167 map_override(ctx, t);
172 radeon_bo_map(t->mt->bo, GL_FALSE);
173 for(face = 0; face < t->mt->faces; ++face) {
174 for(level = t->mt->firstLevel; level <= t->mt->lastLevel; ++level)
175 teximage_set_map_data(get_radeon_texture_image(texObj->Image[face][level]));
179 void radeonUnmapTexture(GLcontext *ctx, struct gl_texture_object *texObj)
181 radeonTexObj* t = radeon_tex_obj(texObj);
184 if (t->image_override && t->bo)
185 unmap_override(ctx, t);
186 /* for r100 3D sw fallbacks don't have mt */
190 for(face = 0; face < t->mt->faces; ++face) {
191 for(level = t->mt->firstLevel; level <= t->mt->lastLevel; ++level)
192 texObj->Image[face][level]->Data = 0;
194 radeon_bo_unmap(t->mt->bo);
197 GLuint radeon_face_for_target(GLenum target)
200 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
201 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
202 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
203 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
204 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
205 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
206 return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
213 * Wraps Mesa's implementation to ensure that the base level image is mapped.
215 * This relies on internal details of _mesa_generate_mipmap, in particular
216 * the fact that the memory for recreated texture images is always freed.
218 static void radeon_generate_mipmap(GLcontext *ctx, GLenum target,
219 struct gl_texture_object *texObj)
221 radeonTexObj* t = radeon_tex_obj(texObj);
222 GLuint nr_faces = (t->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
226 _mesa_generate_mipmap(ctx, target, texObj);
228 for (face = 0; face < nr_faces; face++) {
229 for (i = texObj->BaseLevel + 1; i < texObj->MaxLevel; i++) {
230 radeon_texture_image *image;
232 image = get_radeon_texture_image(texObj->Image[face][i]);
238 image->mtface = face;
240 radeon_miptree_unreference(image->mt);
247 void radeonGenerateMipmap(GLcontext* ctx, GLenum target, struct gl_texture_object *texObj)
249 GLuint face = radeon_face_for_target(target);
250 radeon_texture_image *baseimage = get_radeon_texture_image(texObj->Image[face][texObj->BaseLevel]);
252 radeon_teximage_map(baseimage, GL_FALSE);
253 radeon_generate_mipmap(ctx, target, texObj);
254 radeon_teximage_unmap(baseimage);
258 /* try to find a format which will only need a memcopy */
259 static const struct gl_texture_format *radeonChoose8888TexFormat(radeonContextPtr rmesa,
261 GLenum srcType, GLboolean fbo)
264 const GLubyte littleEndian = *((const GLubyte *)&ui);
266 /* r100 can only do this */
267 if (IS_R100_CLASS(rmesa->radeonScreen) || fbo)
268 return _dri_texformat_argb8888;
270 if ((srcFormat == GL_RGBA && srcType == GL_UNSIGNED_INT_8_8_8_8) ||
271 (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE && !littleEndian) ||
272 (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) ||
273 (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_BYTE && littleEndian)) {
274 return &_mesa_texformat_rgba8888;
275 } else if ((srcFormat == GL_RGBA && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) ||
276 (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE && littleEndian) ||
277 (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_INT_8_8_8_8) ||
278 (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_BYTE && !littleEndian)) {
279 return &_mesa_texformat_rgba8888_rev;
280 } else if (IS_R200_CLASS(rmesa->radeonScreen)) {
281 return _dri_texformat_argb8888;
282 } else if (srcFormat == GL_BGRA && ((srcType == GL_UNSIGNED_BYTE && !littleEndian) ||
283 srcType == GL_UNSIGNED_INT_8_8_8_8)) {
284 return &_mesa_texformat_argb8888_rev;
285 } else if (srcFormat == GL_BGRA && ((srcType == GL_UNSIGNED_BYTE && littleEndian) ||
286 srcType == GL_UNSIGNED_INT_8_8_8_8_REV)) {
287 return &_mesa_texformat_argb8888;
289 return _dri_texformat_argb8888;
292 const struct gl_texture_format *radeonChooseTextureFormat_mesa(GLcontext * ctx,
293 GLint internalFormat,
297 return radeonChooseTextureFormat(ctx, internalFormat, format,
301 const struct gl_texture_format *radeonChooseTextureFormat(GLcontext * ctx,
302 GLint internalFormat,
304 GLenum type, GLboolean fbo)
306 radeonContextPtr rmesa = RADEON_CONTEXT(ctx);
307 const GLboolean do32bpt =
308 (rmesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_32);
309 const GLboolean force16bpt =
310 (rmesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_FORCE_16);
314 fprintf(stderr, "InternalFormat=%s(%d) type=%s format=%s\n",
315 _mesa_lookup_enum_by_nr(internalFormat), internalFormat,
316 _mesa_lookup_enum_by_nr(type), _mesa_lookup_enum_by_nr(format));
317 fprintf(stderr, "do32bpt=%d force16bpt=%d\n", do32bpt, force16bpt);
320 switch (internalFormat) {
323 case GL_COMPRESSED_RGBA:
325 case GL_UNSIGNED_INT_10_10_10_2:
326 case GL_UNSIGNED_INT_2_10_10_10_REV:
327 return do32bpt ? _dri_texformat_argb8888 :
328 _dri_texformat_argb1555;
329 case GL_UNSIGNED_SHORT_4_4_4_4:
330 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
331 return _dri_texformat_argb4444;
332 case GL_UNSIGNED_SHORT_5_5_5_1:
333 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
334 return _dri_texformat_argb1555;
336 return do32bpt ? radeonChoose8888TexFormat(rmesa, format, type, fbo) :
337 _dri_texformat_argb4444;
342 case GL_COMPRESSED_RGB:
344 case GL_UNSIGNED_SHORT_4_4_4_4:
345 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
346 return _dri_texformat_argb4444;
347 case GL_UNSIGNED_SHORT_5_5_5_1:
348 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
349 return _dri_texformat_argb1555;
350 case GL_UNSIGNED_SHORT_5_6_5:
351 case GL_UNSIGNED_SHORT_5_6_5_REV:
352 return _dri_texformat_rgb565;
354 return do32bpt ? _dri_texformat_argb8888 :
355 _dri_texformat_rgb565;
363 radeonChoose8888TexFormat(rmesa, format, type, fbo) :
364 _dri_texformat_argb4444;
368 return _dri_texformat_argb4444;
371 return _dri_texformat_argb1555;
377 return !force16bpt ? _dri_texformat_argb8888 :
378 _dri_texformat_rgb565;
383 return _dri_texformat_rgb565;
390 case GL_COMPRESSED_ALPHA:
391 /* r200: can't use a8 format since interpreting hw I8 as a8 would result
392 in wrong rgb values (same as alpha value instead of 0). */
393 if (IS_R200_CLASS(rmesa->radeonScreen))
394 return _dri_texformat_al88;
396 return _dri_texformat_a8;
403 case GL_COMPRESSED_LUMINANCE:
404 return _dri_texformat_l8;
407 case GL_LUMINANCE_ALPHA:
408 case GL_LUMINANCE4_ALPHA4:
409 case GL_LUMINANCE6_ALPHA2:
410 case GL_LUMINANCE8_ALPHA8:
411 case GL_LUMINANCE12_ALPHA4:
412 case GL_LUMINANCE12_ALPHA12:
413 case GL_LUMINANCE16_ALPHA16:
414 case GL_COMPRESSED_LUMINANCE_ALPHA:
415 return _dri_texformat_al88;
422 case GL_COMPRESSED_INTENSITY:
423 return _dri_texformat_i8;
426 if (type == GL_UNSIGNED_SHORT_8_8_APPLE ||
427 type == GL_UNSIGNED_BYTE)
428 return &_mesa_texformat_ycbcr;
430 return &_mesa_texformat_ycbcr_rev;
434 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
435 return &_mesa_texformat_rgb_dxt1;
437 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
438 return &_mesa_texformat_rgba_dxt1;
442 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
443 return &_mesa_texformat_rgba_dxt3;
445 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
446 return &_mesa_texformat_rgba_dxt5;
448 case GL_ALPHA16F_ARB:
449 return &_mesa_texformat_alpha_float16;
450 case GL_ALPHA32F_ARB:
451 return &_mesa_texformat_alpha_float32;
452 case GL_LUMINANCE16F_ARB:
453 return &_mesa_texformat_luminance_float16;
454 case GL_LUMINANCE32F_ARB:
455 return &_mesa_texformat_luminance_float32;
456 case GL_LUMINANCE_ALPHA16F_ARB:
457 return &_mesa_texformat_luminance_alpha_float16;
458 case GL_LUMINANCE_ALPHA32F_ARB:
459 return &_mesa_texformat_luminance_alpha_float32;
460 case GL_INTENSITY16F_ARB:
461 return &_mesa_texformat_intensity_float16;
462 case GL_INTENSITY32F_ARB:
463 return &_mesa_texformat_intensity_float32;
465 return &_mesa_texformat_rgba_float16;
467 return &_mesa_texformat_rgba_float32;
469 return &_mesa_texformat_rgba_float16;
471 return &_mesa_texformat_rgba_float32;
473 case GL_DEPTH_COMPONENT:
474 case GL_DEPTH_COMPONENT16:
475 case GL_DEPTH_COMPONENT24:
476 case GL_DEPTH_COMPONENT32:
477 case GL_DEPTH_STENCIL_EXT:
478 case GL_DEPTH24_STENCIL8_EXT:
479 return &_mesa_texformat_s8_z24;
482 "unexpected internalFormat 0x%x in %s",
483 (int)internalFormat, __func__);
487 return NULL; /* never get here */
491 * All glTexImage calls go through this function.
493 static void radeon_teximage(
494 GLcontext *ctx, int dims,
495 GLint face, GLint level,
496 GLint internalFormat,
497 GLint width, GLint height, GLint depth,
499 GLenum format, GLenum type, const GLvoid * pixels,
500 const struct gl_pixelstore_attrib *packing,
501 struct gl_texture_object *texObj,
502 struct gl_texture_image *texImage,
505 radeonContextPtr rmesa = RADEON_CONTEXT(ctx);
506 radeonTexObj* t = radeon_tex_obj(texObj);
507 radeon_texture_image* image = get_radeon_texture_image(texImage);
509 GLint postConvWidth = width;
510 GLint postConvHeight = height;
513 radeon_firevertices(rmesa);
515 t->validated = GL_FALSE;
517 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
518 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
522 /* Choose and fill in the texture format for this image */
523 texImage->TexFormat = radeonChooseTextureFormat(ctx, internalFormat, format, type, 0);
524 _mesa_set_fetch_functions(texImage, dims);
526 if (texImage->TexFormat->TexelBytes == 0) {
528 texImage->IsCompressed = GL_TRUE;
529 texImage->CompressedSize =
530 ctx->Driver.CompressedTextureSize(ctx, texImage->Width,
531 texImage->Height, texImage->Depth,
532 texImage->TexFormat->MesaFormat);
534 texImage->IsCompressed = GL_FALSE;
535 texImage->CompressedSize = 0;
537 texelBytes = texImage->TexFormat->TexelBytes;
538 /* Minimum pitch of 32 bytes */
539 if (postConvWidth * texelBytes < 32) {
540 postConvWidth = 32 / texelBytes;
541 texImage->RowStride = postConvWidth;
544 assert(texImage->RowStride == postConvWidth);
548 /* Allocate memory for image */
549 radeonFreeTexImageData(ctx, texImage); /* Mesa core only clears texImage->Data but not image->mt */
552 t->mt->firstLevel == level &&
553 t->mt->lastLevel == level &&
554 t->mt->target != GL_TEXTURE_CUBE_MAP_ARB &&
555 !radeon_miptree_matches_image(t->mt, texImage, face, level)) {
556 radeon_miptree_unreference(t->mt);
561 radeon_try_alloc_miptree(rmesa, t, texImage, face, level);
562 if (t->mt && radeon_miptree_matches_image(t->mt, texImage, face, level)) {
563 radeon_mipmap_level *lvl;
565 image->mtlevel = level - t->mt->firstLevel;
566 image->mtface = face;
567 radeon_miptree_reference(t->mt);
568 lvl = &image->mt->levels[image->mtlevel];
569 dstRowStride = lvl->rowstride;
572 if (texImage->IsCompressed) {
573 size = texImage->CompressedSize;
575 size = texImage->Width * texImage->Height * texImage->Depth * texImage->TexFormat->TexelBytes;
577 texImage->Data = _mesa_alloc_texmemory(size);
580 /* Upload texture image; note that the spec allows pixels to be NULL */
582 pixels = _mesa_validate_pbo_compressed_teximage(
583 ctx, imageSize, pixels, packing, "glCompressedTexImage");
585 pixels = _mesa_validate_pbo_teximage(
586 ctx, dims, width, height, depth,
587 format, type, pixels, packing, "glTexImage");
591 radeon_teximage_map(image, GL_TRUE);
594 memcpy(texImage->Data, pixels, imageSize);
598 radeon_mipmap_level *lvl = &image->mt->levels[image->mtlevel];
599 dstRowStride = lvl->rowstride;
601 dstRowStride = texImage->Width * texImage->TexFormat->TexelBytes;
604 if (!texImage->TexFormat->StoreImage(ctx, dims,
605 texImage->_BaseFormat,
607 texImage->Data, 0, 0, 0, /* dstX/Y/Zoffset */
609 texImage->ImageOffsets,
610 width, height, depth,
611 format, type, pixels, packing))
612 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
617 /* SGIS_generate_mipmap */
618 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
619 radeon_generate_mipmap(ctx, texObj->Target, texObj);
622 _mesa_unmap_teximage_pbo(ctx, packing);
625 radeon_teximage_unmap(image);
630 void radeonTexImage1D(GLcontext * ctx, GLenum target, GLint level,
631 GLint internalFormat,
632 GLint width, GLint border,
633 GLenum format, GLenum type, const GLvoid * pixels,
634 const struct gl_pixelstore_attrib *packing,
635 struct gl_texture_object *texObj,
636 struct gl_texture_image *texImage)
638 radeon_teximage(ctx, 1, 0, level, internalFormat, width, 1, 1,
639 0, format, type, pixels, packing, texObj, texImage, 0);
642 void radeonTexImage2D(GLcontext * ctx, GLenum target, GLint level,
643 GLint internalFormat,
644 GLint width, GLint height, GLint border,
645 GLenum format, GLenum type, const GLvoid * pixels,
646 const struct gl_pixelstore_attrib *packing,
647 struct gl_texture_object *texObj,
648 struct gl_texture_image *texImage)
651 GLuint face = radeon_face_for_target(target);
653 radeon_teximage(ctx, 2, face, level, internalFormat, width, height, 1,
654 0, format, type, pixels, packing, texObj, texImage, 0);
657 void radeonCompressedTexImage2D(GLcontext * ctx, GLenum target,
658 GLint level, GLint internalFormat,
659 GLint width, GLint height, GLint border,
660 GLsizei imageSize, const GLvoid * data,
661 struct gl_texture_object *texObj,
662 struct gl_texture_image *texImage)
664 GLuint face = radeon_face_for_target(target);
666 radeon_teximage(ctx, 2, face, level, internalFormat, width, height, 1,
667 imageSize, 0, 0, data, &ctx->Unpack, texObj, texImage, 1);
670 void radeonTexImage3D(GLcontext * ctx, GLenum target, GLint level,
671 GLint internalFormat,
672 GLint width, GLint height, GLint depth,
674 GLenum format, GLenum type, const GLvoid * pixels,
675 const struct gl_pixelstore_attrib *packing,
676 struct gl_texture_object *texObj,
677 struct gl_texture_image *texImage)
679 radeon_teximage(ctx, 3, 0, level, internalFormat, width, height, depth,
680 0, format, type, pixels, packing, texObj, texImage, 0);
684 * Update a subregion of the given texture image.
686 static void radeon_texsubimage(GLcontext* ctx, int dims, int level,
687 GLint xoffset, GLint yoffset, GLint zoffset,
688 GLsizei width, GLsizei height, GLsizei depth,
690 GLenum format, GLenum type,
691 const GLvoid * pixels,
692 const struct gl_pixelstore_attrib *packing,
693 struct gl_texture_object *texObj,
694 struct gl_texture_image *texImage,
697 radeonContextPtr rmesa = RADEON_CONTEXT(ctx);
698 radeonTexObj* t = radeon_tex_obj(texObj);
699 radeon_texture_image* image = get_radeon_texture_image(texImage);
701 radeon_firevertices(rmesa);
703 t->validated = GL_FALSE;
705 pixels = _mesa_validate_pbo_compressed_teximage(
706 ctx, imageSize, pixels, packing, "glCompressedTexImage");
708 pixels = _mesa_validate_pbo_teximage(ctx, dims,
709 width, height, depth, format, type, pixels, packing, "glTexSubImage1D");
714 radeon_teximage_map(image, GL_TRUE);
717 radeon_mipmap_level *lvl = &image->mt->levels[image->mtlevel];
718 dstRowStride = lvl->rowstride;
720 dstRowStride = texImage->RowStride * texImage->TexFormat->TexelBytes;
724 uint32_t srcRowStride, bytesPerRow, rows;
725 dstRowStride = _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, texImage->Width);
726 srcRowStride = _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
727 bytesPerRow = srcRowStride;
730 copy_rows(texImage->Data, dstRowStride, image->base.Data, srcRowStride, rows,
734 if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat,
735 texImage->TexFormat, texImage->Data,
736 xoffset, yoffset, zoffset,
738 texImage->ImageOffsets,
739 width, height, depth,
740 format, type, pixels, packing))
741 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
746 /* GL_SGIS_generate_mipmap */
747 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
748 radeon_generate_mipmap(ctx, texObj->Target, texObj);
750 radeon_teximage_unmap(image);
752 _mesa_unmap_teximage_pbo(ctx, packing);
757 void radeonTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
760 GLenum format, GLenum type,
761 const GLvoid * pixels,
762 const struct gl_pixelstore_attrib *packing,
763 struct gl_texture_object *texObj,
764 struct gl_texture_image *texImage)
766 radeon_texsubimage(ctx, 1, level, xoffset, 0, 0, width, 1, 1, 0,
767 format, type, pixels, packing, texObj, texImage, 0);
770 void radeonTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
771 GLint xoffset, GLint yoffset,
772 GLsizei width, GLsizei height,
773 GLenum format, GLenum type,
774 const GLvoid * pixels,
775 const struct gl_pixelstore_attrib *packing,
776 struct gl_texture_object *texObj,
777 struct gl_texture_image *texImage)
779 radeon_texsubimage(ctx, 2, level, xoffset, yoffset, 0, width, height, 1,
780 0, format, type, pixels, packing, texObj, texImage,
784 void radeonCompressedTexSubImage2D(GLcontext * ctx, GLenum target,
785 GLint level, GLint xoffset,
786 GLint yoffset, GLsizei width,
787 GLsizei height, GLenum format,
788 GLsizei imageSize, const GLvoid * data,
789 struct gl_texture_object *texObj,
790 struct gl_texture_image *texImage)
792 radeon_texsubimage(ctx, 2, level, xoffset, yoffset, 0, width, height, 1,
793 imageSize, format, 0, data, &ctx->Unpack, texObj, texImage, 1);
797 void radeonTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
798 GLint xoffset, GLint yoffset, GLint zoffset,
799 GLsizei width, GLsizei height, GLsizei depth,
800 GLenum format, GLenum type,
801 const GLvoid * pixels,
802 const struct gl_pixelstore_attrib *packing,
803 struct gl_texture_object *texObj,
804 struct gl_texture_image *texImage)
806 radeon_texsubimage(ctx, 3, level, xoffset, yoffset, zoffset, width, height, depth, 0,
807 format, type, pixels, packing, texObj, texImage, 0);
813 * Ensure that the given image is stored in the given miptree from now on.
815 static void migrate_image_to_miptree(radeon_mipmap_tree *mt, radeon_texture_image *image, int face, int level)
817 radeon_mipmap_level *dstlvl = &mt->levels[level - mt->firstLevel];
820 assert(image->mt != mt);
821 assert(dstlvl->width == image->base.Width);
822 assert(dstlvl->height == image->base.Height);
823 assert(dstlvl->depth == image->base.Depth);
826 radeon_bo_map(mt->bo, GL_TRUE);
827 dest = mt->bo->ptr + dstlvl->faces[face].offset;
830 /* Format etc. should match, so we really just need a memcpy().
831 * In fact, that memcpy() could be done by the hardware in many
832 * cases, provided that we have a proper memory manager.
834 radeon_mipmap_level *srclvl = &image->mt->levels[image->mtlevel];
836 assert(srclvl->size == dstlvl->size);
837 assert(srclvl->rowstride == dstlvl->rowstride);
839 radeon_bo_map(image->mt->bo, GL_FALSE);
842 image->mt->bo->ptr + srclvl->faces[face].offset,
844 radeon_bo_unmap(image->mt->bo);
846 radeon_miptree_unreference(image->mt);
848 uint32_t srcrowstride;
850 /* need to confirm this value is correct */
851 if (mt->compressed) {
852 height = image->base.Height / 4;
853 srcrowstride = image->base.RowStride * mt->bpp;
855 height = image->base.Height * image->base.Depth;
856 srcrowstride = image->base.Width * image->base.TexFormat->TexelBytes;
860 // WARN_ONCE("%s: tiling not supported yet", __FUNCTION__);
862 copy_rows(dest, dstlvl->rowstride, image->base.Data, srcrowstride,
863 height, srcrowstride);
865 _mesa_free_texmemory(image->base.Data);
866 image->base.Data = 0;
869 radeon_bo_unmap(mt->bo);
872 image->mtface = face;
873 image->mtlevel = level;
874 radeon_miptree_reference(image->mt);
877 int radeon_validate_texture_miptree(GLcontext * ctx, struct gl_texture_object *texObj)
879 radeonContextPtr rmesa = RADEON_CONTEXT(ctx);
880 radeonTexObj *t = radeon_tex_obj(texObj);
881 radeon_texture_image *baseimage = get_radeon_texture_image(texObj->Image[0][texObj->BaseLevel]);
884 if (t->validated || t->image_override)
887 if (RADEON_DEBUG & DEBUG_TEXTURE)
888 fprintf(stderr, "%s: Validating texture %p now\n", __FUNCTION__, texObj);
890 if (baseimage->base.Border > 0)
893 /* Ensure a matching miptree exists.
895 * Differing mipmap trees can result when the app uses TexImage to
896 * change texture dimensions.
898 * Prefer to use base image's miptree if it
899 * exists, since that most likely contains more valid data (remember
900 * that the base level is usually significantly larger than the rest
901 * of the miptree, so cubemaps are the only possible exception).
904 baseimage->mt != t->mt &&
905 radeon_miptree_matches_texture(baseimage->mt, &t->base)) {
906 radeon_miptree_unreference(t->mt);
907 t->mt = baseimage->mt;
908 radeon_miptree_reference(t->mt);
909 } else if (t->mt && !radeon_miptree_matches_texture(t->mt, &t->base)) {
910 radeon_miptree_unreference(t->mt);
915 if (RADEON_DEBUG & DEBUG_TEXTURE)
916 fprintf(stderr, " Allocate new miptree\n");
917 radeon_try_alloc_miptree(rmesa, t, &baseimage->base, 0, texObj->BaseLevel);
919 _mesa_problem(ctx, "r300_validate_texture failed to alloc miptree");
924 /* Ensure all images are stored in the single main miptree */
925 for(face = 0; face < t->mt->faces; ++face) {
926 for(level = t->mt->firstLevel; level <= t->mt->lastLevel; ++level) {
927 radeon_texture_image *image = get_radeon_texture_image(texObj->Image[face][level]);
928 if (RADEON_DEBUG & DEBUG_TEXTURE)
929 fprintf(stderr, " face %i, level %i... %p vs %p ", face, level, t->mt, image->mt);
930 if (t->mt == image->mt) {
931 if (RADEON_DEBUG & DEBUG_TEXTURE)
932 fprintf(stderr, "OK\n");
936 if (RADEON_DEBUG & DEBUG_TEXTURE)
937 fprintf(stderr, "migrating\n");
938 migrate_image_to_miptree(t->mt, image, face, level);
947 * Need to map texture image into memory before copying image data,
951 radeon_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
952 GLenum format, GLenum type, GLvoid * pixels,
953 struct gl_texture_object *texObj,
954 struct gl_texture_image *texImage, int compressed)
956 radeon_texture_image *image = get_radeon_texture_image(texImage);
959 /* Map the texture image read-only */
960 radeon_teximage_map(image, GL_FALSE);
962 /* Image hasn't been uploaded to a miptree yet */
963 assert(image->base.Data);
967 _mesa_get_compressed_teximage(ctx, target, level, pixels,
970 _mesa_get_teximage(ctx, target, level, format, type, pixels,
975 radeon_teximage_unmap(image);
980 radeonGetTexImage(GLcontext * ctx, GLenum target, GLint level,
981 GLenum format, GLenum type, GLvoid * pixels,
982 struct gl_texture_object *texObj,
983 struct gl_texture_image *texImage)
985 radeon_get_tex_image(ctx, target, level, format, type, pixels,
986 texObj, texImage, 0);
990 radeonGetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
992 struct gl_texture_object *texObj,
993 struct gl_texture_image *texImage)
995 radeon_get_tex_image(ctx, target, level, 0, 0, pixels,
996 texObj, texImage, 1);