Merge branch 'master' of ../mesa into vulkan
[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
365 /* Handle byte swapping if required */
366 if (ctx->Pack.SwapBytes) {
367 _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
368 width, height, dest, dest);
369 }
370
371 tempSlice += 4 * width * height;
372 }
373
374 free(tempImage);
375 }
376
377
378 /**
379 * Return a base GL format given the user-requested format
380 * for glGetTexImage().
381 */
382 GLenum
383 _mesa_base_pack_format(GLenum format)
384 {
385 switch (format) {
386 case GL_ABGR_EXT:
387 case GL_BGRA:
388 case GL_BGRA_INTEGER:
389 case GL_RGBA_INTEGER:
390 return GL_RGBA;
391 case GL_BGR:
392 case GL_BGR_INTEGER:
393 case GL_RGB_INTEGER:
394 return GL_RGB;
395 case GL_RED_INTEGER:
396 return GL_RED;
397 case GL_GREEN_INTEGER:
398 return GL_GREEN;
399 case GL_BLUE_INTEGER:
400 return GL_BLUE;
401 case GL_ALPHA_INTEGER:
402 return GL_ALPHA;
403 case GL_LUMINANCE_INTEGER_EXT:
404 return GL_LUMINANCE;
405 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
406 return GL_LUMINANCE_ALPHA;
407 default:
408 return format;
409 }
410 }
411
412
413 /**
414 * Get an uncompressed color texture image.
415 */
416 static void
417 get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
418 GLint xoffset, GLint yoffset, GLint zoffset,
419 GLsizei width, GLsizei height, GLint depth,
420 GLenum format, GLenum type, GLvoid *pixels,
421 struct gl_texture_image *texImage,
422 GLbitfield transferOps)
423 {
424 /* don't want to apply sRGB -> RGB conversion here so override the format */
425 const mesa_format texFormat =
426 _mesa_get_srgb_format_linear(texImage->TexFormat);
427 GLuint img;
428 GLboolean dst_is_integer;
429 uint32_t dst_format;
430 int dst_stride;
431 uint8_t rebaseSwizzle[4];
432 bool needsRebase;
433 void *rgba = NULL;
434
435 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
436 depth = height;
437 height = 1;
438 zoffset = yoffset;
439 yoffset = 0;
440 }
441
442 /* Depending on the base format involved we may need to apply a rebase
443 * transform (for example: if we download to a Luminance format we want
444 * G=0 and B=0).
445 */
446 if (texImage->_BaseFormat == GL_LUMINANCE ||
447 texImage->_BaseFormat == GL_INTENSITY) {
448 needsRebase = true;
449 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
450 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
451 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
452 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
453 } else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
454 needsRebase = true;
455 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
456 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
457 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
458 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
459 } else if (texImage->_BaseFormat !=
460 _mesa_get_format_base_format(texFormat)) {
461 needsRebase =
462 _mesa_compute_rgba2base2rgba_component_mapping(texImage->_BaseFormat,
463 rebaseSwizzle);
464 } else {
465 needsRebase = false;
466 }
467
468 /* Describe the dst format */
469 dst_is_integer = _mesa_is_enum_format_integer(format);
470 dst_format = _mesa_format_from_format_and_type(format, type);
471 dst_stride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
472
473 /* Since _mesa_format_convert does not handle transferOps we need to handle
474 * them before we call the function. This requires to convert to RGBA float
475 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
476 * integer then transferOps do not apply.
477 */
478 assert(!transferOps || (transferOps && !dst_is_integer));
479 (void) dst_is_integer; /* silence unused var warning */
480
481 for (img = 0; img < depth; img++) {
482 GLubyte *srcMap;
483 GLint rowstride;
484 GLubyte *img_src;
485 void *dest;
486 void *src;
487 int src_stride;
488 uint32_t src_format;
489
490 /* map src texture buffer */
491 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
492 xoffset, yoffset, width, height,
493 GL_MAP_READ_BIT,
494 &srcMap, &rowstride);
495 if (!srcMap) {
496 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
497 goto done;
498 }
499
500 img_src = srcMap;
501 dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
502 width, height, format, type,
503 img, 0, 0);
504
505 if (transferOps) {
506 uint32_t rgba_format;
507 int rgba_stride;
508 bool need_convert = false;
509
510 /* We will convert to RGBA float */
511 rgba_format = RGBA32_FLOAT;
512 rgba_stride = width * 4 * sizeof(GLfloat);
513
514 /* If we are lucky and the dst format matches the RGBA format we need
515 * to convert to, then we can convert directly into the dst buffer
516 * and avoid the final conversion/copy from the rgba buffer to the dst
517 * buffer.
518 */
519 if (format == rgba_format) {
520 rgba = dest;
521 } else if (rgba == NULL) { /* Allocate the RGBA buffer only once */
522 need_convert = true;
523 rgba = malloc(height * rgba_stride);
524 if (!rgba) {
525 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
526 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
527 return;
528 }
529 }
530
531 _mesa_format_convert(rgba, rgba_format, rgba_stride,
532 img_src, texFormat, rowstride,
533 width, height,
534 needsRebase ? rebaseSwizzle : NULL);
535
536 /* Handle transfer ops now */
537 _mesa_apply_rgba_transfer_ops(ctx, transferOps, width * height, rgba);
538
539 /* If we had to rebase, we have already handled that */
540 needsRebase = false;
541
542 /* If we were lucky and our RGBA conversion matches the dst format,
543 * then we are done.
544 */
545 if (!need_convert)
546 goto do_swap;
547
548 /* Otherwise, we need to convert from RGBA to dst next */
549 src = rgba;
550 src_format = rgba_format;
551 src_stride = rgba_stride;
552 } else {
553 /* No RGBA conversion needed, convert directly to dst */
554 src = img_src;
555 src_format = texFormat;
556 src_stride = rowstride;
557 }
558
559 /* Do the conversion to destination format */
560 _mesa_format_convert(dest, dst_format, dst_stride,
561 src, src_format, src_stride,
562 width, height,
563 needsRebase ? rebaseSwizzle : NULL);
564
565 do_swap:
566 /* Handle byte swapping if required */
567 if (ctx->Pack.SwapBytes)
568 _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
569 width, height, dest, dest);
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, NULL);
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.GetCompressedTexSubImage().
776 * All error checking will have been done before this routine is called.
777 */
778 void
779 _mesa_GetCompressedTexSubImage_sw(struct gl_context *ctx,
780 struct gl_texture_image *texImage,
781 GLint xoffset, GLint yoffset,
782 GLint zoffset, GLsizei width,
783 GLint height, GLint depth,
784 GLvoid *img)
785 {
786 const GLuint dimensions =
787 _mesa_get_texture_dimensions(texImage->TexObject->Target);
788 struct compressed_pixelstore store;
789 GLint slice;
790 GLubyte *dest;
791
792 _mesa_compute_compressed_pixelstore(dimensions, texImage->TexFormat,
793 width, height, depth,
794 &ctx->Pack, &store);
795
796 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
797 /* pack texture image into a PBO */
798 dest = (GLubyte *)
799 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
800 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
801 MAP_INTERNAL);
802 if (!dest) {
803 /* out of memory or other unexpected error */
804 _mesa_error(ctx, GL_OUT_OF_MEMORY,
805 "glGetCompresssedTexImage(map PBO failed)");
806 return;
807 }
808 dest = ADD_POINTERS(dest, img);
809 } else {
810 dest = img;
811 }
812
813 dest += store.SkipBytes;
814
815 for (slice = 0; slice < store.CopySlices; slice++) {
816 GLint srcRowStride;
817 GLubyte *src;
818
819 /* map src texture buffer */
820 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
821 xoffset, yoffset, width, height,
822 GL_MAP_READ_BIT, &src, &srcRowStride);
823
824 if (src) {
825 GLint i;
826 for (i = 0; i < store.CopyRowsPerSlice; i++) {
827 memcpy(dest, src, store.CopyBytesPerRow);
828 dest += store.TotalBytesPerRow;
829 src += srcRowStride;
830 }
831
832 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
833
834 /* Advance to next slice */
835 dest += store.TotalBytesPerRow * (store.TotalRowsPerSlice -
836 store.CopyRowsPerSlice);
837
838 } else {
839 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
840 }
841 }
842
843 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
844 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
845 }
846 }
847
848
849 /**
850 * Validate the texture target enum supplied to glGetTex(ture)Image or
851 * glGetCompressedTex(ture)Image.
852 */
853 static GLboolean
854 legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
855 {
856 switch (target) {
857 case GL_TEXTURE_1D:
858 case GL_TEXTURE_2D:
859 case GL_TEXTURE_3D:
860 return GL_TRUE;
861 case GL_TEXTURE_RECTANGLE_NV:
862 return ctx->Extensions.NV_texture_rectangle;
863 case GL_TEXTURE_1D_ARRAY_EXT:
864 case GL_TEXTURE_2D_ARRAY_EXT:
865 return ctx->Extensions.EXT_texture_array;
866 case GL_TEXTURE_CUBE_MAP_ARRAY:
867 return ctx->Extensions.ARB_texture_cube_map_array;
868
869 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
870 * (30.10.2014) says:
871 * "An INVALID_ENUM error is generated if the effective target is not
872 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
873 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
874 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
875 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
876 */
877 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
878 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
879 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
880 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
881 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
882 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
883 return dsa ? GL_FALSE : ctx->Extensions.ARB_texture_cube_map;
884 case GL_TEXTURE_CUBE_MAP:
885 return dsa ? GL_TRUE : GL_FALSE;
886 default:
887 return GL_FALSE;
888 }
889 }
890
891
892 /**
893 * Wrapper for _mesa_select_tex_image() which can handle target being
894 * GL_TEXTURE_CUBE_MAP_ARB in which case we use zoffset to select a cube face.
895 * This can happen for glGetTextureImage and glGetTextureSubImage (DSA
896 * functions).
897 */
898 static struct gl_texture_image *
899 select_tex_image(const struct gl_texture_object *texObj, GLenum target,
900 GLint level, GLint zoffset)
901 {
902 assert(level >= 0);
903 assert(level < MAX_TEXTURE_LEVELS);
904 if (target == GL_TEXTURE_CUBE_MAP) {
905 assert(zoffset >= 0);
906 assert(zoffset < 6);
907 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset;
908 }
909 return _mesa_select_tex_image(texObj, target, level);
910 }
911
912
913 /**
914 * Error-check the offset and size arguments to
915 * glGet[Compressed]TextureSubImage(). Also checks if the specified
916 * texture image is missing.
917 * \return true if error, false if no error.
918 */
919 static bool
920 dimensions_error_check(struct gl_context *ctx,
921 struct gl_texture_object *texObj,
922 GLenum target, GLint level,
923 GLint xoffset, GLint yoffset, GLint zoffset,
924 GLsizei width, GLsizei height, GLsizei depth,
925 const char *caller)
926 {
927 const struct gl_texture_image *texImage;
928 int i;
929
930 if (xoffset < 0) {
931 _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset = %d)", caller, xoffset);
932 return true;
933 }
934
935 if (yoffset < 0) {
936 _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset = %d)", caller, yoffset);
937 return true;
938 }
939
940 if (zoffset < 0) {
941 _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset = %d)", caller, zoffset);
942 return true;
943 }
944
945 if (width < 0) {
946 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width = %d)", caller, width);
947 return true;
948 }
949
950 if (height < 0) {
951 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height = %d)", caller, height);
952 return true;
953 }
954
955 if (depth < 0) {
956 _mesa_error(ctx, GL_INVALID_VALUE, "%s(depth = %d)", caller, depth);
957 return true;
958 }
959
960 /* do special per-target checks */
961 switch (target) {
962 case GL_TEXTURE_1D:
963 if (yoffset != 0) {
964 _mesa_error(ctx, GL_INVALID_VALUE,
965 "%s(1D, yoffset = %d)", caller, yoffset);
966 return true;
967 }
968 if (height > 1) {
969 _mesa_error(ctx, GL_INVALID_VALUE,
970 "%s(1D, height = %d)", caller, height);
971 return true;
972 }
973 /* fall-through */
974 case GL_TEXTURE_1D_ARRAY:
975 case GL_TEXTURE_2D:
976 case GL_TEXTURE_RECTANGLE:
977 if (zoffset != 0) {
978 _mesa_error(ctx, GL_INVALID_VALUE,
979 "%s(zoffset = %d)", caller, zoffset);
980 return true;
981 }
982 if (depth > 1) {
983 _mesa_error(ctx, GL_INVALID_VALUE,
984 "%s(depth = %d)", caller, depth);
985 return true;
986 }
987 break;
988 case GL_TEXTURE_CUBE_MAP:
989 /* Non-array cube maps are special because we have a gl_texture_image
990 * per face.
991 */
992 if (zoffset + depth > 6) {
993 _mesa_error(ctx, GL_INVALID_VALUE,
994 "%s(zoffset + depth = %d)", caller, zoffset + depth);
995 return true;
996 }
997 /* check that the range of faces exist */
998 for (i = 0; i < depth; i++) {
999 GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset + i;
1000 if (!_mesa_select_tex_image(texObj, face, level)) {
1001 /* non-existant face */
1002 _mesa_error(ctx, GL_INVALID_OPERATION,
1003 "%s(missing cube face)", caller);
1004 return true;
1005 }
1006 }
1007 break;
1008 default:
1009 ; /* nothing */
1010 }
1011
1012 texImage = select_tex_image(texObj, target, level, zoffset);
1013 if (!texImage) {
1014 /* missing texture image */
1015 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(missing image)", caller);
1016 return true;
1017 }
1018
1019 if (xoffset + width > texImage->Width) {
1020 _mesa_error(ctx, GL_INVALID_VALUE,
1021 "%s(xoffset %d + width %d > %u)",
1022 caller, xoffset, width, texImage->Width);
1023 return true;
1024 }
1025
1026 if (yoffset + height > texImage->Height) {
1027 _mesa_error(ctx, GL_INVALID_VALUE,
1028 "%s(yoffset %d + height %d > %u)",
1029 caller, yoffset, height, texImage->Height);
1030 return true;
1031 }
1032
1033 if (target != GL_TEXTURE_CUBE_MAP) {
1034 /* Cube map error checking was done above */
1035 if (zoffset + depth > texImage->Depth) {
1036 _mesa_error(ctx, GL_INVALID_VALUE,
1037 "%s(zoffset %d + depth %d > %u)",
1038 caller, zoffset, depth, texImage->Depth);
1039 return true;
1040 }
1041 }
1042
1043 /* Extra checks for compressed textures */
1044 {
1045 GLuint bw, bh;
1046 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
1047 if (bw > 1 || bh > 1) {
1048 /* offset must be multiple of block size */
1049 if (xoffset % bw != 0) {
1050 _mesa_error(ctx, GL_INVALID_VALUE,
1051 "%s(xoffset = %d)", caller, xoffset);
1052 return true;
1053 }
1054 if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY) {
1055 if (yoffset % bh != 0) {
1056 _mesa_error(ctx, GL_INVALID_VALUE,
1057 "%s(yoffset = %d)", caller, yoffset);
1058 return true;
1059 }
1060 }
1061
1062 /* The size must be a multiple of bw x bh, or we must be using a
1063 * offset+size that exactly hits the edge of the image.
1064 */
1065 if ((width % bw != 0) &&
1066 (xoffset + width != (GLint) texImage->Width)) {
1067 _mesa_error(ctx, GL_INVALID_VALUE,
1068 "%s(width = %d)", caller, width);
1069 return true;
1070 }
1071
1072 if ((height % bh != 0) &&
1073 (yoffset + height != (GLint) texImage->Height)) {
1074 _mesa_error(ctx, GL_INVALID_VALUE,
1075 "%s(height = %d)", caller, height);
1076 return true;
1077 }
1078 }
1079 }
1080
1081 if (width == 0 || height == 0 || depth == 0) {
1082 /* Not an error, but nothing to do. Return 'true' so that the
1083 * caller simply returns.
1084 */
1085 return true;
1086 }
1087
1088 return false;
1089 }
1090
1091
1092 /**
1093 * Do PBO-related error checking for getting uncompressed images.
1094 * \return true if there was an error (or the GetTexImage is to be a no-op)
1095 */
1096 static bool
1097 pbo_error_check(struct gl_context *ctx, GLenum target,
1098 GLsizei width, GLsizei height, GLsizei depth,
1099 GLenum format, GLenum type, GLsizei clientMemSize,
1100 GLvoid *pixels,
1101 const char *caller)
1102 {
1103 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
1104
1105 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, width, height, depth,
1106 format, type, clientMemSize, pixels)) {
1107 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1108 _mesa_error(ctx, GL_INVALID_OPERATION,
1109 "%s(out of bounds PBO access)", caller);
1110 } else {
1111 _mesa_error(ctx, GL_INVALID_OPERATION,
1112 "%s(out of bounds access: bufSize (%d) is too small)",
1113 caller, clientMemSize);
1114 }
1115 return true;
1116 }
1117
1118 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1119 /* PBO should not be mapped */
1120 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1121 _mesa_error(ctx, GL_INVALID_OPERATION,
1122 "%s(PBO is mapped)", caller);
1123 return true;
1124 }
1125 }
1126
1127 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1128 /* not an error, do nothing */
1129 return true;
1130 }
1131
1132 return false;
1133 }
1134
1135
1136 /**
1137 * Do error checking for all (non-compressed) get-texture-image functions.
1138 * \return true if any error, false if no errors.
1139 */
1140 static bool
1141 getteximage_error_check(struct gl_context *ctx,
1142 struct gl_texture_object *texObj,
1143 GLenum target, GLint level,
1144 GLint xoffset, GLint yoffset, GLint zoffset,
1145 GLsizei width, GLsizei height, GLsizei depth,
1146 GLenum format, GLenum type, GLsizei bufSize,
1147 GLvoid *pixels, const char *caller)
1148 {
1149 struct gl_texture_image *texImage;
1150 GLenum baseFormat, err;
1151 GLint maxLevels;
1152
1153 assert(texObj);
1154
1155 if (texObj->Target == 0) {
1156 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
1157 return true;
1158 }
1159
1160 maxLevels = _mesa_max_texture_levels(ctx, target);
1161 if (level < 0 || level >= maxLevels) {
1162 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level = %d)", caller, level);
1163 return true;
1164 }
1165
1166 err = _mesa_error_check_format_and_type(ctx, format, type);
1167 if (err != GL_NO_ERROR) {
1168 _mesa_error(ctx, err, "%s(format/type)", caller);
1169 return true;
1170 }
1171
1172 if (dimensions_error_check(ctx, texObj, target, level,
1173 xoffset, yoffset, zoffset,
1174 width, height, depth, caller)) {
1175 return true;
1176 }
1177
1178 if (pbo_error_check(ctx, target, width, height, depth,
1179 format, type, bufSize, pixels, caller)) {
1180 return true;
1181 }
1182
1183 texImage = select_tex_image(texObj, target, level, zoffset);
1184 assert(texImage);
1185
1186 /*
1187 * Format and type checking has been moved up to GetnTexImage and
1188 * GetTextureImage so that it happens before getting the texImage object.
1189 */
1190
1191 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
1192
1193 /* Make sure the requested image format is compatible with the
1194 * texture's format.
1195 */
1196 if (_mesa_is_color_format(format)
1197 && !_mesa_is_color_format(baseFormat)) {
1198 _mesa_error(ctx, GL_INVALID_OPERATION,
1199 "%s(format mismatch)", caller);
1200 return true;
1201 }
1202 else if (_mesa_is_depth_format(format)
1203 && !_mesa_is_depth_format(baseFormat)
1204 && !_mesa_is_depthstencil_format(baseFormat)) {
1205 _mesa_error(ctx, GL_INVALID_OPERATION,
1206 "%s(format mismatch)", caller);
1207 return true;
1208 }
1209 else if (_mesa_is_stencil_format(format)
1210 && !ctx->Extensions.ARB_texture_stencil8) {
1211 _mesa_error(ctx, GL_INVALID_ENUM,
1212 "%s(format=GL_STENCIL_INDEX)", caller);
1213 return true;
1214 }
1215 else if (_mesa_is_stencil_format(format)
1216 && !_mesa_is_depthstencil_format(baseFormat)
1217 && !_mesa_is_stencil_format(baseFormat)) {
1218 _mesa_error(ctx, GL_INVALID_OPERATION,
1219 "%s(format mismatch)", caller);
1220 return true;
1221 }
1222 else if (_mesa_is_ycbcr_format(format)
1223 && !_mesa_is_ycbcr_format(baseFormat)) {
1224 _mesa_error(ctx, GL_INVALID_OPERATION,
1225 "%s(format mismatch)", caller);
1226 return true;
1227 }
1228 else if (_mesa_is_depthstencil_format(format)
1229 && !_mesa_is_depthstencil_format(baseFormat)) {
1230 _mesa_error(ctx, GL_INVALID_OPERATION,
1231 "%s(format mismatch)", caller);
1232 return true;
1233 }
1234 else if (!_mesa_is_stencil_format(format) &&
1235 _mesa_is_enum_format_integer(format) !=
1236 _mesa_is_format_integer(texImage->TexFormat)) {
1237 _mesa_error(ctx, GL_INVALID_OPERATION,
1238 "%s(format mismatch)", caller);
1239 return true;
1240 }
1241
1242 return false;
1243 }
1244
1245
1246 /**
1247 * Return the width, height and depth of a texture image.
1248 * This function must be resilient to bad parameter values since
1249 * this is called before full error checking.
1250 */
1251 static void
1252 get_texture_image_dims(const struct gl_texture_object *texObj,
1253 GLenum target, GLint level,
1254 GLsizei *width, GLsizei *height, GLsizei *depth)
1255 {
1256 const struct gl_texture_image *texImage = NULL;
1257
1258 if (level >= 0 && level < MAX_TEXTURE_LEVELS) {
1259 texImage = _mesa_select_tex_image(texObj, target, level);
1260 }
1261
1262 if (texImage) {
1263 *width = texImage->Width;
1264 *height = texImage->Height;
1265 if (target == GL_TEXTURE_CUBE_MAP) {
1266 *depth = 6;
1267 }
1268 else {
1269 *depth = texImage->Depth;
1270 }
1271 }
1272 else {
1273 *width = *height = *depth = 0;
1274 }
1275 }
1276
1277
1278 /**
1279 * Common code for all (uncompressed) get-texture-image functions.
1280 * \param texObj the texture object (should not be null)
1281 * \param target user-provided target, or 0 for DSA
1282 * \param level image level.
1283 * \param format pixel data format for returned image.
1284 * \param type pixel data type for returned image.
1285 * \param bufSize size of the pixels data buffer.
1286 * \param pixels returned pixel data.
1287 * \param caller name of calling function
1288 */
1289 static void
1290 get_texture_image(struct gl_context *ctx,
1291 struct gl_texture_object *texObj,
1292 GLenum target, GLint level,
1293 GLint xoffset, GLint yoffset, GLint zoffset,
1294 GLsizei width, GLsizei height, GLint depth,
1295 GLenum format, GLenum type,
1296 GLvoid *pixels, const char *caller)
1297 {
1298 struct gl_texture_image *texImage;
1299 unsigned firstFace, numFaces, i;
1300 GLint imageStride;
1301
1302 FLUSH_VERTICES(ctx, 0);
1303
1304 texImage = select_tex_image(texObj, target, level, zoffset);
1305 assert(texImage); /* should have been error checked already */
1306
1307 if (_mesa_is_zero_size_texture(texImage)) {
1308 /* no image data to return */
1309 return;
1310 }
1311
1312 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1313 _mesa_debug(ctx, "%s(tex %u) format = %s, w=%d, h=%d,"
1314 " dstFmt=0x%x, dstType=0x%x\n",
1315 caller, texObj->Name,
1316 _mesa_get_format_name(texImage->TexFormat),
1317 texImage->Width, texImage->Height,
1318 format, type);
1319 }
1320
1321 if (target == GL_TEXTURE_CUBE_MAP) {
1322 /* Compute stride between cube faces */
1323 imageStride = _mesa_image_image_stride(&ctx->Pack, width, height,
1324 format, type);
1325 firstFace = zoffset;
1326 numFaces = depth;
1327 zoffset = 0;
1328 depth = 1;
1329 }
1330 else {
1331 imageStride = 0;
1332 firstFace = _mesa_tex_target_to_face(target);
1333 numFaces = 1;
1334 }
1335
1336 _mesa_lock_texture(ctx, texObj);
1337
1338 for (i = 0; i < numFaces; i++) {
1339 texImage = texObj->Image[firstFace + i][level];
1340 assert(texImage);
1341
1342 ctx->Driver.GetTexSubImage(ctx, xoffset, yoffset, zoffset,
1343 width, height, depth,
1344 format, type, pixels, texImage);
1345
1346 /* next cube face */
1347 pixels = (GLubyte *) pixels + imageStride;
1348 }
1349
1350 _mesa_unlock_texture(ctx, texObj);
1351 }
1352
1353
1354 void GLAPIENTRY
1355 _mesa_GetnTexImageARB(GLenum target, GLint level, GLenum format, GLenum type,
1356 GLsizei bufSize, GLvoid *pixels)
1357 {
1358 GET_CURRENT_CONTEXT(ctx);
1359 static const char *caller = "glGetnTexImageARB";
1360 GLsizei width, height, depth;
1361 struct gl_texture_object *texObj;
1362
1363 if (!legal_getteximage_target(ctx, target, false)) {
1364 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1365 return;
1366 }
1367
1368 texObj = _mesa_get_current_tex_object(ctx, target);
1369 assert(texObj);
1370
1371 get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1372
1373 if (getteximage_error_check(ctx, texObj, target, level,
1374 0, 0, 0, width, height, depth,
1375 format, type, bufSize, pixels, caller)) {
1376 return;
1377 }
1378
1379 get_texture_image(ctx, texObj, target, level,
1380 0, 0, 0, width, height, depth,
1381 format, type, pixels, caller);
1382 }
1383
1384
1385 void GLAPIENTRY
1386 _mesa_GetTexImage(GLenum target, GLint level, GLenum format, GLenum type,
1387 GLvoid *pixels )
1388 {
1389 GET_CURRENT_CONTEXT(ctx);
1390 static const char *caller = "glGetTexImage";
1391 GLsizei width, height, depth;
1392 struct gl_texture_object *texObj;
1393
1394 if (!legal_getteximage_target(ctx, target, false)) {
1395 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1396 return;
1397 }
1398
1399 texObj = _mesa_get_current_tex_object(ctx, target);
1400 assert(texObj);
1401
1402 get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1403
1404 if (getteximage_error_check(ctx, texObj, target, level,
1405 0, 0, 0, width, height, depth,
1406 format, type, INT_MAX, pixels, caller)) {
1407 return;
1408 }
1409
1410 get_texture_image(ctx, texObj, target, level,
1411 0, 0, 0, width, height, depth,
1412 format, type, pixels, caller);
1413 }
1414
1415
1416 void GLAPIENTRY
1417 _mesa_GetTextureImage(GLuint texture, GLint level, GLenum format, GLenum type,
1418 GLsizei bufSize, GLvoid *pixels)
1419 {
1420 GET_CURRENT_CONTEXT(ctx);
1421 GLsizei width, height, depth;
1422 static const char *caller = "glGetTextureImage";
1423 struct gl_texture_object *texObj =
1424 _mesa_lookup_texture_err(ctx, texture, caller);
1425
1426 if (!texObj) {
1427 return;
1428 }
1429
1430 get_texture_image_dims(texObj, texObj->Target, level,
1431 &width, &height, &depth);
1432
1433 if (getteximage_error_check(ctx, texObj, texObj->Target, level,
1434 0, 0, 0, width, height, depth,
1435 format, type, bufSize, pixels, caller)) {
1436 return;
1437 }
1438
1439 get_texture_image(ctx, texObj, texObj->Target, level,
1440 0, 0, 0, width, height, depth,
1441 format, type, pixels, caller);
1442 }
1443
1444
1445 void GLAPIENTRY
1446 _mesa_GetTextureSubImage(GLuint texture, GLint level,
1447 GLint xoffset, GLint yoffset, GLint zoffset,
1448 GLsizei width, GLsizei height, GLsizei depth,
1449 GLenum format, GLenum type, GLsizei bufSize,
1450 void *pixels)
1451 {
1452 GET_CURRENT_CONTEXT(ctx);
1453 static const char *caller = "glGetTextureSubImage";
1454 struct gl_texture_object *texObj =
1455 _mesa_lookup_texture_err(ctx, texture, caller);
1456
1457 if (!texObj) {
1458 return;
1459 }
1460
1461 if (getteximage_error_check(ctx, texObj, texObj->Target, level,
1462 xoffset, yoffset, zoffset, width, height, depth,
1463 format, type, bufSize, pixels, caller)) {
1464 return;
1465 }
1466
1467 get_texture_image(ctx, texObj, texObj->Target, level,
1468 xoffset, yoffset, zoffset, width, height, depth,
1469 format, type, pixels, caller);
1470 }
1471
1472
1473
1474 /**
1475 * Compute the number of bytes which will be written when retrieving
1476 * a sub-region of a compressed texture.
1477 */
1478 static GLsizei
1479 packed_compressed_size(GLuint dimensions, mesa_format format,
1480 GLsizei width, GLsizei height, GLsizei depth,
1481 const struct gl_pixelstore_attrib *packing)
1482 {
1483 struct compressed_pixelstore st;
1484 GLsizei totalBytes;
1485
1486 _mesa_compute_compressed_pixelstore(dimensions, format,
1487 width, height, depth,
1488 packing, &st);
1489 totalBytes =
1490 (st.CopySlices - 1) * st.TotalRowsPerSlice * st.TotalBytesPerRow +
1491 st.SkipBytes +
1492 (st.CopyRowsPerSlice - 1) * st.TotalBytesPerRow +
1493 st.CopyBytesPerRow;
1494
1495 return totalBytes;
1496 }
1497
1498
1499 /**
1500 * Do error checking for getting compressed texture images.
1501 * \return true if any error, false if no errors.
1502 */
1503 static bool
1504 getcompressedteximage_error_check(struct gl_context *ctx,
1505 struct gl_texture_object *texObj,
1506 GLenum target, GLint level,
1507 GLint xoffset, GLint yoffset, GLint zoffset,
1508 GLsizei width, GLsizei height, GLsizei depth,
1509 GLsizei bufSize, GLvoid *pixels,
1510 const char *caller)
1511 {
1512 struct gl_texture_image *texImage;
1513 GLint maxLevels;
1514 GLsizei totalBytes;
1515 GLuint dimensions;
1516
1517 assert(texObj);
1518
1519 if (texObj->Target == 0) {
1520 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
1521 return true;
1522 }
1523
1524 maxLevels = _mesa_max_texture_levels(ctx, target);
1525 if (level < 0 || level >= maxLevels) {
1526 _mesa_error(ctx, GL_INVALID_VALUE,
1527 "%s(bad level = %d)", caller, level);
1528 return true;
1529 }
1530
1531 if (dimensions_error_check(ctx, texObj, target, level,
1532 xoffset, yoffset, zoffset,
1533 width, height, depth, caller)) {
1534 return true;
1535 }
1536
1537 texImage = select_tex_image(texObj, target, level, zoffset);
1538 assert(texImage);
1539
1540 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
1541 _mesa_error(ctx, GL_INVALID_OPERATION,
1542 "%s(texture is not compressed)", caller);
1543 return true;
1544 }
1545
1546 /* Check for invalid pixel storage modes */
1547 dimensions = _mesa_get_texture_dimensions(texObj->Target);
1548 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
1549 &ctx->Pack,
1550 caller)) {
1551 return true;
1552 }
1553
1554 /* Compute number of bytes that may be touched in the dest buffer */
1555 totalBytes = packed_compressed_size(dimensions, texImage->TexFormat,
1556 width, height, depth,
1557 &ctx->Pack);
1558
1559 /* Do dest buffer bounds checking */
1560 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1561 /* do bounds checking on PBO write */
1562 if ((GLubyte *) pixels + totalBytes >
1563 (GLubyte *) ctx->Pack.BufferObj->Size) {
1564 _mesa_error(ctx, GL_INVALID_OPERATION,
1565 "%s(out of bounds PBO access)", caller);
1566 return true;
1567 }
1568
1569 /* make sure PBO is not mapped */
1570 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1571 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", caller);
1572 return true;
1573 }
1574 }
1575 else {
1576 /* do bounds checking on writing to client memory */
1577 if (totalBytes > bufSize) {
1578 _mesa_error(ctx, GL_INVALID_OPERATION,
1579 "%s(out of bounds access: bufSize (%d) is too small)",
1580 caller, bufSize);
1581 return true;
1582 }
1583 }
1584
1585 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1586 /* not an error, but do nothing */
1587 return true;
1588 }
1589
1590 return false;
1591 }
1592
1593
1594 /**
1595 * Common helper for all glGetCompressed-teximage functions.
1596 */
1597 static void
1598 get_compressed_texture_image(struct gl_context *ctx,
1599 struct gl_texture_object *texObj,
1600 GLenum target, GLint level,
1601 GLint xoffset, GLint yoffset, GLint zoffset,
1602 GLsizei width, GLsizei height, GLint depth,
1603 GLvoid *pixels,
1604 const char *caller)
1605 {
1606 struct gl_texture_image *texImage;
1607 unsigned firstFace, numFaces, i, imageStride;
1608
1609 FLUSH_VERTICES(ctx, 0);
1610
1611 texImage = select_tex_image(texObj, target, level, zoffset);
1612 assert(texImage); /* should have been error checked already */
1613
1614 if (_mesa_is_zero_size_texture(texImage))
1615 return;
1616
1617 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1618 _mesa_debug(ctx,
1619 "%s(tex %u) format = %s, w=%d, h=%d\n",
1620 caller, texObj->Name,
1621 _mesa_get_format_name(texImage->TexFormat),
1622 texImage->Width, texImage->Height);
1623 }
1624
1625 if (target == GL_TEXTURE_CUBE_MAP) {
1626 struct compressed_pixelstore store;
1627
1628 /* Compute image stride between cube faces */
1629 _mesa_compute_compressed_pixelstore(2, texImage->TexFormat,
1630 width, height, depth,
1631 &ctx->Pack, &store);
1632 imageStride = store.TotalBytesPerRow * store.TotalRowsPerSlice;
1633
1634 firstFace = zoffset;
1635 numFaces = depth;
1636 zoffset = 0;
1637 depth = 1;
1638 }
1639 else {
1640 imageStride = 0;
1641 firstFace = _mesa_tex_target_to_face(target);
1642 numFaces = 1;
1643 }
1644
1645 _mesa_lock_texture(ctx, texObj);
1646
1647 for (i = 0; i < numFaces; i++) {
1648 texImage = texObj->Image[firstFace + i][level];
1649 assert(texImage);
1650
1651 ctx->Driver.GetCompressedTexSubImage(ctx, texImage,
1652 xoffset, yoffset, zoffset,
1653 width, height, depth, pixels);
1654
1655 /* next cube face */
1656 pixels = (GLubyte *) pixels + imageStride;
1657 }
1658
1659 _mesa_unlock_texture(ctx, texObj);
1660 }
1661
1662
1663 void GLAPIENTRY
1664 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1665 GLvoid *pixels)
1666 {
1667 GET_CURRENT_CONTEXT(ctx);
1668 static const char *caller = "glGetnCompressedTexImageARB";
1669 GLsizei width, height, depth;
1670 struct gl_texture_object *texObj;
1671
1672 if (!legal_getteximage_target(ctx, target, false)) {
1673 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1674 return;
1675 }
1676
1677 texObj = _mesa_get_current_tex_object(ctx, target);
1678 assert(texObj);
1679
1680 get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1681
1682 if (getcompressedteximage_error_check(ctx, texObj, target, level,
1683 0, 0, 0, width, height, depth,
1684 INT_MAX, pixels, caller)) {
1685 return;
1686 }
1687
1688 get_compressed_texture_image(ctx, texObj, target, level,
1689 0, 0, 0, width, height, depth,
1690 pixels, caller);
1691 }
1692
1693
1694 void GLAPIENTRY
1695 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *pixels)
1696 {
1697 GET_CURRENT_CONTEXT(ctx);
1698 static const char *caller = "glGetCompressedTexImage";
1699 GLsizei width, height, depth;
1700 struct gl_texture_object *texObj;
1701
1702 if (!legal_getteximage_target(ctx, target, false)) {
1703 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1704 return;
1705 }
1706
1707 texObj = _mesa_get_current_tex_object(ctx, target);
1708 assert(texObj);
1709
1710 get_texture_image_dims(texObj, target, level,
1711 &width, &height, &depth);
1712
1713 if (getcompressedteximage_error_check(ctx, texObj, target, level,
1714 0, 0, 0, width, height, depth,
1715 INT_MAX, pixels, caller)) {
1716 return;
1717 }
1718
1719 get_compressed_texture_image(ctx, texObj, target, level,
1720 0, 0, 0, width, height, depth,
1721 pixels, caller);
1722 }
1723
1724
1725 void GLAPIENTRY
1726 _mesa_GetCompressedTextureImage(GLuint texture, GLint level,
1727 GLsizei bufSize, GLvoid *pixels)
1728 {
1729 GET_CURRENT_CONTEXT(ctx);
1730 static const char *caller = "glGetCompressedTextureImage";
1731 GLsizei width, height, depth;
1732 struct gl_texture_object *texObj =
1733 _mesa_lookup_texture_err(ctx, texture, caller);
1734
1735 if (!texObj) {
1736 return;
1737 }
1738
1739 get_texture_image_dims(texObj, texObj->Target, level,
1740 &width, &height, &depth);
1741
1742 if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
1743 0, 0, 0, width, height, depth,
1744 bufSize, pixels, caller)) {
1745 return;
1746 }
1747
1748 get_compressed_texture_image(ctx, texObj, texObj->Target, level,
1749 0, 0, 0, width, height, depth,
1750 pixels, caller);
1751 }
1752
1753
1754 void APIENTRY
1755 _mesa_GetCompressedTextureSubImage(GLuint texture, GLint level,
1756 GLint xoffset, GLint yoffset,
1757 GLint zoffset, GLsizei width,
1758 GLsizei height, GLsizei depth,
1759 GLsizei bufSize, void *pixels)
1760 {
1761 GET_CURRENT_CONTEXT(ctx);
1762 static const char *caller = "glGetCompressedTextureImage";
1763 struct gl_texture_object *texObj;
1764
1765 texObj = _mesa_lookup_texture_err(ctx, texture, caller);
1766 if (!texObj) {
1767 return;
1768 }
1769
1770 if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
1771 xoffset, yoffset, zoffset,
1772 width, height, depth,
1773 bufSize, pixels, caller)) {
1774 return;
1775 }
1776
1777 get_compressed_texture_image(ctx, texObj, texObj->Target, level,
1778 xoffset, yoffset, zoffset,
1779 width, height, depth,
1780 pixels, caller);
1781 }