2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2009 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * Code for glGetTexImage() and glGetCompressedTexImage().
33 #include "bufferobj.h"
37 #include "format_unpack.h"
38 #include "glformats.h"
43 #include "pixelstore.h"
44 #include "texcompress.h"
45 #include "texgetimage.h"
49 #include "format_utils.h"
50 #include "pixeltransfer.h"
53 * Can the given type represent negative values?
55 static inline GLboolean
56 type_needs_clamping(GLenum type
)
63 case GL_HALF_FLOAT_ARB
:
64 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
65 case GL_UNSIGNED_INT_5_9_9_9_REV
:
74 * glGetTexImage for depth/Z pixels.
77 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
78 GLint xoffset
, GLint yoffset
, GLint zoffset
,
79 GLsizei width
, GLsizei height
, GLint depth
,
80 GLenum format
, GLenum type
, GLvoid
*pixels
,
81 struct gl_texture_image
*texImage
)
84 GLfloat
*depthRow
= malloc(width
* sizeof(GLfloat
));
87 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
91 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
96 assert(zoffset
+ depth
<= texImage
->Depth
);
97 for (img
= 0; img
< depth
; img
++) {
101 /* map src texture buffer */
102 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
103 xoffset
, yoffset
, width
, height
,
104 GL_MAP_READ_BIT
, &srcMap
, &srcRowStride
);
107 for (row
= 0; row
< height
; row
++) {
108 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
109 width
, height
, format
, type
,
111 const GLubyte
*src
= srcMap
+ row
* srcRowStride
;
112 _mesa_unpack_float_z_row(texImage
->TexFormat
, width
, src
, depthRow
);
113 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
116 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
119 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
129 * glGetTexImage for depth/stencil pixels.
132 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
133 GLint xoffset
, GLint yoffset
, GLint zoffset
,
134 GLsizei width
, GLsizei height
, GLint depth
,
135 GLenum format
, GLenum type
, GLvoid
*pixels
,
136 struct gl_texture_image
*texImage
)
140 assert(format
== GL_DEPTH_STENCIL
);
141 assert(type
== GL_UNSIGNED_INT_24_8
||
142 type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
144 assert(zoffset
+ depth
<= texImage
->Depth
);
145 for (img
= 0; img
< depth
; img
++) {
149 /* map src texture buffer */
150 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
151 xoffset
, yoffset
, width
, height
,
152 GL_MAP_READ_BIT
, &srcMap
, &rowstride
);
155 for (row
= 0; row
< height
; row
++) {
156 const GLubyte
*src
= srcMap
+ row
* rowstride
;
157 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
158 width
, height
, format
, type
,
160 _mesa_unpack_depth_stencil_row(texImage
->TexFormat
,
162 (const GLuint
*) src
,
164 if (ctx
->Pack
.SwapBytes
) {
165 _mesa_swap4((GLuint
*) dest
, width
);
169 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
172 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
179 * glGetTexImage for stencil pixels.
182 get_tex_stencil(struct gl_context
*ctx
, GLuint dimensions
,
183 GLint xoffset
, GLint yoffset
, GLint zoffset
,
184 GLsizei width
, GLsizei height
, GLint depth
,
185 GLenum format
, GLenum type
, GLvoid
*pixels
,
186 struct gl_texture_image
*texImage
)
190 assert(format
== GL_STENCIL_INDEX
);
192 for (img
= 0; img
< depth
; img
++) {
196 /* map src texture buffer */
197 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
198 xoffset
, yoffset
, width
, height
,
200 &srcMap
, &rowstride
);
203 for (row
= 0; row
< height
; row
++) {
204 const GLubyte
*src
= srcMap
+ row
* rowstride
;
205 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
206 width
, height
, format
, type
,
208 _mesa_unpack_ubyte_stencil_row(texImage
->TexFormat
,
210 (const GLuint
*) src
,
214 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
217 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
225 * glGetTexImage for YCbCr pixels.
228 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
229 GLint xoffset
, GLint yoffset
, GLint zoffset
,
230 GLsizei width
, GLsizei height
, GLint depth
,
231 GLenum format
, GLenum type
, GLvoid
*pixels
,
232 struct gl_texture_image
*texImage
)
236 assert(zoffset
+ depth
<= texImage
->Depth
);
237 for (img
= 0; img
< depth
; img
++) {
241 /* map src texture buffer */
242 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
243 xoffset
, yoffset
, width
, height
,
244 GL_MAP_READ_BIT
, &srcMap
, &rowstride
);
247 for (row
= 0; row
< height
; row
++) {
248 const GLubyte
*src
= srcMap
+ row
* rowstride
;
249 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
250 width
, height
, format
, type
,
252 memcpy(dest
, src
, width
* sizeof(GLushort
));
254 /* check for byte swapping */
255 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
256 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
257 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
258 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
259 if (!ctx
->Pack
.SwapBytes
)
260 _mesa_swap2((GLushort
*) dest
, width
);
262 else if (ctx
->Pack
.SwapBytes
) {
263 _mesa_swap2((GLushort
*) dest
, width
);
267 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
270 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
278 * Get a color texture image with decompression.
281 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
282 GLint xoffset
, GLint yoffset
, GLint zoffset
,
283 GLsizei width
, GLsizei height
, GLint depth
,
284 GLenum format
, GLenum type
, GLvoid
*pixels
,
285 struct gl_texture_image
*texImage
,
286 GLbitfield transferOps
)
288 /* don't want to apply sRGB -> RGB conversion here so override the format */
289 const mesa_format texFormat
=
290 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
291 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
292 GLfloat
*tempImage
, *tempSlice
;
294 int srcStride
, dstStride
;
297 uint8_t rebaseSwizzle
[4];
299 /* Decompress into temp float buffer, then pack into user buffer */
300 tempImage
= malloc(width
* height
* depth
301 * 4 * sizeof(GLfloat
));
303 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
307 /* Decompress the texture image slices - results in 'tempImage' */
308 for (slice
= 0; slice
< depth
; slice
++) {
312 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
314 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ slice
,
315 xoffset
, yoffset
, width
, height
,
317 &srcMap
, &srcRowStride
);
319 _mesa_decompress_image(texFormat
, width
, height
,
320 srcMap
, srcRowStride
, tempSlice
);
322 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ slice
);
325 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
331 /* Depending on the base format involved we may need to apply a rebase
332 * transform (for example: if we download to a Luminance format we want
335 if (baseFormat
== GL_LUMINANCE
||
336 baseFormat
== GL_INTENSITY
) {
338 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
339 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
340 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
341 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
342 } else if (baseFormat
== GL_LUMINANCE_ALPHA
) {
344 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
345 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
346 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
347 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
352 srcStride
= 4 * width
* sizeof(GLfloat
);
353 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
354 dstFormat
= _mesa_format_from_format_and_type(format
, type
);
355 tempSlice
= tempImage
;
356 for (slice
= 0; slice
< depth
; slice
++) {
357 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
358 width
, height
, format
, type
,
360 _mesa_format_convert(dest
, dstFormat
, dstStride
,
361 tempSlice
, RGBA32_FLOAT
, srcStride
,
363 needsRebase
? rebaseSwizzle
: NULL
);
365 /* Handle byte swapping if required */
366 if (ctx
->Pack
.SwapBytes
) {
367 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
368 width
, height
, dest
, dest
);
371 tempSlice
+= 4 * width
* height
;
379 * Return a base GL format given the user-requested format
380 * for glGetTexImage().
383 _mesa_base_pack_format(GLenum format
)
388 case GL_BGRA_INTEGER
:
389 case GL_RGBA_INTEGER
:
397 case GL_GREEN_INTEGER
:
399 case GL_BLUE_INTEGER
:
401 case GL_ALPHA_INTEGER
:
403 case GL_LUMINANCE_INTEGER_EXT
:
405 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
406 return GL_LUMINANCE_ALPHA
;
414 * Get an uncompressed color texture image.
417 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
418 GLint xoffset
, GLint yoffset
, GLint zoffset
,
419 GLsizei width
, GLsizei height
, GLint depth
,
420 GLenum format
, GLenum type
, GLvoid
*pixels
,
421 struct gl_texture_image
*texImage
,
422 GLbitfield transferOps
)
424 /* don't want to apply sRGB -> RGB conversion here so override the format */
425 const mesa_format texFormat
=
426 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
428 GLboolean dst_is_integer
;
431 uint8_t rebaseSwizzle
[4];
435 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
442 /* Depending on the base format involved we may need to apply a rebase
443 * transform (for example: if we download to a Luminance format we want
446 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
447 texImage
->_BaseFormat
== GL_INTENSITY
) {
449 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
450 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
451 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
452 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
453 } else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
455 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
456 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
457 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
458 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
459 } else if (texImage
->_BaseFormat
!=
460 _mesa_get_format_base_format(texFormat
)) {
462 _mesa_compute_rgba2base2rgba_component_mapping(texImage
->_BaseFormat
,
468 /* Describe the dst format */
469 dst_is_integer
= _mesa_is_enum_format_integer(format
);
470 dst_format
= _mesa_format_from_format_and_type(format
, type
);
471 dst_stride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
473 /* Since _mesa_format_convert does not handle transferOps we need to handle
474 * them before we call the function. This requires to convert to RGBA float
475 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
476 * integer then transferOps do not apply.
478 assert(!transferOps
|| (transferOps
&& !dst_is_integer
));
479 (void) dst_is_integer
; /* silence unused var warning */
481 for (img
= 0; img
< depth
; img
++) {
490 /* map src texture buffer */
491 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
492 xoffset
, yoffset
, width
, height
,
494 &srcMap
, &rowstride
);
496 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
501 dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
502 width
, height
, format
, type
,
506 uint32_t rgba_format
;
508 bool need_convert
= false;
510 /* We will convert to RGBA float */
511 rgba_format
= RGBA32_FLOAT
;
512 rgba_stride
= width
* 4 * sizeof(GLfloat
);
514 /* If we are lucky and the dst format matches the RGBA format we need
515 * to convert to, then we can convert directly into the dst buffer
516 * and avoid the final conversion/copy from the rgba buffer to the dst
519 if (format
== rgba_format
) {
521 } else if (rgba
== NULL
) { /* Allocate the RGBA buffer only once */
523 rgba
= malloc(height
* rgba_stride
);
525 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
526 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
531 _mesa_format_convert(rgba
, rgba_format
, rgba_stride
,
532 img_src
, texFormat
, rowstride
,
534 needsRebase
? rebaseSwizzle
: NULL
);
536 /* Handle transfer ops now */
537 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
* height
, rgba
);
539 /* If we had to rebase, we have already handled that */
542 /* If we were lucky and our RGBA conversion matches the dst format,
548 /* Otherwise, we need to convert from RGBA to dst next */
550 src_format
= rgba_format
;
551 src_stride
= rgba_stride
;
553 /* No RGBA conversion needed, convert directly to dst */
555 src_format
= texFormat
;
556 src_stride
= rowstride
;
559 /* Do the conversion to destination format */
560 _mesa_format_convert(dest
, dst_format
, dst_stride
,
561 src
, src_format
, src_stride
,
563 needsRebase
? rebaseSwizzle
: NULL
);
566 /* Handle byte swapping if required */
567 if (ctx
->Pack
.SwapBytes
)
568 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
569 width
, height
, dest
, dest
);
571 /* Unmap the src texture buffer */
572 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
582 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
583 * Compressed textures are handled here as well.
586 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
587 GLint xoffset
, GLint yoffset
, GLint zoffset
,
588 GLsizei width
, GLsizei height
, GLint depth
,
589 GLenum format
, GLenum type
, GLvoid
*pixels
,
590 struct gl_texture_image
*texImage
)
592 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
593 GLbitfield transferOps
= 0x0;
595 /* In general, clamping does not apply to glGetTexImage, except when
596 * the returned type of the image can't hold negative values.
598 if (type_needs_clamping(type
)) {
599 /* the returned image type can't have negative values */
600 if (dataType
== GL_FLOAT
||
601 dataType
== GL_HALF_FLOAT
||
602 dataType
== GL_SIGNED_NORMALIZED
||
603 format
== GL_LUMINANCE
||
604 format
== GL_LUMINANCE_ALPHA
) {
605 transferOps
|= IMAGE_CLAMP_BIT
;
609 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
610 get_tex_rgba_compressed(ctx
, dimensions
,
611 xoffset
, yoffset
, zoffset
,
612 width
, height
, depth
,
614 pixels
, texImage
, transferOps
);
617 get_tex_rgba_uncompressed(ctx
, dimensions
,
618 xoffset
, yoffset
, zoffset
,
619 width
, height
, depth
,
621 pixels
, texImage
, transferOps
);
627 * Try to do glGetTexImage() with simple memcpy().
628 * \return GL_TRUE if done, GL_FALSE otherwise
631 get_tex_memcpy(struct gl_context
*ctx
,
632 GLint xoffset
, GLint yoffset
, GLint zoffset
,
633 GLsizei width
, GLsizei height
, GLint depth
,
634 GLenum format
, GLenum type
, GLvoid
*pixels
,
635 struct gl_texture_image
*texImage
)
637 const GLenum target
= texImage
->TexObject
->Target
;
638 GLboolean memCopy
= GL_FALSE
;
639 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
642 * Check if we can use memcpy to copy from the hardware texture
643 * format to the user's format/type.
644 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
646 if ((target
== GL_TEXTURE_1D
||
647 target
== GL_TEXTURE_2D
||
648 target
== GL_TEXTURE_RECTANGLE
||
649 _mesa_is_cube_face(target
)) &&
650 texBaseFormat
== texImage
->_BaseFormat
) {
651 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
653 ctx
->Pack
.SwapBytes
, NULL
);
657 /* only a single slice is supported at this time */
662 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
663 const GLint bytesPerRow
= width
* bpp
;
665 _mesa_image_address2d(&ctx
->Pack
, pixels
, width
, height
,
667 const GLint dstRowStride
=
668 _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
672 /* map src texture buffer */
673 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
,
674 xoffset
, yoffset
, width
, height
,
675 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
678 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
679 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
683 for (row
= 0; row
< height
; row
++) {
684 memcpy(dst
, src
, bytesPerRow
);
690 /* unmap src texture buffer */
691 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
);
694 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
703 * This is the software fallback for Driver.GetTexSubImage().
704 * All error checking will have been done before this routine is called.
705 * We'll call ctx->Driver.MapTextureImage() to access the data, then
706 * unmap with ctx->Driver.UnmapTextureImage().
709 _mesa_GetTexSubImage_sw(struct gl_context
*ctx
,
710 GLint xoffset
, GLint yoffset
, GLint zoffset
,
711 GLsizei width
, GLsizei height
, GLint depth
,
712 GLenum format
, GLenum type
, GLvoid
*pixels
,
713 struct gl_texture_image
*texImage
)
715 const GLuint dimensions
=
716 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
718 /* map dest buffer, if PBO */
719 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
720 /* Packing texture image into a PBO.
721 * Map the (potentially) VRAM-based buffer into our process space so
722 * we can write into it with the code below.
723 * A hardware driver might use a sophisticated blit to move the
724 * texture data to the PBO if the PBO is in VRAM along with the texture.
726 GLubyte
*buf
= (GLubyte
*)
727 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
728 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
731 /* out of memory or other unexpected error */
732 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
735 /* <pixels> was an offset into the PBO.
736 * Now make it a real, client-side pointer inside the mapped region.
738 pixels
= ADD_POINTERS(buf
, pixels
);
741 if (get_tex_memcpy(ctx
, xoffset
, yoffset
, zoffset
, width
, height
, depth
,
742 format
, type
, pixels
, texImage
)) {
745 else if (format
== GL_DEPTH_COMPONENT
) {
746 get_tex_depth(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
747 width
, height
, depth
, format
, type
, pixels
, texImage
);
749 else if (format
== GL_DEPTH_STENCIL_EXT
) {
750 get_tex_depth_stencil(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
751 width
, height
, depth
, format
, type
, pixels
,
754 else if (format
== GL_STENCIL_INDEX
) {
755 get_tex_stencil(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
756 width
, height
, depth
, format
, type
, pixels
, texImage
);
758 else if (format
== GL_YCBCR_MESA
) {
759 get_tex_ycbcr(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
760 width
, height
, depth
, format
, type
, pixels
, texImage
);
763 get_tex_rgba(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
764 width
, height
, depth
, format
, type
, pixels
, texImage
);
767 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
768 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
775 * This is the software fallback for Driver.GetCompressedTexSubImage().
776 * All error checking will have been done before this routine is called.
779 _mesa_GetCompressedTexSubImage_sw(struct gl_context
*ctx
,
780 struct gl_texture_image
*texImage
,
781 GLint xoffset
, GLint yoffset
,
782 GLint zoffset
, GLsizei width
,
783 GLint height
, GLint depth
,
786 const GLuint dimensions
=
787 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
788 struct compressed_pixelstore store
;
792 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
793 width
, height
, depth
,
796 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
797 /* pack texture image into a PBO */
799 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
800 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
803 /* out of memory or other unexpected error */
804 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
805 "glGetCompresssedTexImage(map PBO failed)");
808 dest
= ADD_POINTERS(dest
, img
);
813 dest
+= store
.SkipBytes
;
815 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
819 /* map src texture buffer */
820 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ slice
,
821 xoffset
, yoffset
, width
, height
,
822 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
826 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
827 memcpy(dest
, src
, store
.CopyBytesPerRow
);
828 dest
+= store
.TotalBytesPerRow
;
832 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ slice
);
834 /* Advance to next slice */
835 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
-
836 store
.CopyRowsPerSlice
);
839 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
843 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
844 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
850 * Validate the texture target enum supplied to glGetTex(ture)Image or
851 * glGetCompressedTex(ture)Image.
854 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
861 case GL_TEXTURE_RECTANGLE_NV
:
862 return ctx
->Extensions
.NV_texture_rectangle
;
863 case GL_TEXTURE_1D_ARRAY_EXT
:
864 case GL_TEXTURE_2D_ARRAY_EXT
:
865 return ctx
->Extensions
.EXT_texture_array
;
866 case GL_TEXTURE_CUBE_MAP_ARRAY
:
867 return ctx
->Extensions
.ARB_texture_cube_map_array
;
869 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
871 * "An INVALID_ENUM error is generated if the effective target is not
872 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
873 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
874 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
875 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
877 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
878 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
879 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
880 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
881 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
882 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
883 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
884 case GL_TEXTURE_CUBE_MAP
:
885 return dsa
? GL_TRUE
: GL_FALSE
;
893 * Wrapper for _mesa_select_tex_image() which can handle target being
894 * GL_TEXTURE_CUBE_MAP_ARB in which case we use zoffset to select a cube face.
895 * This can happen for glGetTextureImage and glGetTextureSubImage (DSA
898 static struct gl_texture_image
*
899 select_tex_image(const struct gl_texture_object
*texObj
, GLenum target
,
900 GLint level
, GLint zoffset
)
903 assert(level
< MAX_TEXTURE_LEVELS
);
904 if (target
== GL_TEXTURE_CUBE_MAP
) {
905 assert(zoffset
>= 0);
907 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
;
909 return _mesa_select_tex_image(texObj
, target
, level
);
914 * Error-check the offset and size arguments to
915 * glGet[Compressed]TextureSubImage(). Also checks if the specified
916 * texture image is missing.
917 * \return true if error, false if no error.
920 dimensions_error_check(struct gl_context
*ctx
,
921 struct gl_texture_object
*texObj
,
922 GLenum target
, GLint level
,
923 GLint xoffset
, GLint yoffset
, GLint zoffset
,
924 GLsizei width
, GLsizei height
, GLsizei depth
,
927 const struct gl_texture_image
*texImage
;
931 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset = %d)", caller
, xoffset
);
936 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset = %d)", caller
, yoffset
);
941 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset = %d)", caller
, zoffset
);
946 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width = %d)", caller
, width
);
951 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height = %d)", caller
, height
);
956 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(depth = %d)", caller
, depth
);
960 /* do special per-target checks */
964 _mesa_error(ctx
, GL_INVALID_VALUE
,
965 "%s(1D, yoffset = %d)", caller
, yoffset
);
969 _mesa_error(ctx
, GL_INVALID_VALUE
,
970 "%s(1D, height = %d)", caller
, height
);
974 case GL_TEXTURE_1D_ARRAY
:
976 case GL_TEXTURE_RECTANGLE
:
978 _mesa_error(ctx
, GL_INVALID_VALUE
,
979 "%s(zoffset = %d)", caller
, zoffset
);
983 _mesa_error(ctx
, GL_INVALID_VALUE
,
984 "%s(depth = %d)", caller
, depth
);
988 case GL_TEXTURE_CUBE_MAP
:
989 /* Non-array cube maps are special because we have a gl_texture_image
992 if (zoffset
+ depth
> 6) {
993 _mesa_error(ctx
, GL_INVALID_VALUE
,
994 "%s(zoffset + depth = %d)", caller
, zoffset
+ depth
);
997 /* check that the range of faces exist */
998 for (i
= 0; i
< depth
; i
++) {
999 GLenum face
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
+ i
;
1000 if (!_mesa_select_tex_image(texObj
, face
, level
)) {
1001 /* non-existant face */
1002 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1003 "%s(missing cube face)", caller
);
1012 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1014 /* missing texture image */
1015 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(missing image)", caller
);
1019 if (xoffset
+ width
> texImage
->Width
) {
1020 _mesa_error(ctx
, GL_INVALID_VALUE
,
1021 "%s(xoffset %d + width %d > %u)",
1022 caller
, xoffset
, width
, texImage
->Width
);
1026 if (yoffset
+ height
> texImage
->Height
) {
1027 _mesa_error(ctx
, GL_INVALID_VALUE
,
1028 "%s(yoffset %d + height %d > %u)",
1029 caller
, yoffset
, height
, texImage
->Height
);
1033 if (target
!= GL_TEXTURE_CUBE_MAP
) {
1034 /* Cube map error checking was done above */
1035 if (zoffset
+ depth
> texImage
->Depth
) {
1036 _mesa_error(ctx
, GL_INVALID_VALUE
,
1037 "%s(zoffset %d + depth %d > %u)",
1038 caller
, zoffset
, depth
, texImage
->Depth
);
1043 /* Extra checks for compressed textures */
1046 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
1047 if (bw
> 1 || bh
> 1) {
1048 /* offset must be multiple of block size */
1049 if (xoffset
% bw
!= 0) {
1050 _mesa_error(ctx
, GL_INVALID_VALUE
,
1051 "%s(xoffset = %d)", caller
, xoffset
);
1054 if (target
!= GL_TEXTURE_1D
&& target
!= GL_TEXTURE_1D_ARRAY
) {
1055 if (yoffset
% bh
!= 0) {
1056 _mesa_error(ctx
, GL_INVALID_VALUE
,
1057 "%s(yoffset = %d)", caller
, yoffset
);
1062 /* The size must be a multiple of bw x bh, or we must be using a
1063 * offset+size that exactly hits the edge of the image.
1065 if ((width
% bw
!= 0) &&
1066 (xoffset
+ width
!= (GLint
) texImage
->Width
)) {
1067 _mesa_error(ctx
, GL_INVALID_VALUE
,
1068 "%s(width = %d)", caller
, width
);
1072 if ((height
% bh
!= 0) &&
1073 (yoffset
+ height
!= (GLint
) texImage
->Height
)) {
1074 _mesa_error(ctx
, GL_INVALID_VALUE
,
1075 "%s(height = %d)", caller
, height
);
1081 if (width
== 0 || height
== 0 || depth
== 0) {
1082 /* Not an error, but nothing to do. Return 'true' so that the
1083 * caller simply returns.
1093 * Do PBO-related error checking for getting uncompressed images.
1094 * \return true if there was an error (or the GetTexImage is to be a no-op)
1097 pbo_error_check(struct gl_context
*ctx
, GLenum target
,
1098 GLsizei width
, GLsizei height
, GLsizei depth
,
1099 GLenum format
, GLenum type
, GLsizei clientMemSize
,
1103 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
1105 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, width
, height
, depth
,
1106 format
, type
, clientMemSize
, pixels
)) {
1107 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1108 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1109 "%s(out of bounds PBO access)", caller
);
1111 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1112 "%s(out of bounds access: bufSize (%d) is too small)",
1113 caller
, clientMemSize
);
1118 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1119 /* PBO should not be mapped */
1120 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1121 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1122 "%s(PBO is mapped)", caller
);
1127 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1128 /* not an error, do nothing */
1137 * Do error checking for all (non-compressed) get-texture-image functions.
1138 * \return true if any error, false if no errors.
1141 getteximage_error_check(struct gl_context
*ctx
,
1142 struct gl_texture_object
*texObj
,
1143 GLenum target
, GLint level
,
1144 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1145 GLsizei width
, GLsizei height
, GLsizei depth
,
1146 GLenum format
, GLenum type
, GLsizei bufSize
,
1147 GLvoid
*pixels
, const char *caller
)
1149 struct gl_texture_image
*texImage
;
1150 GLenum baseFormat
, err
;
1155 if (texObj
->Target
== 0) {
1156 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture)", caller
);
1160 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1161 if (level
< 0 || level
>= maxLevels
) {
1162 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level = %d)", caller
, level
);
1166 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1167 if (err
!= GL_NO_ERROR
) {
1168 _mesa_error(ctx
, err
, "%s(format/type)", caller
);
1172 if (dimensions_error_check(ctx
, texObj
, target
, level
,
1173 xoffset
, yoffset
, zoffset
,
1174 width
, height
, depth
, caller
)) {
1178 if (pbo_error_check(ctx
, target
, width
, height
, depth
,
1179 format
, type
, bufSize
, pixels
, caller
)) {
1183 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1187 * Format and type checking has been moved up to GetnTexImage and
1188 * GetTextureImage so that it happens before getting the texImage object.
1191 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
1193 /* Make sure the requested image format is compatible with the
1196 if (_mesa_is_color_format(format
)
1197 && !_mesa_is_color_format(baseFormat
)) {
1198 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1199 "%s(format mismatch)", caller
);
1202 else if (_mesa_is_depth_format(format
)
1203 && !_mesa_is_depth_format(baseFormat
)
1204 && !_mesa_is_depthstencil_format(baseFormat
)) {
1205 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1206 "%s(format mismatch)", caller
);
1209 else if (_mesa_is_stencil_format(format
)
1210 && !ctx
->Extensions
.ARB_texture_stencil8
) {
1211 _mesa_error(ctx
, GL_INVALID_ENUM
,
1212 "%s(format=GL_STENCIL_INDEX)", caller
);
1215 else if (_mesa_is_stencil_format(format
)
1216 && !_mesa_is_depthstencil_format(baseFormat
)
1217 && !_mesa_is_stencil_format(baseFormat
)) {
1218 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1219 "%s(format mismatch)", caller
);
1222 else if (_mesa_is_ycbcr_format(format
)
1223 && !_mesa_is_ycbcr_format(baseFormat
)) {
1224 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1225 "%s(format mismatch)", caller
);
1228 else if (_mesa_is_depthstencil_format(format
)
1229 && !_mesa_is_depthstencil_format(baseFormat
)) {
1230 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1231 "%s(format mismatch)", caller
);
1234 else if (!_mesa_is_stencil_format(format
) &&
1235 _mesa_is_enum_format_integer(format
) !=
1236 _mesa_is_format_integer(texImage
->TexFormat
)) {
1237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1238 "%s(format mismatch)", caller
);
1247 * Return the width, height and depth of a texture image.
1248 * This function must be resilient to bad parameter values since
1249 * this is called before full error checking.
1252 get_texture_image_dims(const struct gl_texture_object
*texObj
,
1253 GLenum target
, GLint level
,
1254 GLsizei
*width
, GLsizei
*height
, GLsizei
*depth
)
1256 const struct gl_texture_image
*texImage
= NULL
;
1258 if (level
>= 0 && level
< MAX_TEXTURE_LEVELS
) {
1259 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1263 *width
= texImage
->Width
;
1264 *height
= texImage
->Height
;
1265 if (target
== GL_TEXTURE_CUBE_MAP
) {
1269 *depth
= texImage
->Depth
;
1273 *width
= *height
= *depth
= 0;
1279 * Common code for all (uncompressed) get-texture-image functions.
1280 * \param texObj the texture object (should not be null)
1281 * \param target user-provided target, or 0 for DSA
1282 * \param level image level.
1283 * \param format pixel data format for returned image.
1284 * \param type pixel data type for returned image.
1285 * \param bufSize size of the pixels data buffer.
1286 * \param pixels returned pixel data.
1287 * \param caller name of calling function
1290 get_texture_image(struct gl_context
*ctx
,
1291 struct gl_texture_object
*texObj
,
1292 GLenum target
, GLint level
,
1293 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1294 GLsizei width
, GLsizei height
, GLint depth
,
1295 GLenum format
, GLenum type
,
1296 GLvoid
*pixels
, const char *caller
)
1298 struct gl_texture_image
*texImage
;
1299 unsigned firstFace
, numFaces
, i
;
1302 FLUSH_VERTICES(ctx
, 0);
1304 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1305 assert(texImage
); /* should have been error checked already */
1307 if (_mesa_is_zero_size_texture(texImage
)) {
1308 /* no image data to return */
1312 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1313 _mesa_debug(ctx
, "%s(tex %u) format = %s, w=%d, h=%d,"
1314 " dstFmt=0x%x, dstType=0x%x\n",
1315 caller
, texObj
->Name
,
1316 _mesa_get_format_name(texImage
->TexFormat
),
1317 texImage
->Width
, texImage
->Height
,
1321 if (target
== GL_TEXTURE_CUBE_MAP
) {
1322 /* Compute stride between cube faces */
1323 imageStride
= _mesa_image_image_stride(&ctx
->Pack
, width
, height
,
1325 firstFace
= zoffset
;
1332 firstFace
= _mesa_tex_target_to_face(target
);
1336 _mesa_lock_texture(ctx
, texObj
);
1338 for (i
= 0; i
< numFaces
; i
++) {
1339 texImage
= texObj
->Image
[firstFace
+ i
][level
];
1342 ctx
->Driver
.GetTexSubImage(ctx
, xoffset
, yoffset
, zoffset
,
1343 width
, height
, depth
,
1344 format
, type
, pixels
, texImage
);
1346 /* next cube face */
1347 pixels
= (GLubyte
*) pixels
+ imageStride
;
1350 _mesa_unlock_texture(ctx
, texObj
);
1355 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
, GLenum type
,
1356 GLsizei bufSize
, GLvoid
*pixels
)
1358 GET_CURRENT_CONTEXT(ctx
);
1359 static const char *caller
= "glGetnTexImageARB";
1360 GLsizei width
, height
, depth
;
1361 struct gl_texture_object
*texObj
;
1363 if (!legal_getteximage_target(ctx
, target
, false)) {
1364 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1368 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1371 get_texture_image_dims(texObj
, target
, level
, &width
, &height
, &depth
);
1373 if (getteximage_error_check(ctx
, texObj
, target
, level
,
1374 0, 0, 0, width
, height
, depth
,
1375 format
, type
, bufSize
, pixels
, caller
)) {
1379 get_texture_image(ctx
, texObj
, target
, level
,
1380 0, 0, 0, width
, height
, depth
,
1381 format
, type
, pixels
, caller
);
1386 _mesa_GetTexImage(GLenum target
, GLint level
, GLenum format
, GLenum type
,
1389 GET_CURRENT_CONTEXT(ctx
);
1390 static const char *caller
= "glGetTexImage";
1391 GLsizei width
, height
, depth
;
1392 struct gl_texture_object
*texObj
;
1394 if (!legal_getteximage_target(ctx
, target
, false)) {
1395 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1399 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1402 get_texture_image_dims(texObj
, target
, level
, &width
, &height
, &depth
);
1404 if (getteximage_error_check(ctx
, texObj
, target
, level
,
1405 0, 0, 0, width
, height
, depth
,
1406 format
, type
, INT_MAX
, pixels
, caller
)) {
1410 get_texture_image(ctx
, texObj
, target
, level
,
1411 0, 0, 0, width
, height
, depth
,
1412 format
, type
, pixels
, caller
);
1417 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
, GLenum type
,
1418 GLsizei bufSize
, GLvoid
*pixels
)
1420 GET_CURRENT_CONTEXT(ctx
);
1421 GLsizei width
, height
, depth
;
1422 static const char *caller
= "glGetTextureImage";
1423 struct gl_texture_object
*texObj
=
1424 _mesa_lookup_texture_err(ctx
, texture
, caller
);
1430 get_texture_image_dims(texObj
, texObj
->Target
, level
,
1431 &width
, &height
, &depth
);
1433 if (getteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1434 0, 0, 0, width
, height
, depth
,
1435 format
, type
, bufSize
, pixels
, caller
)) {
1439 get_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1440 0, 0, 0, width
, height
, depth
,
1441 format
, type
, pixels
, caller
);
1446 _mesa_GetTextureSubImage(GLuint texture
, GLint level
,
1447 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1448 GLsizei width
, GLsizei height
, GLsizei depth
,
1449 GLenum format
, GLenum type
, GLsizei bufSize
,
1452 GET_CURRENT_CONTEXT(ctx
);
1453 static const char *caller
= "glGetTextureSubImage";
1454 struct gl_texture_object
*texObj
=
1455 _mesa_lookup_texture_err(ctx
, texture
, caller
);
1461 if (getteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1462 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
1463 format
, type
, bufSize
, pixels
, caller
)) {
1467 get_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1468 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
1469 format
, type
, pixels
, caller
);
1475 * Compute the number of bytes which will be written when retrieving
1476 * a sub-region of a compressed texture.
1479 packed_compressed_size(GLuint dimensions
, mesa_format format
,
1480 GLsizei width
, GLsizei height
, GLsizei depth
,
1481 const struct gl_pixelstore_attrib
*packing
)
1483 struct compressed_pixelstore st
;
1486 _mesa_compute_compressed_pixelstore(dimensions
, format
,
1487 width
, height
, depth
,
1490 (st
.CopySlices
- 1) * st
.TotalRowsPerSlice
* st
.TotalBytesPerRow
+
1492 (st
.CopyRowsPerSlice
- 1) * st
.TotalBytesPerRow
+
1500 * Do error checking for getting compressed texture images.
1501 * \return true if any error, false if no errors.
1504 getcompressedteximage_error_check(struct gl_context
*ctx
,
1505 struct gl_texture_object
*texObj
,
1506 GLenum target
, GLint level
,
1507 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1508 GLsizei width
, GLsizei height
, GLsizei depth
,
1509 GLsizei bufSize
, GLvoid
*pixels
,
1512 struct gl_texture_image
*texImage
;
1519 if (texObj
->Target
== 0) {
1520 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture)", caller
);
1524 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1525 if (level
< 0 || level
>= maxLevels
) {
1526 _mesa_error(ctx
, GL_INVALID_VALUE
,
1527 "%s(bad level = %d)", caller
, level
);
1531 if (dimensions_error_check(ctx
, texObj
, target
, level
,
1532 xoffset
, yoffset
, zoffset
,
1533 width
, height
, depth
, caller
)) {
1537 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1540 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1541 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1542 "%s(texture is not compressed)", caller
);
1546 /* Check for invalid pixel storage modes */
1547 dimensions
= _mesa_get_texture_dimensions(texObj
->Target
);
1548 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1554 /* Compute number of bytes that may be touched in the dest buffer */
1555 totalBytes
= packed_compressed_size(dimensions
, texImage
->TexFormat
,
1556 width
, height
, depth
,
1559 /* Do dest buffer bounds checking */
1560 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1561 /* do bounds checking on PBO write */
1562 if ((GLubyte
*) pixels
+ totalBytes
>
1563 (GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1564 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1565 "%s(out of bounds PBO access)", caller
);
1569 /* make sure PBO is not mapped */
1570 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1571 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", caller
);
1576 /* do bounds checking on writing to client memory */
1577 if (totalBytes
> bufSize
) {
1578 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1579 "%s(out of bounds access: bufSize (%d) is too small)",
1585 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1586 /* not an error, but do nothing */
1595 * Common helper for all glGetCompressed-teximage functions.
1598 get_compressed_texture_image(struct gl_context
*ctx
,
1599 struct gl_texture_object
*texObj
,
1600 GLenum target
, GLint level
,
1601 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1602 GLsizei width
, GLsizei height
, GLint depth
,
1606 struct gl_texture_image
*texImage
;
1607 unsigned firstFace
, numFaces
, i
, imageStride
;
1609 FLUSH_VERTICES(ctx
, 0);
1611 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1612 assert(texImage
); /* should have been error checked already */
1614 if (_mesa_is_zero_size_texture(texImage
))
1617 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1619 "%s(tex %u) format = %s, w=%d, h=%d\n",
1620 caller
, texObj
->Name
,
1621 _mesa_get_format_name(texImage
->TexFormat
),
1622 texImage
->Width
, texImage
->Height
);
1625 if (target
== GL_TEXTURE_CUBE_MAP
) {
1626 struct compressed_pixelstore store
;
1628 /* Compute image stride between cube faces */
1629 _mesa_compute_compressed_pixelstore(2, texImage
->TexFormat
,
1630 width
, height
, depth
,
1631 &ctx
->Pack
, &store
);
1632 imageStride
= store
.TotalBytesPerRow
* store
.TotalRowsPerSlice
;
1634 firstFace
= zoffset
;
1641 firstFace
= _mesa_tex_target_to_face(target
);
1645 _mesa_lock_texture(ctx
, texObj
);
1647 for (i
= 0; i
< numFaces
; i
++) {
1648 texImage
= texObj
->Image
[firstFace
+ i
][level
];
1651 ctx
->Driver
.GetCompressedTexSubImage(ctx
, texImage
,
1652 xoffset
, yoffset
, zoffset
,
1653 width
, height
, depth
, pixels
);
1655 /* next cube face */
1656 pixels
= (GLubyte
*) pixels
+ imageStride
;
1659 _mesa_unlock_texture(ctx
, texObj
);
1664 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1667 GET_CURRENT_CONTEXT(ctx
);
1668 static const char *caller
= "glGetnCompressedTexImageARB";
1669 GLsizei width
, height
, depth
;
1670 struct gl_texture_object
*texObj
;
1672 if (!legal_getteximage_target(ctx
, target
, false)) {
1673 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1677 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1680 get_texture_image_dims(texObj
, target
, level
, &width
, &height
, &depth
);
1682 if (getcompressedteximage_error_check(ctx
, texObj
, target
, level
,
1683 0, 0, 0, width
, height
, depth
,
1684 INT_MAX
, pixels
, caller
)) {
1688 get_compressed_texture_image(ctx
, texObj
, target
, level
,
1689 0, 0, 0, width
, height
, depth
,
1695 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*pixels
)
1697 GET_CURRENT_CONTEXT(ctx
);
1698 static const char *caller
= "glGetCompressedTexImage";
1699 GLsizei width
, height
, depth
;
1700 struct gl_texture_object
*texObj
;
1702 if (!legal_getteximage_target(ctx
, target
, false)) {
1703 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1707 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1710 get_texture_image_dims(texObj
, target
, level
,
1711 &width
, &height
, &depth
);
1713 if (getcompressedteximage_error_check(ctx
, texObj
, target
, level
,
1714 0, 0, 0, width
, height
, depth
,
1715 INT_MAX
, pixels
, caller
)) {
1719 get_compressed_texture_image(ctx
, texObj
, target
, level
,
1720 0, 0, 0, width
, height
, depth
,
1726 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1727 GLsizei bufSize
, GLvoid
*pixels
)
1729 GET_CURRENT_CONTEXT(ctx
);
1730 static const char *caller
= "glGetCompressedTextureImage";
1731 GLsizei width
, height
, depth
;
1732 struct gl_texture_object
*texObj
=
1733 _mesa_lookup_texture_err(ctx
, texture
, caller
);
1739 get_texture_image_dims(texObj
, texObj
->Target
, level
,
1740 &width
, &height
, &depth
);
1742 if (getcompressedteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1743 0, 0, 0, width
, height
, depth
,
1744 bufSize
, pixels
, caller
)) {
1748 get_compressed_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1749 0, 0, 0, width
, height
, depth
,
1755 _mesa_GetCompressedTextureSubImage(GLuint texture
, GLint level
,
1756 GLint xoffset
, GLint yoffset
,
1757 GLint zoffset
, GLsizei width
,
1758 GLsizei height
, GLsizei depth
,
1759 GLsizei bufSize
, void *pixels
)
1761 GET_CURRENT_CONTEXT(ctx
);
1762 static const char *caller
= "glGetCompressedTextureImage";
1763 struct gl_texture_object
*texObj
;
1765 texObj
= _mesa_lookup_texture_err(ctx
, texture
, caller
);
1770 if (getcompressedteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1771 xoffset
, yoffset
, zoffset
,
1772 width
, height
, depth
,
1773 bufSize
, pixels
, caller
)) {
1777 get_compressed_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1778 xoffset
, yoffset
, zoffset
,
1779 width
, height
, depth
,