OSDN Git Service

Merge remote-tracking branch 'mesa/12.0' into marshmallow-x86
[android-x86/external-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 {
506             need_convert = true;
507             if (rgba == NULL) { /* Allocate the RGBA buffer only once */
508                rgba = malloc(height * rgba_stride);
509                if (!rgba) {
510                   _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
511                   ctx->Driver.UnmapTextureImage(ctx, texImage, img);
512                   return;
513                }
514             }
515          }
516
517          _mesa_format_convert(rgba, rgba_format, rgba_stride,
518                               img_src, texFormat, rowstride,
519                               width, height,
520                               needsRebase ? rebaseSwizzle : NULL);
521
522          /* Handle transfer ops now */
523          _mesa_apply_rgba_transfer_ops(ctx, transferOps, width * height, rgba);
524
525          /* If we had to rebase, we have already handled that */
526          needsRebase = false;
527
528          /* If we were lucky and our RGBA conversion matches the dst format,
529           * then we are done.
530           */
531          if (!need_convert)
532             goto do_swap;
533
534          /* Otherwise, we need to convert from RGBA to dst next */
535          src = rgba;
536          src_format = rgba_format;
537          src_stride = rgba_stride;
538       } else {
539          /* No RGBA conversion needed, convert directly to dst */
540          src = img_src;
541          src_format = texFormat;
542          src_stride = rowstride;
543       }
544
545       /* Do the conversion to destination format */
546       _mesa_format_convert(dest, dst_format, dst_stride,
547                            src, src_format, src_stride,
548                            width, height,
549                            needsRebase ? rebaseSwizzle : NULL);
550
551    do_swap:
552       /* Handle byte swapping if required */
553       if (ctx->Pack.SwapBytes)
554          _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
555                                    width, height, dest, dest);
556
557       /* Unmap the src texture buffer */
558       ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
559    }
560
561 done:
562    free(rgba);
563 }
564
565
566 /**
567  * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
568  * Compressed textures are handled here as well.
569  */
570 static void
571 get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
572              GLint xoffset, GLint yoffset, GLint zoffset,
573              GLsizei width, GLsizei height, GLint depth,
574              GLenum format, GLenum type, GLvoid *pixels,
575              struct gl_texture_image *texImage)
576 {
577    const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
578    GLbitfield transferOps = 0x0;
579
580    /* In general, clamping does not apply to glGetTexImage, except when
581     * the returned type of the image can't hold negative values.
582     */
583    if (type_needs_clamping(type)) {
584       /* the returned image type can't have negative values */
585       if (dataType == GL_FLOAT ||
586           dataType == GL_HALF_FLOAT ||
587           dataType == GL_SIGNED_NORMALIZED ||
588           format == GL_LUMINANCE ||
589           format == GL_LUMINANCE_ALPHA) {
590          transferOps |= IMAGE_CLAMP_BIT;
591       }
592    }
593
594    if (_mesa_is_format_compressed(texImage->TexFormat)) {
595       get_tex_rgba_compressed(ctx, dimensions,
596                               xoffset, yoffset, zoffset,
597                               width, height, depth,
598                               format, type,
599                               pixels, texImage, transferOps);
600    }
601    else {
602       get_tex_rgba_uncompressed(ctx, dimensions,
603                                 xoffset, yoffset, zoffset,
604                                 width, height, depth,
605                                 format, type,
606                                 pixels, texImage, transferOps);
607    }
608 }
609
610
611 /**
612  * Try to do glGetTexImage() with simple memcpy().
613  * \return GL_TRUE if done, GL_FALSE otherwise
614  */
615 static GLboolean
616 get_tex_memcpy(struct gl_context *ctx,
617                GLint xoffset, GLint yoffset, GLint zoffset,
618                GLsizei width, GLsizei height, GLint depth,
619                GLenum format, GLenum type, GLvoid *pixels,
620                struct gl_texture_image *texImage)
621 {
622    const GLenum target = texImage->TexObject->Target;
623    GLboolean memCopy = GL_FALSE;
624    GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
625
626    /*
627     * Check if we can use memcpy to copy from the hardware texture
628     * format to the user's format/type.
629     * Note that GL's pixel transfer ops don't apply to glGetTexImage()
630     */
631    if ((target == GL_TEXTURE_1D ||
632         target == GL_TEXTURE_2D ||
633         target == GL_TEXTURE_RECTANGLE ||
634         _mesa_is_cube_face(target)) &&
635        texBaseFormat == texImage->_BaseFormat) {
636       memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
637                                                      format, type,
638                                                      ctx->Pack.SwapBytes, NULL);
639    }
640
641    if (depth > 1) {
642       /* only a single slice is supported at this time */
643       memCopy = FALSE;
644    }
645
646    if (memCopy) {
647       const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
648       const GLint bytesPerRow = width * bpp;
649       GLubyte *dst =
650          _mesa_image_address2d(&ctx->Pack, pixels, width, height,
651                                format, type, 0, 0);
652       const GLint dstRowStride =
653          _mesa_image_row_stride(&ctx->Pack, width, format, type);
654       GLubyte *src;
655       GLint srcRowStride;
656
657       /* map src texture buffer */
658       ctx->Driver.MapTextureImage(ctx, texImage, zoffset,
659                                   xoffset, yoffset, width, height,
660                                   GL_MAP_READ_BIT, &src, &srcRowStride);
661
662       if (src) {
663          if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
664             memcpy(dst, src, bytesPerRow * texImage->Height);
665          }
666          else {
667             GLuint row;
668             for (row = 0; row < height; row++) {
669                memcpy(dst, src, bytesPerRow);
670                dst += dstRowStride;
671                src += srcRowStride;
672             }
673          }
674
675          /* unmap src texture buffer */
676          ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset);
677       }
678       else {
679          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
680       }
681    }
682
683    return memCopy;
684 }
685
686
687 /**
688  * This is the software fallback for Driver.GetTexSubImage().
689  * All error checking will have been done before this routine is called.
690  * We'll call ctx->Driver.MapTextureImage() to access the data, then
691  * unmap with ctx->Driver.UnmapTextureImage().
692  */
693 void
694 _mesa_GetTexSubImage_sw(struct gl_context *ctx,
695                         GLint xoffset, GLint yoffset, GLint zoffset,
696                         GLsizei width, GLsizei height, GLint depth,
697                         GLenum format, GLenum type, GLvoid *pixels,
698                         struct gl_texture_image *texImage)
699 {
700    const GLuint dimensions =
701       _mesa_get_texture_dimensions(texImage->TexObject->Target);
702
703    /* map dest buffer, if PBO */
704    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
705       /* Packing texture image into a PBO.
706        * Map the (potentially) VRAM-based buffer into our process space so
707        * we can write into it with the code below.
708        * A hardware driver might use a sophisticated blit to move the
709        * texture data to the PBO if the PBO is in VRAM along with the texture.
710        */
711       GLubyte *buf = (GLubyte *)
712          ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
713                                     GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
714                                     MAP_INTERNAL);
715       if (!buf) {
716          /* out of memory or other unexpected error */
717          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
718          return;
719       }
720       /* <pixels> was an offset into the PBO.
721        * Now make it a real, client-side pointer inside the mapped region.
722        */
723       pixels = ADD_POINTERS(buf, pixels);
724    }
725
726    /* for all array textures, the Z axis selects the layer */
727    if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
728       depth = height;
729       height = 1;
730       zoffset = yoffset;
731       yoffset = 0;
732       assert(zoffset + depth <= texImage->Height);
733    } else {
734       assert(zoffset + depth <= texImage->Depth);
735    }
736
737    if (get_tex_memcpy(ctx, xoffset, yoffset, zoffset, width, height, depth,
738                       format, type, pixels, texImage)) {
739       /* all done */
740    }
741    else if (format == GL_DEPTH_COMPONENT) {
742       get_tex_depth(ctx, dimensions, xoffset, yoffset, zoffset,
743                     width, height, depth, format, type, pixels, texImage);
744    }
745    else if (format == GL_DEPTH_STENCIL_EXT) {
746       get_tex_depth_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
747                             width, height, depth, format, type, pixels,
748                             texImage);
749    }
750    else if (format == GL_STENCIL_INDEX) {
751       get_tex_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
752                       width, height, depth, format, type, pixels, texImage);
753    }
754    else if (format == GL_YCBCR_MESA) {
755       get_tex_ycbcr(ctx, dimensions, xoffset, yoffset, zoffset,
756                     width, height, depth, format, type, pixels, texImage);
757    }
758    else {
759       get_tex_rgba(ctx, dimensions, xoffset, yoffset, zoffset,
760                    width, height, depth, format, type, pixels, texImage);
761    }
762
763    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
764       ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
765    }
766 }
767
768
769
770 /**
771  * This is the software fallback for Driver.GetCompressedTexSubImage().
772  * All error checking will have been done before this routine is called.
773  */
774 void
775 _mesa_GetCompressedTexSubImage_sw(struct gl_context *ctx,
776                                   struct gl_texture_image *texImage,
777                                   GLint xoffset, GLint yoffset,
778                                   GLint zoffset, GLsizei width,
779                                   GLint height, GLint depth,
780                                   GLvoid *img)
781 {
782    const GLuint dimensions =
783       _mesa_get_texture_dimensions(texImage->TexObject->Target);
784    struct compressed_pixelstore store;
785    GLint slice;
786    GLubyte *dest;
787
788    _mesa_compute_compressed_pixelstore(dimensions, texImage->TexFormat,
789                                        width, height, depth,
790                                        &ctx->Pack, &store);
791
792    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
793       /* pack texture image into a PBO */
794       dest = (GLubyte *)
795          ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
796                                     GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
797                                     MAP_INTERNAL);
798       if (!dest) {
799          /* out of memory or other unexpected error */
800          _mesa_error(ctx, GL_OUT_OF_MEMORY,
801                      "glGetCompresssedTexImage(map PBO failed)");
802          return;
803       }
804       dest = ADD_POINTERS(dest, img);
805    } else {
806       dest = img;
807    }
808
809    dest += store.SkipBytes;
810
811    for (slice = 0; slice < store.CopySlices; slice++) {
812       GLint srcRowStride;
813       GLubyte *src;
814
815       /* map src texture buffer */
816       ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
817                                   xoffset, yoffset, width, height,
818                                   GL_MAP_READ_BIT, &src, &srcRowStride);
819
820       if (src) {
821          GLint i;
822          for (i = 0; i < store.CopyRowsPerSlice; i++) {
823             memcpy(dest, src, store.CopyBytesPerRow);
824             dest += store.TotalBytesPerRow;
825             src += srcRowStride;
826          }
827
828          ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
829
830          /* Advance to next slice */
831          dest += store.TotalBytesPerRow * (store.TotalRowsPerSlice -
832                                            store.CopyRowsPerSlice);
833
834       } else {
835          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
836       }
837    }
838
839    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
840       ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
841    }
842 }
843
844
845 /**
846  * Validate the texture target enum supplied to glGetTex(ture)Image or
847  * glGetCompressedTex(ture)Image.
848  */
849 static GLboolean
850 legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
851 {
852    switch (target) {
853    case GL_TEXTURE_1D:
854    case GL_TEXTURE_2D:
855    case GL_TEXTURE_3D:
856       return GL_TRUE;
857    case GL_TEXTURE_RECTANGLE_NV:
858       return ctx->Extensions.NV_texture_rectangle;
859    case GL_TEXTURE_1D_ARRAY_EXT:
860    case GL_TEXTURE_2D_ARRAY_EXT:
861       return ctx->Extensions.EXT_texture_array;
862    case GL_TEXTURE_CUBE_MAP_ARRAY:
863       return ctx->Extensions.ARB_texture_cube_map_array;
864
865    /* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
866     * (30.10.2014) says:
867     *    "An INVALID_ENUM error is generated if the effective target is not
868     *    one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
869     *    TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
870     *    the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
871     *    or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
872     */
873    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
874    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
875    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
876    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
877    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
878    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
879       return dsa ? GL_FALSE : ctx->Extensions.ARB_texture_cube_map;
880    case GL_TEXTURE_CUBE_MAP:
881       return dsa ? GL_TRUE : GL_FALSE;
882    default:
883       return GL_FALSE;
884    }
885 }
886
887
888 /**
889  * Wrapper for _mesa_select_tex_image() which can handle target being
890  * GL_TEXTURE_CUBE_MAP in which case we use zoffset to select a cube face.
891  * This can happen for glGetTextureImage and glGetTextureSubImage (DSA
892  * functions).
893  */
894 static struct gl_texture_image *
895 select_tex_image(const struct gl_texture_object *texObj, GLenum target,
896                  GLint level, GLint zoffset)
897 {
898    assert(level >= 0);
899    assert(level < MAX_TEXTURE_LEVELS);
900    if (target == GL_TEXTURE_CUBE_MAP) {
901       assert(zoffset >= 0);
902       assert(zoffset < 6);
903       target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset;
904    }
905    return _mesa_select_tex_image(texObj, target, level);
906 }
907
908
909 /**
910  * Error-check the offset and size arguments to
911  * glGet[Compressed]TextureSubImage().  Also checks if the specified
912  * texture image is missing.
913  * \return true if error, false if no error.
914  */
915 static bool
916 dimensions_error_check(struct gl_context *ctx,
917                        struct gl_texture_object *texObj,
918                        GLenum target, GLint level,
919                        GLint xoffset, GLint yoffset, GLint zoffset,
920                        GLsizei width, GLsizei height, GLsizei depth,
921                        const char *caller)
922 {
923    const struct gl_texture_image *texImage;
924    int i;
925
926    if (xoffset < 0) {
927       _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset = %d)", caller, xoffset);
928       return true;
929    }
930
931    if (yoffset < 0) {
932       _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset = %d)", caller, yoffset);
933       return true;
934    }
935
936    if (zoffset < 0) {
937       _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset = %d)", caller, zoffset);
938       return true;
939    }
940
941    if (width < 0) {
942       _mesa_error(ctx, GL_INVALID_VALUE, "%s(width = %d)", caller, width);
943       return true;
944    }
945
946    if (height < 0) {
947       _mesa_error(ctx, GL_INVALID_VALUE, "%s(height = %d)", caller, height);
948       return true;
949    }
950
951    if (depth < 0) {
952       _mesa_error(ctx, GL_INVALID_VALUE, "%s(depth = %d)", caller, depth);
953       return true;
954    }
955
956    /* do special per-target checks */
957    switch (target) {
958    case GL_TEXTURE_1D:
959       if (yoffset != 0) {
960          _mesa_error(ctx, GL_INVALID_VALUE,
961                      "%s(1D, yoffset = %d)", caller, yoffset);
962          return true;
963       }
964       if (height > 1) {
965          _mesa_error(ctx, GL_INVALID_VALUE,
966                      "%s(1D, height = %d)", caller, height);
967          return true;
968       }
969       /* fall-through */
970    case GL_TEXTURE_1D_ARRAY:
971    case GL_TEXTURE_2D:
972    case GL_TEXTURE_RECTANGLE:
973       if (zoffset != 0) {
974          _mesa_error(ctx, GL_INVALID_VALUE,
975                      "%s(zoffset = %d)", caller, zoffset);
976          return true;
977       }
978       if (depth > 1) {
979          _mesa_error(ctx, GL_INVALID_VALUE,
980                      "%s(depth = %d)", caller, depth);
981          return true;
982       }
983       break;
984    case GL_TEXTURE_CUBE_MAP:
985       /* Non-array cube maps are special because we have a gl_texture_image
986        * per face.
987        */
988       if (zoffset + depth > 6) {
989          _mesa_error(ctx, GL_INVALID_VALUE,
990                      "%s(zoffset + depth = %d)", caller, zoffset + depth);
991          return true;
992       }
993       /* check that the range of faces exist */
994       for (i = 0; i < depth; i++) {
995          GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset + i;
996          if (!_mesa_select_tex_image(texObj, face, level)) {
997             /* non-existant face */
998             _mesa_error(ctx, GL_INVALID_OPERATION,
999                         "%s(missing cube face)", caller);
1000             return true;
1001          }
1002       }
1003       break;
1004    default:
1005       ; /* nothing */
1006    }
1007
1008    texImage = select_tex_image(texObj, target, level, zoffset);
1009    if (!texImage) {
1010       /* missing texture image */
1011       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(missing image)", caller);
1012       return true;
1013    }
1014
1015    if (xoffset + width > texImage->Width) {
1016       _mesa_error(ctx, GL_INVALID_VALUE,
1017                   "%s(xoffset %d + width %d > %u)",
1018                   caller, xoffset, width, texImage->Width);
1019       return true;
1020    }
1021
1022    if (yoffset + height > texImage->Height) {
1023       _mesa_error(ctx, GL_INVALID_VALUE,
1024                   "%s(yoffset %d + height %d > %u)",
1025                   caller, yoffset, height, texImage->Height);
1026       return true;
1027    }
1028
1029    if (target != GL_TEXTURE_CUBE_MAP) {
1030       /* Cube map error checking was done above */
1031       if (zoffset + depth > texImage->Depth) {
1032          _mesa_error(ctx, GL_INVALID_VALUE,
1033                      "%s(zoffset %d + depth %d > %u)",
1034                      caller, zoffset, depth, texImage->Depth);
1035          return true;
1036       }
1037    }
1038
1039    /* Extra checks for compressed textures */
1040    {
1041       GLuint bw, bh, bd;
1042       _mesa_get_format_block_size_3d(texImage->TexFormat, &bw, &bh, &bd);
1043       if (bw > 1 || bh > 1 || bd > 1) {
1044          /* offset must be multiple of block size */
1045          if (xoffset % bw != 0) {
1046             _mesa_error(ctx, GL_INVALID_VALUE,
1047                         "%s(xoffset = %d)", caller, xoffset);
1048             return true;
1049          }
1050          if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY) {
1051             if (yoffset % bh != 0) {
1052                _mesa_error(ctx, GL_INVALID_VALUE,
1053                            "%s(yoffset = %d)", caller, yoffset);
1054                return true;
1055             }
1056          }
1057
1058          if (zoffset % bd != 0) {
1059             _mesa_error(ctx, GL_INVALID_VALUE,
1060                         "%s(zoffset = %d)", caller, zoffset);
1061             return true;
1062          }
1063
1064          /* The size must be a multiple of bw x bh x bd, or we must be using a
1065           * offset+size that exactly hits the edge of the image.
1066           */
1067          if ((width % bw != 0) &&
1068              (xoffset + width != (GLint) texImage->Width)) {
1069             _mesa_error(ctx, GL_INVALID_VALUE,
1070                         "%s(width = %d)", caller, width);
1071             return true;
1072          }
1073
1074          if ((height % bh != 0) &&
1075              (yoffset + height != (GLint) texImage->Height)) {
1076             _mesa_error(ctx, GL_INVALID_VALUE,
1077                         "%s(height = %d)", caller, height);
1078             return true;
1079          }
1080
1081          if ((depth % bd != 0) &&
1082              (zoffset + depth != (GLint) texImage->Depth)) {
1083             _mesa_error(ctx, GL_INVALID_VALUE,
1084                         "%s(depth = %d)", caller, depth);
1085             return true;
1086          }
1087       }
1088    }
1089
1090    if (width == 0 || height == 0 || depth == 0) {
1091       /* Not an error, but nothing to do.  Return 'true' so that the
1092        * caller simply returns.
1093        */
1094       return true;
1095    }
1096
1097    return false;
1098 }
1099
1100
1101 /**
1102  * Do PBO-related error checking for getting uncompressed images.
1103  * \return true if there was an error (or the GetTexImage is to be a no-op)
1104  */
1105 static bool
1106 pbo_error_check(struct gl_context *ctx, GLenum target,
1107                 GLsizei width, GLsizei height, GLsizei depth,
1108                 GLenum format, GLenum type, GLsizei clientMemSize,
1109                 GLvoid *pixels,
1110                 const char *caller)
1111 {
1112    const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
1113
1114    if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, width, height, depth,
1115                                   format, type, clientMemSize, pixels)) {
1116       if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1117          _mesa_error(ctx, GL_INVALID_OPERATION,
1118                      "%s(out of bounds PBO access)", caller);
1119       } else {
1120          _mesa_error(ctx, GL_INVALID_OPERATION,
1121                      "%s(out of bounds access: bufSize (%d) is too small)",
1122                      caller, clientMemSize);
1123       }
1124       return true;
1125    }
1126
1127    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1128       /* PBO should not be mapped */
1129       if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1130          _mesa_error(ctx, GL_INVALID_OPERATION,
1131                      "%s(PBO is mapped)", caller);
1132          return true;
1133       }
1134    }
1135
1136    if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1137       /* not an error, do nothing */
1138       return true;
1139    }
1140
1141    return false;
1142 }
1143
1144
1145 /**
1146  * Do error checking for all (non-compressed) get-texture-image functions.
1147  * \return true if any error, false if no errors.
1148  */
1149 static bool
1150 getteximage_error_check(struct gl_context *ctx,
1151                         struct gl_texture_object *texObj,
1152                         GLenum target, GLint level,
1153                         GLint xoffset, GLint yoffset, GLint zoffset,
1154                         GLsizei width, GLsizei height, GLsizei depth,
1155                         GLenum format, GLenum type, GLsizei bufSize,
1156                         GLvoid *pixels, const char *caller)
1157 {
1158    struct gl_texture_image *texImage;
1159    GLenum baseFormat, err;
1160    GLint maxLevels;
1161
1162    assert(texObj);
1163
1164    if (texObj->Target == 0) {
1165       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
1166       return true;
1167    }
1168
1169    maxLevels = _mesa_max_texture_levels(ctx, target);
1170    if (level < 0 || level >= maxLevels) {
1171       _mesa_error(ctx, GL_INVALID_VALUE, "%s(level = %d)", caller, level);
1172       return true;
1173    }
1174
1175    err = _mesa_error_check_format_and_type(ctx, format, type);
1176    if (err != GL_NO_ERROR) {
1177       _mesa_error(ctx, err, "%s(format/type)", caller);
1178       return true;
1179    }
1180
1181    if (dimensions_error_check(ctx, texObj, target, level,
1182                               xoffset, yoffset, zoffset,
1183                               width, height, depth, caller)) {
1184       return true;
1185    }
1186
1187    if (pbo_error_check(ctx, target, width, height, depth,
1188                        format, type, bufSize, pixels, caller)) {
1189       return true;
1190    }
1191
1192    texImage = select_tex_image(texObj, target, level, zoffset);
1193    assert(texImage);
1194
1195    /*
1196     * Format and type checking has been moved up to GetnTexImage and
1197     * GetTextureImage so that it happens before getting the texImage object.
1198     */
1199
1200    baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
1201
1202    /* Make sure the requested image format is compatible with the
1203     * texture's format.
1204     */
1205    if (_mesa_is_color_format(format)
1206        && !_mesa_is_color_format(baseFormat)) {
1207       _mesa_error(ctx, GL_INVALID_OPERATION,
1208                   "%s(format mismatch)", caller);
1209       return true;
1210    }
1211    else if (_mesa_is_depth_format(format)
1212             && !_mesa_is_depth_format(baseFormat)
1213             && !_mesa_is_depthstencil_format(baseFormat)) {
1214       _mesa_error(ctx, GL_INVALID_OPERATION,
1215                   "%s(format mismatch)", caller);
1216       return true;
1217    }
1218    else if (_mesa_is_stencil_format(format)
1219             && !ctx->Extensions.ARB_texture_stencil8) {
1220       _mesa_error(ctx, GL_INVALID_ENUM,
1221                   "%s(format=GL_STENCIL_INDEX)", caller);
1222       return true;
1223    }
1224    else if (_mesa_is_stencil_format(format)
1225             && !_mesa_is_depthstencil_format(baseFormat)
1226             && !_mesa_is_stencil_format(baseFormat)) {
1227       _mesa_error(ctx, GL_INVALID_OPERATION,
1228                   "%s(format mismatch)", caller);
1229       return true;
1230    }
1231    else if (_mesa_is_ycbcr_format(format)
1232             && !_mesa_is_ycbcr_format(baseFormat)) {
1233       _mesa_error(ctx, GL_INVALID_OPERATION,
1234                   "%s(format mismatch)", caller);
1235       return true;
1236    }
1237    else if (_mesa_is_depthstencil_format(format)
1238             && !_mesa_is_depthstencil_format(baseFormat)) {
1239       _mesa_error(ctx, GL_INVALID_OPERATION,
1240                   "%s(format mismatch)", caller);
1241       return true;
1242    }
1243    else if (!_mesa_is_stencil_format(format) &&
1244             _mesa_is_enum_format_integer(format) !=
1245             _mesa_is_format_integer(texImage->TexFormat)) {
1246       _mesa_error(ctx, GL_INVALID_OPERATION,
1247                   "%s(format mismatch)", caller);
1248       return true;
1249    }
1250
1251    return false;
1252 }
1253
1254
1255 /**
1256  * Return the width, height and depth of a texture image.
1257  * This function must be resilient to bad parameter values since
1258  * this is called before full error checking.
1259  */
1260 static void
1261 get_texture_image_dims(const struct gl_texture_object *texObj,
1262                        GLenum target, GLint level,
1263                        GLsizei *width, GLsizei *height, GLsizei *depth)
1264 {
1265    const struct gl_texture_image *texImage = NULL;
1266
1267    if (level >= 0 && level < MAX_TEXTURE_LEVELS) {
1268       texImage = _mesa_select_tex_image(texObj, target, level);
1269    }
1270
1271    if (texImage) {
1272       *width = texImage->Width;
1273       *height = texImage->Height;
1274       if (target == GL_TEXTURE_CUBE_MAP) {
1275          *depth = 6;
1276       }
1277       else {
1278          *depth = texImage->Depth;
1279       }
1280    }
1281    else {
1282       *width = *height = *depth = 0;
1283    }
1284 }
1285
1286
1287 /**
1288  * Common code for all (uncompressed) get-texture-image functions.
1289  * \param texObj  the texture object (should not be null)
1290  * \param target  user-provided target, or 0 for DSA
1291  * \param level image level.
1292  * \param format pixel data format for returned image.
1293  * \param type pixel data type for returned image.
1294  * \param bufSize size of the pixels data buffer.
1295  * \param pixels returned pixel data.
1296  * \param caller  name of calling function
1297  */
1298 static void
1299 get_texture_image(struct gl_context *ctx,
1300                   struct gl_texture_object *texObj,
1301                   GLenum target, GLint level,
1302                   GLint xoffset, GLint yoffset, GLint zoffset,
1303                   GLsizei width, GLsizei height, GLint depth,
1304                   GLenum format, GLenum type,
1305                   GLvoid *pixels, const char *caller)
1306 {
1307    struct gl_texture_image *texImage;
1308    unsigned firstFace, numFaces, i;
1309    GLint imageStride;
1310
1311    FLUSH_VERTICES(ctx, 0);
1312
1313    texImage = select_tex_image(texObj, target, level, zoffset);
1314    assert(texImage);  /* should have been error checked already */
1315
1316    if (_mesa_is_zero_size_texture(texImage)) {
1317       /* no image data to return */
1318       return;
1319    }
1320
1321    if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1322       _mesa_debug(ctx, "%s(tex %u) format = %s, w=%d, h=%d,"
1323                   " dstFmt=0x%x, dstType=0x%x\n",
1324                   caller, texObj->Name,
1325                   _mesa_get_format_name(texImage->TexFormat),
1326                   texImage->Width, texImage->Height,
1327                   format, type);
1328    }
1329
1330    if (target == GL_TEXTURE_CUBE_MAP) {
1331       /* Compute stride between cube faces */
1332       imageStride = _mesa_image_image_stride(&ctx->Pack, width, height,
1333                                              format, type);
1334       firstFace = zoffset;
1335       numFaces = depth;
1336       zoffset = 0;
1337       depth = 1;
1338    }
1339    else {
1340       imageStride = 0;
1341       firstFace = _mesa_tex_target_to_face(target);
1342       numFaces = 1;
1343    }
1344
1345    _mesa_lock_texture(ctx, texObj);
1346
1347    for (i = 0; i < numFaces; i++) {
1348       texImage = texObj->Image[firstFace + i][level];
1349       assert(texImage);
1350
1351       ctx->Driver.GetTexSubImage(ctx, xoffset, yoffset, zoffset,
1352                                  width, height, depth,
1353                                  format, type, pixels, texImage);
1354
1355       /* next cube face */
1356       pixels = (GLubyte *) pixels + imageStride;
1357    }
1358
1359    _mesa_unlock_texture(ctx, texObj);
1360 }
1361
1362
1363 void GLAPIENTRY
1364 _mesa_GetnTexImageARB(GLenum target, GLint level, GLenum format, GLenum type,
1365                       GLsizei bufSize, GLvoid *pixels)
1366 {
1367    GET_CURRENT_CONTEXT(ctx);
1368    static const char *caller = "glGetnTexImageARB";
1369    GLsizei width, height, depth;
1370    struct gl_texture_object *texObj;
1371
1372    if (!legal_getteximage_target(ctx, target, false)) {
1373       _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1374       return;
1375    }
1376
1377    texObj = _mesa_get_current_tex_object(ctx, target);
1378    assert(texObj);
1379
1380    get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1381
1382    if (getteximage_error_check(ctx, texObj, target, level,
1383                                0, 0, 0, width, height, depth,
1384                                format, type, bufSize, pixels, caller)) {
1385       return;
1386    }
1387
1388    get_texture_image(ctx, texObj, target, level,
1389                      0, 0, 0, width, height, depth,
1390                      format, type, pixels, caller);
1391 }
1392
1393
1394 void GLAPIENTRY
1395 _mesa_GetTexImage(GLenum target, GLint level, GLenum format, GLenum type,
1396                   GLvoid *pixels )
1397 {
1398    GET_CURRENT_CONTEXT(ctx);
1399    static const char *caller = "glGetTexImage";
1400    GLsizei width, height, depth;
1401    struct gl_texture_object *texObj;
1402
1403    if (!legal_getteximage_target(ctx, target, false)) {
1404       _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1405       return;
1406    }
1407
1408    texObj = _mesa_get_current_tex_object(ctx, target);
1409    assert(texObj);
1410
1411    get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1412
1413    if (getteximage_error_check(ctx, texObj, target, level,
1414                                0, 0, 0, width, height, depth,
1415                                format, type, INT_MAX, pixels, caller)) {
1416       return;
1417    }
1418
1419    get_texture_image(ctx, texObj, target, level,
1420                      0, 0, 0, width, height, depth,
1421                      format, type, pixels, caller);
1422 }
1423
1424
1425 void GLAPIENTRY
1426 _mesa_GetTextureImage(GLuint texture, GLint level, GLenum format, GLenum type,
1427                       GLsizei bufSize, GLvoid *pixels)
1428 {
1429    GET_CURRENT_CONTEXT(ctx);
1430    GLsizei width, height, depth;
1431    static const char *caller = "glGetTextureImage";
1432    struct gl_texture_object *texObj =
1433       _mesa_lookup_texture_err(ctx, texture, caller);
1434
1435    if (!texObj) {
1436       return;
1437    }
1438
1439    get_texture_image_dims(texObj, texObj->Target, level,
1440                           &width, &height, &depth);
1441
1442    if (getteximage_error_check(ctx, texObj, texObj->Target, level,
1443                                0, 0, 0, width, height, depth,
1444                                format, type, bufSize, pixels, caller)) {
1445       return;
1446    }
1447
1448    get_texture_image(ctx, texObj, texObj->Target, level,
1449                      0, 0, 0, width, height, depth,
1450                      format, type, pixels, caller);
1451 }
1452
1453
1454 void GLAPIENTRY
1455 _mesa_GetTextureSubImage(GLuint texture, GLint level,
1456                          GLint xoffset, GLint yoffset, GLint zoffset,
1457                          GLsizei width, GLsizei height, GLsizei depth,
1458                          GLenum format, GLenum type, GLsizei bufSize,
1459                          void *pixels)
1460 {
1461    GET_CURRENT_CONTEXT(ctx);
1462    static const char *caller = "glGetTextureSubImage";
1463    struct gl_texture_object *texObj =
1464       _mesa_lookup_texture_err(ctx, texture, caller);
1465
1466    if (!texObj) {
1467       return;
1468    }
1469
1470    if (getteximage_error_check(ctx, texObj, texObj->Target, level,
1471                                xoffset, yoffset, zoffset, width, height, depth,
1472                                format, type, bufSize, pixels, caller)) {
1473       return;
1474    }
1475
1476    get_texture_image(ctx, texObj, texObj->Target, level,
1477                      xoffset, yoffset, zoffset, width, height, depth,
1478                      format, type, pixels, caller);
1479 }
1480
1481
1482
1483 /**
1484  * Compute the number of bytes which will be written when retrieving
1485  * a sub-region of a compressed texture.
1486  */
1487 static GLsizei
1488 packed_compressed_size(GLuint dimensions, mesa_format format,
1489                        GLsizei width, GLsizei height, GLsizei depth,
1490                        const struct gl_pixelstore_attrib *packing)
1491 {
1492    struct compressed_pixelstore st;
1493    GLsizei totalBytes;
1494
1495    _mesa_compute_compressed_pixelstore(dimensions, format,
1496                                        width, height, depth,
1497                                        packing, &st);
1498    totalBytes =
1499       (st.CopySlices - 1) * st.TotalRowsPerSlice * st.TotalBytesPerRow +
1500       st.SkipBytes +
1501       (st.CopyRowsPerSlice - 1) * st.TotalBytesPerRow +
1502       st.CopyBytesPerRow;
1503
1504    return totalBytes;
1505 }
1506
1507
1508 /**
1509  * Do error checking for getting compressed texture images.
1510  * \return true if any error, false if no errors.
1511  */
1512 static bool
1513 getcompressedteximage_error_check(struct gl_context *ctx,
1514                                   struct gl_texture_object *texObj,
1515                                   GLenum target, GLint level,
1516                                   GLint xoffset, GLint yoffset, GLint zoffset,
1517                                   GLsizei width, GLsizei height, GLsizei depth,
1518                                   GLsizei bufSize, GLvoid *pixels,
1519                                   const char *caller)
1520 {
1521    struct gl_texture_image *texImage;
1522    GLint maxLevels;
1523    GLsizei totalBytes;
1524    GLuint dimensions;
1525
1526    assert(texObj);
1527
1528    if (texObj->Target == 0) {
1529       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
1530       return true;
1531    }
1532
1533    maxLevels = _mesa_max_texture_levels(ctx, target);
1534    if (level < 0 || level >= maxLevels) {
1535       _mesa_error(ctx, GL_INVALID_VALUE,
1536                   "%s(bad level = %d)", caller, level);
1537       return true;
1538    }
1539
1540    if (dimensions_error_check(ctx, texObj, target, level,
1541                               xoffset, yoffset, zoffset,
1542                               width, height, depth, caller)) {
1543       return true;
1544    }
1545
1546    texImage = select_tex_image(texObj, target, level, zoffset);
1547    assert(texImage);
1548
1549    if (!_mesa_is_format_compressed(texImage->TexFormat)) {
1550       _mesa_error(ctx, GL_INVALID_OPERATION,
1551                   "%s(texture is not compressed)", caller);
1552       return true;
1553    }
1554
1555    /* Check for invalid pixel storage modes */
1556    dimensions = _mesa_get_texture_dimensions(texObj->Target);
1557    if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
1558                                                    &ctx->Pack,
1559                                                    caller)) {
1560       return true;
1561    }
1562
1563    /* Compute number of bytes that may be touched in the dest buffer */
1564    totalBytes = packed_compressed_size(dimensions, texImage->TexFormat,
1565                                        width, height, depth,
1566                                        &ctx->Pack);
1567
1568    /* Do dest buffer bounds checking */
1569    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1570       /* do bounds checking on PBO write */
1571       if ((GLubyte *) pixels + totalBytes >
1572           (GLubyte *) ctx->Pack.BufferObj->Size) {
1573          _mesa_error(ctx, GL_INVALID_OPERATION,
1574                      "%s(out of bounds PBO access)", caller);
1575          return true;
1576       }
1577
1578       /* make sure PBO is not mapped */
1579       if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1580          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", caller);
1581          return true;
1582       }
1583    }
1584    else {
1585       /* do bounds checking on writing to client memory */
1586       if (totalBytes > bufSize) {
1587          _mesa_error(ctx, GL_INVALID_OPERATION,
1588                      "%s(out of bounds access: bufSize (%d) is too small)",
1589                      caller, bufSize);
1590          return true;
1591       }
1592    }
1593
1594    if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
1595       /* not an error, but do nothing */
1596       return true;
1597    }
1598
1599    return false;
1600 }
1601
1602
1603 /**
1604  * Common helper for all glGetCompressed-teximage functions.
1605  */
1606 static void
1607 get_compressed_texture_image(struct gl_context *ctx,
1608                              struct gl_texture_object *texObj,
1609                              GLenum target, GLint level,
1610                              GLint xoffset, GLint yoffset, GLint zoffset,
1611                              GLsizei width, GLsizei height, GLint depth,
1612                              GLvoid *pixels,
1613                              const char *caller)
1614 {
1615    struct gl_texture_image *texImage;
1616    unsigned firstFace, numFaces, i, imageStride;
1617
1618    FLUSH_VERTICES(ctx, 0);
1619
1620    texImage = select_tex_image(texObj, target, level, zoffset);
1621    assert(texImage);  /* should have been error checked already */
1622
1623    if (_mesa_is_zero_size_texture(texImage))
1624       return;
1625
1626    if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
1627       _mesa_debug(ctx,
1628                   "%s(tex %u) format = %s, w=%d, h=%d\n",
1629                   caller, texObj->Name,
1630                   _mesa_get_format_name(texImage->TexFormat),
1631                   texImage->Width, texImage->Height);
1632    }
1633
1634    if (target == GL_TEXTURE_CUBE_MAP) {
1635       struct compressed_pixelstore store;
1636
1637       /* Compute image stride between cube faces */
1638       _mesa_compute_compressed_pixelstore(2, texImage->TexFormat,
1639                                           width, height, depth,
1640                                           &ctx->Pack, &store);
1641       imageStride = store.TotalBytesPerRow * store.TotalRowsPerSlice;
1642
1643       firstFace = zoffset;
1644       numFaces = depth;
1645       zoffset = 0;
1646       depth = 1;
1647    }
1648    else {
1649       imageStride = 0;
1650       firstFace = _mesa_tex_target_to_face(target);
1651       numFaces = 1;
1652    }
1653
1654    _mesa_lock_texture(ctx, texObj);
1655
1656    for (i = 0; i < numFaces; i++) {
1657       texImage = texObj->Image[firstFace + i][level];
1658       assert(texImage);
1659
1660       ctx->Driver.GetCompressedTexSubImage(ctx, texImage,
1661                                            xoffset, yoffset, zoffset,
1662                                            width, height, depth, pixels);
1663
1664       /* next cube face */
1665       pixels = (GLubyte *) pixels + imageStride;
1666    }
1667
1668    _mesa_unlock_texture(ctx, texObj);
1669 }
1670
1671
1672 void GLAPIENTRY
1673 _mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
1674                                 GLvoid *pixels)
1675 {
1676    GET_CURRENT_CONTEXT(ctx);
1677    static const char *caller = "glGetnCompressedTexImageARB";
1678    GLsizei width, height, depth;
1679    struct gl_texture_object *texObj;
1680
1681    if (!legal_getteximage_target(ctx, target, false)) {
1682       _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1683       return;
1684    }
1685
1686    texObj = _mesa_get_current_tex_object(ctx, target);
1687    assert(texObj);
1688
1689    get_texture_image_dims(texObj, target, level, &width, &height, &depth);
1690
1691    if (getcompressedteximage_error_check(ctx, texObj, target, level,
1692                                          0, 0, 0, width, height, depth,
1693                                          INT_MAX, pixels, caller)) {
1694       return;
1695    }
1696
1697    get_compressed_texture_image(ctx, texObj, target, level,
1698                                 0, 0, 0, width, height, depth,
1699                                 pixels, caller);
1700 }
1701
1702
1703 void GLAPIENTRY
1704 _mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *pixels)
1705 {
1706    GET_CURRENT_CONTEXT(ctx);
1707    static const char *caller = "glGetCompressedTexImage";
1708    GLsizei width, height, depth;
1709    struct gl_texture_object *texObj;
1710
1711    if (!legal_getteximage_target(ctx, target, false)) {
1712       _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
1713       return;
1714    }
1715
1716    texObj = _mesa_get_current_tex_object(ctx, target);
1717    assert(texObj);
1718
1719    get_texture_image_dims(texObj, target, level,
1720                           &width, &height, &depth);
1721
1722    if (getcompressedteximage_error_check(ctx, texObj, target, level,
1723                                          0, 0, 0, width, height, depth,
1724                                          INT_MAX, pixels, caller)) {
1725       return;
1726    }
1727
1728    get_compressed_texture_image(ctx, texObj, target, level,
1729                                 0, 0, 0, width, height, depth,
1730                                 pixels, caller);
1731 }
1732
1733
1734 void GLAPIENTRY
1735 _mesa_GetCompressedTextureImage(GLuint texture, GLint level,
1736                                 GLsizei bufSize, GLvoid *pixels)
1737 {
1738    GET_CURRENT_CONTEXT(ctx);
1739    static const char *caller = "glGetCompressedTextureImage";
1740    GLsizei width, height, depth;
1741    struct gl_texture_object *texObj =
1742       _mesa_lookup_texture_err(ctx, texture, caller);
1743
1744    if (!texObj) {
1745       return;
1746    }
1747
1748    get_texture_image_dims(texObj, texObj->Target, level,
1749                           &width, &height, &depth);
1750
1751    if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
1752                                          0, 0, 0, width, height, depth,
1753                                          bufSize, pixels, caller)) {
1754       return;
1755    }
1756
1757    get_compressed_texture_image(ctx, texObj, texObj->Target, level,
1758                                 0, 0, 0, width, height, depth,
1759                                 pixels, caller);
1760 }
1761
1762
1763 void APIENTRY
1764 _mesa_GetCompressedTextureSubImage(GLuint texture, GLint level,
1765                                    GLint xoffset, GLint yoffset,
1766                                    GLint zoffset, GLsizei width,
1767                                    GLsizei height, GLsizei depth,
1768                                    GLsizei bufSize, void *pixels)
1769 {
1770    GET_CURRENT_CONTEXT(ctx);
1771    static const char *caller = "glGetCompressedTextureImage";
1772    struct gl_texture_object *texObj;
1773
1774    texObj = _mesa_lookup_texture_err(ctx, texture, caller);
1775    if (!texObj) {
1776       return;
1777    }
1778
1779    if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
1780                                          xoffset, yoffset, zoffset,
1781                                          width, height, depth,
1782                                          bufSize, pixels, caller)) {
1783       return;
1784    }
1785
1786    get_compressed_texture_image(ctx, texObj, texObj->Target, level,
1787                                 xoffset, yoffset, zoffset,
1788                                 width, height, depth,
1789                                 pixels, caller);
1790 }