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"
38 #include "mfeatures.h"
42 #include "texgetimage.h"
48 * Can the given type represent negative values?
50 static INLINE GLboolean
51 type_with_negative_values(GLenum type
)
58 case GL_HALF_FLOAT_ARB
:
67 * glGetTexImage for color index pixels.
70 get_tex_color_index(struct gl_context
*ctx
, GLuint dimensions
,
71 GLenum format
, GLenum type
, GLvoid
*pixels
,
72 const struct gl_texture_image
*texImage
)
74 const GLint width
= texImage
->Width
;
75 const GLint height
= texImage
->Height
;
76 const GLint depth
= texImage
->Depth
;
77 const GLuint indexBits
=
78 _mesa_get_format_bits(texImage
->TexFormat
, GL_TEXTURE_INDEX_SIZE_EXT
);
79 const GLbitfield transferOps
= 0x0;
82 for (img
= 0; img
< depth
; img
++) {
83 for (row
= 0; row
< height
; row
++) {
84 GLuint indexRow
[MAX_WIDTH
] = { 0 };
85 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
86 width
, height
, format
, type
,
91 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
92 src
+= width
* (img
* texImage
->Height
+ row
);
93 for (col
= 0; col
< width
; col
++) {
94 indexRow
[col
] = src
[col
];
97 else if (indexBits
== 16) {
98 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
99 src
+= width
* (img
* texImage
->Height
+ row
);
100 for (col
= 0; col
< width
; col
++) {
101 indexRow
[col
] = src
[col
];
105 _mesa_problem(ctx
, "Color index problem in _mesa_GetTexImage");
107 _mesa_pack_index_span(ctx
, width
, type
, dest
,
108 indexRow
, &ctx
->Pack
, transferOps
);
115 * glGetTexImage for depth/Z pixels.
118 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
119 GLenum format
, GLenum type
, GLvoid
*pixels
,
120 const struct gl_texture_image
*texImage
)
122 const GLint width
= texImage
->Width
;
123 const GLint height
= texImage
->Height
;
124 const GLint depth
= texImage
->Depth
;
126 GLfloat
*depthRow
= (GLfloat
*) malloc(width
* sizeof(GLfloat
));
129 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
133 for (img
= 0; img
< depth
; img
++) {
134 for (row
= 0; row
< height
; row
++) {
135 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
136 width
, height
, format
, type
,
140 for (col
= 0; col
< width
; col
++) {
141 texImage
->FetchTexelf(texImage
, col
, row
, img
, depthRow
+ col
);
143 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
152 * glGetTexImage for depth/stencil pixels.
155 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
156 GLenum format
, GLenum type
, GLvoid
*pixels
,
157 const struct gl_texture_image
*texImage
)
159 const GLint width
= texImage
->Width
;
160 const GLint height
= texImage
->Height
;
161 const GLint depth
= texImage
->Depth
;
162 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
165 for (img
= 0; img
< depth
; img
++) {
166 for (row
= 0; row
< height
; row
++) {
167 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
168 width
, height
, format
, type
,
170 memcpy(dest
, src
, width
* sizeof(GLuint
));
171 if (ctx
->Pack
.SwapBytes
) {
172 _mesa_swap4((GLuint
*) dest
, width
);
175 src
+= width
* row
+ width
* height
* img
;
182 * glGetTexImage for YCbCr pixels.
185 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
186 GLenum format
, GLenum type
, GLvoid
*pixels
,
187 const struct gl_texture_image
*texImage
)
189 const GLint width
= texImage
->Width
;
190 const GLint height
= texImage
->Height
;
191 const GLint depth
= texImage
->Depth
;
192 const GLint rowstride
= texImage
->RowStride
;
193 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
196 for (img
= 0; img
< depth
; img
++) {
197 for (row
= 0; row
< height
; row
++) {
198 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
199 width
, height
, format
, type
,
201 memcpy(dest
, src
, width
* sizeof(GLushort
));
203 /* check for byte swapping */
204 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
205 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
206 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
207 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
208 if (!ctx
->Pack
.SwapBytes
)
209 _mesa_swap2((GLushort
*) dest
, width
);
211 else if (ctx
->Pack
.SwapBytes
) {
212 _mesa_swap2((GLushort
*) dest
, width
);
221 #if FEATURE_EXT_texture_sRGB
225 * Convert a float value from linear space to a
226 * non-linear sRGB value in [0, 255].
227 * Not terribly efficient.
229 static INLINE GLfloat
230 linear_to_nonlinear(GLfloat cl
)
232 /* can't have values outside [0, 1] */
234 if (cl
< 0.0031308f
) {
238 cs
= (GLfloat
)(1.055 * pow(cl
, 0.41666) - 0.055);
245 * glGetTexImagefor sRGB pixels;
248 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
249 GLenum format
, GLenum type
, GLvoid
*pixels
,
250 const struct gl_texture_image
*texImage
)
252 const GLint width
= texImage
->Width
;
253 const GLint height
= texImage
->Height
;
254 const GLint depth
= texImage
->Depth
;
255 const GLbitfield transferOps
= 0x0;
257 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
260 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
264 for (img
= 0; img
< depth
; img
++) {
265 for (row
= 0; row
< height
; row
++) {
266 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
267 width
, height
, format
, type
,
272 /* convert row to RGBA format */
273 for (col
= 0; col
< width
; col
++) {
274 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
275 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
276 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
277 rgba
[col
][GCOMP
] = 0.0;
278 rgba
[col
][BCOMP
] = 0.0;
280 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
281 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
282 rgba
[col
][GCOMP
] = 0.0;
283 rgba
[col
][BCOMP
] = 0.0;
285 else if (texImage
->_BaseFormat
== GL_RGB
||
286 texImage
->_BaseFormat
== GL_RGBA
) {
287 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
288 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
289 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
292 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
294 &ctx
->Pack
, transferOps
);
302 #else /* FEATURE_EXT_texture_sRGB */
306 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
307 GLenum format
, GLenum type
, GLvoid
*pixels
,
308 const struct gl_texture_image
*texImage
)
314 #endif /* FEATURE_EXT_texture_sRGB */
318 * glGetTexImagefor RGBA, Luminance, etc. pixels.
319 * This is the slow way since we use texture sampling.
322 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
323 GLenum format
, GLenum type
, GLvoid
*pixels
,
324 const struct gl_texture_image
*texImage
)
326 const GLint width
= texImage
->Width
;
327 const GLint height
= texImage
->Height
;
328 const GLint depth
= texImage
->Depth
;
329 /* Normally, no pixel transfer ops are performed during glGetTexImage.
330 * The only possible exception is component clamping to [0,1].
332 GLbitfield transferOps
= 0x0;
334 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
337 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
341 for (img
= 0; img
< depth
; img
++) {
342 for (row
= 0; row
< height
; row
++) {
343 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
344 width
, height
, format
, type
,
347 GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
349 /* clamp does not apply to GetTexImage (final conversion)?
350 * Looks like we need clamp though when going from format
351 * containing negative values to unsigned format.
353 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
) {
354 transferOps
|= IMAGE_CLAMP_BIT
;
356 else if (!type_with_negative_values(type
) &&
357 (dataType
== GL_FLOAT
||
358 dataType
== GL_SIGNED_NORMALIZED
)) {
359 transferOps
|= IMAGE_CLAMP_BIT
;
362 for (col
= 0; col
< width
; col
++) {
363 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
364 if (texImage
->_BaseFormat
== GL_ALPHA
) {
365 rgba
[col
][RCOMP
] = 0.0F
;
366 rgba
[col
][GCOMP
] = 0.0F
;
367 rgba
[col
][BCOMP
] = 0.0F
;
369 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
370 rgba
[col
][GCOMP
] = 0.0F
;
371 rgba
[col
][BCOMP
] = 0.0F
;
372 rgba
[col
][ACOMP
] = 1.0F
;
374 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
375 rgba
[col
][GCOMP
] = 0.0F
;
376 rgba
[col
][BCOMP
] = 0.0F
;
378 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
379 rgba
[col
][GCOMP
] = 0.0F
;
380 rgba
[col
][BCOMP
] = 0.0F
;
381 rgba
[col
][ACOMP
] = 1.0F
;
384 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
386 &ctx
->Pack
, transferOps
);
395 * Try to do glGetTexImage() with simple memcpy().
396 * \return GL_TRUE if done, GL_FALSE otherwise
399 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
, GLvoid
*pixels
,
400 const struct gl_texture_object
*texObj
,
401 const struct gl_texture_image
*texImage
)
403 GLboolean memCopy
= GL_FALSE
;
405 /* Texture image should have been mapped already */
406 assert(texImage
->Data
);
409 * Check if the src/dst formats are compatible.
410 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
411 * so we don't have to worry about those.
412 * XXX more format combinations could be supported here.
414 if ((texObj
->Target
== GL_TEXTURE_1D
||
415 texObj
->Target
== GL_TEXTURE_2D
||
416 texObj
->Target
== GL_TEXTURE_RECTANGLE
||
417 (texObj
->Target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
418 texObj
->Target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
419 if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
&&
421 type
== GL_UNSIGNED_BYTE
&&
422 !ctx
->Pack
.SwapBytes
&&
423 _mesa_little_endian()) {
426 else if (texImage
->TexFormat
== MESA_FORMAT_AL88
&&
427 format
== GL_LUMINANCE_ALPHA
&&
428 type
== GL_UNSIGNED_BYTE
&&
429 !ctx
->Pack
.SwapBytes
&&
430 _mesa_little_endian()) {
433 else if (texImage
->TexFormat
== MESA_FORMAT_L8
&&
434 format
== GL_LUMINANCE
&&
435 type
== GL_UNSIGNED_BYTE
) {
438 else if (texImage
->TexFormat
== MESA_FORMAT_L16
&&
439 format
== GL_LUMINANCE
&&
440 type
== GL_UNSIGNED_SHORT
) {
443 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
444 format
== GL_ALPHA
&&
445 type
== GL_UNSIGNED_BYTE
) {
448 else if (texImage
->TexFormat
== MESA_FORMAT_A16
&&
449 format
== GL_ALPHA
&&
450 type
== GL_UNSIGNED_SHORT
) {
456 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
457 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
459 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
460 texImage
->Height
, format
, type
, 0, 0);
461 const GLint dstRowStride
=
462 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
463 const GLubyte
*src
= texImage
->Data
;
464 const GLint srcRowStride
= texImage
->RowStride
* bpp
;
467 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
468 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
471 for (row
= 0; row
< texImage
->Height
; row
++) {
472 memcpy(dst
, src
, bytesPerRow
);
484 * This is the software fallback for Driver.GetTexImage().
485 * All error checking will have been done before this routine is called.
486 * The texture image must be mapped.
489 _mesa_get_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
490 GLenum format
, GLenum type
, GLvoid
*pixels
,
491 struct gl_texture_object
*texObj
,
492 struct gl_texture_image
*texImage
)
496 /* If we get here, the texture image should be mapped */
497 assert(texImage
->Data
);
510 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
511 /* Packing texture image into a PBO.
512 * Map the (potentially) VRAM-based buffer into our process space so
513 * we can write into it with the code below.
514 * A hardware driver might use a sophisticated blit to move the
515 * texture data to the PBO if the PBO is in VRAM along with the texture.
517 GLubyte
*buf
= (GLubyte
*)
518 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
519 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
521 /* out of memory or other unexpected error */
522 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
525 /* <pixels> was an offset into the PBO.
526 * Now make it a real, client-side pointer inside the mapped region.
528 pixels
= ADD_POINTERS(buf
, pixels
);
531 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texObj
, texImage
)) {
534 else if (format
== GL_COLOR_INDEX
) {
535 get_tex_color_index(ctx
, dimensions
, format
, type
, pixels
, texImage
);
537 else if (format
== GL_DEPTH_COMPONENT
) {
538 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
540 else if (format
== GL_DEPTH_STENCIL_EXT
) {
541 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
543 else if (format
== GL_YCBCR_MESA
) {
544 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
546 else if (_mesa_get_format_color_encoding(texImage
->TexFormat
) == GL_SRGB
) {
547 get_tex_srgb(ctx
, dimensions
, format
, type
, pixels
, texImage
);
550 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
553 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
554 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
555 ctx
->Pack
.BufferObj
);
562 * This is the software fallback for Driver.GetCompressedTexImage().
563 * All error checking will have been done before this routine is called.
566 _mesa_get_compressed_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
568 struct gl_texture_object
*texObj
,
569 struct gl_texture_image
*texImage
)
571 const GLuint row_stride
= _mesa_format_row_stride(texImage
->TexFormat
,
573 const GLuint row_stride_stored
= _mesa_format_row_stride(texImage
->TexFormat
,
574 texImage
->RowStride
);
577 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
578 /* pack texture image into a PBO */
579 GLubyte
*buf
= (GLubyte
*)
580 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
581 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
583 /* out of memory or other unexpected error */
584 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
585 "glGetCompresssedTexImage(map PBO failed)");
588 img
= ADD_POINTERS(buf
, img
);
591 /* no pixelstore or pixel transfer, but respect stride */
593 if (row_stride
== row_stride_stored
) {
594 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
598 memcpy(img
, texImage
->Data
, size
);
602 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
603 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
604 memcpy((GLubyte
*)img
+ i
* row_stride
,
605 (GLubyte
*)texImage
->Data
+ i
* row_stride_stored
,
610 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
611 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
612 ctx
->Pack
.BufferObj
);
619 * Do error checking for a glGetTexImage() call.
620 * \return GL_TRUE if any error, GL_FALSE if no errors.
623 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
624 GLenum format
, GLenum type
, GLvoid
*pixels
)
626 struct gl_texture_object
*texObj
;
627 struct gl_texture_image
*texImage
;
628 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
631 if (maxLevels
== 0) {
632 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
636 if (level
< 0 || level
>= maxLevels
) {
637 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
641 if (_mesa_sizeof_packed_type(type
) <= 0) {
642 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
646 if (_mesa_components_in_format(format
) <= 0 ||
647 format
== GL_STENCIL_INDEX
) {
648 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
652 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
653 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
657 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
658 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
662 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
663 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
667 if (!ctx
->Extensions
.EXT_packed_depth_stencil
668 && _mesa_is_depthstencil_format(format
)) {
669 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
673 if (!ctx
->Extensions
.ATI_envmap_bumpmap
674 && _mesa_is_dudv_format(format
)) {
675 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
679 texObj
= _mesa_get_current_tex_object(ctx
, target
);
681 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
682 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
686 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
692 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
694 /* Make sure the requested image format is compatible with the
695 * texture's format. Note that a color index texture can be converted
696 * to RGBA so that combo is allowed.
698 if (_mesa_is_color_format(format
)
699 && !_mesa_is_color_format(baseFormat
)
700 && !_mesa_is_index_format(baseFormat
)) {
701 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
704 else if (_mesa_is_index_format(format
)
705 && !_mesa_is_index_format(baseFormat
)) {
706 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
709 else if (_mesa_is_depth_format(format
)
710 && !_mesa_is_depth_format(baseFormat
)
711 && !_mesa_is_depthstencil_format(baseFormat
)) {
712 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
715 else if (_mesa_is_ycbcr_format(format
)
716 && !_mesa_is_ycbcr_format(baseFormat
)) {
717 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
720 else if (_mesa_is_depthstencil_format(format
)
721 && !_mesa_is_depthstencil_format(baseFormat
)) {
722 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
725 else if (_mesa_is_dudv_format(format
)
726 && !_mesa_is_dudv_format(baseFormat
)) {
727 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
731 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
732 /* packing texture image into a PBO */
733 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
734 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
735 texImage
->Height
, texImage
->Depth
,
736 format
, type
, pixels
)) {
737 _mesa_error(ctx
, GL_INVALID_OPERATION
,
738 "glGetTexImage(out of bounds PBO write)");
742 /* PBO should not be mapped */
743 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
744 _mesa_error(ctx
, GL_INVALID_OPERATION
,
745 "glGetTexImage(PBO is mapped)");
756 * Get texture image. Called by glGetTexImage.
758 * \param target texture target.
759 * \param level image level.
760 * \param format pixel data format for returned image.
761 * \param type pixel data type for returned image.
762 * \param pixels returned pixel data.
765 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
766 GLenum type
, GLvoid
*pixels
)
768 struct gl_texture_object
*texObj
;
769 struct gl_texture_image
*texImage
;
770 GET_CURRENT_CONTEXT(ctx
);
771 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
773 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
777 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
778 /* not an error, do nothing */
782 texObj
= _mesa_get_current_tex_object(ctx
, target
);
783 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
785 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
786 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
787 " dstFmt=0x%x, dstType=0x%x\n",
789 _mesa_get_format_name(texImage
->TexFormat
),
790 texImage
->Width
, texImage
->Height
,
794 _mesa_lock_texture(ctx
, texObj
);
796 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
799 _mesa_unlock_texture(ctx
, texObj
);
805 * Do error checking for a glGetCompressedTexImage() call.
806 * \return GL_TRUE if any error, GL_FALSE if no errors.
809 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
,
810 GLint level
, GLvoid
*img
)
812 struct gl_texture_object
*texObj
;
813 struct gl_texture_image
*texImage
;
814 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
816 if (maxLevels
== 0) {
817 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
822 if (level
< 0 || level
>= maxLevels
) {
823 _mesa_error(ctx
, GL_INVALID_VALUE
,
824 "glGetCompressedTexImageARB(bad level = %d)", level
);
828 if (_mesa_is_proxy_texture(target
)) {
829 _mesa_error(ctx
, GL_INVALID_ENUM
,
830 "glGetCompressedTexImageARB(bad target = %s)",
831 _mesa_lookup_enum_by_nr(target
));
835 texObj
= _mesa_get_current_tex_object(ctx
, target
);
837 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
841 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
844 /* probably invalid mipmap level */
845 _mesa_error(ctx
, GL_INVALID_VALUE
,
846 "glGetCompressedTexImageARB(level)");
850 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
851 _mesa_error(ctx
, GL_INVALID_OPERATION
,
852 "glGetCompressedTexImageARB(texture is not compressed)");
856 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
857 GLuint compressedSize
;
859 /* make sure PBO is not mapped */
860 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
861 _mesa_error(ctx
, GL_INVALID_OPERATION
,
862 "glGetCompressedTexImage(PBO is mapped)");
866 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
871 /* do bounds checking on PBO write */
872 if ((const GLubyte
*) img
+ compressedSize
>
873 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
874 _mesa_error(ctx
, GL_INVALID_OPERATION
,
875 "glGetCompressedTexImage(out of bounds PBO write)");
885 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
887 struct gl_texture_object
*texObj
;
888 struct gl_texture_image
*texImage
;
889 GET_CURRENT_CONTEXT(ctx
);
890 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
892 if (getcompressedteximage_error_check(ctx
, target
, level
, img
)) {
896 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
897 /* not an error, do nothing */
901 texObj
= _mesa_get_current_tex_object(ctx
, target
);
902 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
904 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
906 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
908 _mesa_get_format_name(texImage
->TexFormat
),
909 texImage
->Width
, texImage
->Height
);
912 _mesa_lock_texture(ctx
, texObj
);
914 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
917 _mesa_unlock_texture(ctx
, texObj
);