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
= (GLfloat
*) 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
= (GLfloat
*) 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 is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
311 /* Allocate buffer for one row of texels */
312 rgba
= (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
313 rgba_uint
= (GLuint (*)[4]) rgba
;
315 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
319 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
324 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
325 texImage
->_BaseFormat
== GL_INTENSITY
||
326 texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
327 /* If a luminance (or intensity) texture is read back as RGB(A), the
328 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
331 rebaseFormat
= texImage
->_BaseFormat
;
333 else if ((texImage
->_BaseFormat
== GL_RGBA
||
334 texImage
->_BaseFormat
== GL_RGB
) &&
335 (destBaseFormat
== GL_LUMINANCE
||
336 destBaseFormat
== GL_LUMINANCE_ALPHA
||
337 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
338 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
339 /* If we're reading back an RGB(A) texture as luminance then we need
340 * to return L=tex(R). Note, that's different from glReadPixels which
343 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
346 for (img
= 0; img
< depth
; img
++) {
350 /* map src texture buffer */
351 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
352 0, 0, width
, height
, GL_MAP_READ_BIT
,
353 &srcMap
, &rowstride
);
355 for (row
= 0; row
< height
; row
++) {
356 const GLubyte
*src
= srcMap
+ row
* rowstride
;
357 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
358 width
, height
, format
, type
,
362 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
364 _mesa_rebase_rgba_uint(width
, rgba_uint
, rebaseFormat
);
365 _mesa_pack_rgba_span_from_uints(ctx
, width
, rgba_uint
,
368 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
370 _mesa_rebase_rgba_float(width
, rgba
, rebaseFormat
);
371 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
373 &ctx
->Pack
, transferOps
);
377 /* Unmap the src texture buffer */
378 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
381 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
391 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
392 * Compressed textures are handled here as well.
395 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
396 GLenum format
, GLenum type
, GLvoid
*pixels
,
397 struct gl_texture_image
*texImage
)
399 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
400 GLbitfield transferOps
= 0x0;
402 /* In general, clamping does not apply to glGetTexImage, except when
403 * the returned type of the image can't hold negative values.
405 if (type_needs_clamping(type
)) {
406 /* the returned image type can't have negative values */
407 if (dataType
== GL_FLOAT
||
408 dataType
== GL_SIGNED_NORMALIZED
||
409 format
== GL_LUMINANCE
||
410 format
== GL_LUMINANCE_ALPHA
) {
411 transferOps
|= IMAGE_CLAMP_BIT
;
414 /* This applies to RGB, RGBA textures. if the format is either LUMINANCE
415 * or LUMINANCE ALPHA, luminance (L) is computed as L=R+G+B .we need to
416 * clamp the sum to [0,1].
418 else if ((format
== GL_LUMINANCE
||
419 format
== GL_LUMINANCE_ALPHA
) &&
420 dataType
== GL_UNSIGNED_NORMALIZED
) {
421 transferOps
|= IMAGE_CLAMP_BIT
;
424 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
425 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
426 pixels
, texImage
, transferOps
);
429 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
430 pixels
, texImage
, transferOps
);
436 * Try to do glGetTexImage() with simple memcpy().
437 * \return GL_TRUE if done, GL_FALSE otherwise
440 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
442 struct gl_texture_image
*texImage
)
444 const GLenum target
= texImage
->TexObject
->Target
;
445 GLboolean memCopy
= GL_FALSE
;
448 * Check if we can use memcpy to copy from the hardware texture
449 * format to the user's format/type.
450 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
452 if (target
== GL_TEXTURE_1D
||
453 target
== GL_TEXTURE_2D
||
454 target
== GL_TEXTURE_RECTANGLE
||
455 _mesa_is_cube_face(target
)) {
456 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
458 ctx
->Pack
.SwapBytes
);
462 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
463 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
465 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
466 texImage
->Height
, format
, type
, 0, 0);
467 const GLint dstRowStride
=
468 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
472 /* map src texture buffer */
473 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
474 0, 0, texImage
->Width
, texImage
->Height
,
475 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
478 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
479 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
483 for (row
= 0; row
< texImage
->Height
; row
++) {
484 memcpy(dst
, src
, bytesPerRow
);
490 /* unmap src texture buffer */
491 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
494 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
503 * This is the software fallback for Driver.GetTexImage().
504 * All error checking will have been done before this routine is called.
505 * We'll call ctx->Driver.MapTextureImage() to access the data, then
506 * unmap with ctx->Driver.UnmapTextureImage().
509 _mesa_get_teximage(struct gl_context
*ctx
,
510 GLenum format
, GLenum type
, GLvoid
*pixels
,
511 struct gl_texture_image
*texImage
)
515 switch (texImage
->TexObject
->Target
) {
526 /* map dest buffer, if PBO */
527 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
528 /* Packing texture image into a PBO.
529 * Map the (potentially) VRAM-based buffer into our process space so
530 * we can write into it with the code below.
531 * A hardware driver might use a sophisticated blit to move the
532 * texture data to the PBO if the PBO is in VRAM along with the texture.
534 GLubyte
*buf
= (GLubyte
*)
535 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
536 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
538 /* out of memory or other unexpected error */
539 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
542 /* <pixels> was an offset into the PBO.
543 * Now make it a real, client-side pointer inside the mapped region.
545 pixels
= ADD_POINTERS(buf
, pixels
);
548 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
551 else if (format
== GL_DEPTH_COMPONENT
) {
552 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
554 else if (format
== GL_DEPTH_STENCIL_EXT
) {
555 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
557 else if (format
== GL_YCBCR_MESA
) {
558 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
561 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
564 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
565 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
572 * This is the software fallback for Driver.GetCompressedTexImage().
573 * All error checking will have been done before this routine is called.
576 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
577 struct gl_texture_image
*texImage
,
580 const GLuint row_stride
=
581 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
586 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
587 /* pack texture image into a PBO */
588 GLubyte
*buf
= (GLubyte
*)
589 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
590 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
592 /* out of memory or other unexpected error */
593 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
594 "glGetCompresssedTexImage(map PBO failed)");
597 img
= ADD_POINTERS(buf
, img
);
600 /* map src texture buffer */
601 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
602 0, 0, texImage
->Width
, texImage
->Height
,
603 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
606 /* no pixelstore or pixel transfer, but respect stride */
608 if (row_stride
== srcRowStride
) {
609 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
613 memcpy(img
, src
, size
);
617 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
618 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
619 memcpy((GLubyte
*)img
+ i
* row_stride
,
620 (GLubyte
*)src
+ i
* srcRowStride
,
625 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
628 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
631 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
632 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
639 * Do error checking for a glGetTexImage() call.
640 * \return GL_TRUE if any error, GL_FALSE if no errors.
643 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
644 GLenum format
, GLenum type
, GLsizei clientMemSize
,
647 struct gl_texture_object
*texObj
;
648 struct gl_texture_image
*texImage
;
649 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
650 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
651 GLenum baseFormat
, err
;
653 if (maxLevels
== 0) {
654 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
658 if (level
< 0 || level
>= maxLevels
) {
659 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
663 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
664 if (err
!= GL_NO_ERROR
) {
665 _mesa_error(ctx
, err
, "glGetTexImage(format/type)");
669 texObj
= _mesa_get_current_tex_object(ctx
, target
);
671 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
672 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
676 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
678 /* non-existant texture image */
682 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
684 /* Make sure the requested image format is compatible with the
687 if (_mesa_is_color_format(format
)
688 && !_mesa_is_color_format(baseFormat
)) {
689 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
692 else if (_mesa_is_depth_format(format
)
693 && !_mesa_is_depth_format(baseFormat
)
694 && !_mesa_is_depthstencil_format(baseFormat
)) {
695 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
698 else if (_mesa_is_ycbcr_format(format
)
699 && !_mesa_is_ycbcr_format(baseFormat
)) {
700 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
703 else if (_mesa_is_depthstencil_format(format
)
704 && !_mesa_is_depthstencil_format(baseFormat
)) {
705 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
708 else if (_mesa_is_dudv_format(format
)
709 && !_mesa_is_dudv_format(baseFormat
)) {
710 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
714 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
715 texImage
->Height
, texImage
->Depth
,
716 format
, type
, clientMemSize
, pixels
)) {
717 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
718 _mesa_error(ctx
, GL_INVALID_OPERATION
,
719 "glGetTexImage(out of bounds PBO access)");
721 _mesa_error(ctx
, GL_INVALID_OPERATION
,
722 "glGetnTexImageARB(out of bounds access:"
723 " bufSize (%d) is too small)", clientMemSize
);
728 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
729 /* PBO should not be mapped */
730 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
731 _mesa_error(ctx
, GL_INVALID_OPERATION
,
732 "glGetTexImage(PBO is mapped)");
743 * Get texture image. Called by glGetTexImage.
745 * \param target texture target.
746 * \param level image level.
747 * \param format pixel data format for returned image.
748 * \param type pixel data type for returned image.
749 * \param bufSize size of the pixels data buffer.
750 * \param pixels returned pixel data.
753 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
754 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
756 struct gl_texture_object
*texObj
;
757 struct gl_texture_image
*texImage
;
758 GET_CURRENT_CONTEXT(ctx
);
759 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
761 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
766 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
767 /* not an error, do nothing */
771 texObj
= _mesa_get_current_tex_object(ctx
, target
);
772 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
774 if (_mesa_is_zero_size_texture(texImage
))
777 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
778 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
779 " dstFmt=0x%x, dstType=0x%x\n",
781 _mesa_get_format_name(texImage
->TexFormat
),
782 texImage
->Width
, texImage
->Height
,
786 _mesa_lock_texture(ctx
, texObj
);
788 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
790 _mesa_unlock_texture(ctx
, texObj
);
795 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
796 GLenum type
, GLvoid
*pixels
)
798 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
803 * Do error checking for a glGetCompressedTexImage() call.
804 * \return GL_TRUE if any error, GL_FALSE if no errors.
807 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
808 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
810 struct gl_texture_object
*texObj
;
811 struct gl_texture_image
*texImage
;
812 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
813 GLuint compressedSize
;
815 if (maxLevels
== 0) {
816 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
821 if (level
< 0 || level
>= maxLevels
) {
822 _mesa_error(ctx
, GL_INVALID_VALUE
,
823 "glGetCompressedTexImageARB(bad level = %d)", level
);
827 if (_mesa_is_proxy_texture(target
)) {
828 _mesa_error(ctx
, GL_INVALID_ENUM
,
829 "glGetCompressedTexImageARB(bad target = %s)",
830 _mesa_lookup_enum_by_nr(target
));
834 texObj
= _mesa_get_current_tex_object(ctx
, target
);
836 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
840 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
843 /* probably invalid mipmap level */
844 _mesa_error(ctx
, GL_INVALID_VALUE
,
845 "glGetCompressedTexImageARB(level)");
849 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
850 _mesa_error(ctx
, GL_INVALID_OPERATION
,
851 "glGetCompressedTexImageARB(texture is not compressed)");
855 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
860 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
861 /* do bounds checking on writing to client memory */
862 if (clientMemSize
< compressedSize
) {
863 _mesa_error(ctx
, GL_INVALID_OPERATION
,
864 "glGetnCompressedTexImageARB(out of bounds access:"
865 " bufSize (%d) is too small)", clientMemSize
);
869 /* do bounds checking on PBO write */
870 if ((const GLubyte
*) img
+ compressedSize
>
871 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
872 _mesa_error(ctx
, GL_INVALID_OPERATION
,
873 "glGetCompressedTexImage(out of bounds PBO access)");
877 /* make sure PBO is not mapped */
878 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
879 _mesa_error(ctx
, GL_INVALID_OPERATION
,
880 "glGetCompressedTexImage(PBO is mapped)");
890 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
893 struct gl_texture_object
*texObj
;
894 struct gl_texture_image
*texImage
;
895 GET_CURRENT_CONTEXT(ctx
);
896 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
898 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
902 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
903 /* not an error, do nothing */
907 texObj
= _mesa_get_current_tex_object(ctx
, target
);
908 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
910 if (_mesa_is_zero_size_texture(texImage
))
913 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
915 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
917 _mesa_get_format_name(texImage
->TexFormat
),
918 texImage
->Width
, texImage
->Height
);
921 _mesa_lock_texture(ctx
, texObj
);
923 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
925 _mesa_unlock_texture(ctx
, texObj
);
929 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
931 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);