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