2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2009 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * Code for glGetTexImage() and glGetCompressedTexImage().
33 #include "bufferobj.h"
37 #include "format_unpack.h"
38 #include "glformats.h"
43 #include "texcompress.h"
44 #include "texgetimage.h"
50 * Can the given type represent negative values?
52 static inline GLboolean
53 type_needs_clamping(GLenum type
)
60 case GL_HALF_FLOAT_ARB
:
61 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
62 case GL_UNSIGNED_INT_5_9_9_9_REV
:
71 * glGetTexImage for depth/Z pixels.
74 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
75 GLenum format
, GLenum type
, GLvoid
*pixels
,
76 struct gl_texture_image
*texImage
)
78 const GLint width
= texImage
->Width
;
79 const GLint height
= texImage
->Height
;
80 const GLint depth
= texImage
->Depth
;
82 GLfloat
*depthRow
= malloc(width
* sizeof(GLfloat
));
85 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
89 for (img
= 0; img
< depth
; img
++) {
93 /* map src texture buffer */
94 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
95 0, 0, width
, height
, GL_MAP_READ_BIT
,
96 &srcMap
, &srcRowStride
);
99 for (row
= 0; row
< height
; row
++) {
100 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
101 width
, height
, format
, type
,
103 const GLubyte
*src
= srcMap
+ row
* srcRowStride
;
104 _mesa_unpack_float_z_row(texImage
->TexFormat
, width
, src
, depthRow
);
105 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
108 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
111 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
121 * glGetTexImage for depth/stencil pixels.
124 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
125 GLenum format
, GLenum type
, GLvoid
*pixels
,
126 struct gl_texture_image
*texImage
)
128 const GLint width
= texImage
->Width
;
129 const GLint height
= texImage
->Height
;
130 const GLint depth
= texImage
->Depth
;
133 assert(format
== GL_DEPTH_STENCIL
);
134 assert(type
== GL_UNSIGNED_INT_24_8
);
135 /* XXX type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV is not handled yet */
137 for (img
= 0; img
< depth
; img
++) {
141 /* map src texture buffer */
142 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
143 0, 0, width
, height
, GL_MAP_READ_BIT
,
144 &srcMap
, &rowstride
);
147 for (row
= 0; row
< height
; row
++) {
148 const GLubyte
*src
= srcMap
+ row
* rowstride
;
149 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
150 width
, height
, format
, type
,
152 /* Unpack from texture's format to GL's z24_s8 layout */
153 _mesa_unpack_uint_24_8_depth_stencil_row(texImage
->TexFormat
,
155 (const GLuint
*) src
,
157 if (ctx
->Pack
.SwapBytes
) {
158 _mesa_swap4((GLuint
*) dest
, width
);
162 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
165 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
173 * glGetTexImage for YCbCr pixels.
176 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
177 GLenum format
, GLenum type
, GLvoid
*pixels
,
178 struct gl_texture_image
*texImage
)
180 const GLint width
= texImage
->Width
;
181 const GLint height
= texImage
->Height
;
182 const GLint depth
= texImage
->Depth
;
185 for (img
= 0; img
< depth
; img
++) {
189 /* map src texture buffer */
190 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
191 0, 0, width
, height
, GL_MAP_READ_BIT
,
192 &srcMap
, &rowstride
);
195 for (row
= 0; row
< height
; row
++) {
196 const GLubyte
*src
= srcMap
+ row
* rowstride
;
197 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
198 width
, height
, format
, type
,
200 memcpy(dest
, src
, width
* sizeof(GLushort
));
202 /* check for byte swapping */
203 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
204 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
205 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
206 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
207 if (!ctx
->Pack
.SwapBytes
)
208 _mesa_swap2((GLushort
*) dest
, width
);
210 else if (ctx
->Pack
.SwapBytes
) {
211 _mesa_swap2((GLushort
*) dest
, width
);
215 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
218 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
226 * Get a color texture image with decompression.
229 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
230 GLenum format
, GLenum type
, GLvoid
*pixels
,
231 struct gl_texture_image
*texImage
,
232 GLbitfield transferOps
)
234 /* don't want to apply sRGB -> RGB conversion here so override the format */
235 const mesa_format texFormat
=
236 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
237 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
238 const GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, format
);
239 GLenum rebaseFormat
= GL_NONE
;
240 const GLuint width
= texImage
->Width
;
241 const GLuint height
= texImage
->Height
;
242 const GLuint depth
= texImage
->Depth
;
243 GLfloat
*tempImage
, *tempSlice
, *srcRow
;
246 /* Decompress into temp float buffer, then pack into user buffer */
247 tempImage
= malloc(width
* height
* depth
248 * 4 * sizeof(GLfloat
));
250 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
254 /* Decompress the texture image slices - results in 'tempImage' */
255 for (slice
= 0; slice
< depth
; slice
++) {
259 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
261 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
264 &srcMap
, &srcRowStride
);
266 _mesa_decompress_image(texFormat
, width
, height
,
267 srcMap
, srcRowStride
, tempSlice
);
269 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
272 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
278 if (baseFormat
== GL_LUMINANCE
||
279 baseFormat
== GL_INTENSITY
||
280 baseFormat
== GL_LUMINANCE_ALPHA
) {
281 /* If a luminance (or intensity) texture is read back as RGB(A), the
282 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
285 rebaseFormat
= texImage
->_BaseFormat
;
287 else if ((baseFormat
== GL_RGBA
||
288 baseFormat
== GL_RGB
||
289 baseFormat
== GL_RG
) &&
290 (destBaseFormat
== GL_LUMINANCE
||
291 destBaseFormat
== GL_LUMINANCE_ALPHA
||
292 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
293 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
294 /* If we're reading back an RGB(A) texture as luminance then we need
295 * to return L=tex(R). Note, that's different from glReadPixels which
298 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
302 _mesa_rebase_rgba_float(width
* height
, (GLfloat (*)[4]) tempImage
,
306 tempSlice
= tempImage
;
307 for (slice
= 0; slice
< depth
; slice
++) {
309 for (row
= 0; row
< height
; row
++) {
310 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
311 width
, height
, format
, type
,
314 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) srcRow
,
315 format
, type
, dest
, &ctx
->Pack
, transferOps
);
318 tempSlice
+= 4 * width
* height
;
326 * Return a base GL format given the user-requested format
327 * for glGetTexImage().
330 _mesa_base_pack_format(GLenum format
)
335 case GL_BGRA_INTEGER
:
336 case GL_RGBA_INTEGER
:
344 case GL_GREEN_INTEGER
:
346 case GL_BLUE_INTEGER
:
348 case GL_ALPHA_INTEGER
:
350 case GL_LUMINANCE_INTEGER_EXT
:
352 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
353 return GL_LUMINANCE_ALPHA
;
361 * Get an uncompressed color texture image.
364 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
365 GLenum format
, GLenum type
, GLvoid
*pixels
,
366 struct gl_texture_image
*texImage
,
367 GLbitfield transferOps
)
369 /* don't want to apply sRGB -> RGB conversion here so override the format */
370 const mesa_format texFormat
=
371 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
372 const GLuint width
= texImage
->Width
;
373 GLenum destBaseFormat
= _mesa_base_pack_format(format
);
374 GLenum rebaseFormat
= GL_NONE
;
375 GLuint height
= texImage
->Height
;
376 GLuint depth
= texImage
->Depth
;
379 GLuint (*rgba_uint
)[4];
380 GLboolean tex_is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
381 GLboolean tex_is_uint
= _mesa_is_format_unsigned(texImage
->TexFormat
);
382 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
384 /* Allocate buffer for one row of texels */
385 rgba
= malloc(4 * width
* sizeof(GLfloat
));
386 rgba_uint
= (GLuint (*)[4]) rgba
;
388 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
392 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
397 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
398 texImage
->_BaseFormat
== GL_INTENSITY
||
399 texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
400 /* If a luminance (or intensity) texture is read back as RGB(A), the
401 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
404 rebaseFormat
= texImage
->_BaseFormat
;
406 else if ((texImage
->_BaseFormat
== GL_RGBA
||
407 texImage
->_BaseFormat
== GL_RGB
||
408 texImage
->_BaseFormat
== GL_RG
) &&
409 (destBaseFormat
== GL_LUMINANCE
||
410 destBaseFormat
== GL_LUMINANCE_ALPHA
||
411 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
412 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
413 /* If we're reading back an RGB(A) texture as luminance then we need
414 * to return L=tex(R). Note, that's different from glReadPixels which
417 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
419 else if (texImage
->_BaseFormat
!= texBaseFormat
) {
420 /* The internal format and the real format differ, so we can't rely
421 * on the unpack functions setting the correct constant values.
422 * (e.g. reading back GL_RGB8 which is actually RGBA won't set alpha=1)
424 switch (texImage
->_BaseFormat
) {
426 if ((texBaseFormat
== GL_RGBA
||
427 texBaseFormat
== GL_RGB
||
428 texBaseFormat
== GL_RG
) &&
429 (destBaseFormat
== GL_RGBA
||
430 destBaseFormat
== GL_RGB
||
431 destBaseFormat
== GL_RG
||
432 destBaseFormat
== GL_GREEN
)) {
433 rebaseFormat
= texImage
->_BaseFormat
;
438 if ((texBaseFormat
== GL_RGBA
||
439 texBaseFormat
== GL_RGB
) &&
440 (destBaseFormat
== GL_RGBA
||
441 destBaseFormat
== GL_RGB
||
442 destBaseFormat
== GL_BLUE
)) {
443 rebaseFormat
= texImage
->_BaseFormat
;
448 if (texBaseFormat
== GL_RGBA
&&
449 (destBaseFormat
== GL_RGBA
||
450 destBaseFormat
== GL_ALPHA
||
451 destBaseFormat
== GL_LUMINANCE_ALPHA
)) {
452 rebaseFormat
= texImage
->_BaseFormat
;
457 if (destBaseFormat
!= GL_ALPHA
) {
458 rebaseFormat
= texImage
->_BaseFormat
;
464 for (img
= 0; img
< depth
; img
++) {
468 /* map src texture buffer */
469 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
470 0, 0, width
, height
, GL_MAP_READ_BIT
,
471 &srcMap
, &rowstride
);
473 for (row
= 0; row
< height
; row
++) {
474 const GLubyte
*src
= srcMap
+ row
* rowstride
;
475 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
476 width
, height
, format
, type
,
479 if (tex_is_integer
) {
480 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
482 _mesa_rebase_rgba_uint(width
, rgba_uint
, rebaseFormat
);
484 _mesa_pack_rgba_span_from_uints(ctx
, width
,
485 (GLuint (*)[4]) rgba_uint
,
488 _mesa_pack_rgba_span_from_ints(ctx
, width
,
489 (GLint (*)[4]) rgba_uint
,
493 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
495 _mesa_rebase_rgba_float(width
, rgba
, rebaseFormat
);
496 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
498 &ctx
->Pack
, transferOps
);
502 /* Unmap the src texture buffer */
503 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
506 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
516 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
517 * Compressed textures are handled here as well.
520 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
521 GLenum format
, GLenum type
, GLvoid
*pixels
,
522 struct gl_texture_image
*texImage
)
524 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
525 GLbitfield transferOps
= 0x0;
527 /* In general, clamping does not apply to glGetTexImage, except when
528 * the returned type of the image can't hold negative values.
530 if (type_needs_clamping(type
)) {
531 /* the returned image type can't have negative values */
532 if (dataType
== GL_FLOAT
||
533 dataType
== GL_HALF_FLOAT
||
534 dataType
== GL_SIGNED_NORMALIZED
||
535 format
== GL_LUMINANCE
||
536 format
== GL_LUMINANCE_ALPHA
) {
537 transferOps
|= IMAGE_CLAMP_BIT
;
541 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
542 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
543 pixels
, texImage
, transferOps
);
546 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
547 pixels
, texImage
, transferOps
);
553 * Try to do glGetTexImage() with simple memcpy().
554 * \return GL_TRUE if done, GL_FALSE otherwise
557 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
559 struct gl_texture_image
*texImage
)
561 const GLenum target
= texImage
->TexObject
->Target
;
562 GLboolean memCopy
= GL_FALSE
;
563 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
566 * Check if we can use memcpy to copy from the hardware texture
567 * format to the user's format/type.
568 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
570 if ((target
== GL_TEXTURE_1D
||
571 target
== GL_TEXTURE_2D
||
572 target
== GL_TEXTURE_RECTANGLE
||
573 _mesa_is_cube_face(target
)) &&
574 texBaseFormat
== texImage
->_BaseFormat
) {
575 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
577 ctx
->Pack
.SwapBytes
);
581 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
582 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
584 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
585 texImage
->Height
, format
, type
, 0, 0);
586 const GLint dstRowStride
=
587 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
591 /* map src texture buffer */
592 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
593 0, 0, texImage
->Width
, texImage
->Height
,
594 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
597 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
598 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
602 for (row
= 0; row
< texImage
->Height
; row
++) {
603 memcpy(dst
, src
, bytesPerRow
);
609 /* unmap src texture buffer */
610 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
613 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
622 * This is the software fallback for Driver.GetTexImage().
623 * All error checking will have been done before this routine is called.
624 * We'll call ctx->Driver.MapTextureImage() to access the data, then
625 * unmap with ctx->Driver.UnmapTextureImage().
628 _mesa_get_teximage(struct gl_context
*ctx
,
629 GLenum format
, GLenum type
, GLvoid
*pixels
,
630 struct gl_texture_image
*texImage
)
632 const GLuint dimensions
=
633 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
635 /* map dest buffer, if PBO */
636 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
637 /* Packing texture image into a PBO.
638 * Map the (potentially) VRAM-based buffer into our process space so
639 * we can write into it with the code below.
640 * A hardware driver might use a sophisticated blit to move the
641 * texture data to the PBO if the PBO is in VRAM along with the texture.
643 GLubyte
*buf
= (GLubyte
*)
644 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
645 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
648 /* out of memory or other unexpected error */
649 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
652 /* <pixels> was an offset into the PBO.
653 * Now make it a real, client-side pointer inside the mapped region.
655 pixels
= ADD_POINTERS(buf
, pixels
);
658 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
661 else if (format
== GL_DEPTH_COMPONENT
) {
662 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
664 else if (format
== GL_DEPTH_STENCIL_EXT
) {
665 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
667 else if (format
== GL_YCBCR_MESA
) {
668 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
671 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
674 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
675 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
682 * This is the software fallback for Driver.GetCompressedTexImage().
683 * All error checking will have been done before this routine is called.
686 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
687 struct gl_texture_image
*texImage
,
690 const GLuint row_stride
=
691 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
696 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
697 /* pack texture image into a PBO */
698 GLubyte
*buf
= (GLubyte
*)
699 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
700 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
703 /* out of memory or other unexpected error */
704 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
705 "glGetCompresssedTexImage(map PBO failed)");
708 img
= ADD_POINTERS(buf
, img
);
711 /* map src texture buffer */
712 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
713 0, 0, texImage
->Width
, texImage
->Height
,
714 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
717 /* no pixelstore or pixel transfer, but respect stride */
719 if (row_stride
== srcRowStride
) {
720 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
724 memcpy(img
, src
, size
);
728 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
729 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
730 memcpy((GLubyte
*)img
+ i
* row_stride
,
731 (GLubyte
*)src
+ i
* srcRowStride
,
736 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
739 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
742 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
743 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
749 * Validate the texture target enum supplied to glTexImage or
750 * glCompressedTexImage.
753 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
)
760 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
761 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
762 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
763 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
764 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
765 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
766 return ctx
->Extensions
.ARB_texture_cube_map
;
767 case GL_TEXTURE_RECTANGLE_NV
:
768 return ctx
->Extensions
.NV_texture_rectangle
;
769 case GL_TEXTURE_1D_ARRAY_EXT
:
770 case GL_TEXTURE_2D_ARRAY_EXT
:
771 return ctx
->Extensions
.EXT_texture_array
;
772 case GL_TEXTURE_CUBE_MAP_ARRAY
:
773 return ctx
->Extensions
.ARB_texture_cube_map_array
;
781 * Do error checking for a glGetTexImage() call.
782 * \return GL_TRUE if any error, GL_FALSE if no errors.
785 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
786 GLenum format
, GLenum type
, GLsizei clientMemSize
,
789 struct gl_texture_object
*texObj
;
790 struct gl_texture_image
*texImage
;
791 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
792 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
793 GLenum baseFormat
, err
;
795 if (!legal_getteximage_target(ctx
, target
)) {
796 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
800 assert(maxLevels
!= 0);
801 if (level
< 0 || level
>= maxLevels
) {
802 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
806 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
807 if (err
!= GL_NO_ERROR
) {
808 _mesa_error(ctx
, err
, "glGetTexImage(format/type)");
812 texObj
= _mesa_get_current_tex_object(ctx
, target
);
815 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
819 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
821 /* non-existant texture image */
825 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
827 /* Make sure the requested image format is compatible with the
830 if (_mesa_is_color_format(format
)
831 && !_mesa_is_color_format(baseFormat
)) {
832 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
835 else if (_mesa_is_depth_format(format
)
836 && !_mesa_is_depth_format(baseFormat
)
837 && !_mesa_is_depthstencil_format(baseFormat
)) {
838 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
841 else if (_mesa_is_ycbcr_format(format
)
842 && !_mesa_is_ycbcr_format(baseFormat
)) {
843 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
846 else if (_mesa_is_depthstencil_format(format
)
847 && !_mesa_is_depthstencil_format(baseFormat
)) {
848 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
851 else if (_mesa_is_dudv_format(format
)
852 && !_mesa_is_dudv_format(baseFormat
)) {
853 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
857 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
858 texImage
->Height
, texImage
->Depth
,
859 format
, type
, clientMemSize
, pixels
)) {
860 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
861 _mesa_error(ctx
, GL_INVALID_OPERATION
,
862 "glGetTexImage(out of bounds PBO access)");
864 _mesa_error(ctx
, GL_INVALID_OPERATION
,
865 "glGetnTexImageARB(out of bounds access:"
866 " bufSize (%d) is too small)", clientMemSize
);
871 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
872 /* PBO should not be mapped */
873 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
874 _mesa_error(ctx
, GL_INVALID_OPERATION
,
875 "glGetTexImage(PBO is mapped)");
886 * Get texture image. Called by glGetTexImage.
888 * \param target texture target.
889 * \param level image level.
890 * \param format pixel data format for returned image.
891 * \param type pixel data type for returned image.
892 * \param bufSize size of the pixels data buffer.
893 * \param pixels returned pixel data.
896 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
897 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
899 struct gl_texture_object
*texObj
;
900 struct gl_texture_image
*texImage
;
901 GET_CURRENT_CONTEXT(ctx
);
903 FLUSH_VERTICES(ctx
, 0);
905 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
910 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
911 /* not an error, do nothing */
915 texObj
= _mesa_get_current_tex_object(ctx
, target
);
916 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
918 if (_mesa_is_zero_size_texture(texImage
))
921 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
922 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
923 " dstFmt=0x%x, dstType=0x%x\n",
925 _mesa_get_format_name(texImage
->TexFormat
),
926 texImage
->Width
, texImage
->Height
,
930 _mesa_lock_texture(ctx
, texObj
);
932 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
934 _mesa_unlock_texture(ctx
, texObj
);
939 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
940 GLenum type
, GLvoid
*pixels
)
942 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
947 * Do error checking for a glGetCompressedTexImage() call.
948 * \return GL_TRUE if any error, GL_FALSE if no errors.
951 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
952 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
954 struct gl_texture_object
*texObj
;
955 struct gl_texture_image
*texImage
;
956 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
957 GLuint compressedSize
;
959 if (!legal_getteximage_target(ctx
, target
)) {
960 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
965 assert(maxLevels
!= 0);
966 if (level
< 0 || level
>= maxLevels
) {
967 _mesa_error(ctx
, GL_INVALID_VALUE
,
968 "glGetCompressedTexImageARB(bad level = %d)", level
);
972 texObj
= _mesa_get_current_tex_object(ctx
, target
);
974 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
978 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
981 /* probably invalid mipmap level */
982 _mesa_error(ctx
, GL_INVALID_VALUE
,
983 "glGetCompressedTexImageARB(level)");
987 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
988 _mesa_error(ctx
, GL_INVALID_OPERATION
,
989 "glGetCompressedTexImageARB(texture is not compressed)");
993 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
998 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
999 /* do bounds checking on writing to client memory */
1000 if (clientMemSize
< (GLsizei
) compressedSize
) {
1001 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1002 "glGetnCompressedTexImageARB(out of bounds access:"
1003 " bufSize (%d) is too small)", clientMemSize
);
1007 /* do bounds checking on PBO write */
1008 if ((const GLubyte
*) img
+ compressedSize
>
1009 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1010 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1011 "glGetCompressedTexImage(out of bounds PBO access)");
1015 /* make sure PBO is not mapped */
1016 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1017 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1018 "glGetCompressedTexImage(PBO is mapped)");
1028 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1031 struct gl_texture_object
*texObj
;
1032 struct gl_texture_image
*texImage
;
1033 GET_CURRENT_CONTEXT(ctx
);
1035 FLUSH_VERTICES(ctx
, 0);
1037 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
1041 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
1042 /* not an error, do nothing */
1046 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1047 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1049 if (_mesa_is_zero_size_texture(texImage
))
1052 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1054 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1056 _mesa_get_format_name(texImage
->TexFormat
),
1057 texImage
->Width
, texImage
->Height
);
1060 _mesa_lock_texture(ctx
, texObj
);
1062 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
1064 _mesa_unlock_texture(ctx
, texObj
);
1068 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1070 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);