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