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
= _mesa_is_enum_format_integer(format
);
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
));
427 for (img
= 0; img
< depth
; img
++) {
436 /* map src texture buffer */
437 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
438 0, 0, width
, height
, GL_MAP_READ_BIT
,
439 &srcMap
, &rowstride
);
441 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
446 dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
447 width
, height
, format
, type
,
451 uint32_t rgba_format
;
453 bool need_convert
= false;
455 /* We will convert to RGBA float */
456 rgba_format
= RGBA32_FLOAT
;
457 rgba_stride
= width
* 4 * sizeof(GLfloat
);
459 /* If we are lucky and the dst format matches the RGBA format we need
460 * to convert to, then we can convert directly into the dst buffer
461 * and avoid the final conversion/copy from the rgba buffer to the dst
464 if (format
== rgba_format
) {
466 } else if (rgba
== NULL
) { /* Allocate the RGBA buffer only once */
468 rgba
= malloc(height
* rgba_stride
);
470 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
471 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
476 _mesa_format_convert(rgba
, rgba_format
, rgba_stride
,
477 img_src
, texFormat
, rowstride
,
479 needsRebase
? rebaseSwizzle
: NULL
);
481 /* Handle transfer ops now */
482 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
* height
, rgba
);
484 /* If we had to rebase, we have already handled that */
487 /* If we were lucky and our RGBA conversion matches the dst format, then
493 /* Otherwise, we need to convert from RGBA to dst next */
495 src_format
= rgba_format
;
496 src_stride
= rgba_stride
;
498 /* No RGBA conversion needed, convert directly to dst */
500 src_format
= texFormat
;
501 src_stride
= rowstride
;
504 /* Do the conversion to destination format */
505 _mesa_format_convert(dest
, dst_format
, dst_stride
,
506 src
, src_format
, src_stride
,
508 needsRebase
? rebaseSwizzle
: NULL
);
511 /* Handle byte swapping if required */
512 if (ctx
->Pack
.SwapBytes
) {
513 GLint swapSize
= _mesa_sizeof_packed_type(type
);
514 if (swapSize
== 2 || swapSize
== 4) {
515 int swapsPerPixel
= _mesa_bytes_per_pixel(format
, type
) / swapSize
;
516 assert(_mesa_bytes_per_pixel(format
, type
) % swapSize
== 0);
518 _mesa_swap2((GLushort
*) dest
, width
* height
* swapsPerPixel
);
519 else if (swapSize
== 4)
520 _mesa_swap4((GLuint
*) dest
, width
* height
* swapsPerPixel
);
524 /* Unmap the src texture buffer */
525 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
535 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
536 * Compressed textures are handled here as well.
539 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
540 GLenum format
, GLenum type
, GLvoid
*pixels
,
541 struct gl_texture_image
*texImage
)
543 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
544 GLbitfield transferOps
= 0x0;
546 /* In general, clamping does not apply to glGetTexImage, except when
547 * the returned type of the image can't hold negative values.
549 if (type_needs_clamping(type
)) {
550 /* the returned image type can't have negative values */
551 if (dataType
== GL_FLOAT
||
552 dataType
== GL_HALF_FLOAT
||
553 dataType
== GL_SIGNED_NORMALIZED
||
554 format
== GL_LUMINANCE
||
555 format
== GL_LUMINANCE_ALPHA
) {
556 transferOps
|= IMAGE_CLAMP_BIT
;
560 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
561 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
562 pixels
, texImage
, transferOps
);
565 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
566 pixels
, texImage
, transferOps
);
572 * Try to do glGetTexImage() with simple memcpy().
573 * \return GL_TRUE if done, GL_FALSE otherwise
576 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
578 struct gl_texture_image
*texImage
)
580 const GLenum target
= texImage
->TexObject
->Target
;
581 GLboolean memCopy
= GL_FALSE
;
582 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
585 * Check if we can use memcpy to copy from the hardware texture
586 * format to the user's format/type.
587 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
589 if ((target
== GL_TEXTURE_1D
||
590 target
== GL_TEXTURE_2D
||
591 target
== GL_TEXTURE_RECTANGLE
||
592 _mesa_is_cube_face(target
)) &&
593 texBaseFormat
== texImage
->_BaseFormat
) {
594 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
596 ctx
->Pack
.SwapBytes
);
600 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
601 const GLint bytesPerRow
= texImage
->Width
* bpp
;
603 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
604 texImage
->Height
, format
, type
, 0, 0);
605 const GLint dstRowStride
=
606 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
610 /* map src texture buffer */
611 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
612 0, 0, texImage
->Width
, texImage
->Height
,
613 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
616 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
617 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
621 for (row
= 0; row
< texImage
->Height
; row
++) {
622 memcpy(dst
, src
, bytesPerRow
);
628 /* unmap src texture buffer */
629 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
632 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
641 * This is the software fallback for Driver.GetTexImage().
642 * All error checking will have been done before this routine is called.
643 * We'll call ctx->Driver.MapTextureImage() to access the data, then
644 * unmap with ctx->Driver.UnmapTextureImage().
647 _mesa_GetTexImage_sw(struct gl_context
*ctx
,
648 GLenum format
, GLenum type
, GLvoid
*pixels
,
649 struct gl_texture_image
*texImage
)
651 const GLuint dimensions
=
652 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
654 /* map dest buffer, if PBO */
655 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
656 /* Packing texture image into a PBO.
657 * Map the (potentially) VRAM-based buffer into our process space so
658 * we can write into it with the code below.
659 * A hardware driver might use a sophisticated blit to move the
660 * texture data to the PBO if the PBO is in VRAM along with the texture.
662 GLubyte
*buf
= (GLubyte
*)
663 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
664 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
667 /* out of memory or other unexpected error */
668 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
671 /* <pixels> was an offset into the PBO.
672 * Now make it a real, client-side pointer inside the mapped region.
674 pixels
= ADD_POINTERS(buf
, pixels
);
677 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
680 else if (format
== GL_DEPTH_COMPONENT
) {
681 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
683 else if (format
== GL_DEPTH_STENCIL_EXT
) {
684 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
686 else if (format
== GL_YCBCR_MESA
) {
687 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
690 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
693 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
694 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
701 * This is the software fallback for Driver.GetCompressedTexImage().
702 * All error checking will have been done before this routine is called.
705 _mesa_GetCompressedTexImage_sw(struct gl_context
*ctx
,
706 struct gl_texture_image
*texImage
,
709 const GLuint dimensions
=
710 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
711 struct compressed_pixelstore store
;
715 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
716 texImage
->Width
, texImage
->Height
,
721 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
722 /* pack texture image into a PBO */
724 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
725 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
728 /* out of memory or other unexpected error */
729 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
730 "glGetCompresssedTexImage(map PBO failed)");
733 dest
= ADD_POINTERS(dest
, img
);
738 dest
+= store
.SkipBytes
;
740 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
744 /* map src texture buffer */
745 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
746 0, 0, texImage
->Width
, texImage
->Height
,
747 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
751 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
752 memcpy(dest
, src
, store
.CopyBytesPerRow
);
753 dest
+= store
.TotalBytesPerRow
;
757 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
759 /* Advance to next slice */
760 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
763 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
767 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
768 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
774 * Validate the texture target enum supplied to glGetTex(ture)Image or
775 * glGetCompressedTex(ture)Image.
778 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
785 case GL_TEXTURE_RECTANGLE_NV
:
786 return ctx
->Extensions
.NV_texture_rectangle
;
787 case GL_TEXTURE_1D_ARRAY_EXT
:
788 case GL_TEXTURE_2D_ARRAY_EXT
:
789 return ctx
->Extensions
.EXT_texture_array
;
790 case GL_TEXTURE_CUBE_MAP_ARRAY
:
791 return ctx
->Extensions
.ARB_texture_cube_map_array
;
793 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
795 * "An INVALID_ENUM error is generated if the effective target is not
796 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
797 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
798 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
799 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
801 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
802 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
803 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
804 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
805 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
806 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
807 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
808 case GL_TEXTURE_CUBE_MAP
:
809 return dsa
? GL_TRUE
: GL_FALSE
;
817 * Do error checking for a glGetTex(ture)Image() call.
818 * \return GL_TRUE if any error, GL_FALSE if no errors.
821 getteximage_error_check(struct gl_context
*ctx
,
822 struct gl_texture_image
*texImage
,
823 GLenum target
, GLint level
,
824 GLenum format
, GLenum type
, GLsizei clientMemSize
,
825 GLvoid
*pixels
, bool dsa
)
827 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
828 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
830 const char *suffix
= dsa
? "ture" : "";
833 assert(maxLevels
!= 0);
834 if (level
< 0 || level
>= maxLevels
) {
835 _mesa_error(ctx
, GL_INVALID_VALUE
,
836 "glGetTex%sImage(level out of range)", suffix
);
841 * Format and type checking has been moved up to GetnTexImage and
842 * GetTextureImage so that it happens before getting the texImage object.
845 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
847 /* Make sure the requested image format is compatible with the
850 if (_mesa_is_color_format(format
)
851 && !_mesa_is_color_format(baseFormat
)) {
852 _mesa_error(ctx
, GL_INVALID_OPERATION
,
853 "glGetTex%sImage(format mismatch)", suffix
);
856 else if (_mesa_is_depth_format(format
)
857 && !_mesa_is_depth_format(baseFormat
)
858 && !_mesa_is_depthstencil_format(baseFormat
)) {
859 _mesa_error(ctx
, GL_INVALID_OPERATION
,
860 "glGetTex%sImage(format mismatch)", suffix
);
863 else if (_mesa_is_stencil_format(format
)
864 && !ctx
->Extensions
.ARB_texture_stencil8
) {
865 _mesa_error(ctx
, GL_INVALID_ENUM
,
866 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix
);
869 else if (_mesa_is_ycbcr_format(format
)
870 && !_mesa_is_ycbcr_format(baseFormat
)) {
871 _mesa_error(ctx
, GL_INVALID_OPERATION
,
872 "glGetTex%sImage(format mismatch)", suffix
);
875 else if (_mesa_is_depthstencil_format(format
)
876 && !_mesa_is_depthstencil_format(baseFormat
)) {
877 _mesa_error(ctx
, GL_INVALID_OPERATION
,
878 "glGetTex%sImage(format mismatch)", suffix
);
881 else if (_mesa_is_enum_format_integer(format
) !=
882 _mesa_is_format_integer(texImage
->TexFormat
)) {
883 _mesa_error(ctx
, GL_INVALID_OPERATION
,
884 "glGetTex%sImage(format mismatch)", suffix
);
888 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
889 texImage
->Height
, texImage
->Depth
,
890 format
, type
, clientMemSize
, pixels
)) {
891 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
892 _mesa_error(ctx
, GL_INVALID_OPERATION
,
893 "glGetTex%sImage(out of bounds PBO access)", suffix
);
895 _mesa_error(ctx
, GL_INVALID_OPERATION
,
896 "%s(out of bounds access:"
897 " bufSize (%d) is too small)",
898 dsa
? "glGetTextureImage" : "glGetnTexImageARB",
904 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
905 /* PBO should not be mapped */
906 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
907 _mesa_error(ctx
, GL_INVALID_OPERATION
,
908 "glGetTex%sImage(PBO is mapped)", suffix
);
918 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
921 * Requires caller to pass in texImage object because _mesa_GetTextureImage
922 * must handle the GL_TEXTURE_CUBE_MAP target.
924 * \param target texture target.
925 * \param level image level.
926 * \param format pixel data format for returned image.
927 * \param type pixel data type for returned image.
928 * \param bufSize size of the pixels data buffer.
929 * \param pixels returned pixel data.
930 * \param dsa True when the caller is an ARB_direct_state_access function,
934 _mesa_get_texture_image(struct gl_context
*ctx
,
935 struct gl_texture_object
*texObj
,
936 struct gl_texture_image
*texImage
, GLenum target
,
937 GLint level
, GLenum format
, GLenum type
,
938 GLsizei bufSize
, GLvoid
*pixels
, bool dsa
)
943 FLUSH_VERTICES(ctx
, 0);
946 * Legal target checking has been moved up to GetnTexImage and
947 * GetTextureImage so that it can be caught before receiving a NULL
948 * texImage object and exiting.
951 if (getteximage_error_check(ctx
, texImage
, target
, level
, format
,
952 type
, bufSize
, pixels
, dsa
)) {
956 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
957 /* not an error, do nothing */
961 if (_mesa_is_zero_size_texture(texImage
))
964 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
965 _mesa_debug(ctx
, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
966 " dstFmt=0x%x, dstType=0x%x\n",
969 _mesa_get_format_name(texImage
->TexFormat
),
970 texImage
->Width
, texImage
->Height
,
974 _mesa_lock_texture(ctx
, texObj
);
976 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
978 _mesa_unlock_texture(ctx
, texObj
);
982 * Get texture image. Called by glGetTexImage.
984 * \param target texture target.
985 * \param level image level.
986 * \param format pixel data format for returned image.
987 * \param type pixel data type for returned image.
988 * \param bufSize size of the pixels data buffer.
989 * \param pixels returned pixel data.
992 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
,
993 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
995 struct gl_texture_object
*texObj
;
996 struct gl_texture_image
*texImage
;
998 GET_CURRENT_CONTEXT(ctx
);
1001 * This has been moved here because a format/type mismatch can cause a NULL
1002 * texImage object, which in turn causes the mismatch error to be
1005 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1006 if (err
!= GL_NO_ERROR
) {
1007 _mesa_error(ctx
, err
, "glGetnTexImage(format/type)");
1012 * Legal target checking has been moved here to prevent exiting with a NULL
1015 if (!legal_getteximage_target(ctx
, target
, false)) {
1016 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetnTexImage(target=0x%x)",
1021 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1025 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1029 _mesa_get_texture_image(ctx
, texObj
, texImage
, target
, level
, format
, type
,
1030 bufSize
, pixels
, false);
1035 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1036 GLenum type
, GLvoid
*pixels
)
1038 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
1042 * Get texture image.
1044 * \param texture texture name.
1045 * \param level image level.
1046 * \param format pixel data format for returned image.
1047 * \param type pixel data type for returned image.
1048 * \param bufSize size of the pixels data buffer.
1049 * \param pixels returned pixel data.
1052 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
,
1053 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1055 struct gl_texture_object
*texObj
;
1056 struct gl_texture_image
*texImage
;
1060 GET_CURRENT_CONTEXT(ctx
);
1063 * This has been moved here because a format/type mismatch can cause a NULL
1064 * texImage object, which in turn causes the mismatch error to be
1067 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1068 if (err
!= GL_NO_ERROR
) {
1069 _mesa_error(ctx
, err
, "glGetTextureImage(format/type)");
1073 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glGetTextureImage");
1078 * Legal target checking has been moved here to prevent exiting with a NULL
1081 if (!legal_getteximage_target(ctx
, texObj
->Target
, true)) {
1082 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTextureImage(target=%s)",
1083 _mesa_lookup_enum_by_nr(texObj
->Target
));
1087 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1088 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1090 /* Error checking */
1091 if (texObj
->NumLayers
< 6) {
1092 /* Not enough image planes for a cube map. The spec does not say
1093 * what should happen in this case because the user has always
1094 * specified each cube face separately (using
1095 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
1096 * This is addressed in Khronos Bug 13223.
1098 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1099 "glGetTextureImage(insufficient cube map storage)");
1104 * What do we do if the user created a texture with the following code
1105 * and then called this function with its handle?
1108 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
1109 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
1110 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
1111 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
1112 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
1113 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
1114 * // wrong format, or given the wrong size, etc.
1115 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
1116 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
1118 * A bug has been filed against the spec for this case. In the
1119 * meantime, we will check for cube completeness.
1121 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
1122 * Core Profile spec (30.10.2014):
1123 * "[A] cube map texture is cube complete if the
1124 * following conditions all hold true: The [base level] texture
1125 * images of each of the six cube map faces have identical, positive,
1126 * and square dimensions. The [base level] images were each specified
1127 * with the same internal format."
1129 * It seems reasonable to check for cube completeness of an arbitrary
1130 * level here so that the returned data has a consistent format and size
1131 * and therefore fits in the user's buffer.
1133 if (!_mesa_cube_level_complete(texObj
, level
)) {
1134 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1135 "glGetTextureImage(cube map incomplete)");
1139 /* Copy each face. */
1140 for (i
= 0; i
< 6; ++i
) {
1141 texImage
= texObj
->Image
[i
][level
];
1142 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1143 format
, type
, bufSize
, pixels
, true);
1145 image_stride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
1146 texImage
->Height
, format
,
1148 pixels
= (GLubyte
*) pixels
+ image_stride
;
1149 bufSize
-= image_stride
;
1153 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1157 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1158 format
, type
, bufSize
, pixels
, true);
1163 * Do error checking for a glGetCompressedTexImage() call.
1164 * \return GL_TRUE if any error, GL_FALSE if no errors.
1167 getcompressedteximage_error_check(struct gl_context
*ctx
,
1168 struct gl_texture_image
*texImage
,
1170 GLint level
, GLsizei clientMemSize
,
1171 GLvoid
*img
, bool dsa
)
1173 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1174 GLuint compressedSize
, dimensions
;
1175 const char *suffix
= dsa
? "ture" : "";
1179 if (!legal_getteximage_target(ctx
, target
, dsa
)) {
1180 _mesa_error(ctx
, GL_INVALID_ENUM
,
1181 "glGetCompressedTex%sImage(target=%s)", suffix
,
1182 _mesa_lookup_enum_by_nr(target
));
1186 assert(maxLevels
!= 0);
1187 if (level
< 0 || level
>= maxLevels
) {
1188 _mesa_error(ctx
, GL_INVALID_VALUE
,
1189 "glGetCompressedTex%sImage(bad level = %d)", suffix
, level
);
1193 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1194 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1195 "glGetCompressedTex%sImage(texture is not compressed)",
1200 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
1205 /* Check for invalid pixel storage modes */
1206 dimensions
= _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
1207 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1209 "glGetCompressedTextureImage":
1210 "glGetCompressedTexImage")) {
1214 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1215 /* do bounds checking on writing to client memory */
1216 if (clientMemSize
< (GLsizei
) compressedSize
) {
1217 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1218 "%s(out of bounds access: bufSize (%d) is too small)",
1219 dsa
? "glGetCompressedTextureImage" :
1220 "glGetnCompressedTexImageARB", clientMemSize
);
1224 /* do bounds checking on PBO write */
1225 if ((const GLubyte
*) img
+ compressedSize
>
1226 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1227 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1228 "glGetCompressedTex%sImage(out of bounds PBO access)",
1233 /* make sure PBO is not mapped */
1234 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1235 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1236 "glGetCompressedTex%sImage(PBO is mapped)", suffix
);
1244 /** Implements glGetnCompressedTexImageARB, glGetCompressedTexImage, and
1245 * glGetCompressedTextureImage.
1247 * texImage must be passed in because glGetCompressedTexImage must handle the
1248 * target GL_TEXTURE_CUBE_MAP.
1251 _mesa_get_compressed_texture_image(struct gl_context
*ctx
,
1252 struct gl_texture_object
*texObj
,
1253 struct gl_texture_image
*texImage
,
1254 GLenum target
, GLint level
,
1255 GLsizei bufSize
, GLvoid
*pixels
,
1261 FLUSH_VERTICES(ctx
, 0);
1263 if (getcompressedteximage_error_check(ctx
, texImage
, target
, level
,
1264 bufSize
, pixels
, dsa
)) {
1268 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1269 /* not an error, do nothing */
1273 if (_mesa_is_zero_size_texture(texImage
))
1276 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1278 "glGetCompressedTex%sImage(tex %u) format = %s, w=%d, h=%d\n",
1279 dsa
? "ture" : "", texObj
->Name
,
1280 _mesa_get_format_name(texImage
->TexFormat
),
1281 texImage
->Width
, texImage
->Height
);
1284 _mesa_lock_texture(ctx
, texObj
);
1286 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, pixels
);
1288 _mesa_unlock_texture(ctx
, texObj
);
1292 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1295 struct gl_texture_object
*texObj
;
1296 struct gl_texture_image
*texImage
;
1297 GET_CURRENT_CONTEXT(ctx
);
1299 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1303 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1307 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
, target
, level
,
1308 bufSize
, img
, false);
1312 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1314 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);
1318 * Get compressed texture image.
1320 * \param texture texture name.
1321 * \param level image level.
1322 * \param bufSize size of the pixels data buffer.
1323 * \param pixels returned pixel data.
1326 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1327 GLsizei bufSize
, GLvoid
*pixels
)
1329 struct gl_texture_object
*texObj
;
1330 struct gl_texture_image
*texImage
;
1333 GET_CURRENT_CONTEXT(ctx
);
1335 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
1336 "glGetCompressedTextureImage");
1340 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1341 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1342 assert(texObj
->NumLayers
>= 6);
1344 /* Copy each face. */
1345 for (i
= 0; i
< 6; ++i
) {
1346 texImage
= texObj
->Image
[i
][level
];
1350 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1351 texObj
->Target
, level
,
1352 bufSize
, pixels
, true);
1354 /* Compressed images don't have a client format */
1355 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
1357 texImage
->Height
, 1);
1359 pixels
= (GLubyte
*) pixels
+ image_stride
;
1360 bufSize
-= image_stride
;
1364 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1368 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1369 texObj
->Target
, level
, bufSize
,