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