2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (c) 2009 VMware, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Code for glGetTexImage() and glGetCompressedTexImage().
33 #include "bufferobj.h"
37 #include "format_unpack.h"
38 #include "glformats.h"
40 #include "mfeatures.h"
44 #include "texcompress.h"
45 #include "texgetimage.h"
51 * Can the given type represent negative values?
53 static inline GLboolean
54 type_needs_clamping(GLenum type
)
61 case GL_HALF_FLOAT_ARB
:
62 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
63 case GL_UNSIGNED_INT_5_9_9_9_REV
:
72 * glGetTexImage for depth/Z pixels.
75 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
76 GLenum format
, GLenum type
, GLvoid
*pixels
,
77 struct gl_texture_image
*texImage
)
79 const GLint width
= texImage
->Width
;
80 const GLint height
= texImage
->Height
;
81 const GLint depth
= texImage
->Depth
;
83 GLfloat
*depthRow
= malloc(width
* sizeof(GLfloat
));
86 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
90 for (img
= 0; img
< depth
; img
++) {
94 /* map src texture buffer */
95 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
96 0, 0, width
, height
, GL_MAP_READ_BIT
,
97 &srcMap
, &srcRowStride
);
100 for (row
= 0; row
< height
; row
++) {
101 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
102 width
, height
, format
, type
,
104 const GLubyte
*src
= srcMap
+ row
* srcRowStride
;
105 _mesa_unpack_float_z_row(texImage
->TexFormat
, width
, src
, depthRow
);
106 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
109 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
112 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
122 * glGetTexImage for depth/stencil pixels.
125 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
126 GLenum format
, GLenum type
, GLvoid
*pixels
,
127 struct gl_texture_image
*texImage
)
129 const GLint width
= texImage
->Width
;
130 const GLint height
= texImage
->Height
;
131 const GLint depth
= texImage
->Depth
;
134 for (img
= 0; img
< depth
; img
++) {
138 /* map src texture buffer */
139 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
140 0, 0, width
, height
, GL_MAP_READ_BIT
,
141 &srcMap
, &rowstride
);
144 for (row
= 0; row
< height
; row
++) {
145 const GLubyte
*src
= srcMap
+ row
* rowstride
;
146 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
147 width
, height
, format
, type
,
149 /* XXX Z24_S8 vs. S8_Z24??? */
150 memcpy(dest
, src
, width
* sizeof(GLuint
));
151 if (ctx
->Pack
.SwapBytes
) {
152 _mesa_swap4((GLuint
*) dest
, width
);
156 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
159 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
167 * glGetTexImage for YCbCr pixels.
170 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
171 GLenum format
, GLenum type
, GLvoid
*pixels
,
172 struct gl_texture_image
*texImage
)
174 const GLint width
= texImage
->Width
;
175 const GLint height
= texImage
->Height
;
176 const GLint depth
= texImage
->Depth
;
179 for (img
= 0; img
< depth
; img
++) {
183 /* map src texture buffer */
184 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
185 0, 0, width
, height
, GL_MAP_READ_BIT
,
186 &srcMap
, &rowstride
);
189 for (row
= 0; row
< height
; row
++) {
190 const GLubyte
*src
= srcMap
+ row
* rowstride
;
191 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
192 width
, height
, format
, type
,
194 memcpy(dest
, src
, width
* sizeof(GLushort
));
196 /* check for byte swapping */
197 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
198 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
199 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
200 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
201 if (!ctx
->Pack
.SwapBytes
)
202 _mesa_swap2((GLushort
*) dest
, width
);
204 else if (ctx
->Pack
.SwapBytes
) {
205 _mesa_swap2((GLushort
*) dest
, width
);
209 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
212 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
220 * Get a color texture image with decompression.
223 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
224 GLenum format
, GLenum type
, GLvoid
*pixels
,
225 struct gl_texture_image
*texImage
,
226 GLbitfield transferOps
)
228 /* don't want to apply sRGB -> RGB conversion here so override the format */
229 const gl_format texFormat
=
230 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
231 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
232 const GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, format
);
233 GLenum rebaseFormat
= GL_NONE
;
234 const GLuint width
= texImage
->Width
;
235 const GLuint height
= texImage
->Height
;
236 const GLuint depth
= texImage
->Depth
;
237 GLfloat
*tempImage
, *srcRow
;
240 /* Decompress into temp float buffer, then pack into user buffer */
241 tempImage
= malloc(width
* height
* depth
242 * 4 * sizeof(GLfloat
));
244 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
248 /* Decompress the texture image - results in 'tempImage' */
253 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
256 &srcMap
, &srcRowStride
);
258 _mesa_decompress_image(texFormat
, width
, height
,
259 srcMap
, srcRowStride
, tempImage
);
261 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
264 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
270 if (baseFormat
== GL_LUMINANCE
||
271 baseFormat
== GL_INTENSITY
||
272 baseFormat
== GL_LUMINANCE_ALPHA
) {
273 /* If a luminance (or intensity) texture is read back as RGB(A), the
274 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
277 rebaseFormat
= texImage
->_BaseFormat
;
279 else if ((baseFormat
== GL_RGBA
||
280 baseFormat
== GL_RGB
||
281 baseFormat
== GL_RG
) &&
282 (destBaseFormat
== GL_LUMINANCE
||
283 destBaseFormat
== GL_LUMINANCE_ALPHA
||
284 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
285 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
286 /* If we're reading back an RGB(A) texture as luminance then we need
287 * to return L=tex(R). Note, that's different from glReadPixels which
290 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
294 _mesa_rebase_rgba_float(width
* height
, (GLfloat (*)[4]) tempImage
,
299 for (row
= 0; row
< height
; row
++) {
300 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
301 width
, height
, format
, type
,
304 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) srcRow
,
305 format
, type
, dest
, &ctx
->Pack
, transferOps
);
314 * Return a base GL format given the user-requested format
315 * for glGetTexImage().
318 _mesa_base_pack_format(GLenum format
)
323 case GL_BGRA_INTEGER
:
324 case GL_RGBA_INTEGER
:
332 case GL_GREEN_INTEGER
:
334 case GL_BLUE_INTEGER
:
336 case GL_ALPHA_INTEGER
:
338 case GL_LUMINANCE_INTEGER_EXT
:
340 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
341 return GL_LUMINANCE_ALPHA
;
349 * Get an uncompressed color texture image.
352 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
353 GLenum format
, GLenum type
, GLvoid
*pixels
,
354 struct gl_texture_image
*texImage
,
355 GLbitfield transferOps
)
357 /* don't want to apply sRGB -> RGB conversion here so override the format */
358 const gl_format texFormat
=
359 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
360 const GLuint width
= texImage
->Width
;
361 GLenum destBaseFormat
= _mesa_base_pack_format(format
);
362 GLenum rebaseFormat
= GL_NONE
;
363 GLuint height
= texImage
->Height
;
364 GLuint depth
= texImage
->Depth
;
367 GLuint (*rgba_uint
)[4];
368 GLboolean tex_is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
369 GLboolean tex_is_uint
= _mesa_is_format_unsigned(texImage
->TexFormat
);
371 /* Allocate buffer for one row of texels */
372 rgba
= malloc(4 * width
* sizeof(GLfloat
));
373 rgba_uint
= (GLuint (*)[4]) rgba
;
375 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
379 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
384 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
385 texImage
->_BaseFormat
== GL_INTENSITY
||
386 texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
387 /* If a luminance (or intensity) texture is read back as RGB(A), the
388 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
391 rebaseFormat
= texImage
->_BaseFormat
;
393 else if ((texImage
->_BaseFormat
== GL_RGBA
||
394 texImage
->_BaseFormat
== GL_RGB
||
395 texImage
->_BaseFormat
== GL_RG
) &&
396 (destBaseFormat
== GL_LUMINANCE
||
397 destBaseFormat
== GL_LUMINANCE_ALPHA
||
398 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
399 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
400 /* If we're reading back an RGB(A) texture as luminance then we need
401 * to return L=tex(R). Note, that's different from glReadPixels which
404 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
407 for (img
= 0; img
< depth
; img
++) {
411 /* map src texture buffer */
412 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
413 0, 0, width
, height
, GL_MAP_READ_BIT
,
414 &srcMap
, &rowstride
);
416 for (row
= 0; row
< height
; row
++) {
417 const GLubyte
*src
= srcMap
+ row
* rowstride
;
418 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
419 width
, height
, format
, type
,
422 if (tex_is_integer
) {
423 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
425 _mesa_rebase_rgba_uint(width
, rgba_uint
, rebaseFormat
);
427 _mesa_pack_rgba_span_from_uints(ctx
, width
,
428 (GLuint (*)[4]) rgba_uint
,
431 _mesa_pack_rgba_span_from_ints(ctx
, width
,
432 (GLint (*)[4]) rgba_uint
,
436 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
438 _mesa_rebase_rgba_float(width
, rgba
, rebaseFormat
);
439 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
441 &ctx
->Pack
, transferOps
);
445 /* Unmap the src texture buffer */
446 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
449 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
459 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
460 * Compressed textures are handled here as well.
463 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
464 GLenum format
, GLenum type
, GLvoid
*pixels
,
465 struct gl_texture_image
*texImage
)
467 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
468 GLbitfield transferOps
= 0x0;
470 /* In general, clamping does not apply to glGetTexImage, except when
471 * the returned type of the image can't hold negative values.
473 if (type_needs_clamping(type
)) {
474 /* the returned image type can't have negative values */
475 if (dataType
== GL_FLOAT
||
476 dataType
== GL_SIGNED_NORMALIZED
||
477 format
== GL_LUMINANCE
||
478 format
== GL_LUMINANCE_ALPHA
) {
479 transferOps
|= IMAGE_CLAMP_BIT
;
483 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
484 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
485 pixels
, texImage
, transferOps
);
488 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
489 pixels
, texImage
, transferOps
);
495 * Try to do glGetTexImage() with simple memcpy().
496 * \return GL_TRUE if done, GL_FALSE otherwise
499 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
501 struct gl_texture_image
*texImage
)
503 const GLenum target
= texImage
->TexObject
->Target
;
504 GLboolean memCopy
= GL_FALSE
;
507 * Check if we can use memcpy to copy from the hardware texture
508 * format to the user's format/type.
509 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
511 if (target
== GL_TEXTURE_1D
||
512 target
== GL_TEXTURE_2D
||
513 target
== GL_TEXTURE_RECTANGLE
||
514 _mesa_is_cube_face(target
)) {
515 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
517 ctx
->Pack
.SwapBytes
);
521 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
522 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
524 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
525 texImage
->Height
, format
, type
, 0, 0);
526 const GLint dstRowStride
=
527 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
531 /* map src texture buffer */
532 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
533 0, 0, texImage
->Width
, texImage
->Height
,
534 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
537 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
538 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
542 for (row
= 0; row
< texImage
->Height
; row
++) {
543 memcpy(dst
, src
, bytesPerRow
);
549 /* unmap src texture buffer */
550 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
553 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
562 * This is the software fallback for Driver.GetTexImage().
563 * All error checking will have been done before this routine is called.
564 * We'll call ctx->Driver.MapTextureImage() to access the data, then
565 * unmap with ctx->Driver.UnmapTextureImage().
568 _mesa_get_teximage(struct gl_context
*ctx
,
569 GLenum format
, GLenum type
, GLvoid
*pixels
,
570 struct gl_texture_image
*texImage
)
574 switch (texImage
->TexObject
->Target
) {
585 /* map dest buffer, if PBO */
586 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
587 /* Packing texture image into a PBO.
588 * Map the (potentially) VRAM-based buffer into our process space so
589 * we can write into it with the code below.
590 * A hardware driver might use a sophisticated blit to move the
591 * texture data to the PBO if the PBO is in VRAM along with the texture.
593 GLubyte
*buf
= (GLubyte
*)
594 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
595 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
597 /* out of memory or other unexpected error */
598 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
601 /* <pixels> was an offset into the PBO.
602 * Now make it a real, client-side pointer inside the mapped region.
604 pixels
= ADD_POINTERS(buf
, pixels
);
607 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
610 else if (format
== GL_DEPTH_COMPONENT
) {
611 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
613 else if (format
== GL_DEPTH_STENCIL_EXT
) {
614 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
616 else if (format
== GL_YCBCR_MESA
) {
617 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
620 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
623 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
624 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
631 * This is the software fallback for Driver.GetCompressedTexImage().
632 * All error checking will have been done before this routine is called.
635 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
636 struct gl_texture_image
*texImage
,
639 const GLuint row_stride
=
640 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
645 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
646 /* pack texture image into a PBO */
647 GLubyte
*buf
= (GLubyte
*)
648 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
649 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
651 /* out of memory or other unexpected error */
652 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
653 "glGetCompresssedTexImage(map PBO failed)");
656 img
= ADD_POINTERS(buf
, img
);
659 /* map src texture buffer */
660 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
661 0, 0, texImage
->Width
, texImage
->Height
,
662 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
665 /* no pixelstore or pixel transfer, but respect stride */
667 if (row_stride
== srcRowStride
) {
668 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
672 memcpy(img
, src
, size
);
676 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
677 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
678 memcpy((GLubyte
*)img
+ i
* row_stride
,
679 (GLubyte
*)src
+ i
* srcRowStride
,
684 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
687 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
690 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
691 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
697 * Validate the texture target enum supplied to glTexImage or
698 * glCompressedTexImage.
701 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
)
708 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
709 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
710 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
711 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
712 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
713 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
714 return ctx
->Extensions
.ARB_texture_cube_map
;
715 case GL_TEXTURE_RECTANGLE_NV
:
716 return ctx
->Extensions
.NV_texture_rectangle
;
717 case GL_TEXTURE_1D_ARRAY_EXT
:
718 case GL_TEXTURE_2D_ARRAY_EXT
:
719 return (ctx
->Extensions
.MESA_texture_array
||
720 ctx
->Extensions
.EXT_texture_array
);
721 case GL_TEXTURE_CUBE_MAP_ARRAY
:
722 return ctx
->Extensions
.ARB_texture_cube_map_array
;
730 * Do error checking for a glGetTexImage() call.
731 * \return GL_TRUE if any error, GL_FALSE if no errors.
734 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
735 GLenum format
, GLenum type
, GLsizei clientMemSize
,
738 struct gl_texture_object
*texObj
;
739 struct gl_texture_image
*texImage
;
740 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
741 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
742 GLenum baseFormat
, err
;
744 if (!legal_getteximage_target(ctx
, target
)) {
745 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
749 assert(maxLevels
!= 0);
750 if (level
< 0 || level
>= maxLevels
) {
751 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
755 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
756 if (err
!= GL_NO_ERROR
) {
757 _mesa_error(ctx
, err
, "glGetTexImage(format/type)");
761 texObj
= _mesa_get_current_tex_object(ctx
, target
);
764 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
768 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
770 /* non-existant texture image */
774 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
776 /* Make sure the requested image format is compatible with the
779 if (_mesa_is_color_format(format
)
780 && !_mesa_is_color_format(baseFormat
)) {
781 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
784 else if (_mesa_is_depth_format(format
)
785 && !_mesa_is_depth_format(baseFormat
)
786 && !_mesa_is_depthstencil_format(baseFormat
)) {
787 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
790 else if (_mesa_is_ycbcr_format(format
)
791 && !_mesa_is_ycbcr_format(baseFormat
)) {
792 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
795 else if (_mesa_is_depthstencil_format(format
)
796 && !_mesa_is_depthstencil_format(baseFormat
)) {
797 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
800 else if (_mesa_is_dudv_format(format
)
801 && !_mesa_is_dudv_format(baseFormat
)) {
802 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
806 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
807 texImage
->Height
, texImage
->Depth
,
808 format
, type
, clientMemSize
, pixels
)) {
809 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
810 _mesa_error(ctx
, GL_INVALID_OPERATION
,
811 "glGetTexImage(out of bounds PBO access)");
813 _mesa_error(ctx
, GL_INVALID_OPERATION
,
814 "glGetnTexImageARB(out of bounds access:"
815 " bufSize (%d) is too small)", clientMemSize
);
820 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
821 /* PBO should not be mapped */
822 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
823 _mesa_error(ctx
, GL_INVALID_OPERATION
,
824 "glGetTexImage(PBO is mapped)");
835 * Get texture image. Called by glGetTexImage.
837 * \param target texture target.
838 * \param level image level.
839 * \param format pixel data format for returned image.
840 * \param type pixel data type for returned image.
841 * \param bufSize size of the pixels data buffer.
842 * \param pixels returned pixel data.
845 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
846 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
848 struct gl_texture_object
*texObj
;
849 struct gl_texture_image
*texImage
;
850 GET_CURRENT_CONTEXT(ctx
);
852 FLUSH_VERTICES(ctx
, 0);
854 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
859 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
860 /* not an error, do nothing */
864 texObj
= _mesa_get_current_tex_object(ctx
, target
);
865 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
867 if (_mesa_is_zero_size_texture(texImage
))
870 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
871 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
872 " dstFmt=0x%x, dstType=0x%x\n",
874 _mesa_get_format_name(texImage
->TexFormat
),
875 texImage
->Width
, texImage
->Height
,
879 _mesa_lock_texture(ctx
, texObj
);
881 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
883 _mesa_unlock_texture(ctx
, texObj
);
888 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
889 GLenum type
, GLvoid
*pixels
)
891 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
896 * Do error checking for a glGetCompressedTexImage() call.
897 * \return GL_TRUE if any error, GL_FALSE if no errors.
900 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
901 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
903 struct gl_texture_object
*texObj
;
904 struct gl_texture_image
*texImage
;
905 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
906 GLuint compressedSize
;
908 if (!legal_getteximage_target(ctx
, target
)) {
909 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
914 assert(maxLevels
!= 0);
915 if (level
< 0 || level
>= maxLevels
) {
916 _mesa_error(ctx
, GL_INVALID_VALUE
,
917 "glGetCompressedTexImageARB(bad level = %d)", level
);
921 texObj
= _mesa_get_current_tex_object(ctx
, target
);
923 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
927 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
930 /* probably invalid mipmap level */
931 _mesa_error(ctx
, GL_INVALID_VALUE
,
932 "glGetCompressedTexImageARB(level)");
936 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
937 _mesa_error(ctx
, GL_INVALID_OPERATION
,
938 "glGetCompressedTexImageARB(texture is not compressed)");
942 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
947 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
948 /* do bounds checking on writing to client memory */
949 if (clientMemSize
< (GLsizei
) compressedSize
) {
950 _mesa_error(ctx
, GL_INVALID_OPERATION
,
951 "glGetnCompressedTexImageARB(out of bounds access:"
952 " bufSize (%d) is too small)", clientMemSize
);
956 /* do bounds checking on PBO write */
957 if ((const GLubyte
*) img
+ compressedSize
>
958 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
959 _mesa_error(ctx
, GL_INVALID_OPERATION
,
960 "glGetCompressedTexImage(out of bounds PBO access)");
964 /* make sure PBO is not mapped */
965 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
966 _mesa_error(ctx
, GL_INVALID_OPERATION
,
967 "glGetCompressedTexImage(PBO is mapped)");
977 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
980 struct gl_texture_object
*texObj
;
981 struct gl_texture_image
*texImage
;
982 GET_CURRENT_CONTEXT(ctx
);
984 FLUSH_VERTICES(ctx
, 0);
986 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
990 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
991 /* not an error, do nothing */
995 texObj
= _mesa_get_current_tex_object(ctx
, target
);
996 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
998 if (_mesa_is_zero_size_texture(texImage
))
1001 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1003 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1005 _mesa_get_format_name(texImage
->TexFormat
),
1006 texImage
->Width
, texImage
->Height
);
1009 _mesa_lock_texture(ctx
, texObj
);
1011 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
1013 _mesa_unlock_texture(ctx
, texObj
);
1017 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1019 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);