2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (c) 2009 VMware, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Code for glGetTexImage() and glGetCompressedTexImage().
33 #include "bufferobj.h"
37 #include "format_unpack.h"
39 #include "mfeatures.h"
43 #include "texcompress.h"
44 #include "texgetimage.h"
50 * Can the given type represent negative values?
52 static inline GLboolean
53 type_needs_clamping(GLenum type
)
60 case GL_HALF_FLOAT_ARB
:
61 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
62 case GL_UNSIGNED_INT_5_9_9_9_REV
:
71 * glGetTexImage for depth/Z pixels.
74 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
75 GLenum format
, GLenum type
, GLvoid
*pixels
,
76 struct gl_texture_image
*texImage
)
78 const GLint width
= texImage
->Width
;
79 const GLint height
= texImage
->Height
;
80 const GLint depth
= texImage
->Depth
;
82 GLfloat
*depthRow
= (GLfloat
*) malloc(width
* sizeof(GLfloat
));
85 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
89 for (img
= 0; img
< depth
; img
++) {
93 /* map src texture buffer */
94 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
95 0, 0, width
, height
, GL_MAP_READ_BIT
,
96 &srcMap
, &srcRowStride
);
99 for (row
= 0; row
< height
; row
++) {
100 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
101 width
, height
, format
, type
,
103 const GLubyte
*src
= srcMap
+ row
* srcRowStride
;
104 _mesa_unpack_float_z_row(texImage
->TexFormat
, width
, src
, depthRow
);
105 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
108 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
111 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
121 * glGetTexImage for depth/stencil pixels.
124 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
125 GLenum format
, GLenum type
, GLvoid
*pixels
,
126 struct gl_texture_image
*texImage
)
128 const GLint width
= texImage
->Width
;
129 const GLint height
= texImage
->Height
;
130 const GLint depth
= texImage
->Depth
;
133 for (img
= 0; img
< depth
; img
++) {
137 /* map src texture buffer */
138 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
139 0, 0, width
, height
, GL_MAP_READ_BIT
,
140 &srcMap
, &rowstride
);
143 for (row
= 0; row
< height
; row
++) {
144 const GLubyte
*src
= srcMap
+ row
* rowstride
;
145 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
146 width
, height
, format
, type
,
148 /* XXX Z24_S8 vs. S8_Z24??? */
149 memcpy(dest
, src
, width
* sizeof(GLuint
));
150 if (ctx
->Pack
.SwapBytes
) {
151 _mesa_swap4((GLuint
*) dest
, width
);
155 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
158 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
166 * glGetTexImage for YCbCr pixels.
169 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
170 GLenum format
, GLenum type
, GLvoid
*pixels
,
171 struct gl_texture_image
*texImage
)
173 const GLint width
= texImage
->Width
;
174 const GLint height
= texImage
->Height
;
175 const GLint depth
= texImage
->Depth
;
178 for (img
= 0; img
< depth
; img
++) {
182 /* map src texture buffer */
183 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
184 0, 0, width
, height
, GL_MAP_READ_BIT
,
185 &srcMap
, &rowstride
);
188 for (row
= 0; row
< height
; row
++) {
189 const GLubyte
*src
= srcMap
+ row
* rowstride
;
190 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
191 width
, height
, format
, type
,
193 memcpy(dest
, src
, width
* sizeof(GLushort
));
195 /* check for byte swapping */
196 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
197 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
198 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
199 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
200 if (!ctx
->Pack
.SwapBytes
)
201 _mesa_swap2((GLushort
*) dest
, width
);
203 else if (ctx
->Pack
.SwapBytes
) {
204 _mesa_swap2((GLushort
*) dest
, width
);
208 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
211 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
219 * Get a color texture image with decompression.
222 get_tex_rgba_compressed(struct gl_context
*ctx
, GLuint dimensions
,
223 GLenum format
, GLenum type
, GLvoid
*pixels
,
224 struct gl_texture_image
*texImage
,
225 GLbitfield transferOps
)
227 /* don't want to apply sRGB -> RGB conversion here so override the format */
228 const gl_format texFormat
=
229 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
230 const GLenum baseFormat
= _mesa_get_format_base_format(texFormat
);
231 const GLuint width
= texImage
->Width
;
232 const GLuint height
= texImage
->Height
;
233 const GLuint depth
= texImage
->Depth
;
234 GLfloat
*tempImage
, *srcRow
;
237 /* Decompress into temp float buffer, then pack into user buffer */
238 tempImage
= (GLfloat
*) malloc(width
* height
* depth
239 * 4 * sizeof(GLfloat
));
241 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
245 /* Decompress the texture image - results in 'tempImage' */
250 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
253 &srcMap
, &srcRowStride
);
255 _mesa_decompress_image(texFormat
, width
, height
,
256 srcMap
, srcRowStride
, tempImage
);
258 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
261 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
265 if (baseFormat
== GL_LUMINANCE
||
266 baseFormat
== GL_LUMINANCE_ALPHA
) {
267 /* Set green and blue to zero since the pack function here will
271 for (i
= 0; i
< width
* height
; i
++) {
272 tempImage
[i
* 4 + GCOMP
] = tempImage
[i
* 4 + BCOMP
] = 0.0f
;
277 for (row
= 0; row
< height
; row
++) {
278 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
279 width
, height
, format
, type
,
282 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) srcRow
,
283 format
, type
, dest
, &ctx
->Pack
, transferOps
);
292 * Get an uncompressed color texture image.
295 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
296 GLenum format
, GLenum type
, GLvoid
*pixels
,
297 struct gl_texture_image
*texImage
,
298 GLbitfield transferOps
)
300 /* don't want to apply sRGB -> RGB conversion here so override the format */
301 const gl_format texFormat
=
302 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
303 const GLuint width
= texImage
->Width
;
304 GLuint height
= texImage
->Height
;
305 GLuint depth
= texImage
->Depth
;
308 GLuint (*rgba_uint
)[4];
309 GLboolean is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
311 /* Allocate buffer for one row of texels */
312 rgba
= (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
313 rgba_uint
= (GLuint (*)[4]) rgba
;
315 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
319 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
324 for (img
= 0; img
< depth
; img
++) {
328 /* map src texture buffer */
329 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
330 0, 0, width
, height
, GL_MAP_READ_BIT
,
331 &srcMap
, &rowstride
);
333 for (row
= 0; row
< height
; row
++) {
334 const GLubyte
*src
= srcMap
+ row
* rowstride
;
335 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
336 width
, height
, format
, type
,
340 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
342 if (texImage
->_BaseFormat
== GL_ALPHA
) {
344 for (col
= 0; col
< width
; col
++) {
345 rgba_uint
[col
][RCOMP
] = 0;
346 rgba_uint
[col
][GCOMP
] = 0;
347 rgba_uint
[col
][BCOMP
] = 0;
350 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
352 for (col
= 0; col
< width
; col
++) {
353 rgba_uint
[col
][GCOMP
] = 0;
354 rgba_uint
[col
][BCOMP
] = 0;
355 rgba_uint
[col
][ACOMP
] = 1;
358 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
360 for (col
= 0; col
< width
; col
++) {
361 rgba_uint
[col
][GCOMP
] = 0;
362 rgba_uint
[col
][BCOMP
] = 0;
365 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
367 for (col
= 0; col
< width
; col
++) {
368 rgba_uint
[col
][GCOMP
] = 0;
369 rgba_uint
[col
][BCOMP
] = 0;
370 rgba_uint
[col
][ACOMP
] = 1;
374 _mesa_pack_rgba_span_int(ctx
, width
, rgba_uint
,
377 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
379 if (texImage
->_BaseFormat
== GL_ALPHA
) {
381 for (col
= 0; col
< width
; col
++) {
382 rgba
[col
][RCOMP
] = 0.0F
;
383 rgba
[col
][GCOMP
] = 0.0F
;
384 rgba
[col
][BCOMP
] = 0.0F
;
387 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
389 for (col
= 0; col
< width
; col
++) {
390 rgba
[col
][GCOMP
] = 0.0F
;
391 rgba
[col
][BCOMP
] = 0.0F
;
392 rgba
[col
][ACOMP
] = 1.0F
;
395 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
397 for (col
= 0; col
< width
; col
++) {
398 rgba
[col
][GCOMP
] = 0.0F
;
399 rgba
[col
][BCOMP
] = 0.0F
;
402 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
404 for (col
= 0; col
< width
; col
++) {
405 rgba
[col
][GCOMP
] = 0.0F
;
406 rgba
[col
][BCOMP
] = 0.0F
;
407 rgba
[col
][ACOMP
] = 1.0F
;
411 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
413 &ctx
->Pack
, transferOps
);
417 /* Unmap the src texture buffer */
418 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
421 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
431 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
432 * Compressed textures are handled here as well.
435 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
436 GLenum format
, GLenum type
, GLvoid
*pixels
,
437 struct gl_texture_image
*texImage
)
439 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
440 GLbitfield transferOps
= 0x0;
442 /* In general, clamping does not apply to glGetTexImage, except when
443 * the returned type of the image can't hold negative values.
445 if (type_needs_clamping(type
)) {
446 /* the returned image type can't have negative values */
447 if (dataType
== GL_FLOAT
||
448 dataType
== GL_SIGNED_NORMALIZED
||
449 format
== GL_LUMINANCE
||
450 format
== GL_LUMINANCE_ALPHA
) {
451 transferOps
|= IMAGE_CLAMP_BIT
;
454 /* This applies to RGB, RGBA textures. if the format is either LUMINANCE
455 * or LUMINANCE ALPHA, luminance (L) is computed as L=R+G+B .we need to
456 * clamp the sum to [0,1].
458 else if ((format
== GL_LUMINANCE
||
459 format
== GL_LUMINANCE_ALPHA
) &&
460 dataType
== GL_UNSIGNED_NORMALIZED
) {
461 transferOps
|= IMAGE_CLAMP_BIT
;
464 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
465 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
466 pixels
, texImage
, transferOps
);
469 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
470 pixels
, texImage
, transferOps
);
476 * Try to do glGetTexImage() with simple memcpy().
477 * \return GL_TRUE if done, GL_FALSE otherwise
480 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
482 struct gl_texture_image
*texImage
)
484 const GLenum target
= texImage
->TexObject
->Target
;
485 GLboolean memCopy
= GL_FALSE
;
488 * Check if the src/dst formats are compatible.
489 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
490 * so we don't have to worry about those.
491 * XXX more format combinations could be supported here.
493 if (target
== GL_TEXTURE_1D
||
494 target
== GL_TEXTURE_2D
||
495 target
== GL_TEXTURE_RECTANGLE
||
496 _mesa_is_cube_face(target
)) {
497 if ((texImage
->TexFormat
== MESA_FORMAT_ARGB8888
||
498 texImage
->TexFormat
== MESA_FORMAT_SARGB8
) &&
500 (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
501 !ctx
->Pack
.SwapBytes
&&
502 _mesa_little_endian()) {
505 else if ((texImage
->TexFormat
== MESA_FORMAT_AL88
||
506 texImage
->TexFormat
== MESA_FORMAT_SLA8
) &&
507 format
== GL_LUMINANCE_ALPHA
&&
508 type
== GL_UNSIGNED_BYTE
&&
509 !ctx
->Pack
.SwapBytes
&&
510 _mesa_little_endian()) {
513 else if ((texImage
->TexFormat
== MESA_FORMAT_L8
||
514 texImage
->TexFormat
== MESA_FORMAT_SL8
) &&
515 format
== GL_LUMINANCE
&&
516 type
== GL_UNSIGNED_BYTE
) {
519 else if (texImage
->TexFormat
== MESA_FORMAT_L16
&&
520 format
== GL_LUMINANCE
&&
521 type
== GL_UNSIGNED_SHORT
) {
524 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
525 format
== GL_ALPHA
&&
526 type
== GL_UNSIGNED_BYTE
) {
529 else if (texImage
->TexFormat
== MESA_FORMAT_A16
&&
530 format
== GL_ALPHA
&&
531 type
== GL_UNSIGNED_SHORT
) {
537 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
538 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
540 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
541 texImage
->Height
, format
, type
, 0, 0);
542 const GLint dstRowStride
=
543 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
547 /* map src texture buffer */
548 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
549 0, 0, texImage
->Width
, texImage
->Height
,
550 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
553 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
554 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
558 for (row
= 0; row
< texImage
->Height
; row
++) {
559 memcpy(dst
, src
, bytesPerRow
);
565 /* unmap src texture buffer */
566 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
569 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
578 * This is the software fallback for Driver.GetTexImage().
579 * All error checking will have been done before this routine is called.
580 * We'll call ctx->Driver.MapTextureImage() to access the data, then
581 * unmap with ctx->Driver.UnmapTextureImage().
584 _mesa_get_teximage(struct gl_context
*ctx
,
585 GLenum format
, GLenum type
, GLvoid
*pixels
,
586 struct gl_texture_image
*texImage
)
590 switch (texImage
->TexObject
->Target
) {
601 /* map dest buffer, if PBO */
602 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
603 /* Packing texture image into a PBO.
604 * Map the (potentially) VRAM-based buffer into our process space so
605 * we can write into it with the code below.
606 * A hardware driver might use a sophisticated blit to move the
607 * texture data to the PBO if the PBO is in VRAM along with the texture.
609 GLubyte
*buf
= (GLubyte
*)
610 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
611 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
613 /* out of memory or other unexpected error */
614 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
617 /* <pixels> was an offset into the PBO.
618 * Now make it a real, client-side pointer inside the mapped region.
620 pixels
= ADD_POINTERS(buf
, pixels
);
623 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
626 else if (format
== GL_DEPTH_COMPONENT
) {
627 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
629 else if (format
== GL_DEPTH_STENCIL_EXT
) {
630 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
632 else if (format
== GL_YCBCR_MESA
) {
633 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
636 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
639 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
640 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
647 * This is the software fallback for Driver.GetCompressedTexImage().
648 * All error checking will have been done before this routine is called.
651 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
652 struct gl_texture_image
*texImage
,
655 const GLuint row_stride
=
656 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
661 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
662 /* pack texture image into a PBO */
663 GLubyte
*buf
= (GLubyte
*)
664 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
665 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
667 /* out of memory or other unexpected error */
668 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
669 "glGetCompresssedTexImage(map PBO failed)");
672 img
= ADD_POINTERS(buf
, img
);
675 /* map src texture buffer */
676 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
677 0, 0, texImage
->Width
, texImage
->Height
,
678 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
681 /* no pixelstore or pixel transfer, but respect stride */
683 if (row_stride
== srcRowStride
) {
684 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
688 memcpy(img
, src
, size
);
692 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
693 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
694 memcpy((GLubyte
*)img
+ i
* row_stride
,
695 (GLubyte
*)src
+ i
* srcRowStride
,
700 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
703 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
706 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
707 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
714 * Do error checking for a glGetTexImage() call.
715 * \return GL_TRUE if any error, GL_FALSE if no errors.
718 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
719 GLenum format
, GLenum type
, GLsizei clientMemSize
,
722 struct gl_texture_object
*texObj
;
723 struct gl_texture_image
*texImage
;
724 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
725 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
726 GLenum baseFormat
, err
;
728 if (maxLevels
== 0) {
729 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
733 if (level
< 0 || level
>= maxLevels
) {
734 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
738 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
739 if (err
!= GL_NO_ERROR
) {
740 _mesa_error(ctx
, err
, "glGetTexImage(format/type)");
744 texObj
= _mesa_get_current_tex_object(ctx
, target
);
746 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
747 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
751 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
753 /* non-existant texture image */
757 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
759 /* Make sure the requested image format is compatible with the
762 if (_mesa_is_color_format(format
)
763 && !_mesa_is_color_format(baseFormat
)) {
764 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
767 else if (_mesa_is_depth_format(format
)
768 && !_mesa_is_depth_format(baseFormat
)
769 && !_mesa_is_depthstencil_format(baseFormat
)) {
770 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
773 else if (_mesa_is_ycbcr_format(format
)
774 && !_mesa_is_ycbcr_format(baseFormat
)) {
775 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
778 else if (_mesa_is_depthstencil_format(format
)
779 && !_mesa_is_depthstencil_format(baseFormat
)) {
780 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
783 else if (_mesa_is_dudv_format(format
)
784 && !_mesa_is_dudv_format(baseFormat
)) {
785 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
789 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
790 texImage
->Height
, texImage
->Depth
,
791 format
, type
, clientMemSize
, pixels
)) {
792 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
793 _mesa_error(ctx
, GL_INVALID_OPERATION
,
794 "glGetTexImage(out of bounds PBO access)");
796 _mesa_error(ctx
, GL_INVALID_OPERATION
,
797 "glGetnTexImageARB(out of bounds access:"
798 " bufSize (%d) is too small)", clientMemSize
);
803 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
804 /* PBO should not be mapped */
805 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
806 _mesa_error(ctx
, GL_INVALID_OPERATION
,
807 "glGetTexImage(PBO is mapped)");
818 * Get texture image. Called by glGetTexImage.
820 * \param target texture target.
821 * \param level image level.
822 * \param format pixel data format for returned image.
823 * \param type pixel data type for returned image.
824 * \param bufSize size of the pixels data buffer.
825 * \param pixels returned pixel data.
828 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
829 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
831 struct gl_texture_object
*texObj
;
832 struct gl_texture_image
*texImage
;
833 GET_CURRENT_CONTEXT(ctx
);
834 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
836 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
841 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
842 /* not an error, do nothing */
846 texObj
= _mesa_get_current_tex_object(ctx
, target
);
847 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
849 if (_mesa_is_zero_size_texture(texImage
))
852 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
853 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
854 " dstFmt=0x%x, dstType=0x%x\n",
856 _mesa_get_format_name(texImage
->TexFormat
),
857 texImage
->Width
, texImage
->Height
,
861 _mesa_lock_texture(ctx
, texObj
);
863 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
865 _mesa_unlock_texture(ctx
, texObj
);
870 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
871 GLenum type
, GLvoid
*pixels
)
873 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
878 * Do error checking for a glGetCompressedTexImage() call.
879 * \return GL_TRUE if any error, GL_FALSE if no errors.
882 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
883 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
885 struct gl_texture_object
*texObj
;
886 struct gl_texture_image
*texImage
;
887 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
888 GLuint compressedSize
;
890 if (maxLevels
== 0) {
891 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
896 if (level
< 0 || level
>= maxLevels
) {
897 _mesa_error(ctx
, GL_INVALID_VALUE
,
898 "glGetCompressedTexImageARB(bad level = %d)", level
);
902 if (_mesa_is_proxy_texture(target
)) {
903 _mesa_error(ctx
, GL_INVALID_ENUM
,
904 "glGetCompressedTexImageARB(bad target = %s)",
905 _mesa_lookup_enum_by_nr(target
));
909 texObj
= _mesa_get_current_tex_object(ctx
, target
);
911 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
915 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
918 /* probably invalid mipmap level */
919 _mesa_error(ctx
, GL_INVALID_VALUE
,
920 "glGetCompressedTexImageARB(level)");
924 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
925 _mesa_error(ctx
, GL_INVALID_OPERATION
,
926 "glGetCompressedTexImageARB(texture is not compressed)");
930 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
935 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
936 /* do bounds checking on writing to client memory */
937 if (clientMemSize
< compressedSize
) {
938 _mesa_error(ctx
, GL_INVALID_OPERATION
,
939 "glGetnCompressedTexImageARB(out of bounds access:"
940 " bufSize (%d) is too small)", clientMemSize
);
944 /* do bounds checking on PBO write */
945 if ((const GLubyte
*) img
+ compressedSize
>
946 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
947 _mesa_error(ctx
, GL_INVALID_OPERATION
,
948 "glGetCompressedTexImage(out of bounds PBO access)");
952 /* make sure PBO is not mapped */
953 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
954 _mesa_error(ctx
, GL_INVALID_OPERATION
,
955 "glGetCompressedTexImage(PBO is mapped)");
965 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
968 struct gl_texture_object
*texObj
;
969 struct gl_texture_image
*texImage
;
970 GET_CURRENT_CONTEXT(ctx
);
971 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
973 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
977 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
978 /* not an error, do nothing */
982 texObj
= _mesa_get_current_tex_object(ctx
, target
);
983 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
985 if (_mesa_is_zero_size_texture(texImage
))
988 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
990 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
992 _mesa_get_format_name(texImage
->TexFormat
),
993 texImage
->Width
, texImage
->Height
);
996 _mesa_lock_texture(ctx
, texObj
);
998 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
1000 _mesa_unlock_texture(ctx
, texObj
);
1004 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
1006 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);