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