Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / mesa / main / texgetimage.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (c) 2009 VMware, Inc.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
24 */
25
26
27 /**
28 * Code for glGetTexImage() and glGetCompressedTexImage().
29 */
30
31
32 #include "glheader.h"
33 #include "bufferobj.h"
34 #include "context.h"
35 #include "image.h"
36 #include "texcompress.h"
37 #include "texformat.h"
38 #include "texgetimage.h"
39 #include "teximage.h"
40 #include "texstate.h"
41
42
43
44 #if FEATURE_EXT_texture_sRGB
45
46 /**
47 * Test if given texture image is an sRGB format.
48 */
49 static GLboolean
50 is_srgb_teximage(const struct gl_texture_image *texImage)
51 {
52 switch (texImage->TexFormat->MesaFormat) {
53 case MESA_FORMAT_SRGB8:
54 case MESA_FORMAT_SRGBA8:
55 case MESA_FORMAT_SARGB8:
56 case MESA_FORMAT_SL8:
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:
62 return GL_TRUE;
63 default:
64 return GL_FALSE;
65 }
66 }
67
68
69 /**
70 * Convert a float value from linear space to a
71 * non-linear sRGB value in [0, 255].
72 * Not terribly efficient.
73 */
74 static INLINE GLfloat
75 linear_to_nonlinear(GLfloat cl)
76 {
77 /* can't have values outside [0, 1] */
78 GLfloat cs;
79 if (cl < 0.0031308f) {
80 cs = 12.92f * cl;
81 }
82 else {
83 cs = (GLfloat)(1.055 * _mesa_pow(cl, 0.41666) - 0.055);
84 }
85 return cs;
86 }
87
88 #endif /* FEATURE_EXT_texture_sRGB */
89
90
91 /**
92 * Can the given type represent negative values?
93 */
94 static INLINE GLboolean
95 type_with_negative_values(GLenum type)
96 {
97 switch (type) {
98 case GL_BYTE:
99 case GL_SHORT:
100 case GL_INT:
101 case GL_FLOAT:
102 case GL_HALF_FLOAT_ARB:
103 return GL_TRUE;
104 default:
105 return GL_FALSE;
106 }
107 }
108
109
110 /**
111 * This is the software fallback for Driver.GetTexImage().
112 * All error checking will have been done before this routine is called.
113 */
114 void
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)
119 {
120 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
121
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.
128 */
129 GLubyte *buf = (GLubyte *)
130 ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
131 GL_WRITE_ONLY_ARB, ctx->Pack.BufferObj);
132 if (!buf) {
133 /* buffer is already mapped - that's an error */
134 _mesa_error(ctx, GL_INVALID_OPERATION,"glGetTexImage(PBO is mapped)");
135 return;
136 }
137 /* <pixels> was an offset into the PBO.
138 * Now make it a real, client-side pointer inside the mapped region.
139 */
140 pixels = ADD_POINTERS(buf, pixels);
141 }
142 else if (!pixels) {
143 /* not an error */
144 return;
145 }
146
147 {
148 const GLint width = texImage->Width;
149 const GLint height = texImage->Height;
150 const GLint depth = texImage->Depth;
151 GLint img, row;
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,
157 img, row, 0);
158 assert(dest);
159
160 if (format == GL_COLOR_INDEX) {
161 GLuint indexRow[MAX_WIDTH];
162 GLint col;
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];
169 }
170 }
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];
176 }
177 }
178 else {
179 _mesa_problem(ctx,
180 "Color index problem in _mesa_GetTexImage");
181 }
182 _mesa_pack_index_span(ctx, width, type, dest,
183 indexRow, &ctx->Pack,
184 0 /* no image transfer */);
185 }
186 else if (format == GL_DEPTH_COMPONENT) {
187 GLfloat depthRow[MAX_WIDTH];
188 GLint col;
189 for (col = 0; col < width; col++) {
190 (*texImage->FetchTexelf)(texImage, col, row, img,
191 depthRow + col);
192 }
193 _mesa_pack_depth_span(ctx, width, dest, type,
194 depthRow, &ctx->Pack);
195 }
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);
203 }
204 }
205 else if (format == GL_YCBCR_MESA) {
206 /* No pixel transfer */
207 const GLint rowstride = texImage->RowStride;
208 MEMCPY(dest,
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);
218 }
219 else if (ctx->Pack.SwapBytes) {
220 _mesa_swap2((GLushort *) dest, width);
221 }
222 }
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];
228 GLint col;
229 GLbitfield transferOps = 0x0;
230
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;
237 }
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;
242 }
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]);
248 }
249 }
250 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
251 format, type, dest,
252 &ctx->Pack, transferOps);
253 }
254 #endif /* FEATURE_EXT_texture_sRGB */
255 else {
256 /* general case: convert row to RGBA format */
257 GLfloat rgba[MAX_WIDTH][4];
258 GLint col;
259 GLbitfield transferOps = 0x0;
260
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.
264 */
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;
271
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;
278 }
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;
283 }
284 else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
285 rgba[col][GCOMP] = 0.0;
286 rgba[col][BCOMP] = 0.0;
287 }
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;
292 }
293 }
294 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
295 format, type, dest,
296 &ctx->Pack, transferOps);
297 } /* format */
298 } /* row */
299 } /* img */
300 }
301
302 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
303 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
304 ctx->Pack.BufferObj);
305 }
306 }
307
308
309
310 /**
311 * This is the software fallback for Driver.GetCompressedTexImage().
312 * All error checking will have been done before this routine is called.
313 */
314 void
315 _mesa_get_compressed_teximage(GLcontext *ctx, GLenum target, GLint level,
316 GLvoid *img,
317 struct gl_texture_object *texObj,
318 struct gl_texture_image *texImage)
319 {
320 GLuint size;
321
322 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
323 /* pack texture image into a PBO */
324 GLubyte *buf;
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)");
329 return;
330 }
331 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
332 GL_WRITE_ONLY_ARB,
333 ctx->Pack.BufferObj);
334 if (!buf) {
335 /* buffer is already mapped - that's an error */
336 _mesa_error(ctx, GL_INVALID_OPERATION,
337 "glGetCompressedTexImage(PBO is mapped)");
338 return;
339 }
340 img = ADD_POINTERS(buf, img);
341 }
342 else if (!img) {
343 /* not an error */
344 return;
345 }
346
347 /* don't use texImage->CompressedSize since that may be padded out */
348 size = _mesa_compressed_texture_size(ctx, texImage->Width, texImage->Height,
349 texImage->Depth,
350 texImage->TexFormat->MesaFormat);
351
352 /* just memcpy, no pixelstore or pixel transfer */
353 _mesa_memcpy(img, texImage->Data, size);
354
355 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
356 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
357 ctx->Pack.BufferObj);
358 }
359 }
360
361
362
363 /**
364 * Do error checking for a glGetTexImage() call.
365 * \return GL_TRUE if any error, GL_FALSE if no errors.
366 */
367 static GLboolean
368 getteximage_error_check(GLcontext *ctx, GLenum target, GLint level,
369 GLenum format, GLenum type, GLvoid *pixels )
370 {
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);
375
376 if (maxLevels == 0) {
377 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target=0x%x)", target);
378 return GL_TRUE;
379 }
380
381 if (level < 0 || level >= maxLevels) {
382 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
383 return GL_TRUE;
384 }
385
386 if (_mesa_sizeof_packed_type(type) <= 0) {
387 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
388 return GL_TRUE;
389 }
390
391 if (_mesa_components_in_format(format) <= 0 ||
392 format == GL_STENCIL_INDEX) {
393 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
394 return GL_TRUE;
395 }
396
397 if (!ctx->Extensions.EXT_paletted_texture && _mesa_is_index_format(format)) {
398 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
399 return GL_TRUE;
400 }
401
402 if (!ctx->Extensions.ARB_depth_texture && _mesa_is_depth_format(format)) {
403 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
404 return GL_TRUE;
405 }
406
407 if (!ctx->Extensions.MESA_ycbcr_texture && _mesa_is_ycbcr_format(format)) {
408 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
409 return GL_TRUE;
410 }
411
412 if (!ctx->Extensions.EXT_packed_depth_stencil
413 && _mesa_is_depthstencil_format(format)) {
414 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
415 return GL_TRUE;
416 }
417
418 if (!ctx->Extensions.ATI_envmap_bumpmap
419 && _mesa_is_dudv_format(format)) {
420 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
421 return GL_TRUE;
422 }
423
424 texUnit = _mesa_get_current_tex_unit(ctx);
425 texObj = _mesa_select_tex_object(ctx, texUnit, target);
426
427 if (!texObj || _mesa_is_proxy_texture(target)) {
428 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
429 return GL_TRUE;
430 }
431
432 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
433 if (!texImage) {
434 /* out of memory */
435 return GL_TRUE;
436 }
437
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.
441 */
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)");
446 return GL_TRUE;
447 }
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)");
451 return GL_TRUE;
452 }
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)");
457 return GL_TRUE;
458 }
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)");
462 return GL_TRUE;
463 }
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)");
467 return GL_TRUE;
468 }
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)");
472 return GL_TRUE;
473 }
474
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)");
483 return GL_TRUE;
484 }
485 }
486
487 return GL_FALSE;
488 }
489
490
491
492 /**
493 * Get texture image. Called by glGetTexImage.
494 *
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.
500 */
501 void GLAPIENTRY
502 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
503 GLenum type, GLvoid *pixels )
504 {
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);
509
510 if (getteximage_error_check(ctx, target, level, format, type, pixels)) {
511 return;
512 }
513
514 texUnit = _mesa_get_current_tex_unit(ctx);
515 texObj = _mesa_select_tex_object(ctx, texUnit, target);
516
517 _mesa_lock_texture(ctx, texObj);
518 {
519 struct gl_texture_image *texImage =
520 _mesa_select_tex_image(ctx, texObj, target, level);
521
522 /* typically, this will call _mesa_get_teximage() */
523 ctx->Driver.GetTexImage(ctx, target, level, format, type, pixels,
524 texObj, texImage);
525 }
526 _mesa_unlock_texture(ctx, texObj);
527 }
528
529
530 void GLAPIENTRY
531 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
532 {
533 const struct gl_texture_unit *texUnit;
534 struct gl_texture_object *texObj;
535 struct gl_texture_image *texImage;
536 GLint maxLevels;
537 GET_CURRENT_CONTEXT(ctx);
538 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
539
540 texUnit = _mesa_get_current_tex_unit(ctx);
541 texObj = _mesa_select_tex_object(ctx, texUnit, target);
542 if (!texObj) {
543 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB");
544 return;
545 }
546
547 maxLevels = _mesa_max_texture_levels(ctx, target);
548 ASSERT(maxLevels > 0); /* 0 indicates bad target, caught above */
549
550 if (level < 0 || level >= maxLevels) {
551 _mesa_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
552 return;
553 }
554
555 if (_mesa_is_proxy_texture(target)) {
556 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
557 return;
558 }
559
560 _mesa_lock_texture(ctx, texObj);
561 {
562 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
563 if (texImage) {
564 if (texImage->IsCompressed) {
565 /* this typically calls _mesa_get_compressed_teximage() */
566 ctx->Driver.GetCompressedTexImage(ctx, target, level, img,
567 texObj, texImage);
568 }
569 else {
570 _mesa_error(ctx, GL_INVALID_OPERATION,
571 "glGetCompressedTexImageARB");
572 }
573 }
574 else {
575 /* probably invalid mipmap level */
576 _mesa_error(ctx, GL_INVALID_VALUE,
577 "glGetCompressedTexImageARB(level)");
578 }
579 }
580 _mesa_unlock_texture(ctx, texObj);
581 }