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