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
* 4 * sizeof(GLfloat
));
302 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
306 /* Decompress the texture image slices - results in 'tempImage' */
307 for (slice
= 0; slice
< depth
; slice
++) {
311 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
313 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ slice
,
314 xoffset
, yoffset
, width
, height
,
316 &srcMap
, &srcRowStride
);
318 _mesa_decompress_image(texFormat
, width
, height
,
319 srcMap
, srcRowStride
, tempSlice
);
321 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ slice
);
324 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
330 /* Depending on the base format involved we may need to apply a rebase
331 * transform (for example: if we download to a Luminance format we want
334 if (baseFormat
== GL_LUMINANCE
||
335 baseFormat
== GL_INTENSITY
) {
337 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
338 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
339 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
340 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
341 } else if (baseFormat
== GL_LUMINANCE_ALPHA
) {
343 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
344 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
345 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
346 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
351 srcStride
= 4 * width
* sizeof(GLfloat
);
352 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
353 dstFormat
= _mesa_format_from_format_and_type(format
, type
);
354 tempSlice
= tempImage
;
355 for (slice
= 0; slice
< depth
; slice
++) {
356 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
357 width
, height
, format
, type
,
359 _mesa_format_convert(dest
, dstFormat
, dstStride
,
360 tempSlice
, RGBA32_FLOAT
, srcStride
,
362 needsRebase
? rebaseSwizzle
: NULL
);
364 /* Handle byte swapping if required */
365 if (ctx
->Pack
.SwapBytes
) {
366 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
367 width
, height
, dest
, dest
);
370 tempSlice
+= 4 * width
* height
;
378 * Return a base GL format given the user-requested format
379 * for glGetTexImage().
382 _mesa_base_pack_format(GLenum format
)
387 case GL_BGRA_INTEGER
:
388 case GL_RGBA_INTEGER
:
396 case GL_GREEN_INTEGER
:
398 case GL_BLUE_INTEGER
:
400 case GL_ALPHA_INTEGER
:
402 case GL_LUMINANCE_INTEGER_EXT
:
404 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
405 return GL_LUMINANCE_ALPHA
;
413 * Get an uncompressed color texture image.
416 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
417 GLint xoffset
, GLint yoffset
, GLint zoffset
,
418 GLsizei width
, GLsizei height
, GLint depth
,
419 GLenum format
, GLenum type
, GLvoid
*pixels
,
420 struct gl_texture_image
*texImage
,
421 GLbitfield transferOps
)
423 /* don't want to apply sRGB -> RGB conversion here so override the format */
424 const mesa_format texFormat
=
425 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
427 GLboolean dst_is_integer
;
430 uint8_t rebaseSwizzle
[4];
434 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
441 /* Depending on the base format involved we may need to apply a rebase
442 * transform (for example: if we download to a Luminance format we want
445 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
446 texImage
->_BaseFormat
== GL_INTENSITY
) {
448 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
449 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
450 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
451 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
452 } else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
454 rebaseSwizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
455 rebaseSwizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
456 rebaseSwizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
457 rebaseSwizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
458 } else if (texImage
->_BaseFormat
!=
459 _mesa_get_format_base_format(texFormat
)) {
461 _mesa_compute_rgba2base2rgba_component_mapping(texImage
->_BaseFormat
,
467 /* Describe the dst format */
468 dst_is_integer
= _mesa_is_enum_format_integer(format
);
469 dst_format
= _mesa_format_from_format_and_type(format
, type
);
470 dst_stride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
472 /* Since _mesa_format_convert does not handle transferOps we need to handle
473 * them before we call the function. This requires to convert to RGBA float
474 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
475 * integer then transferOps do not apply.
477 assert(!transferOps
|| (transferOps
&& !dst_is_integer
));
478 (void) dst_is_integer
; /* silence unused var warning */
480 for (img
= 0; img
< depth
; img
++) {
489 /* map src texture buffer */
490 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ img
,
491 xoffset
, yoffset
, width
, height
,
493 &srcMap
, &rowstride
);
495 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
500 dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
501 width
, height
, format
, type
,
505 uint32_t rgba_format
;
507 bool need_convert
= false;
509 /* We will convert to RGBA float */
510 rgba_format
= RGBA32_FLOAT
;
511 rgba_stride
= width
* 4 * sizeof(GLfloat
);
513 /* If we are lucky and the dst format matches the RGBA format we need
514 * to convert to, then we can convert directly into the dst buffer
515 * and avoid the final conversion/copy from the rgba buffer to the dst
518 if (format
== rgba_format
) {
520 } else if (rgba
== NULL
) { /* Allocate the RGBA buffer only once */
522 rgba
= malloc(height
* rgba_stride
);
524 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
525 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
530 _mesa_format_convert(rgba
, rgba_format
, rgba_stride
,
531 img_src
, texFormat
, rowstride
,
533 needsRebase
? rebaseSwizzle
: NULL
);
535 /* Handle transfer ops now */
536 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
* height
, rgba
);
538 /* If we had to rebase, we have already handled that */
541 /* If we were lucky and our RGBA conversion matches the dst format,
547 /* Otherwise, we need to convert from RGBA to dst next */
549 src_format
= rgba_format
;
550 src_stride
= rgba_stride
;
552 /* No RGBA conversion needed, convert directly to dst */
554 src_format
= texFormat
;
555 src_stride
= rowstride
;
558 /* Do the conversion to destination format */
559 _mesa_format_convert(dest
, dst_format
, dst_stride
,
560 src
, src_format
, src_stride
,
562 needsRebase
? rebaseSwizzle
: NULL
);
565 /* Handle byte swapping if required */
566 if (ctx
->Pack
.SwapBytes
)
567 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
568 width
, height
, dest
, dest
);
570 /* Unmap the src texture buffer */
571 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ img
);
581 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
582 * Compressed textures are handled here as well.
585 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
586 GLint xoffset
, GLint yoffset
, GLint zoffset
,
587 GLsizei width
, GLsizei height
, GLint depth
,
588 GLenum format
, GLenum type
, GLvoid
*pixels
,
589 struct gl_texture_image
*texImage
)
591 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
592 GLbitfield transferOps
= 0x0;
594 /* In general, clamping does not apply to glGetTexImage, except when
595 * the returned type of the image can't hold negative values.
597 if (type_needs_clamping(type
)) {
598 /* the returned image type can't have negative values */
599 if (dataType
== GL_FLOAT
||
600 dataType
== GL_HALF_FLOAT
||
601 dataType
== GL_SIGNED_NORMALIZED
||
602 format
== GL_LUMINANCE
||
603 format
== GL_LUMINANCE_ALPHA
) {
604 transferOps
|= IMAGE_CLAMP_BIT
;
608 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
609 get_tex_rgba_compressed(ctx
, dimensions
,
610 xoffset
, yoffset
, zoffset
,
611 width
, height
, depth
,
613 pixels
, texImage
, transferOps
);
616 get_tex_rgba_uncompressed(ctx
, dimensions
,
617 xoffset
, yoffset
, zoffset
,
618 width
, height
, depth
,
620 pixels
, texImage
, transferOps
);
626 * Try to do glGetTexImage() with simple memcpy().
627 * \return GL_TRUE if done, GL_FALSE otherwise
630 get_tex_memcpy(struct gl_context
*ctx
,
631 GLint xoffset
, GLint yoffset
, GLint zoffset
,
632 GLsizei width
, GLsizei height
, GLint depth
,
633 GLenum format
, GLenum type
, GLvoid
*pixels
,
634 struct gl_texture_image
*texImage
)
636 const GLenum target
= texImage
->TexObject
->Target
;
637 GLboolean memCopy
= GL_FALSE
;
638 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
641 * Check if we can use memcpy to copy from the hardware texture
642 * format to the user's format/type.
643 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
645 if ((target
== GL_TEXTURE_1D
||
646 target
== GL_TEXTURE_2D
||
647 target
== GL_TEXTURE_RECTANGLE
||
648 _mesa_is_cube_face(target
)) &&
649 texBaseFormat
== texImage
->_BaseFormat
) {
650 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
652 ctx
->Pack
.SwapBytes
, NULL
);
656 /* only a single slice is supported at this time */
661 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
662 const GLint bytesPerRow
= width
* bpp
;
664 _mesa_image_address2d(&ctx
->Pack
, pixels
, width
, height
,
666 const GLint dstRowStride
=
667 _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
671 /* map src texture buffer */
672 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
,
673 xoffset
, yoffset
, width
, height
,
674 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
677 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
678 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
682 for (row
= 0; row
< height
; row
++) {
683 memcpy(dst
, src
, bytesPerRow
);
689 /* unmap src texture buffer */
690 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
);
693 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
702 * This is the software fallback for Driver.GetTexSubImage().
703 * All error checking will have been done before this routine is called.
704 * We'll call ctx->Driver.MapTextureImage() to access the data, then
705 * unmap with ctx->Driver.UnmapTextureImage().
708 _mesa_GetTexSubImage_sw(struct gl_context
*ctx
,
709 GLint xoffset
, GLint yoffset
, GLint zoffset
,
710 GLsizei width
, GLsizei height
, GLint depth
,
711 GLenum format
, GLenum type
, GLvoid
*pixels
,
712 struct gl_texture_image
*texImage
)
714 const GLuint dimensions
=
715 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
717 /* map dest buffer, if PBO */
718 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
719 /* Packing texture image into a PBO.
720 * Map the (potentially) VRAM-based buffer into our process space so
721 * we can write into it with the code below.
722 * A hardware driver might use a sophisticated blit to move the
723 * texture data to the PBO if the PBO is in VRAM along with the texture.
725 GLubyte
*buf
= (GLubyte
*)
726 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
727 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
730 /* out of memory or other unexpected error */
731 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
734 /* <pixels> was an offset into the PBO.
735 * Now make it a real, client-side pointer inside the mapped region.
737 pixels
= ADD_POINTERS(buf
, pixels
);
740 if (get_tex_memcpy(ctx
, xoffset
, yoffset
, zoffset
, width
, height
, depth
,
741 format
, type
, pixels
, texImage
)) {
744 else if (format
== GL_DEPTH_COMPONENT
) {
745 get_tex_depth(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
746 width
, height
, depth
, format
, type
, pixels
, texImage
);
748 else if (format
== GL_DEPTH_STENCIL_EXT
) {
749 get_tex_depth_stencil(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
750 width
, height
, depth
, format
, type
, pixels
,
753 else if (format
== GL_STENCIL_INDEX
) {
754 get_tex_stencil(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
755 width
, height
, depth
, format
, type
, pixels
, texImage
);
757 else if (format
== GL_YCBCR_MESA
) {
758 get_tex_ycbcr(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
759 width
, height
, depth
, format
, type
, pixels
, texImage
);
762 get_tex_rgba(ctx
, dimensions
, xoffset
, yoffset
, zoffset
,
763 width
, height
, depth
, format
, type
, pixels
, texImage
);
766 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
767 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
774 * This is the software fallback for Driver.GetCompressedTexSubImage().
775 * All error checking will have been done before this routine is called.
778 _mesa_GetCompressedTexSubImage_sw(struct gl_context
*ctx
,
779 struct gl_texture_image
*texImage
,
780 GLint xoffset
, GLint yoffset
,
781 GLint zoffset
, GLsizei width
,
782 GLint height
, GLint depth
,
785 const GLuint dimensions
=
786 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
787 struct compressed_pixelstore store
;
791 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
792 width
, height
, depth
,
795 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
796 /* pack texture image into a PBO */
798 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
799 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
802 /* out of memory or other unexpected error */
803 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
804 "glGetCompresssedTexImage(map PBO failed)");
807 dest
= ADD_POINTERS(dest
, img
);
812 dest
+= store
.SkipBytes
;
814 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
818 /* map src texture buffer */
819 ctx
->Driver
.MapTextureImage(ctx
, texImage
, zoffset
+ slice
,
820 xoffset
, yoffset
, width
, height
,
821 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
825 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
826 memcpy(dest
, src
, store
.CopyBytesPerRow
);
827 dest
+= store
.TotalBytesPerRow
;
831 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, zoffset
+ slice
);
833 /* Advance to next slice */
834 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
-
835 store
.CopyRowsPerSlice
);
838 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
842 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
843 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
849 * Validate the texture target enum supplied to glGetTex(ture)Image or
850 * glGetCompressedTex(ture)Image.
853 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
, bool dsa
)
860 case GL_TEXTURE_RECTANGLE_NV
:
861 return ctx
->Extensions
.NV_texture_rectangle
;
862 case GL_TEXTURE_1D_ARRAY_EXT
:
863 case GL_TEXTURE_2D_ARRAY_EXT
:
864 return ctx
->Extensions
.EXT_texture_array
;
865 case GL_TEXTURE_CUBE_MAP_ARRAY
:
866 return ctx
->Extensions
.ARB_texture_cube_map_array
;
868 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
870 * "An INVALID_ENUM error is generated if the effective target is not
871 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
872 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
873 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
874 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
876 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
877 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
878 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
879 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
880 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
881 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
882 return dsa
? GL_FALSE
: ctx
->Extensions
.ARB_texture_cube_map
;
883 case GL_TEXTURE_CUBE_MAP
:
884 return dsa
? GL_TRUE
: GL_FALSE
;
892 * Wrapper for _mesa_select_tex_image() which can handle target being
893 * GL_TEXTURE_CUBE_MAP_ARB in which case we use zoffset to select a cube face.
894 * This can happen for glGetTextureImage and glGetTextureSubImage (DSA
897 static struct gl_texture_image
*
898 select_tex_image(const struct gl_texture_object
*texObj
, GLenum target
,
899 GLint level
, GLint zoffset
)
902 assert(level
< MAX_TEXTURE_LEVELS
);
903 if (target
== GL_TEXTURE_CUBE_MAP
) {
904 assert(zoffset
>= 0);
906 target
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
;
908 return _mesa_select_tex_image(texObj
, target
, level
);
913 * Error-check the offset and size arguments to
914 * glGet[Compressed]TextureSubImage(). Also checks if the specified
915 * texture image is missing.
916 * \return true if error, false if no error.
919 dimensions_error_check(struct gl_context
*ctx
,
920 struct gl_texture_object
*texObj
,
921 GLenum target
, GLint level
,
922 GLint xoffset
, GLint yoffset
, GLint zoffset
,
923 GLsizei width
, GLsizei height
, GLsizei depth
,
926 const struct gl_texture_image
*texImage
;
930 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(xoffset = %d)", caller
, xoffset
);
935 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(yoffset = %d)", caller
, yoffset
);
940 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(zoffset = %d)", caller
, zoffset
);
945 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(width = %d)", caller
, width
);
950 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(height = %d)", caller
, height
);
955 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(depth = %d)", caller
, depth
);
959 /* do special per-target checks */
963 _mesa_error(ctx
, GL_INVALID_VALUE
,
964 "%s(1D, yoffset = %d)", caller
, yoffset
);
968 _mesa_error(ctx
, GL_INVALID_VALUE
,
969 "%s(1D, height = %d)", caller
, height
);
973 case GL_TEXTURE_1D_ARRAY
:
975 case GL_TEXTURE_RECTANGLE
:
977 _mesa_error(ctx
, GL_INVALID_VALUE
,
978 "%s(zoffset = %d)", caller
, zoffset
);
982 _mesa_error(ctx
, GL_INVALID_VALUE
,
983 "%s(depth = %d)", caller
, depth
);
987 case GL_TEXTURE_CUBE_MAP
:
988 /* Non-array cube maps are special because we have a gl_texture_image
991 if (zoffset
+ depth
> 6) {
992 _mesa_error(ctx
, GL_INVALID_VALUE
,
993 "%s(zoffset + depth = %d)", caller
, zoffset
+ depth
);
996 /* check that the range of faces exist */
997 for (i
= 0; i
< depth
; i
++) {
998 GLenum face
= GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ zoffset
+ i
;
999 if (!_mesa_select_tex_image(texObj
, face
, level
)) {
1000 /* non-existant face */
1001 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1002 "%s(missing cube face)", caller
);
1011 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1013 /* missing texture image */
1014 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(missing image)", caller
);
1018 if (xoffset
+ width
> texImage
->Width
) {
1019 _mesa_error(ctx
, GL_INVALID_VALUE
,
1020 "%s(xoffset %d + width %d > %u)",
1021 caller
, xoffset
, width
, texImage
->Width
);
1025 if (yoffset
+ height
> texImage
->Height
) {
1026 _mesa_error(ctx
, GL_INVALID_VALUE
,
1027 "%s(yoffset %d + height %d > %u)",
1028 caller
, yoffset
, height
, texImage
->Height
);
1032 if (target
!= GL_TEXTURE_CUBE_MAP
) {
1033 /* Cube map error checking was done above */
1034 if (zoffset
+ depth
> texImage
->Depth
) {
1035 _mesa_error(ctx
, GL_INVALID_VALUE
,
1036 "%s(zoffset %d + depth %d > %u)",
1037 caller
, zoffset
, depth
, texImage
->Depth
);
1042 /* Extra checks for compressed textures */
1045 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
1046 if (bw
> 1 || bh
> 1) {
1047 /* offset must be multiple of block size */
1048 if (xoffset
% bw
!= 0) {
1049 _mesa_error(ctx
, GL_INVALID_VALUE
,
1050 "%s(xoffset = %d)", caller
, xoffset
);
1053 if (target
!= GL_TEXTURE_1D
&& target
!= GL_TEXTURE_1D_ARRAY
) {
1054 if (yoffset
% bh
!= 0) {
1055 _mesa_error(ctx
, GL_INVALID_VALUE
,
1056 "%s(yoffset = %d)", caller
, yoffset
);
1061 /* The size must be a multiple of bw x bh, or we must be using a
1062 * offset+size that exactly hits the edge of the image.
1064 if ((width
% bw
!= 0) &&
1065 (xoffset
+ width
!= (GLint
) texImage
->Width
)) {
1066 _mesa_error(ctx
, GL_INVALID_VALUE
,
1067 "%s(width = %d)", caller
, width
);
1071 if ((height
% bh
!= 0) &&
1072 (yoffset
+ height
!= (GLint
) texImage
->Height
)) {
1073 _mesa_error(ctx
, GL_INVALID_VALUE
,
1074 "%s(height = %d)", caller
, height
);
1080 if (width
== 0 || height
== 0 || depth
== 0) {
1081 /* Not an error, but nothing to do. Return 'true' so that the
1082 * caller simply returns.
1092 * Do PBO-related error checking for getting uncompressed images.
1093 * \return true if there was an error (or the GetTexImage is to be a no-op)
1096 pbo_error_check(struct gl_context
*ctx
, GLenum target
,
1097 GLsizei width
, GLsizei height
, GLsizei depth
,
1098 GLenum format
, GLenum type
, GLsizei clientMemSize
,
1102 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
1104 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, width
, height
, depth
,
1105 format
, type
, clientMemSize
, pixels
)) {
1106 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1107 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1108 "%s(out of bounds PBO access)", caller
);
1110 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1111 "%s(out of bounds access: bufSize (%d) is too small)",
1112 caller
, clientMemSize
);
1117 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1118 /* PBO should not be mapped */
1119 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1120 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1121 "%s(PBO is mapped)", caller
);
1126 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1127 /* not an error, do nothing */
1136 * Do error checking for all (non-compressed) get-texture-image functions.
1137 * \return true if any error, false if no errors.
1140 getteximage_error_check(struct gl_context
*ctx
,
1141 struct gl_texture_object
*texObj
,
1142 GLenum target
, GLint level
,
1143 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1144 GLsizei width
, GLsizei height
, GLsizei depth
,
1145 GLenum format
, GLenum type
, GLsizei bufSize
,
1146 GLvoid
*pixels
, const char *caller
)
1148 struct gl_texture_image
*texImage
;
1149 GLenum baseFormat
, err
;
1154 if (texObj
->Target
== 0) {
1155 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture)", caller
);
1159 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1160 if (level
< 0 || level
>= maxLevels
) {
1161 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(level = %d)", caller
, level
);
1165 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1166 if (err
!= GL_NO_ERROR
) {
1167 _mesa_error(ctx
, err
, "%s(format/type)", caller
);
1171 if (dimensions_error_check(ctx
, texObj
, target
, level
,
1172 xoffset
, yoffset
, zoffset
,
1173 width
, height
, depth
, caller
)) {
1177 if (pbo_error_check(ctx
, target
, width
, height
, depth
,
1178 format
, type
, bufSize
, pixels
, caller
)) {
1182 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1186 * Format and type checking has been moved up to GetnTexImage and
1187 * GetTextureImage so that it happens before getting the texImage object.
1190 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
1192 /* Make sure the requested image format is compatible with the
1195 if (_mesa_is_color_format(format
)
1196 && !_mesa_is_color_format(baseFormat
)) {
1197 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1198 "%s(format mismatch)", caller
);
1201 else if (_mesa_is_depth_format(format
)
1202 && !_mesa_is_depth_format(baseFormat
)
1203 && !_mesa_is_depthstencil_format(baseFormat
)) {
1204 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1205 "%s(format mismatch)", caller
);
1208 else if (_mesa_is_stencil_format(format
)
1209 && !ctx
->Extensions
.ARB_texture_stencil8
) {
1210 _mesa_error(ctx
, GL_INVALID_ENUM
,
1211 "%s(format=GL_STENCIL_INDEX)", caller
);
1214 else if (_mesa_is_stencil_format(format
)
1215 && !_mesa_is_depthstencil_format(baseFormat
)
1216 && !_mesa_is_stencil_format(baseFormat
)) {
1217 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1218 "%s(format mismatch)", caller
);
1221 else if (_mesa_is_ycbcr_format(format
)
1222 && !_mesa_is_ycbcr_format(baseFormat
)) {
1223 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1224 "%s(format mismatch)", caller
);
1227 else if (_mesa_is_depthstencil_format(format
)
1228 && !_mesa_is_depthstencil_format(baseFormat
)) {
1229 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1230 "%s(format mismatch)", caller
);
1233 else if (!_mesa_is_stencil_format(format
) &&
1234 _mesa_is_enum_format_integer(format
) !=
1235 _mesa_is_format_integer(texImage
->TexFormat
)) {
1236 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1237 "%s(format mismatch)", caller
);
1246 * Return the width, height and depth of a texture image.
1247 * This function must be resilient to bad parameter values since
1248 * this is called before full error checking.
1251 get_texture_image_dims(const struct gl_texture_object
*texObj
,
1252 GLenum target
, GLint level
,
1253 GLsizei
*width
, GLsizei
*height
, GLsizei
*depth
)
1255 const struct gl_texture_image
*texImage
= NULL
;
1257 if (level
>= 0 && level
< MAX_TEXTURE_LEVELS
) {
1258 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1262 *width
= texImage
->Width
;
1263 *height
= texImage
->Height
;
1264 if (target
== GL_TEXTURE_CUBE_MAP
) {
1268 *depth
= texImage
->Depth
;
1272 *width
= *height
= *depth
= 0;
1278 * Common code for all (uncompressed) get-texture-image functions.
1279 * \param texObj the texture object (should not be null)
1280 * \param target user-provided target, or 0 for DSA
1281 * \param level image level.
1282 * \param format pixel data format for returned image.
1283 * \param type pixel data type for returned image.
1284 * \param bufSize size of the pixels data buffer.
1285 * \param pixels returned pixel data.
1286 * \param caller name of calling function
1289 get_texture_image(struct gl_context
*ctx
,
1290 struct gl_texture_object
*texObj
,
1291 GLenum target
, GLint level
,
1292 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1293 GLsizei width
, GLsizei height
, GLint depth
,
1294 GLenum format
, GLenum type
,
1295 GLvoid
*pixels
, const char *caller
)
1297 struct gl_texture_image
*texImage
;
1298 unsigned firstFace
, numFaces
, i
;
1301 FLUSH_VERTICES(ctx
, 0);
1303 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1304 assert(texImage
); /* should have been error checked already */
1306 if (_mesa_is_zero_size_texture(texImage
)) {
1307 /* no image data to return */
1311 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1312 _mesa_debug(ctx
, "%s(tex %u) format = %s, w=%d, h=%d,"
1313 " dstFmt=0x%x, dstType=0x%x\n",
1314 caller
, texObj
->Name
,
1315 _mesa_get_format_name(texImage
->TexFormat
),
1316 texImage
->Width
, texImage
->Height
,
1320 if (target
== GL_TEXTURE_CUBE_MAP
) {
1321 /* Compute stride between cube faces */
1322 imageStride
= _mesa_image_image_stride(&ctx
->Pack
, width
, height
,
1324 firstFace
= zoffset
;
1331 firstFace
= _mesa_tex_target_to_face(target
);
1335 _mesa_lock_texture(ctx
, texObj
);
1337 for (i
= 0; i
< numFaces
; i
++) {
1338 texImage
= texObj
->Image
[firstFace
+ i
][level
];
1341 ctx
->Driver
.GetTexSubImage(ctx
, xoffset
, yoffset
, zoffset
,
1342 width
, height
, depth
,
1343 format
, type
, pixels
, texImage
);
1345 /* next cube face */
1346 pixels
= (GLubyte
*) pixels
+ imageStride
;
1349 _mesa_unlock_texture(ctx
, texObj
);
1354 _mesa_GetnTexImageARB(GLenum target
, GLint level
, GLenum format
, GLenum type
,
1355 GLsizei bufSize
, GLvoid
*pixels
)
1357 GET_CURRENT_CONTEXT(ctx
);
1358 static const char *caller
= "glGetnTexImageARB";
1359 GLsizei width
, height
, depth
;
1360 struct gl_texture_object
*texObj
;
1362 if (!legal_getteximage_target(ctx
, target
, false)) {
1363 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1367 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1370 get_texture_image_dims(texObj
, target
, level
, &width
, &height
, &depth
);
1372 if (getteximage_error_check(ctx
, texObj
, target
, level
,
1373 0, 0, 0, width
, height
, depth
,
1374 format
, type
, bufSize
, pixels
, caller
)) {
1378 get_texture_image(ctx
, texObj
, target
, level
,
1379 0, 0, 0, width
, height
, depth
,
1380 format
, type
, pixels
, caller
);
1385 _mesa_GetTexImage(GLenum target
, GLint level
, GLenum format
, GLenum type
,
1388 GET_CURRENT_CONTEXT(ctx
);
1389 static const char *caller
= "glGetTexImage";
1390 GLsizei width
, height
, depth
;
1391 struct gl_texture_object
*texObj
;
1393 if (!legal_getteximage_target(ctx
, target
, false)) {
1394 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1398 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1401 get_texture_image_dims(texObj
, target
, level
, &width
, &height
, &depth
);
1403 if (getteximage_error_check(ctx
, texObj
, target
, level
,
1404 0, 0, 0, width
, height
, depth
,
1405 format
, type
, INT_MAX
, pixels
, caller
)) {
1409 get_texture_image(ctx
, texObj
, target
, level
,
1410 0, 0, 0, width
, height
, depth
,
1411 format
, type
, pixels
, caller
);
1416 _mesa_GetTextureImage(GLuint texture
, GLint level
, GLenum format
, GLenum type
,
1417 GLsizei bufSize
, GLvoid
*pixels
)
1419 GET_CURRENT_CONTEXT(ctx
);
1420 GLsizei width
, height
, depth
;
1421 static const char *caller
= "glGetTextureImage";
1422 struct gl_texture_object
*texObj
=
1423 _mesa_lookup_texture_err(ctx
, texture
, caller
);
1429 get_texture_image_dims(texObj
, texObj
->Target
, level
,
1430 &width
, &height
, &depth
);
1432 if (getteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1433 0, 0, 0, width
, height
, depth
,
1434 format
, type
, bufSize
, pixels
, caller
)) {
1438 get_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1439 0, 0, 0, width
, height
, depth
,
1440 format
, type
, pixels
, caller
);
1445 _mesa_GetTextureSubImage(GLuint texture
, GLint level
,
1446 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1447 GLsizei width
, GLsizei height
, GLsizei depth
,
1448 GLenum format
, GLenum type
, GLsizei bufSize
,
1451 GET_CURRENT_CONTEXT(ctx
);
1452 static const char *caller
= "glGetTextureSubImage";
1453 struct gl_texture_object
*texObj
=
1454 _mesa_lookup_texture_err(ctx
, texture
, caller
);
1460 if (getteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1461 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
1462 format
, type
, bufSize
, pixels
, caller
)) {
1466 get_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1467 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
1468 format
, type
, pixels
, caller
);
1474 * Compute the number of bytes which will be written when retrieving
1475 * a sub-region of a compressed texture.
1478 packed_compressed_size(GLuint dimensions
, mesa_format format
,
1479 GLsizei width
, GLsizei height
, GLsizei depth
,
1480 const struct gl_pixelstore_attrib
*packing
)
1482 struct compressed_pixelstore st
;
1485 _mesa_compute_compressed_pixelstore(dimensions
, format
,
1486 width
, height
, depth
,
1489 (st
.CopySlices
- 1) * st
.TotalRowsPerSlice
* st
.TotalBytesPerRow
+
1491 (st
.CopyRowsPerSlice
- 1) * st
.TotalBytesPerRow
+
1499 * Do error checking for getting compressed texture images.
1500 * \return true if any error, false if no errors.
1503 getcompressedteximage_error_check(struct gl_context
*ctx
,
1504 struct gl_texture_object
*texObj
,
1505 GLenum target
, GLint level
,
1506 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1507 GLsizei width
, GLsizei height
, GLsizei depth
,
1508 GLsizei bufSize
, GLvoid
*pixels
,
1511 struct gl_texture_image
*texImage
;
1518 if (texObj
->Target
== 0) {
1519 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(invalid texture)", caller
);
1523 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
1524 if (level
< 0 || level
>= maxLevels
) {
1525 _mesa_error(ctx
, GL_INVALID_VALUE
,
1526 "%s(bad level = %d)", caller
, level
);
1530 if (dimensions_error_check(ctx
, texObj
, target
, level
,
1531 xoffset
, yoffset
, zoffset
,
1532 width
, height
, depth
, caller
)) {
1536 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1539 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1540 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1541 "%s(texture is not compressed)", caller
);
1545 /* Check for invalid pixel storage modes */
1546 dimensions
= _mesa_get_texture_dimensions(texObj
->Target
);
1547 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1553 /* Compute number of bytes that may be touched in the dest buffer */
1554 totalBytes
= packed_compressed_size(dimensions
, texImage
->TexFormat
,
1555 width
, height
, depth
,
1558 /* Do dest buffer bounds checking */
1559 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1560 /* do bounds checking on PBO write */
1561 if ((GLubyte
*) pixels
+ totalBytes
>
1562 (GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1563 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1564 "%s(out of bounds PBO access)", caller
);
1568 /* make sure PBO is not mapped */
1569 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1570 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(PBO is mapped)", caller
);
1575 /* do bounds checking on writing to client memory */
1576 if (totalBytes
> bufSize
) {
1577 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1578 "%s(out of bounds access: bufSize (%d) is too small)",
1584 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
1585 /* not an error, but do nothing */
1594 * Common helper for all glGetCompressed-teximage functions.
1597 get_compressed_texture_image(struct gl_context
*ctx
,
1598 struct gl_texture_object
*texObj
,
1599 GLenum target
, GLint level
,
1600 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1601 GLsizei width
, GLsizei height
, GLint depth
,
1605 struct gl_texture_image
*texImage
;
1606 unsigned firstFace
, numFaces
, i
, imageStride
;
1608 FLUSH_VERTICES(ctx
, 0);
1610 texImage
= select_tex_image(texObj
, target
, level
, zoffset
);
1611 assert(texImage
); /* should have been error checked already */
1613 if (_mesa_is_zero_size_texture(texImage
))
1616 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1618 "%s(tex %u) format = %s, w=%d, h=%d\n",
1619 caller
, texObj
->Name
,
1620 _mesa_get_format_name(texImage
->TexFormat
),
1621 texImage
->Width
, texImage
->Height
);
1624 if (target
== GL_TEXTURE_CUBE_MAP
) {
1625 struct compressed_pixelstore store
;
1627 /* Compute image stride between cube faces */
1628 _mesa_compute_compressed_pixelstore(2, texImage
->TexFormat
,
1629 width
, height
, depth
,
1630 &ctx
->Pack
, &store
);
1631 imageStride
= store
.TotalBytesPerRow
* store
.TotalRowsPerSlice
;
1633 firstFace
= zoffset
;
1640 firstFace
= _mesa_tex_target_to_face(target
);
1644 _mesa_lock_texture(ctx
, texObj
);
1646 for (i
= 0; i
< numFaces
; i
++) {
1647 texImage
= texObj
->Image
[firstFace
+ i
][level
];
1650 ctx
->Driver
.GetCompressedTexSubImage(ctx
, texImage
,
1651 xoffset
, yoffset
, zoffset
,
1652 width
, height
, depth
, pixels
);
1654 /* next cube face */
1655 pixels
= (GLubyte
*) pixels
+ imageStride
;
1658 _mesa_unlock_texture(ctx
, texObj
);
1663 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1666 GET_CURRENT_CONTEXT(ctx
);
1667 static const char *caller
= "glGetnCompressedTexImageARB";
1668 GLsizei width
, height
, depth
;
1669 struct gl_texture_object
*texObj
;
1671 if (!legal_getteximage_target(ctx
, target
, false)) {
1672 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1676 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1679 get_texture_image_dims(texObj
, target
, level
, &width
, &height
, &depth
);
1681 if (getcompressedteximage_error_check(ctx
, texObj
, target
, level
,
1682 0, 0, 0, width
, height
, depth
,
1683 INT_MAX
, pixels
, caller
)) {
1687 get_compressed_texture_image(ctx
, texObj
, target
, level
,
1688 0, 0, 0, width
, height
, depth
,
1694 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*pixels
)
1696 GET_CURRENT_CONTEXT(ctx
);
1697 static const char *caller
= "glGetCompressedTexImage";
1698 GLsizei width
, height
, depth
;
1699 struct gl_texture_object
*texObj
;
1701 if (!legal_getteximage_target(ctx
, target
, false)) {
1702 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
1706 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1709 get_texture_image_dims(texObj
, target
, level
,
1710 &width
, &height
, &depth
);
1712 if (getcompressedteximage_error_check(ctx
, texObj
, target
, level
,
1713 0, 0, 0, width
, height
, depth
,
1714 INT_MAX
, pixels
, caller
)) {
1718 get_compressed_texture_image(ctx
, texObj
, target
, level
,
1719 0, 0, 0, width
, height
, depth
,
1725 _mesa_GetCompressedTextureImage(GLuint texture
, GLint level
,
1726 GLsizei bufSize
, GLvoid
*pixels
)
1728 GET_CURRENT_CONTEXT(ctx
);
1729 static const char *caller
= "glGetCompressedTextureImage";
1730 GLsizei width
, height
, depth
;
1731 struct gl_texture_object
*texObj
=
1732 _mesa_lookup_texture_err(ctx
, texture
, caller
);
1738 get_texture_image_dims(texObj
, texObj
->Target
, level
,
1739 &width
, &height
, &depth
);
1741 if (getcompressedteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1742 0, 0, 0, width
, height
, depth
,
1743 bufSize
, pixels
, caller
)) {
1747 get_compressed_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1748 0, 0, 0, width
, height
, depth
,
1754 _mesa_GetCompressedTextureSubImage(GLuint texture
, GLint level
,
1755 GLint xoffset
, GLint yoffset
,
1756 GLint zoffset
, GLsizei width
,
1757 GLsizei height
, GLsizei depth
,
1758 GLsizei bufSize
, void *pixels
)
1760 GET_CURRENT_CONTEXT(ctx
);
1761 static const char *caller
= "glGetCompressedTextureImage";
1762 struct gl_texture_object
*texObj
;
1764 texObj
= _mesa_lookup_texture_err(ctx
, texture
, caller
);
1769 if (getcompressedteximage_error_check(ctx
, texObj
, texObj
->Target
, level
,
1770 xoffset
, yoffset
, zoffset
,
1771 width
, height
, depth
,
1772 bufSize
, pixels
, caller
)) {
1776 get_compressed_texture_image(ctx
, texObj
, texObj
->Target
, level
,
1777 xoffset
, yoffset
, zoffset
,
1778 width
, height
, depth
,