2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2009 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * Code for glGetTexImage() and glGetCompressedTexImage().
33 #include "bufferobj.h"
37 #include "format_unpack.h"
38 #include "glformats.h"
43 #include "pixelstore.h"
44 #include "texcompress.h"
45 #include "texgetimage.h"
52 * Can the given type represent negative values?
54 static inline GLboolean
55 type_needs_clamping(GLenum type
)
62 case GL_HALF_FLOAT_ARB
:
63 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
64 case GL_UNSIGNED_INT_5_9_9_9_REV
:
73 * glGetTexImage for depth/Z pixels.
76 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
77 GLenum format
, GLenum type
, GLvoid
*pixels
,
78 struct gl_texture_image
*texImage
)
80 const GLint width
= texImage
->Width
;
81 const GLint height
= texImage
->Height
;
82 const GLint depth
= texImage
->Depth
;
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
, img
,
97 0, 0, width
, height
, GL_MAP_READ_BIT
,
98 &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
, 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 GLenum format
, GLenum type
, GLvoid
*pixels
,
128 struct gl_texture_image
*texImage
)
130 const GLint width
= texImage
->Width
;
131 const GLint height
= texImage
->Height
;
132 const GLint depth
= texImage
->Depth
;
135 assert(format
== GL_DEPTH_STENCIL
);
136 assert(type
== GL_UNSIGNED_INT_24_8
||
137 type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
139 for (img
= 0; img
< depth
; img
++) {
143 /* map src texture buffer */
144 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
145 0, 0, width
, height
, GL_MAP_READ_BIT
,
146 &srcMap
, &rowstride
);
149 for (row
= 0; row
< height
; row
++) {
150 const GLubyte
*src
= srcMap
+ row
* rowstride
;
151 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
152 width
, height
, format
, type
,
154 _mesa_unpack_depth_stencil_row(texImage
->TexFormat
,
156 (const GLuint
*) src
,
158 if (ctx
->Pack
.SwapBytes
) {
159 _mesa_swap4((GLuint
*) dest
, width
);
163 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
166 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
174 * glGetTexImage for YCbCr pixels.
177 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
178 GLenum format
, GLenum type
, GLvoid
*pixels
,
179 struct gl_texture_image
*texImage
)
181 const GLint width
= texImage
->Width
;
182 const GLint height
= texImage
->Height
;
183 const GLint depth
= texImage
->Depth
;
186 for (img
= 0; img
< depth
; img
++) {
190 /* map src texture buffer */
191 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
192 0, 0, width
, height
, GL_MAP_READ_BIT
,
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 memcpy(dest
, src
, width
* sizeof(GLushort
));
203 /* check for byte swapping */
204 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
205 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
206 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
207 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
208 if (!ctx
->Pack
.SwapBytes
)
209 _mesa_swap2((GLushort
*) dest
, width
);
211 else if (ctx
->Pack
.SwapBytes
) {
212 _mesa_swap2((GLushort
*) dest
, width
);
216 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
219 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
227 * Get a color texture image with decompression.
230 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
231 GLenum format
, GLenum type
, GLvoid
*pixels
,
232 struct gl_texture_image
*texImage
,
233 GLbitfield transferOps
)
235 /* don't want to apply sRGB -> RGB conversion here so override the format */
236 const mesa_format texFormat
=
237 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
238 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
239 const GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, format
);
240 GLenum rebaseFormat
= GL_NONE
;
241 const GLuint width
= texImage
->Width
;
242 const GLuint height
= texImage
->Height
;
243 const GLuint depth
= texImage
->Depth
;
244 GLfloat
*tempImage
, *tempSlice
, *srcRow
;
247 /* Decompress into temp float buffer, then pack into user buffer */
248 tempImage
= malloc(width
* height
* depth
249 * 4 * sizeof(GLfloat
));
251 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
255 /* Decompress the texture image slices - results in 'tempImage' */
256 for (slice
= 0; slice
< depth
; slice
++) {
260 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
262 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
265 &srcMap
, &srcRowStride
);
267 _mesa_decompress_image(texFormat
, width
, height
,
268 srcMap
, srcRowStride
, tempSlice
);
270 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
273 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
279 if (baseFormat
== GL_LUMINANCE
||
280 baseFormat
== GL_INTENSITY
||
281 baseFormat
== GL_LUMINANCE_ALPHA
) {
282 /* If a luminance (or intensity) texture is read back as RGB(A), the
283 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
286 rebaseFormat
= texImage
->_BaseFormat
;
288 else if ((baseFormat
== GL_RGBA
||
289 baseFormat
== GL_RGB
||
290 baseFormat
== GL_RG
) &&
291 (destBaseFormat
== GL_LUMINANCE
||
292 destBaseFormat
== GL_LUMINANCE_ALPHA
||
293 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
294 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
295 /* If we're reading back an RGB(A) texture as luminance then we need
296 * to return L=tex(R). Note, that's different from glReadPixels which
299 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
303 _mesa_rebase_rgba_float(width
* height
, (GLfloat (*)[4]) tempImage
,
307 tempSlice
= tempImage
;
308 for (slice
= 0; slice
< depth
; slice
++) {
310 for (row
= 0; row
< height
; row
++) {
311 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
312 width
, height
, format
, type
,
315 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) srcRow
,
316 format
, type
, dest
, &ctx
->Pack
, transferOps
);
319 tempSlice
+= 4 * width
* height
;
327 * Return a base GL format given the user-requested format
328 * for glGetTexImage().
331 _mesa_base_pack_format(GLenum format
)
336 case GL_BGRA_INTEGER
:
337 case GL_RGBA_INTEGER
:
345 case GL_GREEN_INTEGER
:
347 case GL_BLUE_INTEGER
:
349 case GL_ALPHA_INTEGER
:
351 case GL_LUMINANCE_INTEGER_EXT
:
353 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
354 return GL_LUMINANCE_ALPHA
;
362 * Get an uncompressed color texture image.
365 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
366 GLenum format
, GLenum type
, GLvoid
*pixels
,
367 struct gl_texture_image
*texImage
,
368 GLbitfield transferOps
)
370 /* don't want to apply sRGB -> RGB conversion here so override the format */
371 const mesa_format texFormat
=
372 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
373 const GLuint width
= texImage
->Width
;
374 GLenum destBaseFormat
= _mesa_base_pack_format(format
);
375 GLenum rebaseFormat
= GL_NONE
;
376 GLuint height
= texImage
->Height
;
377 GLuint depth
= texImage
->Depth
;
380 GLuint (*rgba_uint
)[4];
381 GLboolean tex_is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
382 GLboolean tex_is_uint
= _mesa_is_format_unsigned(texImage
->TexFormat
);
383 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
385 /* Allocate buffer for one row of texels */
386 rgba
= malloc(4 * width
* sizeof(GLfloat
));
387 rgba_uint
= (GLuint (*)[4]) rgba
;
389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
393 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
398 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
399 texImage
->_BaseFormat
== GL_INTENSITY
||
400 texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
401 /* If a luminance (or intensity) texture is read back as RGB(A), the
402 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
405 rebaseFormat
= texImage
->_BaseFormat
;
407 else if ((texImage
->_BaseFormat
== GL_RGBA
||
408 texImage
->_BaseFormat
== GL_RGB
||
409 texImage
->_BaseFormat
== GL_RG
) &&
410 (destBaseFormat
== GL_LUMINANCE
||
411 destBaseFormat
== GL_LUMINANCE_ALPHA
||
412 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
413 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
414 /* If we're reading back an RGB(A) texture as luminance then we need
415 * to return L=tex(R). Note, that's different from glReadPixels which
418 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
420 else if (texImage
->_BaseFormat
!= texBaseFormat
) {
421 /* The internal format and the real format differ, so we can't rely
422 * on the unpack functions setting the correct constant values.
423 * (e.g. reading back GL_RGB8 which is actually RGBA won't set alpha=1)
425 switch (texImage
->_BaseFormat
) {
427 if ((texBaseFormat
== GL_RGBA
||
428 texBaseFormat
== GL_RGB
||
429 texBaseFormat
== GL_RG
) &&
430 (destBaseFormat
== GL_RGBA
||
431 destBaseFormat
== GL_RGB
||
432 destBaseFormat
== GL_RG
||
433 destBaseFormat
== GL_GREEN
)) {
434 rebaseFormat
= texImage
->_BaseFormat
;
439 if ((texBaseFormat
== GL_RGBA
||
440 texBaseFormat
== GL_RGB
) &&
441 (destBaseFormat
== GL_RGBA
||
442 destBaseFormat
== GL_RGB
||
443 destBaseFormat
== GL_BLUE
)) {
444 rebaseFormat
= texImage
->_BaseFormat
;
449 if (texBaseFormat
== GL_RGBA
&&
450 (destBaseFormat
== GL_RGBA
||
451 destBaseFormat
== GL_ALPHA
||
452 destBaseFormat
== GL_LUMINANCE_ALPHA
)) {
453 rebaseFormat
= texImage
->_BaseFormat
;
458 if (destBaseFormat
!= GL_ALPHA
) {
459 rebaseFormat
= texImage
->_BaseFormat
;
465 for (img
= 0; img
< depth
; img
++) {
469 /* map src texture buffer */
470 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
471 0, 0, width
, height
, GL_MAP_READ_BIT
,
472 &srcMap
, &rowstride
);
474 for (row
= 0; row
< height
; row
++) {
475 const GLubyte
*src
= srcMap
+ row
* rowstride
;
476 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
477 width
, height
, format
, type
,
480 if (tex_is_integer
) {
481 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
483 _mesa_rebase_rgba_uint(width
, rgba_uint
, rebaseFormat
);
485 _mesa_pack_rgba_span_from_uints(ctx
, width
,
486 (GLuint (*)[4]) rgba_uint
,
489 _mesa_pack_rgba_span_from_ints(ctx
, width
,
490 (GLint (*)[4]) rgba_uint
,
494 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
496 _mesa_rebase_rgba_float(width
, rgba
, rebaseFormat
);
497 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
499 &ctx
->Pack
, transferOps
);
503 /* Unmap the src texture buffer */
504 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
507 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
517 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
518 * Compressed textures are handled here as well.
521 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
522 GLenum format
, GLenum type
, GLvoid
*pixels
,
523 struct gl_texture_image
*texImage
)
525 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
526 GLbitfield transferOps
= 0x0;
528 /* In general, clamping does not apply to glGetTexImage, except when
529 * the returned type of the image can't hold negative values.
531 if (type_needs_clamping(type
)) {
532 /* the returned image type can't have negative values */
533 if (dataType
== GL_FLOAT
||
534 dataType
== GL_HALF_FLOAT
||
535 dataType
== GL_SIGNED_NORMALIZED
||
536 format
== GL_LUMINANCE
||
537 format
== GL_LUMINANCE_ALPHA
) {
538 transferOps
|= IMAGE_CLAMP_BIT
;
542 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
543 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
544 pixels
, texImage
, transferOps
);
547 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
548 pixels
, texImage
, transferOps
);
554 * Try to do glGetTexImage() with simple memcpy().
555 * \return GL_TRUE if done, GL_FALSE otherwise
558 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
560 struct gl_texture_image
*texImage
)
562 const GLenum target
= texImage
->TexObject
->Target
;
563 GLboolean memCopy
= GL_FALSE
;
564 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
567 * Check if we can use memcpy to copy from the hardware texture
568 * format to the user's format/type.
569 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
571 if ((target
== GL_TEXTURE_1D
||
572 target
== GL_TEXTURE_2D
||
573 target
== GL_TEXTURE_RECTANGLE
||
574 _mesa_is_cube_face(target
)) &&
575 texBaseFormat
== texImage
->_BaseFormat
) {
576 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
578 ctx
->Pack
.SwapBytes
);
582 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
583 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
585 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
586 texImage
->Height
, format
, type
, 0, 0);
587 const GLint dstRowStride
=
588 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
592 /* map src texture buffer */
593 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
594 0, 0, texImage
->Width
, texImage
->Height
,
595 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
598 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
599 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
603 for (row
= 0; row
< texImage
->Height
; row
++) {
604 memcpy(dst
, src
, bytesPerRow
);
610 /* unmap src texture buffer */
611 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
614 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
623 * This is the software fallback for Driver.GetTexImage().
624 * All error checking will have been done before this routine is called.
625 * We'll call ctx->Driver.MapTextureImage() to access the data, then
626 * unmap with ctx->Driver.UnmapTextureImage().
629 _mesa_get_teximage(struct gl_context
*ctx
,
630 GLenum format
, GLenum type
, GLvoid
*pixels
,
631 struct gl_texture_image
*texImage
)
633 const GLuint dimensions
=
634 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
636 /* map dest buffer, if PBO */
637 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
638 /* Packing texture image into a PBO.
639 * Map the (potentially) VRAM-based buffer into our process space so
640 * we can write into it with the code below.
641 * A hardware driver might use a sophisticated blit to move the
642 * texture data to the PBO if the PBO is in VRAM along with the texture.
644 GLubyte
*buf
= (GLubyte
*)
645 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
646 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
649 /* out of memory or other unexpected error */
650 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
653 /* <pixels> was an offset into the PBO.
654 * Now make it a real, client-side pointer inside the mapped region.
656 pixels
= ADD_POINTERS(buf
, pixels
);
659 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
662 else if (format
== GL_DEPTH_COMPONENT
) {
663 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
665 else if (format
== GL_DEPTH_STENCIL_EXT
) {
666 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
668 else if (format
== GL_YCBCR_MESA
) {
669 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
672 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
675 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
676 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
683 * This is the software fallback for Driver.GetCompressedTexImage().
684 * All error checking will have been done before this routine is called.
687 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
688 struct gl_texture_image
*texImage
,
691 const GLuint dimensions
=
692 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
693 struct compressed_pixelstore store
;
697 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
698 texImage
->Width
, texImage
->Height
,
703 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
704 /* pack texture image into a PBO */
706 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
707 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
710 /* out of memory or other unexpected error */
711 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
712 "glGetCompresssedTexImage(map PBO failed)");
715 dest
= ADD_POINTERS(dest
, img
);
720 dest
+= store
.SkipBytes
;
722 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
726 /* map src texture buffer */
727 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
728 0, 0, texImage
->Width
, texImage
->Height
,
729 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
733 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
734 memcpy(dest
, src
, store
.CopyBytesPerRow
);
735 dest
+= store
.TotalBytesPerRow
;
739 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
741 /* Advance to next slice */
742 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
745 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
749 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
750 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
756 * Validate the texture target enum supplied to glTexImage or
757 * glCompressedTexImage.
760 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
)
767 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
768 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
769 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
770 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
771 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
772 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
773 return ctx
->Extensions
.ARB_texture_cube_map
;
774 case GL_TEXTURE_RECTANGLE_NV
:
775 return ctx
->Extensions
.NV_texture_rectangle
;
776 case GL_TEXTURE_1D_ARRAY_EXT
:
777 case GL_TEXTURE_2D_ARRAY_EXT
:
778 return ctx
->Extensions
.EXT_texture_array
;
779 case GL_TEXTURE_CUBE_MAP_ARRAY
:
780 return ctx
->Extensions
.ARB_texture_cube_map_array
;
788 * Do error checking for a glGetTexImage() call.
789 * \return GL_TRUE if any error, GL_FALSE if no errors.
792 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
793 GLenum format
, GLenum type
, GLsizei clientMemSize
,
796 struct gl_texture_object
*texObj
;
797 struct gl_texture_image
*texImage
;
798 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
799 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
800 GLenum baseFormat
, err
;
802 if (!legal_getteximage_target(ctx
, target
)) {
803 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
807 assert(maxLevels
!= 0);
808 if (level
< 0 || level
>= maxLevels
) {
809 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
813 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
814 if (err
!= GL_NO_ERROR
) {
815 _mesa_error(ctx
, err
, "glGetTexImage(format/type)");
819 texObj
= _mesa_get_current_tex_object(ctx
, target
);
822 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
826 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
828 /* non-existant texture image */
832 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
834 /* Make sure the requested image format is compatible with the
837 if (_mesa_is_color_format(format
)
838 && !_mesa_is_color_format(baseFormat
)) {
839 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
842 else if (_mesa_is_depth_format(format
)
843 && !_mesa_is_depth_format(baseFormat
)
844 && !_mesa_is_depthstencil_format(baseFormat
)) {
845 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
848 else if (_mesa_is_stencil_format(format
)
849 && !ctx
->Extensions
.ARB_texture_stencil8
) {
850 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format=GL_STENCIL_INDEX)");
853 else if (_mesa_is_ycbcr_format(format
)
854 && !_mesa_is_ycbcr_format(baseFormat
)) {
855 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
858 else if (_mesa_is_depthstencil_format(format
)
859 && !_mesa_is_depthstencil_format(baseFormat
)) {
860 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
863 else if (_mesa_is_enum_format_integer(format
) !=
864 _mesa_is_format_integer(texImage
->TexFormat
)) {
865 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
869 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
870 texImage
->Height
, texImage
->Depth
,
871 format
, type
, clientMemSize
, pixels
)) {
872 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
873 _mesa_error(ctx
, GL_INVALID_OPERATION
,
874 "glGetTexImage(out of bounds PBO access)");
876 _mesa_error(ctx
, GL_INVALID_OPERATION
,
877 "glGetnTexImageARB(out of bounds access:"
878 " bufSize (%d) is too small)", clientMemSize
);
883 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
884 /* PBO should not be mapped */
885 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
886 _mesa_error(ctx
, GL_INVALID_OPERATION
,
887 "glGetTexImage(PBO is mapped)");
898 * Get texture image. Called by glGetTexImage.
900 * \param target texture target.
901 * \param level image level.
902 * \param format pixel data format for returned image.
903 * \param type pixel data type for returned image.
904 * \param bufSize size of the pixels data buffer.
905 * \param pixels returned pixel data.
908 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
909 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
911 struct gl_texture_object
*texObj
;
912 struct gl_texture_image
*texImage
;
913 GET_CURRENT_CONTEXT(ctx
);
915 FLUSH_VERTICES(ctx
, 0);
917 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
922 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
923 /* not an error, do nothing */
927 texObj
= _mesa_get_current_tex_object(ctx
, target
);
928 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
930 if (_mesa_is_zero_size_texture(texImage
))
933 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
934 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
935 " dstFmt=0x%x, dstType=0x%x\n",
937 _mesa_get_format_name(texImage
->TexFormat
),
938 texImage
->Width
, texImage
->Height
,
942 _mesa_lock_texture(ctx
, texObj
);
944 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
946 _mesa_unlock_texture(ctx
, texObj
);
951 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
952 GLenum type
, GLvoid
*pixels
)
954 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
959 * Do error checking for a glGetCompressedTexImage() call.
960 * \return GL_TRUE if any error, GL_FALSE if no errors.
963 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
964 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
966 struct gl_texture_object
*texObj
;
967 struct gl_texture_image
*texImage
;
968 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
969 GLuint compressedSize
, dimensions
;
971 if (!legal_getteximage_target(ctx
, target
)) {
972 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
977 assert(maxLevels
!= 0);
978 if (level
< 0 || level
>= maxLevels
) {
979 _mesa_error(ctx
, GL_INVALID_VALUE
,
980 "glGetCompressedTexImageARB(bad level = %d)", level
);
984 texObj
= _mesa_get_current_tex_object(ctx
, target
);
986 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
990 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
993 /* probably invalid mipmap level */
994 _mesa_error(ctx
, GL_INVALID_VALUE
,
995 "glGetCompressedTexImageARB(level)");
999 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1000 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1001 "glGetCompressedTexImageARB(texture is not compressed)");
1005 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
1010 /* Check for invalid pixel storage modes */
1011 dimensions
= _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
1012 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1014 "glGetCompressedTexImageARB")) {
1018 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1019 /* do bounds checking on writing to client memory */
1020 if (clientMemSize
< (GLsizei
) compressedSize
) {
1021 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1022 "glGetnCompressedTexImageARB(out of bounds access:"
1023 " bufSize (%d) is too small)", clientMemSize
);
1027 /* do bounds checking on PBO write */
1028 if ((const GLubyte
*) img
+ compressedSize
>
1029 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1030 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1031 "glGetCompressedTexImage(out of bounds PBO access)");
1035 /* make sure PBO is not mapped */
1036 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1037 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1038 "glGetCompressedTexImage(PBO is mapped)");
1048 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1051 struct gl_texture_object
*texObj
;
1052 struct gl_texture_image
*texImage
;
1053 GET_CURRENT_CONTEXT(ctx
);
1055 FLUSH_VERTICES(ctx
, 0);
1057 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
1061 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
1062 /* not an error, do nothing */
1066 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1067 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1069 if (_mesa_is_zero_size_texture(texImage
))
1072 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1074 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1076 _mesa_get_format_name(texImage
->TexFormat
),
1077 texImage
->Width
, texImage
->Height
);
1080 _mesa_lock_texture(ctx
, texObj
);
1082 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
1084 _mesa_unlock_texture(ctx
, texObj
);
1088 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1090 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);