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