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"
41 #include "texgetimage.h"
47 * Can the given type represent negative values?
49 static INLINE GLboolean
50 type_with_negative_values(GLenum type
)
57 case GL_HALF_FLOAT_ARB
:
66 * glGetTexImage for color index pixels.
69 get_tex_color_index(struct gl_context
*ctx
, GLuint dimensions
,
70 GLenum format
, GLenum type
, GLvoid
*pixels
,
71 const struct gl_texture_image
*texImage
)
73 const GLint width
= texImage
->Width
;
74 const GLint height
= texImage
->Height
;
75 const GLint depth
= texImage
->Depth
;
76 const GLuint indexBits
=
77 _mesa_get_format_bits(texImage
->TexFormat
, GL_TEXTURE_INDEX_SIZE_EXT
);
78 const GLbitfield transferOps
= 0x0;
81 for (img
= 0; img
< depth
; img
++) {
82 for (row
= 0; row
< height
; row
++) {
83 GLuint indexRow
[MAX_WIDTH
] = { 0 };
84 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
85 width
, height
, format
, type
,
90 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
91 src
+= width
* (img
* texImage
->Height
+ row
);
92 for (col
= 0; col
< width
; col
++) {
93 indexRow
[col
] = src
[col
];
96 else if (indexBits
== 16) {
97 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
98 src
+= width
* (img
* texImage
->Height
+ row
);
99 for (col
= 0; col
< width
; col
++) {
100 indexRow
[col
] = src
[col
];
104 _mesa_problem(ctx
, "Color index problem in _mesa_GetTexImage");
106 _mesa_pack_index_span(ctx
, width
, type
, dest
,
107 indexRow
, &ctx
->Pack
, transferOps
);
114 * glGetTexImage for depth/Z pixels.
117 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
118 GLenum format
, GLenum type
, GLvoid
*pixels
,
119 const struct gl_texture_image
*texImage
)
121 const GLint width
= texImage
->Width
;
122 const GLint height
= texImage
->Height
;
123 const GLint depth
= texImage
->Depth
;
125 GLfloat
*depthRow
= (GLfloat
*) malloc(width
* sizeof(GLfloat
));
128 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
132 for (img
= 0; img
< depth
; img
++) {
133 for (row
= 0; row
< height
; row
++) {
134 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
135 width
, height
, format
, type
,
139 for (col
= 0; col
< width
; col
++) {
140 texImage
->FetchTexelf(texImage
, col
, row
, img
, depthRow
+ col
);
142 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
151 * glGetTexImage for depth/stencil pixels.
154 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
155 GLenum format
, GLenum type
, GLvoid
*pixels
,
156 const struct gl_texture_image
*texImage
)
158 const GLint width
= texImage
->Width
;
159 const GLint height
= texImage
->Height
;
160 const GLint depth
= texImage
->Depth
;
161 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
164 for (img
= 0; img
< depth
; img
++) {
165 for (row
= 0; row
< height
; row
++) {
166 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
167 width
, height
, format
, type
,
169 memcpy(dest
, src
, width
* sizeof(GLuint
));
170 if (ctx
->Pack
.SwapBytes
) {
171 _mesa_swap4((GLuint
*) dest
, width
);
174 src
+= width
* row
+ width
* height
* img
;
181 * glGetTexImage for YCbCr pixels.
184 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
185 GLenum format
, GLenum type
, GLvoid
*pixels
,
186 const struct gl_texture_image
*texImage
)
188 const GLint width
= texImage
->Width
;
189 const GLint height
= texImage
->Height
;
190 const GLint depth
= texImage
->Depth
;
191 const GLint rowstride
= texImage
->RowStride
;
192 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
195 for (img
= 0; img
< depth
; img
++) {
196 for (row
= 0; row
< height
; row
++) {
197 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
198 width
, height
, format
, type
,
200 memcpy(dest
, src
, width
* sizeof(GLushort
));
202 /* check for byte swapping */
203 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
204 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
205 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
206 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
207 if (!ctx
->Pack
.SwapBytes
)
208 _mesa_swap2((GLushort
*) dest
, width
);
210 else if (ctx
->Pack
.SwapBytes
) {
211 _mesa_swap2((GLushort
*) dest
, width
);
220 #if FEATURE_EXT_texture_sRGB
224 * Convert a float value from linear space to a
225 * non-linear sRGB value in [0, 255].
226 * Not terribly efficient.
228 static INLINE GLfloat
229 linear_to_nonlinear(GLfloat cl
)
231 /* can't have values outside [0, 1] */
233 if (cl
< 0.0031308f
) {
237 cs
= (GLfloat
)(1.055 * pow(cl
, 0.41666) - 0.055);
244 * glGetTexImagefor sRGB pixels;
247 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
248 GLenum format
, GLenum type
, GLvoid
*pixels
,
249 const struct gl_texture_image
*texImage
)
251 const GLint width
= texImage
->Width
;
252 const GLint height
= texImage
->Height
;
253 const GLint depth
= texImage
->Depth
;
254 const GLbitfield transferOps
= 0x0;
256 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
259 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
263 for (img
= 0; img
< depth
; img
++) {
264 for (row
= 0; row
< height
; row
++) {
265 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
266 width
, height
, format
, type
,
271 /* convert row to RGBA format */
272 for (col
= 0; col
< width
; col
++) {
273 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
274 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
275 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
276 rgba
[col
][GCOMP
] = 0.0;
277 rgba
[col
][BCOMP
] = 0.0;
279 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
280 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
281 rgba
[col
][GCOMP
] = 0.0;
282 rgba
[col
][BCOMP
] = 0.0;
284 else if (texImage
->_BaseFormat
== GL_RGB
||
285 texImage
->_BaseFormat
== GL_RGBA
) {
286 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
287 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
288 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
291 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
293 &ctx
->Pack
, transferOps
);
301 #else /* FEATURE_EXT_texture_sRGB */
305 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
306 GLenum format
, GLenum type
, GLvoid
*pixels
,
307 const struct gl_texture_image
*texImage
)
313 #endif /* FEATURE_EXT_texture_sRGB */
317 * glGetTexImagefor RGBA, Luminance, etc. pixels.
318 * This is the slow way since we use texture sampling.
321 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
322 GLenum format
, GLenum type
, GLvoid
*pixels
,
323 const struct gl_texture_image
*texImage
)
325 const GLint width
= texImage
->Width
;
326 const GLint height
= texImage
->Height
;
327 const GLint depth
= texImage
->Depth
;
328 /* Normally, no pixel transfer ops are performed during glGetTexImage.
329 * The only possible exception is component clamping to [0,1].
331 GLbitfield transferOps
= 0x0;
333 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
336 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
340 for (img
= 0; img
< depth
; img
++) {
341 for (row
= 0; row
< height
; row
++) {
342 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
343 width
, height
, format
, type
,
346 GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
348 /* clamp does not apply to GetTexImage (final conversion)?
349 * Looks like we need clamp though when going from format
350 * containing negative values to unsigned format.
352 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
) {
353 transferOps
|= IMAGE_CLAMP_BIT
;
355 else if (!type_with_negative_values(type
) &&
356 (dataType
== GL_FLOAT
||
357 dataType
== GL_SIGNED_NORMALIZED
)) {
358 transferOps
|= IMAGE_CLAMP_BIT
;
361 for (col
= 0; col
< width
; col
++) {
362 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
363 if (texImage
->_BaseFormat
== GL_ALPHA
) {
364 rgba
[col
][RCOMP
] = 0.0F
;
365 rgba
[col
][GCOMP
] = 0.0F
;
366 rgba
[col
][BCOMP
] = 0.0F
;
368 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
369 rgba
[col
][GCOMP
] = 0.0F
;
370 rgba
[col
][BCOMP
] = 0.0F
;
371 rgba
[col
][ACOMP
] = 1.0F
;
373 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
374 rgba
[col
][GCOMP
] = 0.0F
;
375 rgba
[col
][BCOMP
] = 0.0F
;
377 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
378 rgba
[col
][GCOMP
] = 0.0F
;
379 rgba
[col
][BCOMP
] = 0.0F
;
380 rgba
[col
][ACOMP
] = 1.0F
;
383 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
385 &ctx
->Pack
, transferOps
);
394 * Try to do glGetTexImage() with simple memcpy().
395 * \return GL_TRUE if done, GL_FALSE otherwise
398 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
, GLvoid
*pixels
,
399 const struct gl_texture_object
*texObj
,
400 const struct gl_texture_image
*texImage
)
402 GLboolean memCopy
= GL_FALSE
;
404 /* Texture image should have been mapped already */
405 assert(texImage
->Data
);
408 * Check if the src/dst formats are compatible.
409 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
410 * so we don't have to worry about those.
411 * XXX more format combinations could be supported here.
413 if ((texObj
->Target
== GL_TEXTURE_1D
||
414 texObj
->Target
== GL_TEXTURE_2D
||
415 texObj
->Target
== GL_TEXTURE_RECTANGLE
||
416 (texObj
->Target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
417 texObj
->Target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
418 if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
&&
420 type
== GL_UNSIGNED_BYTE
&&
421 !ctx
->Pack
.SwapBytes
&&
422 _mesa_little_endian()) {
425 else if (texImage
->TexFormat
== MESA_FORMAT_AL88
&&
426 format
== GL_LUMINANCE_ALPHA
&&
427 type
== GL_UNSIGNED_BYTE
&&
428 !ctx
->Pack
.SwapBytes
&&
429 _mesa_little_endian()) {
432 else if (texImage
->TexFormat
== MESA_FORMAT_L8
&&
433 format
== GL_LUMINANCE
&&
434 type
== GL_UNSIGNED_BYTE
) {
437 else if (texImage
->TexFormat
== MESA_FORMAT_L16
&&
438 format
== GL_LUMINANCE
&&
439 type
== GL_UNSIGNED_SHORT
) {
442 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
443 format
== GL_ALPHA
&&
444 type
== GL_UNSIGNED_BYTE
) {
447 else if (texImage
->TexFormat
== MESA_FORMAT_A16
&&
448 format
== GL_ALPHA
&&
449 type
== GL_UNSIGNED_SHORT
) {
455 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
456 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
458 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
459 texImage
->Height
, format
, type
, 0, 0);
460 const GLint dstRowStride
=
461 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
462 const GLubyte
*src
= texImage
->Data
;
463 const GLint srcRowStride
= texImage
->RowStride
* bpp
;
466 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
467 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
470 for (row
= 0; row
< texImage
->Height
; row
++) {
471 memcpy(dst
, src
, bytesPerRow
);
483 * This is the software fallback for Driver.GetTexImage().
484 * All error checking will have been done before this routine is called.
485 * The texture image must be mapped.
488 _mesa_get_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
489 GLenum format
, GLenum type
, GLvoid
*pixels
,
490 struct gl_texture_object
*texObj
,
491 struct gl_texture_image
*texImage
)
495 /* If we get here, the texture image should be mapped */
496 assert(texImage
->Data
);
509 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
510 /* Packing texture image into a PBO.
511 * Map the (potentially) VRAM-based buffer into our process space so
512 * we can write into it with the code below.
513 * A hardware driver might use a sophisticated blit to move the
514 * texture data to the PBO if the PBO is in VRAM along with the texture.
516 GLubyte
*buf
= (GLubyte
*)
517 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
518 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
520 /* out of memory or other unexpected error */
521 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
524 /* <pixels> was an offset into the PBO.
525 * Now make it a real, client-side pointer inside the mapped region.
527 pixels
= ADD_POINTERS(buf
, pixels
);
530 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texObj
, texImage
)) {
533 else if (format
== GL_COLOR_INDEX
) {
534 get_tex_color_index(ctx
, dimensions
, format
, type
, pixels
, texImage
);
536 else if (format
== GL_DEPTH_COMPONENT
) {
537 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
539 else if (format
== GL_DEPTH_STENCIL_EXT
) {
540 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
542 else if (format
== GL_YCBCR_MESA
) {
543 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
545 else if (_mesa_get_format_color_encoding(texImage
->TexFormat
) == GL_SRGB
) {
546 get_tex_srgb(ctx
, dimensions
, format
, type
, pixels
, texImage
);
549 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
552 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
553 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
554 ctx
->Pack
.BufferObj
);
561 * This is the software fallback for Driver.GetCompressedTexImage().
562 * All error checking will have been done before this routine is called.
565 _mesa_get_compressed_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
567 struct gl_texture_object
*texObj
,
568 struct gl_texture_image
*texImage
)
570 const GLuint row_stride
= _mesa_format_row_stride(texImage
->TexFormat
,
572 const GLuint row_stride_stored
= _mesa_format_row_stride(texImage
->TexFormat
,
573 texImage
->RowStride
);
576 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
577 /* pack texture image into a PBO */
578 GLubyte
*buf
= (GLubyte
*)
579 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
580 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
582 /* out of memory or other unexpected error */
583 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
584 "glGetCompresssedTexImage(map PBO failed)");
587 img
= ADD_POINTERS(buf
, img
);
590 /* no pixelstore or pixel transfer, but respect stride */
592 if (row_stride
== row_stride_stored
) {
593 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
597 memcpy(img
, texImage
->Data
, size
);
601 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
602 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
603 memcpy((GLubyte
*)img
+ i
* row_stride
,
604 (GLubyte
*)texImage
->Data
+ i
* row_stride_stored
,
609 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
610 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
611 ctx
->Pack
.BufferObj
);
618 * Do error checking for a glGetTexImage() call.
619 * \return GL_TRUE if any error, GL_FALSE if no errors.
622 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
623 GLenum format
, GLenum type
, GLvoid
*pixels
)
625 struct gl_texture_object
*texObj
;
626 struct gl_texture_image
*texImage
;
627 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
630 if (maxLevels
== 0) {
631 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
635 if (level
< 0 || level
>= maxLevels
) {
636 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
640 if (_mesa_sizeof_packed_type(type
) <= 0) {
641 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
645 if (_mesa_components_in_format(format
) <= 0 ||
646 format
== GL_STENCIL_INDEX
) {
647 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
651 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
652 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
656 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
657 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
661 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
662 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
666 if (!ctx
->Extensions
.EXT_packed_depth_stencil
667 && _mesa_is_depthstencil_format(format
)) {
668 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
672 if (!ctx
->Extensions
.ATI_envmap_bumpmap
673 && _mesa_is_dudv_format(format
)) {
674 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
678 texObj
= _mesa_get_current_tex_object(ctx
, target
);
680 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
681 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
685 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
691 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
693 /* Make sure the requested image format is compatible with the
694 * texture's format. Note that a color index texture can be converted
695 * to RGBA so that combo is allowed.
697 if (_mesa_is_color_format(format
)
698 && !_mesa_is_color_format(baseFormat
)
699 && !_mesa_is_index_format(baseFormat
)) {
700 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
703 else if (_mesa_is_index_format(format
)
704 && !_mesa_is_index_format(baseFormat
)) {
705 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
708 else if (_mesa_is_depth_format(format
)
709 && !_mesa_is_depth_format(baseFormat
)
710 && !_mesa_is_depthstencil_format(baseFormat
)) {
711 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
714 else if (_mesa_is_ycbcr_format(format
)
715 && !_mesa_is_ycbcr_format(baseFormat
)) {
716 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
719 else if (_mesa_is_depthstencil_format(format
)
720 && !_mesa_is_depthstencil_format(baseFormat
)) {
721 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
724 else if (_mesa_is_dudv_format(format
)
725 && !_mesa_is_dudv_format(baseFormat
)) {
726 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
730 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
731 /* packing texture image into a PBO */
732 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
733 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
734 texImage
->Height
, texImage
->Depth
,
735 format
, type
, pixels
)) {
736 _mesa_error(ctx
, GL_INVALID_OPERATION
,
737 "glGetTexImage(out of bounds PBO write)");
741 /* PBO should not be mapped */
742 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
743 _mesa_error(ctx
, GL_INVALID_OPERATION
,
744 "glGetTexImage(PBO is mapped)");
755 * Get texture image. Called by glGetTexImage.
757 * \param target texture target.
758 * \param level image level.
759 * \param format pixel data format for returned image.
760 * \param type pixel data type for returned image.
761 * \param pixels returned pixel data.
764 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
765 GLenum type
, GLvoid
*pixels
)
767 struct gl_texture_object
*texObj
;
768 struct gl_texture_image
*texImage
;
769 GET_CURRENT_CONTEXT(ctx
);
770 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
772 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
776 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
777 /* not an error, do nothing */
781 texObj
= _mesa_get_current_tex_object(ctx
, target
);
782 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
784 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
785 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
786 " dstFmt=0x%x, dstType=0x%x\n",
788 _mesa_get_format_name(texImage
->TexFormat
),
789 texImage
->Width
, texImage
->Height
,
793 _mesa_lock_texture(ctx
, texObj
);
795 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
798 _mesa_unlock_texture(ctx
, texObj
);
804 * Do error checking for a glGetCompressedTexImage() call.
805 * \return GL_TRUE if any error, GL_FALSE if no errors.
808 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
811 struct gl_texture_object
*texObj
;
812 struct gl_texture_image
*texImage
;
813 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
815 if (maxLevels
== 0) {
816 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
821 if (level
< 0 || level
>= maxLevels
) {
822 _mesa_error(ctx
, GL_INVALID_VALUE
,
823 "glGetCompressedTexImageARB(bad level = %d)", level
);
827 if (_mesa_is_proxy_texture(target
)) {
828 _mesa_error(ctx
, GL_INVALID_ENUM
,
829 "glGetCompressedTexImageARB(bad target = %s)",
830 _mesa_lookup_enum_by_nr(target
));
834 texObj
= _mesa_get_current_tex_object(ctx
, target
);
836 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
840 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
843 /* probably invalid mipmap level */
844 _mesa_error(ctx
, GL_INVALID_VALUE
,
845 "glGetCompressedTexImageARB(level)");
849 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
850 _mesa_error(ctx
, GL_INVALID_OPERATION
,
851 "glGetCompressedTexImageARB(texture is not compressed)");
855 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
856 GLuint compressedSize
;
858 /* make sure PBO is not mapped */
859 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
860 _mesa_error(ctx
, GL_INVALID_OPERATION
,
861 "glGetCompressedTexImage(PBO is mapped)");
865 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
870 /* do bounds checking on PBO write */
871 if ((const GLubyte
*) img
+ compressedSize
>
872 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
873 _mesa_error(ctx
, GL_INVALID_OPERATION
,
874 "glGetCompressedTexImage(out of bounds PBO write)");
884 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
886 struct gl_texture_object
*texObj
;
887 struct gl_texture_image
*texImage
;
888 GET_CURRENT_CONTEXT(ctx
);
889 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
891 if (getcompressedteximage_error_check(ctx
, target
, level
, img
)) {
895 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
896 /* not an error, do nothing */
900 texObj
= _mesa_get_current_tex_object(ctx
, target
);
901 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
903 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
905 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
907 _mesa_get_format_name(texImage
->TexFormat
),
908 texImage
->Width
, texImage
->Height
);
911 _mesa_lock_texture(ctx
, texObj
);
913 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
916 _mesa_unlock_texture(ctx
, texObj
);