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