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 "pixelstore.h"
44 #include "texcompress.h"
45 #include "texgetimage.h"
49 #include "format_utils.h"
50 #include "pixeltransfer.h"
53 * Can the given type represent negative values?
55 static inline GLboolean
56 type_needs_clamping(GLenum type
)
63 case GL_HALF_FLOAT_ARB
:
64 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
65 case GL_UNSIGNED_INT_5_9_9_9_REV
:
74 * glGetTexImage for depth/Z pixels.
77 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
78 GLenum format
, GLenum type
, GLvoid
*pixels
,
79 struct gl_texture_image
*texImage
)
81 const GLint width
= texImage
->Width
;
82 GLint height
= texImage
->Height
;
83 GLint depth
= texImage
->Depth
;
85 GLfloat
*depthRow
= malloc(width
* sizeof(GLfloat
));
88 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
92 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
97 for (img
= 0; img
< depth
; img
++) {
101 /* map src texture buffer */
102 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
103 0, 0, width
, height
, GL_MAP_READ_BIT
,
104 &srcMap
, &srcRowStride
);
107 for (row
= 0; row
< height
; row
++) {
108 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
109 width
, height
, format
, type
,
111 const GLubyte
*src
= srcMap
+ row
* srcRowStride
;
112 _mesa_unpack_float_z_row(texImage
->TexFormat
, width
, src
, depthRow
);
113 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
116 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
119 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
129 * glGetTexImage for depth/stencil pixels.
132 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
133 GLenum format
, GLenum type
, GLvoid
*pixels
,
134 struct gl_texture_image
*texImage
)
136 const GLint width
= texImage
->Width
;
137 const GLint height
= texImage
->Height
;
138 const GLint depth
= texImage
->Depth
;
141 assert(format
== GL_DEPTH_STENCIL
);
142 assert(type
== GL_UNSIGNED_INT_24_8
||
143 type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
145 for (img
= 0; img
< depth
; img
++) {
149 /* map src texture buffer */
150 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
151 0, 0, width
, height
, GL_MAP_READ_BIT
,
152 &srcMap
, &rowstride
);
155 for (row
= 0; row
< height
; row
++) {
156 const GLubyte
*src
= srcMap
+ row
* rowstride
;
157 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
158 width
, height
, format
, type
,
160 _mesa_unpack_depth_stencil_row(texImage
->TexFormat
,
162 (const GLuint
*) src
,
164 if (ctx
->Pack
.SwapBytes
) {
165 _mesa_swap4((GLuint
*) dest
, width
);
169 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
172 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
180 * glGetTexImage for YCbCr pixels.
183 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
184 GLenum format
, GLenum type
, GLvoid
*pixels
,
185 struct gl_texture_image
*texImage
)
187 const GLint width
= texImage
->Width
;
188 const GLint height
= texImage
->Height
;
189 const GLint depth
= texImage
->Depth
;
192 for (img
= 0; img
< depth
; img
++) {
196 /* map src texture buffer */
197 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
198 0, 0, width
, height
, GL_MAP_READ_BIT
,
199 &srcMap
, &rowstride
);
202 for (row
= 0; row
< height
; row
++) {
203 const GLubyte
*src
= srcMap
+ row
* rowstride
;
204 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
205 width
, height
, format
, type
,
207 memcpy(dest
, src
, width
* sizeof(GLushort
));
209 /* check for byte swapping */
210 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
211 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
212 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
213 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
214 if (!ctx
->Pack
.SwapBytes
)
215 _mesa_swap2((GLushort
*) dest
, width
);
217 else if (ctx
->Pack
.SwapBytes
) {
218 _mesa_swap2((GLushort
*) dest
, width
);
222 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
225 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
233 * Get a color texture image with decompression.
236 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
237 GLenum format
, GLenum type
, GLvoid
*pixels
,
238 struct gl_texture_image
*texImage
,
239 GLbitfield transferOps
)
241 /* don't want to apply sRGB -> RGB conversion here so override the format */
242 const mesa_format texFormat
=
243 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
244 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
245 const GLuint width
= texImage
->Width
;
246 const GLuint height
= texImage
->Height
;
247 const GLuint depth
= texImage
->Depth
;
248 GLfloat
*tempImage
, *tempSlice
;
250 int srcStride
, dstStride
;
253 uint8_t rebaseSwizzle
[4];
255 /* Decompress into temp float buffer, then pack into user buffer */
256 tempImage
= malloc(width
* height
* depth
257 * 4 * sizeof(GLfloat
));
259 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
263 /* Decompress the texture image slices - results in 'tempImage' */
264 for (slice
= 0; slice
< depth
; slice
++) {
268 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
270 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
273 &srcMap
, &srcRowStride
);
275 _mesa_decompress_image(texFormat
, width
, height
,
276 srcMap
, srcRowStride
, tempSlice
);
278 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
281 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
287 /* Depending on the base format involved we may need to apply a rebase
288 * tranaform (for example: if we download to a Luminance format we want
291 if (baseFormat
== GL_LUMINANCE
||
292 baseFormat
== GL_INTENSITY
) {
294 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
295 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
296 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
297 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
298 } else if (baseFormat
== GL_LUMINANCE_ALPHA
) {
300 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
301 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
302 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
303 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
308 srcStride
= 4 * width
* sizeof(GLfloat
);
309 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
310 dstFormat
= _mesa_format_from_format_and_type(format
, type
);
311 tempSlice
= tempImage
;
312 for (slice
= 0; slice
< depth
; slice
++) {
313 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
314 width
, height
, format
, type
,
316 _mesa_format_convert(dest
, dstFormat
, dstStride
,
317 tempSlice
, RGBA32_FLOAT
, srcStride
,
319 needsRebase
? rebaseSwizzle
: NULL
);
320 tempSlice
+= 4 * width
* height
;
328 * Return a base GL format given the user-requested format
329 * for glGetTexImage().
332 _mesa_base_pack_format(GLenum format
)
337 case GL_BGRA_INTEGER
:
338 case GL_RGBA_INTEGER
:
346 case GL_GREEN_INTEGER
:
348 case GL_BLUE_INTEGER
:
350 case GL_ALPHA_INTEGER
:
352 case GL_LUMINANCE_INTEGER_EXT
:
354 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
355 return GL_LUMINANCE_ALPHA
;
363 * Get an uncompressed color texture image.
366 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
367 GLenum format
, GLenum type
, GLvoid
*pixels
,
368 struct gl_texture_image
*texImage
,
369 GLbitfield transferOps
)
371 /* don't want to apply sRGB -> RGB conversion here so override the format */
372 const mesa_format texFormat
=
373 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
374 const GLuint width
= texImage
->Width
;
375 GLuint height
= texImage
->Height
;
376 GLuint depth
= texImage
->Depth
;
378 GLboolean dst_is_integer
;
381 uint8_t rebaseSwizzle
[4];
385 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
390 /* Depending on the base format involved we may need to apply a rebase
391 * tranaform (for example: if we download to a Luminance format we want
394 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
395 texImage
->_BaseFormat
== GL_INTENSITY
) {
397 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
398 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
399 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
400 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
401 } else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
403 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
404 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
405 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
406 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
407 } else if (texImage
->_BaseFormat
!= _mesa_get_format_base_format(texFormat
)) {
409 _mesa_compute_rgba2base2rgba_component_mapping(texImage
->_BaseFormat
,
415 /* Describe the dst format */
416 dst_is_integer
= _mesa_is_enum_format_integer(format
);
417 dst_format
= _mesa_format_from_format_and_type(format
, type
);
418 dst_stride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
420 /* Since _mesa_format_convert does not handle transferOps we need to handle
421 * them before we call the function. This requires to convert to RGBA float
422 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
423 * integer then transferOps do not apply.
425 assert(!transferOps
|| (transferOps
&& !dst_is_integer
));
426 (void) dst_is_integer
; /* silence unused var warning */
428 for (img
= 0; img
< depth
; img
++) {
437 /* map src texture buffer */
438 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
439 0, 0, width
, height
, GL_MAP_READ_BIT
,
440 &srcMap
, &rowstride
);
442 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
447 dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
448 width
, height
, format
, type
,
452 uint32_t rgba_format
;
454 bool need_convert
= false;
456 /* We will convert to RGBA float */
457 rgba_format
= RGBA32_FLOAT
;
458 rgba_stride
= width
* 4 * sizeof(GLfloat
);
460 /* If we are lucky and the dst format matches the RGBA format we need
461 * to convert to, then we can convert directly into the dst buffer
462 * and avoid the final conversion/copy from the rgba buffer to the dst
465 if (format
== rgba_format
) {
467 } else if (rgba
== NULL
) { /* Allocate the RGBA buffer only once */
469 rgba
= malloc(height
* rgba_stride
);
471 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
472 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
477 _mesa_format_convert(rgba
, rgba_format
, rgba_stride
,
478 img_src
, texFormat
, rowstride
,
480 needsRebase
? rebaseSwizzle
: NULL
);
482 /* Handle transfer ops now */
483 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
* height
, rgba
);
485 /* If we had to rebase, we have already handled that */
488 /* If we were lucky and our RGBA conversion matches the dst format, then
494 /* Otherwise, we need to convert from RGBA to dst next */
496 src_format
= rgba_format
;
497 src_stride
= rgba_stride
;
499 /* No RGBA conversion needed, convert directly to dst */
501 src_format
= texFormat
;
502 src_stride
= rowstride
;
505 /* Do the conversion to destination format */
506 _mesa_format_convert(dest
, dst_format
, dst_stride
,
507 src
, src_format
, src_stride
,
509 needsRebase
? rebaseSwizzle
: NULL
);
512 /* Handle byte swapping if required */
513 if (ctx
->Pack
.SwapBytes
) {
514 GLint swapSize
= _mesa_sizeof_packed_type(type
);
515 if (swapSize
== 2 || swapSize
== 4) {
516 int swapsPerPixel
= _mesa_bytes_per_pixel(format
, type
) / swapSize
;
517 assert(_mesa_bytes_per_pixel(format
, type
) % swapSize
== 0);
519 _mesa_swap2((GLushort
*) dest
, width
* height
* swapsPerPixel
);
520 else if (swapSize
== 4)
521 _mesa_swap4((GLuint
*) dest
, width
* height
* swapsPerPixel
);
525 /* Unmap the src texture buffer */
526 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
536 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
537 * Compressed textures are handled here as well.
540 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
541 GLenum format
, GLenum type
, GLvoid
*pixels
,
542 struct gl_texture_image
*texImage
)
544 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
545 GLbitfield transferOps
= 0x0;
547 /* In general, clamping does not apply to glGetTexImage, except when
548 * the returned type of the image can't hold negative values.
550 if (type_needs_clamping(type
)) {
551 /* the returned image type can't have negative values */
552 if (dataType
== GL_FLOAT
||
553 dataType
== GL_HALF_FLOAT
||
554 dataType
== GL_SIGNED_NORMALIZED
||
555 format
== GL_LUMINANCE
||
556 format
== GL_LUMINANCE_ALPHA
) {
557 transferOps
|= IMAGE_CLAMP_BIT
;
561 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
562 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
563 pixels
, texImage
, transferOps
);
566 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
567 pixels
, texImage
, transferOps
);
573 * Try to do glGetTexImage() with simple memcpy().
574 * \return GL_TRUE if done, GL_FALSE otherwise
577 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
579 struct gl_texture_image
*texImage
)
581 const GLenum target
= texImage
->TexObject
->Target
;
582 GLboolean memCopy
= GL_FALSE
;
583 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
586 * Check if we can use memcpy to copy from the hardware texture
587 * format to the user's format/type.
588 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
590 if ((target
== GL_TEXTURE_1D
||
591 target
== GL_TEXTURE_2D
||
592 target
== GL_TEXTURE_RECTANGLE
||
593 _mesa_is_cube_face(target
)) &&
594 texBaseFormat
== texImage
->_BaseFormat
) {
595 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
597 ctx
->Pack
.SwapBytes
);
601 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
602 const GLint bytesPerRow
= texImage
->Width
* bpp
;
604 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
605 texImage
->Height
, format
, type
, 0, 0);
606 const GLint dstRowStride
=
607 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
611 /* map src texture buffer */
612 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
613 0, 0, texImage
->Width
, texImage
->Height
,
614 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
617 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
618 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
622 for (row
= 0; row
< texImage
->Height
; row
++) {
623 memcpy(dst
, src
, bytesPerRow
);
629 /* unmap src texture buffer */
630 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
633 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
642 * This is the software fallback for Driver.GetTexImage().
643 * All error checking will have been done before this routine is called.
644 * We'll call ctx->Driver.MapTextureImage() to access the data, then
645 * unmap with ctx->Driver.UnmapTextureImage().
648 _mesa_GetTexImage_sw(struct gl_context
*ctx
,
649 GLenum format
, GLenum type
, GLvoid
*pixels
,
650 struct gl_texture_image
*texImage
)
652 const GLuint dimensions
=
653 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
655 /* map dest buffer, if PBO */
656 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
657 /* Packing texture image into a PBO.
658 * Map the (potentially) VRAM-based buffer into our process space so
659 * we can write into it with the code below.
660 * A hardware driver might use a sophisticated blit to move the
661 * texture data to the PBO if the PBO is in VRAM along with the texture.
663 GLubyte
*buf
= (GLubyte
*)
664 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
665 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
668 /* out of memory or other unexpected error */
669 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
672 /* <pixels> was an offset into the PBO.
673 * Now make it a real, client-side pointer inside the mapped region.
675 pixels
= ADD_POINTERS(buf
, pixels
);
678 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
681 else if (format
== GL_DEPTH_COMPONENT
) {
682 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
684 else if (format
== GL_DEPTH_STENCIL_EXT
) {
685 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
687 else if (format
== GL_YCBCR_MESA
) {
688 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
691 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
694 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
695 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
702 * This is the software fallback for Driver.GetCompressedTexImage().
703 * All error checking will have been done before this routine is called.
706 _mesa_GetCompressedTexImage_sw(struct gl_context
*ctx
,
707 struct gl_texture_image
*texImage
,
710 const GLuint dimensions
=
711 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
712 struct compressed_pixelstore store
;
716 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
717 texImage
->Width
, texImage
->Height
,
722 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
723 /* pack texture image into a PBO */
725 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
726 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
729 /* out of memory or other unexpected error */
730 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
731 "glGetCompresssedTexImage(map PBO failed)");
734 dest
= ADD_POINTERS(dest
, img
);
739 dest
+= store
.SkipBytes
;
741 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
745 /* map src texture buffer */
746 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
747 0, 0, texImage
->Width
, texImage
->Height
,
748 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
752 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
753 memcpy(dest
, src
, store
.CopyBytesPerRow
);
754 dest
+= store
.TotalBytesPerRow
;
758 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
760 /* Advance to next slice */
761 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
764 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
768 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
769 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
775 * Validate the texture target enum supplied to glGetTex(ture)Image or
776 * glGetCompressedTex(ture)Image.
779 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
786 case GL_TEXTURE_RECTANGLE_NV
:
787 return ctx
->Extensions
.NV_texture_rectangle
;
788 case GL_TEXTURE_1D_ARRAY_EXT
:
789 case GL_TEXTURE_2D_ARRAY_EXT
:
790 return ctx
->Extensions
.EXT_texture_array
;
791 case GL_TEXTURE_CUBE_MAP_ARRAY
:
792 return ctx
->Extensions
.ARB_texture_cube_map_array
;
794 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
796 * "An INVALID_ENUM error is generated if the effective target is not
797 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
798 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
799 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
800 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
802 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
803 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
804 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
805 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
806 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
807 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
808 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
809 case GL_TEXTURE_CUBE_MAP
:
810 return dsa
? GL_TRUE
: GL_FALSE
;
818 * Do error checking for a glGetTex(ture)Image() call.
819 * \return GL_TRUE if any error, GL_FALSE if no errors.
822 getteximage_error_check(struct gl_context
*ctx
,
823 struct gl_texture_image
*texImage
,
824 GLenum target
, GLint level
,
825 GLenum format
, GLenum type
, GLsizei clientMemSize
,
826 GLvoid
*pixels
, bool dsa
)
828 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
829 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
831 const char *suffix
= dsa
? "ture" : "";
834 assert(maxLevels
!= 0);
835 if (level
< 0 || level
>= maxLevels
) {
836 _mesa_error(ctx
, GL_INVALID_VALUE
,
837 "glGetTex%sImage(level out of range)", suffix
);
842 * Format and type checking has been moved up to GetnTexImage and
843 * GetTextureImage so that it happens before getting the texImage object.
846 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
848 /* Make sure the requested image format is compatible with the
851 if (_mesa_is_color_format(format
)
852 && !_mesa_is_color_format(baseFormat
)) {
853 _mesa_error(ctx
, GL_INVALID_OPERATION
,
854 "glGetTex%sImage(format mismatch)", suffix
);
857 else if (_mesa_is_depth_format(format
)
858 && !_mesa_is_depth_format(baseFormat
)
859 && !_mesa_is_depthstencil_format(baseFormat
)) {
860 _mesa_error(ctx
, GL_INVALID_OPERATION
,
861 "glGetTex%sImage(format mismatch)", suffix
);
864 else if (_mesa_is_stencil_format(format
)
865 && !ctx
->Extensions
.ARB_texture_stencil8
) {
866 _mesa_error(ctx
, GL_INVALID_ENUM
,
867 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix
);
870 else if (_mesa_is_ycbcr_format(format
)
871 && !_mesa_is_ycbcr_format(baseFormat
)) {
872 _mesa_error(ctx
, GL_INVALID_OPERATION
,
873 "glGetTex%sImage(format mismatch)", suffix
);
876 else if (_mesa_is_depthstencil_format(format
)
877 && !_mesa_is_depthstencil_format(baseFormat
)) {
878 _mesa_error(ctx
, GL_INVALID_OPERATION
,
879 "glGetTex%sImage(format mismatch)", suffix
);
882 else if (_mesa_is_enum_format_integer(format
) !=
883 _mesa_is_format_integer(texImage
->TexFormat
)) {
884 _mesa_error(ctx
, GL_INVALID_OPERATION
,
885 "glGetTex%sImage(format mismatch)", suffix
);
889 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
890 texImage
->Height
, texImage
->Depth
,
891 format
, type
, clientMemSize
, pixels
)) {
892 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
893 _mesa_error(ctx
, GL_INVALID_OPERATION
,
894 "glGetTex%sImage(out of bounds PBO access)", suffix
);
896 _mesa_error(ctx
, GL_INVALID_OPERATION
,
897 "%s(out of bounds access:"
898 " bufSize (%d) is too small)",
899 dsa
? "glGetTextureImage" : "glGetnTexImageARB",
905 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
906 /* PBO should not be mapped */
907 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
908 _mesa_error(ctx
, GL_INVALID_OPERATION
,
909 "glGetTex%sImage(PBO is mapped)", suffix
);
919 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
922 * Requires caller to pass in texImage object because _mesa_GetTextureImage
923 * must handle the GL_TEXTURE_CUBE_MAP target.
925 * \param target texture target.
926 * \param level image level.
927 * \param format pixel data format for returned image.
928 * \param type pixel data type for returned image.
929 * \param bufSize size of the pixels data buffer.
930 * \param pixels returned pixel data.
931 * \param dsa True when the caller is an ARB_direct_state_access function,
935 _mesa_get_texture_image(struct gl_context
*ctx
,
936 struct gl_texture_object
*texObj
,
937 struct gl_texture_image
*texImage
, GLenum target
,
938 GLint level
, GLenum format
, GLenum type
,
939 GLsizei bufSize
, GLvoid
*pixels
, bool dsa
)
944 FLUSH_VERTICES(ctx
, 0);
947 * Legal target checking has been moved up to GetnTexImage and
948 * GetTextureImage so that it can be caught before receiving a NULL
949 * texImage object and exiting.
952 if (getteximage_error_check(ctx
, texImage
, target
, level
, format
,
953 type
, bufSize
, pixels
, dsa
)) {
957 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
958 /* not an error, do nothing */
962 if (_mesa_is_zero_size_texture(texImage
))
965 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
966 _mesa_debug(ctx
, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
967 " dstFmt=0x%x, dstType=0x%x\n",
970 _mesa_get_format_name(texImage
->TexFormat
),
971 texImage
->Width
, texImage
->Height
,
975 _mesa_lock_texture(ctx
, texObj
);
977 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
979 _mesa_unlock_texture(ctx
, texObj
);
983 * Get texture image. Called by glGetTexImage.
985 * \param target texture target.
986 * \param level image level.
987 * \param format pixel data format for returned image.
988 * \param type pixel data type for returned image.
989 * \param bufSize size of the pixels data buffer.
990 * \param pixels returned pixel data.
993 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
,
994 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
996 struct gl_texture_object
*texObj
;
997 struct gl_texture_image
*texImage
;
999 GET_CURRENT_CONTEXT(ctx
);
1002 * This has been moved here because a format/type mismatch can cause a NULL
1003 * texImage object, which in turn causes the mismatch error to be
1006 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1007 if (err
!= GL_NO_ERROR
) {
1008 _mesa_error(ctx
, err
, "glGetnTexImage(format/type)");
1013 * Legal target checking has been moved here to prevent exiting with a NULL
1016 if (!legal_getteximage_target(ctx
, target
, false)) {
1017 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetnTexImage(target=0x%x)",
1022 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1026 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1030 _mesa_get_texture_image(ctx
, texObj
, texImage
, target
, level
, format
, type
,
1031 bufSize
, pixels
, false);
1036 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1037 GLenum type
, GLvoid
*pixels
)
1039 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
1043 * Get texture image.
1045 * \param texture texture name.
1046 * \param level image level.
1047 * \param format pixel data format for returned image.
1048 * \param type pixel data type for returned image.
1049 * \param bufSize size of the pixels data buffer.
1050 * \param pixels returned pixel data.
1053 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
,
1054 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1056 struct gl_texture_object
*texObj
;
1057 struct gl_texture_image
*texImage
;
1061 GET_CURRENT_CONTEXT(ctx
);
1064 * This has been moved here because a format/type mismatch can cause a NULL
1065 * texImage object, which in turn causes the mismatch error to be
1068 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1069 if (err
!= GL_NO_ERROR
) {
1070 _mesa_error(ctx
, err
, "glGetTextureImage(format/type)");
1074 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glGetTextureImage");
1079 * Legal target checking has been moved here to prevent exiting with a NULL
1082 if (!legal_getteximage_target(ctx
, texObj
->Target
, true)) {
1083 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTextureImage(target=%s)",
1084 _mesa_lookup_enum_by_nr(texObj
->Target
));
1088 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1089 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1091 /* Error checking */
1092 if (texObj
->NumLayers
< 6) {
1093 /* Not enough image planes for a cube map. The spec does not say
1094 * what should happen in this case because the user has always
1095 * specified each cube face separately (using
1096 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
1097 * This is addressed in Khronos Bug 13223.
1099 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1100 "glGetTextureImage(insufficient cube map storage)");
1105 * What do we do if the user created a texture with the following code
1106 * and then called this function with its handle?
1109 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
1110 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
1111 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
1112 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
1113 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
1114 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
1115 * // wrong format, or given the wrong size, etc.
1116 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
1117 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
1119 * A bug has been filed against the spec for this case. In the
1120 * meantime, we will check for cube completeness.
1122 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
1123 * Core Profile spec (30.10.2014):
1124 * "[A] cube map texture is cube complete if the
1125 * following conditions all hold true: The [base level] texture
1126 * images of each of the six cube map faces have identical, positive,
1127 * and square dimensions. The [base level] images were each specified
1128 * with the same internal format."
1130 * It seems reasonable to check for cube completeness of an arbitrary
1131 * level here so that the returned data has a consistent format and size
1132 * and therefore fits in the user's buffer.
1134 if (!_mesa_cube_level_complete(texObj
, level
)) {
1135 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1136 "glGetTextureImage(cube map incomplete)");
1140 /* Copy each face. */
1141 for (i
= 0; i
< 6; ++i
) {
1142 texImage
= texObj
->Image
[i
][level
];
1143 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1144 format
, type
, bufSize
, pixels
, true);
1146 image_stride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
1147 texImage
->Height
, format
,
1149 pixels
= (GLubyte
*) pixels
+ image_stride
;
1150 bufSize
-= image_stride
;
1154 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1158 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1159 format
, type
, bufSize
, pixels
, true);
1164 * Do error checking for a glGetCompressedTexImage() call.
1165 * \return GL_TRUE if any error, GL_FALSE if no errors.
1168 getcompressedteximage_error_check(struct gl_context
*ctx
,
1169 struct gl_texture_image
*texImage
,
1171 GLint level
, GLsizei clientMemSize
,
1172 GLvoid
*img
, bool dsa
)
1174 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1175 GLuint compressedSize
, dimensions
;
1176 const char *suffix
= dsa
? "ture" : "";
1180 if (!legal_getteximage_target(ctx
, target
, dsa
)) {
1181 _mesa_error(ctx
, GL_INVALID_ENUM
,
1182 "glGetCompressedTex%sImage(target=%s)", suffix
,
1183 _mesa_lookup_enum_by_nr(target
));
1187 assert(maxLevels
!= 0);
1188 if (level
< 0 || level
>= maxLevels
) {
1189 _mesa_error(ctx
, GL_INVALID_VALUE
,
1190 "glGetCompressedTex%sImage(bad level = %d)", suffix
, level
);
1194 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1195 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1196 "glGetCompressedTex%sImage(texture is not compressed)",
1201 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
1206 /* Check for invalid pixel storage modes */
1207 dimensions
= _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
1208 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1210 "glGetCompressedTextureImage":
1211 "glGetCompressedTexImage")) {
1215 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1216 /* do bounds checking on writing to client memory */
1217 if (clientMemSize
< (GLsizei
) compressedSize
) {
1218 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1219 "%s(out of bounds access: bufSize (%d) is too small)",
1220 dsa
? "glGetCompressedTextureImage" :
1221 "glGetnCompressedTexImageARB", clientMemSize
);
1225 /* do bounds checking on PBO write */
1226 if ((const GLubyte
*) img
+ compressedSize
>
1227 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1228 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1229 "glGetCompressedTex%sImage(out of bounds PBO access)",
1234 /* make sure PBO is not mapped */
1235 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1236 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1237 "glGetCompressedTex%sImage(PBO is mapped)", suffix
);
1245 /** Implements glGetnCompressedTexImageARB, glGetCompressedTexImage, and
1246 * glGetCompressedTextureImage.
1248 * texImage must be passed in because glGetCompressedTexImage must handle the
1249 * target GL_TEXTURE_CUBE_MAP.
1252 _mesa_get_compressed_texture_image(struct gl_context
*ctx
,
1253 struct gl_texture_object
*texObj
,
1254 struct gl_texture_image
*texImage
,
1255 GLenum target
, GLint level
,
1256 GLsizei bufSize
, GLvoid
*pixels
,
1262 FLUSH_VERTICES(ctx
, 0);
1264 if (getcompressedteximage_error_check(ctx
, texImage
, target
, level
,
1265 bufSize
, pixels
, dsa
)) {
1269 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1270 /* not an error, do nothing */
1274 if (_mesa_is_zero_size_texture(texImage
))
1277 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1279 "glGetCompressedTex%sImage(tex %u) format = %s, w=%d, h=%d\n",
1280 dsa
? "ture" : "", texObj
->Name
,
1281 _mesa_get_format_name(texImage
->TexFormat
),
1282 texImage
->Width
, texImage
->Height
);
1285 _mesa_lock_texture(ctx
, texObj
);
1287 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, pixels
);
1289 _mesa_unlock_texture(ctx
, texObj
);
1293 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1296 struct gl_texture_object
*texObj
;
1297 struct gl_texture_image
*texImage
;
1298 GET_CURRENT_CONTEXT(ctx
);
1300 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1304 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1308 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
, target
, level
,
1309 bufSize
, img
, false);
1313 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1315 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);
1319 * Get compressed texture image.
1321 * \param texture texture name.
1322 * \param level image level.
1323 * \param bufSize size of the pixels data buffer.
1324 * \param pixels returned pixel data.
1327 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1328 GLsizei bufSize
, GLvoid
*pixels
)
1330 struct gl_texture_object
*texObj
;
1331 struct gl_texture_image
*texImage
;
1334 GET_CURRENT_CONTEXT(ctx
);
1336 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
1337 "glGetCompressedTextureImage");
1341 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1342 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1343 assert(texObj
->NumLayers
>= 6);
1345 /* Copy each face. */
1346 for (i
= 0; i
< 6; ++i
) {
1347 texImage
= texObj
->Image
[i
][level
];
1351 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1352 texObj
->Target
, level
,
1353 bufSize
, pixels
, true);
1355 /* Compressed images don't have a client format */
1356 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
1358 texImage
->Height
, 1);
1360 pixels
= (GLubyte
*) pixels
+ image_stride
;
1361 bufSize
-= image_stride
;
1365 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1369 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1370 texObj
->Target
, level
, bufSize
,