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