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/mipmap.h"
35 #include "main/texcompress.h"
36 #include "main/texformat.h"
37 #include "main/texstore.h"
38 #include "main/teximage.h"
39 #include "main/texobj.h"
41 #include "xmlpool.h" /* for symbolic values of enum-type options */
43 #include "radeon_common.h"
45 #include "radeon_mipmap_tree.h"
49 * Allocate an empty texture image object.
51 struct gl_texture_image
*radeonNewTextureImage(GLcontext
*ctx
)
53 return CALLOC(sizeof(radeon_texture_image
));
57 * Free memory associated with this texture image.
59 void radeonFreeTexImageData(GLcontext
*ctx
, struct gl_texture_image
*timage
)
61 radeon_texture_image
* image
= get_radeon_texture_image(timage
);
64 radeon_miptree_unreference(image
->mt
);
66 assert(!image
->base
.Data
);
68 _mesa_free_texture_image_data(ctx
, timage
);
71 radeon_bo_unref(image
->bo
);
75 _mesa_free_texmemory(timage
->Data
);
80 /* Set Data pointer and additional data for mapped texture image */
81 static void teximage_set_map_data(radeon_texture_image
*image
)
83 radeon_mipmap_level
*lvl
= &image
->mt
->levels
[image
->mtlevel
];
85 image
->base
.Data
= image
->mt
->bo
->ptr
+ lvl
->faces
[image
->mtface
].offset
;
86 image
->base
.RowStride
= lvl
->rowstride
/ image
->mt
->bpp
;
91 * Map a single texture image for glTexImage and friends.
93 void radeon_teximage_map(radeon_texture_image
*image
, GLboolean write_enable
)
96 assert(!image
->base
.Data
);
98 radeon_bo_map(image
->mt
->bo
, write_enable
);
99 teximage_set_map_data(image
);
104 void radeon_teximage_unmap(radeon_texture_image
*image
)
107 assert(image
->base
.Data
);
109 image
->base
.Data
= 0;
110 radeon_bo_unmap(image
->mt
->bo
);
115 * Map a validated texture for reading during software rendering.
117 void radeonMapTexture(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
119 radeonTexObj
* t
= radeon_tex_obj(texObj
);
122 /* for r100 3D sw fallbacks don't have mt */
126 radeon_bo_map(t
->mt
->bo
, GL_FALSE
);
127 for(face
= 0; face
< t
->mt
->faces
; ++face
) {
128 for(level
= t
->mt
->firstLevel
; level
<= t
->mt
->lastLevel
; ++level
)
129 teximage_set_map_data(get_radeon_texture_image(texObj
->Image
[face
][level
]));
133 void radeonUnmapTexture(GLcontext
*ctx
, struct gl_texture_object
*texObj
)
135 radeonTexObj
* t
= radeon_tex_obj(texObj
);
138 /* for r100 3D sw fallbacks don't have mt */
142 for(face
= 0; face
< t
->mt
->faces
; ++face
) {
143 for(level
= t
->mt
->firstLevel
; level
<= t
->mt
->lastLevel
; ++level
)
144 texObj
->Image
[face
][level
]->Data
= 0;
146 radeon_bo_unmap(t
->mt
->bo
);
149 GLuint
radeon_face_for_target(GLenum target
)
152 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
153 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
154 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
155 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
156 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
157 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
158 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
165 * Wraps Mesa's implementation to ensure that the base level image is mapped.
167 * This relies on internal details of _mesa_generate_mipmap, in particular
168 * the fact that the memory for recreated texture images is always freed.
170 static void radeon_generate_mipmap(GLcontext
*ctx
, GLenum target
,
171 struct gl_texture_object
*texObj
)
173 radeonTexObj
* t
= radeon_tex_obj(texObj
);
174 GLuint nr_faces
= (t
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
178 _mesa_generate_mipmap(ctx
, target
, texObj
);
180 for (face
= 0; face
< nr_faces
; face
++) {
181 for (i
= texObj
->BaseLevel
+ 1; i
< texObj
->MaxLevel
; i
++) {
182 radeon_texture_image
*image
;
184 image
= get_radeon_texture_image(texObj
->Image
[face
][i
]);
190 image
->mtface
= face
;
192 radeon_miptree_unreference(image
->mt
);
199 void radeonGenerateMipmap(GLcontext
* ctx
, GLenum target
, struct gl_texture_object
*texObj
)
201 GLuint face
= radeon_face_for_target(target
);
202 radeon_texture_image
*baseimage
= get_radeon_texture_image(texObj
->Image
[face
][texObj
->BaseLevel
]);
204 radeon_teximage_map(baseimage
, GL_FALSE
);
205 radeon_generate_mipmap(ctx
, target
, texObj
);
206 radeon_teximage_unmap(baseimage
);
210 /* try to find a format which will only need a memcopy */
211 static const struct gl_texture_format
*radeonChoose8888TexFormat(radeonContextPtr rmesa
,
216 const GLubyte littleEndian
= *((const GLubyte
*)&ui
);
218 /* r100 can only do this */
219 if (IS_R100_CLASS(rmesa
->radeonScreen
))
220 return _dri_texformat_argb8888
;
222 if ((srcFormat
== GL_RGBA
&& srcType
== GL_UNSIGNED_INT_8_8_8_8
) ||
223 (srcFormat
== GL_RGBA
&& srcType
== GL_UNSIGNED_BYTE
&& !littleEndian
) ||
224 (srcFormat
== GL_ABGR_EXT
&& srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
225 (srcFormat
== GL_ABGR_EXT
&& srcType
== GL_UNSIGNED_BYTE
&& littleEndian
)) {
226 return &_mesa_texformat_rgba8888
;
227 } else if ((srcFormat
== GL_RGBA
&& srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
) ||
228 (srcFormat
== GL_RGBA
&& srcType
== GL_UNSIGNED_BYTE
&& littleEndian
) ||
229 (srcFormat
== GL_ABGR_EXT
&& srcType
== GL_UNSIGNED_INT_8_8_8_8
) ||
230 (srcFormat
== GL_ABGR_EXT
&& srcType
== GL_UNSIGNED_BYTE
&& !littleEndian
)) {
231 return &_mesa_texformat_rgba8888_rev
;
232 } else if (srcFormat
== GL_BGRA
&& ((srcType
== GL_UNSIGNED_BYTE
&& !littleEndian
) ||
233 srcType
== GL_UNSIGNED_INT_8_8_8_8
)) {
234 return &_mesa_texformat_argb8888_rev
;
235 } else if (srcFormat
== GL_BGRA
&& ((srcType
== GL_UNSIGNED_BYTE
&& littleEndian
) ||
236 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
)) {
237 return &_mesa_texformat_argb8888
;
239 return _dri_texformat_argb8888
;
242 const struct gl_texture_format
*radeonChooseTextureFormat(GLcontext
* ctx
,
243 GLint internalFormat
,
247 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
248 const GLboolean do32bpt
=
249 (rmesa
->texture_depth
== DRI_CONF_TEXTURE_DEPTH_32
);
250 const GLboolean force16bpt
=
251 (rmesa
->texture_depth
== DRI_CONF_TEXTURE_DEPTH_FORCE_16
);
255 fprintf(stderr
, "InternalFormat=%s(%d) type=%s format=%s\n",
256 _mesa_lookup_enum_by_nr(internalFormat
), internalFormat
,
257 _mesa_lookup_enum_by_nr(type
), _mesa_lookup_enum_by_nr(format
));
258 fprintf(stderr
, "do32bpt=%d force16bpt=%d\n", do32bpt
, force16bpt
);
261 switch (internalFormat
) {
264 case GL_COMPRESSED_RGBA
:
266 case GL_UNSIGNED_INT_10_10_10_2
:
267 case GL_UNSIGNED_INT_2_10_10_10_REV
:
268 return do32bpt
? _dri_texformat_argb8888
:
269 _dri_texformat_argb1555
;
270 case GL_UNSIGNED_SHORT_4_4_4_4
:
271 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
272 return _dri_texformat_argb4444
;
273 case GL_UNSIGNED_SHORT_5_5_5_1
:
274 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
275 return _dri_texformat_argb1555
;
277 return do32bpt
? radeonChoose8888TexFormat(rmesa
, format
, type
) :
278 _dri_texformat_argb4444
;
283 case GL_COMPRESSED_RGB
:
285 case GL_UNSIGNED_SHORT_4_4_4_4
:
286 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
287 return _dri_texformat_argb4444
;
288 case GL_UNSIGNED_SHORT_5_5_5_1
:
289 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
290 return _dri_texformat_argb1555
;
291 case GL_UNSIGNED_SHORT_5_6_5
:
292 case GL_UNSIGNED_SHORT_5_6_5_REV
:
293 return _dri_texformat_rgb565
;
295 return do32bpt
? _dri_texformat_argb8888
:
296 _dri_texformat_rgb565
;
304 radeonChoose8888TexFormat(rmesa
, format
,type
) :
305 _dri_texformat_argb4444
;
309 return _dri_texformat_argb4444
;
312 return _dri_texformat_argb1555
;
318 return !force16bpt
? _dri_texformat_argb8888
:
319 _dri_texformat_rgb565
;
324 return _dri_texformat_rgb565
;
331 case GL_COMPRESSED_ALPHA
:
332 return _dri_texformat_a8
;
340 case GL_COMPRESSED_LUMINANCE
:
341 return _dri_texformat_l8
;
344 case GL_LUMINANCE_ALPHA
:
345 case GL_LUMINANCE4_ALPHA4
:
346 case GL_LUMINANCE6_ALPHA2
:
347 case GL_LUMINANCE8_ALPHA8
:
348 case GL_LUMINANCE12_ALPHA4
:
349 case GL_LUMINANCE12_ALPHA12
:
350 case GL_LUMINANCE16_ALPHA16
:
351 case GL_COMPRESSED_LUMINANCE_ALPHA
:
352 return _dri_texformat_al88
;
359 case GL_COMPRESSED_INTENSITY
:
360 return _dri_texformat_i8
;
363 if (type
== GL_UNSIGNED_SHORT_8_8_APPLE
||
364 type
== GL_UNSIGNED_BYTE
)
365 return &_mesa_texformat_ycbcr
;
367 return &_mesa_texformat_ycbcr_rev
;
371 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
372 return &_mesa_texformat_rgb_dxt1
;
374 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
375 return &_mesa_texformat_rgba_dxt1
;
379 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
380 return &_mesa_texformat_rgba_dxt3
;
382 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
383 return &_mesa_texformat_rgba_dxt5
;
385 case GL_ALPHA16F_ARB
:
386 return &_mesa_texformat_alpha_float16
;
387 case GL_ALPHA32F_ARB
:
388 return &_mesa_texformat_alpha_float32
;
389 case GL_LUMINANCE16F_ARB
:
390 return &_mesa_texformat_luminance_float16
;
391 case GL_LUMINANCE32F_ARB
:
392 return &_mesa_texformat_luminance_float32
;
393 case GL_LUMINANCE_ALPHA16F_ARB
:
394 return &_mesa_texformat_luminance_alpha_float16
;
395 case GL_LUMINANCE_ALPHA32F_ARB
:
396 return &_mesa_texformat_luminance_alpha_float32
;
397 case GL_INTENSITY16F_ARB
:
398 return &_mesa_texformat_intensity_float16
;
399 case GL_INTENSITY32F_ARB
:
400 return &_mesa_texformat_intensity_float32
;
402 return &_mesa_texformat_rgba_float16
;
404 return &_mesa_texformat_rgba_float32
;
406 return &_mesa_texformat_rgba_float16
;
408 return &_mesa_texformat_rgba_float32
;
410 case GL_DEPTH_COMPONENT
:
411 case GL_DEPTH_COMPONENT16
:
412 case GL_DEPTH_COMPONENT24
:
413 case GL_DEPTH_COMPONENT32
:
416 case GL_UNSIGNED_BYTE
:
417 case GL_UNSIGNED_SHORT
:
418 return &_mesa_texformat_z16
;
419 case GL_UNSIGNED_INT
:
420 return &_mesa_texformat_z32
;
421 case GL_UNSIGNED_INT_24_8_EXT
:
423 return &_mesa_texformat_z24_s8
;
426 return &_mesa_texformat_z16
;
431 "unexpected internalFormat 0x%x in r300ChooseTextureFormat",
432 (int)internalFormat
);
436 return NULL
; /* never get here */
440 * All glTexImage calls go through this function.
442 static void radeon_teximage(
443 GLcontext
*ctx
, int dims
,
444 GLint face
, GLint level
,
445 GLint internalFormat
,
446 GLint width
, GLint height
, GLint depth
,
448 GLenum format
, GLenum type
, const GLvoid
* pixels
,
449 const struct gl_pixelstore_attrib
*packing
,
450 struct gl_texture_object
*texObj
,
451 struct gl_texture_image
*texImage
,
454 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
455 radeonTexObj
* t
= radeon_tex_obj(texObj
);
456 radeon_texture_image
* image
= get_radeon_texture_image(texImage
);
458 GLint postConvWidth
= width
;
459 GLint postConvHeight
= height
;
462 radeon_firevertices(rmesa
);
464 t
->validated
= GL_FALSE
;
466 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
467 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
471 /* Choose and fill in the texture format for this image */
472 texImage
->TexFormat
= radeonChooseTextureFormat(ctx
, internalFormat
, format
, type
);
473 _mesa_set_fetch_functions(texImage
, dims
);
475 if (texImage
->TexFormat
->TexelBytes
== 0) {
477 texImage
->IsCompressed
= GL_TRUE
;
478 texImage
->CompressedSize
=
479 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
480 texImage
->Height
, texImage
->Depth
,
481 texImage
->TexFormat
->MesaFormat
);
483 texImage
->IsCompressed
= GL_FALSE
;
484 texImage
->CompressedSize
= 0;
486 texelBytes
= texImage
->TexFormat
->TexelBytes
;
487 /* Minimum pitch of 32 bytes */
488 if (postConvWidth
* texelBytes
< 32) {
489 postConvWidth
= 32 / texelBytes
;
490 texImage
->RowStride
= postConvWidth
;
493 assert(texImage
->RowStride
== postConvWidth
);
497 /* Allocate memory for image */
498 radeonFreeTexImageData(ctx
, texImage
); /* Mesa core only clears texImage->Data but not image->mt */
501 radeon_try_alloc_miptree(rmesa
, t
, texImage
, face
, level
);
502 if (t
->mt
&& radeon_miptree_matches_image(t
->mt
, texImage
, face
, level
)) {
503 radeon_mipmap_level
*lvl
;
505 image
->mtlevel
= level
- t
->mt
->firstLevel
;
506 image
->mtface
= face
;
507 radeon_miptree_reference(t
->mt
);
508 lvl
= &image
->mt
->levels
[image
->mtlevel
];
509 dstRowStride
= lvl
->rowstride
;
512 if (texImage
->IsCompressed
) {
513 size
= texImage
->CompressedSize
;
515 size
= texImage
->Width
* texImage
->Height
* texImage
->Depth
* texImage
->TexFormat
->TexelBytes
;
517 texImage
->Data
= _mesa_alloc_texmemory(size
);
520 /* Upload texture image; note that the spec allows pixels to be NULL */
522 pixels
= _mesa_validate_pbo_compressed_teximage(
523 ctx
, imageSize
, pixels
, packing
, "glCompressedTexImage");
525 pixels
= _mesa_validate_pbo_teximage(
526 ctx
, dims
, width
, height
, depth
,
527 format
, type
, pixels
, packing
, "glTexImage");
531 radeon_teximage_map(image
, GL_TRUE
);
534 memcpy(texImage
->Data
, pixels
, imageSize
);
538 radeon_mipmap_level
*lvl
= &image
->mt
->levels
[image
->mtlevel
];
539 dstRowStride
= lvl
->rowstride
;
541 dstRowStride
= texImage
->Width
* texImage
->TexFormat
->TexelBytes
;
544 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
545 texImage
->_BaseFormat
,
547 texImage
->Data
, 0, 0, 0, /* dstX/Y/Zoffset */
549 texImage
->ImageOffsets
,
550 width
, height
, depth
,
551 format
, type
, pixels
, packing
))
552 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
557 /* SGIS_generate_mipmap */
558 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
559 radeon_generate_mipmap(ctx
, texObj
->Target
, texObj
);
562 _mesa_unmap_teximage_pbo(ctx
, packing
);
565 radeon_teximage_unmap(image
);
570 void radeonTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
571 GLint internalFormat
,
572 GLint width
, GLint border
,
573 GLenum format
, GLenum type
, const GLvoid
* pixels
,
574 const struct gl_pixelstore_attrib
*packing
,
575 struct gl_texture_object
*texObj
,
576 struct gl_texture_image
*texImage
)
578 radeon_teximage(ctx
, 1, 0, level
, internalFormat
, width
, 1, 1,
579 0, format
, type
, pixels
, packing
, texObj
, texImage
, 0);
582 void radeonTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
583 GLint internalFormat
,
584 GLint width
, GLint height
, GLint border
,
585 GLenum format
, GLenum type
, const GLvoid
* pixels
,
586 const struct gl_pixelstore_attrib
*packing
,
587 struct gl_texture_object
*texObj
,
588 struct gl_texture_image
*texImage
)
591 GLuint face
= radeon_face_for_target(target
);
593 radeon_teximage(ctx
, 2, face
, level
, internalFormat
, width
, height
, 1,
594 0, format
, type
, pixels
, packing
, texObj
, texImage
, 0);
597 void radeonCompressedTexImage2D(GLcontext
* ctx
, GLenum target
,
598 GLint level
, GLint internalFormat
,
599 GLint width
, GLint height
, GLint border
,
600 GLsizei imageSize
, const GLvoid
* data
,
601 struct gl_texture_object
*texObj
,
602 struct gl_texture_image
*texImage
)
604 GLuint face
= radeon_face_for_target(target
);
606 radeon_teximage(ctx
, 2, face
, level
, internalFormat
, width
, height
, 1,
607 imageSize
, 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, 1);
610 void radeonTexImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
611 GLint internalFormat
,
612 GLint width
, GLint height
, GLint depth
,
614 GLenum format
, GLenum type
, const GLvoid
* pixels
,
615 const struct gl_pixelstore_attrib
*packing
,
616 struct gl_texture_object
*texObj
,
617 struct gl_texture_image
*texImage
)
619 radeon_teximage(ctx
, 3, 0, level
, internalFormat
, width
, height
, depth
,
620 0, format
, type
, pixels
, packing
, texObj
, texImage
, 0);
624 * Update a subregion of the given texture image.
626 static void radeon_texsubimage(GLcontext
* ctx
, int dims
, int level
,
627 GLint xoffset
, GLint yoffset
, GLint zoffset
,
628 GLsizei width
, GLsizei height
, GLsizei depth
,
629 GLenum format
, GLenum type
,
630 const GLvoid
* pixels
,
631 const struct gl_pixelstore_attrib
*packing
,
632 struct gl_texture_object
*texObj
,
633 struct gl_texture_image
*texImage
,
636 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
637 radeonTexObj
* t
= radeon_tex_obj(texObj
);
638 radeon_texture_image
* image
= get_radeon_texture_image(texImage
);
640 radeon_firevertices(rmesa
);
642 t
->validated
= GL_FALSE
;
643 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
,
644 width
, height
, depth
, format
, type
, pixels
, packing
, "glTexSubImage1D");
648 radeon_teximage_map(image
, GL_TRUE
);
651 radeon_mipmap_level
*lvl
= &image
->mt
->levels
[image
->mtlevel
];
652 dstRowStride
= lvl
->rowstride
;
654 dstRowStride
= texImage
->RowStride
* texImage
->TexFormat
->TexelBytes
;
657 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
, texImage
->_BaseFormat
,
658 texImage
->TexFormat
, texImage
->Data
,
659 xoffset
, yoffset
, zoffset
,
661 texImage
->ImageOffsets
,
662 width
, height
, depth
,
663 format
, type
, pixels
, packing
))
664 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
669 /* GL_SGIS_generate_mipmap */
670 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
671 radeon_generate_mipmap(ctx
, texObj
->Target
, texObj
);
673 radeon_teximage_unmap(image
);
675 _mesa_unmap_teximage_pbo(ctx
, packing
);
680 void radeonTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
683 GLenum format
, GLenum type
,
684 const GLvoid
* pixels
,
685 const struct gl_pixelstore_attrib
*packing
,
686 struct gl_texture_object
*texObj
,
687 struct gl_texture_image
*texImage
)
689 radeon_texsubimage(ctx
, 1, level
, xoffset
, 0, 0, width
, 1, 1,
690 format
, type
, pixels
, packing
, texObj
, texImage
, 0);
693 void radeonTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
694 GLint xoffset
, GLint yoffset
,
695 GLsizei width
, GLsizei height
,
696 GLenum format
, GLenum type
,
697 const GLvoid
* pixels
,
698 const struct gl_pixelstore_attrib
*packing
,
699 struct gl_texture_object
*texObj
,
700 struct gl_texture_image
*texImage
)
702 radeon_texsubimage(ctx
, 2, level
, xoffset
, yoffset
, 0, width
, height
,
703 1, format
, type
, pixels
, packing
, texObj
, texImage
,
707 void radeonCompressedTexSubImage2D(GLcontext
* ctx
, GLenum target
,
708 GLint level
, GLint xoffset
,
709 GLint yoffset
, GLsizei width
,
710 GLsizei height
, GLenum format
,
711 GLsizei imageSize
, const GLvoid
* data
,
712 struct gl_texture_object
*texObj
,
713 struct gl_texture_image
*texImage
)
715 radeon_texsubimage(ctx
, 2, level
, xoffset
, yoffset
, 0, width
, height
, 1,
716 format
, 0, data
, 0, texObj
, texImage
, 1);
720 void radeonTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
721 GLint xoffset
, GLint yoffset
, GLint zoffset
,
722 GLsizei width
, GLsizei height
, GLsizei depth
,
723 GLenum format
, GLenum type
,
724 const GLvoid
* pixels
,
725 const struct gl_pixelstore_attrib
*packing
,
726 struct gl_texture_object
*texObj
,
727 struct gl_texture_image
*texImage
)
729 radeon_texsubimage(ctx
, 3, level
, xoffset
, yoffset
, zoffset
, width
, height
, depth
,
730 format
, type
, pixels
, packing
, texObj
, texImage
, 0);
733 static void copy_rows(void* dst
, GLuint dststride
, const void* src
, GLuint srcstride
,
734 GLuint numrows
, GLuint rowsize
)
736 assert(rowsize
<= dststride
);
737 assert(rowsize
<= srcstride
);
739 if (rowsize
== srcstride
&& rowsize
== dststride
) {
740 memcpy(dst
, src
, numrows
*rowsize
);
743 for(i
= 0; i
< numrows
; ++i
) {
744 memcpy(dst
, src
, rowsize
);
753 * Ensure that the given image is stored in the given miptree from now on.
755 static void migrate_image_to_miptree(radeon_mipmap_tree
*mt
, radeon_texture_image
*image
, int face
, int level
)
757 radeon_mipmap_level
*dstlvl
= &mt
->levels
[level
- mt
->firstLevel
];
760 assert(image
->mt
!= mt
);
761 assert(dstlvl
->width
== image
->base
.Width
);
762 assert(dstlvl
->height
== image
->base
.Height
);
763 assert(dstlvl
->depth
== image
->base
.Depth
);
766 radeon_bo_map(mt
->bo
, GL_TRUE
);
767 dest
= mt
->bo
->ptr
+ dstlvl
->faces
[face
].offset
;
770 /* Format etc. should match, so we really just need a memcpy().
771 * In fact, that memcpy() could be done by the hardware in many
772 * cases, provided that we have a proper memory manager.
774 radeon_mipmap_level
*srclvl
= &image
->mt
->levels
[image
->mtlevel
];
776 assert(srclvl
->size
== dstlvl
->size
);
777 assert(srclvl
->rowstride
== dstlvl
->rowstride
);
779 radeon_bo_map(image
->mt
->bo
, GL_FALSE
);
782 image
->mt
->bo
->ptr
+ srclvl
->faces
[face
].offset
,
784 radeon_bo_unmap(image
->mt
->bo
);
786 radeon_miptree_unreference(image
->mt
);
788 uint32_t srcrowstride
;
790 /* need to confirm this value is correct */
791 if (mt
->compressed
) {
792 height
= image
->base
.Height
/ 4;
793 srcrowstride
= image
->base
.RowStride
* mt
->bpp
;
795 height
= image
->base
.Height
* image
->base
.Depth
;
796 srcrowstride
= image
->base
.Width
* image
->base
.TexFormat
->TexelBytes
;
800 // WARN_ONCE("%s: tiling not supported yet", __FUNCTION__);
802 copy_rows(dest
, dstlvl
->rowstride
, image
->base
.Data
, srcrowstride
,
803 height
, srcrowstride
);
805 _mesa_free_texmemory(image
->base
.Data
);
806 image
->base
.Data
= 0;
809 radeon_bo_unmap(mt
->bo
);
812 image
->mtface
= face
;
813 image
->mtlevel
= level
;
814 radeon_miptree_reference(image
->mt
);
817 int radeon_validate_texture_miptree(GLcontext
* ctx
, struct gl_texture_object
*texObj
)
819 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
820 radeonTexObj
*t
= radeon_tex_obj(texObj
);
821 radeon_texture_image
*baseimage
= get_radeon_texture_image(texObj
->Image
[0][texObj
->BaseLevel
]);
824 if (t
->validated
|| t
->image_override
)
827 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
828 fprintf(stderr
, "%s: Validating texture %p now\n", __FUNCTION__
, texObj
);
830 if (baseimage
->base
.Border
> 0)
833 /* Ensure a matching miptree exists.
835 * Differing mipmap trees can result when the app uses TexImage to
836 * change texture dimensions.
838 * Prefer to use base image's miptree if it
839 * exists, since that most likely contains more valid data (remember
840 * that the base level is usually significantly larger than the rest
841 * of the miptree, so cubemaps are the only possible exception).
844 baseimage
->mt
!= t
->mt
&&
845 radeon_miptree_matches_texture(baseimage
->mt
, &t
->base
)) {
846 radeon_miptree_unreference(t
->mt
);
847 t
->mt
= baseimage
->mt
;
848 radeon_miptree_reference(t
->mt
);
849 } else if (t
->mt
&& !radeon_miptree_matches_texture(t
->mt
, &t
->base
)) {
850 radeon_miptree_unreference(t
->mt
);
855 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
856 fprintf(stderr
, " Allocate new miptree\n");
857 radeon_try_alloc_miptree(rmesa
, t
, &baseimage
->base
, 0, texObj
->BaseLevel
);
859 _mesa_problem(ctx
, "r300_validate_texture failed to alloc miptree");
864 /* Ensure all images are stored in the single main miptree */
865 for(face
= 0; face
< t
->mt
->faces
; ++face
) {
866 for(level
= t
->mt
->firstLevel
; level
<= t
->mt
->lastLevel
; ++level
) {
867 radeon_texture_image
*image
= get_radeon_texture_image(texObj
->Image
[face
][level
]);
868 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
869 fprintf(stderr
, " face %i, level %i... %p vs %p ", face
, level
, t
->mt
, image
->mt
);
870 if (t
->mt
== image
->mt
) {
871 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
872 fprintf(stderr
, "OK\n");
876 if (RADEON_DEBUG
& DEBUG_TEXTURE
)
877 fprintf(stderr
, "migrating\n");
878 migrate_image_to_miptree(t
->mt
, image
, face
, level
);