main: Added entry point for glGetTextureImage.
[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_GetTexImage_sw(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_GetCompressedTexImage_sw(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 glGetTex(ture)Image or
762 * glGetCompressedTex(ture)Image.
763 */
764 static GLboolean
765 legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
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_RECTANGLE_NV:
773 return ctx->Extensions.NV_texture_rectangle;
774 case GL_TEXTURE_1D_ARRAY_EXT:
775 case GL_TEXTURE_2D_ARRAY_EXT:
776 return ctx->Extensions.EXT_texture_array;
777 case GL_TEXTURE_CUBE_MAP_ARRAY:
778 return ctx->Extensions.ARB_texture_cube_map_array;
779
780 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
781 * (30.10.2014) says:
782 * "An INVALID_ENUM error is generated if the effective target is not
783 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
784 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
785 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
786 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
787 */
788 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
789 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
790 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
791 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
792 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
793 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
794 return dsa ? GL_FALSE : ctx->Extensions.ARB_texture_cube_map;
795 case GL_TEXTURE_CUBE_MAP:
796 return dsa ? GL_TRUE : GL_FALSE;
797 default:
798 return GL_FALSE;
799 }
800 }
801
802
803 /**
804 * Do error checking for a glGetTex(ture)Image() call.
805 * \return GL_TRUE if any error, GL_FALSE if no errors.
806 */
807 static GLboolean
808 getteximage_error_check(struct gl_context *ctx,
809 struct gl_texture_image *texImage,
810 GLenum target, GLint level,
811 GLenum format, GLenum type, GLsizei clientMemSize,
812 GLvoid *pixels, bool dsa)
813 {
814 assert(texImage);
815
816 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
817 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
818 GLenum baseFormat;
819 const char *suffix = dsa ? "ture" : "";
820
821 assert(maxLevels != 0);
822 if (level < 0 || level >= maxLevels) {
823 _mesa_error(ctx, GL_INVALID_VALUE,
824 "glGetTex%sImage(level out of range)", suffix);
825 return GL_TRUE;
826 }
827
828 /*
829 * Format and type checking has been moved up to GetnTexImage and
830 * GetTextureImage so that it happens before getting the texImage object.
831 */
832
833 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
834
835 /* Make sure the requested image format is compatible with the
836 * texture's format.
837 */
838 if (_mesa_is_color_format(format)
839 && !_mesa_is_color_format(baseFormat)) {
840 _mesa_error(ctx, GL_INVALID_OPERATION,
841 "glGetTex%sImage(format mismatch)", suffix);
842 return GL_TRUE;
843 }
844 else if (_mesa_is_depth_format(format)
845 && !_mesa_is_depth_format(baseFormat)
846 && !_mesa_is_depthstencil_format(baseFormat)) {
847 _mesa_error(ctx, GL_INVALID_OPERATION,
848 "glGetTex%sImage(format mismatch)", suffix);
849 return GL_TRUE;
850 }
851 else if (_mesa_is_stencil_format(format)
852 && !ctx->Extensions.ARB_texture_stencil8) {
853 _mesa_error(ctx, GL_INVALID_ENUM,
854 "glGetTex%sImage(format=GL_STENCIL_INDEX)", suffix);
855 return GL_TRUE;
856 }
857 else if (_mesa_is_ycbcr_format(format)
858 && !_mesa_is_ycbcr_format(baseFormat)) {
859 _mesa_error(ctx, GL_INVALID_OPERATION,
860 "glGetTex%sImage(format mismatch)", suffix);
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,
866 "glGetTex%sImage(format mismatch)", suffix);
867 return GL_TRUE;
868 }
869 else if (_mesa_is_enum_format_integer(format) !=
870 _mesa_is_format_integer(texImage->TexFormat)) {
871 _mesa_error(ctx, GL_INVALID_OPERATION,
872 "glGetTex%sImage(format mismatch)", suffix);
873 return GL_TRUE;
874 }
875
876 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
877 texImage->Height, texImage->Depth,
878 format, type, clientMemSize, pixels)) {
879 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
880 _mesa_error(ctx, GL_INVALID_OPERATION,
881 "glGetTex%sImage(out of bounds PBO access)", suffix);
882 } else {
883 _mesa_error(ctx, GL_INVALID_OPERATION,
884 "%s(out of bounds access:"
885 " bufSize (%d) is too small)",
886 dsa ? "glGetTextureImage" : "glGetnTexImageARB",
887 clientMemSize);
888 }
889 return GL_TRUE;
890 }
891
892 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
893 /* PBO should not be mapped */
894 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
895 _mesa_error(ctx, GL_INVALID_OPERATION,
896 "glGetTex%sImage(PBO is mapped)", suffix);
897 return GL_TRUE;
898 }
899 }
900
901 return GL_FALSE;
902 }
903
904
905 /**
906 * This is the implementation for glGetnTexImageARB, glGetTextureImage,
907 * and glGetTexImage.
908 *
909 * Requires caller to pass in texImage object because _mesa_GetTextureImage
910 * must handle the GL_TEXTURE_CUBE_MAP target.
911 *
912 * \param target texture target.
913 * \param level image level.
914 * \param format pixel data format for returned image.
915 * \param type pixel data type for returned image.
916 * \param bufSize size of the pixels data buffer.
917 * \param pixels returned pixel data.
918 * \param dsa True when the caller is an ARB_direct_state_access function,
919 * false otherwise
920 */
921 void
922 _mesa_get_texture_image(struct gl_context *ctx,
923 struct gl_texture_object *texObj,
924 struct gl_texture_image *texImage, GLenum target,
925 GLint level, GLenum format, GLenum type,
926 GLsizei bufSize, GLvoid *pixels, bool dsa)
927 {
928 assert(texObj);
929 assert(texImage);
930
931 FLUSH_VERTICES(ctx, 0);
932
933 /*
934 * Legal target checking has been moved up to GetnTexImage and
935 * GetTextureImage so that it can be caught before receiving a NULL
936 * texImage object and exiting.
937 */
938
939 if (getteximage_error_check(ctx, texImage, target, level, format,
940 type, bufSize, pixels, dsa)) {
941 return;
942 }
943
944 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
945 /* not an error, do nothing */
946 return;
947 }
948
949 if (_mesa_is_zero_size_texture(texImage))
950 return;
951
952 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
953 _mesa_debug(ctx, "glGetTex%sImage(tex %u) format = %s, w=%d, h=%d,"
954 " dstFmt=0x%x, dstType=0x%x\n",
955 dsa ? "ture": "",
956 texObj->Name,
957 _mesa_get_format_name(texImage->TexFormat),
958 texImage->Width, texImage->Height,
959 format, type);
960 }
961
962 _mesa_lock_texture(ctx, texObj);
963 {
964 ctx->Driver.GetTexImage(ctx, format, type, pixels, texImage);
965 }
966 _mesa_unlock_texture(ctx, texObj);
967 }
968
969 /**
970 * Get texture image. Called by glGetTexImage.
971 *
972 * \param target texture target.
973 * \param level image level.
974 * \param format pixel data format for returned image.
975 * \param type pixel data type for returned image.
976 * \param bufSize size of the pixels data buffer.
977 * \param pixels returned pixel data.
978 */
979 void GLAPIENTRY
980 _mesa_GetnTexImageARB(GLenum target, GLint level, GLenum format,
981 GLenum type, GLsizei bufSize, GLvoid *pixels)
982 {
983 struct gl_texture_object *texObj;
984 struct gl_texture_image *texImage;
985 GLenum err;
986 GET_CURRENT_CONTEXT(ctx);
987
988 /*
989 * This has been moved here because a format/type mismatch can cause a NULL
990 * texImage object, which in turn causes the mismatch error to be
991 * ignored.
992 */
993 err = _mesa_error_check_format_and_type(ctx, format, type);
994 if (err != GL_NO_ERROR) {
995 _mesa_error(ctx, err, "glGetnTexImage(format/type)");
996 return;
997 }
998
999 /*
1000 * Legal target checking has been moved here to prevent exiting with a NULL
1001 * texImage object.
1002 */
1003 if (!legal_getteximage_target(ctx, target, false)) {
1004 _mesa_error(ctx, GL_INVALID_ENUM, "glGetnTexImage(target=0x%x)",
1005 target);
1006 return;
1007 }
1008
1009 texObj = _mesa_get_current_tex_object(ctx, target);
1010 if (!texObj)
1011 return;
1012
1013 texImage = _mesa_select_tex_image(texObj, target, level);
1014 if (!texImage)
1015 return;
1016
1017 _mesa_get_texture_image(ctx, texObj, texImage, target, level, format, type,
1018 bufSize, pixels, false);
1019 }
1020
1021
1022 void GLAPIENTRY
1023 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
1024 GLenum type, GLvoid *pixels )
1025 {
1026 _mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
1027 }
1028
1029 /**
1030 * Get texture image.
1031 *
1032 * \param texture texture name.
1033 * \param level image level.
1034 * \param format pixel data format for returned image.
1035 * \param type pixel data type for returned image.
1036 * \param bufSize size of the pixels data buffer.
1037 * \param pixels returned pixel data.
1038 */
1039 void GLAPIENTRY
1040 _mesa_GetTextureImage(GLuint texture, GLint level, GLenum format,
1041 GLenum type, GLsizei bufSize, GLvoid *pixels)
1042 {
1043 struct gl_texture_object *texObj;
1044 struct gl_texture_image *texImage;
1045 int i;
1046 GLint image_stride;
1047 GLenum err;
1048 GET_CURRENT_CONTEXT(ctx);
1049
1050 /*
1051 * This has been moved here because a format/type mismatch can cause a NULL
1052 * texImage object, which in turn causes the mismatch error to be
1053 * ignored.
1054 */
1055 err = _mesa_error_check_format_and_type(ctx, format, type);
1056 if (err != GL_NO_ERROR) {
1057 _mesa_error(ctx, err, "glGetTextureImage(format/type)");
1058 return;
1059 }
1060
1061 texObj = _mesa_lookup_texture_err(ctx, texture, "glGetTextureImage");
1062 if (!texObj)
1063 return;
1064
1065 /*
1066 * Legal target checking has been moved here to prevent exiting with a NULL
1067 * texImage object.
1068 */
1069 if (!legal_getteximage_target(ctx, texObj->Target, true)) {
1070 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTextureImage(target=%s)",
1071 _mesa_lookup_enum_by_nr(texObj->Target));
1072 return;
1073 }
1074
1075 /* Must handle special case GL_TEXTURE_CUBE_MAP. */
1076 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
1077
1078 /* Error checking */
1079 if (texObj->NumLayers < 6) {
1080 /* Not enough image planes for a cube map. The spec does not say
1081 * what should happen in this case because the user has always
1082 * specified each cube face separately (using
1083 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
1084 * This is addressed in Khronos Bug 13223.
1085 */
1086 _mesa_error(ctx, GL_INVALID_OPERATION,
1087 "glGetTextureImage(insufficient cube map storage)");
1088 return;
1089 }
1090 for (i = 0; i < 6; ++i) { /* For each face. */
1091 if (!texObj->Image[i][level]) {
1092 /* Not enough image planes for a cube map. The spec does not say
1093 * what should happen in this case because the user has always
1094 * specified each cube face separately (using
1095 * GL_TEXTURE_CUBE_MAP_POSITIVE_X+i) in previous GL versions.
1096 * This is addressed in Khronos Bug 13223.
1097 */
1098 _mesa_error(ctx, GL_INVALID_OPERATION,
1099 "glGetTextureImage(insufficient cube map storage)");
1100 return;
1101 }
1102 }
1103
1104 /* Copy each face. */
1105 for (i = 0; i < 6; ++i) {
1106 texImage = texObj->Image[i][level];
1107 _mesa_get_texture_image(ctx, texObj, texImage, texObj->Target, level,
1108 format, type, bufSize, pixels, true);
1109
1110 image_stride = _mesa_image_image_stride(&ctx->Pack, texImage->Width,
1111 texImage->Height, format,
1112 type);
1113 pixels += image_stride;
1114 bufSize -= image_stride;
1115 }
1116 }
1117 else {
1118 texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
1119 if (!texImage)
1120 return;
1121
1122 _mesa_get_texture_image(ctx, texObj, texImage, texObj->Target, level,
1123 format, type, bufSize, pixels, true);
1124 }
1125 }
1126
1127 /**
1128 * Do error checking for a glGetCompressedTexImage() call.
1129 * \return GL_TRUE if any error, GL_FALSE if no errors.
1130 */
1131 static GLboolean
1132 getcompressedteximage_error_check(struct gl_context *ctx, GLenum target,
1133 GLint level, GLsizei clientMemSize, GLvoid *img)
1134 {
1135 struct gl_texture_object *texObj;
1136 struct gl_texture_image *texImage;
1137 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
1138 GLuint compressedSize, dimensions;
1139
1140 if (!legal_getteximage_target(ctx, target, false)) {
1141 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImage(target=0x%x)",
1142 target);
1143 return GL_TRUE;
1144 }
1145
1146 assert(maxLevels != 0);
1147 if (level < 0 || level >= maxLevels) {
1148 _mesa_error(ctx, GL_INVALID_VALUE,
1149 "glGetCompressedTexImageARB(bad level = %d)", level);
1150 return GL_TRUE;
1151 }
1152
1153 texObj = _mesa_get_current_tex_object(ctx, target);
1154 if (!texObj) {
1155 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
1156 return GL_TRUE;
1157 }
1158
1159 texImage = _mesa_select_tex_image(texObj, target, level);
1160
1161 if (!texImage) {
1162 /* probably invalid mipmap level */
1163 _mesa_error(ctx, GL_INVALID_VALUE,
1164 "glGetCompressedTexImageARB(level)");
1165 return GL_TRUE;
1166 }
1167
1168 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
1169 _mesa_error(ctx, GL_INVALID_OPERATION,
1170 "glGetCompressedTexImageARB(texture is not compressed)");
1171 return GL_TRUE;
1172 }
1173
1174 compressedSize = _mesa_format_image_size(texImage->TexFormat,
1175 texImage->Width,
1176 texImage->Height,
1177 texImage->Depth);
1178
1179 /* Check for invalid pixel storage modes */
1180 dimensions = _mesa_get_texture_dimensions(texImage->TexObject->Target);
1181 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
1182 &ctx->Pack,
1183 "glGetCompressedTexImageARB")) {
1184 return GL_TRUE;
1185 }
1186
1187 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1188 /* do bounds checking on writing to client memory */
1189 if (clientMemSize < (GLsizei) compressedSize) {
1190 _mesa_error(ctx, GL_INVALID_OPERATION,
1191 "glGetnCompressedTexImageARB(out of bounds access:"
1192 " bufSize (%d) is too small)", clientMemSize);
1193 return GL_TRUE;
1194 }
1195 } else {
1196 /* do bounds checking on PBO write */
1197 if ((const GLubyte *) img + compressedSize >
1198 (const GLubyte *) ctx->Pack.BufferObj->Size) {
1199 _mesa_error(ctx, GL_INVALID_OPERATION,
1200 "glGetCompressedTexImage(out of bounds PBO access)");
1201 return GL_TRUE;
1202 }
1203
1204 /* make sure PBO is not mapped */
1205 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1206 _mesa_error(ctx, GL_INVALID_OPERATION,
1207 "glGetCompressedTexImage(PBO is mapped)");
1208 return GL_TRUE;
1209 }
1210 }
1211
1212 return GL_FALSE;
1213 }
1214
1215
1216 void GLAPIENTRY
1217 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1218 GLvoid *img)
1219 {
1220 struct gl_texture_object *texObj;
1221 struct gl_texture_image *texImage;
1222 GET_CURRENT_CONTEXT(ctx);
1223
1224 FLUSH_VERTICES(ctx, 0);
1225
1226 if (getcompressedteximage_error_check(ctx, target, level, bufSize, img)) {
1227 return;
1228 }
1229
1230 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !img) {
1231 /* not an error, do nothing */
1232 return;
1233 }
1234
1235 texObj = _mesa_get_current_tex_object(ctx, target);
1236 texImage = _mesa_select_tex_image(texObj, target, level);
1237
1238 if (_mesa_is_zero_size_texture(texImage))
1239 return;
1240
1241 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1242 _mesa_debug(ctx,
1243 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1244 texObj->Name,
1245 _mesa_get_format_name(texImage->TexFormat),
1246 texImage->Width, texImage->Height);
1247 }
1248
1249 _mesa_lock_texture(ctx, texObj);
1250 {
1251 ctx->Driver.GetCompressedTexImage(ctx, texImage, img);
1252 }
1253 _mesa_unlock_texture(ctx, texObj);
1254 }
1255
1256 void GLAPIENTRY
1257 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *img)
1258 {
1259 _mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
1260 }