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 GLuint height
= texImage
->Height
;
316 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 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
335 for (img
= 0; img
< depth
; img
++) {
339 /* map src texture buffer */
340 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
341 0, 0, width
, height
, GL_MAP_READ_BIT
,
342 &srcMap
, &rowstride
);
344 for (row
= 0; row
< height
; row
++) {
345 const GLubyte
*src
= srcMap
+ row
* rowstride
;
346 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
347 width
, height
, format
, type
,
351 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
353 if (texImage
->_BaseFormat
== GL_ALPHA
) {
355 for (col
= 0; col
< width
; col
++) {
356 rgba_uint
[col
][RCOMP
] = 0;
357 rgba_uint
[col
][GCOMP
] = 0;
358 rgba_uint
[col
][BCOMP
] = 0;
361 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
363 for (col
= 0; col
< width
; col
++) {
364 rgba_uint
[col
][GCOMP
] = 0;
365 rgba_uint
[col
][BCOMP
] = 0;
366 rgba_uint
[col
][ACOMP
] = 1;
369 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
371 for (col
= 0; col
< width
; col
++) {
372 rgba_uint
[col
][GCOMP
] = 0;
373 rgba_uint
[col
][BCOMP
] = 0;
376 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
378 for (col
= 0; col
< width
; col
++) {
379 rgba_uint
[col
][GCOMP
] = 0;
380 rgba_uint
[col
][BCOMP
] = 0;
381 rgba_uint
[col
][ACOMP
] = 1;
385 _mesa_pack_rgba_span_int(ctx
, width
, rgba_uint
,
388 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
390 if (texImage
->_BaseFormat
== GL_ALPHA
) {
392 for (col
= 0; col
< width
; col
++) {
393 rgba
[col
][RCOMP
] = 0.0F
;
394 rgba
[col
][GCOMP
] = 0.0F
;
395 rgba
[col
][BCOMP
] = 0.0F
;
398 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
400 for (col
= 0; col
< width
; col
++) {
401 rgba
[col
][GCOMP
] = 0.0F
;
402 rgba
[col
][BCOMP
] = 0.0F
;
403 rgba
[col
][ACOMP
] = 1.0F
;
406 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
408 for (col
= 0; col
< width
; col
++) {
409 rgba
[col
][GCOMP
] = 0.0F
;
410 rgba
[col
][BCOMP
] = 0.0F
;
413 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
415 for (col
= 0; col
< width
; col
++) {
416 rgba
[col
][GCOMP
] = 0.0F
;
417 rgba
[col
][BCOMP
] = 0.0F
;
418 rgba
[col
][ACOMP
] = 1.0F
;
422 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
424 &ctx
->Pack
, transferOps
);
428 /* Unmap the src texture buffer */
429 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
432 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
442 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
443 * Compressed textures are handled here as well.
446 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
447 GLenum format
, GLenum type
, GLvoid
*pixels
,
448 struct gl_texture_image
*texImage
)
450 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
451 GLbitfield transferOps
= 0x0;
453 /* In general, clamping does not apply to glGetTexImage, except when
454 * the returned type of the image can't hold negative values.
456 if (type_needs_clamping(type
)) {
457 /* the returned image type can't have negative values */
458 if (dataType
== GL_FLOAT
||
459 dataType
== GL_SIGNED_NORMALIZED
||
460 format
== GL_LUMINANCE
||
461 format
== GL_LUMINANCE_ALPHA
) {
462 transferOps
|= IMAGE_CLAMP_BIT
;
466 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
467 get_tex_rgba_compressed(ctx
, dimensions
, format
, type
,
468 pixels
, texImage
, transferOps
);
471 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
472 pixels
, texImage
, transferOps
);
478 * Try to do glGetTexImage() with simple memcpy().
479 * \return GL_TRUE if done, GL_FALSE otherwise
482 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
484 struct gl_texture_image
*texImage
)
486 const GLenum target
= texImage
->TexObject
->Target
;
487 GLboolean memCopy
= GL_FALSE
;
490 * Check if the src/dst formats are compatible.
491 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
492 * so we don't have to worry about those.
493 * XXX more format combinations could be supported here.
495 if (target
== GL_TEXTURE_1D
||
496 target
== GL_TEXTURE_2D
||
497 target
== GL_TEXTURE_RECTANGLE
||
498 _mesa_is_cube_face(target
)) {
499 if ((texImage
->TexFormat
== MESA_FORMAT_ARGB8888
||
500 texImage
->TexFormat
== MESA_FORMAT_SARGB8
) &&
502 (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
503 !ctx
->Pack
.SwapBytes
&&
504 _mesa_little_endian()) {
507 else if ((texImage
->TexFormat
== MESA_FORMAT_AL88
||
508 texImage
->TexFormat
== MESA_FORMAT_SLA8
) &&
509 format
== GL_LUMINANCE_ALPHA
&&
510 type
== GL_UNSIGNED_BYTE
&&
511 !ctx
->Pack
.SwapBytes
&&
512 _mesa_little_endian()) {
515 else if ((texImage
->TexFormat
== MESA_FORMAT_L8
||
516 texImage
->TexFormat
== MESA_FORMAT_SL8
) &&
517 format
== GL_LUMINANCE
&&
518 type
== GL_UNSIGNED_BYTE
) {
521 else if (texImage
->TexFormat
== MESA_FORMAT_L16
&&
522 format
== GL_LUMINANCE
&&
523 type
== GL_UNSIGNED_SHORT
) {
526 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
527 format
== GL_ALPHA
&&
528 type
== GL_UNSIGNED_BYTE
) {
531 else if (texImage
->TexFormat
== MESA_FORMAT_A16
&&
532 format
== GL_ALPHA
&&
533 type
== GL_UNSIGNED_SHORT
) {
539 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
540 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
542 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
543 texImage
->Height
, format
, type
, 0, 0);
544 const GLint dstRowStride
=
545 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
549 /* map src texture buffer */
550 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
551 0, 0, texImage
->Width
, texImage
->Height
,
552 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
555 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
556 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
560 for (row
= 0; row
< texImage
->Height
; row
++) {
561 memcpy(dst
, src
, bytesPerRow
);
567 /* unmap src texture buffer */
568 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
571 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
580 * This is the software fallback for Driver.GetTexImage().
581 * All error checking will have been done before this routine is called.
582 * We'll call ctx->Driver.MapTextureImage() to access the data, then
583 * unmap with ctx->Driver.UnmapTextureImage().
586 _mesa_get_teximage(struct gl_context
*ctx
,
587 GLenum format
, GLenum type
, GLvoid
*pixels
,
588 struct gl_texture_image
*texImage
)
592 switch (texImage
->TexObject
->Target
) {
603 /* map dest buffer, if PBO */
604 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
605 /* Packing texture image into a PBO.
606 * Map the (potentially) VRAM-based buffer into our process space so
607 * we can write into it with the code below.
608 * A hardware driver might use a sophisticated blit to move the
609 * texture data to the PBO if the PBO is in VRAM along with the texture.
611 GLubyte
*buf
= (GLubyte
*)
612 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
613 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
615 /* out of memory or other unexpected error */
616 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
619 /* <pixels> was an offset into the PBO.
620 * Now make it a real, client-side pointer inside the mapped region.
622 pixels
= ADD_POINTERS(buf
, pixels
);
625 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
628 else if (format
== GL_DEPTH_COMPONENT
) {
629 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
631 else if (format
== GL_DEPTH_STENCIL_EXT
) {
632 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
634 else if (format
== GL_YCBCR_MESA
) {
635 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
638 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
641 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
642 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
649 * This is the software fallback for Driver.GetCompressedTexImage().
650 * All error checking will have been done before this routine is called.
653 _mesa_get_compressed_teximage(struct gl_context
*ctx
,
654 struct gl_texture_image
*texImage
,
657 const GLuint row_stride
=
658 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width
);
663 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
664 /* pack texture image into a PBO */
665 GLubyte
*buf
= (GLubyte
*)
666 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
667 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
669 /* out of memory or other unexpected error */
670 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
671 "glGetCompresssedTexImage(map PBO failed)");
674 img
= ADD_POINTERS(buf
, img
);
677 /* map src texture buffer */
678 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
679 0, 0, texImage
->Width
, texImage
->Height
,
680 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
683 /* no pixelstore or pixel transfer, but respect stride */
685 if (row_stride
== srcRowStride
) {
686 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
690 memcpy(img
, src
, size
);
694 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
695 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
696 memcpy((GLubyte
*)img
+ i
* row_stride
,
697 (GLubyte
*)src
+ i
* srcRowStride
,
702 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
705 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetCompresssedTexImage");
708 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
709 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
716 * Do error checking for a glGetTexImage() call.
717 * \return GL_TRUE if any error, GL_FALSE if no errors.
720 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
721 GLenum format
, GLenum type
, GLsizei clientMemSize
,
724 struct gl_texture_object
*texObj
;
725 struct gl_texture_image
*texImage
;
726 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
727 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
730 if (maxLevels
== 0) {
731 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
735 if (level
< 0 || level
>= maxLevels
) {
736 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
740 if (_mesa_sizeof_packed_type(type
) <= 0) {
741 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
745 if (_mesa_components_in_format(format
) <= 0 ||
746 format
== GL_STENCIL_INDEX
||
747 format
== GL_COLOR_INDEX
) {
748 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
752 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
753 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
757 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
758 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
762 if (!ctx
->Extensions
.EXT_packed_depth_stencil
763 && _mesa_is_depthstencil_format(format
)) {
764 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
768 if (!ctx
->Extensions
.ATI_envmap_bumpmap
769 && _mesa_is_dudv_format(format
)) {
770 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
774 texObj
= _mesa_get_current_tex_object(ctx
, target
);
776 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
777 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
781 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
782 /* GL_INVALID_OPERATION is generated by a format/type
783 * mismatch (see the 1.2 spec page 94, sec 3.6.4.)
785 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(target)");
789 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
791 /* non-existant texture image */
795 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
797 /* Make sure the requested image format is compatible with the
800 if (_mesa_is_color_format(format
)
801 && !_mesa_is_color_format(baseFormat
)) {
802 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
805 else if (_mesa_is_depth_format(format
)
806 && !_mesa_is_depth_format(baseFormat
)
807 && !_mesa_is_depthstencil_format(baseFormat
)) {
808 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
811 else if (_mesa_is_ycbcr_format(format
)
812 && !_mesa_is_ycbcr_format(baseFormat
)) {
813 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
816 else if (_mesa_is_depthstencil_format(format
)
817 && !_mesa_is_depthstencil_format(baseFormat
)) {
818 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
821 else if (_mesa_is_dudv_format(format
)
822 && !_mesa_is_dudv_format(baseFormat
)) {
823 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
827 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
828 texImage
->Height
, texImage
->Depth
,
829 format
, type
, clientMemSize
, pixels
)) {
830 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
831 _mesa_error(ctx
, GL_INVALID_OPERATION
,
832 "glGetTexImage(out of bounds PBO access)");
834 _mesa_error(ctx
, GL_INVALID_OPERATION
,
835 "glGetnTexImageARB(out of bounds access:"
836 " bufSize (%d) is too small)", clientMemSize
);
841 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
842 /* PBO should not be mapped */
843 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
844 _mesa_error(ctx
, GL_INVALID_OPERATION
,
845 "glGetTexImage(PBO is mapped)");
856 * Get texture image. Called by glGetTexImage.
858 * \param target texture target.
859 * \param level image level.
860 * \param format pixel data format for returned image.
861 * \param type pixel data type for returned image.
862 * \param bufSize size of the pixels data buffer.
863 * \param pixels returned pixel data.
866 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
867 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
869 struct gl_texture_object
*texObj
;
870 struct gl_texture_image
*texImage
;
871 GET_CURRENT_CONTEXT(ctx
);
872 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
874 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
879 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
880 /* not an error, do nothing */
884 texObj
= _mesa_get_current_tex_object(ctx
, target
);
885 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
887 if (_mesa_is_zero_size_texture(texImage
))
890 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
891 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
892 " dstFmt=0x%x, dstType=0x%x\n",
894 _mesa_get_format_name(texImage
->TexFormat
),
895 texImage
->Width
, texImage
->Height
,
899 _mesa_lock_texture(ctx
, texObj
);
901 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
903 _mesa_unlock_texture(ctx
, texObj
);
908 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
909 GLenum type
, GLvoid
*pixels
)
911 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);
916 * Do error checking for a glGetCompressedTexImage() call.
917 * \return GL_TRUE if any error, GL_FALSE if no errors.
920 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
921 GLint level
, GLsizei clientMemSize
, GLvoid
*img
)
923 struct gl_texture_object
*texObj
;
924 struct gl_texture_image
*texImage
;
925 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
926 GLuint compressedSize
;
928 if (maxLevels
== 0) {
929 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
934 if (level
< 0 || level
>= maxLevels
) {
935 _mesa_error(ctx
, GL_INVALID_VALUE
,
936 "glGetCompressedTexImageARB(bad level = %d)", level
);
940 if (_mesa_is_proxy_texture(target
)) {
941 _mesa_error(ctx
, GL_INVALID_ENUM
,
942 "glGetCompressedTexImageARB(bad target = %s)",
943 _mesa_lookup_enum_by_nr(target
));
947 texObj
= _mesa_get_current_tex_object(ctx
, target
);
949 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
953 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
956 /* probably invalid mipmap level */
957 _mesa_error(ctx
, GL_INVALID_VALUE
,
958 "glGetCompressedTexImageARB(level)");
962 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
963 _mesa_error(ctx
, GL_INVALID_OPERATION
,
964 "glGetCompressedTexImageARB(texture is not compressed)");
968 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
973 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
974 /* do bounds checking on writing to client memory */
975 if (clientMemSize
< compressedSize
) {
976 _mesa_error(ctx
, GL_INVALID_OPERATION
,
977 "glGetnCompressedTexImageARB(out of bounds access:"
978 " bufSize (%d) is too small)", clientMemSize
);
982 /* do bounds checking on PBO write */
983 if ((const GLubyte
*) img
+ compressedSize
>
984 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
985 _mesa_error(ctx
, GL_INVALID_OPERATION
,
986 "glGetCompressedTexImage(out of bounds PBO access)");
990 /* make sure PBO is not mapped */
991 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
992 _mesa_error(ctx
, GL_INVALID_OPERATION
,
993 "glGetCompressedTexImage(PBO is mapped)");
1003 _mesa_GetnCompressedTexImageARB(GLenum target
, GLint level
, GLsizei bufSize
,
1006 struct gl_texture_object
*texObj
;
1007 struct gl_texture_image
*texImage
;
1008 GET_CURRENT_CONTEXT(ctx
);
1009 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1011 if (getcompressedteximage_error_check(ctx
, target
, level
, bufSize
, img
)) {
1015 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
1016 /* not an error, do nothing */
1020 texObj
= _mesa_get_current_tex_object(ctx
, target
);
1021 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1023 if (_mesa_is_zero_size_texture(texImage
))
1026 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
1028 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1030 _mesa_get_format_name(texImage
->TexFormat
),
1031 texImage
->Width
, texImage
->Height
);
1034 _mesa_lock_texture(ctx
, texObj
);
1036 ctx
->Driver
.GetCompressedTexImage(ctx
, texImage
, img
);
1038 _mesa_unlock_texture(ctx
, texObj
);
1042 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
1044 _mesa_GetnCompressedTexImageARB(target
, level
, INT_MAX
, img
);