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"
36 #include "texcompress.h"
37 #include "texformat.h"
38 #include "texgetimage.h"
44 #if FEATURE_EXT_texture_sRGB
47 * Test if given texture image is an sRGB format.
50 is_srgb_teximage(const struct gl_texture_image
*texImage
)
52 switch (texImage
->TexFormat
->MesaFormat
) {
53 case MESA_FORMAT_SRGB8
:
54 case MESA_FORMAT_SRGBA8
:
55 case MESA_FORMAT_SARGB8
:
57 case MESA_FORMAT_SLA8
:
58 case MESA_FORMAT_SRGB_DXT1
:
59 case MESA_FORMAT_SRGBA_DXT1
:
60 case MESA_FORMAT_SRGBA_DXT3
:
61 case MESA_FORMAT_SRGBA_DXT5
:
70 * Convert a float value from linear space to a
71 * non-linear sRGB value in [0, 255].
72 * Not terribly efficient.
75 linear_to_nonlinear(GLfloat cl
)
77 /* can't have values outside [0, 1] */
79 if (cl
< 0.0031308f
) {
83 cs
= (GLfloat
)(1.055 * _mesa_pow(cl
, 0.41666) - 0.055);
88 #endif /* FEATURE_EXT_texture_sRGB */
92 * Can the given type represent negative values?
94 static INLINE GLboolean
95 type_with_negative_values(GLenum type
)
102 case GL_HALF_FLOAT_ARB
:
111 * This is the software fallback for Driver.GetTexImage().
112 * All error checking will have been done before this routine is called.
115 _mesa_get_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
116 GLenum format
, GLenum type
, GLvoid
*pixels
,
117 struct gl_texture_object
*texObj
,
118 struct gl_texture_image
*texImage
)
120 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
122 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
123 /* Packing texture image into a PBO.
124 * Map the (potentially) VRAM-based buffer into our process space so
125 * we can write into it with the code below.
126 * A hardware driver might use a sophisticated blit to move the
127 * texture data to the PBO if the PBO is in VRAM along with the texture.
129 GLubyte
*buf
= (GLubyte
*)
130 ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
131 GL_WRITE_ONLY_ARB
, ctx
->Pack
.BufferObj
);
133 /* buffer is already mapped - that's an error */
134 _mesa_error(ctx
, GL_INVALID_OPERATION
,"glGetTexImage(PBO is mapped)");
137 /* <pixels> was an offset into the PBO.
138 * Now make it a real, client-side pointer inside the mapped region.
140 pixels
= ADD_POINTERS(buf
, pixels
);
148 const GLint width
= texImage
->Width
;
149 const GLint height
= texImage
->Height
;
150 const GLint depth
= texImage
->Depth
;
152 for (img
= 0; img
< depth
; img
++) {
153 for (row
= 0; row
< height
; row
++) {
154 /* compute destination address in client memory */
155 GLvoid
*dest
= _mesa_image_address( dimensions
, &ctx
->Pack
, pixels
,
156 width
, height
, format
, type
,
160 if (format
== GL_COLOR_INDEX
) {
161 GLuint indexRow
[MAX_WIDTH
];
163 /* Can't use FetchTexel here because that returns RGBA */
164 if (texImage
->TexFormat
->IndexBits
== 8) {
165 const GLubyte
*src
= (const GLubyte
*) texImage
->Data
;
166 src
+= width
* (img
* texImage
->Height
+ row
);
167 for (col
= 0; col
< width
; col
++) {
168 indexRow
[col
] = src
[col
];
171 else if (texImage
->TexFormat
->IndexBits
== 16) {
172 const GLushort
*src
= (const GLushort
*) texImage
->Data
;
173 src
+= width
* (img
* texImage
->Height
+ row
);
174 for (col
= 0; col
< width
; col
++) {
175 indexRow
[col
] = src
[col
];
180 "Color index problem in _mesa_GetTexImage");
182 _mesa_pack_index_span(ctx
, width
, type
, dest
,
183 indexRow
, &ctx
->Pack
,
184 0 /* no image transfer */);
186 else if (format
== GL_DEPTH_COMPONENT
) {
187 GLfloat depthRow
[MAX_WIDTH
];
189 for (col
= 0; col
< width
; col
++) {
190 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
,
193 _mesa_pack_depth_span(ctx
, width
, dest
, type
,
194 depthRow
, &ctx
->Pack
);
196 else if (format
== GL_DEPTH_STENCIL_EXT
) {
197 /* XXX Note: we're bypassing texImage->FetchTexel()! */
198 const GLuint
*src
= (const GLuint
*) texImage
->Data
;
199 src
+= width
* row
+ width
* height
* img
;
200 _mesa_memcpy(dest
, src
, width
* sizeof(GLuint
));
201 if (ctx
->Pack
.SwapBytes
) {
202 _mesa_swap4((GLuint
*) dest
, width
);
205 else if (format
== GL_YCBCR_MESA
) {
206 /* No pixel transfer */
207 const GLint rowstride
= texImage
->RowStride
;
209 (const GLushort
*) texImage
->Data
+ row
* rowstride
,
210 width
* sizeof(GLushort
));
211 /* check for byte swapping */
212 if ((texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR
213 && type
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ||
214 (texImage
->TexFormat
->MesaFormat
== MESA_FORMAT_YCBCR_REV
215 && type
== GL_UNSIGNED_SHORT_8_8_MESA
)) {
216 if (!ctx
->Pack
.SwapBytes
)
217 _mesa_swap2((GLushort
*) dest
, width
);
219 else if (ctx
->Pack
.SwapBytes
) {
220 _mesa_swap2((GLushort
*) dest
, width
);
223 #if FEATURE_EXT_texture_sRGB
224 else if (is_srgb_teximage(texImage
)) {
225 /* special case this since need to backconvert values */
226 /* convert row to RGBA format */
227 GLfloat rgba
[MAX_WIDTH
][4];
229 GLbitfield transferOps
= 0x0;
231 for (col
= 0; col
< width
; col
++) {
232 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
233 if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
234 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
235 rgba
[col
][GCOMP
] = 0.0;
236 rgba
[col
][BCOMP
] = 0.0;
238 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
239 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
240 rgba
[col
][GCOMP
] = 0.0;
241 rgba
[col
][BCOMP
] = 0.0;
243 else if (texImage
->_BaseFormat
== GL_RGB
||
244 texImage
->_BaseFormat
== GL_RGBA
) {
245 rgba
[col
][RCOMP
] = linear_to_nonlinear(rgba
[col
][RCOMP
]);
246 rgba
[col
][GCOMP
] = linear_to_nonlinear(rgba
[col
][GCOMP
]);
247 rgba
[col
][BCOMP
] = linear_to_nonlinear(rgba
[col
][BCOMP
]);
250 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
252 &ctx
->Pack
, transferOps
);
254 #endif /* FEATURE_EXT_texture_sRGB */
256 /* general case: convert row to RGBA format */
257 GLfloat rgba
[MAX_WIDTH
][4];
259 GLbitfield transferOps
= 0x0;
261 /* clamp does not apply to GetTexImage (final conversion)?
262 * Looks like we need clamp though when going from format
263 * containing negative values to unsigned format.
265 if (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
)
266 transferOps
|= IMAGE_CLAMP_BIT
;
267 else if (!type_with_negative_values(type
) &&
268 (texImage
->TexFormat
->DataType
== GL_FLOAT
||
269 texImage
->TexFormat
->DataType
== GL_SIGNED_NORMALIZED
))
270 transferOps
|= IMAGE_CLAMP_BIT
;
272 for (col
= 0; col
< width
; col
++) {
273 (*texImage
->FetchTexelf
)(texImage
, col
, row
, img
, rgba
[col
]);
274 if (texImage
->_BaseFormat
== GL_ALPHA
) {
275 rgba
[col
][RCOMP
] = 0.0;
276 rgba
[col
][GCOMP
] = 0.0;
277 rgba
[col
][BCOMP
] = 0.0;
279 else if (texImage
->_BaseFormat
== GL_LUMINANCE
) {
280 rgba
[col
][GCOMP
] = 0.0;
281 rgba
[col
][BCOMP
] = 0.0;
282 rgba
[col
][ACOMP
] = 1.0;
284 else if (texImage
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
285 rgba
[col
][GCOMP
] = 0.0;
286 rgba
[col
][BCOMP
] = 0.0;
288 else if (texImage
->_BaseFormat
== GL_INTENSITY
) {
289 rgba
[col
][GCOMP
] = 0.0;
290 rgba
[col
][BCOMP
] = 0.0;
291 rgba
[col
][ACOMP
] = 1.0;
294 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
,
296 &ctx
->Pack
, transferOps
);
302 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
303 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
304 ctx
->Pack
.BufferObj
);
311 * This is the software fallback for Driver.GetCompressedTexImage().
312 * All error checking will have been done before this routine is called.
315 _mesa_get_compressed_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
317 struct gl_texture_object
*texObj
,
318 struct gl_texture_image
*texImage
)
322 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
323 /* pack texture image into a PBO */
325 if ((const GLubyte
*) img
+ texImage
->CompressedSize
>
326 (const GLubyte
*) ctx
->Pack
.BufferObj
->Size
) {
327 _mesa_error(ctx
, GL_INVALID_OPERATION
,
328 "glGetCompressedTexImage(invalid PBO access)");
331 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
333 ctx
->Pack
.BufferObj
);
335 /* buffer is already mapped - that's an error */
336 _mesa_error(ctx
, GL_INVALID_OPERATION
,
337 "glGetCompressedTexImage(PBO is mapped)");
340 img
= ADD_POINTERS(buf
, img
);
347 /* don't use texImage->CompressedSize since that may be padded out */
348 size
= _mesa_compressed_texture_size(ctx
, texImage
->Width
, texImage
->Height
,
350 texImage
->TexFormat
->MesaFormat
);
352 /* just memcpy, no pixelstore or pixel transfer */
353 _mesa_memcpy(img
, texImage
->Data
, size
);
355 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
356 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
357 ctx
->Pack
.BufferObj
);
364 * Do error checking for a glGetTexImage() call.
365 * \return GL_TRUE if any error, GL_FALSE if no errors.
368 getteximage_error_check(GLcontext
*ctx
, GLenum target
, GLint level
,
369 GLenum format
, GLenum type
, GLvoid
*pixels
)
371 const struct gl_texture_unit
*texUnit
;
372 struct gl_texture_object
*texObj
;
373 struct gl_texture_image
*texImage
;
374 const GLuint maxLevels
= _mesa_max_texture_levels(ctx
, target
);
376 if (maxLevels
== 0) {
377 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target=0x%x)", target
);
381 if (level
< 0 || level
>= maxLevels
) {
382 _mesa_error( ctx
, GL_INVALID_VALUE
, "glGetTexImage(level)" );
386 if (_mesa_sizeof_packed_type(type
) <= 0) {
387 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(type)" );
391 if (_mesa_components_in_format(format
) <= 0 ||
392 format
== GL_STENCIL_INDEX
) {
393 _mesa_error( ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)" );
397 if (!ctx
->Extensions
.EXT_paletted_texture
&& _mesa_is_index_format(format
)) {
398 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
402 if (!ctx
->Extensions
.ARB_depth_texture
&& _mesa_is_depth_format(format
)) {
403 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
407 if (!ctx
->Extensions
.MESA_ycbcr_texture
&& _mesa_is_ycbcr_format(format
)) {
408 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
412 if (!ctx
->Extensions
.EXT_packed_depth_stencil
413 && _mesa_is_depthstencil_format(format
)) {
414 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
418 if (!ctx
->Extensions
.ATI_envmap_bumpmap
419 && _mesa_is_dudv_format(format
)) {
420 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(format)");
424 texUnit
= _mesa_get_current_tex_unit(ctx
);
425 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
427 if (!texObj
|| _mesa_is_proxy_texture(target
)) {
428 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetTexImage(target)");
432 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
438 /* Make sure the requested image format is compatible with the
439 * texture's format. Note that a color index texture can be converted
440 * to RGBA so that combo is allowed.
442 if (_mesa_is_color_format(format
)
443 && !_mesa_is_color_format(texImage
->TexFormat
->BaseFormat
)
444 && !_mesa_is_index_format(texImage
->TexFormat
->BaseFormat
)) {
445 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
448 else if (_mesa_is_index_format(format
)
449 && !_mesa_is_index_format(texImage
->TexFormat
->BaseFormat
)) {
450 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
453 else if (_mesa_is_depth_format(format
)
454 && !_mesa_is_depth_format(texImage
->TexFormat
->BaseFormat
)
455 && !_mesa_is_depthstencil_format(texImage
->TexFormat
->BaseFormat
)) {
456 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
459 else if (_mesa_is_ycbcr_format(format
)
460 && !_mesa_is_ycbcr_format(texImage
->TexFormat
->BaseFormat
)) {
461 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
464 else if (_mesa_is_depthstencil_format(format
)
465 && !_mesa_is_depthstencil_format(texImage
->TexFormat
->BaseFormat
)) {
466 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
469 else if (_mesa_is_dudv_format(format
)
470 && !_mesa_is_dudv_format(texImage
->TexFormat
->BaseFormat
)) {
471 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glGetTexImage(format mismatch)");
475 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
476 /* packing texture image into a PBO */
477 const GLuint dimensions
= (target
== GL_TEXTURE_3D
) ? 3 : 2;
478 if (!_mesa_validate_pbo_access(dimensions
, &ctx
->Pack
, texImage
->Width
,
479 texImage
->Height
, texImage
->Depth
,
480 format
, type
, pixels
)) {
481 _mesa_error(ctx
, GL_INVALID_OPERATION
,
482 "glGetTexImage(invalid PBO access)");
493 * Get texture image. Called by glGetTexImage.
495 * \param target texture target.
496 * \param level image level.
497 * \param format pixel data format for returned image.
498 * \param type pixel data type for returned image.
499 * \param pixels returned pixel data.
502 _mesa_GetTexImage( GLenum target
, GLint level
, GLenum format
,
503 GLenum type
, GLvoid
*pixels
)
505 const struct gl_texture_unit
*texUnit
;
506 struct gl_texture_object
*texObj
;
507 GET_CURRENT_CONTEXT(ctx
);
508 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
510 if (getteximage_error_check(ctx
, target
, level
, format
, type
, pixels
)) {
514 texUnit
= _mesa_get_current_tex_unit(ctx
);
515 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
517 _mesa_lock_texture(ctx
, texObj
);
519 struct gl_texture_image
*texImage
=
520 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
522 /* typically, this will call _mesa_get_teximage() */
523 ctx
->Driver
.GetTexImage(ctx
, target
, level
, format
, type
, pixels
,
526 _mesa_unlock_texture(ctx
, texObj
);
531 _mesa_GetCompressedTexImageARB(GLenum target
, GLint level
, GLvoid
*img
)
533 const struct gl_texture_unit
*texUnit
;
534 struct gl_texture_object
*texObj
;
535 struct gl_texture_image
*texImage
;
537 GET_CURRENT_CONTEXT(ctx
);
538 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
540 texUnit
= _mesa_get_current_tex_unit(ctx
);
541 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
543 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB");
547 maxLevels
= _mesa_max_texture_levels(ctx
, target
);
548 ASSERT(maxLevels
> 0); /* 0 indicates bad target, caught above */
550 if (level
< 0 || level
>= maxLevels
) {
551 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetCompressedTexImageARB(level)");
555 if (_mesa_is_proxy_texture(target
)) {
556 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetCompressedTexImageARB(target)");
560 _mesa_lock_texture(ctx
, texObj
);
562 texImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
564 if (texImage
->IsCompressed
) {
565 /* this typically calls _mesa_get_compressed_teximage() */
566 ctx
->Driver
.GetCompressedTexImage(ctx
, target
, level
, img
,
570 _mesa_error(ctx
, GL_INVALID_OPERATION
,
571 "glGetCompressedTexImageARB");
575 /* probably invalid mipmap level */
576 _mesa_error(ctx
, GL_INVALID_VALUE
,
577 "glGetCompressedTexImageARB(level)");
580 _mesa_unlock_texture(ctx
, texObj
);