2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2009 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * Code for glGetTexImage() and glGetCompressedTexImage().
33 #include "bufferobj.h"
37 #include "format_unpack.h"
38 #include "glformats.h"
43 #include "pixelstore.h"
44 #include "texcompress.h"
45 #include "texgetimage.h"
52 * Can the given type represent negative values?
54 static inline GLboolean
55 type_needs_clamping(GLenum type
)
62 case GL_HALF_FLOAT_ARB
:
63 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
64 case GL_UNSIGNED_INT_5_9_9_9_REV
:
73 * glGetTexImage for depth/Z pixels.
76 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
77 GLenum format
, GLenum type
, GLvoid
*pixels
,
78 struct gl_texture_image
*texImage
)
80 const GLint width
= texImage
->Width
;
81 GLint height
= texImage
->Height
;
82 GLint depth
= texImage
->Depth
;
84 GLfloat
*depthRow
= malloc(width
* sizeof(GLfloat
));
87 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
91 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
96 for (img
= 0; img
< depth
; img
++) {
100 /* map src texture buffer */
101 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
102 0, 0, width
, height
, GL_MAP_READ_BIT
,
103 &srcMap
, &srcRowStride
);
106 for (row
= 0; row
< height
; row
++) {
107 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
108 width
, height
, format
, type
,
110 const GLubyte
*src
= srcMap
+ row
* srcRowStride
;
111 _mesa_unpack_float_z_row(texImage
->TexFormat
, width
, src
, depthRow
);
112 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
115 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
118 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
128 * glGetTexImage for depth/stencil pixels.
131 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
132 GLenum format
, GLenum type
, GLvoid
*pixels
,
133 struct gl_texture_image
*texImage
)
135 const GLint width
= texImage
->Width
;
136 const GLint height
= texImage
->Height
;
137 const GLint depth
= texImage
->Depth
;
140 assert(format
== GL_DEPTH_STENCIL
);
141 assert(type
== GL_UNSIGNED_INT_24_8
||
142 type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
144 for (img
= 0; img
< depth
; img
++) {
148 /* map src texture buffer */
149 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
150 0, 0, width
, height
, GL_MAP_READ_BIT
,
151 &srcMap
, &rowstride
);
154 for (row
= 0; row
< height
; row
++) {
155 const GLubyte
*src
= srcMap
+ row
* rowstride
;
156 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
157 width
, height
, format
, type
,
159 _mesa_unpack_depth_stencil_row(texImage
->TexFormat
,
161 (const GLuint
*) src
,
163 if (ctx
->Pack
.SwapBytes
) {
164 _mesa_swap4((GLuint
*) dest
, width
);
168 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
171 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
179 * glGetTexImage for YCbCr pixels.
182 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
183 GLenum format
, GLenum type
, GLvoid
*pixels
,
184 struct gl_texture_image
*texImage
)
186 const GLint width
= texImage
->Width
;
187 const GLint height
= texImage
->Height
;
188 const GLint depth
= texImage
->Depth
;
191 for (img
= 0; img
< depth
; img
++) {
195 /* map src texture buffer */
196 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
197 0, 0, width
, height
, GL_MAP_READ_BIT
,
198 &srcMap
, &rowstride
);
201 for (row
= 0; row
< height
; row
++) {
202 const GLubyte
*src
= srcMap
+ row
* rowstride
;
203 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
204 width
, height
, format
, type
,
206 memcpy(dest
, src
, width
* sizeof(GLushort
));
208 /* check for byte swapping */
209 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
210 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
211 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
212 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
213 if (!ctx
->Pack
.SwapBytes
)
214 _mesa_swap2((GLushort
*) dest
, width
);
216 else if (ctx
->Pack
.SwapBytes
) {
217 _mesa_swap2((GLushort
*) dest
, width
);
221 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
224 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
232 * Get a color texture image with decompression.
235 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
236 GLenum format
, GLenum type
, GLvoid
*pixels
,
237 struct gl_texture_image
*texImage
,
238 GLbitfield transferOps
)
240 /* don't want to apply sRGB -> RGB conversion here so override the format */
241 const mesa_format texFormat
=
242 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
243 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
244 const GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, format
);
245 GLenum rebaseFormat
= GL_NONE
;
246 const GLuint width
= texImage
->Width
;
247 const GLuint height
= texImage
->Height
;
248 const GLuint depth
= texImage
->Depth
;
249 GLfloat
*tempImage
, *tempSlice
, *srcRow
;
252 /* Decompress into temp float buffer, then pack into user buffer */
253 tempImage
= malloc(width
* height
* depth
254 * 4 * sizeof(GLfloat
));
256 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
260 /* Decompress the texture image slices - results in 'tempImage' */
261 for (slice
= 0; slice
< depth
; slice
++) {
265 tempSlice
= tempImage
+ slice
* 4 * width
* height
;
267 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
,
270 &srcMap
, &srcRowStride
);
272 _mesa_decompress_image(texFormat
, width
, height
,
273 srcMap
, srcRowStride
, tempSlice
);
275 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
);
278 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
284 if (baseFormat
== GL_LUMINANCE
||
285 baseFormat
== GL_INTENSITY
||
286 baseFormat
== GL_LUMINANCE_ALPHA
) {
287 /* If a luminance (or intensity) texture is read back as RGB(A), the
288 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
291 rebaseFormat
= texImage
->_BaseFormat
;
293 else if ((baseFormat
== GL_RGBA
||
294 baseFormat
== GL_RGB
||
295 baseFormat
== GL_RG
) &&
296 (destBaseFormat
== GL_LUMINANCE
||
297 destBaseFormat
== GL_LUMINANCE_ALPHA
||
298 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
299 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
300 /* If we're reading back an RGB(A) texture as luminance then we need
301 * to return L=tex(R). Note, that's different from glReadPixels which
304 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
308 _mesa_rebase_rgba_float(width
* height
, (GLfloat (*)[4]) tempImage
,
312 tempSlice
= tempImage
;
313 for (slice
= 0; slice
< depth
; slice
++) {
315 for (row
= 0; row
< height
; row
++) {
316 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
317 width
, height
, format
, type
,
320 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) srcRow
,
321 format
, type
, dest
, &ctx
->Pack
, transferOps
);
324 tempSlice
+= 4 * width
* height
;
332 * Return a base GL format given the user-requested format
333 * for glGetTexImage().
336 _mesa_base_pack_format(GLenum format
)
341 case GL_BGRA_INTEGER
:
342 case GL_RGBA_INTEGER
:
350 case GL_GREEN_INTEGER
:
352 case GL_BLUE_INTEGER
:
354 case GL_ALPHA_INTEGER
:
356 case GL_LUMINANCE_INTEGER_EXT
:
358 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
359 return GL_LUMINANCE_ALPHA
;
367 * Get an uncompressed color texture image.
370 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
371 GLenum format
, GLenum type
, GLvoid
*pixels
,
372 struct gl_texture_image
*texImage
,
373 GLbitfield transferOps
)
375 /* don't want to apply sRGB -> RGB conversion here so override the format */
376 const mesa_format texFormat
=
377 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
378 const GLuint width
= texImage
->Width
;
379 GLenum destBaseFormat
= _mesa_base_pack_format(format
);
380 GLenum rebaseFormat
= GL_NONE
;
381 GLuint height
= texImage
->Height
;
382 GLuint depth
= texImage
->Depth
;
385 GLuint (*rgba_uint
)[4];
386 GLboolean tex_is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
387 GLboolean tex_is_uint
= _mesa_is_format_unsigned(texImage
->TexFormat
);
388 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
390 /* Allocate buffer for one row of texels */
391 rgba
= malloc(4 * width
* sizeof(GLfloat
));
392 rgba_uint
= (GLuint (*)[4]) rgba
;
394 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
398 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
403 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
404 texImage
->_BaseFormat
== GL_INTENSITY
||
405 texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
406 /* If a luminance (or intensity) texture is read back as RGB(A), the
407 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
410 rebaseFormat
= texImage
->_BaseFormat
;
412 else if ((texImage
->_BaseFormat
== GL_RGBA
||
413 texImage
->_BaseFormat
== GL_RGB
||
414 texImage
->_BaseFormat
== GL_RG
) &&
415 (destBaseFormat
== GL_LUMINANCE
||
416 destBaseFormat
== GL_LUMINANCE_ALPHA
||
417 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
418 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
419 /* If we're reading back an RGB(A) texture as luminance then we need
420 * to return L=tex(R). Note, that's different from glReadPixels which
423 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
425 else if (texImage
->_BaseFormat
!= texBaseFormat
) {
426 /* The internal format and the real format differ, so we can't rely
427 * on the unpack functions setting the correct constant values.
428 * (e.g. reading back GL_RGB8 which is actually RGBA won't set alpha=1)
430 switch (texImage
->_BaseFormat
) {
432 if ((texBaseFormat
== GL_RGBA
||
433 texBaseFormat
== GL_RGB
||
434 texBaseFormat
== GL_RG
) &&
435 (destBaseFormat
== GL_RGBA
||
436 destBaseFormat
== GL_RGB
||
437 destBaseFormat
== GL_RG
||
438 destBaseFormat
== GL_GREEN
)) {
439 rebaseFormat
= texImage
->_BaseFormat
;
444 if ((texBaseFormat
== GL_RGBA
||
445 texBaseFormat
== GL_RGB
) &&
446 (destBaseFormat
== GL_RGBA
||
447 destBaseFormat
== GL_RGB
||
448 destBaseFormat
== GL_BLUE
)) {
449 rebaseFormat
= texImage
->_BaseFormat
;
454 if (texBaseFormat
== GL_RGBA
&&
455 (destBaseFormat
== GL_RGBA
||
456 destBaseFormat
== GL_ALPHA
||
457 destBaseFormat
== GL_LUMINANCE_ALPHA
)) {
458 rebaseFormat
= texImage
->_BaseFormat
;
463 if (destBaseFormat
!= GL_ALPHA
) {
464 rebaseFormat
= texImage
->_BaseFormat
;
470 for (img
= 0; img
< depth
; img
++) {
474 /* map src texture buffer */
475 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
476 0, 0, width
, height
, GL_MAP_READ_BIT
,
477 &srcMap
, &rowstride
);
479 for (row
= 0; row
< height
; row
++) {
480 const GLubyte
*src
= srcMap
+ row
* rowstride
;
481 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
482 width
, height
, format
, type
,
485 if (tex_is_integer
) {
486 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
488 _mesa_rebase_rgba_uint(width
, rgba_uint
, rebaseFormat
);
490 _mesa_pack_rgba_span_from_uints(ctx
, width
,
491 (GLuint (*)[4]) rgba_uint
,
494 _mesa_pack_rgba_span_from_ints(ctx
, width
,
495 (GLint (*)[4]) rgba_uint
,
499 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
501 _mesa_rebase_rgba_float(width
, rgba
, rebaseFormat
);
502 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
504 &ctx
->Pack
, transferOps
);
508 /* Unmap the src texture buffer */
509 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
512 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
522 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
523 * Compressed textures are handled here as well.
526 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
527 GLenum format
, GLenum type
, GLvoid
*pixels
,
528 struct gl_texture_image
*texImage
)
530 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
531 GLbitfield transferOps
= 0x0;
533 /* In general, clamping does not apply to glGetTexImage, except when
534 * the returned type of the image can't hold negative values.
536 if (type_needs_clamping(type
)) {
537 /* the returned image type can't have negative values */
538 if (dataType
== GL_FLOAT
||
539 dataType
== GL_HALF_FLOAT
||
540 dataType
== GL_SIGNED_NORMALIZED
||
541 format
== GL_LUMINANCE
||
542 format
== GL_LUMINANCE_ALPHA
) {
543 transferOps
|= IMAGE_CLAMP_BIT
;
547 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
548 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
549 pixels
, texImage
, transferOps
);
552 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
553 pixels
, texImage
, transferOps
);
559 * Try to do glGetTexImage() with simple memcpy().
560 * \return GL_TRUE if done, GL_FALSE otherwise
563 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
565 struct gl_texture_image
*texImage
)
567 const GLenum target
= texImage
->TexObject
->Target
;
568 GLboolean memCopy
= GL_FALSE
;
569 GLenum texBaseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
572 * Check if we can use memcpy to copy from the hardware texture
573 * format to the user's format/type.
574 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
576 if ((target
== GL_TEXTURE_1D
||
577 target
== GL_TEXTURE_2D
||
578 target
== GL_TEXTURE_RECTANGLE
||
579 _mesa_is_cube_face(target
)) &&
580 texBaseFormat
== texImage
->_BaseFormat
) {
581 memCopy
= _mesa_format_matches_format_and_type(texImage
->TexFormat
,
583 ctx
->Pack
.SwapBytes
);
587 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
588 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
590 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
591 texImage
->Height
, format
, type
, 0, 0);
592 const GLint dstRowStride
=
593 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
597 /* map src texture buffer */
598 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
599 0, 0, texImage
->Width
, texImage
->Height
,
600 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
603 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
604 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
608 for (row
= 0; row
< texImage
->Height
; row
++) {
609 memcpy(dst
, src
, bytesPerRow
);
615 /* unmap src texture buffer */
616 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
619 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
628 * This is the software fallback for Driver.GetTexImage().
629 * All error checking will have been done before this routine is called.
630 * We'll call ctx->Driver.MapTextureImage() to access the data, then
631 * unmap with ctx->Driver.UnmapTextureImage().
634 _mesa_GetTexImage_sw(struct gl_context
*ctx
,
635 GLenum format
, GLenum type
, GLvoid
*pixels
,
636 struct gl_texture_image
*texImage
)
638 const GLuint dimensions
=
639 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
641 /* map dest buffer, if PBO */
642 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
643 /* Packing texture image into a PBO.
644 * Map the (potentially) VRAM-based buffer into our process space so
645 * we can write into it with the code below.
646 * A hardware driver might use a sophisticated blit to move the
647 * texture data to the PBO if the PBO is in VRAM along with the texture.
649 GLubyte
*buf
= (GLubyte
*)
650 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
651 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
654 /* out of memory or other unexpected error */
655 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
658 /* <pixels> was an offset into the PBO.
659 * Now make it a real, client-side pointer inside the mapped region.
661 pixels
= ADD_POINTERS(buf
, pixels
);
664 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
667 else if (format
== GL_DEPTH_COMPONENT
) {
668 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
670 else if (format
== GL_DEPTH_STENCIL_EXT
) {
671 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
673 else if (format
== GL_YCBCR_MESA
) {
674 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
677 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
680 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
681 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
688 * This is the software fallback for Driver.GetCompressedTexImage().
689 * All error checking will have been done before this routine is called.
692 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
693 struct gl_texture_image
*texImage
,
696 const GLuint dimensions
=
697 _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
698 struct compressed_pixelstore store
;
702 _mesa_compute_compressed_pixelstore(dimensions
, texImage
->TexFormat
,
703 texImage
->Width
, texImage
->Height
,
708 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
709 /* pack texture image into a PBO */
711 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
712 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
,
715 /* out of memory or other unexpected error */
716 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
717 "glGetCompresssedTexImage(map PBO failed)");
720 dest
= ADD_POINTERS(dest
, img
);
725 dest
+= store
.SkipBytes
;
727 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
731 /* map src texture buffer */
732 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
733 0, 0, texImage
->Width
, texImage
->Height
,
734 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
738 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
739 memcpy(dest
, src
, store
.CopyBytesPerRow
);
740 dest
+= store
.TotalBytesPerRow
;
744 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
746 /* Advance to next slice */
747 dest
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
750 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
754 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
755 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
, MAP_INTERNAL
);
761 * Validate the texture target enum supplied to glTexImage or
762 * glCompressedTexImage.
765 legal_getteximage_target(struct gl_context
*ctx
, GLenum target
)
772 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
773 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
774 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
775 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
776 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
777 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
778 return ctx
->Extensions
.ARB_texture_cube_map
;
779 case GL_TEXTURE_RECTANGLE_NV
:
780 return ctx
->Extensions
.NV_texture_rectangle
;
781 case GL_TEXTURE_1D_ARRAY_EXT
:
782 case GL_TEXTURE_2D_ARRAY_EXT
:
783 return ctx
->Extensions
.EXT_texture_array
;
784 case GL_TEXTURE_CUBE_MAP_ARRAY
:
785 return ctx
->Extensions
.ARB_texture_cube_map_array
;
793 * Do error checking for a glGetTexImage() call.
794 * \return GL_TRUE if any error, GL_FALSE if no errors.
797 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
798 GLenum format
, GLenum type
, GLsizei clientMemSize
,
801 struct gl_texture_object
*texObj
;
802 struct gl_texture_image
*texImage
;
803 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
804 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
805 GLenum baseFormat
, err
;
807 if (!legal_getteximage_target(ctx
, target
)) {
808 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
812 assert(maxLevels
!= 0);
813 if (level
< 0 || level
>= maxLevels
) {
814 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
818 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
819 if (err
!= GL_NO_ERROR
) {
820 _mesa_error(ctx
, err
, "glGetTexImage(format/type)");
824 texObj
= _mesa_get_current_tex_object(ctx
, target
);
827 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
831 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
833 /* non-existant texture image */
837 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
839 /* Make sure the requested image format is compatible with the
842 if (_mesa_is_color_format(format
)
843 && !_mesa_is_color_format(baseFormat
)) {
844 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
847 else if (_mesa_is_depth_format(format
)
848 && !_mesa_is_depth_format(baseFormat
)
849 && !_mesa_is_depthstencil_format(baseFormat
)) {
850 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
853 else if (_mesa_is_stencil_format(format
)
854 && !ctx
->Extensions
.ARB_texture_stencil8
) {
855 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format=GL_STENCIL_INDEX)");
858 else if (_mesa_is_ycbcr_format(format
)
859 && !_mesa_is_ycbcr_format(baseFormat
)) {
860 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
863 else if (_mesa_is_depthstencil_format(format
)
864 && !_mesa_is_depthstencil_format(baseFormat
)) {
865 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
868 else if (_mesa_is_enum_format_integer(format
) !=
869 _mesa_is_format_integer(texImage
->TexFormat
)) {
870 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
874 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
875 texImage
->Height
, texImage
->Depth
,
876 format
, type
, clientMemSize
, pixels
)) {
877 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
878 _mesa_error(ctx
, GL_INVALID_OPERATION
,
879 "glGetTexImage(out of bounds PBO access)");
881 _mesa_error(ctx
, GL_INVALID_OPERATION
,
882 "glGetnTexImageARB(out of bounds access:"
883 " bufSize (%d) is too small)", clientMemSize
);
888 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
889 /* PBO should not be mapped */
890 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
891 _mesa_error(ctx
, GL_INVALID_OPERATION
,
892 "glGetTexImage(PBO is mapped)");
903 * Get texture image. Called by glGetTexImage.
905 * \param target texture target.
906 * \param level image level.
907 * \param format pixel data format for returned image.
908 * \param type pixel data type for returned image.
909 * \param bufSize size of the pixels data buffer.
910 * \param pixels returned pixel data.
913 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
914 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
916 struct gl_texture_object
*texObj
;
917 struct gl_texture_image
*texImage
;
918 GET_CURRENT_CONTEXT(ctx
);
920 FLUSH_VERTICES(ctx
, 0);
922 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
927 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
928 /* not an error, do nothing */
932 texObj
= _mesa_get_current_tex_object(ctx
, target
);
933 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
935 if (_mesa_is_zero_size_texture(texImage
))
938 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
939 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
940 " dstFmt=0x%x, dstType=0x%x\n",
942 _mesa_get_format_name(texImage
->TexFormat
),
943 texImage
->Width
, texImage
->Height
,
947 _mesa_lock_texture(ctx
, texObj
);
949 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
951 _mesa_unlock_texture(ctx
, texObj
);
956 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
957 GLenum type
, GLvoid
*pixels
)
959 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
964 * Do error checking for a glGetCompressedTexImage() call.
965 * \return GL_TRUE if any error, GL_FALSE if no errors.
968 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
969 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
971 struct gl_texture_object
*texObj
;
972 struct gl_texture_image
*texImage
;
973 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
974 GLuint compressedSize
, dimensions
;
976 if (!legal_getteximage_target(ctx
, target
)) {
977 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
982 assert(maxLevels
!= 0);
983 if (level
< 0 || level
>= maxLevels
) {
984 _mesa_error(ctx
, GL_INVALID_VALUE
,
985 "glGetCompressedTexImageARB(bad level = %d)", level
);
989 texObj
= _mesa_get_current_tex_object(ctx
, target
);
991 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
995 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
998 /* probably invalid mipmap level */
999 _mesa_error(ctx
, GL_INVALID_VALUE
,
1000 "glGetCompressedTexImageARB(level)");
1004 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
1005 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1006 "glGetCompressedTexImageARB(texture is not compressed)");
1010 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
1015 /* Check for invalid pixel storage modes */
1016 dimensions
= _mesa_get_texture_dimensions(texImage
->TexObject
->Target
);
1017 if (!_mesa_compressed_pixel_storage_error_check(ctx
, dimensions
,
1019 "glGetCompressedTexImageARB")) {
1023 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1024 /* do bounds checking on writing to client memory */
1025 if (clientMemSize
< (GLsizei
) compressedSize
) {
1026 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1027 "glGetnCompressedTexImageARB(out of bounds access:"
1028 " bufSize (%d) is too small)", clientMemSize
);
1032 /* do bounds checking on PBO write */
1033 if ((const GLubyte
*) img
+ compressedSize
>
1034 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
1035 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1036 "glGetCompressedTexImage(out of bounds PBO access)");
1040 /* make sure PBO is not mapped */
1041 if (_mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1042 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1043 "glGetCompressedTexImage(PBO is mapped)");
1053 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1056 struct gl_texture_object
*texObj
;
1057 struct gl_texture_image
*texImage
;
1058 GET_CURRENT_CONTEXT(ctx
);
1060 FLUSH_VERTICES(ctx
, 0);
1062 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
1066 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
1067 /* not an error, do nothing */
1071 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1072 texImage
= _mesa_select_tex_image(texObj
, target
, level
);
1074 if (_mesa_is_zero_size_texture(texImage
))
1077 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1079 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1081 _mesa_get_format_name(texImage
->TexFormat
),
1082 texImage
->Width
, texImage
->Height
);
1085 _mesa_lock_texture(ctx
, texObj
);
1087 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
1089 _mesa_unlock_texture(ctx
, texObj
);
1093 _mesa_GetCompressedTexImage(GLenum target
, GLint level
, GLvoid
*img
)
1095 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);