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 GLint xoffset
, GLint yoffset
, GLint zoffset
,
79 GLsizei width
, GLsizei height
, GLint depth
,
80 GLenum format
, GLenum type
, GLvoid
*pixels
,
81 struct gl_texture_image
*texImage
)
84 GLfloat
*depthRow
= malloc(width
* sizeof(GLfloat
));
87 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
91 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
96 assert(zoffset
+ depth
<= texImage
->Depth
);
97 for (img
= 0; img
< depth
; img
++) {
101 /* map src texture buffer */
102 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
103 xoffset
, yoffset
, width
, height
,
104 GL_MAP_READ_BIT
, &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
, zoffset
+ 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 GLint xoffset
, GLint yoffset
, GLint zoffset
,
134 GLsizei width
, GLsizei height
, GLint depth
,
135 GLenum format
, GLenum type
, GLvoid
*pixels
,
136 struct gl_texture_image
*texImage
)
140 assert(format
== GL_DEPTH_STENCIL
);
141 assert(type
== GL_UNSIGNED_INT_24_8
||
142 type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
144 assert(zoffset
+ depth
<= texImage
->Depth
);
145 for (img
= 0; img
< depth
; img
++) {
149 /* map src texture buffer */
150 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
151 xoffset
, yoffset
, width
, height
,
152 GL_MAP_READ_BIT
, &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
, zoffset
+ img
);
172 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
179 * glGetTexImage for stencil pixels.
182 get_tex_stencil(struct gl_context
*ctx
, GLuint dimensions
,
183 GLint xoffset
, GLint yoffset
, GLint zoffset
,
184 GLsizei width
, GLsizei height
, GLint depth
,
185 GLenum format
, GLenum type
, GLvoid
*pixels
,
186 struct gl_texture_image
*texImage
)
190 assert(format
== GL_STENCIL_INDEX
);
192 for (img
= 0; img
< depth
; img
++) {
196 /* map src texture buffer */
197 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
198 xoffset
, yoffset
, width
, height
,
200 &srcMap
, &rowstride
);
203 for (row
= 0; row
< height
; row
++) {
204 const GLubyte
*src
= srcMap
+ row
* rowstride
;
205 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
206 width
, height
, format
, type
,
208 _mesa_unpack_ubyte_stencil_row(texImage
->TexFormat
,
210 (const GLuint
*) src
,
214 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
217 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
225 * glGetTexImage for YCbCr pixels.
228 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
229 GLint xoffset
, GLint yoffset
, GLint zoffset
,
230 GLsizei width
, GLsizei height
, GLint depth
,
231 GLenum format
, GLenum type
, GLvoid
*pixels
,
232 struct gl_texture_image
*texImage
)
236 assert(zoffset
+ depth
<= texImage
->Depth
);
237 for (img
= 0; img
< depth
; img
++) {
241 /* map src texture buffer */
242 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
243 xoffset
, yoffset
, width
, height
,
244 GL_MAP_READ_BIT
, &srcMap
, &rowstride
);
247 for (row
= 0; row
< height
; row
++) {
248 const GLubyte
*src
= srcMap
+ row
* rowstride
;
249 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
250 width
, height
, format
, type
,
252 memcpy(dest
, src
, width
* sizeof(GLushort
));
254 /* check for byte swapping */
255 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
256 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
257 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
258 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
259 if (!ctx
->Pack
.SwapBytes
)
260 _mesa_swap2((GLushort
*) dest
, width
);
262 else if (ctx
->Pack
.SwapBytes
) {
263 _mesa_swap2((GLushort
*) dest
, width
);
267 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
270 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
278 * Get a color texture image with decompression.
281 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
282 GLint xoffset
, GLint yoffset
, GLint zoffset
,
283 GLsizei width
, GLsizei height
, GLint depth
,
284 GLenum format
, GLenum type
, GLvoid
*pixels
,
285 struct gl_texture_image
*texImage
,
286 GLbitfield transferOps
)
288 /* don't want to apply sRGB -> RGB conversion here so override the format */
289 const mesa_format texFormat
=
290 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
291 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
292 GLfloat
*tempImage
, *tempSlice
;
294 int srcStride
, dstStride
;
297 uint8_t rebaseSwizzle
[4];
299 /* Decompress into temp float buffer, then pack into user buffer */
300 tempImage
= malloc(width
* height
* depth
301 * 4 * sizeof(GLfloat
));
303 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
307 /* Decompress the texture image slices - results in 'tempImage' */
308 for (slice
= 0; slice
< depth
; slice
++) {
312 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
314 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ slice
,
315 xoffset
, yoffset
, width
, height
,
317 &srcMap
, &srcRowStride
);
319 _mesa_decompress_image(texFormat
, width
, height
,
320 srcMap
, srcRowStride
, tempSlice
);
322 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ slice
);
325 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
331 /* Depending on the base format involved we may need to apply a rebase
332 * transform (for example: if we download to a Luminance format we want
335 if (baseFormat
== GL_LUMINANCE
||
336 baseFormat
== GL_INTENSITY
) {
338 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
339 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
340 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
341 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
342 } else if (baseFormat
== GL_LUMINANCE_ALPHA
) {
344 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
345 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
346 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
347 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
352 srcStride
= 4 * width
* sizeof(GLfloat
);
353 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
354 dstFormat
= _mesa_format_from_format_and_type(format
, type
);
355 tempSlice
= tempImage
;
356 for (slice
= 0; slice
< depth
; slice
++) {
357 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
358 width
, height
, format
, type
,
360 _mesa_format_convert(dest
, dstFormat
, dstStride
,
361 tempSlice
, RGBA32_FLOAT
, srcStride
,
363 needsRebase
? rebaseSwizzle
: NULL
);
364 tempSlice
+= 4 * width
* height
;
372 * Return a base GL format given the user-requested format
373 * for glGetTexImage().
376 _mesa_base_pack_format(GLenum format
)
381 case GL_BGRA_INTEGER
:
382 case GL_RGBA_INTEGER
:
390 case GL_GREEN_INTEGER
:
392 case GL_BLUE_INTEGER
:
394 case GL_ALPHA_INTEGER
:
396 case GL_LUMINANCE_INTEGER_EXT
:
398 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
399 return GL_LUMINANCE_ALPHA
;
407 * Get an uncompressed color texture image.
410 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
411 GLint xoffset
, GLint yoffset
, GLint zoffset
,
412 GLsizei width
, GLsizei height
, GLint depth
,
413 GLenum format
, GLenum type
, GLvoid
*pixels
,
414 struct gl_texture_image
*texImage
,
415 GLbitfield transferOps
)
417 /* don't want to apply sRGB -> RGB conversion here so override the format */
418 const mesa_format texFormat
=
419 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
421 GLboolean dst_is_integer
;
424 uint8_t rebaseSwizzle
[4];
428 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
435 /* Depending on the base format involved we may need to apply a rebase
436 * transform (for example: if we download to a Luminance format we want
439 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
440 texImage
->_BaseFormat
== GL_INTENSITY
) {
442 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
443 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
444 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
445 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
446 } else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
448 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
449 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
450 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
451 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
452 } else if (texImage
->_BaseFormat
!=
453 _mesa_get_format_base_format(texFormat
)) {
455 _mesa_compute_rgba2base2rgba_component_mapping(texImage
->_BaseFormat
,
461 /* Describe the dst format */
462 dst_is_integer
= _mesa_is_enum_format_integer(format
);
463 dst_format
= _mesa_format_from_format_and_type(format
, type
);
464 dst_stride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
466 /* Since _mesa_format_convert does not handle transferOps we need to handle
467 * them before we call the function. This requires to convert to RGBA float
468 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
469 * integer then transferOps do not apply.
471 assert(!transferOps
|| (transferOps
&& !dst_is_integer
));
472 (void) dst_is_integer
; /* silence unused var warning */
474 for (img
= 0; img
< depth
; img
++) {
483 /* map src texture buffer */
484 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
485 xoffset
, yoffset
, width
, height
,
487 &srcMap
, &rowstride
);
489 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
494 dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
495 width
, height
, format
, type
,
499 uint32_t rgba_format
;
501 bool need_convert
= false;
503 /* We will convert to RGBA float */
504 rgba_format
= RGBA32_FLOAT
;
505 rgba_stride
= width
* 4 * sizeof(GLfloat
);
507 /* If we are lucky and the dst format matches the RGBA format we need
508 * to convert to, then we can convert directly into the dst buffer
509 * and avoid the final conversion/copy from the rgba buffer to the dst
512 if (format
== rgba_format
) {
514 } else if (rgba
== NULL
) { /* Allocate the RGBA buffer only once */
516 rgba
= malloc(height
* rgba_stride
);
518 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
519 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
524 _mesa_format_convert(rgba
, rgba_format
, rgba_stride
,
525 img_src
, texFormat
, rowstride
,
527 needsRebase
? rebaseSwizzle
: NULL
);
529 /* Handle transfer ops now */
530 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
* height
, rgba
);
532 /* If we had to rebase, we have already handled that */
535 /* If we were lucky and our RGBA conversion matches the dst format,
541 /* Otherwise, we need to convert from RGBA to dst next */
543 src_format
= rgba_format
;
544 src_stride
= rgba_stride
;
546 /* No RGBA conversion needed, convert directly to dst */
548 src_format
= texFormat
;
549 src_stride
= rowstride
;
552 /* Do the conversion to destination format */
553 _mesa_format_convert(dest
, dst_format
, dst_stride
,
554 src
, src_format
, src_stride
,
556 needsRebase
? rebaseSwizzle
: NULL
);
559 /* Handle byte swapping if required */
560 if (ctx
->Pack
.SwapBytes
) {
561 GLint swapSize
= _mesa_sizeof_packed_type(type
);
562 if (swapSize
== 2 || swapSize
== 4) {
563 int swapsPerPixel
= _mesa_bytes_per_pixel(format
, type
) / swapSize
;
564 assert(_mesa_bytes_per_pixel(format
, type
) % swapSize
== 0);
566 _mesa_swap2((GLushort
*) dest
, width
* height
* swapsPerPixel
);
567 else if (swapSize
== 4)
568 _mesa_swap4((GLuint
*) dest
, width
* height
* swapsPerPixel
);
572 /* Unmap the src texture buffer */
573 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
583 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
584 * Compressed textures are handled here as well.
587 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
588 GLint xoffset
, GLint yoffset
, GLint zoffset
,
589 GLsizei width
, GLsizei height
, GLint depth
,
590 GLenum format
, GLenum type
, GLvoid
*pixels
,
591 struct gl_texture_image
*texImage
)
593 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
594 GLbitfield transferOps
= 0x0;
596 /* In general, clamping does not apply to glGetTexImage, except when
597 * the returned type of the image can't hold negative values.
599 if (type_needs_clamping(type
)) {
600 /* the returned image type can't have negative values */
601 if (dataType
== GL_FLOAT
||
602 dataType
== GL_HALF_FLOAT
||
603 dataType
== GL_SIGNED_NORMALIZED
||
604 format
== GL_LUMINANCE
||
605 format
== GL_LUMINANCE_ALPHA
) {
606 transferOps
|= IMAGE_CLAMP_BIT
;
610 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
611 get_tex_rgba_compressed(ctx
, dimensions
,
612 xoffset
, yoffset
, zoffset
,
613 width
, height
, depth
,
615 pixels
, texImage
, transferOps
);
618 get_tex_rgba_uncompressed(ctx
, dimensions
,
619 xoffset
, yoffset
, zoffset
,
620 width
, height
, depth
,
622 pixels
, texImage
, transferOps
);
628 * Try to do glGetTexImage() with simple memcpy().
629 * \return GL_TRUE if done, GL_FALSE otherwise
632 get_tex_memcpy(struct gl_context
*ctx
,
633 GLint xoffset
, GLint yoffset
, GLint zoffset
,
634 GLsizei width
, GLsizei height
, GLint depth
,
635 GLenum format
, GLenum type
, GLvoid
*pixels
,
636 struct gl_texture_image
*texImage
)
638 const GLenum target
= texImage
->TexObject
->Target
;
639 GLboolean memCopy
= GL_FALSE
;
640 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
643 * Check if we can use memcpy to copy from the hardware texture
644 * format to the user's format/type.
645 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
647 if ((target
== GL_TEXTURE_1D
||
648 target
== GL_TEXTURE_2D
||
649 target
== GL_TEXTURE_RECTANGLE
||
650 _mesa_is_cube_face(target
)) &&
651 texBaseFormat
== texImage
->_BaseFormat
) {
652 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
654 ctx
->Pack
.SwapBytes
);
658 /* only a single slice is supported at this time */
663 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
664 const GLint bytesPerRow
= width
* bpp
;
666 _mesa_image_address2d(&ctx
->Pack
, pixels
, width
, height
,
668 const GLint dstRowStride
=
669 _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
673 /* map src texture buffer */
674 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
,
675 xoffset
, yoffset
, width
, height
,
676 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
679 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
680 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
684 for (row
= 0; row
< height
; row
++) {
685 memcpy(dst
, src
, bytesPerRow
);
691 /* unmap src texture buffer */
692 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
);
695 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
704 * This is the software fallback for Driver.GetTexSubImage().
705 * All error checking will have been done before this routine is called.
706 * We'll call ctx->Driver.MapTextureImage() to access the data, then
707 * unmap with ctx->Driver.UnmapTextureImage().
710 _mesa_GetTexSubImage_sw(struct gl_context
*ctx
,
711 GLint xoffset
, GLint yoffset
, GLint zoffset
,
712 GLsizei width
, GLsizei height
, GLint depth
,
713 GLenum format
, GLenum type
, GLvoid
*pixels
,
714 struct gl_texture_image
*texImage
)
716 const GLuint dimensions
=
717 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
719 /* map dest buffer, if PBO */
720 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
721 /* Packing texture image into a PBO.
722 * Map the (potentially) VRAM-based buffer into our process space so
723 * we can write into it with the code below.
724 * A hardware driver might use a sophisticated blit to move the
725 * texture data to the PBO if the PBO is in VRAM along with the texture.
727 GLubyte
*buf
= (GLubyte
*)
728 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
729 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
732 /* out of memory or other unexpected error */
733 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
736 /* <pixels> was an offset into the PBO.
737 * Now make it a real, client-side pointer inside the mapped region.
739 pixels
= ADD_POINTERS(buf
, pixels
);
742 if (get_tex_memcpy(ctx
, xoffset
, yoffset
, zoffset
, width
, height
, depth
,
743 format
, type
, pixels
, texImage
)) {
746 else if (format
== GL_DEPTH_COMPONENT
) {
747 get_tex_depth(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
748 width
, height
, depth
, format
, type
, pixels
, texImage
);
750 else if (format
== GL_DEPTH_STENCIL_EXT
) {
751 get_tex_depth_stencil(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
752 width
, height
, depth
, format
, type
, pixels
,
755 else if (format
== GL_STENCIL_INDEX
) {
756 get_tex_stencil(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
757 width
, height
, depth
, format
, type
, pixels
, texImage
);
759 else if (format
== GL_YCBCR_MESA
) {
760 get_tex_ycbcr(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
761 width
, height
, depth
, format
, type
, pixels
, texImage
);
764 get_tex_rgba(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
765 width
, height
, depth
, format
, type
, pixels
, texImage
);
768 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
769 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
776 * This is the software fallback for Driver.GetCompressedTexSubImage().
777 * All error checking will have been done before this routine is called.
780 _mesa_GetCompressedTexSubImage_sw(struct gl_context
*ctx
,
781 struct gl_texture_image
*texImage
,
782 GLint xoffset
, GLint yoffset
,
783 GLint zoffset
, GLsizei width
,
784 GLint height
, GLint depth
,
787 const GLuint dimensions
=
788 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
789 struct compressed_pixelstore store
;
793 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
794 width
, height
, depth
,
797 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
798 /* pack texture image into a PBO */
800 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
801 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
804 /* out of memory or other unexpected error */
805 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
806 "glGetCompresssedTexImage(map PBO failed)");
809 dest
= ADD_POINTERS(dest
, img
);
814 dest
+= store
.SkipBytes
;
816 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
820 /* map src texture buffer */
821 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ slice
,
822 xoffset
, yoffset
, width
, height
,
823 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
827 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
828 memcpy(dest
, src
, store
.CopyBytesPerRow
);
829 dest
+= store
.TotalBytesPerRow
;
833 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ slice
);
835 /* Advance to next slice */
836 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
-
837 store
.CopyRowsPerSlice
);
840 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
844 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
845 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
851 * Validate the texture target enum supplied to glGetTex(ture)Image or
852 * glGetCompressedTex(ture)Image.
855 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
862 case GL_TEXTURE_RECTANGLE_NV
:
863 return ctx
->Extensions
.NV_texture_rectangle
;
864 case GL_TEXTURE_1D_ARRAY_EXT
:
865 case GL_TEXTURE_2D_ARRAY_EXT
:
866 return ctx
->Extensions
.EXT_texture_array
;
867 case GL_TEXTURE_CUBE_MAP_ARRAY
:
868 return ctx
->Extensions
.ARB_texture_cube_map_array
;
870 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
872 * "An INVALID_ENUM error is generated if the effective target is not
873 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
874 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
875 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
876 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
878 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
879 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
880 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
881 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
882 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
883 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
884 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
885 case GL_TEXTURE_CUBE_MAP
:
886 return dsa
? GL_TRUE
: GL_FALSE
;
894 * Do error checking for a glGetTex(ture)Image() call.
895 * \return GL_TRUE if any error, GL_FALSE if no errors.
898 getteximage_error_check(struct gl_context
*ctx
,
899 struct gl_texture_image
*texImage
,
900 GLenum target
, GLint level
,
901 GLenum format
, GLenum type
, GLsizei clientMemSize
,
902 GLvoid
*pixels
, bool dsa
)
904 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
905 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
907 const char *suffix
= dsa
? "ture" : "";
910 assert(maxLevels
!= 0);
911 if (level
< 0 || level
>= maxLevels
) {
912 _mesa_error(ctx
, GL_INVALID_VALUE
,
913 "glGetTex%sImage(level out of range)", suffix
);
918 * Format and type checking has been moved up to GetnTexImage and
919 * GetTextureImage so that it happens before getting the texImage object.
922 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
924 /* Make sure the requested image format is compatible with the
927 if (_mesa_is_color_format(format
)
928 && !_mesa_is_color_format(baseFormat
)) {
929 _mesa_error(ctx
, GL_INVALID_OPERATION
,
930 "glGetTex%sImage(format mismatch)", suffix
);
933 else if (_mesa_is_depth_format(format
)
934 && !_mesa_is_depth_format(baseFormat
)
935 && !_mesa_is_depthstencil_format(baseFormat
)) {
936 _mesa_error(ctx
, GL_INVALID_OPERATION
,
937 "glGetTex%sImage(format mismatch)", suffix
);
940 else if (_mesa_is_stencil_format(format
)
941 && !ctx
->Extensions
.ARB_texture_stencil8
) {
942 _mesa_error(ctx
, GL_INVALID_ENUM
,
943 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix
);
946 else if (_mesa_is_ycbcr_format(format
)
947 && !_mesa_is_ycbcr_format(baseFormat
)) {
948 _mesa_error(ctx
, GL_INVALID_OPERATION
,
949 "glGetTex%sImage(format mismatch)", suffix
);
952 else if (_mesa_is_depthstencil_format(format
)
953 && !_mesa_is_depthstencil_format(baseFormat
)) {
954 _mesa_error(ctx
, GL_INVALID_OPERATION
,
955 "glGetTex%sImage(format mismatch)", suffix
);
958 else if (!_mesa_is_stencil_format(format
) &&
959 _mesa_is_enum_format_integer(format
) !=
960 _mesa_is_format_integer(texImage
->TexFormat
)) {
961 _mesa_error(ctx
, GL_INVALID_OPERATION
,
962 "glGetTex%sImage(format mismatch)", suffix
);
966 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
967 texImage
->Height
, texImage
->Depth
,
968 format
, type
, clientMemSize
, pixels
)) {
969 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
970 _mesa_error(ctx
, GL_INVALID_OPERATION
,
971 "glGetTex%sImage(out of bounds PBO access)", suffix
);
973 _mesa_error(ctx
, GL_INVALID_OPERATION
,
974 "%s(out of bounds access:"
975 " bufSize (%d) is too small)",
976 dsa
? "glGetTextureImage" : "glGetnTexImageARB",
982 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
983 /* PBO should not be mapped */
984 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
985 _mesa_error(ctx
, GL_INVALID_OPERATION
,
986 "glGetTex%sImage(PBO is mapped)", suffix
);
996 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
999 * Requires caller to pass in texImage object because _mesa_GetTextureImage
1000 * must handle the GL_TEXTURE_CUBE_MAP target.
1002 * \param target texture target.
1003 * \param level image level.
1004 * \param format pixel data format for returned image.
1005 * \param type pixel data type for returned image.
1006 * \param bufSize size of the pixels data buffer.
1007 * \param pixels returned pixel data.
1008 * \param dsa True when the caller is an ARB_direct_state_access function,
1012 get_texture_image(struct gl_context
*ctx
,
1013 struct gl_texture_object
*texObj
,
1014 struct gl_texture_image
*texImage
, GLenum target
,
1015 GLint level
, GLenum format
, GLenum type
,
1016 GLsizei bufSize
, GLvoid
*pixels
, bool dsa
)
1021 FLUSH_VERTICES(ctx
, 0);
1024 * Legal target checking has been moved up to GetnTexImage and
1025 * GetTextureImage so that it can be caught before receiving a NULL
1026 * texImage object and exiting.
1029 if (getteximage_error_check(ctx
, texImage
, target
, level
, format
,
1030 type
, bufSize
, pixels
, dsa
)) {
1034 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1035 /* not an error, do nothing */
1039 if (_mesa_is_zero_size_texture(texImage
))
1042 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1043 _mesa_debug(ctx
, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
1044 " dstFmt=0x%x, dstType=0x%x\n",
1047 _mesa_get_format_name(texImage
->TexFormat
),
1048 texImage
->Width
, texImage
->Height
,
1052 _mesa_lock_texture(ctx
, texObj
);
1054 ctx
->Driver
.GetTexSubImage(ctx
, 0, 0, 0,
1055 texImage
->Width
, texImage
->Height
,
1057 format
, type
, pixels
, texImage
);
1059 _mesa_unlock_texture(ctx
, texObj
);
1063 * Get texture image. Called by glGetTexImage.
1065 * \param target texture target.
1066 * \param level image level.
1067 * \param format pixel data format for returned image.
1068 * \param type pixel data type for returned image.
1069 * \param bufSize size of the pixels data buffer.
1070 * \param pixels returned pixel data.
1073 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
,
1074 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1076 struct gl_texture_object
*texObj
;
1077 struct gl_texture_image
*texImage
;
1079 GET_CURRENT_CONTEXT(ctx
);
1082 * This has been moved here because a format/type mismatch can cause a NULL
1083 * texImage object, which in turn causes the mismatch error to be
1086 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1087 if (err
!= GL_NO_ERROR
) {
1088 _mesa_error(ctx
, err
, "glGetnTexImage(format/type)");
1093 * Legal target checking has been moved here to prevent exiting with a NULL
1096 if (!legal_getteximage_target(ctx
, target
, false)) {
1097 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetnTexImage(target=0x%x)",
1102 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1106 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1110 get_texture_image(ctx
, texObj
, texImage
, target
, level
, format
, type
,
1111 bufSize
, pixels
, false);
1116 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1117 GLenum type
, GLvoid
*pixels
)
1119 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
1123 * Get texture image.
1125 * \param texture texture name.
1126 * \param level image level.
1127 * \param format pixel data format for returned image.
1128 * \param type pixel data type for returned image.
1129 * \param bufSize size of the pixels data buffer.
1130 * \param pixels returned pixel data.
1133 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
,
1134 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1136 struct gl_texture_object
*texObj
;
1137 struct gl_texture_image
*texImage
;
1141 GET_CURRENT_CONTEXT(ctx
);
1144 * This has been moved here because a format/type mismatch can cause a NULL
1145 * texImage object, which in turn causes the mismatch error to be
1148 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1149 if (err
!= GL_NO_ERROR
) {
1150 _mesa_error(ctx
, err
, "glGetTextureImage(format/type)");
1154 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glGetTextureImage");
1159 * Legal target checking has been moved here to prevent exiting with a NULL
1162 if (!legal_getteximage_target(ctx
, texObj
->Target
, true)) {
1163 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTextureImage(target=%s)",
1164 _mesa_enum_to_string(texObj
->Target
));
1168 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1169 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1171 /* Make sure the texture object is a proper cube.
1172 * (See texturesubimage in teximage.c for details on why this check is
1175 if (!_mesa_cube_level_complete(texObj
, level
)) {
1176 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1177 "glGetTextureImage(cube map incomplete)");
1181 /* Copy each face. */
1182 for (i
= 0; i
< 6; ++i
) {
1183 texImage
= texObj
->Image
[i
][level
];
1186 get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1187 format
, type
, bufSize
, pixels
, true);
1189 image_stride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
1190 texImage
->Height
, format
,
1192 pixels
= (GLubyte
*) pixels
+ image_stride
;
1193 bufSize
-= image_stride
;
1197 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1201 get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1202 format
, type
, bufSize
, pixels
, true);
1207 * Do error checking for a glGetCompressedTexImage() call.
1208 * \return GL_TRUE if any error, GL_FALSE if no errors.
1211 getcompressedteximage_error_check(struct gl_context
*ctx
,
1212 struct gl_texture_image
*texImage
,
1214 GLint level
, GLsizei clientMemSize
,
1215 GLvoid
*img
, bool dsa
)
1217 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1218 GLuint compressedSize
, dimensions
;
1219 const char *suffix
= dsa
? "ture" : "";
1223 if (!legal_getteximage_target(ctx
, target
, dsa
)) {
1224 _mesa_error(ctx
, GL_INVALID_ENUM
,
1225 "glGetCompressedTex%sImage(target=%s)", suffix
,
1226 _mesa_enum_to_string(target
));
1230 assert(maxLevels
!= 0);
1231 if (level
< 0 || level
>= maxLevels
) {
1232 _mesa_error(ctx
, GL_INVALID_VALUE
,
1233 "glGetCompressedTex%sImage(bad level = %d)", suffix
, level
);
1237 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1238 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1239 "glGetCompressedTex%sImage(texture is not compressed)",
1244 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
1249 /* Check for invalid pixel storage modes */
1250 dimensions
= _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
1251 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1253 "glGetCompressedTextureImage":
1254 "glGetCompressedTexImage")) {
1258 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1259 /* do bounds checking on writing to client memory */
1260 if (clientMemSize
< (GLsizei
) compressedSize
) {
1261 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1262 "%s(out of bounds access: bufSize (%d) is too small)",
1263 dsa
? "glGetCompressedTextureImage" :
1264 "glGetnCompressedTexImageARB", clientMemSize
);
1268 /* do bounds checking on PBO write */
1269 if ((const GLubyte
*) img
+ compressedSize
>
1270 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1271 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1272 "glGetCompressedTex%sImage(out of bounds PBO access)",
1277 /* make sure PBO is not mapped */
1278 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1279 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1280 "glGetCompressedTex%sImage(PBO is mapped)", suffix
);
1288 /** Implements glGetnCompressedTexImageARB, glGetCompressedTexImage, and
1289 * glGetCompressedTextureImage.
1291 * texImage must be passed in because glGetCompressedTexImage must handle the
1292 * target GL_TEXTURE_CUBE_MAP.
1295 _mesa_get_compressed_texture_image(struct gl_context
*ctx
,
1296 struct gl_texture_object
*texObj
,
1297 struct gl_texture_image
*texImage
,
1298 GLenum target
, GLint level
,
1299 GLsizei bufSize
, GLvoid
*pixels
,
1305 FLUSH_VERTICES(ctx
, 0);
1307 if (getcompressedteximage_error_check(ctx
, texImage
, target
, level
,
1308 bufSize
, pixels
, dsa
)) {
1312 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1313 /* not an error, do nothing */
1317 if (_mesa_is_zero_size_texture(texImage
))
1320 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1322 "glGetCompressedTex%sImage(tex %u) format = %s, w=%d, h=%d\n",
1323 dsa
? "ture" : "", texObj
->Name
,
1324 _mesa_get_format_name(texImage
->TexFormat
),
1325 texImage
->Width
, texImage
->Height
);
1328 _mesa_lock_texture(ctx
, texObj
);
1330 ctx
->Driver
.GetCompressedTexSubImage(ctx
, texImage
,
1332 texImage
->Width
, texImage
->Height
,
1333 texImage
->Depth
, pixels
);
1335 _mesa_unlock_texture(ctx
, texObj
);
1339 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1342 struct gl_texture_object
*texObj
;
1343 struct gl_texture_image
*texImage
;
1344 GET_CURRENT_CONTEXT(ctx
);
1346 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1350 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1354 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
, target
, level
,
1355 bufSize
, img
, false);
1359 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1361 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);
1365 * Get compressed texture image.
1367 * \param texture texture name.
1368 * \param level image level.
1369 * \param bufSize size of the pixels data buffer.
1370 * \param pixels returned pixel data.
1373 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1374 GLsizei bufSize
, GLvoid
*pixels
)
1376 struct gl_texture_object
*texObj
;
1377 struct gl_texture_image
*texImage
;
1380 GET_CURRENT_CONTEXT(ctx
);
1382 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
1383 "glGetCompressedTextureImage");
1387 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1388 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1390 /* Make sure the texture object is a proper cube.
1391 * (See texturesubimage in teximage.c for details on why this check is
1394 if (!_mesa_cube_level_complete(texObj
, level
)) {
1395 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1396 "glGetCompressedTextureImage(cube map incomplete)");
1400 /* Copy each face. */
1401 for (i
= 0; i
< 6; ++i
) {
1402 texImage
= texObj
->Image
[i
][level
];
1405 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1406 texObj
->Target
, level
,
1407 bufSize
, pixels
, true);
1409 /* Compressed images don't have a client format */
1410 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
1412 texImage
->Height
, 1);
1414 pixels
= (GLubyte
*) pixels
+ image_stride
;
1415 bufSize
-= image_stride
;
1419 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1423 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1424 texObj
->Target
, level
, bufSize
,