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"
39 #include "texgetimage.h"
45 * Can the given type represent negative values?
47 static INLINE GLboolean
48 type_with_negative_values(GLenum type
)
55 case GL_HALF_FLOAT_ARB
:
64 * glGetTexImage for color index pixels.
67 get_tex_color_index(struct gl_context
*ctx
, GLuint dimensions
,
68 GLenum format
, GLenum type
, GLvoid
*pixels
,
69 const struct gl_texture_image
*texImage
)
71 const GLint width
= texImage
->Width
;
72 const GLint height
= texImage
->Height
;
73 const GLint depth
= texImage
->Depth
;
74 const GLuint indexBits
=
75 _mesa_get_format_bits(texImage
->TexFormat
, GL_TEXTURE_INDEX_SIZE_EXT
);
76 const GLbitfield transferOps
= 0x0;
79 for (img
= 0; img
< depth
; img
++) {
80 for (row
= 0; row
< height
; row
++) {
81 GLuint indexRow
[MAX_WIDTH
] = { 0 };
82 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
83 width
, height
, format
, type
,
88 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
89 src
+= width
* (img
* texImage
->Height
+ row
);
90 for (col
= 0; col
< width
; col
++) {
91 indexRow
[col
] = src
[col
];
94 else if (indexBits
== 16) {
95 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
96 src
+= width
* (img
* texImage
->Height
+ row
);
97 for (col
= 0; col
< width
; col
++) {
98 indexRow
[col
] = src
[col
];
102 _mesa_problem(ctx
, "Color index problem in _mesa_GetTexImage");
104 _mesa_pack_index_span(ctx
, width
, type
, dest
,
105 indexRow
, &ctx
->Pack
, transferOps
);
112 * glGetTexImage for depth/Z pixels.
115 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
116 GLenum format
, GLenum type
, GLvoid
*pixels
,
117 const struct gl_texture_image
*texImage
)
119 const GLint width
= texImage
->Width
;
120 const GLint height
= texImage
->Height
;
121 const GLint depth
= texImage
->Depth
;
123 GLfloat
*depthRow
= (GLfloat
*) malloc(width
* sizeof(GLfloat
));
126 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
130 for (img
= 0; img
< depth
; img
++) {
131 for (row
= 0; row
< height
; row
++) {
132 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
133 width
, height
, format
, type
,
137 for (col
= 0; col
< width
; col
++) {
138 texImage
->FetchTexelf(texImage
, col
, row
, img
, depthRow
+ col
);
140 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
149 * glGetTexImage for depth/stencil pixels.
152 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
153 GLenum format
, GLenum type
, GLvoid
*pixels
,
154 const struct gl_texture_image
*texImage
)
156 const GLint width
= texImage
->Width
;
157 const GLint height
= texImage
->Height
;
158 const GLint depth
= texImage
->Depth
;
159 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
162 for (img
= 0; img
< depth
; img
++) {
163 for (row
= 0; row
< height
; row
++) {
164 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
165 width
, height
, format
, type
,
167 memcpy(dest
, src
, width
* sizeof(GLuint
));
168 if (ctx
->Pack
.SwapBytes
) {
169 _mesa_swap4((GLuint
*) dest
, width
);
172 src
+= width
* row
+ width
* height
* img
;
179 * glGetTexImage for YCbCr pixels.
182 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
183 GLenum format
, GLenum type
, GLvoid
*pixels
,
184 const struct gl_texture_image
*texImage
)
186 const GLint width
= texImage
->Width
;
187 const GLint height
= texImage
->Height
;
188 const GLint depth
= texImage
->Depth
;
189 const GLint rowstride
= texImage
->RowStride
;
190 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
193 for (img
= 0; img
< depth
; img
++) {
194 for (row
= 0; row
< height
; row
++) {
195 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
196 width
, height
, format
, type
,
198 memcpy(dest
, src
, width
* sizeof(GLushort
));
200 /* check for byte swapping */
201 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
202 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
203 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
204 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
205 if (!ctx
->Pack
.SwapBytes
)
206 _mesa_swap2((GLushort
*) dest
, width
);
208 else if (ctx
->Pack
.SwapBytes
) {
209 _mesa_swap2((GLushort
*) dest
, width
);
218 #if FEATURE_EXT_texture_sRGB
222 * Convert a float value from linear space to a
223 * non-linear sRGB value in [0, 255].
224 * Not terribly efficient.
226 static INLINE GLfloat
227 linear_to_nonlinear(GLfloat cl
)
229 /* can't have values outside [0, 1] */
231 if (cl
< 0.0031308f
) {
235 cs
= (GLfloat
)(1.055 * pow(cl
, 0.41666) - 0.055);
242 * glGetTexImagefor sRGB pixels;
245 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
246 GLenum format
, GLenum type
, GLvoid
*pixels
,
247 const struct gl_texture_image
*texImage
)
249 const GLint width
= texImage
->Width
;
250 const GLint height
= texImage
->Height
;
251 const GLint depth
= texImage
->Depth
;
252 const GLbitfield transferOps
= 0x0;
254 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
257 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
261 for (img
= 0; img
< depth
; img
++) {
262 for (row
= 0; row
< height
; row
++) {
263 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
264 width
, height
, format
, type
,
269 /* convert row to RGBA format */
270 for (col
= 0; col
< width
; col
++) {
271 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
272 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
273 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
274 rgba
[col
][GCOMP
] = 0.0;
275 rgba
[col
][BCOMP
] = 0.0;
277 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
278 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
279 rgba
[col
][GCOMP
] = 0.0;
280 rgba
[col
][BCOMP
] = 0.0;
282 else if (texImage
->_BaseFormat
== GL_RGB
||
283 texImage
->_BaseFormat
== GL_RGBA
) {
284 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
285 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
286 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
289 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
291 &ctx
->Pack
, transferOps
);
299 #else /* FEATURE_EXT_texture_sRGB */
303 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
304 GLenum format
, GLenum type
, GLvoid
*pixels
,
305 const struct gl_texture_image
*texImage
)
311 #endif /* FEATURE_EXT_texture_sRGB */
315 * glGetTexImagefor RGBA, Luminance, etc. pixels.
316 * This is the slow way since we use texture sampling.
319 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
320 GLenum format
, GLenum type
, GLvoid
*pixels
,
321 const struct gl_texture_image
*texImage
)
323 const GLint width
= texImage
->Width
;
324 const GLint height
= texImage
->Height
;
325 const GLint depth
= texImage
->Depth
;
326 /* Normally, no pixel transfer ops are performed during glGetTexImage.
327 * The only possible exception is component clamping to [0,1].
329 GLbitfield transferOps
= 0x0;
331 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
334 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
338 for (img
= 0; img
< depth
; img
++) {
339 for (row
= 0; row
< height
; row
++) {
340 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
341 width
, height
, format
, type
,
344 GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
346 /* clamp does not apply to GetTexImage (final conversion)?
347 * Looks like we need clamp though when going from format
348 * containing negative values to unsigned format.
350 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
) {
351 transferOps
|= IMAGE_CLAMP_BIT
;
353 else if (!type_with_negative_values(type
) &&
354 (dataType
== GL_FLOAT
||
355 dataType
== GL_SIGNED_NORMALIZED
)) {
356 transferOps
|= IMAGE_CLAMP_BIT
;
359 for (col
= 0; col
< width
; col
++) {
360 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
361 if (texImage
->_BaseFormat
== GL_ALPHA
) {
362 rgba
[col
][RCOMP
] = 0.0F
;
363 rgba
[col
][GCOMP
] = 0.0F
;
364 rgba
[col
][BCOMP
] = 0.0F
;
366 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
367 rgba
[col
][GCOMP
] = 0.0F
;
368 rgba
[col
][BCOMP
] = 0.0F
;
369 rgba
[col
][ACOMP
] = 1.0F
;
371 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
372 rgba
[col
][GCOMP
] = 0.0F
;
373 rgba
[col
][BCOMP
] = 0.0F
;
375 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
376 rgba
[col
][GCOMP
] = 0.0F
;
377 rgba
[col
][BCOMP
] = 0.0F
;
378 rgba
[col
][ACOMP
] = 1.0F
;
381 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
383 &ctx
->Pack
, transferOps
);
392 * Try to do glGetTexImage() with simple memcpy().
393 * \return GL_TRUE if done, GL_FALSE otherwise
396 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
, GLvoid
*pixels
,
397 const struct gl_texture_object
*texObj
,
398 const struct gl_texture_image
*texImage
)
400 GLboolean memCopy
= GL_FALSE
;
402 /* Texture image should have been mapped already */
403 assert(texImage
->Data
);
406 * Check if the src/dst formats are compatible.
407 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
408 * so we don't have to worry about those.
409 * XXX more format combinations could be supported here.
411 if ((texObj
->Target
== GL_TEXTURE_1D
||
412 texObj
->Target
== GL_TEXTURE_2D
||
413 texObj
->Target
== GL_TEXTURE_RECTANGLE
||
414 (texObj
->Target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
415 texObj
->Target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
416 if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
&&
418 type
== GL_UNSIGNED_BYTE
&&
419 !ctx
->Pack
.SwapBytes
&&
420 _mesa_little_endian()) {
423 else if (texImage
->TexFormat
== MESA_FORMAT_AL88
&&
424 format
== GL_LUMINANCE_ALPHA
&&
425 type
== GL_UNSIGNED_BYTE
&&
426 !ctx
->Pack
.SwapBytes
&&
427 _mesa_little_endian()) {
430 else if (texImage
->TexFormat
== MESA_FORMAT_L8
&&
431 format
== GL_LUMINANCE
&&
432 type
== GL_UNSIGNED_BYTE
) {
435 else if (texImage
->TexFormat
== MESA_FORMAT_L16
&&
436 format
== GL_LUMINANCE
&&
437 type
== GL_UNSIGNED_SHORT
) {
440 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
441 format
== GL_ALPHA
&&
442 type
== GL_UNSIGNED_BYTE
) {
445 else if (texImage
->TexFormat
== MESA_FORMAT_A16
&&
446 format
== GL_ALPHA
&&
447 type
== GL_UNSIGNED_SHORT
) {
453 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
454 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
456 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
457 texImage
->Height
, format
, type
, 0, 0);
458 const GLint dstRowStride
=
459 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
460 const GLubyte
*src
= texImage
->Data
;
461 const GLint srcRowStride
= texImage
->RowStride
* bpp
;
464 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
465 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
468 for (row
= 0; row
< texImage
->Height
; row
++) {
469 memcpy(dst
, src
, bytesPerRow
);
481 * This is the software fallback for Driver.GetTexImage().
482 * All error checking will have been done before this routine is called.
483 * The texture image must be mapped.
486 _mesa_get_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
487 GLenum format
, GLenum type
, GLvoid
*pixels
,
488 struct gl_texture_object
*texObj
,
489 struct gl_texture_image
*texImage
)
493 /* If we get here, the texture image should be mapped */
494 assert(texImage
->Data
);
507 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
508 /* Packing texture image into a PBO.
509 * Map the (potentially) VRAM-based buffer into our process space so
510 * we can write into it with the code below.
511 * A hardware driver might use a sophisticated blit to move the
512 * texture data to the PBO if the PBO is in VRAM along with the texture.
514 GLubyte
*buf
= (GLubyte
*)
515 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
516 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
518 /* out of memory or other unexpected error */
519 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
522 /* <pixels> was an offset into the PBO.
523 * Now make it a real, client-side pointer inside the mapped region.
525 pixels
= ADD_POINTERS(buf
, pixels
);
528 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texObj
, texImage
)) {
531 else if (format
== GL_COLOR_INDEX
) {
532 get_tex_color_index(ctx
, dimensions
, format
, type
, pixels
, texImage
);
534 else if (format
== GL_DEPTH_COMPONENT
) {
535 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
537 else if (format
== GL_DEPTH_STENCIL_EXT
) {
538 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
540 else if (format
== GL_YCBCR_MESA
) {
541 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
543 else if (_mesa_get_format_color_encoding(texImage
->TexFormat
) == GL_SRGB
) {
544 get_tex_srgb(ctx
, dimensions
, format
, type
, pixels
, texImage
);
547 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
550 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
551 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
552 ctx
->Pack
.BufferObj
);
559 * This is the software fallback for Driver.GetCompressedTexImage().
560 * All error checking will have been done before this routine is called.
563 _mesa_get_compressed_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
565 struct gl_texture_object
*texObj
,
566 struct gl_texture_image
*texImage
)
568 const GLuint row_stride
= _mesa_format_row_stride(texImage
->TexFormat
,
570 const GLuint row_stride_stored
= _mesa_format_row_stride(texImage
->TexFormat
,
571 texImage
->RowStride
);
574 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
575 /* pack texture image into a PBO */
576 GLubyte
*buf
= (GLubyte
*)
577 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
578 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
580 /* out of memory or other unexpected error */
581 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
582 "glGetCompresssedTexImage(map PBO failed)");
585 img
= ADD_POINTERS(buf
, img
);
588 /* no pixelstore or pixel transfer, but respect stride */
590 if (row_stride
== row_stride_stored
) {
591 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
595 memcpy(img
, texImage
->Data
, size
);
599 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
600 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
601 memcpy((GLubyte
*)img
+ i
* row_stride
,
602 (GLubyte
*)texImage
->Data
+ i
* row_stride_stored
,
607 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
608 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
609 ctx
->Pack
.BufferObj
);
616 * Do error checking for a glGetTexImage() call.
617 * \return GL_TRUE if any error, GL_FALSE if no errors.
620 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
621 GLenum format
, GLenum type
, GLvoid
*pixels
)
623 struct gl_texture_object
*texObj
;
624 struct gl_texture_image
*texImage
;
625 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
628 if (maxLevels
== 0) {
629 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
633 if (level
< 0 || level
>= maxLevels
) {
634 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
638 if (_mesa_sizeof_packed_type(type
) <= 0) {
639 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
643 if (_mesa_components_in_format(format
) <= 0 ||
644 format
== GL_STENCIL_INDEX
) {
645 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
649 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
650 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
654 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
655 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
659 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
660 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
664 if (!ctx
->Extensions
.EXT_packed_depth_stencil
665 && _mesa_is_depthstencil_format(format
)) {
666 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
670 if (!ctx
->Extensions
.ATI_envmap_bumpmap
671 && _mesa_is_dudv_format(format
)) {
672 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
676 texObj
= _mesa_get_current_tex_object(ctx
, target
);
678 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
679 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
683 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
689 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
691 /* Make sure the requested image format is compatible with the
692 * texture's format. Note that a color index texture can be converted
693 * to RGBA so that combo is allowed.
695 if (_mesa_is_color_format(format
)
696 && !_mesa_is_color_format(baseFormat
)
697 && !_mesa_is_index_format(baseFormat
)) {
698 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
701 else if (_mesa_is_index_format(format
)
702 && !_mesa_is_index_format(baseFormat
)) {
703 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
706 else if (_mesa_is_depth_format(format
)
707 && !_mesa_is_depth_format(baseFormat
)
708 && !_mesa_is_depthstencil_format(baseFormat
)) {
709 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
712 else if (_mesa_is_ycbcr_format(format
)
713 && !_mesa_is_ycbcr_format(baseFormat
)) {
714 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
717 else if (_mesa_is_depthstencil_format(format
)
718 && !_mesa_is_depthstencil_format(baseFormat
)) {
719 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
722 else if (_mesa_is_dudv_format(format
)
723 && !_mesa_is_dudv_format(baseFormat
)) {
724 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
728 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
729 /* packing texture image into a PBO */
730 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
731 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
732 texImage
->Height
, texImage
->Depth
,
733 format
, type
, pixels
)) {
734 _mesa_error(ctx
, GL_INVALID_OPERATION
,
735 "glGetTexImage(out of bounds PBO write)");
739 /* PBO should not be mapped */
740 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
741 _mesa_error(ctx
, GL_INVALID_OPERATION
,
742 "glGetTexImage(PBO is mapped)");
753 * Get texture image. Called by glGetTexImage.
755 * \param target texture target.
756 * \param level image level.
757 * \param format pixel data format for returned image.
758 * \param type pixel data type for returned image.
759 * \param pixels returned pixel data.
762 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
763 GLenum type
, GLvoid
*pixels
)
765 struct gl_texture_object
*texObj
;
766 struct gl_texture_image
*texImage
;
767 GET_CURRENT_CONTEXT(ctx
);
768 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
770 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
774 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
775 /* not an error, do nothing */
779 texObj
= _mesa_get_current_tex_object(ctx
, target
);
780 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
782 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
783 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
784 " dstFmt=0x%x, dstType=0x%x\n",
786 _mesa_get_format_name(texImage
->TexFormat
),
787 texImage
->Width
, texImage
->Height
,
791 _mesa_lock_texture(ctx
, texObj
);
793 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
796 _mesa_unlock_texture(ctx
, texObj
);
802 * Do error checking for a glGetCompressedTexImage() call.
803 * \return GL_TRUE if any error, GL_FALSE if no errors.
806 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
809 struct gl_texture_object
*texObj
;
810 struct gl_texture_image
*texImage
;
811 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
813 if (maxLevels
== 0) {
814 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
819 if (level
< 0 || level
>= maxLevels
) {
820 _mesa_error(ctx
, GL_INVALID_VALUE
,
821 "glGetCompressedTexImageARB(bad level = %d)", level
);
825 if (_mesa_is_proxy_texture(target
)) {
826 _mesa_error(ctx
, GL_INVALID_ENUM
,
827 "glGetCompressedTexImageARB(bad target = %s)",
828 _mesa_lookup_enum_by_nr(target
));
832 texObj
= _mesa_get_current_tex_object(ctx
, target
);
834 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
838 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
841 /* probably invalid mipmap level */
842 _mesa_error(ctx
, GL_INVALID_VALUE
,
843 "glGetCompressedTexImageARB(level)");
847 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
848 _mesa_error(ctx
, GL_INVALID_OPERATION
,
849 "glGetCompressedTexImageARB(texture is not compressed)");
853 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
854 GLuint compressedSize
;
856 /* make sure PBO is not mapped */
857 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
858 _mesa_error(ctx
, GL_INVALID_OPERATION
,
859 "glGetCompressedTexImage(PBO is mapped)");
863 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
868 /* do bounds checking on PBO write */
869 if ((const GLubyte
*) img
+ compressedSize
>
870 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
871 _mesa_error(ctx
, GL_INVALID_OPERATION
,
872 "glGetCompressedTexImage(out of bounds PBO write)");
882 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
884 struct gl_texture_object
*texObj
;
885 struct gl_texture_image
*texImage
;
886 GET_CURRENT_CONTEXT(ctx
);
887 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
889 if (getcompressedteximage_error_check(ctx
, target
, level
, img
)) {
893 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
894 /* not an error, do nothing */
898 texObj
= _mesa_get_current_tex_object(ctx
, target
);
899 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
901 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
903 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
905 _mesa_get_format_name(texImage
->TexFormat
),
906 texImage
->Width
, texImage
->Height
);
909 _mesa_lock_texture(ctx
, texObj
);
911 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
914 _mesa_unlock_texture(ctx
, texObj
);