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 GLuint width
= texImage
->Width
;
233 const GLuint height
= texImage
->Height
;
234 const GLuint depth
= texImage
->Depth
;
235 GLfloat
*tempImage
, *srcRow
;
238 /* Decompress into temp float buffer, then pack into user buffer */
239 tempImage
= malloc(width
* height
* depth
240 * 4 * sizeof(GLfloat
));
242 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
246 /* Decompress the texture image - results in 'tempImage' */
251 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
254 &srcMap
, &srcRowStride
);
256 _mesa_decompress_image(texFormat
, width
, height
,
257 srcMap
, srcRowStride
, tempImage
);
259 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
262 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
268 if (baseFormat
== GL_LUMINANCE
||
269 baseFormat
== GL_LUMINANCE_ALPHA
) {
270 _mesa_rebase_rgba_float(width
* height
, (GLfloat (*)[4]) tempImage
,
275 for (row
= 0; row
< height
; row
++) {
276 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
277 width
, height
, format
, type
,
280 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) srcRow
,
281 format
, type
, dest
, &ctx
->Pack
, transferOps
);
290 * Get an uncompressed color texture image.
293 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
294 GLenum format
, GLenum type
, GLvoid
*pixels
,
295 struct gl_texture_image
*texImage
,
296 GLbitfield transferOps
)
298 /* don't want to apply sRGB -> RGB conversion here so override the format */
299 const gl_format texFormat
=
300 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
301 const GLuint width
= texImage
->Width
;
302 const GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, format
);
303 GLenum rebaseFormat
= GL_NONE
;
304 GLuint height
= texImage
->Height
;
305 GLuint depth
= texImage
->Depth
;
308 GLuint (*rgba_uint
)[4];
309 GLboolean tex_is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
310 GLboolean tex_is_uint
= _mesa_is_format_unsigned(texImage
->TexFormat
);
312 /* Allocate buffer for one row of texels */
313 rgba
= malloc(4 * width
* sizeof(GLfloat
));
314 rgba_uint
= (GLuint (*)[4]) rgba
;
316 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
320 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
325 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
326 texImage
->_BaseFormat
== GL_INTENSITY
||
327 texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
328 /* If a luminance (or intensity) texture is read back as RGB(A), the
329 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
332 rebaseFormat
= texImage
->_BaseFormat
;
334 else if ((texImage
->_BaseFormat
== GL_RGBA
||
335 texImage
->_BaseFormat
== GL_RGB
) &&
336 (destBaseFormat
== GL_LUMINANCE
||
337 destBaseFormat
== GL_LUMINANCE_ALPHA
||
338 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
339 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
340 /* If we're reading back an RGB(A) texture as luminance then we need
341 * to return L=tex(R). Note, that's different from glReadPixels which
344 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
347 for (img
= 0; img
< depth
; img
++) {
351 /* map src texture buffer */
352 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
353 0, 0, width
, height
, GL_MAP_READ_BIT
,
354 &srcMap
, &rowstride
);
356 for (row
= 0; row
< height
; row
++) {
357 const GLubyte
*src
= srcMap
+ row
* rowstride
;
358 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
359 width
, height
, format
, type
,
362 if (tex_is_integer
) {
363 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
365 _mesa_rebase_rgba_uint(width
, rgba_uint
, rebaseFormat
);
367 _mesa_pack_rgba_span_from_uints(ctx
, width
,
368 (GLuint (*)[4]) rgba_uint
,
371 _mesa_pack_rgba_span_from_ints(ctx
, width
,
372 (GLint (*)[4]) rgba_uint
,
376 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
378 _mesa_rebase_rgba_float(width
, rgba
, rebaseFormat
);
379 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
381 &ctx
->Pack
, transferOps
);
385 /* Unmap the src texture buffer */
386 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
399 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
400 * Compressed textures are handled here as well.
403 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
404 GLenum format
, GLenum type
, GLvoid
*pixels
,
405 struct gl_texture_image
*texImage
)
407 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
408 GLbitfield transferOps
= 0x0;
410 /* In general, clamping does not apply to glGetTexImage, except when
411 * the returned type of the image can't hold negative values.
413 if (type_needs_clamping(type
)) {
414 /* the returned image type can't have negative values */
415 if (dataType
== GL_FLOAT
||
416 dataType
== GL_SIGNED_NORMALIZED
||
417 format
== GL_LUMINANCE
||
418 format
== GL_LUMINANCE_ALPHA
) {
419 transferOps
|= IMAGE_CLAMP_BIT
;
422 /* This applies to RGB, RGBA textures. if the format is either LUMINANCE
423 * or LUMINANCE ALPHA, luminance (L) is computed as L=R+G+B .we need to
424 * clamp the sum to [0,1].
426 else if ((format
== GL_LUMINANCE
||
427 format
== GL_LUMINANCE_ALPHA
) &&
428 dataType
== GL_UNSIGNED_NORMALIZED
) {
429 transferOps
|= IMAGE_CLAMP_BIT
;
432 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
433 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
434 pixels
, texImage
, transferOps
);
437 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
438 pixels
, texImage
, transferOps
);
444 * Try to do glGetTexImage() with simple memcpy().
445 * \return GL_TRUE if done, GL_FALSE otherwise
448 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
450 struct gl_texture_image
*texImage
)
452 const GLenum target
= texImage
->TexObject
->Target
;
453 GLboolean memCopy
= GL_FALSE
;
456 * Check if we can use memcpy to copy from the hardware texture
457 * format to the user's format/type.
458 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
460 if (target
== GL_TEXTURE_1D
||
461 target
== GL_TEXTURE_2D
||
462 target
== GL_TEXTURE_RECTANGLE
||
463 _mesa_is_cube_face(target
)) {
464 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
466 ctx
->Pack
.SwapBytes
);
470 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
471 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
473 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
474 texImage
->Height
, format
, type
, 0, 0);
475 const GLint dstRowStride
=
476 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
480 /* map src texture buffer */
481 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
482 0, 0, texImage
->Width
, texImage
->Height
,
483 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
486 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
487 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
491 for (row
= 0; row
< texImage
->Height
; row
++) {
492 memcpy(dst
, src
, bytesPerRow
);
498 /* unmap src texture buffer */
499 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
502 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
511 * This is the software fallback for Driver.GetTexImage().
512 * All error checking will have been done before this routine is called.
513 * We'll call ctx->Driver.MapTextureImage() to access the data, then
514 * unmap with ctx->Driver.UnmapTextureImage().
517 _mesa_get_teximage(struct gl_context
*ctx
,
518 GLenum format
, GLenum type
, GLvoid
*pixels
,
519 struct gl_texture_image
*texImage
)
523 switch (texImage
->TexObject
->Target
) {
534 /* map dest buffer, if PBO */
535 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
536 /* Packing texture image into a PBO.
537 * Map the (potentially) VRAM-based buffer into our process space so
538 * we can write into it with the code below.
539 * A hardware driver might use a sophisticated blit to move the
540 * texture data to the PBO if the PBO is in VRAM along with the texture.
542 GLubyte
*buf
= (GLubyte
*)
543 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
544 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
546 /* out of memory or other unexpected error */
547 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
550 /* <pixels> was an offset into the PBO.
551 * Now make it a real, client-side pointer inside the mapped region.
553 pixels
= ADD_POINTERS(buf
, pixels
);
556 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
559 else if (format
== GL_DEPTH_COMPONENT
) {
560 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
562 else if (format
== GL_DEPTH_STENCIL_EXT
) {
563 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
565 else if (format
== GL_YCBCR_MESA
) {
566 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
569 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
572 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
573 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
580 * This is the software fallback for Driver.GetCompressedTexImage().
581 * All error checking will have been done before this routine is called.
584 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
585 struct gl_texture_image
*texImage
,
588 const GLuint row_stride
=
589 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
594 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
595 /* pack texture image into a PBO */
596 GLubyte
*buf
= (GLubyte
*)
597 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
598 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
600 /* out of memory or other unexpected error */
601 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
602 "glGetCompresssedTexImage(map PBO failed)");
605 img
= ADD_POINTERS(buf
, img
);
608 /* map src texture buffer */
609 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
610 0, 0, texImage
->Width
, texImage
->Height
,
611 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
614 /* no pixelstore or pixel transfer, but respect stride */
616 if (row_stride
== srcRowStride
) {
617 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
621 memcpy(img
, src
, size
);
625 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
626 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
627 memcpy((GLubyte
*)img
+ i
* row_stride
,
628 (GLubyte
*)src
+ i
* srcRowStride
,
633 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
636 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
639 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
640 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
646 * Validate the texture target enum supplied to glTexImage or
647 * glCompressedTexImage.
650 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
)
657 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
658 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
659 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
660 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
661 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
662 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
663 return ctx
->Extensions
.ARB_texture_cube_map
;
664 case GL_TEXTURE_RECTANGLE_NV
:
665 return ctx
->Extensions
.NV_texture_rectangle
;
666 case GL_TEXTURE_1D_ARRAY_EXT
:
667 case GL_TEXTURE_2D_ARRAY_EXT
:
668 return (ctx
->Extensions
.MESA_texture_array
||
669 ctx
->Extensions
.EXT_texture_array
);
677 * Do error checking for a glGetTexImage() call.
678 * \return GL_TRUE if any error, GL_FALSE if no errors.
681 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
682 GLenum format
, GLenum type
, GLsizei clientMemSize
,
685 struct gl_texture_object
*texObj
;
686 struct gl_texture_image
*texImage
;
687 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
688 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
689 GLenum baseFormat
, err
;
691 if (!legal_getteximage_target(ctx
, target
)) {
692 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
696 assert(maxLevels
!= 0);
697 if (level
< 0 || level
>= maxLevels
) {
698 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
702 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
703 if (err
!= GL_NO_ERROR
) {
704 _mesa_error(ctx
, err
, "glGetTexImage(format/type)");
708 texObj
= _mesa_get_current_tex_object(ctx
, target
);
711 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
715 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
717 /* non-existant texture image */
721 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
723 /* Make sure the requested image format is compatible with the
726 if (_mesa_is_color_format(format
)
727 && !_mesa_is_color_format(baseFormat
)) {
728 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
731 else if (_mesa_is_depth_format(format
)
732 && !_mesa_is_depth_format(baseFormat
)
733 && !_mesa_is_depthstencil_format(baseFormat
)) {
734 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
737 else if (_mesa_is_ycbcr_format(format
)
738 && !_mesa_is_ycbcr_format(baseFormat
)) {
739 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
742 else if (_mesa_is_depthstencil_format(format
)
743 && !_mesa_is_depthstencil_format(baseFormat
)) {
744 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
747 else if (_mesa_is_dudv_format(format
)
748 && !_mesa_is_dudv_format(baseFormat
)) {
749 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
753 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
754 texImage
->Height
, texImage
->Depth
,
755 format
, type
, clientMemSize
, pixels
)) {
756 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
757 _mesa_error(ctx
, GL_INVALID_OPERATION
,
758 "glGetTexImage(out of bounds PBO access)");
760 _mesa_error(ctx
, GL_INVALID_OPERATION
,
761 "glGetnTexImageARB(out of bounds access:"
762 " bufSize (%d) is too small)", clientMemSize
);
767 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
768 /* PBO should not be mapped */
769 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
770 _mesa_error(ctx
, GL_INVALID_OPERATION
,
771 "glGetTexImage(PBO is mapped)");
782 * Get texture image. Called by glGetTexImage.
784 * \param target texture target.
785 * \param level image level.
786 * \param format pixel data format for returned image.
787 * \param type pixel data type for returned image.
788 * \param bufSize size of the pixels data buffer.
789 * \param pixels returned pixel data.
792 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
793 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
795 struct gl_texture_object
*texObj
;
796 struct gl_texture_image
*texImage
;
797 GET_CURRENT_CONTEXT(ctx
);
798 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
800 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
805 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
806 /* not an error, do nothing */
810 texObj
= _mesa_get_current_tex_object(ctx
, target
);
811 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
813 if (_mesa_is_zero_size_texture(texImage
))
816 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
817 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
818 " dstFmt=0x%x, dstType=0x%x\n",
820 _mesa_get_format_name(texImage
->TexFormat
),
821 texImage
->Width
, texImage
->Height
,
825 _mesa_lock_texture(ctx
, texObj
);
827 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
829 _mesa_unlock_texture(ctx
, texObj
);
834 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
835 GLenum type
, GLvoid
*pixels
)
837 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
842 * Do error checking for a glGetCompressedTexImage() call.
843 * \return GL_TRUE if any error, GL_FALSE if no errors.
846 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
847 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
849 struct gl_texture_object
*texObj
;
850 struct gl_texture_image
*texImage
;
851 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
852 GLuint compressedSize
;
854 if (!legal_getteximage_target(ctx
, target
)) {
855 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
860 assert(maxLevels
!= 0);
861 if (level
< 0 || level
>= maxLevels
) {
862 _mesa_error(ctx
, GL_INVALID_VALUE
,
863 "glGetCompressedTexImageARB(bad level = %d)", level
);
867 texObj
= _mesa_get_current_tex_object(ctx
, target
);
869 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
873 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
876 /* probably invalid mipmap level */
877 _mesa_error(ctx
, GL_INVALID_VALUE
,
878 "glGetCompressedTexImageARB(level)");
882 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
883 _mesa_error(ctx
, GL_INVALID_OPERATION
,
884 "glGetCompressedTexImageARB(texture is not compressed)");
888 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
893 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
894 /* do bounds checking on writing to client memory */
895 if (clientMemSize
< (GLsizei
) compressedSize
) {
896 _mesa_error(ctx
, GL_INVALID_OPERATION
,
897 "glGetnCompressedTexImageARB(out of bounds access:"
898 " bufSize (%d) is too small)", clientMemSize
);
902 /* do bounds checking on PBO write */
903 if ((const GLubyte
*) img
+ compressedSize
>
904 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
905 _mesa_error(ctx
, GL_INVALID_OPERATION
,
906 "glGetCompressedTexImage(out of bounds PBO access)");
910 /* make sure PBO is not mapped */
911 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
912 _mesa_error(ctx
, GL_INVALID_OPERATION
,
913 "glGetCompressedTexImage(PBO is mapped)");
923 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
926 struct gl_texture_object
*texObj
;
927 struct gl_texture_image
*texImage
;
928 GET_CURRENT_CONTEXT(ctx
);
929 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
931 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
935 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
936 /* not an error, do nothing */
940 texObj
= _mesa_get_current_tex_object(ctx
, target
);
941 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
943 if (_mesa_is_zero_size_texture(texImage
))
946 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
948 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
950 _mesa_get_format_name(texImage
->TexFormat
),
951 texImage
->Width
, texImage
->Height
);
954 _mesa_lock_texture(ctx
, texObj
);
956 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
958 _mesa_unlock_texture(ctx
, texObj
);
962 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
964 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);