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