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 * Get an uncompressed color texture image.
317 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
318 GLenum format
, GLenum type
, GLvoid
*pixels
,
319 struct gl_texture_image
*texImage
,
320 GLbitfield transferOps
)
322 /* don't want to apply sRGB -> RGB conversion here so override the format */
323 const gl_format texFormat
=
324 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
325 const GLuint width
= texImage
->Width
;
326 const GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, format
);
327 GLenum rebaseFormat
= GL_NONE
;
328 GLuint height
= texImage
->Height
;
329 GLuint depth
= texImage
->Depth
;
332 GLuint (*rgba_uint
)[4];
333 GLboolean tex_is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
334 GLboolean tex_is_uint
= _mesa_is_format_unsigned(texImage
->TexFormat
);
336 /* Allocate buffer for one row of texels */
337 rgba
= malloc(4 * width
* sizeof(GLfloat
));
338 rgba_uint
= (GLuint (*)[4]) rgba
;
340 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
344 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
349 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
350 texImage
->_BaseFormat
== GL_INTENSITY
||
351 texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
352 /* If a luminance (or intensity) texture is read back as RGB(A), the
353 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
356 rebaseFormat
= texImage
->_BaseFormat
;
358 else if ((texImage
->_BaseFormat
== GL_RGBA
||
359 texImage
->_BaseFormat
== GL_RGB
||
360 texImage
->_BaseFormat
== GL_RG
) &&
361 (destBaseFormat
== GL_LUMINANCE
||
362 destBaseFormat
== GL_LUMINANCE_ALPHA
||
363 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
364 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
365 /* If we're reading back an RGB(A) texture as luminance then we need
366 * to return L=tex(R). Note, that's different from glReadPixels which
369 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
372 for (img
= 0; img
< depth
; img
++) {
376 /* map src texture buffer */
377 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
378 0, 0, width
, height
, GL_MAP_READ_BIT
,
379 &srcMap
, &rowstride
);
381 for (row
= 0; row
< height
; row
++) {
382 const GLubyte
*src
= srcMap
+ row
* rowstride
;
383 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
384 width
, height
, format
, type
,
387 if (tex_is_integer
) {
388 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
390 _mesa_rebase_rgba_uint(width
, rgba_uint
, rebaseFormat
);
392 _mesa_pack_rgba_span_from_uints(ctx
, width
,
393 (GLuint (*)[4]) rgba_uint
,
396 _mesa_pack_rgba_span_from_ints(ctx
, width
,
397 (GLint (*)[4]) rgba_uint
,
401 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
403 _mesa_rebase_rgba_float(width
, rgba
, rebaseFormat
);
404 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
406 &ctx
->Pack
, transferOps
);
410 /* Unmap the src texture buffer */
411 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
414 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
424 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
425 * Compressed textures are handled here as well.
428 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
429 GLenum format
, GLenum type
, GLvoid
*pixels
,
430 struct gl_texture_image
*texImage
)
432 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
433 GLbitfield transferOps
= 0x0;
435 /* In general, clamping does not apply to glGetTexImage, except when
436 * the returned type of the image can't hold negative values.
438 if (type_needs_clamping(type
)) {
439 /* the returned image type can't have negative values */
440 if (dataType
== GL_FLOAT
||
441 dataType
== GL_SIGNED_NORMALIZED
||
442 format
== GL_LUMINANCE
||
443 format
== GL_LUMINANCE_ALPHA
) {
444 transferOps
|= IMAGE_CLAMP_BIT
;
448 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
449 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
450 pixels
, texImage
, transferOps
);
453 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
454 pixels
, texImage
, transferOps
);
460 * Try to do glGetTexImage() with simple memcpy().
461 * \return GL_TRUE if done, GL_FALSE otherwise
464 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
466 struct gl_texture_image
*texImage
)
468 const GLenum target
= texImage
->TexObject
->Target
;
469 GLboolean memCopy
= GL_FALSE
;
472 * Check if we can use memcpy to copy from the hardware texture
473 * format to the user's format/type.
474 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
476 if (target
== GL_TEXTURE_1D
||
477 target
== GL_TEXTURE_2D
||
478 target
== GL_TEXTURE_RECTANGLE
||
479 _mesa_is_cube_face(target
)) {
480 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
482 ctx
->Pack
.SwapBytes
);
486 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
487 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
489 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
490 texImage
->Height
, format
, type
, 0, 0);
491 const GLint dstRowStride
=
492 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
496 /* map src texture buffer */
497 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
498 0, 0, texImage
->Width
, texImage
->Height
,
499 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
502 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
503 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
507 for (row
= 0; row
< texImage
->Height
; row
++) {
508 memcpy(dst
, src
, bytesPerRow
);
514 /* unmap src texture buffer */
515 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
518 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
527 * This is the software fallback for Driver.GetTexImage().
528 * All error checking will have been done before this routine is called.
529 * We'll call ctx->Driver.MapTextureImage() to access the data, then
530 * unmap with ctx->Driver.UnmapTextureImage().
533 _mesa_get_teximage(struct gl_context
*ctx
,
534 GLenum format
, GLenum type
, GLvoid
*pixels
,
535 struct gl_texture_image
*texImage
)
539 switch (texImage
->TexObject
->Target
) {
550 /* map dest buffer, if PBO */
551 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
552 /* Packing texture image into a PBO.
553 * Map the (potentially) VRAM-based buffer into our process space so
554 * we can write into it with the code below.
555 * A hardware driver might use a sophisticated blit to move the
556 * texture data to the PBO if the PBO is in VRAM along with the texture.
558 GLubyte
*buf
= (GLubyte
*)
559 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
560 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
562 /* out of memory or other unexpected error */
563 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
566 /* <pixels> was an offset into the PBO.
567 * Now make it a real, client-side pointer inside the mapped region.
569 pixels
= ADD_POINTERS(buf
, pixels
);
572 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
575 else if (format
== GL_DEPTH_COMPONENT
) {
576 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
578 else if (format
== GL_DEPTH_STENCIL_EXT
) {
579 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
581 else if (format
== GL_YCBCR_MESA
) {
582 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
585 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
588 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
589 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
596 * This is the software fallback for Driver.GetCompressedTexImage().
597 * All error checking will have been done before this routine is called.
600 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
601 struct gl_texture_image
*texImage
,
604 const GLuint row_stride
=
605 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
610 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
611 /* pack texture image into a PBO */
612 GLubyte
*buf
= (GLubyte
*)
613 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
614 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
616 /* out of memory or other unexpected error */
617 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
618 "glGetCompresssedTexImage(map PBO failed)");
621 img
= ADD_POINTERS(buf
, img
);
624 /* map src texture buffer */
625 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
626 0, 0, texImage
->Width
, texImage
->Height
,
627 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
630 /* no pixelstore or pixel transfer, but respect stride */
632 if (row_stride
== srcRowStride
) {
633 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
637 memcpy(img
, src
, size
);
641 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
642 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
643 memcpy((GLubyte
*)img
+ i
* row_stride
,
644 (GLubyte
*)src
+ i
* srcRowStride
,
649 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
652 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
655 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
656 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
662 * Validate the texture target enum supplied to glTexImage or
663 * glCompressedTexImage.
666 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
)
673 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
674 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
675 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
676 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
677 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
678 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
679 return ctx
->Extensions
.ARB_texture_cube_map
;
680 case GL_TEXTURE_RECTANGLE_NV
:
681 return ctx
->Extensions
.NV_texture_rectangle
;
682 case GL_TEXTURE_1D_ARRAY_EXT
:
683 case GL_TEXTURE_2D_ARRAY_EXT
:
684 return (ctx
->Extensions
.MESA_texture_array
||
685 ctx
->Extensions
.EXT_texture_array
);
686 case GL_TEXTURE_CUBE_MAP_ARRAY
:
687 return ctx
->Extensions
.ARB_texture_cube_map_array
;
695 * Do error checking for a glGetTexImage() call.
696 * \return GL_TRUE if any error, GL_FALSE if no errors.
699 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
700 GLenum format
, GLenum type
, GLsizei clientMemSize
,
703 struct gl_texture_object
*texObj
;
704 struct gl_texture_image
*texImage
;
705 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
706 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
707 GLenum baseFormat
, err
;
709 if (!legal_getteximage_target(ctx
, target
)) {
710 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
714 assert(maxLevels
!= 0);
715 if (level
< 0 || level
>= maxLevels
) {
716 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
720 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
721 if (err
!= GL_NO_ERROR
) {
722 _mesa_error(ctx
, err
, "glGetTexImage(format/type)");
726 texObj
= _mesa_get_current_tex_object(ctx
, target
);
729 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
733 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
735 /* non-existant texture image */
739 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
741 /* Make sure the requested image format is compatible with the
744 if (_mesa_is_color_format(format
)
745 && !_mesa_is_color_format(baseFormat
)) {
746 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
749 else if (_mesa_is_depth_format(format
)
750 && !_mesa_is_depth_format(baseFormat
)
751 && !_mesa_is_depthstencil_format(baseFormat
)) {
752 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
755 else if (_mesa_is_ycbcr_format(format
)
756 && !_mesa_is_ycbcr_format(baseFormat
)) {
757 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
760 else if (_mesa_is_depthstencil_format(format
)
761 && !_mesa_is_depthstencil_format(baseFormat
)) {
762 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
765 else if (_mesa_is_dudv_format(format
)
766 && !_mesa_is_dudv_format(baseFormat
)) {
767 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
771 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
772 texImage
->Height
, texImage
->Depth
,
773 format
, type
, clientMemSize
, pixels
)) {
774 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
775 _mesa_error(ctx
, GL_INVALID_OPERATION
,
776 "glGetTexImage(out of bounds PBO access)");
778 _mesa_error(ctx
, GL_INVALID_OPERATION
,
779 "glGetnTexImageARB(out of bounds access:"
780 " bufSize (%d) is too small)", clientMemSize
);
785 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
786 /* PBO should not be mapped */
787 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
788 _mesa_error(ctx
, GL_INVALID_OPERATION
,
789 "glGetTexImage(PBO is mapped)");
800 * Get texture image. Called by glGetTexImage.
802 * \param target texture target.
803 * \param level image level.
804 * \param format pixel data format for returned image.
805 * \param type pixel data type for returned image.
806 * \param bufSize size of the pixels data buffer.
807 * \param pixels returned pixel data.
810 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
811 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
813 struct gl_texture_object
*texObj
;
814 struct gl_texture_image
*texImage
;
815 GET_CURRENT_CONTEXT(ctx
);
817 FLUSH_VERTICES(ctx
, 0);
819 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
824 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
825 /* not an error, do nothing */
829 texObj
= _mesa_get_current_tex_object(ctx
, target
);
830 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
832 if (_mesa_is_zero_size_texture(texImage
))
835 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
836 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
837 " dstFmt=0x%x, dstType=0x%x\n",
839 _mesa_get_format_name(texImage
->TexFormat
),
840 texImage
->Width
, texImage
->Height
,
844 _mesa_lock_texture(ctx
, texObj
);
846 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
848 _mesa_unlock_texture(ctx
, texObj
);
853 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
854 GLenum type
, GLvoid
*pixels
)
856 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
861 * Do error checking for a glGetCompressedTexImage() call.
862 * \return GL_TRUE if any error, GL_FALSE if no errors.
865 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
866 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
868 struct gl_texture_object
*texObj
;
869 struct gl_texture_image
*texImage
;
870 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
871 GLuint compressedSize
;
873 if (!legal_getteximage_target(ctx
, target
)) {
874 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
879 assert(maxLevels
!= 0);
880 if (level
< 0 || level
>= maxLevels
) {
881 _mesa_error(ctx
, GL_INVALID_VALUE
,
882 "glGetCompressedTexImageARB(bad level = %d)", level
);
886 texObj
= _mesa_get_current_tex_object(ctx
, target
);
888 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
892 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
895 /* probably invalid mipmap level */
896 _mesa_error(ctx
, GL_INVALID_VALUE
,
897 "glGetCompressedTexImageARB(level)");
901 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
902 _mesa_error(ctx
, GL_INVALID_OPERATION
,
903 "glGetCompressedTexImageARB(texture is not compressed)");
907 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
912 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
913 /* do bounds checking on writing to client memory */
914 if (clientMemSize
< (GLsizei
) compressedSize
) {
915 _mesa_error(ctx
, GL_INVALID_OPERATION
,
916 "glGetnCompressedTexImageARB(out of bounds access:"
917 " bufSize (%d) is too small)", clientMemSize
);
921 /* do bounds checking on PBO write */
922 if ((const GLubyte
*) img
+ compressedSize
>
923 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
924 _mesa_error(ctx
, GL_INVALID_OPERATION
,
925 "glGetCompressedTexImage(out of bounds PBO access)");
929 /* make sure PBO is not mapped */
930 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
931 _mesa_error(ctx
, GL_INVALID_OPERATION
,
932 "glGetCompressedTexImage(PBO is mapped)");
942 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
945 struct gl_texture_object
*texObj
;
946 struct gl_texture_image
*texImage
;
947 GET_CURRENT_CONTEXT(ctx
);
949 FLUSH_VERTICES(ctx
, 0);
951 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
955 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
956 /* not an error, do nothing */
960 texObj
= _mesa_get_current_tex_object(ctx
, target
);
961 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
963 if (_mesa_is_zero_size_texture(texImage
))
966 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
968 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
970 _mesa_get_format_name(texImage
->TexFormat
),
971 texImage
->Width
, texImage
->Height
);
974 _mesa_lock_texture(ctx
, texObj
);
976 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
978 _mesa_unlock_texture(ctx
, texObj
);
982 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
984 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);