Merge branch 'hiz' of ssh://people.freedesktop.org/~chadversary/mesa
[mesa.git] / src / mesa / main / texgetimage.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.7
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 "enums.h"
35 #include "context.h"
36 #include "formats.h"
37 #include "format_unpack.h"
38 #include "image.h"
39 #include "mfeatures.h"
40 #include "mtypes.h"
41 #include "pack.h"
42 #include "pbo.h"
43 #include "texcompress.h"
44 #include "texgetimage.h"
45 #include "teximage.h"
46
47
48
49 /**
50 * Can the given type represent negative values?
51 */
52 static inline GLboolean
53 type_needs_clamping(GLenum type)
54 {
55 switch (type) {
56 case GL_BYTE:
57 case GL_SHORT:
58 case GL_INT:
59 case GL_FLOAT:
60 case GL_HALF_FLOAT_ARB:
61 case GL_UNSIGNED_INT_10F_11F_11F_REV:
62 case GL_UNSIGNED_INT_5_9_9_9_REV:
63 return GL_FALSE;
64 default:
65 return GL_TRUE;
66 }
67 }
68
69
70 /**
71 * glGetTexImage for depth/Z pixels.
72 */
73 static void
74 get_tex_depth(struct gl_context *ctx, GLuint dimensions,
75 GLenum format, GLenum type, GLvoid *pixels,
76 struct gl_texture_image *texImage)
77 {
78 const GLint width = texImage->Width;
79 const GLint height = texImage->Height;
80 const GLint depth = texImage->Depth;
81 GLint img, row;
82 GLfloat *depthRow = (GLfloat *) malloc(width * sizeof(GLfloat));
83
84 if (!depthRow) {
85 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
86 return;
87 }
88
89 for (img = 0; img < depth; img++) {
90 GLubyte *srcMap;
91 GLint srcRowStride;
92
93 /* map src texture buffer */
94 ctx->Driver.MapTextureImage(ctx, texImage, img,
95 0, 0, width, height, GL_MAP_READ_BIT,
96 &srcMap, &srcRowStride);
97
98 for (row = 0; row < height; row++) {
99 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
100 width, height, format, type,
101 img, row, 0);
102 const GLubyte *src = srcMap + row * srcRowStride;
103 _mesa_unpack_float_z_row(texImage->TexFormat, width, src, depthRow);
104 _mesa_pack_depth_span(ctx, width, dest, type, depthRow, &ctx->Pack);
105 }
106
107 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
108 }
109
110 free(depthRow);
111 }
112
113
114 /**
115 * glGetTexImage for depth/stencil pixels.
116 */
117 static void
118 get_tex_depth_stencil(struct gl_context *ctx, GLuint dimensions,
119 GLenum format, GLenum type, GLvoid *pixels,
120 struct gl_texture_image *texImage)
121 {
122 const GLint width = texImage->Width;
123 const GLint height = texImage->Height;
124 const GLint depth = texImage->Depth;
125 GLint img, row;
126
127 for (img = 0; img < depth; img++) {
128 GLubyte *srcMap;
129 GLint rowstride;
130
131 /* map src texture buffer */
132 ctx->Driver.MapTextureImage(ctx, texImage, img,
133 0, 0, width, height, GL_MAP_READ_BIT,
134 &srcMap, &rowstride);
135
136 for (row = 0; row < height; row++) {
137 const GLubyte *src = srcMap + row * rowstride;
138 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
139 width, height, format, type,
140 img, row, 0);
141 /* XXX Z24_S8 vs. S8_Z24??? */
142 memcpy(dest, src, width * sizeof(GLuint));
143 if (ctx->Pack.SwapBytes) {
144 _mesa_swap4((GLuint *) dest, width);
145 }
146 }
147
148 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
149 }
150 }
151
152
153 /**
154 * glGetTexImage for YCbCr pixels.
155 */
156 static void
157 get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
158 GLenum format, GLenum type, GLvoid *pixels,
159 struct gl_texture_image *texImage)
160 {
161 const GLint width = texImage->Width;
162 const GLint height = texImage->Height;
163 const GLint depth = texImage->Depth;
164 GLint img, row;
165
166 for (img = 0; img < depth; img++) {
167 GLubyte *srcMap;
168 GLint rowstride;
169
170 /* map src texture buffer */
171 ctx->Driver.MapTextureImage(ctx, texImage, img,
172 0, 0, width, height, GL_MAP_READ_BIT,
173 &srcMap, &rowstride);
174
175 for (row = 0; row < height; row++) {
176 const GLubyte *src = srcMap + row * rowstride;
177 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
178 width, height, format, type,
179 img, row, 0);
180 memcpy(dest, src, width * sizeof(GLushort));
181
182 /* check for byte swapping */
183 if ((texImage->TexFormat == MESA_FORMAT_YCBCR
184 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
185 (texImage->TexFormat == MESA_FORMAT_YCBCR_REV
186 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
187 if (!ctx->Pack.SwapBytes)
188 _mesa_swap2((GLushort *) dest, width);
189 }
190 else if (ctx->Pack.SwapBytes) {
191 _mesa_swap2((GLushort *) dest, width);
192 }
193 }
194
195 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
196 }
197 }
198
199
200 /**
201 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
202 * Compressed textures are handled here as well.
203 */
204 static void
205 get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
206 GLenum format, GLenum type, GLvoid *pixels,
207 struct gl_texture_image *texImage)
208 {
209 /* don't want to apply sRGB -> RGB conversion here so override the format */
210 const gl_format texFormat = _mesa_get_srgb_format_linear(texImage->TexFormat);
211 const GLuint width = texImage->Width;
212 const GLuint height = texImage->Height;
213 const GLuint depth = texImage->Depth;
214 const GLenum dataType = _mesa_get_format_datatype(texFormat);
215 const GLenum baseFormat = _mesa_get_format_base_format(texFormat);
216 /* Normally, no pixel transfer ops are performed during glGetTexImage.
217 * The only possible exception is component clamping to [0,1].
218 */
219 GLbitfield transferOps = 0x0;
220
221 /* In general, clamping does not apply to glGetTexImage, except when
222 * the returned type of the image can't hold negative values.
223 */
224 if (type_needs_clamping(type)) {
225 /* the returned image type can't have negative values */
226 if (dataType == GL_FLOAT ||
227 dataType == GL_SIGNED_NORMALIZED ||
228 format == GL_LUMINANCE ||
229 format == GL_LUMINANCE_ALPHA) {
230 transferOps |= IMAGE_CLAMP_BIT;
231 }
232 }
233
234 if (_mesa_is_format_compressed(texFormat)) {
235 /* Decompress into temp buffer, then pack into user buffer */
236 GLfloat *tempImage, *srcRow;
237 GLuint row;
238
239 tempImage = (GLfloat *) malloc(texImage->Width * texImage->Height *
240 texImage->Depth * 4 * sizeof(GLfloat));
241 if (!tempImage) {
242 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
243 return;
244 }
245
246 /* Decompress the texture image - results in 'tempImage' */
247 {
248 GLubyte *srcMap;
249 GLint srcRowStride;
250 GLuint bytes, bw, bh;
251
252 bytes = _mesa_get_format_bytes(texImage->TexFormat);
253 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
254
255 ctx->Driver.MapTextureImage(ctx, texImage, 0,
256 0, 0, width, height,
257 GL_MAP_READ_BIT,
258 &srcMap, &srcRowStride);
259
260 /* XXX This line is a bit of a hack to work around the
261 * mismatch of compressed row strides as returned by
262 * MapTextureImage() vs. what the texture decompression code
263 * uses. This will be fixed in the future.
264 */
265 srcRowStride = srcRowStride * bh / bytes;
266
267 _mesa_decompress_image(texFormat, width, height,
268 srcMap, srcRowStride, tempImage);
269
270 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
271 }
272
273 if (baseFormat == GL_LUMINANCE ||
274 baseFormat == GL_LUMINANCE_ALPHA) {
275 /* Set green and blue to zero since the pack function here will
276 * compute L=R+G+B.
277 */
278 GLuint i;
279 for (i = 0; i < width * height; i++) {
280 tempImage[i * 4 + GCOMP] = tempImage[i * 4 + BCOMP] = 0.0f;
281 }
282 }
283
284 srcRow = tempImage;
285 for (row = 0; row < height; row++) {
286 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
287 width, height, format, type,
288 0, row, 0);
289
290 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) srcRow,
291 format, type, dest, &ctx->Pack, transferOps);
292 srcRow += width * 4;
293 }
294
295 free(tempImage);
296 }
297 else {
298 /* No decompression needed */
299 GLuint img, row;
300 GLfloat (*rgba)[4];
301
302 rgba = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat));
303 if (!rgba) {
304 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
305 return;
306 }
307
308 for (img = 0; img < depth; img++) {
309 GLubyte *srcMap;
310 GLint rowstride;
311
312 /* map src texture buffer */
313 ctx->Driver.MapTextureImage(ctx, texImage, img,
314 0, 0, width, height, GL_MAP_READ_BIT,
315 &srcMap, &rowstride);
316
317 for (row = 0; row < height; row++) {
318 const GLubyte *src = srcMap + row * rowstride;
319 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
320 width, height, format, type,
321 img, row, 0);
322
323 _mesa_unpack_rgba_row(texFormat, width, src, rgba);
324
325 if (texImage->_BaseFormat == GL_ALPHA) {
326 GLint col;
327 for (col = 0; col < width; col++) {
328 rgba[col][RCOMP] = 0.0F;
329 rgba[col][GCOMP] = 0.0F;
330 rgba[col][BCOMP] = 0.0F;
331 }
332 }
333 else if (texImage->_BaseFormat == GL_LUMINANCE) {
334 GLint col;
335 for (col = 0; col < width; col++) {
336 rgba[col][GCOMP] = 0.0F;
337 rgba[col][BCOMP] = 0.0F;
338 rgba[col][ACOMP] = 1.0F;
339 }
340 }
341 else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
342 GLint col;
343 for (col = 0; col < width; col++) {
344 rgba[col][GCOMP] = 0.0F;
345 rgba[col][BCOMP] = 0.0F;
346 }
347 }
348 else if (texImage->_BaseFormat == GL_INTENSITY) {
349 GLint col;
350 for (col = 0; col < width; col++) {
351 rgba[col][GCOMP] = 0.0F;
352 rgba[col][BCOMP] = 0.0F;
353 rgba[col][ACOMP] = 1.0F;
354 }
355 }
356
357 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
358 format, type, dest,
359 &ctx->Pack, transferOps);
360 }
361
362 /* Unmap the src texture buffer */
363 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
364 }
365
366 free(rgba);
367 }
368 }
369
370
371 /**
372 * Try to do glGetTexImage() with simple memcpy().
373 * \return GL_TRUE if done, GL_FALSE otherwise
374 */
375 static GLboolean
376 get_tex_memcpy(struct gl_context *ctx, GLenum format, GLenum type,
377 GLvoid *pixels,
378 struct gl_texture_image *texImage)
379 {
380 const GLenum target = texImage->TexObject->Target;
381 GLboolean memCopy = GL_FALSE;
382
383 /*
384 * Check if the src/dst formats are compatible.
385 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
386 * so we don't have to worry about those.
387 * XXX more format combinations could be supported here.
388 */
389 if (target == GL_TEXTURE_1D ||
390 target == GL_TEXTURE_2D ||
391 target == GL_TEXTURE_RECTANGLE ||
392 _mesa_is_cube_face(target)) {
393 if ((texImage->TexFormat == MESA_FORMAT_ARGB8888 ||
394 texImage->TexFormat == MESA_FORMAT_SARGB8) &&
395 format == GL_BGRA &&
396 (type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
397 !ctx->Pack.SwapBytes &&
398 _mesa_little_endian()) {
399 memCopy = GL_TRUE;
400 }
401 else if ((texImage->TexFormat == MESA_FORMAT_AL88 ||
402 texImage->TexFormat == MESA_FORMAT_SLA8) &&
403 format == GL_LUMINANCE_ALPHA &&
404 type == GL_UNSIGNED_BYTE &&
405 !ctx->Pack.SwapBytes &&
406 _mesa_little_endian()) {
407 memCopy = GL_TRUE;
408 }
409 else if ((texImage->TexFormat == MESA_FORMAT_L8 ||
410 texImage->TexFormat == MESA_FORMAT_SL8) &&
411 format == GL_LUMINANCE &&
412 type == GL_UNSIGNED_BYTE) {
413 memCopy = GL_TRUE;
414 }
415 else if (texImage->TexFormat == MESA_FORMAT_L16 &&
416 format == GL_LUMINANCE &&
417 type == GL_UNSIGNED_SHORT) {
418 memCopy = GL_TRUE;
419 }
420 else if (texImage->TexFormat == MESA_FORMAT_A8 &&
421 format == GL_ALPHA &&
422 type == GL_UNSIGNED_BYTE) {
423 memCopy = GL_TRUE;
424 }
425 else if (texImage->TexFormat == MESA_FORMAT_A16 &&
426 format == GL_ALPHA &&
427 type == GL_UNSIGNED_SHORT) {
428 memCopy = GL_TRUE;
429 }
430 }
431
432 if (memCopy) {
433 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
434 const GLuint bytesPerRow = texImage->Width * bpp;
435 GLubyte *dst =
436 _mesa_image_address2d(&ctx->Pack, pixels, texImage->Width,
437 texImage->Height, format, type, 0, 0);
438 const GLint dstRowStride =
439 _mesa_image_row_stride(&ctx->Pack, texImage->Width, format, type);
440 GLubyte *src;
441 GLint srcRowStride;
442
443 /* map src texture buffer */
444 ctx->Driver.MapTextureImage(ctx, texImage, 0,
445 0, 0, texImage->Width, texImage->Height,
446 GL_MAP_READ_BIT, &src, &srcRowStride);
447
448 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
449 memcpy(dst, src, bytesPerRow * texImage->Height);
450 }
451 else {
452 GLuint row;
453 for (row = 0; row < texImage->Height; row++) {
454 memcpy(dst, src, bytesPerRow);
455 dst += dstRowStride;
456 src += srcRowStride;
457 }
458 }
459
460 /* unmap src texture buffer */
461 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
462 }
463
464 return memCopy;
465 }
466
467
468 /**
469 * This is the software fallback for Driver.GetTexImage().
470 * All error checking will have been done before this routine is called.
471 * We'll call ctx->Driver.MapTextureImage() to access the data, then
472 * unmap with ctx->Driver.UnmapTextureImage().
473 */
474 void
475 _mesa_get_teximage(struct gl_context *ctx,
476 GLenum format, GLenum type, GLvoid *pixels,
477 struct gl_texture_image *texImage)
478 {
479 GLuint dimensions;
480
481 switch (texImage->TexObject->Target) {
482 case GL_TEXTURE_1D:
483 dimensions = 1;
484 break;
485 case GL_TEXTURE_3D:
486 dimensions = 3;
487 break;
488 default:
489 dimensions = 2;
490 }
491
492 /* map dest buffer, if PBO */
493 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
494 /* Packing texture image into a PBO.
495 * Map the (potentially) VRAM-based buffer into our process space so
496 * we can write into it with the code below.
497 * A hardware driver might use a sophisticated blit to move the
498 * texture data to the PBO if the PBO is in VRAM along with the texture.
499 */
500 GLubyte *buf = (GLubyte *)
501 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
502 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
503 if (!buf) {
504 /* out of memory or other unexpected error */
505 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
506 return;
507 }
508 /* <pixels> was an offset into the PBO.
509 * Now make it a real, client-side pointer inside the mapped region.
510 */
511 pixels = ADD_POINTERS(buf, pixels);
512 }
513
514 if (get_tex_memcpy(ctx, format, type, pixels, texImage)) {
515 /* all done */
516 }
517 else if (format == GL_DEPTH_COMPONENT) {
518 get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
519 }
520 else if (format == GL_DEPTH_STENCIL_EXT) {
521 get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
522 }
523 else if (format == GL_YCBCR_MESA) {
524 get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
525 }
526 else {
527 get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
528 }
529
530 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
531 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
532 }
533 }
534
535
536
537 /**
538 * This is the software fallback for Driver.GetCompressedTexImage().
539 * All error checking will have been done before this routine is called.
540 */
541 void
542 _mesa_get_compressed_teximage(struct gl_context *ctx, GLenum target, GLint level,
543 GLvoid *img,
544 struct gl_texture_object *texObj,
545 struct gl_texture_image *texImage)
546 {
547 const GLuint row_stride =
548 _mesa_format_row_stride(texImage->TexFormat, texImage->Width);
549 GLuint i;
550 GLubyte *src;
551 GLint srcRowStride;
552
553 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
554 /* pack texture image into a PBO */
555 GLubyte *buf = (GLubyte *)
556 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
557 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
558 if (!buf) {
559 /* out of memory or other unexpected error */
560 _mesa_error(ctx, GL_OUT_OF_MEMORY,
561 "glGetCompresssedTexImage(map PBO failed)");
562 return;
563 }
564 img = ADD_POINTERS(buf, img);
565 }
566
567 /* map src texture buffer */
568 ctx->Driver.MapTextureImage(ctx, texImage, 0,
569 0, 0, texImage->Width, texImage->Height,
570 GL_MAP_READ_BIT, &src, &srcRowStride);
571
572 /* no pixelstore or pixel transfer, but respect stride */
573
574 if (row_stride == srcRowStride) {
575 const GLuint size = _mesa_format_image_size(texImage->TexFormat,
576 texImage->Width,
577 texImage->Height,
578 texImage->Depth);
579 memcpy(img, src, size);
580 }
581 else {
582 GLuint bw, bh;
583 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
584 for (i = 0; i < (texImage->Height + bh - 1) / bh; i++) {
585 memcpy((GLubyte *)img + i * row_stride,
586 (GLubyte *)src + i * srcRowStride,
587 row_stride);
588 }
589 }
590
591 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
592
593 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
594 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
595 }
596 }
597
598
599
600 /**
601 * Do error checking for a glGetTexImage() call.
602 * \return GL_TRUE if any error, GL_FALSE if no errors.
603 */
604 static GLboolean
605 getteximage_error_check(struct gl_context *ctx, GLenum target, GLint level,
606 GLenum format, GLenum type, GLsizei clientMemSize,
607 GLvoid *pixels )
608 {
609 struct gl_texture_object *texObj;
610 struct gl_texture_image *texImage;
611 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
612 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
613 GLenum baseFormat;
614
615 if (maxLevels == 0) {
616 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target=0x%x)", target);
617 return GL_TRUE;
618 }
619
620 if (level < 0 || level >= maxLevels) {
621 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
622 return GL_TRUE;
623 }
624
625 if (_mesa_sizeof_packed_type(type) <= 0) {
626 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
627 return GL_TRUE;
628 }
629
630 if (_mesa_components_in_format(format) <= 0 ||
631 format == GL_STENCIL_INDEX ||
632 format == GL_COLOR_INDEX) {
633 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
634 return GL_TRUE;
635 }
636
637 if (!ctx->Extensions.ARB_depth_texture && _mesa_is_depth_format(format)) {
638 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
639 return GL_TRUE;
640 }
641
642 if (!ctx->Extensions.MESA_ycbcr_texture && _mesa_is_ycbcr_format(format)) {
643 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
644 return GL_TRUE;
645 }
646
647 if (!ctx->Extensions.EXT_packed_depth_stencil
648 && _mesa_is_depthstencil_format(format)) {
649 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
650 return GL_TRUE;
651 }
652
653 if (!ctx->Extensions.ATI_envmap_bumpmap
654 && _mesa_is_dudv_format(format)) {
655 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
656 return GL_TRUE;
657 }
658
659 texObj = _mesa_get_current_tex_object(ctx, target);
660
661 if (!texObj || _mesa_is_proxy_texture(target)) {
662 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
663 return GL_TRUE;
664 }
665
666 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
667 if (!texImage) {
668 /* out of memory */
669 return GL_TRUE;
670 }
671
672 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
673
674 /* Make sure the requested image format is compatible with the
675 * texture's format.
676 */
677 if (_mesa_is_color_format(format)
678 && !_mesa_is_color_format(baseFormat)) {
679 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
680 return GL_TRUE;
681 }
682 else if (_mesa_is_depth_format(format)
683 && !_mesa_is_depth_format(baseFormat)
684 && !_mesa_is_depthstencil_format(baseFormat)) {
685 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
686 return GL_TRUE;
687 }
688 else if (_mesa_is_ycbcr_format(format)
689 && !_mesa_is_ycbcr_format(baseFormat)) {
690 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
691 return GL_TRUE;
692 }
693 else if (_mesa_is_depthstencil_format(format)
694 && !_mesa_is_depthstencil_format(baseFormat)) {
695 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
696 return GL_TRUE;
697 }
698 else if (_mesa_is_dudv_format(format)
699 && !_mesa_is_dudv_format(baseFormat)) {
700 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
701 return GL_TRUE;
702 }
703
704 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
705 texImage->Height, texImage->Depth,
706 format, type, clientMemSize, pixels)) {
707 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
708 _mesa_error(ctx, GL_INVALID_OPERATION,
709 "glGetTexImage(out of bounds PBO access)");
710 } else {
711 _mesa_error(ctx, GL_INVALID_OPERATION,
712 "glGetnTexImageARB(out of bounds access:"
713 " bufSize (%d) is too small)", clientMemSize);
714 }
715 return GL_TRUE;
716 }
717
718 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
719 /* PBO should not be mapped */
720 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
721 _mesa_error(ctx, GL_INVALID_OPERATION,
722 "glGetTexImage(PBO is mapped)");
723 return GL_TRUE;
724 }
725 }
726
727 return GL_FALSE;
728 }
729
730
731
732 /**
733 * Get texture image. Called by glGetTexImage.
734 *
735 * \param target texture target.
736 * \param level image level.
737 * \param format pixel data format for returned image.
738 * \param type pixel data type for returned image.
739 * \param bufSize size of the pixels data buffer.
740 * \param pixels returned pixel data.
741 */
742 void GLAPIENTRY
743 _mesa_GetnTexImageARB( GLenum target, GLint level, GLenum format,
744 GLenum type, GLsizei bufSize, GLvoid *pixels )
745 {
746 struct gl_texture_object *texObj;
747 struct gl_texture_image *texImage;
748 GET_CURRENT_CONTEXT(ctx);
749 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
750
751 if (getteximage_error_check(ctx, target, level, format, type,
752 bufSize, pixels)) {
753 return;
754 }
755
756 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
757 /* not an error, do nothing */
758 return;
759 }
760
761 texObj = _mesa_get_current_tex_object(ctx, target);
762 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
763
764 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
765 _mesa_debug(ctx, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
766 " dstFmt=0x%x, dstType=0x%x\n",
767 texObj->Name,
768 _mesa_get_format_name(texImage->TexFormat),
769 texImage->Width, texImage->Height,
770 format, type);
771 }
772
773 _mesa_lock_texture(ctx, texObj);
774 {
775 ctx->Driver.GetTexImage(ctx, format, type, pixels, texImage);
776 }
777 _mesa_unlock_texture(ctx, texObj);
778 }
779
780
781 void GLAPIENTRY
782 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
783 GLenum type, GLvoid *pixels )
784 {
785 _mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
786 }
787
788
789 /**
790 * Do error checking for a glGetCompressedTexImage() call.
791 * \return GL_TRUE if any error, GL_FALSE if no errors.
792 */
793 static GLboolean
794 getcompressedteximage_error_check(struct gl_context *ctx, GLenum target,
795 GLint level, GLsizei clientMemSize, GLvoid *img)
796 {
797 struct gl_texture_object *texObj;
798 struct gl_texture_image *texImage;
799 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
800 GLuint compressedSize;
801
802 if (maxLevels == 0) {
803 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImage(target=0x%x)",
804 target);
805 return GL_TRUE;
806 }
807
808 if (level < 0 || level >= maxLevels) {
809 _mesa_error(ctx, GL_INVALID_VALUE,
810 "glGetCompressedTexImageARB(bad level = %d)", level);
811 return GL_TRUE;
812 }
813
814 if (_mesa_is_proxy_texture(target)) {
815 _mesa_error(ctx, GL_INVALID_ENUM,
816 "glGetCompressedTexImageARB(bad target = %s)",
817 _mesa_lookup_enum_by_nr(target));
818 return GL_TRUE;
819 }
820
821 texObj = _mesa_get_current_tex_object(ctx, target);
822 if (!texObj) {
823 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
824 return GL_TRUE;
825 }
826
827 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
828
829 if (!texImage) {
830 /* probably invalid mipmap level */
831 _mesa_error(ctx, GL_INVALID_VALUE,
832 "glGetCompressedTexImageARB(level)");
833 return GL_TRUE;
834 }
835
836 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
837 _mesa_error(ctx, GL_INVALID_OPERATION,
838 "glGetCompressedTexImageARB(texture is not compressed)");
839 return GL_TRUE;
840 }
841
842 compressedSize = _mesa_format_image_size(texImage->TexFormat,
843 texImage->Width,
844 texImage->Height,
845 texImage->Depth);
846
847 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
848 /* do bounds checking on writing to client memory */
849 if (clientMemSize < compressedSize) {
850 _mesa_error(ctx, GL_INVALID_OPERATION,
851 "glGetnCompressedTexImageARB(out of bounds access:"
852 " bufSize (%d) is too small)", clientMemSize);
853 return GL_TRUE;
854 }
855 } else {
856 /* do bounds checking on PBO write */
857 if ((const GLubyte *) img + compressedSize >
858 (const GLubyte *) ctx->Pack.BufferObj->Size) {
859 _mesa_error(ctx, GL_INVALID_OPERATION,
860 "glGetCompressedTexImage(out of bounds PBO access)");
861 return GL_TRUE;
862 }
863
864 /* make sure PBO is not mapped */
865 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
866 _mesa_error(ctx, GL_INVALID_OPERATION,
867 "glGetCompressedTexImage(PBO is mapped)");
868 return GL_TRUE;
869 }
870 }
871
872 return GL_FALSE;
873 }
874
875
876 void GLAPIENTRY
877 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
878 GLvoid *img)
879 {
880 struct gl_texture_object *texObj;
881 struct gl_texture_image *texImage;
882 GET_CURRENT_CONTEXT(ctx);
883 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
884
885 if (getcompressedteximage_error_check(ctx, target, level, bufSize, img)) {
886 return;
887 }
888
889 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !img) {
890 /* not an error, do nothing */
891 return;
892 }
893
894 texObj = _mesa_get_current_tex_object(ctx, target);
895 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
896
897 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
898 _mesa_debug(ctx,
899 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
900 texObj->Name,
901 _mesa_get_format_name(texImage->TexFormat),
902 texImage->Width, texImage->Height);
903 }
904
905 _mesa_lock_texture(ctx, texObj);
906 {
907 ctx->Driver.GetCompressedTexImage(ctx, target, level, img,
908 texObj, texImage);
909 }
910 _mesa_unlock_texture(ctx, texObj);
911 }
912
913 void GLAPIENTRY
914 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
915 {
916 _mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
917 }