98e4aa78c4eaaac711e91561b2ea11611886837e
[mesa.git] / src / mesa / main / texgetimage.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2009 VMware, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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 "glformats.h"
39 #include "image.h"
40 #include "mtypes.h"
41 #include "pack.h"
42 #include "pbo.h"
43 #include "pixelstore.h"
44 #include "texcompress.h"
45 #include "texgetimage.h"
46 #include "teximage.h"
47 #include "texobj.h"
48 #include "texstore.h"
49 #include "format_utils.h"
50 #include "pixeltransfer.h"
51
52 /**
53 * Can the given type represent negative values?
54 */
55 static inline GLboolean
56 type_needs_clamping(GLenum type)
57 {
58 switch (type) {
59 case GL_BYTE:
60 case GL_SHORT:
61 case GL_INT:
62 case GL_FLOAT:
63 case GL_HALF_FLOAT_ARB:
64 case GL_UNSIGNED_INT_10F_11F_11F_REV:
65 case GL_UNSIGNED_INT_5_9_9_9_REV:
66 return GL_FALSE;
67 default:
68 return GL_TRUE;
69 }
70 }
71
72
73 /**
74 * glGetTexImage for depth/Z pixels.
75 */
76 static void
77 get_tex_depth(struct gl_context *ctx, GLuint dimensions,
78 GLint xoffset, GLint yoffset, GLint zoffset,
79 GLsizei width, GLsizei height, GLint depth,
80 GLenum format, GLenum type, GLvoid *pixels,
81 struct gl_texture_image *texImage)
82 {
83 GLint img, row;
84 GLfloat *depthRow = malloc(width * sizeof(GLfloat));
85
86 if (!depthRow) {
87 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
88 return;
89 }
90
91 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
92 depth = height;
93 height = 1;
94 }
95
96 assert(zoffset + depth <= texImage->Depth);
97 for (img = 0; img < depth; img++) {
98 GLubyte *srcMap;
99 GLint srcRowStride;
100
101 /* map src texture buffer */
102 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
103 xoffset, yoffset, width, height,
104 GL_MAP_READ_BIT, &srcMap, &srcRowStride);
105
106 if (srcMap) {
107 for (row = 0; row < height; row++) {
108 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
109 width, height, format, type,
110 img, row, 0);
111 const GLubyte *src = srcMap + row * srcRowStride;
112 _mesa_unpack_float_z_row(texImage->TexFormat, width, src, depthRow);
113 _mesa_pack_depth_span(ctx, width, dest, type, depthRow, &ctx->Pack);
114 }
115
116 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
117 }
118 else {
119 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
120 break;
121 }
122 }
123
124 free(depthRow);
125 }
126
127
128 /**
129 * glGetTexImage for depth/stencil pixels.
130 */
131 static void
132 get_tex_depth_stencil(struct gl_context *ctx, GLuint dimensions,
133 GLint xoffset, GLint yoffset, GLint zoffset,
134 GLsizei width, GLsizei height, GLint depth,
135 GLenum format, GLenum type, GLvoid *pixels,
136 struct gl_texture_image *texImage)
137 {
138 GLint img, row;
139
140 assert(format == GL_DEPTH_STENCIL);
141 assert(type == GL_UNSIGNED_INT_24_8 ||
142 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
143
144 assert(zoffset + depth <= texImage->Depth);
145 for (img = 0; img < depth; img++) {
146 GLubyte *srcMap;
147 GLint rowstride;
148
149 /* map src texture buffer */
150 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
151 xoffset, yoffset, width, height,
152 GL_MAP_READ_BIT, &srcMap, &rowstride);
153
154 if (srcMap) {
155 for (row = 0; row < height; row++) {
156 const GLubyte *src = srcMap + row * rowstride;
157 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
158 width, height, format, type,
159 img, row, 0);
160 _mesa_unpack_depth_stencil_row(texImage->TexFormat,
161 width,
162 (const GLuint *) src,
163 type, dest);
164 if (ctx->Pack.SwapBytes) {
165 _mesa_swap4((GLuint *) dest, width);
166 }
167 }
168
169 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
170 }
171 else {
172 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
173 break;
174 }
175 }
176 }
177
178 /**
179 * glGetTexImage for stencil pixels.
180 */
181 static void
182 get_tex_stencil(struct gl_context *ctx, GLuint dimensions,
183 GLint xoffset, GLint yoffset, GLint zoffset,
184 GLsizei width, GLsizei height, GLint depth,
185 GLenum format, GLenum type, GLvoid *pixels,
186 struct gl_texture_image *texImage)
187 {
188 GLint img, row;
189
190 assert(format == GL_STENCIL_INDEX);
191
192 for (img = 0; img < depth; img++) {
193 GLubyte *srcMap;
194 GLint rowstride;
195
196 /* map src texture buffer */
197 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
198 xoffset, yoffset, width, height,
199 GL_MAP_READ_BIT,
200 &srcMap, &rowstride);
201
202 if (srcMap) {
203 for (row = 0; row < height; row++) {
204 const GLubyte *src = srcMap + row * rowstride;
205 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
206 width, height, format, type,
207 img, row, 0);
208 _mesa_unpack_ubyte_stencil_row(texImage->TexFormat,
209 width,
210 (const GLuint *) src,
211 dest);
212 }
213
214 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
215 }
216 else {
217 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
218 break;
219 }
220 }
221 }
222
223
224 /**
225 * glGetTexImage for YCbCr pixels.
226 */
227 static void
228 get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
229 GLint xoffset, GLint yoffset, GLint zoffset,
230 GLsizei width, GLsizei height, GLint depth,
231 GLenum format, GLenum type, GLvoid *pixels,
232 struct gl_texture_image *texImage)
233 {
234 GLint img, row;
235
236 assert(zoffset + depth <= texImage->Depth);
237 for (img = 0; img < depth; img++) {
238 GLubyte *srcMap;
239 GLint rowstride;
240
241 /* map src texture buffer */
242 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
243 xoffset, yoffset, width, height,
244 GL_MAP_READ_BIT, &srcMap, &rowstride);
245
246 if (srcMap) {
247 for (row = 0; row < height; row++) {
248 const GLubyte *src = srcMap + row * rowstride;
249 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
250 width, height, format, type,
251 img, row, 0);
252 memcpy(dest, src, width * sizeof(GLushort));
253
254 /* check for byte swapping */
255 if ((texImage->TexFormat == MESA_FORMAT_YCBCR
256 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
257 (texImage->TexFormat == MESA_FORMAT_YCBCR_REV
258 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
259 if (!ctx->Pack.SwapBytes)
260 _mesa_swap2((GLushort *) dest, width);
261 }
262 else if (ctx->Pack.SwapBytes) {
263 _mesa_swap2((GLushort *) dest, width);
264 }
265 }
266
267 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
268 }
269 else {
270 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
271 break;
272 }
273 }
274 }
275
276
277 /**
278 * Get a color texture image with decompression.
279 */
280 static void
281 get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
282 GLint xoffset, GLint yoffset, GLint zoffset,
283 GLsizei width, GLsizei height, GLint depth,
284 GLenum format, GLenum type, GLvoid *pixels,
285 struct gl_texture_image *texImage,
286 GLbitfield transferOps)
287 {
288 /* don't want to apply sRGB -> RGB conversion here so override the format */
289 const mesa_format texFormat =
290 _mesa_get_srgb_format_linear(texImage->TexFormat);
291 const GLenum baseFormat = _mesa_get_format_base_format(texFormat);
292 GLfloat *tempImage, *tempSlice;
293 GLuint slice;
294 int srcStride, dstStride;
295 uint32_t dstFormat;
296 bool needsRebase;
297 uint8_t rebaseSwizzle[4];
298
299 /* Decompress into temp float buffer, then pack into user buffer */
300 tempImage = malloc(width * height * depth
301 * 4 * sizeof(GLfloat));
302 if (!tempImage) {
303 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
304 return;
305 }
306
307 /* Decompress the texture image slices - results in 'tempImage' */
308 for (slice = 0; slice < depth; slice++) {
309 GLubyte *srcMap;
310 GLint srcRowStride;
311
312 tempSlice = tempImage + slice * 4 * width * height;
313
314 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
315 xoffset, yoffset, width, height,
316 GL_MAP_READ_BIT,
317 &srcMap, &srcRowStride);
318 if (srcMap) {
319 _mesa_decompress_image(texFormat, width, height,
320 srcMap, srcRowStride, tempSlice);
321
322 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
323 }
324 else {
325 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
326 free(tempImage);
327 return;
328 }
329 }
330
331 /* Depending on the base format involved we may need to apply a rebase
332 * transform (for example: if we download to a Luminance format we want
333 * G=0 and B=0).
334 */
335 if (baseFormat == GL_LUMINANCE ||
336 baseFormat == GL_INTENSITY) {
337 needsRebase = true;
338 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
339 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
340 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
341 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
342 } else if (baseFormat == GL_LUMINANCE_ALPHA) {
343 needsRebase = true;
344 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
345 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
346 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
347 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
348 } else {
349 needsRebase = false;
350 }
351
352 srcStride = 4 * width * sizeof(GLfloat);
353 dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
354 dstFormat = _mesa_format_from_format_and_type(format, type);
355 tempSlice = tempImage;
356 for (slice = 0; slice < depth; slice++) {
357 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
358 width, height, format, type,
359 slice, 0, 0);
360 _mesa_format_convert(dest, dstFormat, dstStride,
361 tempSlice, RGBA32_FLOAT, srcStride,
362 width, height,
363 needsRebase ? rebaseSwizzle : NULL);
364 tempSlice += 4 * width * height;
365 }
366
367 free(tempImage);
368 }
369
370
371 /**
372 * Return a base GL format given the user-requested format
373 * for glGetTexImage().
374 */
375 GLenum
376 _mesa_base_pack_format(GLenum format)
377 {
378 switch (format) {
379 case GL_ABGR_EXT:
380 case GL_BGRA:
381 case GL_BGRA_INTEGER:
382 case GL_RGBA_INTEGER:
383 return GL_RGBA;
384 case GL_BGR:
385 case GL_BGR_INTEGER:
386 case GL_RGB_INTEGER:
387 return GL_RGB;
388 case GL_RED_INTEGER:
389 return GL_RED;
390 case GL_GREEN_INTEGER:
391 return GL_GREEN;
392 case GL_BLUE_INTEGER:
393 return GL_BLUE;
394 case GL_ALPHA_INTEGER:
395 return GL_ALPHA;
396 case GL_LUMINANCE_INTEGER_EXT:
397 return GL_LUMINANCE;
398 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
399 return GL_LUMINANCE_ALPHA;
400 default:
401 return format;
402 }
403 }
404
405
406 /**
407 * Get an uncompressed color texture image.
408 */
409 static void
410 get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
411 GLint xoffset, GLint yoffset, GLint zoffset,
412 GLsizei width, GLsizei height, GLint depth,
413 GLenum format, GLenum type, GLvoid *pixels,
414 struct gl_texture_image *texImage,
415 GLbitfield transferOps)
416 {
417 /* don't want to apply sRGB -> RGB conversion here so override the format */
418 const mesa_format texFormat =
419 _mesa_get_srgb_format_linear(texImage->TexFormat);
420 GLuint img;
421 GLboolean dst_is_integer;
422 uint32_t dst_format;
423 int dst_stride;
424 uint8_t rebaseSwizzle[4];
425 bool needsRebase;
426 void *rgba = NULL;
427
428 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
429 depth = height;
430 height = 1;
431 zoffset = yoffset;
432 yoffset = 0;
433 }
434
435 /* Depending on the base format involved we may need to apply a rebase
436 * transform (for example: if we download to a Luminance format we want
437 * G=0 and B=0).
438 */
439 if (texImage->_BaseFormat == GL_LUMINANCE ||
440 texImage->_BaseFormat == GL_INTENSITY) {
441 needsRebase = true;
442 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
443 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
444 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
445 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
446 } else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
447 needsRebase = true;
448 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
449 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
450 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
451 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
452 } else if (texImage->_BaseFormat != _mesa_get_format_base_format(texFormat)) {
453 needsRebase =
454 _mesa_compute_rgba2base2rgba_component_mapping(texImage->_BaseFormat,
455 rebaseSwizzle);
456 } else {
457 needsRebase = false;
458 }
459
460 /* Describe the dst format */
461 dst_is_integer = _mesa_is_enum_format_integer(format);
462 dst_format = _mesa_format_from_format_and_type(format, type);
463 dst_stride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
464
465 /* Since _mesa_format_convert does not handle transferOps we need to handle
466 * them before we call the function. This requires to convert to RGBA float
467 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
468 * integer then transferOps do not apply.
469 */
470 assert(!transferOps || (transferOps && !dst_is_integer));
471 (void) dst_is_integer; /* silence unused var warning */
472
473 for (img = 0; img < depth; img++) {
474 GLubyte *srcMap;
475 GLint rowstride;
476 GLubyte *img_src;
477 void *dest;
478 void *src;
479 int src_stride;
480 uint32_t src_format;
481
482 /* map src texture buffer */
483 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
484 xoffset, yoffset, width, height,
485 GL_MAP_READ_BIT,
486 &srcMap, &rowstride);
487 if (!srcMap) {
488 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
489 goto done;
490 }
491
492 img_src = srcMap;
493 dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
494 width, height, format, type,
495 img, 0, 0);
496
497 if (transferOps) {
498 uint32_t rgba_format;
499 int rgba_stride;
500 bool need_convert = false;
501
502 /* We will convert to RGBA float */
503 rgba_format = RGBA32_FLOAT;
504 rgba_stride = width * 4 * sizeof(GLfloat);
505
506 /* If we are lucky and the dst format matches the RGBA format we need
507 * to convert to, then we can convert directly into the dst buffer
508 * and avoid the final conversion/copy from the rgba buffer to the dst
509 * buffer.
510 */
511 if (format == rgba_format) {
512 rgba = dest;
513 } else if (rgba == NULL) { /* Allocate the RGBA buffer only once */
514 need_convert = true;
515 rgba = malloc(height * rgba_stride);
516 if (!rgba) {
517 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
518 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
519 return;
520 }
521 }
522
523 _mesa_format_convert(rgba, rgba_format, rgba_stride,
524 img_src, texFormat, rowstride,
525 width, height,
526 needsRebase ? rebaseSwizzle : NULL);
527
528 /* Handle transfer ops now */
529 _mesa_apply_rgba_transfer_ops(ctx, transferOps, width * height, rgba);
530
531 /* If we had to rebase, we have already handled that */
532 needsRebase = false;
533
534 /* If we were lucky and our RGBA conversion matches the dst format, then
535 * we are done.
536 */
537 if (!need_convert)
538 goto do_swap;
539
540 /* Otherwise, we need to convert from RGBA to dst next */
541 src = rgba;
542 src_format = rgba_format;
543 src_stride = rgba_stride;
544 } else {
545 /* No RGBA conversion needed, convert directly to dst */
546 src = img_src;
547 src_format = texFormat;
548 src_stride = rowstride;
549 }
550
551 /* Do the conversion to destination format */
552 _mesa_format_convert(dest, dst_format, dst_stride,
553 src, src_format, src_stride,
554 width, height,
555 needsRebase ? rebaseSwizzle : NULL);
556
557 do_swap:
558 /* Handle byte swapping if required */
559 if (ctx->Pack.SwapBytes) {
560 GLint swapSize = _mesa_sizeof_packed_type(type);
561 if (swapSize == 2 || swapSize == 4) {
562 int swapsPerPixel = _mesa_bytes_per_pixel(format, type) / swapSize;
563 assert(_mesa_bytes_per_pixel(format, type) % swapSize == 0);
564 if (swapSize == 2)
565 _mesa_swap2((GLushort *) dest, width * height * swapsPerPixel);
566 else if (swapSize == 4)
567 _mesa_swap4((GLuint *) dest, width * height * swapsPerPixel);
568 }
569 }
570
571 /* Unmap the src texture buffer */
572 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
573 }
574
575 done:
576 if (rgba)
577 free(rgba);
578 }
579
580
581 /**
582 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
583 * Compressed textures are handled here as well.
584 */
585 static void
586 get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
587 GLint xoffset, GLint yoffset, GLint zoffset,
588 GLsizei width, GLsizei height, GLint depth,
589 GLenum format, GLenum type, GLvoid *pixels,
590 struct gl_texture_image *texImage)
591 {
592 const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
593 GLbitfield transferOps = 0x0;
594
595 /* In general, clamping does not apply to glGetTexImage, except when
596 * the returned type of the image can't hold negative values.
597 */
598 if (type_needs_clamping(type)) {
599 /* the returned image type can't have negative values */
600 if (dataType == GL_FLOAT ||
601 dataType == GL_HALF_FLOAT ||
602 dataType == GL_SIGNED_NORMALIZED ||
603 format == GL_LUMINANCE ||
604 format == GL_LUMINANCE_ALPHA) {
605 transferOps |= IMAGE_CLAMP_BIT;
606 }
607 }
608
609 if (_mesa_is_format_compressed(texImage->TexFormat)) {
610 get_tex_rgba_compressed(ctx, dimensions,
611 xoffset, yoffset, zoffset,
612 width, height, depth,
613 format, type,
614 pixels, texImage, transferOps);
615 }
616 else {
617 get_tex_rgba_uncompressed(ctx, dimensions,
618 xoffset, yoffset, zoffset,
619 width, height, depth,
620 format, type,
621 pixels, texImage, transferOps);
622 }
623 }
624
625
626 /**
627 * Try to do glGetTexImage() with simple memcpy().
628 * \return GL_TRUE if done, GL_FALSE otherwise
629 */
630 static GLboolean
631 get_tex_memcpy(struct gl_context *ctx,
632 GLint xoffset, GLint yoffset, GLint zoffset,
633 GLsizei width, GLsizei height, GLint depth,
634 GLenum format, GLenum type, GLvoid *pixels,
635 struct gl_texture_image *texImage)
636 {
637 const GLenum target = texImage->TexObject->Target;
638 GLboolean memCopy = GL_FALSE;
639 GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
640
641 /*
642 * Check if we can use memcpy to copy from the hardware texture
643 * format to the user's format/type.
644 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
645 */
646 if ((target == GL_TEXTURE_1D ||
647 target == GL_TEXTURE_2D ||
648 target == GL_TEXTURE_RECTANGLE ||
649 _mesa_is_cube_face(target)) &&
650 texBaseFormat == texImage->_BaseFormat) {
651 memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
652 format, type,
653 ctx->Pack.SwapBytes);
654 }
655
656 if (depth > 1) {
657 /* only a single slice is supported at this time */
658 memCopy = FALSE;
659 }
660
661 if (memCopy) {
662 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
663 const GLint bytesPerRow = width * bpp;
664 GLubyte *dst =
665 _mesa_image_address2d(&ctx->Pack, pixels, width, height,
666 format, type, 0, 0);
667 const GLint dstRowStride =
668 _mesa_image_row_stride(&ctx->Pack, width, format, type);
669 GLubyte *src;
670 GLint srcRowStride;
671
672 /* map src texture buffer */
673 ctx->Driver.MapTextureImage(ctx, texImage, zoffset,
674 xoffset, yoffset, width, height,
675 GL_MAP_READ_BIT, &src, &srcRowStride);
676
677 if (src) {
678 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
679 memcpy(dst, src, bytesPerRow * texImage->Height);
680 }
681 else {
682 GLuint row;
683 for (row = 0; row < height; row++) {
684 memcpy(dst, src, bytesPerRow);
685 dst += dstRowStride;
686 src += srcRowStride;
687 }
688 }
689
690 /* unmap src texture buffer */
691 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset);
692 }
693 else {
694 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
695 }
696 }
697
698 return memCopy;
699 }
700
701
702 /**
703 * This is the software fallback for Driver.GetTexSubImage().
704 * All error checking will have been done before this routine is called.
705 * We'll call ctx->Driver.MapTextureImage() to access the data, then
706 * unmap with ctx->Driver.UnmapTextureImage().
707 */
708 void
709 _mesa_GetTexSubImage_sw(struct gl_context *ctx,
710 GLint xoffset, GLint yoffset, GLint zoffset,
711 GLsizei width, GLsizei height, GLint depth,
712 GLenum format, GLenum type, GLvoid *pixels,
713 struct gl_texture_image *texImage)
714 {
715 const GLuint dimensions =
716 _mesa_get_texture_dimensions(texImage->TexObject->Target);
717
718 /* map dest buffer, if PBO */
719 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
720 /* Packing texture image into a PBO.
721 * Map the (potentially) VRAM-based buffer into our process space so
722 * we can write into it with the code below.
723 * A hardware driver might use a sophisticated blit to move the
724 * texture data to the PBO if the PBO is in VRAM along with the texture.
725 */
726 GLubyte *buf = (GLubyte *)
727 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
728 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
729 MAP_INTERNAL);
730 if (!buf) {
731 /* out of memory or other unexpected error */
732 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
733 return;
734 }
735 /* <pixels> was an offset into the PBO.
736 * Now make it a real, client-side pointer inside the mapped region.
737 */
738 pixels = ADD_POINTERS(buf, pixels);
739 }
740
741 if (get_tex_memcpy(ctx, xoffset, yoffset, zoffset, width, height, depth,
742 format, type, pixels, texImage)) {
743 /* all done */
744 }
745 else if (format == GL_DEPTH_COMPONENT) {
746 get_tex_depth(ctx, dimensions, xoffset, yoffset, zoffset,
747 width, height, depth, format, type, pixels, texImage);
748 }
749 else if (format == GL_DEPTH_STENCIL_EXT) {
750 get_tex_depth_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
751 width, height, depth, format, type, pixels,
752 texImage);
753 }
754 else if (format == GL_STENCIL_INDEX) {
755 get_tex_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
756 width, height, depth, format, type, pixels, texImage);
757 }
758 else if (format == GL_YCBCR_MESA) {
759 get_tex_ycbcr(ctx, dimensions, xoffset, yoffset, zoffset,
760 width, height, depth, format, type, pixels, texImage);
761 }
762 else {
763 get_tex_rgba(ctx, dimensions, xoffset, yoffset, zoffset,
764 width, height, depth, format, type, pixels, texImage);
765 }
766
767 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
768 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
769 }
770 }
771
772
773
774 /**
775 * This is the software fallback for Driver.GetCompressedTexSubImage().
776 * All error checking will have been done before this routine is called.
777 */
778 void
779 _mesa_GetCompressedTexSubImage_sw(struct gl_context *ctx,
780 struct gl_texture_image *texImage,
781 GLint xoffset, GLint yoffset,
782 GLint zoffset, GLsizei width,
783 GLint height, GLint depth,
784 GLvoid *img)
785 {
786 const GLuint dimensions =
787 _mesa_get_texture_dimensions(texImage->TexObject->Target);
788 struct compressed_pixelstore store;
789 GLint slice;
790 GLubyte *dest;
791
792 _mesa_compute_compressed_pixelstore(dimensions, texImage->TexFormat,
793 width, height, depth,
794 &ctx->Pack, &store);
795
796 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
797 /* pack texture image into a PBO */
798 dest = (GLubyte *)
799 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
800 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
801 MAP_INTERNAL);
802 if (!dest) {
803 /* out of memory or other unexpected error */
804 _mesa_error(ctx, GL_OUT_OF_MEMORY,
805 "glGetCompresssedTexImage(map PBO failed)");
806 return;
807 }
808 dest = ADD_POINTERS(dest, img);
809 } else {
810 dest = img;
811 }
812
813 dest += store.SkipBytes;
814
815 for (slice = 0; slice < store.CopySlices; slice++) {
816 GLint srcRowStride;
817 GLubyte *src;
818
819 /* map src texture buffer */
820 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
821 xoffset, yoffset, width, height,
822 GL_MAP_READ_BIT, &src, &srcRowStride);
823
824 if (src) {
825 GLint i;
826 for (i = 0; i < store.CopyRowsPerSlice; i++) {
827 memcpy(dest, src, store.CopyBytesPerRow);
828 dest += store.TotalBytesPerRow;
829 src += srcRowStride;
830 }
831
832 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
833
834 /* Advance to next slice */
835 dest += store.TotalBytesPerRow * (store.TotalRowsPerSlice - store.CopyRowsPerSlice);
836
837 } else {
838 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
839 }
840 }
841
842 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
843 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
844 }
845 }
846
847
848 /**
849 * Validate the texture target enum supplied to glGetTex(ture)Image or
850 * glGetCompressedTex(ture)Image.
851 */
852 static GLboolean
853 legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
854 {
855 switch (target) {
856 case GL_TEXTURE_1D:
857 case GL_TEXTURE_2D:
858 case GL_TEXTURE_3D:
859 return GL_TRUE;
860 case GL_TEXTURE_RECTANGLE_NV:
861 return ctx->Extensions.NV_texture_rectangle;
862 case GL_TEXTURE_1D_ARRAY_EXT:
863 case GL_TEXTURE_2D_ARRAY_EXT:
864 return ctx->Extensions.EXT_texture_array;
865 case GL_TEXTURE_CUBE_MAP_ARRAY:
866 return ctx->Extensions.ARB_texture_cube_map_array;
867
868 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
869 * (30.10.2014) says:
870 * "An INVALID_ENUM error is generated if the effective target is not
871 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
872 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
873 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
874 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
875 */
876 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
877 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
878 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
879 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
880 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
881 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
882 return dsa ? GL_FALSE : ctx->Extensions.ARB_texture_cube_map;
883 case GL_TEXTURE_CUBE_MAP:
884 return dsa ? GL_TRUE : GL_FALSE;
885 default:
886 return GL_FALSE;
887 }
888 }
889
890
891 /**
892 * Do error checking for a glGetTex(ture)Image() call.
893 * \return GL_TRUE if any error, GL_FALSE if no errors.
894 */
895 static GLboolean
896 getteximage_error_check(struct gl_context *ctx,
897 struct gl_texture_image *texImage,
898 GLenum target, GLint level,
899 GLenum format, GLenum type, GLsizei clientMemSize,
900 GLvoid *pixels, bool dsa)
901 {
902 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
903 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
904 GLenum baseFormat;
905 const char *suffix = dsa ? "ture" : "";
906
907 assert(texImage);
908 assert(maxLevels != 0);
909 if (level < 0 || level >= maxLevels) {
910 _mesa_error(ctx, GL_INVALID_VALUE,
911 "glGetTex%sImage(level out of range)", suffix);
912 return GL_TRUE;
913 }
914
915 /*
916 * Format and type checking has been moved up to GetnTexImage and
917 * GetTextureImage so that it happens before getting the texImage object.
918 */
919
920 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
921
922 /* Make sure the requested image format is compatible with the
923 * texture's format.
924 */
925 if (_mesa_is_color_format(format)
926 && !_mesa_is_color_format(baseFormat)) {
927 _mesa_error(ctx, GL_INVALID_OPERATION,
928 "glGetTex%sImage(format mismatch)", suffix);
929 return GL_TRUE;
930 }
931 else if (_mesa_is_depth_format(format)
932 && !_mesa_is_depth_format(baseFormat)
933 && !_mesa_is_depthstencil_format(baseFormat)) {
934 _mesa_error(ctx, GL_INVALID_OPERATION,
935 "glGetTex%sImage(format mismatch)", suffix);
936 return GL_TRUE;
937 }
938 else if (_mesa_is_stencil_format(format)
939 && !ctx->Extensions.ARB_texture_stencil8) {
940 _mesa_error(ctx, GL_INVALID_ENUM,
941 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix);
942 return GL_TRUE;
943 }
944 else if (_mesa_is_ycbcr_format(format)
945 && !_mesa_is_ycbcr_format(baseFormat)) {
946 _mesa_error(ctx, GL_INVALID_OPERATION,
947 "glGetTex%sImage(format mismatch)", suffix);
948 return GL_TRUE;
949 }
950 else if (_mesa_is_depthstencil_format(format)
951 && !_mesa_is_depthstencil_format(baseFormat)) {
952 _mesa_error(ctx, GL_INVALID_OPERATION,
953 "glGetTex%sImage(format mismatch)", suffix);
954 return GL_TRUE;
955 }
956 else if (!_mesa_is_stencil_format(format) && _mesa_is_enum_format_integer(format) !=
957 _mesa_is_format_integer(texImage->TexFormat)) {
958 _mesa_error(ctx, GL_INVALID_OPERATION,
959 "glGetTex%sImage(format mismatch)", suffix);
960 return GL_TRUE;
961 }
962
963 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
964 texImage->Height, texImage->Depth,
965 format, type, clientMemSize, pixels)) {
966 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
967 _mesa_error(ctx, GL_INVALID_OPERATION,
968 "glGetTex%sImage(out of bounds PBO access)", suffix);
969 } else {
970 _mesa_error(ctx, GL_INVALID_OPERATION,
971 "%s(out of bounds access:"
972 " bufSize (%d) is too small)",
973 dsa ? "glGetTextureImage" : "glGetnTexImageARB",
974 clientMemSize);
975 }
976 return GL_TRUE;
977 }
978
979 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
980 /* PBO should not be mapped */
981 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
982 _mesa_error(ctx, GL_INVALID_OPERATION,
983 "glGetTex%sImage(PBO is mapped)", suffix);
984 return GL_TRUE;
985 }
986 }
987
988 return GL_FALSE;
989 }
990
991
992 /**
993 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
994 * and glGetTexImage.
995 *
996 * Requires caller to pass in texImage object because _mesa_GetTextureImage
997 * must handle the GL_TEXTURE_CUBE_MAP target.
998 *
999 * \param target texture target.
1000 * \param level image level.
1001 * \param format pixel data format for returned image.
1002 * \param type pixel data type for returned image.
1003 * \param bufSize size of the pixels data buffer.
1004 * \param pixels returned pixel data.
1005 * \param dsa True when the caller is an ARB_direct_state_access function,
1006 * false otherwise
1007 */
1008 static void
1009 get_texture_image(struct gl_context *ctx,
1010 struct gl_texture_object *texObj,
1011 struct gl_texture_image *texImage, GLenum target,
1012 GLint level, GLenum format, GLenum type,
1013 GLsizei bufSize, GLvoid *pixels, bool dsa)
1014 {
1015 assert(texObj);
1016 assert(texImage);
1017
1018 FLUSH_VERTICES(ctx, 0);
1019
1020 /*
1021 * Legal target checking has been moved up to GetnTexImage and
1022 * GetTextureImage so that it can be caught before receiving a NULL
1023 * texImage object and exiting.
1024 */
1025
1026 if (getteximage_error_check(ctx, texImage, target, level, format,
1027 type, bufSize, pixels, dsa)) {
1028 return;
1029 }
1030
1031 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1032 /* not an error, do nothing */
1033 return;
1034 }
1035
1036 if (_mesa_is_zero_size_texture(texImage))
1037 return;
1038
1039 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1040 _mesa_debug(ctx, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
1041 " dstFmt=0x%x, dstType=0x%x\n",
1042 dsa ? "ture": "",
1043 texObj->Name,
1044 _mesa_get_format_name(texImage->TexFormat),
1045 texImage->Width, texImage->Height,
1046 format, type);
1047 }
1048
1049 _mesa_lock_texture(ctx, texObj);
1050 {
1051 ctx->Driver.GetTexSubImage(ctx, 0, 0, 0,
1052 texImage->Width, texImage->Height,
1053 texImage->Depth,
1054 format, type, pixels, texImage);
1055 }
1056 _mesa_unlock_texture(ctx, texObj);
1057 }
1058
1059 /**
1060 * Get texture image. Called by glGetTexImage.
1061 *
1062 * \param target texture target.
1063 * \param level image level.
1064 * \param format pixel data format for returned image.
1065 * \param type pixel data type for returned image.
1066 * \param bufSize size of the pixels data buffer.
1067 * \param pixels returned pixel data.
1068 */
1069 void GLAPIENTRY
1070 _mesa_GetnTexImageARB(GLenum target, GLint level, GLenum format,
1071 GLenum type, GLsizei bufSize, GLvoid *pixels)
1072 {
1073 struct gl_texture_object *texObj;
1074 struct gl_texture_image *texImage;
1075 GLenum err;
1076 GET_CURRENT_CONTEXT(ctx);
1077
1078 /*
1079 * This has been moved here because a format/type mismatch can cause a NULL
1080 * texImage object, which in turn causes the mismatch error to be
1081 * ignored.
1082 */
1083 err = _mesa_error_check_format_and_type(ctx, format, type);
1084 if (err != GL_NO_ERROR) {
1085 _mesa_error(ctx, err, "glGetnTexImage(format/type)");
1086 return;
1087 }
1088
1089 /*
1090 * Legal target checking has been moved here to prevent exiting with a NULL
1091 * texImage object.
1092 */
1093 if (!legal_getteximage_target(ctx, target, false)) {
1094 _mesa_error(ctx, GL_INVALID_ENUM, "glGetnTexImage(target=0x%x)",
1095 target);
1096 return;
1097 }
1098
1099 texObj = _mesa_get_current_tex_object(ctx, target);
1100 if (!texObj)
1101 return;
1102
1103 texImage = _mesa_select_tex_image(texObj, target, level);
1104 if (!texImage)
1105 return;
1106
1107 get_texture_image(ctx, texObj, texImage, target, level, format, type,
1108 bufSize, pixels, false);
1109 }
1110
1111
1112 void GLAPIENTRY
1113 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1114 GLenum type, GLvoid *pixels )
1115 {
1116 _mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
1117 }
1118
1119 /**
1120 * Get texture image.
1121 *
1122 * \param texture texture name.
1123 * \param level image level.
1124 * \param format pixel data format for returned image.
1125 * \param type pixel data type for returned image.
1126 * \param bufSize size of the pixels data buffer.
1127 * \param pixels returned pixel data.
1128 */
1129 void GLAPIENTRY
1130 _mesa_GetTextureImage(GLuint texture, GLint level, GLenum format,
1131 GLenum type, GLsizei bufSize, GLvoid *pixels)
1132 {
1133 struct gl_texture_object *texObj;
1134 struct gl_texture_image *texImage;
1135 int i;
1136 GLint image_stride;
1137 GLenum err;
1138 GET_CURRENT_CONTEXT(ctx);
1139
1140 /*
1141 * This has been moved here because a format/type mismatch can cause a NULL
1142 * texImage object, which in turn causes the mismatch error to be
1143 * ignored.
1144 */
1145 err = _mesa_error_check_format_and_type(ctx, format, type);
1146 if (err != GL_NO_ERROR) {
1147 _mesa_error(ctx, err, "glGetTextureImage(format/type)");
1148 return;
1149 }
1150
1151 texObj = _mesa_lookup_texture_err(ctx, texture, "glGetTextureImage");
1152 if (!texObj)
1153 return;
1154
1155 /*
1156 * Legal target checking has been moved here to prevent exiting with a NULL
1157 * texImage object.
1158 */
1159 if (!legal_getteximage_target(ctx, texObj->Target, true)) {
1160 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTextureImage(target=%s)",
1161 _mesa_enum_to_string(texObj->Target));
1162 return;
1163 }
1164
1165 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1166 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
1167
1168 /* Make sure the texture object is a proper cube.
1169 * (See texturesubimage in teximage.c for details on why this check is
1170 * performed.)
1171 */
1172 if (!_mesa_cube_level_complete(texObj, level)) {
1173 _mesa_error(ctx, GL_INVALID_OPERATION,
1174 "glGetTextureImage(cube map incomplete)");
1175 return;
1176 }
1177
1178 /* Copy each face. */
1179 for (i = 0; i < 6; ++i) {
1180 texImage = texObj->Image[i][level];
1181 assert(texImage);
1182
1183 get_texture_image(ctx, texObj, texImage, texObj->Target, level,
1184 format, type, bufSize, pixels, true);
1185
1186 image_stride = _mesa_image_image_stride(&ctx->Pack, texImage->Width,
1187 texImage->Height, format,
1188 type);
1189 pixels = (GLubyte *) pixels + image_stride;
1190 bufSize -= image_stride;
1191 }
1192 }
1193 else {
1194 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
1195 if (!texImage)
1196 return;
1197
1198 get_texture_image(ctx, texObj, texImage, texObj->Target, level,
1199 format, type, bufSize, pixels, true);
1200 }
1201 }
1202
1203 /**
1204 * Do error checking for a glGetCompressedTexImage() call.
1205 * \return GL_TRUE if any error, GL_FALSE if no errors.
1206 */
1207 static GLboolean
1208 getcompressedteximage_error_check(struct gl_context *ctx,
1209 struct gl_texture_image *texImage,
1210 GLenum target,
1211 GLint level, GLsizei clientMemSize,
1212 GLvoid *img, bool dsa)
1213 {
1214 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
1215 GLuint compressedSize, dimensions;
1216 const char *suffix = dsa ? "ture" : "";
1217
1218 assert(texImage);
1219
1220 if (!legal_getteximage_target(ctx, target, dsa)) {
1221 _mesa_error(ctx, GL_INVALID_ENUM,
1222 "glGetCompressedTex%sImage(target=%s)", suffix,
1223 _mesa_enum_to_string(target));
1224 return GL_TRUE;
1225 }
1226
1227 assert(maxLevels != 0);
1228 if (level < 0 || level >= maxLevels) {
1229 _mesa_error(ctx, GL_INVALID_VALUE,
1230 "glGetCompressedTex%sImage(bad level = %d)", suffix, level);
1231 return GL_TRUE;
1232 }
1233
1234 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
1235 _mesa_error(ctx, GL_INVALID_OPERATION,
1236 "glGetCompressedTex%sImage(texture is not compressed)",
1237 suffix);
1238 return GL_TRUE;
1239 }
1240
1241 compressedSize = _mesa_format_image_size(texImage->TexFormat,
1242 texImage->Width,
1243 texImage->Height,
1244 texImage->Depth);
1245
1246 /* Check for invalid pixel storage modes */
1247 dimensions = _mesa_get_texture_dimensions(texImage->TexObject->Target);
1248 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
1249 &ctx->Pack, dsa ?
1250 "glGetCompressedTextureImage":
1251 "glGetCompressedTexImage")) {
1252 return GL_TRUE;
1253 }
1254
1255 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1256 /* do bounds checking on writing to client memory */
1257 if (clientMemSize < (GLsizei) compressedSize) {
1258 _mesa_error(ctx, GL_INVALID_OPERATION,
1259 "%s(out of bounds access: bufSize (%d) is too small)",
1260 dsa ? "glGetCompressedTextureImage" :
1261 "glGetnCompressedTexImageARB", clientMemSize);
1262 return GL_TRUE;
1263 }
1264 } else {
1265 /* do bounds checking on PBO write */
1266 if ((const GLubyte *) img + compressedSize >
1267 (const GLubyte *) ctx->Pack.BufferObj->Size) {
1268 _mesa_error(ctx, GL_INVALID_OPERATION,
1269 "glGetCompressedTex%sImage(out of bounds PBO access)",
1270 suffix);
1271 return GL_TRUE;
1272 }
1273
1274 /* make sure PBO is not mapped */
1275 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1276 _mesa_error(ctx, GL_INVALID_OPERATION,
1277 "glGetCompressedTex%sImage(PBO is mapped)", suffix);
1278 return GL_TRUE;
1279 }
1280 }
1281
1282 return GL_FALSE;
1283 }
1284
1285 /** Implements glGetnCompressedTexImageARB, glGetCompressedTexImage, and
1286 * glGetCompressedTextureImage.
1287 *
1288 * texImage must be passed in because glGetCompressedTexImage must handle the
1289 * target GL_TEXTURE_CUBE_MAP.
1290 */
1291 void
1292 _mesa_get_compressed_texture_image(struct gl_context *ctx,
1293 struct gl_texture_object *texObj,
1294 struct gl_texture_image *texImage,
1295 GLenum target, GLint level,
1296 GLsizei bufSize, GLvoid *pixels,
1297 bool dsa)
1298 {
1299 assert(texObj);
1300 assert(texImage);
1301
1302 FLUSH_VERTICES(ctx, 0);
1303
1304 if (getcompressedteximage_error_check(ctx, texImage, target, level,
1305 bufSize, pixels, dsa)) {
1306 return;
1307 }
1308
1309 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1310 /* not an error, do nothing */
1311 return;
1312 }
1313
1314 if (_mesa_is_zero_size_texture(texImage))
1315 return;
1316
1317 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1318 _mesa_debug(ctx,
1319 "glGetCompressedTex%sImage(tex %u) format = %s, w=%d, h=%d\n",
1320 dsa ? "ture" : "", texObj->Name,
1321 _mesa_get_format_name(texImage->TexFormat),
1322 texImage->Width, texImage->Height);
1323 }
1324
1325 _mesa_lock_texture(ctx, texObj);
1326 {
1327 ctx->Driver.GetCompressedTexSubImage(ctx, texImage,
1328 0, 0, 0,
1329 texImage->Width, texImage->Height,
1330 texImage->Depth, pixels);
1331 }
1332 _mesa_unlock_texture(ctx, texObj);
1333 }
1334
1335 void GLAPIENTRY
1336 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1337 GLvoid *img)
1338 {
1339 struct gl_texture_object *texObj;
1340 struct gl_texture_image *texImage;
1341 GET_CURRENT_CONTEXT(ctx);
1342
1343 texObj = _mesa_get_current_tex_object(ctx, target);
1344 if (!texObj)
1345 return;
1346
1347 texImage = _mesa_select_tex_image(texObj, target, level);
1348 if (!texImage)
1349 return;
1350
1351 _mesa_get_compressed_texture_image(ctx, texObj, texImage, target, level,
1352 bufSize, img, false);
1353 }
1354
1355 void GLAPIENTRY
1356 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *img)
1357 {
1358 _mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
1359 }
1360
1361 /**
1362 * Get compressed texture image.
1363 *
1364 * \param texture texture name.
1365 * \param level image level.
1366 * \param bufSize size of the pixels data buffer.
1367 * \param pixels returned pixel data.
1368 */
1369 void GLAPIENTRY
1370 _mesa_GetCompressedTextureImage(GLuint texture, GLint level,
1371 GLsizei bufSize, GLvoid *pixels)
1372 {
1373 struct gl_texture_object *texObj;
1374 struct gl_texture_image *texImage;
1375 int i;
1376 GLint image_stride;
1377 GET_CURRENT_CONTEXT(ctx);
1378
1379 texObj = _mesa_lookup_texture_err(ctx, texture,
1380 "glGetCompressedTextureImage");
1381 if (!texObj)
1382 return;
1383
1384 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1385 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
1386
1387 /* Make sure the texture object is a proper cube.
1388 * (See texturesubimage in teximage.c for details on why this check is
1389 * performed.)
1390 */
1391 if (!_mesa_cube_level_complete(texObj, level)) {
1392 _mesa_error(ctx, GL_INVALID_OPERATION,
1393 "glGetCompressedTextureImage(cube map incomplete)");
1394 return;
1395 }
1396
1397 /* Copy each face. */
1398 for (i = 0; i < 6; ++i) {
1399 texImage = texObj->Image[i][level];
1400 assert(texImage);
1401
1402 _mesa_get_compressed_texture_image(ctx, texObj, texImage,
1403 texObj->Target, level,
1404 bufSize, pixels, true);
1405
1406 /* Compressed images don't have a client format */
1407 image_stride = _mesa_format_image_size(texImage->TexFormat,
1408 texImage->Width,
1409 texImage->Height, 1);
1410
1411 pixels = (GLubyte *) pixels + image_stride;
1412 bufSize -= image_stride;
1413 }
1414 }
1415 else {
1416 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
1417 if (!texImage)
1418 return;
1419
1420 _mesa_get_compressed_texture_image(ctx, texObj, texImage,
1421 texObj->Target, level, bufSize,
1422 pixels, true);
1423 }
1424 }