031e04d472c501238072da0a593f728a25244dff
[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
371 /* Allocate buffer for one row of texels */
372 rgba = malloc(4 * width * sizeof(GLfloat));
373 rgba_uint = (GLuint (*)[4]) rgba;
374 if (!rgba) {
375 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
376 return;
377 }
378
379 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
380 depth = height;
381 height = 1;
382 }
383
384 if (texImage->_BaseFormat == GL_LUMINANCE ||
385 texImage->_BaseFormat == GL_INTENSITY ||
386 texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
387 /* If a luminance (or intensity) texture is read back as RGB(A), the
388 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
389 * here to get G=B=0.
390 */
391 rebaseFormat = texImage->_BaseFormat;
392 }
393 else if ((texImage->_BaseFormat == GL_RGBA ||
394 texImage->_BaseFormat == GL_RGB ||
395 texImage->_BaseFormat == GL_RG) &&
396 (destBaseFormat == GL_LUMINANCE ||
397 destBaseFormat == GL_LUMINANCE_ALPHA ||
398 destBaseFormat == GL_LUMINANCE_INTEGER_EXT ||
399 destBaseFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT)) {
400 /* If we're reading back an RGB(A) texture as luminance then we need
401 * to return L=tex(R). Note, that's different from glReadPixels which
402 * returns L=R+G+B.
403 */
404 rebaseFormat = GL_LUMINANCE_ALPHA; /* this covers GL_LUMINANCE too */
405 }
406
407 for (img = 0; img < depth; img++) {
408 GLubyte *srcMap;
409 GLint rowstride;
410
411 /* map src texture buffer */
412 ctx->Driver.MapTextureImage(ctx, texImage, img,
413 0, 0, width, height, GL_MAP_READ_BIT,
414 &srcMap, &rowstride);
415 if (srcMap) {
416 for (row = 0; row < height; row++) {
417 const GLubyte *src = srcMap + row * rowstride;
418 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
419 width, height, format, type,
420 img, row, 0);
421
422 if (tex_is_integer) {
423 _mesa_unpack_uint_rgba_row(texFormat, width, src, rgba_uint);
424 if (rebaseFormat)
425 _mesa_rebase_rgba_uint(width, rgba_uint, rebaseFormat);
426 if (tex_is_uint) {
427 _mesa_pack_rgba_span_from_uints(ctx, width,
428 (GLuint (*)[4]) rgba_uint,
429 format, type, dest);
430 } else {
431 _mesa_pack_rgba_span_from_ints(ctx, width,
432 (GLint (*)[4]) rgba_uint,
433 format, type, dest);
434 }
435 } else {
436 _mesa_unpack_rgba_row(texFormat, width, src, rgba);
437 if (rebaseFormat)
438 _mesa_rebase_rgba_float(width, rgba, rebaseFormat);
439 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
440 format, type, dest,
441 &ctx->Pack, transferOps);
442 }
443 }
444
445 /* Unmap the src texture buffer */
446 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
447 }
448 else {
449 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
450 break;
451 }
452 }
453
454 free(rgba);
455 }
456
457
458 /**
459 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
460 * Compressed textures are handled here as well.
461 */
462 static void
463 get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
464 GLenum format, GLenum type, GLvoid *pixels,
465 struct gl_texture_image *texImage)
466 {
467 const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
468 GLbitfield transferOps = 0x0;
469
470 /* In general, clamping does not apply to glGetTexImage, except when
471 * the returned type of the image can't hold negative values.
472 */
473 if (type_needs_clamping(type)) {
474 /* the returned image type can't have negative values */
475 if (dataType == GL_FLOAT ||
476 dataType == GL_SIGNED_NORMALIZED ||
477 format == GL_LUMINANCE ||
478 format == GL_LUMINANCE_ALPHA) {
479 transferOps |= IMAGE_CLAMP_BIT;
480 }
481 }
482
483 if (_mesa_is_format_compressed(texImage->TexFormat)) {
484 get_tex_rgba_compressed(ctx, dimensions, format, type,
485 pixels, texImage, transferOps);
486 }
487 else {
488 get_tex_rgba_uncompressed(ctx, dimensions, format, type,
489 pixels, texImage, transferOps);
490 }
491 }
492
493
494 /**
495 * Try to do glGetTexImage() with simple memcpy().
496 * \return GL_TRUE if done, GL_FALSE otherwise
497 */
498 static GLboolean
499 get_tex_memcpy(struct gl_context *ctx, GLenum format, GLenum type,
500 GLvoid *pixels,
501 struct gl_texture_image *texImage)
502 {
503 const GLenum target = texImage->TexObject->Target;
504 GLboolean memCopy = GL_FALSE;
505
506 /*
507 * Check if we can use memcpy to copy from the hardware texture
508 * format to the user's format/type.
509 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
510 */
511 if (target == GL_TEXTURE_1D ||
512 target == GL_TEXTURE_2D ||
513 target == GL_TEXTURE_RECTANGLE ||
514 _mesa_is_cube_face(target)) {
515 memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
516 format, type,
517 ctx->Pack.SwapBytes);
518 }
519
520 if (memCopy) {
521 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
522 const GLuint bytesPerRow = texImage->Width * bpp;
523 GLubyte *dst =
524 _mesa_image_address2d(&ctx->Pack, pixels, texImage->Width,
525 texImage->Height, format, type, 0, 0);
526 const GLint dstRowStride =
527 _mesa_image_row_stride(&ctx->Pack, texImage->Width, format, type);
528 GLubyte *src;
529 GLint srcRowStride;
530
531 /* map src texture buffer */
532 ctx->Driver.MapTextureImage(ctx, texImage, 0,
533 0, 0, texImage->Width, texImage->Height,
534 GL_MAP_READ_BIT, &src, &srcRowStride);
535
536 if (src) {
537 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
538 memcpy(dst, src, bytesPerRow * texImage->Height);
539 }
540 else {
541 GLuint row;
542 for (row = 0; row < texImage->Height; row++) {
543 memcpy(dst, src, bytesPerRow);
544 dst += dstRowStride;
545 src += srcRowStride;
546 }
547 }
548
549 /* unmap src texture buffer */
550 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
551 }
552 else {
553 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
554 }
555 }
556
557 return memCopy;
558 }
559
560
561 /**
562 * This is the software fallback for Driver.GetTexImage().
563 * All error checking will have been done before this routine is called.
564 * We'll call ctx->Driver.MapTextureImage() to access the data, then
565 * unmap with ctx->Driver.UnmapTextureImage().
566 */
567 void
568 _mesa_get_teximage(struct gl_context *ctx,
569 GLenum format, GLenum type, GLvoid *pixels,
570 struct gl_texture_image *texImage)
571 {
572 GLuint dimensions;
573
574 switch (texImage->TexObject->Target) {
575 case GL_TEXTURE_1D:
576 dimensions = 1;
577 break;
578 case GL_TEXTURE_3D:
579 dimensions = 3;
580 break;
581 default:
582 dimensions = 2;
583 }
584
585 /* map dest buffer, if PBO */
586 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
587 /* Packing texture image into a PBO.
588 * Map the (potentially) VRAM-based buffer into our process space so
589 * we can write into it with the code below.
590 * A hardware driver might use a sophisticated blit to move the
591 * texture data to the PBO if the PBO is in VRAM along with the texture.
592 */
593 GLubyte *buf = (GLubyte *)
594 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
595 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
596 if (!buf) {
597 /* out of memory or other unexpected error */
598 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
599 return;
600 }
601 /* <pixels> was an offset into the PBO.
602 * Now make it a real, client-side pointer inside the mapped region.
603 */
604 pixels = ADD_POINTERS(buf, pixels);
605 }
606
607 if (get_tex_memcpy(ctx, format, type, pixels, texImage)) {
608 /* all done */
609 }
610 else if (format == GL_DEPTH_COMPONENT) {
611 get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
612 }
613 else if (format == GL_DEPTH_STENCIL_EXT) {
614 get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
615 }
616 else if (format == GL_YCBCR_MESA) {
617 get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
618 }
619 else {
620 get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
621 }
622
623 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
624 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
625 }
626 }
627
628
629
630 /**
631 * This is the software fallback for Driver.GetCompressedTexImage().
632 * All error checking will have been done before this routine is called.
633 */
634 void
635 _mesa_get_compressed_teximage(struct gl_context *ctx,
636 struct gl_texture_image *texImage,
637 GLvoid *img)
638 {
639 const GLuint row_stride =
640 _mesa_format_row_stride(texImage->TexFormat, texImage->Width);
641 GLuint i;
642 GLubyte *src;
643 GLint srcRowStride;
644
645 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
646 /* pack texture image into a PBO */
647 GLubyte *buf = (GLubyte *)
648 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
649 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
650 if (!buf) {
651 /* out of memory or other unexpected error */
652 _mesa_error(ctx, GL_OUT_OF_MEMORY,
653 "glGetCompresssedTexImage(map PBO failed)");
654 return;
655 }
656 img = ADD_POINTERS(buf, img);
657 }
658
659 /* map src texture buffer */
660 ctx->Driver.MapTextureImage(ctx, texImage, 0,
661 0, 0, texImage->Width, texImage->Height,
662 GL_MAP_READ_BIT, &src, &srcRowStride);
663
664 if (src) {
665 /* no pixelstore or pixel transfer, but respect stride */
666
667 if (row_stride == srcRowStride) {
668 const GLuint size = _mesa_format_image_size(texImage->TexFormat,
669 texImage->Width,
670 texImage->Height,
671 texImage->Depth);
672 memcpy(img, src, size);
673 }
674 else {
675 GLuint bw, bh;
676 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
677 for (i = 0; i < (texImage->Height + bh - 1) / bh; i++) {
678 memcpy((GLubyte *)img + i * row_stride,
679 (GLubyte *)src + i * srcRowStride,
680 row_stride);
681 }
682 }
683
684 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
685 }
686 else {
687 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
688 }
689
690 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
691 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
692 }
693 }
694
695
696 /**
697 * Validate the texture target enum supplied to glTexImage or
698 * glCompressedTexImage.
699 */
700 static GLboolean
701 legal_getteximage_target(struct gl_context *ctx, GLenum target)
702 {
703 switch (target) {
704 case GL_TEXTURE_1D:
705 case GL_TEXTURE_2D:
706 case GL_TEXTURE_3D:
707 return GL_TRUE;
708 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
709 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
710 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
711 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
712 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
713 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
714 return ctx->Extensions.ARB_texture_cube_map;
715 case GL_TEXTURE_RECTANGLE_NV:
716 return ctx->Extensions.NV_texture_rectangle;
717 case GL_TEXTURE_1D_ARRAY_EXT:
718 case GL_TEXTURE_2D_ARRAY_EXT:
719 return (ctx->Extensions.MESA_texture_array ||
720 ctx->Extensions.EXT_texture_array);
721 case GL_TEXTURE_CUBE_MAP_ARRAY:
722 return ctx->Extensions.ARB_texture_cube_map_array;
723 default:
724 return GL_FALSE;
725 }
726 }
727
728
729 /**
730 * Do error checking for a glGetTexImage() call.
731 * \return GL_TRUE if any error, GL_FALSE if no errors.
732 */
733 static GLboolean
734 getteximage_error_check(struct gl_context *ctx, GLenum target, GLint level,
735 GLenum format, GLenum type, GLsizei clientMemSize,
736 GLvoid *pixels )
737 {
738 struct gl_texture_object *texObj;
739 struct gl_texture_image *texImage;
740 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
741 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
742 GLenum baseFormat, err;
743
744 if (!legal_getteximage_target(ctx, target)) {
745 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target=0x%x)", target);
746 return GL_TRUE;
747 }
748
749 assert(maxLevels != 0);
750 if (level < 0 || level >= maxLevels) {
751 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
752 return GL_TRUE;
753 }
754
755 err = _mesa_error_check_format_and_type(ctx, format, type);
756 if (err != GL_NO_ERROR) {
757 _mesa_error(ctx, err, "glGetTexImage(format/type)");
758 return GL_TRUE;
759 }
760
761 texObj = _mesa_get_current_tex_object(ctx, target);
762
763 if (!texObj) {
764 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
765 return GL_TRUE;
766 }
767
768 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
769 if (!texImage) {
770 /* non-existant texture image */
771 return GL_TRUE;
772 }
773
774 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
775
776 /* Make sure the requested image format is compatible with the
777 * texture's format.
778 */
779 if (_mesa_is_color_format(format)
780 && !_mesa_is_color_format(baseFormat)) {
781 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
782 return GL_TRUE;
783 }
784 else if (_mesa_is_depth_format(format)
785 && !_mesa_is_depth_format(baseFormat)
786 && !_mesa_is_depthstencil_format(baseFormat)) {
787 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
788 return GL_TRUE;
789 }
790 else if (_mesa_is_ycbcr_format(format)
791 && !_mesa_is_ycbcr_format(baseFormat)) {
792 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
793 return GL_TRUE;
794 }
795 else if (_mesa_is_depthstencil_format(format)
796 && !_mesa_is_depthstencil_format(baseFormat)) {
797 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
798 return GL_TRUE;
799 }
800 else if (_mesa_is_dudv_format(format)
801 && !_mesa_is_dudv_format(baseFormat)) {
802 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
803 return GL_TRUE;
804 }
805
806 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
807 texImage->Height, texImage->Depth,
808 format, type, clientMemSize, pixels)) {
809 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
810 _mesa_error(ctx, GL_INVALID_OPERATION,
811 "glGetTexImage(out of bounds PBO access)");
812 } else {
813 _mesa_error(ctx, GL_INVALID_OPERATION,
814 "glGetnTexImageARB(out of bounds access:"
815 " bufSize (%d) is too small)", clientMemSize);
816 }
817 return GL_TRUE;
818 }
819
820 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
821 /* PBO should not be mapped */
822 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
823 _mesa_error(ctx, GL_INVALID_OPERATION,
824 "glGetTexImage(PBO is mapped)");
825 return GL_TRUE;
826 }
827 }
828
829 return GL_FALSE;
830 }
831
832
833
834 /**
835 * Get texture image. Called by glGetTexImage.
836 *
837 * \param target texture target.
838 * \param level image level.
839 * \param format pixel data format for returned image.
840 * \param type pixel data type for returned image.
841 * \param bufSize size of the pixels data buffer.
842 * \param pixels returned pixel data.
843 */
844 void GLAPIENTRY
845 _mesa_GetnTexImageARB( GLenum target, GLint level, GLenum format,
846 GLenum type, GLsizei bufSize, GLvoid *pixels )
847 {
848 struct gl_texture_object *texObj;
849 struct gl_texture_image *texImage;
850 GET_CURRENT_CONTEXT(ctx);
851
852 FLUSH_VERTICES(ctx, 0);
853
854 if (getteximage_error_check(ctx, target, level, format, type,
855 bufSize, pixels)) {
856 return;
857 }
858
859 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
860 /* not an error, do nothing */
861 return;
862 }
863
864 texObj = _mesa_get_current_tex_object(ctx, target);
865 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
866
867 if (_mesa_is_zero_size_texture(texImage))
868 return;
869
870 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
871 _mesa_debug(ctx, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
872 " dstFmt=0x%x, dstType=0x%x\n",
873 texObj->Name,
874 _mesa_get_format_name(texImage->TexFormat),
875 texImage->Width, texImage->Height,
876 format, type);
877 }
878
879 _mesa_lock_texture(ctx, texObj);
880 {
881 ctx->Driver.GetTexImage(ctx, format, type, pixels, texImage);
882 }
883 _mesa_unlock_texture(ctx, texObj);
884 }
885
886
887 void GLAPIENTRY
888 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
889 GLenum type, GLvoid *pixels )
890 {
891 _mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
892 }
893
894
895 /**
896 * Do error checking for a glGetCompressedTexImage() call.
897 * \return GL_TRUE if any error, GL_FALSE if no errors.
898 */
899 static GLboolean
900 getcompressedteximage_error_check(struct gl_context *ctx, GLenum target,
901 GLint level, GLsizei clientMemSize, GLvoid *img)
902 {
903 struct gl_texture_object *texObj;
904 struct gl_texture_image *texImage;
905 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
906 GLuint compressedSize;
907
908 if (!legal_getteximage_target(ctx, target)) {
909 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImage(target=0x%x)",
910 target);
911 return GL_TRUE;
912 }
913
914 assert(maxLevels != 0);
915 if (level < 0 || level >= maxLevels) {
916 _mesa_error(ctx, GL_INVALID_VALUE,
917 "glGetCompressedTexImageARB(bad level = %d)", level);
918 return GL_TRUE;
919 }
920
921 texObj = _mesa_get_current_tex_object(ctx, target);
922 if (!texObj) {
923 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
924 return GL_TRUE;
925 }
926
927 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
928
929 if (!texImage) {
930 /* probably invalid mipmap level */
931 _mesa_error(ctx, GL_INVALID_VALUE,
932 "glGetCompressedTexImageARB(level)");
933 return GL_TRUE;
934 }
935
936 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
937 _mesa_error(ctx, GL_INVALID_OPERATION,
938 "glGetCompressedTexImageARB(texture is not compressed)");
939 return GL_TRUE;
940 }
941
942 compressedSize = _mesa_format_image_size(texImage->TexFormat,
943 texImage->Width,
944 texImage->Height,
945 texImage->Depth);
946
947 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
948 /* do bounds checking on writing to client memory */
949 if (clientMemSize < (GLsizei) compressedSize) {
950 _mesa_error(ctx, GL_INVALID_OPERATION,
951 "glGetnCompressedTexImageARB(out of bounds access:"
952 " bufSize (%d) is too small)", clientMemSize);
953 return GL_TRUE;
954 }
955 } else {
956 /* do bounds checking on PBO write */
957 if ((const GLubyte *) img + compressedSize >
958 (const GLubyte *) ctx->Pack.BufferObj->Size) {
959 _mesa_error(ctx, GL_INVALID_OPERATION,
960 "glGetCompressedTexImage(out of bounds PBO access)");
961 return GL_TRUE;
962 }
963
964 /* make sure PBO is not mapped */
965 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
966 _mesa_error(ctx, GL_INVALID_OPERATION,
967 "glGetCompressedTexImage(PBO is mapped)");
968 return GL_TRUE;
969 }
970 }
971
972 return GL_FALSE;
973 }
974
975
976 void GLAPIENTRY
977 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
978 GLvoid *img)
979 {
980 struct gl_texture_object *texObj;
981 struct gl_texture_image *texImage;
982 GET_CURRENT_CONTEXT(ctx);
983
984 FLUSH_VERTICES(ctx, 0);
985
986 if (getcompressedteximage_error_check(ctx, target, level, bufSize, img)) {
987 return;
988 }
989
990 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !img) {
991 /* not an error, do nothing */
992 return;
993 }
994
995 texObj = _mesa_get_current_tex_object(ctx, target);
996 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
997
998 if (_mesa_is_zero_size_texture(texImage))
999 return;
1000
1001 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1002 _mesa_debug(ctx,
1003 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
1004 texObj->Name,
1005 _mesa_get_format_name(texImage->TexFormat),
1006 texImage->Width, texImage->Height);
1007 }
1008
1009 _mesa_lock_texture(ctx, texObj);
1010 {
1011 ctx->Driver.GetCompressedTexImage(ctx, texImage, img);
1012 }
1013 _mesa_unlock_texture(ctx, texObj);
1014 }
1015
1016 void GLAPIENTRY
1017 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *img)
1018 {
1019 _mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
1020 }