mesa: remove _ARB suffix from cube map enums
[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 for (img = 0; img < depth; img++) {
92 GLubyte *srcMap;
93 GLint srcRowStride;
94
95 /* map src texture buffer */
96 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
97 xoffset, yoffset, width, height,
98 GL_MAP_READ_BIT, &srcMap, &srcRowStride);
99
100 if (srcMap) {
101 for (row = 0; row < height; row++) {
102 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
103 width, height, format, type,
104 img, row, 0);
105 const GLubyte *src = srcMap + row * srcRowStride;
106 _mesa_unpack_float_z_row(texImage->TexFormat, width, src, depthRow);
107 _mesa_pack_depth_span(ctx, width, dest, type, depthRow, &ctx->Pack);
108 }
109
110 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
111 }
112 else {
113 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
114 break;
115 }
116 }
117
118 free(depthRow);
119 }
120
121
122 /**
123 * glGetTexImage for depth/stencil pixels.
124 */
125 static void
126 get_tex_depth_stencil(struct gl_context *ctx, GLuint dimensions,
127 GLint xoffset, GLint yoffset, GLint zoffset,
128 GLsizei width, GLsizei height, GLint depth,
129 GLenum format, GLenum type, GLvoid *pixels,
130 struct gl_texture_image *texImage)
131 {
132 GLint img, row;
133
134 assert(format == GL_DEPTH_STENCIL);
135 assert(type == GL_UNSIGNED_INT_24_8 ||
136 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
137
138 for (img = 0; img < depth; img++) {
139 GLubyte *srcMap;
140 GLint rowstride;
141
142 /* map src texture buffer */
143 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
144 xoffset, yoffset, width, height,
145 GL_MAP_READ_BIT, &srcMap, &rowstride);
146
147 if (srcMap) {
148 for (row = 0; row < height; row++) {
149 const GLubyte *src = srcMap + row * rowstride;
150 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
151 width, height, format, type,
152 img, row, 0);
153 _mesa_unpack_depth_stencil_row(texImage->TexFormat,
154 width,
155 (const GLuint *) src,
156 type, dest);
157 if (ctx->Pack.SwapBytes) {
158 _mesa_swap4((GLuint *) dest, width);
159 }
160 }
161
162 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
163 }
164 else {
165 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
166 break;
167 }
168 }
169 }
170
171 /**
172 * glGetTexImage for stencil pixels.
173 */
174 static void
175 get_tex_stencil(struct gl_context *ctx, GLuint dimensions,
176 GLint xoffset, GLint yoffset, GLint zoffset,
177 GLsizei width, GLsizei height, GLint depth,
178 GLenum format, GLenum type, GLvoid *pixels,
179 struct gl_texture_image *texImage)
180 {
181 GLint img, row;
182
183 assert(format == GL_STENCIL_INDEX);
184
185 for (img = 0; img < depth; img++) {
186 GLubyte *srcMap;
187 GLint rowstride;
188
189 /* map src texture buffer */
190 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
191 xoffset, yoffset, width, height,
192 GL_MAP_READ_BIT,
193 &srcMap, &rowstride);
194
195 if (srcMap) {
196 for (row = 0; row < height; row++) {
197 const GLubyte *src = srcMap + row * rowstride;
198 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
199 width, height, format, type,
200 img, row, 0);
201 _mesa_unpack_ubyte_stencil_row(texImage->TexFormat,
202 width,
203 (const GLuint *) src,
204 dest);
205 }
206
207 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
208 }
209 else {
210 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
211 break;
212 }
213 }
214 }
215
216
217 /**
218 * glGetTexImage for YCbCr pixels.
219 */
220 static void
221 get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
222 GLint xoffset, GLint yoffset, GLint zoffset,
223 GLsizei width, GLsizei height, GLint depth,
224 GLenum format, GLenum type, GLvoid *pixels,
225 struct gl_texture_image *texImage)
226 {
227 GLint img, row;
228
229 for (img = 0; img < depth; img++) {
230 GLubyte *srcMap;
231 GLint rowstride;
232
233 /* map src texture buffer */
234 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
235 xoffset, yoffset, width, height,
236 GL_MAP_READ_BIT, &srcMap, &rowstride);
237
238 if (srcMap) {
239 for (row = 0; row < height; row++) {
240 const GLubyte *src = srcMap + row * rowstride;
241 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
242 width, height, format, type,
243 img, row, 0);
244 memcpy(dest, src, width * sizeof(GLushort));
245
246 /* check for byte swapping */
247 if ((texImage->TexFormat == MESA_FORMAT_YCBCR
248 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
249 (texImage->TexFormat == MESA_FORMAT_YCBCR_REV
250 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
251 if (!ctx->Pack.SwapBytes)
252 _mesa_swap2((GLushort *) dest, width);
253 }
254 else if (ctx->Pack.SwapBytes) {
255 _mesa_swap2((GLushort *) dest, width);
256 }
257 }
258
259 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
260 }
261 else {
262 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
263 break;
264 }
265 }
266 }
267
268
269 /**
270 * Get a color texture image with decompression.
271 */
272 static void
273 get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
274 GLint xoffset, GLint yoffset, GLint zoffset,
275 GLsizei width, GLsizei height, GLint depth,
276 GLenum format, GLenum type, GLvoid *pixels,
277 struct gl_texture_image *texImage,
278 GLbitfield transferOps)
279 {
280 /* don't want to apply sRGB -> RGB conversion here so override the format */
281 const mesa_format texFormat =
282 _mesa_get_srgb_format_linear(texImage->TexFormat);
283 const GLenum baseFormat = _mesa_get_format_base_format(texFormat);
284 GLfloat *tempImage, *tempSlice;
285 GLuint slice;
286 int srcStride, dstStride;
287 uint32_t dstFormat;
288 bool needsRebase;
289 uint8_t rebaseSwizzle[4];
290
291 /* Decompress into temp float buffer, then pack into user buffer */
292 tempImage = malloc(width * height * depth * 4 * sizeof(GLfloat));
293 if (!tempImage) {
294 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
295 return;
296 }
297
298 /* Decompress the texture image slices - results in 'tempImage' */
299 for (slice = 0; slice < depth; slice++) {
300 GLubyte *srcMap;
301 GLint srcRowStride;
302
303 tempSlice = tempImage + slice * 4 * width * height;
304
305 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
306 xoffset, yoffset, width, height,
307 GL_MAP_READ_BIT,
308 &srcMap, &srcRowStride);
309 if (srcMap) {
310 _mesa_decompress_image(texFormat, width, height,
311 srcMap, srcRowStride, tempSlice);
312
313 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
314 }
315 else {
316 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
317 free(tempImage);
318 return;
319 }
320 }
321
322 /* Depending on the base format involved we may need to apply a rebase
323 * transform (for example: if we download to a Luminance format we want
324 * G=0 and B=0).
325 */
326 if (baseFormat == GL_LUMINANCE ||
327 baseFormat == GL_INTENSITY) {
328 needsRebase = true;
329 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
330 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
331 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
332 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
333 } else if (baseFormat == GL_LUMINANCE_ALPHA) {
334 needsRebase = true;
335 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
336 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
337 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
338 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
339 } else {
340 needsRebase = false;
341 }
342
343 srcStride = 4 * width * sizeof(GLfloat);
344 dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
345 dstFormat = _mesa_format_from_format_and_type(format, type);
346 tempSlice = tempImage;
347 for (slice = 0; slice < depth; slice++) {
348 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
349 width, height, format, type,
350 slice, 0, 0);
351 _mesa_format_convert(dest, dstFormat, dstStride,
352 tempSlice, RGBA32_FLOAT, srcStride,
353 width, height,
354 needsRebase ? rebaseSwizzle : NULL);
355
356 /* Handle byte swapping if required */
357 if (ctx->Pack.SwapBytes) {
358 _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
359 width, height, dest, dest);
360 }
361
362 tempSlice += 4 * width * height;
363 }
364
365 free(tempImage);
366 }
367
368
369 /**
370 * Return a base GL format given the user-requested format
371 * for glGetTexImage().
372 */
373 GLenum
374 _mesa_base_pack_format(GLenum format)
375 {
376 switch (format) {
377 case GL_ABGR_EXT:
378 case GL_BGRA:
379 case GL_BGRA_INTEGER:
380 case GL_RGBA_INTEGER:
381 return GL_RGBA;
382 case GL_BGR:
383 case GL_BGR_INTEGER:
384 case GL_RGB_INTEGER:
385 return GL_RGB;
386 case GL_RED_INTEGER:
387 return GL_RED;
388 case GL_GREEN_INTEGER:
389 return GL_GREEN;
390 case GL_BLUE_INTEGER:
391 return GL_BLUE;
392 case GL_ALPHA_INTEGER:
393 return GL_ALPHA;
394 case GL_LUMINANCE_INTEGER_EXT:
395 return GL_LUMINANCE;
396 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
397 return GL_LUMINANCE_ALPHA;
398 default:
399 return format;
400 }
401 }
402
403
404 /**
405 * Get an uncompressed color texture image.
406 */
407 static void
408 get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
409 GLint xoffset, GLint yoffset, GLint zoffset,
410 GLsizei width, GLsizei height, GLint depth,
411 GLenum format, GLenum type, GLvoid *pixels,
412 struct gl_texture_image *texImage,
413 GLbitfield transferOps)
414 {
415 /* don't want to apply sRGB -> RGB conversion here so override the format */
416 const mesa_format texFormat =
417 _mesa_get_srgb_format_linear(texImage->TexFormat);
418 GLuint img;
419 GLboolean dst_is_integer;
420 uint32_t dst_format;
421 int dst_stride;
422 uint8_t rebaseSwizzle[4];
423 bool needsRebase;
424 void *rgba = NULL;
425
426 /* Depending on the base format involved we may need to apply a rebase
427 * transform (for example: if we download to a Luminance format we want
428 * G=0 and B=0).
429 */
430 if (texImage->_BaseFormat == GL_LUMINANCE ||
431 texImage->_BaseFormat == GL_INTENSITY) {
432 needsRebase = true;
433 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
434 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
435 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
436 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
437 } else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
438 needsRebase = true;
439 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
440 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
441 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
442 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
443 } else if (texImage->_BaseFormat !=
444 _mesa_get_format_base_format(texFormat)) {
445 needsRebase =
446 _mesa_compute_rgba2base2rgba_component_mapping(texImage->_BaseFormat,
447 rebaseSwizzle);
448 } else {
449 needsRebase = false;
450 }
451
452 /* Describe the dst format */
453 dst_is_integer = _mesa_is_enum_format_integer(format);
454 dst_format = _mesa_format_from_format_and_type(format, type);
455 dst_stride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
456
457 /* Since _mesa_format_convert does not handle transferOps we need to handle
458 * them before we call the function. This requires to convert to RGBA float
459 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
460 * integer then transferOps do not apply.
461 */
462 assert(!transferOps || (transferOps && !dst_is_integer));
463 (void) dst_is_integer; /* silence unused var warning */
464
465 for (img = 0; img < depth; img++) {
466 GLubyte *srcMap;
467 GLint rowstride;
468 GLubyte *img_src;
469 void *dest;
470 void *src;
471 int src_stride;
472 uint32_t src_format;
473
474 /* map src texture buffer */
475 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
476 xoffset, yoffset, width, height,
477 GL_MAP_READ_BIT,
478 &srcMap, &rowstride);
479 if (!srcMap) {
480 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
481 goto done;
482 }
483
484 img_src = srcMap;
485 dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
486 width, height, format, type,
487 img, 0, 0);
488
489 if (transferOps) {
490 uint32_t rgba_format;
491 int rgba_stride;
492 bool need_convert = false;
493
494 /* We will convert to RGBA float */
495 rgba_format = RGBA32_FLOAT;
496 rgba_stride = width * 4 * sizeof(GLfloat);
497
498 /* If we are lucky and the dst format matches the RGBA format we need
499 * to convert to, then we can convert directly into the dst buffer
500 * and avoid the final conversion/copy from the rgba buffer to the dst
501 * buffer.
502 */
503 if (format == rgba_format) {
504 rgba = dest;
505 } else if (rgba == NULL) { /* Allocate the RGBA buffer only once */
506 need_convert = true;
507 rgba = malloc(height * rgba_stride);
508 if (!rgba) {
509 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
510 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
511 return;
512 }
513 }
514
515 _mesa_format_convert(rgba, rgba_format, rgba_stride,
516 img_src, texFormat, rowstride,
517 width, height,
518 needsRebase ? rebaseSwizzle : NULL);
519
520 /* Handle transfer ops now */
521 _mesa_apply_rgba_transfer_ops(ctx, transferOps, width * height, rgba);
522
523 /* If we had to rebase, we have already handled that */
524 needsRebase = false;
525
526 /* If we were lucky and our RGBA conversion matches the dst format,
527 * then we are done.
528 */
529 if (!need_convert)
530 goto do_swap;
531
532 /* Otherwise, we need to convert from RGBA to dst next */
533 src = rgba;
534 src_format = rgba_format;
535 src_stride = rgba_stride;
536 } else {
537 /* No RGBA conversion needed, convert directly to dst */
538 src = img_src;
539 src_format = texFormat;
540 src_stride = rowstride;
541 }
542
543 /* Do the conversion to destination format */
544 _mesa_format_convert(dest, dst_format, dst_stride,
545 src, src_format, src_stride,
546 width, height,
547 needsRebase ? rebaseSwizzle : NULL);
548
549 do_swap:
550 /* Handle byte swapping if required */
551 if (ctx->Pack.SwapBytes)
552 _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
553 width, height, dest, dest);
554
555 /* Unmap the src texture buffer */
556 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
557 }
558
559 done:
560 if (rgba)
561 free(rgba);
562 }
563
564
565 /**
566 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
567 * Compressed textures are handled here as well.
568 */
569 static void
570 get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
571 GLint xoffset, GLint yoffset, GLint zoffset,
572 GLsizei width, GLsizei height, GLint depth,
573 GLenum format, GLenum type, GLvoid *pixels,
574 struct gl_texture_image *texImage)
575 {
576 const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
577 GLbitfield transferOps = 0x0;
578
579 /* In general, clamping does not apply to glGetTexImage, except when
580 * the returned type of the image can't hold negative values.
581 */
582 if (type_needs_clamping(type)) {
583 /* the returned image type can't have negative values */
584 if (dataType == GL_FLOAT ||
585 dataType == GL_HALF_FLOAT ||
586 dataType == GL_SIGNED_NORMALIZED ||
587 format == GL_LUMINANCE ||
588 format == GL_LUMINANCE_ALPHA) {
589 transferOps |= IMAGE_CLAMP_BIT;
590 }
591 }
592
593 if (_mesa_is_format_compressed(texImage->TexFormat)) {
594 get_tex_rgba_compressed(ctx, dimensions,
595 xoffset, yoffset, zoffset,
596 width, height, depth,
597 format, type,
598 pixels, texImage, transferOps);
599 }
600 else {
601 get_tex_rgba_uncompressed(ctx, dimensions,
602 xoffset, yoffset, zoffset,
603 width, height, depth,
604 format, type,
605 pixels, texImage, transferOps);
606 }
607 }
608
609
610 /**
611 * Try to do glGetTexImage() with simple memcpy().
612 * \return GL_TRUE if done, GL_FALSE otherwise
613 */
614 static GLboolean
615 get_tex_memcpy(struct gl_context *ctx,
616 GLint xoffset, GLint yoffset, GLint zoffset,
617 GLsizei width, GLsizei height, GLint depth,
618 GLenum format, GLenum type, GLvoid *pixels,
619 struct gl_texture_image *texImage)
620 {
621 const GLenum target = texImage->TexObject->Target;
622 GLboolean memCopy = GL_FALSE;
623 GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
624
625 /*
626 * Check if we can use memcpy to copy from the hardware texture
627 * format to the user's format/type.
628 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
629 */
630 if ((target == GL_TEXTURE_1D ||
631 target == GL_TEXTURE_2D ||
632 target == GL_TEXTURE_RECTANGLE ||
633 _mesa_is_cube_face(target)) &&
634 texBaseFormat == texImage->_BaseFormat) {
635 memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
636 format, type,
637 ctx->Pack.SwapBytes, NULL);
638 }
639
640 if (depth > 1) {
641 /* only a single slice is supported at this time */
642 memCopy = FALSE;
643 }
644
645 if (memCopy) {
646 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
647 const GLint bytesPerRow = width * bpp;
648 GLubyte *dst =
649 _mesa_image_address2d(&ctx->Pack, pixels, width, height,
650 format, type, 0, 0);
651 const GLint dstRowStride =
652 _mesa_image_row_stride(&ctx->Pack, width, format, type);
653 GLubyte *src;
654 GLint srcRowStride;
655
656 /* map src texture buffer */
657 ctx->Driver.MapTextureImage(ctx, texImage, zoffset,
658 xoffset, yoffset, width, height,
659 GL_MAP_READ_BIT, &src, &srcRowStride);
660
661 if (src) {
662 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
663 memcpy(dst, src, bytesPerRow * texImage->Height);
664 }
665 else {
666 GLuint row;
667 for (row = 0; row < height; row++) {
668 memcpy(dst, src, bytesPerRow);
669 dst += dstRowStride;
670 src += srcRowStride;
671 }
672 }
673
674 /* unmap src texture buffer */
675 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset);
676 }
677 else {
678 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
679 }
680 }
681
682 return memCopy;
683 }
684
685
686 /**
687 * This is the software fallback for Driver.GetTexSubImage().
688 * All error checking will have been done before this routine is called.
689 * We'll call ctx->Driver.MapTextureImage() to access the data, then
690 * unmap with ctx->Driver.UnmapTextureImage().
691 */
692 void
693 _mesa_GetTexSubImage_sw(struct gl_context *ctx,
694 GLint xoffset, GLint yoffset, GLint zoffset,
695 GLsizei width, GLsizei height, GLint depth,
696 GLenum format, GLenum type, GLvoid *pixels,
697 struct gl_texture_image *texImage)
698 {
699 const GLuint dimensions =
700 _mesa_get_texture_dimensions(texImage->TexObject->Target);
701
702 /* map dest buffer, if PBO */
703 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
704 /* Packing texture image into a PBO.
705 * Map the (potentially) VRAM-based buffer into our process space so
706 * we can write into it with the code below.
707 * A hardware driver might use a sophisticated blit to move the
708 * texture data to the PBO if the PBO is in VRAM along with the texture.
709 */
710 GLubyte *buf = (GLubyte *)
711 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
712 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
713 MAP_INTERNAL);
714 if (!buf) {
715 /* out of memory or other unexpected error */
716 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
717 return;
718 }
719 /* <pixels> was an offset into the PBO.
720 * Now make it a real, client-side pointer inside the mapped region.
721 */
722 pixels = ADD_POINTERS(buf, pixels);
723 }
724
725 /* for all array textures, the Z axis selects the layer */
726 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
727 depth = height;
728 height = 1;
729 zoffset = yoffset;
730 yoffset = 0;
731 assert(zoffset + depth <= texImage->Height);
732 } else {
733 assert(zoffset + depth <= texImage->Depth);
734 }
735
736 if (get_tex_memcpy(ctx, xoffset, yoffset, zoffset, width, height, depth,
737 format, type, pixels, texImage)) {
738 /* all done */
739 }
740 else if (format == GL_DEPTH_COMPONENT) {
741 get_tex_depth(ctx, dimensions, xoffset, yoffset, zoffset,
742 width, height, depth, format, type, pixels, texImage);
743 }
744 else if (format == GL_DEPTH_STENCIL_EXT) {
745 get_tex_depth_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
746 width, height, depth, format, type, pixels,
747 texImage);
748 }
749 else if (format == GL_STENCIL_INDEX) {
750 get_tex_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
751 width, height, depth, format, type, pixels, texImage);
752 }
753 else if (format == GL_YCBCR_MESA) {
754 get_tex_ycbcr(ctx, dimensions, xoffset, yoffset, zoffset,
755 width, height, depth, format, type, pixels, texImage);
756 }
757 else {
758 get_tex_rgba(ctx, dimensions, xoffset, yoffset, zoffset,
759 width, height, depth, format, type, pixels, texImage);
760 }
761
762 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
763 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
764 }
765 }
766
767
768
769 /**
770 * This is the software fallback for Driver.GetCompressedTexSubImage().
771 * All error checking will have been done before this routine is called.
772 */
773 void
774 _mesa_GetCompressedTexSubImage_sw(struct gl_context *ctx,
775 struct gl_texture_image *texImage,
776 GLint xoffset, GLint yoffset,
777 GLint zoffset, GLsizei width,
778 GLint height, GLint depth,
779 GLvoid *img)
780 {
781 const GLuint dimensions =
782 _mesa_get_texture_dimensions(texImage->TexObject->Target);
783 struct compressed_pixelstore store;
784 GLint slice;
785 GLubyte *dest;
786
787 _mesa_compute_compressed_pixelstore(dimensions, texImage->TexFormat,
788 width, height, depth,
789 &ctx->Pack, &store);
790
791 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
792 /* pack texture image into a PBO */
793 dest = (GLubyte *)
794 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
795 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
796 MAP_INTERNAL);
797 if (!dest) {
798 /* out of memory or other unexpected error */
799 _mesa_error(ctx, GL_OUT_OF_MEMORY,
800 "glGetCompresssedTexImage(map PBO failed)");
801 return;
802 }
803 dest = ADD_POINTERS(dest, img);
804 } else {
805 dest = img;
806 }
807
808 dest += store.SkipBytes;
809
810 for (slice = 0; slice < store.CopySlices; slice++) {
811 GLint srcRowStride;
812 GLubyte *src;
813
814 /* map src texture buffer */
815 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
816 xoffset, yoffset, width, height,
817 GL_MAP_READ_BIT, &src, &srcRowStride);
818
819 if (src) {
820 GLint i;
821 for (i = 0; i < store.CopyRowsPerSlice; i++) {
822 memcpy(dest, src, store.CopyBytesPerRow);
823 dest += store.TotalBytesPerRow;
824 src += srcRowStride;
825 }
826
827 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
828
829 /* Advance to next slice */
830 dest += store.TotalBytesPerRow * (store.TotalRowsPerSlice -
831 store.CopyRowsPerSlice);
832
833 } else {
834 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
835 }
836 }
837
838 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
839 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
840 }
841 }
842
843
844 /**
845 * Validate the texture target enum supplied to glGetTex(ture)Image or
846 * glGetCompressedTex(ture)Image.
847 */
848 static GLboolean
849 legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
850 {
851 switch (target) {
852 case GL_TEXTURE_1D:
853 case GL_TEXTURE_2D:
854 case GL_TEXTURE_3D:
855 return GL_TRUE;
856 case GL_TEXTURE_RECTANGLE_NV:
857 return ctx->Extensions.NV_texture_rectangle;
858 case GL_TEXTURE_1D_ARRAY_EXT:
859 case GL_TEXTURE_2D_ARRAY_EXT:
860 return ctx->Extensions.EXT_texture_array;
861 case GL_TEXTURE_CUBE_MAP_ARRAY:
862 return ctx->Extensions.ARB_texture_cube_map_array;
863
864 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
865 * (30.10.2014) says:
866 * "An INVALID_ENUM error is generated if the effective target is not
867 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
868 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
869 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
870 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
871 */
872 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
873 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
874 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
875 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
876 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
877 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
878 return dsa ? GL_FALSE : ctx->Extensions.ARB_texture_cube_map;
879 case GL_TEXTURE_CUBE_MAP:
880 return dsa ? GL_TRUE : GL_FALSE;
881 default:
882 return GL_FALSE;
883 }
884 }
885
886
887 /**
888 * Wrapper for _mesa_select_tex_image() which can handle target being
889 * GL_TEXTURE_CUBE_MAP in which case we use zoffset to select a cube face.
890 * This can happen for glGetTextureImage and glGetTextureSubImage (DSA
891 * functions).
892 */
893 static struct gl_texture_image *
894 select_tex_image(const struct gl_texture_object *texObj, GLenum target,
895 GLint level, GLint zoffset)
896 {
897 assert(level >= 0);
898 assert(level < MAX_TEXTURE_LEVELS);
899 if (target == GL_TEXTURE_CUBE_MAP) {
900 assert(zoffset >= 0);
901 assert(zoffset < 6);
902 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset;
903 }
904 return _mesa_select_tex_image(texObj, target, level);
905 }
906
907
908 /**
909 * Error-check the offset and size arguments to
910 * glGet[Compressed]TextureSubImage(). Also checks if the specified
911 * texture image is missing.
912 * \return true if error, false if no error.
913 */
914 static bool
915 dimensions_error_check(struct gl_context *ctx,
916 struct gl_texture_object *texObj,
917 GLenum target, GLint level,
918 GLint xoffset, GLint yoffset, GLint zoffset,
919 GLsizei width, GLsizei height, GLsizei depth,
920 const char *caller)
921 {
922 const struct gl_texture_image *texImage;
923 int i;
924
925 if (xoffset < 0) {
926 _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset = %d)", caller, xoffset);
927 return true;
928 }
929
930 if (yoffset < 0) {
931 _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset = %d)", caller, yoffset);
932 return true;
933 }
934
935 if (zoffset < 0) {
936 _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset = %d)", caller, zoffset);
937 return true;
938 }
939
940 if (width < 0) {
941 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width = %d)", caller, width);
942 return true;
943 }
944
945 if (height < 0) {
946 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height = %d)", caller, height);
947 return true;
948 }
949
950 if (depth < 0) {
951 _mesa_error(ctx, GL_INVALID_VALUE, "%s(depth = %d)", caller, depth);
952 return true;
953 }
954
955 /* do special per-target checks */
956 switch (target) {
957 case GL_TEXTURE_1D:
958 if (yoffset != 0) {
959 _mesa_error(ctx, GL_INVALID_VALUE,
960 "%s(1D, yoffset = %d)", caller, yoffset);
961 return true;
962 }
963 if (height > 1) {
964 _mesa_error(ctx, GL_INVALID_VALUE,
965 "%s(1D, height = %d)", caller, height);
966 return true;
967 }
968 /* fall-through */
969 case GL_TEXTURE_1D_ARRAY:
970 case GL_TEXTURE_2D:
971 case GL_TEXTURE_RECTANGLE:
972 if (zoffset != 0) {
973 _mesa_error(ctx, GL_INVALID_VALUE,
974 "%s(zoffset = %d)", caller, zoffset);
975 return true;
976 }
977 if (depth > 1) {
978 _mesa_error(ctx, GL_INVALID_VALUE,
979 "%s(depth = %d)", caller, depth);
980 return true;
981 }
982 break;
983 case GL_TEXTURE_CUBE_MAP:
984 /* Non-array cube maps are special because we have a gl_texture_image
985 * per face.
986 */
987 if (zoffset + depth > 6) {
988 _mesa_error(ctx, GL_INVALID_VALUE,
989 "%s(zoffset + depth = %d)", caller, zoffset + depth);
990 return true;
991 }
992 /* check that the range of faces exist */
993 for (i = 0; i < depth; i++) {
994 GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset + i;
995 if (!_mesa_select_tex_image(texObj, face, level)) {
996 /* non-existant face */
997 _mesa_error(ctx, GL_INVALID_OPERATION,
998 "%s(missing cube face)", caller);
999 return true;
1000 }
1001 }
1002 break;
1003 default:
1004 ; /* nothing */
1005 }
1006
1007 texImage = select_tex_image(texObj, target, level, zoffset);
1008 if (!texImage) {
1009 /* missing texture image */
1010 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(missing image)", caller);
1011 return true;
1012 }
1013
1014 if (xoffset + width > texImage->Width) {
1015 _mesa_error(ctx, GL_INVALID_VALUE,
1016 "%s(xoffset %d + width %d > %u)",
1017 caller, xoffset, width, texImage->Width);
1018 return true;
1019 }
1020
1021 if (yoffset + height > texImage->Height) {
1022 _mesa_error(ctx, GL_INVALID_VALUE,
1023 "%s(yoffset %d + height %d > %u)",
1024 caller, yoffset, height, texImage->Height);
1025 return true;
1026 }
1027
1028 if (target != GL_TEXTURE_CUBE_MAP) {
1029 /* Cube map error checking was done above */
1030 if (zoffset + depth > texImage->Depth) {
1031 _mesa_error(ctx, GL_INVALID_VALUE,
1032 "%s(zoffset %d + depth %d > %u)",
1033 caller, zoffset, depth, texImage->Depth);
1034 return true;
1035 }
1036 }
1037
1038 /* Extra checks for compressed textures */
1039 {
1040 GLuint bw, bh;
1041 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
1042 if (bw > 1 || bh > 1) {
1043 /* offset must be multiple of block size */
1044 if (xoffset % bw != 0) {
1045 _mesa_error(ctx, GL_INVALID_VALUE,
1046 "%s(xoffset = %d)", caller, xoffset);
1047 return true;
1048 }
1049 if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY) {
1050 if (yoffset % bh != 0) {
1051 _mesa_error(ctx, GL_INVALID_VALUE,
1052 "%s(yoffset = %d)", caller, yoffset);
1053 return true;
1054 }
1055 }
1056
1057 /* The size must be a multiple of bw x bh, or we must be using a
1058 * offset+size that exactly hits the edge of the image.
1059 */
1060 if ((width % bw != 0) &&
1061 (xoffset + width != (GLint) texImage->Width)) {
1062 _mesa_error(ctx, GL_INVALID_VALUE,
1063 "%s(width = %d)", caller, width);
1064 return true;
1065 }
1066
1067 if ((height % bh != 0) &&
1068 (yoffset + height != (GLint) texImage->Height)) {
1069 _mesa_error(ctx, GL_INVALID_VALUE,
1070 "%s(height = %d)", caller, height);
1071 return true;
1072 }
1073 }
1074 }
1075
1076 if (width == 0 || height == 0 || depth == 0) {
1077 /* Not an error, but nothing to do. Return 'true' so that the
1078 * caller simply returns.
1079 */
1080 return true;
1081 }
1082
1083 return false;
1084 }
1085
1086
1087 /**
1088 * Do PBO-related error checking for getting uncompressed images.
1089 * \return true if there was an error (or the GetTexImage is to be a no-op)
1090 */
1091 static bool
1092 pbo_error_check(struct gl_context *ctx, GLenum target,
1093 GLsizei width, GLsizei height, GLsizei depth,
1094 GLenum format, GLenum type, GLsizei clientMemSize,
1095 GLvoid *pixels,
1096 const char *caller)
1097 {
1098 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
1099
1100 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, width, height, depth,
1101 format, type, clientMemSize, pixels)) {
1102 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1103 _mesa_error(ctx, GL_INVALID_OPERATION,
1104 "%s(out of bounds PBO access)", caller);
1105 } else {
1106 _mesa_error(ctx, GL_INVALID_OPERATION,
1107 "%s(out of bounds access: bufSize (%d) is too small)",
1108 caller, clientMemSize);
1109 }
1110 return true;
1111 }
1112
1113 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1114 /* PBO should not be mapped */
1115 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1116 _mesa_error(ctx, GL_INVALID_OPERATION,
1117 "%s(PBO is mapped)", caller);
1118 return true;
1119 }
1120 }
1121
1122 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1123 /* not an error, do nothing */
1124 return true;
1125 }
1126
1127 return false;
1128 }
1129
1130
1131 /**
1132 * Do error checking for all (non-compressed) get-texture-image functions.
1133 * \return true if any error, false if no errors.
1134 */
1135 static bool
1136 getteximage_error_check(struct gl_context *ctx,
1137 struct gl_texture_object *texObj,
1138 GLenum target, GLint level,
1139 GLint xoffset, GLint yoffset, GLint zoffset,
1140 GLsizei width, GLsizei height, GLsizei depth,
1141 GLenum format, GLenum type, GLsizei bufSize,
1142 GLvoid *pixels, const char *caller)
1143 {
1144 struct gl_texture_image *texImage;
1145 GLenum baseFormat, err;
1146 GLint maxLevels;
1147
1148 assert(texObj);
1149
1150 if (texObj->Target == 0) {
1151 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
1152 return true;
1153 }
1154
1155 maxLevels = _mesa_max_texture_levels(ctx, target);
1156 if (level < 0 || level >= maxLevels) {
1157 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level = %d)", caller, level);
1158 return true;
1159 }
1160
1161 err = _mesa_error_check_format_and_type(ctx, format, type);
1162 if (err != GL_NO_ERROR) {
1163 _mesa_error(ctx, err, "%s(format/type)", caller);
1164 return true;
1165 }
1166
1167 if (dimensions_error_check(ctx, texObj, target, level,
1168 xoffset, yoffset, zoffset,
1169 width, height, depth, caller)) {
1170 return true;
1171 }
1172
1173 if (pbo_error_check(ctx, target, width, height, depth,
1174 format, type, bufSize, pixels, caller)) {
1175 return true;
1176 }
1177
1178 texImage = select_tex_image(texObj, target, level, zoffset);
1179 assert(texImage);
1180
1181 /*
1182 * Format and type checking has been moved up to GetnTexImage and
1183 * GetTextureImage so that it happens before getting the texImage object.
1184 */
1185
1186 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
1187
1188 /* Make sure the requested image format is compatible with the
1189 * texture's format.
1190 */
1191 if (_mesa_is_color_format(format)
1192 && !_mesa_is_color_format(baseFormat)) {
1193 _mesa_error(ctx, GL_INVALID_OPERATION,
1194 "%s(format mismatch)", caller);
1195 return true;
1196 }
1197 else if (_mesa_is_depth_format(format)
1198 && !_mesa_is_depth_format(baseFormat)
1199 && !_mesa_is_depthstencil_format(baseFormat)) {
1200 _mesa_error(ctx, GL_INVALID_OPERATION,
1201 "%s(format mismatch)", caller);
1202 return true;
1203 }
1204 else if (_mesa_is_stencil_format(format)
1205 && !ctx->Extensions.ARB_texture_stencil8) {
1206 _mesa_error(ctx, GL_INVALID_ENUM,
1207 "%s(format=GL_STENCIL_INDEX)", caller);
1208 return true;
1209 }
1210 else if (_mesa_is_stencil_format(format)
1211 && !_mesa_is_depthstencil_format(baseFormat)
1212 && !_mesa_is_stencil_format(baseFormat)) {
1213 _mesa_error(ctx, GL_INVALID_OPERATION,
1214 "%s(format mismatch)", caller);
1215 return true;
1216 }
1217 else if (_mesa_is_ycbcr_format(format)
1218 && !_mesa_is_ycbcr_format(baseFormat)) {
1219 _mesa_error(ctx, GL_INVALID_OPERATION,
1220 "%s(format mismatch)", caller);
1221 return true;
1222 }
1223 else if (_mesa_is_depthstencil_format(format)
1224 && !_mesa_is_depthstencil_format(baseFormat)) {
1225 _mesa_error(ctx, GL_INVALID_OPERATION,
1226 "%s(format mismatch)", caller);
1227 return true;
1228 }
1229 else if (!_mesa_is_stencil_format(format) &&
1230 _mesa_is_enum_format_integer(format) !=
1231 _mesa_is_format_integer(texImage->TexFormat)) {
1232 _mesa_error(ctx, GL_INVALID_OPERATION,
1233 "%s(format mismatch)", caller);
1234 return true;
1235 }
1236
1237 return false;
1238 }
1239
1240
1241 /**
1242 * Return the width, height and depth of a texture image.
1243 * This function must be resilient to bad parameter values since
1244 * this is called before full error checking.
1245 */
1246 static void
1247 get_texture_image_dims(const struct gl_texture_object *texObj,
1248 GLenum target, GLint level,
1249 GLsizei *width, GLsizei *height, GLsizei *depth)
1250 {
1251 const struct gl_texture_image *texImage = NULL;
1252
1253 if (level >= 0 && level < MAX_TEXTURE_LEVELS) {
1254 texImage = _mesa_select_tex_image(texObj, target, level);
1255 }
1256
1257 if (texImage) {
1258 *width = texImage->Width;
1259 *height = texImage->Height;
1260 if (target == GL_TEXTURE_CUBE_MAP) {
1261 *depth = 6;
1262 }
1263 else {
1264 *depth = texImage->Depth;
1265 }
1266 }
1267 else {
1268 *width = *height = *depth = 0;
1269 }
1270 }
1271
1272
1273 /**
1274 * Common code for all (uncompressed) get-texture-image functions.
1275 * \param texObj the texture object (should not be null)
1276 * \param target user-provided target, or 0 for DSA
1277 * \param level image level.
1278 * \param format pixel data format for returned image.
1279 * \param type pixel data type for returned image.
1280 * \param bufSize size of the pixels data buffer.
1281 * \param pixels returned pixel data.
1282 * \param caller name of calling function
1283 */
1284 static void
1285 get_texture_image(struct gl_context *ctx,
1286 struct gl_texture_object *texObj,
1287 GLenum target, GLint level,
1288 GLint xoffset, GLint yoffset, GLint zoffset,
1289 GLsizei width, GLsizei height, GLint depth,
1290 GLenum format, GLenum type,
1291 GLvoid *pixels, const char *caller)
1292 {
1293 struct gl_texture_image *texImage;
1294 unsigned firstFace, numFaces, i;
1295 GLint imageStride;
1296
1297 FLUSH_VERTICES(ctx, 0);
1298
1299 texImage = select_tex_image(texObj, target, level, zoffset);
1300 assert(texImage); /* should have been error checked already */
1301
1302 if (_mesa_is_zero_size_texture(texImage)) {
1303 /* no image data to return */
1304 return;
1305 }
1306
1307 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1308 _mesa_debug(ctx, "%s(tex %u) format = %s, w=%d, h=%d,"
1309 " dstFmt=0x%x, dstType=0x%x\n",
1310 caller, texObj->Name,
1311 _mesa_get_format_name(texImage->TexFormat),
1312 texImage->Width, texImage->Height,
1313 format, type);
1314 }
1315
1316 if (target == GL_TEXTURE_CUBE_MAP) {
1317 /* Compute stride between cube faces */
1318 imageStride = _mesa_image_image_stride(&ctx->Pack, width, height,
1319 format, type);
1320 firstFace = zoffset;
1321 numFaces = depth;
1322 zoffset = 0;
1323 depth = 1;
1324 }
1325 else {
1326 imageStride = 0;
1327 firstFace = _mesa_tex_target_to_face(target);
1328 numFaces = 1;
1329 }
1330
1331 _mesa_lock_texture(ctx, texObj);
1332
1333 for (i = 0; i < numFaces; i++) {
1334 texImage = texObj->Image[firstFace + i][level];
1335 assert(texImage);
1336
1337 ctx->Driver.GetTexSubImage(ctx, xoffset, yoffset, zoffset,
1338 width, height, depth,
1339 format, type, pixels, texImage);
1340
1341 /* next cube face */
1342 pixels = (GLubyte *) pixels + imageStride;
1343 }
1344
1345 _mesa_unlock_texture(ctx, texObj);
1346 }
1347
1348
1349 void GLAPIENTRY
1350 _mesa_GetnTexImageARB(GLenum target, GLint level, GLenum format, GLenum type,
1351 GLsizei bufSize, GLvoid *pixels)
1352 {
1353 GET_CURRENT_CONTEXT(ctx);
1354 static const char *caller = "glGetnTexImageARB";
1355 GLsizei width, height, depth;
1356 struct gl_texture_object *texObj;
1357
1358 if (!legal_getteximage_target(ctx, target, false)) {
1359 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1360 return;
1361 }
1362
1363 texObj = _mesa_get_current_tex_object(ctx, target);
1364 assert(texObj);
1365
1366 get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1367
1368 if (getteximage_error_check(ctx, texObj, target, level,
1369 0, 0, 0, width, height, depth,
1370 format, type, bufSize, pixels, caller)) {
1371 return;
1372 }
1373
1374 get_texture_image(ctx, texObj, target, level,
1375 0, 0, 0, width, height, depth,
1376 format, type, pixels, caller);
1377 }
1378
1379
1380 void GLAPIENTRY
1381 _mesa_GetTexImage(GLenum target, GLint level, GLenum format, GLenum type,
1382 GLvoid *pixels )
1383 {
1384 GET_CURRENT_CONTEXT(ctx);
1385 static const char *caller = "glGetTexImage";
1386 GLsizei width, height, depth;
1387 struct gl_texture_object *texObj;
1388
1389 if (!legal_getteximage_target(ctx, target, false)) {
1390 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1391 return;
1392 }
1393
1394 texObj = _mesa_get_current_tex_object(ctx, target);
1395 assert(texObj);
1396
1397 get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1398
1399 if (getteximage_error_check(ctx, texObj, target, level,
1400 0, 0, 0, width, height, depth,
1401 format, type, INT_MAX, pixels, caller)) {
1402 return;
1403 }
1404
1405 get_texture_image(ctx, texObj, target, level,
1406 0, 0, 0, width, height, depth,
1407 format, type, pixels, caller);
1408 }
1409
1410
1411 void GLAPIENTRY
1412 _mesa_GetTextureImage(GLuint texture, GLint level, GLenum format, GLenum type,
1413 GLsizei bufSize, GLvoid *pixels)
1414 {
1415 GET_CURRENT_CONTEXT(ctx);
1416 GLsizei width, height, depth;
1417 static const char *caller = "glGetTextureImage";
1418 struct gl_texture_object *texObj =
1419 _mesa_lookup_texture_err(ctx, texture, caller);
1420
1421 if (!texObj) {
1422 return;
1423 }
1424
1425 get_texture_image_dims(texObj, texObj->Target, level,
1426 &width, &height, &depth);
1427
1428 if (getteximage_error_check(ctx, texObj, texObj->Target, level,
1429 0, 0, 0, width, height, depth,
1430 format, type, bufSize, pixels, caller)) {
1431 return;
1432 }
1433
1434 get_texture_image(ctx, texObj, texObj->Target, level,
1435 0, 0, 0, width, height, depth,
1436 format, type, pixels, caller);
1437 }
1438
1439
1440 void GLAPIENTRY
1441 _mesa_GetTextureSubImage(GLuint texture, GLint level,
1442 GLint xoffset, GLint yoffset, GLint zoffset,
1443 GLsizei width, GLsizei height, GLsizei depth,
1444 GLenum format, GLenum type, GLsizei bufSize,
1445 void *pixels)
1446 {
1447 GET_CURRENT_CONTEXT(ctx);
1448 static const char *caller = "glGetTextureSubImage";
1449 struct gl_texture_object *texObj =
1450 _mesa_lookup_texture_err(ctx, texture, caller);
1451
1452 if (!texObj) {
1453 return;
1454 }
1455
1456 if (getteximage_error_check(ctx, texObj, texObj->Target, level,
1457 xoffset, yoffset, zoffset, width, height, depth,
1458 format, type, bufSize, pixels, caller)) {
1459 return;
1460 }
1461
1462 get_texture_image(ctx, texObj, texObj->Target, level,
1463 xoffset, yoffset, zoffset, width, height, depth,
1464 format, type, pixels, caller);
1465 }
1466
1467
1468
1469 /**
1470 * Compute the number of bytes which will be written when retrieving
1471 * a sub-region of a compressed texture.
1472 */
1473 static GLsizei
1474 packed_compressed_size(GLuint dimensions, mesa_format format,
1475 GLsizei width, GLsizei height, GLsizei depth,
1476 const struct gl_pixelstore_attrib *packing)
1477 {
1478 struct compressed_pixelstore st;
1479 GLsizei totalBytes;
1480
1481 _mesa_compute_compressed_pixelstore(dimensions, format,
1482 width, height, depth,
1483 packing, &st);
1484 totalBytes =
1485 (st.CopySlices - 1) * st.TotalRowsPerSlice * st.TotalBytesPerRow +
1486 st.SkipBytes +
1487 (st.CopyRowsPerSlice - 1) * st.TotalBytesPerRow +
1488 st.CopyBytesPerRow;
1489
1490 return totalBytes;
1491 }
1492
1493
1494 /**
1495 * Do error checking for getting compressed texture images.
1496 * \return true if any error, false if no errors.
1497 */
1498 static bool
1499 getcompressedteximage_error_check(struct gl_context *ctx,
1500 struct gl_texture_object *texObj,
1501 GLenum target, GLint level,
1502 GLint xoffset, GLint yoffset, GLint zoffset,
1503 GLsizei width, GLsizei height, GLsizei depth,
1504 GLsizei bufSize, GLvoid *pixels,
1505 const char *caller)
1506 {
1507 struct gl_texture_image *texImage;
1508 GLint maxLevels;
1509 GLsizei totalBytes;
1510 GLuint dimensions;
1511
1512 assert(texObj);
1513
1514 if (texObj->Target == 0) {
1515 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
1516 return true;
1517 }
1518
1519 maxLevels = _mesa_max_texture_levels(ctx, target);
1520 if (level < 0 || level >= maxLevels) {
1521 _mesa_error(ctx, GL_INVALID_VALUE,
1522 "%s(bad level = %d)", caller, level);
1523 return true;
1524 }
1525
1526 if (dimensions_error_check(ctx, texObj, target, level,
1527 xoffset, yoffset, zoffset,
1528 width, height, depth, caller)) {
1529 return true;
1530 }
1531
1532 texImage = select_tex_image(texObj, target, level, zoffset);
1533 assert(texImage);
1534
1535 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
1536 _mesa_error(ctx, GL_INVALID_OPERATION,
1537 "%s(texture is not compressed)", caller);
1538 return true;
1539 }
1540
1541 /* Check for invalid pixel storage modes */
1542 dimensions = _mesa_get_texture_dimensions(texObj->Target);
1543 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
1544 &ctx->Pack,
1545 caller)) {
1546 return true;
1547 }
1548
1549 /* Compute number of bytes that may be touched in the dest buffer */
1550 totalBytes = packed_compressed_size(dimensions, texImage->TexFormat,
1551 width, height, depth,
1552 &ctx->Pack);
1553
1554 /* Do dest buffer bounds checking */
1555 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1556 /* do bounds checking on PBO write */
1557 if ((GLubyte *) pixels + totalBytes >
1558 (GLubyte *) ctx->Pack.BufferObj->Size) {
1559 _mesa_error(ctx, GL_INVALID_OPERATION,
1560 "%s(out of bounds PBO access)", caller);
1561 return true;
1562 }
1563
1564 /* make sure PBO is not mapped */
1565 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1566 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", caller);
1567 return true;
1568 }
1569 }
1570 else {
1571 /* do bounds checking on writing to client memory */
1572 if (totalBytes > bufSize) {
1573 _mesa_error(ctx, GL_INVALID_OPERATION,
1574 "%s(out of bounds access: bufSize (%d) is too small)",
1575 caller, bufSize);
1576 return true;
1577 }
1578 }
1579
1580 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1581 /* not an error, but do nothing */
1582 return true;
1583 }
1584
1585 return false;
1586 }
1587
1588
1589 /**
1590 * Common helper for all glGetCompressed-teximage functions.
1591 */
1592 static void
1593 get_compressed_texture_image(struct gl_context *ctx,
1594 struct gl_texture_object *texObj,
1595 GLenum target, GLint level,
1596 GLint xoffset, GLint yoffset, GLint zoffset,
1597 GLsizei width, GLsizei height, GLint depth,
1598 GLvoid *pixels,
1599 const char *caller)
1600 {
1601 struct gl_texture_image *texImage;
1602 unsigned firstFace, numFaces, i, imageStride;
1603
1604 FLUSH_VERTICES(ctx, 0);
1605
1606 texImage = select_tex_image(texObj, target, level, zoffset);
1607 assert(texImage); /* should have been error checked already */
1608
1609 if (_mesa_is_zero_size_texture(texImage))
1610 return;
1611
1612 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1613 _mesa_debug(ctx,
1614 "%s(tex %u) format = %s, w=%d, h=%d\n",
1615 caller, texObj->Name,
1616 _mesa_get_format_name(texImage->TexFormat),
1617 texImage->Width, texImage->Height);
1618 }
1619
1620 if (target == GL_TEXTURE_CUBE_MAP) {
1621 struct compressed_pixelstore store;
1622
1623 /* Compute image stride between cube faces */
1624 _mesa_compute_compressed_pixelstore(2, texImage->TexFormat,
1625 width, height, depth,
1626 &ctx->Pack, &store);
1627 imageStride = store.TotalBytesPerRow * store.TotalRowsPerSlice;
1628
1629 firstFace = zoffset;
1630 numFaces = depth;
1631 zoffset = 0;
1632 depth = 1;
1633 }
1634 else {
1635 imageStride = 0;
1636 firstFace = _mesa_tex_target_to_face(target);
1637 numFaces = 1;
1638 }
1639
1640 _mesa_lock_texture(ctx, texObj);
1641
1642 for (i = 0; i < numFaces; i++) {
1643 texImage = texObj->Image[firstFace + i][level];
1644 assert(texImage);
1645
1646 ctx->Driver.GetCompressedTexSubImage(ctx, texImage,
1647 xoffset, yoffset, zoffset,
1648 width, height, depth, pixels);
1649
1650 /* next cube face */
1651 pixels = (GLubyte *) pixels + imageStride;
1652 }
1653
1654 _mesa_unlock_texture(ctx, texObj);
1655 }
1656
1657
1658 void GLAPIENTRY
1659 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1660 GLvoid *pixels)
1661 {
1662 GET_CURRENT_CONTEXT(ctx);
1663 static const char *caller = "glGetnCompressedTexImageARB";
1664 GLsizei width, height, depth;
1665 struct gl_texture_object *texObj;
1666
1667 if (!legal_getteximage_target(ctx, target, false)) {
1668 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1669 return;
1670 }
1671
1672 texObj = _mesa_get_current_tex_object(ctx, target);
1673 assert(texObj);
1674
1675 get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1676
1677 if (getcompressedteximage_error_check(ctx, texObj, target, level,
1678 0, 0, 0, width, height, depth,
1679 INT_MAX, pixels, caller)) {
1680 return;
1681 }
1682
1683 get_compressed_texture_image(ctx, texObj, target, level,
1684 0, 0, 0, width, height, depth,
1685 pixels, caller);
1686 }
1687
1688
1689 void GLAPIENTRY
1690 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *pixels)
1691 {
1692 GET_CURRENT_CONTEXT(ctx);
1693 static const char *caller = "glGetCompressedTexImage";
1694 GLsizei width, height, depth;
1695 struct gl_texture_object *texObj;
1696
1697 if (!legal_getteximage_target(ctx, target, false)) {
1698 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1699 return;
1700 }
1701
1702 texObj = _mesa_get_current_tex_object(ctx, target);
1703 assert(texObj);
1704
1705 get_texture_image_dims(texObj, target, level,
1706 &width, &height, &depth);
1707
1708 if (getcompressedteximage_error_check(ctx, texObj, target, level,
1709 0, 0, 0, width, height, depth,
1710 INT_MAX, pixels, caller)) {
1711 return;
1712 }
1713
1714 get_compressed_texture_image(ctx, texObj, target, level,
1715 0, 0, 0, width, height, depth,
1716 pixels, caller);
1717 }
1718
1719
1720 void GLAPIENTRY
1721 _mesa_GetCompressedTextureImage(GLuint texture, GLint level,
1722 GLsizei bufSize, GLvoid *pixels)
1723 {
1724 GET_CURRENT_CONTEXT(ctx);
1725 static const char *caller = "glGetCompressedTextureImage";
1726 GLsizei width, height, depth;
1727 struct gl_texture_object *texObj =
1728 _mesa_lookup_texture_err(ctx, texture, caller);
1729
1730 if (!texObj) {
1731 return;
1732 }
1733
1734 get_texture_image_dims(texObj, texObj->Target, level,
1735 &width, &height, &depth);
1736
1737 if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
1738 0, 0, 0, width, height, depth,
1739 bufSize, pixels, caller)) {
1740 return;
1741 }
1742
1743 get_compressed_texture_image(ctx, texObj, texObj->Target, level,
1744 0, 0, 0, width, height, depth,
1745 pixels, caller);
1746 }
1747
1748
1749 void APIENTRY
1750 _mesa_GetCompressedTextureSubImage(GLuint texture, GLint level,
1751 GLint xoffset, GLint yoffset,
1752 GLint zoffset, GLsizei width,
1753 GLsizei height, GLsizei depth,
1754 GLsizei bufSize, void *pixels)
1755 {
1756 GET_CURRENT_CONTEXT(ctx);
1757 static const char *caller = "glGetCompressedTextureImage";
1758 struct gl_texture_object *texObj;
1759
1760 texObj = _mesa_lookup_texture_err(ctx, texture, caller);
1761 if (!texObj) {
1762 return;
1763 }
1764
1765 if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
1766 xoffset, yoffset, zoffset,
1767 width, height, depth,
1768 bufSize, pixels, caller)) {
1769 return;
1770 }
1771
1772 get_compressed_texture_image(ctx, texObj, texObj->Target, level,
1773 xoffset, yoffset, zoffset,
1774 width, height, depth,
1775 pixels, caller);
1776 }