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