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"
52 * Can the given type represent negative values?
54 static inline GLboolean
55 type_needs_clamping(GLenum type
)
62 case GL_HALF_FLOAT_ARB
:
63 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
64 case GL_UNSIGNED_INT_5_9_9_9_REV
:
73 * glGetTexImage for depth/Z pixels.
76 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
77 GLenum format
, GLenum type
, GLvoid
*pixels
,
78 struct gl_texture_image
*texImage
)
80 const GLint width
= texImage
->Width
;
81 GLint height
= texImage
->Height
;
82 GLint depth
= texImage
->Depth
;
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 for (img
= 0; img
< depth
; img
++) {
100 /* map src texture buffer */
101 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
102 0, 0, width
, height
, GL_MAP_READ_BIT
,
103 &srcMap
, &srcRowStride
);
106 for (row
= 0; row
< height
; row
++) {
107 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
108 width
, height
, format
, type
,
110 const GLubyte
*src
= srcMap
+ row
* srcRowStride
;
111 _mesa_unpack_float_z_row(texImage
->TexFormat
, width
, src
, depthRow
);
112 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
115 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
118 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
128 * glGetTexImage for depth/stencil pixels.
131 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
132 GLenum format
, GLenum type
, GLvoid
*pixels
,
133 struct gl_texture_image
*texImage
)
135 const GLint width
= texImage
->Width
;
136 const GLint height
= texImage
->Height
;
137 const GLint depth
= texImage
->Depth
;
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 for (img
= 0; img
< depth
; img
++) {
148 /* map src texture buffer */
149 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
150 0, 0, width
, height
, GL_MAP_READ_BIT
,
151 &srcMap
, &rowstride
);
154 for (row
= 0; row
< height
; row
++) {
155 const GLubyte
*src
= srcMap
+ row
* rowstride
;
156 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
157 width
, height
, format
, type
,
159 _mesa_unpack_depth_stencil_row(texImage
->TexFormat
,
161 (const GLuint
*) src
,
163 if (ctx
->Pack
.SwapBytes
) {
164 _mesa_swap4((GLuint
*) dest
, width
);
168 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
171 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
179 * glGetTexImage for YCbCr pixels.
182 get_tex_ycbcr(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 for (img
= 0; img
< depth
; img
++) {
195 /* map src texture buffer */
196 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
197 0, 0, width
, height
, GL_MAP_READ_BIT
,
198 &srcMap
, &rowstride
);
201 for (row
= 0; row
< height
; row
++) {
202 const GLubyte
*src
= srcMap
+ row
* rowstride
;
203 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
204 width
, height
, format
, type
,
206 memcpy(dest
, src
, width
* sizeof(GLushort
));
208 /* check for byte swapping */
209 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
210 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
211 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
212 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
213 if (!ctx
->Pack
.SwapBytes
)
214 _mesa_swap2((GLushort
*) dest
, width
);
216 else if (ctx
->Pack
.SwapBytes
) {
217 _mesa_swap2((GLushort
*) dest
, width
);
221 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
224 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
232 * Get a color texture image with decompression.
235 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
236 GLenum format
, GLenum type
, GLvoid
*pixels
,
237 struct gl_texture_image
*texImage
,
238 GLbitfield transferOps
)
240 /* don't want to apply sRGB -> RGB conversion here so override the format */
241 const mesa_format texFormat
=
242 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
243 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
244 const GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, format
);
245 GLenum rebaseFormat
= GL_NONE
;
246 const GLuint width
= texImage
->Width
;
247 const GLuint height
= texImage
->Height
;
248 const GLuint depth
= texImage
->Depth
;
249 GLfloat
*tempImage
, *tempSlice
, *srcRow
;
252 /* Decompress into temp float buffer, then pack into user buffer */
253 tempImage
= malloc(width
* height
* depth
254 * 4 * sizeof(GLfloat
));
256 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
260 /* Decompress the texture image slices - results in 'tempImage' */
261 for (slice
= 0; slice
< depth
; slice
++) {
265 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
267 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
270 &srcMap
, &srcRowStride
);
272 _mesa_decompress_image(texFormat
, width
, height
,
273 srcMap
, srcRowStride
, tempSlice
);
275 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
278 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
284 if (baseFormat
== GL_LUMINANCE
||
285 baseFormat
== GL_INTENSITY
||
286 baseFormat
== GL_LUMINANCE_ALPHA
) {
287 /* If a luminance (or intensity) texture is read back as RGB(A), the
288 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
291 rebaseFormat
= texImage
->_BaseFormat
;
293 else if ((baseFormat
== GL_RGBA
||
294 baseFormat
== GL_RGB
||
295 baseFormat
== GL_RG
) &&
296 (destBaseFormat
== GL_LUMINANCE
||
297 destBaseFormat
== GL_LUMINANCE_ALPHA
||
298 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
299 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
300 /* If we're reading back an RGB(A) texture as luminance then we need
301 * to return L=tex(R). Note, that's different from glReadPixels which
304 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
308 _mesa_rebase_rgba_float(width
* height
, (GLfloat (*)[4]) tempImage
,
312 tempSlice
= tempImage
;
313 for (slice
= 0; slice
< depth
; slice
++) {
315 for (row
= 0; row
< height
; row
++) {
316 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
317 width
, height
, format
, type
,
320 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) srcRow
,
321 format
, type
, dest
, &ctx
->Pack
, transferOps
);
324 tempSlice
+= 4 * width
* height
;
332 * Return a base GL format given the user-requested format
333 * for glGetTexImage().
336 _mesa_base_pack_format(GLenum format
)
341 case GL_BGRA_INTEGER
:
342 case GL_RGBA_INTEGER
:
350 case GL_GREEN_INTEGER
:
352 case GL_BLUE_INTEGER
:
354 case GL_ALPHA_INTEGER
:
356 case GL_LUMINANCE_INTEGER_EXT
:
358 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
359 return GL_LUMINANCE_ALPHA
;
367 * Get an uncompressed color texture image.
370 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
371 GLenum format
, GLenum type
, GLvoid
*pixels
,
372 struct gl_texture_image
*texImage
,
373 GLbitfield transferOps
)
375 /* don't want to apply sRGB -> RGB conversion here so override the format */
376 const mesa_format texFormat
=
377 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
378 const GLuint width
= texImage
->Width
;
379 GLenum destBaseFormat
= _mesa_base_pack_format(format
);
380 GLenum rebaseFormat
= GL_NONE
;
381 GLuint height
= texImage
->Height
;
382 GLuint depth
= texImage
->Depth
;
385 GLuint (*rgba_uint
)[4];
386 GLboolean tex_is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
387 GLboolean tex_is_uint
= _mesa_is_format_unsigned(texImage
->TexFormat
);
388 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
390 /* Allocate buffer for one row of texels */
391 rgba
= malloc(4 * width
* sizeof(GLfloat
));
392 rgba_uint
= (GLuint (*)[4]) rgba
;
394 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
398 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
403 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
404 texImage
->_BaseFormat
== GL_INTENSITY
||
405 texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
406 /* If a luminance (or intensity) texture is read back as RGB(A), the
407 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
410 rebaseFormat
= texImage
->_BaseFormat
;
412 else if ((texImage
->_BaseFormat
== GL_RGBA
||
413 texImage
->_BaseFormat
== GL_RGB
||
414 texImage
->_BaseFormat
== GL_RG
) &&
415 (destBaseFormat
== GL_LUMINANCE
||
416 destBaseFormat
== GL_LUMINANCE_ALPHA
||
417 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
418 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
419 /* If we're reading back an RGB(A) texture as luminance then we need
420 * to return L=tex(R). Note, that's different from glReadPixels which
423 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
425 else if (texImage
->_BaseFormat
!= texBaseFormat
) {
426 /* The internal format and the real format differ, so we can't rely
427 * on the unpack functions setting the correct constant values.
428 * (e.g. reading back GL_RGB8 which is actually RGBA won't set alpha=1)
430 switch (texImage
->_BaseFormat
) {
432 if ((texBaseFormat
== GL_RGBA
||
433 texBaseFormat
== GL_RGB
||
434 texBaseFormat
== GL_RG
) &&
435 (destBaseFormat
== GL_RGBA
||
436 destBaseFormat
== GL_RGB
||
437 destBaseFormat
== GL_RG
||
438 destBaseFormat
== GL_GREEN
)) {
439 rebaseFormat
= texImage
->_BaseFormat
;
444 if ((texBaseFormat
== GL_RGBA
||
445 texBaseFormat
== GL_RGB
) &&
446 (destBaseFormat
== GL_RGBA
||
447 destBaseFormat
== GL_RGB
||
448 destBaseFormat
== GL_BLUE
)) {
449 rebaseFormat
= texImage
->_BaseFormat
;
454 if (texBaseFormat
== GL_RGBA
&&
455 (destBaseFormat
== GL_RGBA
||
456 destBaseFormat
== GL_ALPHA
||
457 destBaseFormat
== GL_LUMINANCE_ALPHA
)) {
458 rebaseFormat
= texImage
->_BaseFormat
;
463 if (destBaseFormat
!= GL_ALPHA
) {
464 rebaseFormat
= texImage
->_BaseFormat
;
470 for (img
= 0; img
< depth
; img
++) {
474 /* map src texture buffer */
475 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
476 0, 0, width
, height
, GL_MAP_READ_BIT
,
477 &srcMap
, &rowstride
);
479 for (row
= 0; row
< height
; row
++) {
480 const GLubyte
*src
= srcMap
+ row
* rowstride
;
481 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
482 width
, height
, format
, type
,
485 if (tex_is_integer
) {
486 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
488 _mesa_rebase_rgba_uint(width
, rgba_uint
, rebaseFormat
);
490 _mesa_pack_rgba_span_from_uints(ctx
, width
,
491 (GLuint (*)[4]) rgba_uint
,
494 _mesa_pack_rgba_span_from_ints(ctx
, width
,
495 (GLint (*)[4]) rgba_uint
,
499 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
501 _mesa_rebase_rgba_float(width
, rgba
, rebaseFormat
);
502 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
504 &ctx
->Pack
, transferOps
);
508 /* Unmap the src texture buffer */
509 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
512 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
522 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
523 * Compressed textures are handled here as well.
526 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
527 GLenum format
, GLenum type
, GLvoid
*pixels
,
528 struct gl_texture_image
*texImage
)
530 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
531 GLbitfield transferOps
= 0x0;
533 /* In general, clamping does not apply to glGetTexImage, except when
534 * the returned type of the image can't hold negative values.
536 if (type_needs_clamping(type
)) {
537 /* the returned image type can't have negative values */
538 if (dataType
== GL_FLOAT
||
539 dataType
== GL_HALF_FLOAT
||
540 dataType
== GL_SIGNED_NORMALIZED
||
541 format
== GL_LUMINANCE
||
542 format
== GL_LUMINANCE_ALPHA
) {
543 transferOps
|= IMAGE_CLAMP_BIT
;
547 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
548 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
549 pixels
, texImage
, transferOps
);
552 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
553 pixels
, texImage
, transferOps
);
559 * Try to do glGetTexImage() with simple memcpy().
560 * \return GL_TRUE if done, GL_FALSE otherwise
563 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
565 struct gl_texture_image
*texImage
)
567 const GLenum target
= texImage
->TexObject
->Target
;
568 GLboolean memCopy
= GL_FALSE
;
569 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
572 * Check if we can use memcpy to copy from the hardware texture
573 * format to the user's format/type.
574 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
576 if ((target
== GL_TEXTURE_1D
||
577 target
== GL_TEXTURE_2D
||
578 target
== GL_TEXTURE_RECTANGLE
||
579 _mesa_is_cube_face(target
)) &&
580 texBaseFormat
== texImage
->_BaseFormat
) {
581 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
583 ctx
->Pack
.SwapBytes
);
587 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
588 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
590 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
591 texImage
->Height
, format
, type
, 0, 0);
592 const GLint dstRowStride
=
593 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
597 /* map src texture buffer */
598 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
599 0, 0, texImage
->Width
, texImage
->Height
,
600 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
603 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
604 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
608 for (row
= 0; row
< texImage
->Height
; row
++) {
609 memcpy(dst
, src
, bytesPerRow
);
615 /* unmap src texture buffer */
616 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
619 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
628 * This is the software fallback for Driver.GetTexImage().
629 * All error checking will have been done before this routine is called.
630 * We'll call ctx->Driver.MapTextureImage() to access the data, then
631 * unmap with ctx->Driver.UnmapTextureImage().
634 _mesa_GetTexImage_sw(struct gl_context
*ctx
,
635 GLenum format
, GLenum type
, GLvoid
*pixels
,
636 struct gl_texture_image
*texImage
)
638 const GLuint dimensions
=
639 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
641 /* map dest buffer, if PBO */
642 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
643 /* Packing texture image into a PBO.
644 * Map the (potentially) VRAM-based buffer into our process space so
645 * we can write into it with the code below.
646 * A hardware driver might use a sophisticated blit to move the
647 * texture data to the PBO if the PBO is in VRAM along with the texture.
649 GLubyte
*buf
= (GLubyte
*)
650 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
651 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
654 /* out of memory or other unexpected error */
655 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
658 /* <pixels> was an offset into the PBO.
659 * Now make it a real, client-side pointer inside the mapped region.
661 pixels
= ADD_POINTERS(buf
, pixels
);
664 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
667 else if (format
== GL_DEPTH_COMPONENT
) {
668 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
670 else if (format
== GL_DEPTH_STENCIL_EXT
) {
671 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
673 else if (format
== GL_YCBCR_MESA
) {
674 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
677 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
680 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
681 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
688 * This is the software fallback for Driver.GetCompressedTexImage().
689 * All error checking will have been done before this routine is called.
692 _mesa_GetCompressedTexImage_sw(struct gl_context
*ctx
,
693 struct gl_texture_image
*texImage
,
696 const GLuint dimensions
=
697 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
698 struct compressed_pixelstore store
;
702 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
703 texImage
->Width
, texImage
->Height
,
708 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
709 /* pack texture image into a PBO */
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
,
717 "glGetCompresssedTexImage(map PBO failed)");
720 dest
= ADD_POINTERS(dest
, img
);
725 dest
+= store
.SkipBytes
;
727 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
731 /* map src texture buffer */
732 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
733 0, 0, texImage
->Width
, texImage
->Height
,
734 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
738 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
739 memcpy(dest
, src
, store
.CopyBytesPerRow
);
740 dest
+= store
.TotalBytesPerRow
;
744 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
746 /* Advance to next slice */
747 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
750 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
754 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
755 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
761 * Validate the texture target enum supplied to glGetTex(ture)Image or
762 * glGetCompressedTex(ture)Image.
765 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
772 case GL_TEXTURE_RECTANGLE_NV
:
773 return ctx
->Extensions
.NV_texture_rectangle
;
774 case GL_TEXTURE_1D_ARRAY_EXT
:
775 case GL_TEXTURE_2D_ARRAY_EXT
:
776 return ctx
->Extensions
.EXT_texture_array
;
777 case GL_TEXTURE_CUBE_MAP_ARRAY
:
778 return ctx
->Extensions
.ARB_texture_cube_map_array
;
780 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
782 * "An INVALID_ENUM error is generated if the effective target is not
783 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
784 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
785 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
786 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
788 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
789 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
790 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
791 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
792 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
793 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
794 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
795 case GL_TEXTURE_CUBE_MAP
:
796 return dsa
? GL_TRUE
: GL_FALSE
;
804 * Do error checking for a glGetTex(ture)Image() call.
805 * \return GL_TRUE if any error, GL_FALSE if no errors.
808 getteximage_error_check(struct gl_context
*ctx
,
809 struct gl_texture_image
*texImage
,
810 GLenum target
, GLint level
,
811 GLenum format
, GLenum type
, GLsizei clientMemSize
,
812 GLvoid
*pixels
, bool dsa
)
816 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
817 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
819 const char *suffix
= dsa
? "ture" : "";
821 assert(maxLevels
!= 0);
822 if (level
< 0 || level
>= maxLevels
) {
823 _mesa_error(ctx
, GL_INVALID_VALUE
,
824 "glGetTex%sImage(level out of range)", suffix
);
829 * Format and type checking has been moved up to GetnTexImage and
830 * GetTextureImage so that it happens before getting the texImage object.
833 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
835 /* Make sure the requested image format is compatible with the
838 if (_mesa_is_color_format(format
)
839 && !_mesa_is_color_format(baseFormat
)) {
840 _mesa_error(ctx
, GL_INVALID_OPERATION
,
841 "glGetTex%sImage(format mismatch)", suffix
);
844 else if (_mesa_is_depth_format(format
)
845 && !_mesa_is_depth_format(baseFormat
)
846 && !_mesa_is_depthstencil_format(baseFormat
)) {
847 _mesa_error(ctx
, GL_INVALID_OPERATION
,
848 "glGetTex%sImage(format mismatch)", suffix
);
851 else if (_mesa_is_stencil_format(format
)
852 && !ctx
->Extensions
.ARB_texture_stencil8
) {
853 _mesa_error(ctx
, GL_INVALID_ENUM
,
854 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix
);
857 else if (_mesa_is_ycbcr_format(format
)
858 && !_mesa_is_ycbcr_format(baseFormat
)) {
859 _mesa_error(ctx
, GL_INVALID_OPERATION
,
860 "glGetTex%sImage(format mismatch)", suffix
);
863 else if (_mesa_is_depthstencil_format(format
)
864 && !_mesa_is_depthstencil_format(baseFormat
)) {
865 _mesa_error(ctx
, GL_INVALID_OPERATION
,
866 "glGetTex%sImage(format mismatch)", suffix
);
869 else if (_mesa_is_enum_format_integer(format
) !=
870 _mesa_is_format_integer(texImage
->TexFormat
)) {
871 _mesa_error(ctx
, GL_INVALID_OPERATION
,
872 "glGetTex%sImage(format mismatch)", suffix
);
876 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
877 texImage
->Height
, texImage
->Depth
,
878 format
, type
, clientMemSize
, pixels
)) {
879 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
880 _mesa_error(ctx
, GL_INVALID_OPERATION
,
881 "glGetTex%sImage(out of bounds PBO access)", suffix
);
883 _mesa_error(ctx
, GL_INVALID_OPERATION
,
884 "%s(out of bounds access:"
885 " bufSize (%d) is too small)",
886 dsa
? "glGetTextureImage" : "glGetnTexImageARB",
892 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
893 /* PBO should not be mapped */
894 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
895 _mesa_error(ctx
, GL_INVALID_OPERATION
,
896 "glGetTex%sImage(PBO is mapped)", suffix
);
906 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
909 * Requires caller to pass in texImage object because _mesa_GetTextureImage
910 * must handle the GL_TEXTURE_CUBE_MAP target.
912 * \param target texture target.
913 * \param level image level.
914 * \param format pixel data format for returned image.
915 * \param type pixel data type for returned image.
916 * \param bufSize size of the pixels data buffer.
917 * \param pixels returned pixel data.
918 * \param dsa True when the caller is an ARB_direct_state_access function,
922 _mesa_get_texture_image(struct gl_context
*ctx
,
923 struct gl_texture_object
*texObj
,
924 struct gl_texture_image
*texImage
, GLenum target
,
925 GLint level
, GLenum format
, GLenum type
,
926 GLsizei bufSize
, GLvoid
*pixels
, bool dsa
)
931 FLUSH_VERTICES(ctx
, 0);
934 * Legal target checking has been moved up to GetnTexImage and
935 * GetTextureImage so that it can be caught before receiving a NULL
936 * texImage object and exiting.
939 if (getteximage_error_check(ctx
, texImage
, target
, level
, format
,
940 type
, bufSize
, pixels
, dsa
)) {
944 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
945 /* not an error, do nothing */
949 if (_mesa_is_zero_size_texture(texImage
))
952 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
953 _mesa_debug(ctx
, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
954 " dstFmt=0x%x, dstType=0x%x\n",
957 _mesa_get_format_name(texImage
->TexFormat
),
958 texImage
->Width
, texImage
->Height
,
962 _mesa_lock_texture(ctx
, texObj
);
964 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
966 _mesa_unlock_texture(ctx
, texObj
);
970 * Get texture image. Called by glGetTexImage.
972 * \param target texture target.
973 * \param level image level.
974 * \param format pixel data format for returned image.
975 * \param type pixel data type for returned image.
976 * \param bufSize size of the pixels data buffer.
977 * \param pixels returned pixel data.
980 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
,
981 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
983 struct gl_texture_object
*texObj
;
984 struct gl_texture_image
*texImage
;
986 GET_CURRENT_CONTEXT(ctx
);
989 * This has been moved here because a format/type mismatch can cause a NULL
990 * texImage object, which in turn causes the mismatch error to be
993 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
994 if (err
!= GL_NO_ERROR
) {
995 _mesa_error(ctx
, err
, "glGetnTexImage(format/type)");
1000 * Legal target checking has been moved here to prevent exiting with a NULL
1003 if (!legal_getteximage_target(ctx
, target
, false)) {
1004 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetnTexImage(target=0x%x)",
1009 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1013 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1017 _mesa_get_texture_image(ctx
, texObj
, texImage
, target
, level
, format
, type
,
1018 bufSize
, pixels
, false);
1023 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
1024 GLenum type
, GLvoid
*pixels
)
1026 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
1030 * Get texture image.
1032 * \param texture texture name.
1033 * \param level image level.
1034 * \param format pixel data format for returned image.
1035 * \param type pixel data type for returned image.
1036 * \param bufSize size of the pixels data buffer.
1037 * \param pixels returned pixel data.
1040 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
,
1041 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
1043 struct gl_texture_object
*texObj
;
1044 struct gl_texture_image
*texImage
;
1048 GET_CURRENT_CONTEXT(ctx
);
1051 * This has been moved here because a format/type mismatch can cause a NULL
1052 * texImage object, which in turn causes the mismatch error to be
1055 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1056 if (err
!= GL_NO_ERROR
) {
1057 _mesa_error(ctx
, err
, "glGetTextureImage(format/type)");
1061 texObj
= _mesa_lookup_texture_err(ctx
, texture
, "glGetTextureImage");
1066 * Legal target checking has been moved here to prevent exiting with a NULL
1069 if (!legal_getteximage_target(ctx
, texObj
->Target
, true)) {
1070 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTextureImage(target=%s)",
1071 _mesa_lookup_enum_by_nr(texObj
->Target
));
1075 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1076 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1078 /* Error checking */
1079 if (texObj
->NumLayers
< 6) {
1080 /* Not enough image planes for a cube map. The spec does not say
1081 * what should happen in this case because the user has always
1082 * specified each cube face separately (using
1083 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
1084 * This is addressed in Khronos Bug 13223.
1086 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1087 "glGetTextureImage(insufficient cube map storage)");
1090 for (i
= 0; i
< 6; ++i
) { /* For each face. */
1091 if (!texObj
->Image
[i
][level
]) {
1092 /* Not enough image planes for a cube map. The spec does not say
1093 * what should happen in this case because the user has always
1094 * specified each cube face separately (using
1095 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
1096 * This is addressed in Khronos Bug 13223.
1098 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1099 "glGetTextureImage(insufficient cube map storage)");
1104 /* Copy each face. */
1105 for (i
= 0; i
< 6; ++i
) {
1106 texImage
= texObj
->Image
[i
][level
];
1107 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1108 format
, type
, bufSize
, pixels
, true);
1110 image_stride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
1111 texImage
->Height
, format
,
1113 pixels
+= image_stride
;
1114 bufSize
-= image_stride
;
1118 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1122 _mesa_get_texture_image(ctx
, texObj
, texImage
, texObj
->Target
, level
,
1123 format
, type
, bufSize
, pixels
, true);
1128 * Do error checking for a glGetCompressedTexImage() call.
1129 * \return GL_TRUE if any error, GL_FALSE if no errors.
1132 getcompressedteximage_error_check(struct gl_context
*ctx
,
1133 struct gl_texture_image
*texImage
,
1135 GLint level
, GLsizei clientMemSize
,
1136 GLvoid
*img
, bool dsa
)
1139 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1140 GLuint compressedSize
, dimensions
;
1141 const char *suffix
= dsa
? "ture" : "";
1143 if (!legal_getteximage_target(ctx
, target
, dsa
)) {
1144 _mesa_error(ctx
, GL_INVALID_ENUM
,
1145 "glGetCompressedTex%sImage(target=%s)", suffix
,
1146 _mesa_lookup_enum_by_nr(target
));
1150 assert(maxLevels
!= 0);
1151 if (level
< 0 || level
>= maxLevels
) {
1152 _mesa_error(ctx
, GL_INVALID_VALUE
,
1153 "glGetCompressedTex%sImage(bad level = %d)", suffix
, level
);
1157 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1158 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1159 "glGetCompressedTex%sImage(texture is not compressed)",
1164 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
1169 /* Check for invalid pixel storage modes */
1170 dimensions
= _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
1171 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1173 "glGetCompressedTextureImage":
1174 "glGetCompressedTexImage")) {
1178 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1179 /* do bounds checking on writing to client memory */
1180 if (clientMemSize
< (GLsizei
) compressedSize
) {
1181 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1182 "%s(out of bounds access: bufSize (%d) is too small)",
1183 dsa
? "glGetCompressedTextureImage" :
1184 "glGetnCompressedTexImageARB", clientMemSize
);
1188 /* do bounds checking on PBO write */
1189 if ((const GLubyte
*) img
+ compressedSize
>
1190 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1191 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1192 "glGetCompressedTex%sImage(out of bounds PBO access)",
1197 /* make sure PBO is not mapped */
1198 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1199 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1200 "glGetCompressedTex%sImage(PBO is mapped)", suffix
);
1208 /** Implements glGetnCompressedTexImageARB, glGetCompressedTexImage, and
1209 * glGetCompressedTextureImage.
1211 * texImage must be passed in because glGetCompressedTexImage must handle the
1212 * target GL_TEXTURE_CUBE_MAP.
1215 _mesa_get_compressed_texture_image(struct gl_context
*ctx
,
1216 struct gl_texture_object
*texObj
,
1217 struct gl_texture_image
*texImage
,
1218 GLenum target
, GLint level
,
1219 GLsizei bufSize
, GLvoid
*pixels
,
1225 FLUSH_VERTICES(ctx
, 0);
1227 if (getcompressedteximage_error_check(ctx
, texImage
, target
, level
,
1228 bufSize
, pixels
, dsa
)) {
1232 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1233 /* not an error, do nothing */
1237 if (_mesa_is_zero_size_texture(texImage
))
1240 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1242 "glGetCompressedTex%sImage(tex %u) format = %s, w=%d, h=%d\n",
1243 dsa
? "ture" : "", texObj
->Name
,
1244 _mesa_get_format_name(texImage
->TexFormat
),
1245 texImage
->Width
, texImage
->Height
);
1248 _mesa_lock_texture(ctx
, texObj
);
1250 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, pixels
);
1252 _mesa_unlock_texture(ctx
, texObj
);
1256 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1259 struct gl_texture_object
*texObj
;
1260 struct gl_texture_image
*texImage
;
1261 GET_CURRENT_CONTEXT(ctx
);
1263 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1267 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1271 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
, target
, level
,
1272 bufSize
, img
, false);
1276 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1278 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);
1282 * Get compressed texture image.
1284 * \param texture texture name.
1285 * \param level image level.
1286 * \param bufSize size of the pixels data buffer.
1287 * \param pixels returned pixel data.
1290 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1291 GLsizei bufSize
, GLvoid
*pixels
)
1293 struct gl_texture_object
*texObj
;
1294 struct gl_texture_image
*texImage
;
1297 GET_CURRENT_CONTEXT(ctx
);
1299 texObj
= _mesa_lookup_texture_err(ctx
, texture
,
1300 "glGetCompressedTextureImage");
1304 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1305 if (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
1306 assert(texObj
->NumLayers
>= 6);
1308 /* Copy each face. */
1309 for (i
= 0; i
< 6; ++i
) {
1310 texImage
= texObj
->Image
[i
][level
];
1314 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1315 texObj
->Target
, level
,
1316 bufSize
, pixels
, true);
1318 /* Compressed images don't have a client format */
1319 image_stride
= _mesa_format_image_size(texImage
->TexFormat
,
1321 texImage
->Height
, 1);
1323 pixels
+= image_stride
;
1324 bufSize
-= image_stride
;
1328 texImage
= _mesa_select_tex_image(texObj
, texObj
->Target
, level
);
1332 _mesa_get_compressed_texture_image(ctx
, texObj
, texImage
,
1333 texObj
->Target
, level
, bufSize
,