mesa: fix GL_LUMINANCE handling in glGetTexImage
[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
250 ctx->Driver.MapTextureImage(ctx, texImage, 0,
251 0, 0, width, height,
252 GL_MAP_READ_BIT,
253 &srcMap, &srcRowStride);
254 if (srcMap) {
255 _mesa_decompress_image(texFormat, width, height,
256 srcMap, srcRowStride, tempImage);
257
258 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
259 }
260 else {
261 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
262 free(tempImage);
263 return;
264 }
265 }
266
267 if (baseFormat == GL_LUMINANCE ||
268 baseFormat == GL_LUMINANCE_ALPHA) {
269 _mesa_rebase_rgba_float(width * height, (GLfloat (*)[4]) tempImage,
270 baseFormat);
271 }
272
273 srcRow = tempImage;
274 for (row = 0; row < height; row++) {
275 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
276 width, height, format, type,
277 0, row, 0);
278
279 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) srcRow,
280 format, type, dest, &ctx->Pack, transferOps);
281 srcRow += width * 4;
282 }
283
284 free(tempImage);
285 }
286
287
288 /**
289 * Get an uncompressed color texture image.
290 */
291 static void
292 get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
293 GLenum format, GLenum type, GLvoid *pixels,
294 struct gl_texture_image *texImage,
295 GLbitfield transferOps)
296 {
297 /* don't want to apply sRGB -> RGB conversion here so override the format */
298 const gl_format texFormat =
299 _mesa_get_srgb_format_linear(texImage->TexFormat);
300 const GLuint width = texImage->Width;
301 const GLenum destBaseFormat = _mesa_base_tex_format(ctx, format);
302 GLenum rebaseFormat = GL_NONE;
303 GLuint height = texImage->Height;
304 GLuint depth = texImage->Depth;
305 GLuint img, row;
306 GLfloat (*rgba)[4];
307 GLuint (*rgba_uint)[4];
308 GLboolean is_integer = _mesa_is_format_integer_color(texImage->TexFormat);
309
310 /* Allocate buffer for one row of texels */
311 rgba = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat));
312 rgba_uint = (GLuint (*)[4]) rgba;
313 if (!rgba) {
314 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
315 return;
316 }
317
318 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
319 depth = height;
320 height = 1;
321 }
322
323 if (texImage->_BaseFormat == GL_LUMINANCE ||
324 texImage->_BaseFormat == GL_INTENSITY ||
325 texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
326 /* If a luminance (or intensity) texture is read back as RGB(A), the
327 * returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
328 * here to get G=B=0.
329 */
330 rebaseFormat = texImage->_BaseFormat;
331 }
332 else if ((texImage->_BaseFormat == GL_RGBA ||
333 texImage->_BaseFormat == GL_RGB) &&
334 (destBaseFormat == GL_LUMINANCE ||
335 destBaseFormat == GL_LUMINANCE_ALPHA ||
336 destBaseFormat == GL_LUMINANCE_INTEGER_EXT ||
337 destBaseFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT)) {
338 /* If we're reading back an RGB(A) texture as luminance then we need
339 * to return L=tex(R). Note, that's different from glReadPixels which
340 * returns L=R+G+B.
341 */
342 rebaseFormat = GL_LUMINANCE_ALPHA; /* this covers GL_LUMINANCE too */
343 }
344
345 for (img = 0; img < depth; img++) {
346 GLubyte *srcMap;
347 GLint rowstride;
348
349 /* map src texture buffer */
350 ctx->Driver.MapTextureImage(ctx, texImage, img,
351 0, 0, width, height, GL_MAP_READ_BIT,
352 &srcMap, &rowstride);
353 if (srcMap) {
354 for (row = 0; row < height; row++) {
355 const GLubyte *src = srcMap + row * rowstride;
356 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
357 width, height, format, type,
358 img, row, 0);
359
360 if (is_integer) {
361 _mesa_unpack_uint_rgba_row(texFormat, width, src, rgba_uint);
362 if (rebaseFormat)
363 _mesa_rebase_rgba_uint(width, rgba_uint, rebaseFormat);
364 _mesa_pack_rgba_span_int(ctx, width, rgba_uint,
365 format, type, dest);
366 } else {
367 _mesa_unpack_rgba_row(texFormat, width, src, rgba);
368 if (rebaseFormat)
369 _mesa_rebase_rgba_float(width, rgba, rebaseFormat);
370 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
371 format, type, dest,
372 &ctx->Pack, transferOps);
373 }
374 }
375
376 /* Unmap the src texture buffer */
377 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
378 }
379 else {
380 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
381 break;
382 }
383 }
384
385 free(rgba);
386 }
387
388
389 /**
390 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
391 * Compressed textures are handled here as well.
392 */
393 static void
394 get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
395 GLenum format, GLenum type, GLvoid *pixels,
396 struct gl_texture_image *texImage)
397 {
398 const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
399 GLbitfield transferOps = 0x0;
400
401 /* In general, clamping does not apply to glGetTexImage, except when
402 * the returned type of the image can't hold negative values.
403 */
404 if (type_needs_clamping(type)) {
405 /* the returned image type can't have negative values */
406 if (dataType == GL_FLOAT ||
407 dataType == GL_SIGNED_NORMALIZED ||
408 format == GL_LUMINANCE ||
409 format == GL_LUMINANCE_ALPHA) {
410 transferOps |= IMAGE_CLAMP_BIT;
411 }
412 }
413 /* This applies to RGB, RGBA textures. if the format is either LUMINANCE
414 * or LUMINANCE ALPHA, luminance (L) is computed as L=R+G+B .we need to
415 * clamp the sum to [0,1].
416 */
417 else if ((format == GL_LUMINANCE ||
418 format == GL_LUMINANCE_ALPHA) &&
419 dataType == GL_UNSIGNED_NORMALIZED) {
420 transferOps |= IMAGE_CLAMP_BIT;
421 }
422
423 if (_mesa_is_format_compressed(texImage->TexFormat)) {
424 get_tex_rgba_compressed(ctx, dimensions, format, type,
425 pixels, texImage, transferOps);
426 }
427 else {
428 get_tex_rgba_uncompressed(ctx, dimensions, format, type,
429 pixels, texImage, transferOps);
430 }
431 }
432
433
434 /**
435 * Try to do glGetTexImage() with simple memcpy().
436 * \return GL_TRUE if done, GL_FALSE otherwise
437 */
438 static GLboolean
439 get_tex_memcpy(struct gl_context *ctx, GLenum format, GLenum type,
440 GLvoid *pixels,
441 struct gl_texture_image *texImage)
442 {
443 const GLenum target = texImage->TexObject->Target;
444 GLboolean memCopy = GL_FALSE;
445
446 /*
447 * Check if we can use memcpy to copy from the hardware texture
448 * format to the user's format/type.
449 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
450 */
451 if (target == GL_TEXTURE_1D ||
452 target == GL_TEXTURE_2D ||
453 target == GL_TEXTURE_RECTANGLE ||
454 _mesa_is_cube_face(target)) {
455 memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
456 format, type,
457 ctx->Pack.SwapBytes);
458 }
459
460 if (memCopy) {
461 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
462 const GLuint bytesPerRow = texImage->Width * bpp;
463 GLubyte *dst =
464 _mesa_image_address2d(&ctx->Pack, pixels, texImage->Width,
465 texImage->Height, format, type, 0, 0);
466 const GLint dstRowStride =
467 _mesa_image_row_stride(&ctx->Pack, texImage->Width, format, type);
468 GLubyte *src;
469 GLint srcRowStride;
470
471 /* map src texture buffer */
472 ctx->Driver.MapTextureImage(ctx, texImage, 0,
473 0, 0, texImage->Width, texImage->Height,
474 GL_MAP_READ_BIT, &src, &srcRowStride);
475
476 if (src) {
477 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
478 memcpy(dst, src, bytesPerRow * texImage->Height);
479 }
480 else {
481 GLuint row;
482 for (row = 0; row < texImage->Height; row++) {
483 memcpy(dst, src, bytesPerRow);
484 dst += dstRowStride;
485 src += srcRowStride;
486 }
487 }
488
489 /* unmap src texture buffer */
490 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
491 }
492 else {
493 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
494 }
495 }
496
497 return memCopy;
498 }
499
500
501 /**
502 * This is the software fallback for Driver.GetTexImage().
503 * All error checking will have been done before this routine is called.
504 * We'll call ctx->Driver.MapTextureImage() to access the data, then
505 * unmap with ctx->Driver.UnmapTextureImage().
506 */
507 void
508 _mesa_get_teximage(struct gl_context *ctx,
509 GLenum format, GLenum type, GLvoid *pixels,
510 struct gl_texture_image *texImage)
511 {
512 GLuint dimensions;
513
514 switch (texImage->TexObject->Target) {
515 case GL_TEXTURE_1D:
516 dimensions = 1;
517 break;
518 case GL_TEXTURE_3D:
519 dimensions = 3;
520 break;
521 default:
522 dimensions = 2;
523 }
524
525 /* map dest buffer, if PBO */
526 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
527 /* Packing texture image into a PBO.
528 * Map the (potentially) VRAM-based buffer into our process space so
529 * we can write into it with the code below.
530 * A hardware driver might use a sophisticated blit to move the
531 * texture data to the PBO if the PBO is in VRAM along with the texture.
532 */
533 GLubyte *buf = (GLubyte *)
534 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
535 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
536 if (!buf) {
537 /* out of memory or other unexpected error */
538 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
539 return;
540 }
541 /* <pixels> was an offset into the PBO.
542 * Now make it a real, client-side pointer inside the mapped region.
543 */
544 pixels = ADD_POINTERS(buf, pixels);
545 }
546
547 if (get_tex_memcpy(ctx, format, type, pixels, texImage)) {
548 /* all done */
549 }
550 else if (format == GL_DEPTH_COMPONENT) {
551 get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
552 }
553 else if (format == GL_DEPTH_STENCIL_EXT) {
554 get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
555 }
556 else if (format == GL_YCBCR_MESA) {
557 get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
558 }
559 else {
560 get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
561 }
562
563 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
564 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
565 }
566 }
567
568
569
570 /**
571 * This is the software fallback for Driver.GetCompressedTexImage().
572 * All error checking will have been done before this routine is called.
573 */
574 void
575 _mesa_get_compressed_teximage(struct gl_context *ctx,
576 struct gl_texture_image *texImage,
577 GLvoid *img)
578 {
579 const GLuint row_stride =
580 _mesa_format_row_stride(texImage->TexFormat, texImage->Width);
581 GLuint i;
582 GLubyte *src;
583 GLint srcRowStride;
584
585 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
586 /* pack texture image into a PBO */
587 GLubyte *buf = (GLubyte *)
588 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
589 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
590 if (!buf) {
591 /* out of memory or other unexpected error */
592 _mesa_error(ctx, GL_OUT_OF_MEMORY,
593 "glGetCompresssedTexImage(map PBO failed)");
594 return;
595 }
596 img = ADD_POINTERS(buf, img);
597 }
598
599 /* map src texture buffer */
600 ctx->Driver.MapTextureImage(ctx, texImage, 0,
601 0, 0, texImage->Width, texImage->Height,
602 GL_MAP_READ_BIT, &src, &srcRowStride);
603
604 if (src) {
605 /* no pixelstore or pixel transfer, but respect stride */
606
607 if (row_stride == srcRowStride) {
608 const GLuint size = _mesa_format_image_size(texImage->TexFormat,
609 texImage->Width,
610 texImage->Height,
611 texImage->Depth);
612 memcpy(img, src, size);
613 }
614 else {
615 GLuint bw, bh;
616 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
617 for (i = 0; i < (texImage->Height + bh - 1) / bh; i++) {
618 memcpy((GLubyte *)img + i * row_stride,
619 (GLubyte *)src + i * srcRowStride,
620 row_stride);
621 }
622 }
623
624 ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
625 }
626 else {
627 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
628 }
629
630 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
631 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
632 }
633 }
634
635
636
637 /**
638 * Do error checking for a glGetTexImage() call.
639 * \return GL_TRUE if any error, GL_FALSE if no errors.
640 */
641 static GLboolean
642 getteximage_error_check(struct gl_context *ctx, GLenum target, GLint level,
643 GLenum format, GLenum type, GLsizei clientMemSize,
644 GLvoid *pixels )
645 {
646 struct gl_texture_object *texObj;
647 struct gl_texture_image *texImage;
648 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
649 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
650 GLenum baseFormat, err;
651
652 if (maxLevels == 0) {
653 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target=0x%x)", target);
654 return GL_TRUE;
655 }
656
657 if (level < 0 || level >= maxLevels) {
658 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
659 return GL_TRUE;
660 }
661
662 err = _mesa_error_check_format_and_type(ctx, format, type);
663 if (err != GL_NO_ERROR) {
664 _mesa_error(ctx, err, "glGetTexImage(format/type)");
665 return GL_TRUE;
666 }
667
668 texObj = _mesa_get_current_tex_object(ctx, target);
669
670 if (!texObj || _mesa_is_proxy_texture(target)) {
671 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
672 return GL_TRUE;
673 }
674
675 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
676 if (!texImage) {
677 /* non-existant texture image */
678 return GL_TRUE;
679 }
680
681 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
682
683 /* Make sure the requested image format is compatible with the
684 * texture's format.
685 */
686 if (_mesa_is_color_format(format)
687 && !_mesa_is_color_format(baseFormat)) {
688 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
689 return GL_TRUE;
690 }
691 else if (_mesa_is_depth_format(format)
692 && !_mesa_is_depth_format(baseFormat)
693 && !_mesa_is_depthstencil_format(baseFormat)) {
694 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
695 return GL_TRUE;
696 }
697 else if (_mesa_is_ycbcr_format(format)
698 && !_mesa_is_ycbcr_format(baseFormat)) {
699 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
700 return GL_TRUE;
701 }
702 else if (_mesa_is_depthstencil_format(format)
703 && !_mesa_is_depthstencil_format(baseFormat)) {
704 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
705 return GL_TRUE;
706 }
707 else if (_mesa_is_dudv_format(format)
708 && !_mesa_is_dudv_format(baseFormat)) {
709 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
710 return GL_TRUE;
711 }
712
713 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
714 texImage->Height, texImage->Depth,
715 format, type, clientMemSize, pixels)) {
716 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
717 _mesa_error(ctx, GL_INVALID_OPERATION,
718 "glGetTexImage(out of bounds PBO access)");
719 } else {
720 _mesa_error(ctx, GL_INVALID_OPERATION,
721 "glGetnTexImageARB(out of bounds access:"
722 " bufSize (%d) is too small)", clientMemSize);
723 }
724 return GL_TRUE;
725 }
726
727 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
728 /* PBO should not be mapped */
729 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
730 _mesa_error(ctx, GL_INVALID_OPERATION,
731 "glGetTexImage(PBO is mapped)");
732 return GL_TRUE;
733 }
734 }
735
736 return GL_FALSE;
737 }
738
739
740
741 /**
742 * Get texture image. Called by glGetTexImage.
743 *
744 * \param target texture target.
745 * \param level image level.
746 * \param format pixel data format for returned image.
747 * \param type pixel data type for returned image.
748 * \param bufSize size of the pixels data buffer.
749 * \param pixels returned pixel data.
750 */
751 void GLAPIENTRY
752 _mesa_GetnTexImageARB( GLenum target, GLint level, GLenum format,
753 GLenum type, GLsizei bufSize, GLvoid *pixels )
754 {
755 struct gl_texture_object *texObj;
756 struct gl_texture_image *texImage;
757 GET_CURRENT_CONTEXT(ctx);
758 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
759
760 if (getteximage_error_check(ctx, target, level, format, type,
761 bufSize, pixels)) {
762 return;
763 }
764
765 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
766 /* not an error, do nothing */
767 return;
768 }
769
770 texObj = _mesa_get_current_tex_object(ctx, target);
771 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
772
773 if (_mesa_is_zero_size_texture(texImage))
774 return;
775
776 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
777 _mesa_debug(ctx, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
778 " dstFmt=0x%x, dstType=0x%x\n",
779 texObj->Name,
780 _mesa_get_format_name(texImage->TexFormat),
781 texImage->Width, texImage->Height,
782 format, type);
783 }
784
785 _mesa_lock_texture(ctx, texObj);
786 {
787 ctx->Driver.GetTexImage(ctx, format, type, pixels, texImage);
788 }
789 _mesa_unlock_texture(ctx, texObj);
790 }
791
792
793 void GLAPIENTRY
794 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
795 GLenum type, GLvoid *pixels )
796 {
797 _mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
798 }
799
800
801 /**
802 * Do error checking for a glGetCompressedTexImage() call.
803 * \return GL_TRUE if any error, GL_FALSE if no errors.
804 */
805 static GLboolean
806 getcompressedteximage_error_check(struct gl_context *ctx, GLenum target,
807 GLint level, GLsizei clientMemSize, GLvoid *img)
808 {
809 struct gl_texture_object *texObj;
810 struct gl_texture_image *texImage;
811 const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
812 GLuint compressedSize;
813
814 if (maxLevels == 0) {
815 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImage(target=0x%x)",
816 target);
817 return GL_TRUE;
818 }
819
820 if (level < 0 || level >= maxLevels) {
821 _mesa_error(ctx, GL_INVALID_VALUE,
822 "glGetCompressedTexImageARB(bad level = %d)", level);
823 return GL_TRUE;
824 }
825
826 if (_mesa_is_proxy_texture(target)) {
827 _mesa_error(ctx, GL_INVALID_ENUM,
828 "glGetCompressedTexImageARB(bad target = %s)",
829 _mesa_lookup_enum_by_nr(target));
830 return GL_TRUE;
831 }
832
833 texObj = _mesa_get_current_tex_object(ctx, target);
834 if (!texObj) {
835 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
836 return GL_TRUE;
837 }
838
839 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
840
841 if (!texImage) {
842 /* probably invalid mipmap level */
843 _mesa_error(ctx, GL_INVALID_VALUE,
844 "glGetCompressedTexImageARB(level)");
845 return GL_TRUE;
846 }
847
848 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
849 _mesa_error(ctx, GL_INVALID_OPERATION,
850 "glGetCompressedTexImageARB(texture is not compressed)");
851 return GL_TRUE;
852 }
853
854 compressedSize = _mesa_format_image_size(texImage->TexFormat,
855 texImage->Width,
856 texImage->Height,
857 texImage->Depth);
858
859 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
860 /* do bounds checking on writing to client memory */
861 if (clientMemSize < compressedSize) {
862 _mesa_error(ctx, GL_INVALID_OPERATION,
863 "glGetnCompressedTexImageARB(out of bounds access:"
864 " bufSize (%d) is too small)", clientMemSize);
865 return GL_TRUE;
866 }
867 } else {
868 /* do bounds checking on PBO write */
869 if ((const GLubyte *) img + compressedSize >
870 (const GLubyte *) ctx->Pack.BufferObj->Size) {
871 _mesa_error(ctx, GL_INVALID_OPERATION,
872 "glGetCompressedTexImage(out of bounds PBO access)");
873 return GL_TRUE;
874 }
875
876 /* make sure PBO is not mapped */
877 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
878 _mesa_error(ctx, GL_INVALID_OPERATION,
879 "glGetCompressedTexImage(PBO is mapped)");
880 return GL_TRUE;
881 }
882 }
883
884 return GL_FALSE;
885 }
886
887
888 void GLAPIENTRY
889 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
890 GLvoid *img)
891 {
892 struct gl_texture_object *texObj;
893 struct gl_texture_image *texImage;
894 GET_CURRENT_CONTEXT(ctx);
895 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
896
897 if (getcompressedteximage_error_check(ctx, target, level, bufSize, img)) {
898 return;
899 }
900
901 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !img) {
902 /* not an error, do nothing */
903 return;
904 }
905
906 texObj = _mesa_get_current_tex_object(ctx, target);
907 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
908
909 if (_mesa_is_zero_size_texture(texImage))
910 return;
911
912 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
913 _mesa_debug(ctx,
914 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
915 texObj->Name,
916 _mesa_get_format_name(texImage->TexFormat),
917 texImage->Width, texImage->Height);
918 }
919
920 _mesa_lock_texture(ctx, texObj);
921 {
922 ctx->Driver.GetCompressedTexImage(ctx, texImage, img);
923 }
924 _mesa_unlock_texture(ctx, texObj);
925 }
926
927 void GLAPIENTRY
928 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
929 {
930 _mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
931 }