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)");
115 /* <pixels> was an offset into the PBO.
116 * Now make it a real, client-side pointer inside the mapped region.
118 pixels
= ADD_POINTERS(buf
, pixels
);
122 const GLint width
= texImage
->Width
;
123 const GLint height
= texImage
->Height
;
124 const GLint depth
= texImage
->Depth
;
126 for (img
= 0; img
< depth
; img
++) {
127 for (row
= 0; row
< height
; row
++) {
128 /* compute destination address in client memory */
129 GLvoid
*dest
= _mesa_image_address( dimensions
, &ctx
->Pack
, pixels
,
130 width
, height
, format
, type
,
134 if (format
== GL_COLOR_INDEX
) {
135 GLuint indexRow
[MAX_WIDTH
];
137 GLuint indexBits
= _mesa_get_format_bits(texImage
->TexFormat
, GL_TEXTURE_INDEX_SIZE_EXT
);
138 /* Can't use FetchTexel here because that returns RGBA */
139 if (indexBits
== 8) {
140 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
141 src
+= width
* (img
* texImage
->Height
+ row
);
142 for (col
= 0; col
< width
; col
++) {
143 indexRow
[col
] = src
[col
];
146 else if (indexBits
== 16) {
147 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
148 src
+= width
* (img
* texImage
->Height
+ row
);
149 for (col
= 0; col
< width
; col
++) {
150 indexRow
[col
] = src
[col
];
155 "Color index problem in _mesa_GetTexImage");
157 _mesa_pack_index_span(ctx
, width
, type
, dest
,
158 indexRow
, &ctx
->Pack
,
159 0 /* no image transfer */);
161 else if (format
== GL_DEPTH_COMPONENT
) {
162 GLfloat depthRow
[MAX_WIDTH
];
164 for (col
= 0; col
< width
; col
++) {
165 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
,
168 _mesa_pack_depth_span(ctx
, width
, dest
, type
,
169 depthRow
, &ctx
->Pack
);
171 else if (format
== GL_DEPTH_STENCIL_EXT
) {
172 /* XXX Note: we're bypassing texImage->FetchTexel()! */
173 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
174 src
+= width
* row
+ width
* height
* img
;
175 _mesa_memcpy(dest
, src
, width
* sizeof(GLuint
));
176 if (ctx
->Pack
.SwapBytes
) {
177 _mesa_swap4((GLuint
*) dest
, width
);
180 else if (format
== GL_YCBCR_MESA
) {
181 /* No pixel transfer */
182 const GLint rowstride
= texImage
->RowStride
;
184 (const GLushort
*) texImage
->Data
+ row
* rowstride
,
185 width
* sizeof(GLushort
));
186 /* check for byte swapping */
187 if ((texImage
->TexFormat
== MESA_FORMAT_YCBCR
188 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
189 (texImage
->TexFormat
== MESA_FORMAT_YCBCR_REV
190 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
191 if (!ctx
->Pack
.SwapBytes
)
192 _mesa_swap2((GLushort
*) dest
, width
);
194 else if (ctx
->Pack
.SwapBytes
) {
195 _mesa_swap2((GLushort
*) dest
, width
);
198 #if FEATURE_EXT_texture_sRGB
199 else if (_mesa_get_format_color_encoding(texImage
->TexFormat
)
201 /* special case this since need to backconvert values */
202 /* convert row to RGBA format */
203 GLfloat rgba
[MAX_WIDTH
][4];
205 GLbitfield transferOps
= 0x0;
207 for (col
= 0; col
< width
; col
++) {
208 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
209 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
210 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
211 rgba
[col
][GCOMP
] = 0.0;
212 rgba
[col
][BCOMP
] = 0.0;
214 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
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_RGB
||
220 texImage
->_BaseFormat
== GL_RGBA
) {
221 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
222 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
223 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
226 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
228 &ctx
->Pack
, transferOps
);
230 #endif /* FEATURE_EXT_texture_sRGB */
232 /* general case: convert row to RGBA format */
233 GLfloat rgba
[MAX_WIDTH
][4];
235 GLbitfield transferOps
= 0x0;
237 _mesa_get_format_datatype(texImage
->TexFormat
);
239 /* clamp does not apply to GetTexImage (final conversion)?
240 * Looks like we need clamp though when going from format
241 * containing negative values to unsigned format.
243 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
)
244 transferOps
|= IMAGE_CLAMP_BIT
;
245 else if (!type_with_negative_values(type
) &&
246 (dataType
== GL_FLOAT
||
247 dataType
== GL_SIGNED_NORMALIZED
))
248 transferOps
|= IMAGE_CLAMP_BIT
;
250 for (col
= 0; col
< width
; col
++) {
251 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
252 if (texImage
->_BaseFormat
== GL_ALPHA
) {
253 rgba
[col
][RCOMP
] = 0.0;
254 rgba
[col
][GCOMP
] = 0.0;
255 rgba
[col
][BCOMP
] = 0.0;
257 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
258 rgba
[col
][GCOMP
] = 0.0;
259 rgba
[col
][BCOMP
] = 0.0;
260 rgba
[col
][ACOMP
] = 1.0;
262 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
263 rgba
[col
][GCOMP
] = 0.0;
264 rgba
[col
][BCOMP
] = 0.0;
266 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
267 rgba
[col
][GCOMP
] = 0.0;
268 rgba
[col
][BCOMP
] = 0.0;
269 rgba
[col
][ACOMP
] = 1.0;
272 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
274 &ctx
->Pack
, transferOps
);
280 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
281 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
282 ctx
->Pack
.BufferObj
);
289 * This is the software fallback for Driver.GetCompressedTexImage().
290 * All error checking will have been done before this routine is called.
293 _mesa_get_compressed_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
295 struct gl_texture_object
*texObj
,
296 struct gl_texture_image
*texImage
)
298 const GLuint size
= _mesa_format_image_size(texImage
->TexFormat
,
303 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
304 /* pack texture image into a PBO */
305 GLubyte
*buf
= (GLubyte
*)
306 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
307 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
309 /* out of memory or other unexpected error */
310 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
311 "glGetCompresssedTexImage(map PBO failed)");
314 img
= ADD_POINTERS(buf
, img
);
317 /* just memcpy, no pixelstore or pixel transfer */
318 _mesa_memcpy(img
, texImage
->Data
, size
);
320 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
321 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
322 ctx
->Pack
.BufferObj
);
329 * Do error checking for a glGetTexImage() call.
330 * \return GL_TRUE if any error, GL_FALSE if no errors.
333 getteximage_error_check(GLcontext
*ctx
, GLenum target
, GLint level
,
334 GLenum format
, GLenum type
, GLvoid
*pixels
)
336 const struct gl_texture_unit
*texUnit
;
337 struct gl_texture_object
*texObj
;
338 struct gl_texture_image
*texImage
;
339 const GLuint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
342 if (maxLevels
== 0) {
343 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
347 if (level
< 0 || level
>= maxLevels
) {
348 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
352 if (_mesa_sizeof_packed_type(type
) <= 0) {
353 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
357 if (_mesa_components_in_format(format
) <= 0 ||
358 format
== GL_STENCIL_INDEX
) {
359 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
363 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
364 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
368 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
369 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
373 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
374 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
378 if (!ctx
->Extensions
.EXT_packed_depth_stencil
379 && _mesa_is_depthstencil_format(format
)) {
380 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
384 if (!ctx
->Extensions
.ATI_envmap_bumpmap
385 && _mesa_is_dudv_format(format
)) {
386 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
390 texUnit
= _mesa_get_current_tex_unit(ctx
);
391 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
393 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
394 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
398 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
404 baseFormat
= _mesa_get_format_base_format(texImage
->TexFormat
);
406 /* Make sure the requested image format is compatible with the
407 * texture's format. Note that a color index texture can be converted
408 * to RGBA so that combo is allowed.
410 if (_mesa_is_color_format(format
)
411 && !_mesa_is_color_format(baseFormat
)
412 && !_mesa_is_index_format(baseFormat
)) {
413 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
416 else if (_mesa_is_index_format(format
)
417 && !_mesa_is_index_format(baseFormat
)) {
418 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
421 else if (_mesa_is_depth_format(format
)
422 && !_mesa_is_depth_format(baseFormat
)
423 && !_mesa_is_depthstencil_format(baseFormat
)) {
424 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
427 else if (_mesa_is_ycbcr_format(format
)
428 && !_mesa_is_ycbcr_format(baseFormat
)) {
429 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
432 else if (_mesa_is_depthstencil_format(format
)
433 && !_mesa_is_depthstencil_format(baseFormat
)) {
434 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
437 else if (_mesa_is_dudv_format(format
)
438 && !_mesa_is_dudv_format(baseFormat
)) {
439 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
443 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
444 /* packing texture image into a PBO */
445 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
446 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
447 texImage
->Height
, texImage
->Depth
,
448 format
, type
, pixels
)) {
449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
450 "glGetTexImage(out of bounds PBO write)");
454 /* PBO should not be mapped */
455 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
456 _mesa_error(ctx
, GL_INVALID_OPERATION
,
457 "glGetTexImage(PBO is mapped)");
468 * Get texture image. Called by glGetTexImage.
470 * \param target texture target.
471 * \param level image level.
472 * \param format pixel data format for returned image.
473 * \param type pixel data type for returned image.
474 * \param pixels returned pixel data.
477 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
478 GLenum type
, GLvoid
*pixels
)
480 const struct gl_texture_unit
*texUnit
;
481 struct gl_texture_object
*texObj
;
482 struct gl_texture_image
*texImage
;
483 GET_CURRENT_CONTEXT(ctx
);
484 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
486 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
490 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !pixels
) {
491 /* not an error, do nothing */
495 texUnit
= _mesa_get_current_tex_unit(ctx
);
496 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
497 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
499 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
500 _mesa_debug(ctx
, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
501 " dstFmt=0x%x, dstType=0x%x\n",
503 _mesa_get_format_name(texImage
->TexFormat
),
504 texImage
->Width
, texImage
->Height
,
508 _mesa_lock_texture(ctx
, texObj
);
510 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
513 _mesa_unlock_texture(ctx
, texObj
);
519 * Do error checking for a glGetCompressedTexImage() call.
520 * \return GL_TRUE if any error, GL_FALSE if no errors.
523 getcompressedteximage_error_check(GLcontext
*ctx
, GLenum target
, GLint level
,
526 const struct gl_texture_unit
*texUnit
;
527 struct gl_texture_object
*texObj
;
528 struct gl_texture_image
*texImage
;
529 const GLuint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
531 if (maxLevels
== 0) {
532 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImage(target=0x%x)",
537 if (level
< 0 || level
>= maxLevels
) {
538 _mesa_error(ctx
, GL_INVALID_VALUE
,
539 "glGetCompressedTexImageARB(bad level = %d)", level
);
543 if (_mesa_is_proxy_texture(target
)) {
544 _mesa_error(ctx
, GL_INVALID_ENUM
,
545 "glGetCompressedTexImageARB(bad target = %s)",
546 _mesa_lookup_enum_by_nr(target
));
550 texUnit
= _mesa_get_current_tex_unit(ctx
);
551 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
554 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
558 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
561 /* probably invalid mipmap level */
562 _mesa_error(ctx
, GL_INVALID_VALUE
,
563 "glGetCompressedTexImageARB(level)");
567 if (!_mesa_is_format_compressed(texImage
->TexFormat
)) {
568 _mesa_error(ctx
, GL_INVALID_OPERATION
,
569 "glGetCompressedTexImageARB(texture is not compressed)");
573 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
574 GLuint compressedSize
;
576 /* make sure PBO is not mapped */
577 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
578 _mesa_error(ctx
, GL_INVALID_OPERATION
,
579 "glGetCompressedTexImage(PBO is mapped)");
583 compressedSize
= _mesa_format_image_size(texImage
->TexFormat
,
588 /* do bounds checking on PBO write */
589 if ((const GLubyte
*) img
+ compressedSize
>
590 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
591 _mesa_error(ctx
, GL_INVALID_OPERATION
,
592 "glGetCompressedTexImage(out of bounds PBO write)");
602 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
604 const struct gl_texture_unit
*texUnit
;
605 struct gl_texture_object
*texObj
;
606 struct gl_texture_image
*texImage
;
607 GET_CURRENT_CONTEXT(ctx
);
608 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
610 if (getcompressedteximage_error_check(ctx
, target
, level
, img
)) {
614 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) && !img
) {
615 /* not an error, do nothing */
619 texUnit
= _mesa_get_current_tex_unit(ctx
);
620 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
621 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
623 if (MESA_VERBOSE
& (VERBOSE_API
| VERBOSE_TEXTURE
)) {
625 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
627 _mesa_get_format_name(texImage
->TexFormat
),
628 texImage
->Width
, texImage
->Height
);
631 _mesa_lock_texture(ctx
, texObj
);
633 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
636 _mesa_unlock_texture(ctx
, texObj
);