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