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.GetTexSubImage().
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_GetTexSubImage_sw(struct gl_context
*ctx
,
694 GLint xoffset
, GLint yoffset
, GLint zoffset
,
695 GLsizei width
, GLsizei height
, GLint depth
,
696 GLenum format
, GLenum type
, GLvoid
*pixels
,
697 struct gl_texture_image
*texImage
)
699 const GLuint dimensions
=
700 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
702 /* map dest buffer, if PBO */
703 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
704 /* Packing texture image into a PBO.
705 * Map the (potentially) VRAM-based buffer into our process space so
706 * we can write into it with the code below.
707 * A hardware driver might use a sophisticated blit to move the
708 * texture data to the PBO if the PBO is in VRAM along with the texture.
710 GLubyte
*buf
= (GLubyte
*)
711 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
712 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
715 /* out of memory or other unexpected error */
716 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
719 /* <pixels> was an offset into the PBO.
720 * Now make it a real, client-side pointer inside the mapped region.
722 pixels
= ADD_POINTERS(buf
, pixels
);
725 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
728 else if (format
== GL_DEPTH_COMPONENT
) {
729 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
731 else if (format
== GL_DEPTH_STENCIL_EXT
) {
732 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
734 else if (format
== GL_STENCIL_INDEX
) {
735 get_tex_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
737 else if (format
== GL_YCBCR_MESA
) {
738 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
741 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
744 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
745 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
752 * This is the software fallback for Driver.GetCompressedTexImage().
753 * All error checking will have been done before this routine is called.
756 _mesa_GetCompressedTexImage_sw(struct gl_context
*ctx
,
757 struct gl_texture_image
*texImage
,
760 const GLuint dimensions
=
761 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
762 struct compressed_pixelstore store
;
766 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
767 texImage
->Width
, texImage
->Height
,
772 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
773 /* pack texture image into a PBO */
775 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
776 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
779 /* out of memory or other unexpected error */
780 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
781 "glGetCompresssedTexImage(map PBO failed)");
784 dest
= ADD_POINTERS(dest
, img
);
789 dest
+= store
.SkipBytes
;
791 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
795 /* map src texture buffer */
796 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
797 0, 0, texImage
->Width
, texImage
->Height
,
798 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
802 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
803 memcpy(dest
, src
, store
.CopyBytesPerRow
);
804 dest
+= store
.TotalBytesPerRow
;
808 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
810 /* Advance to next slice */
811 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
814 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
818 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
819 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
825 * Validate the texture target enum supplied to glGetTex(ture)Image or
826 * glGetCompressedTex(ture)Image.
829 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
836 case GL_TEXTURE_RECTANGLE_NV
:
837 return ctx
->Extensions
.NV_texture_rectangle
;
838 case GL_TEXTURE_1D_ARRAY_EXT
:
839 case GL_TEXTURE_2D_ARRAY_EXT
:
840 return ctx
->Extensions
.EXT_texture_array
;
841 case GL_TEXTURE_CUBE_MAP_ARRAY
:
842 return ctx
->Extensions
.ARB_texture_cube_map_array
;
844 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
846 * "An INVALID_ENUM error is generated if the effective target is not
847 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
848 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
849 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
850 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
852 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
853 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
854 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
855 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
856 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
857 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
858 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
859 case GL_TEXTURE_CUBE_MAP
:
860 return dsa
? GL_TRUE
: GL_FALSE
;
868 * Do error checking for a glGetTex(ture)Image() call.
869 * \return GL_TRUE if any error, GL_FALSE if no errors.
872 getteximage_error_check(struct gl_context
*ctx
,
873 struct gl_texture_image
*texImage
,
874 GLenum target
, GLint level
,
875 GLenum format
, GLenum type
, GLsizei clientMemSize
,
876 GLvoid
*pixels
, bool dsa
)
878 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
879 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
881 const char *suffix
= dsa
? "ture" : "";
884 assert(maxLevels
!= 0);
885 if (level
< 0 || level
>= maxLevels
) {
886 _mesa_error(ctx
, GL_INVALID_VALUE
,
887 "glGetTex%sImage(level out of range)", suffix
);
892 * Format and type checking has been moved up to GetnTexImage and
893 * GetTextureImage so that it happens before getting the texImage object.
896 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
898 /* Make sure the requested image format is compatible with the
901 if (_mesa_is_color_format(format
)
902 && !_mesa_is_color_format(baseFormat
)) {
903 _mesa_error(ctx
, GL_INVALID_OPERATION
,
904 "glGetTex%sImage(format mismatch)", suffix
);
907 else if (_mesa_is_depth_format(format
)
908 && !_mesa_is_depth_format(baseFormat
)
909 && !_mesa_is_depthstencil_format(baseFormat
)) {
910 _mesa_error(ctx
, GL_INVALID_OPERATION
,
911 "glGetTex%sImage(format mismatch)", suffix
);
914 else if (_mesa_is_stencil_format(format
)
915 && !ctx
->Extensions
.ARB_texture_stencil8
) {
916 _mesa_error(ctx
, GL_INVALID_ENUM
,
917 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix
);
920 else if (_mesa_is_ycbcr_format(format
)
921 && !_mesa_is_ycbcr_format(baseFormat
)) {
922 _mesa_error(ctx
, GL_INVALID_OPERATION
,
923 "glGetTex%sImage(format mismatch)", suffix
);
926 else if (_mesa_is_depthstencil_format(format
)
927 && !_mesa_is_depthstencil_format(baseFormat
)) {
928 _mesa_error(ctx
, GL_INVALID_OPERATION
,
929 "glGetTex%sImage(format mismatch)", suffix
);
932 else if (!_mesa_is_stencil_format(format
) && _mesa_is_enum_format_integer(format
) !=
933 _mesa_is_format_integer(texImage
->TexFormat
)) {
934 _mesa_error(ctx
, GL_INVALID_OPERATION
,
935 "glGetTex%sImage(format mismatch)", suffix
);
939 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
940 texImage
->Height
, texImage
->Depth
,
941 format
, type
, clientMemSize
, pixels
)) {
942 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
943 _mesa_error(ctx
, GL_INVALID_OPERATION
,
944 "glGetTex%sImage(out of bounds PBO access)", suffix
);
946 _mesa_error(ctx
, GL_INVALID_OPERATION
,
947 "%s(out of bounds access:"
948 " bufSize (%d) is too small)",
949 dsa
? "glGetTextureImage" : "glGetnTexImageARB",
955 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
956 /* PBO should not be mapped */
957 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
958 _mesa_error(ctx
, GL_INVALID_OPERATION
,
959 "glGetTex%sImage(PBO is mapped)", suffix
);
969 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
972 * Requires caller to pass in texImage object because _mesa_GetTextureImage
973 * must handle the GL_TEXTURE_CUBE_MAP target.
975 * \param target texture target.
976 * \param level image level.
977 * \param format pixel data format for returned image.
978 * \param type pixel data type for returned image.
979 * \param bufSize size of the pixels data buffer.
980 * \param pixels returned pixel data.
981 * \param dsa True when the caller is an ARB_direct_state_access function,
985 _mesa_get_texture_image(struct gl_context
*ctx
,
986 struct gl_texture_object
*texObj
,
987 struct gl_texture_image
*texImage
, GLenum target
,
988 GLint level
, GLenum format
, GLenum type
,
989 GLsizei bufSize
, GLvoid
*pixels
, bool dsa
)
994 FLUSH_VERTICES(ctx
, 0);
997 * Legal target checking has been moved up to GetnTexImage and
998 * GetTextureImage so that it can be caught before receiving a NULL
999 * texImage object and exiting.
1002 if (getteximage_error_check(ctx
, texImage
, target
, level
, format
,
1003 type
, bufSize
, pixels
, dsa
)) {
1007 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1008 /* not an error, do nothing */
1012 if (_mesa_is_zero_size_texture(texImage
))
1015 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1016 _mesa_debug(ctx
, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
1017 " dstFmt=0x%x, dstType=0x%x\n",
1020 _mesa_get_format_name(texImage
->TexFormat
),
1021 texImage
->Width
, texImage
->Height
,
1025 _mesa_lock_texture(ctx
, texObj
);
1027 ctx
->Driver
.GetTexSubImage(ctx
, 0, 0, 0,
1028 texImage
->Width
, texImage
->Height
,
1030 format
, type
, pixels
, texImage
);
1032 _mesa_unlock_texture(ctx
, texObj
);
1036 * Get texture image. Called by glGetTexImage.
1038 * \param target texture target.
1039 * \param level image level.
1040 * \param format pixel data format for returned image.
1041 * \param type pixel data type for returned image.
1042 * \param bufSize size of the pixels data buffer.
1043 * \param pixels returned pixel data.
1046 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
,
1047 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1049 struct gl_texture_object
*texObj
;
1050 struct gl_texture_image
*texImage
;
1052 GET_CURRENT_CONTEXT(ctx
);
1055 * This has been moved here because a format/type mismatch can cause a NULL
1056 * texImage object, which in turn causes the mismatch error to be
1059 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1060 if (err
!= GL_NO_ERROR
) {
1061 _mesa_error(ctx
, err
, "glGetnTexImage(format/type)");
1066 * Legal target checking has been moved here to prevent exiting with a NULL
1069 if (!legal_getteximage_target(ctx
, target
, false)) {
1070 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetnTexImage(target=0x%x)",
1075 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1079 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1083 _mesa_get_texture_image(ctx
, texObj
, texImage
, target
, level
, format
, type
,
1084 bufSize
, pixels
, false);
1089 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1090 GLenum type
, GLvoid
*pixels
)
1092 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
1096 * Get texture image.
1098 * \param texture texture name.
1099 * \param level image level.
1100 * \param format pixel data format for returned image.
1101 * \param type pixel data type for returned image.
1102 * \param bufSize size of the pixels data buffer.
1103 * \param pixels returned pixel data.
1106 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
,
1107 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1109 struct gl_texture_object
*texObj
;
1110 struct gl_texture_image
*texImage
;
1114 GET_CURRENT_CONTEXT(ctx
);
1117 * This has been moved here because a format/type mismatch can cause a NULL
1118 * texImage object, which in turn causes the mismatch error to be
1121 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1122 if (err
!= GL_NO_ERROR
) {
1123 _mesa_error(ctx
, err
, "glGetTextureImage(format/type)");
1127 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glGetTextureImage");
1132 * Legal target checking has been moved here to prevent exiting with a NULL
1135 if (!legal_getteximage_target(ctx
, texObj
->Target
, true)) {
1136 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTextureImage(target=%s)",
1137 _mesa_enum_to_string(texObj
->Target
));
1141 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1142 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1144 /* Make sure the texture object is a proper cube.
1145 * (See texturesubimage in teximage.c for details on why this check is
1148 if (!_mesa_cube_level_complete(texObj
, level
)) {
1149 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1150 "glGetTextureImage(cube map incomplete)");
1154 /* Copy each face. */
1155 for (i
= 0; i
< 6; ++i
) {
1156 texImage
= texObj
->Image
[i
][level
];
1159 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1160 format
, type
, bufSize
, pixels
, true);
1162 image_stride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
1163 texImage
->Height
, format
,
1165 pixels
= (GLubyte
*) pixels
+ image_stride
;
1166 bufSize
-= image_stride
;
1170 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1174 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1175 format
, type
, bufSize
, pixels
, true);
1180 * Do error checking for a glGetCompressedTexImage() call.
1181 * \return GL_TRUE if any error, GL_FALSE if no errors.
1184 getcompressedteximage_error_check(struct gl_context
*ctx
,
1185 struct gl_texture_image
*texImage
,
1187 GLint level
, GLsizei clientMemSize
,
1188 GLvoid
*img
, bool dsa
)
1190 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1191 GLuint compressedSize
, dimensions
;
1192 const char *suffix
= dsa
? "ture" : "";
1196 if (!legal_getteximage_target(ctx
, target
, dsa
)) {
1197 _mesa_error(ctx
, GL_INVALID_ENUM
,
1198 "glGetCompressedTex%sImage(target=%s)", suffix
,
1199 _mesa_enum_to_string(target
));
1203 assert(maxLevels
!= 0);
1204 if (level
< 0 || level
>= maxLevels
) {
1205 _mesa_error(ctx
, GL_INVALID_VALUE
,
1206 "glGetCompressedTex%sImage(bad level = %d)", suffix
, level
);
1210 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1211 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1212 "glGetCompressedTex%sImage(texture is not compressed)",
1217 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
1222 /* Check for invalid pixel storage modes */
1223 dimensions
= _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
1224 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1226 "glGetCompressedTextureImage":
1227 "glGetCompressedTexImage")) {
1231 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1232 /* do bounds checking on writing to client memory */
1233 if (clientMemSize
< (GLsizei
) compressedSize
) {
1234 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1235 "%s(out of bounds access: bufSize (%d) is too small)",
1236 dsa
? "glGetCompressedTextureImage" :
1237 "glGetnCompressedTexImageARB", clientMemSize
);
1241 /* do bounds checking on PBO write */
1242 if ((const GLubyte
*) img
+ compressedSize
>
1243 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1244 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1245 "glGetCompressedTex%sImage(out of bounds PBO access)",
1250 /* make sure PBO is not mapped */
1251 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1252 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1253 "glGetCompressedTex%sImage(PBO is mapped)", suffix
);
1261 /** Implements glGetnCompressedTexImageARB, glGetCompressedTexImage, and
1262 * glGetCompressedTextureImage.
1264 * texImage must be passed in because glGetCompressedTexImage must handle the
1265 * target GL_TEXTURE_CUBE_MAP.
1268 _mesa_get_compressed_texture_image(struct gl_context
*ctx
,
1269 struct gl_texture_object
*texObj
,
1270 struct gl_texture_image
*texImage
,
1271 GLenum target
, GLint level
,
1272 GLsizei bufSize
, GLvoid
*pixels
,
1278 FLUSH_VERTICES(ctx
, 0);
1280 if (getcompressedteximage_error_check(ctx
, texImage
, target
, level
,
1281 bufSize
, pixels
, dsa
)) {
1285 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1286 /* not an error, do nothing */
1290 if (_mesa_is_zero_size_texture(texImage
))
1293 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1295 "glGetCompressedTex%sImage(tex %u) format = %s, w=%d, h=%d\n",
1296 dsa
? "ture" : "", texObj
->Name
,
1297 _mesa_get_format_name(texImage
->TexFormat
),
1298 texImage
->Width
, texImage
->Height
);
1301 _mesa_lock_texture(ctx
, texObj
);
1303 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, pixels
);
1305 _mesa_unlock_texture(ctx
, texObj
);
1309 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1312 struct gl_texture_object
*texObj
;
1313 struct gl_texture_image
*texImage
;
1314 GET_CURRENT_CONTEXT(ctx
);
1316 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1320 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1324 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
, target
, level
,
1325 bufSize
, img
, false);
1329 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1331 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);
1335 * Get compressed texture image.
1337 * \param texture texture name.
1338 * \param level image level.
1339 * \param bufSize size of the pixels data buffer.
1340 * \param pixels returned pixel data.
1343 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1344 GLsizei bufSize
, GLvoid
*pixels
)
1346 struct gl_texture_object
*texObj
;
1347 struct gl_texture_image
*texImage
;
1350 GET_CURRENT_CONTEXT(ctx
);
1352 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
1353 "glGetCompressedTextureImage");
1357 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1358 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1360 /* Make sure the texture object is a proper cube.
1361 * (See texturesubimage in teximage.c for details on why this check is
1364 if (!_mesa_cube_level_complete(texObj
, level
)) {
1365 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1366 "glGetCompressedTextureImage(cube map incomplete)");
1370 /* Copy each face. */
1371 for (i
= 0; i
< 6; ++i
) {
1372 texImage
= texObj
->Image
[i
][level
];
1375 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1376 texObj
->Target
, level
,
1377 bufSize
, pixels
, true);
1379 /* Compressed images don't have a client format */
1380 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
1382 texImage
->Height
, 1);
1384 pixels
= (GLubyte
*) pixels
+ image_stride
;
1385 bufSize
-= image_stride
;
1389 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1393 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1394 texObj
->Target
, level
, bufSize
,