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 "texcompress.h"
38 #include "texformat.h"
39 #include "texgetimage.h"
45 #if FEATURE_EXT_texture_sRGB
48 * Test if given texture image is an sRGB format.
51 is_srgb_teximage(const struct gl_texture_image
*texImage
)
53 switch (texImage
->TexFormat
->MesaFormat
) {
54 case MESA_FORMAT_SRGB8
:
55 case MESA_FORMAT_SRGBA8
:
56 case MESA_FORMAT_SARGB8
:
58 case MESA_FORMAT_SLA8
:
59 case MESA_FORMAT_SRGB_DXT1
:
60 case MESA_FORMAT_SRGBA_DXT1
:
61 case MESA_FORMAT_SRGBA_DXT3
:
62 case MESA_FORMAT_SRGBA_DXT5
:
71 * Convert a float value from linear space to a
72 * non-linear sRGB value in [0, 255].
73 * Not terribly efficient.
76 linear_to_nonlinear(GLfloat cl
)
78 /* can't have values outside [0, 1] */
80 if (cl
< 0.0031308f
) {
84 cs
= (GLfloat
)(1.055 * _mesa_pow(cl
, 0.41666) - 0.055);
89 #endif /* FEATURE_EXT_texture_sRGB */
93 * Can the given type represent negative values?
95 static INLINE GLboolean
96 type_with_negative_values(GLenum type
)
103 case GL_HALF_FLOAT_ARB
:
112 * This is the software fallback for Driver.GetTexImage().
113 * All error checking will have been done before this routine is called.
116 _mesa_get_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
117 GLenum format
, GLenum type
, GLvoid
*pixels
,
118 struct gl_texture_object
*texObj
,
119 struct gl_texture_image
*texImage
)
121 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
123 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
124 /* Packing texture image into a PBO.
125 * Map the (potentially) VRAM-based buffer into our process space so
126 * we can write into it with the code below.
127 * A hardware driver might use a sophisticated blit to move the
128 * texture data to the PBO if the PBO is in VRAM along with the texture.
130 GLubyte
*buf
= (GLubyte
*)
131 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
132 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
134 /* out of memory or other unexpected error */
135 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage(map PBO failed)");
139 /* <pixels> was an offset into the PBO.
140 * Now make it a real, client-side pointer inside the mapped region.
142 pixels
= ADD_POINTERS(buf
, pixels
);
150 const GLint width
= texImage
->Width
;
151 const GLint height
= texImage
->Height
;
152 const GLint depth
= texImage
->Depth
;
154 for (img
= 0; img
< depth
; img
++) {
155 for (row
= 0; row
< height
; row
++) {
156 /* compute destination address in client memory */
157 GLvoid
*dest
= _mesa_image_address( dimensions
, &ctx
->Pack
, pixels
,
158 width
, height
, format
, type
,
162 if (format
== GL_COLOR_INDEX
) {
163 GLuint indexRow
[MAX_WIDTH
];
165 /* Can't use FetchTexel here because that returns RGBA */
166 if (texImage
->TexFormat
->IndexBits
== 8) {
167 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
168 src
+= width
* (img
* texImage
->Height
+ row
);
169 for (col
= 0; col
< width
; col
++) {
170 indexRow
[col
] = src
[col
];
173 else if (texImage
->TexFormat
->IndexBits
== 16) {
174 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
175 src
+= width
* (img
* texImage
->Height
+ row
);
176 for (col
= 0; col
< width
; col
++) {
177 indexRow
[col
] = src
[col
];
182 "Color index problem in _mesa_GetTexImage");
184 _mesa_pack_index_span(ctx
, width
, type
, dest
,
185 indexRow
, &ctx
->Pack
,
186 0 /* no image transfer */);
188 else if (format
== GL_DEPTH_COMPONENT
) {
189 GLfloat depthRow
[MAX_WIDTH
];
191 for (col
= 0; col
< width
; col
++) {
192 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
,
195 _mesa_pack_depth_span(ctx
, width
, dest
, type
,
196 depthRow
, &ctx
->Pack
);
198 else if (format
== GL_DEPTH_STENCIL_EXT
) {
199 /* XXX Note: we're bypassing texImage->FetchTexel()! */
200 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
201 src
+= width
* row
+ width
* height
* img
;
202 _mesa_memcpy(dest
, src
, width
* sizeof(GLuint
));
203 if (ctx
->Pack
.SwapBytes
) {
204 _mesa_swap4((GLuint
*) dest
, width
);
207 else if (format
== GL_YCBCR_MESA
) {
208 /* No pixel transfer */
209 const GLint rowstride
= texImage
->RowStride
;
211 (const GLushort
*) texImage
->Data
+ row
* rowstride
,
212 width
* sizeof(GLushort
));
213 /* check for byte swapping */
214 if ((texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR
215 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
216 (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR_REV
217 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
218 if (!ctx
->Pack
.SwapBytes
)
219 _mesa_swap2((GLushort
*) dest
, width
);
221 else if (ctx
->Pack
.SwapBytes
) {
222 _mesa_swap2((GLushort
*) dest
, width
);
225 #if FEATURE_EXT_texture_sRGB
226 else if (is_srgb_teximage(texImage
)) {
227 /* special case this since need to backconvert values */
228 /* convert row to RGBA format */
229 GLfloat rgba
[MAX_WIDTH
][4];
231 GLbitfield transferOps
= 0x0;
233 for (col
= 0; col
< width
; col
++) {
234 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
235 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
236 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
237 rgba
[col
][GCOMP
] = 0.0;
238 rgba
[col
][BCOMP
] = 0.0;
240 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
241 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
242 rgba
[col
][GCOMP
] = 0.0;
243 rgba
[col
][BCOMP
] = 0.0;
245 else if (texImage
->_BaseFormat
== GL_RGB
||
246 texImage
->_BaseFormat
== GL_RGBA
) {
247 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
248 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
249 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
252 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
254 &ctx
->Pack
, transferOps
);
256 #endif /* FEATURE_EXT_texture_sRGB */
258 /* general case: convert row to RGBA format */
259 GLfloat rgba
[MAX_WIDTH
][4];
261 GLbitfield transferOps
= 0x0;
263 /* clamp does not apply to GetTexImage (final conversion)?
264 * Looks like we need clamp though when going from format
265 * containing negative values to unsigned format.
267 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
)
268 transferOps
|= IMAGE_CLAMP_BIT
;
269 else if (!type_with_negative_values(type
) &&
270 (texImage
->TexFormat
->DataType
== GL_FLOAT
||
271 texImage
->TexFormat
->DataType
== GL_SIGNED_NORMALIZED
))
272 transferOps
|= IMAGE_CLAMP_BIT
;
274 for (col
= 0; col
< width
; col
++) {
275 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
276 if (texImage
->_BaseFormat
== GL_ALPHA
) {
277 rgba
[col
][RCOMP
] = 0.0;
278 rgba
[col
][GCOMP
] = 0.0;
279 rgba
[col
][BCOMP
] = 0.0;
281 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
282 rgba
[col
][GCOMP
] = 0.0;
283 rgba
[col
][BCOMP
] = 0.0;
284 rgba
[col
][ACOMP
] = 1.0;
286 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
287 rgba
[col
][GCOMP
] = 0.0;
288 rgba
[col
][BCOMP
] = 0.0;
290 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
291 rgba
[col
][GCOMP
] = 0.0;
292 rgba
[col
][BCOMP
] = 0.0;
293 rgba
[col
][ACOMP
] = 1.0;
296 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
298 &ctx
->Pack
, transferOps
);
304 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
305 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
306 ctx
->Pack
.BufferObj
);
313 * This is the software fallback for Driver.GetCompressedTexImage().
314 * All error checking will have been done before this routine is called.
317 _mesa_get_compressed_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
319 struct gl_texture_object
*texObj
,
320 struct gl_texture_image
*texImage
)
324 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
325 /* pack texture image into a PBO */
326 GLubyte
*buf
= (GLubyte
*)
327 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
328 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
330 /* out of memory or other unexpected error */
331 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
332 "glGetCompresssedTexImage(map PBO failed)");
335 img
= ADD_POINTERS(buf
, img
);
342 /* don't use texImage->CompressedSize since that may be padded out */
343 size
= _mesa_compressed_texture_size(ctx
, texImage
->Width
, texImage
->Height
,
345 texImage
->TexFormat
->MesaFormat
);
347 /* just memcpy, no pixelstore or pixel transfer */
348 _mesa_memcpy(img
, texImage
->Data
, size
);
350 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
351 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
352 ctx
->Pack
.BufferObj
);
359 * Do error checking for a glGetTexImage() call.
360 * \return GL_TRUE if any error, GL_FALSE if no errors.
363 getteximage_error_check(GLcontext
*ctx
, GLenum target
, GLint level
,
364 GLenum format
, GLenum type
, GLvoid
*pixels
)
366 const struct gl_texture_unit
*texUnit
;
367 struct gl_texture_object
*texObj
;
368 struct gl_texture_image
*texImage
;
369 const GLuint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
371 if (maxLevels
== 0) {
372 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
376 if (level
< 0 || level
>= maxLevels
) {
377 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
381 if (_mesa_sizeof_packed_type(type
) <= 0) {
382 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
386 if (_mesa_components_in_format(format
) <= 0 ||
387 format
== GL_STENCIL_INDEX
) {
388 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
392 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
393 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
397 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
398 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
402 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
403 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
407 if (!ctx
->Extensions
.EXT_packed_depth_stencil
408 && _mesa_is_depthstencil_format(format
)) {
409 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
413 if (!ctx
->Extensions
.ATI_envmap_bumpmap
414 && _mesa_is_dudv_format(format
)) {
415 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
419 texUnit
= _mesa_get_current_tex_unit(ctx
);
420 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
422 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
423 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
427 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
433 /* Make sure the requested image format is compatible with the
434 * texture's format. Note that a color index texture can be converted
435 * to RGBA so that combo is allowed.
437 if (_mesa_is_color_format(format
)
438 && !_mesa_is_color_format(texImage
->TexFormat
->BaseFormat
)
439 && !_mesa_is_index_format(texImage
->TexFormat
->BaseFormat
)) {
440 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
443 else if (_mesa_is_index_format(format
)
444 && !_mesa_is_index_format(texImage
->TexFormat
->BaseFormat
)) {
445 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
448 else if (_mesa_is_depth_format(format
)
449 && !_mesa_is_depth_format(texImage
->TexFormat
->BaseFormat
)
450 && !_mesa_is_depthstencil_format(texImage
->TexFormat
->BaseFormat
)) {
451 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
454 else if (_mesa_is_ycbcr_format(format
)
455 && !_mesa_is_ycbcr_format(texImage
->TexFormat
->BaseFormat
)) {
456 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
459 else if (_mesa_is_depthstencil_format(format
)
460 && !_mesa_is_depthstencil_format(texImage
->TexFormat
->BaseFormat
)) {
461 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
464 else if (_mesa_is_dudv_format(format
)
465 && !_mesa_is_dudv_format(texImage
->TexFormat
->BaseFormat
)) {
466 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
470 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
471 /* packing texture image into a PBO */
472 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
473 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
474 texImage
->Height
, texImage
->Depth
,
475 format
, type
, pixels
)) {
476 _mesa_error(ctx
, GL_INVALID_OPERATION
,
477 "glGetTexImage(out of bounds PBO write)");
481 /* PBO should not be mapped */
482 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
483 _mesa_error(ctx
, GL_INVALID_OPERATION
,
484 "glGetTexImage(PBO is mapped)");
495 * Get texture image. Called by glGetTexImage.
497 * \param target texture target.
498 * \param level image level.
499 * \param format pixel data format for returned image.
500 * \param type pixel data type for returned image.
501 * \param pixels returned pixel data.
504 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
505 GLenum type
, GLvoid
*pixels
)
507 const struct gl_texture_unit
*texUnit
;
508 struct gl_texture_object
*texObj
;
509 GET_CURRENT_CONTEXT(ctx
);
510 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
512 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
516 texUnit
= _mesa_get_current_tex_unit(ctx
);
517 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
519 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
520 struct gl_texture_image
*texImage
=
521 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
522 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %d, w=%d, h=%d,"
523 " dstFmt=0x%x, dstType=0x%x\n",
525 texImage
->TexFormat
->MesaFormat
,
526 texImage
->Width
, texImage
->Height
,
530 _mesa_lock_texture(ctx
, texObj
);
532 struct gl_texture_image
*texImage
=
533 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
535 /* typically, this will call _mesa_get_teximage() */
536 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
539 _mesa_unlock_texture(ctx
, texObj
);
545 * Do error checking for a glGetCompressedTexImage() call.
546 * \return GL_TRUE if any error, GL_FALSE if no errors.
549 getcompressedteximage_error_check(GLcontext
*ctx
, GLenum target
, GLint level
,
552 const struct gl_texture_unit
*texUnit
;
553 struct gl_texture_object
*texObj
;
554 struct gl_texture_image
*texImage
;
555 const GLuint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
557 if (maxLevels
== 0) {
558 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
563 if (level
< 0 || level
>= maxLevels
) {
564 _mesa_error(ctx
, GL_INVALID_VALUE
,
565 "glGetCompressedTexImageARB(bad level = %d)", level
);
569 if (_mesa_is_proxy_texture(target
)) {
570 _mesa_error(ctx
, GL_INVALID_ENUM
,
571 "glGetCompressedTexImageARB(bad target = %s)",
572 _mesa_lookup_enum_by_nr(target
));
576 texUnit
= _mesa_get_current_tex_unit(ctx
);
577 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
580 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
584 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
587 /* probably invalid mipmap level */
588 _mesa_error(ctx
, GL_INVALID_VALUE
,
589 "glGetCompressedTexImageARB(level)");
593 if (!texImage
->IsCompressed
) {
594 _mesa_error(ctx
, GL_INVALID_OPERATION
,
595 "glGetCompressedTexImageARB(texture is not compressed)");
599 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
600 /* make sure PBO is not mapped */
601 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
602 _mesa_error(ctx
, GL_INVALID_OPERATION
,
603 "glGetCompressedTexImage(PBO is mapped)");
607 /* do bounds checking on PBO write */
608 if ((const GLubyte
*) img
+ texImage
->CompressedSize
>
609 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
610 _mesa_error(ctx
, GL_INVALID_OPERATION
,
611 "glGetCompressedTexImage(out of bounds PBO write)");
621 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
623 const struct gl_texture_unit
*texUnit
;
624 struct gl_texture_object
*texObj
;
625 GET_CURRENT_CONTEXT(ctx
);
626 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
628 if (getcompressedteximage_error_check(ctx
, target
, level
, img
)) {
632 texUnit
= _mesa_get_current_tex_unit(ctx
);
633 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
635 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
636 struct gl_texture_image
*texImage
=
637 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
639 "glGetCompressedTexImage(tex %u) format = %d, w=%d, h=%d\n",
641 texImage
->TexFormat
->MesaFormat
,
642 texImage
->Width
, texImage
->Height
);
645 _mesa_lock_texture(ctx
, texObj
);
647 struct gl_texture_image
*texImage
=
648 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
650 /* this typically calls _mesa_get_compressed_teximage() */
651 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
654 _mesa_unlock_texture(ctx
, texObj
);