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