2c54e4a35f167fb9aaff1fb526065019d4eaf6aa
[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 "texstore.h"
48
49
50
51 /**
52 * Can the given type represent negative values?
53 */
54 static inline GLboolean
55 type_needs_clamping(GLenum type)
56 {
57 switch (type) {
58 case GL_BYTE:
59 case GL_SHORT:
60 case GL_INT:
61 case GL_FLOAT:
62 case GL_HALF_FLOAT_ARB:
63 case GL_UNSIGNED_INT_10F_11F_11F_REV:
64 case GL_UNSIGNED_INT_5_9_9_9_REV:
65 return GL_FALSE;
66 default:
67 return GL_TRUE;
68 }
69 }
70
71
72 /**
73 * glGetTexImage for depth/Z pixels.
74 */
75 static void
76 get_tex_depth(struct gl_context *ctx, GLuint dimensions,
77 GLenum format, GLenum type, GLvoid *pixels,
78 struct gl_texture_image *texImage)
79 {
80 const GLint width = texImage->Width;
81 const GLint height = texImage->Height;
82 const GLint depth = texImage->Depth;
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, img,
97 0, 0, width, height, GL_MAP_READ_BIT,
98 &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, 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 GLenum format, GLenum type, GLvoid *pixels,
128 struct gl_texture_image *texImage)
129 {
130 const GLint width = texImage->Width;
131 const GLint height = texImage->Height;
132 const GLint depth = texImage->Depth;
133 GLint img, row;
134
135 assert(format == GL_DEPTH_STENCIL);
136 assert(type == GL_UNSIGNED_INT_24_8 ||
137 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
138
139 for (img = 0; img < depth; img++) {
140 GLubyte *srcMap;
141 GLint rowstride;
142
143 /* map src texture buffer */
144 ctx->Driver.MapTextureImage(ctx, texImage, img,
145 0, 0, width, height, GL_MAP_READ_BIT,
146 &srcMap, &rowstride);
147
148 if (srcMap) {
149 for (row = 0; row < height; row++) {
150 const GLubyte *src = srcMap + row * rowstride;
151 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
152 width, height, format, type,
153 img, row, 0);
154 _mesa_unpack_depth_stencil_row(texImage->TexFormat,
155 width,
156 (const GLuint *) src,
157 type, dest);
158 if (ctx->Pack.SwapBytes) {
159 _mesa_swap4((GLuint *) dest, width);
160 }
161 }
162
163 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
164 }
165 else {
166 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
167 break;
168 }
169 }
170 }
171
172
173 /**
174 * glGetTexImage for YCbCr pixels.
175 */
176 static void
177 get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
178 GLenum format, GLenum type, GLvoid *pixels,
179 struct gl_texture_image *texImage)
180 {
181 const GLint width = texImage->Width;
182 const GLint height = texImage->Height;
183 const GLint depth = texImage->Depth;
184 GLint img, row;
185
186 for (img = 0; img < depth; img++) {
187 GLubyte *srcMap;
188 GLint rowstride;
189
190 /* map src texture buffer */
191 ctx->Driver.MapTextureImage(ctx, texImage, img,
192 0, 0, width, height, 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 memcpy(dest, src, width * sizeof(GLushort));
202
203 /* check for byte swapping */
204 if ((texImage->TexFormat == MESA_FORMAT_YCBCR
205 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
206 (texImage->TexFormat == MESA_FORMAT_YCBCR_REV
207 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
208 if (!ctx->Pack.SwapBytes)
209 _mesa_swap2((GLushort *) dest, width);
210 }
211 else if (ctx->Pack.SwapBytes) {
212 _mesa_swap2((GLushort *) dest, width);
213 }
214 }
215
216 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
217 }
218 else {
219 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
220 break;
221 }
222 }
223 }
224
225
226 /**
227 * Get a color texture image with decompression.
228 */
229 static void
230 get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
231 GLenum format, GLenum type, GLvoid *pixels,
232 struct gl_texture_image *texImage,
233 GLbitfield transferOps)
234 {
235 /* don't want to apply sRGB -> RGB conversion here so override the format */
236 const mesa_format texFormat =
237 _mesa_get_srgb_format_linear(texImage->TexFormat);
238 const GLenum baseFormat = _mesa_get_format_base_format(texFormat);
239 const GLenum destBaseFormat = _mesa_base_tex_format(ctx, format);
240 GLenum rebaseFormat = GL_NONE;
241 const GLuint width = texImage->Width;
242 const GLuint height = texImage->Height;
243 const GLuint depth = texImage->Depth;
244 GLfloat *tempImage, *tempSlice, *srcRow;
245 GLuint row, slice;
246
247 /* Decompress into temp float buffer, then pack into user buffer */
248 tempImage = malloc(width * height * depth
249 * 4 * sizeof(GLfloat));
250 if (!tempImage) {
251 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
252 return;
253 }
254
255 /* Decompress the texture image slices - results in 'tempImage' */
256 for (slice = 0; slice < depth; slice++) {
257 GLubyte *srcMap;
258 GLint srcRowStride;
259
260 tempSlice = tempImage + slice * 4 * width * height;
261
262 ctx->Driver.MapTextureImage(ctx, texImage, slice,
263 0, 0, width, height,
264 GL_MAP_READ_BIT,
265 &srcMap, &srcRowStride);
266 if (srcMap) {
267 _mesa_decompress_image(texFormat, width, height,
268 srcMap, srcRowStride, tempSlice);
269
270 ctx->Driver.UnmapTextureImage(ctx, texImage, slice);
271 }
272 else {
273 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
274 free(tempImage);
275 return;
276 }
277 }
278
279 if (baseFormat == GL_LUMINANCE ||
280 baseFormat == GL_INTENSITY ||
281 baseFormat == GL_LUMINANCE_ALPHA) {
282 /* If a luminance (or intensity) texture is read back as RGB(A), the
283 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
284 * here to get G=B=0.
285 */
286 rebaseFormat = texImage->_BaseFormat;
287 }
288 else if ((baseFormat == GL_RGBA ||
289 baseFormat == GL_RGB ||
290 baseFormat == GL_RG) &&
291 (destBaseFormat == GL_LUMINANCE ||
292 destBaseFormat == GL_LUMINANCE_ALPHA ||
293 destBaseFormat == GL_LUMINANCE_INTEGER_EXT ||
294 destBaseFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT)) {
295 /* If we're reading back an RGB(A) texture as luminance then we need
296 * to return L=tex(R). Note, that's different from glReadPixels which
297 * returns L=R+G+B.
298 */
299 rebaseFormat = GL_LUMINANCE_ALPHA; /* this covers GL_LUMINANCE too */
300 }
301
302 if (rebaseFormat) {
303 _mesa_rebase_rgba_float(width * height, (GLfloat (*)[4]) tempImage,
304 rebaseFormat);
305 }
306
307 tempSlice = tempImage;
308 for (slice = 0; slice < depth; slice++) {
309 srcRow = tempSlice;
310 for (row = 0; row < height; row++) {
311 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
312 width, height, format, type,
313 slice, row, 0);
314
315 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) srcRow,
316 format, type, dest, &ctx->Pack, transferOps);
317 srcRow += 4 * width;
318 }
319 tempSlice += 4 * width * height;
320 }
321
322 free(tempImage);
323 }
324
325
326 /**
327 * Return a base GL format given the user-requested format
328 * for glGetTexImage().
329 */
330 GLenum
331 _mesa_base_pack_format(GLenum format)
332 {
333 switch (format) {
334 case GL_ABGR_EXT:
335 case GL_BGRA:
336 case GL_BGRA_INTEGER:
337 case GL_RGBA_INTEGER:
338 return GL_RGBA;
339 case GL_BGR:
340 case GL_BGR_INTEGER:
341 case GL_RGB_INTEGER:
342 return GL_RGB;
343 case GL_RED_INTEGER:
344 return GL_RED;
345 case GL_GREEN_INTEGER:
346 return GL_GREEN;
347 case GL_BLUE_INTEGER:
348 return GL_BLUE;
349 case GL_ALPHA_INTEGER:
350 return GL_ALPHA;
351 case GL_LUMINANCE_INTEGER_EXT:
352 return GL_LUMINANCE;
353 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
354 return GL_LUMINANCE_ALPHA;
355 default:
356 return format;
357 }
358 }
359
360
361 /**
362 * Get an uncompressed color texture image.
363 */
364 static void
365 get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
366 GLenum format, GLenum type, GLvoid *pixels,
367 struct gl_texture_image *texImage,
368 GLbitfield transferOps)
369 {
370 /* don't want to apply sRGB -> RGB conversion here so override the format */
371 const mesa_format texFormat =
372 _mesa_get_srgb_format_linear(texImage->TexFormat);
373 const GLuint width = texImage->Width;
374 GLenum destBaseFormat = _mesa_base_pack_format(format);
375 GLenum rebaseFormat = GL_NONE;
376 GLuint height = texImage->Height;
377 GLuint depth = texImage->Depth;
378 GLuint img, row;
379 GLfloat (*rgba)[4];
380 GLuint (*rgba_uint)[4];
381 GLboolean tex_is_integer = _mesa_is_format_integer_color(texImage->TexFormat);
382 GLboolean tex_is_uint = _mesa_is_format_unsigned(texImage->TexFormat);
383 GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
384
385 /* Allocate buffer for one row of texels */
386 rgba = malloc(4 * width * sizeof(GLfloat));
387 rgba_uint = (GLuint (*)[4]) rgba;
388 if (!rgba) {
389 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
390 return;
391 }
392
393 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
394 depth = height;
395 height = 1;
396 }
397
398 if (texImage->_BaseFormat == GL_LUMINANCE ||
399 texImage->_BaseFormat == GL_INTENSITY ||
400 texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
401 /* If a luminance (or intensity) texture is read back as RGB(A), the
402 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
403 * here to get G=B=0.
404 */
405 rebaseFormat = texImage->_BaseFormat;
406 }
407 else if ((texImage->_BaseFormat == GL_RGBA ||
408 texImage->_BaseFormat == GL_RGB ||
409 texImage->_BaseFormat == GL_RG) &&
410 (destBaseFormat == GL_LUMINANCE ||
411 destBaseFormat == GL_LUMINANCE_ALPHA ||
412 destBaseFormat == GL_LUMINANCE_INTEGER_EXT ||
413 destBaseFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT)) {
414 /* If we're reading back an RGB(A) texture as luminance then we need
415 * to return L=tex(R). Note, that's different from glReadPixels which
416 * returns L=R+G+B.
417 */
418 rebaseFormat = GL_LUMINANCE_ALPHA; /* this covers GL_LUMINANCE too */
419 }
420 else if (texImage->_BaseFormat != texBaseFormat) {
421 /* The internal format and the real format differ, so we can't rely
422 * on the unpack functions setting the correct constant values.
423 * (e.g. reading back GL_RGB8 which is actually RGBA won't set alpha=1)
424 */
425 switch (texImage->_BaseFormat) {
426 case GL_RED:
427 if ((texBaseFormat == GL_RGBA ||
428 texBaseFormat == GL_RGB ||
429 texBaseFormat == GL_RG) &&
430 (destBaseFormat == GL_RGBA ||
431 destBaseFormat == GL_RGB ||
432 destBaseFormat == GL_RG ||
433 destBaseFormat == GL_GREEN)) {
434 rebaseFormat = texImage->_BaseFormat;
435 break;
436 }
437 /* fall through */
438 case GL_RG:
439 if ((texBaseFormat == GL_RGBA ||
440 texBaseFormat == GL_RGB) &&
441 (destBaseFormat == GL_RGBA ||
442 destBaseFormat == GL_RGB ||
443 destBaseFormat == GL_BLUE)) {
444 rebaseFormat = texImage->_BaseFormat;
445 break;
446 }
447 /* fall through */
448 case GL_RGB:
449 if (texBaseFormat == GL_RGBA &&
450 (destBaseFormat == GL_RGBA ||
451 destBaseFormat == GL_ALPHA ||
452 destBaseFormat == GL_LUMINANCE_ALPHA)) {
453 rebaseFormat = texImage->_BaseFormat;
454 }
455 break;
456
457 case GL_ALPHA:
458 if (destBaseFormat != GL_ALPHA) {
459 rebaseFormat = texImage->_BaseFormat;
460 }
461 break;
462 }
463 }
464
465 for (img = 0; img < depth; img++) {
466 GLubyte *srcMap;
467 GLint rowstride;
468
469 /* map src texture buffer */
470 ctx->Driver.MapTextureImage(ctx, texImage, img,
471 0, 0, width, height, GL_MAP_READ_BIT,
472 &srcMap, &rowstride);
473 if (srcMap) {
474 for (row = 0; row < height; row++) {
475 const GLubyte *src = srcMap + row * rowstride;
476 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
477 width, height, format, type,
478 img, row, 0);
479
480 if (tex_is_integer) {
481 _mesa_unpack_uint_rgba_row(texFormat, width, src, rgba_uint);
482 if (rebaseFormat)
483 _mesa_rebase_rgba_uint(width, rgba_uint, rebaseFormat);
484 if (tex_is_uint) {
485 _mesa_pack_rgba_span_from_uints(ctx, width,
486 (GLuint (*)[4]) rgba_uint,
487 format, type, dest);
488 } else {
489 _mesa_pack_rgba_span_from_ints(ctx, width,
490 (GLint (*)[4]) rgba_uint,
491 format, type, dest);
492 }
493 } else {
494 _mesa_unpack_rgba_row(texFormat, width, src, rgba);
495 if (rebaseFormat)
496 _mesa_rebase_rgba_float(width, rgba, rebaseFormat);
497 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
498 format, type, dest,
499 &ctx->Pack, transferOps);
500 }
501 }
502
503 /* Unmap the src texture buffer */
504 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
505 }
506 else {
507 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
508 break;
509 }
510 }
511
512 free(rgba);
513 }
514
515
516 /**
517 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
518 * Compressed textures are handled here as well.
519 */
520 static void
521 get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
522 GLenum format, GLenum type, GLvoid *pixels,
523 struct gl_texture_image *texImage)
524 {
525 const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
526 GLbitfield transferOps = 0x0;
527
528 /* In general, clamping does not apply to glGetTexImage, except when
529 * the returned type of the image can't hold negative values.
530 */
531 if (type_needs_clamping(type)) {
532 /* the returned image type can't have negative values */
533 if (dataType == GL_FLOAT ||
534 dataType == GL_HALF_FLOAT ||
535 dataType == GL_SIGNED_NORMALIZED ||
536 format == GL_LUMINANCE ||
537 format == GL_LUMINANCE_ALPHA) {
538 transferOps |= IMAGE_CLAMP_BIT;
539 }
540 }
541
542 if (_mesa_is_format_compressed(texImage->TexFormat)) {
543 get_tex_rgba_compressed(ctx, dimensions, format, type,
544 pixels, texImage, transferOps);
545 }
546 else {
547 get_tex_rgba_uncompressed(ctx, dimensions, format, type,
548 pixels, texImage, transferOps);
549 }
550 }
551
552
553 /**
554 * Try to do glGetTexImage() with simple memcpy().
555 * \return GL_TRUE if done, GL_FALSE otherwise
556 */
557 static GLboolean
558 get_tex_memcpy(struct gl_context *ctx, GLenum format, GLenum type,
559 GLvoid *pixels,
560 struct gl_texture_image *texImage)
561 {
562 const GLenum target = texImage->TexObject->Target;
563 GLboolean memCopy = GL_FALSE;
564 GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
565
566 /*
567 * Check if we can use memcpy to copy from the hardware texture
568 * format to the user's format/type.
569 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
570 */
571 if ((target == GL_TEXTURE_1D ||
572 target == GL_TEXTURE_2D ||
573 target == GL_TEXTURE_RECTANGLE ||
574 _mesa_is_cube_face(target)) &&
575 texBaseFormat == texImage->_BaseFormat) {
576 memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
577 format, type,
578 ctx->Pack.SwapBytes);
579 }
580
581 if (memCopy) {
582 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
583 const GLuint bytesPerRow = texImage->Width * bpp;
584 GLubyte *dst =
585 _mesa_image_address2d(&ctx->Pack, pixels, texImage->Width,
586 texImage->Height, format, type, 0, 0);
587 const GLint dstRowStride =
588 _mesa_image_row_stride(&ctx->Pack, texImage->Width, format, type);
589 GLubyte *src;
590 GLint srcRowStride;
591
592 /* map src texture buffer */
593 ctx->Driver.MapTextureImage(ctx, texImage, 0,
594 0, 0, texImage->Width, texImage->Height,
595 GL_MAP_READ_BIT, &src, &srcRowStride);
596
597 if (src) {
598 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
599 memcpy(dst, src, bytesPerRow * texImage->Height);
600 }
601 else {
602 GLuint row;
603 for (row = 0; row < texImage->Height; row++) {
604 memcpy(dst, src, bytesPerRow);
605 dst += dstRowStride;
606 src += srcRowStride;
607 }
608 }
609
610 /* unmap src texture buffer */
611 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
612 }
613 else {
614 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
615 }
616 }
617
618 return memCopy;
619 }
620
621
622 /**
623 * This is the software fallback for Driver.GetTexImage().
624 * All error checking will have been done before this routine is called.
625 * We'll call ctx->Driver.MapTextureImage() to access the data, then
626 * unmap with ctx->Driver.UnmapTextureImage().
627 */
628 void
629 _mesa_get_teximage(struct gl_context *ctx,
630 GLenum format, GLenum type, GLvoid *pixels,
631 struct gl_texture_image *texImage)
632 {
633 const GLuint dimensions =
634 _mesa_get_texture_dimensions(texImage->TexObject->Target);
635
636 /* map dest buffer, if PBO */
637 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
638 /* Packing texture image into a PBO.
639 * Map the (potentially) VRAM-based buffer into our process space so
640 * we can write into it with the code below.
641 * A hardware driver might use a sophisticated blit to move the
642 * texture data to the PBO if the PBO is in VRAM along with the texture.
643 */
644 GLubyte *buf = (GLubyte *)
645 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
646 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
647 MAP_INTERNAL);
648 if (!buf) {
649 /* out of memory or other unexpected error */
650 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
651 return;
652 }
653 /* <pixels> was an offset into the PBO.
654 * Now make it a real, client-side pointer inside the mapped region.
655 */
656 pixels = ADD_POINTERS(buf, pixels);
657 }
658
659 if (get_tex_memcpy(ctx, format, type, pixels, texImage)) {
660 /* all done */
661 }
662 else if (format == GL_DEPTH_COMPONENT) {
663 get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
664 }
665 else if (format == GL_DEPTH_STENCIL_EXT) {
666 get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
667 }
668 else if (format == GL_YCBCR_MESA) {
669 get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
670 }
671 else {
672 get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
673 }
674
675 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
676 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
677 }
678 }
679
680
681
682 /**
683 * This is the software fallback for Driver.GetCompressedTexImage().
684 * All error checking will have been done before this routine is called.
685 */
686 void
687 _mesa_get_compressed_teximage(struct gl_context *ctx,
688 struct gl_texture_image *texImage,
689 GLvoid *img)
690 {
691 const GLuint dimensions =
692 _mesa_get_texture_dimensions(texImage->TexObject->Target);
693 struct compressed_pixelstore store;
694 GLuint i, slice;
695 GLubyte *dest;
696
697 _mesa_compute_compressed_pixelstore(dimensions, texImage->TexFormat,
698 texImage->Width, texImage->Height,
699 texImage->Depth,
700 &ctx->Pack,
701 &store);
702
703 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
704 /* pack texture image into a PBO */
705 dest = (GLubyte *)
706 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
707 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
708 MAP_INTERNAL);
709 if (!dest) {
710 /* out of memory or other unexpected error */
711 _mesa_error(ctx, GL_OUT_OF_MEMORY,
712 "glGetCompresssedTexImage(map PBO failed)");
713 return;
714 }
715 dest = ADD_POINTERS(dest, img);
716 } else {
717 dest = img;
718 }
719
720 dest += store.SkipBytes;
721
722 for (slice = 0; slice < store.CopySlices; slice++) {
723 GLint srcRowStride;
724 GLubyte *src;
725
726 /* map src texture buffer */
727 ctx->Driver.MapTextureImage(ctx, texImage, 0,
728 0, 0, texImage->Width, texImage->Height,
729 GL_MAP_READ_BIT, &src, &srcRowStride);
730
731 if (src) {
732
733 for (i = 0; i < store.CopyRowsPerSlice; i++) {
734 memcpy(dest, src, store.CopyBytesPerRow);
735 dest += store.TotalBytesPerRow;
736 src += srcRowStride;
737 }
738
739 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
740
741 /* Advance to next slice */
742 dest += store.TotalBytesPerRow * (store.TotalRowsPerSlice - store.CopyRowsPerSlice);
743
744 } else {
745 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
746 }
747 }
748
749 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
750 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
751 }
752 }
753
754
755 /**
756 * Validate the texture target enum supplied to glTexImage or
757 * glCompressedTexImage.
758 */
759 static GLboolean
760 legal_getteximage_target(struct gl_context *ctx, GLenum target)
761 {
762 switch (target) {
763 case GL_TEXTURE_1D:
764 case GL_TEXTURE_2D:
765 case GL_TEXTURE_3D:
766 return GL_TRUE;
767 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
768 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
769 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
770 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
771 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
772 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
773 return ctx->Extensions.ARB_texture_cube_map;
774 case GL_TEXTURE_RECTANGLE_NV:
775 return ctx->Extensions.NV_texture_rectangle;
776 case GL_TEXTURE_1D_ARRAY_EXT:
777 case GL_TEXTURE_2D_ARRAY_EXT:
778 return ctx->Extensions.EXT_texture_array;
779 case GL_TEXTURE_CUBE_MAP_ARRAY:
780 return ctx->Extensions.ARB_texture_cube_map_array;
781 default:
782 return GL_FALSE;
783 }
784 }
785
786
787 /**
788 * Do error checking for a glGetTexImage() call.
789 * \return GL_TRUE if any error, GL_FALSE if no errors.
790 */
791 static GLboolean
792 getteximage_error_check(struct gl_context *ctx, GLenum target, GLint level,
793 GLenum format, GLenum type, GLsizei clientMemSize,
794 GLvoid *pixels )
795 {
796 struct gl_texture_object *texObj;
797 struct gl_texture_image *texImage;
798 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
799 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
800 GLenum baseFormat, err;
801
802 if (!legal_getteximage_target(ctx, target)) {
803 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target=0x%x)", target);
804 return GL_TRUE;
805 }
806
807 assert(maxLevels != 0);
808 if (level < 0 || level >= maxLevels) {
809 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
810 return GL_TRUE;
811 }
812
813 err = _mesa_error_check_format_and_type(ctx, format, type);
814 if (err != GL_NO_ERROR) {
815 _mesa_error(ctx, err, "glGetTexImage(format/type)");
816 return GL_TRUE;
817 }
818
819 texObj = _mesa_get_current_tex_object(ctx, target);
820
821 if (!texObj) {
822 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
823 return GL_TRUE;
824 }
825
826 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
827 if (!texImage) {
828 /* non-existant texture image */
829 return GL_TRUE;
830 }
831
832 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
833
834 /* Make sure the requested image format is compatible with the
835 * texture's format.
836 */
837 if (_mesa_is_color_format(format)
838 && !_mesa_is_color_format(baseFormat)) {
839 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
840 return GL_TRUE;
841 }
842 else if (_mesa_is_depth_format(format)
843 && !_mesa_is_depth_format(baseFormat)
844 && !_mesa_is_depthstencil_format(baseFormat)) {
845 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
846 return GL_TRUE;
847 }
848 else if (_mesa_is_stencil_format(format)
849 && !ctx->Extensions.ARB_texture_stencil8) {
850 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format=GL_STENCIL_INDEX)");
851 return GL_TRUE;
852 }
853 else if (_mesa_is_ycbcr_format(format)
854 && !_mesa_is_ycbcr_format(baseFormat)) {
855 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
856 return GL_TRUE;
857 }
858 else if (_mesa_is_depthstencil_format(format)
859 && !_mesa_is_depthstencil_format(baseFormat)) {
860 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
861 return GL_TRUE;
862 }
863 else if (_mesa_is_enum_format_integer(format) !=
864 _mesa_is_format_integer(texImage->TexFormat)) {
865 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
866 return GL_TRUE;
867 }
868
869 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
870 texImage->Height, texImage->Depth,
871 format, type, clientMemSize, pixels)) {
872 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
873 _mesa_error(ctx, GL_INVALID_OPERATION,
874 "glGetTexImage(out of bounds PBO access)");
875 } else {
876 _mesa_error(ctx, GL_INVALID_OPERATION,
877 "glGetnTexImageARB(out of bounds access:"
878 " bufSize (%d) is too small)", clientMemSize);
879 }
880 return GL_TRUE;
881 }
882
883 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
884 /* PBO should not be mapped */
885 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
886 _mesa_error(ctx, GL_INVALID_OPERATION,
887 "glGetTexImage(PBO is mapped)");
888 return GL_TRUE;
889 }
890 }
891
892 return GL_FALSE;
893 }
894
895
896
897 /**
898 * Get texture image. Called by glGetTexImage.
899 *
900 * \param target texture target.
901 * \param level image level.
902 * \param format pixel data format for returned image.
903 * \param type pixel data type for returned image.
904 * \param bufSize size of the pixels data buffer.
905 * \param pixels returned pixel data.
906 */
907 void GLAPIENTRY
908 _mesa_GetnTexImageARB( GLenum target, GLint level, GLenum format,
909 GLenum type, GLsizei bufSize, GLvoid *pixels )
910 {
911 struct gl_texture_object *texObj;
912 struct gl_texture_image *texImage;
913 GET_CURRENT_CONTEXT(ctx);
914
915 FLUSH_VERTICES(ctx, 0);
916
917 if (getteximage_error_check(ctx, target, level, format, type,
918 bufSize, pixels)) {
919 return;
920 }
921
922 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
923 /* not an error, do nothing */
924 return;
925 }
926
927 texObj = _mesa_get_current_tex_object(ctx, target);
928 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
929
930 if (_mesa_is_zero_size_texture(texImage))
931 return;
932
933 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
934 _mesa_debug(ctx, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
935 " dstFmt=0x%x, dstType=0x%x\n",
936 texObj->Name,
937 _mesa_get_format_name(texImage->TexFormat),
938 texImage->Width, texImage->Height,
939 format, type);
940 }
941
942 _mesa_lock_texture(ctx, texObj);
943 {
944 ctx->Driver.GetTexImage(ctx, format, type, pixels, texImage);
945 }
946 _mesa_unlock_texture(ctx, texObj);
947 }
948
949
950 void GLAPIENTRY
951 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
952 GLenum type, GLvoid *pixels )
953 {
954 _mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
955 }
956
957
958 /**
959 * Do error checking for a glGetCompressedTexImage() call.
960 * \return GL_TRUE if any error, GL_FALSE if no errors.
961 */
962 static GLboolean
963 getcompressedteximage_error_check(struct gl_context *ctx, GLenum target,
964 GLint level, GLsizei clientMemSize, GLvoid *img)
965 {
966 struct gl_texture_object *texObj;
967 struct gl_texture_image *texImage;
968 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
969 GLuint compressedSize, dimensions;
970
971 if (!legal_getteximage_target(ctx, target)) {
972 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImage(target=0x%x)",
973 target);
974 return GL_TRUE;
975 }
976
977 assert(maxLevels != 0);
978 if (level < 0 || level >= maxLevels) {
979 _mesa_error(ctx, GL_INVALID_VALUE,
980 "glGetCompressedTexImageARB(bad level = %d)", level);
981 return GL_TRUE;
982 }
983
984 texObj = _mesa_get_current_tex_object(ctx, target);
985 if (!texObj) {
986 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
987 return GL_TRUE;
988 }
989
990 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
991
992 if (!texImage) {
993 /* probably invalid mipmap level */
994 _mesa_error(ctx, GL_INVALID_VALUE,
995 "glGetCompressedTexImageARB(level)");
996 return GL_TRUE;
997 }
998
999 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
1000 _mesa_error(ctx, GL_INVALID_OPERATION,
1001 "glGetCompressedTexImageARB(texture is not compressed)");
1002 return GL_TRUE;
1003 }
1004
1005 compressedSize = _mesa_format_image_size(texImage->TexFormat,
1006 texImage->Width,
1007 texImage->Height,
1008 texImage->Depth);
1009
1010 /* Check for invalid pixel storage modes */
1011 dimensions = _mesa_get_texture_dimensions(texImage->TexObject->Target);
1012 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
1013 &ctx->Pack,
1014 "glGetCompressedTexImageARB")) {
1015 return GL_TRUE;
1016 }
1017
1018 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1019 /* do bounds checking on writing to client memory */
1020 if (clientMemSize < (GLsizei) compressedSize) {
1021 _mesa_error(ctx, GL_INVALID_OPERATION,
1022 "glGetnCompressedTexImageARB(out of bounds access:"
1023 " bufSize (%d) is too small)", clientMemSize);
1024 return GL_TRUE;
1025 }
1026 } else {
1027 /* do bounds checking on PBO write */
1028 if ((const GLubyte *) img + compressedSize >
1029 (const GLubyte *) ctx->Pack.BufferObj->Size) {
1030 _mesa_error(ctx, GL_INVALID_OPERATION,
1031 "glGetCompressedTexImage(out of bounds PBO access)");
1032 return GL_TRUE;
1033 }
1034
1035 /* make sure PBO is not mapped */
1036 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1037 _mesa_error(ctx, GL_INVALID_OPERATION,
1038 "glGetCompressedTexImage(PBO is mapped)");
1039 return GL_TRUE;
1040 }
1041 }
1042
1043 return GL_FALSE;
1044 }
1045
1046
1047 void GLAPIENTRY
1048 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1049 GLvoid *img)
1050 {
1051 struct gl_texture_object *texObj;
1052 struct gl_texture_image *texImage;
1053 GET_CURRENT_CONTEXT(ctx);
1054
1055 FLUSH_VERTICES(ctx, 0);
1056
1057 if (getcompressedteximage_error_check(ctx, target, level, bufSize, img)) {
1058 return;
1059 }
1060
1061 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !img) {
1062 /* not an error, do nothing */
1063 return;
1064 }
1065
1066 texObj = _mesa_get_current_tex_object(ctx, target);
1067 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
1068
1069 if (_mesa_is_zero_size_texture(texImage))
1070 return;
1071
1072 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1073 _mesa_debug(ctx,
1074 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1075 texObj->Name,
1076 _mesa_get_format_name(texImage->TexFormat),
1077 texImage->Width, texImage->Height);
1078 }
1079
1080 _mesa_lock_texture(ctx, texObj);
1081 {
1082 ctx->Driver.GetCompressedTexImage(ctx, texImage, img);
1083 }
1084 _mesa_unlock_texture(ctx, texObj);
1085 }
1086
1087 void GLAPIENTRY
1088 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *img)
1089 {
1090 _mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
1091 }