mesa: Use _mesa_format_convert to implement get_tex_rgba_compressed.
[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, RGBA8888_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;
454
455 /* We will convert to RGBA float */
456 rgba_format = RGBA8888_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 need_convert = false;
466 rgba = dest;
467 } else if (rgba == NULL) { /* Allocate the RGBA buffer only once */
468 need_convert = true;
469 rgba = malloc(height * rgba_stride);
470 if (!rgba) {
471 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
472 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
473 return;
474 }
475 }
476
477 _mesa_format_convert(rgba, rgba_format, rgba_stride,
478 img_src, texFormat, rowstride,
479 width, height,
480 needsRebase ? rebaseSwizzle : NULL);
481
482 /* Handle transfer ops now */
483 _mesa_apply_rgba_transfer_ops(ctx, transferOps, width * height, rgba);
484
485 /* If we had to rebase, we have already handled that */
486 needsRebase = false;
487
488 /* If we were lucky and our RGBA conversion matches the dst format, then
489 * we are done.
490 */
491 if (!need_convert)
492 goto do_swap;
493
494 /* Otherwise, we need to convert from RGBA to dst next */
495 src = rgba;
496 src_format = rgba_format;
497 src_stride = rgba_stride;
498 } else {
499 /* No RGBA conversion needed, convert directly to dst */
500 src = img_src;
501 src_format = texFormat;
502 src_stride = rowstride;
503 }
504
505 /* Do the conversion to destination format */
506 _mesa_format_convert(dest, dst_format, dst_stride,
507 src, src_format, src_stride,
508 width, height,
509 needsRebase ? rebaseSwizzle : NULL);
510
511 do_swap:
512 /* Handle byte swapping if required */
513 if (ctx->Pack.SwapBytes) {
514 int components = _mesa_components_in_format(format);
515 GLint swapSize = _mesa_sizeof_packed_type(type);
516 if (swapSize == 2)
517 _mesa_swap2((GLushort *) dest, width * height * components);
518 else if (swapSize == 4)
519 _mesa_swap4((GLuint *) dest, width * height * components);
520 }
521
522 /* Unmap the src texture buffer */
523 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
524 }
525
526 done:
527 if (rgba)
528 free(rgba);
529 }
530
531
532 /**
533 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
534 * Compressed textures are handled here as well.
535 */
536 static void
537 get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
538 GLenum format, GLenum type, GLvoid *pixels,
539 struct gl_texture_image *texImage)
540 {
541 const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
542 GLbitfield transferOps = 0x0;
543
544 /* In general, clamping does not apply to glGetTexImage, except when
545 * the returned type of the image can't hold negative values.
546 */
547 if (type_needs_clamping(type)) {
548 /* the returned image type can't have negative values */
549 if (dataType == GL_FLOAT ||
550 dataType == GL_HALF_FLOAT ||
551 dataType == GL_SIGNED_NORMALIZED ||
552 format == GL_LUMINANCE ||
553 format == GL_LUMINANCE_ALPHA) {
554 transferOps |= IMAGE_CLAMP_BIT;
555 }
556 }
557
558 if (_mesa_is_format_compressed(texImage->TexFormat)) {
559 get_tex_rgba_compressed(ctx, dimensions, format, type,
560 pixels, texImage, transferOps);
561 }
562 else {
563 get_tex_rgba_uncompressed(ctx, dimensions, format, type,
564 pixels, texImage, transferOps);
565 }
566 }
567
568
569 /**
570 * Try to do glGetTexImage() with simple memcpy().
571 * \return GL_TRUE if done, GL_FALSE otherwise
572 */
573 static GLboolean
574 get_tex_memcpy(struct gl_context *ctx, GLenum format, GLenum type,
575 GLvoid *pixels,
576 struct gl_texture_image *texImage)
577 {
578 const GLenum target = texImage->TexObject->Target;
579 GLboolean memCopy = GL_FALSE;
580 GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
581
582 /*
583 * Check if we can use memcpy to copy from the hardware texture
584 * format to the user's format/type.
585 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
586 */
587 if ((target == GL_TEXTURE_1D ||
588 target == GL_TEXTURE_2D ||
589 target == GL_TEXTURE_RECTANGLE ||
590 _mesa_is_cube_face(target)) &&
591 texBaseFormat == texImage->_BaseFormat) {
592 memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
593 format, type,
594 ctx->Pack.SwapBytes);
595 }
596
597 if (memCopy) {
598 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
599 const GLuint bytesPerRow = texImage->Width * bpp;
600 GLubyte *dst =
601 _mesa_image_address2d(&ctx->Pack, pixels, texImage->Width,
602 texImage->Height, format, type, 0, 0);
603 const GLint dstRowStride =
604 _mesa_image_row_stride(&ctx->Pack, texImage->Width, format, type);
605 GLubyte *src;
606 GLint srcRowStride;
607
608 /* map src texture buffer */
609 ctx->Driver.MapTextureImage(ctx, texImage, 0,
610 0, 0, texImage->Width, texImage->Height,
611 GL_MAP_READ_BIT, &src, &srcRowStride);
612
613 if (src) {
614 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
615 memcpy(dst, src, bytesPerRow * texImage->Height);
616 }
617 else {
618 GLuint row;
619 for (row = 0; row < texImage->Height; row++) {
620 memcpy(dst, src, bytesPerRow);
621 dst += dstRowStride;
622 src += srcRowStride;
623 }
624 }
625
626 /* unmap src texture buffer */
627 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
628 }
629 else {
630 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
631 }
632 }
633
634 return memCopy;
635 }
636
637
638 /**
639 * This is the software fallback for Driver.GetTexImage().
640 * All error checking will have been done before this routine is called.
641 * We'll call ctx->Driver.MapTextureImage() to access the data, then
642 * unmap with ctx->Driver.UnmapTextureImage().
643 */
644 void
645 _mesa_GetTexImage_sw(struct gl_context *ctx,
646 GLenum format, GLenum type, GLvoid *pixels,
647 struct gl_texture_image *texImage)
648 {
649 const GLuint dimensions =
650 _mesa_get_texture_dimensions(texImage->TexObject->Target);
651
652 /* map dest buffer, if PBO */
653 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
654 /* Packing texture image into a PBO.
655 * Map the (potentially) VRAM-based buffer into our process space so
656 * we can write into it with the code below.
657 * A hardware driver might use a sophisticated blit to move the
658 * texture data to the PBO if the PBO is in VRAM along with the texture.
659 */
660 GLubyte *buf = (GLubyte *)
661 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
662 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
663 MAP_INTERNAL);
664 if (!buf) {
665 /* out of memory or other unexpected error */
666 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
667 return;
668 }
669 /* <pixels> was an offset into the PBO.
670 * Now make it a real, client-side pointer inside the mapped region.
671 */
672 pixels = ADD_POINTERS(buf, pixels);
673 }
674
675 if (get_tex_memcpy(ctx, format, type, pixels, texImage)) {
676 /* all done */
677 }
678 else if (format == GL_DEPTH_COMPONENT) {
679 get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
680 }
681 else if (format == GL_DEPTH_STENCIL_EXT) {
682 get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
683 }
684 else if (format == GL_YCBCR_MESA) {
685 get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
686 }
687 else {
688 get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
689 }
690
691 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
692 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
693 }
694 }
695
696
697
698 /**
699 * This is the software fallback for Driver.GetCompressedTexImage().
700 * All error checking will have been done before this routine is called.
701 */
702 void
703 _mesa_GetCompressedTexImage_sw(struct gl_context *ctx,
704 struct gl_texture_image *texImage,
705 GLvoid *img)
706 {
707 const GLuint dimensions =
708 _mesa_get_texture_dimensions(texImage->TexObject->Target);
709 struct compressed_pixelstore store;
710 GLuint i, slice;
711 GLubyte *dest;
712
713 _mesa_compute_compressed_pixelstore(dimensions, texImage->TexFormat,
714 texImage->Width, texImage->Height,
715 texImage->Depth,
716 &ctx->Pack,
717 &store);
718
719 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
720 /* pack texture image into a PBO */
721 dest = (GLubyte *)
722 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
723 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
724 MAP_INTERNAL);
725 if (!dest) {
726 /* out of memory or other unexpected error */
727 _mesa_error(ctx, GL_OUT_OF_MEMORY,
728 "glGetCompresssedTexImage(map PBO failed)");
729 return;
730 }
731 dest = ADD_POINTERS(dest, img);
732 } else {
733 dest = img;
734 }
735
736 dest += store.SkipBytes;
737
738 for (slice = 0; slice < store.CopySlices; slice++) {
739 GLint srcRowStride;
740 GLubyte *src;
741
742 /* map src texture buffer */
743 ctx->Driver.MapTextureImage(ctx, texImage, 0,
744 0, 0, texImage->Width, texImage->Height,
745 GL_MAP_READ_BIT, &src, &srcRowStride);
746
747 if (src) {
748
749 for (i = 0; i < store.CopyRowsPerSlice; i++) {
750 memcpy(dest, src, store.CopyBytesPerRow);
751 dest += store.TotalBytesPerRow;
752 src += srcRowStride;
753 }
754
755 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
756
757 /* Advance to next slice */
758 dest += store.TotalBytesPerRow * (store.TotalRowsPerSlice - store.CopyRowsPerSlice);
759
760 } else {
761 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
762 }
763 }
764
765 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
766 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
767 }
768 }
769
770
771 /**
772 * Validate the texture target enum supplied to glGetTex(ture)Image or
773 * glGetCompressedTex(ture)Image.
774 */
775 static GLboolean
776 legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
777 {
778 switch (target) {
779 case GL_TEXTURE_1D:
780 case GL_TEXTURE_2D:
781 case GL_TEXTURE_3D:
782 return GL_TRUE;
783 case GL_TEXTURE_RECTANGLE_NV:
784 return ctx->Extensions.NV_texture_rectangle;
785 case GL_TEXTURE_1D_ARRAY_EXT:
786 case GL_TEXTURE_2D_ARRAY_EXT:
787 return ctx->Extensions.EXT_texture_array;
788 case GL_TEXTURE_CUBE_MAP_ARRAY:
789 return ctx->Extensions.ARB_texture_cube_map_array;
790
791 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
792 * (30.10.2014) says:
793 * "An INVALID_ENUM error is generated if the effective target is not
794 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
795 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
796 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
797 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
798 */
799 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
800 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
801 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
802 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
803 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
804 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
805 return dsa ? GL_FALSE : ctx->Extensions.ARB_texture_cube_map;
806 case GL_TEXTURE_CUBE_MAP:
807 return dsa ? GL_TRUE : GL_FALSE;
808 default:
809 return GL_FALSE;
810 }
811 }
812
813
814 /**
815 * Do error checking for a glGetTex(ture)Image() call.
816 * \return GL_TRUE if any error, GL_FALSE if no errors.
817 */
818 static GLboolean
819 getteximage_error_check(struct gl_context *ctx,
820 struct gl_texture_image *texImage,
821 GLenum target, GLint level,
822 GLenum format, GLenum type, GLsizei clientMemSize,
823 GLvoid *pixels, bool dsa)
824 {
825 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
826 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
827 GLenum baseFormat;
828 const char *suffix = dsa ? "ture" : "";
829
830 assert(texImage);
831 assert(maxLevels != 0);
832 if (level < 0 || level >= maxLevels) {
833 _mesa_error(ctx, GL_INVALID_VALUE,
834 "glGetTex%sImage(level out of range)", suffix);
835 return GL_TRUE;
836 }
837
838 /*
839 * Format and type checking has been moved up to GetnTexImage and
840 * GetTextureImage so that it happens before getting the texImage object.
841 */
842
843 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
844
845 /* Make sure the requested image format is compatible with the
846 * texture's format.
847 */
848 if (_mesa_is_color_format(format)
849 && !_mesa_is_color_format(baseFormat)) {
850 _mesa_error(ctx, GL_INVALID_OPERATION,
851 "glGetTex%sImage(format mismatch)", suffix);
852 return GL_TRUE;
853 }
854 else if (_mesa_is_depth_format(format)
855 && !_mesa_is_depth_format(baseFormat)
856 && !_mesa_is_depthstencil_format(baseFormat)) {
857 _mesa_error(ctx, GL_INVALID_OPERATION,
858 "glGetTex%sImage(format mismatch)", suffix);
859 return GL_TRUE;
860 }
861 else if (_mesa_is_stencil_format(format)
862 && !ctx->Extensions.ARB_texture_stencil8) {
863 _mesa_error(ctx, GL_INVALID_ENUM,
864 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix);
865 return GL_TRUE;
866 }
867 else if (_mesa_is_ycbcr_format(format)
868 && !_mesa_is_ycbcr_format(baseFormat)) {
869 _mesa_error(ctx, GL_INVALID_OPERATION,
870 "glGetTex%sImage(format mismatch)", suffix);
871 return GL_TRUE;
872 }
873 else if (_mesa_is_depthstencil_format(format)
874 && !_mesa_is_depthstencil_format(baseFormat)) {
875 _mesa_error(ctx, GL_INVALID_OPERATION,
876 "glGetTex%sImage(format mismatch)", suffix);
877 return GL_TRUE;
878 }
879 else if (_mesa_is_enum_format_integer(format) !=
880 _mesa_is_format_integer(texImage->TexFormat)) {
881 _mesa_error(ctx, GL_INVALID_OPERATION,
882 "glGetTex%sImage(format mismatch)", suffix);
883 return GL_TRUE;
884 }
885
886 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
887 texImage->Height, texImage->Depth,
888 format, type, clientMemSize, pixels)) {
889 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
890 _mesa_error(ctx, GL_INVALID_OPERATION,
891 "glGetTex%sImage(out of bounds PBO access)", suffix);
892 } else {
893 _mesa_error(ctx, GL_INVALID_OPERATION,
894 "%s(out of bounds access:"
895 " bufSize (%d) is too small)",
896 dsa ? "glGetTextureImage" : "glGetnTexImageARB",
897 clientMemSize);
898 }
899 return GL_TRUE;
900 }
901
902 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
903 /* PBO should not be mapped */
904 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
905 _mesa_error(ctx, GL_INVALID_OPERATION,
906 "glGetTex%sImage(PBO is mapped)", suffix);
907 return GL_TRUE;
908 }
909 }
910
911 return GL_FALSE;
912 }
913
914
915 /**
916 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
917 * and glGetTexImage.
918 *
919 * Requires caller to pass in texImage object because _mesa_GetTextureImage
920 * must handle the GL_TEXTURE_CUBE_MAP target.
921 *
922 * \param target texture target.
923 * \param level image level.
924 * \param format pixel data format for returned image.
925 * \param type pixel data type for returned image.
926 * \param bufSize size of the pixels data buffer.
927 * \param pixels returned pixel data.
928 * \param dsa True when the caller is an ARB_direct_state_access function,
929 * false otherwise
930 */
931 void
932 _mesa_get_texture_image(struct gl_context *ctx,
933 struct gl_texture_object *texObj,
934 struct gl_texture_image *texImage, GLenum target,
935 GLint level, GLenum format, GLenum type,
936 GLsizei bufSize, GLvoid *pixels, bool dsa)
937 {
938 assert(texObj);
939 assert(texImage);
940
941 FLUSH_VERTICES(ctx, 0);
942
943 /*
944 * Legal target checking has been moved up to GetnTexImage and
945 * GetTextureImage so that it can be caught before receiving a NULL
946 * texImage object and exiting.
947 */
948
949 if (getteximage_error_check(ctx, texImage, target, level, format,
950 type, bufSize, pixels, dsa)) {
951 return;
952 }
953
954 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
955 /* not an error, do nothing */
956 return;
957 }
958
959 if (_mesa_is_zero_size_texture(texImage))
960 return;
961
962 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
963 _mesa_debug(ctx, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
964 " dstFmt=0x%x, dstType=0x%x\n",
965 dsa ? "ture": "",
966 texObj->Name,
967 _mesa_get_format_name(texImage->TexFormat),
968 texImage->Width, texImage->Height,
969 format, type);
970 }
971
972 _mesa_lock_texture(ctx, texObj);
973 {
974 ctx->Driver.GetTexImage(ctx, format, type, pixels, texImage);
975 }
976 _mesa_unlock_texture(ctx, texObj);
977 }
978
979 /**
980 * Get texture image. Called by glGetTexImage.
981 *
982 * \param target texture target.
983 * \param level image level.
984 * \param format pixel data format for returned image.
985 * \param type pixel data type for returned image.
986 * \param bufSize size of the pixels data buffer.
987 * \param pixels returned pixel data.
988 */
989 void GLAPIENTRY
990 _mesa_GetnTexImageARB(GLenum target, GLint level, GLenum format,
991 GLenum type, GLsizei bufSize, GLvoid *pixels)
992 {
993 struct gl_texture_object *texObj;
994 struct gl_texture_image *texImage;
995 GLenum err;
996 GET_CURRENT_CONTEXT(ctx);
997
998 /*
999 * This has been moved here because a format/type mismatch can cause a NULL
1000 * texImage object, which in turn causes the mismatch error to be
1001 * ignored.
1002 */
1003 err = _mesa_error_check_format_and_type(ctx, format, type);
1004 if (err != GL_NO_ERROR) {
1005 _mesa_error(ctx, err, "glGetnTexImage(format/type)");
1006 return;
1007 }
1008
1009 /*
1010 * Legal target checking has been moved here to prevent exiting with a NULL
1011 * texImage object.
1012 */
1013 if (!legal_getteximage_target(ctx, target, false)) {
1014 _mesa_error(ctx, GL_INVALID_ENUM, "glGetnTexImage(target=0x%x)",
1015 target);
1016 return;
1017 }
1018
1019 texObj = _mesa_get_current_tex_object(ctx, target);
1020 if (!texObj)
1021 return;
1022
1023 texImage = _mesa_select_tex_image(texObj, target, level);
1024 if (!texImage)
1025 return;
1026
1027 _mesa_get_texture_image(ctx, texObj, texImage, target, level, format, type,
1028 bufSize, pixels, false);
1029 }
1030
1031
1032 void GLAPIENTRY
1033 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1034 GLenum type, GLvoid *pixels )
1035 {
1036 _mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
1037 }
1038
1039 /**
1040 * Get texture image.
1041 *
1042 * \param texture texture name.
1043 * \param level image level.
1044 * \param format pixel data format for returned image.
1045 * \param type pixel data type for returned image.
1046 * \param bufSize size of the pixels data buffer.
1047 * \param pixels returned pixel data.
1048 */
1049 void GLAPIENTRY
1050 _mesa_GetTextureImage(GLuint texture, GLint level, GLenum format,
1051 GLenum type, GLsizei bufSize, GLvoid *pixels)
1052 {
1053 struct gl_texture_object *texObj;
1054 struct gl_texture_image *texImage;
1055 int i;
1056 GLint image_stride;
1057 GLenum err;
1058 GET_CURRENT_CONTEXT(ctx);
1059
1060 /*
1061 * This has been moved here because a format/type mismatch can cause a NULL
1062 * texImage object, which in turn causes the mismatch error to be
1063 * ignored.
1064 */
1065 err = _mesa_error_check_format_and_type(ctx, format, type);
1066 if (err != GL_NO_ERROR) {
1067 _mesa_error(ctx, err, "glGetTextureImage(format/type)");
1068 return;
1069 }
1070
1071 texObj = _mesa_lookup_texture_err(ctx, texture, "glGetTextureImage");
1072 if (!texObj)
1073 return;
1074
1075 /*
1076 * Legal target checking has been moved here to prevent exiting with a NULL
1077 * texImage object.
1078 */
1079 if (!legal_getteximage_target(ctx, texObj->Target, true)) {
1080 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTextureImage(target=%s)",
1081 _mesa_lookup_enum_by_nr(texObj->Target));
1082 return;
1083 }
1084
1085 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1086 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
1087
1088 /* Error checking */
1089 if (texObj->NumLayers < 6) {
1090 /* Not enough image planes for a cube map. The spec does not say
1091 * what should happen in this case because the user has always
1092 * specified each cube face separately (using
1093 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
1094 * This is addressed in Khronos Bug 13223.
1095 */
1096 _mesa_error(ctx, GL_INVALID_OPERATION,
1097 "glGetTextureImage(insufficient cube map storage)");
1098 return;
1099 }
1100
1101 /*
1102 * What do we do if the user created a texture with the following code
1103 * and then called this function with its handle?
1104 *
1105 * GLuint tex;
1106 * glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
1107 * glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
1108 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
1109 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
1110 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
1111 * // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
1112 * // wrong format, or given the wrong size, etc.
1113 * glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
1114 * glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
1115 *
1116 * A bug has been filed against the spec for this case. In the
1117 * meantime, we will check for cube completeness.
1118 *
1119 * According to Section 8.17 Texture Completeness in the OpenGL 4.5
1120 * Core Profile spec (30.10.2014):
1121 * "[A] cube map texture is cube complete if the
1122 * following conditions all hold true: The [base level] texture
1123 * images of each of the six cube map faces have identical, positive,
1124 * and square dimensions. The [base level] images were each specified
1125 * with the same internal format."
1126 *
1127 * It seems reasonable to check for cube completeness of an arbitrary
1128 * level here so that the returned data has a consistent format and size
1129 * and therefore fits in the user's buffer.
1130 */
1131 if (!_mesa_cube_level_complete(texObj, level)) {
1132 _mesa_error(ctx, GL_INVALID_OPERATION,
1133 "glGetTextureImage(cube map incomplete)");
1134 return;
1135 }
1136
1137 /* Copy each face. */
1138 for (i = 0; i < 6; ++i) {
1139 texImage = texObj->Image[i][level];
1140 _mesa_get_texture_image(ctx, texObj, texImage, texObj->Target, level,
1141 format, type, bufSize, pixels, true);
1142
1143 image_stride = _mesa_image_image_stride(&ctx->Pack, texImage->Width,
1144 texImage->Height, format,
1145 type);
1146 pixels = (GLubyte *) pixels + image_stride;
1147 bufSize -= image_stride;
1148 }
1149 }
1150 else {
1151 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
1152 if (!texImage)
1153 return;
1154
1155 _mesa_get_texture_image(ctx, texObj, texImage, texObj->Target, level,
1156 format, type, bufSize, pixels, true);
1157 }
1158 }
1159
1160 /**
1161 * Do error checking for a glGetCompressedTexImage() call.
1162 * \return GL_TRUE if any error, GL_FALSE if no errors.
1163 */
1164 static GLboolean
1165 getcompressedteximage_error_check(struct gl_context *ctx,
1166 struct gl_texture_image *texImage,
1167 GLenum target,
1168 GLint level, GLsizei clientMemSize,
1169 GLvoid *img, bool dsa)
1170 {
1171 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
1172 GLuint compressedSize, dimensions;
1173 const char *suffix = dsa ? "ture" : "";
1174
1175 assert(texImage);
1176
1177 if (!legal_getteximage_target(ctx, target, dsa)) {
1178 _mesa_error(ctx, GL_INVALID_ENUM,
1179 "glGetCompressedTex%sImage(target=%s)", suffix,
1180 _mesa_lookup_enum_by_nr(target));
1181 return GL_TRUE;
1182 }
1183
1184 assert(maxLevels != 0);
1185 if (level < 0 || level >= maxLevels) {
1186 _mesa_error(ctx, GL_INVALID_VALUE,
1187 "glGetCompressedTex%sImage(bad level = %d)", suffix, level);
1188 return GL_TRUE;
1189 }
1190
1191 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
1192 _mesa_error(ctx, GL_INVALID_OPERATION,
1193 "glGetCompressedTex%sImage(texture is not compressed)",
1194 suffix);
1195 return GL_TRUE;
1196 }
1197
1198 compressedSize = _mesa_format_image_size(texImage->TexFormat,
1199 texImage->Width,
1200 texImage->Height,
1201 texImage->Depth);
1202
1203 /* Check for invalid pixel storage modes */
1204 dimensions = _mesa_get_texture_dimensions(texImage->TexObject->Target);
1205 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
1206 &ctx->Pack, dsa ?
1207 "glGetCompressedTextureImage":
1208 "glGetCompressedTexImage")) {
1209 return GL_TRUE;
1210 }
1211
1212 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1213 /* do bounds checking on writing to client memory */
1214 if (clientMemSize < (GLsizei) compressedSize) {
1215 _mesa_error(ctx, GL_INVALID_OPERATION,
1216 "%s(out of bounds access: bufSize (%d) is too small)",
1217 dsa ? "glGetCompressedTextureImage" :
1218 "glGetnCompressedTexImageARB", clientMemSize);
1219 return GL_TRUE;
1220 }
1221 } else {
1222 /* do bounds checking on PBO write */
1223 if ((const GLubyte *) img + compressedSize >
1224 (const GLubyte *) ctx->Pack.BufferObj->Size) {
1225 _mesa_error(ctx, GL_INVALID_OPERATION,
1226 "glGetCompressedTex%sImage(out of bounds PBO access)",
1227 suffix);
1228 return GL_TRUE;
1229 }
1230
1231 /* make sure PBO is not mapped */
1232 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1233 _mesa_error(ctx, GL_INVALID_OPERATION,
1234 "glGetCompressedTex%sImage(PBO is mapped)", suffix);
1235 return GL_TRUE;
1236 }
1237 }
1238
1239 return GL_FALSE;
1240 }
1241
1242 /** Implements glGetnCompressedTexImageARB, glGetCompressedTexImage, and
1243 * glGetCompressedTextureImage.
1244 *
1245 * texImage must be passed in because glGetCompressedTexImage must handle the
1246 * target GL_TEXTURE_CUBE_MAP.
1247 */
1248 void
1249 _mesa_get_compressed_texture_image(struct gl_context *ctx,
1250 struct gl_texture_object *texObj,
1251 struct gl_texture_image *texImage,
1252 GLenum target, GLint level,
1253 GLsizei bufSize, GLvoid *pixels,
1254 bool dsa)
1255 {
1256 assert(texObj);
1257 assert(texImage);
1258
1259 FLUSH_VERTICES(ctx, 0);
1260
1261 if (getcompressedteximage_error_check(ctx, texImage, target, level,
1262 bufSize, pixels, dsa)) {
1263 return;
1264 }
1265
1266 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1267 /* not an error, do nothing */
1268 return;
1269 }
1270
1271 if (_mesa_is_zero_size_texture(texImage))
1272 return;
1273
1274 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1275 _mesa_debug(ctx,
1276 "glGetCompressedTex%sImage(tex %u) format = %s, w=%d, h=%d\n",
1277 dsa ? "ture" : "", texObj->Name,
1278 _mesa_get_format_name(texImage->TexFormat),
1279 texImage->Width, texImage->Height);
1280 }
1281
1282 _mesa_lock_texture(ctx, texObj);
1283 {
1284 ctx->Driver.GetCompressedTexImage(ctx, texImage, pixels);
1285 }
1286 _mesa_unlock_texture(ctx, texObj);
1287 }
1288
1289 void GLAPIENTRY
1290 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1291 GLvoid *img)
1292 {
1293 struct gl_texture_object *texObj;
1294 struct gl_texture_image *texImage;
1295 GET_CURRENT_CONTEXT(ctx);
1296
1297 texObj = _mesa_get_current_tex_object(ctx, target);
1298 if (!texObj)
1299 return;
1300
1301 texImage = _mesa_select_tex_image(texObj, target, level);
1302 if (!texImage)
1303 return;
1304
1305 _mesa_get_compressed_texture_image(ctx, texObj, texImage, target, level,
1306 bufSize, img, false);
1307 }
1308
1309 void GLAPIENTRY
1310 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *img)
1311 {
1312 _mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
1313 }
1314
1315 /**
1316 * Get compressed texture image.
1317 *
1318 * \param texture texture name.
1319 * \param level image level.
1320 * \param bufSize size of the pixels data buffer.
1321 * \param pixels returned pixel data.
1322 */
1323 void GLAPIENTRY
1324 _mesa_GetCompressedTextureImage(GLuint texture, GLint level,
1325 GLsizei bufSize, GLvoid *pixels)
1326 {
1327 struct gl_texture_object *texObj;
1328 struct gl_texture_image *texImage;
1329 int i;
1330 GLint image_stride;
1331 GET_CURRENT_CONTEXT(ctx);
1332
1333 texObj = _mesa_lookup_texture_err(ctx, texture,
1334 "glGetCompressedTextureImage");
1335 if (!texObj)
1336 return;
1337
1338 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1339 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
1340 assert(texObj->NumLayers >= 6);
1341
1342 /* Copy each face. */
1343 for (i = 0; i < 6; ++i) {
1344 texImage = texObj->Image[i][level];
1345 if (!texImage)
1346 return;
1347
1348 _mesa_get_compressed_texture_image(ctx, texObj, texImage,
1349 texObj->Target, level,
1350 bufSize, pixels, true);
1351
1352 /* Compressed images don't have a client format */
1353 image_stride = _mesa_format_image_size(texImage->TexFormat,
1354 texImage->Width,
1355 texImage->Height, 1);
1356
1357 pixels = (GLubyte *) pixels + image_stride;
1358 bufSize -= image_stride;
1359 }
1360 }
1361 else {
1362 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
1363 if (!texImage)
1364 return;
1365
1366 _mesa_get_compressed_texture_image(ctx, texObj, texImage,
1367 texObj->Target, level, bufSize,
1368 pixels, true);
1369 }
1370 }