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");
179 * glGetTexImage for stencil pixels.
182 get_tex_stencil(struct gl_context
*ctx
, GLuint dimensions
,
183 GLenum format
, GLenum type
, GLvoid
*pixels
,
184 struct gl_texture_image
*texImage
)
186 const GLint width
= texImage
->Width
;
187 const GLint height
= texImage
->Height
;
188 const GLint depth
= texImage
->Depth
;
191 assert(format
== GL_STENCIL_INDEX
);
193 for (img
= 0; img
< depth
; img
++) {
197 /* map src texture buffer */
198 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
199 0, 0, width
, height
, GL_MAP_READ_BIT
,
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
, 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 GLenum format
, GLenum type
, GLvoid
*pixels
,
230 struct gl_texture_image
*texImage
)
232 const GLint width
= texImage
->Width
;
233 const GLint height
= texImage
->Height
;
234 const GLint depth
= texImage
->Depth
;
237 for (img
= 0; img
< depth
; img
++) {
241 /* map src texture buffer */
242 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
243 0, 0, width
, height
, GL_MAP_READ_BIT
,
244 &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
, 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 GLenum format
, GLenum type
, GLvoid
*pixels
,
283 struct gl_texture_image
*texImage
,
284 GLbitfield transferOps
)
286 /* don't want to apply sRGB -> RGB conversion here so override the format */
287 const mesa_format texFormat
=
288 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
289 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
290 const GLuint width
= texImage
->Width
;
291 const GLuint height
= texImage
->Height
;
292 const GLuint depth
= texImage
->Depth
;
293 GLfloat
*tempImage
, *tempSlice
;
295 int srcStride
, dstStride
;
298 uint8_t rebaseSwizzle
[4];
300 /* Decompress into temp float buffer, then pack into user buffer */
301 tempImage
= malloc(width
* height
* depth
302 * 4 * sizeof(GLfloat
));
304 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
308 /* Decompress the texture image slices - results in 'tempImage' */
309 for (slice
= 0; slice
< depth
; slice
++) {
313 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
315 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
318 &srcMap
, &srcRowStride
);
320 _mesa_decompress_image(texFormat
, width
, height
,
321 srcMap
, srcRowStride
, tempSlice
);
323 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
326 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
332 /* Depending on the base format involved we may need to apply a rebase
333 * transform (for example: if we download to a Luminance format we want
336 if (baseFormat
== GL_LUMINANCE
||
337 baseFormat
== GL_INTENSITY
) {
339 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
340 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
341 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
342 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
343 } else if (baseFormat
== GL_LUMINANCE_ALPHA
) {
345 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
346 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
347 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
348 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
353 srcStride
= 4 * width
* sizeof(GLfloat
);
354 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
355 dstFormat
= _mesa_format_from_format_and_type(format
, type
);
356 tempSlice
= tempImage
;
357 for (slice
= 0; slice
< depth
; slice
++) {
358 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
359 width
, height
, format
, type
,
361 _mesa_format_convert(dest
, dstFormat
, dstStride
,
362 tempSlice
, RGBA32_FLOAT
, srcStride
,
364 needsRebase
? rebaseSwizzle
: NULL
);
365 tempSlice
+= 4 * width
* height
;
373 * Return a base GL format given the user-requested format
374 * for glGetTexImage().
377 _mesa_base_pack_format(GLenum format
)
382 case GL_BGRA_INTEGER
:
383 case GL_RGBA_INTEGER
:
391 case GL_GREEN_INTEGER
:
393 case GL_BLUE_INTEGER
:
395 case GL_ALPHA_INTEGER
:
397 case GL_LUMINANCE_INTEGER_EXT
:
399 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
400 return GL_LUMINANCE_ALPHA
;
408 * Get an uncompressed color texture image.
411 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
412 GLenum format
, GLenum type
, GLvoid
*pixels
,
413 struct gl_texture_image
*texImage
,
414 GLbitfield transferOps
)
416 /* don't want to apply sRGB -> RGB conversion here so override the format */
417 const mesa_format texFormat
=
418 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
419 const GLuint width
= texImage
->Width
;
420 GLuint height
= texImage
->Height
;
421 GLuint depth
= texImage
->Depth
;
423 GLboolean dst_is_integer
;
426 uint8_t rebaseSwizzle
[4];
430 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
!= _mesa_get_format_base_format(texFormat
)) {
454 _mesa_compute_rgba2base2rgba_component_mapping(texImage
->_BaseFormat
,
460 /* Describe the dst format */
461 dst_is_integer
= _mesa_is_enum_format_integer(format
);
462 dst_format
= _mesa_format_from_format_and_type(format
, type
);
463 dst_stride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
465 /* Since _mesa_format_convert does not handle transferOps we need to handle
466 * them before we call the function. This requires to convert to RGBA float
467 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
468 * integer then transferOps do not apply.
470 assert(!transferOps
|| (transferOps
&& !dst_is_integer
));
471 (void) dst_is_integer
; /* silence unused var warning */
473 for (img
= 0; img
< depth
; img
++) {
482 /* map src texture buffer */
483 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
484 0, 0, width
, height
, GL_MAP_READ_BIT
,
485 &srcMap
, &rowstride
);
487 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
492 dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
493 width
, height
, format
, type
,
497 uint32_t rgba_format
;
499 bool need_convert
= false;
501 /* We will convert to RGBA float */
502 rgba_format
= RGBA32_FLOAT
;
503 rgba_stride
= width
* 4 * sizeof(GLfloat
);
505 /* If we are lucky and the dst format matches the RGBA format we need
506 * to convert to, then we can convert directly into the dst buffer
507 * and avoid the final conversion/copy from the rgba buffer to the dst
510 if (format
== rgba_format
) {
512 } else if (rgba
== NULL
) { /* Allocate the RGBA buffer only once */
514 rgba
= malloc(height
* rgba_stride
);
516 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
517 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
522 _mesa_format_convert(rgba
, rgba_format
, rgba_stride
,
523 img_src
, texFormat
, rowstride
,
525 needsRebase
? rebaseSwizzle
: NULL
);
527 /* Handle transfer ops now */
528 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
* height
, rgba
);
530 /* If we had to rebase, we have already handled that */
533 /* If we were lucky and our RGBA conversion matches the dst format, then
539 /* Otherwise, we need to convert from RGBA to dst next */
541 src_format
= rgba_format
;
542 src_stride
= rgba_stride
;
544 /* No RGBA conversion needed, convert directly to dst */
546 src_format
= texFormat
;
547 src_stride
= rowstride
;
550 /* Do the conversion to destination format */
551 _mesa_format_convert(dest
, dst_format
, dst_stride
,
552 src
, src_format
, src_stride
,
554 needsRebase
? rebaseSwizzle
: NULL
);
557 /* Handle byte swapping if required */
558 if (ctx
->Pack
.SwapBytes
) {
559 GLint swapSize
= _mesa_sizeof_packed_type(type
);
560 if (swapSize
== 2 || swapSize
== 4) {
561 int swapsPerPixel
= _mesa_bytes_per_pixel(format
, type
) / swapSize
;
562 assert(_mesa_bytes_per_pixel(format
, type
) % swapSize
== 0);
564 _mesa_swap2((GLushort
*) dest
, width
* height
* swapsPerPixel
);
565 else if (swapSize
== 4)
566 _mesa_swap4((GLuint
*) dest
, width
* height
* swapsPerPixel
);
570 /* Unmap the src texture buffer */
571 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
581 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
582 * Compressed textures are handled here as well.
585 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
586 GLenum format
, GLenum type
, GLvoid
*pixels
,
587 struct gl_texture_image
*texImage
)
589 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
590 GLbitfield transferOps
= 0x0;
592 /* In general, clamping does not apply to glGetTexImage, except when
593 * the returned type of the image can't hold negative values.
595 if (type_needs_clamping(type
)) {
596 /* the returned image type can't have negative values */
597 if (dataType
== GL_FLOAT
||
598 dataType
== GL_HALF_FLOAT
||
599 dataType
== GL_SIGNED_NORMALIZED
||
600 format
== GL_LUMINANCE
||
601 format
== GL_LUMINANCE_ALPHA
) {
602 transferOps
|= IMAGE_CLAMP_BIT
;
606 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
607 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
608 pixels
, texImage
, transferOps
);
611 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
612 pixels
, texImage
, transferOps
);
618 * Try to do glGetTexImage() with simple memcpy().
619 * \return GL_TRUE if done, GL_FALSE otherwise
622 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
624 struct gl_texture_image
*texImage
)
626 const GLenum target
= texImage
->TexObject
->Target
;
627 GLboolean memCopy
= GL_FALSE
;
628 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
631 * Check if we can use memcpy to copy from the hardware texture
632 * format to the user's format/type.
633 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
635 if ((target
== GL_TEXTURE_1D
||
636 target
== GL_TEXTURE_2D
||
637 target
== GL_TEXTURE_RECTANGLE
||
638 _mesa_is_cube_face(target
)) &&
639 texBaseFormat
== texImage
->_BaseFormat
) {
640 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
642 ctx
->Pack
.SwapBytes
);
646 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
647 const GLint bytesPerRow
= texImage
->Width
* bpp
;
649 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
650 texImage
->Height
, format
, type
, 0, 0);
651 const GLint dstRowStride
=
652 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
656 /* map src texture buffer */
657 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
658 0, 0, texImage
->Width
, texImage
->Height
,
659 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
662 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
663 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
667 for (row
= 0; row
< texImage
->Height
; row
++) {
668 memcpy(dst
, src
, bytesPerRow
);
674 /* unmap src texture buffer */
675 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
678 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
687 * This is the software fallback for Driver.GetTexImage().
688 * All error checking will have been done before this routine is called.
689 * We'll call ctx->Driver.MapTextureImage() to access the data, then
690 * unmap with ctx->Driver.UnmapTextureImage().
693 _mesa_GetTexImage_sw(struct gl_context
*ctx
,
694 GLenum format
, GLenum type
, GLvoid
*pixels
,
695 struct gl_texture_image
*texImage
)
697 const GLuint dimensions
=
698 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
700 /* map dest buffer, if PBO */
701 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
702 /* Packing texture image into a PBO.
703 * Map the (potentially) VRAM-based buffer into our process space so
704 * we can write into it with the code below.
705 * A hardware driver might use a sophisticated blit to move the
706 * texture data to the PBO if the PBO is in VRAM along with the texture.
708 GLubyte
*buf
= (GLubyte
*)
709 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
710 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
713 /* out of memory or other unexpected error */
714 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
717 /* <pixels> was an offset into the PBO.
718 * Now make it a real, client-side pointer inside the mapped region.
720 pixels
= ADD_POINTERS(buf
, pixels
);
723 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
726 else if (format
== GL_DEPTH_COMPONENT
) {
727 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
729 else if (format
== GL_DEPTH_STENCIL_EXT
) {
730 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
732 else if (format
== GL_STENCIL_INDEX
) {
733 get_tex_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
735 else if (format
== GL_YCBCR_MESA
) {
736 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
739 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
742 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
743 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
750 * This is the software fallback for Driver.GetCompressedTexImage().
751 * All error checking will have been done before this routine is called.
754 _mesa_GetCompressedTexImage_sw(struct gl_context
*ctx
,
755 struct gl_texture_image
*texImage
,
758 const GLuint dimensions
=
759 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
760 struct compressed_pixelstore store
;
764 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
765 texImage
->Width
, texImage
->Height
,
770 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
771 /* pack texture image into a PBO */
773 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
774 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
777 /* out of memory or other unexpected error */
778 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
779 "glGetCompresssedTexImage(map PBO failed)");
782 dest
= ADD_POINTERS(dest
, img
);
787 dest
+= store
.SkipBytes
;
789 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
793 /* map src texture buffer */
794 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
795 0, 0, texImage
->Width
, texImage
->Height
,
796 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
800 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
801 memcpy(dest
, src
, store
.CopyBytesPerRow
);
802 dest
+= store
.TotalBytesPerRow
;
806 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
808 /* Advance to next slice */
809 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
812 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
816 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
817 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
823 * Validate the texture target enum supplied to glGetTex(ture)Image or
824 * glGetCompressedTex(ture)Image.
827 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
834 case GL_TEXTURE_RECTANGLE_NV
:
835 return ctx
->Extensions
.NV_texture_rectangle
;
836 case GL_TEXTURE_1D_ARRAY_EXT
:
837 case GL_TEXTURE_2D_ARRAY_EXT
:
838 return ctx
->Extensions
.EXT_texture_array
;
839 case GL_TEXTURE_CUBE_MAP_ARRAY
:
840 return ctx
->Extensions
.ARB_texture_cube_map_array
;
842 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
844 * "An INVALID_ENUM error is generated if the effective target is not
845 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
846 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
847 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
848 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
850 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
851 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
852 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
853 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
854 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
855 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
856 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
857 case GL_TEXTURE_CUBE_MAP
:
858 return dsa
? GL_TRUE
: GL_FALSE
;
866 * Do error checking for a glGetTex(ture)Image() call.
867 * \return GL_TRUE if any error, GL_FALSE if no errors.
870 getteximage_error_check(struct gl_context
*ctx
,
871 struct gl_texture_image
*texImage
,
872 GLenum target
, GLint level
,
873 GLenum format
, GLenum type
, GLsizei clientMemSize
,
874 GLvoid
*pixels
, bool dsa
)
876 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
877 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
879 const char *suffix
= dsa
? "ture" : "";
882 assert(maxLevels
!= 0);
883 if (level
< 0 || level
>= maxLevels
) {
884 _mesa_error(ctx
, GL_INVALID_VALUE
,
885 "glGetTex%sImage(level out of range)", suffix
);
890 * Format and type checking has been moved up to GetnTexImage and
891 * GetTextureImage so that it happens before getting the texImage object.
894 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
896 /* Make sure the requested image format is compatible with the
899 if (_mesa_is_color_format(format
)
900 && !_mesa_is_color_format(baseFormat
)) {
901 _mesa_error(ctx
, GL_INVALID_OPERATION
,
902 "glGetTex%sImage(format mismatch)", suffix
);
905 else if (_mesa_is_depth_format(format
)
906 && !_mesa_is_depth_format(baseFormat
)
907 && !_mesa_is_depthstencil_format(baseFormat
)) {
908 _mesa_error(ctx
, GL_INVALID_OPERATION
,
909 "glGetTex%sImage(format mismatch)", suffix
);
912 else if (_mesa_is_stencil_format(format
)
913 && !ctx
->Extensions
.ARB_texture_stencil8
) {
914 _mesa_error(ctx
, GL_INVALID_ENUM
,
915 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix
);
918 else if (_mesa_is_ycbcr_format(format
)
919 && !_mesa_is_ycbcr_format(baseFormat
)) {
920 _mesa_error(ctx
, GL_INVALID_OPERATION
,
921 "glGetTex%sImage(format mismatch)", suffix
);
924 else if (_mesa_is_depthstencil_format(format
)
925 && !_mesa_is_depthstencil_format(baseFormat
)) {
926 _mesa_error(ctx
, GL_INVALID_OPERATION
,
927 "glGetTex%sImage(format mismatch)", suffix
);
930 else if (!_mesa_is_stencil_format(format
) && _mesa_is_enum_format_integer(format
) !=
931 _mesa_is_format_integer(texImage
->TexFormat
)) {
932 _mesa_error(ctx
, GL_INVALID_OPERATION
,
933 "glGetTex%sImage(format mismatch)", suffix
);
937 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
938 texImage
->Height
, texImage
->Depth
,
939 format
, type
, clientMemSize
, pixels
)) {
940 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
941 _mesa_error(ctx
, GL_INVALID_OPERATION
,
942 "glGetTex%sImage(out of bounds PBO access)", suffix
);
944 _mesa_error(ctx
, GL_INVALID_OPERATION
,
945 "%s(out of bounds access:"
946 " bufSize (%d) is too small)",
947 dsa
? "glGetTextureImage" : "glGetnTexImageARB",
953 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
954 /* PBO should not be mapped */
955 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
956 _mesa_error(ctx
, GL_INVALID_OPERATION
,
957 "glGetTex%sImage(PBO is mapped)", suffix
);
967 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
970 * Requires caller to pass in texImage object because _mesa_GetTextureImage
971 * must handle the GL_TEXTURE_CUBE_MAP target.
973 * \param target texture target.
974 * \param level image level.
975 * \param format pixel data format for returned image.
976 * \param type pixel data type for returned image.
977 * \param bufSize size of the pixels data buffer.
978 * \param pixels returned pixel data.
979 * \param dsa True when the caller is an ARB_direct_state_access function,
983 _mesa_get_texture_image(struct gl_context
*ctx
,
984 struct gl_texture_object
*texObj
,
985 struct gl_texture_image
*texImage
, GLenum target
,
986 GLint level
, GLenum format
, GLenum type
,
987 GLsizei bufSize
, GLvoid
*pixels
, bool dsa
)
992 FLUSH_VERTICES(ctx
, 0);
995 * Legal target checking has been moved up to GetnTexImage and
996 * GetTextureImage so that it can be caught before receiving a NULL
997 * texImage object and exiting.
1000 if (getteximage_error_check(ctx
, texImage
, target
, level
, format
,
1001 type
, bufSize
, pixels
, dsa
)) {
1005 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1006 /* not an error, do nothing */
1010 if (_mesa_is_zero_size_texture(texImage
))
1013 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1014 _mesa_debug(ctx
, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
1015 " dstFmt=0x%x, dstType=0x%x\n",
1018 _mesa_get_format_name(texImage
->TexFormat
),
1019 texImage
->Width
, texImage
->Height
,
1023 _mesa_lock_texture(ctx
, texObj
);
1025 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
1027 _mesa_unlock_texture(ctx
, texObj
);
1031 * Get texture image. Called by glGetTexImage.
1033 * \param target texture target.
1034 * \param level image level.
1035 * \param format pixel data format for returned image.
1036 * \param type pixel data type for returned image.
1037 * \param bufSize size of the pixels data buffer.
1038 * \param pixels returned pixel data.
1041 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
,
1042 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1044 struct gl_texture_object
*texObj
;
1045 struct gl_texture_image
*texImage
;
1047 GET_CURRENT_CONTEXT(ctx
);
1050 * This has been moved here because a format/type mismatch can cause a NULL
1051 * texImage object, which in turn causes the mismatch error to be
1054 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1055 if (err
!= GL_NO_ERROR
) {
1056 _mesa_error(ctx
, err
, "glGetnTexImage(format/type)");
1061 * Legal target checking has been moved here to prevent exiting with a NULL
1064 if (!legal_getteximage_target(ctx
, target
, false)) {
1065 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetnTexImage(target=0x%x)",
1070 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1074 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1078 _mesa_get_texture_image(ctx
, texObj
, texImage
, target
, level
, format
, type
,
1079 bufSize
, pixels
, false);
1084 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1085 GLenum type
, GLvoid
*pixels
)
1087 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
1091 * Get texture image.
1093 * \param texture texture name.
1094 * \param level image level.
1095 * \param format pixel data format for returned image.
1096 * \param type pixel data type for returned image.
1097 * \param bufSize size of the pixels data buffer.
1098 * \param pixels returned pixel data.
1101 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
,
1102 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1104 struct gl_texture_object
*texObj
;
1105 struct gl_texture_image
*texImage
;
1109 GET_CURRENT_CONTEXT(ctx
);
1112 * This has been moved here because a format/type mismatch can cause a NULL
1113 * texImage object, which in turn causes the mismatch error to be
1116 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1117 if (err
!= GL_NO_ERROR
) {
1118 _mesa_error(ctx
, err
, "glGetTextureImage(format/type)");
1122 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glGetTextureImage");
1127 * Legal target checking has been moved here to prevent exiting with a NULL
1130 if (!legal_getteximage_target(ctx
, texObj
->Target
, true)) {
1131 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTextureImage(target=%s)",
1132 _mesa_enum_to_string(texObj
->Target
));
1136 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1137 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1139 /* Make sure the texture object is a proper cube.
1140 * (See texturesubimage in teximage.c for details on why this check is
1143 if (!_mesa_cube_level_complete(texObj
, level
)) {
1144 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1145 "glGetTextureImage(cube map incomplete)");
1149 /* Copy each face. */
1150 for (i
= 0; i
< 6; ++i
) {
1151 texImage
= texObj
->Image
[i
][level
];
1154 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1155 format
, type
, bufSize
, pixels
, true);
1157 image_stride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
1158 texImage
->Height
, format
,
1160 pixels
= (GLubyte
*) pixels
+ image_stride
;
1161 bufSize
-= image_stride
;
1165 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1169 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1170 format
, type
, bufSize
, pixels
, true);
1175 * Do error checking for a glGetCompressedTexImage() call.
1176 * \return GL_TRUE if any error, GL_FALSE if no errors.
1179 getcompressedteximage_error_check(struct gl_context
*ctx
,
1180 struct gl_texture_image
*texImage
,
1182 GLint level
, GLsizei clientMemSize
,
1183 GLvoid
*img
, bool dsa
)
1185 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1186 GLuint compressedSize
, dimensions
;
1187 const char *suffix
= dsa
? "ture" : "";
1191 if (!legal_getteximage_target(ctx
, target
, dsa
)) {
1192 _mesa_error(ctx
, GL_INVALID_ENUM
,
1193 "glGetCompressedTex%sImage(target=%s)", suffix
,
1194 _mesa_enum_to_string(target
));
1198 assert(maxLevels
!= 0);
1199 if (level
< 0 || level
>= maxLevels
) {
1200 _mesa_error(ctx
, GL_INVALID_VALUE
,
1201 "glGetCompressedTex%sImage(bad level = %d)", suffix
, level
);
1205 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1206 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1207 "glGetCompressedTex%sImage(texture is not compressed)",
1212 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
1217 /* Check for invalid pixel storage modes */
1218 dimensions
= _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
1219 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1221 "glGetCompressedTextureImage":
1222 "glGetCompressedTexImage")) {
1226 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1227 /* do bounds checking on writing to client memory */
1228 if (clientMemSize
< (GLsizei
) compressedSize
) {
1229 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1230 "%s(out of bounds access: bufSize (%d) is too small)",
1231 dsa
? "glGetCompressedTextureImage" :
1232 "glGetnCompressedTexImageARB", clientMemSize
);
1236 /* do bounds checking on PBO write */
1237 if ((const GLubyte
*) img
+ compressedSize
>
1238 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1239 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1240 "glGetCompressedTex%sImage(out of bounds PBO access)",
1245 /* make sure PBO is not mapped */
1246 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1247 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1248 "glGetCompressedTex%sImage(PBO is mapped)", suffix
);
1256 /** Implements glGetnCompressedTexImageARB, glGetCompressedTexImage, and
1257 * glGetCompressedTextureImage.
1259 * texImage must be passed in because glGetCompressedTexImage must handle the
1260 * target GL_TEXTURE_CUBE_MAP.
1263 _mesa_get_compressed_texture_image(struct gl_context
*ctx
,
1264 struct gl_texture_object
*texObj
,
1265 struct gl_texture_image
*texImage
,
1266 GLenum target
, GLint level
,
1267 GLsizei bufSize
, GLvoid
*pixels
,
1273 FLUSH_VERTICES(ctx
, 0);
1275 if (getcompressedteximage_error_check(ctx
, texImage
, target
, level
,
1276 bufSize
, pixels
, dsa
)) {
1280 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1281 /* not an error, do nothing */
1285 if (_mesa_is_zero_size_texture(texImage
))
1288 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1290 "glGetCompressedTex%sImage(tex %u) format = %s, w=%d, h=%d\n",
1291 dsa
? "ture" : "", texObj
->Name
,
1292 _mesa_get_format_name(texImage
->TexFormat
),
1293 texImage
->Width
, texImage
->Height
);
1296 _mesa_lock_texture(ctx
, texObj
);
1298 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, pixels
);
1300 _mesa_unlock_texture(ctx
, texObj
);
1304 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1307 struct gl_texture_object
*texObj
;
1308 struct gl_texture_image
*texImage
;
1309 GET_CURRENT_CONTEXT(ctx
);
1311 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1315 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1319 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
, target
, level
,
1320 bufSize
, img
, false);
1324 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1326 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);
1330 * Get compressed texture image.
1332 * \param texture texture name.
1333 * \param level image level.
1334 * \param bufSize size of the pixels data buffer.
1335 * \param pixels returned pixel data.
1338 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1339 GLsizei bufSize
, GLvoid
*pixels
)
1341 struct gl_texture_object
*texObj
;
1342 struct gl_texture_image
*texImage
;
1345 GET_CURRENT_CONTEXT(ctx
);
1347 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
1348 "glGetCompressedTextureImage");
1352 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1353 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1355 /* Make sure the texture object is a proper cube.
1356 * (See texturesubimage in teximage.c for details on why this check is
1359 if (!_mesa_cube_level_complete(texObj
, level
)) {
1360 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1361 "glGetCompressedTextureImage(cube map incomplete)");
1365 /* Copy each face. */
1366 for (i
= 0; i
< 6; ++i
) {
1367 texImage
= texObj
->Image
[i
][level
];
1370 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1371 texObj
->Target
, level
,
1372 bufSize
, pixels
, true);
1374 /* Compressed images don't have a client format */
1375 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
1377 texImage
->Height
, 1);
1379 pixels
= (GLubyte
*) pixels
+ image_stride
;
1380 bufSize
-= image_stride
;
1384 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1388 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1389 texObj
->Target
, level
, bufSize
,