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