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