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 "texcompress.h"
39 #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(GLcontext
*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
];
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(GLcontext
*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
;
125 for (img
= 0; img
< depth
; img
++) {
126 for (row
= 0; row
< height
; row
++) {
127 GLfloat depthRow
[MAX_WIDTH
];
128 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
129 width
, height
, format
, type
,
133 for (col
= 0; col
< width
; col
++) {
134 texImage
->FetchTexelf(texImage
, col
, row
, img
, depthRow
+ col
);
136 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
143 * glGetTexImage for depth/stencil pixels.
146 get_tex_depth_stencil(GLcontext
*ctx
, GLuint dimensions
,
147 GLenum format
, GLenum type
, GLvoid
*pixels
,
148 const struct gl_texture_image
*texImage
)
150 const GLint width
= texImage
->Width
;
151 const GLint height
= texImage
->Height
;
152 const GLint depth
= texImage
->Depth
;
153 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
156 for (img
= 0; img
< depth
; img
++) {
157 for (row
= 0; row
< height
; row
++) {
158 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
159 width
, height
, format
, type
,
161 _mesa_memcpy(dest
, src
, width
* sizeof(GLuint
));
162 if (ctx
->Pack
.SwapBytes
) {
163 _mesa_swap4((GLuint
*) dest
, width
);
166 src
+= width
* row
+ width
* height
* img
;
173 * glGetTexImage for YCbCr pixels.
176 get_tex_ycbcr(GLcontext
*ctx
, GLuint dimensions
,
177 GLenum format
, GLenum type
, GLvoid
*pixels
,
178 const struct gl_texture_image
*texImage
)
180 const GLint width
= texImage
->Width
;
181 const GLint height
= texImage
->Height
;
182 const GLint depth
= texImage
->Depth
;
183 const GLint rowstride
= texImage
->RowStride
;
184 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
187 for (img
= 0; img
< depth
; img
++) {
188 for (row
= 0; row
< height
; row
++) {
189 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
190 width
, height
, format
, type
,
192 _mesa_memcpy(dest
, src
, width
* sizeof(GLushort
));
194 /* check for byte swapping */
195 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
196 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
197 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
198 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
199 if (!ctx
->Pack
.SwapBytes
)
200 _mesa_swap2((GLushort
*) dest
, width
);
202 else if (ctx
->Pack
.SwapBytes
) {
203 _mesa_swap2((GLushort
*) dest
, width
);
212 #if FEATURE_EXT_texture_sRGB
216 * Convert a float value from linear space to a
217 * non-linear sRGB value in [0, 255].
218 * Not terribly efficient.
220 static INLINE GLfloat
221 linear_to_nonlinear(GLfloat cl
)
223 /* can't have values outside [0, 1] */
225 if (cl
< 0.0031308f
) {
229 cs
= (GLfloat
)(1.055 * _mesa_pow(cl
, 0.41666) - 0.055);
236 * glGetTexImagefor sRGB pixels;
239 get_tex_srgb(GLcontext
*ctx
, GLuint dimensions
,
240 GLenum format
, GLenum type
, GLvoid
*pixels
,
241 const struct gl_texture_image
*texImage
)
243 const GLint width
= texImage
->Width
;
244 const GLint height
= texImage
->Height
;
245 const GLint depth
= texImage
->Depth
;
246 const GLbitfield transferOps
= 0x0;
249 for (img
= 0; img
< depth
; img
++) {
250 for (row
= 0; row
< height
; row
++) {
251 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
252 width
, height
, format
, type
,
255 GLfloat rgba
[MAX_WIDTH
][4];
258 /* convert row to RGBA format */
259 for (col
= 0; col
< width
; col
++) {
260 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
261 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
262 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
263 rgba
[col
][GCOMP
] = 0.0;
264 rgba
[col
][BCOMP
] = 0.0;
266 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
267 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
268 rgba
[col
][GCOMP
] = 0.0;
269 rgba
[col
][BCOMP
] = 0.0;
271 else if (texImage
->_BaseFormat
== GL_RGB
||
272 texImage
->_BaseFormat
== GL_RGBA
) {
273 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
274 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
275 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
278 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
280 &ctx
->Pack
, transferOps
);
286 #else /* FEATURE_EXT_texture_sRGB */
290 get_tex_srgb(GLcontext
*ctx
, GLuint dimensions
,
291 GLenum format
, GLenum type
, GLvoid
*pixels
,
292 const struct gl_texture_image
*texImage
)
298 #endif /* FEATURE_EXT_texture_sRGB */
302 * glGetTexImagefor RGBA, Luminance, etc. pixels.
303 * This is the slow way since we use texture sampling.
306 get_tex_rgba(GLcontext
*ctx
, GLuint dimensions
,
307 GLenum format
, GLenum type
, GLvoid
*pixels
,
308 const struct gl_texture_image
*texImage
)
310 const GLint width
= texImage
->Width
;
311 const GLint height
= texImage
->Height
;
312 const GLint depth
= texImage
->Depth
;
313 /* Normally, no pixel transfer ops are performed during glGetTexImage.
314 * The only possible exception is component clamping to [0,1].
316 GLbitfield transferOps
= 0x0;
319 for (img
= 0; img
< depth
; img
++) {
320 for (row
= 0; row
< height
; row
++) {
321 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
322 width
, height
, format
, type
,
324 GLfloat rgba
[MAX_WIDTH
][4];
326 GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
328 /* clamp does not apply to GetTexImage (final conversion)?
329 * Looks like we need clamp though when going from format
330 * containing negative values to unsigned format.
332 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
) {
333 transferOps
|= IMAGE_CLAMP_BIT
;
335 else if (!type_with_negative_values(type
) &&
336 (dataType
== GL_FLOAT
||
337 dataType
== GL_SIGNED_NORMALIZED
)) {
338 transferOps
|= IMAGE_CLAMP_BIT
;
341 for (col
= 0; col
< width
; col
++) {
342 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
343 if (texImage
->_BaseFormat
== GL_ALPHA
) {
344 rgba
[col
][RCOMP
] = 0.0F
;
345 rgba
[col
][GCOMP
] = 0.0F
;
346 rgba
[col
][BCOMP
] = 0.0F
;
348 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
349 rgba
[col
][GCOMP
] = 0.0F
;
350 rgba
[col
][BCOMP
] = 0.0F
;
351 rgba
[col
][ACOMP
] = 1.0F
;
353 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
354 rgba
[col
][GCOMP
] = 0.0F
;
355 rgba
[col
][BCOMP
] = 0.0F
;
357 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
358 rgba
[col
][GCOMP
] = 0.0F
;
359 rgba
[col
][BCOMP
] = 0.0F
;
360 rgba
[col
][ACOMP
] = 1.0F
;
363 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
365 &ctx
->Pack
, transferOps
);
372 * Try to do glGetTexImage() with simple memcpy().
373 * \return GL_TRUE if done, GL_FALSE otherwise
376 get_tex_memcpy(GLcontext
*ctx
, GLenum format
, GLenum type
, GLvoid
*pixels
,
377 const struct gl_texture_object
*texObj
,
378 const struct gl_texture_image
*texImage
)
380 GLboolean memCopy
= GL_FALSE
;
382 /* Texture image should have been mapped already */
383 assert(texImage
->Data
);
386 * Check if the src/dst formats are compatible.
387 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
388 * so we don't have to worry about those.
389 * XXX more format combinations could be supported here.
391 if ((texObj
->Target
== GL_TEXTURE_1D
||
392 texObj
->Target
== GL_TEXTURE_2D
||
393 texObj
->Target
== GL_TEXTURE_RECTANGLE
||
394 (texObj
->Target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
395 texObj
->Target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
396 if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
&&
398 type
== GL_UNSIGNED_BYTE
&&
399 !ctx
->Pack
.SwapBytes
&&
400 _mesa_little_endian()) {
403 else if (texImage
->TexFormat
== MESA_FORMAT_AL88
&&
404 format
== GL_LUMINANCE_ALPHA
&&
405 type
== GL_UNSIGNED_BYTE
&&
406 !ctx
->Pack
.SwapBytes
&&
407 _mesa_little_endian()) {
410 else if (texImage
->TexFormat
== MESA_FORMAT_L8
&&
411 format
== GL_LUMINANCE
&&
412 type
== GL_UNSIGNED_BYTE
) {
415 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
416 format
== GL_ALPHA
&&
417 type
== GL_UNSIGNED_BYTE
) {
423 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
424 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
426 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
427 texImage
->Height
, format
, type
, 0, 0);
428 const GLint dstRowStride
=
429 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
430 const GLubyte
*src
= texImage
->Data
;
431 const GLint srcRowStride
= texImage
->RowStride
* bpp
;
434 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
435 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
438 for (row
= 0; row
< texImage
->Height
; row
++) {
439 memcpy(dst
, src
, bytesPerRow
);
451 * This is the software fallback for Driver.GetTexImage().
452 * All error checking will have been done before this routine is called.
453 * The texture image must be mapped.
456 _mesa_get_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
457 GLenum format
, GLenum type
, GLvoid
*pixels
,
458 struct gl_texture_object
*texObj
,
459 struct gl_texture_image
*texImage
)
463 /* If we get here, the texture image should be mapped */
464 assert(texImage
->Data
);
477 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
478 /* Packing texture image into a PBO.
479 * Map the (potentially) VRAM-based buffer into our process space so
480 * we can write into it with the code below.
481 * A hardware driver might use a sophisticated blit to move the
482 * texture data to the PBO if the PBO is in VRAM along with the texture.
484 GLubyte
*buf
= (GLubyte
*)
485 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
486 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
488 /* out of memory or other unexpected error */
489 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
492 /* <pixels> was an offset into the PBO.
493 * Now make it a real, client-side pointer inside the mapped region.
495 pixels
= ADD_POINTERS(buf
, pixels
);
498 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texObj
, texImage
)) {
501 else if (format
== GL_COLOR_INDEX
) {
502 get_tex_color_index(ctx
, dimensions
, format
, type
, pixels
, texImage
);
504 else if (format
== GL_DEPTH_COMPONENT
) {
505 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
507 else if (format
== GL_DEPTH_STENCIL_EXT
) {
508 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
510 else if (format
== GL_YCBCR_MESA
) {
511 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
513 else if (_mesa_get_format_color_encoding(texImage
->TexFormat
) == GL_SRGB
) {
514 get_tex_srgb(ctx
, dimensions
, format
, type
, pixels
, texImage
);
517 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
520 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
521 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
522 ctx
->Pack
.BufferObj
);
529 * This is the software fallback for Driver.GetCompressedTexImage().
530 * All error checking will have been done before this routine is called.
533 _mesa_get_compressed_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
535 struct gl_texture_object
*texObj
,
536 struct gl_texture_image
*texImage
)
538 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
543 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
544 /* pack texture image into a PBO */
545 GLubyte
*buf
= (GLubyte
*)
546 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
547 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
549 /* out of memory or other unexpected error */
550 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
551 "glGetCompresssedTexImage(map PBO failed)");
554 img
= ADD_POINTERS(buf
, img
);
557 /* just memcpy, no pixelstore or pixel transfer */
558 _mesa_memcpy(img
, texImage
->Data
, size
);
560 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
561 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
562 ctx
->Pack
.BufferObj
);
569 * Do error checking for a glGetTexImage() call.
570 * \return GL_TRUE if any error, GL_FALSE if no errors.
573 getteximage_error_check(GLcontext
*ctx
, GLenum target
, GLint level
,
574 GLenum format
, GLenum type
, GLvoid
*pixels
)
576 struct gl_texture_object
*texObj
;
577 struct gl_texture_image
*texImage
;
578 const GLuint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
581 if (maxLevels
== 0) {
582 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
586 if (level
< 0 || level
>= maxLevels
) {
587 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
591 if (_mesa_sizeof_packed_type(type
) <= 0) {
592 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
596 if (_mesa_components_in_format(format
) <= 0 ||
597 format
== GL_STENCIL_INDEX
) {
598 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
602 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
603 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
607 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
608 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
612 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
613 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
617 if (!ctx
->Extensions
.EXT_packed_depth_stencil
618 && _mesa_is_depthstencil_format(format
)) {
619 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
623 if (!ctx
->Extensions
.ATI_envmap_bumpmap
624 && _mesa_is_dudv_format(format
)) {
625 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
629 texObj
= _mesa_get_current_tex_object(ctx
, target
);
631 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
632 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
636 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
642 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
644 /* Make sure the requested image format is compatible with the
645 * texture's format. Note that a color index texture can be converted
646 * to RGBA so that combo is allowed.
648 if (_mesa_is_color_format(format
)
649 && !_mesa_is_color_format(baseFormat
)
650 && !_mesa_is_index_format(baseFormat
)) {
651 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
654 else if (_mesa_is_index_format(format
)
655 && !_mesa_is_index_format(baseFormat
)) {
656 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
659 else if (_mesa_is_depth_format(format
)
660 && !_mesa_is_depth_format(baseFormat
)
661 && !_mesa_is_depthstencil_format(baseFormat
)) {
662 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
665 else if (_mesa_is_ycbcr_format(format
)
666 && !_mesa_is_ycbcr_format(baseFormat
)) {
667 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
670 else if (_mesa_is_depthstencil_format(format
)
671 && !_mesa_is_depthstencil_format(baseFormat
)) {
672 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
675 else if (_mesa_is_dudv_format(format
)
676 && !_mesa_is_dudv_format(baseFormat
)) {
677 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
681 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
682 /* packing texture image into a PBO */
683 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
684 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
685 texImage
->Height
, texImage
->Depth
,
686 format
, type
, pixels
)) {
687 _mesa_error(ctx
, GL_INVALID_OPERATION
,
688 "glGetTexImage(out of bounds PBO write)");
692 /* PBO should not be mapped */
693 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
694 _mesa_error(ctx
, GL_INVALID_OPERATION
,
695 "glGetTexImage(PBO is mapped)");
706 * Get texture image. Called by glGetTexImage.
708 * \param target texture target.
709 * \param level image level.
710 * \param format pixel data format for returned image.
711 * \param type pixel data type for returned image.
712 * \param pixels returned pixel data.
715 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
716 GLenum type
, GLvoid
*pixels
)
718 struct gl_texture_object
*texObj
;
719 struct gl_texture_image
*texImage
;
720 GET_CURRENT_CONTEXT(ctx
);
721 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
723 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
727 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
728 /* not an error, do nothing */
732 texObj
= _mesa_get_current_tex_object(ctx
, target
);
733 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
735 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
736 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
737 " dstFmt=0x%x, dstType=0x%x\n",
739 _mesa_get_format_name(texImage
->TexFormat
),
740 texImage
->Width
, texImage
->Height
,
744 _mesa_lock_texture(ctx
, texObj
);
746 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
749 _mesa_unlock_texture(ctx
, texObj
);
755 * Do error checking for a glGetCompressedTexImage() call.
756 * \return GL_TRUE if any error, GL_FALSE if no errors.
759 getcompressedteximage_error_check(GLcontext
*ctx
, GLenum target
, GLint level
,
762 struct gl_texture_object
*texObj
;
763 struct gl_texture_image
*texImage
;
764 const GLuint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
766 if (maxLevels
== 0) {
767 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
772 if (level
< 0 || level
>= maxLevels
) {
773 _mesa_error(ctx
, GL_INVALID_VALUE
,
774 "glGetCompressedTexImageARB(bad level = %d)", level
);
778 if (_mesa_is_proxy_texture(target
)) {
779 _mesa_error(ctx
, GL_INVALID_ENUM
,
780 "glGetCompressedTexImageARB(bad target = %s)",
781 _mesa_lookup_enum_by_nr(target
));
785 texObj
= _mesa_get_current_tex_object(ctx
, target
);
787 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
791 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
794 /* probably invalid mipmap level */
795 _mesa_error(ctx
, GL_INVALID_VALUE
,
796 "glGetCompressedTexImageARB(level)");
800 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
801 _mesa_error(ctx
, GL_INVALID_OPERATION
,
802 "glGetCompressedTexImageARB(texture is not compressed)");
806 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
807 GLuint compressedSize
;
809 /* make sure PBO is not mapped */
810 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
811 _mesa_error(ctx
, GL_INVALID_OPERATION
,
812 "glGetCompressedTexImage(PBO is mapped)");
816 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
821 /* do bounds checking on PBO write */
822 if ((const GLubyte
*) img
+ compressedSize
>
823 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
824 _mesa_error(ctx
, GL_INVALID_OPERATION
,
825 "glGetCompressedTexImage(out of bounds PBO write)");
835 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
837 struct gl_texture_object
*texObj
;
838 struct gl_texture_image
*texImage
;
839 GET_CURRENT_CONTEXT(ctx
);
840 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
842 if (getcompressedteximage_error_check(ctx
, target
, level
, img
)) {
846 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
847 /* not an error, do nothing */
851 texObj
= _mesa_get_current_tex_object(ctx
, target
);
852 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
854 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
856 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
858 _mesa_get_format_name(texImage
->TexFormat
),
859 texImage
->Width
, texImage
->Height
);
862 _mesa_lock_texture(ctx
, texObj
);
864 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
867 _mesa_unlock_texture(ctx
, texObj
);