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
, RGBA8888_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
;
455 /* We will convert to RGBA float */
456 rgba_format
= RGBA8888_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
) {
465 need_convert
= false;
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 int components
= _mesa_components_in_format(format
);
515 GLint swapSize
= _mesa_sizeof_packed_type(type
);
517 _mesa_swap2((GLushort
*) dest
, width
* height
* components
);
518 else if (swapSize
== 4)
519 _mesa_swap4((GLuint
*) dest
, width
* height
* components
);
522 /* Unmap the src texture buffer */
523 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
533 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
534 * Compressed textures are handled here as well.
537 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
538 GLenum format
, GLenum type
, GLvoid
*pixels
,
539 struct gl_texture_image
*texImage
)
541 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
542 GLbitfield transferOps
= 0x0;
544 /* In general, clamping does not apply to glGetTexImage, except when
545 * the returned type of the image can't hold negative values.
547 if (type_needs_clamping(type
)) {
548 /* the returned image type can't have negative values */
549 if (dataType
== GL_FLOAT
||
550 dataType
== GL_HALF_FLOAT
||
551 dataType
== GL_SIGNED_NORMALIZED
||
552 format
== GL_LUMINANCE
||
553 format
== GL_LUMINANCE_ALPHA
) {
554 transferOps
|= IMAGE_CLAMP_BIT
;
558 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
559 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
560 pixels
, texImage
, transferOps
);
563 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
564 pixels
, texImage
, transferOps
);
570 * Try to do glGetTexImage() with simple memcpy().
571 * \return GL_TRUE if done, GL_FALSE otherwise
574 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
576 struct gl_texture_image
*texImage
)
578 const GLenum target
= texImage
->TexObject
->Target
;
579 GLboolean memCopy
= GL_FALSE
;
580 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
583 * Check if we can use memcpy to copy from the hardware texture
584 * format to the user's format/type.
585 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
587 if ((target
== GL_TEXTURE_1D
||
588 target
== GL_TEXTURE_2D
||
589 target
== GL_TEXTURE_RECTANGLE
||
590 _mesa_is_cube_face(target
)) &&
591 texBaseFormat
== texImage
->_BaseFormat
) {
592 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
594 ctx
->Pack
.SwapBytes
);
598 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
599 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
601 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
602 texImage
->Height
, format
, type
, 0, 0);
603 const GLint dstRowStride
=
604 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
608 /* map src texture buffer */
609 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
610 0, 0, texImage
->Width
, texImage
->Height
,
611 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
614 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
615 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
619 for (row
= 0; row
< texImage
->Height
; row
++) {
620 memcpy(dst
, src
, bytesPerRow
);
626 /* unmap src texture buffer */
627 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
630 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
639 * This is the software fallback for Driver.GetTexImage().
640 * All error checking will have been done before this routine is called.
641 * We'll call ctx->Driver.MapTextureImage() to access the data, then
642 * unmap with ctx->Driver.UnmapTextureImage().
645 _mesa_GetTexImage_sw(struct gl_context
*ctx
,
646 GLenum format
, GLenum type
, GLvoid
*pixels
,
647 struct gl_texture_image
*texImage
)
649 const GLuint dimensions
=
650 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
652 /* map dest buffer, if PBO */
653 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
654 /* Packing texture image into a PBO.
655 * Map the (potentially) VRAM-based buffer into our process space so
656 * we can write into it with the code below.
657 * A hardware driver might use a sophisticated blit to move the
658 * texture data to the PBO if the PBO is in VRAM along with the texture.
660 GLubyte
*buf
= (GLubyte
*)
661 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
662 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
665 /* out of memory or other unexpected error */
666 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
669 /* <pixels> was an offset into the PBO.
670 * Now make it a real, client-side pointer inside the mapped region.
672 pixels
= ADD_POINTERS(buf
, pixels
);
675 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
678 else if (format
== GL_DEPTH_COMPONENT
) {
679 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
681 else if (format
== GL_DEPTH_STENCIL_EXT
) {
682 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
684 else if (format
== GL_YCBCR_MESA
) {
685 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
688 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
691 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
692 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
699 * This is the software fallback for Driver.GetCompressedTexImage().
700 * All error checking will have been done before this routine is called.
703 _mesa_GetCompressedTexImage_sw(struct gl_context
*ctx
,
704 struct gl_texture_image
*texImage
,
707 const GLuint dimensions
=
708 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
709 struct compressed_pixelstore store
;
713 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
714 texImage
->Width
, texImage
->Height
,
719 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
720 /* pack texture image into a PBO */
722 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
723 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
726 /* out of memory or other unexpected error */
727 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
728 "glGetCompresssedTexImage(map PBO failed)");
731 dest
= ADD_POINTERS(dest
, img
);
736 dest
+= store
.SkipBytes
;
738 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
742 /* map src texture buffer */
743 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
744 0, 0, texImage
->Width
, texImage
->Height
,
745 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
749 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
750 memcpy(dest
, src
, store
.CopyBytesPerRow
);
751 dest
+= store
.TotalBytesPerRow
;
755 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
757 /* Advance to next slice */
758 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
761 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
765 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
766 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
772 * Validate the texture target enum supplied to glGetTex(ture)Image or
773 * glGetCompressedTex(ture)Image.
776 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
783 case GL_TEXTURE_RECTANGLE_NV
:
784 return ctx
->Extensions
.NV_texture_rectangle
;
785 case GL_TEXTURE_1D_ARRAY_EXT
:
786 case GL_TEXTURE_2D_ARRAY_EXT
:
787 return ctx
->Extensions
.EXT_texture_array
;
788 case GL_TEXTURE_CUBE_MAP_ARRAY
:
789 return ctx
->Extensions
.ARB_texture_cube_map_array
;
791 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
793 * "An INVALID_ENUM error is generated if the effective target is not
794 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
795 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
796 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
797 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
799 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
800 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
801 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
802 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
803 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
804 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
805 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
806 case GL_TEXTURE_CUBE_MAP
:
807 return dsa
? GL_TRUE
: GL_FALSE
;
815 * Do error checking for a glGetTex(ture)Image() call.
816 * \return GL_TRUE if any error, GL_FALSE if no errors.
819 getteximage_error_check(struct gl_context
*ctx
,
820 struct gl_texture_image
*texImage
,
821 GLenum target
, GLint level
,
822 GLenum format
, GLenum type
, GLsizei clientMemSize
,
823 GLvoid
*pixels
, bool dsa
)
825 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
826 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
828 const char *suffix
= dsa
? "ture" : "";
831 assert(maxLevels
!= 0);
832 if (level
< 0 || level
>= maxLevels
) {
833 _mesa_error(ctx
, GL_INVALID_VALUE
,
834 "glGetTex%sImage(level out of range)", suffix
);
839 * Format and type checking has been moved up to GetnTexImage and
840 * GetTextureImage so that it happens before getting the texImage object.
843 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
845 /* Make sure the requested image format is compatible with the
848 if (_mesa_is_color_format(format
)
849 && !_mesa_is_color_format(baseFormat
)) {
850 _mesa_error(ctx
, GL_INVALID_OPERATION
,
851 "glGetTex%sImage(format mismatch)", suffix
);
854 else if (_mesa_is_depth_format(format
)
855 && !_mesa_is_depth_format(baseFormat
)
856 && !_mesa_is_depthstencil_format(baseFormat
)) {
857 _mesa_error(ctx
, GL_INVALID_OPERATION
,
858 "glGetTex%sImage(format mismatch)", suffix
);
861 else if (_mesa_is_stencil_format(format
)
862 && !ctx
->Extensions
.ARB_texture_stencil8
) {
863 _mesa_error(ctx
, GL_INVALID_ENUM
,
864 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix
);
867 else if (_mesa_is_ycbcr_format(format
)
868 && !_mesa_is_ycbcr_format(baseFormat
)) {
869 _mesa_error(ctx
, GL_INVALID_OPERATION
,
870 "glGetTex%sImage(format mismatch)", suffix
);
873 else if (_mesa_is_depthstencil_format(format
)
874 && !_mesa_is_depthstencil_format(baseFormat
)) {
875 _mesa_error(ctx
, GL_INVALID_OPERATION
,
876 "glGetTex%sImage(format mismatch)", suffix
);
879 else if (_mesa_is_enum_format_integer(format
) !=
880 _mesa_is_format_integer(texImage
->TexFormat
)) {
881 _mesa_error(ctx
, GL_INVALID_OPERATION
,
882 "glGetTex%sImage(format mismatch)", suffix
);
886 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
887 texImage
->Height
, texImage
->Depth
,
888 format
, type
, clientMemSize
, pixels
)) {
889 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
890 _mesa_error(ctx
, GL_INVALID_OPERATION
,
891 "glGetTex%sImage(out of bounds PBO access)", suffix
);
893 _mesa_error(ctx
, GL_INVALID_OPERATION
,
894 "%s(out of bounds access:"
895 " bufSize (%d) is too small)",
896 dsa
? "glGetTextureImage" : "glGetnTexImageARB",
902 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
903 /* PBO should not be mapped */
904 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
905 _mesa_error(ctx
, GL_INVALID_OPERATION
,
906 "glGetTex%sImage(PBO is mapped)", suffix
);
916 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
919 * Requires caller to pass in texImage object because _mesa_GetTextureImage
920 * must handle the GL_TEXTURE_CUBE_MAP target.
922 * \param target texture target.
923 * \param level image level.
924 * \param format pixel data format for returned image.
925 * \param type pixel data type for returned image.
926 * \param bufSize size of the pixels data buffer.
927 * \param pixels returned pixel data.
928 * \param dsa True when the caller is an ARB_direct_state_access function,
932 _mesa_get_texture_image(struct gl_context
*ctx
,
933 struct gl_texture_object
*texObj
,
934 struct gl_texture_image
*texImage
, GLenum target
,
935 GLint level
, GLenum format
, GLenum type
,
936 GLsizei bufSize
, GLvoid
*pixels
, bool dsa
)
941 FLUSH_VERTICES(ctx
, 0);
944 * Legal target checking has been moved up to GetnTexImage and
945 * GetTextureImage so that it can be caught before receiving a NULL
946 * texImage object and exiting.
949 if (getteximage_error_check(ctx
, texImage
, target
, level
, format
,
950 type
, bufSize
, pixels
, dsa
)) {
954 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
955 /* not an error, do nothing */
959 if (_mesa_is_zero_size_texture(texImage
))
962 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
963 _mesa_debug(ctx
, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
964 " dstFmt=0x%x, dstType=0x%x\n",
967 _mesa_get_format_name(texImage
->TexFormat
),
968 texImage
->Width
, texImage
->Height
,
972 _mesa_lock_texture(ctx
, texObj
);
974 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
976 _mesa_unlock_texture(ctx
, texObj
);
980 * Get texture image. Called by glGetTexImage.
982 * \param target texture target.
983 * \param level image level.
984 * \param format pixel data format for returned image.
985 * \param type pixel data type for returned image.
986 * \param bufSize size of the pixels data buffer.
987 * \param pixels returned pixel data.
990 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
,
991 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
993 struct gl_texture_object
*texObj
;
994 struct gl_texture_image
*texImage
;
996 GET_CURRENT_CONTEXT(ctx
);
999 * This has been moved here because a format/type mismatch can cause a NULL
1000 * texImage object, which in turn causes the mismatch error to be
1003 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1004 if (err
!= GL_NO_ERROR
) {
1005 _mesa_error(ctx
, err
, "glGetnTexImage(format/type)");
1010 * Legal target checking has been moved here to prevent exiting with a NULL
1013 if (!legal_getteximage_target(ctx
, target
, false)) {
1014 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetnTexImage(target=0x%x)",
1019 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1023 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1027 _mesa_get_texture_image(ctx
, texObj
, texImage
, target
, level
, format
, type
,
1028 bufSize
, pixels
, false);
1033 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1034 GLenum type
, GLvoid
*pixels
)
1036 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
1040 * Get texture image.
1042 * \param texture texture name.
1043 * \param level image level.
1044 * \param format pixel data format for returned image.
1045 * \param type pixel data type for returned image.
1046 * \param bufSize size of the pixels data buffer.
1047 * \param pixels returned pixel data.
1050 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
,
1051 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1053 struct gl_texture_object
*texObj
;
1054 struct gl_texture_image
*texImage
;
1058 GET_CURRENT_CONTEXT(ctx
);
1061 * This has been moved here because a format/type mismatch can cause a NULL
1062 * texImage object, which in turn causes the mismatch error to be
1065 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1066 if (err
!= GL_NO_ERROR
) {
1067 _mesa_error(ctx
, err
, "glGetTextureImage(format/type)");
1071 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glGetTextureImage");
1076 * Legal target checking has been moved here to prevent exiting with a NULL
1079 if (!legal_getteximage_target(ctx
, texObj
->Target
, true)) {
1080 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTextureImage(target=%s)",
1081 _mesa_lookup_enum_by_nr(texObj
->Target
));
1085 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1086 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1088 /* Error checking */
1089 if (texObj
->NumLayers
< 6) {
1090 /* Not enough image planes for a cube map. The spec does not say
1091 * what should happen in this case because the user has always
1092 * specified each cube face separately (using
1093 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
1094 * This is addressed in Khronos Bug 13223.
1096 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1097 "glGetTextureImage(insufficient cube map storage)");
1102 * What do we do if the user created a texture with the following code
1103 * and then called this function with its handle?
1106 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
1107 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
1108 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
1109 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
1110 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
1111 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
1112 * // wrong format, or given the wrong size, etc.
1113 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
1114 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
1116 * A bug has been filed against the spec for this case. In the
1117 * meantime, we will check for cube completeness.
1119 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
1120 * Core Profile spec (30.10.2014):
1121 * "[A] cube map texture is cube complete if the
1122 * following conditions all hold true: The [base level] texture
1123 * images of each of the six cube map faces have identical, positive,
1124 * and square dimensions. The [base level] images were each specified
1125 * with the same internal format."
1127 * It seems reasonable to check for cube completeness of an arbitrary
1128 * level here so that the returned data has a consistent format and size
1129 * and therefore fits in the user's buffer.
1131 if (!_mesa_cube_level_complete(texObj
, level
)) {
1132 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1133 "glGetTextureImage(cube map incomplete)");
1137 /* Copy each face. */
1138 for (i
= 0; i
< 6; ++i
) {
1139 texImage
= texObj
->Image
[i
][level
];
1140 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1141 format
, type
, bufSize
, pixels
, true);
1143 image_stride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
1144 texImage
->Height
, format
,
1146 pixels
= (GLubyte
*) pixels
+ image_stride
;
1147 bufSize
-= image_stride
;
1151 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1155 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1156 format
, type
, bufSize
, pixels
, true);
1161 * Do error checking for a glGetCompressedTexImage() call.
1162 * \return GL_TRUE if any error, GL_FALSE if no errors.
1165 getcompressedteximage_error_check(struct gl_context
*ctx
,
1166 struct gl_texture_image
*texImage
,
1168 GLint level
, GLsizei clientMemSize
,
1169 GLvoid
*img
, bool dsa
)
1171 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1172 GLuint compressedSize
, dimensions
;
1173 const char *suffix
= dsa
? "ture" : "";
1177 if (!legal_getteximage_target(ctx
, target
, dsa
)) {
1178 _mesa_error(ctx
, GL_INVALID_ENUM
,
1179 "glGetCompressedTex%sImage(target=%s)", suffix
,
1180 _mesa_lookup_enum_by_nr(target
));
1184 assert(maxLevels
!= 0);
1185 if (level
< 0 || level
>= maxLevels
) {
1186 _mesa_error(ctx
, GL_INVALID_VALUE
,
1187 "glGetCompressedTex%sImage(bad level = %d)", suffix
, level
);
1191 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1192 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1193 "glGetCompressedTex%sImage(texture is not compressed)",
1198 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
1203 /* Check for invalid pixel storage modes */
1204 dimensions
= _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
1205 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1207 "glGetCompressedTextureImage":
1208 "glGetCompressedTexImage")) {
1212 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1213 /* do bounds checking on writing to client memory */
1214 if (clientMemSize
< (GLsizei
) compressedSize
) {
1215 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1216 "%s(out of bounds access: bufSize (%d) is too small)",
1217 dsa
? "glGetCompressedTextureImage" :
1218 "glGetnCompressedTexImageARB", clientMemSize
);
1222 /* do bounds checking on PBO write */
1223 if ((const GLubyte
*) img
+ compressedSize
>
1224 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1225 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1226 "glGetCompressedTex%sImage(out of bounds PBO access)",
1231 /* make sure PBO is not mapped */
1232 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1233 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1234 "glGetCompressedTex%sImage(PBO is mapped)", suffix
);
1242 /** Implements glGetnCompressedTexImageARB, glGetCompressedTexImage, and
1243 * glGetCompressedTextureImage.
1245 * texImage must be passed in because glGetCompressedTexImage must handle the
1246 * target GL_TEXTURE_CUBE_MAP.
1249 _mesa_get_compressed_texture_image(struct gl_context
*ctx
,
1250 struct gl_texture_object
*texObj
,
1251 struct gl_texture_image
*texImage
,
1252 GLenum target
, GLint level
,
1253 GLsizei bufSize
, GLvoid
*pixels
,
1259 FLUSH_VERTICES(ctx
, 0);
1261 if (getcompressedteximage_error_check(ctx
, texImage
, target
, level
,
1262 bufSize
, pixels
, dsa
)) {
1266 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1267 /* not an error, do nothing */
1271 if (_mesa_is_zero_size_texture(texImage
))
1274 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1276 "glGetCompressedTex%sImage(tex %u) format = %s, w=%d, h=%d\n",
1277 dsa
? "ture" : "", texObj
->Name
,
1278 _mesa_get_format_name(texImage
->TexFormat
),
1279 texImage
->Width
, texImage
->Height
);
1282 _mesa_lock_texture(ctx
, texObj
);
1284 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, pixels
);
1286 _mesa_unlock_texture(ctx
, texObj
);
1290 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1293 struct gl_texture_object
*texObj
;
1294 struct gl_texture_image
*texImage
;
1295 GET_CURRENT_CONTEXT(ctx
);
1297 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1301 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1305 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
, target
, level
,
1306 bufSize
, img
, false);
1310 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1312 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);
1316 * Get compressed texture image.
1318 * \param texture texture name.
1319 * \param level image level.
1320 * \param bufSize size of the pixels data buffer.
1321 * \param pixels returned pixel data.
1324 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1325 GLsizei bufSize
, GLvoid
*pixels
)
1327 struct gl_texture_object
*texObj
;
1328 struct gl_texture_image
*texImage
;
1331 GET_CURRENT_CONTEXT(ctx
);
1333 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
1334 "glGetCompressedTextureImage");
1338 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1339 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1340 assert(texObj
->NumLayers
>= 6);
1342 /* Copy each face. */
1343 for (i
= 0; i
< 6; ++i
) {
1344 texImage
= texObj
->Image
[i
][level
];
1348 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1349 texObj
->Target
, level
,
1350 bufSize
, pixels
, true);
1352 /* Compressed images don't have a client format */
1353 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
1355 texImage
->Height
, 1);
1357 pixels
= (GLubyte
*) pixels
+ image_stride
;
1358 bufSize
-= image_stride
;
1362 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1366 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1367 texObj
->Target
, level
, bufSize
,