mesa: remove null check before free
[mesa.git] / src / mesa / main / texgetimage.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2009 VMware, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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 "format_unpack.h"
38 #include "glformats.h"
39 #include "image.h"
40 #include "mtypes.h"
41 #include "pack.h"
42 #include "pbo.h"
43 #include "pixelstore.h"
44 #include "texcompress.h"
45 #include "texgetimage.h"
46 #include "teximage.h"
47 #include "texobj.h"
48 #include "texstore.h"
49 #include "format_utils.h"
50 #include "pixeltransfer.h"
51
52 /**
53 * Can the given type represent negative values?
54 */
55 static inline GLboolean
56 type_needs_clamping(GLenum type)
57 {
58 switch (type) {
59 case GL_BYTE:
60 case GL_SHORT:
61 case GL_INT:
62 case GL_FLOAT:
63 case GL_HALF_FLOAT_ARB:
64 case GL_UNSIGNED_INT_10F_11F_11F_REV:
65 case GL_UNSIGNED_INT_5_9_9_9_REV:
66 return GL_FALSE;
67 default:
68 return GL_TRUE;
69 }
70 }
71
72
73 /**
74 * glGetTexImage for depth/Z pixels.
75 */
76 static void
77 get_tex_depth(struct gl_context *ctx, GLuint dimensions,
78 GLint xoffset, GLint yoffset, GLint zoffset,
79 GLsizei width, GLsizei height, GLint depth,
80 GLenum format, GLenum type, GLvoid *pixels,
81 struct gl_texture_image *texImage)
82 {
83 GLint img, row;
84 GLfloat *depthRow = malloc(width * sizeof(GLfloat));
85
86 if (!depthRow) {
87 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
88 return;
89 }
90
91 for (img = 0; img < depth; img++) {
92 GLubyte *srcMap;
93 GLint srcRowStride;
94
95 /* map src texture buffer */
96 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
97 xoffset, yoffset, width, height,
98 GL_MAP_READ_BIT, &srcMap, &srcRowStride);
99
100 if (srcMap) {
101 for (row = 0; row < height; row++) {
102 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
103 width, height, format, type,
104 img, row, 0);
105 const GLubyte *src = srcMap + row * srcRowStride;
106 _mesa_unpack_float_z_row(texImage->TexFormat, width, src, depthRow);
107 _mesa_pack_depth_span(ctx, width, dest, type, depthRow, &ctx->Pack);
108 }
109
110 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
111 }
112 else {
113 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
114 break;
115 }
116 }
117
118 free(depthRow);
119 }
120
121
122 /**
123 * glGetTexImage for depth/stencil pixels.
124 */
125 static void
126 get_tex_depth_stencil(struct gl_context *ctx, GLuint dimensions,
127 GLint xoffset, GLint yoffset, GLint zoffset,
128 GLsizei width, GLsizei height, GLint depth,
129 GLenum format, GLenum type, GLvoid *pixels,
130 struct gl_texture_image *texImage)
131 {
132 GLint img, row;
133
134 assert(format == GL_DEPTH_STENCIL);
135 assert(type == GL_UNSIGNED_INT_24_8 ||
136 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
137
138 for (img = 0; img < depth; img++) {
139 GLubyte *srcMap;
140 GLint rowstride;
141
142 /* map src texture buffer */
143 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
144 xoffset, yoffset, width, height,
145 GL_MAP_READ_BIT, &srcMap, &rowstride);
146
147 if (srcMap) {
148 for (row = 0; row < height; row++) {
149 const GLubyte *src = srcMap + row * rowstride;
150 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
151 width, height, format, type,
152 img, row, 0);
153 _mesa_unpack_depth_stencil_row(texImage->TexFormat,
154 width,
155 (const GLuint *) src,
156 type, dest);
157 if (ctx->Pack.SwapBytes) {
158 _mesa_swap4((GLuint *) dest, width);
159 }
160 }
161
162 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
163 }
164 else {
165 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
166 break;
167 }
168 }
169 }
170
171 /**
172 * glGetTexImage for stencil pixels.
173 */
174 static void
175 get_tex_stencil(struct gl_context *ctx, GLuint dimensions,
176 GLint xoffset, GLint yoffset, GLint zoffset,
177 GLsizei width, GLsizei height, GLint depth,
178 GLenum format, GLenum type, GLvoid *pixels,
179 struct gl_texture_image *texImage)
180 {
181 GLint img, row;
182
183 assert(format == GL_STENCIL_INDEX);
184
185 for (img = 0; img < depth; img++) {
186 GLubyte *srcMap;
187 GLint rowstride;
188
189 /* map src texture buffer */
190 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
191 xoffset, yoffset, width, height,
192 GL_MAP_READ_BIT,
193 &srcMap, &rowstride);
194
195 if (srcMap) {
196 for (row = 0; row < height; row++) {
197 const GLubyte *src = srcMap + row * rowstride;
198 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
199 width, height, format, type,
200 img, row, 0);
201 _mesa_unpack_ubyte_stencil_row(texImage->TexFormat,
202 width,
203 (const GLuint *) src,
204 dest);
205 }
206
207 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
208 }
209 else {
210 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
211 break;
212 }
213 }
214 }
215
216
217 /**
218 * glGetTexImage for YCbCr pixels.
219 */
220 static void
221 get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
222 GLint xoffset, GLint yoffset, GLint zoffset,
223 GLsizei width, GLsizei height, GLint depth,
224 GLenum format, GLenum type, GLvoid *pixels,
225 struct gl_texture_image *texImage)
226 {
227 GLint img, row;
228
229 for (img = 0; img < depth; img++) {
230 GLubyte *srcMap;
231 GLint rowstride;
232
233 /* map src texture buffer */
234 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
235 xoffset, yoffset, width, height,
236 GL_MAP_READ_BIT, &srcMap, &rowstride);
237
238 if (srcMap) {
239 for (row = 0; row < height; row++) {
240 const GLubyte *src = srcMap + row * rowstride;
241 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
242 width, height, format, type,
243 img, row, 0);
244 memcpy(dest, src, width * sizeof(GLushort));
245
246 /* check for byte swapping */
247 if ((texImage->TexFormat == MESA_FORMAT_YCBCR
248 && type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
249 (texImage->TexFormat == MESA_FORMAT_YCBCR_REV
250 && type == GL_UNSIGNED_SHORT_8_8_MESA)) {
251 if (!ctx->Pack.SwapBytes)
252 _mesa_swap2((GLushort *) dest, width);
253 }
254 else if (ctx->Pack.SwapBytes) {
255 _mesa_swap2((GLushort *) dest, width);
256 }
257 }
258
259 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
260 }
261 else {
262 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
263 break;
264 }
265 }
266 }
267
268
269 /**
270 * Get a color texture image with decompression.
271 */
272 static void
273 get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
274 GLint xoffset, GLint yoffset, GLint zoffset,
275 GLsizei width, GLsizei height, GLint depth,
276 GLenum format, GLenum type, GLvoid *pixels,
277 struct gl_texture_image *texImage,
278 GLbitfield transferOps)
279 {
280 /* don't want to apply sRGB -> RGB conversion here so override the format */
281 const mesa_format texFormat =
282 _mesa_get_srgb_format_linear(texImage->TexFormat);
283 const GLenum baseFormat = _mesa_get_format_base_format(texFormat);
284 GLfloat *tempImage, *tempSlice;
285 GLuint slice;
286 int srcStride, dstStride;
287 uint32_t dstFormat;
288 bool needsRebase;
289 uint8_t rebaseSwizzle[4];
290
291 /* Decompress into temp float buffer, then pack into user buffer */
292 tempImage = malloc(width * height * depth * 4 * sizeof(GLfloat));
293 if (!tempImage) {
294 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
295 return;
296 }
297
298 /* Decompress the texture image slices - results in 'tempImage' */
299 for (slice = 0; slice < depth; slice++) {
300 GLubyte *srcMap;
301 GLint srcRowStride;
302
303 tempSlice = tempImage + slice * 4 * width * height;
304
305 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
306 xoffset, yoffset, width, height,
307 GL_MAP_READ_BIT,
308 &srcMap, &srcRowStride);
309 if (srcMap) {
310 _mesa_decompress_image(texFormat, width, height,
311 srcMap, srcRowStride, tempSlice);
312
313 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
314 }
315 else {
316 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
317 free(tempImage);
318 return;
319 }
320 }
321
322 /* Depending on the base format involved we may need to apply a rebase
323 * transform (for example: if we download to a Luminance format we want
324 * G=0 and B=0).
325 */
326 if (baseFormat == GL_LUMINANCE ||
327 baseFormat == GL_INTENSITY) {
328 needsRebase = true;
329 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
330 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
331 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
332 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
333 } else if (baseFormat == GL_LUMINANCE_ALPHA) {
334 needsRebase = true;
335 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
336 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
337 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
338 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
339 } else {
340 needsRebase = false;
341 }
342
343 srcStride = 4 * width * sizeof(GLfloat);
344 dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
345 dstFormat = _mesa_format_from_format_and_type(format, type);
346 tempSlice = tempImage;
347 for (slice = 0; slice < depth; slice++) {
348 void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
349 width, height, format, type,
350 slice, 0, 0);
351 _mesa_format_convert(dest, dstFormat, dstStride,
352 tempSlice, RGBA32_FLOAT, srcStride,
353 width, height,
354 needsRebase ? rebaseSwizzle : NULL);
355
356 /* Handle byte swapping if required */
357 if (ctx->Pack.SwapBytes) {
358 _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
359 width, height, dest, dest);
360 }
361
362 tempSlice += 4 * width * height;
363 }
364
365 free(tempImage);
366 }
367
368
369 /**
370 * Return a base GL format given the user-requested format
371 * for glGetTexImage().
372 */
373 GLenum
374 _mesa_base_pack_format(GLenum format)
375 {
376 switch (format) {
377 case GL_ABGR_EXT:
378 case GL_BGRA:
379 case GL_BGRA_INTEGER:
380 case GL_RGBA_INTEGER:
381 return GL_RGBA;
382 case GL_BGR:
383 case GL_BGR_INTEGER:
384 case GL_RGB_INTEGER:
385 return GL_RGB;
386 case GL_RED_INTEGER:
387 return GL_RED;
388 case GL_GREEN_INTEGER:
389 return GL_GREEN;
390 case GL_BLUE_INTEGER:
391 return GL_BLUE;
392 case GL_ALPHA_INTEGER:
393 return GL_ALPHA;
394 case GL_LUMINANCE_INTEGER_EXT:
395 return GL_LUMINANCE;
396 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
397 return GL_LUMINANCE_ALPHA;
398 default:
399 return format;
400 }
401 }
402
403
404 /**
405 * Get an uncompressed color texture image.
406 */
407 static void
408 get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
409 GLint xoffset, GLint yoffset, GLint zoffset,
410 GLsizei width, GLsizei height, GLint depth,
411 GLenum format, GLenum type, GLvoid *pixels,
412 struct gl_texture_image *texImage,
413 GLbitfield transferOps)
414 {
415 /* don't want to apply sRGB -> RGB conversion here so override the format */
416 const mesa_format texFormat =
417 _mesa_get_srgb_format_linear(texImage->TexFormat);
418 GLuint img;
419 GLboolean dst_is_integer;
420 uint32_t dst_format;
421 int dst_stride;
422 uint8_t rebaseSwizzle[4];
423 bool needsRebase;
424 void *rgba = NULL;
425
426 /* Depending on the base format involved we may need to apply a rebase
427 * transform (for example: if we download to a Luminance format we want
428 * G=0 and B=0).
429 */
430 if (texImage->_BaseFormat == GL_LUMINANCE ||
431 texImage->_BaseFormat == GL_INTENSITY) {
432 needsRebase = true;
433 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
434 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
435 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
436 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
437 } else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
438 needsRebase = true;
439 rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
440 rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
441 rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
442 rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
443 } else if (texImage->_BaseFormat !=
444 _mesa_get_format_base_format(texFormat)) {
445 needsRebase =
446 _mesa_compute_rgba2base2rgba_component_mapping(texImage->_BaseFormat,
447 rebaseSwizzle);
448 } else {
449 needsRebase = false;
450 }
451
452 /* Describe the dst format */
453 dst_is_integer = _mesa_is_enum_format_integer(format);
454 dst_format = _mesa_format_from_format_and_type(format, type);
455 dst_stride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
456
457 /* Since _mesa_format_convert does not handle transferOps we need to handle
458 * them before we call the function. This requires to convert to RGBA float
459 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
460 * integer then transferOps do not apply.
461 */
462 assert(!transferOps || (transferOps && !dst_is_integer));
463 (void) dst_is_integer; /* silence unused var warning */
464
465 for (img = 0; img < depth; img++) {
466 GLubyte *srcMap;
467 GLint rowstride;
468 GLubyte *img_src;
469 void *dest;
470 void *src;
471 int src_stride;
472 uint32_t src_format;
473
474 /* map src texture buffer */
475 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
476 xoffset, yoffset, width, height,
477 GL_MAP_READ_BIT,
478 &srcMap, &rowstride);
479 if (!srcMap) {
480 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
481 goto done;
482 }
483
484 img_src = srcMap;
485 dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
486 width, height, format, type,
487 img, 0, 0);
488
489 if (transferOps) {
490 uint32_t rgba_format;
491 int rgba_stride;
492 bool need_convert = false;
493
494 /* We will convert to RGBA float */
495 rgba_format = RGBA32_FLOAT;
496 rgba_stride = width * 4 * sizeof(GLfloat);
497
498 /* If we are lucky and the dst format matches the RGBA format we need
499 * to convert to, then we can convert directly into the dst buffer
500 * and avoid the final conversion/copy from the rgba buffer to the dst
501 * buffer.
502 */
503 if (format == rgba_format) {
504 rgba = dest;
505 } else if (rgba == NULL) { /* Allocate the RGBA buffer only once */
506 need_convert = true;
507 rgba = malloc(height * rgba_stride);
508 if (!rgba) {
509 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
510 ctx->Driver.UnmapTextureImage(ctx, texImage, img);
511 return;
512 }
513 }
514
515 _mesa_format_convert(rgba, rgba_format, rgba_stride,
516 img_src, texFormat, rowstride,
517 width, height,
518 needsRebase ? rebaseSwizzle : NULL);
519
520 /* Handle transfer ops now */
521 _mesa_apply_rgba_transfer_ops(ctx, transferOps, width * height, rgba);
522
523 /* If we had to rebase, we have already handled that */
524 needsRebase = false;
525
526 /* If we were lucky and our RGBA conversion matches the dst format,
527 * then we are done.
528 */
529 if (!need_convert)
530 goto do_swap;
531
532 /* Otherwise, we need to convert from RGBA to dst next */
533 src = rgba;
534 src_format = rgba_format;
535 src_stride = rgba_stride;
536 } else {
537 /* No RGBA conversion needed, convert directly to dst */
538 src = img_src;
539 src_format = texFormat;
540 src_stride = rowstride;
541 }
542
543 /* Do the conversion to destination format */
544 _mesa_format_convert(dest, dst_format, dst_stride,
545 src, src_format, src_stride,
546 width, height,
547 needsRebase ? rebaseSwizzle : NULL);
548
549 do_swap:
550 /* Handle byte swapping if required */
551 if (ctx->Pack.SwapBytes)
552 _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
553 width, height, dest, dest);
554
555 /* Unmap the src texture buffer */
556 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
557 }
558
559 done:
560 free(rgba);
561 }
562
563
564 /**
565 * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
566 * Compressed textures are handled here as well.
567 */
568 static void
569 get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
570 GLint xoffset, GLint yoffset, GLint zoffset,
571 GLsizei width, GLsizei height, GLint depth,
572 GLenum format, GLenum type, GLvoid *pixels,
573 struct gl_texture_image *texImage)
574 {
575 const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
576 GLbitfield transferOps = 0x0;
577
578 /* In general, clamping does not apply to glGetTexImage, except when
579 * the returned type of the image can't hold negative values.
580 */
581 if (type_needs_clamping(type)) {
582 /* the returned image type can't have negative values */
583 if (dataType == GL_FLOAT ||
584 dataType == GL_HALF_FLOAT ||
585 dataType == GL_SIGNED_NORMALIZED ||
586 format == GL_LUMINANCE ||
587 format == GL_LUMINANCE_ALPHA) {
588 transferOps |= IMAGE_CLAMP_BIT;
589 }
590 }
591
592 if (_mesa_is_format_compressed(texImage->TexFormat)) {
593 get_tex_rgba_compressed(ctx, dimensions,
594 xoffset, yoffset, zoffset,
595 width, height, depth,
596 format, type,
597 pixels, texImage, transferOps);
598 }
599 else {
600 get_tex_rgba_uncompressed(ctx, dimensions,
601 xoffset, yoffset, zoffset,
602 width, height, depth,
603 format, type,
604 pixels, texImage, transferOps);
605 }
606 }
607
608
609 /**
610 * Try to do glGetTexImage() with simple memcpy().
611 * \return GL_TRUE if done, GL_FALSE otherwise
612 */
613 static GLboolean
614 get_tex_memcpy(struct gl_context *ctx,
615 GLint xoffset, GLint yoffset, GLint zoffset,
616 GLsizei width, GLsizei height, GLint depth,
617 GLenum format, GLenum type, GLvoid *pixels,
618 struct gl_texture_image *texImage)
619 {
620 const GLenum target = texImage->TexObject->Target;
621 GLboolean memCopy = GL_FALSE;
622 GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
623
624 /*
625 * Check if we can use memcpy to copy from the hardware texture
626 * format to the user's format/type.
627 * Note that GL's pixel transfer ops don't apply to glGetTexImage()
628 */
629 if ((target == GL_TEXTURE_1D ||
630 target == GL_TEXTURE_2D ||
631 target == GL_TEXTURE_RECTANGLE ||
632 _mesa_is_cube_face(target)) &&
633 texBaseFormat == texImage->_BaseFormat) {
634 memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
635 format, type,
636 ctx->Pack.SwapBytes, NULL);
637 }
638
639 if (depth > 1) {
640 /* only a single slice is supported at this time */
641 memCopy = FALSE;
642 }
643
644 if (memCopy) {
645 const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
646 const GLint bytesPerRow = width * bpp;
647 GLubyte *dst =
648 _mesa_image_address2d(&ctx->Pack, pixels, width, height,
649 format, type, 0, 0);
650 const GLint dstRowStride =
651 _mesa_image_row_stride(&ctx->Pack, width, format, type);
652 GLubyte *src;
653 GLint srcRowStride;
654
655 /* map src texture buffer */
656 ctx->Driver.MapTextureImage(ctx, texImage, zoffset,
657 xoffset, yoffset, width, height,
658 GL_MAP_READ_BIT, &src, &srcRowStride);
659
660 if (src) {
661 if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
662 memcpy(dst, src, bytesPerRow * texImage->Height);
663 }
664 else {
665 GLuint row;
666 for (row = 0; row < height; row++) {
667 memcpy(dst, src, bytesPerRow);
668 dst += dstRowStride;
669 src += srcRowStride;
670 }
671 }
672
673 /* unmap src texture buffer */
674 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset);
675 }
676 else {
677 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
678 }
679 }
680
681 return memCopy;
682 }
683
684
685 /**
686 * This is the software fallback for Driver.GetTexSubImage().
687 * All error checking will have been done before this routine is called.
688 * We'll call ctx->Driver.MapTextureImage() to access the data, then
689 * unmap with ctx->Driver.UnmapTextureImage().
690 */
691 void
692 _mesa_GetTexSubImage_sw(struct gl_context *ctx,
693 GLint xoffset, GLint yoffset, GLint zoffset,
694 GLsizei width, GLsizei height, GLint depth,
695 GLenum format, GLenum type, GLvoid *pixels,
696 struct gl_texture_image *texImage)
697 {
698 const GLuint dimensions =
699 _mesa_get_texture_dimensions(texImage->TexObject->Target);
700
701 /* map dest buffer, if PBO */
702 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
703 /* Packing texture image into a PBO.
704 * Map the (potentially) VRAM-based buffer into our process space so
705 * we can write into it with the code below.
706 * A hardware driver might use a sophisticated blit to move the
707 * texture data to the PBO if the PBO is in VRAM along with the texture.
708 */
709 GLubyte *buf = (GLubyte *)
710 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
711 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
712 MAP_INTERNAL);
713 if (!buf) {
714 /* out of memory or other unexpected error */
715 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
716 return;
717 }
718 /* <pixels> was an offset into the PBO.
719 * Now make it a real, client-side pointer inside the mapped region.
720 */
721 pixels = ADD_POINTERS(buf, pixels);
722 }
723
724 /* for all array textures, the Z axis selects the layer */
725 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
726 depth = height;
727 height = 1;
728 zoffset = yoffset;
729 yoffset = 0;
730 assert(zoffset + depth <= texImage->Height);
731 } else {
732 assert(zoffset + depth <= texImage->Depth);
733 }
734
735 if (get_tex_memcpy(ctx, xoffset, yoffset, zoffset, width, height, depth,
736 format, type, pixels, texImage)) {
737 /* all done */
738 }
739 else if (format == GL_DEPTH_COMPONENT) {
740 get_tex_depth(ctx, dimensions, xoffset, yoffset, zoffset,
741 width, height, depth, format, type, pixels, texImage);
742 }
743 else if (format == GL_DEPTH_STENCIL_EXT) {
744 get_tex_depth_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
745 width, height, depth, format, type, pixels,
746 texImage);
747 }
748 else if (format == GL_STENCIL_INDEX) {
749 get_tex_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
750 width, height, depth, format, type, pixels, texImage);
751 }
752 else if (format == GL_YCBCR_MESA) {
753 get_tex_ycbcr(ctx, dimensions, xoffset, yoffset, zoffset,
754 width, height, depth, format, type, pixels, texImage);
755 }
756 else {
757 get_tex_rgba(ctx, dimensions, xoffset, yoffset, zoffset,
758 width, height, depth, format, type, pixels, texImage);
759 }
760
761 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
762 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
763 }
764 }
765
766
767
768 /**
769 * This is the software fallback for Driver.GetCompressedTexSubImage().
770 * All error checking will have been done before this routine is called.
771 */
772 void
773 _mesa_GetCompressedTexSubImage_sw(struct gl_context *ctx,
774 struct gl_texture_image *texImage,
775 GLint xoffset, GLint yoffset,
776 GLint zoffset, GLsizei width,
777 GLint height, GLint depth,
778 GLvoid *img)
779 {
780 const GLuint dimensions =
781 _mesa_get_texture_dimensions(texImage->TexObject->Target);
782 struct compressed_pixelstore store;
783 GLint slice;
784 GLubyte *dest;
785
786 _mesa_compute_compressed_pixelstore(dimensions, texImage->TexFormat,
787 width, height, depth,
788 &ctx->Pack, &store);
789
790 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
791 /* pack texture image into a PBO */
792 dest = (GLubyte *)
793 ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
794 GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
795 MAP_INTERNAL);
796 if (!dest) {
797 /* out of memory or other unexpected error */
798 _mesa_error(ctx, GL_OUT_OF_MEMORY,
799 "glGetCompresssedTexImage(map PBO failed)");
800 return;
801 }
802 dest = ADD_POINTERS(dest, img);
803 } else {
804 dest = img;
805 }
806
807 dest += store.SkipBytes;
808
809 for (slice = 0; slice < store.CopySlices; slice++) {
810 GLint srcRowStride;
811 GLubyte *src;
812
813 /* map src texture buffer */
814 ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
815 xoffset, yoffset, width, height,
816 GL_MAP_READ_BIT, &src, &srcRowStride);
817
818 if (src) {
819 GLint i;
820 for (i = 0; i < store.CopyRowsPerSlice; i++) {
821 memcpy(dest, src, store.CopyBytesPerRow);
822 dest += store.TotalBytesPerRow;
823 src += srcRowStride;
824 }
825
826 ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
827
828 /* Advance to next slice */
829 dest += store.TotalBytesPerRow * (store.TotalRowsPerSlice -
830 store.CopyRowsPerSlice);
831
832 } else {
833 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
834 }
835 }
836
837 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
838 ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
839 }
840 }
841
842
843 /**
844 * Validate the texture target enum supplied to glGetTex(ture)Image or
845 * glGetCompressedTex(ture)Image.
846 */
847 static GLboolean
848 legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
849 {
850 switch (target) {
851 case GL_TEXTURE_1D:
852 case GL_TEXTURE_2D:
853 case GL_TEXTURE_3D:
854 return GL_TRUE;
855 case GL_TEXTURE_RECTANGLE_NV:
856 return ctx->Extensions.NV_texture_rectangle;
857 case GL_TEXTURE_1D_ARRAY_EXT:
858 case GL_TEXTURE_2D_ARRAY_EXT:
859 return ctx->Extensions.EXT_texture_array;
860 case GL_TEXTURE_CUBE_MAP_ARRAY:
861 return ctx->Extensions.ARB_texture_cube_map_array;
862
863 /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
864 * (30.10.2014) says:
865 * "An INVALID_ENUM error is generated if the effective target is not
866 * one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
867 * TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
868 * the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
869 * or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
870 */
871 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
872 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
873 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
874 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
875 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
876 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
877 return dsa ? GL_FALSE : ctx->Extensions.ARB_texture_cube_map;
878 case GL_TEXTURE_CUBE_MAP:
879 return dsa ? GL_TRUE : GL_FALSE;
880 default:
881 return GL_FALSE;
882 }
883 }
884
885
886 /**
887 * Wrapper for _mesa_select_tex_image() which can handle target being
888 * GL_TEXTURE_CUBE_MAP in which case we use zoffset to select a cube face.
889 * This can happen for glGetTextureImage and glGetTextureSubImage (DSA
890 * functions).
891 */
892 static struct gl_texture_image *
893 select_tex_image(const struct gl_texture_object *texObj, GLenum target,
894 GLint level, GLint zoffset)
895 {
896 assert(level >= 0);
897 assert(level < MAX_TEXTURE_LEVELS);
898 if (target == GL_TEXTURE_CUBE_MAP) {
899 assert(zoffset >= 0);
900 assert(zoffset < 6);
901 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset;
902 }
903 return _mesa_select_tex_image(texObj, target, level);
904 }
905
906
907 /**
908 * Error-check the offset and size arguments to
909 * glGet[Compressed]TextureSubImage(). Also checks if the specified
910 * texture image is missing.
911 * \return true if error, false if no error.
912 */
913 static bool
914 dimensions_error_check(struct gl_context *ctx,
915 struct gl_texture_object *texObj,
916 GLenum target, GLint level,
917 GLint xoffset, GLint yoffset, GLint zoffset,
918 GLsizei width, GLsizei height, GLsizei depth,
919 const char *caller)
920 {
921 const struct gl_texture_image *texImage;
922 int i;
923
924 if (xoffset < 0) {
925 _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset = %d)", caller, xoffset);
926 return true;
927 }
928
929 if (yoffset < 0) {
930 _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset = %d)", caller, yoffset);
931 return true;
932 }
933
934 if (zoffset < 0) {
935 _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset = %d)", caller, zoffset);
936 return true;
937 }
938
939 if (width < 0) {
940 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width = %d)", caller, width);
941 return true;
942 }
943
944 if (height < 0) {
945 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height = %d)", caller, height);
946 return true;
947 }
948
949 if (depth < 0) {
950 _mesa_error(ctx, GL_INVALID_VALUE, "%s(depth = %d)", caller, depth);
951 return true;
952 }
953
954 /* do special per-target checks */
955 switch (target) {
956 case GL_TEXTURE_1D:
957 if (yoffset != 0) {
958 _mesa_error(ctx, GL_INVALID_VALUE,
959 "%s(1D, yoffset = %d)", caller, yoffset);
960 return true;
961 }
962 if (height > 1) {
963 _mesa_error(ctx, GL_INVALID_VALUE,
964 "%s(1D, height = %d)", caller, height);
965 return true;
966 }
967 /* fall-through */
968 case GL_TEXTURE_1D_ARRAY:
969 case GL_TEXTURE_2D:
970 case GL_TEXTURE_RECTANGLE:
971 if (zoffset != 0) {
972 _mesa_error(ctx, GL_INVALID_VALUE,
973 "%s(zoffset = %d)", caller, zoffset);
974 return true;
975 }
976 if (depth > 1) {
977 _mesa_error(ctx, GL_INVALID_VALUE,
978 "%s(depth = %d)", caller, depth);
979 return true;
980 }
981 break;
982 case GL_TEXTURE_CUBE_MAP:
983 /* Non-array cube maps are special because we have a gl_texture_image
984 * per face.
985 */
986 if (zoffset + depth > 6) {
987 _mesa_error(ctx, GL_INVALID_VALUE,
988 "%s(zoffset + depth = %d)", caller, zoffset + depth);
989 return true;
990 }
991 /* check that the range of faces exist */
992 for (i = 0; i < depth; i++) {
993 GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset + i;
994 if (!_mesa_select_tex_image(texObj, face, level)) {
995 /* non-existant face */
996 _mesa_error(ctx, GL_INVALID_OPERATION,
997 "%s(missing cube face)", caller);
998 return true;
999 }
1000 }
1001 break;
1002 default:
1003 ; /* nothing */
1004 }
1005
1006 texImage = select_tex_image(texObj, target, level, zoffset);
1007 if (!texImage) {
1008 /* missing texture image */
1009 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(missing image)", caller);
1010 return true;
1011 }
1012
1013 if (xoffset + width > texImage->Width) {
1014 _mesa_error(ctx, GL_INVALID_VALUE,
1015 "%s(xoffset %d + width %d > %u)",
1016 caller, xoffset, width, texImage->Width);
1017 return true;
1018 }
1019
1020 if (yoffset + height > texImage->Height) {
1021 _mesa_error(ctx, GL_INVALID_VALUE,
1022 "%s(yoffset %d + height %d > %u)",
1023 caller, yoffset, height, texImage->Height);
1024 return true;
1025 }
1026
1027 if (target != GL_TEXTURE_CUBE_MAP) {
1028 /* Cube map error checking was done above */
1029 if (zoffset + depth > texImage->Depth) {
1030 _mesa_error(ctx, GL_INVALID_VALUE,
1031 "%s(zoffset %d + depth %d > %u)",
1032 caller, zoffset, depth, texImage->Depth);
1033 return true;
1034 }
1035 }
1036
1037 /* Extra checks for compressed textures */
1038 {
1039 GLuint bw, bh, bd;
1040 _mesa_get_format_block_size_3d(texImage->TexFormat, &bw, &bh, &bd);
1041 if (bw > 1 || bh > 1 || bd > 1) {
1042 /* offset must be multiple of block size */
1043 if (xoffset % bw != 0) {
1044 _mesa_error(ctx, GL_INVALID_VALUE,
1045 "%s(xoffset = %d)", caller, xoffset);
1046 return true;
1047 }
1048 if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY) {
1049 if (yoffset % bh != 0) {
1050 _mesa_error(ctx, GL_INVALID_VALUE,
1051 "%s(yoffset = %d)", caller, yoffset);
1052 return true;
1053 }
1054 }
1055
1056 if (zoffset % bd != 0) {
1057 _mesa_error(ctx, GL_INVALID_VALUE,
1058 "%s(zoffset = %d)", caller, zoffset);
1059 return true;
1060 }
1061
1062 /* The size must be a multiple of bw x bh x bd, or we must be using a
1063 * offset+size that exactly hits the edge of the image.
1064 */
1065 if ((width % bw != 0) &&
1066 (xoffset + width != (GLint) texImage->Width)) {
1067 _mesa_error(ctx, GL_INVALID_VALUE,
1068 "%s(width = %d)", caller, width);
1069 return true;
1070 }
1071
1072 if ((height % bh != 0) &&
1073 (yoffset + height != (GLint) texImage->Height)) {
1074 _mesa_error(ctx, GL_INVALID_VALUE,
1075 "%s(height = %d)", caller, height);
1076 return true;
1077 }
1078
1079 if ((depth % bd != 0) &&
1080 (zoffset + depth != (GLint) texImage->Depth)) {
1081 _mesa_error(ctx, GL_INVALID_VALUE,
1082 "%s(depth = %d)", caller, depth);
1083 return true;
1084 }
1085 }
1086 }
1087
1088 if (width == 0 || height == 0 || depth == 0) {
1089 /* Not an error, but nothing to do. Return 'true' so that the
1090 * caller simply returns.
1091 */
1092 return true;
1093 }
1094
1095 return false;
1096 }
1097
1098
1099 /**
1100 * Do PBO-related error checking for getting uncompressed images.
1101 * \return true if there was an error (or the GetTexImage is to be a no-op)
1102 */
1103 static bool
1104 pbo_error_check(struct gl_context *ctx, GLenum target,
1105 GLsizei width, GLsizei height, GLsizei depth,
1106 GLenum format, GLenum type, GLsizei clientMemSize,
1107 GLvoid *pixels,
1108 const char *caller)
1109 {
1110 const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
1111
1112 if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, width, height, depth,
1113 format, type, clientMemSize, pixels)) {
1114 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1115 _mesa_error(ctx, GL_INVALID_OPERATION,
1116 "%s(out of bounds PBO access)", caller);
1117 } else {
1118 _mesa_error(ctx, GL_INVALID_OPERATION,
1119 "%s(out of bounds access: bufSize (%d) is too small)",
1120 caller, clientMemSize);
1121 }
1122 return true;
1123 }
1124
1125 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1126 /* PBO should not be mapped */
1127 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1128 _mesa_error(ctx, GL_INVALID_OPERATION,
1129 "%s(PBO is mapped)", caller);
1130 return true;
1131 }
1132 }
1133
1134 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1135 /* not an error, do nothing */
1136 return true;
1137 }
1138
1139 return false;
1140 }
1141
1142
1143 /**
1144 * Do error checking for all (non-compressed) get-texture-image functions.
1145 * \return true if any error, false if no errors.
1146 */
1147 static bool
1148 getteximage_error_check(struct gl_context *ctx,
1149 struct gl_texture_object *texObj,
1150 GLenum target, GLint level,
1151 GLint xoffset, GLint yoffset, GLint zoffset,
1152 GLsizei width, GLsizei height, GLsizei depth,
1153 GLenum format, GLenum type, GLsizei bufSize,
1154 GLvoid *pixels, const char *caller)
1155 {
1156 struct gl_texture_image *texImage;
1157 GLenum baseFormat, err;
1158 GLint maxLevels;
1159
1160 assert(texObj);
1161
1162 if (texObj->Target == 0) {
1163 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
1164 return true;
1165 }
1166
1167 maxLevels = _mesa_max_texture_levels(ctx, target);
1168 if (level < 0 || level >= maxLevels) {
1169 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level = %d)", caller, level);
1170 return true;
1171 }
1172
1173 err = _mesa_error_check_format_and_type(ctx, format, type);
1174 if (err != GL_NO_ERROR) {
1175 _mesa_error(ctx, err, "%s(format/type)", caller);
1176 return true;
1177 }
1178
1179 if (dimensions_error_check(ctx, texObj, target, level,
1180 xoffset, yoffset, zoffset,
1181 width, height, depth, caller)) {
1182 return true;
1183 }
1184
1185 if (pbo_error_check(ctx, target, width, height, depth,
1186 format, type, bufSize, pixels, caller)) {
1187 return true;
1188 }
1189
1190 texImage = select_tex_image(texObj, target, level, zoffset);
1191 assert(texImage);
1192
1193 /*
1194 * Format and type checking has been moved up to GetnTexImage and
1195 * GetTextureImage so that it happens before getting the texImage object.
1196 */
1197
1198 baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
1199
1200 /* Make sure the requested image format is compatible with the
1201 * texture's format.
1202 */
1203 if (_mesa_is_color_format(format)
1204 && !_mesa_is_color_format(baseFormat)) {
1205 _mesa_error(ctx, GL_INVALID_OPERATION,
1206 "%s(format mismatch)", caller);
1207 return true;
1208 }
1209 else if (_mesa_is_depth_format(format)
1210 && !_mesa_is_depth_format(baseFormat)
1211 && !_mesa_is_depthstencil_format(baseFormat)) {
1212 _mesa_error(ctx, GL_INVALID_OPERATION,
1213 "%s(format mismatch)", caller);
1214 return true;
1215 }
1216 else if (_mesa_is_stencil_format(format)
1217 && !ctx->Extensions.ARB_texture_stencil8) {
1218 _mesa_error(ctx, GL_INVALID_ENUM,
1219 "%s(format=GL_STENCIL_INDEX)", caller);
1220 return true;
1221 }
1222 else if (_mesa_is_stencil_format(format)
1223 && !_mesa_is_depthstencil_format(baseFormat)
1224 && !_mesa_is_stencil_format(baseFormat)) {
1225 _mesa_error(ctx, GL_INVALID_OPERATION,
1226 "%s(format mismatch)", caller);
1227 return true;
1228 }
1229 else if (_mesa_is_ycbcr_format(format)
1230 && !_mesa_is_ycbcr_format(baseFormat)) {
1231 _mesa_error(ctx, GL_INVALID_OPERATION,
1232 "%s(format mismatch)", caller);
1233 return true;
1234 }
1235 else if (_mesa_is_depthstencil_format(format)
1236 && !_mesa_is_depthstencil_format(baseFormat)) {
1237 _mesa_error(ctx, GL_INVALID_OPERATION,
1238 "%s(format mismatch)", caller);
1239 return true;
1240 }
1241 else if (!_mesa_is_stencil_format(format) &&
1242 _mesa_is_enum_format_integer(format) !=
1243 _mesa_is_format_integer(texImage->TexFormat)) {
1244 _mesa_error(ctx, GL_INVALID_OPERATION,
1245 "%s(format mismatch)", caller);
1246 return true;
1247 }
1248
1249 return false;
1250 }
1251
1252
1253 /**
1254 * Return the width, height and depth of a texture image.
1255 * This function must be resilient to bad parameter values since
1256 * this is called before full error checking.
1257 */
1258 static void
1259 get_texture_image_dims(const struct gl_texture_object *texObj,
1260 GLenum target, GLint level,
1261 GLsizei *width, GLsizei *height, GLsizei *depth)
1262 {
1263 const struct gl_texture_image *texImage = NULL;
1264
1265 if (level >= 0 && level < MAX_TEXTURE_LEVELS) {
1266 texImage = _mesa_select_tex_image(texObj, target, level);
1267 }
1268
1269 if (texImage) {
1270 *width = texImage->Width;
1271 *height = texImage->Height;
1272 if (target == GL_TEXTURE_CUBE_MAP) {
1273 *depth = 6;
1274 }
1275 else {
1276 *depth = texImage->Depth;
1277 }
1278 }
1279 else {
1280 *width = *height = *depth = 0;
1281 }
1282 }
1283
1284
1285 /**
1286 * Common code for all (uncompressed) get-texture-image functions.
1287 * \param texObj the texture object (should not be null)
1288 * \param target user-provided target, or 0 for DSA
1289 * \param level image level.
1290 * \param format pixel data format for returned image.
1291 * \param type pixel data type for returned image.
1292 * \param bufSize size of the pixels data buffer.
1293 * \param pixels returned pixel data.
1294 * \param caller name of calling function
1295 */
1296 static void
1297 get_texture_image(struct gl_context *ctx,
1298 struct gl_texture_object *texObj,
1299 GLenum target, GLint level,
1300 GLint xoffset, GLint yoffset, GLint zoffset,
1301 GLsizei width, GLsizei height, GLint depth,
1302 GLenum format, GLenum type,
1303 GLvoid *pixels, const char *caller)
1304 {
1305 struct gl_texture_image *texImage;
1306 unsigned firstFace, numFaces, i;
1307 GLint imageStride;
1308
1309 FLUSH_VERTICES(ctx, 0);
1310
1311 texImage = select_tex_image(texObj, target, level, zoffset);
1312 assert(texImage); /* should have been error checked already */
1313
1314 if (_mesa_is_zero_size_texture(texImage)) {
1315 /* no image data to return */
1316 return;
1317 }
1318
1319 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1320 _mesa_debug(ctx, "%s(tex %u) format = %s, w=%d, h=%d,"
1321 " dstFmt=0x%x, dstType=0x%x\n",
1322 caller, texObj->Name,
1323 _mesa_get_format_name(texImage->TexFormat),
1324 texImage->Width, texImage->Height,
1325 format, type);
1326 }
1327
1328 if (target == GL_TEXTURE_CUBE_MAP) {
1329 /* Compute stride between cube faces */
1330 imageStride = _mesa_image_image_stride(&ctx->Pack, width, height,
1331 format, type);
1332 firstFace = zoffset;
1333 numFaces = depth;
1334 zoffset = 0;
1335 depth = 1;
1336 }
1337 else {
1338 imageStride = 0;
1339 firstFace = _mesa_tex_target_to_face(target);
1340 numFaces = 1;
1341 }
1342
1343 _mesa_lock_texture(ctx, texObj);
1344
1345 for (i = 0; i < numFaces; i++) {
1346 texImage = texObj->Image[firstFace + i][level];
1347 assert(texImage);
1348
1349 ctx->Driver.GetTexSubImage(ctx, xoffset, yoffset, zoffset,
1350 width, height, depth,
1351 format, type, pixels, texImage);
1352
1353 /* next cube face */
1354 pixels = (GLubyte *) pixels + imageStride;
1355 }
1356
1357 _mesa_unlock_texture(ctx, texObj);
1358 }
1359
1360
1361 void GLAPIENTRY
1362 _mesa_GetnTexImageARB(GLenum target, GLint level, GLenum format, GLenum type,
1363 GLsizei bufSize, GLvoid *pixels)
1364 {
1365 GET_CURRENT_CONTEXT(ctx);
1366 static const char *caller = "glGetnTexImageARB";
1367 GLsizei width, height, depth;
1368 struct gl_texture_object *texObj;
1369
1370 if (!legal_getteximage_target(ctx, target, false)) {
1371 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1372 return;
1373 }
1374
1375 texObj = _mesa_get_current_tex_object(ctx, target);
1376 assert(texObj);
1377
1378 get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1379
1380 if (getteximage_error_check(ctx, texObj, target, level,
1381 0, 0, 0, width, height, depth,
1382 format, type, bufSize, pixels, caller)) {
1383 return;
1384 }
1385
1386 get_texture_image(ctx, texObj, target, level,
1387 0, 0, 0, width, height, depth,
1388 format, type, pixels, caller);
1389 }
1390
1391
1392 void GLAPIENTRY
1393 _mesa_GetTexImage(GLenum target, GLint level, GLenum format, GLenum type,
1394 GLvoid *pixels )
1395 {
1396 GET_CURRENT_CONTEXT(ctx);
1397 static const char *caller = "glGetTexImage";
1398 GLsizei width, height, depth;
1399 struct gl_texture_object *texObj;
1400
1401 if (!legal_getteximage_target(ctx, target, false)) {
1402 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1403 return;
1404 }
1405
1406 texObj = _mesa_get_current_tex_object(ctx, target);
1407 assert(texObj);
1408
1409 get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1410
1411 if (getteximage_error_check(ctx, texObj, target, level,
1412 0, 0, 0, width, height, depth,
1413 format, type, INT_MAX, pixels, caller)) {
1414 return;
1415 }
1416
1417 get_texture_image(ctx, texObj, target, level,
1418 0, 0, 0, width, height, depth,
1419 format, type, pixels, caller);
1420 }
1421
1422
1423 void GLAPIENTRY
1424 _mesa_GetTextureImage(GLuint texture, GLint level, GLenum format, GLenum type,
1425 GLsizei bufSize, GLvoid *pixels)
1426 {
1427 GET_CURRENT_CONTEXT(ctx);
1428 GLsizei width, height, depth;
1429 static const char *caller = "glGetTextureImage";
1430 struct gl_texture_object *texObj =
1431 _mesa_lookup_texture_err(ctx, texture, caller);
1432
1433 if (!texObj) {
1434 return;
1435 }
1436
1437 get_texture_image_dims(texObj, texObj->Target, level,
1438 &width, &height, &depth);
1439
1440 if (getteximage_error_check(ctx, texObj, texObj->Target, level,
1441 0, 0, 0, width, height, depth,
1442 format, type, bufSize, pixels, caller)) {
1443 return;
1444 }
1445
1446 get_texture_image(ctx, texObj, texObj->Target, level,
1447 0, 0, 0, width, height, depth,
1448 format, type, pixels, caller);
1449 }
1450
1451
1452 void GLAPIENTRY
1453 _mesa_GetTextureSubImage(GLuint texture, GLint level,
1454 GLint xoffset, GLint yoffset, GLint zoffset,
1455 GLsizei width, GLsizei height, GLsizei depth,
1456 GLenum format, GLenum type, GLsizei bufSize,
1457 void *pixels)
1458 {
1459 GET_CURRENT_CONTEXT(ctx);
1460 static const char *caller = "glGetTextureSubImage";
1461 struct gl_texture_object *texObj =
1462 _mesa_lookup_texture_err(ctx, texture, caller);
1463
1464 if (!texObj) {
1465 return;
1466 }
1467
1468 if (getteximage_error_check(ctx, texObj, texObj->Target, level,
1469 xoffset, yoffset, zoffset, width, height, depth,
1470 format, type, bufSize, pixels, caller)) {
1471 return;
1472 }
1473
1474 get_texture_image(ctx, texObj, texObj->Target, level,
1475 xoffset, yoffset, zoffset, width, height, depth,
1476 format, type, pixels, caller);
1477 }
1478
1479
1480
1481 /**
1482 * Compute the number of bytes which will be written when retrieving
1483 * a sub-region of a compressed texture.
1484 */
1485 static GLsizei
1486 packed_compressed_size(GLuint dimensions, mesa_format format,
1487 GLsizei width, GLsizei height, GLsizei depth,
1488 const struct gl_pixelstore_attrib *packing)
1489 {
1490 struct compressed_pixelstore st;
1491 GLsizei totalBytes;
1492
1493 _mesa_compute_compressed_pixelstore(dimensions, format,
1494 width, height, depth,
1495 packing, &st);
1496 totalBytes =
1497 (st.CopySlices - 1) * st.TotalRowsPerSlice * st.TotalBytesPerRow +
1498 st.SkipBytes +
1499 (st.CopyRowsPerSlice - 1) * st.TotalBytesPerRow +
1500 st.CopyBytesPerRow;
1501
1502 return totalBytes;
1503 }
1504
1505
1506 /**
1507 * Do error checking for getting compressed texture images.
1508 * \return true if any error, false if no errors.
1509 */
1510 static bool
1511 getcompressedteximage_error_check(struct gl_context *ctx,
1512 struct gl_texture_object *texObj,
1513 GLenum target, GLint level,
1514 GLint xoffset, GLint yoffset, GLint zoffset,
1515 GLsizei width, GLsizei height, GLsizei depth,
1516 GLsizei bufSize, GLvoid *pixels,
1517 const char *caller)
1518 {
1519 struct gl_texture_image *texImage;
1520 GLint maxLevels;
1521 GLsizei totalBytes;
1522 GLuint dimensions;
1523
1524 assert(texObj);
1525
1526 if (texObj->Target == 0) {
1527 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
1528 return true;
1529 }
1530
1531 maxLevels = _mesa_max_texture_levels(ctx, target);
1532 if (level < 0 || level >= maxLevels) {
1533 _mesa_error(ctx, GL_INVALID_VALUE,
1534 "%s(bad level = %d)", caller, level);
1535 return true;
1536 }
1537
1538 if (dimensions_error_check(ctx, texObj, target, level,
1539 xoffset, yoffset, zoffset,
1540 width, height, depth, caller)) {
1541 return true;
1542 }
1543
1544 texImage = select_tex_image(texObj, target, level, zoffset);
1545 assert(texImage);
1546
1547 if (!_mesa_is_format_compressed(texImage->TexFormat)) {
1548 _mesa_error(ctx, GL_INVALID_OPERATION,
1549 "%s(texture is not compressed)", caller);
1550 return true;
1551 }
1552
1553 /* Check for invalid pixel storage modes */
1554 dimensions = _mesa_get_texture_dimensions(texObj->Target);
1555 if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
1556 &ctx->Pack,
1557 caller)) {
1558 return true;
1559 }
1560
1561 /* Compute number of bytes that may be touched in the dest buffer */
1562 totalBytes = packed_compressed_size(dimensions, texImage->TexFormat,
1563 width, height, depth,
1564 &ctx->Pack);
1565
1566 /* Do dest buffer bounds checking */
1567 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1568 /* do bounds checking on PBO write */
1569 if ((GLubyte *) pixels + totalBytes >
1570 (GLubyte *) ctx->Pack.BufferObj->Size) {
1571 _mesa_error(ctx, GL_INVALID_OPERATION,
1572 "%s(out of bounds PBO access)", caller);
1573 return true;
1574 }
1575
1576 /* make sure PBO is not mapped */
1577 if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1578 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", caller);
1579 return true;
1580 }
1581 }
1582 else {
1583 /* do bounds checking on writing to client memory */
1584 if (totalBytes > bufSize) {
1585 _mesa_error(ctx, GL_INVALID_OPERATION,
1586 "%s(out of bounds access: bufSize (%d) is too small)",
1587 caller, bufSize);
1588 return true;
1589 }
1590 }
1591
1592 if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1593 /* not an error, but do nothing */
1594 return true;
1595 }
1596
1597 return false;
1598 }
1599
1600
1601 /**
1602 * Common helper for all glGetCompressed-teximage functions.
1603 */
1604 static void
1605 get_compressed_texture_image(struct gl_context *ctx,
1606 struct gl_texture_object *texObj,
1607 GLenum target, GLint level,
1608 GLint xoffset, GLint yoffset, GLint zoffset,
1609 GLsizei width, GLsizei height, GLint depth,
1610 GLvoid *pixels,
1611 const char *caller)
1612 {
1613 struct gl_texture_image *texImage;
1614 unsigned firstFace, numFaces, i, imageStride;
1615
1616 FLUSH_VERTICES(ctx, 0);
1617
1618 texImage = select_tex_image(texObj, target, level, zoffset);
1619 assert(texImage); /* should have been error checked already */
1620
1621 if (_mesa_is_zero_size_texture(texImage))
1622 return;
1623
1624 if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1625 _mesa_debug(ctx,
1626 "%s(tex %u) format = %s, w=%d, h=%d\n",
1627 caller, texObj->Name,
1628 _mesa_get_format_name(texImage->TexFormat),
1629 texImage->Width, texImage->Height);
1630 }
1631
1632 if (target == GL_TEXTURE_CUBE_MAP) {
1633 struct compressed_pixelstore store;
1634
1635 /* Compute image stride between cube faces */
1636 _mesa_compute_compressed_pixelstore(2, texImage->TexFormat,
1637 width, height, depth,
1638 &ctx->Pack, &store);
1639 imageStride = store.TotalBytesPerRow * store.TotalRowsPerSlice;
1640
1641 firstFace = zoffset;
1642 numFaces = depth;
1643 zoffset = 0;
1644 depth = 1;
1645 }
1646 else {
1647 imageStride = 0;
1648 firstFace = _mesa_tex_target_to_face(target);
1649 numFaces = 1;
1650 }
1651
1652 _mesa_lock_texture(ctx, texObj);
1653
1654 for (i = 0; i < numFaces; i++) {
1655 texImage = texObj->Image[firstFace + i][level];
1656 assert(texImage);
1657
1658 ctx->Driver.GetCompressedTexSubImage(ctx, texImage,
1659 xoffset, yoffset, zoffset,
1660 width, height, depth, pixels);
1661
1662 /* next cube face */
1663 pixels = (GLubyte *) pixels + imageStride;
1664 }
1665
1666 _mesa_unlock_texture(ctx, texObj);
1667 }
1668
1669
1670 void GLAPIENTRY
1671 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1672 GLvoid *pixels)
1673 {
1674 GET_CURRENT_CONTEXT(ctx);
1675 static const char *caller = "glGetnCompressedTexImageARB";
1676 GLsizei width, height, depth;
1677 struct gl_texture_object *texObj;
1678
1679 if (!legal_getteximage_target(ctx, target, false)) {
1680 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1681 return;
1682 }
1683
1684 texObj = _mesa_get_current_tex_object(ctx, target);
1685 assert(texObj);
1686
1687 get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1688
1689 if (getcompressedteximage_error_check(ctx, texObj, target, level,
1690 0, 0, 0, width, height, depth,
1691 INT_MAX, pixels, caller)) {
1692 return;
1693 }
1694
1695 get_compressed_texture_image(ctx, texObj, target, level,
1696 0, 0, 0, width, height, depth,
1697 pixels, caller);
1698 }
1699
1700
1701 void GLAPIENTRY
1702 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *pixels)
1703 {
1704 GET_CURRENT_CONTEXT(ctx);
1705 static const char *caller = "glGetCompressedTexImage";
1706 GLsizei width, height, depth;
1707 struct gl_texture_object *texObj;
1708
1709 if (!legal_getteximage_target(ctx, target, false)) {
1710 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1711 return;
1712 }
1713
1714 texObj = _mesa_get_current_tex_object(ctx, target);
1715 assert(texObj);
1716
1717 get_texture_image_dims(texObj, target, level,
1718 &width, &height, &depth);
1719
1720 if (getcompressedteximage_error_check(ctx, texObj, target, level,
1721 0, 0, 0, width, height, depth,
1722 INT_MAX, pixels, caller)) {
1723 return;
1724 }
1725
1726 get_compressed_texture_image(ctx, texObj, target, level,
1727 0, 0, 0, width, height, depth,
1728 pixels, caller);
1729 }
1730
1731
1732 void GLAPIENTRY
1733 _mesa_GetCompressedTextureImage(GLuint texture, GLint level,
1734 GLsizei bufSize, GLvoid *pixels)
1735 {
1736 GET_CURRENT_CONTEXT(ctx);
1737 static const char *caller = "glGetCompressedTextureImage";
1738 GLsizei width, height, depth;
1739 struct gl_texture_object *texObj =
1740 _mesa_lookup_texture_err(ctx, texture, caller);
1741
1742 if (!texObj) {
1743 return;
1744 }
1745
1746 get_texture_image_dims(texObj, texObj->Target, level,
1747 &width, &height, &depth);
1748
1749 if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
1750 0, 0, 0, width, height, depth,
1751 bufSize, pixels, caller)) {
1752 return;
1753 }
1754
1755 get_compressed_texture_image(ctx, texObj, texObj->Target, level,
1756 0, 0, 0, width, height, depth,
1757 pixels, caller);
1758 }
1759
1760
1761 void APIENTRY
1762 _mesa_GetCompressedTextureSubImage(GLuint texture, GLint level,
1763 GLint xoffset, GLint yoffset,
1764 GLint zoffset, GLsizei width,
1765 GLsizei height, GLsizei depth,
1766 GLsizei bufSize, void *pixels)
1767 {
1768 GET_CURRENT_CONTEXT(ctx);
1769 static const char *caller = "glGetCompressedTextureImage";
1770 struct gl_texture_object *texObj;
1771
1772 texObj = _mesa_lookup_texture_err(ctx, texture, caller);
1773 if (!texObj) {
1774 return;
1775 }
1776
1777 if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
1778 xoffset, yoffset, zoffset,
1779 width, height, depth,
1780 bufSize, pixels, caller)) {
1781 return;
1782 }
1783
1784 get_compressed_texture_image(ctx, texObj, texObj->Target, level,
1785 xoffset, yoffset, zoffset,
1786 width, height, depth,
1787 pixels, caller);
1788 }