Subversion Repositories Kolibri OS

Rev

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 "pixelstore.h"
  45. #include "state.h"
  46. #include "texcompress.h"
  47. #include "texcompress_cpal.h"
  48. #include "teximage.h"
  49. #include "texobj.h"
  50. #include "texstate.h"
  51. #include "texstorage.h"
  52. #include "textureview.h"
  53. #include "mtypes.h"
  54. #include "glformats.h"
  55. #include "texstore.h"
  56. #include "pbo.h"
  57.  
  58.  
  59. /**
  60.  * State changes which we care about for glCopyTex[Sub]Image() calls.
  61.  * In particular, we care about pixel transfer state and buffer state
  62.  * (such as glReadBuffer to make sure we read from the right renderbuffer).
  63.  */
  64. #define NEW_COPY_TEX_STATE (_NEW_BUFFERS | _NEW_PIXEL)
  65.  
  66. /**
  67.  * Returns a corresponding internal floating point format for a given base
  68.  * format as specifed by OES_texture_float. In case of GL_FLOAT, the internal
  69.  * format needs to be a 32 bit component and in case of GL_HALF_FLOAT_OES it
  70.  * needs to be a 16 bit component.
  71.  *
  72.  * For example, given base format GL_RGBA, type GL_Float return GL_RGBA32F_ARB.
  73.  */
  74. static GLenum
  75. adjust_for_oes_float_texture(GLenum format, GLenum type)
  76. {
  77.    switch (type) {
  78.    case GL_FLOAT:
  79.       switch (format) {
  80.       case GL_RGBA:
  81.          return GL_RGBA32F;
  82.       case GL_RGB:
  83.          return GL_RGB32F;
  84.       case GL_ALPHA:
  85.          return GL_ALPHA32F_ARB;
  86.       case GL_LUMINANCE:
  87.          return GL_LUMINANCE32F_ARB;
  88.       case GL_LUMINANCE_ALPHA:
  89.          return GL_LUMINANCE_ALPHA32F_ARB;
  90.       default:
  91.          break;
  92.       }
  93.       break;
  94.  
  95.    case GL_HALF_FLOAT_OES:
  96.       switch (format) {
  97.       case GL_RGBA:
  98.          return GL_RGBA16F;
  99.       case GL_RGB:
  100.          return GL_RGB16F;
  101.       case GL_ALPHA:
  102.          return GL_ALPHA16F_ARB;
  103.       case GL_LUMINANCE:
  104.          return GL_LUMINANCE16F_ARB;
  105.       case GL_LUMINANCE_ALPHA:
  106.          return GL_LUMINANCE_ALPHA16F_ARB;
  107.       default:
  108.          break;
  109.       }
  110.       break;
  111.  
  112.    default:
  113.       break;
  114.    }
  115.  
  116.    return format;
  117. }
  118.  
  119. /**
  120.  * Return the simple base format for a given internal texture format.
  121.  * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
  122.  *
  123.  * \param ctx GL context.
  124.  * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
  125.  *
  126.  * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
  127.  * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
  128.  *
  129.  * This is the format which is used during texture application (i.e. the
  130.  * texture format and env mode determine the arithmetic used.
  131.  */
  132. GLint
  133. _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
  134. {
  135.    switch (internalFormat) {
  136.    case GL_ALPHA:
  137.    case GL_ALPHA4:
  138.    case GL_ALPHA8:
  139.    case GL_ALPHA12:
  140.    case GL_ALPHA16:
  141.       return (ctx->API != API_OPENGL_CORE) ? GL_ALPHA : -1;
  142.    case 1:
  143.    case GL_LUMINANCE:
  144.    case GL_LUMINANCE4:
  145.    case GL_LUMINANCE8:
  146.    case GL_LUMINANCE12:
  147.    case GL_LUMINANCE16:
  148.       return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE : -1;
  149.    case 2:
  150.    case GL_LUMINANCE_ALPHA:
  151.    case GL_LUMINANCE4_ALPHA4:
  152.    case GL_LUMINANCE6_ALPHA2:
  153.    case GL_LUMINANCE8_ALPHA8:
  154.    case GL_LUMINANCE12_ALPHA4:
  155.    case GL_LUMINANCE12_ALPHA12:
  156.    case GL_LUMINANCE16_ALPHA16:
  157.       return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE_ALPHA : -1;
  158.    case GL_INTENSITY:
  159.    case GL_INTENSITY4:
  160.    case GL_INTENSITY8:
  161.    case GL_INTENSITY12:
  162.    case GL_INTENSITY16:
  163.       return (ctx->API != API_OPENGL_CORE) ? GL_INTENSITY : -1;
  164.    case 3:
  165.       return (ctx->API != API_OPENGL_CORE) ? GL_RGB : -1;
  166.    case GL_RGB:
  167.    case GL_R3_G3_B2:
  168.    case GL_RGB4:
  169.    case GL_RGB5:
  170.    case GL_RGB8:
  171.    case GL_RGB10:
  172.    case GL_RGB12:
  173.    case GL_RGB16:
  174.       return GL_RGB;
  175.    case 4:
  176.       return (ctx->API != API_OPENGL_CORE) ? GL_RGBA : -1;
  177.    case GL_RGBA:
  178.    case GL_RGBA2:
  179.    case GL_RGBA4:
  180.    case GL_RGB5_A1:
  181.    case GL_RGBA8:
  182.    case GL_RGB10_A2:
  183.    case GL_RGBA12:
  184.    case GL_RGBA16:
  185.       return GL_RGBA;
  186.    default:
  187.       ; /* fallthrough */
  188.    }
  189.  
  190.    /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
  191.     */
  192.    if (_mesa_is_gles(ctx)) {
  193.       switch (internalFormat) {
  194.       case GL_BGRA:
  195.          return GL_RGBA;
  196.       default:
  197.          ; /* fallthrough */
  198.       }
  199.    }
  200.  
  201.    if (ctx->Extensions.ARB_ES2_compatibility) {
  202.       switch (internalFormat) {
  203.       case GL_RGB565:
  204.          return GL_RGB;
  205.       default:
  206.          ; /* fallthrough */
  207.       }
  208.    }
  209.  
  210.    if (ctx->Extensions.ARB_depth_texture) {
  211.       switch (internalFormat) {
  212.       case GL_DEPTH_COMPONENT:
  213.       case GL_DEPTH_COMPONENT16:
  214.       case GL_DEPTH_COMPONENT24:
  215.       case GL_DEPTH_COMPONENT32:
  216.          return GL_DEPTH_COMPONENT;
  217.       case GL_DEPTH_STENCIL:
  218.       case GL_DEPTH24_STENCIL8:
  219.          return GL_DEPTH_STENCIL;
  220.       default:
  221.          ; /* fallthrough */
  222.       }
  223.    }
  224.  
  225.    if (ctx->Extensions.ARB_stencil_texturing) {
  226.       switch (internalFormat) {
  227.       case GL_STENCIL_INDEX:
  228.       case GL_STENCIL_INDEX1:
  229.       case GL_STENCIL_INDEX4:
  230.       case GL_STENCIL_INDEX8:
  231.       case GL_STENCIL_INDEX16:
  232.          return GL_STENCIL_INDEX;
  233.       default:
  234.          ; /* fallthrough */
  235.       }
  236.    }
  237.  
  238.    switch (internalFormat) {
  239.    case GL_COMPRESSED_ALPHA:
  240.       return GL_ALPHA;
  241.    case GL_COMPRESSED_LUMINANCE:
  242.       return GL_LUMINANCE;
  243.    case GL_COMPRESSED_LUMINANCE_ALPHA:
  244.       return GL_LUMINANCE_ALPHA;
  245.    case GL_COMPRESSED_INTENSITY:
  246.       return GL_INTENSITY;
  247.    case GL_COMPRESSED_RGB:
  248.       return GL_RGB;
  249.    case GL_COMPRESSED_RGBA:
  250.       return GL_RGBA;
  251.    default:
  252.       ; /* fallthrough */
  253.    }
  254.  
  255.    if (ctx->Extensions.TDFX_texture_compression_FXT1) {
  256.       switch (internalFormat) {
  257.       case GL_COMPRESSED_RGB_FXT1_3DFX:
  258.          return GL_RGB;
  259.       case GL_COMPRESSED_RGBA_FXT1_3DFX:
  260.          return GL_RGBA;
  261.       default:
  262.          ; /* fallthrough */
  263.       }
  264.    }
  265.  
  266.    /* Assume that the ANGLE flag will always be set if the EXT flag is set.
  267.     */
  268.    if (ctx->Extensions.ANGLE_texture_compression_dxt) {
  269.       switch (internalFormat) {
  270.       case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  271.          return GL_RGB;
  272.       case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  273.       case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  274.       case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  275.          return GL_RGBA;
  276.       default:
  277.          ; /* fallthrough */
  278.       }
  279.    }
  280.  
  281.    if (_mesa_is_desktop_gl(ctx)
  282.        && ctx->Extensions.ANGLE_texture_compression_dxt) {
  283.       switch (internalFormat) {
  284.       case GL_RGB_S3TC:
  285.       case GL_RGB4_S3TC:
  286.          return GL_RGB;
  287.       case GL_RGBA_S3TC:
  288.       case GL_RGBA4_S3TC:
  289.          return GL_RGBA;
  290.       default:
  291.          ; /* fallthrough */
  292.       }
  293.    }
  294.  
  295.    if (ctx->Extensions.MESA_ycbcr_texture) {
  296.       if (internalFormat == GL_YCBCR_MESA)
  297.          return GL_YCBCR_MESA;
  298.    }
  299.  
  300.    if (ctx->Extensions.ARB_texture_float) {
  301.       switch (internalFormat) {
  302.       case GL_ALPHA16F_ARB:
  303.       case GL_ALPHA32F_ARB:
  304.          return GL_ALPHA;
  305.       case GL_RGBA16F_ARB:
  306.       case GL_RGBA32F_ARB:
  307.          return GL_RGBA;
  308.       case GL_RGB16F_ARB:
  309.       case GL_RGB32F_ARB:
  310.          return GL_RGB;
  311.       case GL_INTENSITY16F_ARB:
  312.       case GL_INTENSITY32F_ARB:
  313.          return GL_INTENSITY;
  314.       case GL_LUMINANCE16F_ARB:
  315.       case GL_LUMINANCE32F_ARB:
  316.          return GL_LUMINANCE;
  317.       case GL_LUMINANCE_ALPHA16F_ARB:
  318.       case GL_LUMINANCE_ALPHA32F_ARB:
  319.          return GL_LUMINANCE_ALPHA;
  320.       default:
  321.          ; /* fallthrough */
  322.       }
  323.    }
  324.  
  325.    if (ctx->Extensions.EXT_texture_snorm) {
  326.       switch (internalFormat) {
  327.       case GL_RED_SNORM:
  328.       case GL_R8_SNORM:
  329.       case GL_R16_SNORM:
  330.          return GL_RED;
  331.       case GL_RG_SNORM:
  332.       case GL_RG8_SNORM:
  333.       case GL_RG16_SNORM:
  334.          return GL_RG;
  335.       case GL_RGB_SNORM:
  336.       case GL_RGB8_SNORM:
  337.       case GL_RGB16_SNORM:
  338.          return GL_RGB;
  339.       case GL_RGBA_SNORM:
  340.       case GL_RGBA8_SNORM:
  341.       case GL_RGBA16_SNORM:
  342.          return GL_RGBA;
  343.       case GL_ALPHA_SNORM:
  344.       case GL_ALPHA8_SNORM:
  345.       case GL_ALPHA16_SNORM:
  346.          return GL_ALPHA;
  347.       case GL_LUMINANCE_SNORM:
  348.       case GL_LUMINANCE8_SNORM:
  349.       case GL_LUMINANCE16_SNORM:
  350.          return GL_LUMINANCE;
  351.       case GL_LUMINANCE_ALPHA_SNORM:
  352.       case GL_LUMINANCE8_ALPHA8_SNORM:
  353.       case GL_LUMINANCE16_ALPHA16_SNORM:
  354.          return GL_LUMINANCE_ALPHA;
  355.       case GL_INTENSITY_SNORM:
  356.       case GL_INTENSITY8_SNORM:
  357.       case GL_INTENSITY16_SNORM:
  358.          return GL_INTENSITY;
  359.       default:
  360.          ; /* fallthrough */
  361.       }
  362.    }
  363.  
  364.    if (ctx->Extensions.EXT_texture_sRGB) {
  365.       switch (internalFormat) {
  366.       case GL_SRGB_EXT:
  367.       case GL_SRGB8_EXT:
  368.       case GL_COMPRESSED_SRGB_EXT:
  369.          return GL_RGB;
  370.       case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
  371.          return ctx->Extensions.EXT_texture_compression_s3tc ? GL_RGB : -1;
  372.       case GL_SRGB_ALPHA_EXT:
  373.       case GL_SRGB8_ALPHA8_EXT:
  374.       case GL_COMPRESSED_SRGB_ALPHA_EXT:
  375.          return GL_RGBA;
  376.       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
  377.       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
  378.       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
  379.          return ctx->Extensions.EXT_texture_compression_s3tc ? GL_RGBA : -1;
  380.       case GL_SLUMINANCE_ALPHA_EXT:
  381.       case GL_SLUMINANCE8_ALPHA8_EXT:
  382.       case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
  383.          return GL_LUMINANCE_ALPHA;
  384.       case GL_SLUMINANCE_EXT:
  385.       case GL_SLUMINANCE8_EXT:
  386.       case GL_COMPRESSED_SLUMINANCE_EXT:
  387.          return GL_LUMINANCE;
  388.       default:
  389.          ; /* fallthrough */
  390.       }
  391.    }
  392.  
  393.    if (ctx->Version >= 30 ||
  394.        ctx->Extensions.EXT_texture_integer) {
  395.       switch (internalFormat) {
  396.       case GL_RGBA8UI_EXT:
  397.       case GL_RGBA16UI_EXT:
  398.       case GL_RGBA32UI_EXT:
  399.       case GL_RGBA8I_EXT:
  400.       case GL_RGBA16I_EXT:
  401.       case GL_RGBA32I_EXT:
  402.       case GL_RGB10_A2UI:
  403.          return GL_RGBA;
  404.       case GL_RGB8UI_EXT:
  405.       case GL_RGB16UI_EXT:
  406.       case GL_RGB32UI_EXT:
  407.       case GL_RGB8I_EXT:
  408.       case GL_RGB16I_EXT:
  409.       case GL_RGB32I_EXT:
  410.          return GL_RGB;
  411.       }
  412.    }
  413.  
  414.    if (ctx->Extensions.EXT_texture_integer) {
  415.       switch (internalFormat) {
  416.       case GL_ALPHA8UI_EXT:
  417.       case GL_ALPHA16UI_EXT:
  418.       case GL_ALPHA32UI_EXT:
  419.       case GL_ALPHA8I_EXT:
  420.       case GL_ALPHA16I_EXT:
  421.       case GL_ALPHA32I_EXT:
  422.          return GL_ALPHA;
  423.       case GL_INTENSITY8UI_EXT:
  424.       case GL_INTENSITY16UI_EXT:
  425.       case GL_INTENSITY32UI_EXT:
  426.       case GL_INTENSITY8I_EXT:
  427.       case GL_INTENSITY16I_EXT:
  428.       case GL_INTENSITY32I_EXT:
  429.          return GL_INTENSITY;
  430.       case GL_LUMINANCE8UI_EXT:
  431.       case GL_LUMINANCE16UI_EXT:
  432.       case GL_LUMINANCE32UI_EXT:
  433.       case GL_LUMINANCE8I_EXT:
  434.       case GL_LUMINANCE16I_EXT:
  435.       case GL_LUMINANCE32I_EXT:
  436.          return GL_LUMINANCE;
  437.       case GL_LUMINANCE_ALPHA8UI_EXT:
  438.       case GL_LUMINANCE_ALPHA16UI_EXT:
  439.       case GL_LUMINANCE_ALPHA32UI_EXT:
  440.       case GL_LUMINANCE_ALPHA8I_EXT:
  441.       case GL_LUMINANCE_ALPHA16I_EXT:
  442.       case GL_LUMINANCE_ALPHA32I_EXT:
  443.          return GL_LUMINANCE_ALPHA;
  444.       default:
  445.          ; /* fallthrough */
  446.       }
  447.    }
  448.  
  449.    if (ctx->Extensions.ARB_texture_rg) {
  450.       switch (internalFormat) {
  451.       case GL_R16F:
  452.       case GL_R32F:
  453.          if (!ctx->Extensions.ARB_texture_float)
  454.             break;
  455.          return GL_RED;
  456.       case GL_R8I:
  457.       case GL_R8UI:
  458.       case GL_R16I:
  459.       case GL_R16UI:
  460.       case GL_R32I:
  461.       case GL_R32UI:
  462.          if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
  463.             break;
  464.          /* FALLTHROUGH */
  465.       case GL_R8:
  466.       case GL_R16:
  467.       case GL_RED:
  468.       case GL_COMPRESSED_RED:
  469.          return GL_RED;
  470.  
  471.       case GL_RG16F:
  472.       case GL_RG32F:
  473.          if (!ctx->Extensions.ARB_texture_float)
  474.             break;
  475.          return GL_RG;
  476.       case GL_RG8I:
  477.       case GL_RG8UI:
  478.       case GL_RG16I:
  479.       case GL_RG16UI:
  480.       case GL_RG32I:
  481.       case GL_RG32UI:
  482.          if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
  483.             break;
  484.          /* FALLTHROUGH */
  485.       case GL_RG:
  486.       case GL_RG8:
  487.       case GL_RG16:
  488.       case GL_COMPRESSED_RG:
  489.          return GL_RG;
  490.       default:
  491.          ; /* fallthrough */
  492.       }
  493.    }
  494.  
  495.    if (ctx->Extensions.EXT_texture_shared_exponent) {
  496.       switch (internalFormat) {
  497.       case GL_RGB9_E5_EXT:
  498.          return GL_RGB;
  499.       default:
  500.          ; /* fallthrough */
  501.       }
  502.    }
  503.  
  504.    if (ctx->Extensions.EXT_packed_float) {
  505.       switch (internalFormat) {
  506.       case GL_R11F_G11F_B10F_EXT:
  507.          return GL_RGB;
  508.       default:
  509.          ; /* fallthrough */
  510.       }
  511.    }
  512.  
  513.    if (ctx->Extensions.ARB_depth_buffer_float) {
  514.       switch (internalFormat) {
  515.       case GL_DEPTH_COMPONENT32F:
  516.          return GL_DEPTH_COMPONENT;
  517.       case GL_DEPTH32F_STENCIL8:
  518.          return GL_DEPTH_STENCIL;
  519.       default:
  520.          ; /* fallthrough */
  521.       }
  522.    }
  523.  
  524.    if (ctx->Extensions.ARB_texture_compression_rgtc) {
  525.       switch (internalFormat) {
  526.       case GL_COMPRESSED_RED_RGTC1:
  527.       case GL_COMPRESSED_SIGNED_RED_RGTC1:
  528.          return GL_RED;
  529.       case GL_COMPRESSED_RG_RGTC2:
  530.       case GL_COMPRESSED_SIGNED_RG_RGTC2:
  531.          return GL_RG;
  532.       default:
  533.          ; /* fallthrough */
  534.       }
  535.    }
  536.  
  537.    if (ctx->Extensions.EXT_texture_compression_latc) {
  538.       switch (internalFormat) {
  539.       case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
  540.       case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
  541.          return GL_LUMINANCE;
  542.       case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
  543.       case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
  544.          return GL_LUMINANCE_ALPHA;
  545.       default:
  546.          ; /* fallthrough */
  547.       }
  548.    }
  549.  
  550.    if (ctx->Extensions.ATI_texture_compression_3dc) {
  551.       switch (internalFormat) {
  552.       case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
  553.          return GL_LUMINANCE_ALPHA;
  554.       default:
  555.          ; /* fallthrough */
  556.       }
  557.    }
  558.  
  559.    if (ctx->Extensions.OES_compressed_ETC1_RGB8_texture) {
  560.       switch (internalFormat) {
  561.       case GL_ETC1_RGB8_OES:
  562.          return GL_RGB;
  563.       default:
  564.          ; /* fallthrough */
  565.       }
  566.    }
  567.  
  568.    if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility) {
  569.       switch (internalFormat) {
  570.       case GL_COMPRESSED_RGB8_ETC2:
  571.       case GL_COMPRESSED_SRGB8_ETC2:
  572.          return GL_RGB;
  573.       case GL_COMPRESSED_RGBA8_ETC2_EAC:
  574.       case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
  575.       case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
  576.       case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
  577.          return GL_RGBA;
  578.       case GL_COMPRESSED_R11_EAC:
  579.       case GL_COMPRESSED_SIGNED_R11_EAC:
  580.          return GL_RED;
  581.       case GL_COMPRESSED_RG11_EAC:
  582.       case GL_COMPRESSED_SIGNED_RG11_EAC:
  583.          return GL_RG;
  584.       default:
  585.          ; /* fallthrough */
  586.       }
  587.    }
  588.  
  589.    if (_mesa_is_desktop_gl(ctx) &&
  590.        ctx->Extensions.ARB_texture_compression_bptc) {
  591.       switch (internalFormat) {
  592.       case GL_COMPRESSED_RGBA_BPTC_UNORM:
  593.       case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
  594.          return GL_RGBA;
  595.       case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
  596.       case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
  597.          return GL_RGB;
  598.       default:
  599.          ; /* fallthrough */
  600.       }
  601.    }
  602.  
  603.    if (ctx->API == API_OPENGLES) {
  604.       switch (internalFormat) {
  605.       case GL_PALETTE4_RGB8_OES:
  606.       case GL_PALETTE4_R5_G6_B5_OES:
  607.       case GL_PALETTE8_RGB8_OES:
  608.       case GL_PALETTE8_R5_G6_B5_OES:
  609.          return GL_RGB;
  610.       case GL_PALETTE4_RGBA8_OES:
  611.       case GL_PALETTE8_RGB5_A1_OES:
  612.       case GL_PALETTE4_RGBA4_OES:
  613.       case GL_PALETTE4_RGB5_A1_OES:
  614.       case GL_PALETTE8_RGBA8_OES:
  615.       case GL_PALETTE8_RGBA4_OES:
  616.          return GL_RGBA;
  617.       default:
  618.          ; /* fallthrough */
  619.       }
  620.    }
  621.  
  622.    return -1; /* error */
  623. }
  624.  
  625.  
  626. /**
  627.  * For cube map faces, return a face index in [0,5].
  628.  * For other targets return 0;
  629.  */
  630. GLuint
  631. _mesa_tex_target_to_face(GLenum target)
  632. {
  633.    if (_mesa_is_cube_face(target))
  634.       return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  635.    else
  636.       return 0;
  637. }
  638.  
  639.  
  640.  
  641. /**
  642.  * Install gl_texture_image in a gl_texture_object according to the target
  643.  * and level parameters.
  644.  *
  645.  * \param tObj texture object.
  646.  * \param target texture target.
  647.  * \param level image level.
  648.  * \param texImage texture image.
  649.  */
  650. static void
  651. set_tex_image(struct gl_texture_object *tObj,
  652.               GLenum target, GLint level,
  653.               struct gl_texture_image *texImage)
  654. {
  655.    const GLuint face = _mesa_tex_target_to_face(target);
  656.  
  657.    assert(tObj);
  658.    assert(texImage);
  659.    if (target == GL_TEXTURE_RECTANGLE_NV || target == GL_TEXTURE_EXTERNAL_OES)
  660.       assert(level == 0);
  661.  
  662.    tObj->Image[face][level] = texImage;
  663.  
  664.    /* Set the 'back' pointer */
  665.    texImage->TexObject = tObj;
  666.    texImage->Level = level;
  667.    texImage->Face = face;
  668. }
  669.  
  670.  
  671. /**
  672.  * Allocate a texture image structure.
  673.  *
  674.  * Called via ctx->Driver.NewTextureImage() unless overriden by a device
  675.  * driver.
  676.  *
  677.  * \return a pointer to gl_texture_image struct with all fields initialized to
  678.  * zero.
  679.  */
  680. struct gl_texture_image *
  681. _mesa_new_texture_image( struct gl_context *ctx )
  682. {
  683.    (void) ctx;
  684.    return CALLOC_STRUCT(gl_texture_image);
  685. }
  686.  
  687.  
  688. /**
  689.  * Free a gl_texture_image and associated data.
  690.  * This function is a fallback called via ctx->Driver.DeleteTextureImage().
  691.  *
  692.  * \param texImage texture image.
  693.  *
  694.  * Free the texture image structure and the associated image data.
  695.  */
  696. void
  697. _mesa_delete_texture_image(struct gl_context *ctx,
  698.                            struct gl_texture_image *texImage)
  699. {
  700.    /* Free texImage->Data and/or any other driver-specific texture
  701.     * image storage.
  702.     */
  703.    assert(ctx->Driver.FreeTextureImageBuffer);
  704.    ctx->Driver.FreeTextureImageBuffer( ctx, texImage );
  705.    free(texImage);
  706. }
  707.  
  708.  
  709. /**
  710.  * Test if a target is a proxy target.
  711.  *
  712.  * \param target texture target.
  713.  *
  714.  * \return GL_TRUE if the target is a proxy target, GL_FALSE otherwise.
  715.  */
  716. GLboolean
  717. _mesa_is_proxy_texture(GLenum target)
  718. {
  719.    unsigned i;
  720.    static const GLenum targets[] = {
  721.       GL_PROXY_TEXTURE_1D,
  722.       GL_PROXY_TEXTURE_2D,
  723.       GL_PROXY_TEXTURE_3D,
  724.       GL_PROXY_TEXTURE_CUBE_MAP,
  725.       GL_PROXY_TEXTURE_RECTANGLE,
  726.       GL_PROXY_TEXTURE_1D_ARRAY,
  727.       GL_PROXY_TEXTURE_2D_ARRAY,
  728.       GL_PROXY_TEXTURE_CUBE_MAP_ARRAY,
  729.       GL_PROXY_TEXTURE_2D_MULTISAMPLE,
  730.       GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
  731.    };
  732.    /*
  733.     * NUM_TEXTURE_TARGETS should match number of terms above, except there's no
  734.     * proxy for GL_TEXTURE_BUFFER and GL_TEXTURE_EXTERNAL_OES.
  735.     */
  736.    STATIC_ASSERT(NUM_TEXTURE_TARGETS == ARRAY_SIZE(targets) + 2);
  737.  
  738.    for (i = 0; i < ARRAY_SIZE(targets); ++i)
  739.       if (target == targets[i])
  740.          return GL_TRUE;
  741.    return GL_FALSE;
  742. }
  743.  
  744.  
  745. /**
  746.  * Test if a target is an array target.
  747.  *
  748.  * \param target texture target.
  749.  *
  750.  * \return true if the target is an array target, false otherwise.
  751.  */
  752. bool
  753. _mesa_is_array_texture(GLenum target)
  754. {
  755.    switch (target) {
  756.    case GL_TEXTURE_1D_ARRAY:
  757.    case GL_TEXTURE_2D_ARRAY:
  758.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  759.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  760.       return true;
  761.    default:
  762.       return false;
  763.    };
  764. }
  765.  
  766.  
  767. /**
  768.  * Return the proxy target which corresponds to the given texture target
  769.  */
  770. static GLenum
  771. proxy_target(GLenum target)
  772. {
  773.    switch (target) {
  774.    case GL_TEXTURE_1D:
  775.    case GL_PROXY_TEXTURE_1D:
  776.       return GL_PROXY_TEXTURE_1D;
  777.    case GL_TEXTURE_2D:
  778.    case GL_PROXY_TEXTURE_2D:
  779.       return GL_PROXY_TEXTURE_2D;
  780.    case GL_TEXTURE_3D:
  781.    case GL_PROXY_TEXTURE_3D:
  782.       return GL_PROXY_TEXTURE_3D;
  783.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
  784.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
  785.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
  786.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
  787.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
  788.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
  789.    case GL_TEXTURE_CUBE_MAP_ARB:
  790.    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
  791.       return GL_PROXY_TEXTURE_CUBE_MAP_ARB;
  792.    case GL_TEXTURE_RECTANGLE_NV:
  793.    case GL_PROXY_TEXTURE_RECTANGLE_NV:
  794.       return GL_PROXY_TEXTURE_RECTANGLE_NV;
  795.    case GL_TEXTURE_1D_ARRAY_EXT:
  796.    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  797.       return GL_PROXY_TEXTURE_1D_ARRAY_EXT;
  798.    case GL_TEXTURE_2D_ARRAY_EXT:
  799.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  800.       return GL_PROXY_TEXTURE_2D_ARRAY_EXT;
  801.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  802.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  803.       return GL_PROXY_TEXTURE_CUBE_MAP_ARRAY;
  804.    case GL_TEXTURE_2D_MULTISAMPLE:
  805.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  806.       return GL_PROXY_TEXTURE_2D_MULTISAMPLE;
  807.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  808.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  809.       return GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY;
  810.    default:
  811.       _mesa_problem(NULL, "unexpected target in proxy_target()");
  812.       return 0;
  813.    }
  814. }
  815.  
  816.  
  817.  
  818.  
  819. /**
  820.  * Get a texture image pointer from a texture object, given a texture
  821.  * target and mipmap level.  The target and level parameters should
  822.  * have already been error-checked.
  823.  *
  824.  * \param texObj texture unit.
  825.  * \param target texture target.
  826.  * \param level image level.
  827.  *
  828.  * \return pointer to the texture image structure, or NULL on failure.
  829.  */
  830. struct gl_texture_image *
  831. _mesa_select_tex_image(const struct gl_texture_object *texObj,
  832.                                  GLenum target, GLint level)
  833. {
  834.    const GLuint face = _mesa_tex_target_to_face(target);
  835.  
  836.    assert(texObj);
  837.    assert(level >= 0);
  838.    assert(level < MAX_TEXTURE_LEVELS);
  839.  
  840.    return texObj->Image[face][level];
  841. }
  842.  
  843.  
  844. /**
  845.  * Like _mesa_select_tex_image() but if the image doesn't exist, allocate
  846.  * it and install it.  Only return NULL if passed a bad parameter or run
  847.  * out of memory.
  848.  */
  849. struct gl_texture_image *
  850. _mesa_get_tex_image(struct gl_context *ctx, struct gl_texture_object *texObj,
  851.                     GLenum target, GLint level)
  852. {
  853.    struct gl_texture_image *texImage;
  854.  
  855.    if (!texObj)
  856.       return NULL;
  857.  
  858.    texImage = _mesa_select_tex_image(texObj, target, level);
  859.    if (!texImage) {
  860.       texImage = ctx->Driver.NewTextureImage(ctx);
  861.       if (!texImage) {
  862.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "texture image allocation");
  863.          return NULL;
  864.       }
  865.  
  866.       set_tex_image(texObj, target, level, texImage);
  867.    }
  868.  
  869.    return texImage;
  870. }
  871.  
  872.  
  873. /**
  874.  * Return pointer to the specified proxy texture image.
  875.  * Note that proxy textures are per-context, not per-texture unit.
  876.  * \return pointer to texture image or NULL if invalid target, invalid
  877.  *         level, or out of memory.
  878.  */
  879. static struct gl_texture_image *
  880. get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
  881. {
  882.    struct gl_texture_image *texImage;
  883.    GLuint texIndex;
  884.  
  885.    if (level < 0)
  886.       return NULL;
  887.  
  888.    switch (target) {
  889.    case GL_PROXY_TEXTURE_1D:
  890.       if (level >= ctx->Const.MaxTextureLevels)
  891.          return NULL;
  892.       texIndex = TEXTURE_1D_INDEX;
  893.       break;
  894.    case GL_PROXY_TEXTURE_2D:
  895.       if (level >= ctx->Const.MaxTextureLevels)
  896.          return NULL;
  897.       texIndex = TEXTURE_2D_INDEX;
  898.       break;
  899.    case GL_PROXY_TEXTURE_3D:
  900.       if (level >= ctx->Const.Max3DTextureLevels)
  901.          return NULL;
  902.       texIndex = TEXTURE_3D_INDEX;
  903.       break;
  904.    case GL_PROXY_TEXTURE_CUBE_MAP:
  905.       if (level >= ctx->Const.MaxCubeTextureLevels)
  906.          return NULL;
  907.       texIndex = TEXTURE_CUBE_INDEX;
  908.       break;
  909.    case GL_PROXY_TEXTURE_RECTANGLE_NV:
  910.       if (level > 0)
  911.          return NULL;
  912.       texIndex = TEXTURE_RECT_INDEX;
  913.       break;
  914.    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  915.       if (level >= ctx->Const.MaxTextureLevels)
  916.          return NULL;
  917.       texIndex = TEXTURE_1D_ARRAY_INDEX;
  918.       break;
  919.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  920.       if (level >= ctx->Const.MaxTextureLevels)
  921.          return NULL;
  922.       texIndex = TEXTURE_2D_ARRAY_INDEX;
  923.       break;
  924.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  925.       if (level >= ctx->Const.MaxCubeTextureLevels)
  926.          return NULL;
  927.       texIndex = TEXTURE_CUBE_ARRAY_INDEX;
  928.       break;
  929.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  930.       if (level > 0)
  931.          return 0;
  932.       texIndex = TEXTURE_2D_MULTISAMPLE_INDEX;
  933.       break;
  934.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  935.       if (level > 0)
  936.          return 0;
  937.       texIndex = TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX;
  938.       break;
  939.    default:
  940.       return NULL;
  941.    }
  942.  
  943.    texImage = ctx->Texture.ProxyTex[texIndex]->Image[0][level];
  944.    if (!texImage) {
  945.       texImage = ctx->Driver.NewTextureImage(ctx);
  946.       if (!texImage) {
  947.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "proxy texture allocation");
  948.          return NULL;
  949.       }
  950.       ctx->Texture.ProxyTex[texIndex]->Image[0][level] = texImage;
  951.       /* Set the 'back' pointer */
  952.       texImage->TexObject = ctx->Texture.ProxyTex[texIndex];
  953.    }
  954.    return texImage;
  955. }
  956.  
  957.  
  958. /**
  959.  * Get the maximum number of allowed mipmap levels.
  960.  *
  961.  * \param ctx GL context.
  962.  * \param target texture target.
  963.  *
  964.  * \return the maximum number of allowed mipmap levels for the given
  965.  * texture target, or zero if passed a bad target.
  966.  *
  967.  * \sa gl_constants.
  968.  */
  969. GLint
  970. _mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
  971. {
  972.    switch (target) {
  973.    case GL_TEXTURE_1D:
  974.    case GL_PROXY_TEXTURE_1D:
  975.    case GL_TEXTURE_2D:
  976.    case GL_PROXY_TEXTURE_2D:
  977.       return ctx->Const.MaxTextureLevels;
  978.    case GL_TEXTURE_3D:
  979.    case GL_PROXY_TEXTURE_3D:
  980.       return ctx->Const.Max3DTextureLevels;
  981.    case GL_TEXTURE_CUBE_MAP:
  982.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
  983.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
  984.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
  985.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
  986.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
  987.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
  988.    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
  989.       return ctx->Extensions.ARB_texture_cube_map
  990.          ? ctx->Const.MaxCubeTextureLevels : 0;
  991.    case GL_TEXTURE_RECTANGLE_NV:
  992.    case GL_PROXY_TEXTURE_RECTANGLE_NV:
  993.       return ctx->Extensions.NV_texture_rectangle ? 1 : 0;
  994.    case GL_TEXTURE_1D_ARRAY_EXT:
  995.    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  996.    case GL_TEXTURE_2D_ARRAY_EXT:
  997.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  998.       return ctx->Extensions.EXT_texture_array
  999.          ? ctx->Const.MaxTextureLevels : 0;
  1000.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1001.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1002.       return ctx->Extensions.ARB_texture_cube_map_array
  1003.          ? ctx->Const.MaxCubeTextureLevels : 0;
  1004.    case GL_TEXTURE_BUFFER:
  1005.       return ctx->API == API_OPENGL_CORE &&
  1006.              ctx->Extensions.ARB_texture_buffer_object ? 1 : 0;
  1007.    case GL_TEXTURE_2D_MULTISAMPLE:
  1008.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1009.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1010.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1011.       return _mesa_is_desktop_gl(ctx)
  1012.          && ctx->Extensions.ARB_texture_multisample
  1013.          ? 1 : 0;
  1014.    case GL_TEXTURE_EXTERNAL_OES:
  1015.       /* fall-through */
  1016.    default:
  1017.       return 0; /* bad target */
  1018.    }
  1019. }
  1020.  
  1021.  
  1022. /**
  1023.  * Return number of dimensions per mipmap level for the given texture target.
  1024.  */
  1025. GLint
  1026. _mesa_get_texture_dimensions(GLenum target)
  1027. {
  1028.    switch (target) {
  1029.    case GL_TEXTURE_1D:
  1030.    case GL_PROXY_TEXTURE_1D:
  1031.       return 1;
  1032.    case GL_TEXTURE_2D:
  1033.    case GL_TEXTURE_RECTANGLE:
  1034.    case GL_TEXTURE_CUBE_MAP:
  1035.    case GL_PROXY_TEXTURE_2D:
  1036.    case GL_PROXY_TEXTURE_RECTANGLE:
  1037.    case GL_PROXY_TEXTURE_CUBE_MAP:
  1038.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1039.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1040.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1041.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1042.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1043.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1044.    case GL_TEXTURE_1D_ARRAY:
  1045.    case GL_PROXY_TEXTURE_1D_ARRAY:
  1046.    case GL_TEXTURE_EXTERNAL_OES:
  1047.    case GL_TEXTURE_2D_MULTISAMPLE:
  1048.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1049.       return 2;
  1050.    case GL_TEXTURE_3D:
  1051.    case GL_PROXY_TEXTURE_3D:
  1052.    case GL_TEXTURE_2D_ARRAY:
  1053.    case GL_PROXY_TEXTURE_2D_ARRAY:
  1054.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1055.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1056.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1057.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1058.       return 3;
  1059.    case GL_TEXTURE_BUFFER:
  1060.       /* fall-through */
  1061.    default:
  1062.       _mesa_problem(NULL, "invalid target 0x%x in get_texture_dimensions()",
  1063.                     target);
  1064.       return 2;
  1065.    }
  1066. }
  1067.  
  1068.  
  1069. /**
  1070.  * Check if a texture target can have more than one layer.
  1071.  */
  1072. GLboolean
  1073. _mesa_tex_target_is_layered(GLenum target)
  1074. {
  1075.    switch (target) {
  1076.    case GL_TEXTURE_1D:
  1077.    case GL_PROXY_TEXTURE_1D:
  1078.    case GL_TEXTURE_2D:
  1079.    case GL_PROXY_TEXTURE_2D:
  1080.    case GL_TEXTURE_RECTANGLE:
  1081.    case GL_PROXY_TEXTURE_RECTANGLE:
  1082.    case GL_TEXTURE_2D_MULTISAMPLE:
  1083.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1084.    case GL_TEXTURE_BUFFER:
  1085.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1086.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1087.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1088.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1089.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1090.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1091.    case GL_TEXTURE_EXTERNAL_OES:
  1092.       return GL_FALSE;
  1093.  
  1094.    case GL_TEXTURE_3D:
  1095.    case GL_PROXY_TEXTURE_3D:
  1096.    case GL_TEXTURE_CUBE_MAP:
  1097.    case GL_PROXY_TEXTURE_CUBE_MAP:
  1098.    case GL_TEXTURE_1D_ARRAY:
  1099.    case GL_PROXY_TEXTURE_1D_ARRAY:
  1100.    case GL_TEXTURE_2D_ARRAY:
  1101.    case GL_PROXY_TEXTURE_2D_ARRAY:
  1102.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1103.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1104.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1105.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1106.       return GL_TRUE;
  1107.  
  1108.    default:
  1109.       assert(!"Invalid texture target.");
  1110.       return GL_FALSE;
  1111.    }
  1112. }
  1113.  
  1114.  
  1115. /**
  1116.  * Return the number of layers present in the given level of an array,
  1117.  * cubemap or 3D texture.  If the texture is not layered return zero.
  1118.  */
  1119. GLuint
  1120. _mesa_get_texture_layers(const struct gl_texture_object *texObj, GLint level)
  1121. {
  1122.    assert(level >= 0 && level < MAX_TEXTURE_LEVELS);
  1123.  
  1124.    switch (texObj->Target) {
  1125.    case GL_TEXTURE_1D:
  1126.    case GL_TEXTURE_2D:
  1127.    case GL_TEXTURE_RECTANGLE:
  1128.    case GL_TEXTURE_2D_MULTISAMPLE:
  1129.    case GL_TEXTURE_BUFFER:
  1130.    case GL_TEXTURE_EXTERNAL_OES:
  1131.       return 0;
  1132.  
  1133.    case GL_TEXTURE_CUBE_MAP:
  1134.       return 6;
  1135.  
  1136.    case GL_TEXTURE_1D_ARRAY: {
  1137.       struct gl_texture_image *img = texObj->Image[0][level];
  1138.       return img ? img->Height : 0;
  1139.    }
  1140.  
  1141.    case GL_TEXTURE_3D:
  1142.    case GL_TEXTURE_2D_ARRAY:
  1143.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1144.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: {
  1145.       struct gl_texture_image *img = texObj->Image[0][level];
  1146.       return img ? img->Depth : 0;
  1147.    }
  1148.  
  1149.    default:
  1150.       assert(!"Invalid texture target.");
  1151.       return 0;
  1152.    }
  1153. }
  1154.  
  1155.  
  1156. /**
  1157.  * Return the maximum number of mipmap levels for the given target
  1158.  * and the dimensions.
  1159.  * The dimensions are expected not to include the border.
  1160.  */
  1161. GLsizei
  1162. _mesa_get_tex_max_num_levels(GLenum target, GLsizei width, GLsizei height,
  1163.                              GLsizei depth)
  1164. {
  1165.    GLsizei size;
  1166.  
  1167.    switch (target) {
  1168.    case GL_TEXTURE_1D:
  1169.    case GL_TEXTURE_1D_ARRAY:
  1170.    case GL_PROXY_TEXTURE_1D:
  1171.    case GL_PROXY_TEXTURE_1D_ARRAY:
  1172.       size = width;
  1173.       break;
  1174.    case GL_TEXTURE_CUBE_MAP:
  1175.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1176.    case GL_PROXY_TEXTURE_CUBE_MAP:
  1177.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1178.       size = width;
  1179.       break;
  1180.    case GL_TEXTURE_2D:
  1181.    case GL_TEXTURE_2D_ARRAY:
  1182.    case GL_PROXY_TEXTURE_2D:
  1183.    case GL_PROXY_TEXTURE_2D_ARRAY:
  1184.       size = MAX2(width, height);
  1185.       break;
  1186.    case GL_TEXTURE_3D:
  1187.    case GL_PROXY_TEXTURE_3D:
  1188.       size = MAX3(width, height, depth);
  1189.       break;
  1190.    case GL_TEXTURE_RECTANGLE:
  1191.    case GL_TEXTURE_EXTERNAL_OES:
  1192.    case GL_TEXTURE_2D_MULTISAMPLE:
  1193.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1194.    case GL_PROXY_TEXTURE_RECTANGLE:
  1195.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1196.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1197.       return 1;
  1198.    default:
  1199.       assert(0);
  1200.       return 1;
  1201.    }
  1202.  
  1203.    return _mesa_logbase2(size) + 1;
  1204. }
  1205.  
  1206.  
  1207. #if 000 /* not used anymore */
  1208. /*
  1209.  * glTexImage[123]D can accept a NULL image pointer.  In this case we
  1210.  * create a texture image with unspecified image contents per the OpenGL
  1211.  * spec.
  1212.  */
  1213. static GLubyte *
  1214. make_null_texture(GLint width, GLint height, GLint depth, GLenum format)
  1215. {
  1216.    const GLint components = _mesa_components_in_format(format);
  1217.    const GLint numPixels = width * height * depth;
  1218.    GLubyte *data = (GLubyte *) malloc(numPixels * components * sizeof(GLubyte));
  1219.  
  1220. #ifdef DEBUG
  1221.    /*
  1222.     * Let's see if anyone finds this.  If glTexImage2D() is called with
  1223.     * a NULL image pointer then load the texture image with something
  1224.     * interesting instead of leaving it indeterminate.
  1225.     */
  1226.    if (data) {
  1227.       static const char message[8][32] = {
  1228.          "   X   X  XXXXX   XXX     X    ",
  1229.          "   XX XX  X      X   X   X X   ",
  1230.          "   X X X  X      X      X   X  ",
  1231.          "   X   X  XXXX    XXX   XXXXX  ",
  1232.          "   X   X  X          X  X   X  ",
  1233.          "   X   X  X      X   X  X   X  ",
  1234.          "   X   X  XXXXX   XXX   X   X  ",
  1235.          "                               "
  1236.       };
  1237.  
  1238.       GLubyte *imgPtr = data;
  1239.       GLint h, i, j, k;
  1240.       for (h = 0; h < depth; h++) {
  1241.          for (i = 0; i < height; i++) {
  1242.             GLint srcRow = 7 - (i % 8);
  1243.             for (j = 0; j < width; j++) {
  1244.                GLint srcCol = j % 32;
  1245.                GLubyte texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
  1246.                for (k = 0; k < components; k++) {
  1247.                   *imgPtr++ = texel;
  1248.                }
  1249.             }
  1250.          }
  1251.       }
  1252.    }
  1253. #endif
  1254.  
  1255.    return data;
  1256. }
  1257. #endif
  1258.  
  1259.  
  1260.  
  1261. /**
  1262.  * Set the size and format-related fields of a gl_texture_image struct
  1263.  * to zero.  This is used when a proxy texture test fails.
  1264.  */
  1265. static void
  1266. clear_teximage_fields(struct gl_texture_image *img)
  1267. {
  1268.    assert(img);
  1269.    img->_BaseFormat = 0;
  1270.    img->InternalFormat = 0;
  1271.    img->Border = 0;
  1272.    img->Width = 0;
  1273.    img->Height = 0;
  1274.    img->Depth = 0;
  1275.    img->Width2 = 0;
  1276.    img->Height2 = 0;
  1277.    img->Depth2 = 0;
  1278.    img->WidthLog2 = 0;
  1279.    img->HeightLog2 = 0;
  1280.    img->DepthLog2 = 0;
  1281.    img->TexFormat = MESA_FORMAT_NONE;
  1282.    img->NumSamples = 0;
  1283.    img->FixedSampleLocations = GL_TRUE;
  1284. }
  1285.  
  1286.  
  1287. /**
  1288.  * Initialize basic fields of the gl_texture_image struct.
  1289.  *
  1290.  * \param ctx GL context.
  1291.  * \param img texture image structure to be initialized.
  1292.  * \param width image width.
  1293.  * \param height image height.
  1294.  * \param depth image depth.
  1295.  * \param border image border.
  1296.  * \param internalFormat internal format.
  1297.  * \param format  the actual hardware format (one of MESA_FORMAT_*)
  1298.  * \param numSamples  number of samples per texel, or zero for non-MS.
  1299.  * \param fixedSampleLocations  are sample locations fixed?
  1300.  *
  1301.  * Fills in the fields of \p img with the given information.
  1302.  * Note: width, height and depth include the border.
  1303.  */
  1304. static void
  1305. init_teximage_fields_ms(struct gl_context *ctx,
  1306.                         struct gl_texture_image *img,
  1307.                         GLsizei width, GLsizei height, GLsizei depth,
  1308.                         GLint border, GLenum internalFormat,
  1309.                         mesa_format format,
  1310.                         GLuint numSamples, GLboolean fixedSampleLocations)
  1311. {
  1312.    GLenum target;
  1313.    assert(img);
  1314.    assert(width >= 0);
  1315.    assert(height >= 0);
  1316.    assert(depth >= 0);
  1317.  
  1318.    target = img->TexObject->Target;
  1319.    img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
  1320.    assert(img->_BaseFormat != -1);
  1321.    img->InternalFormat = internalFormat;
  1322.    img->Border = border;
  1323.    img->Width = width;
  1324.    img->Height = height;
  1325.    img->Depth = depth;
  1326.  
  1327.    img->Width2 = width - 2 * border;   /* == 1 << img->WidthLog2; */
  1328.    img->WidthLog2 = _mesa_logbase2(img->Width2);
  1329.  
  1330.    img->NumSamples = 0;
  1331.    img->FixedSampleLocations = GL_TRUE;
  1332.  
  1333.    switch(target) {
  1334.    case GL_TEXTURE_1D:
  1335.    case GL_TEXTURE_BUFFER:
  1336.    case GL_PROXY_TEXTURE_1D:
  1337.       if (height == 0)
  1338.          img->Height2 = 0;
  1339.       else
  1340.          img->Height2 = 1;
  1341.       img->HeightLog2 = 0;
  1342.       if (depth == 0)
  1343.          img->Depth2 = 0;
  1344.       else
  1345.          img->Depth2 = 1;
  1346.       img->DepthLog2 = 0;
  1347.       break;
  1348.    case GL_TEXTURE_1D_ARRAY:
  1349.    case GL_PROXY_TEXTURE_1D_ARRAY:
  1350.       img->Height2 = height; /* no border */
  1351.       img->HeightLog2 = 0; /* not used */
  1352.       if (depth == 0)
  1353.          img->Depth2 = 0;
  1354.       else
  1355.          img->Depth2 = 1;
  1356.       img->DepthLog2 = 0;
  1357.       break;
  1358.    case GL_TEXTURE_2D:
  1359.    case GL_TEXTURE_RECTANGLE:
  1360.    case GL_TEXTURE_CUBE_MAP:
  1361.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1362.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1363.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1364.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1365.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1366.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1367.    case GL_TEXTURE_EXTERNAL_OES:
  1368.    case GL_PROXY_TEXTURE_2D:
  1369.    case GL_PROXY_TEXTURE_RECTANGLE:
  1370.    case GL_PROXY_TEXTURE_CUBE_MAP:
  1371.    case GL_TEXTURE_2D_MULTISAMPLE:
  1372.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  1373.       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
  1374.       img->HeightLog2 = _mesa_logbase2(img->Height2);
  1375.       if (depth == 0)
  1376.          img->Depth2 = 0;
  1377.       else
  1378.          img->Depth2 = 1;
  1379.       img->DepthLog2 = 0;
  1380.       break;
  1381.    case GL_TEXTURE_2D_ARRAY:
  1382.    case GL_PROXY_TEXTURE_2D_ARRAY:
  1383.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1384.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1385.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1386.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1387.       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
  1388.       img->HeightLog2 = _mesa_logbase2(img->Height2);
  1389.       img->Depth2 = depth; /* no border */
  1390.       img->DepthLog2 = 0; /* not used */
  1391.       break;
  1392.    case GL_TEXTURE_3D:
  1393.    case GL_PROXY_TEXTURE_3D:
  1394.       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
  1395.       img->HeightLog2 = _mesa_logbase2(img->Height2);
  1396.       img->Depth2 = depth - 2 * border;   /* == 1 << img->DepthLog2; */
  1397.       img->DepthLog2 = _mesa_logbase2(img->Depth2);
  1398.       break;
  1399.    default:
  1400.       _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
  1401.                     target);
  1402.    }
  1403.  
  1404.    img->MaxNumLevels =
  1405.       _mesa_get_tex_max_num_levels(target,
  1406.                                    img->Width2, img->Height2, img->Depth2);
  1407.    img->TexFormat = format;
  1408.    img->NumSamples = numSamples;
  1409.    img->FixedSampleLocations = fixedSampleLocations;
  1410. }
  1411.  
  1412.  
  1413. void
  1414. _mesa_init_teximage_fields(struct gl_context *ctx,
  1415.                            struct gl_texture_image *img,
  1416.                            GLsizei width, GLsizei height, GLsizei depth,
  1417.                            GLint border, GLenum internalFormat,
  1418.                            mesa_format format)
  1419. {
  1420.    init_teximage_fields_ms(ctx, img, width, height, depth, border,
  1421.                            internalFormat, format, 0, GL_TRUE);
  1422. }
  1423.  
  1424.  
  1425. /**
  1426.  * Free and clear fields of the gl_texture_image struct.
  1427.  *
  1428.  * \param ctx GL context.
  1429.  * \param texImage texture image structure to be cleared.
  1430.  *
  1431.  * After the call, \p texImage will have no data associated with it.  Its
  1432.  * fields are cleared so that its parent object will test incomplete.
  1433.  */
  1434. void
  1435. _mesa_clear_texture_image(struct gl_context *ctx,
  1436.                           struct gl_texture_image *texImage)
  1437. {
  1438.    ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  1439.    clear_teximage_fields(texImage);
  1440. }
  1441.  
  1442.  
  1443. /**
  1444.  * Check the width, height, depth and border of a texture image are legal.
  1445.  * Used by all the glTexImage, glCompressedTexImage and glCopyTexImage
  1446.  * functions.
  1447.  * The target and level parameters will have already been validated.
  1448.  * \return GL_TRUE if size is OK, GL_FALSE otherwise.
  1449.  */
  1450. GLboolean
  1451. _mesa_legal_texture_dimensions(struct gl_context *ctx, GLenum target,
  1452.                                GLint level, GLint width, GLint height,
  1453.                                GLint depth, GLint border)
  1454. {
  1455.    GLint maxSize;
  1456.  
  1457.    switch (target) {
  1458.    case GL_TEXTURE_1D:
  1459.    case GL_PROXY_TEXTURE_1D:
  1460.       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1); /* level zero size */
  1461.       maxSize >>= level;  /* level size */
  1462.       if (width < 2 * border || width > 2 * border + maxSize)
  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:
  1471.    case GL_PROXY_TEXTURE_2D:
  1472.    case GL_TEXTURE_2D_MULTISAMPLE:
  1473.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  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 (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1481.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1482.             return GL_FALSE;
  1483.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1484.             return GL_FALSE;
  1485.       }
  1486.       return GL_TRUE;
  1487.  
  1488.    case GL_TEXTURE_3D:
  1489.    case GL_PROXY_TEXTURE_3D:
  1490.       maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
  1491.       maxSize >>= level;
  1492.       if (width < 2 * border || width > 2 * border + maxSize)
  1493.          return GL_FALSE;
  1494.       if (height < 2 * border || height > 2 * border + maxSize)
  1495.          return GL_FALSE;
  1496.       if (depth < 2 * border || depth > 2 * border + maxSize)
  1497.          return GL_FALSE;
  1498.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1499.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1500.             return GL_FALSE;
  1501.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1502.             return GL_FALSE;
  1503.          if (depth > 0 && !_mesa_is_pow_two(depth - 2 * border))
  1504.             return GL_FALSE;
  1505.       }
  1506.       return GL_TRUE;
  1507.  
  1508.    case GL_TEXTURE_RECTANGLE_NV:
  1509.    case GL_PROXY_TEXTURE_RECTANGLE_NV:
  1510.       if (level != 0)
  1511.          return GL_FALSE;
  1512.       maxSize = ctx->Const.MaxTextureRectSize;
  1513.       if (width < 0 || width > maxSize)
  1514.          return GL_FALSE;
  1515.       if (height < 0 || height > maxSize)
  1516.          return GL_FALSE;
  1517.       return GL_TRUE;
  1518.  
  1519.    case GL_TEXTURE_CUBE_MAP:
  1520.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1521.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1522.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1523.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1524.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1525.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1526.    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
  1527.       maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
  1528.       maxSize >>= level;
  1529.       if (width != height)
  1530.          return GL_FALSE;
  1531.       if (width < 2 * border || width > 2 * border + maxSize)
  1532.          return GL_FALSE;
  1533.       if (height < 2 * border || height > 2 * border + maxSize)
  1534.          return GL_FALSE;
  1535.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1536.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1537.             return GL_FALSE;
  1538.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1539.             return GL_FALSE;
  1540.       }
  1541.       return GL_TRUE;
  1542.  
  1543.    case GL_TEXTURE_1D_ARRAY_EXT:
  1544.    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  1545.       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
  1546.       maxSize >>= level;
  1547.       if (width < 2 * border || width > 2 * border + maxSize)
  1548.          return GL_FALSE;
  1549.       if (height < 0 || height > ctx->Const.MaxArrayTextureLayers)
  1550.          return GL_FALSE;
  1551.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1552.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1553.             return GL_FALSE;
  1554.       }
  1555.       return GL_TRUE;
  1556.  
  1557.    case GL_TEXTURE_2D_ARRAY_EXT:
  1558.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  1559.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1560.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  1561.       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
  1562.       maxSize >>= level;
  1563.       if (width < 2 * border || width > 2 * border + maxSize)
  1564.          return GL_FALSE;
  1565.       if (height < 2 * border || height > 2 * border + maxSize)
  1566.          return GL_FALSE;
  1567.       if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers)
  1568.          return GL_FALSE;
  1569.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1570.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1571.             return GL_FALSE;
  1572.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1573.             return GL_FALSE;
  1574.       }
  1575.       return GL_TRUE;
  1576.  
  1577.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1578.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1579.       maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
  1580.       if (width < 2 * border || width > 2 * border + maxSize)
  1581.          return GL_FALSE;
  1582.       if (height < 2 * border || height > 2 * border + maxSize)
  1583.          return GL_FALSE;
  1584.       if (depth < 0 || depth > ctx->Const.MaxArrayTextureLayers || depth % 6)
  1585.          return GL_FALSE;
  1586.       if (width != height)
  1587.          return GL_FALSE;
  1588.       if (level >= ctx->Const.MaxCubeTextureLevels)
  1589.          return GL_FALSE;
  1590.       if (!ctx->Extensions.ARB_texture_non_power_of_two) {
  1591.          if (width > 0 && !_mesa_is_pow_two(width - 2 * border))
  1592.             return GL_FALSE;
  1593.          if (height > 0 && !_mesa_is_pow_two(height - 2 * border))
  1594.             return GL_FALSE;
  1595.       }
  1596.       return GL_TRUE;
  1597.    default:
  1598.       _mesa_problem(ctx, "Invalid target in _mesa_legal_texture_dimensions()");
  1599.       return GL_FALSE;
  1600.    }
  1601. }
  1602.  
  1603.  
  1604. /**
  1605.  * Do error checking of xoffset, yoffset, zoffset, width, height and depth
  1606.  * for glTexSubImage, glCopyTexSubImage and glCompressedTexSubImage.
  1607.  * \param destImage  the destination texture image.
  1608.  * \return GL_TRUE if error found, GL_FALSE otherwise.
  1609.  */
  1610. static GLboolean
  1611. error_check_subtexture_dimensions(struct gl_context *ctx, GLuint dims,
  1612.                                   const struct gl_texture_image *destImage,
  1613.                                   GLint xoffset, GLint yoffset, GLint zoffset,
  1614.                                   GLsizei subWidth, GLsizei subHeight,
  1615.                                   GLsizei subDepth, const char *func)
  1616. {
  1617.    const GLenum target = destImage->TexObject->Target;
  1618.    GLuint bw, bh;
  1619.  
  1620.    /* Check size */
  1621.    if (subWidth < 0) {
  1622.       _mesa_error(ctx, GL_INVALID_VALUE,
  1623.                   "%s(width=%d)", func, subWidth);
  1624.       return GL_TRUE;
  1625.    }
  1626.  
  1627.    if (dims > 1 && subHeight < 0) {
  1628.       _mesa_error(ctx, GL_INVALID_VALUE,
  1629.                   "%s(height=%d)", func, subHeight);
  1630.       return GL_TRUE;
  1631.    }
  1632.  
  1633.    if (dims > 2 && subDepth < 0) {
  1634.       _mesa_error(ctx, GL_INVALID_VALUE,
  1635.                   "%s(depth=%d)", func, subDepth);
  1636.       return GL_TRUE;
  1637.    }
  1638.  
  1639.    /* check xoffset and width */
  1640.    if (xoffset < - (GLint) destImage->Border) {
  1641.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset)", func);
  1642.       return GL_TRUE;
  1643.    }
  1644.  
  1645.    if (xoffset + subWidth > (GLint) destImage->Width) {
  1646.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset+width)", func);
  1647.       return GL_TRUE;
  1648.    }
  1649.  
  1650.    /* check yoffset and height */
  1651.    if (dims > 1) {
  1652.       GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destImage->Border;
  1653.       if (yoffset < -yBorder) {
  1654.          _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset)", func);
  1655.          return GL_TRUE;
  1656.       }
  1657.       if (yoffset + subHeight > (GLint) destImage->Height) {
  1658.          _mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset+height)", func);
  1659.          return GL_TRUE;
  1660.       }
  1661.    }
  1662.  
  1663.    /* check zoffset and depth */
  1664.    if (dims > 2) {
  1665.       GLint depth;
  1666.       GLint zBorder = (target == GL_TEXTURE_2D_ARRAY ||
  1667.                        target == GL_TEXTURE_CUBE_MAP_ARRAY) ?
  1668.                          0 : destImage->Border;
  1669.  
  1670.       if (zoffset < -zBorder) {
  1671.          _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset)", func);
  1672.          return GL_TRUE;
  1673.       }
  1674.  
  1675.       depth = (GLint) destImage->Depth;
  1676.       if (target == GL_TEXTURE_CUBE_MAP)
  1677.          depth = 6;
  1678.       if (zoffset + subDepth  > depth) {
  1679.          _mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset+depth)", func);
  1680.          return GL_TRUE;
  1681.       }
  1682.    }
  1683.  
  1684.    /*
  1685.     * The OpenGL spec (and GL_ARB_texture_compression) says only whole
  1686.     * compressed texture images can be updated.  But, that restriction may be
  1687.     * relaxed for particular compressed formats.  At this time, all the
  1688.     * compressed formats supported by Mesa allow sub-textures to be updated
  1689.     * along compressed block boundaries.
  1690.     */
  1691.    _mesa_get_format_block_size(destImage->TexFormat, &bw, &bh);
  1692.  
  1693.    if (bw != 1 || bh != 1) {
  1694.       /* offset must be multiple of block size */
  1695.       if ((xoffset % bw != 0) || (yoffset % bh != 0)) {
  1696.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1697.                      "%s(xoffset = %d, yoffset = %d)",
  1698.                      func, xoffset, yoffset);
  1699.          return GL_TRUE;
  1700.       }
  1701.  
  1702.       /* The size must be a multiple of bw x bh, or we must be using a
  1703.        * offset+size that exactly hits the edge of the image.  This
  1704.        * is important for small mipmap levels (1x1, 2x1, etc) and for
  1705.        * NPOT textures.
  1706.        */
  1707.       if ((subWidth % bw != 0) &&
  1708.           (xoffset + subWidth != (GLint) destImage->Width)) {
  1709.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1710.                      "%s(width = %d)", func, subWidth);
  1711.          return GL_TRUE;
  1712.       }
  1713.  
  1714.       if ((subHeight % bh != 0) &&
  1715.           (yoffset + subHeight != (GLint) destImage->Height)) {
  1716.          _mesa_error(ctx, GL_INVALID_OPERATION,
  1717.                      "%s(height = %d)", func, subHeight);
  1718.          return GL_TRUE;
  1719.       }
  1720.    }
  1721.  
  1722.    return GL_FALSE;
  1723. }
  1724.  
  1725.  
  1726.  
  1727.  
  1728. /**
  1729.  * This is the fallback for Driver.TestProxyTexImage() for doing device-
  1730.  * specific texture image size checks.
  1731.  *
  1732.  * A hardware driver might override this function if, for example, the
  1733.  * max 3D texture size is 512x512x64 (i.e. not a cube).
  1734.  *
  1735.  * Note that width, height, depth == 0 is not an error.  However, a
  1736.  * texture with zero width/height/depth will be considered "incomplete"
  1737.  * and texturing will effectively be disabled.
  1738.  *
  1739.  * \param target  any texture target/type
  1740.  * \param level  as passed to glTexImage
  1741.  * \param format  the MESA_FORMAT_x for the tex image
  1742.  * \param width  as passed to glTexImage
  1743.  * \param height  as passed to glTexImage
  1744.  * \param depth  as passed to glTexImage
  1745.  * \param border  as passed to glTexImage
  1746.  * \return GL_TRUE if the image is acceptable, GL_FALSE if not acceptable.
  1747.  */
  1748. GLboolean
  1749. _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
  1750.                           mesa_format format,
  1751.                           GLint width, GLint height, GLint depth, GLint border)
  1752. {
  1753.    /* We just check if the image size is less than MaxTextureMbytes.
  1754.     * Some drivers may do more specific checks.
  1755.     */
  1756.    uint64_t bytes = _mesa_format_image_size64(format, width, height, depth);
  1757.    uint64_t mbytes = bytes / (1024 * 1024); /* convert to MB */
  1758.    mbytes *= _mesa_num_tex_faces(target);
  1759.    return mbytes <= (uint64_t) ctx->Const.MaxTextureMbytes;
  1760. }
  1761.  
  1762.  
  1763. /**
  1764.  * Return true if the format is only valid for glCompressedTexImage.
  1765.  */
  1766. static GLboolean
  1767. compressedteximage_only_format(const struct gl_context *ctx, GLenum format)
  1768. {
  1769.    switch (format) {
  1770.    case GL_ETC1_RGB8_OES:
  1771.    case GL_PALETTE4_RGB8_OES:
  1772.    case GL_PALETTE4_RGBA8_OES:
  1773.    case GL_PALETTE4_R5_G6_B5_OES:
  1774.    case GL_PALETTE4_RGBA4_OES:
  1775.    case GL_PALETTE4_RGB5_A1_OES:
  1776.    case GL_PALETTE8_RGB8_OES:
  1777.    case GL_PALETTE8_RGBA8_OES:
  1778.    case GL_PALETTE8_R5_G6_B5_OES:
  1779.    case GL_PALETTE8_RGBA4_OES:
  1780.    case GL_PALETTE8_RGB5_A1_OES:
  1781.       return GL_TRUE;
  1782.    default:
  1783.       return GL_FALSE;
  1784.    }
  1785. }
  1786.  
  1787.  
  1788. /**
  1789.  * Helper function to determine whether a target and specific compression
  1790.  * format are supported.
  1791.  */
  1792. GLboolean
  1793. _mesa_target_can_be_compressed(const struct gl_context *ctx, GLenum target,
  1794.                                GLenum intFormat)
  1795. {
  1796.    (void) intFormat;  /* not used yet */
  1797.  
  1798.    switch (target) {
  1799.    case GL_TEXTURE_2D:
  1800.    case GL_PROXY_TEXTURE_2D:
  1801.       return GL_TRUE; /* true for any compressed format so far */
  1802.    case GL_PROXY_TEXTURE_CUBE_MAP:
  1803.    case GL_TEXTURE_CUBE_MAP:
  1804.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1805.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1806.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1807.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1808.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1809.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1810.       return ctx->Extensions.ARB_texture_cube_map;
  1811.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  1812.    case GL_TEXTURE_2D_ARRAY_EXT:
  1813.       return ctx->Extensions.EXT_texture_array;
  1814.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1815.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  1816.       return ctx->Extensions.ARB_texture_cube_map_array;
  1817.    default:
  1818.       return GL_FALSE;
  1819.    }
  1820. }
  1821.  
  1822.  
  1823. /**
  1824.  * Check if the given texture target value is legal for a
  1825.  * glTexImage1/2/3D call.
  1826.  */
  1827. static GLboolean
  1828. legal_teximage_target(struct gl_context *ctx, GLuint dims, GLenum target)
  1829. {
  1830.    switch (dims) {
  1831.    case 1:
  1832.       switch (target) {
  1833.       case GL_TEXTURE_1D:
  1834.       case GL_PROXY_TEXTURE_1D:
  1835.          return _mesa_is_desktop_gl(ctx);
  1836.       default:
  1837.          return GL_FALSE;
  1838.       }
  1839.    case 2:
  1840.       switch (target) {
  1841.       case GL_TEXTURE_2D:
  1842.          return GL_TRUE;
  1843.       case GL_PROXY_TEXTURE_2D:
  1844.          return _mesa_is_desktop_gl(ctx);
  1845.       case GL_PROXY_TEXTURE_CUBE_MAP:
  1846.          return _mesa_is_desktop_gl(ctx)
  1847.             && ctx->Extensions.ARB_texture_cube_map;
  1848.       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1849.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1850.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1851.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1852.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1853.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1854.          return ctx->Extensions.ARB_texture_cube_map;
  1855.       case GL_TEXTURE_RECTANGLE_NV:
  1856.       case GL_PROXY_TEXTURE_RECTANGLE_NV:
  1857.          return _mesa_is_desktop_gl(ctx)
  1858.             && ctx->Extensions.NV_texture_rectangle;
  1859.       case GL_TEXTURE_1D_ARRAY_EXT:
  1860.       case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  1861.          return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
  1862.       default:
  1863.          return GL_FALSE;
  1864.       }
  1865.    case 3:
  1866.       switch (target) {
  1867.       case GL_TEXTURE_3D:
  1868.          return GL_TRUE;
  1869.       case GL_PROXY_TEXTURE_3D:
  1870.          return _mesa_is_desktop_gl(ctx);
  1871.       case GL_TEXTURE_2D_ARRAY_EXT:
  1872.          return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
  1873.             || _mesa_is_gles3(ctx);
  1874.       case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  1875.          return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
  1876.       case GL_TEXTURE_CUBE_MAP_ARRAY:
  1877.       case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1878.          return ctx->Extensions.ARB_texture_cube_map_array;
  1879.       default:
  1880.          return GL_FALSE;
  1881.       }
  1882.    default:
  1883.       _mesa_problem(ctx, "invalid dims=%u in legal_teximage_target()", dims);
  1884.       return GL_FALSE;
  1885.    }
  1886. }
  1887.  
  1888.  
  1889. /**
  1890.  * Check if the given texture target value is legal for a
  1891.  * glTexSubImage, glCopyTexSubImage or glCopyTexImage call.
  1892.  * The difference compared to legal_teximage_target() above is that
  1893.  * proxy targets are not supported.
  1894.  */
  1895. static GLboolean
  1896. legal_texsubimage_target(struct gl_context *ctx, GLuint dims, GLenum target,
  1897.                          bool dsa)
  1898. {
  1899.    switch (dims) {
  1900.    case 1:
  1901.       return _mesa_is_desktop_gl(ctx) && target == GL_TEXTURE_1D;
  1902.    case 2:
  1903.       switch (target) {
  1904.       case GL_TEXTURE_2D:
  1905.          return GL_TRUE;
  1906.       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  1907.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  1908.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  1909.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  1910.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  1911.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  1912.          return ctx->Extensions.ARB_texture_cube_map;
  1913.       case GL_TEXTURE_RECTANGLE_NV:
  1914.          return _mesa_is_desktop_gl(ctx)
  1915.             && ctx->Extensions.NV_texture_rectangle;
  1916.       case GL_TEXTURE_1D_ARRAY_EXT:
  1917.          return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array;
  1918.       default:
  1919.          return GL_FALSE;
  1920.       }
  1921.    case 3:
  1922.       switch (target) {
  1923.       case GL_TEXTURE_3D:
  1924.          return GL_TRUE;
  1925.       case GL_TEXTURE_2D_ARRAY_EXT:
  1926.          return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
  1927.             || _mesa_is_gles3(ctx);
  1928.       case GL_TEXTURE_CUBE_MAP_ARRAY:
  1929.       case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  1930.          return ctx->Extensions.ARB_texture_cube_map_array;
  1931.  
  1932.       /* Table 8.15 of the OpenGL 4.5 core profile spec
  1933.        * (20141030) says that TEXTURE_CUBE_MAP is valid for TextureSubImage3D
  1934.        * and CopyTextureSubImage3D.
  1935.        */
  1936.       case GL_TEXTURE_CUBE_MAP:
  1937.          return dsa;
  1938.       default:
  1939.          return GL_FALSE;
  1940.       }
  1941.    default:
  1942.       _mesa_problem(ctx, "invalid dims=%u in legal_texsubimage_target()",
  1943.                     dims);
  1944.       return GL_FALSE;
  1945.    }
  1946. }
  1947.  
  1948.  
  1949. /**
  1950.  * Helper function to determine if a texture object is mutable (in terms
  1951.  * of GL_ARB_texture_storage).
  1952.  */
  1953. static GLboolean
  1954. mutable_tex_object(struct gl_context *ctx, GLenum target)
  1955. {
  1956.    struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
  1957.    if (!texObj)
  1958.       return GL_FALSE;
  1959.  
  1960.    return !texObj->Immutable;
  1961. }
  1962.  
  1963.  
  1964. /**
  1965.  * Return expected size of a compressed texture.
  1966.  */
  1967. static GLuint
  1968. compressed_tex_size(GLsizei width, GLsizei height, GLsizei depth,
  1969.                     GLenum glformat)
  1970. {
  1971.    mesa_format mesaFormat = _mesa_glenum_to_compressed_format(glformat);
  1972.    return _mesa_format_image_size(mesaFormat, width, height, depth);
  1973. }
  1974.  
  1975. /**
  1976.  * Verify that a texture format is valid with a particular target
  1977.  *
  1978.  * In particular, textures with base format of \c GL_DEPTH_COMPONENT or
  1979.  * \c GL_DEPTH_STENCIL are only valid with certain, context dependent texture
  1980.  * targets.
  1981.  *
  1982.  * \param ctx             GL context
  1983.  * \param target          Texture target
  1984.  * \param internalFormat  Internal format of the texture image
  1985.  * \param dimensions      Dimensionality at the caller.  This is \b not used
  1986.  *                        in the validation.  It is only used when logging
  1987.  *                        error messages.
  1988.  * \param caller          Base name of the calling function (e.g.,
  1989.  *                        "glTexImage" or "glTexStorage").
  1990.  *
  1991.  * \returns true if the combination is legal, false otherwise.
  1992.  */
  1993. bool
  1994. _mesa_legal_texture_base_format_for_target(struct gl_context *ctx,
  1995.                                            GLenum target, GLenum internalFormat,
  1996.                                            unsigned dimensions,
  1997.                                            const char *caller)
  1998. {
  1999.    if (_mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_COMPONENT
  2000.        || _mesa_base_tex_format(ctx, internalFormat) == GL_DEPTH_STENCIL
  2001.        || _mesa_base_tex_format(ctx, internalFormat) == GL_STENCIL_INDEX) {
  2002.       /* Section 3.8.3 (Texture Image Specification) of the OpenGL 3.3 Core
  2003.        * Profile spec says:
  2004.        *
  2005.        *     "Textures with a base internal format of DEPTH_COMPONENT or
  2006.        *     DEPTH_STENCIL are supported by texture image specification
  2007.        *     commands only if target is TEXTURE_1D, TEXTURE_2D,
  2008.        *     TEXTURE_1D_ARRAY, TEXTURE_2D_ARRAY, TEXTURE_RECTANGLE,
  2009.        *     TEXTURE_CUBE_MAP, PROXY_TEXTURE_1D, PROXY_TEXTURE_2D,
  2010.        *     PROXY_TEXTURE_1D_ARRAY, PROXY_TEXTURE_2D_ARRAY,
  2011.        *     PROXY_TEXTURE_RECTANGLE, or PROXY_TEXTURE_CUBE_MAP. Using these
  2012.        *     formats in conjunction with any other target will result in an
  2013.        *     INVALID_OPERATION error."
  2014.        *
  2015.        * Cubemaps are only supported with desktop OpenGL version >= 3.0,
  2016.        * EXT_gpu_shader4, or, on OpenGL ES 2.0+, OES_depth_texture_cube_map.
  2017.        */
  2018.       if (target != GL_TEXTURE_1D &&
  2019.           target != GL_PROXY_TEXTURE_1D &&
  2020.           target != GL_TEXTURE_2D &&
  2021.           target != GL_PROXY_TEXTURE_2D &&
  2022.           target != GL_TEXTURE_1D_ARRAY &&
  2023.           target != GL_PROXY_TEXTURE_1D_ARRAY &&
  2024.           target != GL_TEXTURE_2D_ARRAY &&
  2025.           target != GL_PROXY_TEXTURE_2D_ARRAY &&
  2026.           target != GL_TEXTURE_RECTANGLE_ARB &&
  2027.           target != GL_PROXY_TEXTURE_RECTANGLE_ARB &&
  2028.          !((_mesa_is_cube_face(target) ||
  2029.             target == GL_TEXTURE_CUBE_MAP ||
  2030.             target == GL_PROXY_TEXTURE_CUBE_MAP) &&
  2031.            (ctx->Version >= 30 || ctx->Extensions.EXT_gpu_shader4
  2032.             || (ctx->API == API_OPENGLES2 && ctx->Extensions.OES_depth_texture_cube_map))) &&
  2033.           !((target == GL_TEXTURE_CUBE_MAP_ARRAY ||
  2034.              target == GL_PROXY_TEXTURE_CUBE_MAP_ARRAY) &&
  2035.             ctx->Extensions.ARB_texture_cube_map_array)) {
  2036.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2037.                      "%s%dD(bad target for depth texture)",
  2038.                      caller, dimensions);
  2039.          return false;
  2040.       }
  2041.    }
  2042.  
  2043.    return true;
  2044. }
  2045.  
  2046. static bool
  2047. texture_formats_agree(GLenum internalFormat,
  2048.                       GLenum format)
  2049. {
  2050.    GLboolean colorFormat;
  2051.    GLboolean is_format_depth_or_depthstencil;
  2052.    GLboolean is_internalFormat_depth_or_depthstencil;
  2053.  
  2054.    /* Even though there are no color-index textures, we still have to support
  2055.     * uploading color-index data and remapping it to RGB via the
  2056.     * GL_PIXEL_MAP_I_TO_[RGBA] tables.
  2057.     */
  2058.    const GLboolean indexFormat = (format == GL_COLOR_INDEX);
  2059.  
  2060.    is_internalFormat_depth_or_depthstencil =
  2061.       _mesa_is_depth_format(internalFormat) ||
  2062.       _mesa_is_depthstencil_format(internalFormat);
  2063.  
  2064.    is_format_depth_or_depthstencil =
  2065.       _mesa_is_depth_format(format) ||
  2066.       _mesa_is_depthstencil_format(format);
  2067.  
  2068.    colorFormat = _mesa_is_color_format(format);
  2069.  
  2070.    if (_mesa_is_color_format(internalFormat) && !colorFormat && !indexFormat)
  2071.       return false;
  2072.  
  2073.    if (is_internalFormat_depth_or_depthstencil !=
  2074.        is_format_depth_or_depthstencil)
  2075.       return false;
  2076.  
  2077.    if (_mesa_is_ycbcr_format(internalFormat) != _mesa_is_ycbcr_format(format))
  2078.       return false;
  2079.  
  2080.    return true;
  2081. }
  2082.  
  2083. /**
  2084.  * Test the glTexImage[123]D() parameters for errors.
  2085.  *
  2086.  * \param ctx GL context.
  2087.  * \param dimensions texture image dimensions (must be 1, 2 or 3).
  2088.  * \param target texture target given by the user (already validated).
  2089.  * \param level image level given by the user.
  2090.  * \param internalFormat internal format given by the user.
  2091.  * \param format pixel data format given by the user.
  2092.  * \param type pixel data type given by the user.
  2093.  * \param width image width given by the user.
  2094.  * \param height image height given by the user.
  2095.  * \param depth image depth given by the user.
  2096.  * \param border image border given by the user.
  2097.  *
  2098.  * \return GL_TRUE if a error is found, GL_FALSE otherwise
  2099.  *
  2100.  * Verifies each of the parameters against the constants specified in
  2101.  * __struct gl_contextRec::Const and the supported extensions, and according
  2102.  * to the OpenGL specification.
  2103.  * Note that we don't fully error-check the width, height, depth values
  2104.  * here.  That's done in _mesa_legal_texture_dimensions() which is used
  2105.  * by several other GL entrypoints.  Plus, texture dims have a special
  2106.  * interaction with proxy textures.
  2107.  */
  2108. static GLboolean
  2109. texture_error_check( struct gl_context *ctx,
  2110.                      GLuint dimensions, GLenum target,
  2111.                      GLint level, GLint internalFormat,
  2112.                      GLenum format, GLenum type,
  2113.                      GLint width, GLint height,
  2114.                      GLint depth, GLint border,
  2115.                      const GLvoid *pixels )
  2116. {
  2117.    GLenum err;
  2118.  
  2119.    /* Note: for proxy textures, some error conditions immediately generate
  2120.     * a GL error in the usual way.  But others do not generate a GL error.
  2121.     * Instead, they cause the width, height, depth, format fields of the
  2122.     * texture image to be zeroed-out.  The GL spec seems to indicate that the
  2123.     * zero-out behaviour is only used in cases related to memory allocation.
  2124.     */
  2125.  
  2126.    /* level check */
  2127.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  2128.       _mesa_error(ctx, GL_INVALID_VALUE,
  2129.                   "glTexImage%dD(level=%d)", dimensions, level);
  2130.       return GL_TRUE;
  2131.    }
  2132.  
  2133.    /* Check border */
  2134.    if (border < 0 || border > 1 ||
  2135.        ((ctx->API != API_OPENGL_COMPAT ||
  2136.          target == GL_TEXTURE_RECTANGLE_NV ||
  2137.          target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
  2138.       _mesa_error(ctx, GL_INVALID_VALUE,
  2139.                   "glTexImage%dD(border=%d)", dimensions, border);
  2140.       return GL_TRUE;
  2141.    }
  2142.  
  2143.    if (width < 0 || height < 0 || depth < 0) {
  2144.       _mesa_error(ctx, GL_INVALID_VALUE,
  2145.                   "glTexImage%dD(width, height or depth < 0)", dimensions);
  2146.       return GL_TRUE;
  2147.    }
  2148.  
  2149.    /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
  2150.     * combinations of format, internalFormat, and type that can be used.
  2151.     * Formats and types that require additional extensions (e.g., GL_FLOAT
  2152.     * requires GL_OES_texture_float) are filtered elsewhere.
  2153.     */
  2154.  
  2155.    if (_mesa_is_gles(ctx)) {
  2156.       if (_mesa_is_gles3(ctx)) {
  2157.          err = _mesa_es3_error_check_format_and_type(ctx, format, type,
  2158.                                                      internalFormat);
  2159.       } else {
  2160.          if (format != internalFormat) {
  2161.             _mesa_error(ctx, GL_INVALID_OPERATION,
  2162.                         "glTexImage%dD(format = %s, internalFormat = %s)",
  2163.                         dimensions,
  2164.                         _mesa_lookup_enum_by_nr(format),
  2165.                         _mesa_lookup_enum_by_nr(internalFormat));
  2166.             return GL_TRUE;
  2167.          }
  2168.  
  2169.          err = _mesa_es_error_check_format_and_type(format, type, dimensions);
  2170.       }
  2171.       if (err != GL_NO_ERROR) {
  2172.          _mesa_error(ctx, err,
  2173.                      "glTexImage%dD(format = %s, type = %s, internalFormat = %s)",
  2174.                      dimensions,
  2175.                      _mesa_lookup_enum_by_nr(format),
  2176.                      _mesa_lookup_enum_by_nr(type),
  2177.                      _mesa_lookup_enum_by_nr(internalFormat));
  2178.          return GL_TRUE;
  2179.       }
  2180.    }
  2181.  
  2182.    /* Check internalFormat */
  2183.    if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
  2184.       _mesa_error(ctx, GL_INVALID_VALUE,
  2185.                   "glTexImage%dD(internalFormat=%s)",
  2186.                   dimensions, _mesa_lookup_enum_by_nr(internalFormat));
  2187.       return GL_TRUE;
  2188.    }
  2189.  
  2190.    /* Check incoming image format and type */
  2191.    err = _mesa_error_check_format_and_type(ctx, format, type);
  2192.    if (err != GL_NO_ERROR) {
  2193.       _mesa_error(ctx, err,
  2194.                   "glTexImage%dD(incompatible format = %s, type = %s)",
  2195.                   dimensions, _mesa_lookup_enum_by_nr(format),
  2196.                   _mesa_lookup_enum_by_nr(type));
  2197.       return GL_TRUE;
  2198.    }
  2199.  
  2200.    /* validate the bound PBO, if any */
  2201.    if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
  2202.                                   width, height, depth, format, type,
  2203.                                   INT_MAX, pixels, "glTexImage")) {
  2204.       return GL_TRUE;
  2205.    }
  2206.  
  2207.    /* make sure internal format and format basically agree */
  2208.    if (!texture_formats_agree(internalFormat, format)) {
  2209.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2210.                   "glTexImage%dD(incompatible internalFormat = %s, format = %s)",
  2211.                   dimensions, _mesa_lookup_enum_by_nr(internalFormat),
  2212.                   _mesa_lookup_enum_by_nr(format));
  2213.       return GL_TRUE;
  2214.    }
  2215.  
  2216.    /* additional checks for ycbcr textures */
  2217.    if (internalFormat == GL_YCBCR_MESA) {
  2218.       assert(ctx->Extensions.MESA_ycbcr_texture);
  2219.       if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
  2220.           type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
  2221.          char message[100];
  2222.          _mesa_snprintf(message, sizeof(message),
  2223.                         "glTexImage%dD(format/type YCBCR mismatch)",
  2224.                         dimensions);
  2225.          _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
  2226.          return GL_TRUE; /* error */
  2227.       }
  2228.       if (target != GL_TEXTURE_2D &&
  2229.           target != GL_PROXY_TEXTURE_2D &&
  2230.           target != GL_TEXTURE_RECTANGLE_NV &&
  2231.           target != GL_PROXY_TEXTURE_RECTANGLE_NV) {
  2232.          _mesa_error(ctx, GL_INVALID_ENUM,
  2233.                      "glTexImage%dD(bad target for YCbCr texture)",
  2234.                      dimensions);
  2235.          return GL_TRUE;
  2236.       }
  2237.       if (border != 0) {
  2238.          char message[100];
  2239.          _mesa_snprintf(message, sizeof(message),
  2240.                         "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
  2241.                         dimensions, border);
  2242.          _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
  2243.          return GL_TRUE;
  2244.       }
  2245.    }
  2246.  
  2247.    /* additional checks for depth textures */
  2248.    if (!_mesa_legal_texture_base_format_for_target(ctx, target, internalFormat,
  2249.                                                    dimensions, "glTexImage"))
  2250.       return GL_TRUE;
  2251.  
  2252.    /* additional checks for compressed textures */
  2253.    if (_mesa_is_compressed_format(ctx, internalFormat)) {
  2254.       if (!_mesa_target_can_be_compressed(ctx, target, internalFormat)) {
  2255.          _mesa_error(ctx, GL_INVALID_ENUM,
  2256.                      "glTexImage%dD(target can't be compressed)", dimensions);
  2257.          return GL_TRUE;
  2258.       }
  2259.       if (compressedteximage_only_format(ctx, internalFormat)) {
  2260.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2261.                      "glTexImage%dD(no compression for format)", dimensions);
  2262.          return GL_TRUE;
  2263.       }
  2264.       if (border != 0) {
  2265.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2266.                      "glTexImage%dD(border!=0)", dimensions);
  2267.          return GL_TRUE;
  2268.       }
  2269.    }
  2270.  
  2271.    /* additional checks for integer textures */
  2272.    if ((ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) &&
  2273.        (_mesa_is_enum_format_integer(format) !=
  2274.         _mesa_is_enum_format_integer(internalFormat))) {
  2275.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2276.                   "glTexImage%dD(integer/non-integer format mismatch)",
  2277.                   dimensions);
  2278.       return GL_TRUE;
  2279.    }
  2280.  
  2281.    if (!mutable_tex_object(ctx, target)) {
  2282.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2283.                   "glTexImage%dD(immutable texture)", dimensions);
  2284.       return GL_TRUE;
  2285.    }
  2286.  
  2287.    /* if we get here, the parameters are OK */
  2288.    return GL_FALSE;
  2289. }
  2290.  
  2291.  
  2292. /**
  2293.  * Error checking for glCompressedTexImage[123]D().
  2294.  * Note that the width, height and depth values are not fully error checked
  2295.  * here.
  2296.  * \return GL_TRUE if a error is found, GL_FALSE otherwise
  2297.  */
  2298. static GLenum
  2299. compressed_texture_error_check(struct gl_context *ctx, GLint dimensions,
  2300.                                GLenum target, GLint level,
  2301.                                GLenum internalFormat, GLsizei width,
  2302.                                GLsizei height, GLsizei depth, GLint border,
  2303.                                GLsizei imageSize, const GLvoid *data)
  2304. {
  2305.    const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
  2306.    GLint expectedSize;
  2307.    GLenum error = GL_NO_ERROR;
  2308.    char *reason = ""; /* no error */
  2309.  
  2310.    if (!_mesa_target_can_be_compressed(ctx, target, internalFormat)) {
  2311.       reason = "target";
  2312.       /* From section 3.8.6, page 146 of OpenGL ES 3.0 spec:
  2313.        *
  2314.        *    "The ETC2/EAC texture compression algorithm supports only
  2315.        *     two-dimensional images. If internalformat is an ETC2/EAC format,
  2316.        *     CompressedTexImage3D will generate an INVALID_OPERATION error if
  2317.        *     target is not TEXTURE_2D_ARRAY."
  2318.        */
  2319.       error = _mesa_is_desktop_gl(ctx) ? GL_INVALID_ENUM : GL_INVALID_OPERATION;
  2320.       goto error;
  2321.    }
  2322.  
  2323.    /* This will detect any invalid internalFormat value */
  2324.    if (!_mesa_is_compressed_format(ctx, internalFormat)) {
  2325.       _mesa_error(ctx, GL_INVALID_ENUM,
  2326.                   "glCompressedTexImage%dD(internalFormat=%s)",
  2327.                   dimensions, _mesa_lookup_enum_by_nr(internalFormat));
  2328.       return GL_TRUE;
  2329.    }
  2330.  
  2331.    /* validate the bound PBO, if any */
  2332.    if (!_mesa_validate_pbo_source_compressed(ctx, dimensions, &ctx->Unpack,
  2333.                                              imageSize, data,
  2334.                                              "glCompressedTexImage")) {
  2335.       return GL_TRUE;
  2336.    }
  2337.  
  2338.    switch (internalFormat) {
  2339.    case GL_PALETTE4_RGB8_OES:
  2340.    case GL_PALETTE4_RGBA8_OES:
  2341.    case GL_PALETTE4_R5_G6_B5_OES:
  2342.    case GL_PALETTE4_RGBA4_OES:
  2343.    case GL_PALETTE4_RGB5_A1_OES:
  2344.    case GL_PALETTE8_RGB8_OES:
  2345.    case GL_PALETTE8_RGBA8_OES:
  2346.    case GL_PALETTE8_R5_G6_B5_OES:
  2347.    case GL_PALETTE8_RGBA4_OES:
  2348.    case GL_PALETTE8_RGB5_A1_OES:
  2349.       /* check level (note that level should be zero or less!) */
  2350.       if (level > 0 || level < -maxLevels) {
  2351.          reason = "level";
  2352.          error = GL_INVALID_VALUE;
  2353.          goto error;
  2354.       }
  2355.  
  2356.       if (dimensions != 2) {
  2357.          reason = "compressed paletted textures must be 2D";
  2358.          error = GL_INVALID_OPERATION;
  2359.          goto error;
  2360.       }
  2361.  
  2362.       /* Figure out the expected texture size (in bytes).  This will be
  2363.        * checked against the actual size later.
  2364.        */
  2365.       expectedSize = _mesa_cpal_compressed_size(level, internalFormat,
  2366.                                                 width, height);
  2367.  
  2368.       /* This is for the benefit of the TestProxyTexImage below.  It expects
  2369.        * level to be non-negative.  OES_compressed_paletted_texture uses a
  2370.        * weird mechanism where the level specified to glCompressedTexImage2D
  2371.        * is -(n-1) number of levels in the texture, and the data specifies the
  2372.        * complete mipmap stack.  This is done to ensure the palette is the
  2373.        * same for all levels.
  2374.        */
  2375.       level = -level;
  2376.       break;
  2377.  
  2378.    default:
  2379.       /* check level */
  2380.       if (level < 0 || level >= maxLevels) {
  2381.          reason = "level";
  2382.          error = GL_INVALID_VALUE;
  2383.          goto error;
  2384.       }
  2385.  
  2386.       /* Figure out the expected texture size (in bytes).  This will be
  2387.        * checked against the actual size later.
  2388.        */
  2389.       expectedSize = compressed_tex_size(width, height, depth, internalFormat);
  2390.       break;
  2391.    }
  2392.  
  2393.    /* This should really never fail */
  2394.    if (_mesa_base_tex_format(ctx, internalFormat) < 0) {
  2395.       reason = "internalFormat";
  2396.       error = GL_INVALID_ENUM;
  2397.       goto error;
  2398.    }
  2399.  
  2400.    /* No compressed formats support borders at this time */
  2401.    if (border != 0) {
  2402.       reason = "border != 0";
  2403.       error = GL_INVALID_VALUE;
  2404.       goto error;
  2405.    }
  2406.  
  2407.    /* Check for invalid pixel storage modes */
  2408.    if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
  2409.                                                    &ctx->Unpack,
  2410.                                                    "glCompressedTexImage")) {
  2411.       return GL_FALSE;
  2412.    }
  2413.  
  2414.    /* check image size in bytes */
  2415.    if (expectedSize != imageSize) {
  2416.       /* Per GL_ARB_texture_compression:  GL_INVALID_VALUE is generated [...]
  2417.        * if <imageSize> is not consistent with the format, dimensions, and
  2418.        * contents of the specified image.
  2419.        */
  2420.       reason = "imageSize inconsistant with width/height/format";
  2421.       error = GL_INVALID_VALUE;
  2422.       goto error;
  2423.    }
  2424.  
  2425.    if (!mutable_tex_object(ctx, target)) {
  2426.       reason = "immutable texture";
  2427.       error = GL_INVALID_OPERATION;
  2428.       goto error;
  2429.    }
  2430.  
  2431.    return GL_FALSE;
  2432.  
  2433. error:
  2434.    /* Note: not all error paths exit through here. */
  2435.    _mesa_error(ctx, error, "glCompressedTexImage%dD(%s)",
  2436.                dimensions, reason);
  2437.    return GL_TRUE;
  2438. }
  2439.  
  2440.  
  2441.  
  2442. /**
  2443.  * Test glTexSubImage[123]D() parameters for errors.
  2444.  *
  2445.  * \param ctx GL context.
  2446.  * \param dimensions texture image dimensions (must be 1, 2 or 3).
  2447.  * \param target texture target given by the user (already validated)
  2448.  * \param level image level given by the user.
  2449.  * \param xoffset sub-image x offset given by the user.
  2450.  * \param yoffset sub-image y offset given by the user.
  2451.  * \param zoffset sub-image z offset given by the user.
  2452.  * \param format pixel data format given by the user.
  2453.  * \param type pixel data type given by the user.
  2454.  * \param width image width given by the user.
  2455.  * \param height image height given by the user.
  2456.  * \param depth image depth given by the user.
  2457.  *
  2458.  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
  2459.  *
  2460.  * Verifies each of the parameters against the constants specified in
  2461.  * __struct gl_contextRec::Const and the supported extensions, and according
  2462.  * to the OpenGL specification.
  2463.  */
  2464. static GLboolean
  2465. texsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
  2466.                         struct gl_texture_object *texObj,
  2467.                         GLenum target, GLint level,
  2468.                         GLint xoffset, GLint yoffset, GLint zoffset,
  2469.                         GLint width, GLint height, GLint depth,
  2470.                         GLenum format, GLenum type, const GLvoid *pixels,
  2471.                         bool dsa, const char *callerName)
  2472. {
  2473.    struct gl_texture_image *texImage;
  2474.    GLenum err;
  2475.  
  2476.    if (!texObj) {
  2477.       /* must be out of memory */
  2478.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", callerName);
  2479.       return GL_TRUE;
  2480.    }
  2481.  
  2482.    /* check target (proxies not allowed) */
  2483.    if (!legal_texsubimage_target(ctx, dimensions, target, dsa)) {
  2484.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%s)",
  2485.                   callerName, _mesa_lookup_enum_by_nr(target));
  2486.       return GL_TRUE;
  2487.    }
  2488.  
  2489.    /* level check */
  2490.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  2491.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", callerName, level);
  2492.       return GL_TRUE;
  2493.    }
  2494.  
  2495.    /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
  2496.     * combinations of format and type that can be used.  Formats and types
  2497.     * that require additional extensions (e.g., GL_FLOAT requires
  2498.     * GL_OES_texture_float) are filtered elsewhere.
  2499.     */
  2500.    if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
  2501.       err = _mesa_es_error_check_format_and_type(format, type, dimensions);
  2502.       if (err != GL_NO_ERROR) {
  2503.          _mesa_error(ctx, err, "%s(format = %s, type = %s)",
  2504.                      callerName, _mesa_lookup_enum_by_nr(format),
  2505.                      _mesa_lookup_enum_by_nr(type));
  2506.          return GL_TRUE;
  2507.       }
  2508.    }
  2509.  
  2510.    err = _mesa_error_check_format_and_type(ctx, format, type);
  2511.    if (err != GL_NO_ERROR) {
  2512.       _mesa_error(ctx, err,
  2513.                   "%s(incompatible format = %s, type = %s)",
  2514.                   callerName, _mesa_lookup_enum_by_nr(format),
  2515.                   _mesa_lookup_enum_by_nr(type));
  2516.       return GL_TRUE;
  2517.    }
  2518.  
  2519.    /* validate the bound PBO, if any */
  2520.    if (!_mesa_validate_pbo_source(ctx, dimensions, &ctx->Unpack,
  2521.                                   width, height, depth, format, type,
  2522.                                   INT_MAX, pixels, callerName)) {
  2523.       return GL_TRUE;
  2524.    }
  2525.  
  2526.    texImage = _mesa_select_tex_image(texObj, target, level);
  2527.    if (!texImage) {
  2528.       /* non-existant texture level */
  2529.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture image)",
  2530.                   callerName);
  2531.       return GL_TRUE;
  2532.    }
  2533.  
  2534.    if (error_check_subtexture_dimensions(ctx, dimensions,
  2535.                                          texImage, xoffset, yoffset, zoffset,
  2536.                                          width, height, depth, callerName)) {
  2537.       return GL_TRUE;
  2538.    }
  2539.  
  2540.    if (_mesa_is_format_compressed(texImage->TexFormat)) {
  2541.       if (compressedteximage_only_format(ctx, texImage->InternalFormat)) {
  2542.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2543.                "%s(no compression for format)", callerName);
  2544.          return GL_TRUE;
  2545.       }
  2546.    }
  2547.  
  2548.    if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
  2549.       /* both source and dest must be integer-valued, or neither */
  2550.       if (_mesa_is_format_integer_color(texImage->TexFormat) !=
  2551.           _mesa_is_enum_format_integer(format)) {
  2552.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2553.                      "%s(integer/non-integer format mismatch)", callerName);
  2554.          return GL_TRUE;
  2555.       }
  2556.    }
  2557.  
  2558.    return GL_FALSE;
  2559. }
  2560.  
  2561.  
  2562. /**
  2563.  * Test glCopyTexImage[12]D() parameters for errors.
  2564.  *
  2565.  * \param ctx GL context.
  2566.  * \param dimensions texture image dimensions (must be 1, 2 or 3).
  2567.  * \param target texture target given by the user.
  2568.  * \param level image level given by the user.
  2569.  * \param internalFormat internal format given by the user.
  2570.  * \param width image width given by the user.
  2571.  * \param height image height given by the user.
  2572.  * \param border texture border.
  2573.  *
  2574.  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
  2575.  *
  2576.  * Verifies each of the parameters against the constants specified in
  2577.  * __struct gl_contextRec::Const and the supported extensions, and according
  2578.  * to the OpenGL specification.
  2579.  */
  2580. static GLboolean
  2581. copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
  2582.                          GLenum target, GLint level, GLint internalFormat,
  2583.                          GLint width, GLint height, GLint border )
  2584. {
  2585.    GLint baseFormat;
  2586.    GLint rb_base_format;
  2587.    struct gl_renderbuffer *rb;
  2588.    GLenum rb_internal_format;
  2589.  
  2590.    /* check target */
  2591.    if (!legal_texsubimage_target(ctx, dimensions, target, false)) {
  2592.       _mesa_error(ctx, GL_INVALID_ENUM, "glCopyTexImage%uD(target=%s)",
  2593.                   dimensions, _mesa_lookup_enum_by_nr(target));
  2594.       return GL_TRUE;
  2595.    }
  2596.  
  2597.    /* level check */
  2598.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  2599.       _mesa_error(ctx, GL_INVALID_VALUE,
  2600.                   "glCopyTexImage%dD(level=%d)", dimensions, level);
  2601.       return GL_TRUE;
  2602.    }
  2603.  
  2604.    /* Check that the source buffer is complete */
  2605.    if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
  2606.       if (ctx->ReadBuffer->_Status == 0) {
  2607.          _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
  2608.       }
  2609.       if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  2610.          _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
  2611.                      "glCopyTexImage%dD(invalid readbuffer)", dimensions);
  2612.          return GL_TRUE;
  2613.       }
  2614.  
  2615.       if (ctx->ReadBuffer->Visual.samples > 0) {
  2616.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2617.                      "glCopyTexImage%dD(multisample FBO)", dimensions);
  2618.          return GL_TRUE;
  2619.       }
  2620.    }
  2621.  
  2622.    /* Check border */
  2623.    if (border < 0 || border > 1 ||
  2624.        ((ctx->API != API_OPENGL_COMPAT ||
  2625.          target == GL_TEXTURE_RECTANGLE_NV ||
  2626.          target == GL_PROXY_TEXTURE_RECTANGLE_NV) && border != 0)) {
  2627.       _mesa_error(ctx, GL_INVALID_VALUE,
  2628.                   "glCopyTexImage%dD(border=%d)", dimensions, border);
  2629.       return GL_TRUE;
  2630.    }
  2631.  
  2632.    rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
  2633.    if (rb == NULL) {
  2634.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2635.                   "glCopyTexImage%dD(read buffer)", dimensions);
  2636.       return GL_TRUE;
  2637.    }
  2638.  
  2639.    /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
  2640.     * internalFormat.
  2641.     */
  2642.    if (_mesa_is_gles(ctx) && !_mesa_is_gles3(ctx)) {
  2643.       switch (internalFormat) {
  2644.       case GL_ALPHA:
  2645.       case GL_RGB:
  2646.       case GL_RGBA:
  2647.       case GL_LUMINANCE:
  2648.       case GL_LUMINANCE_ALPHA:
  2649.          break;
  2650.       default:
  2651.          _mesa_error(ctx, GL_INVALID_VALUE,
  2652.                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
  2653.                      _mesa_lookup_enum_by_nr(internalFormat));
  2654.          return GL_TRUE;
  2655.       }
  2656.    }
  2657.  
  2658.    baseFormat = _mesa_base_tex_format(ctx, internalFormat);
  2659.    if (baseFormat < 0) {
  2660.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2661.                   "glCopyTexImage%dD(internalFormat=%s)", dimensions,
  2662.                   _mesa_lookup_enum_by_nr(internalFormat));
  2663.       return GL_TRUE;
  2664.    }
  2665.  
  2666.    rb_internal_format = rb->InternalFormat;
  2667.    rb_base_format = _mesa_base_tex_format(ctx, rb->InternalFormat);
  2668.    if (_mesa_is_color_format(internalFormat)) {
  2669.       if (rb_base_format < 0) {
  2670.          _mesa_error(ctx, GL_INVALID_VALUE,
  2671.                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
  2672.                      _mesa_lookup_enum_by_nr(internalFormat));
  2673.          return GL_TRUE;
  2674.       }
  2675.    }
  2676.  
  2677.    if (_mesa_is_gles(ctx)) {
  2678.       bool valid = true;
  2679.       if (_mesa_base_format_component_count(baseFormat) >
  2680.           _mesa_base_format_component_count(rb_base_format)) {
  2681.          valid = false;
  2682.       }
  2683.       if (baseFormat == GL_DEPTH_COMPONENT ||
  2684.           baseFormat == GL_DEPTH_STENCIL ||
  2685.           rb_base_format == GL_DEPTH_COMPONENT ||
  2686.           rb_base_format == GL_DEPTH_STENCIL ||
  2687.           ((baseFormat == GL_LUMINANCE_ALPHA ||
  2688.             baseFormat == GL_ALPHA) &&
  2689.            rb_base_format != GL_RGBA) ||
  2690.           internalFormat == GL_RGB9_E5) {
  2691.          valid = false;
  2692.       }
  2693.       if (internalFormat == GL_RGB9_E5) {
  2694.          valid = false;
  2695.       }
  2696.       if (!valid) {
  2697.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2698.                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
  2699.                      _mesa_lookup_enum_by_nr(internalFormat));
  2700.          return GL_TRUE;
  2701.       }
  2702.    }
  2703.  
  2704.    if (_mesa_is_gles3(ctx)) {
  2705.       bool rb_is_srgb = false;
  2706.       bool dst_is_srgb = false;
  2707.  
  2708.       if (ctx->Extensions.EXT_framebuffer_sRGB &&
  2709.           _mesa_get_format_color_encoding(rb->Format) == GL_SRGB) {
  2710.          rb_is_srgb = true;
  2711.       }
  2712.  
  2713.       if (_mesa_get_linear_internalformat(internalFormat) != internalFormat) {
  2714.          dst_is_srgb = true;
  2715.       }
  2716.  
  2717.       if (rb_is_srgb != dst_is_srgb) {
  2718.          /* Page 137 (page 149 of the PDF) in section 3.8.5 of the
  2719.           * OpenGLES 3.0.0 spec says:
  2720.           *
  2721.           *     "The error INVALID_OPERATION is also generated if the
  2722.           *     value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING for the
  2723.           *     framebuffer attachment corresponding to the read buffer
  2724.           *     is LINEAR (see section 6.1.13) and internalformat is
  2725.           *     one of the sRGB formats described in section 3.8.16, or
  2726.           *     if the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING is
  2727.           *     SRGB and internalformat is not one of the sRGB formats."
  2728.           */
  2729.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2730.                      "glCopyTexImage%dD(srgb usage mismatch)", dimensions);
  2731.          return GL_TRUE;
  2732.       }
  2733.  
  2734.       /* Page 139, Table 3.15 of OpenGL ES 3.0 spec does not define ReadPixels
  2735.        * types for SNORM formats. Also, conversion to SNORM formats is not
  2736.        * allowed by Table 3.2 on Page 110.
  2737.        */
  2738.       if(_mesa_is_enum_format_snorm(internalFormat)) {
  2739.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2740.                      "glCopyTexImage%dD(internalFormat=%s)", dimensions,
  2741.                      _mesa_lookup_enum_by_nr(internalFormat));
  2742.          return GL_TRUE;
  2743.       }
  2744.    }
  2745.  
  2746.    if (!_mesa_source_buffer_exists(ctx, baseFormat)) {
  2747.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2748.                   "glCopyTexImage%dD(missing readbuffer)", dimensions);
  2749.       return GL_TRUE;
  2750.    }
  2751.  
  2752.    /* From the EXT_texture_integer spec:
  2753.     *
  2754.     *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
  2755.     *      if the texture internalformat is an integer format and the read color
  2756.     *      buffer is not an integer format, or if the internalformat is not an
  2757.     *      integer format and the read color buffer is an integer format."
  2758.     */
  2759.    if (_mesa_is_color_format(internalFormat)) {
  2760.       bool is_int = _mesa_is_enum_format_integer(internalFormat);
  2761.       bool is_rbint = _mesa_is_enum_format_integer(rb_internal_format);
  2762.       bool is_unorm = _mesa_is_enum_format_unorm(internalFormat);
  2763.       bool is_rbunorm = _mesa_is_enum_format_unorm(rb_internal_format);
  2764.       if (is_int || is_rbint) {
  2765.          if (is_int != is_rbint) {
  2766.             _mesa_error(ctx, GL_INVALID_OPERATION,
  2767.                         "glCopyTexImage%dD(integer vs non-integer)", dimensions);
  2768.             return GL_TRUE;
  2769.          } else if (_mesa_is_gles(ctx) &&
  2770.                     _mesa_is_enum_format_unsigned_int(internalFormat) !=
  2771.                       _mesa_is_enum_format_unsigned_int(rb_internal_format)) {
  2772.             _mesa_error(ctx, GL_INVALID_OPERATION,
  2773.                         "glCopyTexImage%dD(signed vs unsigned integer)", dimensions);
  2774.             return GL_TRUE;
  2775.          }
  2776.       }
  2777.  
  2778.       /* From page 138 of OpenGL ES 3.0 spec:
  2779.        *    "The error INVALID_OPERATION is generated if floating-point RGBA
  2780.        *    data is required; if signed integer RGBA data is required and the
  2781.        *    format of the current color buffer is not signed integer; if
  2782.        *    unsigned integer RGBA data is required and the format of the
  2783.        *    current color buffer is not unsigned integer; or if fixed-point
  2784.        *    RGBA data is required and the format of the current color buffer
  2785.        *    is not fixed-point.
  2786.        */
  2787.       if (_mesa_is_gles(ctx) && is_unorm != is_rbunorm)
  2788.             _mesa_error(ctx, GL_INVALID_OPERATION,
  2789.                         "glCopyTexImage%dD(unorm vs non-unorm)", dimensions);
  2790.    }
  2791.  
  2792.    if (_mesa_is_compressed_format(ctx, internalFormat)) {
  2793.       if (!_mesa_target_can_be_compressed(ctx, target, internalFormat)) {
  2794.          _mesa_error(ctx, GL_INVALID_ENUM,
  2795.                      "glCopyTexImage%dD(target)", dimensions);
  2796.          return GL_TRUE;
  2797.       }
  2798.       if (compressedteximage_only_format(ctx, internalFormat)) {
  2799.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2800.                "glCopyTexImage%dD(no compression for format)", dimensions);
  2801.          return GL_TRUE;
  2802.       }
  2803.       if (border != 0) {
  2804.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2805.                      "glCopyTexImage%dD(border!=0)", dimensions);
  2806.          return GL_TRUE;
  2807.       }
  2808.    }
  2809.  
  2810.    if (!mutable_tex_object(ctx, target)) {
  2811.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2812.                   "glCopyTexImage%dD(immutable texture)", dimensions);
  2813.       return GL_TRUE;
  2814.    }
  2815.  
  2816.    /* if we get here, the parameters are OK */
  2817.    return GL_FALSE;
  2818. }
  2819.  
  2820.  
  2821. /**
  2822.  * Test glCopyTexSubImage[12]D() parameters for errors.
  2823.  * \return GL_TRUE if an error was detected, or GL_FALSE if no errors.
  2824.  */
  2825. static GLboolean
  2826. copytexsubimage_error_check(struct gl_context *ctx, GLuint dimensions,
  2827.                             const struct gl_texture_object *texObj,
  2828.                             GLenum target, GLint level,
  2829.                             GLint xoffset, GLint yoffset, GLint zoffset,
  2830.                             GLint width, GLint height, const char *caller)
  2831. {
  2832.    struct gl_texture_image *texImage;
  2833.  
  2834.    /* Check that the source buffer is complete */
  2835.    if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
  2836.       if (ctx->ReadBuffer->_Status == 0) {
  2837.          _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
  2838.       }
  2839.       if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  2840.          _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
  2841.                      "%s(invalid readbuffer)", caller);
  2842.          return GL_TRUE;
  2843.       }
  2844.  
  2845.       if (ctx->ReadBuffer->Visual.samples > 0) {
  2846.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2847.                 "%s(multisample FBO)", caller);
  2848.          return GL_TRUE;
  2849.       }
  2850.    }
  2851.  
  2852.    /* Check level */
  2853.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  2854.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(level=%d)", caller, level);
  2855.       return GL_TRUE;
  2856.    }
  2857.  
  2858.    /* Get dest image pointers */
  2859.    if (!texObj) {
  2860.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", caller);
  2861.       return GL_TRUE;
  2862.    }
  2863.  
  2864.    texImage = _mesa_select_tex_image(texObj, target, level);
  2865.    if (!texImage) {
  2866.       /* destination image does not exist */
  2867.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2868.                   "%s(invalid texture image)", caller);
  2869.       return GL_TRUE;
  2870.    }
  2871.  
  2872.    if (error_check_subtexture_dimensions(ctx, dimensions, texImage,
  2873.                                          xoffset, yoffset, zoffset,
  2874.                                          width, height, 1, caller)) {
  2875.       return GL_TRUE;
  2876.    }
  2877.  
  2878.    if (_mesa_is_format_compressed(texImage->TexFormat)) {
  2879.       if (compressedteximage_only_format(ctx, texImage->InternalFormat)) {
  2880.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2881.                "%s(no compression for format)", caller);
  2882.          return GL_TRUE;
  2883.       }
  2884.    }
  2885.  
  2886.    if (texImage->InternalFormat == GL_YCBCR_MESA) {
  2887.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s()", caller);
  2888.       return GL_TRUE;
  2889.    }
  2890.  
  2891.    if (!_mesa_source_buffer_exists(ctx, texImage->_BaseFormat)) {
  2892.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2893.                   "%s(missing readbuffer, format=0x%x)", caller,
  2894.                   texImage->_BaseFormat);
  2895.       return GL_TRUE;
  2896.    }
  2897.  
  2898.    /* From the EXT_texture_integer spec:
  2899.     *
  2900.     *     "INVALID_OPERATION is generated by CopyTexImage* and
  2901.     *     CopyTexSubImage* if the texture internalformat is an integer format
  2902.     *     and the read color buffer is not an integer format, or if the
  2903.     *     internalformat is not an integer format and the read color buffer
  2904.     *     is an integer format."
  2905.     */
  2906.    if (_mesa_is_color_format(texImage->InternalFormat)) {
  2907.       struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
  2908.  
  2909.       if (_mesa_is_format_integer_color(rb->Format) !=
  2910.           _mesa_is_format_integer_color(texImage->TexFormat)) {
  2911.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2912.                      "%s(integer vs non-integer)", caller);
  2913.          return GL_TRUE;
  2914.       }
  2915.    }
  2916.  
  2917.    /* if we get here, the parameters are OK */
  2918.    return GL_FALSE;
  2919. }
  2920.  
  2921.  
  2922. /** Callback info for walking over FBO hash table */
  2923. struct cb_info
  2924. {
  2925.    struct gl_context *ctx;
  2926.    struct gl_texture_object *texObj;
  2927.    GLuint level, face;
  2928. };
  2929.  
  2930.  
  2931. /**
  2932.  * Check render to texture callback.  Called from _mesa_HashWalk().
  2933.  */
  2934. static void
  2935. check_rtt_cb(GLuint key, void *data, void *userData)
  2936. {
  2937.    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
  2938.    const struct cb_info *info = (struct cb_info *) userData;
  2939.    struct gl_context *ctx = info->ctx;
  2940.    const struct gl_texture_object *texObj = info->texObj;
  2941.    const GLuint level = info->level, face = info->face;
  2942.  
  2943.    /* If this is a user-created FBO */
  2944.    if (_mesa_is_user_fbo(fb)) {
  2945.       GLuint i;
  2946.       /* check if any of the FBO's attachments point to 'texObj' */
  2947.       for (i = 0; i < BUFFER_COUNT; i++) {
  2948.          struct gl_renderbuffer_attachment *att = fb->Attachment + i;
  2949.          if (att->Type == GL_TEXTURE &&
  2950.              att->Texture == texObj &&
  2951.              att->TextureLevel == level &&
  2952.              att->CubeMapFace == face) {
  2953.             _mesa_update_texture_renderbuffer(ctx, ctx->DrawBuffer, att);
  2954.             assert(att->Renderbuffer->TexImage);
  2955.             /* Mark fb status as indeterminate to force re-validation */
  2956.             fb->_Status = 0;
  2957.          }
  2958.       }
  2959.    }
  2960. }
  2961.  
  2962.  
  2963. /**
  2964.  * When a texture image is specified we have to check if it's bound to
  2965.  * any framebuffer objects (render to texture) in order to detect changes
  2966.  * in size or format since that effects FBO completeness.
  2967.  * Any FBOs rendering into the texture must be re-validated.
  2968.  */
  2969. void
  2970. _mesa_update_fbo_texture(struct gl_context *ctx,
  2971.                          struct gl_texture_object *texObj,
  2972.                          GLuint face, GLuint level)
  2973. {
  2974.    /* Only check this texture if it's been marked as RenderToTexture */
  2975.    if (texObj->_RenderToTexture) {
  2976.       struct cb_info info;
  2977.       info.ctx = ctx;
  2978.       info.texObj = texObj;
  2979.       info.level = level;
  2980.       info.face = face;
  2981.       _mesa_HashWalk(ctx->Shared->FrameBuffers, check_rtt_cb, &info);
  2982.    }
  2983. }
  2984.  
  2985.  
  2986. /**
  2987.  * If the texture object's GenerateMipmap flag is set and we've
  2988.  * changed the texture base level image, regenerate the rest of the
  2989.  * mipmap levels now.
  2990.  */
  2991. static inline void
  2992. check_gen_mipmap(struct gl_context *ctx, GLenum target,
  2993.                  struct gl_texture_object *texObj, GLint level)
  2994. {
  2995.    if (texObj->GenerateMipmap &&
  2996.        level == texObj->BaseLevel &&
  2997.        level < texObj->MaxLevel) {
  2998.       assert(ctx->Driver.GenerateMipmap);
  2999.       ctx->Driver.GenerateMipmap(ctx, target, texObj);
  3000.    }
  3001. }
  3002.  
  3003.  
  3004. /** Debug helper: override the user-requested internal format */
  3005. static GLenum
  3006. override_internal_format(GLenum internalFormat, GLint width, GLint height)
  3007. {
  3008. #if 0
  3009.    if (internalFormat == GL_RGBA16F_ARB ||
  3010.        internalFormat == GL_RGBA32F_ARB) {
  3011.       printf("Convert rgba float tex to int %d x %d\n", width, height);
  3012.       return GL_RGBA;
  3013.    }
  3014.    else if (internalFormat == GL_RGB16F_ARB ||
  3015.             internalFormat == GL_RGB32F_ARB) {
  3016.       printf("Convert rgb float tex to int %d x %d\n", width, height);
  3017.       return GL_RGB;
  3018.    }
  3019.    else if (internalFormat == GL_LUMINANCE_ALPHA16F_ARB ||
  3020.             internalFormat == GL_LUMINANCE_ALPHA32F_ARB) {
  3021.       printf("Convert luminance float tex to int %d x %d\n", width, height);
  3022.       return GL_LUMINANCE_ALPHA;
  3023.    }
  3024.    else if (internalFormat == GL_LUMINANCE16F_ARB ||
  3025.             internalFormat == GL_LUMINANCE32F_ARB) {
  3026.       printf("Convert luminance float tex to int %d x %d\n", width, height);
  3027.       return GL_LUMINANCE;
  3028.    }
  3029.    else if (internalFormat == GL_ALPHA16F_ARB ||
  3030.             internalFormat == GL_ALPHA32F_ARB) {
  3031.       printf("Convert luminance float tex to int %d x %d\n", width, height);
  3032.       return GL_ALPHA;
  3033.    }
  3034.    /*
  3035.    else if (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) {
  3036.       internalFormat = GL_RGBA;
  3037.    }
  3038.    */
  3039.    else {
  3040.       return internalFormat;
  3041.    }
  3042. #else
  3043.    return internalFormat;
  3044. #endif
  3045. }
  3046.  
  3047.  
  3048. /**
  3049.  * Choose the actual hardware format for a texture image.
  3050.  * Try to use the same format as the previous image level when possible.
  3051.  * Otherwise, ask the driver for the best format.
  3052.  * It's important to try to choose a consistant format for all levels
  3053.  * for efficient texture memory layout/allocation.  In particular, this
  3054.  * comes up during automatic mipmap generation.
  3055.  */
  3056. mesa_format
  3057. _mesa_choose_texture_format(struct gl_context *ctx,
  3058.                             struct gl_texture_object *texObj,
  3059.                             GLenum target, GLint level,
  3060.                             GLenum internalFormat, GLenum format, GLenum type)
  3061. {
  3062.    mesa_format f;
  3063.  
  3064.    /* see if we've already chosen a format for the previous level */
  3065.    if (level > 0) {
  3066.       struct gl_texture_image *prevImage =
  3067.          _mesa_select_tex_image(texObj, target, level - 1);
  3068.       /* See if the prev level is defined and has an internal format which
  3069.        * matches the new internal format.
  3070.        */
  3071.       if (prevImage &&
  3072.           prevImage->Width > 0 &&
  3073.           prevImage->InternalFormat == internalFormat) {
  3074.          /* use the same format */
  3075.          assert(prevImage->TexFormat != MESA_FORMAT_NONE);
  3076.          return prevImage->TexFormat;
  3077.       }
  3078.    }
  3079.  
  3080.    /* If the application requested compression to an S3TC format but we don't
  3081.     * have the DXTn library, force a generic compressed format instead.
  3082.     */
  3083.    if (internalFormat != format && format != GL_NONE) {
  3084.       const GLenum before = internalFormat;
  3085.  
  3086.       switch (internalFormat) {
  3087.       case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  3088.          if (!ctx->Mesa_DXTn)
  3089.             internalFormat = GL_COMPRESSED_RGB;
  3090.          break;
  3091.       case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  3092.       case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  3093.       case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  3094.          if (!ctx->Mesa_DXTn)
  3095.             internalFormat = GL_COMPRESSED_RGBA;
  3096.          break;
  3097.       default:
  3098.          break;
  3099.       }
  3100.  
  3101.       if (before != internalFormat) {
  3102.          _mesa_warning(ctx,
  3103.                        "DXT compression requested (%s), "
  3104.                        "but libtxc_dxtn library not installed.  Using %s "
  3105.                        "instead.",
  3106.                        _mesa_lookup_enum_by_nr(before),
  3107.                        _mesa_lookup_enum_by_nr(internalFormat));
  3108.       }
  3109.    }
  3110.  
  3111.    /* choose format from scratch */
  3112.    f = ctx->Driver.ChooseTextureFormat(ctx, target, internalFormat,
  3113.                                        format, type);
  3114.    assert(f != MESA_FORMAT_NONE);
  3115.    return f;
  3116. }
  3117.  
  3118.  
  3119. /**
  3120.  * Adjust pixel unpack params and image dimensions to strip off the
  3121.  * one-pixel texture border.
  3122.  *
  3123.  * Gallium and intel don't support texture borders.  They've seldem been used
  3124.  * and seldom been implemented correctly anyway.
  3125.  *
  3126.  * \param unpackNew returns the new pixel unpack parameters
  3127.  */
  3128. static void
  3129. strip_texture_border(GLenum target,
  3130.                      GLint *width, GLint *height, GLint *depth,
  3131.                      const struct gl_pixelstore_attrib *unpack,
  3132.                      struct gl_pixelstore_attrib *unpackNew)
  3133. {
  3134.    assert(width);
  3135.    assert(height);
  3136.    assert(depth);
  3137.  
  3138.    *unpackNew = *unpack;
  3139.  
  3140.    if (unpackNew->RowLength == 0)
  3141.       unpackNew->RowLength = *width;
  3142.  
  3143.    if (unpackNew->ImageHeight == 0)
  3144.       unpackNew->ImageHeight = *height;
  3145.  
  3146.    assert(*width >= 3);
  3147.    unpackNew->SkipPixels++;  /* skip the border */
  3148.    *width = *width - 2;      /* reduce the width by two border pixels */
  3149.  
  3150.    /* The min height of a texture with a border is 3 */
  3151.    if (*height >= 3 && target != GL_TEXTURE_1D_ARRAY) {
  3152.       unpackNew->SkipRows++;  /* skip the border */
  3153.       *height = *height - 2;  /* reduce the height by two border pixels */
  3154.    }
  3155.  
  3156.    if (*depth >= 3 &&
  3157.        target != GL_TEXTURE_2D_ARRAY &&
  3158.        target != GL_TEXTURE_CUBE_MAP_ARRAY) {
  3159.       unpackNew->SkipImages++;  /* skip the border */
  3160.       *depth = *depth - 2;      /* reduce the depth by two border pixels */
  3161.    }
  3162. }
  3163.  
  3164.  
  3165. /**
  3166.  * Common code to implement all the glTexImage1D/2D/3D functions
  3167.  * as well as glCompressedTexImage1D/2D/3D.
  3168.  * \param compressed  only GL_TRUE for glCompressedTexImage1D/2D/3D calls.
  3169.  * \param format  the user's image format (only used if !compressed)
  3170.  * \param type  the user's image type (only used if !compressed)
  3171.  * \param imageSize  only used for glCompressedTexImage1D/2D/3D calls.
  3172.  */
  3173. static void
  3174. teximage(struct gl_context *ctx, GLboolean compressed, GLuint dims,
  3175.          GLenum target, GLint level, GLint internalFormat,
  3176.          GLsizei width, GLsizei height, GLsizei depth,
  3177.          GLint border, GLenum format, GLenum type,
  3178.          GLsizei imageSize, const GLvoid *pixels)
  3179. {
  3180.    const char *func = compressed ? "glCompressedTexImage" : "glTexImage";
  3181.    struct gl_pixelstore_attrib unpack_no_border;
  3182.    const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
  3183.    struct gl_texture_object *texObj;
  3184.    mesa_format texFormat;
  3185.    GLboolean dimensionsOK, sizeOK;
  3186.  
  3187.    FLUSH_VERTICES(ctx, 0);
  3188.  
  3189.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) {
  3190.       if (compressed)
  3191.          _mesa_debug(ctx,
  3192.                      "glCompressedTexImage%uD %s %d %s %d %d %d %d %p\n",
  3193.                      dims,
  3194.                      _mesa_lookup_enum_by_nr(target), level,
  3195.                      _mesa_lookup_enum_by_nr(internalFormat),
  3196.                      width, height, depth, border, pixels);
  3197.       else
  3198.          _mesa_debug(ctx,
  3199.                      "glTexImage%uD %s %d %s %d %d %d %d %s %s %p\n",
  3200.                      dims,
  3201.                      _mesa_lookup_enum_by_nr(target), level,
  3202.                      _mesa_lookup_enum_by_nr(internalFormat),
  3203.                      width, height, depth, border,
  3204.                      _mesa_lookup_enum_by_nr(format),
  3205.                      _mesa_lookup_enum_by_nr(type), pixels);
  3206.    }
  3207.  
  3208.    internalFormat = override_internal_format(internalFormat, width, height);
  3209.  
  3210.    /* target error checking */
  3211.    if (!legal_teximage_target(ctx, dims, target)) {
  3212.       _mesa_error(ctx, GL_INVALID_ENUM, "%s%uD(target=%s)",
  3213.                   func, dims, _mesa_lookup_enum_by_nr(target));
  3214.       return;
  3215.    }
  3216.  
  3217.    /* general error checking */
  3218.    if (compressed) {
  3219.       if (compressed_texture_error_check(ctx, dims, target, level,
  3220.                                          internalFormat,
  3221.                                          width, height, depth,
  3222.                                          border, imageSize, pixels))
  3223.          return;
  3224.    }
  3225.    else {
  3226.       if (texture_error_check(ctx, dims, target, level, internalFormat,
  3227.                               format, type, width, height, depth, border,
  3228.                               pixels))
  3229.          return;
  3230.    }
  3231.  
  3232.    /* Here we convert a cpal compressed image into a regular glTexImage2D
  3233.     * call by decompressing the texture.  If we really want to support cpal
  3234.     * textures in any driver this would have to be changed.
  3235.     */
  3236.    if (ctx->API == API_OPENGLES && compressed && dims == 2) {
  3237.       switch (internalFormat) {
  3238.       case GL_PALETTE4_RGB8_OES:
  3239.       case GL_PALETTE4_RGBA8_OES:
  3240.       case GL_PALETTE4_R5_G6_B5_OES:
  3241.       case GL_PALETTE4_RGBA4_OES:
  3242.       case GL_PALETTE4_RGB5_A1_OES:
  3243.       case GL_PALETTE8_RGB8_OES:
  3244.       case GL_PALETTE8_RGBA8_OES:
  3245.       case GL_PALETTE8_R5_G6_B5_OES:
  3246.       case GL_PALETTE8_RGBA4_OES:
  3247.       case GL_PALETTE8_RGB5_A1_OES:
  3248.          _mesa_cpal_compressed_teximage2d(target, level, internalFormat,
  3249.                                           width, height, imageSize, pixels);
  3250.          return;
  3251.       }
  3252.    }
  3253.  
  3254.    texObj = _mesa_get_current_tex_object(ctx, target);
  3255.    assert(texObj);
  3256.  
  3257.    if (compressed) {
  3258.       /* For glCompressedTexImage() the driver has no choice about the
  3259.        * texture format since we'll never transcode the user's compressed
  3260.        * image data.  The internalFormat was error checked earlier.
  3261.        */
  3262.       texFormat = _mesa_glenum_to_compressed_format(internalFormat);
  3263.    }
  3264.    else {
  3265.       /* In case of HALF_FLOAT_OES or FLOAT_OES, find corresponding sized
  3266.        * internal floating point format for the given base format.
  3267.        */
  3268.       if (_mesa_is_gles(ctx) && format == internalFormat) {
  3269.          if (type == GL_FLOAT) {
  3270.             texObj->_IsFloat = GL_TRUE;
  3271.          } else if (type == GL_HALF_FLOAT_OES || type == GL_HALF_FLOAT) {
  3272.             texObj->_IsHalfFloat = GL_TRUE;
  3273.          }
  3274.  
  3275.          internalFormat = adjust_for_oes_float_texture(format, type);
  3276.       }
  3277.  
  3278.       texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
  3279.                                               internalFormat, format, type);
  3280.    }
  3281.  
  3282.    assert(texFormat != MESA_FORMAT_NONE);
  3283.  
  3284.    /* check that width, height, depth are legal for the mipmap level */
  3285.    dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, level, width,
  3286.                                                  height, depth, border);
  3287.  
  3288.    /* check that the texture won't take too much memory, etc */
  3289.    sizeOK = ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
  3290.                                           level, texFormat,
  3291.                                           width, height, depth, border);
  3292.  
  3293.    if (_mesa_is_proxy_texture(target)) {
  3294.       /* Proxy texture: just clear or set state depending on error checking */
  3295.       struct gl_texture_image *texImage =
  3296.          get_proxy_tex_image(ctx, target, level);
  3297.  
  3298.       if (!texImage)
  3299.          return;  /* GL_OUT_OF_MEMORY already recorded */
  3300.  
  3301.       if (dimensionsOK && sizeOK) {
  3302.          _mesa_init_teximage_fields(ctx, texImage, width, height, depth,
  3303.                                     border, internalFormat, texFormat);
  3304.       }
  3305.       else {
  3306.          clear_teximage_fields(texImage);
  3307.       }
  3308.    }
  3309.    else {
  3310.       /* non-proxy target */
  3311.       const GLuint face = _mesa_tex_target_to_face(target);
  3312.       struct gl_texture_image *texImage;
  3313.  
  3314.       if (!dimensionsOK) {
  3315.          _mesa_error(ctx, GL_INVALID_VALUE,
  3316.                      "glTexImage%uD(invalid width or height or depth)",
  3317.                      dims);
  3318.          return;
  3319.       }
  3320.  
  3321.       if (!sizeOK) {
  3322.          _mesa_error(ctx, GL_OUT_OF_MEMORY,
  3323.                      "glTexImage%uD(image too large: %d x %d x %d, %s format)",
  3324.                      dims, width, height, depth,
  3325.                      _mesa_lookup_enum_by_nr(internalFormat));
  3326.          return;
  3327.       }
  3328.  
  3329.       /* Allow a hardware driver to just strip out the border, to provide
  3330.        * reliable but slightly incorrect hardware rendering instead of
  3331.        * rarely-tested software fallback rendering.
  3332.        */
  3333.       if (border && ctx->Const.StripTextureBorder) {
  3334.          strip_texture_border(target, &width, &height, &depth, unpack,
  3335.                               &unpack_no_border);
  3336.          border = 0;
  3337.          unpack = &unpack_no_border;
  3338.       }
  3339.  
  3340.       if (ctx->NewState & _NEW_PIXEL)
  3341.          _mesa_update_state(ctx);
  3342.  
  3343.       _mesa_lock_texture(ctx, texObj);
  3344.       {
  3345.          texImage = _mesa_get_tex_image(ctx, texObj, target, level);
  3346.  
  3347.          if (!texImage) {
  3348.             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s%uD", func, dims);
  3349.          }
  3350.          else {
  3351.             ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  3352.  
  3353.             _mesa_init_teximage_fields(ctx, texImage,
  3354.                                        width, height, depth,
  3355.                                        border, internalFormat, texFormat);
  3356.  
  3357.             /* Give the texture to the driver.  <pixels> may be null. */
  3358.             if (width > 0 && height > 0 && depth > 0) {
  3359.                if (compressed) {
  3360.                   ctx->Driver.CompressedTexImage(ctx, dims, texImage,
  3361.                                                  imageSize, pixels);
  3362.                }
  3363.                else {
  3364.                   ctx->Driver.TexImage(ctx, dims, texImage, format,
  3365.                                        type, pixels, unpack);
  3366.                }
  3367.             }
  3368.  
  3369.             check_gen_mipmap(ctx, target, texObj, level);
  3370.  
  3371.             _mesa_update_fbo_texture(ctx, texObj, face, level);
  3372.  
  3373.             _mesa_dirty_texobj(ctx, texObj);
  3374.          }
  3375.       }
  3376.       _mesa_unlock_texture(ctx, texObj);
  3377.    }
  3378. }
  3379.  
  3380.  
  3381.  
  3382. /*
  3383.  * Called from the API.  Note that width includes the border.
  3384.  */
  3385. void GLAPIENTRY
  3386. _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
  3387.                   GLsizei width, GLint border, GLenum format,
  3388.                   GLenum type, const GLvoid *pixels )
  3389. {
  3390.    GET_CURRENT_CONTEXT(ctx);
  3391.    teximage(ctx, GL_FALSE, 1, target, level, internalFormat, width, 1, 1,
  3392.             border, format, type, 0, pixels);
  3393. }
  3394.  
  3395.  
  3396. void GLAPIENTRY
  3397. _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
  3398.                   GLsizei width, GLsizei height, GLint border,
  3399.                   GLenum format, GLenum type,
  3400.                   const GLvoid *pixels )
  3401. {
  3402.    GET_CURRENT_CONTEXT(ctx);
  3403.    teximage(ctx, GL_FALSE, 2, target, level, internalFormat, width, height, 1,
  3404.             border, format, type, 0, pixels);
  3405. }
  3406.  
  3407.  
  3408. /*
  3409.  * Called by the API or display list executor.
  3410.  * Note that width and height include the border.
  3411.  */
  3412. void GLAPIENTRY
  3413. _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
  3414.                   GLsizei width, GLsizei height, GLsizei depth,
  3415.                   GLint border, GLenum format, GLenum type,
  3416.                   const GLvoid *pixels )
  3417. {
  3418.    GET_CURRENT_CONTEXT(ctx);
  3419.    teximage(ctx, GL_FALSE, 3, target, level, internalFormat,
  3420.             width, height, depth,
  3421.             border, format, type, 0, pixels);
  3422. }
  3423.  
  3424.  
  3425. void GLAPIENTRY
  3426. _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
  3427.                      GLsizei width, GLsizei height, GLsizei depth,
  3428.                      GLint border, GLenum format, GLenum type,
  3429.                      const GLvoid *pixels )
  3430. {
  3431.    _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
  3432.                     depth, border, format, type, pixels);
  3433. }
  3434.  
  3435.  
  3436. void GLAPIENTRY
  3437. _mesa_EGLImageTargetTexture2DOES (GLenum target, GLeglImageOES image)
  3438. {
  3439.    struct gl_texture_object *texObj;
  3440.    struct gl_texture_image *texImage;
  3441.    bool valid_target;
  3442.    GET_CURRENT_CONTEXT(ctx);
  3443.    FLUSH_VERTICES(ctx, 0);
  3444.  
  3445.    switch (target) {
  3446.    case GL_TEXTURE_2D:
  3447.       valid_target = ctx->Extensions.OES_EGL_image;
  3448.       break;
  3449.    case GL_TEXTURE_EXTERNAL_OES:
  3450.       valid_target =
  3451.          _mesa_is_gles(ctx) ? ctx->Extensions.OES_EGL_image_external : false;
  3452.       break;
  3453.    default:
  3454.       valid_target = false;
  3455.       break;
  3456.    }
  3457.  
  3458.    if (!valid_target) {
  3459.       _mesa_error(ctx, GL_INVALID_ENUM,
  3460.                   "glEGLImageTargetTexture2D(target=%d)", target);
  3461.       return;
  3462.    }
  3463.  
  3464.    if (!image) {
  3465.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3466.                   "glEGLImageTargetTexture2D(image=%p)", image);
  3467.       return;
  3468.    }
  3469.  
  3470.    if (ctx->NewState & _NEW_PIXEL)
  3471.       _mesa_update_state(ctx);
  3472.  
  3473.    texObj = _mesa_get_current_tex_object(ctx, target);
  3474.    if (!texObj)
  3475.       return;
  3476.  
  3477.    _mesa_lock_texture(ctx, texObj);
  3478.  
  3479.    if (texObj->Immutable) {
  3480.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3481.                   "glEGLImageTargetTexture2D(texture is immutable)");
  3482.       _mesa_unlock_texture(ctx, texObj);
  3483.       return;
  3484.    }
  3485.  
  3486.    texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
  3487.    if (!texImage) {
  3488.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glEGLImageTargetTexture2D");
  3489.    } else {
  3490.       ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  3491.  
  3492.       ctx->Driver.EGLImageTargetTexture2D(ctx, target,
  3493.                                           texObj, texImage, image);
  3494.  
  3495.       _mesa_dirty_texobj(ctx, texObj);
  3496.    }
  3497.    _mesa_unlock_texture(ctx, texObj);
  3498.  
  3499. }
  3500.  
  3501.  
  3502. /**
  3503.  * Helper that implements the glTexSubImage1/2/3D()
  3504.  * and glTextureSubImage1/2/3D() functions.
  3505.  */
  3506. void
  3507. _mesa_texture_sub_image(struct gl_context *ctx, GLuint dims,
  3508.                         struct gl_texture_object *texObj,
  3509.                         struct gl_texture_image *texImage,
  3510.                         GLenum target, GLint level,
  3511.                         GLint xoffset, GLint yoffset, GLint zoffset,
  3512.                         GLsizei width, GLsizei height, GLsizei depth,
  3513.                         GLenum format, GLenum type, const GLvoid *pixels,
  3514.                         bool dsa)
  3515. {
  3516.    FLUSH_VERTICES(ctx, 0);
  3517.  
  3518.    /* check target (proxies not allowed) */
  3519.    if (!legal_texsubimage_target(ctx, dims, target, dsa)) {
  3520.       _mesa_error(ctx, GL_INVALID_ENUM, "glTex%sSubImage%uD(target=%s)",
  3521.                   dsa ? "ture" : "",
  3522.                   dims, _mesa_lookup_enum_by_nr(target));
  3523.       return;
  3524.    }
  3525.  
  3526.    if (ctx->NewState & _NEW_PIXEL)
  3527.       _mesa_update_state(ctx);
  3528.  
  3529.    _mesa_lock_texture(ctx, texObj);
  3530.    {
  3531.       if (width > 0 && height > 0 && depth > 0) {
  3532.          /* If we have a border, offset=-1 is legal.  Bias by border width. */
  3533.          switch (dims) {
  3534.          case 3:
  3535.             if (target != GL_TEXTURE_2D_ARRAY)
  3536.                zoffset += texImage->Border;
  3537.             /* fall-through */
  3538.          case 2:
  3539.             if (target != GL_TEXTURE_1D_ARRAY)
  3540.                yoffset += texImage->Border;
  3541.             /* fall-through */
  3542.          case 1:
  3543.             xoffset += texImage->Border;
  3544.          }
  3545.  
  3546.          ctx->Driver.TexSubImage(ctx, dims, texImage,
  3547.                                  xoffset, yoffset, zoffset,
  3548.                                  width, height, depth,
  3549.                                  format, type, pixels, &ctx->Unpack);
  3550.  
  3551.          check_gen_mipmap(ctx, target, texObj, level);
  3552.  
  3553.          /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
  3554.           * the texel data, not the texture format, size, etc.
  3555.           */
  3556.       }
  3557.    }
  3558.    _mesa_unlock_texture(ctx, texObj);
  3559. }
  3560.  
  3561. /**
  3562.  * Implement all the glTexSubImage1/2/3D() functions.
  3563.  * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
  3564.  */
  3565. static void
  3566. texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
  3567.             GLint xoffset, GLint yoffset, GLint zoffset,
  3568.             GLsizei width, GLsizei height, GLsizei depth,
  3569.             GLenum format, GLenum type, const GLvoid *pixels,
  3570.             const char *callerName)
  3571. {
  3572.    struct gl_texture_object *texObj;
  3573.    struct gl_texture_image *texImage;
  3574.  
  3575.    texObj = _mesa_get_current_tex_object(ctx, target);
  3576.    if (!texObj)
  3577.       return;
  3578.  
  3579.    if (texsubimage_error_check(ctx, dims, texObj, target, level,
  3580.                                xoffset, yoffset, zoffset,
  3581.                                width, height, depth, format, type,
  3582.                                pixels, false, callerName)) {
  3583.       return;   /* error was detected */
  3584.    }
  3585.  
  3586.    texImage = _mesa_select_tex_image(texObj, target, level);
  3587.    /* texsubimage_error_check ensures that texImage is not NULL */
  3588.  
  3589.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
  3590.       _mesa_debug(ctx, "glTexSubImage%uD %s %d %d %d %d %d %d %d %s %s %p\n",
  3591.                   dims,
  3592.                   _mesa_lookup_enum_by_nr(target), level,
  3593.                   xoffset, yoffset, zoffset, width, height, depth,
  3594.                   _mesa_lookup_enum_by_nr(format),
  3595.                   _mesa_lookup_enum_by_nr(type), pixels);
  3596.  
  3597.    _mesa_texture_sub_image(ctx, dims, texObj, texImage, target, level,
  3598.                            xoffset, yoffset, zoffset, width, height, depth,
  3599.                            format, type, pixels, false);
  3600. }
  3601.  
  3602.  
  3603. /**
  3604.  * Implement all the glTextureSubImage1/2/3D() functions.
  3605.  * Must split this out this way because of GL_TEXTURE_CUBE_MAP.
  3606.  */
  3607. static void
  3608. texturesubimage(struct gl_context *ctx, GLuint dims,
  3609.                 GLuint texture, GLint level,
  3610.                 GLint xoffset, GLint yoffset, GLint zoffset,
  3611.                 GLsizei width, GLsizei height, GLsizei depth,
  3612.                 GLenum format, GLenum type, const GLvoid *pixels,
  3613.                 const char *callerName)
  3614. {
  3615.    struct gl_texture_object *texObj;
  3616.    struct gl_texture_image *texImage;
  3617.    int i;
  3618.  
  3619.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
  3620.       _mesa_debug(ctx,
  3621.                   "glTextureSubImage%uD %d %d %d %d %d %d %d %d %s %s %p\n",
  3622.                   dims, texture, level,
  3623.                   xoffset, yoffset, zoffset, width, height, depth,
  3624.                   _mesa_lookup_enum_by_nr(format),
  3625.                   _mesa_lookup_enum_by_nr(type), pixels);
  3626.  
  3627.    if (!ctx->Extensions.ARB_direct_state_access) {
  3628.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3629.                   "glTextureSubImage%uD(GL_ARB_direct_state_access "
  3630.                   "is not supported)", dims);
  3631.       return;
  3632.    }
  3633.  
  3634.    /* Get the texture object by Name. */
  3635.    texObj = _mesa_lookup_texture(ctx, texture);
  3636.    if (!texObj) {
  3637.       _mesa_error(ctx, GL_INVALID_OPERATION, "glTextureSubImage%uD(texture)",
  3638.                   dims);
  3639.       return;
  3640.    }
  3641.  
  3642.    if (texsubimage_error_check(ctx, dims, texObj, texObj->Target, level,
  3643.                                xoffset, yoffset, zoffset,
  3644.                                width, height, depth, format, type,
  3645.                                pixels, true, callerName)) {
  3646.       return;   /* error was detected */
  3647.    }
  3648.  
  3649.  
  3650.    /* Must handle special case GL_TEXTURE_CUBE_MAP. */
  3651.    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
  3652.       GLint rowStride;
  3653.  
  3654.       /*
  3655.        * What do we do if the user created a texture with the following code
  3656.        * and then called this function with its handle?
  3657.        *
  3658.        *    GLuint tex;
  3659.        *    glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &tex);
  3660.        *    glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
  3661.        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, ...);
  3662.        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, ...);
  3663.        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, ...);
  3664.        *    // Note: GL_TEXTURE_CUBE_MAP_NEGATIVE_Y not set, or given the
  3665.        *    // wrong format, or given the wrong size, etc.
  3666.        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, ...);
  3667.        *    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, ...);
  3668.        *
  3669.        * A bug has been filed against the spec for this case.  In the
  3670.        * meantime, we will check for cube completeness.
  3671.        *
  3672.        * According to Section 8.17 Texture Completeness in the OpenGL 4.5
  3673.        * Core Profile spec (30.10.2014):
  3674.        *    "[A] cube map texture is cube complete if the
  3675.        *    following conditions all hold true: The [base level] texture
  3676.        *    images of each of the six cube map faces have identical, positive,
  3677.        *    and square dimensions. The [base level] images were each specified
  3678.        *    with the same internal format."
  3679.        *
  3680.        * It seems reasonable to check for cube completeness of an arbitrary
  3681.        * level here so that the image data has a consistent format and size.
  3682.        */
  3683.       if (!_mesa_cube_level_complete(texObj, level)) {
  3684.          _mesa_error(ctx, GL_INVALID_OPERATION,
  3685.                      "glTextureSubImage%uD(cube map incomplete)",
  3686.                      dims);
  3687.          return;
  3688.       }
  3689.  
  3690.       rowStride = _mesa_image_image_stride(&ctx->Unpack, width, height,
  3691.                                            format, type);
  3692.       /* Copy in each face. */
  3693.       for (i = 0; i < 6; ++i) {
  3694.          texImage = texObj->Image[i][level];
  3695.          assert(texImage);
  3696.  
  3697.          _mesa_texture_sub_image(ctx, 3, texObj, texImage, texObj->Target,
  3698.                                  level, xoffset, yoffset, zoffset,
  3699.                                  width, height, 1, format,
  3700.                                  type, pixels, true);
  3701.          pixels = (GLubyte *) pixels + rowStride;
  3702.       }
  3703.    }
  3704.    else {
  3705.       texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
  3706.       assert(texImage);
  3707.  
  3708.       _mesa_texture_sub_image(ctx, dims, texObj, texImage, texObj->Target,
  3709.                               level, xoffset, yoffset, zoffset,
  3710.                               width, height, depth, format,
  3711.                               type, pixels, true);
  3712.    }
  3713. }
  3714.  
  3715.  
  3716. void GLAPIENTRY
  3717. _mesa_TexSubImage1D( GLenum target, GLint level,
  3718.                      GLint xoffset, GLsizei width,
  3719.                      GLenum format, GLenum type,
  3720.                      const GLvoid *pixels )
  3721. {
  3722.    GET_CURRENT_CONTEXT(ctx);
  3723.    texsubimage(ctx, 1, target, level,
  3724.                xoffset, 0, 0,
  3725.                width, 1, 1,
  3726.                format, type, pixels, "glTexSubImage1D");
  3727. }
  3728.  
  3729.  
  3730. void GLAPIENTRY
  3731. _mesa_TexSubImage2D( GLenum target, GLint level,
  3732.                      GLint xoffset, GLint yoffset,
  3733.                      GLsizei width, GLsizei height,
  3734.                      GLenum format, GLenum type,
  3735.                      const GLvoid *pixels )
  3736. {
  3737.    GET_CURRENT_CONTEXT(ctx);
  3738.    texsubimage(ctx, 2, target, level,
  3739.                xoffset, yoffset, 0,
  3740.                width, height, 1,
  3741.                format, type, pixels, "glTexSubImage2D");
  3742. }
  3743.  
  3744.  
  3745.  
  3746. void GLAPIENTRY
  3747. _mesa_TexSubImage3D( GLenum target, GLint level,
  3748.                      GLint xoffset, GLint yoffset, GLint zoffset,
  3749.                      GLsizei width, GLsizei height, GLsizei depth,
  3750.                      GLenum format, GLenum type,
  3751.                      const GLvoid *pixels )
  3752. {
  3753.    GET_CURRENT_CONTEXT(ctx);
  3754.    texsubimage(ctx, 3, target, level,
  3755.                xoffset, yoffset, zoffset,
  3756.                width, height, depth,
  3757.                format, type, pixels, "glTexSubImage3D");
  3758. }
  3759.  
  3760. void GLAPIENTRY
  3761. _mesa_TextureSubImage1D(GLuint texture, GLint level,
  3762.                         GLint xoffset, GLsizei width,
  3763.                         GLenum format, GLenum type,
  3764.                         const GLvoid *pixels)
  3765. {
  3766.    GET_CURRENT_CONTEXT(ctx);
  3767.    texturesubimage(ctx, 1, texture, level,
  3768.                    xoffset, 0, 0,
  3769.                    width, 1, 1,
  3770.                    format, type, pixels, "glTextureSubImage1D");
  3771. }
  3772.  
  3773.  
  3774. void GLAPIENTRY
  3775. _mesa_TextureSubImage2D(GLuint texture, GLint level,
  3776.                         GLint xoffset, GLint yoffset,
  3777.                         GLsizei width, GLsizei height,
  3778.                         GLenum format, GLenum type,
  3779.                         const GLvoid *pixels)
  3780. {
  3781.    GET_CURRENT_CONTEXT(ctx);
  3782.    texturesubimage(ctx, 2, texture, level,
  3783.                    xoffset, yoffset, 0,
  3784.                    width, height, 1,
  3785.                    format, type, pixels, "glTextureSubImage2D");
  3786. }
  3787.  
  3788.  
  3789. void GLAPIENTRY
  3790. _mesa_TextureSubImage3D(GLuint texture, GLint level,
  3791.                         GLint xoffset, GLint yoffset, GLint zoffset,
  3792.                         GLsizei width, GLsizei height, GLsizei depth,
  3793.                         GLenum format, GLenum type,
  3794.                         const GLvoid *pixels)
  3795. {
  3796.    GET_CURRENT_CONTEXT(ctx);
  3797.    texturesubimage(ctx, 3, texture, level,
  3798.                    xoffset, yoffset, zoffset,
  3799.                    width, height, depth,
  3800.                    format, type, pixels, "glTextureSubImage3D");
  3801. }
  3802.  
  3803.  
  3804. /**
  3805.  * For glCopyTexSubImage, return the source renderbuffer to copy texel data
  3806.  * from.  This depends on whether the texture contains color or depth values.
  3807.  */
  3808. static struct gl_renderbuffer *
  3809. get_copy_tex_image_source(struct gl_context *ctx, mesa_format texFormat)
  3810. {
  3811.    if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
  3812.       /* reading from depth/stencil buffer */
  3813.       return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
  3814.    }
  3815.    else {
  3816.       /* copying from color buffer */
  3817.       return ctx->ReadBuffer->_ColorReadBuffer;
  3818.    }
  3819. }
  3820.  
  3821. static void
  3822. copytexsubimage_by_slice(struct gl_context *ctx,
  3823.                          struct gl_texture_image *texImage,
  3824.                          GLuint dims,
  3825.                          GLint xoffset, GLint yoffset, GLint zoffset,
  3826.                          struct gl_renderbuffer *rb,
  3827.                          GLint x, GLint y,
  3828.                          GLsizei width, GLsizei height)
  3829. {
  3830.    if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
  3831.       int slice;
  3832.  
  3833.       /* For 1D arrays, we copy each scanline of the source rectangle into the
  3834.        * next array slice.
  3835.        */
  3836.       assert(zoffset == 0);
  3837.  
  3838.       for (slice = 0; slice < height; slice++) {
  3839.          assert(yoffset + slice < texImage->Height);
  3840.          ctx->Driver.CopyTexSubImage(ctx, 2, texImage,
  3841.                                      xoffset, 0, yoffset + slice,
  3842.                                      rb, x, y + slice, width, 1);
  3843.       }
  3844.    } else {
  3845.       ctx->Driver.CopyTexSubImage(ctx, dims, texImage,
  3846.                                   xoffset, yoffset, zoffset,
  3847.                                   rb, x, y, width, height);
  3848.    }
  3849. }
  3850.  
  3851. static GLboolean
  3852. formats_differ_in_component_sizes (mesa_format f1,
  3853.                                    mesa_format f2)
  3854. {
  3855.    GLint f1_r_bits = _mesa_get_format_bits(f1, GL_RED_BITS);
  3856.    GLint f1_g_bits = _mesa_get_format_bits(f1, GL_GREEN_BITS);
  3857.    GLint f1_b_bits = _mesa_get_format_bits(f1, GL_BLUE_BITS);
  3858.    GLint f1_a_bits = _mesa_get_format_bits(f1, GL_ALPHA_BITS);
  3859.  
  3860.    GLint f2_r_bits = _mesa_get_format_bits(f2, GL_RED_BITS);
  3861.    GLint f2_g_bits = _mesa_get_format_bits(f2, GL_GREEN_BITS);
  3862.    GLint f2_b_bits = _mesa_get_format_bits(f2, GL_BLUE_BITS);
  3863.    GLint f2_a_bits = _mesa_get_format_bits(f2, GL_ALPHA_BITS);
  3864.  
  3865.    if ((f1_r_bits && f2_r_bits && f1_r_bits != f2_r_bits)
  3866.        || (f1_g_bits && f2_g_bits && f1_g_bits != f2_g_bits)
  3867.        || (f1_b_bits && f2_b_bits && f1_b_bits != f2_b_bits)
  3868.        || (f1_a_bits && f2_a_bits && f1_a_bits != f2_a_bits))
  3869.       return GL_TRUE;
  3870.  
  3871.    return GL_FALSE;
  3872. }
  3873.  
  3874. /**
  3875.  * Implement the glCopyTexImage1/2D() functions.
  3876.  */
  3877. static void
  3878. copyteximage(struct gl_context *ctx, GLuint dims,
  3879.              GLenum target, GLint level, GLenum internalFormat,
  3880.              GLint x, GLint y, GLsizei width, GLsizei height, GLint border )
  3881. {
  3882.    struct gl_texture_object *texObj;
  3883.    struct gl_texture_image *texImage;
  3884.    const GLuint face = _mesa_tex_target_to_face(target);
  3885.    mesa_format texFormat;
  3886.    struct gl_renderbuffer *rb;
  3887.  
  3888.    FLUSH_VERTICES(ctx, 0);
  3889.  
  3890.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
  3891.       _mesa_debug(ctx, "glCopyTexImage%uD %s %d %s %d %d %d %d %d\n",
  3892.                   dims,
  3893.                   _mesa_lookup_enum_by_nr(target), level,
  3894.                   _mesa_lookup_enum_by_nr(internalFormat),
  3895.                   x, y, width, height, border);
  3896.  
  3897.    if (ctx->NewState & NEW_COPY_TEX_STATE)
  3898.       _mesa_update_state(ctx);
  3899.  
  3900.    if (copytexture_error_check(ctx, dims, target, level, internalFormat,
  3901.                                width, height, border))
  3902.       return;
  3903.  
  3904.    if (!_mesa_legal_texture_dimensions(ctx, target, level, width, height,
  3905.                                        1, border)) {
  3906.       _mesa_error(ctx, GL_INVALID_VALUE,
  3907.                   "glCopyTexImage%uD(invalid width or height)", dims);
  3908.       return;
  3909.    }
  3910.  
  3911.    texObj = _mesa_get_current_tex_object(ctx, target);
  3912.    assert(texObj);
  3913.  
  3914.    texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
  3915.                                            internalFormat, GL_NONE, GL_NONE);
  3916.  
  3917.    rb = _mesa_get_read_renderbuffer_for_format(ctx, internalFormat);
  3918.  
  3919.    if (_mesa_is_gles3(ctx)) {
  3920.       if (_mesa_is_enum_format_unsized(internalFormat)) {
  3921.       /* Conversion from GL_RGB10_A2 source buffer format is not allowed in
  3922.        * OpenGL ES 3.0. Khronos bug# 9807.
  3923.        */
  3924.          if (rb->InternalFormat == GL_RGB10_A2) {
  3925.                _mesa_error(ctx, GL_INVALID_OPERATION,
  3926.                            "glCopyTexImage%uD(Reading from GL_RGB10_A2 buffer and"
  3927.                            " writing to unsized internal format)", dims);
  3928.                return;
  3929.          }
  3930.       }
  3931.       /* From Page 139 of OpenGL ES 3.0 spec:
  3932.        *    "If internalformat is sized, the internal format of the new texel
  3933.        *    array is internalformat, and this is also the new texel array’s
  3934.        *    effective internal format. If the component sizes of internalformat
  3935.        *    do not exactly match the corresponding component sizes of the source
  3936.        *    buffer’s effective internal format, described below, an
  3937.        *    INVALID_OPERATION error is generated. If internalformat is unsized,
  3938.        *    the internal format of the new texel array is the effective internal
  3939.        *    format of the source buffer, and this is also the new texel array’s
  3940.        *    effective internal format.
  3941.        */
  3942.       else if (formats_differ_in_component_sizes (texFormat, rb->Format)) {
  3943.             _mesa_error(ctx, GL_INVALID_OPERATION,
  3944.                         "glCopyTexImage%uD(componenet size changed in"
  3945.                         " internal format)", dims);
  3946.             return;
  3947.       }
  3948.    }
  3949.  
  3950.    assert(texFormat != MESA_FORMAT_NONE);
  3951.  
  3952.    if (!ctx->Driver.TestProxyTexImage(ctx, proxy_target(target),
  3953.                                       level, texFormat,
  3954.                                       width, height, 1, border)) {
  3955.       _mesa_error(ctx, GL_OUT_OF_MEMORY,
  3956.                   "glCopyTexImage%uD(image too large)", dims);
  3957.       return;
  3958.    }
  3959.  
  3960.    if (border && ctx->Const.StripTextureBorder) {
  3961.       x += border;
  3962.       width -= border * 2;
  3963.       if (dims == 2) {
  3964.          y += border;
  3965.          height -= border * 2;
  3966.       }
  3967.       border = 0;
  3968.    }
  3969.  
  3970.    _mesa_lock_texture(ctx, texObj);
  3971.    {
  3972.       texImage = _mesa_get_tex_image(ctx, texObj, target, level);
  3973.  
  3974.       if (!texImage) {
  3975.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexImage%uD", dims);
  3976.       }
  3977.       else {
  3978.          GLint srcX = x, srcY = y, dstX = 0, dstY = 0, dstZ = 0;
  3979.  
  3980.          /* Free old texture image */
  3981.          ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  3982.  
  3983.          _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
  3984.                                     border, internalFormat, texFormat);
  3985.  
  3986.          if (width && height) {
  3987.             /* Allocate texture memory (no pixel data yet) */
  3988.             ctx->Driver.AllocTextureImageBuffer(ctx, texImage);
  3989.  
  3990.             if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
  3991.                                            &width, &height)) {
  3992.                struct gl_renderbuffer *srcRb =
  3993.                   get_copy_tex_image_source(ctx, texImage->TexFormat);
  3994.  
  3995.                copytexsubimage_by_slice(ctx, texImage, dims,
  3996.                                         dstX, dstY, dstZ,
  3997.                                         srcRb, srcX, srcY, width, height);
  3998.             }
  3999.  
  4000.             check_gen_mipmap(ctx, target, texObj, level);
  4001.          }
  4002.  
  4003.          _mesa_update_fbo_texture(ctx, texObj, face, level);
  4004.  
  4005.          _mesa_dirty_texobj(ctx, texObj);
  4006.       }
  4007.    }
  4008.    _mesa_unlock_texture(ctx, texObj);
  4009. }
  4010.  
  4011.  
  4012.  
  4013. void GLAPIENTRY
  4014. _mesa_CopyTexImage1D( GLenum target, GLint level,
  4015.                       GLenum internalFormat,
  4016.                       GLint x, GLint y,
  4017.                       GLsizei width, GLint border )
  4018. {
  4019.    GET_CURRENT_CONTEXT(ctx);
  4020.    copyteximage(ctx, 1, target, level, internalFormat, x, y, width, 1, border);
  4021. }
  4022.  
  4023.  
  4024.  
  4025. void GLAPIENTRY
  4026. _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
  4027.                       GLint x, GLint y, GLsizei width, GLsizei height,
  4028.                       GLint border )
  4029. {
  4030.    GET_CURRENT_CONTEXT(ctx);
  4031.    copyteximage(ctx, 2, target, level, internalFormat,
  4032.                 x, y, width, height, border);
  4033. }
  4034.  
  4035. /**
  4036.  * Implementation for glCopyTex(ture)SubImage1/2/3D() functions.
  4037.  */
  4038. void
  4039. _mesa_copy_texture_sub_image(struct gl_context *ctx, GLuint dims,
  4040.                              struct gl_texture_object *texObj,
  4041.                              GLenum target, GLint level,
  4042.                              GLint xoffset, GLint yoffset, GLint zoffset,
  4043.                              GLint x, GLint y,
  4044.                              GLsizei width, GLsizei height,
  4045.                              const char *caller)
  4046. {
  4047.    struct gl_texture_image *texImage;
  4048.  
  4049.    FLUSH_VERTICES(ctx, 0);
  4050.  
  4051.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
  4052.       _mesa_debug(ctx, "%s %s %d %d %d %d %d %d %d %d\n", caller,
  4053.                   _mesa_lookup_enum_by_nr(target),
  4054.                   level, xoffset, yoffset, zoffset, x, y, width, height);
  4055.  
  4056.    if (ctx->NewState & NEW_COPY_TEX_STATE)
  4057.       _mesa_update_state(ctx);
  4058.  
  4059.    if (copytexsubimage_error_check(ctx, dims, texObj, target, level,
  4060.                                    xoffset, yoffset, zoffset,
  4061.                                    width, height, caller)) {
  4062.       return;
  4063.    }
  4064.  
  4065.    _mesa_lock_texture(ctx, texObj);
  4066.    {
  4067.       texImage = _mesa_select_tex_image(texObj, target, level);
  4068.  
  4069.       /* If we have a border, offset=-1 is legal.  Bias by border width. */
  4070.       switch (dims) {
  4071.       case 3:
  4072.          if (target != GL_TEXTURE_2D_ARRAY)
  4073.             zoffset += texImage->Border;
  4074.          /* fall-through */
  4075.       case 2:
  4076.          if (target != GL_TEXTURE_1D_ARRAY)
  4077.             yoffset += texImage->Border;
  4078.          /* fall-through */
  4079.       case 1:
  4080.          xoffset += texImage->Border;
  4081.       }
  4082.  
  4083.       if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
  4084.                                      &width, &height)) {
  4085.          struct gl_renderbuffer *srcRb =
  4086.             get_copy_tex_image_source(ctx, texImage->TexFormat);
  4087.  
  4088.          copytexsubimage_by_slice(ctx, texImage, dims,
  4089.                                   xoffset, yoffset, zoffset,
  4090.                                   srcRb, x, y, width, height);
  4091.  
  4092.          check_gen_mipmap(ctx, target, texObj, level);
  4093.  
  4094.          /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
  4095.           * the texel data, not the texture format, size, etc.
  4096.           */
  4097.       }
  4098.    }
  4099.    _mesa_unlock_texture(ctx, texObj);
  4100. }
  4101.  
  4102. void GLAPIENTRY
  4103. _mesa_CopyTexSubImage1D( GLenum target, GLint level,
  4104.                          GLint xoffset, GLint x, GLint y, GLsizei width )
  4105. {
  4106.    struct gl_texture_object* texObj;
  4107.    const char *self = "glCopyTexSubImage1D";
  4108.    GET_CURRENT_CONTEXT(ctx);
  4109.  
  4110.    /* Check target (proxies not allowed). Target must be checked prior to
  4111.     * calling _mesa_get_current_tex_object.
  4112.     */
  4113.    if (!legal_texsubimage_target(ctx, 1, target, false)) {
  4114.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
  4115.                   _mesa_lookup_enum_by_nr(target));
  4116.       return;
  4117.    }
  4118.  
  4119.    texObj = _mesa_get_current_tex_object(ctx, target);
  4120.    if (!texObj)
  4121.       return;
  4122.  
  4123.    _mesa_copy_texture_sub_image(ctx, 1, texObj, target, level, xoffset, 0, 0,
  4124.                                 x, y, width, 1, self);
  4125. }
  4126.  
  4127.  
  4128.  
  4129. void GLAPIENTRY
  4130. _mesa_CopyTexSubImage2D( GLenum target, GLint level,
  4131.                          GLint xoffset, GLint yoffset,
  4132.                          GLint x, GLint y, GLsizei width, GLsizei height )
  4133. {
  4134.    struct gl_texture_object* texObj;
  4135.    const char *self = "glCopyTexSubImage2D";
  4136.    GET_CURRENT_CONTEXT(ctx);
  4137.  
  4138.    /* Check target (proxies not allowed). Target must be checked prior to
  4139.     * calling _mesa_get_current_tex_object.
  4140.     */
  4141.    if (!legal_texsubimage_target(ctx, 2, target, false)) {
  4142.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
  4143.                   _mesa_lookup_enum_by_nr(target));
  4144.       return;
  4145.    }
  4146.  
  4147.    texObj = _mesa_get_current_tex_object(ctx, target);
  4148.    if (!texObj)
  4149.       return;
  4150.  
  4151.    _mesa_copy_texture_sub_image(ctx, 2, texObj, target, level,
  4152.                                 xoffset, yoffset, 0,
  4153.                                 x, y, width, height, self);
  4154. }
  4155.  
  4156.  
  4157.  
  4158. void GLAPIENTRY
  4159. _mesa_CopyTexSubImage3D( GLenum target, GLint level,
  4160.                          GLint xoffset, GLint yoffset, GLint zoffset,
  4161.                          GLint x, GLint y, GLsizei width, GLsizei height )
  4162. {
  4163.    struct gl_texture_object* texObj;
  4164.    const char *self = "glCopyTexSubImage3D";
  4165.    GET_CURRENT_CONTEXT(ctx);
  4166.  
  4167.    /* Check target (proxies not allowed). Target must be checked prior to
  4168.     * calling _mesa_get_current_tex_object.
  4169.     */
  4170.    if (!legal_texsubimage_target(ctx, 3, target, false)) {
  4171.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
  4172.                   _mesa_lookup_enum_by_nr(target));
  4173.       return;
  4174.    }
  4175.  
  4176.    texObj = _mesa_get_current_tex_object(ctx, target);
  4177.    if (!texObj)
  4178.       return;
  4179.  
  4180.    _mesa_copy_texture_sub_image(ctx, 3, texObj, target, level,
  4181.                                 xoffset, yoffset, zoffset,
  4182.                                 x, y, width, height, self);
  4183. }
  4184.  
  4185. void GLAPIENTRY
  4186. _mesa_CopyTextureSubImage1D(GLuint texture, GLint level,
  4187.                             GLint xoffset, GLint x, GLint y, GLsizei width)
  4188. {
  4189.    struct gl_texture_object* texObj;
  4190.    const char *self = "glCopyTextureSubImage1D";
  4191.    GET_CURRENT_CONTEXT(ctx);
  4192.  
  4193.    if (!ctx->Extensions.ARB_direct_state_access) {
  4194.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4195.                   "%s(GL_ARB_direct_state_access is not supported)", self);
  4196.       return;
  4197.    }
  4198.  
  4199.    texObj = _mesa_lookup_texture_err(ctx, texture, self);
  4200.    if (!texObj)
  4201.       return;
  4202.  
  4203.    /* Check target (proxies not allowed). */
  4204.    if (!legal_texsubimage_target(ctx, 1, texObj->Target, true)) {
  4205.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
  4206.                   _mesa_lookup_enum_by_nr(texObj->Target));
  4207.       return;
  4208.    }
  4209.  
  4210.    _mesa_copy_texture_sub_image(ctx, 1, texObj, texObj->Target, level,
  4211.                                 xoffset, 0, 0, x, y, width, 1, self);
  4212. }
  4213.  
  4214. void GLAPIENTRY
  4215. _mesa_CopyTextureSubImage2D(GLuint texture, GLint level,
  4216.                             GLint xoffset, GLint yoffset,
  4217.                             GLint x, GLint y, GLsizei width, GLsizei height)
  4218. {
  4219.    struct gl_texture_object* texObj;
  4220.    const char *self = "glCopyTextureSubImage2D";
  4221.    GET_CURRENT_CONTEXT(ctx);
  4222.  
  4223.    if (!ctx->Extensions.ARB_direct_state_access) {
  4224.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4225.                   "%s(GL_ARB_direct_state_access is not supported)", self);
  4226.       return;
  4227.    }
  4228.  
  4229.    texObj = _mesa_lookup_texture_err(ctx, texture, self);
  4230.    if (!texObj)
  4231.       return;
  4232.  
  4233.    /* Check target (proxies not allowed). */
  4234.    if (!legal_texsubimage_target(ctx, 2, texObj->Target, true)) {
  4235.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
  4236.                   _mesa_lookup_enum_by_nr(texObj->Target));
  4237.       return;
  4238.    }
  4239.  
  4240.    _mesa_copy_texture_sub_image(ctx, 2, texObj, texObj->Target, level,
  4241.                                 xoffset, yoffset, 0,
  4242.                                 x, y, width, height, self);
  4243. }
  4244.  
  4245.  
  4246.  
  4247. void GLAPIENTRY
  4248. _mesa_CopyTextureSubImage3D(GLuint texture, GLint level,
  4249.                             GLint xoffset, GLint yoffset, GLint zoffset,
  4250.                             GLint x, GLint y, GLsizei width, GLsizei height)
  4251. {
  4252.    struct gl_texture_object* texObj;
  4253.    const char *self = "glCopyTextureSubImage3D";
  4254.    GET_CURRENT_CONTEXT(ctx);
  4255.  
  4256.    if (!ctx->Extensions.ARB_direct_state_access) {
  4257.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4258.                   "%s(GL_ARB_direct_state_access is not supported)", self);
  4259.       return;
  4260.    }
  4261.  
  4262.    texObj = _mesa_lookup_texture_err(ctx, texture, self);
  4263.    if (!texObj)
  4264.       return;
  4265.  
  4266.    /* Check target (proxies not allowed). */
  4267.    if (!legal_texsubimage_target(ctx, 3, texObj->Target, true)) {
  4268.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", self,
  4269.                   _mesa_lookup_enum_by_nr(texObj->Target));
  4270.       return;
  4271.    }
  4272.  
  4273.    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
  4274.       /* Act like CopyTexSubImage2D */
  4275.       _mesa_copy_texture_sub_image(ctx, 2, texObj,
  4276.                                    GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset,
  4277.                                    level, xoffset, yoffset, 0,
  4278.                                    x, y, width, height, self);
  4279.    }
  4280.    else
  4281.       _mesa_copy_texture_sub_image(ctx, 3, texObj, texObj->Target, level,
  4282.                                    xoffset, yoffset, zoffset,
  4283.                                    x, y, width, height, self);
  4284. }
  4285.  
  4286. static bool
  4287. check_clear_tex_image(struct gl_context *ctx,
  4288.                       const char *function,
  4289.                       struct gl_texture_image *texImage,
  4290.                       GLenum format, GLenum type,
  4291.                       const void *data,
  4292.                       GLubyte *clearValue)
  4293. {
  4294.    struct gl_texture_object *texObj = texImage->TexObject;
  4295.    static const GLubyte zeroData[MAX_PIXEL_BYTES];
  4296.    GLenum internalFormat = texImage->InternalFormat;
  4297.    GLenum err;
  4298.  
  4299.    if (texObj->Target == GL_TEXTURE_BUFFER) {
  4300.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4301.                   "%s(buffer texture)", function);
  4302.       return false;
  4303.    }
  4304.  
  4305.    if (_mesa_is_compressed_format(ctx, internalFormat)) {
  4306.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4307.                   "%s(compressed texture)", function);
  4308.       return false;
  4309.    }
  4310.  
  4311.    err = _mesa_error_check_format_and_type(ctx, format, type);
  4312.    if (err != GL_NO_ERROR) {
  4313.       _mesa_error(ctx, err,
  4314.                   "%s(incompatible format = %s, type = %s)",
  4315.                   function,
  4316.                   _mesa_lookup_enum_by_nr(format),
  4317.                   _mesa_lookup_enum_by_nr(type));
  4318.       return false;
  4319.    }
  4320.  
  4321.    /* make sure internal format and format basically agree */
  4322.    if (!texture_formats_agree(internalFormat, format)) {
  4323.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4324.                   "%s(incompatible internalFormat = %s, format = %s)",
  4325.                   function,
  4326.                   _mesa_lookup_enum_by_nr(internalFormat),
  4327.                   _mesa_lookup_enum_by_nr(format));
  4328.       return false;
  4329.    }
  4330.  
  4331.    if (ctx->Version >= 30 || ctx->Extensions.EXT_texture_integer) {
  4332.       /* both source and dest must be integer-valued, or neither */
  4333.       if (_mesa_is_format_integer_color(texImage->TexFormat) !=
  4334.           _mesa_is_enum_format_integer(format)) {
  4335.          _mesa_error(ctx, GL_INVALID_OPERATION,
  4336.                      "%s(integer/non-integer format mismatch)",
  4337.                      function);
  4338.          return false;
  4339.       }
  4340.    }
  4341.  
  4342.    if (!_mesa_texstore(ctx,
  4343.                        1, /* dims */
  4344.                        texImage->_BaseFormat,
  4345.                        texImage->TexFormat,
  4346.                        0, /* dstRowStride */
  4347.                        &clearValue,
  4348.                        1, 1, 1, /* srcWidth/Height/Depth */
  4349.                        format, type,
  4350.                        data ? data : zeroData,
  4351.                        &ctx->DefaultPacking)) {
  4352.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid format)", function);
  4353.       return false;
  4354.    }
  4355.  
  4356.    return true;
  4357. }
  4358.  
  4359. static struct gl_texture_object *
  4360. get_tex_obj_for_clear(struct gl_context *ctx,
  4361.                       const char *function,
  4362.                       GLuint texture)
  4363. {
  4364.    struct gl_texture_object *texObj;
  4365.  
  4366.    if (texture == 0) {
  4367.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(zero texture)", function);
  4368.       return NULL;
  4369.    }
  4370.  
  4371.    texObj = _mesa_lookup_texture(ctx, texture);
  4372.  
  4373.    if (texObj == NULL) {
  4374.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", function);
  4375.       return NULL;
  4376.    }
  4377.  
  4378.    if (texObj->Target == 0) {
  4379.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unbound tex)", function);
  4380.       return NULL;
  4381.    }
  4382.  
  4383.    return texObj;
  4384. }
  4385.  
  4386. static int
  4387. get_tex_images_for_clear(struct gl_context *ctx,
  4388.                          const char *function,
  4389.                          struct gl_texture_object *texObj,
  4390.                          GLint level,
  4391.                          struct gl_texture_image **texImages)
  4392. {
  4393.    GLenum target;
  4394.    int i;
  4395.  
  4396.    if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
  4397.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
  4398.       return 0;
  4399.    }
  4400.  
  4401.    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
  4402.       for (i = 0; i < MAX_FACES; i++) {
  4403.          target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + i;
  4404.  
  4405.          texImages[i] = _mesa_select_tex_image(texObj, target, level);
  4406.          if (texImages[i] == NULL) {
  4407.             _mesa_error(ctx, GL_INVALID_OPERATION,
  4408.                         "%s(invalid level)", function);
  4409.             return 0;
  4410.          }
  4411.       }
  4412.  
  4413.       return MAX_FACES;
  4414.    }
  4415.  
  4416.    texImages[0] = _mesa_select_tex_image(texObj, texObj->Target, level);
  4417.  
  4418.    if (texImages[0] == NULL) {
  4419.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid level)", function);
  4420.       return 0;
  4421.    }
  4422.  
  4423.    return 1;
  4424. }
  4425.  
  4426. void GLAPIENTRY
  4427. _mesa_ClearTexSubImage( GLuint texture, GLint level,
  4428.                         GLint xoffset, GLint yoffset, GLint zoffset,
  4429.                         GLsizei width, GLsizei height, GLsizei depth,
  4430.                         GLenum format, GLenum type, const void *data )
  4431. {
  4432.    GET_CURRENT_CONTEXT(ctx);
  4433.    struct gl_texture_object *texObj;
  4434.    struct gl_texture_image *texImages[MAX_FACES];
  4435.    GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
  4436.    int i, numImages;
  4437.    int minDepth, maxDepth;
  4438.  
  4439.    texObj = get_tex_obj_for_clear(ctx, "glClearTexSubImage", texture);
  4440.  
  4441.    if (texObj == NULL)
  4442.       return;
  4443.  
  4444.    _mesa_lock_texture(ctx, texObj);
  4445.  
  4446.    numImages = get_tex_images_for_clear(ctx, "glClearTexSubImage",
  4447.                                         texObj, level, texImages);
  4448.    if (numImages == 0)
  4449.       goto out;
  4450.  
  4451.    if (numImages == 1) {
  4452.       minDepth = -(int) texImages[0]->Border;
  4453.       maxDepth = texImages[0]->Depth;
  4454.    } else {
  4455.       minDepth = 0;
  4456.       maxDepth = numImages;
  4457.    }
  4458.  
  4459.    if (xoffset < -(GLint) texImages[0]->Border ||
  4460.        yoffset < -(GLint) texImages[0]->Border ||
  4461.        zoffset < minDepth ||
  4462.        width < 0 ||
  4463.        height < 0 ||
  4464.        depth < 0 ||
  4465.        xoffset + width > texImages[0]->Width ||
  4466.        yoffset + height > texImages[0]->Height ||
  4467.        zoffset + depth > maxDepth) {
  4468.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4469.                   "glClearSubTexImage(invalid dimensions)");
  4470.       goto out;
  4471.    }
  4472.  
  4473.    if (numImages == 1) {
  4474.       if (check_clear_tex_image(ctx, "glClearTexSubImage",
  4475.                                 texImages[0],
  4476.                                 format, type, data, clearValue[0])) {
  4477.          ctx->Driver.ClearTexSubImage(ctx,
  4478.                                       texImages[0],
  4479.                                       xoffset, yoffset, zoffset,
  4480.                                       width, height, depth,
  4481.                                       data ? clearValue[0] : NULL);
  4482.       }
  4483.    } else {
  4484.       for (i = zoffset; i < zoffset + depth; i++) {
  4485.          if (!check_clear_tex_image(ctx, "glClearTexSubImage",
  4486.                                     texImages[i],
  4487.                                     format, type, data, clearValue[i]))
  4488.             goto out;
  4489.       }
  4490.       for (i = zoffset; i < zoffset + depth; i++) {
  4491.          ctx->Driver.ClearTexSubImage(ctx,
  4492.                                       texImages[i],
  4493.                                       xoffset, yoffset, 0,
  4494.                                       width, height, 1,
  4495.                                       data ? clearValue[i] : NULL);
  4496.       }
  4497.    }
  4498.  
  4499.  out:
  4500.    _mesa_unlock_texture(ctx, texObj);
  4501. }
  4502.  
  4503. void GLAPIENTRY
  4504. _mesa_ClearTexImage( GLuint texture, GLint level,
  4505.                      GLenum format, GLenum type, const void *data )
  4506. {
  4507.    GET_CURRENT_CONTEXT(ctx);
  4508.    struct gl_texture_object *texObj;
  4509.    struct gl_texture_image *texImages[MAX_FACES];
  4510.    GLubyte clearValue[MAX_FACES][MAX_PIXEL_BYTES];
  4511.    int i, numImages;
  4512.  
  4513.    texObj = get_tex_obj_for_clear(ctx, "glClearTexImage", texture);
  4514.  
  4515.    if (texObj == NULL)
  4516.       return;
  4517.  
  4518.    _mesa_lock_texture(ctx, texObj);
  4519.  
  4520.    numImages = get_tex_images_for_clear(ctx, "glClearTexImage",
  4521.                                         texObj, level, texImages);
  4522.  
  4523.    for (i = 0; i < numImages; i++) {
  4524.       if (!check_clear_tex_image(ctx, "glClearTexImage",
  4525.                                  texImages[i],
  4526.                                  format, type, data,
  4527.                                  clearValue[i]))
  4528.          goto out;
  4529.    }
  4530.  
  4531.    for (i = 0; i < numImages; i++) {
  4532.       ctx->Driver.ClearTexSubImage(ctx, texImages[i],
  4533.                                    -(GLint) texImages[i]->Border, /* xoffset */
  4534.                                    -(GLint) texImages[i]->Border, /* yoffset */
  4535.                                    -(GLint) texImages[i]->Border, /* zoffset */
  4536.                                    texImages[i]->Width,
  4537.                                    texImages[i]->Height,
  4538.                                    texImages[i]->Depth,
  4539.                                    data ? clearValue[i] : NULL);
  4540.    }
  4541.  
  4542. out:
  4543.    _mesa_unlock_texture(ctx, texObj);
  4544. }
  4545.  
  4546.  
  4547.  
  4548.  
  4549. /**********************************************************************/
  4550. /******                   Compressed Textures                    ******/
  4551. /**********************************************************************/
  4552.  
  4553.  
  4554. /**
  4555.  * Target checking for glCompressedTexSubImage[123]D().
  4556.  * \return GL_TRUE if error, GL_FALSE if no error
  4557.  * Must come before other error checking so that the texture object can
  4558.  * be correctly retrieved using _mesa_get_current_tex_object.
  4559.  */
  4560. static GLboolean
  4561. compressed_subtexture_target_check(struct gl_context *ctx, GLenum target,
  4562.                                    GLint dims, GLenum format, bool dsa,
  4563.                                    const char *caller)
  4564. {
  4565.    GLboolean targetOK;
  4566.  
  4567.    if (dsa && target == GL_TEXTURE_RECTANGLE) {
  4568.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid target %s)", caller,
  4569.                   _mesa_lookup_enum_by_nr(target));
  4570.       return GL_TRUE;
  4571.    }
  4572.  
  4573.    switch (dims) {
  4574.    case 2:
  4575.       switch (target) {
  4576.       case GL_TEXTURE_2D:
  4577.       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  4578.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  4579.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  4580.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  4581.       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  4582.       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  4583.          targetOK = GL_TRUE;
  4584.          break;
  4585.       default:
  4586.          targetOK = GL_FALSE;
  4587.          break;
  4588.       }
  4589.       break;
  4590.    case 3:
  4591.       targetOK = (target == GL_TEXTURE_3D) ||
  4592.                  (target == GL_TEXTURE_2D_ARRAY) ||
  4593.                  (target == GL_TEXTURE_CUBE_MAP_ARRAY) ||
  4594.                  (target == GL_TEXTURE_CUBE_MAP && dsa);
  4595.  
  4596.       /* OpenGL 4.5 spec (30.10.2014) says in Section 8.7 Compressed Texture
  4597.        * Images:
  4598.        *    "An INVALID_OPERATION error is generated by
  4599.        *    CompressedTex*SubImage3D if the internal format of the texture is
  4600.        *    one of the EAC, ETC2, or RGTC formats and either border is
  4601.        *    non-zero, or the effective target for the texture is not
  4602.        *    TEXTURE_2D_ARRAY."
  4603.        */
  4604.       if (target != GL_TEXTURE_2D_ARRAY) {
  4605.          bool invalidformat;
  4606.          switch (format) {
  4607.             /* These came from _mesa_is_compressed_format in glformats.c. */
  4608.             /* EAC formats */
  4609.             case GL_COMPRESSED_RGBA8_ETC2_EAC:
  4610.             case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
  4611.             case GL_COMPRESSED_R11_EAC:
  4612.             case GL_COMPRESSED_RG11_EAC:
  4613.             case GL_COMPRESSED_SIGNED_R11_EAC:
  4614.             case GL_COMPRESSED_SIGNED_RG11_EAC:
  4615.             /* ETC2 formats */
  4616.             case GL_COMPRESSED_RGB8_ETC2:
  4617.             case GL_COMPRESSED_SRGB8_ETC2:
  4618.             case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
  4619.             case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
  4620.             /* RGTC formats */
  4621.             case GL_COMPRESSED_RED_RGTC1:
  4622.             case GL_COMPRESSED_SIGNED_RED_RGTC1:
  4623.             case GL_COMPRESSED_RG_RGTC2:
  4624.             case GL_COMPRESSED_SIGNED_RG_RGTC2:
  4625.                invalidformat = true;
  4626.                break;
  4627.             default:
  4628.                invalidformat = false;
  4629.          }
  4630.          if (invalidformat) {
  4631.             _mesa_error(ctx, GL_INVALID_OPERATION,
  4632.                         "%s(invalid target %s for format %s)", caller,
  4633.                         _mesa_lookup_enum_by_nr(target),
  4634.                         _mesa_lookup_enum_by_nr(format));
  4635.             return GL_TRUE;
  4636.          }
  4637.       }
  4638.  
  4639.       break;
  4640.    default:
  4641.       assert(dims == 1);
  4642.       /* no 1D compressed textures at this time */
  4643.       targetOK = GL_FALSE;
  4644.       break;
  4645.    }
  4646.  
  4647.    if (!targetOK) {
  4648.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
  4649.                   _mesa_lookup_enum_by_nr(target));
  4650.       return GL_TRUE;
  4651.    }
  4652.  
  4653.    return GL_FALSE;
  4654. }
  4655.  
  4656. /**
  4657.  * Error checking for glCompressedTexSubImage[123]D().
  4658.  * \return GL_TRUE if error, GL_FALSE if no error
  4659.  */
  4660. static GLboolean
  4661. compressed_subtexture_error_check(struct gl_context *ctx, GLint dims,
  4662.                                   const struct gl_texture_object *texObj,
  4663.                                   GLenum target, GLint level,
  4664.                                   GLint xoffset, GLint yoffset, GLint zoffset,
  4665.                                   GLsizei width, GLsizei height, GLsizei depth,
  4666.                                   GLenum format, GLsizei imageSize,
  4667.                                   const GLvoid *data, const char *callerName)
  4668. {
  4669.    struct gl_texture_image *texImage;
  4670.    GLint expectedSize;
  4671.  
  4672.    /* this will catch any invalid compressed format token */
  4673.    if (!_mesa_is_compressed_format(ctx, format)) {
  4674.       _mesa_error(ctx, GL_INVALID_ENUM,
  4675.                   "%s(format)", callerName);
  4676.       return GL_TRUE;
  4677.    }
  4678.  
  4679.    if (level < 0 || level >= _mesa_max_texture_levels(ctx, target)) {
  4680.       _mesa_error(ctx, GL_INVALID_VALUE,
  4681.                   "%s(level=%d)",
  4682.                   callerName, level);
  4683.       return GL_TRUE;
  4684.    }
  4685.  
  4686.    /* validate the bound PBO, if any */
  4687.    if (!_mesa_validate_pbo_source_compressed(ctx, dims, &ctx->Unpack,
  4688.                                      imageSize, data, callerName)) {
  4689.       return GL_TRUE;
  4690.    }
  4691.  
  4692.    /* Check for invalid pixel storage modes */
  4693.    if (!_mesa_compressed_pixel_storage_error_check(ctx, dims,
  4694.                                                    &ctx->Unpack, callerName)) {
  4695.       return GL_TRUE;
  4696.    }
  4697.  
  4698.    expectedSize = compressed_tex_size(width, height, depth, format);
  4699.    if (expectedSize != imageSize) {
  4700.       _mesa_error(ctx, GL_INVALID_VALUE,
  4701.                   "%s(size=%d)",
  4702.                   callerName, imageSize);
  4703.       return GL_TRUE;
  4704.    }
  4705.  
  4706.    texImage = _mesa_select_tex_image(texObj, target, level);
  4707.    if (!texImage) {
  4708.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4709.                   "%s(invalid texture image)",
  4710.                   callerName);
  4711.       return GL_TRUE;
  4712.    }
  4713.  
  4714.    if ((GLint) format != texImage->InternalFormat) {
  4715.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4716.                   "%s(format=0x%x)",
  4717.                   callerName, format);
  4718.       return GL_TRUE;
  4719.    }
  4720.  
  4721.    if (compressedteximage_only_format(ctx, format)) {
  4722.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4723.                "%s(format=0x%x cannot be updated)",
  4724.                callerName, format);
  4725.       return GL_TRUE;
  4726.    }
  4727.  
  4728.    if (error_check_subtexture_dimensions(ctx, dims,
  4729.                                          texImage, xoffset, yoffset, zoffset,
  4730.                                          width, height, depth,
  4731.                                          callerName)) {
  4732.       return GL_TRUE;
  4733.    }
  4734.  
  4735.    return GL_FALSE;
  4736. }
  4737.  
  4738.  
  4739. void GLAPIENTRY
  4740. _mesa_CompressedTexImage1D(GLenum target, GLint level,
  4741.                               GLenum internalFormat, GLsizei width,
  4742.                               GLint border, GLsizei imageSize,
  4743.                               const GLvoid *data)
  4744. {
  4745.    GET_CURRENT_CONTEXT(ctx);
  4746.    teximage(ctx, GL_TRUE, 1, target, level, internalFormat,
  4747.             width, 1, 1, border, GL_NONE, GL_NONE, imageSize, data);
  4748. }
  4749.  
  4750.  
  4751. void GLAPIENTRY
  4752. _mesa_CompressedTexImage2D(GLenum target, GLint level,
  4753.                               GLenum internalFormat, GLsizei width,
  4754.                               GLsizei height, GLint border, GLsizei imageSize,
  4755.                               const GLvoid *data)
  4756. {
  4757.    GET_CURRENT_CONTEXT(ctx);
  4758.    teximage(ctx, GL_TRUE, 2, target, level, internalFormat,
  4759.             width, height, 1, border, GL_NONE, GL_NONE, imageSize, data);
  4760. }
  4761.  
  4762.  
  4763. void GLAPIENTRY
  4764. _mesa_CompressedTexImage3D(GLenum target, GLint level,
  4765.                               GLenum internalFormat, GLsizei width,
  4766.                               GLsizei height, GLsizei depth, GLint border,
  4767.                               GLsizei imageSize, const GLvoid *data)
  4768. {
  4769.    GET_CURRENT_CONTEXT(ctx);
  4770.    teximage(ctx, GL_TRUE, 3, target, level, internalFormat,
  4771.             width, height, depth, border, GL_NONE, GL_NONE, imageSize, data);
  4772. }
  4773.  
  4774.  
  4775. /**
  4776.  * Common helper for glCompressedTexSubImage1/2/3D() and
  4777.  * glCompressedTextureSubImage1/2/3D().
  4778.  */
  4779. void
  4780. _mesa_compressed_texture_sub_image(struct gl_context *ctx, GLuint dims,
  4781.                                    struct gl_texture_object *texObj,
  4782.                                    struct gl_texture_image *texImage,
  4783.                                    GLenum target, GLint level,
  4784.                                    GLint xoffset, GLint yoffset,
  4785.                                    GLint zoffset,
  4786.                                    GLsizei width, GLsizei height,
  4787.                                    GLsizei depth,
  4788.                                    GLenum format, GLsizei imageSize,
  4789.                                    const GLvoid *data)
  4790. {
  4791.    FLUSH_VERTICES(ctx, 0);
  4792.  
  4793.    _mesa_lock_texture(ctx, texObj);
  4794.    {
  4795.       if (width > 0 && height > 0 && depth > 0) {
  4796.          ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
  4797.                                            xoffset, yoffset, zoffset,
  4798.                                            width, height, depth,
  4799.                                            format, imageSize, data);
  4800.  
  4801.          check_gen_mipmap(ctx, target, texObj, level);
  4802.  
  4803.          /* NOTE: Don't signal _NEW_TEXTURE since we've only changed
  4804.           * the texel data, not the texture format, size, etc.
  4805.           */
  4806.       }
  4807.    }
  4808.    _mesa_unlock_texture(ctx, texObj);
  4809. }
  4810.  
  4811.  
  4812. void GLAPIENTRY
  4813. _mesa_CompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset,
  4814.                               GLsizei width, GLenum format,
  4815.                               GLsizei imageSize, const GLvoid *data)
  4816. {
  4817.    struct gl_texture_object *texObj;
  4818.    struct gl_texture_image *texImage;
  4819.  
  4820.    GET_CURRENT_CONTEXT(ctx);
  4821.  
  4822.    if (compressed_subtexture_target_check(ctx, target, 1, format, false,
  4823.                                           "glCompressedTexSubImage1D")) {
  4824.       return;
  4825.    }
  4826.  
  4827.    texObj = _mesa_get_current_tex_object(ctx, target);
  4828.    if (!texObj)
  4829.       return;
  4830.  
  4831.    if (compressed_subtexture_error_check(ctx, 1, texObj, target,
  4832.                                          level, xoffset, 0, 0,
  4833.                                          width, 1, 1,
  4834.                                          format, imageSize, data,
  4835.                                          "glCompressedTexSubImage1D")) {
  4836.       return;
  4837.    }
  4838.  
  4839.    texImage = _mesa_select_tex_image(texObj, target, level);
  4840.    assert(texImage);
  4841.  
  4842.    _mesa_compressed_texture_sub_image(ctx, 1, texObj, texImage, target, level,
  4843.                                       xoffset, 0, 0, width, 1, 1,
  4844.                                       format, imageSize, data);
  4845. }
  4846.  
  4847. void GLAPIENTRY
  4848. _mesa_CompressedTextureSubImage1D(GLuint texture, GLint level, GLint xoffset,
  4849.                                   GLsizei width, GLenum format,
  4850.                                   GLsizei imageSize, const GLvoid *data)
  4851. {
  4852.    struct gl_texture_object *texObj;
  4853.    struct gl_texture_image *texImage;
  4854.  
  4855.    GET_CURRENT_CONTEXT(ctx);
  4856.  
  4857.    if (!ctx->Extensions.ARB_direct_state_access) {
  4858.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4859.                   "glCompressedTextureSubImage1D(GL_ARB_direct_state_access "
  4860.                   "is not supported)");
  4861.       return;
  4862.    }
  4863.  
  4864.    texObj = _mesa_lookup_texture_err(ctx, texture,
  4865.                                      "glCompressedTextureSubImage1D");
  4866.    if (!texObj)
  4867.       return;
  4868.  
  4869.    if (compressed_subtexture_target_check(ctx, texObj->Target, 1, format,
  4870.                                           true,
  4871.                                           "glCompressedTextureSubImage1D")) {
  4872.       return;
  4873.    }
  4874.  
  4875.    if (compressed_subtexture_error_check(ctx, 1, texObj, texObj->Target,
  4876.                                          level, xoffset, 0, 0,
  4877.                                          width, 1, 1,
  4878.                                          format, imageSize, data,
  4879.                                          "glCompressedTextureSubImage1D")) {
  4880.       return;
  4881.    }
  4882.  
  4883.    texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
  4884.    assert(texImage);
  4885.  
  4886.    _mesa_compressed_texture_sub_image(ctx, 1, texObj, texImage,
  4887.                                       texObj->Target, level,
  4888.                                       xoffset, 0, 0, width, 1, 1,
  4889.                                       format, imageSize, data);
  4890. }
  4891.  
  4892.  
  4893. void GLAPIENTRY
  4894. _mesa_CompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset,
  4895.                               GLint yoffset, GLsizei width, GLsizei height,
  4896.                               GLenum format, GLsizei imageSize,
  4897.                               const GLvoid *data)
  4898. {
  4899.    struct gl_texture_object *texObj;
  4900.    struct gl_texture_image *texImage;
  4901.  
  4902.    GET_CURRENT_CONTEXT(ctx);
  4903.  
  4904.    if (compressed_subtexture_target_check(ctx, target, 2, format, false,
  4905.                                           "glCompressedTexSubImage2D")) {
  4906.       return;
  4907.    }
  4908.  
  4909.    texObj = _mesa_get_current_tex_object(ctx, target);
  4910.    if (!texObj)
  4911.       return;
  4912.  
  4913.    if (compressed_subtexture_error_check(ctx, 2, texObj, target,
  4914.                                          level, xoffset, yoffset, 0,
  4915.                                          width, height, 1,
  4916.                                          format, imageSize, data,
  4917.                                          "glCompressedTexSubImage2D")) {
  4918.       return;
  4919.    }
  4920.  
  4921.  
  4922.    texImage = _mesa_select_tex_image(texObj, target, level);
  4923.    assert(texImage);
  4924.  
  4925.    _mesa_compressed_texture_sub_image(ctx, 2, texObj, texImage, target, level,
  4926.                                       xoffset, yoffset, 0, width, height, 1,
  4927.                                       format, imageSize, data);
  4928. }
  4929.  
  4930. void GLAPIENTRY
  4931. _mesa_CompressedTextureSubImage2D(GLuint texture, GLint level, GLint xoffset,
  4932.                                   GLint yoffset,
  4933.                                   GLsizei width, GLsizei height,
  4934.                                   GLenum format, GLsizei imageSize,
  4935.                                   const GLvoid *data)
  4936. {
  4937.    struct gl_texture_object *texObj;
  4938.    struct gl_texture_image *texImage;
  4939.  
  4940.    GET_CURRENT_CONTEXT(ctx);
  4941.  
  4942.    if (!ctx->Extensions.ARB_direct_state_access) {
  4943.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4944.                   "glCompressedTextureSubImage2D(GL_ARB_direct_state_access "
  4945.                   "is not supported)");
  4946.       return;
  4947.    }
  4948.  
  4949.    texObj = _mesa_lookup_texture_err(ctx, texture,
  4950.                                  "glCompressedTextureSubImage2D");
  4951.    if (!texObj)
  4952.       return;
  4953.  
  4954.    if (compressed_subtexture_target_check(ctx, texObj->Target, 2, format,
  4955.                                           true,
  4956.                                           "glCompressedTextureSubImage2D")) {
  4957.       return;
  4958.    }
  4959.  
  4960.    if (compressed_subtexture_error_check(ctx, 2, texObj, texObj->Target,
  4961.                                          level, xoffset, yoffset, 0,
  4962.                                          width, height, 1,
  4963.                                          format, imageSize, data,
  4964.                                          "glCompressedTextureSubImage2D")) {
  4965.       return;
  4966.    }
  4967.  
  4968.    texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
  4969.    assert(texImage);
  4970.  
  4971.    _mesa_compressed_texture_sub_image(ctx, 2, texObj, texImage,
  4972.                                       texObj->Target, level,
  4973.                                       xoffset, yoffset, 0, width, height, 1,
  4974.                                       format, imageSize, data);
  4975. }
  4976.  
  4977. void GLAPIENTRY
  4978. _mesa_CompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset,
  4979.                               GLint yoffset, GLint zoffset, GLsizei width,
  4980.                               GLsizei height, GLsizei depth, GLenum format,
  4981.                               GLsizei imageSize, const GLvoid *data)
  4982. {
  4983.    struct gl_texture_object *texObj;
  4984.    struct gl_texture_image *texImage;
  4985.  
  4986.    GET_CURRENT_CONTEXT(ctx);
  4987.  
  4988.    if (compressed_subtexture_target_check(ctx, target, 3, format, false,
  4989.                                           "glCompressedTexSubImage3D")) {
  4990.       return;
  4991.    }
  4992.  
  4993.    texObj = _mesa_get_current_tex_object(ctx, target);
  4994.    if (!texObj)
  4995.       return;
  4996.  
  4997.    if (compressed_subtexture_error_check(ctx, 3, texObj, target,
  4998.                                          level, xoffset, yoffset, zoffset,
  4999.                                          width, height, depth,
  5000.                                          format, imageSize, data,
  5001.                                          "glCompressedTexSubImage3D")) {
  5002.       return;
  5003.    }
  5004.  
  5005.  
  5006.    texImage = _mesa_select_tex_image(texObj, target, level);
  5007.    assert(texImage);
  5008.  
  5009.    _mesa_compressed_texture_sub_image(ctx, 3, texObj, texImage, target, level,
  5010.                                       xoffset, yoffset, zoffset,
  5011.                                       width, height, depth,
  5012.                                       format, imageSize, data);
  5013. }
  5014.  
  5015. void GLAPIENTRY
  5016. _mesa_CompressedTextureSubImage3D(GLuint texture, GLint level, GLint xoffset,
  5017.                                   GLint yoffset, GLint zoffset, GLsizei width,
  5018.                                   GLsizei height, GLsizei depth,
  5019.                                   GLenum format, GLsizei imageSize,
  5020.                                   const GLvoid *data)
  5021. {
  5022.    struct gl_texture_object *texObj;
  5023.    struct gl_texture_image *texImage;
  5024.  
  5025.    GET_CURRENT_CONTEXT(ctx);
  5026.  
  5027.    if (!ctx->Extensions.ARB_direct_state_access) {
  5028.       _mesa_error(ctx, GL_INVALID_OPERATION,
  5029.                   "glCompressedTextureSubImage3D(GL_ARB_direct_state_access "
  5030.                   "is not supported)");
  5031.       return;
  5032.    }
  5033.  
  5034.    texObj = _mesa_lookup_texture_err(ctx, texture,
  5035.                                      "glCompressedTextureSubImage3D");
  5036.    if (!texObj)
  5037.       return;
  5038.  
  5039.    if (compressed_subtexture_target_check(ctx, texObj->Target, 3, format,
  5040.                                           true,
  5041.                                           "glCompressedTextureSubImage3D")) {
  5042.       return;
  5043.    }
  5044.  
  5045.    if (compressed_subtexture_error_check(ctx, 3, texObj, texObj->Target,
  5046.                                          level, xoffset, yoffset, zoffset,
  5047.                                          width, height, depth,
  5048.                                          format, imageSize, data,
  5049.                                          "glCompressedTextureSubImage3D")) {
  5050.       return;
  5051.    }
  5052.  
  5053.    /* Must handle special case GL_TEXTURE_CUBE_MAP. */
  5054.    if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
  5055.       const char *pixels = data;
  5056.       int i;
  5057.       GLint image_stride;
  5058.  
  5059.       /* Make sure the texture object is a proper cube.
  5060.        * (See texturesubimage in teximage.c for details on why this check is
  5061.        * performed.)
  5062.        */
  5063.       if (!_mesa_cube_level_complete(texObj, level)) {
  5064.          _mesa_error(ctx, GL_INVALID_OPERATION,
  5065.                      "glCompressedTextureSubImage3D(cube map incomplete)");
  5066.          return;
  5067.       }
  5068.  
  5069.       /* Copy in each face. */
  5070.       for (i = 0; i < 6; ++i) {
  5071.          texImage = texObj->Image[i][level];
  5072.          assert(texImage);
  5073.  
  5074.          _mesa_compressed_texture_sub_image(ctx, 3, texObj, texImage,
  5075.                                             texObj->Target, level,
  5076.                                             xoffset, yoffset, zoffset,
  5077.                                             width, height, 1,
  5078.                                             format, imageSize, pixels);
  5079.  
  5080.          /* Compressed images don't have a client format */
  5081.          image_stride = _mesa_format_image_size(texImage->TexFormat,
  5082.                                                 texImage->Width,
  5083.                                                 texImage->Height, 1);
  5084.  
  5085.          pixels += image_stride;
  5086.          imageSize -= image_stride;
  5087.       }
  5088.    }
  5089.    else {
  5090.       texImage = _mesa_select_tex_image(texObj, texObj->Target, level);
  5091.       assert(texImage);
  5092.  
  5093.       _mesa_compressed_texture_sub_image(ctx, 3, texObj, texImage,
  5094.                                          texObj->Target, level,
  5095.                                          xoffset, yoffset, zoffset,
  5096.                                          width, height, depth,
  5097.                                          format, imageSize, data);
  5098.    }
  5099. }
  5100.  
  5101. static mesa_format
  5102. get_texbuffer_format(const struct gl_context *ctx, GLenum internalFormat)
  5103. {
  5104.    if (ctx->API != API_OPENGL_CORE) {
  5105.       switch (internalFormat) {
  5106.       case GL_ALPHA8:
  5107.          return MESA_FORMAT_A_UNORM8;
  5108.       case GL_ALPHA16:
  5109.          return MESA_FORMAT_A_UNORM16;
  5110.       case GL_ALPHA16F_ARB:
  5111.          return MESA_FORMAT_A_FLOAT16;
  5112.       case GL_ALPHA32F_ARB:
  5113.          return MESA_FORMAT_A_FLOAT32;
  5114.       case GL_ALPHA8I_EXT:
  5115.          return MESA_FORMAT_A_SINT8;
  5116.       case GL_ALPHA16I_EXT:
  5117.          return MESA_FORMAT_A_SINT16;
  5118.       case GL_ALPHA32I_EXT:
  5119.          return MESA_FORMAT_A_SINT32;
  5120.       case GL_ALPHA8UI_EXT:
  5121.          return MESA_FORMAT_A_UINT8;
  5122.       case GL_ALPHA16UI_EXT:
  5123.          return MESA_FORMAT_A_UINT16;
  5124.       case GL_ALPHA32UI_EXT:
  5125.          return MESA_FORMAT_A_UINT32;
  5126.       case GL_LUMINANCE8:
  5127.          return MESA_FORMAT_L_UNORM8;
  5128.       case GL_LUMINANCE16:
  5129.          return MESA_FORMAT_L_UNORM16;
  5130.       case GL_LUMINANCE16F_ARB:
  5131.          return MESA_FORMAT_L_FLOAT16;
  5132.       case GL_LUMINANCE32F_ARB:
  5133.          return MESA_FORMAT_L_FLOAT32;
  5134.       case GL_LUMINANCE8I_EXT:
  5135.          return MESA_FORMAT_L_SINT8;
  5136.       case GL_LUMINANCE16I_EXT:
  5137.          return MESA_FORMAT_L_SINT16;
  5138.       case GL_LUMINANCE32I_EXT:
  5139.          return MESA_FORMAT_L_SINT32;
  5140.       case GL_LUMINANCE8UI_EXT:
  5141.          return MESA_FORMAT_L_UINT8;
  5142.       case GL_LUMINANCE16UI_EXT:
  5143.          return MESA_FORMAT_L_UINT16;
  5144.       case GL_LUMINANCE32UI_EXT:
  5145.          return MESA_FORMAT_L_UINT32;
  5146.       case GL_LUMINANCE8_ALPHA8:
  5147.          return MESA_FORMAT_L8A8_UNORM;
  5148.       case GL_LUMINANCE16_ALPHA16:
  5149.          return MESA_FORMAT_L16A16_UNORM;
  5150.       case GL_LUMINANCE_ALPHA16F_ARB:
  5151.          return MESA_FORMAT_LA_FLOAT16;
  5152.       case GL_LUMINANCE_ALPHA32F_ARB:
  5153.          return MESA_FORMAT_LA_FLOAT32;
  5154.       case GL_LUMINANCE_ALPHA8I_EXT:
  5155.          return MESA_FORMAT_LA_SINT8;
  5156.       case GL_LUMINANCE_ALPHA16I_EXT:
  5157.          return MESA_FORMAT_LA_SINT16;
  5158.       case GL_LUMINANCE_ALPHA32I_EXT:
  5159.          return MESA_FORMAT_LA_SINT32;
  5160.       case GL_LUMINANCE_ALPHA8UI_EXT:
  5161.          return MESA_FORMAT_LA_UINT8;
  5162.       case GL_LUMINANCE_ALPHA16UI_EXT:
  5163.          return MESA_FORMAT_LA_UINT16;
  5164.       case GL_LUMINANCE_ALPHA32UI_EXT:
  5165.          return MESA_FORMAT_LA_UINT32;
  5166.       case GL_INTENSITY8:
  5167.          return MESA_FORMAT_I_UNORM8;
  5168.       case GL_INTENSITY16:
  5169.          return MESA_FORMAT_I_UNORM16;
  5170.       case GL_INTENSITY16F_ARB:
  5171.          return MESA_FORMAT_I_FLOAT16;
  5172.       case GL_INTENSITY32F_ARB:
  5173.          return MESA_FORMAT_I_FLOAT32;
  5174.       case GL_INTENSITY8I_EXT:
  5175.          return MESA_FORMAT_I_SINT8;
  5176.       case GL_INTENSITY16I_EXT:
  5177.          return MESA_FORMAT_I_SINT16;
  5178.       case GL_INTENSITY32I_EXT:
  5179.          return MESA_FORMAT_I_SINT32;
  5180.       case GL_INTENSITY8UI_EXT:
  5181.          return MESA_FORMAT_I_UINT8;
  5182.       case GL_INTENSITY16UI_EXT:
  5183.          return MESA_FORMAT_I_UINT16;
  5184.       case GL_INTENSITY32UI_EXT:
  5185.          return MESA_FORMAT_I_UINT32;
  5186.       default:
  5187.          break;
  5188.       }
  5189.    }
  5190.  
  5191.    if (ctx->API == API_OPENGL_CORE &&
  5192.        ctx->Extensions.ARB_texture_buffer_object_rgb32) {
  5193.       switch (internalFormat) {
  5194.       case GL_RGB32F:
  5195.          return MESA_FORMAT_RGB_FLOAT32;
  5196.       case GL_RGB32UI:
  5197.          return MESA_FORMAT_RGB_UINT32;
  5198.       case GL_RGB32I:
  5199.          return MESA_FORMAT_RGB_SINT32;
  5200.       default:
  5201.          break;
  5202.       }
  5203.    }
  5204.  
  5205.    switch (internalFormat) {
  5206.    case GL_RGBA8:
  5207.       return MESA_FORMAT_R8G8B8A8_UNORM;
  5208.    case GL_RGBA16:
  5209.       return MESA_FORMAT_RGBA_UNORM16;
  5210.    case GL_RGBA16F_ARB:
  5211.       return MESA_FORMAT_RGBA_FLOAT16;
  5212.    case GL_RGBA32F_ARB:
  5213.       return MESA_FORMAT_RGBA_FLOAT32;
  5214.    case GL_RGBA8I_EXT:
  5215.       return MESA_FORMAT_RGBA_SINT8;
  5216.    case GL_RGBA16I_EXT:
  5217.       return MESA_FORMAT_RGBA_SINT16;
  5218.    case GL_RGBA32I_EXT:
  5219.       return MESA_FORMAT_RGBA_SINT32;
  5220.    case GL_RGBA8UI_EXT:
  5221.       return MESA_FORMAT_RGBA_UINT8;
  5222.    case GL_RGBA16UI_EXT:
  5223.       return MESA_FORMAT_RGBA_UINT16;
  5224.    case GL_RGBA32UI_EXT:
  5225.       return MESA_FORMAT_RGBA_UINT32;
  5226.  
  5227.    case GL_RG8:
  5228.       return MESA_FORMAT_R8G8_UNORM;
  5229.    case GL_RG16:
  5230.       return MESA_FORMAT_R16G16_UNORM;
  5231.    case GL_RG16F:
  5232.       return MESA_FORMAT_RG_FLOAT16;
  5233.    case GL_RG32F:
  5234.       return MESA_FORMAT_RG_FLOAT32;
  5235.    case GL_RG8I:
  5236.       return MESA_FORMAT_RG_SINT8;
  5237.    case GL_RG16I:
  5238.       return MESA_FORMAT_RG_SINT16;
  5239.    case GL_RG32I:
  5240.       return MESA_FORMAT_RG_SINT32;
  5241.    case GL_RG8UI:
  5242.       return MESA_FORMAT_RG_UINT8;
  5243.    case GL_RG16UI:
  5244.       return MESA_FORMAT_RG_UINT16;
  5245.    case GL_RG32UI:
  5246.       return MESA_FORMAT_RG_UINT32;
  5247.  
  5248.    case GL_R8:
  5249.       return MESA_FORMAT_R_UNORM8;
  5250.    case GL_R16:
  5251.       return MESA_FORMAT_R_UNORM16;
  5252.    case GL_R16F:
  5253.       return MESA_FORMAT_R_FLOAT16;
  5254.    case GL_R32F:
  5255.       return MESA_FORMAT_R_FLOAT32;
  5256.    case GL_R8I:
  5257.       return MESA_FORMAT_R_SINT8;
  5258.    case GL_R16I:
  5259.       return MESA_FORMAT_R_SINT16;
  5260.    case GL_R32I:
  5261.       return MESA_FORMAT_R_SINT32;
  5262.    case GL_R8UI:
  5263.       return MESA_FORMAT_R_UINT8;
  5264.    case GL_R16UI:
  5265.       return MESA_FORMAT_R_UINT16;
  5266.    case GL_R32UI:
  5267.       return MESA_FORMAT_R_UINT32;
  5268.  
  5269.    default:
  5270.       return MESA_FORMAT_NONE;
  5271.    }
  5272. }
  5273.  
  5274.  
  5275. mesa_format
  5276. _mesa_validate_texbuffer_format(const struct gl_context *ctx,
  5277.                                 GLenum internalFormat)
  5278. {
  5279.    mesa_format format = get_texbuffer_format(ctx, internalFormat);
  5280.    GLenum datatype;
  5281.  
  5282.    if (format == MESA_FORMAT_NONE)
  5283.       return MESA_FORMAT_NONE;
  5284.  
  5285.    datatype = _mesa_get_format_datatype(format);
  5286.  
  5287.    /* The GL_ARB_texture_buffer_object spec says:
  5288.     *
  5289.     *     "If ARB_texture_float is not supported, references to the
  5290.     *     floating-point internal formats provided by that extension should be
  5291.     *     removed, and such formats may not be passed to TexBufferARB."
  5292.     *
  5293.     * As a result, GL_HALF_FLOAT internal format depends on both
  5294.     * GL_ARB_texture_float and GL_ARB_half_float_pixel.
  5295.     */
  5296.    if ((datatype == GL_FLOAT || datatype == GL_HALF_FLOAT) &&
  5297.        !ctx->Extensions.ARB_texture_float)
  5298.       return MESA_FORMAT_NONE;
  5299.  
  5300.    if (!ctx->Extensions.ARB_texture_rg) {
  5301.       GLenum base_format = _mesa_get_format_base_format(format);
  5302.       if (base_format == GL_R || base_format == GL_RG)
  5303.          return MESA_FORMAT_NONE;
  5304.    }
  5305.  
  5306.    if (!ctx->Extensions.ARB_texture_buffer_object_rgb32) {
  5307.       GLenum base_format = _mesa_get_format_base_format(format);
  5308.       if (base_format == GL_RGB)
  5309.          return MESA_FORMAT_NONE;
  5310.    }
  5311.    return format;
  5312. }
  5313.  
  5314.  
  5315. void
  5316. _mesa_texture_buffer_range(struct gl_context *ctx,
  5317.                            struct gl_texture_object *texObj,
  5318.                            GLenum internalFormat,
  5319.                            struct gl_buffer_object *bufObj,
  5320.                            GLintptr offset, GLsizeiptr size,
  5321.                            const char *caller)
  5322. {
  5323.    mesa_format format;
  5324.  
  5325.    /* NOTE: ARB_texture_buffer_object has interactions with
  5326.     * the compatibility profile that are not implemented.
  5327.     */
  5328.    if (!(ctx->API == API_OPENGL_CORE &&
  5329.          ctx->Extensions.ARB_texture_buffer_object)) {
  5330.       _mesa_error(ctx, GL_INVALID_OPERATION,
  5331.                   "%s(ARB_texture_buffer_object is not"
  5332.                   " implemented for the compatibility profile)", caller);
  5333.       return;
  5334.    }
  5335.  
  5336.    format = _mesa_validate_texbuffer_format(ctx, internalFormat);
  5337.    if (format == MESA_FORMAT_NONE) {
  5338.       _mesa_error(ctx, GL_INVALID_ENUM,
  5339.                   "%s(internalFormat 0x%x)", caller, internalFormat);
  5340.       return;
  5341.    }
  5342.  
  5343.    FLUSH_VERTICES(ctx, 0);
  5344.  
  5345.    _mesa_lock_texture(ctx, texObj);
  5346.    {
  5347.       _mesa_reference_buffer_object(ctx, &texObj->BufferObject, bufObj);
  5348.       texObj->BufferObjectFormat = internalFormat;
  5349.       texObj->_BufferObjectFormat = format;
  5350.       texObj->BufferOffset = offset;
  5351.       texObj->BufferSize = size;
  5352.    }
  5353.    _mesa_unlock_texture(ctx, texObj);
  5354.  
  5355.    ctx->NewDriverState |= ctx->DriverFlags.NewTextureBuffer;
  5356.  
  5357.    if (bufObj) {
  5358.       bufObj->UsageHistory |= USAGE_TEXTURE_BUFFER;
  5359.    }
  5360. }
  5361.  
  5362.  
  5363. /**
  5364.  * Make sure the texture buffer target is GL_TEXTURE_BUFFER.
  5365.  * Return true if it is, and return false if it is not
  5366.  * (and throw INVALID ENUM as dictated in the OpenGL 4.5
  5367.  * core spec, 02.02.2015, PDF page 245).
  5368.  */
  5369. static bool
  5370. check_texture_buffer_target(struct gl_context *ctx, GLenum target,
  5371.                             const char *caller)
  5372. {
  5373.    if (target != GL_TEXTURE_BUFFER_ARB) {
  5374.       _mesa_error(ctx, GL_INVALID_ENUM,
  5375.                   "%s(texture target is not GL_TEXTURE_BUFFER)", caller);
  5376.       return false;
  5377.    }
  5378.    else
  5379.       return true;
  5380. }
  5381.  
  5382. /**
  5383.  * Check for errors related to the texture buffer range.
  5384.  * Return false if errors are found, true if none are found.
  5385.  */
  5386. static bool
  5387. check_texture_buffer_range(struct gl_context *ctx,
  5388.                            struct gl_buffer_object *bufObj,
  5389.                            GLintptr offset, GLsizeiptr size,
  5390.                            const char *caller)
  5391. {
  5392.    /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
  5393.     * Textures (PDF page 245):
  5394.     *    "An INVALID_VALUE error is generated if offset is negative, if
  5395.     *    size is less than or equal to zero, or if offset + size is greater
  5396.     *    than the value of BUFFER_SIZE for the buffer bound to target."
  5397.     */
  5398.    if (offset < 0) {
  5399.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d < 0)", caller,
  5400.                   (int) offset);
  5401.       return false;
  5402.    }
  5403.  
  5404.    if (size <= 0) {
  5405.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d <= 0)", caller,
  5406.                   (int) size);
  5407.       return false;
  5408.    }
  5409.  
  5410.    if (offset + size > bufObj->Size) {
  5411.       _mesa_error(ctx, GL_INVALID_VALUE,
  5412.                   "%s(offset=%d + size=%d > buffer_size=%d)", caller,
  5413.                   (int) offset, (int) size, (int) bufObj->Size);
  5414.       return false;
  5415.    }
  5416.  
  5417.    /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
  5418.     * Textures (PDF page 245):
  5419.     *    "An INVALID_VALUE error is generated if offset is not an integer
  5420.     *    multiple of the value of TEXTURE_BUFFER_OFFSET_ALIGNMENT."
  5421.     */
  5422.    if (offset % ctx->Const.TextureBufferOffsetAlignment) {
  5423.       _mesa_error(ctx, GL_INVALID_VALUE,
  5424.                   "%s(invalid offset alignment)", caller);
  5425.       return false;
  5426.    }
  5427.  
  5428.    return true;
  5429. }
  5430.  
  5431.  
  5432. /** GL_ARB_texture_buffer_object */
  5433. void GLAPIENTRY
  5434. _mesa_TexBuffer(GLenum target, GLenum internalFormat, GLuint buffer)
  5435. {
  5436.    struct gl_texture_object *texObj;
  5437.    struct gl_buffer_object *bufObj;
  5438.  
  5439.    GET_CURRENT_CONTEXT(ctx);
  5440.  
  5441.    /* Need to catch a bad target before it gets to
  5442.     * _mesa_get_current_tex_object.
  5443.     */
  5444.    if (!check_texture_buffer_target(ctx, target, "glTexBuffer"))
  5445.       return;
  5446.  
  5447.    if (buffer) {
  5448.       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBuffer");
  5449.       if (!bufObj)
  5450.          return;
  5451.    } else
  5452.       bufObj = NULL;
  5453.  
  5454.    texObj = _mesa_get_current_tex_object(ctx, target);
  5455.    if (!texObj)
  5456.       return;
  5457.  
  5458.    _mesa_texture_buffer_range(ctx, texObj, internalFormat, bufObj, 0,
  5459.                               buffer ? -1 : 0, "glTexBuffer");
  5460. }
  5461.  
  5462.  
  5463. /** GL_ARB_texture_buffer_range */
  5464. void GLAPIENTRY
  5465. _mesa_TexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer,
  5466.                      GLintptr offset, GLsizeiptr size)
  5467. {
  5468.    struct gl_texture_object *texObj;
  5469.    struct gl_buffer_object *bufObj;
  5470.  
  5471.    GET_CURRENT_CONTEXT(ctx);
  5472.  
  5473.    /* Need to catch a bad target before it gets to
  5474.     * _mesa_get_current_tex_object.
  5475.     */
  5476.    if (!check_texture_buffer_target(ctx, target, "glTexBufferRange"))
  5477.       return;
  5478.  
  5479.    if (buffer) {
  5480.       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTexBufferRange");
  5481.       if (!bufObj)
  5482.          return;
  5483.  
  5484.       if (!check_texture_buffer_range(ctx, bufObj, offset, size,
  5485.           "glTexBufferRange"))
  5486.          return;
  5487.  
  5488.    } else {
  5489.  
  5490.       /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
  5491.        * Textures (PDF page 254):
  5492.        *    "If buffer is zero, then any buffer object attached to the buffer
  5493.        *    texture is detached, the values offset and size are ignored and
  5494.        *    the state for offset and size for the buffer texture are reset to
  5495.        *    zero."
  5496.        */
  5497.       offset = 0;
  5498.       size = 0;
  5499.       bufObj = NULL;
  5500.    }
  5501.  
  5502.    texObj = _mesa_get_current_tex_object(ctx, target);
  5503.    if (!texObj)
  5504.       return;
  5505.  
  5506.    _mesa_texture_buffer_range(ctx, texObj, internalFormat, bufObj,
  5507.                               offset, size, "glTexBufferRange");
  5508. }
  5509.  
  5510. void GLAPIENTRY
  5511. _mesa_TextureBuffer(GLuint texture, GLenum internalFormat, GLuint buffer)
  5512. {
  5513.    struct gl_texture_object *texObj;
  5514.    struct gl_buffer_object *bufObj;
  5515.  
  5516.    GET_CURRENT_CONTEXT(ctx);
  5517.  
  5518.    if (!ctx->Extensions.ARB_direct_state_access) {
  5519.       _mesa_error(ctx, GL_INVALID_OPERATION,
  5520.                   "glTextureBuffer(GL_ARB_direct_state_access "
  5521.                   "is not supported)");
  5522.       return;
  5523.    }
  5524.  
  5525.    if (buffer) {
  5526.       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glTextureBuffer");
  5527.       if (!bufObj)
  5528.          return;
  5529.    } else
  5530.       bufObj = NULL;
  5531.  
  5532.    /* Get the texture object by Name. */
  5533.    texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBuffer");
  5534.    if (!texObj)
  5535.       return;
  5536.  
  5537.    if (!check_texture_buffer_target(ctx, texObj->Target, "glTextureBuffer"))
  5538.       return;
  5539.  
  5540.    _mesa_texture_buffer_range(ctx, texObj, internalFormat,
  5541.                               bufObj, 0, buffer ? -1 : 0, "glTextureBuffer");
  5542. }
  5543.  
  5544. void GLAPIENTRY
  5545. _mesa_TextureBufferRange(GLuint texture, GLenum internalFormat, GLuint buffer,
  5546.                          GLintptr offset, GLsizeiptr size)
  5547. {
  5548.    struct gl_texture_object *texObj;
  5549.    struct gl_buffer_object *bufObj;
  5550.  
  5551.    GET_CURRENT_CONTEXT(ctx);
  5552.  
  5553.    if (!ctx->Extensions.ARB_direct_state_access) {
  5554.       _mesa_error(ctx, GL_INVALID_OPERATION,
  5555.                   "glTextureBufferRange(GL_ARB_direct_state_access "
  5556.                   "is not supported)");
  5557.       return;
  5558.    }
  5559.  
  5560.    if (buffer) {
  5561.       bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
  5562.                                           "glTextureBufferRange");
  5563.       if (!bufObj)
  5564.          return;
  5565.  
  5566.       if (!check_texture_buffer_range(ctx, bufObj, offset, size,
  5567.           "glTextureBufferRange"))
  5568.          return;
  5569.  
  5570.    } else {
  5571.  
  5572.       /* OpenGL 4.5 core spec (02.02.2015) says in Section 8.9 Buffer
  5573.        * Textures (PDF page 254):
  5574.        *    "If buffer is zero, then any buffer object attached to the buffer
  5575.        *    texture is detached, the values offset and size are ignored and
  5576.        *    the state for offset and size for the buffer texture are reset to
  5577.        *    zero."
  5578.        */
  5579.       offset = 0;
  5580.       size = 0;
  5581.       bufObj = NULL;
  5582.    }
  5583.  
  5584.    /* Get the texture object by Name. */
  5585.    texObj = _mesa_lookup_texture_err(ctx, texture, "glTextureBufferRange");
  5586.    if (!texObj)
  5587.       return;
  5588.  
  5589.    if (!check_texture_buffer_target(ctx, texObj->Target,
  5590.        "glTextureBufferRange"))
  5591.       return;
  5592.  
  5593.    _mesa_texture_buffer_range(ctx, texObj, internalFormat,
  5594.                               bufObj, offset, size, "glTextureBufferRange");
  5595. }
  5596.  
  5597. static GLboolean
  5598. is_renderable_texture_format(struct gl_context *ctx, GLenum internalformat)
  5599. {
  5600.    /* Everything that is allowed for renderbuffers,
  5601.     * except for a base format of GL_STENCIL_INDEX.
  5602.     */
  5603.    GLenum baseFormat = _mesa_base_fbo_format(ctx, internalformat);
  5604.    return baseFormat != 0 && baseFormat != GL_STENCIL_INDEX;
  5605. }
  5606.  
  5607.  
  5608. /** GL_ARB_texture_multisample */
  5609. static GLboolean
  5610. check_multisample_target(GLuint dims, GLenum target, bool dsa)
  5611. {
  5612.    switch(target) {
  5613.    case GL_TEXTURE_2D_MULTISAMPLE:
  5614.       return dims == 2;
  5615.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  5616.       return dims == 2 && !dsa;
  5617.  
  5618.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  5619.       return dims == 3;
  5620.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  5621.       return dims == 3 && !dsa;
  5622.  
  5623.    default:
  5624.       return GL_FALSE;
  5625.    }
  5626. }
  5627.  
  5628.  
  5629. void
  5630. _mesa_texture_image_multisample(struct gl_context *ctx, GLuint dims,
  5631.                                 struct gl_texture_object *texObj,
  5632.                                 GLenum target, GLsizei samples,
  5633.                                 GLint internalformat, GLsizei width,
  5634.                                 GLsizei height, GLsizei depth,
  5635.                                 GLboolean fixedsamplelocations,
  5636.                                 GLboolean immutable, const char *func)
  5637. {
  5638.    struct gl_texture_image *texImage;
  5639.    GLboolean sizeOK, dimensionsOK, samplesOK;
  5640.    mesa_format texFormat;
  5641.    GLenum sample_count_error;
  5642.    bool dsa = strstr(func, "ture") ? true : false;
  5643.  
  5644.    if (!(ctx->Extensions.ARB_texture_multisample
  5645.       && _mesa_is_desktop_gl(ctx))) {
  5646.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
  5647.       return;
  5648.    }
  5649.  
  5650.    if (!check_multisample_target(dims, target, dsa)) {
  5651.       if (dsa) {
  5652.          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(target)", func);
  5653.          return;
  5654.       }
  5655.       else {
  5656.          _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
  5657.          return;
  5658.       }
  5659.    }
  5660.  
  5661.    /* check that the specified internalformat is color/depth/stencil-renderable;
  5662.     * refer GL3.1 spec 4.4.4
  5663.     */
  5664.  
  5665.    if (immutable && !_mesa_is_legal_tex_storage_format(ctx, internalformat)) {
  5666.       _mesa_error(ctx, GL_INVALID_ENUM,
  5667.             "%s(internalformat=%s not legal for immutable-format)",
  5668.             func, _mesa_lookup_enum_by_nr(internalformat));
  5669.       return;
  5670.    }
  5671.  
  5672.    if (!is_renderable_texture_format(ctx, internalformat)) {
  5673.       _mesa_error(ctx, GL_INVALID_OPERATION,
  5674.             "%s(internalformat=%s)",
  5675.             func, _mesa_lookup_enum_by_nr(internalformat));
  5676.       return;
  5677.    }
  5678.  
  5679.    sample_count_error = _mesa_check_sample_count(ctx, target,
  5680.          internalformat, samples);
  5681.    samplesOK = sample_count_error == GL_NO_ERROR;
  5682.  
  5683.    /* Page 254 of OpenGL 4.4 spec says:
  5684.     *   "Proxy arrays for two-dimensional multisample and two-dimensional
  5685.     *    multisample array textures are operated on in the same way when
  5686.     *    TexImage2DMultisample is called with target specified as
  5687.     *    PROXY_TEXTURE_2D_MULTISAMPLE, or TexImage3DMultisample is called
  5688.     *    with target specified as PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY.
  5689.     *    However, if samples is not supported, then no error is generated.
  5690.     */
  5691.    if (!samplesOK && !_mesa_is_proxy_texture(target)) {
  5692.       _mesa_error(ctx, sample_count_error, "%s(samples)", func);
  5693.       return;
  5694.    }
  5695.  
  5696.    if (immutable && (!texObj || (texObj->Name == 0))) {
  5697.       _mesa_error(ctx, GL_INVALID_OPERATION,
  5698.             "%s(texture object 0)",
  5699.             func);
  5700.       return;
  5701.    }
  5702.  
  5703.    texImage = _mesa_get_tex_image(ctx, texObj, 0, 0);
  5704.  
  5705.    if (texImage == NULL) {
  5706.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s()", func);
  5707.       return;
  5708.    }
  5709.  
  5710.    texFormat = _mesa_choose_texture_format(ctx, texObj, target, 0,
  5711.          internalformat, GL_NONE, GL_NONE);
  5712.    assert(texFormat != MESA_FORMAT_NONE);
  5713.  
  5714.    dimensionsOK = _mesa_legal_texture_dimensions(ctx, target, 0,
  5715.          width, height, depth, 0);
  5716.  
  5717.    sizeOK = ctx->Driver.TestProxyTexImage(ctx, target, 0, texFormat,
  5718.          width, height, depth, 0);
  5719.  
  5720.    if (_mesa_is_proxy_texture(target)) {
  5721.       if (samplesOK && dimensionsOK && sizeOK) {
  5722.          init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
  5723.                                  internalformat, texFormat,
  5724.                                  samples, fixedsamplelocations);
  5725.       }
  5726.       else {
  5727.          /* clear all image fields */
  5728.          clear_teximage_fields(texImage);
  5729.       }
  5730.    }
  5731.    else {
  5732.       if (!dimensionsOK) {
  5733.          _mesa_error(ctx, GL_INVALID_VALUE,
  5734.                "%s(invalid width or height)", func);
  5735.          return;
  5736.       }
  5737.  
  5738.       if (!sizeOK) {
  5739.          _mesa_error(ctx, GL_OUT_OF_MEMORY,
  5740.                "%s(texture too large)", func);
  5741.          return;
  5742.       }
  5743.  
  5744.       /* Check if texObj->Immutable is set */
  5745.       if (texObj->Immutable) {
  5746.          _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
  5747.          return;
  5748.       }
  5749.  
  5750.       ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
  5751.  
  5752.       init_teximage_fields_ms(ctx, texImage, width, height, depth, 0,
  5753.                               internalformat, texFormat,
  5754.                               samples, fixedsamplelocations);
  5755.  
  5756.       if (width > 0 && height > 0 && depth > 0) {
  5757.          if (!ctx->Driver.AllocTextureStorage(ctx, texObj, 1,
  5758.                   width, height, depth)) {
  5759.             /* tidy up the texture image state. strictly speaking,
  5760.              * we're allowed to just leave this in whatever state we
  5761.              * like, but being tidy is good.
  5762.              */
  5763.             _mesa_init_teximage_fields(ctx, texImage,
  5764.                   0, 0, 0, 0, GL_NONE, MESA_FORMAT_NONE);
  5765.          }
  5766.       }
  5767.  
  5768.       texObj->Immutable |= immutable;
  5769.  
  5770.       if (immutable) {
  5771.          _mesa_set_texture_view_state(ctx, texObj, target, 1);
  5772.       }
  5773.  
  5774.       _mesa_update_fbo_texture(ctx, texObj, 0, 0);
  5775.    }
  5776. }
  5777.  
  5778.  
  5779. void GLAPIENTRY
  5780. _mesa_TexImage2DMultisample(GLenum target, GLsizei samples,
  5781.                             GLenum internalformat, GLsizei width,
  5782.                             GLsizei height, GLboolean fixedsamplelocations)
  5783. {
  5784.    struct gl_texture_object *texObj;
  5785.    GET_CURRENT_CONTEXT(ctx);
  5786.  
  5787.    texObj = _mesa_get_current_tex_object(ctx, target);
  5788.    if (!texObj)
  5789.       return;
  5790.  
  5791.    _mesa_texture_image_multisample(ctx, 2, texObj, target, samples,
  5792.                                    internalformat, width, height, 1,
  5793.                                    fixedsamplelocations, GL_FALSE,
  5794.                                    "glTexImage2DMultisample");
  5795. }
  5796.  
  5797.  
  5798. void GLAPIENTRY
  5799. _mesa_TexImage3DMultisample(GLenum target, GLsizei samples,
  5800.                             GLenum internalformat, GLsizei width,
  5801.                             GLsizei height, GLsizei depth,
  5802.                             GLboolean fixedsamplelocations)
  5803. {
  5804.    struct gl_texture_object *texObj;
  5805.    GET_CURRENT_CONTEXT(ctx);
  5806.  
  5807.    texObj = _mesa_get_current_tex_object(ctx, target);
  5808.    if (!texObj)
  5809.       return;
  5810.  
  5811.    _mesa_texture_image_multisample(ctx, 3, texObj, target, samples,
  5812.                                    internalformat, width, height, depth,
  5813.                                    fixedsamplelocations, GL_FALSE,
  5814.                                    "glTexImage3DMultisample");
  5815. }
  5816.  
  5817.  
  5818. void GLAPIENTRY
  5819. _mesa_TexStorage2DMultisample(GLenum target, GLsizei samples,
  5820.                               GLenum internalformat, GLsizei width,
  5821.                               GLsizei height, GLboolean fixedsamplelocations)
  5822. {
  5823.    struct gl_texture_object *texObj;
  5824.    GET_CURRENT_CONTEXT(ctx);
  5825.  
  5826.    texObj = _mesa_get_current_tex_object(ctx, target);
  5827.    if (!texObj)
  5828.       return;
  5829.  
  5830.    _mesa_texture_image_multisample(ctx, 2, texObj, target, samples,
  5831.                                    internalformat, width, height, 1,
  5832.                                    fixedsamplelocations, GL_TRUE,
  5833.                                    "glTexStorage2DMultisample");
  5834. }
  5835.  
  5836. void GLAPIENTRY
  5837. _mesa_TexStorage3DMultisample(GLenum target, GLsizei samples,
  5838.                               GLenum internalformat, GLsizei width,
  5839.                               GLsizei height, GLsizei depth,
  5840.                               GLboolean fixedsamplelocations)
  5841. {
  5842.    struct gl_texture_object *texObj;
  5843.    GET_CURRENT_CONTEXT(ctx);
  5844.  
  5845.    texObj = _mesa_get_current_tex_object(ctx, target);
  5846.    if (!texObj)
  5847.       return;
  5848.  
  5849.    _mesa_texture_image_multisample(ctx, 3, texObj, target, samples,
  5850.                                    internalformat, width, height, depth,
  5851.                                    fixedsamplelocations, GL_TRUE,
  5852.                                    "glTexStorage3DMultisample");
  5853. }
  5854.  
  5855. void GLAPIENTRY
  5856. _mesa_TextureStorage2DMultisample(GLuint texture, GLsizei samples,
  5857.                                   GLenum internalformat, GLsizei width,
  5858.                                   GLsizei height,
  5859.                                   GLboolean fixedsamplelocations)
  5860. {
  5861.    struct gl_texture_object *texObj;
  5862.    GET_CURRENT_CONTEXT(ctx);
  5863.  
  5864.    if (!ctx->Extensions.ARB_direct_state_access) {
  5865.       _mesa_error(ctx, GL_INVALID_OPERATION,
  5866.                   "glTextureStorage2DMultisample(GL_ARB_direct_state_access "
  5867.                   "is not supported)");
  5868.       return;
  5869.    }
  5870.  
  5871.    texObj = _mesa_lookup_texture_err(ctx, texture,
  5872.                                      "glTextureStorage2DMultisample");
  5873.    if (!texObj)
  5874.       return;
  5875.  
  5876.    _mesa_texture_image_multisample(ctx, 2, texObj, texObj->Target, samples,
  5877.                                    internalformat, width, height, 1,
  5878.                                    fixedsamplelocations, GL_TRUE,
  5879.                                    "glTextureStorage2DMultisample");
  5880. }
  5881.  
  5882. void GLAPIENTRY
  5883. _mesa_TextureStorage3DMultisample(GLuint texture, GLsizei samples,
  5884.                                   GLenum internalformat, GLsizei width,
  5885.                                   GLsizei height, GLsizei depth,
  5886.                                   GLboolean fixedsamplelocations)
  5887. {
  5888.    struct gl_texture_object *texObj;
  5889.    GET_CURRENT_CONTEXT(ctx);
  5890.  
  5891.    if (!ctx->Extensions.ARB_direct_state_access) {
  5892.       _mesa_error(ctx, GL_INVALID_OPERATION,
  5893.                   "glTextureStorage3DMultisample(GL_ARB_direct_state_access "
  5894.                   "is not supported)");
  5895.       return;
  5896.    }
  5897.  
  5898.    /* Get the texture object by Name. */
  5899.    texObj = _mesa_lookup_texture_err(ctx, texture,
  5900.                                      "glTextureStorage3DMultisample");
  5901.    if (!texObj)
  5902.       return;
  5903.  
  5904.    _mesa_texture_image_multisample(ctx, 3, texObj, texObj->Target, samples,
  5905.                                    internalformat, width, height, depth,
  5906.                                    fixedsamplelocations, GL_TRUE,
  5907.                                    "glTextureStorage3DMultisample");
  5908. }
  5909.