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
;
124 for (img
= 0; img
< depth
; img
++) {
125 for (row
= 0; row
< height
; row
++) {
126 GLfloat depthRow
[MAX_WIDTH
];
127 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
128 width
, height
, format
, type
,
132 for (col
= 0; col
< width
; col
++) {
133 texImage
->FetchTexelf(texImage
, col
, row
, img
, depthRow
+ col
);
135 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
142 * glGetTexImage for depth/stencil pixels.
145 get_tex_depth_stencil(struct gl_context
*ctx
, GLuint dimensions
,
146 GLenum format
, GLenum type
, GLvoid
*pixels
,
147 const struct gl_texture_image
*texImage
)
149 const GLint width
= texImage
->Width
;
150 const GLint height
= texImage
->Height
;
151 const GLint depth
= texImage
->Depth
;
152 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
155 for (img
= 0; img
< depth
; img
++) {
156 for (row
= 0; row
< height
; row
++) {
157 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
158 width
, height
, format
, type
,
160 memcpy(dest
, src
, width
* sizeof(GLuint
));
161 if (ctx
->Pack
.SwapBytes
) {
162 _mesa_swap4((GLuint
*) dest
, width
);
165 src
+= width
* row
+ width
* height
* img
;
172 * glGetTexImage for YCbCr pixels.
175 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
176 GLenum format
, GLenum type
, GLvoid
*pixels
,
177 const struct gl_texture_image
*texImage
)
179 const GLint width
= texImage
->Width
;
180 const GLint height
= texImage
->Height
;
181 const GLint depth
= texImage
->Depth
;
182 const GLint rowstride
= texImage
->RowStride
;
183 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
186 for (img
= 0; img
< depth
; img
++) {
187 for (row
= 0; row
< height
; row
++) {
188 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
189 width
, height
, format
, type
,
191 memcpy(dest
, src
, width
* sizeof(GLushort
));
193 /* check for byte swapping */
194 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
195 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
196 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
197 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
198 if (!ctx
->Pack
.SwapBytes
)
199 _mesa_swap2((GLushort
*) dest
, width
);
201 else if (ctx
->Pack
.SwapBytes
) {
202 _mesa_swap2((GLushort
*) dest
, width
);
211 #if FEATURE_EXT_texture_sRGB
215 * Convert a float value from linear space to a
216 * non-linear sRGB value in [0, 255].
217 * Not terribly efficient.
219 static INLINE GLfloat
220 linear_to_nonlinear(GLfloat cl
)
222 /* can't have values outside [0, 1] */
224 if (cl
< 0.0031308f
) {
228 cs
= (GLfloat
)(1.055 * pow(cl
, 0.41666) - 0.055);
235 * glGetTexImagefor sRGB pixels;
238 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
239 GLenum format
, GLenum type
, GLvoid
*pixels
,
240 const struct gl_texture_image
*texImage
)
242 const GLint width
= texImage
->Width
;
243 const GLint height
= texImage
->Height
;
244 const GLint depth
= texImage
->Depth
;
245 const GLbitfield transferOps
= 0x0;
248 for (img
= 0; img
< depth
; img
++) {
249 for (row
= 0; row
< height
; row
++) {
250 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
251 width
, height
, format
, type
,
254 GLfloat rgba
[MAX_WIDTH
][4];
257 /* convert row to RGBA format */
258 for (col
= 0; col
< width
; col
++) {
259 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
260 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
261 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
262 rgba
[col
][GCOMP
] = 0.0;
263 rgba
[col
][BCOMP
] = 0.0;
265 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
266 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
267 rgba
[col
][GCOMP
] = 0.0;
268 rgba
[col
][BCOMP
] = 0.0;
270 else if (texImage
->_BaseFormat
== GL_RGB
||
271 texImage
->_BaseFormat
== GL_RGBA
) {
272 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
273 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
274 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
277 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
279 &ctx
->Pack
, transferOps
);
285 #else /* FEATURE_EXT_texture_sRGB */
289 get_tex_srgb(struct gl_context
*ctx
, GLuint dimensions
,
290 GLenum format
, GLenum type
, GLvoid
*pixels
,
291 const struct gl_texture_image
*texImage
)
297 #endif /* FEATURE_EXT_texture_sRGB */
301 * glGetTexImagefor RGBA, Luminance, etc. pixels.
302 * This is the slow way since we use texture sampling.
305 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
306 GLenum format
, GLenum type
, GLvoid
*pixels
,
307 const struct gl_texture_image
*texImage
)
309 const GLint width
= texImage
->Width
;
310 const GLint height
= texImage
->Height
;
311 const GLint depth
= texImage
->Depth
;
312 /* Normally, no pixel transfer ops are performed during glGetTexImage.
313 * The only possible exception is component clamping to [0,1].
315 GLbitfield transferOps
= 0x0;
318 for (img
= 0; img
< depth
; img
++) {
319 for (row
= 0; row
< height
; row
++) {
320 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
321 width
, height
, format
, type
,
323 GLfloat rgba
[MAX_WIDTH
][4];
325 GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
327 /* clamp does not apply to GetTexImage (final conversion)?
328 * Looks like we need clamp though when going from format
329 * containing negative values to unsigned format.
331 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
) {
332 transferOps
|= IMAGE_CLAMP_BIT
;
334 else if (!type_with_negative_values(type
) &&
335 (dataType
== GL_FLOAT
||
336 dataType
== GL_SIGNED_NORMALIZED
)) {
337 transferOps
|= IMAGE_CLAMP_BIT
;
340 for (col
= 0; col
< width
; col
++) {
341 texImage
->FetchTexelf(texImage
, col
, row
, img
, rgba
[col
]);
342 if (texImage
->_BaseFormat
== GL_ALPHA
) {
343 rgba
[col
][RCOMP
] = 0.0F
;
344 rgba
[col
][GCOMP
] = 0.0F
;
345 rgba
[col
][BCOMP
] = 0.0F
;
347 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
348 rgba
[col
][GCOMP
] = 0.0F
;
349 rgba
[col
][BCOMP
] = 0.0F
;
350 rgba
[col
][ACOMP
] = 1.0F
;
352 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
353 rgba
[col
][GCOMP
] = 0.0F
;
354 rgba
[col
][BCOMP
] = 0.0F
;
356 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
357 rgba
[col
][GCOMP
] = 0.0F
;
358 rgba
[col
][BCOMP
] = 0.0F
;
359 rgba
[col
][ACOMP
] = 1.0F
;
362 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
364 &ctx
->Pack
, transferOps
);
371 * Try to do glGetTexImage() with simple memcpy().
372 * \return GL_TRUE if done, GL_FALSE otherwise
375 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
, GLvoid
*pixels
,
376 const struct gl_texture_object
*texObj
,
377 const struct gl_texture_image
*texImage
)
379 GLboolean memCopy
= GL_FALSE
;
381 /* Texture image should have been mapped already */
382 assert(texImage
->Data
);
385 * Check if the src/dst formats are compatible.
386 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
387 * so we don't have to worry about those.
388 * XXX more format combinations could be supported here.
390 if ((texObj
->Target
== GL_TEXTURE_1D
||
391 texObj
->Target
== GL_TEXTURE_2D
||
392 texObj
->Target
== GL_TEXTURE_RECTANGLE
||
393 (texObj
->Target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
394 texObj
->Target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
))) {
395 if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
&&
397 type
== GL_UNSIGNED_BYTE
&&
398 !ctx
->Pack
.SwapBytes
&&
399 _mesa_little_endian()) {
402 else if (texImage
->TexFormat
== MESA_FORMAT_AL88
&&
403 format
== GL_LUMINANCE_ALPHA
&&
404 type
== GL_UNSIGNED_BYTE
&&
405 !ctx
->Pack
.SwapBytes
&&
406 _mesa_little_endian()) {
409 else if (texImage
->TexFormat
== MESA_FORMAT_L8
&&
410 format
== GL_LUMINANCE
&&
411 type
== GL_UNSIGNED_BYTE
) {
414 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
415 format
== GL_ALPHA
&&
416 type
== GL_UNSIGNED_BYTE
) {
422 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
423 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
425 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
426 texImage
->Height
, format
, type
, 0, 0);
427 const GLint dstRowStride
=
428 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
429 const GLubyte
*src
= texImage
->Data
;
430 const GLint srcRowStride
= texImage
->RowStride
* bpp
;
433 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
434 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
437 for (row
= 0; row
< texImage
->Height
; row
++) {
438 memcpy(dst
, src
, bytesPerRow
);
450 * This is the software fallback for Driver.GetTexImage().
451 * All error checking will have been done before this routine is called.
452 * The texture image must be mapped.
455 _mesa_get_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
456 GLenum format
, GLenum type
, GLvoid
*pixels
,
457 struct gl_texture_object
*texObj
,
458 struct gl_texture_image
*texImage
)
462 /* If we get here, the texture image should be mapped */
463 assert(texImage
->Data
);
476 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
477 /* Packing texture image into a PBO.
478 * Map the (potentially) VRAM-based buffer into our process space so
479 * we can write into it with the code below.
480 * A hardware driver might use a sophisticated blit to move the
481 * texture data to the PBO if the PBO is in VRAM along with the texture.
483 GLubyte
*buf
= (GLubyte
*)
484 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
485 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
487 /* out of memory or other unexpected error */
488 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
491 /* <pixels> was an offset into the PBO.
492 * Now make it a real, client-side pointer inside the mapped region.
494 pixels
= ADD_POINTERS(buf
, pixels
);
497 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texObj
, texImage
)) {
500 else if (format
== GL_COLOR_INDEX
) {
501 get_tex_color_index(ctx
, dimensions
, format
, type
, pixels
, texImage
);
503 else if (format
== GL_DEPTH_COMPONENT
) {
504 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
506 else if (format
== GL_DEPTH_STENCIL_EXT
) {
507 get_tex_depth_stencil(ctx
, dimensions
, format
, type
, pixels
, texImage
);
509 else if (format
== GL_YCBCR_MESA
) {
510 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
512 else if (_mesa_get_format_color_encoding(texImage
->TexFormat
) == GL_SRGB
) {
513 get_tex_srgb(ctx
, dimensions
, format
, type
, pixels
, texImage
);
516 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
519 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
520 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
521 ctx
->Pack
.BufferObj
);
528 * This is the software fallback for Driver.GetCompressedTexImage().
529 * All error checking will have been done before this routine is called.
532 _mesa_get_compressed_teximage(struct gl_context
*ctx
, GLenum target
, GLint level
,
534 struct gl_texture_object
*texObj
,
535 struct gl_texture_image
*texImage
)
537 const GLuint row_stride
= _mesa_format_row_stride(texImage
->TexFormat
,
539 const GLuint row_stride_stored
= _mesa_format_row_stride(texImage
->TexFormat
,
540 texImage
->RowStride
);
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 /* no pixelstore or pixel transfer, but respect stride */
559 if (row_stride
== row_stride_stored
) {
560 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
564 memcpy(img
, texImage
->Data
, size
);
568 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
569 for (i
= 0; i
< (texImage
->Height
+ bh
- 1) / bh
; i
++) {
570 memcpy((GLubyte
*)img
+ i
* row_stride
,
571 (GLubyte
*)texImage
->Data
+ i
* row_stride_stored
,
576 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
577 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
578 ctx
->Pack
.BufferObj
);
585 * Do error checking for a glGetTexImage() call.
586 * \return GL_TRUE if any error, GL_FALSE if no errors.
589 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
590 GLenum format
, GLenum type
, GLvoid
*pixels
)
592 struct gl_texture_object
*texObj
;
593 struct gl_texture_image
*texImage
;
594 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
597 if (maxLevels
== 0) {
598 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
602 if (level
< 0 || level
>= maxLevels
) {
603 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
607 if (_mesa_sizeof_packed_type(type
) <= 0) {
608 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
612 if (_mesa_components_in_format(format
) <= 0 ||
613 format
== GL_STENCIL_INDEX
) {
614 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
618 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
619 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
623 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
624 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
628 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
629 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
633 if (!ctx
->Extensions
.EXT_packed_depth_stencil
634 && _mesa_is_depthstencil_format(format
)) {
635 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
639 if (!ctx
->Extensions
.ATI_envmap_bumpmap
640 && _mesa_is_dudv_format(format
)) {
641 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
645 texObj
= _mesa_get_current_tex_object(ctx
, target
);
647 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
648 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
652 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
658 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
660 /* Make sure the requested image format is compatible with the
661 * texture's format. Note that a color index texture can be converted
662 * to RGBA so that combo is allowed.
664 if (_mesa_is_color_format(format
)
665 && !_mesa_is_color_format(baseFormat
)
666 && !_mesa_is_index_format(baseFormat
)) {
667 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
670 else if (_mesa_is_index_format(format
)
671 && !_mesa_is_index_format(baseFormat
)) {
672 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
675 else if (_mesa_is_depth_format(format
)
676 && !_mesa_is_depth_format(baseFormat
)
677 && !_mesa_is_depthstencil_format(baseFormat
)) {
678 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
681 else if (_mesa_is_ycbcr_format(format
)
682 && !_mesa_is_ycbcr_format(baseFormat
)) {
683 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
686 else if (_mesa_is_depthstencil_format(format
)
687 && !_mesa_is_depthstencil_format(baseFormat
)) {
688 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
691 else if (_mesa_is_dudv_format(format
)
692 && !_mesa_is_dudv_format(baseFormat
)) {
693 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
697 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
698 /* packing texture image into a PBO */
699 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
700 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
701 texImage
->Height
, texImage
->Depth
,
702 format
, type
, pixels
)) {
703 _mesa_error(ctx
, GL_INVALID_OPERATION
,
704 "glGetTexImage(out of bounds PBO write)");
708 /* PBO should not be mapped */
709 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
710 _mesa_error(ctx
, GL_INVALID_OPERATION
,
711 "glGetTexImage(PBO is mapped)");
722 * Get texture image. Called by glGetTexImage.
724 * \param target texture target.
725 * \param level image level.
726 * \param format pixel data format for returned image.
727 * \param type pixel data type for returned image.
728 * \param pixels returned pixel data.
731 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
732 GLenum type
, GLvoid
*pixels
)
734 struct gl_texture_object
*texObj
;
735 struct gl_texture_image
*texImage
;
736 GET_CURRENT_CONTEXT(ctx
);
737 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
739 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
743 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
744 /* not an error, do nothing */
748 texObj
= _mesa_get_current_tex_object(ctx
, target
);
749 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
751 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
752 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
753 " dstFmt=0x%x, dstType=0x%x\n",
755 _mesa_get_format_name(texImage
->TexFormat
),
756 texImage
->Width
, texImage
->Height
,
760 _mesa_lock_texture(ctx
, texObj
);
762 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
765 _mesa_unlock_texture(ctx
, texObj
);
771 * Do error checking for a glGetCompressedTexImage() call.
772 * \return GL_TRUE if any error, GL_FALSE if no errors.
775 getcompressedteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
778 struct gl_texture_object
*texObj
;
779 struct gl_texture_image
*texImage
;
780 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
782 if (maxLevels
== 0) {
783 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
788 if (level
< 0 || level
>= maxLevels
) {
789 _mesa_error(ctx
, GL_INVALID_VALUE
,
790 "glGetCompressedTexImageARB(bad level = %d)", level
);
794 if (_mesa_is_proxy_texture(target
)) {
795 _mesa_error(ctx
, GL_INVALID_ENUM
,
796 "glGetCompressedTexImageARB(bad target = %s)",
797 _mesa_lookup_enum_by_nr(target
));
801 texObj
= _mesa_get_current_tex_object(ctx
, target
);
803 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
807 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
810 /* probably invalid mipmap level */
811 _mesa_error(ctx
, GL_INVALID_VALUE
,
812 "glGetCompressedTexImageARB(level)");
816 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
817 _mesa_error(ctx
, GL_INVALID_OPERATION
,
818 "glGetCompressedTexImageARB(texture is not compressed)");
822 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
823 GLuint compressedSize
;
825 /* make sure PBO is not mapped */
826 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
827 _mesa_error(ctx
, GL_INVALID_OPERATION
,
828 "glGetCompressedTexImage(PBO is mapped)");
832 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
837 /* do bounds checking on PBO write */
838 if ((const GLubyte
*) img
+ compressedSize
>
839 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
840 _mesa_error(ctx
, GL_INVALID_OPERATION
,
841 "glGetCompressedTexImage(out of bounds PBO write)");
851 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
853 struct gl_texture_object
*texObj
;
854 struct gl_texture_image
*texImage
;
855 GET_CURRENT_CONTEXT(ctx
);
856 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
858 if (getcompressedteximage_error_check(ctx
, target
, level
, img
)) {
862 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
863 /* not an error, do nothing */
867 texObj
= _mesa_get_current_tex_object(ctx
, target
);
868 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
870 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
872 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
874 _mesa_get_format_name(texImage
->TexFormat
),
875 texImage
->Width
, texImage
->Height
);
878 _mesa_lock_texture(ctx
, texObj
);
880 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
883 _mesa_unlock_texture(ctx
, texObj
);