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