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"
40 #include "texgetimage.h"
46 * Can the given type represent negative values?
48 static INLINE GLboolean
49 type_with_negative_values(GLenum type
)
56 case GL_HALF_FLOAT_ARB
:
65 * glGetTexImage for color index pixels.
68 get_tex_color_index(struct gl_context
*ctx
, GLuint dimensions
,
69 GLenum format
, GLenum type
, GLvoid
*pixels
,
70 const struct gl_texture_image
*texImage
)
72 const GLint width
= texImage
->Width
;
73 const GLint height
= texImage
->Height
;
74 const GLint depth
= texImage
->Depth
;
75 const GLuint indexBits
=
76 _mesa_get_format_bits(texImage
->TexFormat
, GL_TEXTURE_INDEX_SIZE_EXT
);
77 const GLbitfield transferOps
= 0x0;
80 for (img
= 0; img
< depth
; img
++) {
81 for (row
= 0; row
< height
; row
++) {
82 GLuint indexRow
[MAX_WIDTH
] = { 0 };
83 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
84 width
, height
, format
, type
,
89 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
90 src
+= width
* (img
* texImage
->Height
+ row
);
91 for (col
= 0; col
< width
; col
++) {
92 indexRow
[col
] = src
[col
];
95 else if (indexBits
== 16) {
96 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
97 src
+= width
* (img
* texImage
->Height
+ row
);
98 for (col
= 0; col
< width
; col
++) {
99 indexRow
[col
] = src
[col
];
103 _mesa_problem(ctx
, "Color index problem in _mesa_GetTexImage");
105 _mesa_pack_index_span(ctx
, width
, type
, dest
,
106 indexRow
, &ctx
->Pack
, transferOps
);
113 * glGetTexImage for depth/Z pixels.
116 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
117 GLenum format
, GLenum type
, GLvoid
*pixels
,
118 const struct gl_texture_image
*texImage
)
120 const GLint width
= texImage
->Width
;
121 const GLint height
= texImage
->Height
;
122 const GLint depth
= texImage
->Depth
;
124 GLfloat
*depthRow
= (GLfloat
*) malloc(width
* sizeof(GLfloat
));
127 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
131 for (img
= 0; img
< depth
; img
++) {
132 for (row
= 0; row
< height
; row
++) {
133 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
134 width
, height
, format
, type
,
138 for (col
= 0; col
< width
; col
++) {
139 texImage
->FetchTexelf(texImage
, col
, row
, img
, depthRow
+ col
);
141 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
150 * glGetTexImage for depth/stencil pixels.
153 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
154 GLenum format
, GLenum type
, GLvoid
*pixels
,
155 const struct gl_texture_image
*texImage
)
157 const GLint width
= texImage
->Width
;
158 const GLint height
= texImage
->Height
;
159 const GLint depth
= texImage
->Depth
;
160 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
163 for (img
= 0; img
< depth
; img
++) {
164 for (row
= 0; row
< height
; row
++) {
165 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
166 width
, height
, format
, type
,
168 memcpy(dest
, src
, width
* sizeof(GLuint
));
169 if (ctx
->Pack
.SwapBytes
) {
170 _mesa_swap4((GLuint
*) dest
, width
);
173 src
+= width
* row
+ width
* height
* img
;
180 * glGetTexImage for YCbCr pixels.
183 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
184 GLenum format
, GLenum type
, GLvoid
*pixels
,
185 const struct gl_texture_image
*texImage
)
187 const GLint width
= texImage
->Width
;
188 const GLint height
= texImage
->Height
;
189 const GLint depth
= texImage
->Depth
;
190 const GLint rowstride
= texImage
->RowStride
;
191 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
194 for (img
= 0; img
< depth
; img
++) {
195 for (row
= 0; row
< height
; row
++) {
196 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
197 width
, height
, format
, type
,
199 memcpy(dest
, src
, width
* sizeof(GLushort
));
201 /* check for byte swapping */
202 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
203 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
204 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
205 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
206 if (!ctx
->Pack
.SwapBytes
)
207 _mesa_swap2((GLushort
*) dest
, width
);
209 else if (ctx
->Pack
.SwapBytes
) {
210 _mesa_swap2((GLushort
*) dest
, width
);
219 #if FEATURE_EXT_texture_sRGB
223 * Convert a float value from linear space to a
224 * non-linear sRGB value in [0, 255].
225 * Not terribly efficient.
227 static INLINE GLfloat
228 linear_to_nonlinear(GLfloat cl
)
230 /* can't have values outside [0, 1] */
232 if (cl
< 0.0031308f
) {
236 cs
= (GLfloat
)(1.055 * pow(cl
, 0.41666) - 0.055);
243 * glGetTexImagefor sRGB pixels;
246 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
247 GLenum format
, GLenum type
, GLvoid
*pixels
,
248 const struct gl_texture_image
*texImage
)
250 const GLint width
= texImage
->Width
;
251 const GLint height
= texImage
->Height
;
252 const GLint depth
= texImage
->Depth
;
253 const GLbitfield transferOps
= 0x0;
255 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
258 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
262 for (img
= 0; img
< depth
; img
++) {
263 for (row
= 0; row
< height
; row
++) {
264 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
265 width
, height
, format
, type
,
270 /* convert row to RGBA format */
271 for (col
= 0; col
< width
; col
++) {
272 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
273 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
274 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
275 rgba
[col
][GCOMP
] = 0.0;
276 rgba
[col
][BCOMP
] = 0.0;
278 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
279 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
280 rgba
[col
][GCOMP
] = 0.0;
281 rgba
[col
][BCOMP
] = 0.0;
283 else if (texImage
->_BaseFormat
== GL_RGB
||
284 texImage
->_BaseFormat
== GL_RGBA
) {
285 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
286 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
287 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
290 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
292 &ctx
->Pack
, transferOps
);
300 #else /* FEATURE_EXT_texture_sRGB */
304 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
305 GLenum format
, GLenum type
, GLvoid
*pixels
,
306 const struct gl_texture_image
*texImage
)
312 #endif /* FEATURE_EXT_texture_sRGB */
316 * glGetTexImagefor RGBA, Luminance, etc. pixels.
317 * This is the slow way since we use texture sampling.
320 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
321 GLenum format
, GLenum type
, GLvoid
*pixels
,
322 const struct gl_texture_image
*texImage
)
324 const GLint width
= texImage
->Width
;
325 const GLint height
= texImage
->Height
;
326 const GLint depth
= texImage
->Depth
;
327 /* Normally, no pixel transfer ops are performed during glGetTexImage.
328 * The only possible exception is component clamping to [0,1].
330 GLbitfield transferOps
= 0x0;
332 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
335 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
339 for (img
= 0; img
< depth
; img
++) {
340 for (row
= 0; row
< height
; row
++) {
341 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
342 width
, height
, format
, type
,
345 GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
347 /* clamp does not apply to GetTexImage (final conversion)?
348 * Looks like we need clamp though when going from format
349 * containing negative values to unsigned format.
351 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
) {
352 transferOps
|= IMAGE_CLAMP_BIT
;
354 else if (!type_with_negative_values(type
) &&
355 (dataType
== GL_FLOAT
||
356 dataType
== GL_SIGNED_NORMALIZED
)) {
357 transferOps
|= IMAGE_CLAMP_BIT
;
360 for (col
= 0; col
< width
; col
++) {
361 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
362 if (texImage
->_BaseFormat
== GL_ALPHA
) {
363 rgba
[col
][RCOMP
] = 0.0F
;
364 rgba
[col
][GCOMP
] = 0.0F
;
365 rgba
[col
][BCOMP
] = 0.0F
;
367 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
368 rgba
[col
][GCOMP
] = 0.0F
;
369 rgba
[col
][BCOMP
] = 0.0F
;
370 rgba
[col
][ACOMP
] = 1.0F
;
372 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
373 rgba
[col
][GCOMP
] = 0.0F
;
374 rgba
[col
][BCOMP
] = 0.0F
;
376 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
377 rgba
[col
][GCOMP
] = 0.0F
;
378 rgba
[col
][BCOMP
] = 0.0F
;
379 rgba
[col
][ACOMP
] = 1.0F
;
382 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
384 &ctx
->Pack
, transferOps
);
393 * Try to do glGetTexImage() with simple memcpy().
394 * \return GL_TRUE if done, GL_FALSE otherwise
397 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
, GLvoid
*pixels
,
398 const struct gl_texture_object
*texObj
,
399 const struct gl_texture_image
*texImage
)
401 GLboolean memCopy
= GL_FALSE
;
403 /* Texture image should have been mapped already */
404 assert(texImage
->Data
);
407 * Check if the src/dst formats are compatible.
408 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
409 * so we don't have to worry about those.
410 * XXX more format combinations could be supported here.
412 if ((texObj
->Target
== GL_TEXTURE_1D
||
413 texObj
->Target
== GL_TEXTURE_2D
||
414 texObj
->Target
== GL_TEXTURE_RECTANGLE
||
415 (texObj
->Target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
416 texObj
->Target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
417 if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
&&
419 type
== GL_UNSIGNED_BYTE
&&
420 !ctx
->Pack
.SwapBytes
&&
421 _mesa_little_endian()) {
424 else if (texImage
->TexFormat
== MESA_FORMAT_AL88
&&
425 format
== GL_LUMINANCE_ALPHA
&&
426 type
== GL_UNSIGNED_BYTE
&&
427 !ctx
->Pack
.SwapBytes
&&
428 _mesa_little_endian()) {
431 else if (texImage
->TexFormat
== MESA_FORMAT_L8
&&
432 format
== GL_LUMINANCE
&&
433 type
== GL_UNSIGNED_BYTE
) {
436 else if (texImage
->TexFormat
== MESA_FORMAT_L16
&&
437 format
== GL_LUMINANCE
&&
438 type
== GL_UNSIGNED_SHORT
) {
441 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
442 format
== GL_ALPHA
&&
443 type
== GL_UNSIGNED_BYTE
) {
446 else if (texImage
->TexFormat
== MESA_FORMAT_A16
&&
447 format
== GL_ALPHA
&&
448 type
== GL_UNSIGNED_SHORT
) {
454 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
455 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
457 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
458 texImage
->Height
, format
, type
, 0, 0);
459 const GLint dstRowStride
=
460 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
461 const GLubyte
*src
= texImage
->Data
;
462 const GLint srcRowStride
= texImage
->RowStride
* bpp
;
465 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
466 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
469 for (row
= 0; row
< texImage
->Height
; row
++) {
470 memcpy(dst
, src
, bytesPerRow
);
482 * This is the software fallback for Driver.GetTexImage().
483 * All error checking will have been done before this routine is called.
484 * The texture image must be mapped.
487 _mesa_get_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
488 GLenum format
, GLenum type
, GLvoid
*pixels
,
489 struct gl_texture_object
*texObj
,
490 struct gl_texture_image
*texImage
)
494 /* If we get here, the texture image should be mapped */
495 assert(texImage
->Data
);
508 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
509 /* Packing texture image into a PBO.
510 * Map the (potentially) VRAM-based buffer into our process space so
511 * we can write into it with the code below.
512 * A hardware driver might use a sophisticated blit to move the
513 * texture data to the PBO if the PBO is in VRAM along with the texture.
515 GLubyte
*buf
= (GLubyte
*)
516 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
517 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
519 /* out of memory or other unexpected error */
520 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
523 /* <pixels> was an offset into the PBO.
524 * Now make it a real, client-side pointer inside the mapped region.
526 pixels
= ADD_POINTERS(buf
, pixels
);
529 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texObj
, texImage
)) {
532 else if (format
== GL_COLOR_INDEX
) {
533 get_tex_color_index(ctx
, dimensions
, format
, type
, pixels
, texImage
);
535 else if (format
== GL_DEPTH_COMPONENT
) {
536 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
538 else if (format
== GL_DEPTH_STENCIL_EXT
) {
539 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
541 else if (format
== GL_YCBCR_MESA
) {
542 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
544 else if (_mesa_get_format_color_encoding(texImage
->TexFormat
) == GL_SRGB
) {
545 get_tex_srgb(ctx
, dimensions
, format
, type
, pixels
, texImage
);
548 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
551 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
552 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
553 ctx
->Pack
.BufferObj
);
560 * This is the software fallback for Driver.GetCompressedTexImage().
561 * All error checking will have been done before this routine is called.
564 _mesa_get_compressed_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
566 struct gl_texture_object
*texObj
,
567 struct gl_texture_image
*texImage
)
569 const GLuint row_stride
= _mesa_format_row_stride(texImage
->TexFormat
,
571 const GLuint row_stride_stored
= _mesa_format_row_stride(texImage
->TexFormat
,
572 texImage
->RowStride
);
575 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
576 /* pack texture image into a PBO */
577 GLubyte
*buf
= (GLubyte
*)
578 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
579 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
581 /* out of memory or other unexpected error */
582 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
583 "glGetCompresssedTexImage(map PBO failed)");
586 img
= ADD_POINTERS(buf
, img
);
589 /* no pixelstore or pixel transfer, but respect stride */
591 if (row_stride
== row_stride_stored
) {
592 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
596 memcpy(img
, texImage
->Data
, size
);
600 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
601 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
602 memcpy((GLubyte
*)img
+ i
* row_stride
,
603 (GLubyte
*)texImage
->Data
+ i
* row_stride_stored
,
608 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
609 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
610 ctx
->Pack
.BufferObj
);
617 * Do error checking for a glGetTexImage() call.
618 * \return GL_TRUE if any error, GL_FALSE if no errors.
621 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
622 GLenum format
, GLenum type
, GLvoid
*pixels
)
624 struct gl_texture_object
*texObj
;
625 struct gl_texture_image
*texImage
;
626 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
629 if (maxLevels
== 0) {
630 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
634 if (level
< 0 || level
>= maxLevels
) {
635 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
639 if (_mesa_sizeof_packed_type(type
) <= 0) {
640 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
644 if (_mesa_components_in_format(format
) <= 0 ||
645 format
== GL_STENCIL_INDEX
) {
646 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
650 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
651 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
655 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
656 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
660 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
661 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
665 if (!ctx
->Extensions
.EXT_packed_depth_stencil
666 && _mesa_is_depthstencil_format(format
)) {
667 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
671 if (!ctx
->Extensions
.ATI_envmap_bumpmap
672 && _mesa_is_dudv_format(format
)) {
673 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
677 texObj
= _mesa_get_current_tex_object(ctx
, target
);
679 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
680 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
684 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
690 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
692 /* Make sure the requested image format is compatible with the
693 * texture's format. Note that a color index texture can be converted
694 * to RGBA so that combo is allowed.
696 if (_mesa_is_color_format(format
)
697 && !_mesa_is_color_format(baseFormat
)
698 && !_mesa_is_index_format(baseFormat
)) {
699 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
702 else if (_mesa_is_index_format(format
)
703 && !_mesa_is_index_format(baseFormat
)) {
704 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
707 else if (_mesa_is_depth_format(format
)
708 && !_mesa_is_depth_format(baseFormat
)
709 && !_mesa_is_depthstencil_format(baseFormat
)) {
710 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
713 else if (_mesa_is_ycbcr_format(format
)
714 && !_mesa_is_ycbcr_format(baseFormat
)) {
715 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
718 else if (_mesa_is_depthstencil_format(format
)
719 && !_mesa_is_depthstencil_format(baseFormat
)) {
720 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
723 else if (_mesa_is_dudv_format(format
)
724 && !_mesa_is_dudv_format(baseFormat
)) {
725 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
729 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
730 /* packing texture image into a PBO */
731 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
732 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
733 texImage
->Height
, texImage
->Depth
,
734 format
, type
, pixels
)) {
735 _mesa_error(ctx
, GL_INVALID_OPERATION
,
736 "glGetTexImage(out of bounds PBO write)");
740 /* PBO should not be mapped */
741 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
742 _mesa_error(ctx
, GL_INVALID_OPERATION
,
743 "glGetTexImage(PBO is mapped)");
754 * Get texture image. Called by glGetTexImage.
756 * \param target texture target.
757 * \param level image level.
758 * \param format pixel data format for returned image.
759 * \param type pixel data type for returned image.
760 * \param pixels returned pixel data.
763 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
764 GLenum type
, GLvoid
*pixels
)
766 struct gl_texture_object
*texObj
;
767 struct gl_texture_image
*texImage
;
768 GET_CURRENT_CONTEXT(ctx
);
769 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
771 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
775 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
776 /* not an error, do nothing */
780 texObj
= _mesa_get_current_tex_object(ctx
, target
);
781 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
783 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
784 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
785 " dstFmt=0x%x, dstType=0x%x\n",
787 _mesa_get_format_name(texImage
->TexFormat
),
788 texImage
->Width
, texImage
->Height
,
792 _mesa_lock_texture(ctx
, texObj
);
794 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
797 _mesa_unlock_texture(ctx
, texObj
);
803 * Do error checking for a glGetCompressedTexImage() call.
804 * \return GL_TRUE if any error, GL_FALSE if no errors.
807 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
810 struct gl_texture_object
*texObj
;
811 struct gl_texture_image
*texImage
;
812 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
814 if (maxLevels
== 0) {
815 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
820 if (level
< 0 || level
>= maxLevels
) {
821 _mesa_error(ctx
, GL_INVALID_VALUE
,
822 "glGetCompressedTexImageARB(bad level = %d)", level
);
826 if (_mesa_is_proxy_texture(target
)) {
827 _mesa_error(ctx
, GL_INVALID_ENUM
,
828 "glGetCompressedTexImageARB(bad target = %s)",
829 _mesa_lookup_enum_by_nr(target
));
833 texObj
= _mesa_get_current_tex_object(ctx
, target
);
835 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
839 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
842 /* probably invalid mipmap level */
843 _mesa_error(ctx
, GL_INVALID_VALUE
,
844 "glGetCompressedTexImageARB(level)");
848 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
849 _mesa_error(ctx
, GL_INVALID_OPERATION
,
850 "glGetCompressedTexImageARB(texture is not compressed)");
854 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
855 GLuint compressedSize
;
857 /* make sure PBO is not mapped */
858 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
859 _mesa_error(ctx
, GL_INVALID_OPERATION
,
860 "glGetCompressedTexImage(PBO is mapped)");
864 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
869 /* do bounds checking on PBO write */
870 if ((const GLubyte
*) img
+ compressedSize
>
871 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
872 _mesa_error(ctx
, GL_INVALID_OPERATION
,
873 "glGetCompressedTexImage(out of bounds PBO write)");
883 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
885 struct gl_texture_object
*texObj
;
886 struct gl_texture_image
*texImage
;
887 GET_CURRENT_CONTEXT(ctx
);
888 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
890 if (getcompressedteximage_error_check(ctx
, target
, level
, img
)) {
894 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
895 /* not an error, do nothing */
899 texObj
= _mesa_get_current_tex_object(ctx
, target
);
900 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
902 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
904 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
906 _mesa_get_format_name(texImage
->TexFormat
),
907 texImage
->Width
, texImage
->Height
);
910 _mesa_lock_texture(ctx
, texObj
);
912 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
915 _mesa_unlock_texture(ctx
, texObj
);