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