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