mesa: Remove unnecessary headers from fbobject.c.
[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 "texgetimage.h"
39 #include "teximage.h"
40
41
42
43 #if FEATURE_EXT_texture_sRGB
44
45 /**
46 * Convert a float value from linear space to a
47 * non-linear sRGB value in [0, 255].
48 * Not terribly efficient.
49 */
50 static INLINE GLfloat
51 linear_to_nonlinear(GLfloat cl)
52 {
53 /* can't have values outside [0, 1] */
54 GLfloat cs;
55 if (cl < 0.0031308f) {
56 cs = 12.92f * cl;
57 }
58 else {
59 cs = (GLfloat)(1.055 * _mesa_pow(cl, 0.41666) - 0.055);
60 }
61 return cs;
62 }
63
64 #endif /* FEATURE_EXT_texture_sRGB */
65
66
67 /**
68 * Can the given type represent negative values?
69 */
70 static INLINE GLboolean
71 type_with_negative_values(GLenum type)
72 {
73 switch (type) {
74 case GL_BYTE:
75 case GL_SHORT:
76 case GL_INT:
77 case GL_FLOAT:
78 case GL_HALF_FLOAT_ARB:
79 return GL_TRUE;
80 default:
81 return GL_FALSE;
82 }
83 }
84
85
86 /**
87 * glGetTexImage for color index pixels.
88 */
89 static void
90 get_tex_color_index(GLcontext *ctx, GLuint dimensions,
91 GLenum format, GLenum type, GLvoid *pixels,
92 const struct gl_texture_image *texImage)
93 {
94 const GLint width = texImage->Width;
95 const GLint height = texImage->Height;
96 const GLint depth = texImage->Depth;
97 const GLuint indexBits =
98 _mesa_get_format_bits(texImage->TexFormat, GL_TEXTURE_INDEX_SIZE_EXT);
99 const GLbitfield transferOps = 0x0;
100 GLint img, row, col;
101
102 for (img = 0; img < depth; img++) {
103 for (row = 0; row < height; row++) {
104 GLuint indexRow[MAX_WIDTH] = { 0 };
105 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
106 width, height, format, type,
107 img, row, 0);
108 assert(dest);
109
110 if (indexBits == 8) {
111 const GLubyte *src = (const GLubyte *) texImage->Data;
112 src += width * (img * texImage->Height + row);
113 for (col = 0; col < width; col++) {
114 indexRow[col] = src[col];
115 }
116 }
117 else if (indexBits == 16) {
118 const GLushort *src = (const GLushort *) texImage->Data;
119 src += width * (img * texImage->Height + row);
120 for (col = 0; col < width; col++) {
121 indexRow[col] = src[col];
122 }
123 }
124 else {
125 _mesa_problem(ctx, "Color index problem in _mesa_GetTexImage");
126 }
127 _mesa_pack_index_span(ctx, width, type, dest,
128 indexRow, &ctx->Pack, transferOps);
129 }
130 }
131 }
132
133
134 /**
135 * glGetTexImage for depth/Z pixels.
136 */
137 static void
138 get_tex_depth(GLcontext *ctx, GLuint dimensions,
139 GLenum format, GLenum type, GLvoid *pixels,
140 const struct gl_texture_image *texImage)
141 {
142 const GLint width = texImage->Width;
143 const GLint height = texImage->Height;
144 const GLint depth = texImage->Depth;
145 GLint img, row, col;
146
147 for (img = 0; img < depth; img++) {
148 for (row = 0; row < height; row++) {
149 GLfloat depthRow[MAX_WIDTH];
150 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
151 width, height, format, type,
152 img, row, 0);
153 assert(dest);
154
155 for (col = 0; col < width; col++) {
156 texImage->FetchTexelf(texImage, col, row, img, depthRow + col);
157 }
158 _mesa_pack_depth_span(ctx, width, dest, type, depthRow, &ctx->Pack);
159 }
160 }
161 }
162
163
164 /**
165 * glGetTexImage for depth/stencil pixels.
166 */
167 static void
168 get_tex_depth_stencil(GLcontext *ctx, GLuint dimensions,
169 GLenum format, GLenum type, GLvoid *pixels,
170 const struct gl_texture_image *texImage)
171 {
172 const GLint width = texImage->Width;
173 const GLint height = texImage->Height;
174 const GLint depth = texImage->Depth;
175 const GLuint *src = (const GLuint *) texImage->Data;
176 GLint img, row;
177
178 for (img = 0; img < depth; img++) {
179 for (row = 0; row < height; row++) {
180 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
181 width, height, format, type,
182 img, row, 0);
183 _mesa_memcpy(dest, src, width * sizeof(GLuint));
184 if (ctx->Pack.SwapBytes) {
185 _mesa_swap4((GLuint *) dest, width);
186 }
187
188 src += width * row + width * height * img;
189 }
190 }
191 }
192
193
194 /**
195 * glGetTexImage for YCbCr pixels.
196 */
197 static void
198 get_tex_ycbcr(GLcontext *ctx, GLuint dimensions,
199 GLenum format, GLenum type, GLvoid *pixels,
200 const struct gl_texture_image *texImage)
201 {
202 const GLint width = texImage->Width;
203 const GLint height = texImage->Height;
204 const GLint depth = texImage->Depth;
205 const GLint rowstride = texImage->RowStride;
206 const GLushort *src = (const GLushort *) texImage->Data;
207 GLint img, row;
208
209 for (img = 0; img < depth; img++) {
210 for (row = 0; row < height; row++) {
211 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
212 width, height, format, type,
213 img, row, 0);
214 _mesa_memcpy(dest, src, width * sizeof(GLushort));
215
216 /* check for byte swapping */
217 if ((texImage->TexFormat == MESA_FORMAT_YCBCR
218 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
219 (texImage->TexFormat == MESA_FORMAT_YCBCR_REV
220 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
221 if (!ctx->Pack.SwapBytes)
222 _mesa_swap2((GLushort *) dest, width);
223 }
224 else if (ctx->Pack.SwapBytes) {
225 _mesa_swap2((GLushort *) dest, width);
226 }
227
228 src += rowstride;
229 }
230 }
231 }
232
233
234 /**
235 * glGetTexImagefor sRGB pixels;
236 */
237 static void
238 get_tex_srgb(GLcontext *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 /**
286 * glGetTexImagefor RGBA, Luminance, etc. pixels.
287 * This is the slow way since we use texture sampling.
288 */
289 static void
290 get_tex_rgba(GLcontext *ctx, GLuint dimensions,
291 GLenum format, GLenum type, GLvoid *pixels,
292 const struct gl_texture_image *texImage)
293 {
294 const GLint width = texImage->Width;
295 const GLint height = texImage->Height;
296 const GLint depth = texImage->Depth;
297 /* Normally, no pixel transfer ops are performed during glGetTexImage.
298 * The only possible exception is component clamping to [0,1].
299 */
300 GLbitfield transferOps = 0x0;
301 GLint img, row;
302
303 for (img = 0; img < depth; img++) {
304 for (row = 0; row < height; row++) {
305 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
306 width, height, format, type,
307 img, row, 0);
308 GLfloat rgba[MAX_WIDTH][4];
309 GLint col;
310 GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
311
312 /* clamp does not apply to GetTexImage (final conversion)?
313 * Looks like we need clamp though when going from format
314 * containing negative values to unsigned format.
315 */
316 if (format == GL_LUMINANCE || format == GL_LUMINANCE_ALPHA) {
317 transferOps |= IMAGE_CLAMP_BIT;
318 }
319 else if (!type_with_negative_values(type) &&
320 (dataType == GL_FLOAT ||
321 dataType == GL_SIGNED_NORMALIZED)) {
322 transferOps |= IMAGE_CLAMP_BIT;
323 }
324
325 for (col = 0; col < width; col++) {
326 texImage->FetchTexelf(texImage, col, row, img, rgba[col]);
327 if (texImage->_BaseFormat == GL_ALPHA) {
328 rgba[col][RCOMP] = 0.0F;
329 rgba[col][GCOMP] = 0.0F;
330 rgba[col][BCOMP] = 0.0F;
331 }
332 else if (texImage->_BaseFormat == GL_LUMINANCE) {
333 rgba[col][GCOMP] = 0.0F;
334 rgba[col][BCOMP] = 0.0F;
335 rgba[col][ACOMP] = 1.0F;
336 }
337 else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
338 rgba[col][GCOMP] = 0.0F;
339 rgba[col][BCOMP] = 0.0F;
340 }
341 else if (texImage->_BaseFormat == GL_INTENSITY) {
342 rgba[col][GCOMP] = 0.0F;
343 rgba[col][BCOMP] = 0.0F;
344 rgba[col][ACOMP] = 1.0F;
345 }
346 }
347 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
348 format, type, dest,
349 &ctx->Pack, transferOps);
350 }
351 }
352 }
353
354
355 /**
356 * Try to do glGetTexImage() with simple memcpy().
357 * \return GL_TRUE if done, GL_FALSE otherwise
358 */
359 static GLboolean
360 get_tex_memcpy(GLcontext *ctx, GLenum format, GLenum type, GLvoid *pixels,
361 const struct gl_texture_object *texObj,
362 const struct gl_texture_image *texImage)
363 {
364 GLboolean memCopy = GL_FALSE;
365
366 /* Texture image should have been mapped already */
367 assert(texImage->Data);
368
369 /*
370 * Check if the src/dst formats are compatible.
371 * Also note that GL's pixel transfer ops don't apply to glGetTexImage()
372 * so we don't have to worry about those.
373 * XXX more format combinations could be supported here.
374 */
375 if ((texObj->Target == GL_TEXTURE_1D ||
376 texObj->Target == GL_TEXTURE_2D ||
377 texObj->Target == GL_TEXTURE_RECTANGLE ||
378 (texObj->Target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
379 texObj->Target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))) {
380 if (texImage->TexFormat == MESA_FORMAT_ARGB8888 &&
381 format == GL_BGRA &&
382 type == GL_UNSIGNED_BYTE &&
383 !ctx->Pack.SwapBytes &&
384 _mesa_little_endian()) {
385 memCopy = GL_TRUE;
386 }
387 else if (texImage->TexFormat == MESA_FORMAT_AL88 &&
388 format == GL_LUMINANCE_ALPHA &&
389 type == GL_UNSIGNED_BYTE &&
390 !ctx->Pack.SwapBytes &&
391 _mesa_little_endian()) {
392 memCopy = GL_TRUE;
393 }
394 else if (texImage->TexFormat == MESA_FORMAT_L8 &&
395 format == GL_LUMINANCE &&
396 type == GL_UNSIGNED_BYTE) {
397 memCopy = GL_TRUE;
398 }
399 else if (texImage->TexFormat == MESA_FORMAT_A8 &&
400 format == GL_ALPHA &&
401 type == GL_UNSIGNED_BYTE) {
402 memCopy = GL_TRUE;
403 }
404 }
405
406 if (memCopy) {
407 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
408 const GLuint bytesPerRow = texImage->Width * bpp;
409 GLubyte *dst =
410 _mesa_image_address2d(&ctx->Pack, pixels, texImage->Width,
411 texImage->Height, format, type, 0, 0);
412 const GLint dstRowStride =
413 _mesa_image_row_stride(&ctx->Pack, texImage->Width, format, type);
414 const GLubyte *src = texImage->Data;
415 const GLint srcRowStride = texImage->RowStride * bpp;
416 GLuint row;
417
418 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
419 memcpy(dst, src, bytesPerRow * texImage->Height);
420 }
421 else {
422 for (row = 0; row < texImage->Height; row++) {
423 memcpy(dst, src, bytesPerRow);
424 dst += dstRowStride;
425 src += srcRowStride;
426 }
427 }
428 }
429
430 return memCopy;
431 }
432
433
434 /**
435 * This is the software fallback for Driver.GetTexImage().
436 * All error checking will have been done before this routine is called.
437 * The texture image must be mapped.
438 */
439 void
440 _mesa_get_teximage(GLcontext *ctx, GLenum target, GLint level,
441 GLenum format, GLenum type, GLvoid *pixels,
442 struct gl_texture_object *texObj,
443 struct gl_texture_image *texImage)
444 {
445 GLuint dimensions;
446
447 /* If we get here, the texture image should be mapped */
448 assert(texImage->Data);
449
450 switch (target) {
451 case GL_TEXTURE_1D:
452 dimensions = 1;
453 break;
454 case GL_TEXTURE_3D:
455 dimensions = 3;
456 break;
457 default:
458 dimensions = 2;
459 }
460
461 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
462 /* Packing texture image into a PBO.
463 * Map the (potentially) VRAM-based buffer into our process space so
464 * we can write into it with the code below.
465 * A hardware driver might use a sophisticated blit to move the
466 * texture data to the PBO if the PBO is in VRAM along with the texture.
467 */
468 GLubyte *buf = (GLubyte *)
469 ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
470 GL_WRITE_ONLY_ARB, ctx->Pack.BufferObj);
471 if (!buf) {
472 /* out of memory or other unexpected error */
473 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
474 return;
475 }
476 /* <pixels> was an offset into the PBO.
477 * Now make it a real, client-side pointer inside the mapped region.
478 */
479 pixels = ADD_POINTERS(buf, pixels);
480 }
481
482 if (get_tex_memcpy(ctx, format, type, pixels, texObj, texImage)) {
483 /* all done */
484 }
485 else if (format == GL_COLOR_INDEX) {
486 get_tex_color_index(ctx, dimensions, format, type, pixels, texImage);
487 }
488 else if (format == GL_DEPTH_COMPONENT) {
489 get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
490 }
491 else if (format == GL_DEPTH_STENCIL_EXT) {
492 get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
493 }
494 else if (format == GL_YCBCR_MESA) {
495 get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
496 }
497 else if (_mesa_get_format_color_encoding(texImage->TexFormat) == GL_SRGB) {
498 get_tex_srgb(ctx, dimensions, format, type, pixels, texImage);
499 }
500 else {
501 get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
502 }
503
504 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
505 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
506 ctx->Pack.BufferObj);
507 }
508 }
509
510
511
512 /**
513 * This is the software fallback for Driver.GetCompressedTexImage().
514 * All error checking will have been done before this routine is called.
515 */
516 void
517 _mesa_get_compressed_teximage(GLcontext *ctx, GLenum target, GLint level,
518 GLvoid *img,
519 struct gl_texture_object *texObj,
520 struct gl_texture_image *texImage)
521 {
522 const GLuint row_stride = _mesa_format_row_stride(texImage->TexFormat,
523 texImage->Width);
524 const GLuint row_stride_stored = _mesa_format_row_stride(texImage->TexFormat,
525 texImage->RowStride);
526 GLuint i;
527
528 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
529 /* pack texture image into a PBO */
530 GLubyte *buf = (GLubyte *)
531 ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
532 GL_WRITE_ONLY_ARB, ctx->Pack.BufferObj);
533 if (!buf) {
534 /* out of memory or other unexpected error */
535 _mesa_error(ctx, GL_OUT_OF_MEMORY,
536 "glGetCompresssedTexImage(map PBO failed)");
537 return;
538 }
539 img = ADD_POINTERS(buf, img);
540 }
541
542 /* no pixelstore or pixel transfer, but respect stride */
543
544 if (row_stride == row_stride_stored) {
545 const GLuint size = _mesa_format_image_size(texImage->TexFormat,
546 texImage->Width,
547 texImage->Height,
548 texImage->Depth);
549 _mesa_memcpy(img, texImage->Data, size);
550 }
551 else {
552 GLuint bw, bh;
553 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
554 for (i = 0; i < (texImage->Height + bh - 1) / bh; i++) {
555 memcpy((GLubyte *)img + i * row_stride,
556 (GLubyte *)texImage->Data + i * row_stride_stored, row_stride);
557 }
558 }
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 }