2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2009 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * Code for glGetTexImage() and glGetCompressedTexImage().
33 #include "bufferobj.h"
37 #include "format_unpack.h"
38 #include "glformats.h"
43 #include "pixelstore.h"
44 #include "texcompress.h"
45 #include "texgetimage.h"
49 #include "format_utils.h"
50 #include "pixeltransfer.h"
53 * Can the given type represent negative values?
55 static inline GLboolean
56 type_needs_clamping(GLenum type
)
63 case GL_HALF_FLOAT_ARB
:
64 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
65 case GL_UNSIGNED_INT_5_9_9_9_REV
:
74 * glGetTexImage for depth/Z pixels.
77 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
78 GLint xoffset
, GLint yoffset
, GLint zoffset
,
79 GLsizei width
, GLsizei height
, GLint depth
,
80 GLenum format
, GLenum type
, GLvoid
*pixels
,
81 struct gl_texture_image
*texImage
)
84 GLfloat
*depthRow
= malloc(width
* sizeof(GLfloat
));
87 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
91 for (img
= 0; img
< depth
; img
++) {
95 /* map src texture buffer */
96 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
97 xoffset
, yoffset
, width
, height
,
98 GL_MAP_READ_BIT
, &srcMap
, &srcRowStride
);
101 for (row
= 0; row
< height
; row
++) {
102 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
103 width
, height
, format
, type
,
105 const GLubyte
*src
= srcMap
+ row
* srcRowStride
;
106 _mesa_unpack_float_z_row(texImage
->TexFormat
, width
, src
, depthRow
);
107 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
110 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
113 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
123 * glGetTexImage for depth/stencil pixels.
126 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
127 GLint xoffset
, GLint yoffset
, GLint zoffset
,
128 GLsizei width
, GLsizei height
, GLint depth
,
129 GLenum format
, GLenum type
, GLvoid
*pixels
,
130 struct gl_texture_image
*texImage
)
134 assert(format
== GL_DEPTH_STENCIL
);
135 assert(type
== GL_UNSIGNED_INT_24_8
||
136 type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
138 for (img
= 0; img
< depth
; img
++) {
142 /* map src texture buffer */
143 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
144 xoffset
, yoffset
, width
, height
,
145 GL_MAP_READ_BIT
, &srcMap
, &rowstride
);
148 for (row
= 0; row
< height
; row
++) {
149 const GLubyte
*src
= srcMap
+ row
* rowstride
;
150 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
151 width
, height
, format
, type
,
153 _mesa_unpack_depth_stencil_row(texImage
->TexFormat
,
155 (const GLuint
*) src
,
157 if (ctx
->Pack
.SwapBytes
) {
158 _mesa_swap4((GLuint
*) dest
, width
);
162 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
165 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
172 * glGetTexImage for stencil pixels.
175 get_tex_stencil(struct gl_context
*ctx
, GLuint dimensions
,
176 GLint xoffset
, GLint yoffset
, GLint zoffset
,
177 GLsizei width
, GLsizei height
, GLint depth
,
178 GLenum format
, GLenum type
, GLvoid
*pixels
,
179 struct gl_texture_image
*texImage
)
183 assert(format
== GL_STENCIL_INDEX
);
185 for (img
= 0; img
< depth
; img
++) {
189 /* map src texture buffer */
190 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
191 xoffset
, yoffset
, width
, height
,
193 &srcMap
, &rowstride
);
196 for (row
= 0; row
< height
; row
++) {
197 const GLubyte
*src
= srcMap
+ row
* rowstride
;
198 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
199 width
, height
, format
, type
,
201 _mesa_unpack_ubyte_stencil_row(texImage
->TexFormat
,
203 (const GLuint
*) src
,
207 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
210 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
218 * glGetTexImage for YCbCr pixels.
221 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
222 GLint xoffset
, GLint yoffset
, GLint zoffset
,
223 GLsizei width
, GLsizei height
, GLint depth
,
224 GLenum format
, GLenum type
, GLvoid
*pixels
,
225 struct gl_texture_image
*texImage
)
229 for (img
= 0; img
< depth
; img
++) {
233 /* map src texture buffer */
234 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
235 xoffset
, yoffset
, width
, height
,
236 GL_MAP_READ_BIT
, &srcMap
, &rowstride
);
239 for (row
= 0; row
< height
; row
++) {
240 const GLubyte
*src
= srcMap
+ row
* rowstride
;
241 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
242 width
, height
, format
, type
,
244 memcpy(dest
, src
, width
* sizeof(GLushort
));
246 /* check for byte swapping */
247 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
248 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
249 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
250 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
251 if (!ctx
->Pack
.SwapBytes
)
252 _mesa_swap2((GLushort
*) dest
, width
);
254 else if (ctx
->Pack
.SwapBytes
) {
255 _mesa_swap2((GLushort
*) dest
, width
);
259 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
262 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
269 * Depending on the base format involved we may need to apply a rebase
270 * transform (for example: if we download to a Luminance format we want
274 teximage_needs_rebase(mesa_format texFormat
, GLenum baseFormat
,
275 bool is_compressed
, uint8_t *rebaseSwizzle
)
277 bool needsRebase
= false;
279 if (baseFormat
== GL_LUMINANCE
||
280 baseFormat
== GL_INTENSITY
) {
282 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
283 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
284 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
285 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
286 } else if (baseFormat
== GL_LUMINANCE_ALPHA
) {
288 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
289 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
290 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
291 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
292 } else if (!is_compressed
&&
293 (baseFormat
!= _mesa_get_format_base_format(texFormat
))) {
295 _mesa_compute_rgba2base2rgba_component_mapping(baseFormat
,
304 * Get a color texture image with decompression.
307 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
308 GLint xoffset
, GLint yoffset
, GLint zoffset
,
309 GLsizei width
, GLsizei height
, GLint depth
,
310 GLenum format
, GLenum type
, GLvoid
*pixels
,
311 struct gl_texture_image
*texImage
,
312 GLbitfield transferOps
)
314 /* don't want to apply sRGB -> RGB conversion here so override the format */
315 const mesa_format texFormat
=
316 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
317 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
318 GLfloat
*tempImage
, *tempSlice
;
320 int srcStride
, dstStride
;
323 uint8_t rebaseSwizzle
[4];
325 /* Decompress into temp float buffer, then pack into user buffer */
326 tempImage
= malloc(width
* height
* depth
* 4 * sizeof(GLfloat
));
328 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
332 /* Decompress the texture image slices - results in 'tempImage' */
333 for (slice
= 0; slice
< depth
; slice
++) {
337 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
339 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ slice
,
340 xoffset
, yoffset
, width
, height
,
342 &srcMap
, &srcRowStride
);
344 _mesa_decompress_image(texFormat
, width
, height
,
345 srcMap
, srcRowStride
, tempSlice
);
347 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ slice
);
350 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
356 needsRebase
= teximage_needs_rebase(texFormat
, baseFormat
, true,
359 srcStride
= 4 * width
* sizeof(GLfloat
);
360 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
361 dstFormat
= _mesa_format_from_format_and_type(format
, type
);
362 tempSlice
= tempImage
;
363 for (slice
= 0; slice
< depth
; slice
++) {
364 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
365 width
, height
, format
, type
,
367 _mesa_format_convert(dest
, dstFormat
, dstStride
,
368 tempSlice
, RGBA32_FLOAT
, srcStride
,
370 needsRebase
? rebaseSwizzle
: NULL
);
372 /* Handle byte swapping if required */
373 if (ctx
->Pack
.SwapBytes
) {
374 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
375 width
, height
, dest
, dest
);
378 tempSlice
+= 4 * width
* height
;
386 * Return a base GL format given the user-requested format
387 * for glGetTexImage().
390 _mesa_base_pack_format(GLenum format
)
395 case GL_BGRA_INTEGER
:
396 case GL_RGBA_INTEGER
:
404 case GL_GREEN_INTEGER
:
406 case GL_BLUE_INTEGER
:
408 case GL_ALPHA_INTEGER
:
410 case GL_LUMINANCE_INTEGER_EXT
:
412 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
413 return GL_LUMINANCE_ALPHA
;
421 * Get an uncompressed color texture image.
424 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
425 GLint xoffset
, GLint yoffset
, GLint zoffset
,
426 GLsizei width
, GLsizei height
, GLint depth
,
427 GLenum format
, GLenum type
, GLvoid
*pixels
,
428 struct gl_texture_image
*texImage
,
429 GLbitfield transferOps
)
431 /* don't want to apply sRGB -> RGB conversion here so override the format */
432 const mesa_format texFormat
=
433 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
435 GLboolean dst_is_integer
;
438 uint8_t rebaseSwizzle
[4];
442 needsRebase
= teximage_needs_rebase(texFormat
, texImage
->_BaseFormat
, false,
445 /* Describe the dst format */
446 dst_is_integer
= _mesa_is_enum_format_integer(format
);
447 dst_format
= _mesa_format_from_format_and_type(format
, type
);
448 dst_stride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
450 /* Since _mesa_format_convert does not handle transferOps we need to handle
451 * them before we call the function. This requires to convert to RGBA float
452 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
453 * integer then transferOps do not apply.
455 assert(!transferOps
|| (transferOps
&& !dst_is_integer
));
456 (void) dst_is_integer
; /* silence unused var warning */
458 for (img
= 0; img
< depth
; img
++) {
467 /* map src texture buffer */
468 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
469 xoffset
, yoffset
, width
, height
,
471 &srcMap
, &rowstride
);
473 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
478 dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
479 width
, height
, format
, type
,
483 uint32_t rgba_format
;
485 bool need_convert
= false;
487 /* We will convert to RGBA float */
488 rgba_format
= RGBA32_FLOAT
;
489 rgba_stride
= width
* 4 * sizeof(GLfloat
);
491 /* If we are lucky and the dst format matches the RGBA format we need
492 * to convert to, then we can convert directly into the dst buffer
493 * and avoid the final conversion/copy from the rgba buffer to the dst
496 if (format
== rgba_format
) {
500 if (rgba
== NULL
) { /* Allocate the RGBA buffer only once */
501 rgba
= malloc(height
* rgba_stride
);
503 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
504 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
510 _mesa_format_convert(rgba
, rgba_format
, rgba_stride
,
511 img_src
, texFormat
, rowstride
,
513 needsRebase
? rebaseSwizzle
: NULL
);
515 /* Handle transfer ops now */
516 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
* height
, rgba
);
518 /* If we had to rebase, we have already handled that */
521 /* If we were lucky and our RGBA conversion matches the dst format,
527 /* Otherwise, we need to convert from RGBA to dst next */
529 src_format
= rgba_format
;
530 src_stride
= rgba_stride
;
532 /* No RGBA conversion needed, convert directly to dst */
534 src_format
= texFormat
;
535 src_stride
= rowstride
;
538 /* Do the conversion to destination format */
539 _mesa_format_convert(dest
, dst_format
, dst_stride
,
540 src
, src_format
, src_stride
,
542 needsRebase
? rebaseSwizzle
: NULL
);
545 /* Handle byte swapping if required */
546 if (ctx
->Pack
.SwapBytes
)
547 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
548 width
, height
, dest
, dest
);
550 /* Unmap the src texture buffer */
551 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
560 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
561 * Compressed textures are handled here as well.
564 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
565 GLint xoffset
, GLint yoffset
, GLint zoffset
,
566 GLsizei width
, GLsizei height
, GLint depth
,
567 GLenum format
, GLenum type
, GLvoid
*pixels
,
568 struct gl_texture_image
*texImage
)
570 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
571 GLbitfield transferOps
= 0x0;
573 /* In general, clamping does not apply to glGetTexImage, except when
574 * the returned type of the image can't hold negative values.
576 if (type_needs_clamping(type
)) {
577 /* the returned image type can't have negative values */
578 if (dataType
== GL_FLOAT
||
579 dataType
== GL_HALF_FLOAT
||
580 dataType
== GL_SIGNED_NORMALIZED
||
581 format
== GL_LUMINANCE
||
582 format
== GL_LUMINANCE_ALPHA
) {
583 transferOps
|= IMAGE_CLAMP_BIT
;
587 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
588 get_tex_rgba_compressed(ctx
, dimensions
,
589 xoffset
, yoffset
, zoffset
,
590 width
, height
, depth
,
592 pixels
, texImage
, transferOps
);
595 get_tex_rgba_uncompressed(ctx
, dimensions
,
596 xoffset
, yoffset
, zoffset
,
597 width
, height
, depth
,
599 pixels
, texImage
, transferOps
);
605 * Try to do glGetTexImage() with simple memcpy().
606 * \return GL_TRUE if done, GL_FALSE otherwise
609 get_tex_memcpy(struct gl_context
*ctx
,
610 GLint xoffset
, GLint yoffset
, GLint zoffset
,
611 GLsizei width
, GLsizei height
, GLint depth
,
612 GLenum format
, GLenum type
, GLvoid
*pixels
,
613 struct gl_texture_image
*texImage
)
615 const GLenum target
= texImage
->TexObject
->Target
;
616 GLboolean memCopy
= GL_FALSE
;
617 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
620 * Check if we can use memcpy to copy from the hardware texture
621 * format to the user's format/type.
622 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
624 if ((target
== GL_TEXTURE_1D
||
625 target
== GL_TEXTURE_2D
||
626 target
== GL_TEXTURE_RECTANGLE
||
627 _mesa_is_cube_face(target
)) &&
628 texBaseFormat
== texImage
->_BaseFormat
) {
629 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
631 ctx
->Pack
.SwapBytes
, NULL
);
635 /* only a single slice is supported at this time */
640 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
641 const GLint bytesPerRow
= width
* bpp
;
643 _mesa_image_address2d(&ctx
->Pack
, pixels
, width
, height
,
645 const GLint dstRowStride
=
646 _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
650 /* map src texture buffer */
651 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
,
652 xoffset
, yoffset
, width
, height
,
653 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
656 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
657 memcpy(dst
, src
, bytesPerRow
* height
);
661 for (row
= 0; row
< height
; row
++) {
662 memcpy(dst
, src
, bytesPerRow
);
668 /* unmap src texture buffer */
669 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
);
672 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
681 * This is the software fallback for Driver.GetTexSubImage().
682 * All error checking will have been done before this routine is called.
683 * We'll call ctx->Driver.MapTextureImage() to access the data, then
684 * unmap with ctx->Driver.UnmapTextureImage().
687 _mesa_GetTexSubImage_sw(struct gl_context
*ctx
,
688 GLint xoffset
, GLint yoffset
, GLint zoffset
,
689 GLsizei width
, GLsizei height
, GLint depth
,
690 GLenum format
, GLenum type
, GLvoid
*pixels
,
691 struct gl_texture_image
*texImage
)
693 const GLuint dimensions
=
694 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
696 /* map dest buffer, if PBO */
697 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
698 /* Packing texture image into a PBO.
699 * Map the (potentially) VRAM-based buffer into our process space so
700 * we can write into it with the code below.
701 * A hardware driver might use a sophisticated blit to move the
702 * texture data to the PBO if the PBO is in VRAM along with the texture.
704 GLubyte
*buf
= (GLubyte
*)
705 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
706 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
709 /* out of memory or other unexpected error */
710 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
713 /* <pixels> was an offset into the PBO.
714 * Now make it a real, client-side pointer inside the mapped region.
716 pixels
= ADD_POINTERS(buf
, pixels
);
719 /* for all array textures, the Z axis selects the layer */
720 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
725 assert(zoffset
+ depth
<= texImage
->Height
);
727 assert(zoffset
+ depth
<= texImage
->Depth
);
730 if (get_tex_memcpy(ctx
, xoffset
, yoffset
, zoffset
, width
, height
, depth
,
731 format
, type
, pixels
, texImage
)) {
734 else if (format
== GL_DEPTH_COMPONENT
) {
735 get_tex_depth(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
736 width
, height
, depth
, format
, type
, pixels
, texImage
);
738 else if (format
== GL_DEPTH_STENCIL_EXT
) {
739 get_tex_depth_stencil(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
740 width
, height
, depth
, format
, type
, pixels
,
743 else if (format
== GL_STENCIL_INDEX
) {
744 get_tex_stencil(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
745 width
, height
, depth
, format
, type
, pixels
, texImage
);
747 else if (format
== GL_YCBCR_MESA
) {
748 get_tex_ycbcr(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
749 width
, height
, depth
, format
, type
, pixels
, texImage
);
752 get_tex_rgba(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
753 width
, height
, depth
, format
, type
, pixels
, texImage
);
756 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
757 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
764 * This function assumes that all error checking has been done.
767 get_compressed_texsubimage_sw(struct gl_context
*ctx
,
768 struct gl_texture_image
*texImage
,
769 GLint xoffset
, GLint yoffset
,
770 GLint zoffset
, GLsizei width
,
771 GLint height
, GLint depth
,
774 const GLuint dimensions
=
775 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
776 struct compressed_pixelstore store
;
780 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
781 width
, height
, depth
,
784 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
785 /* pack texture image into a PBO */
787 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
788 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
791 /* out of memory or other unexpected error */
792 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
793 "glGetCompresssedTexImage(map PBO failed)");
796 dest
= ADD_POINTERS(dest
, img
);
801 dest
+= store
.SkipBytes
;
803 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
807 /* map src texture buffer */
808 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ slice
,
809 xoffset
, yoffset
, width
, height
,
810 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
814 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
815 memcpy(dest
, src
, store
.CopyBytesPerRow
);
816 dest
+= store
.TotalBytesPerRow
;
820 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ slice
);
822 /* Advance to next slice */
823 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
-
824 store
.CopyRowsPerSlice
);
827 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
831 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
832 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
838 * Validate the texture target enum supplied to glGetTex(ture)Image or
839 * glGetCompressedTex(ture)Image.
842 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
849 case GL_TEXTURE_RECTANGLE_NV
:
850 return ctx
->Extensions
.NV_texture_rectangle
;
851 case GL_TEXTURE_1D_ARRAY_EXT
:
852 case GL_TEXTURE_2D_ARRAY_EXT
:
853 return ctx
->Extensions
.EXT_texture_array
;
854 case GL_TEXTURE_CUBE_MAP_ARRAY
:
855 return ctx
->Extensions
.ARB_texture_cube_map_array
;
857 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
859 * "An INVALID_ENUM error is generated if the effective target is not
860 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
861 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
862 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
863 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
865 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
866 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
867 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
868 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
869 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
870 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
871 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
872 case GL_TEXTURE_CUBE_MAP
:
873 return dsa
? GL_TRUE
: GL_FALSE
;
881 * Wrapper for _mesa_select_tex_image() which can handle target being
882 * GL_TEXTURE_CUBE_MAP in which case we use zoffset to select a cube face.
883 * This can happen for glGetTextureImage and glGetTextureSubImage (DSA
886 static struct gl_texture_image
*
887 select_tex_image(const struct gl_texture_object
*texObj
, GLenum target
,
888 GLint level
, GLint zoffset
)
891 assert(level
< MAX_TEXTURE_LEVELS
);
892 if (target
== GL_TEXTURE_CUBE_MAP
) {
893 assert(zoffset
>= 0);
895 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
;
897 return _mesa_select_tex_image(texObj
, target
, level
);
902 * Error-check the offset and size arguments to
903 * glGet[Compressed]TextureSubImage(). Also checks if the specified
904 * texture image is missing.
905 * \return true if error, false if no error.
908 dimensions_error_check(struct gl_context
*ctx
,
909 struct gl_texture_object
*texObj
,
910 GLenum target
, GLint level
,
911 GLint xoffset
, GLint yoffset
, GLint zoffset
,
912 GLsizei width
, GLsizei height
, GLsizei depth
,
915 const struct gl_texture_image
*texImage
;
918 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset = %d)", caller
, xoffset
);
923 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset = %d)", caller
, yoffset
);
928 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset = %d)", caller
, zoffset
);
933 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width = %d)", caller
, width
);
938 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height = %d)", caller
, height
);
943 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(depth = %d)", caller
, depth
);
947 /* do special per-target checks */
951 _mesa_error(ctx
, GL_INVALID_VALUE
,
952 "%s(1D, yoffset = %d)", caller
, yoffset
);
956 _mesa_error(ctx
, GL_INVALID_VALUE
,
957 "%s(1D, height = %d)", caller
, height
);
961 case GL_TEXTURE_1D_ARRAY
:
963 case GL_TEXTURE_RECTANGLE
:
965 _mesa_error(ctx
, GL_INVALID_VALUE
,
966 "%s(zoffset = %d)", caller
, zoffset
);
970 _mesa_error(ctx
, GL_INVALID_VALUE
,
971 "%s(depth = %d)", caller
, depth
);
975 case GL_TEXTURE_CUBE_MAP
:
976 /* Non-array cube maps are special because we have a gl_texture_image
979 if (zoffset
+ depth
> 6) {
980 _mesa_error(ctx
, GL_INVALID_VALUE
,
981 "%s(zoffset + depth = %d)", caller
, zoffset
+ depth
);
984 /* According to OpenGL 4.6 spec, section 8.11.4 ("Texture Image Queries"):
986 * "An INVALID_OPERATION error is generated by GetTextureImage if the
987 * effective target is TEXTURE_CUBE_MAP or TEXTURE_CUBE_MAP_ARRAY ,
988 * and the texture object is not cube complete or cube array complete,
991 * This applies also to GetTextureSubImage, GetCompressedTexImage,
992 * GetCompressedTextureImage, and GetnCompressedTexImage.
994 if (!_mesa_cube_complete(texObj
)) {
995 _mesa_error(ctx
, GL_INVALID_OPERATION
,
996 "%s(cube incomplete)", caller
);
1004 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1006 /* Trying to return a non-defined level is a valid operation per se, as
1007 * OpenGL 4.6 spec, section 8.11.4 ("Texture Image Queries") does not
1008 * handle this case as an error.
1010 * Rather, we need to look at section 8.22 ("Texture State and Proxy
1013 * "Each initial texture image is null. It has zero width, height, and
1014 * depth, internal format RGBA, or R8 for buffer textures, component
1015 * sizes set to zero and component types set to NONE, the compressed
1016 * flag set to FALSE, a zero compressed size, and the bound buffer
1017 * object name is zero."
1019 * This means we need to assume the image for the non-defined level is
1020 * an empty image. With this assumption, we can go back to section
1021 * 8.11.4 and checking again the errors:
1023 * "An INVALID_VALUE error is generated if xoffset + width is greater
1024 * than the texture’s width, yoffset + height is greater than the
1025 * texture’s height, or zoffset + depth is greater than the texture’s
1028 * Thus why we return INVALID_VALUE.
1030 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(missing image)", caller
);
1034 if (xoffset
+ width
> texImage
->Width
) {
1035 _mesa_error(ctx
, GL_INVALID_VALUE
,
1036 "%s(xoffset %d + width %d > %u)",
1037 caller
, xoffset
, width
, texImage
->Width
);
1041 if (yoffset
+ height
> texImage
->Height
) {
1042 _mesa_error(ctx
, GL_INVALID_VALUE
,
1043 "%s(yoffset %d + height %d > %u)",
1044 caller
, yoffset
, height
, texImage
->Height
);
1048 if (target
!= GL_TEXTURE_CUBE_MAP
) {
1049 /* Cube map error checking was done above */
1050 if (zoffset
+ depth
> texImage
->Depth
) {
1051 _mesa_error(ctx
, GL_INVALID_VALUE
,
1052 "%s(zoffset %d + depth %d > %u)",
1053 caller
, zoffset
, depth
, texImage
->Depth
);
1058 /* Extra checks for compressed textures */
1061 _mesa_get_format_block_size_3d(texImage
->TexFormat
, &bw
, &bh
, &bd
);
1062 if (bw
> 1 || bh
> 1 || bd
> 1) {
1063 /* offset must be multiple of block size */
1064 if (xoffset
% bw
!= 0) {
1065 _mesa_error(ctx
, GL_INVALID_VALUE
,
1066 "%s(xoffset = %d)", caller
, xoffset
);
1069 if (target
!= GL_TEXTURE_1D
&& target
!= GL_TEXTURE_1D_ARRAY
) {
1070 if (yoffset
% bh
!= 0) {
1071 _mesa_error(ctx
, GL_INVALID_VALUE
,
1072 "%s(yoffset = %d)", caller
, yoffset
);
1077 if (zoffset
% bd
!= 0) {
1078 _mesa_error(ctx
, GL_INVALID_VALUE
,
1079 "%s(zoffset = %d)", caller
, zoffset
);
1083 /* The size must be a multiple of bw x bh x bd, or we must be using a
1084 * offset+size that exactly hits the edge of the image.
1086 if ((width
% bw
!= 0) &&
1087 (xoffset
+ width
!= (GLint
) texImage
->Width
)) {
1088 _mesa_error(ctx
, GL_INVALID_VALUE
,
1089 "%s(width = %d)", caller
, width
);
1093 if ((height
% bh
!= 0) &&
1094 (yoffset
+ height
!= (GLint
) texImage
->Height
)) {
1095 _mesa_error(ctx
, GL_INVALID_VALUE
,
1096 "%s(height = %d)", caller
, height
);
1100 if ((depth
% bd
!= 0) &&
1101 (zoffset
+ depth
!= (GLint
) texImage
->Depth
)) {
1102 _mesa_error(ctx
, GL_INVALID_VALUE
,
1103 "%s(depth = %d)", caller
, depth
);
1109 if (width
== 0 || height
== 0 || depth
== 0) {
1110 /* Not an error, but nothing to do. Return 'true' so that the
1111 * caller simply returns.
1121 * Do PBO-related error checking for getting uncompressed images.
1122 * \return true if there was an error (or the GetTexImage is to be a no-op)
1125 pbo_error_check(struct gl_context
*ctx
, GLenum target
,
1126 GLsizei width
, GLsizei height
, GLsizei depth
,
1127 GLenum format
, GLenum type
, GLsizei clientMemSize
,
1131 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
1133 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, width
, height
, depth
,
1134 format
, type
, clientMemSize
, pixels
)) {
1135 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1136 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1137 "%s(out of bounds PBO access)", caller
);
1139 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1140 "%s(out of bounds access: bufSize (%d) is too small)",
1141 caller
, clientMemSize
);
1146 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1147 /* PBO should not be mapped */
1148 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1149 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1150 "%s(PBO is mapped)", caller
);
1155 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1156 /* not an error, do nothing */
1165 * Do error checking for all (non-compressed) get-texture-image functions.
1166 * \return true if any error, false if no errors.
1169 getteximage_error_check(struct gl_context
*ctx
,
1170 struct gl_texture_object
*texObj
,
1171 GLenum target
, GLint level
,
1172 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1173 GLsizei width
, GLsizei height
, GLsizei depth
,
1174 GLenum format
, GLenum type
, GLsizei bufSize
,
1175 GLvoid
*pixels
, const char *caller
)
1177 struct gl_texture_image
*texImage
;
1178 GLenum baseFormat
, err
;
1183 if (texObj
->Target
== 0) {
1184 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture)", caller
);
1188 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1189 if (level
< 0 || level
>= maxLevels
) {
1190 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level = %d)", caller
, level
);
1194 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1195 if (err
!= GL_NO_ERROR
) {
1196 _mesa_error(ctx
, err
, "%s(format/type)", caller
);
1200 if (dimensions_error_check(ctx
, texObj
, target
, level
,
1201 xoffset
, yoffset
, zoffset
,
1202 width
, height
, depth
, caller
)) {
1206 if (pbo_error_check(ctx
, target
, width
, height
, depth
,
1207 format
, type
, bufSize
, pixels
, caller
)) {
1211 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1215 * Format and type checking has been moved up to GetnTexImage and
1216 * GetTextureImage so that it happens before getting the texImage object.
1219 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
1221 /* Make sure the requested image format is compatible with the
1224 if (_mesa_is_color_format(format
)
1225 && !_mesa_is_color_format(baseFormat
)) {
1226 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1227 "%s(format mismatch)", caller
);
1230 else if (_mesa_is_depth_format(format
)
1231 && !_mesa_is_depth_format(baseFormat
)
1232 && !_mesa_is_depthstencil_format(baseFormat
)) {
1233 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1234 "%s(format mismatch)", caller
);
1237 else if (_mesa_is_stencil_format(format
)
1238 && !ctx
->Extensions
.ARB_texture_stencil8
) {
1239 _mesa_error(ctx
, GL_INVALID_ENUM
,
1240 "%s(format=GL_STENCIL_INDEX)", caller
);
1243 else if (_mesa_is_stencil_format(format
)
1244 && !_mesa_is_depthstencil_format(baseFormat
)
1245 && !_mesa_is_stencil_format(baseFormat
)) {
1246 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1247 "%s(format mismatch)", caller
);
1250 else if (_mesa_is_ycbcr_format(format
)
1251 && !_mesa_is_ycbcr_format(baseFormat
)) {
1252 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1253 "%s(format mismatch)", caller
);
1256 else if (_mesa_is_depthstencil_format(format
)
1257 && !_mesa_is_depthstencil_format(baseFormat
)) {
1258 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1259 "%s(format mismatch)", caller
);
1262 else if (!_mesa_is_stencil_format(format
) &&
1263 _mesa_is_enum_format_integer(format
) !=
1264 _mesa_is_format_integer(texImage
->TexFormat
)) {
1265 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1266 "%s(format mismatch)", caller
);
1275 * Return the width, height and depth of a texture image.
1276 * This function must be resilient to bad parameter values since
1277 * this is called before full error checking.
1280 get_texture_image_dims(const struct gl_texture_object
*texObj
,
1281 GLenum target
, GLint level
,
1282 GLsizei
*width
, GLsizei
*height
, GLsizei
*depth
)
1284 const struct gl_texture_image
*texImage
= NULL
;
1286 if (level
>= 0 && level
< MAX_TEXTURE_LEVELS
) {
1287 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1291 *width
= texImage
->Width
;
1292 *height
= texImage
->Height
;
1293 if (target
== GL_TEXTURE_CUBE_MAP
) {
1297 *depth
= texImage
->Depth
;
1301 *width
= *height
= *depth
= 0;
1307 * Common code for all (uncompressed) get-texture-image functions.
1308 * \param texObj the texture object (should not be null)
1309 * \param target user-provided target, or 0 for DSA
1310 * \param level image level.
1311 * \param format pixel data format for returned image.
1312 * \param type pixel data type for returned image.
1313 * \param bufSize size of the pixels data buffer.
1314 * \param pixels returned pixel data.
1315 * \param caller name of calling function
1318 get_texture_image(struct gl_context
*ctx
,
1319 struct gl_texture_object
*texObj
,
1320 GLenum target
, GLint level
,
1321 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1322 GLsizei width
, GLsizei height
, GLint depth
,
1323 GLenum format
, GLenum type
,
1324 GLvoid
*pixels
, const char *caller
)
1326 struct gl_texture_image
*texImage
;
1327 unsigned firstFace
, numFaces
, i
;
1330 FLUSH_VERTICES(ctx
, 0);
1332 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1333 assert(texImage
); /* should have been error checked already */
1335 if (_mesa_is_zero_size_texture(texImage
)) {
1336 /* no image data to return */
1340 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1341 _mesa_debug(ctx
, "%s(tex %u) format = %s, w=%d, h=%d,"
1342 " dstFmt=0x%x, dstType=0x%x\n",
1343 caller
, texObj
->Name
,
1344 _mesa_get_format_name(texImage
->TexFormat
),
1345 texImage
->Width
, texImage
->Height
,
1349 if (target
== GL_TEXTURE_CUBE_MAP
) {
1350 /* Compute stride between cube faces */
1351 imageStride
= _mesa_image_image_stride(&ctx
->Pack
, width
, height
,
1353 firstFace
= zoffset
;
1360 firstFace
= _mesa_tex_target_to_face(target
);
1364 _mesa_lock_texture(ctx
, texObj
);
1366 for (i
= 0; i
< numFaces
; i
++) {
1367 texImage
= texObj
->Image
[firstFace
+ i
][level
];
1370 ctx
->Driver
.GetTexSubImage(ctx
, xoffset
, yoffset
, zoffset
,
1371 width
, height
, depth
,
1372 format
, type
, pixels
, texImage
);
1374 /* next cube face */
1375 pixels
= (GLubyte
*) pixels
+ imageStride
;
1378 _mesa_unlock_texture(ctx
, texObj
);
1383 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
, GLenum type
,
1384 GLsizei bufSize
, GLvoid
*pixels
)
1386 GET_CURRENT_CONTEXT(ctx
);
1387 static const char *caller
= "glGetnTexImageARB";
1388 GLsizei width
, height
, depth
;
1389 struct gl_texture_object
*texObj
;
1391 if (!legal_getteximage_target(ctx
, target
, false)) {
1392 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1396 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1399 get_texture_image_dims(texObj
, target
, level
, &width
, &height
, &depth
);
1401 if (getteximage_error_check(ctx
, texObj
, target
, level
,
1402 0, 0, 0, width
, height
, depth
,
1403 format
, type
, bufSize
, pixels
, caller
)) {
1407 get_texture_image(ctx
, texObj
, target
, level
,
1408 0, 0, 0, width
, height
, depth
,
1409 format
, type
, pixels
, caller
);
1414 _mesa_GetTexImage(GLenum target
, GLint level
, GLenum format
, GLenum type
,
1417 GET_CURRENT_CONTEXT(ctx
);
1418 static const char *caller
= "glGetTexImage";
1419 GLsizei width
, height
, depth
;
1420 struct gl_texture_object
*texObj
;
1422 if (!legal_getteximage_target(ctx
, target
, false)) {
1423 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1427 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1430 get_texture_image_dims(texObj
, target
, level
, &width
, &height
, &depth
);
1432 if (getteximage_error_check(ctx
, texObj
, target
, level
,
1433 0, 0, 0, width
, height
, depth
,
1434 format
, type
, INT_MAX
, pixels
, caller
)) {
1438 get_texture_image(ctx
, texObj
, target
, level
,
1439 0, 0, 0, width
, height
, depth
,
1440 format
, type
, pixels
, caller
);
1445 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
, GLenum type
,
1446 GLsizei bufSize
, GLvoid
*pixels
)
1448 GET_CURRENT_CONTEXT(ctx
);
1449 GLsizei width
, height
, depth
;
1450 static const char *caller
= "glGetTextureImage";
1451 struct gl_texture_object
*texObj
=
1452 _mesa_lookup_texture_err(ctx
, texture
, caller
);
1458 if (!legal_getteximage_target(ctx
, texObj
->Target
, true)) {
1459 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s", caller
);
1463 get_texture_image_dims(texObj
, texObj
->Target
, level
,
1464 &width
, &height
, &depth
);
1466 if (getteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1467 0, 0, 0, width
, height
, depth
,
1468 format
, type
, bufSize
, pixels
, caller
)) {
1472 get_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1473 0, 0, 0, width
, height
, depth
,
1474 format
, type
, pixels
, caller
);
1479 _mesa_GetTextureSubImage(GLuint texture
, GLint level
,
1480 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1481 GLsizei width
, GLsizei height
, GLsizei depth
,
1482 GLenum format
, GLenum type
, GLsizei bufSize
,
1485 GET_CURRENT_CONTEXT(ctx
);
1486 static const char *caller
= "glGetTextureSubImage";
1487 struct gl_texture_object
*texObj
=
1488 _mesa_lookup_texture_err(ctx
, texture
, caller
);
1494 if (!legal_getteximage_target(ctx
, texObj
->Target
, true)) {
1495 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1496 "%s(buffer/multisample texture)", caller
);
1500 if (getteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1501 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
1502 format
, type
, bufSize
, pixels
, caller
)) {
1506 get_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1507 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
1508 format
, type
, pixels
, caller
);
1514 * Compute the number of bytes which will be written when retrieving
1515 * a sub-region of a compressed texture.
1518 packed_compressed_size(GLuint dimensions
, mesa_format format
,
1519 GLsizei width
, GLsizei height
, GLsizei depth
,
1520 const struct gl_pixelstore_attrib
*packing
)
1522 struct compressed_pixelstore st
;
1525 _mesa_compute_compressed_pixelstore(dimensions
, format
,
1526 width
, height
, depth
,
1529 (st
.CopySlices
- 1) * st
.TotalRowsPerSlice
* st
.TotalBytesPerRow
+
1531 (st
.CopyRowsPerSlice
- 1) * st
.TotalBytesPerRow
+
1539 * Do error checking for getting compressed texture images.
1540 * \return true if any error, false if no errors.
1543 getcompressedteximage_error_check(struct gl_context
*ctx
,
1544 struct gl_texture_object
*texObj
,
1545 GLenum target
, GLint level
,
1546 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1547 GLsizei width
, GLsizei height
, GLsizei depth
,
1548 GLsizei bufSize
, GLvoid
*pixels
,
1551 struct gl_texture_image
*texImage
;
1558 if (texObj
->Target
== 0) {
1559 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture)", caller
);
1563 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1564 if (level
< 0 || level
>= maxLevels
) {
1565 _mesa_error(ctx
, GL_INVALID_VALUE
,
1566 "%s(bad level = %d)", caller
, level
);
1570 if (dimensions_error_check(ctx
, texObj
, target
, level
,
1571 xoffset
, yoffset
, zoffset
,
1572 width
, height
, depth
, caller
)) {
1576 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1579 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1580 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1581 "%s(texture is not compressed)", caller
);
1585 /* Check for invalid pixel storage modes */
1586 dimensions
= _mesa_get_texture_dimensions(texObj
->Target
);
1587 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1593 /* Compute number of bytes that may be touched in the dest buffer */
1594 totalBytes
= packed_compressed_size(dimensions
, texImage
->TexFormat
,
1595 width
, height
, depth
,
1598 /* Do dest buffer bounds checking */
1599 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1600 /* do bounds checking on PBO write */
1601 if ((GLubyte
*) pixels
+ totalBytes
>
1602 (GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1603 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1604 "%s(out of bounds PBO access)", caller
);
1608 /* make sure PBO is not mapped */
1609 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1610 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", caller
);
1615 /* do bounds checking on writing to client memory */
1616 if (totalBytes
> bufSize
) {
1617 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1618 "%s(out of bounds access: bufSize (%d) is too small)",
1624 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1625 /* not an error, but do nothing */
1634 * Common helper for all glGetCompressed-teximage functions.
1637 get_compressed_texture_image(struct gl_context
*ctx
,
1638 struct gl_texture_object
*texObj
,
1639 GLenum target
, GLint level
,
1640 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1641 GLsizei width
, GLsizei height
, GLint depth
,
1645 struct gl_texture_image
*texImage
;
1646 unsigned firstFace
, numFaces
, i
, imageStride
;
1648 FLUSH_VERTICES(ctx
, 0);
1650 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1651 assert(texImage
); /* should have been error checked already */
1653 if (_mesa_is_zero_size_texture(texImage
))
1656 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1658 "%s(tex %u) format = %s, w=%d, h=%d\n",
1659 caller
, texObj
->Name
,
1660 _mesa_get_format_name(texImage
->TexFormat
),
1661 texImage
->Width
, texImage
->Height
);
1664 if (target
== GL_TEXTURE_CUBE_MAP
) {
1665 struct compressed_pixelstore store
;
1667 /* Compute image stride between cube faces */
1668 _mesa_compute_compressed_pixelstore(2, texImage
->TexFormat
,
1669 width
, height
, depth
,
1670 &ctx
->Pack
, &store
);
1671 imageStride
= store
.TotalBytesPerRow
* store
.TotalRowsPerSlice
;
1673 firstFace
= zoffset
;
1680 firstFace
= _mesa_tex_target_to_face(target
);
1684 _mesa_lock_texture(ctx
, texObj
);
1686 for (i
= 0; i
< numFaces
; i
++) {
1687 texImage
= texObj
->Image
[firstFace
+ i
][level
];
1690 get_compressed_texsubimage_sw(ctx
, texImage
,
1691 xoffset
, yoffset
, zoffset
,
1692 width
, height
, depth
, pixels
);
1694 /* next cube face */
1695 pixels
= (GLubyte
*) pixels
+ imageStride
;
1698 _mesa_unlock_texture(ctx
, texObj
);
1703 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1706 GET_CURRENT_CONTEXT(ctx
);
1707 static const char *caller
= "glGetnCompressedTexImageARB";
1708 GLsizei width
, height
, depth
;
1709 struct gl_texture_object
*texObj
;
1711 if (!legal_getteximage_target(ctx
, target
, false)) {
1712 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1716 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1719 get_texture_image_dims(texObj
, target
, level
, &width
, &height
, &depth
);
1721 if (getcompressedteximage_error_check(ctx
, texObj
, target
, level
,
1722 0, 0, 0, width
, height
, depth
,
1723 INT_MAX
, pixels
, caller
)) {
1727 get_compressed_texture_image(ctx
, texObj
, target
, level
,
1728 0, 0, 0, width
, height
, depth
,
1734 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*pixels
)
1736 GET_CURRENT_CONTEXT(ctx
);
1737 static const char *caller
= "glGetCompressedTexImage";
1738 GLsizei width
, height
, depth
;
1739 struct gl_texture_object
*texObj
;
1741 if (!legal_getteximage_target(ctx
, target
, false)) {
1742 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1746 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1749 get_texture_image_dims(texObj
, target
, level
,
1750 &width
, &height
, &depth
);
1752 if (getcompressedteximage_error_check(ctx
, texObj
, target
, level
,
1753 0, 0, 0, width
, height
, depth
,
1754 INT_MAX
, pixels
, caller
)) {
1758 get_compressed_texture_image(ctx
, texObj
, target
, level
,
1759 0, 0, 0, width
, height
, depth
,
1765 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1766 GLsizei bufSize
, GLvoid
*pixels
)
1768 GET_CURRENT_CONTEXT(ctx
);
1769 static const char *caller
= "glGetCompressedTextureImage";
1770 GLsizei width
, height
, depth
;
1771 struct gl_texture_object
*texObj
=
1772 _mesa_lookup_texture_err(ctx
, texture
, caller
);
1778 get_texture_image_dims(texObj
, texObj
->Target
, level
,
1779 &width
, &height
, &depth
);
1781 if (getcompressedteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1782 0, 0, 0, width
, height
, depth
,
1783 bufSize
, pixels
, caller
)) {
1787 get_compressed_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1788 0, 0, 0, width
, height
, depth
,
1794 _mesa_GetCompressedTextureSubImage(GLuint texture
, GLint level
,
1795 GLint xoffset
, GLint yoffset
,
1796 GLint zoffset
, GLsizei width
,
1797 GLsizei height
, GLsizei depth
,
1798 GLsizei bufSize
, void *pixels
)
1800 GET_CURRENT_CONTEXT(ctx
);
1801 static const char *caller
= "glGetCompressedTextureImage";
1802 struct gl_texture_object
*texObj
= NULL
;
1804 texObj
= _mesa_lookup_texture_err(ctx
, texture
, caller
);
1809 if (getcompressedteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1810 xoffset
, yoffset
, zoffset
,
1811 width
, height
, depth
,
1812 bufSize
, pixels
, caller
)) {
1816 get_compressed_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1817 xoffset
, yoffset
, zoffset
,
1818 width
, height
, depth
,