Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  5.  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
  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.  * \file teximage.c
  29.  * Texture image-related functions.
  30.  */
  31.  
  32. #include <stdbool.h>
  33. #include "glheader.h"
  34. #include "bufferobj.h"
  35. #include "context.h"
  36. #include "enums.h"
  37. #include "fbobject.h"
  38. #include "framebuffer.h"
  39. #include "hash.h"
  40. #include "image.h"
  41. #include "imports.h"
  42. #include "macros.h"
  43. #include "multisample.h"
  44. #include "state.h"
  45. #include "texcompress.h"
  46. #include "texcompress_cpal.h"
  47. #include "teximage.h"
  48. #include "texobj.h"
  49. #include "texstate.h"
  50. #include "texstorage.h"
  51. #include "mtypes.h"
  52. #include "glformats.h"
  53.  
  54.  
  55. /**
  56.  * State changes which we care about for glCopyTex[Sub]Image() calls.
  57.  * In particular, we care about pixel transfer state and buffer state
  58.  * (such as glReadBuffer to make sure we read from the right renderbuffer).
  59.  */
  60. #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
  61.  
  62.  
  63.  
  64. /**
  65.  * Return the simple base format for a given internal texture format.
  66.  * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
  67.  *
  68.  * \param ctx GL context.
  69.  * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
  70.  *
  71.  * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
  72.  * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
  73.  *
  74.  * This is the format which is used during texture application (i.e. the
  75.  * texture format and env mode determine the arithmetic used.
  76.  */
  77. GLint
  78. _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
  79. {
  80.    switch (internalFormat) {
  81.       case GL_ALPHA:
  82.       case GL_ALPHA4:
  83.       case GL_ALPHA8:
  84.       case GL_ALPHA12:
  85.       case GL_ALPHA16:
  86.          return (ctx->API != API_OPENGL_CORE) ? GL_ALPHA : -1;
  87.       case 1:
  88.       case GL_LUMINANCE:
  89.       case GL_LUMINANCE4:
  90.       case GL_LUMINANCE8:
  91.       case GL_LUMINANCE12:
  92.       case GL_LUMINANCE16:
  93.          return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE : -1;
  94.       case 2:
  95.       case GL_LUMINANCE_ALPHA:
  96.       case GL_LUMINANCE4_ALPHA4:
  97.       case GL_LUMINANCE6_ALPHA2:
  98.       case GL_LUMINANCE8_ALPHA8:
  99.       case GL_LUMINANCE12_ALPHA4:
  100.       case GL_LUMINANCE12_ALPHA12:
  101.       case GL_LUMINANCE16_ALPHA16:
  102.          return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE_ALPHA : -1;
  103.       case GL_INTENSITY:
  104.       case GL_INTENSITY4:
  105.       case GL_INTENSITY8:
  106.       case GL_INTENSITY12:
  107.       case GL_INTENSITY16:
  108.          return (ctx->API != API_OPENGL_CORE) ? GL_INTENSITY : -1;
  109.       case 3:
  110.          return (ctx->API != API_OPENGL_CORE) ? GL_RGB : -1;
  111.       case GL_RGB:
  112.       case GL_R3_G3_B2:
  113.       case GL_RGB4:
  114.       case GL_RGB5:
  115.       case GL_RGB8:
  116.       case GL_RGB10:
  117.       case GL_RGB12:
  118.       case GL_RGB16:
  119.          return GL_RGB;
  120.       case 4:
  121.          return (ctx->API != API_OPENGL_CORE) ? GL_RGBA : -1;
  122.       case GL_RGBA:
  123.       case GL_RGBA2:
  124.       case GL_RGBA4:
  125.       case GL_RGB5_A1:
  126.       case GL_RGBA8:
  127.       case GL_RGB10_A2:
  128.       case GL_RGBA12:
  129.       case GL_RGBA16:
  130.          return GL_RGBA;
  131.       default:
  132.          ; /* fallthrough */
  133.    }
  134.  
  135.    /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
  136.     */
  137.    if (_mesa_is_gles(ctx)) {
  138.       switch (internalFormat) {
  139.          case GL_BGRA:
  140.             return GL_RGBA;
  141.          default:
  142.             ; /* fallthrough */
  143.       }
  144.    }
  145.  
  146.    if (ctx->Extensions.ARB_ES2_compatibility) {
  147.       switch (internalFormat) {
  148.          case GL_RGB565:
  149.             return GL_RGB;
  150.          default:
  151.             ; /* fallthrough */
  152.       }
  153.    }
  154.  
  155.    if (ctx->Extensions.ARB_depth_texture) {
  156.       switch (internalFormat) {
  157.          case GL_DEPTH_COMPONENT:
  158.          case GL_DEPTH_COMPONENT16:
  159.          case GL_DEPTH_COMPONENT24:
  160.          case GL_DEPTH_COMPONENT32:
  161.             return GL_DEPTH_COMPONENT;
  162.          default:
  163.             ; /* fallthrough */
  164.       }
  165.    }
  166.  
  167.    switch (internalFormat) {
  168.    case GL_COMPRESSED_ALPHA:
  169.       return GL_ALPHA;
  170.    case GL_COMPRESSED_LUMINANCE:
  171.       return GL_LUMINANCE;
  172.    case GL_COMPRESSED_LUMINANCE_ALPHA:
  173.       return GL_LUMINANCE_ALPHA;
  174.    case GL_COMPRESSED_INTENSITY:
  175.       return GL_INTENSITY;
  176.    case GL_COMPRESSED_RGB:
  177.       return GL_RGB;
  178.    case GL_COMPRESSED_RGBA:
  179.       return GL_RGBA;
  180.    default:
  181.       ; /* fallthrough */
  182.    }
  183.  
  184.    if (ctx->Extensions.TDFX_texture_compression_FXT1) {
  185.       switch (internalFormat) {
  186.          case GL_COMPRESSED_RGB_FXT1_3DFX:
  187.             return GL_RGB;
  188.          case GL_COMPRESSED_RGBA_FXT1_3DFX:
  189.             return GL_RGBA;
  190.          default:
  191.             ; /* fallthrough */
  192.       }
  193.    }
  194.  
  195.    /* Assume that the ANGLE flag will always be set if the EXT flag is set.
  196.     */
  197.    if (ctx->Extensions.ANGLE_texture_compression_dxt) {
  198.       switch (internalFormat) {
  199.          case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  200.             return GL_RGB;
  201.          case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  202.          case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  203.          case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  204.             return GL_RGBA;
  205.          default:
  206.             ; /* fallthrough */
  207.       }
  208.    }
  209.  
  210.    if (_mesa_is_desktop_gl(ctx)
  211.        && ctx->Extensions.ANGLE_texture_compression_dxt) {
  212.       switch (internalFormat) {
  213.          case GL_RGB_S3TC:
  214.          case GL_RGB4_S3TC:
  215.             return GL_RGB;
  216.          case GL_RGBA_S3TC:
  217.          case GL_RGBA4_S3TC:
  218.             return GL_RGBA;
  219.          default:
  220.             ; /* fallthrough */
  221.       }
  222.    }
  223.  
  224.    if (ctx->Extensions.MESA_ycbcr_texture) {
  225.       if (internalFormat == GL_YCBCR_MESA)
  226.          return GL_YCBCR_MESA;
  227.    }
  228.  
  229.    if (ctx->Extensions.ARB_texture_float) {
  230.       switch (internalFormat) {
  231.          case GL_ALPHA16F_ARB:
  232.          case GL_ALPHA32F_ARB:
  233.             return GL_ALPHA;
  234.          case GL_RGBA16F_ARB:
  235.          case GL_RGBA32F_ARB:
  236.             return GL_RGBA;
  237.          case GL_RGB16F_ARB:
  238.          case GL_RGB32F_ARB:
  239.             return GL_RGB;
  240.          case GL_INTENSITY16F_ARB:
  241.          case GL_INTENSITY32F_ARB:
  242.             return GL_INTENSITY;
  243.          case GL_LUMINANCE16F_ARB:
  244.          case GL_LUMINANCE32F_ARB:
  245.             return GL_LUMINANCE;
  246.          case GL_LUMINANCE_ALPHA16F_ARB:
  247.          case GL_LUMINANCE_ALPHA32F_ARB:
  248.             return GL_LUMINANCE_ALPHA;
  249.          default:
  250.             ; /* fallthrough */
  251.       }
  252.    }
  253.  
  254.    if (ctx->Extensions.ATI_envmap_bumpmap) {
  255.       switch (internalFormat) {
  256.          case GL_DUDV_ATI:
  257.          case GL_DU8DV8_ATI:
  258.             return GL_DUDV_ATI;
  259.          default:
  260.             ; /* fallthrough */
  261.       }
  262.    }
  263.  
  264.    if (ctx->Extensions.EXT_texture_snorm) {
  265.       switch (internalFormat) {
  266.          case GL_RED_SNORM:
  267.          case GL_R8_SNORM:
  268.          case GL_R16_SNORM:
  269.             return GL_RED;
  270.          case GL_RG_SNORM:
  271.          case GL_RG8_SNORM:
  272.          case GL_RG16_SNORM:
  273.             return GL_RG;
  274.          case GL_RGB_SNORM:
  275.          case GL_RGB8_SNORM:
  276.          case GL_RGB16_SNORM:
  277.             return GL_RGB;
  278.          case GL_RGBA_SNORM:
  279.          case GL_RGBA8_SNORM:
  280.          case GL_RGBA16_SNORM:
  281.             return GL_RGBA;
  282.          case GL_ALPHA_SNORM:
  283.          case GL_ALPHA8_SNORM:
  284.          case GL_ALPHA16_SNORM:
  285.             return GL_ALPHA;
  286.          case GL_LUMINANCE_SNORM:
  287.          case GL_LUMINANCE8_SNORM:
  288.          case GL_LUMINANCE16_SNORM:
  289.             return GL_LUMINANCE;
  290.          case GL_LUMINANCE_ALPHA_SNORM:
  291.          case GL_LUMINANCE8_ALPHA8_SNORM:
  292.          case GL_LUMINANCE16_ALPHA16_SNORM:
  293.             return GL_LUMINANCE_ALPHA;
  294.          case GL_INTENSITY_SNORM:
  295.          case GL_INTENSITY8_SNORM:
  296.          case GL_INTENSITY16_SNORM:
  297.             return GL_INTENSITY;
  298.          default:
  299.             ; /* fallthrough */
  300.       }
  301.    }
  302.  
  303.    if (ctx->Extensions.EXT_packed_depth_stencil) {
  304.       switch (internalFormat) {
  305.          case GL_DEPTH_STENCIL_EXT:
  306.          case GL_DEPTH24_STENCIL8_EXT:
  307.             return GL_DEPTH_STENCIL_EXT;
  308.          default:
  309.             ; /* fallthrough */
  310.       }
  311.    }
  312.  
  313.    if (ctx->Extensions.EXT_texture_sRGB) {
  314.       switch (internalFormat) {
  315.       case GL_SRGB_EXT:
  316.       case GL_SRGB8_EXT:
  317.       case GL_COMPRESSED_SRGB_EXT:
  318.          return GL_RGB;
  319.       case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
  320.          return ctx->Extensions.EXT_texture_compression_s3tc ? GL_RGB : -1;
  321.       case GL_SRGB_ALPHA_EXT:
  322.       case GL_SRGB8_ALPHA8_EXT:
  323.       case GL_COMPRESSED_SRGB_ALPHA_EXT:
  324.          return GL_RGBA;
  325.       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
  326.       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
  327.       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
  328.          return ctx->Extensions.EXT_texture_compression_s3tc ? GL_RGBA : -1;
  329.       case GL_SLUMINANCE_ALPHA_EXT:
  330.       case GL_SLUMINANCE8_ALPHA8_EXT:
  331.       case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
  332.          return GL_LUMINANCE_ALPHA;
  333.       case GL_SLUMINANCE_EXT:
  334.       case GL_SLUMINANCE8_EXT:
  335.       case GL_COMPRESSED_SLUMINANCE_EXT:
  336.          return GL_LUMINANCE;
  337.       default:
  338.          ; /* fallthrough */
  339.       }
  340.    }
  341.  
  342.    if (ctx->Version >= 30 ||
  343.        ctx->Extensions.EXT_texture_integer) {
  344.       switch (internalFormat) {
  345.       case GL_RGBA8UI_EXT:
  346.       case GL_RGBA16UI_EXT:
  347.       case GL_RGBA32UI_EXT:
  348.       case GL_RGBA8I_EXT:
  349.       case GL_RGBA16I_EXT:
  350.       case GL_RGBA32I_EXT:
  351.       case GL_RGB10_A2UI:
  352.          return GL_RGBA;
  353.       case GL_RGB8UI_EXT:
  354.       case GL_RGB16UI_EXT:
  355.       case GL_RGB32UI_EXT:
  356.       case GL_RGB8I_EXT:
  357.       case GL_RGB16I_EXT:
  358.       case GL_RGB32I_EXT:
  359.          return GL_RGB;
  360.       }
  361.    }
  362.  
  363.    if (ctx->Extensions.EXT_texture_integer) {
  364.       switch (internalFormat) {
  365.       case GL_ALPHA8UI_EXT:
  366.       case GL_ALPHA16UI_EXT:
  367.       case GL_ALPHA32UI_EXT:
  368.       case GL_ALPHA8I_EXT:
  369.       case GL_ALPHA16I_EXT:
  370.       case GL_ALPHA32I_EXT:
  371.          return GL_ALPHA;
  372.       case GL_INTENSITY8UI_EXT:
  373.       case GL_INTENSITY16UI_EXT:
  374.       case GL_INTENSITY32UI_EXT:
  375.       case GL_INTENSITY8I_EXT:
  376.       case GL_INTENSITY16I_EXT:
  377.       case GL_INTENSITY32I_EXT:
  378.          return GL_INTENSITY;
  379.       case GL_LUMINANCE8UI_EXT:
  380.       case GL_LUMINANCE16UI_EXT:
  381.       case GL_LUMINANCE32UI_EXT:
  382.       case GL_LUMINANCE8I_EXT:
  383.       case GL_LUMINANCE16I_EXT:
  384.       case GL_LUMINANCE32I_EXT:
  385.          return GL_LUMINANCE;
  386.       case GL_LUMINANCE_ALPHA8UI_EXT:
  387.       case GL_LUMINANCE_ALPHA16UI_EXT:
  388.       case GL_LUMINANCE_ALPHA32UI_EXT:
  389.       case GL_LUMINANCE_ALPHA8I_EXT:
  390.       case GL_LUMINANCE_ALPHA16I_EXT:
  391.       case GL_LUMINANCE_ALPHA32I_EXT:
  392.          return GL_LUMINANCE_ALPHA;
  393.       default:
  394.          ; /* fallthrough */
  395.       }
  396.    }
  397.  
  398.    if (ctx->Extensions.ARB_texture_rg) {
  399.       switch (internalFormat) {
  400.       case GL_R16F:
  401.          /* R16F depends on both ARB_half_float_pixel and ARB_texture_float.
  402.           */
  403.          if (!ctx->Extensions.ARB_half_float_pixel)
  404.             break;
  405.          /* FALLTHROUGH */
  406.       case GL_R32F:
  407.          if (!ctx->Extensions.ARB_texture_float)
  408.             break;
  409.          return GL_RED;
  410.       case GL_R8I:
  411.       case GL_R8UI:
  412.       case GL_R16I:
  413.       case GL_R16UI:
  414.       case GL_R32I:
  415.       case GL_R32UI:
  416.          if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
  417.             break;
  418.          /* FALLTHROUGH */
  419.       case GL_R8:
  420.       case GL_R16:
  421.       case GL_RED:
  422.       case GL_COMPRESSED_RED:
  423.          return GL_RED;
  424.  
  425.       case GL_RG16F:
  426.          /* RG16F depends on both ARB_half_float_pixel and ARB_texture_float.
  427.           */
  428.          if (!ctx->Extensions.ARB_half_float_pixel)
  429.             break;
  430.          /* FALLTHROUGH */
  431.       case GL_RG32F:
  432.          if (!ctx->Extensions.ARB_texture_float)
  433.             break;
  434.          return GL_RG;
  435.       case GL_RG8I:
  436.       case GL_RG8UI:
  437.       case GL_RG16I:
  438.       case GL_RG16UI:
  439.       case GL_RG32I:
  440.       case GL_RG32UI:
  441.          if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
  442.             break;
  443.          /* FALLTHROUGH */
  444.       case GL_RG:
  445.       case GL_RG8:
  446.       case GL_RG16:
  447.       case GL_COMPRESSED_RG:
  448.          return GL_RG;
  449.       default:
  450.          ; /* fallthrough */
  451.       }
  452.    }
  453.  
  454.    if (ctx->Extensions.EXT_texture_shared_exponent) {
  455.       switch (internalFormat) {
  456.       case GL_RGB9_E5_EXT:
  457.          return GL_RGB;
  458.       default:
  459.          ; /* fallthrough */
  460.       }
  461.    }
  462.  
  463.    if (ctx->Extensions.EXT_packed_float) {
  464.       switch (internalFormat) {
  465.       case GL_R11F_G11F_B10F_EXT:
  466.          return GL_RGB;
  467.       default:
  468.          ; /* fallthrough */
  469.       }
  470.    }
  471.  
  472.    if (ctx->Extensions.ARB_depth_buffer_float) {
  473.       switch (internalFormat) {
  474.       case GL_DEPTH_COMPONENT32F:
  475.          return GL_DEPTH_COMPONENT;
  476.       case GL_DEPTH32F_STENCIL8:
  477.          return GL_DEPTH_STENCIL;
  478.       default:
  479.          ; /* fallthrough */
  480.       }
  481.    }
  482.  
  483.    if (ctx->Extensions.ARB_texture_compression_rgtc) {
  484.       switch (internalFormat) {
  485.       case GL_COMPRESSED_RED_RGTC1:
  486.       case GL_COMPRESSED_SIGNED_RED_RGTC1:
  487.          return GL_RED;
  488.       case GL_COMPRESSED_RG_RGTC2:
  489.       case GL_COMPRESSED_SIGNED_RG_RGTC2:
  490.          return GL_RG;
  491.       default:
  492.          ; /* fallthrough */
  493.       }
  494.    }
  495.  
  496.    if (ctx->Extensions.EXT_texture_compression_latc) {
  497.       switch (internalFormat) {
  498.       case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
  499.       case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
  500.          return GL_LUMINANCE;
  501.       case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
  502.       case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
  503.          return GL_LUMINANCE_ALPHA;
  504.       default:
  505.          ; /* fallthrough */
  506.       }
  507.    }
  508.  
  509.    if (ctx->Extensions.ATI_texture_compression_3dc) {
  510.       switch (internalFormat) {
  511.       case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
  512.          return GL_LUMINANCE_ALPHA;
  513.       default:
  514.          ; /* fallthrough */
  515.       }
  516.    }
  517.  
  518.    if (ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
  519.       switch (internalFormat) {
  520.       case GL_ETC1_RGB8_OES:
  521.          return GL_RGB;
  522.       default:
  523.          ; /* fallthrough */
  524.       }
  525.    }
  526.  
  527.    if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility) {
  528.       switch (internalFormat) {
  529.       case GL_COMPRESSED_RGB8_ETC2:
  530.       case GL_COMPRESSED_SRGB8_ETC2:
  531.          return GL_RGB;
  532.       case GL_COMPRESSED_RGBA8_ETC2_EAC:
  533.       case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
  534.       case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
  535.       case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
  536.          return GL_RGBA;
  537.       case GL_COMPRESSED_R11_EAC:
  538.       case GL_COMPRESSED_SIGNED_R11_EAC:
  539.          return GL_RED;
  540.       case GL_COMPRESSED_RG11_EAC:
  541.       case GL_COMPRESSED_SIGNED_RG11_EAC:
  542.          return GL_RG;
  543.       default:
  544.          ; /* fallthrough */
  545.       }
  546.    }
  547.  
  548.    if (ctx->API == API_OPENGLES) {
  549.       switch (internalFormat) {
  550.       case GL_PALETTE4_RGB8_OES:
  551.       case GL_PALETTE4_R5_G6_B5_OES:
  552.       case GL_PALETTE8_RGB8_OES:
  553.       case GL_PALETTE8_R5_G6_B5_OES:
  554.          return GL_RGB;
  555.       case GL_PALETTE4_RGBA8_OES:
  556.       case GL_PALETTE8_RGB5_A1_OES:
  557.       case GL_PALETTE4_RGBA4_OES:
  558.       case GL_PALETTE4_RGB5_A1_OES:
  559.       case GL_PALETTE8_RGBA8_OES:
  560.       case GL_PALETTE8_RGBA4_OES:
  561.          return GL_RGBA;
  562.       default:
  563.          ; /* fallthrough */
  564.       }
  565.    }
  566.  
  567.    return -1; /* error */
  568. }
  569.  
  570.  
  571. /**
  572.  * For cube map faces, return a face index in [0,5].
  573.  * For other targets return 0;
  574.  */
  575. GLuint
  576. _mesa_tex_target_to_face(GLenum target)
  577. {
  578.    if (_mesa_is_cube_face(target))
  579.       return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  580.    else
  581.       return 0;
  582. }
  583.  
  584.  
  585.  
  586. /**
  587.  * Install gl_texture_image in a gl_texture_object according to the target
  588.  * and level parameters.
  589.  *
  590.  * \param tObj texture object.
  591.  * \param target texture target.
  592.  * \param level image level.
  593.  * \param texImage texture image.
  594.  */
  595. static void
  596. set_tex_image(struct gl_texture_object *tObj,
  597.               GLenum target, GLint level,
  598.               struct gl_texture_image *texImage)
  599. {
  600.    const GLuint face = _mesa_tex_target_to_face(target);
  601.  
  602.    ASSERT(tObj);
  603.    ASSERT(texImage);
  604.    if (target == GL_TEXTURE_RECTANGLE_NV || target == GL_TEXTURE_EXTERNAL_OES)
  605.       assert(level == 0);
  606.  
  607.    tObj->Image[face][level] = texImage;
  608.  
  609.    /* Set the 'back' pointer */
  610.    texImage->TexObject = tObj;
  611.    texImage->Level = level;
  612.    texImage->Face = face;
  613. }
  614.  
  615.  
  616. /**
  617.  * Allocate a texture image structure.
  618.  *
  619.  * Called via ctx->Driver.NewTextureImage() unless overriden by a device
  620.  * driver.
  621.  *
  622.  * \return a pointer to gl_texture_image struct with all fields initialized to
  623.  * zero.
  624.  */
  625. struct gl_texture_image *
  626. _mesa_new_texture_image( struct gl_context *ctx )
  627. {
  628.    (void) ctx;
  629.    return CALLOC_STRUCT(gl_texture_image);
  630. }
  631.  
  632.  
  633. /**
  634.  * Free a gl_texture_image and associated data.
  635.  * This function is a fallback called via ctx->Driver.DeleteTextureImage().
  636.  *
  637.  * \param texImage texture image.
  638.  *
  639.  * Free the texture image structure and the associated image data.
  640.  */
  641. void
  642. _mesa_delete_texture_image(struct gl_context *ctx,
  643.                            struct gl_texture_image *texImage)
  644. {
  645.    /* Free texImage->Data and/or any other driver-specific texture
  646.     * image storage.
  647.     */
  648.    ASSERT(ctx->Driver.FreeTextureImageBuffer);
  649.    ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
  650.    free(texImage);
  651. }
  652.  
  653.  
  654. /**
  655.  * Test if a target is a proxy target.
  656.  *
  657.  * \param target texture target.
  658.  *
  659.  * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
  660.  */
  661. GLboolean
  662. _mesa_is_proxy_texture(GLenum target)
  663. {
  664.    /*
  665.     * NUM_TEXTURE_TARGETS should match number of terms below, except there's no
  666.     * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
  667.     */
  668.    assert(NUM_TEXTURE_TARGETS == 10 + 2);
  669.  
  670.    return (target == GL_PROXY_TEXTURE_1D ||
  671.            target == GL_PROXY_TEXTURE_2D ||
  672.            target == GL_PROXY_TEXTURE_3D ||
  673.            target == GL_PROXY_TEXTURE_CUBE_MAP_ARB ||
  674.            target == GL_PROXY_TEXTURE_RECTANGLE_NV ||
  675.            target == GL_PROXY_TEXTURE_1D_ARRAY_EXT ||
  676.            target == GL_PROXY_TEXTURE_2D_ARRAY_EXT ||
  677.            target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY ||
  678.            target == GL_PROXY_TEXTURE_2D_MULTISAMPLE ||
  679.            target == GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY);
  680. }
  681.  
  682.  
  683. /**
  684.  * Return the proxy target which corresponds to the given texture target
  685.  */
  686. GLenum
  687. _mesa_get_proxy_target(GLenum target)
  688. {
  689.    switch (target) {
  690.    case GL_TEXTURE_1D:
  691.    case GL_PROXY_TEXTURE_1D:
  692.       return GL_PROXY_TEXTURE_1D;
  693.    case GL_TEXTURE_2D:
  694.    case GL_PROXY_TEXTURE_2D:
  695.       return GL_PROXY_TEXTURE_2D;
  696.    case GL_TEXTURE_3D:
  697.    case GL_PROXY_TEXTURE_3D:
  698.       return GL_PROXY_TEXTURE_3D;
  699.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
  700.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
  701.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
  702.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
  703.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
  704.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
  705.    case GL_TEXTURE_CUBE_MAP_ARB:
  706.    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
  707.       return GL_PROXY_TEXTURE_CUBE_MAP_ARB;
  708.    case GL_TEXTURE_RECTANGLE_NV:
  709.    case GL_PROXY_TEXTURE_RECTANGLE_NV:
  710.       return GL_PROXY_TEXTURE_RECTANGLE_NV;
  711.    case GL_TEXTURE_1D_ARRAY_EXT:
  712.    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  713.       return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
  714.    case GL_TEXTURE_2D_ARRAY_EXT:
  715.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  716.       return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
  717.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  718.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  719.       return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY;
  720.    case GL_TEXTURE_2D_MULTISAMPLE:
  721.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  722.       return GL_PROXY_TEXTURE_2D_MULTISAMPLE;
  723.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  724.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  725.       return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY;
  726.    default:
  727.       _mesa_problem(NULL, "unexpected target in _mesa_get_proxy_target()");
  728.       return 0;
  729.    }
  730. }
  731.  
  732.  
  733. /**
  734.  * Get the texture object that corresponds to the target of the given
  735.  * texture unit.  The target should have already been checked for validity.
  736.  *
  737.  * \param ctx GL context.
  738.  * \param texUnit texture unit.
  739.  * \param target texture target.
  740.  *
  741.  * \return pointer to the texture object on success, or NULL on failure.
  742.  */
  743. struct gl_texture_object *
  744. _mesa_select_tex_object(struct gl_context *ctx,
  745.                         const struct gl_texture_unit *texUnit,
  746.                         GLenum target)
  747. {
  748.    const GLboolean arrayTex = (ctx->Extensions.MESA_texture_array ||
  749.                                ctx->Extensions.EXT_texture_array);
  750.  
  751.    switch (target) {
  752.       case GL_TEXTURE_1D:
  753.          return texUnit->CurrentTex[TEXTURE_1D_INDEX];
  754.       case GL_PROXY_TEXTURE_1D:
  755.          return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX];
  756.       case GL_TEXTURE_2D:
  757.          return texUnit->CurrentTex[TEXTURE_2D_INDEX];
  758.       case GL_PROXY_TEXTURE_2D:
  759.          return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX];
  760.       case GL_TEXTURE_3D:
  761.          return texUnit->CurrentTex[TEXTURE_3D_INDEX];
  762.       case GL_PROXY_TEXTURE_3D:
  763.          return ctx->Texture.ProxyTex[TEXTURE_3D_INDEX];
  764.       case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
  765.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
  766.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
  767.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
  768.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
  769.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
  770.       case GL_TEXTURE_CUBE_MAP_ARB:
  771.          return ctx->Extensions.ARB_texture_cube_map
  772.                 ? texUnit->CurrentTex[TEXTURE_CUBE_INDEX] : NULL;
  773.       case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
  774.          return ctx->Extensions.ARB_texture_cube_map
  775.                 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_INDEX] : NULL;
  776.       case GL_TEXTURE_CUBE_MAP_ARRAY:
  777.          return ctx->Extensions.ARB_texture_cube_map_array
  778.                 ? texUnit->CurrentTex[TEXTURE_CUBE_ARRAY_INDEX] : NULL;
  779.       case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  780.          return ctx->Extensions.ARB_texture_cube_map_array
  781.                 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_ARRAY_INDEX] : NULL;
  782.       case GL_TEXTURE_RECTANGLE_NV:
  783.          return ctx->Extensions.NV_texture_rectangle
  784.                 ? texUnit->CurrentTex[TEXTURE_RECT_INDEX] : NULL;
  785.       case GL_PROXY_TEXTURE_RECTANGLE_NV:
  786.          return ctx->Extensions.NV_texture_rectangle
  787.                 ? ctx->Texture.ProxyTex[TEXTURE_RECT_INDEX] : NULL;
  788.       case GL_TEXTURE_1D_ARRAY_EXT:
  789.          return arrayTex ? texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
  790.       case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  791.          return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
  792.       case GL_TEXTURE_2D_ARRAY_EXT:
  793.          return arrayTex ? texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
  794.       case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  795.          return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
  796.       case GL_TEXTURE_BUFFER:
  797.          return ctx->API == API_OPENGL_CORE &&
  798.                 ctx->Extensions.ARB_texture_buffer_object ?
  799.                 texUnit->CurrentTex[TEXTURE_BUFFER_INDEX] : NULL;
  800.       case GL_TEXTURE_EXTERNAL_OES:
  801.          return _mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external
  802.             ? texUnit->CurrentTex[TEXTURE_EXTERNAL_INDEX] : NULL;
  803.       case GL_TEXTURE_2D_MULTISAMPLE:
  804.          return ctx->Extensions.ARB_texture_multisample
  805.             ? texUnit->CurrentTex[TEXTURE_2D_MULTISAMPLE_INDEX] : NULL;
  806.       case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  807.          return ctx->Extensions.ARB_texture_multisample
  808.             ? ctx->Texture.ProxyTex[TEXTURE_2D_MULTISAMPLE_INDEX] : NULL;
  809.       case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  810.          return ctx->Extensions.ARB_texture_multisample
  811.             ? texUnit->CurrentTex[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX] : NULL;
  812.       case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  813.          return ctx->Extensions.ARB_texture_multisample
  814.             ? ctx->Texture.ProxyTex[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX] : NULL;
  815.       default:
  816.          _mesa_problem(NULL, "bad target in _mesa_select_tex_object()");
  817.          return NULL;
  818.    }
  819. }
  820.  
  821.  
  822. /**
  823.  * Return pointer to texture object for given target on current texture unit.
  824.  */
  825. struct gl_texture_object *
  826. _mesa_get_current_tex_object(struct gl_context *ctx, GLenum target)
  827. {
  828.    struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
  829.    return _mesa_select_tex_object(ctx, texUnit, target);
  830. }
  831.  
  832.  
  833. /**
  834.  * Get a texture image pointer from a texture object, given a texture
  835.  * target and mipmap level.  The target and level parameters should
  836.  * have already been error-checked.
  837.  *
  838.  * \param ctx GL context.
  839.  * \param texObj texture unit.
  840.  * \param target texture target.
  841.  * \param level image level.
  842.  *
  843.  * \return pointer to the texture image structure, or NULL on failure.
  844.  */
  845. struct gl_texture_image *
  846. _mesa_select_tex_image(struct gl_context *ctx,
  847.                        const struct gl_texture_object *texObj,
  848.                        GLenum target, GLint level)
  849. {
  850.    const GLuint face = _mesa_tex_target_to_face(target);
  851.  
  852.    ASSERT(texObj);
  853.    ASSERT(level >= 0);
  854.    ASSERT(level < MAX_TEXTURE_LEVELS);
  855.  
  856.    return texObj->Image[face][level];
  857. }
  858.  
  859.  
  860. /**
  861.  * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
  862.  * it and install it.  Only return NULL if passed a bad parameter or run
  863.  * out of memory.
  864.  */
  865. struct gl_texture_image *
  866. _mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
  867.                     GLenum target, GLint level)
  868. {
  869.    struct gl_texture_image *texImage;
  870.  
  871.    if (!texObj)
  872.       return NULL;
  873.  
  874.    texImage = _mesa_select_tex_image(ctx, texObj, target, level);
  875.    if (!texImage) {
  876.       texImage = ctx->Driver.NewTextureImage(ctx);
  877.       if (!texImage) {
  878.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
  879.          return NULL;
  880.       }
  881.  
  882.       set_tex_image(texObj, target, level, texImage);
  883.    }
  884.  
  885.    return texImage;
  886. }
  887.  
  888.  
  889. /**
  890.  * Return pointer to the specified proxy texture image.
  891.  * Note that proxy textures are per-context, not per-texture unit.
  892.  * \return pointer to texture image or NULL if invalid target, invalid
  893.  *         level, or out of memory.
  894.  */
  895. static struct gl_texture_image *
  896. get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
  897. {
  898.    struct gl_texture_image *texImage;
  899.    GLuint texIndex;
  900.  
  901.    if (level < 0)
  902.       return NULL;
  903.  
  904.    switch (target) {
  905.    case GL_PROXY_TEXTURE_1D:
  906.       if (level >= ctx->Const.MaxTextureLevels)
  907.          return NULL;
  908.       texIndex = TEXTURE_1D_INDEX;
  909.       break;
  910.    case GL_PROXY_TEXTURE_2D:
  911.       if (level >= ctx->Const.MaxTextureLevels)
  912.          return NULL;
  913.       texIndex = TEXTURE_2D_INDEX;
  914.       break;
  915.    case GL_PROXY_TEXTURE_3D:
  916.       if (level >= ctx->Const.Max3DTextureLevels)
  917.          return NULL;
  918.       texIndex = TEXTURE_3D_INDEX;
  919.       break;
  920.    case GL_PROXY_TEXTURE_CUBE_MAP:
  921.       if (level >= ctx->Const.MaxCubeTextureLevels)
  922.          return NULL;
  923.       texIndex = TEXTURE_CUBE_INDEX;
  924.       break;
  925.    case GL_PROXY_TEXTURE_RECTANGLE_NV:
  926.       if (level > 0)
  927.          return NULL;
  928.       texIndex = TEXTURE_RECT_INDEX;
  929.       break;
  930.    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  931.       if (level >= ctx->Const.MaxTextureLevels)
  932.          return NULL;
  933.       texIndex = TEXTURE_1D_ARRAY_INDEX;
  934.       break;
  935.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  936.       if (level >= ctx->Const.MaxTextureLevels)
  937.          return NULL;
  938.       texIndex = TEXTURE_2D_ARRAY_INDEX;
  939.       break;
  940.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  941.       if (level >= ctx->Const.MaxCubeTextureLevels)
  942.          return NULL;
  943.       texIndex = TEXTURE_CUBE_ARRAY_INDEX;
  944.       break;
  945.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  946.       if (level > 0)
  947.          return 0;
  948.       texIndex = TEXTURE_2D_MULTISAMPLE_INDEX;
  949.       break;
  950.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  951.       if (level > 0)
  952.          return 0;
  953.       texIndex = TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
  954.       break;
  955.    default:
  956.       return NULL;
  957.    }
  958.  
  959.    texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
  960.    if (!texImage) {
  961.       texImage = ctx->Driver.NewTextureImage(ctx);
  962.       if (!texImage) {
  963.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
  964.          return NULL;
  965.       }
  966.       ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
  967.       /* Set the 'back' pointer */
  968.       texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
  969.    }
  970.    return texImage;
  971. }
  972.  
  973.  
  974. /**
  975.  * Get the maximum number of allowed mipmap levels.
  976.  *
  977.  * \param ctx GL context.
  978.  * \param target texture target.
  979.  *
  980.  * \return the maximum number of allowed mipmap levels for the given
  981.  * texture target, or zero if passed a bad target.
  982.  *
  983.  * \sa gl_constants.
  984.  */
  985. GLint
  986. _mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
  987. {
  988.    switch (target) {
  989.    case GL_TEXTURE_1D:
  990.    case GL_PROXY_TEXTURE_1D:
  991.    case GL_TEXTURE_2D:
  992.    case GL_PROXY_TEXTURE_2D:
  993.       return ctx->Const.MaxTextureLevels;
  994.    case GL_TEXTURE_3D:
  995.    case GL_PROXY_TEXTURE_3D:
  996.       return ctx->Const.Max3DTextureLevels;
  997.    case GL_TEXTURE_CUBE_MAP:
  998.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
  999.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
  1000.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
  1001.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
  1002.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
  1003.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
  1004.    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
  1005.       return ctx->Extensions.ARB_texture_cube_map
  1006.          ? ctx->Const.MaxCubeTextureLevels : 0;
  1007.    case GL_TEXTURE_RECTANGLE_NV:
  1008.    case GL_PROXY_TEXTURE_RECTANGLE_NV:
  1009.       return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
  1010.    case GL_TEXTURE_1D_ARRAY_EXT:
  1011.    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  1012.    case GL_TEXTURE_2D_ARRAY_EXT:
  1013.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  1014.       return (ctx->Extensions.MESA_texture_array ||
  1015.               ctx->Extensions.EXT_texture_array)
  1016.          ? ctx->Const.MaxTextureLevels : 0;
  1017.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1018.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1019.       return ctx->Extensions.ARB_texture_cube_map_array
  1020.          ? ctx->Const.MaxCubeTextureLevels : 0;
  1021.    case GL_TEXTURE_BUFFER:
  1022.       return ctx->API == API_OPENGL_CORE &&
  1023.              ctx->Extensions.ARB_texture_buffer_object ? 1 : 0;
  1024.    case GL_TEXTURE_2D_MULTISAMPLE:
  1025.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1026.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1027.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1028.       return _mesa_is_desktop_gl(ctx)
  1029.          && ctx->Extensions.ARB_texture_multisample
  1030.          ? 1 : 0;
  1031.    case GL_TEXTURE_EXTERNAL_OES:
  1032.       /* fall-through */
  1033.    default:
  1034.       return 0; /* bad target */
  1035.    }
  1036. }
  1037.  
  1038.  
  1039. /**
  1040.  * Return number of dimensions per mipmap level for the given texture target.
  1041.  */
  1042. GLint
  1043. _mesa_get_texture_dimensions(GLenum target)
  1044. {
  1045.    switch (target) {
  1046.    case GL_TEXTURE_1D:
  1047.    case GL_PROXY_TEXTURE_1D:
  1048.       return 1;
  1049.    case GL_TEXTURE_2D:
  1050.    case GL_TEXTURE_RECTANGLE:
  1051.    case GL_TEXTURE_CUBE_MAP:
  1052.    case GL_PROXY_TEXTURE_2D:
  1053.    case GL_PROXY_TEXTURE_RECTANGLE:
  1054.    case GL_PROXY_TEXTURE_CUBE_MAP:
  1055.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1056.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1057.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1058.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1059.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1060.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1061.    case GL_TEXTURE_1D_ARRAY:
  1062.    case GL_PROXY_TEXTURE_1D_ARRAY:
  1063.    case GL_TEXTURE_EXTERNAL_OES:
  1064.    case GL_TEXTURE_2D_MULTISAMPLE:
  1065.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1066.       return 2;
  1067.    case GL_TEXTURE_3D:
  1068.    case GL_PROXY_TEXTURE_3D:
  1069.    case GL_TEXTURE_2D_ARRAY:
  1070.    case GL_PROXY_TEXTURE_2D_ARRAY:
  1071.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1072.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1073.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1074.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1075.       return 3;
  1076.    case GL_TEXTURE_BUFFER:
  1077.       /* fall-through */
  1078.    default:
  1079.       _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
  1080.                     target);
  1081.       return 2;
  1082.    }
  1083. }
  1084.  
  1085.  
  1086. /**
  1087.  * Return the maximum number of mipmap levels for the given target
  1088.  * and the dimensions.
  1089.  * The dimensions are expected not to include the border.
  1090.  */
  1091. GLsizei
  1092. _mesa_get_tex_max_num_levels(GLenum target, GLsizei width, GLsizei height,
  1093.                              GLsizei depth)
  1094. {
  1095.    GLsizei size;
  1096.  
  1097.    switch (target) {
  1098.    case GL_TEXTURE_1D:
  1099.    case GL_TEXTURE_1D_ARRAY:
  1100.    case GL_PROXY_TEXTURE_1D:
  1101.    case GL_PROXY_TEXTURE_1D_ARRAY:
  1102.       size = width;
  1103.       break;
  1104.    case GL_TEXTURE_CUBE_MAP:
  1105.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1106.    case GL_PROXY_TEXTURE_CUBE_MAP:
  1107.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1108.       size = width;
  1109.       break;
  1110.    case GL_TEXTURE_2D:
  1111.    case GL_TEXTURE_2D_ARRAY:
  1112.    case GL_PROXY_TEXTURE_2D:
  1113.    case GL_PROXY_TEXTURE_2D_ARRAY:
  1114.       size = MAX2(width, height);
  1115.       break;
  1116.    case GL_TEXTURE_3D:
  1117.    case GL_PROXY_TEXTURE_3D:
  1118.       size = MAX3(width, height, depth);
  1119.       break;
  1120.    case GL_TEXTURE_RECTANGLE:
  1121.    case GL_TEXTURE_EXTERNAL_OES:
  1122.    case GL_TEXTURE_2D_MULTISAMPLE:
  1123.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1124.    case GL_PROXY_TEXTURE_RECTANGLE:
  1125.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1126.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1127.       return 1;
  1128.    default:
  1129.       assert(0);
  1130.       return 1;
  1131.    }
  1132.  
  1133.    return _mesa_logbase2(size) + 1;
  1134. }
  1135.  
  1136.  
  1137. #if 000 /* not used anymore */
  1138. /*
  1139.  * glTexImage[123]D can accept a NULL image pointer.  In this case we
  1140.  * create a texture image with unspecified image contents per the OpenGL
  1141.  * spec.
  1142.  */
  1143. static GLubyte *
  1144. make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
  1145. {
  1146.    const GLint components = _mesa_components_in_format(format);
  1147.    const GLint numPixels = width * height * depth;
  1148.    GLubyte *data = (GLubyte *) malloc(numPixels * components * sizeof(GLubyte));
  1149.  
  1150. #ifdef DEBUG
  1151.    /*
  1152.     * Let's see if anyone finds this.  If glTexImage2D() is called with
  1153.     * a NULL image pointer then load the texture image with something
  1154.     * interesting instead of leaving it indeterminate.
  1155.     */
  1156.    if (data) {
  1157.       static const char message[8][32] = {
  1158.          "   X   X  XXXXX   XXX     X    ",
  1159.          "   XX XX  X      X   X   X X   ",
  1160.          "   X X X  X      X      X   X  ",
  1161.          "   X   X  XXXX    XXX   XXXXX  ",
  1162.          "   X   X  X          X  X   X  ",
  1163.          "   X   X  X      X   X  X   X  ",
  1164.          "   X   X  XXXXX   XXX   X   X  ",
  1165.          "                               "
  1166.       };
  1167.  
  1168.       GLubyte *imgPtr = data;
  1169.       GLint h, i, j, k;
  1170.       for (h = 0; h < depth; h++) {
  1171.          for (i = 0; i < height; i++) {
  1172.             GLint srcRow = 7 - (i % 8);
  1173.             for (j = 0; j < width; j++) {
  1174.                GLint srcCol = j % 32;
  1175.                GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
  1176.                for (k = 0; k < components; k++) {
  1177.                   *imgPtr++ = texel;
  1178.                }
  1179.             }
  1180.          }
  1181.       }
  1182.    }
  1183. #endif
  1184.  
  1185.    return data;
  1186. }
  1187. #endif
  1188.  
  1189.  
  1190.  
  1191. /**
  1192.  * Set the size and format-related fields of a gl_texture_image struct
  1193.  * to zero.  This is used when a proxy texture test fails.
  1194.  */
  1195. static void
  1196. clear_teximage_fields(struct gl_texture_image *img)
  1197. {
  1198.    ASSERT(img);
  1199.    img->_BaseFormat = 0;
  1200.    img->InternalFormat = 0;
  1201.    img->Border = 0;
  1202.    img->Width = 0;
  1203.    img->Height = 0;
  1204.    img->Depth = 0;
  1205.    img->Width2 = 0;
  1206.    img->Height2 = 0;
  1207.    img->Depth2 = 0;
  1208.    img->WidthLog2 = 0;
  1209.    img->HeightLog2 = 0;
  1210.    img->DepthLog2 = 0;
  1211.    img->TexFormat = MESA_FORMAT_NONE;
  1212.    img->NumSamples = 0;
  1213.    img->FixedSampleLocations = GL_TRUE;
  1214. }
  1215.  
  1216.  
  1217. /**
  1218.  * Initialize basic fields of the gl_texture_image struct.
  1219.  *
  1220.  * \param ctx GL context.
  1221.  * \param img texture image structure to be initialized.
  1222.  * \param width image width.
  1223.  * \param height image height.
  1224.  * \param depth image depth.
  1225.  * \param border image border.
  1226.  * \param internalFormat internal format.
  1227.  * \param format  the actual hardware format (one of MESA_FORMAT_*)
  1228.  *
  1229.  * Fills in the fields of \p img with the given information.
  1230.  * Note: width, height and depth include the border.
  1231.  */
  1232. void
  1233. _mesa_init_teximage_fields(struct gl_context *ctx,
  1234.                            struct gl_texture_image *img,
  1235.                            GLsizei width, GLsizei height, GLsizei depth,
  1236.                            GLint border, GLenum internalFormat,
  1237.                            gl_format format)
  1238. {
  1239.    GLenum target;
  1240.    ASSERT(img);
  1241.    ASSERT(width >= 0);
  1242.    ASSERT(height >= 0);
  1243.    ASSERT(depth >= 0);
  1244.  
  1245.    target = img->TexObject->Target;
  1246.    img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
  1247.    ASSERT(img->_BaseFormat > 0);
  1248.    img->InternalFormat = internalFormat;
  1249.    img->Border = border;
  1250.    img->Width = width;
  1251.    img->Height = height;
  1252.    img->Depth = depth;
  1253.  
  1254.    img->Width2 = width - 2 * border;   /* == 1 << img->WidthLog2; */
  1255.    img->WidthLog2 = _mesa_logbase2(img->Width2);
  1256.  
  1257.    img->NumSamples = 0;
  1258.    img->FixedSampleLocations = GL_TRUE;
  1259.  
  1260.    switch(target) {
  1261.    case GL_TEXTURE_1D:
  1262.    case GL_TEXTURE_BUFFER:
  1263.    case GL_PROXY_TEXTURE_1D:
  1264.       if (height == 0)
  1265.          img->Height2 = 0;
  1266.       else
  1267.          img->Height2 = 1;
  1268.       img->HeightLog2 = 0;
  1269.       if (depth == 0)
  1270.          img->Depth2 = 0;
  1271.       else
  1272.          img->Depth2 = 1;
  1273.       img->DepthLog2 = 0;
  1274.       break;
  1275.    case GL_TEXTURE_1D_ARRAY:
  1276.    case GL_PROXY_TEXTURE_1D_ARRAY:
  1277.       img->Height2 = height; /* no border */
  1278.       img->HeightLog2 = 0; /* not used */
  1279.       if (depth == 0)
  1280.          img->Depth2 = 0;
  1281.       else
  1282.          img->Depth2 = 1;
  1283.       img->DepthLog2 = 0;
  1284.       break;
  1285.    case GL_TEXTURE_2D:
  1286.    case GL_TEXTURE_RECTANGLE:
  1287.    case GL_TEXTURE_CUBE_MAP:
  1288.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1289.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1290.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1291.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1292.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1293.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1294.    case GL_TEXTURE_EXTERNAL_OES:
  1295.    case GL_PROXY_TEXTURE_2D:
  1296.    case GL_PROXY_TEXTURE_RECTANGLE:
  1297.    case GL_PROXY_TEXTURE_CUBE_MAP:
  1298.    case GL_TEXTURE_2D_MULTISAMPLE:
  1299.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1300.       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
  1301.       img->HeightLog2 = _mesa_logbase2(img->Height2);
  1302.       if (depth == 0)
  1303.          img->Depth2 = 0;
  1304.       else
  1305.          img->Depth2 = 1;
  1306.       img->DepthLog2 = 0;
  1307.       break;
  1308.    case GL_TEXTURE_2D_ARRAY:
  1309.    case GL_PROXY_TEXTURE_2D_ARRAY:
  1310.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1311.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1312.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1313.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1314.       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
  1315.       img->HeightLog2 = _mesa_logbase2(img->Height2);
  1316.       img->Depth2 = depth; /* no border */
  1317.       img->DepthLog2 = 0; /* not used */
  1318.       break;
  1319.    case GL_TEXTURE_3D:
  1320.    case GL_PROXY_TEXTURE_3D:
  1321.       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
  1322.       img->HeightLog2 = _mesa_logbase2(img->Height2);
  1323.       img->Depth2 = depth - 2 * border;   /* == 1 << img->DepthLog2; */
  1324.       img->DepthLog2 = _mesa_logbase2(img->Depth2);
  1325.       break;
  1326.    default:
  1327.       _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
  1328.                     target);
  1329.    }
  1330.  
  1331.    img->MaxNumLevels =
  1332.       _mesa_get_tex_max_num_levels(target,
  1333.                                    img->Width2, img->Height2, img->Depth2);
  1334.    img->TexFormat = format;
  1335. }
  1336.  
  1337.  
  1338. /**
  1339.  * Free and clear fields of the gl_texture_image struct.
  1340.  *
  1341.  * \param ctx GL context.
  1342.  * \param texImage texture image structure to be cleared.
  1343.  *
  1344.  * After the call, \p texImage will have no data associated with it.  Its
  1345.  * fields are cleared so that its parent object will test incomplete.
  1346.  */
  1347. void
  1348. _mesa_clear_texture_image(struct gl_context *ctx,
  1349.                           struct gl_texture_image *texImage)
  1350. {
  1351.    ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  1352.    clear_teximage_fields(texImage);
  1353. }
  1354.  
  1355.  
  1356. /**
  1357.  * Check the width, height, depth and border of a texture image are legal.
  1358.  * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
  1359.  * functions.
  1360.  * The target and level parameters will have already been validated.
  1361.  * \return GL_TRUE if size is OK, GL_FALSE otherwise.
  1362.  */
  1363. GLboolean
  1364. _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
  1365.                                GLint level, GLint width, GLint height,
  1366.                                GLint depth, GLint border)
  1367. {
  1368.    GLint maxSize;
  1369.  
  1370.    switch (target) {
  1371.    case GL_TEXTURE_1D:
  1372.    case GL_PROXY_TEXTURE_1D:
  1373.       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1); /* level zero size */
  1374.       maxSize >>= level;  /* level size */
  1375.       if (width < 2 * border || width > 2 * border + maxSize)
  1376.          return GL_FALSE;
  1377.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1378.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1379.             return GL_FALSE;
  1380.       }
  1381.       return GL_TRUE;
  1382.  
  1383.    case GL_TEXTURE_2D:
  1384.    case GL_PROXY_TEXTURE_2D:
  1385.    case GL_TEXTURE_2D_MULTISAMPLE:
  1386.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1387.       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
  1388.       maxSize >>= level;
  1389.       if (width < 2 * border || width > 2 * border + maxSize)
  1390.          return GL_FALSE;
  1391.       if (height < 2 * border || height > 2 * border + maxSize)
  1392.          return GL_FALSE;
  1393.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1394.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1395.             return GL_FALSE;
  1396.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1397.             return GL_FALSE;
  1398.       }
  1399.       return GL_TRUE;
  1400.  
  1401.    case GL_TEXTURE_3D:
  1402.    case GL_PROXY_TEXTURE_3D:
  1403.       maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
  1404.       maxSize >>= level;
  1405.       if (width < 2 * border || width > 2 * border + maxSize)
  1406.          return GL_FALSE;
  1407.       if (height < 2 * border || height > 2 * border + maxSize)
  1408.          return GL_FALSE;
  1409.       if (depth < 2 * border || depth > 2 * border + maxSize)
  1410.          return GL_FALSE;
  1411.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1412.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1413.             return GL_FALSE;
  1414.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1415.             return GL_FALSE;
  1416.          if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
  1417.             return GL_FALSE;
  1418.       }
  1419.       return GL_TRUE;
  1420.  
  1421.    case GL_TEXTURE_RECTANGLE_NV:
  1422.    case GL_PROXY_TEXTURE_RECTANGLE_NV:
  1423.       if (level != 0)
  1424.          return GL_FALSE;
  1425.       maxSize = ctx->Const.MaxTextureRectSize;
  1426.       if (width < 0 || width > maxSize)
  1427.          return GL_FALSE;
  1428.       if (height < 0 || height > maxSize)
  1429.          return GL_FALSE;
  1430.       return GL_TRUE;
  1431.  
  1432.    case GL_TEXTURE_CUBE_MAP:
  1433.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1434.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1435.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1436.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1437.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1438.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1439.    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
  1440.       maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
  1441.       maxSize >>= level;
  1442.       if (width != height)
  1443.          return GL_FALSE;
  1444.       if (width < 2 * border || width > 2 * border + maxSize)
  1445.          return GL_FALSE;
  1446.       if (height < 2 * border || height > 2 * border + maxSize)
  1447.          return GL_FALSE;
  1448.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1449.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1450.             return GL_FALSE;
  1451.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1452.             return GL_FALSE;
  1453.       }
  1454.       return GL_TRUE;
  1455.  
  1456.    case GL_TEXTURE_1D_ARRAY_EXT:
  1457.    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  1458.       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
  1459.       maxSize >>= level;
  1460.       if (width < 2 * border || width > 2 * border + maxSize)
  1461.          return GL_FALSE;
  1462.       if (height < 1 || height > ctx->Const.MaxArrayTextureLayers)
  1463.          return GL_FALSE;
  1464.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1465.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1466.             return GL_FALSE;
  1467.       }
  1468.       return GL_TRUE;
  1469.  
  1470.    case GL_TEXTURE_2D_ARRAY_EXT:
  1471.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  1472.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1473.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1474.       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
  1475.       maxSize >>= level;
  1476.       if (width < 2 * border || width > 2 * border + maxSize)
  1477.          return GL_FALSE;
  1478.       if (height < 2 * border || height > 2 * border + maxSize)
  1479.          return GL_FALSE;
  1480.       if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers)
  1481.          return GL_FALSE;
  1482.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1483.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1484.             return GL_FALSE;
  1485.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1486.             return GL_FALSE;
  1487.       }
  1488.       return GL_TRUE;
  1489.  
  1490.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1491.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1492.       maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
  1493.       if (width < 2 * border || width > 2 * border + maxSize)
  1494.          return GL_FALSE;
  1495.       if (height < 2 * border || height > 2 * border + maxSize)
  1496.          return GL_FALSE;
  1497.       if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers || depth % 6)
  1498.          return GL_FALSE;
  1499.       if (width != height)
  1500.          return GL_FALSE;
  1501.       if (level >= ctx->Const.MaxCubeTextureLevels)
  1502.          return GL_FALSE;
  1503.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1504.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1505.             return GL_FALSE;
  1506.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1507.             return GL_FALSE;
  1508.       }
  1509.       return GL_TRUE;
  1510.    default:
  1511.       _mesa_problem(ctx, "Invalid target in _mesa_legal_texture_dimensions()");
  1512.       return GL_FALSE;
  1513.    }
  1514. }
  1515.  
  1516.  
  1517. /**
  1518.  * Do error checking of xoffset, yoffset, zoffset, width, height and depth
  1519.  * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
  1520.  * \param destImage  the destination texture image.
  1521.  * \return GL_TRUE if error found, GL_FALSE otherwise.
  1522.  */
  1523. static GLboolean
  1524. error_check_subtexture_dimensions(struct gl_context *ctx,
  1525.                                   const char *function, GLuint dims,
  1526.                                   const struct gl_texture_image *destImage,
  1527.                                   GLint xoffset, GLint yoffset, GLint zoffset,
  1528.                                   GLsizei subWidth, GLsizei subHeight,
  1529.                                   GLsizei subDepth)
  1530. {
  1531.    const GLenum target = destImage->TexObject->Target;
  1532.    GLuint bw, bh;
  1533.  
  1534.    /* Check size */
  1535.    if (subWidth < 0) {
  1536.       _mesa_error(ctx, GL_INVALID_VALUE,
  1537.                   "%s%dD(width=%d)", function, dims, subWidth);
  1538.       return GL_TRUE;
  1539.    }
  1540.  
  1541.    if (dims > 1 && subHeight < 0) {
  1542.       _mesa_error(ctx, GL_INVALID_VALUE,
  1543.                   "%s%dD(height=%d)", function, dims, subHeight);
  1544.       return GL_TRUE;
  1545.    }
  1546.  
  1547.    if (dims > 2 && subDepth < 0) {
  1548.       _mesa_error(ctx, GL_INVALID_VALUE,
  1549.                   "%s%dD(depth=%d)", function, dims, subDepth);
  1550.       return GL_TRUE;
  1551.    }
  1552.  
  1553.    /* check xoffset and width */
  1554.    if (xoffset < - (GLint) destImage->Border) {
  1555.       _mesa_error(ctx, GL_INVALID_VALUE, "%s%dD(xoffset)",
  1556.                   function, dims);
  1557.       return GL_TRUE;
  1558.    }
  1559.  
  1560.    if (xoffset + subWidth > (GLint) destImage->Width) {
  1561.       _mesa_error(ctx, GL_INVALID_VALUE, "%s%dD(xoffset+width)",
  1562.                   function, dims);
  1563.       return GL_TRUE;
  1564.    }
  1565.  
  1566.    /* check yoffset and height */
  1567.    if (dims > 1) {
  1568.       GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destImage->Border;
  1569.       if (yoffset < -yBorder) {
  1570.          _mesa_error(ctx, GL_INVALID_VALUE, "%s%dD(yoffset)",
  1571.                      function, dims);
  1572.          return GL_TRUE;
  1573.       }
  1574.       if (yoffset + subHeight > (GLint) destImage->Height) {
  1575.          _mesa_error(ctx, GL_INVALID_VALUE, "%s%dD(yoffset+height)",
  1576.                      function, dims);
  1577.          return GL_TRUE;
  1578.       }
  1579.    }
  1580.  
  1581.    /* check zoffset and depth */
  1582.    if (dims > 2) {
  1583.       GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : destImage->Border;
  1584.       if (zoffset < -zBorder) {
  1585.          _mesa_error(ctx, GL_INVALID_VALUE, "%s3D(zoffset)", function);
  1586.          return GL_TRUE;
  1587.       }
  1588.       if (zoffset + subDepth  > (GLint) destImage->Depth) {
  1589.          _mesa_error(ctx, GL_INVALID_VALUE, "%s3D(zoffset+depth)", function);
  1590.          return GL_TRUE;
  1591.       }
  1592.    }
  1593.  
  1594.    /*
  1595.     * The OpenGL spec (and GL_ARB_texture_compression) says only whole
  1596.     * compressed texture images can be updated.  But, that restriction may be
  1597.     * relaxed for particular compressed formats.  At this time, all the
  1598.     * compressed formats supported by Mesa allow sub-textures to be updated
  1599.     * along compressed block boundaries.
  1600.     */
  1601.    _mesa_get_format_block_size(destImage->TexFormat, &bw, &bh);
  1602.  
  1603.    if (bw != 1 || bh != 1) {
  1604.       /* offset must be multiple of block size */
  1605.       if ((xoffset % bw != 0) || (yoffset % bh != 0)) {
  1606.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1607.                      "%s%dD(xoffset = %d, yoffset = %d)",
  1608.                      function, dims, xoffset, yoffset);
  1609.          return GL_TRUE;
  1610.       }
  1611.  
  1612.       /* The size must be a multiple of bw x bh, or we must be using a
  1613.        * offset+size that exactly hits the edge of the image.  This
  1614.        * is important for small mipmap levels (1x1, 2x1, etc) and for
  1615.        * NPOT textures.
  1616.        */
  1617.       if ((subWidth % bw != 0) &&
  1618.           (xoffset + subWidth != (GLint) destImage->Width)) {
  1619.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1620.                      "%s%dD(width = %d)", function, dims, subWidth);
  1621.          return GL_TRUE;
  1622.       }
  1623.  
  1624.       if ((subHeight % bh != 0) &&
  1625.           (yoffset + subHeight != (GLint) destImage->Height)) {
  1626.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1627.                      "%s%dD(height = %d)", function, dims, subHeight);
  1628.          return GL_TRUE;
  1629.       }
  1630.    }
  1631.  
  1632.    return GL_FALSE;
  1633. }
  1634.  
  1635.  
  1636.  
  1637.  
  1638. /**
  1639.  * This is the fallback for Driver.TestProxyTexImage() for doing device-
  1640.  * specific texture image size checks.
  1641.  *
  1642.  * A hardware driver might override this function if, for example, the
  1643.  * max 3D texture size is 512x512x64 (i.e. not a cube).
  1644.  *
  1645.  * Note that width, height, depth == 0 is not an error.  However, a
  1646.  * texture with zero width/height/depth will be considered "incomplete"
  1647.  * and texturing will effectively be disabled.
  1648.  *
  1649.  * \param target  any texture target/type
  1650.  * \param level  as passed to glTexImage
  1651.  * \param format  the MESA_FORMAT_x for the tex image
  1652.  * \param width  as passed to glTexImage
  1653.  * \param height  as passed to glTexImage
  1654.  * \param depth  as passed to glTexImage
  1655.  * \param border  as passed to glTexImage
  1656.  * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
  1657.  */
  1658. GLboolean
  1659. _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
  1660.                           gl_format format,
  1661.                           GLint width, GLint height, GLint depth, GLint border)
  1662. {
  1663.    /* We just check if the image size is less than MaxTextureMbytes.
  1664.     * Some drivers may do more specific checks.
  1665.     */
  1666.    uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
  1667.    uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
  1668.    mbytes *= _mesa_num_tex_faces(target);
  1669.    return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
  1670. }
  1671.  
  1672.  
  1673. /**
  1674.  * Return true if the format is only valid for glCompressedTexImage.
  1675.  */
  1676. static GLboolean
  1677. compressedteximage_only_format(const struct gl_context *ctx, GLenum format)
  1678. {
  1679.    switch (format) {
  1680.    case GL_ETC1_RGB8_OES:
  1681.    case GL_PALETTE4_RGB8_OES:
  1682.    case GL_PALETTE4_RGBA8_OES:
  1683.    case GL_PALETTE4_R5_G6_B5_OES:
  1684.    case GL_PALETTE4_RGBA4_OES:
  1685.    case GL_PALETTE4_RGB5_A1_OES:
  1686.    case GL_PALETTE8_RGB8_OES:
  1687.    case GL_PALETTE8_RGBA8_OES:
  1688.    case GL_PALETTE8_R5_G6_B5_OES:
  1689.    case GL_PALETTE8_RGBA4_OES:
  1690.    case GL_PALETTE8_RGB5_A1_OES:
  1691.       return GL_TRUE;
  1692.    default:
  1693.       return GL_FALSE;
  1694.    }
  1695. }
  1696.  
  1697.  
  1698. /**
  1699.  * Helper function to determine whether a target and specific compression
  1700.  * format are supported.
  1701.  */
  1702. static GLboolean
  1703. target_can_be_compressed(const struct gl_context *ctx, GLenum target,
  1704.                          GLenum intFormat)
  1705. {
  1706.    (void) intFormat;  /* not used yet */
  1707.  
  1708.    switch (target) {
  1709.    case GL_TEXTURE_2D:
  1710.    case GL_PROXY_TEXTURE_2D:
  1711.       return GL_TRUE; /* true for any compressed format so far */
  1712.    case GL_PROXY_TEXTURE_CUBE_MAP:
  1713.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1714.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1715.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1716.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1717.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1718.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1719.       return ctx->Extensions.ARB_texture_cube_map;
  1720.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  1721.    case GL_TEXTURE_2D_ARRAY_EXT:
  1722.       return (ctx->Extensions.MESA_texture_array ||
  1723.               ctx->Extensions.EXT_texture_array);
  1724.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1725.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1726.       return ctx->Extensions.ARB_texture_cube_map_array;
  1727.    default:
  1728.       return GL_FALSE;
  1729.    }
  1730. }
  1731.  
  1732.  
  1733. /**
  1734.  * Check if the given texture target value is legal for a
  1735.  * glTexImage1/2/3D call.
  1736.  */
  1737. static GLboolean
  1738. legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
  1739. {
  1740.    switch (dims) {
  1741.    case 1:
  1742.       switch (target) {
  1743.       case GL_TEXTURE_1D:
  1744.       case GL_PROXY_TEXTURE_1D:
  1745.          return _mesa_is_desktop_gl(ctx);
  1746.       default:
  1747.          return GL_FALSE;
  1748.       }
  1749.    case 2:
  1750.       switch (target) {
  1751.       case GL_TEXTURE_2D:
  1752.          return GL_TRUE;
  1753.       case GL_PROXY_TEXTURE_2D:
  1754.          return _mesa_is_desktop_gl(ctx);
  1755.       case GL_PROXY_TEXTURE_CUBE_MAP:
  1756.          return _mesa_is_desktop_gl(ctx)
  1757.             && ctx->Extensions.ARB_texture_cube_map;
  1758.       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1759.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1760.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1761.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1762.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1763.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1764.          return ctx->Extensions.ARB_texture_cube_map;
  1765.       case GL_TEXTURE_RECTANGLE_NV:
  1766.       case GL_PROXY_TEXTURE_RECTANGLE_NV:
  1767.          return _mesa_is_desktop_gl(ctx)
  1768.             && ctx->Extensions.NV_texture_rectangle;
  1769.       case GL_TEXTURE_1D_ARRAY_EXT:
  1770.       case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  1771.          return _mesa_is_desktop_gl(ctx)
  1772.             && (ctx->Extensions.MESA_texture_array ||
  1773.                 ctx->Extensions.EXT_texture_array);
  1774.       default:
  1775.          return GL_FALSE;
  1776.       }
  1777.    case 3:
  1778.       switch (target) {
  1779.       case GL_TEXTURE_3D:
  1780.          return GL_TRUE;
  1781.       case GL_PROXY_TEXTURE_3D:
  1782.          return _mesa_is_desktop_gl(ctx);
  1783.       case GL_TEXTURE_2D_ARRAY_EXT:
  1784.          return (_mesa_is_desktop_gl(ctx)
  1785.                  && (ctx->Extensions.MESA_texture_array ||
  1786.                      ctx->Extensions.EXT_texture_array))
  1787.             || _mesa_is_gles3(ctx);
  1788.       case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  1789.          return _mesa_is_desktop_gl(ctx)
  1790.             && (ctx->Extensions.MESA_texture_array ||
  1791.                 ctx->Extensions.EXT_texture_array);
  1792.       case GL_TEXTURE_CUBE_MAP_ARRAY:
  1793.       case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1794.          return ctx->Extensions.ARB_texture_cube_map_array;
  1795.       default:
  1796.          return GL_FALSE;
  1797.       }
  1798.    default:
  1799.       _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
  1800.       return GL_FALSE;
  1801.    }
  1802. }
  1803.  
  1804.  
  1805. /**
  1806.  * Check if the given texture target value is legal for a
  1807.  * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
  1808.  * The difference compared to legal_teximage_target() above is that
  1809.  * proxy targets are not supported.
  1810.  */
  1811. static GLboolean
  1812. legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target)
  1813. {
  1814.    switch (dims) {
  1815.    case 1:
  1816.       return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
  1817.    case 2:
  1818.       switch (target) {
  1819.       case GL_TEXTURE_2D:
  1820.          return GL_TRUE;
  1821.       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1822.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1823.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1824.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1825.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1826.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1827.          return ctx->Extensions.ARB_texture_cube_map;
  1828.       case GL_TEXTURE_RECTANGLE_NV:
  1829.          return _mesa_is_desktop_gl(ctx)
  1830.             && ctx->Extensions.NV_texture_rectangle;
  1831.       case GL_TEXTURE_1D_ARRAY_EXT:
  1832.          return _mesa_is_desktop_gl(ctx)
  1833.             && (ctx->Extensions.MESA_texture_array ||
  1834.                 ctx->Extensions.EXT_texture_array);
  1835.       default:
  1836.          return GL_FALSE;
  1837.       }
  1838.    case 3:
  1839.       switch (target) {
  1840.       case GL_TEXTURE_3D:
  1841.          return GL_TRUE;
  1842.       case GL_TEXTURE_2D_ARRAY_EXT:
  1843.          return (_mesa_is_desktop_gl(ctx)
  1844.                  && (ctx->Extensions.MESA_texture_array ||
  1845.                      ctx->Extensions.EXT_texture_array))
  1846.             || _mesa_is_gles3(ctx);
  1847.       case GL_TEXTURE_CUBE_MAP_ARRAY:
  1848.       case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1849.          return ctx->Extensions.ARB_texture_cube_map_array;
  1850.       default:
  1851.          return GL_FALSE;
  1852.       }
  1853.    default:
  1854.       _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
  1855.                     dims);
  1856.       return GL_FALSE;
  1857.    }
  1858. }
  1859.  
  1860.  
  1861. /**
  1862.  * Helper function to determine if a texture object is mutable (in terms
  1863.  * of GL_ARB_texture_storage).
  1864.  */
  1865. static GLboolean
  1866. mutable_tex_object(struct gl_context *ctx, GLenum target)
  1867. {
  1868.    struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
  1869.    return !texObj->Immutable;
  1870. }
  1871.  
  1872.  
  1873. /**
  1874.  * Return expected size of a compressed texture.
  1875.  */
  1876. static GLuint
  1877. compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
  1878.                     GLenum glformat)
  1879. {
  1880.    gl_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
  1881.    return _mesa_format_image_size(mesaFormat, width, height, depth);
  1882. }
  1883.  
  1884.  
  1885. /**
  1886.  * Test the glTexImage[123]D() parameters for errors.
  1887.  *
  1888.  * \param ctx GL context.
  1889.  * \param dimensions texture image dimensions (must be 1, 2 or 3).
  1890.  * \param target texture target given by the user (already validated).
  1891.  * \param level image level given by the user.
  1892.  * \param internalFormat internal format given by the user.
  1893.  * \param format pixel data format given by the user.
  1894.  * \param type pixel data type given by the user.
  1895.  * \param width image width given by the user.
  1896.  * \param height image height given by the user.
  1897.  * \param depth image depth given by the user.
  1898.  * \param border image border given by the user.
  1899.  *
  1900.  * \return GL_TRUE if a error is found, GL_FALSE otherwise
  1901.  *
  1902.  * Verifies each of the parameters against the constants specified in
  1903.  * __struct gl_contextRec::Const and the supported extensions, and according
  1904.  * to the OpenGL specification.
  1905.  * Note that we don't fully error-check the width, height, depth values
  1906.  * here.  That's done in _mesa_legal_texture_dimensions() which is used
  1907.  * by several other GL entrypoints.  Plus, texture dims have a special
  1908.  * interaction with proxy textures.
  1909.  */
  1910. static GLboolean
  1911. texture_error_check( struct gl_context *ctx,
  1912.                      GLuint dimensions, GLenum target,
  1913.                      GLint level, GLint internalFormat,
  1914.                      GLenum format, GLenum type,
  1915.                      GLint width, GLint height,
  1916.                      GLint depth, GLint border )
  1917. {
  1918.    GLboolean colorFormat;
  1919.    GLenum err;
  1920.  
  1921.    /* Even though there are no color-index textures, we still have to support
  1922.     * uploading color-index data and remapping it to RGB via the
  1923.     * GL_PIXEL_MAP_I_TO_[RGBA] tables.
  1924.     */
  1925.    const GLboolean indexFormat = (format == GL_COLOR_INDEX);
  1926.  
  1927.    /* Note: for proxy textures, some error conditions immediately generate
  1928.     * a GL error in the usual way.  But others do not generate a GL error.
  1929.     * Instead, they cause the width, height, depth, format fields of the
  1930.     * texture image to be zeroed-out.  The GL spec seems to indicate that the
  1931.     * zero-out behaviour is only used in cases related to memory allocation.
  1932.     */
  1933.  
  1934.    /* level check */
  1935.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  1936.       _mesa_error(ctx, GL_INVALID_VALUE,
  1937.                   "glTexImage%dD(level=%d)", dimensions, level);
  1938.       return GL_TRUE;
  1939.    }
  1940.  
  1941.    /* Check border */
  1942.    if (border < 0 || border > 1 ||
  1943.        ((ctx->API != API_OPENGL_COMPAT ||
  1944.          target == GL_TEXTURE_RECTANGLE_NV ||
  1945.          target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
  1946.       _mesa_error(ctx, GL_INVALID_VALUE,
  1947.                   "glTexImage%dD(border=%d)", dimensions, border);
  1948.       return GL_TRUE;
  1949.    }
  1950.  
  1951.    if (width < 0 || height < 0 || depth < 0) {
  1952.       _mesa_error(ctx, GL_INVALID_VALUE,
  1953.                   "glTexImage%dD(width, height or depth < 0)", dimensions);
  1954.       return GL_TRUE;
  1955.    }
  1956.  
  1957.    /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
  1958.     * combinations of format, internalFormat, and type that can be used.
  1959.     * Formats and types that require additional extensions (e.g., GL_FLOAT
  1960.     * requires GL_OES_texture_float) are filtered elsewhere.
  1961.     */
  1962.  
  1963.    if (_mesa_is_gles(ctx)) {
  1964.       if (_mesa_is_gles3(ctx)) {
  1965.          err = _mesa_es3_error_check_format_and_type(format, type,
  1966.                                                      internalFormat);
  1967.       } else {
  1968.          if (format != internalFormat) {
  1969.             _mesa_error(ctx, GL_INVALID_OPERATION,
  1970.                         "glTexImage%dD(format = %s, internalFormat = %s)",
  1971.                         dimensions,
  1972.                         _mesa_lookup_enum_by_nr(format),
  1973.                         _mesa_lookup_enum_by_nr(internalFormat));
  1974.             return GL_TRUE;
  1975.          }
  1976.  
  1977.          err = _mesa_es_error_check_format_and_type(format, type, dimensions);
  1978.       }
  1979.       if (err != GL_NO_ERROR) {
  1980.          _mesa_error(ctx, err,
  1981.                      "glTexImage%dD(format = %s, type = %s, internalFormat = %s)",
  1982.                      dimensions,
  1983.                      _mesa_lookup_enum_by_nr(format),
  1984.                      _mesa_lookup_enum_by_nr(type),
  1985.                      _mesa_lookup_enum_by_nr(internalFormat));
  1986.          return GL_TRUE;
  1987.       }
  1988.    }
  1989.  
  1990.    /* Check internalFormat */
  1991.    if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
  1992.       _mesa_error(ctx, GL_INVALID_VALUE,
  1993.                   "glTexImage%dD(internalFormat=%s)",
  1994.                   dimensions, _mesa_lookup_enum_by_nr(internalFormat));
  1995.       return GL_TRUE;
  1996.    }
  1997.  
  1998.    /* Check incoming image format and type */
  1999.    err = _mesa_error_check_format_and_type(ctx, format, type);
  2000.    if (err != GL_NO_ERROR) {
  2001.       _mesa_error(ctx, err,
  2002.                   "glTexImage%dD(incompatible format = %s, type = %s)",
  2003.                   dimensions, _mesa_lookup_enum_by_nr(format),
  2004.                   _mesa_lookup_enum_by_nr(type));
  2005.       return GL_TRUE;
  2006.    }
  2007.  
  2008.    /* make sure internal format and format basically agree */
  2009.    colorFormat = _mesa_is_color_format(format);
  2010.    if ((_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat) ||
  2011.        (_mesa_is_depth_format(internalFormat) != _mesa_is_depth_format(format)) ||
  2012.        (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format)) ||
  2013.        (_mesa_is_depthstencil_format(internalFormat) != _mesa_is_depthstencil_format(format)) ||
  2014.        (_mesa_is_dudv_format(internalFormat) != _mesa_is_dudv_format(format))) {
  2015.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2016.                   "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
  2017.                   dimensions, _mesa_lookup_enum_by_nr(internalFormat),
  2018.                   _mesa_lookup_enum_by_nr(format));
  2019.       return GL_TRUE;
  2020.    }
  2021.  
  2022.    /* additional checks for ycbcr textures */
  2023.    if (internalFormat == GL_YCBCR_MESA) {
  2024.       ASSERT(ctx->Extensions.MESA_ycbcr_texture);
  2025.       if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
  2026.           type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
  2027.          char message[100];
  2028.          _mesa_snprintf(message, sizeof(message),
  2029.                         "glTexImage%dD(format/type YCBCR mismatch)",
  2030.                         dimensions);
  2031.          _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
  2032.          return GL_TRUE; /* error */
  2033.       }
  2034.       if (target != GL_TEXTURE_2D &&
  2035.           target != GL_PROXY_TEXTURE_2D &&
  2036.           target != GL_TEXTURE_RECTANGLE_NV &&
  2037.           target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
  2038.          _mesa_error(ctx, GL_INVALID_ENUM,
  2039.                      "glTexImage%dD(bad target for YCbCr texture)",
  2040.                      dimensions);
  2041.          return GL_TRUE;
  2042.       }
  2043.       if (border != 0) {
  2044.          char message[100];
  2045.          _mesa_snprintf(message, sizeof(message),
  2046.                         "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
  2047.                         dimensions, border);
  2048.          _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
  2049.          return GL_TRUE;
  2050.       }
  2051.    }
  2052.  
  2053.    /* additional checks for depth textures */
  2054.    if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
  2055.        || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL) {
  2056.       /* Only 1D, 2D, rect, array and cube textures supported, not 3D
  2057.        * Cubemaps are only supported for GL version > 3.0 or with EXT_gpu_shader4 */
  2058.       if (target != GL_TEXTURE_1D &&
  2059.           target != GL_PROXY_TEXTURE_1D &&
  2060.           target != GL_TEXTURE_2D &&
  2061.           target != GL_PROXY_TEXTURE_2D &&
  2062.           target != GL_TEXTURE_1D_ARRAY &&
  2063.           target != GL_PROXY_TEXTURE_1D_ARRAY &&
  2064.           target != GL_TEXTURE_2D_ARRAY &&
  2065.           target != GL_PROXY_TEXTURE_2D_ARRAY &&
  2066.           target != GL_TEXTURE_RECTANGLE_ARB &&
  2067.           target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
  2068.          !((_mesa_is_cube_face(target) || target == GL_PROXY_TEXTURE_CUBE_MAP) &&
  2069.            (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4
  2070.             || (ctx->API == API_OPENGLES2 && ctx->Extensions.OES_depth_texture_cube_map))) &&
  2071.           !((target == GL_TEXTURE_CUBE_MAP_ARRAY ||
  2072.              target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY) &&
  2073.             ctx->Extensions.ARB_texture_cube_map_array)) {
  2074.          _mesa_error(ctx, GL_INVALID_ENUM,
  2075.                      "glTexImage%dD(bad target for depth texture)",
  2076.                      dimensions);
  2077.          return GL_TRUE;
  2078.       }
  2079.    }
  2080.  
  2081.    /* additional checks for compressed textures */
  2082.    if (_mesa_is_compressed_format(ctx, internalFormat)) {
  2083.       if (!target_can_be_compressed(ctx, target, internalFormat)) {
  2084.          _mesa_error(ctx, GL_INVALID_ENUM,
  2085.                      "glTexImage%dD(target can't be compressed)", dimensions);
  2086.          return GL_TRUE;
  2087.       }
  2088.       if (compressedteximage_only_format(ctx, internalFormat)) {
  2089.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2090.                      "glTexImage%dD(no compression for format)", dimensions);
  2091.          return GL_TRUE;
  2092.       }
  2093.       if (border != 0) {
  2094.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2095.                      "glTexImage%dD(border!=0)", dimensions);
  2096.          return GL_TRUE;
  2097.       }
  2098.    }
  2099.  
  2100.    /* additional checks for integer textures */
  2101.    if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
  2102.        (_mesa_is_enum_format_integer(format) !=
  2103.         _mesa_is_enum_format_integer(internalFormat))) {
  2104.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2105.                   "glTexImage%dD(integer/non-integer format mismatch)",
  2106.                   dimensions);
  2107.       return GL_TRUE;
  2108.    }
  2109.  
  2110.    if (!mutable_tex_object(ctx, target)) {
  2111.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2112.                   "glTexImage%dD(immutable texture)", dimensions);
  2113.       return GL_TRUE;
  2114.    }
  2115.  
  2116.    /* if we get here, the parameters are OK */
  2117.    return GL_FALSE;
  2118. }
  2119.  
  2120.  
  2121. /**
  2122.  * Error checking for glCompressedTexImage[123]D().
  2123.  * Note that the width, height and depth values are not fully error checked
  2124.  * here.
  2125.  * \return GL_TRUE if a error is found, GL_FALSE otherwise
  2126.  */
  2127. static GLenum
  2128. compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
  2129.                                GLenum target, GLint level,
  2130.                                GLenum internalFormat, GLsizei width,
  2131.                                GLsizei height, GLsizei depth, GLint border,
  2132.                                GLsizei imageSize)
  2133. {
  2134.    const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
  2135.    GLint expectedSize;
  2136.    GLenum error = GL_NO_ERROR;
  2137.    char *reason = ""; /* no error */
  2138.  
  2139.    if (!target_can_be_compressed(ctx, target, internalFormat)) {
  2140.       reason = "target";
  2141.       error = GL_INVALID_ENUM;
  2142.       goto error;
  2143.    }
  2144.  
  2145.    /* This will detect any invalid internalFormat value */
  2146.    if (!_mesa_is_compressed_format(ctx, internalFormat)) {
  2147.       reason = "internalFormat";
  2148.       error = GL_INVALID_ENUM;
  2149.       goto error;
  2150.    }
  2151.  
  2152.    switch (internalFormat) {
  2153.    case GL_PALETTE4_RGB8_OES:
  2154.    case GL_PALETTE4_RGBA8_OES:
  2155.    case GL_PALETTE4_R5_G6_B5_OES:
  2156.    case GL_PALETTE4_RGBA4_OES:
  2157.    case GL_PALETTE4_RGB5_A1_OES:
  2158.    case GL_PALETTE8_RGB8_OES:
  2159.    case GL_PALETTE8_RGBA8_OES:
  2160.    case GL_PALETTE8_R5_G6_B5_OES:
  2161.    case GL_PALETTE8_RGBA4_OES:
  2162.    case GL_PALETTE8_RGB5_A1_OES:
  2163.       /* check level (note that level should be zero or less!) */
  2164.       if (level > 0 || level < -maxLevels) {
  2165.          reason = "level";
  2166.          error = GL_INVALID_VALUE;
  2167.          goto error;
  2168.       }
  2169.  
  2170.       if (dimensions != 2) {
  2171.          reason = "compressed paletted textures must be 2D";
  2172.          error = GL_INVALID_OPERATION;
  2173.          goto error;
  2174.       }
  2175.  
  2176.       /* Figure out the expected texture size (in bytes).  This will be
  2177.        * checked against the actual size later.
  2178.        */
  2179.       expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
  2180.                                                 width, height);
  2181.  
  2182.       /* This is for the benefit of the TestProxyTexImage below.  It expects
  2183.        * level to be non-negative.  OES_compressed_paletted_texture uses a
  2184.        * weird mechanism where the level specified to glCompressedTexImage2D
  2185.        * is -(n-1) number of levels in the texture, and the data specifies the
  2186.        * complete mipmap stack.  This is done to ensure the palette is the
  2187.        * same for all levels.
  2188.        */
  2189.       level = -level;
  2190.       break;
  2191.  
  2192.    default:
  2193.       /* check level */
  2194.       if (level < 0 || level >= maxLevels) {
  2195.          reason = "level";
  2196.          error = GL_INVALID_VALUE;
  2197.          goto error;
  2198.       }
  2199.  
  2200.       /* Figure out the expected texture size (in bytes).  This will be
  2201.        * checked against the actual size later.
  2202.        */
  2203.       expectedSize = compressed_tex_size(width, height, depth, internalFormat);
  2204.       break;
  2205.    }
  2206.  
  2207.    /* This should really never fail */
  2208.    if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
  2209.       reason = "internalFormat";
  2210.       error = GL_INVALID_ENUM;
  2211.       goto error;
  2212.    }
  2213.  
  2214.    /* No compressed formats support borders at this time */
  2215.    if (border != 0) {
  2216.       reason = "border != 0";
  2217.       error = GL_INVALID_VALUE;
  2218.       goto error;
  2219.    }
  2220.  
  2221.    /* check image size in bytes */
  2222.    if (expectedSize != imageSize) {
  2223.       /* Per GL_ARB_texture_compression:  GL_INVALID_VALUE is generated [...]
  2224.        * if <imageSize> is not consistent with the format, dimensions, and
  2225.        * contents of the specified image.
  2226.        */
  2227.       reason = "imageSize inconsistant with width/height/format";
  2228.       error = GL_INVALID_VALUE;
  2229.       goto error;
  2230.    }
  2231.  
  2232.    if (!mutable_tex_object(ctx, target)) {
  2233.       reason = "immutable texture";
  2234.       error = GL_INVALID_OPERATION;
  2235.       goto error;
  2236.    }
  2237.  
  2238.    return GL_FALSE;
  2239.  
  2240. error:
  2241.    _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)", dimensions, reason);
  2242.    return GL_TRUE;
  2243. }
  2244.  
  2245.  
  2246.  
  2247. /**
  2248.  * Test glTexSubImage[123]D() parameters for errors.
  2249.  *
  2250.  * \param ctx GL context.
  2251.  * \param dimensions texture image dimensions (must be 1, 2 or 3).
  2252.  * \param target texture target given by the user (already validated)
  2253.  * \param level image level given by the user.
  2254.  * \param xoffset sub-image x offset given by the user.
  2255.  * \param yoffset sub-image y offset given by the user.
  2256.  * \param zoffset sub-image z offset given by the user.
  2257.  * \param format pixel data format given by the user.
  2258.  * \param type pixel data type given by the user.
  2259.  * \param width image width given by the user.
  2260.  * \param height image height given by the user.
  2261.  * \param depth image depth given by the user.
  2262.  *
  2263.  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
  2264.  *
  2265.  * Verifies each of the parameters against the constants specified in
  2266.  * __struct gl_contextRec::Const and the supported extensions, and according
  2267.  * to the OpenGL specification.
  2268.  */
  2269. static GLboolean
  2270. texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
  2271.                         GLenum target, GLint level,
  2272.                         GLint xoffset, GLint yoffset, GLint zoffset,
  2273.                         GLint width, GLint height, GLint depth,
  2274.                         GLenum format, GLenum type)
  2275. {
  2276.    struct gl_texture_object *texObj;
  2277.    struct gl_texture_image *texImage;
  2278.    GLenum err;
  2279.  
  2280.    /* check target (proxies not allowed) */
  2281.    if (!legal_texsubimage_target(ctx, dimensions, target)) {
  2282.       _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
  2283.                   dimensions, _mesa_lookup_enum_by_nr(target));
  2284.       return GL_TRUE;
  2285.    }
  2286.  
  2287.    /* level check */
  2288.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  2289.       _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(level=%d)",
  2290.                   dimensions, level);
  2291.       return GL_TRUE;
  2292.    }
  2293.  
  2294.    /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
  2295.     * combinations of format and type that can be used.  Formats and types
  2296.     * that require additional extensions (e.g., GL_FLOAT requires
  2297.     * GL_OES_texture_float) are filtered elsewhere.
  2298.     */
  2299.    if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
  2300.       err = _mesa_es_error_check_format_and_type(format, type, dimensions);
  2301.       if (err != GL_NO_ERROR) {
  2302.          _mesa_error(ctx, err,
  2303.                      "glTexSubImage%dD(format = %s, type = %s)",
  2304.                      dimensions,
  2305.                      _mesa_lookup_enum_by_nr(format),
  2306.                      _mesa_lookup_enum_by_nr(type));
  2307.          return GL_TRUE;
  2308.       }
  2309.    }
  2310.  
  2311.    err = _mesa_error_check_format_and_type(ctx, format, type);
  2312.    if (err != GL_NO_ERROR) {
  2313.       _mesa_error(ctx, err,
  2314.                   "glTexSubImage%dD(incompatible format = %s, type = %s)",
  2315.                   dimensions, _mesa_lookup_enum_by_nr(format),
  2316.                   _mesa_lookup_enum_by_nr(type));
  2317.       return GL_TRUE;
  2318.    }
  2319.  
  2320.    /* Get dest texture object / image pointers */
  2321.    texObj = _mesa_get_current_tex_object(ctx, target);
  2322.    if (!texObj) {
  2323.       /* must be out of memory */
  2324.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage%dD()", dimensions);
  2325.       return GL_TRUE;
  2326.    }
  2327.  
  2328.    texImage = _mesa_select_tex_image(ctx, texObj, target, level);
  2329.    if (!texImage) {
  2330.       /* non-existant texture level */
  2331.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2332.                   "glTexSubImage%dD(invalid texture image)", dimensions);
  2333.       return GL_TRUE;
  2334.    }
  2335.  
  2336.    if (error_check_subtexture_dimensions(ctx, "glTexSubImage", dimensions,
  2337.                                          texImage, xoffset, yoffset, 0,
  2338.                                          width, height, 1)) {
  2339.       return GL_TRUE;
  2340.    }
  2341.  
  2342.    if (_mesa_is_format_compressed(texImage->TexFormat)) {
  2343.       if (compressedteximage_only_format(ctx, texImage->InternalFormat)) {
  2344.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2345.                "glTexSubImage%dD(no compression for format)", dimensions);
  2346.          return GL_TRUE;
  2347.       }
  2348.    }
  2349.  
  2350.    if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
  2351.       /* both source and dest must be integer-valued, or neither */
  2352.       if (_mesa_is_format_integer_color(texImage->TexFormat) !=
  2353.           _mesa_is_enum_format_integer(format)) {
  2354.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2355.                      "glTexSubImage%dD(integer/non-integer format mismatch)",
  2356.                      dimensions);
  2357.          return GL_TRUE;
  2358.       }
  2359.    }
  2360.  
  2361.    return GL_FALSE;
  2362. }
  2363.  
  2364.  
  2365. /**
  2366.  * Test glCopyTexImage[12]D() parameters for errors.
  2367.  *
  2368.  * \param ctx GL context.
  2369.  * \param dimensions texture image dimensions (must be 1, 2 or 3).
  2370.  * \param target texture target given by the user.
  2371.  * \param level image level given by the user.
  2372.  * \param internalFormat internal format given by the user.
  2373.  * \param width image width given by the user.
  2374.  * \param height image height given by the user.
  2375.  * \param border texture border.
  2376.  *
  2377.  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
  2378.  *
  2379.  * Verifies each of the parameters against the constants specified in
  2380.  * __struct gl_contextRec::Const and the supported extensions, and according
  2381.  * to the OpenGL specification.
  2382.  */
  2383. static GLboolean
  2384. copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
  2385.                          GLenum target, GLint level, GLint internalFormat,
  2386.                          GLint width, GLint height, GLint border )
  2387. {
  2388.    GLint baseFormat;
  2389.    GLint rb_base_format;
  2390.    struct gl_renderbuffer *rb;
  2391.    GLenum rb_internal_format;
  2392.  
  2393.    /* check target */
  2394.    if (!legal_texsubimage_target(ctx, dimensions, target)) {
  2395.       _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
  2396.                   dimensions, _mesa_lookup_enum_by_nr(target));
  2397.       return GL_TRUE;
  2398.    }
  2399.  
  2400.    /* level check */
  2401.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  2402.       _mesa_error(ctx, GL_INVALID_VALUE,
  2403.                   "glCopyTexImage%dD(level=%d)", dimensions, level);
  2404.       return GL_TRUE;
  2405.    }
  2406.  
  2407.    /* Check that the source buffer is complete */
  2408.    if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
  2409.       if (ctx->ReadBuffer->_Status == 0) {
  2410.          _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
  2411.       }
  2412.       if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  2413.          _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
  2414.                      "glCopyTexImage%dD(invalid readbuffer)", dimensions);
  2415.          return GL_TRUE;
  2416.       }
  2417.  
  2418.       if (ctx->ReadBuffer->Visual.samples > 0) {
  2419.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2420.                      "glCopyTexImage%dD(multisample FBO)",
  2421.                      dimensions);
  2422.          return GL_TRUE;
  2423.       }
  2424.    }
  2425.  
  2426.    /* Check border */
  2427.    if (border < 0 || border > 1 ||
  2428.        ((ctx->API != API_OPENGL_COMPAT ||
  2429.          target == GL_TEXTURE_RECTANGLE_NV ||
  2430.          target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
  2431.       _mesa_error(ctx, GL_INVALID_VALUE,
  2432.                   "glCopyTexImage%dD(border=%d)", dimensions, border);
  2433.       return GL_TRUE;
  2434.    }
  2435.  
  2436.    rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
  2437.    if (rb == NULL) {
  2438.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2439.                   "glCopyTexImage%dD(read buffer)", dimensions);
  2440.       return GL_TRUE;
  2441.    }
  2442.  
  2443.    /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
  2444.     * internalFormat.
  2445.     */
  2446.    if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
  2447.       switch (internalFormat) {
  2448.       case GL_ALPHA:
  2449.       case GL_RGB:
  2450.       case GL_RGBA:
  2451.       case GL_LUMINANCE:
  2452.       case GL_LUMINANCE_ALPHA:
  2453.          break;
  2454.       default:
  2455.          _mesa_error(ctx, GL_INVALID_VALUE,
  2456.                      "glCopyTexImage%dD(internalFormat)", dimensions);
  2457.          return GL_TRUE;
  2458.       }
  2459.    }
  2460.  
  2461.    baseFormat = _mesa_base_tex_format(ctx, internalFormat);
  2462.    if (baseFormat < 0) {
  2463.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2464.                   "glCopyTexImage%dD(internalFormat)", dimensions);
  2465.       return GL_TRUE;
  2466.    }
  2467.  
  2468.    rb_internal_format = rb->InternalFormat;
  2469.    rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
  2470.    if (_mesa_is_color_format(internalFormat)) {
  2471.       if (rb_base_format < 0) {
  2472.          _mesa_error(ctx, GL_INVALID_VALUE,
  2473.                      "glCopyTexImage%dD(internalFormat)", dimensions);
  2474.          return GL_TRUE;
  2475.       }
  2476.    }
  2477.  
  2478.    if (_mesa_is_gles(ctx)) {
  2479.       bool valid = true;
  2480.       if (_mesa_base_format_component_count(baseFormat) >
  2481.           _mesa_base_format_component_count(rb_base_format)) {
  2482.          valid = false;
  2483.       }
  2484.       if (baseFormat == GL_DEPTH_COMPONENT ||
  2485.           baseFormat == GL_DEPTH_STENCIL ||
  2486.           rb_base_format == GL_DEPTH_COMPONENT ||
  2487.           rb_base_format == GL_DEPTH_STENCIL ||
  2488.           ((baseFormat == GL_LUMINANCE_ALPHA ||
  2489.             baseFormat == GL_ALPHA) &&
  2490.            rb_base_format != GL_RGBA) ||
  2491.           internalFormat == GL_RGB9_E5) {
  2492.          valid = false;
  2493.       }
  2494.       if (internalFormat == GL_RGB9_E5) {
  2495.          valid = false;
  2496.       }
  2497.       if (!valid) {
  2498.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2499.                      "glCopyTexImage%dD(internalFormat)", dimensions);
  2500.          return GL_TRUE;
  2501.       }
  2502.    }
  2503.  
  2504.    if (_mesa_is_gles3(ctx)) {
  2505.       bool rb_is_srgb = false;
  2506.       bool dst_is_srgb = false;
  2507.  
  2508.       if (ctx->Extensions.EXT_framebuffer_sRGB &&
  2509.           _mesa_get_format_color_encoding(rb->Format) == GL_SRGB) {
  2510.          rb_is_srgb = true;
  2511.       }
  2512.  
  2513.       if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
  2514.          dst_is_srgb = true;
  2515.       }
  2516.  
  2517.       if (rb_is_srgb != dst_is_srgb) {
  2518.          /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
  2519.           * OpenGLES 3.0.0 spec says:
  2520.           *
  2521.           *     "The error INVALID_OPERATION is also generated if the
  2522.           *     value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
  2523.           *     framebuffer attachment corresponding to the read buffer
  2524.           *     is LINEAR (see section 6.1.13) and internalformat is
  2525.           *     one of the sRGB formats described in section 3.8.16, or
  2526.           *     if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
  2527.           *     SRGB and internalformat is not one of the sRGB formats."
  2528.           */
  2529.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2530.                      "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
  2531.          return GL_TRUE;
  2532.       }
  2533.    }
  2534.  
  2535.    if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
  2536.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2537.                   "glCopyTexImage%dD(missing readbuffer)", dimensions);
  2538.       return GL_TRUE;
  2539.    }
  2540.  
  2541.    /* From the EXT_texture_integer spec:
  2542.     *
  2543.     *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
  2544.     *      if the texture internalformat is an integer format and the read color
  2545.     *      buffer is not an integer format, or if the internalformat is not an
  2546.     *      integer format and the read color buffer is an integer format."
  2547.     */
  2548.    if (_mesa_is_color_format(internalFormat)) {
  2549.       bool is_int = _mesa_is_enum_format_integer(internalFormat);
  2550.       bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
  2551.       if (is_int || is_rbint) {
  2552.          if (is_int != is_rbint) {
  2553.             _mesa_error(ctx, GL_INVALID_OPERATION,
  2554.                         "glCopyTexImage%dD(integer vs non-integer)", dimensions);
  2555.             return GL_TRUE;
  2556.          } else if (_mesa_is_gles(ctx) &&
  2557.                     _mesa_is_enum_format_unsigned_int(internalFormat) !=
  2558.                       _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
  2559.             _mesa_error(ctx, GL_INVALID_OPERATION,
  2560.                         "glCopyTexImage%dD(signed vs unsigned integer)", dimensions);
  2561.             return GL_TRUE;
  2562.          }
  2563.       }
  2564.    }
  2565.  
  2566.    if (_mesa_is_compressed_format(ctx, internalFormat)) {
  2567.       if (!target_can_be_compressed(ctx, target, internalFormat)) {
  2568.          _mesa_error(ctx, GL_INVALID_ENUM,
  2569.                      "glCopyTexImage%dD(target)", dimensions);
  2570.          return GL_TRUE;
  2571.       }
  2572.       if (compressedteximage_only_format(ctx, internalFormat)) {
  2573.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2574.                "glCopyTexImage%dD(no compression for format)", dimensions);
  2575.          return GL_TRUE;
  2576.       }
  2577.       if (border != 0) {
  2578.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2579.                      "glCopyTexImage%dD(border!=0)", dimensions);
  2580.          return GL_TRUE;
  2581.       }
  2582.    }
  2583.  
  2584.    if (!mutable_tex_object(ctx, target)) {
  2585.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2586.                   "glCopyTexImage%dD(immutable texture)", dimensions);
  2587.       return GL_TRUE;
  2588.    }
  2589.  
  2590.    /* if we get here, the parameters are OK */
  2591.    return GL_FALSE;
  2592. }
  2593.  
  2594.  
  2595. /**
  2596.  * Test glCopyTexSubImage[12]D() parameters for errors.
  2597.  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
  2598.  */
  2599. static GLboolean
  2600. copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
  2601.                             GLenum target, GLint level,
  2602.                             GLint xoffset, GLint yoffset, GLint zoffset,
  2603.                             GLint width, GLint height)
  2604. {
  2605.    struct gl_texture_object *texObj;
  2606.    struct gl_texture_image *texImage;
  2607.  
  2608.    /* Check that the source buffer is complete */
  2609.    if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
  2610.       if (ctx->ReadBuffer->_Status == 0) {
  2611.          _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
  2612.       }
  2613.       if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  2614.          _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
  2615.                      "glCopyTexImage%dD(invalid readbuffer)", dimensions);
  2616.          return GL_TRUE;
  2617.       }
  2618.  
  2619.       if (ctx->ReadBuffer->Visual.samples > 0) {
  2620.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2621.                      "glCopyTexSubImage%dD(multisample FBO)",
  2622.                      dimensions);
  2623.          return GL_TRUE;
  2624.       }
  2625.    }
  2626.  
  2627.    /* check target (proxies not allowed) */
  2628.    if (!legal_texsubimage_target(ctx, dimensions, target)) {
  2629.       _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexSubImage%uD(target=%s)",
  2630.                   dimensions, _mesa_lookup_enum_by_nr(target));
  2631.       return GL_TRUE;
  2632.    }
  2633.  
  2634.    /* Check level */
  2635.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  2636.       _mesa_error(ctx, GL_INVALID_VALUE,
  2637.                   "glCopyTexSubImage%dD(level=%d)", dimensions, level);
  2638.       return GL_TRUE;
  2639.    }
  2640.  
  2641.    /* Get dest texture object / image pointers */
  2642.    texObj = _mesa_get_current_tex_object(ctx, target);
  2643.    if (!texObj) {
  2644.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%dD()", dimensions);
  2645.       return GL_TRUE;
  2646.    }
  2647.  
  2648.    texImage = _mesa_select_tex_image(ctx, texObj, target, level);
  2649.    if (!texImage) {
  2650.       /* destination image does not exist */
  2651.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2652.                   "glCopyTexSubImage%dD(invalid texture image)", dimensions);
  2653.       return GL_TRUE;
  2654.    }
  2655.  
  2656.    if (error_check_subtexture_dimensions(ctx, "glCopyTexSubImage",
  2657.                                          dimensions, texImage,
  2658.                                          xoffset, yoffset, zoffset,
  2659.                                          width, height, 1)) {
  2660.       return GL_TRUE;
  2661.    }
  2662.  
  2663.    if (_mesa_is_format_compressed(texImage->TexFormat)) {
  2664.       if (compressedteximage_only_format(ctx, texImage->InternalFormat)) {
  2665.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2666.                "glCopyTexSubImage%dD(no compression for format)", dimensions);
  2667.          return GL_TRUE;
  2668.       }
  2669.    }
  2670.  
  2671.    if (texImage->InternalFormat == GL_YCBCR_MESA) {
  2672.       _mesa_error(ctx, GL_INVALID_OPERATION, "glCopyTexSubImage2D");
  2673.       return GL_TRUE;
  2674.    }
  2675.  
  2676.    if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
  2677.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2678.                   "glCopyTexSubImage%dD(missing readbuffer, format=0x%x)",
  2679.                   dimensions, texImage->_BaseFormat);
  2680.       return GL_TRUE;
  2681.    }
  2682.  
  2683.    /* From the EXT_texture_integer spec:
  2684.     *
  2685.     *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
  2686.     *      if the texture internalformat is an integer format and the read color
  2687.     *      buffer is not an integer format, or if the internalformat is not an
  2688.     *      integer format and the read color buffer is an integer format."
  2689.     */
  2690.    if (_mesa_is_color_format(texImage->InternalFormat)) {
  2691.       struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
  2692.  
  2693.       if (_mesa_is_format_integer_color(rb->Format) !=
  2694.           _mesa_is_format_integer_color(texImage->TexFormat)) {
  2695.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2696.                      "glCopyTexImage%dD(integer vs non-integer)", dimensions);
  2697.          return GL_TRUE;
  2698.       }
  2699.    }
  2700.  
  2701.    /* if we get here, the parameters are OK */
  2702.    return GL_FALSE;
  2703. }
  2704.  
  2705.  
  2706. /** Callback info for walking over FBO hash table */
  2707. struct cb_info
  2708. {
  2709.    struct gl_context *ctx;
  2710.    struct gl_texture_object *texObj;
  2711.    GLuint level, face;
  2712. };
  2713.  
  2714.  
  2715. /**
  2716.  * Check render to texture callback.  Called from _mesa_HashWalk().
  2717.  */
  2718. static void
  2719. check_rtt_cb(GLuint key, void *data, void *userData)
  2720. {
  2721.    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
  2722.    const struct cb_info *info = (struct cb_info *) userData;
  2723.    struct gl_context *ctx = info->ctx;
  2724.    const struct gl_texture_object *texObj = info->texObj;
  2725.    const GLuint level = info->level, face = info->face;
  2726.  
  2727.    /* If this is a user-created FBO */
  2728.    if (_mesa_is_user_fbo(fb)) {
  2729.       GLuint i;
  2730.       /* check if any of the FBO's attachments point to 'texObj' */
  2731.       for (i = 0; i < BUFFER_COUNT; i++) {
  2732.          struct gl_renderbuffer_attachment *att = fb->Attachment + i;
  2733.          if (att->Type == GL_TEXTURE &&
  2734.              att->Texture == texObj &&
  2735.              att->TextureLevel == level &&
  2736.              att->CubeMapFace == face) {
  2737.             _mesa_update_texture_renderbuffer(ctx, ctx->DrawBuffer, att);
  2738.             ASSERT(att->Renderbuffer->TexImage);
  2739.             /* Mark fb status as indeterminate to force re-validation */
  2740.             fb->_Status = 0;
  2741.          }
  2742.       }
  2743.    }
  2744. }
  2745.  
  2746.  
  2747. /**
  2748.  * When a texture image is specified we have to check if it's bound to
  2749.  * any framebuffer objects (render to texture) in order to detect changes
  2750.  * in size or format since that effects FBO completeness.
  2751.  * Any FBOs rendering into the texture must be re-validated.
  2752.  */
  2753. void
  2754. _mesa_update_fbo_texture(struct gl_context *ctx,
  2755.                          struct gl_texture_object *texObj,
  2756.                          GLuint face, GLuint level)
  2757. {
  2758.    /* Only check this texture if it's been marked as RenderToTexture */
  2759.    if (texObj->_RenderToTexture) {
  2760.       struct cb_info info;
  2761.       info.ctx = ctx;
  2762.       info.texObj = texObj;
  2763.       info.level = level;
  2764.       info.face = face;
  2765.       _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
  2766.    }
  2767. }
  2768.  
  2769.  
  2770. /**
  2771.  * If the texture object's GenerateMipmap flag is set and we've
  2772.  * changed the texture base level image, regenerate the rest of the
  2773.  * mipmap levels now.
  2774.  */
  2775. static inline void
  2776. check_gen_mipmap(struct gl_context *ctx, GLenum target,
  2777.                  struct gl_texture_object *texObj, GLint level)
  2778. {
  2779.    ASSERT(target != GL_TEXTURE_CUBE_MAP);
  2780.    if (texObj->GenerateMipmap &&
  2781.        level == texObj->BaseLevel &&
  2782.        level < texObj->MaxLevel) {
  2783.       ASSERT(ctx->Driver.GenerateMipmap);
  2784.       ctx->Driver.GenerateMipmap(ctx, target, texObj);
  2785.    }
  2786. }
  2787.  
  2788.  
  2789. /** Debug helper: override the user-requested internal format */
  2790. static GLenum
  2791. override_internal_format(GLenum internalFormat, GLint width, GLint height)
  2792. {
  2793. #if 0
  2794.    if (internalFormat == GL_RGBA16F_ARB ||
  2795.        internalFormat == GL_RGBA32F_ARB) {
  2796.       printf("Convert rgba float tex to int %d x %d\n", width, height);
  2797.       return GL_RGBA;
  2798.    }
  2799.    else if (internalFormat == GL_RGB16F_ARB ||
  2800.             internalFormat == GL_RGB32F_ARB) {
  2801.       printf("Convert rgb float tex to int %d x %d\n", width, height);
  2802.       return GL_RGB;
  2803.    }
  2804.    else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
  2805.             internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
  2806.       printf("Convert luminance float tex to int %d x %d\n", width, height);
  2807.       return GL_LUMINANCE_ALPHA;
  2808.    }
  2809.    else if (internalFormat == GL_LUMINANCE16F_ARB ||
  2810.             internalFormat == GL_LUMINANCE32F_ARB) {
  2811.       printf("Convert luminance float tex to int %d x %d\n", width, height);
  2812.       return GL_LUMINANCE;
  2813.    }
  2814.    else if (internalFormat == GL_ALPHA16F_ARB ||
  2815.             internalFormat == GL_ALPHA32F_ARB) {
  2816.       printf("Convert luminance float tex to int %d x %d\n", width, height);
  2817.       return GL_ALPHA;
  2818.    }
  2819.    /*
  2820.    else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
  2821.       internalFormat = GL_RGBA;
  2822.    }
  2823.    */
  2824.    else {
  2825.       return internalFormat;
  2826.    }
  2827. #else
  2828.    return internalFormat;
  2829. #endif
  2830. }
  2831.  
  2832.  
  2833. /**
  2834.  * Choose the actual hardware format for a texture image.
  2835.  * Try to use the same format as the previous image level when possible.
  2836.  * Otherwise, ask the driver for the best format.
  2837.  * It's important to try to choose a consistant format for all levels
  2838.  * for efficient texture memory layout/allocation.  In particular, this
  2839.  * comes up during automatic mipmap generation.
  2840.  */
  2841. gl_format
  2842. _mesa_choose_texture_format(struct gl_context *ctx,
  2843.                             struct gl_texture_object *texObj,
  2844.                             GLenum target, GLint level,
  2845.                             GLenum internalFormat, GLenum format, GLenum type)
  2846. {
  2847.    gl_format f;
  2848.  
  2849.    /* see if we've already chosen a format for the previous level */
  2850.    if (level > 0) {
  2851.       struct gl_texture_image *prevImage =
  2852.          _mesa_select_tex_image(ctx, texObj, target, level - 1);
  2853.       /* See if the prev level is defined and has an internal format which
  2854.        * matches the new internal format.
  2855.        */
  2856.       if (prevImage &&
  2857.           prevImage->Width > 0 &&
  2858.           prevImage->InternalFormat == internalFormat) {
  2859.          /* use the same format */
  2860.          ASSERT(prevImage->TexFormat != MESA_FORMAT_NONE);
  2861.          return prevImage->TexFormat;
  2862.       }
  2863.    }
  2864.  
  2865.    /* If the application requested compression to an S3TC format but we don't
  2866.     * have the DTXn library, force a generic compressed format instead.
  2867.     */
  2868.    if (internalFormat != format && format != GL_NONE) {
  2869.       const GLenum before = internalFormat;
  2870.  
  2871.       switch (internalFormat) {
  2872.       case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  2873.          if (!ctx->Mesa_DXTn)
  2874.             internalFormat = GL_COMPRESSED_RGB;
  2875.          break;
  2876.       case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  2877.       case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  2878.       case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  2879.          if (!ctx->Mesa_DXTn)
  2880.             internalFormat = GL_COMPRESSED_RGBA;
  2881.          break;
  2882.       default:
  2883.          break;
  2884.       }
  2885.  
  2886.       if (before != internalFormat) {
  2887.          _mesa_warning(ctx,
  2888.                        "DXT compression requested (%s), "
  2889.                        "but libtxc_dxtn library not installed.  Using %s "
  2890.                        "instead.",
  2891.                        _mesa_lookup_enum_by_nr(before),
  2892.                        _mesa_lookup_enum_by_nr(internalFormat));
  2893.       }
  2894.    }
  2895.  
  2896.    /* choose format from scratch */
  2897.    f = ctx->Driver.ChooseTextureFormat(ctx, texObj->Target, internalFormat,
  2898.                                        format, type);
  2899.    ASSERT(f != MESA_FORMAT_NONE);
  2900.    return f;
  2901. }
  2902.  
  2903.  
  2904. /**
  2905.  * Adjust pixel unpack params and image dimensions to strip off the
  2906.  * one-pixel texture border.
  2907.  *
  2908.  * Gallium and intel don't support texture borders.  They've seldem been used
  2909.  * and seldom been implemented correctly anyway.
  2910.  *
  2911.  * \param unpackNew returns the new pixel unpack parameters
  2912.  */
  2913. static void
  2914. strip_texture_border(GLenum target,
  2915.                      GLint *width, GLint *height, GLint *depth,
  2916.                      const struct gl_pixelstore_attrib *unpack,
  2917.                      struct gl_pixelstore_attrib *unpackNew)
  2918. {
  2919.    assert(width);
  2920.    assert(height);
  2921.    assert(depth);
  2922.  
  2923.    *unpackNew = *unpack;
  2924.  
  2925.    if (unpackNew->RowLength == 0)
  2926.       unpackNew->RowLength = *width;
  2927.  
  2928.    if (unpackNew->ImageHeight == 0)
  2929.       unpackNew->ImageHeight = *height;
  2930.  
  2931.    assert(*width >= 3);
  2932.    unpackNew->SkipPixels++;  /* skip the border */
  2933.    *width = *width - 2;      /* reduce the width by two border pixels */
  2934.  
  2935.    /* The min height of a texture with a border is 3 */
  2936.    if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
  2937.       unpackNew->SkipRows++;  /* skip the border */
  2938.       *height = *height - 2;  /* reduce the height by two border pixels */
  2939.    }
  2940.  
  2941.    if (*depth >= 3 &&
  2942.        target != GL_TEXTURE_2D_ARRAY &&
  2943.        target != GL_TEXTURE_CUBE_MAP_ARRAY) {
  2944.       unpackNew->SkipImages++;  /* skip the border */
  2945.       *depth = *depth - 2;      /* reduce the depth by two border pixels */
  2946.    }
  2947. }
  2948.  
  2949.  
  2950. /**
  2951.  * Common code to implement all the glTexImage1D/2D/3D functions
  2952.  * as well as glCompressedTexImage1D/2D/3D.
  2953.  * \param compressed  only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
  2954.  * \param format  the user's image format (only used if !compressed)
  2955.  * \param type  the user's image type (only used if !compressed)
  2956.  * \param imageSize  only used for glCompressedTexImage1D/2D/3D calls.
  2957.  */
  2958. static void
  2959. teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
  2960.          GLenum target, GLint level, GLint internalFormat,
  2961.          GLsizei width, GLsizei height, GLsizei depth,
  2962.          GLint border, GLenum format, GLenum type,
  2963.          GLsizei imageSize, const GLvoid *pixels)
  2964. {
  2965.    const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
  2966.    struct gl_pixelstore_attrib unpack_no_border;
  2967.    const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
  2968.    struct gl_texture_object *texObj;
  2969.    gl_format texFormat;
  2970.    GLboolean dimensionsOK, sizeOK;
  2971.  
  2972.    FLUSH_VERTICES(ctx, 0);
  2973.  
  2974.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
  2975.       if (compressed)
  2976.          _mesa_debug(ctx,
  2977.                      "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
  2978.                      dims,
  2979.                      _mesa_lookup_enum_by_nr(target), level,
  2980.                      _mesa_lookup_enum_by_nr(internalFormat),
  2981.                      width, height, depth, border, pixels);
  2982.       else
  2983.          _mesa_debug(ctx,
  2984.                      "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
  2985.                      dims,
  2986.                      _mesa_lookup_enum_by_nr(target), level,
  2987.                      _mesa_lookup_enum_by_nr(internalFormat),
  2988.                      width, height, depth, border,
  2989.                      _mesa_lookup_enum_by_nr(format),
  2990.                      _mesa_lookup_enum_by_nr(type), pixels);
  2991.    }
  2992.  
  2993.    internalFormat = override_internal_format(internalFormat, width, height);
  2994.  
  2995.    /* target error checking */
  2996.    if (!legal_teximage_target(ctx, dims, target)) {
  2997.       _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
  2998.                   func, dims, _mesa_lookup_enum_by_nr(target));
  2999.       return;
  3000.    }
  3001.  
  3002.    /* general error checking */
  3003.    if (compressed) {
  3004.       if (compressed_texture_error_check(ctx, dims, target, level,
  3005.                                          internalFormat,
  3006.                                          width, height, depth,
  3007.                                          border, imageSize))
  3008.          return;
  3009.    }
  3010.    else {
  3011.       if (texture_error_check(ctx, dims, target, level, internalFormat,
  3012.                               format, type, width, height, depth, border))
  3013.          return;
  3014.    }
  3015.  
  3016.    /* Here we convert a cpal compressed image into a regular glTexImage2D
  3017.     * call by decompressing the texture.  If we really want to support cpal
  3018.     * textures in any driver this would have to be changed.
  3019.     */
  3020.    if (ctx->API == API_OPENGLES && compressed && dims == 2) {
  3021.       switch (internalFormat) {
  3022.       case GL_PALETTE4_RGB8_OES:
  3023.       case GL_PALETTE4_RGBA8_OES:
  3024.       case GL_PALETTE4_R5_G6_B5_OES:
  3025.       case GL_PALETTE4_RGBA4_OES:
  3026.       case GL_PALETTE4_RGB5_A1_OES:
  3027.       case GL_PALETTE8_RGB8_OES:
  3028.       case GL_PALETTE8_RGBA8_OES:
  3029.       case GL_PALETTE8_R5_G6_B5_OES:
  3030.       case GL_PALETTE8_RGBA4_OES:
  3031.       case GL_PALETTE8_RGB5_A1_OES:
  3032.          _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
  3033.                                           width, height, imageSize, pixels);
  3034.          return;
  3035.       }
  3036.    }
  3037.  
  3038.    texObj = _mesa_get_current_tex_object(ctx, target);
  3039.    assert(texObj);
  3040.  
  3041.    if (compressed) {
  3042.       /* For glCompressedTexImage() the driver has no choice about the
  3043.        * texture format since we'll never transcode the user's compressed
  3044.        * image data.  The internalFormat was error checked earlier.
  3045.        */
  3046.       texFormat = _mesa_glenum_to_compressed_format(internalFormat);
  3047.    }
  3048.    else {
  3049.       texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
  3050.                                               internalFormat, format, type);
  3051.    }
  3052.  
  3053.    assert(texFormat != MESA_FORMAT_NONE);
  3054.  
  3055.    /* check that width, height, depth are legal for the mipmap level */
  3056.    dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
  3057.                                                  height, depth, border);
  3058.  
  3059.    /* check that the texture won't take too much memory, etc */
  3060.    sizeOK = ctx->Driver.TestProxyTexImage(ctx, _mesa_get_proxy_target(target),
  3061.                                           level, texFormat,
  3062.                                           width, height, depth, border);
  3063.  
  3064.    if (_mesa_is_proxy_texture(target)) {
  3065.       /* Proxy texture: just clear or set state depending on error checking */
  3066.       struct gl_texture_image *texImage =
  3067.          get_proxy_tex_image(ctx, target, level);
  3068.  
  3069.       if (!texImage)
  3070.          return;  /* GL_OUT_OF_MEMORY already recorded */
  3071.  
  3072.       if (dimensionsOK && sizeOK) {
  3073.          _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
  3074.                                     border, internalFormat, texFormat);
  3075.       }
  3076.       else {
  3077.          clear_teximage_fields(texImage);
  3078.       }
  3079.    }
  3080.    else {
  3081.       /* non-proxy target */
  3082.       const GLuint face = _mesa_tex_target_to_face(target);
  3083.       struct gl_texture_image *texImage;
  3084.  
  3085.       if (!dimensionsOK) {
  3086.          _mesa_error(ctx, GL_INVALID_VALUE,
  3087.                      "glTexImage%uD(invalid width or height or depth)",
  3088.                      dims);
  3089.          return;
  3090.       }
  3091.  
  3092.       if (!sizeOK) {
  3093.          _mesa_error(ctx, GL_OUT_OF_MEMORY,
  3094.                      "glTexImage%uD(image too large)", dims);
  3095.          return;
  3096.       }
  3097.  
  3098.       /* Allow a hardware driver to just strip out the border, to provide
  3099.        * reliable but slightly incorrect hardware rendering instead of
  3100.        * rarely-tested software fallback rendering.
  3101.        */
  3102.       if (border && ctx->Const.StripTextureBorder) {
  3103.          strip_texture_border(target, &width, &height, &depth, unpack,
  3104.                               &unpack_no_border);
  3105.          border = 0;
  3106.          unpack = &unpack_no_border;
  3107.       }
  3108.  
  3109.       if (ctx->NewState & _NEW_PIXEL)
  3110.          _mesa_update_state(ctx);
  3111.  
  3112.       _mesa_lock_texture(ctx, texObj);
  3113.       {
  3114.          texImage = _mesa_get_tex_image(ctx, texObj, target, level);
  3115.  
  3116.          if (!texImage) {
  3117.             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
  3118.          }
  3119.          else {
  3120.             ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  3121.  
  3122.             _mesa_init_teximage_fields(ctx, texImage,
  3123.                                        width, height, depth,
  3124.                                        border, internalFormat, texFormat);
  3125.  
  3126.             /* Give the texture to the driver.  <pixels> may be null. */
  3127.             if (width > 0 && height > 0 && depth > 0) {
  3128.                if (compressed) {
  3129.                   ctx->Driver.CompressedTexImage(ctx, dims, texImage,
  3130.                                                  imageSize, pixels);
  3131.                }
  3132.                else {
  3133.                   ctx->Driver.TexImage(ctx, dims, texImage, format,
  3134.                                        type, pixels, unpack);
  3135.                }
  3136.             }
  3137.  
  3138.             check_gen_mipmap(ctx, target, texObj, level);
  3139.  
  3140.             _mesa_update_fbo_texture(ctx, texObj, face, level);
  3141.  
  3142.             _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
  3143.          }
  3144.       }
  3145.       _mesa_unlock_texture(ctx, texObj);
  3146.    }
  3147. }
  3148.  
  3149.  
  3150.  
  3151. /*
  3152.  * Called from the API.  Note that width includes the border.
  3153.  */
  3154. void GLAPIENTRY
  3155. _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
  3156.                   GLsizei width, GLint border, GLenum format,
  3157.                   GLenum type, const GLvoid *pixels )
  3158. {
  3159.    GET_CURRENT_CONTEXT(ctx);
  3160.    teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
  3161.             border, format, type, 0, pixels);
  3162. }
  3163.  
  3164.  
  3165. void GLAPIENTRY
  3166. _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
  3167.                   GLsizei width, GLsizei height, GLint border,
  3168.                   GLenum format, GLenum type,
  3169.                   const GLvoid *pixels )
  3170. {
  3171.    GET_CURRENT_CONTEXT(ctx);
  3172.    teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
  3173.             border, format, type, 0, pixels);
  3174. }
  3175.  
  3176.  
  3177. /*
  3178.  * Called by the API or display list executor.
  3179.  * Note that width and height include the border.
  3180.  */
  3181. void GLAPIENTRY
  3182. _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
  3183.                   GLsizei width, GLsizei height, GLsizei depth,
  3184.                   GLint border, GLenum format, GLenum type,
  3185.                   const GLvoid *pixels )
  3186. {
  3187.    GET_CURRENT_CONTEXT(ctx);
  3188.    teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
  3189.             width, height, depth,
  3190.             border, format, type, 0, pixels);
  3191. }
  3192.  
  3193.  
  3194. void GLAPIENTRY
  3195. _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
  3196.                      GLsizei width, GLsizei height, GLsizei depth,
  3197.                      GLint border, GLenum format, GLenum type,
  3198.                      const GLvoid *pixels )
  3199. {
  3200.    _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
  3201.                     depth, border, format, type, pixels);
  3202. }
  3203.  
  3204.  
  3205. void GLAPIENTRY
  3206. _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
  3207. {
  3208.    struct gl_texture_object *texObj;
  3209.    struct gl_texture_image *texImage;
  3210.    bool valid_target;
  3211.    GET_CURRENT_CONTEXT(ctx);
  3212.    FLUSH_VERTICES(ctx, 0);
  3213.  
  3214.    switch (target) {
  3215.    case GL_TEXTURE_2D:
  3216.       valid_target = ctx->Extensions.OES_EGL_image;
  3217.       break;
  3218.    case GL_TEXTURE_EXTERNAL_OES:
  3219.       valid_target =
  3220.          _mesa_is_gles(ctx) ? ctx->Extensions.OES_EGL_image_external : false;
  3221.       break;
  3222.    default:
  3223.       valid_target = false;
  3224.       break;
  3225.    }
  3226.  
  3227.    if (!valid_target) {
  3228.       _mesa_error(ctx, GL_INVALID_ENUM,
  3229.                   "glEGLImageTargetTexture2D(target=%d)", target);
  3230.       return;
  3231.    }
  3232.  
  3233.    if (!image) {
  3234.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3235.                   "glEGLImageTargetTexture2D(image=%p)", image);
  3236.       return;
  3237.    }
  3238.  
  3239.    if (ctx->NewState & _NEW_PIXEL)
  3240.       _mesa_update_state(ctx);
  3241.  
  3242.    texObj = _mesa_get_current_tex_object(ctx, target);
  3243.    _mesa_lock_texture(ctx, texObj);
  3244.  
  3245.    if (texObj->Immutable) {
  3246.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3247.                   "glEGLImageTargetTexture2D(texture is immutable)");
  3248.       _mesa_unlock_texture(ctx, texObj);
  3249.       return;
  3250.    }
  3251.  
  3252.    texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
  3253.    if (!texImage) {
  3254.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
  3255.    } else {
  3256.       ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  3257.  
  3258.       ctx->Driver.EGLImageTargetTexture2D(ctx, target,
  3259.                                           texObj, texImage, image);
  3260.  
  3261.       _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
  3262.    }
  3263.    _mesa_unlock_texture(ctx, texObj);
  3264.  
  3265. }
  3266.  
  3267.  
  3268.  
  3269. /**
  3270.  * Implement all the glTexSubImage1/2/3D() functions.
  3271.  */
  3272. static void
  3273. texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
  3274.             GLint xoffset, GLint yoffset, GLint zoffset,
  3275.             GLsizei width, GLsizei height, GLsizei depth,
  3276.             GLenum format, GLenum type, const GLvoid *pixels )
  3277. {
  3278.    struct gl_texture_object *texObj;
  3279.    struct gl_texture_image *texImage;
  3280.  
  3281.    FLUSH_VERTICES(ctx, 0);
  3282.  
  3283.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
  3284.       _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
  3285.                   dims,
  3286.                   _mesa_lookup_enum_by_nr(target), level,
  3287.                   xoffset, yoffset, zoffset, width, height, depth,
  3288.                   _mesa_lookup_enum_by_nr(format),
  3289.                   _mesa_lookup_enum_by_nr(type), pixels);
  3290.  
  3291.    /* check target (proxies not allowed) */
  3292.    if (!legal_texsubimage_target(ctx, dims, target)) {
  3293.       _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage%uD(target=%s)",
  3294.                   dims, _mesa_lookup_enum_by_nr(target));
  3295.       return;
  3296.    }
  3297.  
  3298.    if (ctx->NewState & _NEW_PIXEL)
  3299.       _mesa_update_state(ctx);
  3300.  
  3301.    if (texsubimage_error_check(ctx, dims, target, level,
  3302.                                xoffset, yoffset, zoffset,
  3303.                                width, height, depth, format, type)) {
  3304.       return;   /* error was detected */
  3305.    }
  3306.  
  3307.    texObj = _mesa_get_current_tex_object(ctx, target);
  3308.  
  3309.    _mesa_lock_texture(ctx, texObj);
  3310.    {
  3311.       texImage = _mesa_select_tex_image(ctx, texObj, target, level);
  3312.  
  3313.       if (width > 0 && height > 0 && depth > 0) {
  3314.          /* If we have a border, offset=-1 is legal.  Bias by border width. */
  3315.          switch (dims) {
  3316.          case 3:
  3317.             if (target != GL_TEXTURE_2D_ARRAY)
  3318.                zoffset += texImage->Border;
  3319.             /* fall-through */
  3320.          case 2:
  3321.             if (target != GL_TEXTURE_1D_ARRAY)
  3322.                yoffset += texImage->Border;
  3323.             /* fall-through */
  3324.          case 1:
  3325.             xoffset += texImage->Border;
  3326.          }
  3327.  
  3328.          ctx->Driver.TexSubImage(ctx, dims, texImage,
  3329.                                  xoffset, yoffset, zoffset,
  3330.                                  width, height, depth,
  3331.                                  format, type, pixels, &ctx->Unpack);
  3332.  
  3333.          check_gen_mipmap(ctx, target, texObj, level);
  3334.  
  3335.          ctx->NewState |= _NEW_TEXTURE;
  3336.       }
  3337.    }
  3338.    _mesa_unlock_texture(ctx, texObj);
  3339. }
  3340.  
  3341.  
  3342. void GLAPIENTRY
  3343. _mesa_TexSubImage1D( GLenum target, GLint level,
  3344.                      GLint xoffset, GLsizei width,
  3345.                      GLenum format, GLenum type,
  3346.                      const GLvoid *pixels )
  3347. {
  3348.    GET_CURRENT_CONTEXT(ctx);
  3349.    texsubimage(ctx, 1, target, level,
  3350.                xoffset, 0, 0,
  3351.                width, 1, 1,
  3352.                format, type, pixels);
  3353. }
  3354.  
  3355.  
  3356. void GLAPIENTRY
  3357. _mesa_TexSubImage2D( GLenum target, GLint level,
  3358.                      GLint xoffset, GLint yoffset,
  3359.                      GLsizei width, GLsizei height,
  3360.                      GLenum format, GLenum type,
  3361.                      const GLvoid *pixels )
  3362. {
  3363.    GET_CURRENT_CONTEXT(ctx);
  3364.    texsubimage(ctx, 2, target, level,
  3365.                xoffset, yoffset, 0,
  3366.                width, height, 1,
  3367.                format, type, pixels);
  3368. }
  3369.  
  3370.  
  3371.  
  3372. void GLAPIENTRY
  3373. _mesa_TexSubImage3D( GLenum target, GLint level,
  3374.                      GLint xoffset, GLint yoffset, GLint zoffset,
  3375.                      GLsizei width, GLsizei height, GLsizei depth,
  3376.                      GLenum format, GLenum type,
  3377.                      const GLvoid *pixels )
  3378. {
  3379.    GET_CURRENT_CONTEXT(ctx);
  3380.    texsubimage(ctx, 3, target, level,
  3381.                xoffset, yoffset, zoffset,
  3382.                width, height, depth,
  3383.                format, type, pixels);
  3384. }
  3385.  
  3386.  
  3387.  
  3388. /**
  3389.  * For glCopyTexSubImage, return the source renderbuffer to copy texel data
  3390.  * from.  This depends on whether the texture contains color or depth values.
  3391.  */
  3392. static struct gl_renderbuffer *
  3393. get_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
  3394. {
  3395.    if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
  3396.       /* reading from depth/stencil buffer */
  3397.       return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
  3398.    }
  3399.    else {
  3400.       /* copying from color buffer */
  3401.       return ctx->ReadBuffer->_ColorReadBuffer;
  3402.    }
  3403. }
  3404.  
  3405. static void
  3406. copytexsubimage_by_slice(struct gl_context *ctx,
  3407.                          struct gl_texture_image *texImage,
  3408.                          GLuint dims,
  3409.                          GLint xoffset, GLint yoffset, GLint zoffset,
  3410.                          struct gl_renderbuffer *rb,
  3411.                          GLint x, GLint y,
  3412.                          GLsizei width, GLsizei height)
  3413. {
  3414.    if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
  3415.       int slice;
  3416.  
  3417.       /* For 1D arrays, we copy each scanline of the source rectangle into the
  3418.        * next array slice.
  3419.        */
  3420.       assert(zoffset == 0);
  3421.  
  3422.       for (slice = 0; slice < height; slice++) {
  3423.          assert(yoffset + slice < texImage->Height);
  3424.          ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
  3425.                                      xoffset, 0, yoffset + slice,
  3426.                                      rb, x, y + slice, width, 1);
  3427.       }
  3428.    } else {
  3429.       ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
  3430.                                   xoffset, yoffset, zoffset,
  3431.                                   rb, x, y, width, height);
  3432.    }
  3433. }
  3434.  
  3435.  
  3436. /**
  3437.  * Implement the glCopyTexImage1/2D() functions.
  3438.  */
  3439. static void
  3440. copyteximage(struct gl_context *ctx, GLuint dims,
  3441.              GLenum target, GLint level, GLenum internalFormat,
  3442.              GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
  3443. {
  3444.    struct gl_texture_object *texObj;
  3445.    struct gl_texture_image *texImage;
  3446.    const GLuint face = _mesa_tex_target_to_face(target);
  3447.    gl_format texFormat;
  3448.  
  3449.    FLUSH_VERTICES(ctx, 0);
  3450.  
  3451.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
  3452.       _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
  3453.                   dims,
  3454.                   _mesa_lookup_enum_by_nr(target), level,
  3455.                   _mesa_lookup_enum_by_nr(internalFormat),
  3456.                   x, y, width, height, border);
  3457.  
  3458.    if (ctx->NewState & NEW_COPY_TEX_STATE)
  3459.       _mesa_update_state(ctx);
  3460.  
  3461.    if (copytexture_error_check(ctx, dims, target, level, internalFormat,
  3462.                                width, height, border))
  3463.       return;
  3464.  
  3465.    if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
  3466.                                        1, border)) {
  3467.       _mesa_error(ctx, GL_INVALID_VALUE,
  3468.                   "glCopyTexImage%uD(invalid width or height)", dims);
  3469.       return;
  3470.    }
  3471.  
  3472.    texObj = _mesa_get_current_tex_object(ctx, target);
  3473.    assert(texObj);
  3474.  
  3475.    texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
  3476.                                            internalFormat, GL_NONE, GL_NONE);
  3477.    assert(texFormat != MESA_FORMAT_NONE);
  3478.  
  3479.    if (!ctx->Driver.TestProxyTexImage(ctx, _mesa_get_proxy_target(target),
  3480.                                       level, texFormat,
  3481.                                       width, height, 1, border)) {
  3482.       _mesa_error(ctx, GL_OUT_OF_MEMORY,
  3483.                   "glCopyTexImage%uD(image too large)", dims);
  3484.       return;
  3485.    }
  3486.  
  3487.    if (border && ctx->Const.StripTextureBorder) {
  3488.       x += border;
  3489.       width -= border * 2;
  3490.       if (dims == 2) {
  3491.          y += border;
  3492.          height -= border * 2;
  3493.       }
  3494.       border = 0;
  3495.    }
  3496.  
  3497.    _mesa_lock_texture(ctx, texObj);
  3498.    {
  3499.       texImage = _mesa_get_tex_image(ctx, texObj, target, level);
  3500.  
  3501.       if (!texImage) {
  3502.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
  3503.       }
  3504.       else {
  3505.          GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
  3506.  
  3507.          /* Free old texture image */
  3508.          ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  3509.  
  3510.          _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
  3511.                                     border, internalFormat, texFormat);
  3512.  
  3513.          if (width && height) {
  3514.             /* Allocate texture memory (no pixel data yet) */
  3515.             ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
  3516.  
  3517.             if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
  3518.                                            &width, &height)) {
  3519.                struct gl_renderbuffer *srcRb =
  3520.                   get_copy_tex_image_source(ctx, texImage->TexFormat);
  3521.  
  3522.                copytexsubimage_by_slice(ctx, texImage, dims,
  3523.                                         dstX, dstY, dstZ,
  3524.                                         srcRb, srcX, srcY, width, height);
  3525.             }
  3526.  
  3527.             check_gen_mipmap(ctx, target, texObj, level);
  3528.          }
  3529.  
  3530.          _mesa_update_fbo_texture(ctx, texObj, face, level);
  3531.  
  3532.          _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
  3533.       }
  3534.    }
  3535.    _mesa_unlock_texture(ctx, texObj);
  3536. }
  3537.  
  3538.  
  3539.  
  3540. void GLAPIENTRY
  3541. _mesa_CopyTexImage1D( GLenum target, GLint level,
  3542.                       GLenum internalFormat,
  3543.                       GLint x, GLint y,
  3544.                       GLsizei width, GLint border )
  3545. {
  3546.    GET_CURRENT_CONTEXT(ctx);
  3547.    copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
  3548. }
  3549.  
  3550.  
  3551.  
  3552. void GLAPIENTRY
  3553. _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
  3554.                       GLint x, GLint y, GLsizei width, GLsizei height,
  3555.                       GLint border )
  3556. {
  3557.    GET_CURRENT_CONTEXT(ctx);
  3558.    copyteximage(ctx, 2, target, level, internalFormat,
  3559.                 x, y, width, height, border);
  3560. }
  3561.  
  3562.  
  3563.  
  3564. /**
  3565.  * Implementation for glCopyTexSubImage1/2/3D() functions.
  3566.  */
  3567. static void
  3568. copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
  3569.                 GLint xoffset, GLint yoffset, GLint zoffset,
  3570.                 GLint x, GLint y, GLsizei width, GLsizei height)
  3571. {
  3572.    struct gl_texture_object *texObj;
  3573.    struct gl_texture_image *texImage;
  3574.  
  3575.    FLUSH_VERTICES(ctx, 0);
  3576.  
  3577.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
  3578.       _mesa_debug(ctx, "glCopyTexSubImage%uD %s %d %d %d %d %d %d %d %d\n",
  3579.                   dims,
  3580.                   _mesa_lookup_enum_by_nr(target),
  3581.                   level, xoffset, yoffset, zoffset, x, y, width, height);
  3582.  
  3583.    if (ctx->NewState & NEW_COPY_TEX_STATE)
  3584.       _mesa_update_state(ctx);
  3585.  
  3586.    if (copytexsubimage_error_check(ctx, dims, target, level,
  3587.                                    xoffset, yoffset, zoffset, width, height)) {
  3588.       return;
  3589.    }
  3590.  
  3591.    texObj = _mesa_get_current_tex_object(ctx, target);
  3592.  
  3593.    _mesa_lock_texture(ctx, texObj);
  3594.    {
  3595.       texImage = _mesa_select_tex_image(ctx, texObj, target, level);
  3596.  
  3597.       /* If we have a border, offset=-1 is legal.  Bias by border width. */
  3598.       switch (dims) {
  3599.       case 3:
  3600.          if (target != GL_TEXTURE_2D_ARRAY)
  3601.             zoffset += texImage->Border;
  3602.          /* fall-through */
  3603.       case 2:
  3604.          if (target != GL_TEXTURE_1D_ARRAY)
  3605.             yoffset += texImage->Border;
  3606.          /* fall-through */
  3607.       case 1:
  3608.          xoffset += texImage->Border;
  3609.       }
  3610.  
  3611.       if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
  3612.                                      &width, &height)) {
  3613.          struct gl_renderbuffer *srcRb =
  3614.             get_copy_tex_image_source(ctx, texImage->TexFormat);
  3615.  
  3616.          copytexsubimage_by_slice(ctx, texImage, dims,
  3617.                                   xoffset, yoffset, zoffset,
  3618.                                   srcRb, x, y, width, height);
  3619.  
  3620.          check_gen_mipmap(ctx, target, texObj, level);
  3621.  
  3622.          ctx->NewState |= _NEW_TEXTURE;
  3623.       }
  3624.    }
  3625.    _mesa_unlock_texture(ctx, texObj);
  3626. }
  3627.  
  3628.  
  3629. void GLAPIENTRY
  3630. _mesa_CopyTexSubImage1D( GLenum target, GLint level,
  3631.                          GLint xoffset, GLint x, GLint y, GLsizei width )
  3632. {
  3633.    GET_CURRENT_CONTEXT(ctx);
  3634.    copytexsubimage(ctx, 1, target, level, xoffset, 0, 0, x, y, width, 1);
  3635. }
  3636.  
  3637.  
  3638.  
  3639. void GLAPIENTRY
  3640. _mesa_CopyTexSubImage2D( GLenum target, GLint level,
  3641.                          GLint xoffset, GLint yoffset,
  3642.                          GLint x, GLint y, GLsizei width, GLsizei height )
  3643. {
  3644.    GET_CURRENT_CONTEXT(ctx);
  3645.    copytexsubimage(ctx, 2, target, level, xoffset, yoffset, 0, x, y,
  3646.                    width, height);
  3647. }
  3648.  
  3649.  
  3650.  
  3651. void GLAPIENTRY
  3652. _mesa_CopyTexSubImage3D( GLenum target, GLint level,
  3653.                          GLint xoffset, GLint yoffset, GLint zoffset,
  3654.                          GLint x, GLint y, GLsizei width, GLsizei height )
  3655. {
  3656.    GET_CURRENT_CONTEXT(ctx);
  3657.    copytexsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
  3658.                    x, y, width, height);
  3659. }
  3660.  
  3661.  
  3662.  
  3663.  
  3664. /**********************************************************************/
  3665. /******                   Compressed Textures                    ******/
  3666. /**********************************************************************/
  3667.  
  3668.  
  3669. /**
  3670.  * Error checking for glCompressedTexSubImage[123]D().
  3671.  * \return error code or GL_NO_ERROR.
  3672.  */
  3673. static GLenum
  3674. compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
  3675.                                   GLenum target, GLint level,
  3676.                                   GLint xoffset, GLint yoffset, GLint zoffset,
  3677.                                   GLsizei width, GLsizei height, GLsizei depth,
  3678.                                   GLenum format, GLsizei imageSize)
  3679. {
  3680.    struct gl_texture_object *texObj;
  3681.    struct gl_texture_image *texImage;
  3682.    GLint expectedSize;
  3683.    GLboolean targetOK;
  3684.  
  3685.    switch (dims) {
  3686.    case 2:
  3687.       switch (target) {
  3688.       case GL_TEXTURE_2D:
  3689.       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  3690.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  3691.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  3692.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  3693.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  3694.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  3695.          targetOK = GL_TRUE;
  3696.          break;
  3697.       default:
  3698.          targetOK = GL_FALSE;
  3699.          break;
  3700.       }
  3701.       break;
  3702.    case 3:
  3703.       targetOK = (target == GL_TEXTURE_2D_ARRAY);
  3704.       break;
  3705.    default:
  3706.       assert(dims == 1);
  3707.       /* no 1D compressed textures at this time */
  3708.       targetOK = GL_FALSE;
  3709.       break;
  3710.    }
  3711.  
  3712.    if (!targetOK) {
  3713.       _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(target)",
  3714.                   dims);
  3715.       return GL_TRUE;
  3716.    }
  3717.  
  3718.    /* this will catch any invalid compressed format token */
  3719.    if (!_mesa_is_compressed_format(ctx, format)) {
  3720.       _mesa_error(ctx, GL_INVALID_ENUM, "glCompressedTexImage%uD(format)",
  3721.                   dims);
  3722.       return GL_TRUE;
  3723.    }
  3724.  
  3725.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  3726.       _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexImage%uD(level=%d)",
  3727.                   dims, level);
  3728.       return GL_TRUE;
  3729.    }
  3730.  
  3731.    expectedSize = compressed_tex_size(width, height, depth, format);
  3732.    if (expectedSize != imageSize) {
  3733.       _mesa_error(ctx, GL_INVALID_VALUE, "glCompressedTexImage%uD(size=%d)",
  3734.                   dims, imageSize);
  3735.       return GL_TRUE;
  3736.    }
  3737.  
  3738.    texObj = _mesa_get_current_tex_object(ctx, target);
  3739.    if (!texObj) {
  3740.       _mesa_error(ctx, GL_OUT_OF_MEMORY,
  3741.                   "glCompressedTexSubImage%uD()", dims);
  3742.       return GL_TRUE;
  3743.    }
  3744.  
  3745.    texImage = _mesa_select_tex_image(ctx, texObj, target, level);
  3746.    if (!texImage) {
  3747.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3748.                   "glCompressedTexSubImage%uD(invalid texture image)", dims);
  3749.       return GL_TRUE;
  3750.    }
  3751.  
  3752.    if ((GLint) format != texImage->InternalFormat) {
  3753.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3754.                   "glCompressedTexSubImage%uD(format=0x%x)", dims, format);
  3755.       return GL_TRUE;
  3756.    }
  3757.  
  3758.    if (compressedteximage_only_format(ctx, format)) {
  3759.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3760.                   "glCompressedTexSubImage%uD(format=0x%x cannot be updated)"
  3761.                   , dims, format);
  3762.       return GL_TRUE;
  3763.    }
  3764.  
  3765.    if (error_check_subtexture_dimensions(ctx, "glCompressedTexSubImage", dims,
  3766.                                          texImage, xoffset, yoffset, zoffset,
  3767.                                          width, height, depth)) {
  3768.       return GL_TRUE;
  3769.    }
  3770.  
  3771.    return GL_FALSE;
  3772. }
  3773.  
  3774.  
  3775. void GLAPIENTRY
  3776. _mesa_CompressedTexImage1D(GLenum target, GLint level,
  3777.                               GLenum internalFormat, GLsizei width,
  3778.                               GLint border, GLsizei imageSize,
  3779.                               const GLvoid *data)
  3780. {
  3781.    GET_CURRENT_CONTEXT(ctx);
  3782.    teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
  3783.             width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
  3784. }
  3785.  
  3786.  
  3787. void GLAPIENTRY
  3788. _mesa_CompressedTexImage2D(GLenum target, GLint level,
  3789.                               GLenum internalFormat, GLsizei width,
  3790.                               GLsizei height, GLint border, GLsizei imageSize,
  3791.                               const GLvoid *data)
  3792. {
  3793.    GET_CURRENT_CONTEXT(ctx);
  3794.    teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
  3795.             width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
  3796. }
  3797.  
  3798.  
  3799. void GLAPIENTRY
  3800. _mesa_CompressedTexImage3D(GLenum target, GLint level,
  3801.                               GLenum internalFormat, GLsizei width,
  3802.                               GLsizei height, GLsizei depth, GLint border,
  3803.                               GLsizei imageSize, const GLvoid *data)
  3804. {
  3805.    GET_CURRENT_CONTEXT(ctx);
  3806.    teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
  3807.             width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
  3808. }
  3809.  
  3810.  
  3811. /**
  3812.  * Common helper for glCompressedTexSubImage1/2/3D().
  3813.  */
  3814. static void
  3815. compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
  3816.                          GLint xoffset, GLint yoffset, GLint zoffset,
  3817.                          GLsizei width, GLsizei height, GLsizei depth,
  3818.                          GLenum format, GLsizei imageSize, const GLvoid *data)
  3819. {
  3820.    struct gl_texture_object *texObj;
  3821.    struct gl_texture_image *texImage;
  3822.    GET_CURRENT_CONTEXT(ctx);
  3823.    FLUSH_VERTICES(ctx, 0);
  3824.  
  3825.    if (compressed_subtexture_error_check(ctx, dims, target, level,
  3826.                                          xoffset, yoffset, zoffset,
  3827.                                          width, height, depth,
  3828.                                          format, imageSize)) {
  3829.       return;
  3830.    }
  3831.  
  3832.    texObj = _mesa_get_current_tex_object(ctx, target);
  3833.  
  3834.    _mesa_lock_texture(ctx, texObj);
  3835.    {
  3836.       texImage = _mesa_select_tex_image(ctx, texObj, target, level);
  3837.       assert(texImage);
  3838.  
  3839.       if (width > 0 && height > 0 && depth > 0) {
  3840.          ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
  3841.                                            xoffset, yoffset, zoffset,
  3842.                                            width, height, depth,
  3843.                                            format, imageSize, data);
  3844.  
  3845.          check_gen_mipmap(ctx, target, texObj, level);
  3846.  
  3847.          ctx->NewState |= _NEW_TEXTURE;
  3848.       }
  3849.    }
  3850.    _mesa_unlock_texture(ctx, texObj);
  3851. }
  3852.  
  3853.  
  3854. void GLAPIENTRY
  3855. _mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
  3856.                                  GLsizei width, GLenum format,
  3857.                                  GLsizei imageSize, const GLvoid *data)
  3858. {
  3859.    compressed_tex_sub_image(1, target, level, xoffset, 0, 0, width, 1, 1,
  3860.                             format, imageSize, data);
  3861. }
  3862.  
  3863.  
  3864. void GLAPIENTRY
  3865. _mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
  3866.                                  GLint yoffset, GLsizei width, GLsizei height,
  3867.                                  GLenum format, GLsizei imageSize,
  3868.                                  const GLvoid *data)
  3869. {
  3870.    compressed_tex_sub_image(2, target, level, xoffset, yoffset, 0,
  3871.                             width, height, 1, format, imageSize, data);
  3872. }
  3873.  
  3874.  
  3875. void GLAPIENTRY
  3876. _mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
  3877.                                  GLint yoffset, GLint zoffset, GLsizei width,
  3878.                                  GLsizei height, GLsizei depth, GLenum format,
  3879.                                  GLsizei imageSize, const GLvoid *data)
  3880. {
  3881.    compressed_tex_sub_image(3, target, level, xoffset, yoffset, zoffset,
  3882.                             width, height, depth, format, imageSize, data);
  3883. }
  3884.  
  3885. static gl_format
  3886. get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
  3887. {
  3888.    switch (internalFormat) {
  3889.    case GL_ALPHA8:
  3890.       return MESA_FORMAT_A8;
  3891.    case GL_ALPHA16:
  3892.       return MESA_FORMAT_A16;
  3893.    case GL_ALPHA16F_ARB:
  3894.       return MESA_FORMAT_ALPHA_FLOAT16;
  3895.    case GL_ALPHA32F_ARB:
  3896.       return MESA_FORMAT_ALPHA_FLOAT32;
  3897.    case GL_ALPHA8I_EXT:
  3898.       return MESA_FORMAT_ALPHA_INT8;
  3899.    case GL_ALPHA16I_EXT:
  3900.       return MESA_FORMAT_ALPHA_INT16;
  3901.    case GL_ALPHA32I_EXT:
  3902.       return MESA_FORMAT_ALPHA_INT32;
  3903.    case GL_ALPHA8UI_EXT:
  3904.       return MESA_FORMAT_ALPHA_UINT8;
  3905.    case GL_ALPHA16UI_EXT:
  3906.       return MESA_FORMAT_ALPHA_UINT16;
  3907.    case GL_ALPHA32UI_EXT:
  3908.       return MESA_FORMAT_ALPHA_UINT32;
  3909.    case GL_LUMINANCE8:
  3910.       return MESA_FORMAT_L8;
  3911.    case GL_LUMINANCE16:
  3912.       return MESA_FORMAT_L16;
  3913.    case GL_LUMINANCE16F_ARB:
  3914.       return MESA_FORMAT_LUMINANCE_FLOAT16;
  3915.    case GL_LUMINANCE32F_ARB:
  3916.       return MESA_FORMAT_LUMINANCE_FLOAT32;
  3917.    case GL_LUMINANCE8I_EXT:
  3918.       return MESA_FORMAT_LUMINANCE_INT8;
  3919.    case GL_LUMINANCE16I_EXT:
  3920.       return MESA_FORMAT_LUMINANCE_INT16;
  3921.    case GL_LUMINANCE32I_EXT:
  3922.       return MESA_FORMAT_LUMINANCE_INT32;
  3923.    case GL_LUMINANCE8UI_EXT:
  3924.       return MESA_FORMAT_LUMINANCE_UINT8;
  3925.    case GL_LUMINANCE16UI_EXT:
  3926.       return MESA_FORMAT_LUMINANCE_UINT16;
  3927.    case GL_LUMINANCE32UI_EXT:
  3928.       return MESA_FORMAT_LUMINANCE_UINT32;
  3929.    case GL_LUMINANCE8_ALPHA8:
  3930.       return MESA_FORMAT_AL88;
  3931.    case GL_LUMINANCE16_ALPHA16:
  3932.       return MESA_FORMAT_AL1616;
  3933.    case GL_LUMINANCE_ALPHA16F_ARB:
  3934.       return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16;
  3935.    case GL_LUMINANCE_ALPHA32F_ARB:
  3936.       return MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32;
  3937.    case GL_LUMINANCE_ALPHA8I_EXT:
  3938.       return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
  3939.    case GL_LUMINANCE_ALPHA16I_EXT:
  3940.       return MESA_FORMAT_LUMINANCE_ALPHA_INT8;
  3941.    case GL_LUMINANCE_ALPHA32I_EXT:
  3942.       return MESA_FORMAT_LUMINANCE_ALPHA_INT16;
  3943.    case GL_LUMINANCE_ALPHA8UI_EXT:
  3944.       return MESA_FORMAT_LUMINANCE_ALPHA_UINT8;
  3945.    case GL_LUMINANCE_ALPHA16UI_EXT:
  3946.       return MESA_FORMAT_LUMINANCE_ALPHA_UINT16;
  3947.    case GL_LUMINANCE_ALPHA32UI_EXT:
  3948.       return MESA_FORMAT_LUMINANCE_ALPHA_UINT32;
  3949.    case GL_INTENSITY8:
  3950.       return MESA_FORMAT_I8;
  3951.    case GL_INTENSITY16:
  3952.       return MESA_FORMAT_I16;
  3953.    case GL_INTENSITY16F_ARB:
  3954.       return MESA_FORMAT_INTENSITY_FLOAT16;
  3955.    case GL_INTENSITY32F_ARB:
  3956.       return MESA_FORMAT_INTENSITY_FLOAT32;
  3957.    case GL_INTENSITY8I_EXT:
  3958.       return MESA_FORMAT_INTENSITY_INT8;
  3959.    case GL_INTENSITY16I_EXT:
  3960.       return MESA_FORMAT_INTENSITY_INT16;
  3961.    case GL_INTENSITY32I_EXT:
  3962.       return MESA_FORMAT_INTENSITY_INT32;
  3963.    case GL_INTENSITY8UI_EXT:
  3964.       return MESA_FORMAT_INTENSITY_UINT8;
  3965.    case GL_INTENSITY16UI_EXT:
  3966.       return MESA_FORMAT_INTENSITY_UINT16;
  3967.    case GL_INTENSITY32UI_EXT:
  3968.       return MESA_FORMAT_INTENSITY_UINT32;
  3969.    case GL_RGBA8:
  3970.       return MESA_FORMAT_RGBA8888_REV;
  3971.    case GL_RGBA16:
  3972.       return MESA_FORMAT_RGBA_16;
  3973.    case GL_RGBA16F_ARB:
  3974.       return MESA_FORMAT_RGBA_FLOAT16;
  3975.    case GL_RGBA32F_ARB:
  3976.       return MESA_FORMAT_RGBA_FLOAT32;
  3977.    case GL_RGBA8I_EXT:
  3978.       return MESA_FORMAT_RGBA_INT8;
  3979.    case GL_RGBA16I_EXT:
  3980.       return MESA_FORMAT_RGBA_INT16;
  3981.    case GL_RGBA32I_EXT:
  3982.       return MESA_FORMAT_RGBA_INT32;
  3983.    case GL_RGBA8UI_EXT:
  3984.       return MESA_FORMAT_RGBA_UINT8;
  3985.    case GL_RGBA16UI_EXT:
  3986.       return MESA_FORMAT_RGBA_UINT16;
  3987.    case GL_RGBA32UI_EXT:
  3988.       return MESA_FORMAT_RGBA_UINT32;
  3989.  
  3990.    case GL_RG8:
  3991.       return MESA_FORMAT_GR88;
  3992.    case GL_RG16:
  3993.       return MESA_FORMAT_GR1616;
  3994.    case GL_RG16F:
  3995.       return MESA_FORMAT_RG_FLOAT16;
  3996.    case GL_RG32F:
  3997.       return MESA_FORMAT_RG_FLOAT32;
  3998.    case GL_RG8I:
  3999.       return MESA_FORMAT_RG_INT8;
  4000.    case GL_RG16I:
  4001.       return MESA_FORMAT_RG_INT16;
  4002.    case GL_RG32I:
  4003.       return MESA_FORMAT_RG_INT32;
  4004.    case GL_RG8UI:
  4005.       return MESA_FORMAT_RG_UINT8;
  4006.    case GL_RG16UI:
  4007.       return MESA_FORMAT_RG_UINT16;
  4008.    case GL_RG32UI:
  4009.       return MESA_FORMAT_RG_UINT32;
  4010.  
  4011.    case GL_R8:
  4012.       return MESA_FORMAT_R8;
  4013.    case GL_R16:
  4014.       return MESA_FORMAT_R16;
  4015.    case GL_R16F:
  4016.       return MESA_FORMAT_R_FLOAT16;
  4017.    case GL_R32F:
  4018.       return MESA_FORMAT_R_FLOAT32;
  4019.    case GL_R8I:
  4020.       return MESA_FORMAT_R_INT8;
  4021.    case GL_R16I:
  4022.       return MESA_FORMAT_R_INT16;
  4023.    case GL_R32I:
  4024.       return MESA_FORMAT_R_INT32;
  4025.    case GL_R8UI:
  4026.       return MESA_FORMAT_R_UINT8;
  4027.    case GL_R16UI:
  4028.       return MESA_FORMAT_R_UINT16;
  4029.    case GL_R32UI:
  4030.       return MESA_FORMAT_R_UINT32;
  4031.  
  4032.    case GL_RGB32F:
  4033.       return MESA_FORMAT_RGB_FLOAT32;
  4034.    case GL_RGB32UI:
  4035.       return MESA_FORMAT_RGB_UINT32;
  4036.    case GL_RGB32I:
  4037.       return MESA_FORMAT_RGB_INT32;
  4038.  
  4039.    default:
  4040.       return MESA_FORMAT_NONE;
  4041.    }
  4042. }
  4043.  
  4044.  
  4045. static gl_format
  4046. validate_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
  4047. {
  4048.    gl_format format = get_texbuffer_format(ctx, internalFormat);
  4049.    GLenum datatype;
  4050.  
  4051.    if (format == MESA_FORMAT_NONE)
  4052.       return MESA_FORMAT_NONE;
  4053.  
  4054.    datatype = _mesa_get_format_datatype(format);
  4055.    if (datatype == GL_FLOAT && !ctx->Extensions.ARB_texture_float)
  4056.       return MESA_FORMAT_NONE;
  4057.  
  4058.    if (datatype == GL_HALF_FLOAT && !ctx->Extensions.ARB_half_float_pixel)
  4059.       return MESA_FORMAT_NONE;
  4060.  
  4061.    /* The GL_ARB_texture_rg and GL_ARB_texture_buffer_object specs don't make
  4062.     * any mention of R/RG formats, but they appear in the GL 3.1 core
  4063.     * specification.
  4064.     */
  4065.    if (ctx->Version <= 30) {
  4066.       GLenum base_format = _mesa_get_format_base_format(format);
  4067.  
  4068.       if (base_format == GL_R || base_format == GL_RG)
  4069.          return MESA_FORMAT_NONE;
  4070.    }
  4071.  
  4072.    if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
  4073.       GLenum base_format = _mesa_get_format_base_format(format);
  4074.       if (base_format == GL_RGB)
  4075.          return MESA_FORMAT_NONE;
  4076.    }
  4077.    return format;
  4078. }
  4079.  
  4080.  
  4081. static void
  4082. texbufferrange(struct gl_context *ctx, GLenum target, GLenum internalFormat,
  4083.                struct gl_buffer_object *bufObj,
  4084.                GLintptr offset, GLsizeiptr size)
  4085. {
  4086.    struct gl_texture_object *texObj;
  4087.    gl_format format;
  4088.  
  4089.    FLUSH_VERTICES(ctx, 0);
  4090.  
  4091.    if (target != GL_TEXTURE_BUFFER_ARB) {
  4092.       _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(target)");
  4093.       return;
  4094.    }
  4095.  
  4096.    format = validate_texbuffer_format(ctx, internalFormat);
  4097.    if (format == MESA_FORMAT_NONE) {
  4098.       _mesa_error(ctx, GL_INVALID_ENUM, "glTexBuffer(internalFormat 0x%x)",
  4099.                   internalFormat);
  4100.       return;
  4101.    }
  4102.  
  4103.    texObj = _mesa_get_current_tex_object(ctx, target);
  4104.  
  4105.    _mesa_lock_texture(ctx, texObj);
  4106.    {
  4107.       _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
  4108.       texObj->BufferObjectFormat = internalFormat;
  4109.       texObj->_BufferObjectFormat = format;
  4110.       texObj->BufferOffset = offset;
  4111.       texObj->BufferSize = size;
  4112.    }
  4113.    _mesa_unlock_texture(ctx, texObj);
  4114. }
  4115.  
  4116.  
  4117. /** GL_ARB_texture_buffer_object */
  4118. void GLAPIENTRY
  4119. _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
  4120. {
  4121.    struct gl_buffer_object *bufObj;
  4122.  
  4123.    GET_CURRENT_CONTEXT(ctx);
  4124.  
  4125.    /* NOTE: ARB_texture_buffer_object has interactions with
  4126.     * the compatibility profile that are not implemented.
  4127.     */
  4128.    if (!(ctx->API == API_OPENGL_CORE &&
  4129.          ctx->Extensions.ARB_texture_buffer_object)) {
  4130.       _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer");
  4131.       return;
  4132.    }
  4133.  
  4134.    bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  4135.    if (!bufObj && buffer) {
  4136.       _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBuffer(buffer %u)", buffer);
  4137.       return;
  4138.    }
  4139.  
  4140.    texbufferrange(ctx, target, internalFormat, bufObj, 0, buffer ? -1 : 0);
  4141. }
  4142.  
  4143.  
  4144. /** GL_ARB_texture_buffer_range */
  4145. void GLAPIENTRY
  4146. _mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
  4147.                      GLintptr offset, GLsizeiptr size)
  4148. {
  4149.    struct gl_buffer_object *bufObj;
  4150.  
  4151.    GET_CURRENT_CONTEXT(ctx);
  4152.  
  4153.    if (!(ctx->API == API_OPENGL_CORE &&
  4154.          ctx->Extensions.ARB_texture_buffer_range)) {
  4155.       _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBufferRange");
  4156.       return;
  4157.    }
  4158.  
  4159.    bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  4160.    if (bufObj) {
  4161.       if (offset < 0 ||
  4162.           size <= 0 ||
  4163.           (offset + size) > bufObj->Size) {
  4164.          _mesa_error(ctx, GL_INVALID_VALUE, "glTexBufferRange");
  4165.          return;
  4166.       }
  4167.       if (offset % ctx->Const.TextureBufferOffsetAlignment) {
  4168.          _mesa_error(ctx, GL_INVALID_VALUE,
  4169.                      "glTexBufferRange(invalid offset alignment)");
  4170.          return;
  4171.       }
  4172.    } else if (buffer) {
  4173.       _mesa_error(ctx, GL_INVALID_OPERATION, "glTexBufferRange(buffer %u)",
  4174.                   buffer);
  4175.       return;
  4176.    } else {
  4177.       offset = 0;
  4178.       size = 0;
  4179.    }
  4180.  
  4181.    texbufferrange(ctx, target, internalFormat, bufObj, offset, size);
  4182. }
  4183.  
  4184.  
  4185. static GLboolean
  4186. is_renderable_texture_format(struct gl_context *ctx, GLenum internalformat)
  4187. {
  4188.    /* Everything that is allowed for renderbuffers,
  4189.     * except for a base format of GL_STENCIL_INDEX.
  4190.     */
  4191.    GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
  4192.    return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
  4193. }
  4194.  
  4195.  
  4196. /** GL_ARB_texture_multisample */
  4197. static GLboolean
  4198. check_multisample_target(GLuint dims, GLenum target)
  4199. {
  4200.    switch(target) {
  4201.    case GL_TEXTURE_2D_MULTISAMPLE:
  4202.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  4203.       return dims == 2;
  4204.  
  4205.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  4206.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  4207.       return dims == 3;
  4208.  
  4209.    default:
  4210.       return GL_FALSE;
  4211.    }
  4212. }
  4213.  
  4214.  
  4215. static void
  4216. teximagemultisample(GLuint dims, GLenum target, GLsizei samples,
  4217.                     GLint internalformat, GLsizei width, GLsizei height,
  4218.                     GLsizei depth, GLboolean fixedsamplelocations,
  4219.                     GLboolean immutable, const char *func)
  4220. {
  4221.    struct gl_texture_object *texObj;
  4222.    struct gl_texture_image *texImage;
  4223.    GLboolean sizeOK, dimensionsOK;
  4224.    gl_format texFormat;
  4225.    GLenum sample_count_error;
  4226.  
  4227.    GET_CURRENT_CONTEXT(ctx);
  4228.  
  4229.    if (!(ctx->Extensions.ARB_texture_multisample
  4230.       && _mesa_is_desktop_gl(ctx))) {
  4231.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
  4232.       return;
  4233.    }
  4234.  
  4235.    if (!check_multisample_target(dims, target)) {
  4236.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
  4237.       return;
  4238.    }
  4239.  
  4240.    /* check that the specified internalformat is color/depth/stencil-renderable;
  4241.     * refer GL3.1 spec 4.4.4
  4242.     */
  4243.  
  4244.    if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
  4245.       _mesa_error(ctx, GL_INVALID_ENUM,
  4246.             "%s(internalformat=%s not legal for immutable-format)",
  4247.             func, _mesa_lookup_enum_by_nr(internalformat));
  4248.       return;
  4249.    }
  4250.  
  4251.    if (!is_renderable_texture_format(ctx, internalformat)) {
  4252.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4253.             "%s(internalformat=%s)",
  4254.             func, _mesa_lookup_enum_by_nr(internalformat));
  4255.       return;
  4256.    }
  4257.  
  4258.    sample_count_error = _mesa_check_sample_count(ctx, target,
  4259.          internalformat, samples);
  4260.    if (sample_count_error != GL_NO_ERROR) {
  4261.       _mesa_error(ctx, sample_count_error, "%s(samples)", func);
  4262.       return;
  4263.    }
  4264.  
  4265.    texObj = _mesa_get_current_tex_object(ctx, target);
  4266.  
  4267.    if (immutable && (!texObj || (texObj->Name == 0))) {
  4268.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4269.             "%s(texture object 0)",
  4270.             func);
  4271.       return;
  4272.    }
  4273.  
  4274.    texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
  4275.  
  4276.    if (texImage == NULL) {
  4277.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
  4278.       return;
  4279.    }
  4280.  
  4281.    texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
  4282.          internalformat, GL_NONE, GL_NONE);
  4283.    assert(texFormat != MESA_FORMAT_NONE);
  4284.  
  4285.    dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
  4286.          width, height, depth, 0);
  4287.  
  4288.    sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
  4289.          width, height, depth, 0);
  4290.  
  4291.    if (_mesa_is_proxy_texture(target)) {
  4292.       if (dimensionsOK && sizeOK) {
  4293.          _mesa_init_teximage_fields(ctx, texImage,
  4294.                width, height, depth, 0, internalformat, texFormat);
  4295.          texImage->NumSamples = samples;
  4296.          texImage->FixedSampleLocations = fixedsamplelocations;
  4297.       }
  4298.       else {
  4299.          /* clear all image fields */
  4300.          _mesa_init_teximage_fields(ctx, texImage,
  4301.                0, 0, 0, 0, GL_NONE, MESA_FORMAT_NONE);
  4302.       }
  4303.    }
  4304.    else {
  4305.       if (!dimensionsOK) {
  4306.          _mesa_error(ctx, GL_INVALID_VALUE,
  4307.                "%s(invalid width or height)", func);
  4308.          return;
  4309.       }
  4310.  
  4311.       if (!sizeOK) {
  4312.          _mesa_error(ctx, GL_OUT_OF_MEMORY,
  4313.                "%s(texture too large)", func);
  4314.          return;
  4315.       }
  4316.  
  4317.       /* Check if texObj->Immutable is set */
  4318.       if (texObj->Immutable) {
  4319.          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
  4320.          return;
  4321.       }
  4322.  
  4323.       ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  4324.  
  4325.       _mesa_init_teximage_fields(ctx, texImage,
  4326.             width, height, depth, 0, internalformat, texFormat);
  4327.  
  4328.       texImage->NumSamples = samples;
  4329.       texImage->FixedSampleLocations = fixedsamplelocations;
  4330.  
  4331.       if (width > 0 && height > 0 && depth > 0) {
  4332.          if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
  4333.                   width, height, depth)) {
  4334.             /* tidy up the texture image state. strictly speaking,
  4335.              * we're allowed to just leave this in whatever state we
  4336.              * like, but being tidy is good.
  4337.              */
  4338.             _mesa_init_teximage_fields(ctx, texImage,
  4339.                   0, 0, 0, 0, GL_NONE, MESA_FORMAT_NONE);
  4340.          }
  4341.       }
  4342.  
  4343.       texObj->Immutable = immutable;
  4344.       _mesa_update_fbo_texture(ctx, texObj, 0, 0);
  4345.    }
  4346. }
  4347.  
  4348.  
  4349. void GLAPIENTRY
  4350. _mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
  4351.                             GLint internalformat, GLsizei width,
  4352.                             GLsizei height, GLboolean fixedsamplelocations)
  4353. {
  4354.    teximagemultisample(2, target, samples, internalformat,
  4355.                        width, height, 1, fixedsamplelocations, GL_FALSE,
  4356.                        "glTexImage2DMultisample");
  4357. }
  4358.  
  4359.  
  4360. void GLAPIENTRY
  4361. _mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
  4362.                             GLint internalformat, GLsizei width,
  4363.                             GLsizei height, GLsizei depth,
  4364.                             GLboolean fixedsamplelocations)
  4365. {
  4366.    teximagemultisample(3, target, samples, internalformat,
  4367.                        width, height, depth, fixedsamplelocations, GL_FALSE,
  4368.                        "glTexImage3DMultisample");
  4369. }
  4370.  
  4371.  
  4372. void GLAPIENTRY
  4373. _mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
  4374.                               GLenum internalformat, GLsizei width,
  4375.                               GLsizei height, GLboolean fixedsamplelocations)
  4376. {
  4377.    teximagemultisample(2, target, samples, internalformat,
  4378.                        width, height, 1, fixedsamplelocations, GL_TRUE,
  4379.                        "glTexStorage2DMultisample");
  4380. }
  4381.  
  4382.  
  4383. void GLAPIENTRY
  4384. _mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
  4385.                               GLenum internalformat, GLsizei width,
  4386.                               GLsizei height, GLsizei depth,
  4387.                               GLboolean fixedsamplelocations)
  4388. {
  4389.    teximagemultisample(3, target, samples, internalformat,
  4390.                        width, height, depth, fixedsamplelocations, GL_TRUE,
  4391.                        "glTexStorage3DMultisample");
  4392. }
  4393.