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"
37 #include "format_unpack.h"
39 #include "mfeatures.h"
43 #include "texgetimage.h"
49 * Can the given type represent negative values?
51 static inline GLboolean
52 type_needs_clamping(GLenum type
)
59 case GL_HALF_FLOAT_ARB
:
60 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
61 case GL_UNSIGNED_INT_5_9_9_9_REV
:
70 * glGetTexImage for depth/Z pixels.
73 get_tex_depth(struct gl_context
*ctx
, GLuint dimensions
,
74 GLenum format
, GLenum type
, GLvoid
*pixels
,
75 struct gl_texture_image
*texImage
)
77 const GLint width
= texImage
->Width
;
78 const GLint height
= texImage
->Height
;
79 const GLint depth
= texImage
->Depth
;
81 GLfloat
*depthRow
= (GLfloat
*) malloc(width
* sizeof(GLfloat
));
84 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
88 for (img
= 0; img
< depth
; img
++) {
92 /* map src texture buffer */
93 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
94 0, 0, width
, height
, GL_MAP_READ_BIT
,
95 &srcMap
, &srcRowStride
);
98 for (row
= 0; row
< height
; row
++) {
99 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
100 width
, height
, format
, type
,
102 const GLubyte
*src
= srcMap
+ row
* srcRowStride
;
103 _mesa_unpack_float_z_row(texImage
->TexFormat
, width
, src
, depthRow
);
104 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthRow
, &ctx
->Pack
);
107 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
110 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
119 * glGetTexImage for YCbCr pixels.
122 get_tex_ycbcr(struct gl_context
*ctx
, GLuint dimensions
,
123 GLenum format
, GLenum type
, GLvoid
*pixels
,
124 struct gl_texture_image
*texImage
)
126 const GLint width
= texImage
->Width
;
127 const GLint height
= texImage
->Height
;
128 const GLint depth
= texImage
->Depth
;
131 for (img
= 0; img
< depth
; img
++) {
135 /* map src texture buffer */
136 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
137 0, 0, width
, height
, GL_MAP_READ_BIT
,
138 &srcMap
, &rowstride
);
141 for (row
= 0; row
< height
; row
++) {
142 const GLubyte
*src
= srcMap
+ row
* rowstride
;
143 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
144 width
, height
, format
, type
,
146 memcpy(dest
, src
, width
* sizeof(GLushort
));
148 /* check for byte swapping */
149 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
150 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
151 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
152 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
153 if (!ctx
->Pack
.SwapBytes
)
154 _mesa_swap2((GLushort
*) dest
, width
);
156 else if (ctx
->Pack
.SwapBytes
) {
157 _mesa_swap2((GLushort
*) dest
, width
);
161 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
164 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
171 * Get an uncompressed color texture image.
174 get_tex_rgba_uncompressed(struct gl_context
*ctx
, GLuint dimensions
,
175 GLenum format
, GLenum type
, GLvoid
*pixels
,
176 struct gl_texture_image
*texImage
,
177 GLbitfield transferOps
)
179 const gl_format texFormat
= texImage
->TexFormat
;
180 const GLuint width
= texImage
->Width
;
181 const GLenum destBaseFormat
= _mesa_base_tex_format(ctx
, format
);
182 GLenum rebaseFormat
= GL_NONE
;
183 GLuint height
= texImage
->Height
;
184 GLuint depth
= texImage
->Depth
;
187 GLuint (*rgba_uint
)[4];
188 GLboolean is_integer
= _mesa_is_format_integer_color(texImage
->TexFormat
);
190 /* Allocate buffer for one row of texels */
191 rgba
= (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
192 rgba_uint
= (GLuint (*)[4]) rgba
;
194 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
198 if (texImage
->_BaseFormat
== GL_LUMINANCE
||
199 texImage
->_BaseFormat
== GL_INTENSITY
||
200 texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
201 /* If a luminance (or intensity) texture is read back as RGB(A), the
202 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
205 rebaseFormat
= texImage
->_BaseFormat
;
207 else if ((texImage
->_BaseFormat
== GL_RGBA
||
208 texImage
->_BaseFormat
== GL_RGB
) &&
209 (destBaseFormat
== GL_LUMINANCE
||
210 destBaseFormat
== GL_LUMINANCE_ALPHA
||
211 destBaseFormat
== GL_LUMINANCE_INTEGER_EXT
||
212 destBaseFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
)) {
213 /* If we're reading back an RGB(A) texture as luminance then we need
214 * to return L=tex(R). Note, that's different from glReadPixels which
217 rebaseFormat
= GL_LUMINANCE_ALPHA
; /* this covers GL_LUMINANCE too */
220 for (img
= 0; img
< depth
; img
++) {
224 /* map src texture buffer */
225 ctx
->Driver
.MapTextureImage(ctx
, texImage
, img
,
226 0, 0, width
, height
, GL_MAP_READ_BIT
,
227 &srcMap
, &rowstride
);
229 for (row
= 0; row
< height
; row
++) {
230 const GLubyte
*src
= srcMap
+ row
* rowstride
;
231 void *dest
= _mesa_image_address(dimensions
, &ctx
->Pack
, pixels
,
232 width
, height
, format
, type
,
236 _mesa_unpack_uint_rgba_row(texFormat
, width
, src
, rgba_uint
);
238 _mesa_rebase_rgba_uint(width
, rgba_uint
, rebaseFormat
);
239 _mesa_pack_rgba_span_int(ctx
, width
, rgba_uint
,
242 _mesa_unpack_rgba_row(texFormat
, width
, src
, rgba
);
244 _mesa_rebase_rgba_float(width
, rgba
, rebaseFormat
);
245 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
247 &ctx
->Pack
, transferOps
);
251 /* Unmap the src texture buffer */
252 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, img
);
255 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
265 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
266 * Compressed textures are handled here as well.
269 get_tex_rgba(struct gl_context
*ctx
, GLuint dimensions
,
270 GLenum format
, GLenum type
, GLvoid
*pixels
,
271 struct gl_texture_image
*texImage
)
273 const GLenum dataType
= _mesa_get_format_datatype(texImage
->TexFormat
);
274 GLbitfield transferOps
= 0x0;
276 /* In general, clamping does not apply to glGetTexImage, except when
277 * the returned type of the image can't hold negative values.
279 if (type_needs_clamping(type
)) {
280 /* the returned image type can't have negative values */
281 if (dataType
== GL_FLOAT
||
282 dataType
== GL_SIGNED_NORMALIZED
||
283 format
== GL_LUMINANCE
||
284 format
== GL_LUMINANCE_ALPHA
) {
285 transferOps
|= IMAGE_CLAMP_BIT
;
288 /* This applies to RGB, RGBA textures. if the format is either LUMINANCE
289 * or LUMINANCE ALPHA, luminance (L) is computed as L=R+G+B .we need to
290 * clamp the sum to [0,1].
292 else if ((format
== GL_LUMINANCE
||
293 format
== GL_LUMINANCE_ALPHA
) &&
294 dataType
== GL_UNSIGNED_NORMALIZED
) {
295 transferOps
|= IMAGE_CLAMP_BIT
;
297 get_tex_rgba_uncompressed(ctx
, dimensions
, format
, type
,
298 pixels
, texImage
, transferOps
);
303 * Try to do glGetTexImage() with simple memcpy().
304 * \return GL_TRUE if done, GL_FALSE otherwise
307 get_tex_memcpy(struct gl_context
*ctx
, GLenum format
, GLenum type
,
309 struct gl_texture_image
*texImage
)
311 const GLenum target
= texImage
->TexObject
->Target
;
312 GLboolean memCopy
= GL_FALSE
;
315 * Check if the src/dst formats are compatible.
316 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
317 * so we don't have to worry about those.
318 * XXX more format combinations could be supported here.
320 if (target
== GL_TEXTURE_1D
||
321 target
== GL_TEXTURE_2D
||
322 _mesa_is_cube_face(target
)) {
323 if ((texImage
->TexFormat
== MESA_FORMAT_ARGB8888
) &&
325 (type
== GL_UNSIGNED_BYTE
|| type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
326 !ctx
->Pack
.SwapBytes
&&
327 _mesa_little_endian()) {
330 else if ((texImage
->TexFormat
== MESA_FORMAT_AL88
) &&
331 format
== GL_LUMINANCE_ALPHA
&&
332 type
== GL_UNSIGNED_BYTE
&&
333 !ctx
->Pack
.SwapBytes
&&
334 _mesa_little_endian()) {
337 else if ((texImage
->TexFormat
== MESA_FORMAT_L8
) &&
338 format
== GL_LUMINANCE
&&
339 type
== GL_UNSIGNED_BYTE
) {
342 else if (texImage
->TexFormat
== MESA_FORMAT_L16
&&
343 format
== GL_LUMINANCE
&&
344 type
== GL_UNSIGNED_SHORT
) {
347 else if (texImage
->TexFormat
== MESA_FORMAT_A8
&&
348 format
== GL_ALPHA
&&
349 type
== GL_UNSIGNED_BYTE
) {
352 else if (texImage
->TexFormat
== MESA_FORMAT_A16
&&
353 format
== GL_ALPHA
&&
354 type
== GL_UNSIGNED_SHORT
) {
360 const GLuint bpp
= _mesa_get_format_bytes(texImage
->TexFormat
);
361 const GLuint bytesPerRow
= texImage
->Width
* bpp
;
363 _mesa_image_address2d(&ctx
->Pack
, pixels
, texImage
->Width
,
364 texImage
->Height
, format
, type
, 0, 0);
365 const GLint dstRowStride
=
366 _mesa_image_row_stride(&ctx
->Pack
, texImage
->Width
, format
, type
);
370 /* map src texture buffer */
371 ctx
->Driver
.MapTextureImage(ctx
, texImage
, 0,
372 0, 0, texImage
->Width
, texImage
->Height
,
373 GL_MAP_READ_BIT
, &src
, &srcRowStride
);
376 if (bytesPerRow
== dstRowStride
&& bytesPerRow
== srcRowStride
) {
377 memcpy(dst
, src
, bytesPerRow
* texImage
->Height
);
381 for (row
= 0; row
< texImage
->Height
; row
++) {
382 memcpy(dst
, src
, bytesPerRow
);
388 /* unmap src texture buffer */
389 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, 0);
392 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
401 * This is the software fallback for Driver.GetTexImage().
402 * All error checking will have been done before this routine is called.
403 * We'll call ctx->Driver.MapTextureImage() to access the data, then
404 * unmap with ctx->Driver.UnmapTextureImage().
407 _mesa_get_teximage(struct gl_context
*ctx
,
408 GLenum format
, GLenum type
, GLvoid
*pixels
,
409 struct gl_texture_image
*texImage
)
413 switch (texImage
->TexObject
->Target
) {
424 /* map dest buffer, if PBO */
425 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
426 /* Packing texture image into a PBO.
427 * Map the (potentially) VRAM-based buffer into our process space so
428 * we can write into it with the code below.
429 * A hardware driver might use a sophisticated blit to move the
430 * texture data to the PBO if the PBO is in VRAM along with the texture.
432 GLubyte
*buf
= (GLubyte
*)
433 ctx
->Driver
.MapBufferRange(ctx
, 0, ctx
->Pack
.BufferObj
->Size
,
434 GL_MAP_WRITE_BIT
, ctx
->Pack
.BufferObj
);
436 /* out of memory or other unexpected error */
437 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
440 /* <pixels> was an offset into the PBO.
441 * Now make it a real, client-side pointer inside the mapped region.
443 pixels
= ADD_POINTERS(buf
, pixels
);
446 if (get_tex_memcpy(ctx
, format
, type
, pixels
, texImage
)) {
449 else if (format
== GL_DEPTH_COMPONENT
) {
450 get_tex_depth(ctx
, dimensions
, format
, type
, pixels
, texImage
);
452 else if (format
== GL_YCBCR_MESA
) {
453 get_tex_ycbcr(ctx
, dimensions
, format
, type
, pixels
, texImage
);
456 get_tex_rgba(ctx
, dimensions
, format
, type
, pixels
, texImage
);
459 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
460 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Pack
.BufferObj
);
465 * Do error checking for a glGetTexImage() call.
466 * \return GL_TRUE if any error, GL_FALSE if no errors.
469 getteximage_error_check(struct gl_context
*ctx
, GLenum target
, GLint level
,
470 GLenum format
, GLenum type
, GLsizei clientMemSize
,
473 struct gl_texture_object
*texObj
;
474 struct gl_texture_image
*texImage
;
475 const GLint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
476 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
477 GLenum baseFormat
, err
;
479 if (maxLevels
== 0) {
480 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
484 if (level
< 0 || level
>= maxLevels
) {
485 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
489 if (_mesa_sizeof_packed_type(type
) <= 0) {
490 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
494 if (_mesa_components_in_format(format
) <= 0 ||
495 format
== GL_STENCIL_INDEX
||
496 format
== GL_COLOR_INDEX
) {
497 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
501 if (_mesa_is_depth_format(format
)) {
502 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
506 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
507 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
511 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
512 if (err
!= GL_NO_ERROR
) {
513 _mesa_error(ctx
, err
, "glGetTexImage(format/type)");
517 texObj
= _mesa_select_tex_object(ctx
, target
);
519 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
520 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
524 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
526 /* non-existant texture image */
530 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
532 /* Make sure the requested image format is compatible with the
535 if (_mesa_is_color_format(format
)
536 && !_mesa_is_color_format(baseFormat
)) {
537 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
540 else if (_mesa_is_depth_format(format
)
541 && !_mesa_is_depth_format(baseFormat
)) {
542 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
545 else if (_mesa_is_ycbcr_format(format
)
546 && !_mesa_is_ycbcr_format(baseFormat
)) {
547 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
551 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
552 texImage
->Height
, texImage
->Depth
,
553 format
, type
, clientMemSize
, pixels
)) {
554 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
555 _mesa_error(ctx
, GL_INVALID_OPERATION
,
556 "glGetTexImage(out of bounds PBO access)");
558 _mesa_error(ctx
, GL_INVALID_OPERATION
,
559 "glGetnTexImageARB(out of bounds access:"
560 " bufSize (%d) is too small)", clientMemSize
);
565 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
566 /* PBO should not be mapped */
567 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
568 _mesa_error(ctx
, GL_INVALID_OPERATION
,
569 "glGetTexImage(PBO is mapped)");
580 * Get texture image. Called by glGetTexImage.
582 * \param target texture target.
583 * \param level image level.
584 * \param format pixel data format for returned image.
585 * \param type pixel data type for returned image.
586 * \param bufSize size of the pixels data buffer.
587 * \param pixels returned pixel data.
590 _mesa_GetnTexImageARB( GLenum target
, GLint level
, GLenum format
,
591 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
)
593 struct gl_texture_object
*texObj
;
594 struct gl_texture_image
*texImage
;
595 GET_CURRENT_CONTEXT(ctx
);
596 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
598 if (getteximage_error_check(ctx
, target
, level
, format
, type
,
603 if (!_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
604 /* not an error, do nothing */
608 texObj
= _mesa_select_tex_object(ctx
, target
);
609 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
611 if (_mesa_is_zero_size_texture(texImage
))
614 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
615 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
616 " dstFmt=0x%x, dstType=0x%x\n",
618 _mesa_get_format_name(texImage
->TexFormat
),
619 texImage
->Width
, texImage
->Height
,
623 _mesa_lock_texture(ctx
, texObj
);
625 ctx
->Driver
.GetTexImage(ctx
, format
, type
, pixels
, texImage
);
627 _mesa_unlock_texture(ctx
, texObj
);
632 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
633 GLenum type
, GLvoid
*pixels
)
635 _mesa_GetnTexImageARB(target
, level
, format
, type
, INT_MAX
, pixels
);