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"
45 #if FEATURE_EXT_texture_sRGB
48 * Convert a float value from linear space to a
49 * non-linear sRGB value in [0, 255].
50 * Not terribly efficient.
53 linear_to_nonlinear(GLfloat cl
)
55 /* can't have values outside [0, 1] */
57 if (cl
< 0.0031308f
) {
61 cs
= (GLfloat
)(1.055 * _mesa_pow(cl
, 0.41666) - 0.055);
66 #endif /* FEATURE_EXT_texture_sRGB */
70 * Can the given type represent negative values?
72 static INLINE GLboolean
73 type_with_negative_values(GLenum type
)
80 case GL_HALF_FLOAT_ARB
:
89 * This is the software fallback for Driver.GetTexImage().
90 * All error checking will have been done before this routine is called.
93 _mesa_get_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
94 GLenum format
, GLenum type
, GLvoid
*pixels
,
95 struct gl_texture_object
*texObj
,
96 struct gl_texture_image
*texImage
)
98 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
100 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
101 /* Packing texture image into a PBO.
102 * Map the (potentially) VRAM-based buffer into our process space so
103 * we can write into it with the code below.
104 * A hardware driver might use a sophisticated blit to move the
105 * texture data to the PBO if the PBO is in VRAM along with the texture.
107 GLubyte
*buf
= (GLubyte
*)
108 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
109 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
111 /* out of memory or other unexpected error */
112 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
116 /* <pixels> was an offset into the PBO.
117 * Now make it a real, client-side pointer inside the mapped region.
119 pixels
= ADD_POINTERS(buf
, pixels
);
127 const GLint width
= texImage
->Width
;
128 const GLint height
= texImage
->Height
;
129 const GLint depth
= texImage
->Depth
;
131 for (img
= 0; img
< depth
; img
++) {
132 for (row
= 0; row
< height
; row
++) {
133 /* compute destination address in client memory */
134 GLvoid
*dest
= _mesa_image_address( dimensions
, &ctx
->Pack
, pixels
,
135 width
, height
, format
, type
,
139 if (format
== GL_COLOR_INDEX
) {
140 GLuint indexRow
[MAX_WIDTH
];
142 GLuint indexBits
= _mesa_get_format_bits(texImage
->TexFormat
, GL_TEXTURE_INDEX_SIZE_EXT
);
143 /* Can't use FetchTexel here because that returns RGBA */
144 if (indexBits
== 8) {
145 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
146 src
+= width
* (img
* texImage
->Height
+ row
);
147 for (col
= 0; col
< width
; col
++) {
148 indexRow
[col
] = src
[col
];
151 else if (indexBits
== 16) {
152 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
153 src
+= width
* (img
* texImage
->Height
+ row
);
154 for (col
= 0; col
< width
; col
++) {
155 indexRow
[col
] = src
[col
];
160 "Color index problem in _mesa_GetTexImage");
162 _mesa_pack_index_span(ctx
, width
, type
, dest
,
163 indexRow
, &ctx
->Pack
,
164 0 /* no image transfer */);
166 else if (format
== GL_DEPTH_COMPONENT
) {
167 GLfloat depthRow
[MAX_WIDTH
];
169 for (col
= 0; col
< width
; col
++) {
170 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
,
173 _mesa_pack_depth_span(ctx
, width
, dest
, type
,
174 depthRow
, &ctx
->Pack
);
176 else if (format
== GL_DEPTH_STENCIL_EXT
) {
177 /* XXX Note: we're bypassing texImage->FetchTexel()! */
178 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
179 src
+= width
* row
+ width
* height
* img
;
180 _mesa_memcpy(dest
, src
, width
* sizeof(GLuint
));
181 if (ctx
->Pack
.SwapBytes
) {
182 _mesa_swap4((GLuint
*) dest
, width
);
185 else if (format
== GL_YCBCR_MESA
) {
186 /* No pixel transfer */
187 const GLint rowstride
= texImage
->RowStride
;
189 (const GLushort
*) texImage
->Data
+ row
* rowstride
,
190 width
* sizeof(GLushort
));
191 /* check for byte swapping */
192 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
193 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
194 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
195 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
196 if (!ctx
->Pack
.SwapBytes
)
197 _mesa_swap2((GLushort
*) dest
, width
);
199 else if (ctx
->Pack
.SwapBytes
) {
200 _mesa_swap2((GLushort
*) dest
, width
);
203 #if FEATURE_EXT_texture_sRGB
204 else if (_mesa_get_format_color_encoding(texImage
->TexFormat
)
206 /* special case this since need to backconvert values */
207 /* convert row to RGBA format */
208 GLfloat rgba
[MAX_WIDTH
][4];
210 GLbitfield transferOps
= 0x0;
212 for (col
= 0; col
< width
; col
++) {
213 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
214 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
215 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
216 rgba
[col
][GCOMP
] = 0.0;
217 rgba
[col
][BCOMP
] = 0.0;
219 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
220 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
221 rgba
[col
][GCOMP
] = 0.0;
222 rgba
[col
][BCOMP
] = 0.0;
224 else if (texImage
->_BaseFormat
== GL_RGB
||
225 texImage
->_BaseFormat
== GL_RGBA
) {
226 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
227 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
228 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
231 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
233 &ctx
->Pack
, transferOps
);
235 #endif /* FEATURE_EXT_texture_sRGB */
237 /* general case: convert row to RGBA format */
238 GLfloat rgba
[MAX_WIDTH
][4];
240 GLbitfield transferOps
= 0x0;
242 _mesa_get_format_datatype(texImage
->TexFormat
);
244 /* clamp does not apply to GetTexImage (final conversion)?
245 * Looks like we need clamp though when going from format
246 * containing negative values to unsigned format.
248 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
)
249 transferOps
|= IMAGE_CLAMP_BIT
;
250 else if (!type_with_negative_values(type
) &&
251 (dataType
== GL_FLOAT
||
252 dataType
== GL_SIGNED_NORMALIZED
))
253 transferOps
|= IMAGE_CLAMP_BIT
;
255 for (col
= 0; col
< width
; col
++) {
256 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
257 if (texImage
->_BaseFormat
== GL_ALPHA
) {
258 rgba
[col
][RCOMP
] = 0.0;
259 rgba
[col
][GCOMP
] = 0.0;
260 rgba
[col
][BCOMP
] = 0.0;
262 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
263 rgba
[col
][GCOMP
] = 0.0;
264 rgba
[col
][BCOMP
] = 0.0;
265 rgba
[col
][ACOMP
] = 1.0;
267 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
268 rgba
[col
][GCOMP
] = 0.0;
269 rgba
[col
][BCOMP
] = 0.0;
271 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
272 rgba
[col
][GCOMP
] = 0.0;
273 rgba
[col
][BCOMP
] = 0.0;
274 rgba
[col
][ACOMP
] = 1.0;
277 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
279 &ctx
->Pack
, transferOps
);
285 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
286 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
287 ctx
->Pack
.BufferObj
);
294 * This is the software fallback for Driver.GetCompressedTexImage().
295 * All error checking will have been done before this routine is called.
298 _mesa_get_compressed_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
300 struct gl_texture_object
*texObj
,
301 struct gl_texture_image
*texImage
)
303 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
308 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
309 /* pack texture image into a PBO */
310 GLubyte
*buf
= (GLubyte
*)
311 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
312 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
314 /* out of memory or other unexpected error */
315 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
316 "glGetCompresssedTexImage(map PBO failed)");
319 img
= ADD_POINTERS(buf
, img
);
326 /* just memcpy, no pixelstore or pixel transfer */
327 _mesa_memcpy(img
, texImage
->Data
, size
);
329 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
330 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
331 ctx
->Pack
.BufferObj
);
338 * Do error checking for a glGetTexImage() call.
339 * \return GL_TRUE if any error, GL_FALSE if no errors.
342 getteximage_error_check(GLcontext
*ctx
, GLenum target
, GLint level
,
343 GLenum format
, GLenum type
, GLvoid
*pixels
)
345 const struct gl_texture_unit
*texUnit
;
346 struct gl_texture_object
*texObj
;
347 struct gl_texture_image
*texImage
;
348 const GLuint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
351 if (maxLevels
== 0) {
352 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
356 if (level
< 0 || level
>= maxLevels
) {
357 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
361 if (_mesa_sizeof_packed_type(type
) <= 0) {
362 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
366 if (_mesa_components_in_format(format
) <= 0 ||
367 format
== GL_STENCIL_INDEX
) {
368 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
372 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
373 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
377 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
378 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
382 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
383 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
387 if (!ctx
->Extensions
.EXT_packed_depth_stencil
388 && _mesa_is_depthstencil_format(format
)) {
389 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
393 if (!ctx
->Extensions
.ATI_envmap_bumpmap
394 && _mesa_is_dudv_format(format
)) {
395 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
399 texUnit
= _mesa_get_current_tex_unit(ctx
);
400 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
402 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
403 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
407 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
413 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
415 /* Make sure the requested image format is compatible with the
416 * texture's format. Note that a color index texture can be converted
417 * to RGBA so that combo is allowed.
419 if (_mesa_is_color_format(format
)
420 && !_mesa_is_color_format(baseFormat
)
421 && !_mesa_is_index_format(baseFormat
)) {
422 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
425 else if (_mesa_is_index_format(format
)
426 && !_mesa_is_index_format(baseFormat
)) {
427 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
430 else if (_mesa_is_depth_format(format
)
431 && !_mesa_is_depth_format(baseFormat
)
432 && !_mesa_is_depthstencil_format(baseFormat
)) {
433 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
436 else if (_mesa_is_ycbcr_format(format
)
437 && !_mesa_is_ycbcr_format(baseFormat
)) {
438 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
441 else if (_mesa_is_depthstencil_format(format
)
442 && !_mesa_is_depthstencil_format(baseFormat
)) {
443 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
446 else if (_mesa_is_dudv_format(format
)
447 && !_mesa_is_dudv_format(baseFormat
)) {
448 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
452 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
453 /* packing texture image into a PBO */
454 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
455 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
456 texImage
->Height
, texImage
->Depth
,
457 format
, type
, pixels
)) {
458 _mesa_error(ctx
, GL_INVALID_OPERATION
,
459 "glGetTexImage(out of bounds PBO write)");
463 /* PBO should not be mapped */
464 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
465 _mesa_error(ctx
, GL_INVALID_OPERATION
,
466 "glGetTexImage(PBO is mapped)");
477 * Get texture image. Called by glGetTexImage.
479 * \param target texture target.
480 * \param level image level.
481 * \param format pixel data format for returned image.
482 * \param type pixel data type for returned image.
483 * \param pixels returned pixel data.
486 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
487 GLenum type
, GLvoid
*pixels
)
489 const struct gl_texture_unit
*texUnit
;
490 struct gl_texture_object
*texObj
;
491 GET_CURRENT_CONTEXT(ctx
);
492 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
494 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
498 texUnit
= _mesa_get_current_tex_unit(ctx
);
499 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
501 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
502 struct gl_texture_image
*texImage
=
503 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
504 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
505 " dstFmt=0x%x, dstType=0x%x\n",
507 _mesa_get_format_name(texImage
->TexFormat
),
508 texImage
->Width
, texImage
->Height
,
512 _mesa_lock_texture(ctx
, texObj
);
514 struct gl_texture_image
*texImage
=
515 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
517 /* typically, this will call _mesa_get_teximage() */
518 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
521 _mesa_unlock_texture(ctx
, texObj
);
527 * Do error checking for a glGetCompressedTexImage() call.
528 * \return GL_TRUE if any error, GL_FALSE if no errors.
531 getcompressedteximage_error_check(GLcontext
*ctx
, GLenum target
, GLint level
,
534 const struct gl_texture_unit
*texUnit
;
535 struct gl_texture_object
*texObj
;
536 struct gl_texture_image
*texImage
;
537 const GLuint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
539 if (maxLevels
== 0) {
540 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
545 if (level
< 0 || level
>= maxLevels
) {
546 _mesa_error(ctx
, GL_INVALID_VALUE
,
547 "glGetCompressedTexImageARB(bad level = %d)", level
);
551 if (_mesa_is_proxy_texture(target
)) {
552 _mesa_error(ctx
, GL_INVALID_ENUM
,
553 "glGetCompressedTexImageARB(bad target = %s)",
554 _mesa_lookup_enum_by_nr(target
));
558 texUnit
= _mesa_get_current_tex_unit(ctx
);
559 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
562 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
566 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
569 /* probably invalid mipmap level */
570 _mesa_error(ctx
, GL_INVALID_VALUE
,
571 "glGetCompressedTexImageARB(level)");
575 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
576 _mesa_error(ctx
, GL_INVALID_OPERATION
,
577 "glGetCompressedTexImageARB(texture is not compressed)");
581 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
582 GLuint compressedSize
;
584 /* make sure PBO is not mapped */
585 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
586 _mesa_error(ctx
, GL_INVALID_OPERATION
,
587 "glGetCompressedTexImage(PBO is mapped)");
591 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
596 /* do bounds checking on PBO write */
597 if ((const GLubyte
*) img
+ compressedSize
>
598 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
599 _mesa_error(ctx
, GL_INVALID_OPERATION
,
600 "glGetCompressedTexImage(out of bounds PBO write)");
610 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
612 const struct gl_texture_unit
*texUnit
;
613 struct gl_texture_object
*texObj
;
614 GET_CURRENT_CONTEXT(ctx
);
615 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
617 if (getcompressedteximage_error_check(ctx
, target
, level
, img
)) {
621 texUnit
= _mesa_get_current_tex_unit(ctx
);
622 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
624 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
625 struct gl_texture_image
*texImage
=
626 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
628 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
630 _mesa_get_format_name(texImage
->TexFormat
),
631 texImage
->Width
, texImage
->Height
);
634 _mesa_lock_texture(ctx
, texObj
);
636 struct gl_texture_image
*texImage
=
637 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
639 /* this typically calls _mesa_get_compressed_teximage() */
640 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
643 _mesa_unlock_texture(ctx
, texObj
);