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