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