mesa: handle HALF_FLOAT like FLOAT in get_tex_rgba
[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_HALF_FLOAT ||
522 dataType == GL_SIGNED_NORMALIZED ||
523 format == GL_LUMINANCE ||
524 format == GL_LUMINANCE_ALPHA) {
525 transferOps |= IMAGE_CLAMP_BIT;
526 }
527 }
528
529 if (_mesa_is_format_compressed(texImage->TexFormat)) {
530 get_tex_rgba_compressed(ctx, dimensions, format, type,
531 pixels, texImage, transferOps);
532 }
533 else {
534 get_tex_rgba_uncompressed(ctx, dimensions, format, type,
535 pixels, texImage, transferOps);
536 }
537 }
538
539
540 /**
541 * Try to do glGetTexImage() with simple memcpy().
542 * \return GL_TRUE if done, GL_FALSE otherwise
543 */
544 static GLboolean
545 get_tex_memcpy(struct gl_context *ctx, GLenum format, GLenum type,
546 GLvoid *pixels,
547 struct gl_texture_image *texImage)
548 {
549 const GLenum target = texImage->TexObject->Target;
550 GLboolean memCopy = GL_FALSE;
551 GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
552
553 /*
554 * Check if we can use memcpy to copy from the hardware texture
555 * format to the user's format/type.
556 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
557 */
558 if ((target == GL_TEXTURE_1D ||
559 target == GL_TEXTURE_2D ||
560 target == GL_TEXTURE_RECTANGLE ||
561 _mesa_is_cube_face(target)) &&
562 texBaseFormat == texImage->_BaseFormat) {
563 memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
564 format, type,
565 ctx->Pack.SwapBytes);
566 }
567
568 if (memCopy) {
569 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
570 const GLuint bytesPerRow = texImage->Width * bpp;
571 GLubyte *dst =
572 _mesa_image_address2d(&ctx->Pack, pixels, texImage->Width,
573 texImage->Height, format, type, 0, 0);
574 const GLint dstRowStride =
575 _mesa_image_row_stride(&ctx->Pack, texImage->Width, format, type);
576 GLubyte *src;
577 GLint srcRowStride;
578
579 /* map src texture buffer */
580 ctx->Driver.MapTextureImage(ctx, texImage, 0,
581 0, 0, texImage->Width, texImage->Height,
582 GL_MAP_READ_BIT, &src, &srcRowStride);
583
584 if (src) {
585 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
586 memcpy(dst, src, bytesPerRow * texImage->Height);
587 }
588 else {
589 GLuint row;
590 for (row = 0; row < texImage->Height; row++) {
591 memcpy(dst, src, bytesPerRow);
592 dst += dstRowStride;
593 src += srcRowStride;
594 }
595 }
596
597 /* unmap src texture buffer */
598 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
599 }
600 else {
601 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
602 }
603 }
604
605 return memCopy;
606 }
607
608
609 /**
610 * This is the software fallback for Driver.GetTexImage().
611 * All error checking will have been done before this routine is called.
612 * We'll call ctx->Driver.MapTextureImage() to access the data, then
613 * unmap with ctx->Driver.UnmapTextureImage().
614 */
615 void
616 _mesa_get_teximage(struct gl_context *ctx,
617 GLenum format, GLenum type, GLvoid *pixels,
618 struct gl_texture_image *texImage)
619 {
620 GLuint dimensions;
621
622 switch (texImage->TexObject->Target) {
623 case GL_TEXTURE_1D:
624 dimensions = 1;
625 break;
626 case GL_TEXTURE_3D:
627 dimensions = 3;
628 break;
629 default:
630 dimensions = 2;
631 }
632
633 /* map dest buffer, if PBO */
634 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
635 /* Packing texture image into a PBO.
636 * Map the (potentially) VRAM-based buffer into our process space so
637 * we can write into it with the code below.
638 * A hardware driver might use a sophisticated blit to move the
639 * texture data to the PBO if the PBO is in VRAM along with the texture.
640 */
641 GLubyte *buf = (GLubyte *)
642 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
643 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
644 if (!buf) {
645 /* out of memory or other unexpected error */
646 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
647 return;
648 }
649 /* <pixels> was an offset into the PBO.
650 * Now make it a real, client-side pointer inside the mapped region.
651 */
652 pixels = ADD_POINTERS(buf, pixels);
653 }
654
655 if (get_tex_memcpy(ctx, format, type, pixels, texImage)) {
656 /* all done */
657 }
658 else if (format == GL_DEPTH_COMPONENT) {
659 get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
660 }
661 else if (format == GL_DEPTH_STENCIL_EXT) {
662 get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
663 }
664 else if (format == GL_YCBCR_MESA) {
665 get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
666 }
667 else {
668 get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
669 }
670
671 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
672 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
673 }
674 }
675
676
677
678 /**
679 * This is the software fallback for Driver.GetCompressedTexImage().
680 * All error checking will have been done before this routine is called.
681 */
682 void
683 _mesa_get_compressed_teximage(struct gl_context *ctx,
684 struct gl_texture_image *texImage,
685 GLvoid *img)
686 {
687 const GLuint row_stride =
688 _mesa_format_row_stride(texImage->TexFormat, texImage->Width);
689 GLuint i;
690 GLubyte *src;
691 GLint srcRowStride;
692
693 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
694 /* pack texture image into a PBO */
695 GLubyte *buf = (GLubyte *)
696 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
697 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
698 if (!buf) {
699 /* out of memory or other unexpected error */
700 _mesa_error(ctx, GL_OUT_OF_MEMORY,
701 "glGetCompresssedTexImage(map PBO failed)");
702 return;
703 }
704 img = ADD_POINTERS(buf, img);
705 }
706
707 /* map src texture buffer */
708 ctx->Driver.MapTextureImage(ctx, texImage, 0,
709 0, 0, texImage->Width, texImage->Height,
710 GL_MAP_READ_BIT, &src, &srcRowStride);
711
712 if (src) {
713 /* no pixelstore or pixel transfer, but respect stride */
714
715 if (row_stride == srcRowStride) {
716 const GLuint size = _mesa_format_image_size(texImage->TexFormat,
717 texImage->Width,
718 texImage->Height,
719 texImage->Depth);
720 memcpy(img, src, size);
721 }
722 else {
723 GLuint bw, bh;
724 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
725 for (i = 0; i < (texImage->Height + bh - 1) / bh; i++) {
726 memcpy((GLubyte *)img + i * row_stride,
727 (GLubyte *)src + i * srcRowStride,
728 row_stride);
729 }
730 }
731
732 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
733 }
734 else {
735 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
736 }
737
738 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
739 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
740 }
741 }
742
743
744 /**
745 * Validate the texture target enum supplied to glTexImage or
746 * glCompressedTexImage.
747 */
748 static GLboolean
749 legal_getteximage_target(struct gl_context *ctx, GLenum target)
750 {
751 switch (target) {
752 case GL_TEXTURE_1D:
753 case GL_TEXTURE_2D:
754 case GL_TEXTURE_3D:
755 return GL_TRUE;
756 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
757 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
758 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
759 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
760 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
761 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
762 return ctx->Extensions.ARB_texture_cube_map;
763 case GL_TEXTURE_RECTANGLE_NV:
764 return ctx->Extensions.NV_texture_rectangle;
765 case GL_TEXTURE_1D_ARRAY_EXT:
766 case GL_TEXTURE_2D_ARRAY_EXT:
767 return (ctx->Extensions.MESA_texture_array ||
768 ctx->Extensions.EXT_texture_array);
769 case GL_TEXTURE_CUBE_MAP_ARRAY:
770 return ctx->Extensions.ARB_texture_cube_map_array;
771 default:
772 return GL_FALSE;
773 }
774 }
775
776
777 /**
778 * Do error checking for a glGetTexImage() call.
779 * \return GL_TRUE if any error, GL_FALSE if no errors.
780 */
781 static GLboolean
782 getteximage_error_check(struct gl_context *ctx, GLenum target, GLint level,
783 GLenum format, GLenum type, GLsizei clientMemSize,
784 GLvoid *pixels )
785 {
786 struct gl_texture_object *texObj;
787 struct gl_texture_image *texImage;
788 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
789 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
790 GLenum baseFormat, err;
791
792 if (!legal_getteximage_target(ctx, target)) {
793 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target=0x%x)", target);
794 return GL_TRUE;
795 }
796
797 assert(maxLevels != 0);
798 if (level < 0 || level >= maxLevels) {
799 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
800 return GL_TRUE;
801 }
802
803 err = _mesa_error_check_format_and_type(ctx, format, type);
804 if (err != GL_NO_ERROR) {
805 _mesa_error(ctx, err, "glGetTexImage(format/type)");
806 return GL_TRUE;
807 }
808
809 texObj = _mesa_get_current_tex_object(ctx, target);
810
811 if (!texObj) {
812 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
813 return GL_TRUE;
814 }
815
816 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
817 if (!texImage) {
818 /* non-existant texture image */
819 return GL_TRUE;
820 }
821
822 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
823
824 /* Make sure the requested image format is compatible with the
825 * texture's format.
826 */
827 if (_mesa_is_color_format(format)
828 && !_mesa_is_color_format(baseFormat)) {
829 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
830 return GL_TRUE;
831 }
832 else if (_mesa_is_depth_format(format)
833 && !_mesa_is_depth_format(baseFormat)
834 && !_mesa_is_depthstencil_format(baseFormat)) {
835 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
836 return GL_TRUE;
837 }
838 else if (_mesa_is_ycbcr_format(format)
839 && !_mesa_is_ycbcr_format(baseFormat)) {
840 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
841 return GL_TRUE;
842 }
843 else if (_mesa_is_depthstencil_format(format)
844 && !_mesa_is_depthstencil_format(baseFormat)) {
845 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
846 return GL_TRUE;
847 }
848 else if (_mesa_is_dudv_format(format)
849 && !_mesa_is_dudv_format(baseFormat)) {
850 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
851 return GL_TRUE;
852 }
853
854 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
855 texImage->Height, texImage->Depth,
856 format, type, clientMemSize, pixels)) {
857 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
858 _mesa_error(ctx, GL_INVALID_OPERATION,
859 "glGetTexImage(out of bounds PBO access)");
860 } else {
861 _mesa_error(ctx, GL_INVALID_OPERATION,
862 "glGetnTexImageARB(out of bounds access:"
863 " bufSize (%d) is too small)", clientMemSize);
864 }
865 return GL_TRUE;
866 }
867
868 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
869 /* PBO should not be mapped */
870 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
871 _mesa_error(ctx, GL_INVALID_OPERATION,
872 "glGetTexImage(PBO is mapped)");
873 return GL_TRUE;
874 }
875 }
876
877 return GL_FALSE;
878 }
879
880
881
882 /**
883 * Get texture image. Called by glGetTexImage.
884 *
885 * \param target texture target.
886 * \param level image level.
887 * \param format pixel data format for returned image.
888 * \param type pixel data type for returned image.
889 * \param bufSize size of the pixels data buffer.
890 * \param pixels returned pixel data.
891 */
892 void GLAPIENTRY
893 _mesa_GetnTexImageARB( GLenum target, GLint level, GLenum format,
894 GLenum type, GLsizei bufSize, GLvoid *pixels )
895 {
896 struct gl_texture_object *texObj;
897 struct gl_texture_image *texImage;
898 GET_CURRENT_CONTEXT(ctx);
899
900 FLUSH_VERTICES(ctx, 0);
901
902 if (getteximage_error_check(ctx, target, level, format, type,
903 bufSize, pixels)) {
904 return;
905 }
906
907 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
908 /* not an error, do nothing */
909 return;
910 }
911
912 texObj = _mesa_get_current_tex_object(ctx, target);
913 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
914
915 if (_mesa_is_zero_size_texture(texImage))
916 return;
917
918 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
919 _mesa_debug(ctx, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
920 " dstFmt=0x%x, dstType=0x%x\n",
921 texObj->Name,
922 _mesa_get_format_name(texImage->TexFormat),
923 texImage->Width, texImage->Height,
924 format, type);
925 }
926
927 _mesa_lock_texture(ctx, texObj);
928 {
929 ctx->Driver.GetTexImage(ctx, format, type, pixels, texImage);
930 }
931 _mesa_unlock_texture(ctx, texObj);
932 }
933
934
935 void GLAPIENTRY
936 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
937 GLenum type, GLvoid *pixels )
938 {
939 _mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
940 }
941
942
943 /**
944 * Do error checking for a glGetCompressedTexImage() call.
945 * \return GL_TRUE if any error, GL_FALSE if no errors.
946 */
947 static GLboolean
948 getcompressedteximage_error_check(struct gl_context *ctx, GLenum target,
949 GLint level, GLsizei clientMemSize, GLvoid *img)
950 {
951 struct gl_texture_object *texObj;
952 struct gl_texture_image *texImage;
953 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
954 GLuint compressedSize;
955
956 if (!legal_getteximage_target(ctx, target)) {
957 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImage(target=0x%x)",
958 target);
959 return GL_TRUE;
960 }
961
962 assert(maxLevels != 0);
963 if (level < 0 || level >= maxLevels) {
964 _mesa_error(ctx, GL_INVALID_VALUE,
965 "glGetCompressedTexImageARB(bad level = %d)", level);
966 return GL_TRUE;
967 }
968
969 texObj = _mesa_get_current_tex_object(ctx, target);
970 if (!texObj) {
971 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
972 return GL_TRUE;
973 }
974
975 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
976
977 if (!texImage) {
978 /* probably invalid mipmap level */
979 _mesa_error(ctx, GL_INVALID_VALUE,
980 "glGetCompressedTexImageARB(level)");
981 return GL_TRUE;
982 }
983
984 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
985 _mesa_error(ctx, GL_INVALID_OPERATION,
986 "glGetCompressedTexImageARB(texture is not compressed)");
987 return GL_TRUE;
988 }
989
990 compressedSize = _mesa_format_image_size(texImage->TexFormat,
991 texImage->Width,
992 texImage->Height,
993 texImage->Depth);
994
995 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
996 /* do bounds checking on writing to client memory */
997 if (clientMemSize < (GLsizei) compressedSize) {
998 _mesa_error(ctx, GL_INVALID_OPERATION,
999 "glGetnCompressedTexImageARB(out of bounds access:"
1000 " bufSize (%d) is too small)", clientMemSize);
1001 return GL_TRUE;
1002 }
1003 } else {
1004 /* do bounds checking on PBO write */
1005 if ((const GLubyte *) img + compressedSize >
1006 (const GLubyte *) ctx->Pack.BufferObj->Size) {
1007 _mesa_error(ctx, GL_INVALID_OPERATION,
1008 "glGetCompressedTexImage(out of bounds PBO access)");
1009 return GL_TRUE;
1010 }
1011
1012 /* make sure PBO is not mapped */
1013 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
1014 _mesa_error(ctx, GL_INVALID_OPERATION,
1015 "glGetCompressedTexImage(PBO is mapped)");
1016 return GL_TRUE;
1017 }
1018 }
1019
1020 return GL_FALSE;
1021 }
1022
1023
1024 void GLAPIENTRY
1025 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1026 GLvoid *img)
1027 {
1028 struct gl_texture_object *texObj;
1029 struct gl_texture_image *texImage;
1030 GET_CURRENT_CONTEXT(ctx);
1031
1032 FLUSH_VERTICES(ctx, 0);
1033
1034 if (getcompressedteximage_error_check(ctx, target, level, bufSize, img)) {
1035 return;
1036 }
1037
1038 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !img) {
1039 /* not an error, do nothing */
1040 return;
1041 }
1042
1043 texObj = _mesa_get_current_tex_object(ctx, target);
1044 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
1045
1046 if (_mesa_is_zero_size_texture(texImage))
1047 return;
1048
1049 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1050 _mesa_debug(ctx,
1051 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1052 texObj->Name,
1053 _mesa_get_format_name(texImage->TexFormat),
1054 texImage->Width, texImage->Height);
1055 }
1056
1057 _mesa_lock_texture(ctx, texObj);
1058 {
1059 ctx->Driver.GetCompressedTexImage(ctx, texImage, img);
1060 }
1061 _mesa_unlock_texture(ctx, texObj);
1062 }
1063
1064 void GLAPIENTRY
1065 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *img)
1066 {
1067 _mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
1068 }