Merge branch 'master' into opengl-es-v2
[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] = { 0 };
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 row_stride = _mesa_format_row_stride(texImage->TexFormat,
539 texImage->Width);
540 const GLuint row_stride_stored = _mesa_format_row_stride(texImage->TexFormat,
541 texImage->RowStride);
542 GLuint i;
543
544 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
545 /* pack texture image into a PBO */
546 GLubyte *buf = (GLubyte *)
547 ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
548 GL_WRITE_ONLY_ARB, ctx->Pack.BufferObj);
549 if (!buf) {
550 /* out of memory or other unexpected error */
551 _mesa_error(ctx, GL_OUT_OF_MEMORY,
552 "glGetCompresssedTexImage(map PBO failed)");
553 return;
554 }
555 img = ADD_POINTERS(buf, img);
556 }
557
558 /* no pixelstore or pixel transfer, but respect stride */
559
560 if (row_stride == row_stride_stored) {
561 const GLuint size = _mesa_format_image_size(texImage->TexFormat,
562 texImage->Width,
563 texImage->Height,
564 texImage->Depth);
565 _mesa_memcpy(img, texImage->Data, size);
566 }
567 else {
568 GLuint bw, bh;
569 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
570 for (i = 0; i < (texImage->Height + bh - 1) / bh; i++) {
571 memcpy((GLubyte *)img + i * row_stride,
572 (GLubyte *)texImage->Data + i * row_stride_stored,
573 row_stride);
574 }
575 }
576
577 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
578 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
579 ctx->Pack.BufferObj);
580 }
581 }
582
583
584
585 /**
586 * Do error checking for a glGetTexImage() call.
587 * \return GL_TRUE if any error, GL_FALSE if no errors.
588 */
589 static GLboolean
590 getteximage_error_check(GLcontext *ctx, GLenum target, GLint level,
591 GLenum format, GLenum type, GLvoid *pixels )
592 {
593 struct gl_texture_object *texObj;
594 struct gl_texture_image *texImage;
595 const GLuint maxLevels = _mesa_max_texture_levels(ctx, target);
596 GLenum baseFormat;
597
598 if (maxLevels == 0) {
599 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target=0x%x)", target);
600 return GL_TRUE;
601 }
602
603 if (level < 0 || level >= maxLevels) {
604 _mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
605 return GL_TRUE;
606 }
607
608 if (_mesa_sizeof_packed_type(type) <= 0) {
609 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
610 return GL_TRUE;
611 }
612
613 if (_mesa_components_in_format(format) <= 0 ||
614 format == GL_STENCIL_INDEX) {
615 _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
616 return GL_TRUE;
617 }
618
619 if (!ctx->Extensions.EXT_paletted_texture && _mesa_is_index_format(format)) {
620 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
621 return GL_TRUE;
622 }
623
624 if (!ctx->Extensions.ARB_depth_texture && _mesa_is_depth_format(format)) {
625 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
626 return GL_TRUE;
627 }
628
629 if (!ctx->Extensions.MESA_ycbcr_texture && _mesa_is_ycbcr_format(format)) {
630 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
631 return GL_TRUE;
632 }
633
634 if (!ctx->Extensions.EXT_packed_depth_stencil
635 && _mesa_is_depthstencil_format(format)) {
636 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
637 return GL_TRUE;
638 }
639
640 if (!ctx->Extensions.ATI_envmap_bumpmap
641 && _mesa_is_dudv_format(format)) {
642 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
643 return GL_TRUE;
644 }
645
646 texObj = _mesa_get_current_tex_object(ctx, target);
647
648 if (!texObj || _mesa_is_proxy_texture(target)) {
649 _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
650 return GL_TRUE;
651 }
652
653 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
654 if (!texImage) {
655 /* out of memory */
656 return GL_TRUE;
657 }
658
659 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
660
661 /* Make sure the requested image format is compatible with the
662 * texture's format. Note that a color index texture can be converted
663 * to RGBA so that combo is allowed.
664 */
665 if (_mesa_is_color_format(format)
666 && !_mesa_is_color_format(baseFormat)
667 && !_mesa_is_index_format(baseFormat)) {
668 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
669 return GL_TRUE;
670 }
671 else if (_mesa_is_index_format(format)
672 && !_mesa_is_index_format(baseFormat)) {
673 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
674 return GL_TRUE;
675 }
676 else if (_mesa_is_depth_format(format)
677 && !_mesa_is_depth_format(baseFormat)
678 && !_mesa_is_depthstencil_format(baseFormat)) {
679 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
680 return GL_TRUE;
681 }
682 else if (_mesa_is_ycbcr_format(format)
683 && !_mesa_is_ycbcr_format(baseFormat)) {
684 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
685 return GL_TRUE;
686 }
687 else if (_mesa_is_depthstencil_format(format)
688 && !_mesa_is_depthstencil_format(baseFormat)) {
689 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
690 return GL_TRUE;
691 }
692 else if (_mesa_is_dudv_format(format)
693 && !_mesa_is_dudv_format(baseFormat)) {
694 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
695 return GL_TRUE;
696 }
697
698 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
699 /* packing texture image into a PBO */
700 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
701 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
702 texImage->Height, texImage->Depth,
703 format, type, pixels)) {
704 _mesa_error(ctx, GL_INVALID_OPERATION,
705 "glGetTexImage(out of bounds PBO write)");
706 return GL_TRUE;
707 }
708
709 /* PBO should not be mapped */
710 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
711 _mesa_error(ctx, GL_INVALID_OPERATION,
712 "glGetTexImage(PBO is mapped)");
713 return GL_TRUE;
714 }
715 }
716
717 return GL_FALSE;
718 }
719
720
721
722 /**
723 * Get texture image. Called by glGetTexImage.
724 *
725 * \param target texture target.
726 * \param level image level.
727 * \param format pixel data format for returned image.
728 * \param type pixel data type for returned image.
729 * \param pixels returned pixel data.
730 */
731 void GLAPIENTRY
732 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
733 GLenum type, GLvoid *pixels )
734 {
735 struct gl_texture_object *texObj;
736 struct gl_texture_image *texImage;
737 GET_CURRENT_CONTEXT(ctx);
738 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
739
740 if (getteximage_error_check(ctx, target, level, format, type, pixels)) {
741 return;
742 }
743
744 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
745 /* not an error, do nothing */
746 return;
747 }
748
749 texObj = _mesa_get_current_tex_object(ctx, target);
750 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
751
752 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
753 _mesa_debug(ctx, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
754 " dstFmt=0x%x, dstType=0x%x\n",
755 texObj->Name,
756 _mesa_get_format_name(texImage->TexFormat),
757 texImage->Width, texImage->Height,
758 format, type);
759 }
760
761 _mesa_lock_texture(ctx, texObj);
762 {
763 ctx->Driver.GetTexImage(ctx, target, level, format, type, pixels,
764 texObj, texImage);
765 }
766 _mesa_unlock_texture(ctx, texObj);
767 }
768
769
770
771 /**
772 * Do error checking for a glGetCompressedTexImage() call.
773 * \return GL_TRUE if any error, GL_FALSE if no errors.
774 */
775 static GLboolean
776 getcompressedteximage_error_check(GLcontext *ctx, GLenum target, GLint level,
777 GLvoid *img)
778 {
779 struct gl_texture_object *texObj;
780 struct gl_texture_image *texImage;
781 const GLuint maxLevels = _mesa_max_texture_levels(ctx, target);
782
783 if (maxLevels == 0) {
784 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImage(target=0x%x)",
785 target);
786 return GL_TRUE;
787 }
788
789 if (level < 0 || level >= maxLevels) {
790 _mesa_error(ctx, GL_INVALID_VALUE,
791 "glGetCompressedTexImageARB(bad level = %d)", level);
792 return GL_TRUE;
793 }
794
795 if (_mesa_is_proxy_texture(target)) {
796 _mesa_error(ctx, GL_INVALID_ENUM,
797 "glGetCompressedTexImageARB(bad target = %s)",
798 _mesa_lookup_enum_by_nr(target));
799 return GL_TRUE;
800 }
801
802 texObj = _mesa_get_current_tex_object(ctx, target);
803 if (!texObj) {
804 _mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
805 return GL_TRUE;
806 }
807
808 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
809
810 if (!texImage) {
811 /* probably invalid mipmap level */
812 _mesa_error(ctx, GL_INVALID_VALUE,
813 "glGetCompressedTexImageARB(level)");
814 return GL_TRUE;
815 }
816
817 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
818 _mesa_error(ctx, GL_INVALID_OPERATION,
819 "glGetCompressedTexImageARB(texture is not compressed)");
820 return GL_TRUE;
821 }
822
823 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
824 GLuint compressedSize;
825
826 /* make sure PBO is not mapped */
827 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
828 _mesa_error(ctx, GL_INVALID_OPERATION,
829 "glGetCompressedTexImage(PBO is mapped)");
830 return GL_TRUE;
831 }
832
833 compressedSize = _mesa_format_image_size(texImage->TexFormat,
834 texImage->Width,
835 texImage->Height,
836 texImage->Depth);
837
838 /* do bounds checking on PBO write */
839 if ((const GLubyte *) img + compressedSize >
840 (const GLubyte *) ctx->Pack.BufferObj->Size) {
841 _mesa_error(ctx, GL_INVALID_OPERATION,
842 "glGetCompressedTexImage(out of bounds PBO write)");
843 return GL_TRUE;
844 }
845 }
846
847 return GL_FALSE;
848 }
849
850
851 void GLAPIENTRY
852 _mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
853 {
854 struct gl_texture_object *texObj;
855 struct gl_texture_image *texImage;
856 GET_CURRENT_CONTEXT(ctx);
857 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
858
859 if (getcompressedteximage_error_check(ctx, target, level, img)) {
860 return;
861 }
862
863 if (_mesa_is_bufferobj(ctx->Pack.BufferObj) && !img) {
864 /* not an error, do nothing */
865 return;
866 }
867
868 texObj = _mesa_get_current_tex_object(ctx, target);
869 texImage = _mesa_select_tex_image(ctx, texObj, target, level);
870
871 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
872 _mesa_debug(ctx,
873 "glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
874 texObj->Name,
875 _mesa_get_format_name(texImage->TexFormat),
876 texImage->Width, texImage->Height);
877 }
878
879 _mesa_lock_texture(ctx, texObj);
880 {
881 ctx->Driver.GetCompressedTexImage(ctx, target, level, img,
882 texObj, texImage);
883 }
884 _mesa_unlock_texture(ctx, texObj);
885 }