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' */
249 GLuint bytes
, bw
, bh
;
251 bytes
= _mesa_get_format_bytes(texFormat
);
252 _mesa_get_format_block_size(texFormat
, &bw
, &bh
);
254 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
257 &srcMap
, &srcRowStride
);
259 /* XXX This line is a bit of a hack to work around the
260 * mismatch of compressed row strides as returned by
261 * MapTextureImage() vs. what the texture decompression code
262 * uses. This will be fixed in the future.
264 srcRowStride
= srcRowStride
* bh
/ bytes
;
266 _mesa_decompress_image(texFormat
, width
, height
,
267 srcMap
, srcRowStride
, tempImage
);
269 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
272 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
276 if (baseFormat
== GL_LUMINANCE
||
277 baseFormat
== GL_LUMINANCE_ALPHA
) {
278 /* Set green and blue to zero since the pack function here will
282 for (i
= 0; i
< width
* height
; i
++) {
283 tempImage
[i
* 4 + GCOMP
] = tempImage
[i
* 4 + BCOMP
] = 0.0f
;
288 for (row
= 0; row
< height
; row
++) {
289 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
290 width
, height
, format
, type
,
293 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) srcRow
,
294 format
, type
, dest
, &ctx
->Pack
, transferOps
);
303 * Get an uncompressed color texture image.
306 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
307 GLenum format
, GLenum type
, GLvoid
*pixels
,
308 struct gl_texture_image
*texImage
,
309 GLbitfield transferOps
)
311 /* don't want to apply sRGB -> RGB conversion here so override the format */
312 const gl_format texFormat
=
313 _mesa_get_srgb_format_linear(texImage
->TexFormat
);
314 const GLuint width
= texImage
->Width
;
315 const GLuint height
= texImage
->Height
;
316 const GLuint depth
= texImage
->Depth
;
319 GLuint (*rgba_uint
)[4];
320 GLboolean is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
322 /* Allocate buffer for one row of texels */
323 rgba
= (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
324 rgba_uint
= (GLuint (*)[4]) rgba
;
326 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
330 for (img
= 0; img
< depth
; img
++) {
334 /* map src texture buffer */
335 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
336 0, 0, width
, height
, GL_MAP_READ_BIT
,
337 &srcMap
, &rowstride
);
339 for (row
= 0; row
< height
; row
++) {
340 const GLubyte
*src
= srcMap
+ row
* rowstride
;
341 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
342 width
, height
, format
, type
,
346 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
348 if (texImage
->_BaseFormat
== GL_ALPHA
) {
350 for (col
= 0; col
< width
; col
++) {
351 rgba_uint
[col
][RCOMP
] = 0;
352 rgba_uint
[col
][GCOMP
] = 0;
353 rgba_uint
[col
][BCOMP
] = 0;
356 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
358 for (col
= 0; col
< width
; col
++) {
359 rgba_uint
[col
][GCOMP
] = 0;
360 rgba_uint
[col
][BCOMP
] = 0;
361 rgba_uint
[col
][ACOMP
] = 1;
364 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
366 for (col
= 0; col
< width
; col
++) {
367 rgba_uint
[col
][GCOMP
] = 0;
368 rgba_uint
[col
][BCOMP
] = 0;
371 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
373 for (col
= 0; col
< width
; col
++) {
374 rgba_uint
[col
][GCOMP
] = 0;
375 rgba_uint
[col
][BCOMP
] = 0;
376 rgba_uint
[col
][ACOMP
] = 1;
380 _mesa_pack_rgba_span_int(ctx
, width
, rgba_uint
,
383 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
385 if (texImage
->_BaseFormat
== GL_ALPHA
) {
387 for (col
= 0; col
< width
; col
++) {
388 rgba
[col
][RCOMP
] = 0.0F
;
389 rgba
[col
][GCOMP
] = 0.0F
;
390 rgba
[col
][BCOMP
] = 0.0F
;
393 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
395 for (col
= 0; col
< width
; col
++) {
396 rgba
[col
][GCOMP
] = 0.0F
;
397 rgba
[col
][BCOMP
] = 0.0F
;
398 rgba
[col
][ACOMP
] = 1.0F
;
401 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
403 for (col
= 0; col
< width
; col
++) {
404 rgba
[col
][GCOMP
] = 0.0F
;
405 rgba
[col
][BCOMP
] = 0.0F
;
408 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
410 for (col
= 0; col
< width
; col
++) {
411 rgba
[col
][GCOMP
] = 0.0F
;
412 rgba
[col
][BCOMP
] = 0.0F
;
413 rgba
[col
][ACOMP
] = 1.0F
;
417 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
419 &ctx
->Pack
, transferOps
);
423 /* Unmap the src texture buffer */
424 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
427 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
437 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
438 * Compressed textures are handled here as well.
441 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
442 GLenum format
, GLenum type
, GLvoid
*pixels
,
443 struct gl_texture_image
*texImage
)
445 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
446 GLbitfield transferOps
= 0x0;
448 /* In general, clamping does not apply to glGetTexImage, except when
449 * the returned type of the image can't hold negative values.
451 if (type_needs_clamping(type
)) {
452 /* the returned image type can't have negative values */
453 if (dataType
== GL_FLOAT
||
454 dataType
== GL_SIGNED_NORMALIZED
||
455 format
== GL_LUMINANCE
||
456 format
== GL_LUMINANCE_ALPHA
) {
457 transferOps
|= IMAGE_CLAMP_BIT
;
461 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
462 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
463 pixels
, texImage
, transferOps
);
466 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
467 pixels
, texImage
, transferOps
);
473 * Try to do glGetTexImage() with simple memcpy().
474 * \return GL_TRUE if done, GL_FALSE otherwise
477 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
479 struct gl_texture_image
*texImage
)
481 const GLenum target
= texImage
->TexObject
->Target
;
482 GLboolean memCopy
= GL_FALSE
;
485 * Check if the src/dst formats are compatible.
486 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
487 * so we don't have to worry about those.
488 * XXX more format combinations could be supported here.
490 if (target
== GL_TEXTURE_1D
||
491 target
== GL_TEXTURE_2D
||
492 target
== GL_TEXTURE_RECTANGLE
||
493 _mesa_is_cube_face(target
)) {
494 if ((texImage
->TexFormat
== MESA_FORMAT_ARGB8888
||
495 texImage
->TexFormat
== MESA_FORMAT_SARGB8
) &&
497 (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
498 !ctx
->Pack
.SwapBytes
&&
499 _mesa_little_endian()) {
502 else if ((texImage
->TexFormat
== MESA_FORMAT_AL88
||
503 texImage
->TexFormat
== MESA_FORMAT_SLA8
) &&
504 format
== GL_LUMINANCE_ALPHA
&&
505 type
== GL_UNSIGNED_BYTE
&&
506 !ctx
->Pack
.SwapBytes
&&
507 _mesa_little_endian()) {
510 else if ((texImage
->TexFormat
== MESA_FORMAT_L8
||
511 texImage
->TexFormat
== MESA_FORMAT_SL8
) &&
512 format
== GL_LUMINANCE
&&
513 type
== GL_UNSIGNED_BYTE
) {
516 else if (texImage
->TexFormat
== MESA_FORMAT_L16
&&
517 format
== GL_LUMINANCE
&&
518 type
== GL_UNSIGNED_SHORT
) {
521 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
522 format
== GL_ALPHA
&&
523 type
== GL_UNSIGNED_BYTE
) {
526 else if (texImage
->TexFormat
== MESA_FORMAT_A16
&&
527 format
== GL_ALPHA
&&
528 type
== GL_UNSIGNED_SHORT
) {
534 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
535 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
537 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
538 texImage
->Height
, format
, type
, 0, 0);
539 const GLint dstRowStride
=
540 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
544 /* map src texture buffer */
545 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
546 0, 0, texImage
->Width
, texImage
->Height
,
547 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
550 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
551 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
555 for (row
= 0; row
< texImage
->Height
; row
++) {
556 memcpy(dst
, src
, bytesPerRow
);
562 /* unmap src texture buffer */
563 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
566 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
575 * This is the software fallback for Driver.GetTexImage().
576 * All error checking will have been done before this routine is called.
577 * We'll call ctx->Driver.MapTextureImage() to access the data, then
578 * unmap with ctx->Driver.UnmapTextureImage().
581 _mesa_get_teximage(struct gl_context
*ctx
,
582 GLenum format
, GLenum type
, GLvoid
*pixels
,
583 struct gl_texture_image
*texImage
)
587 switch (texImage
->TexObject
->Target
) {
598 /* map dest buffer, if PBO */
599 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
600 /* Packing texture image into a PBO.
601 * Map the (potentially) VRAM-based buffer into our process space so
602 * we can write into it with the code below.
603 * A hardware driver might use a sophisticated blit to move the
604 * texture data to the PBO if the PBO is in VRAM along with the texture.
606 GLubyte
*buf
= (GLubyte
*)
607 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
608 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
610 /* out of memory or other unexpected error */
611 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
614 /* <pixels> was an offset into the PBO.
615 * Now make it a real, client-side pointer inside the mapped region.
617 pixels
= ADD_POINTERS(buf
, pixels
);
620 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
623 else if (format
== GL_DEPTH_COMPONENT
) {
624 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
626 else if (format
== GL_DEPTH_STENCIL_EXT
) {
627 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
629 else if (format
== GL_YCBCR_MESA
) {
630 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
633 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
636 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
637 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
644 * This is the software fallback for Driver.GetCompressedTexImage().
645 * All error checking will have been done before this routine is called.
648 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
649 struct gl_texture_image
*texImage
,
652 const GLuint row_stride
=
653 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
658 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
659 /* pack texture image into a PBO */
660 GLubyte
*buf
= (GLubyte
*)
661 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
662 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
664 /* out of memory or other unexpected error */
665 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
666 "glGetCompresssedTexImage(map PBO failed)");
669 img
= ADD_POINTERS(buf
, img
);
672 /* map src texture buffer */
673 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
674 0, 0, texImage
->Width
, texImage
->Height
,
675 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
678 /* no pixelstore or pixel transfer, but respect stride */
680 if (row_stride
== srcRowStride
) {
681 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
685 memcpy(img
, src
, size
);
689 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
690 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
691 memcpy((GLubyte
*)img
+ i
* row_stride
,
692 (GLubyte
*)src
+ i
* srcRowStride
,
697 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
700 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
703 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
704 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
711 * Do error checking for a glGetTexImage() call.
712 * \return GL_TRUE if any error, GL_FALSE if no errors.
715 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
716 GLenum format
, GLenum type
, GLsizei clientMemSize
,
719 struct gl_texture_object
*texObj
;
720 struct gl_texture_image
*texImage
;
721 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
722 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
725 if (maxLevels
== 0) {
726 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
730 if (level
< 0 || level
>= maxLevels
) {
731 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
735 if (_mesa_sizeof_packed_type(type
) <= 0) {
736 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
740 if (_mesa_components_in_format(format
) <= 0 ||
741 format
== GL_STENCIL_INDEX
||
742 format
== GL_COLOR_INDEX
) {
743 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
747 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
748 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
752 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
753 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
757 if (!ctx
->Extensions
.EXT_packed_depth_stencil
758 && _mesa_is_depthstencil_format(format
)) {
759 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
763 if (!ctx
->Extensions
.ATI_envmap_bumpmap
764 && _mesa_is_dudv_format(format
)) {
765 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
769 texObj
= _mesa_get_current_tex_object(ctx
, target
);
771 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
772 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
776 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
777 /* GL_INVALID_OPERATION is generated by a format/type
778 * mismatch (see the 1.2 spec page 94, sec 3.6.4.)
780 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(target)");
784 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
786 /* non-existant texture image */
790 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
792 /* Make sure the requested image format is compatible with the
795 if (_mesa_is_color_format(format
)
796 && !_mesa_is_color_format(baseFormat
)) {
797 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
800 else if (_mesa_is_depth_format(format
)
801 && !_mesa_is_depth_format(baseFormat
)
802 && !_mesa_is_depthstencil_format(baseFormat
)) {
803 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
806 else if (_mesa_is_ycbcr_format(format
)
807 && !_mesa_is_ycbcr_format(baseFormat
)) {
808 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
811 else if (_mesa_is_depthstencil_format(format
)
812 && !_mesa_is_depthstencil_format(baseFormat
)) {
813 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
816 else if (_mesa_is_dudv_format(format
)
817 && !_mesa_is_dudv_format(baseFormat
)) {
818 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
822 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
823 texImage
->Height
, texImage
->Depth
,
824 format
, type
, clientMemSize
, pixels
)) {
825 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
826 _mesa_error(ctx
, GL_INVALID_OPERATION
,
827 "glGetTexImage(out of bounds PBO access)");
829 _mesa_error(ctx
, GL_INVALID_OPERATION
,
830 "glGetnTexImageARB(out of bounds access:"
831 " bufSize (%d) is too small)", clientMemSize
);
836 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
837 /* PBO should not be mapped */
838 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
839 _mesa_error(ctx
, GL_INVALID_OPERATION
,
840 "glGetTexImage(PBO is mapped)");
851 * Get texture image. Called by glGetTexImage.
853 * \param target texture target.
854 * \param level image level.
855 * \param format pixel data format for returned image.
856 * \param type pixel data type for returned image.
857 * \param bufSize size of the pixels data buffer.
858 * \param pixels returned pixel data.
861 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
862 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
864 struct gl_texture_object
*texObj
;
865 struct gl_texture_image
*texImage
;
866 GET_CURRENT_CONTEXT(ctx
);
867 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
869 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
874 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
875 /* not an error, do nothing */
879 texObj
= _mesa_get_current_tex_object(ctx
, target
);
880 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
882 if (_mesa_is_zero_size_texture(texImage
))
885 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
886 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
887 " dstFmt=0x%x, dstType=0x%x\n",
889 _mesa_get_format_name(texImage
->TexFormat
),
890 texImage
->Width
, texImage
->Height
,
894 _mesa_lock_texture(ctx
, texObj
);
896 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
898 _mesa_unlock_texture(ctx
, texObj
);
903 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
904 GLenum type
, GLvoid
*pixels
)
906 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
911 * Do error checking for a glGetCompressedTexImage() call.
912 * \return GL_TRUE if any error, GL_FALSE if no errors.
915 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
916 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
918 struct gl_texture_object
*texObj
;
919 struct gl_texture_image
*texImage
;
920 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
921 GLuint compressedSize
;
923 if (maxLevels
== 0) {
924 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
929 if (level
< 0 || level
>= maxLevels
) {
930 _mesa_error(ctx
, GL_INVALID_VALUE
,
931 "glGetCompressedTexImageARB(bad level = %d)", level
);
935 if (_mesa_is_proxy_texture(target
)) {
936 _mesa_error(ctx
, GL_INVALID_ENUM
,
937 "glGetCompressedTexImageARB(bad target = %s)",
938 _mesa_lookup_enum_by_nr(target
));
942 texObj
= _mesa_get_current_tex_object(ctx
, target
);
944 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
948 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
951 /* probably invalid mipmap level */
952 _mesa_error(ctx
, GL_INVALID_VALUE
,
953 "glGetCompressedTexImageARB(level)");
957 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
958 _mesa_error(ctx
, GL_INVALID_OPERATION
,
959 "glGetCompressedTexImageARB(texture is not compressed)");
963 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
968 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
969 /* do bounds checking on writing to client memory */
970 if (clientMemSize
< compressedSize
) {
971 _mesa_error(ctx
, GL_INVALID_OPERATION
,
972 "glGetnCompressedTexImageARB(out of bounds access:"
973 " bufSize (%d) is too small)", clientMemSize
);
977 /* do bounds checking on PBO write */
978 if ((const GLubyte
*) img
+ compressedSize
>
979 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
980 _mesa_error(ctx
, GL_INVALID_OPERATION
,
981 "glGetCompressedTexImage(out of bounds PBO access)");
985 /* make sure PBO is not mapped */
986 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
987 _mesa_error(ctx
, GL_INVALID_OPERATION
,
988 "glGetCompressedTexImage(PBO is mapped)");
998 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1001 struct gl_texture_object
*texObj
;
1002 struct gl_texture_image
*texImage
;
1003 GET_CURRENT_CONTEXT(ctx
);
1004 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1006 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
1010 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
1011 /* not an error, do nothing */
1015 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1016 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1018 if (_mesa_is_zero_size_texture(texImage
))
1021 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1023 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1025 _mesa_get_format_name(texImage
->TexFormat
),
1026 texImage
->Width
, texImage
->Height
);
1029 _mesa_lock_texture(ctx
, texObj
);
1031 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
1033 _mesa_unlock_texture(ctx
, texObj
);
1037 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
1039 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);