mesa: implement glGet for AMD_framebuffer_multisample_advanced
[mesa.git] / src / mesa / main / glformats.c
index 9274dc017753c14231ed5ebc6682cef468e31dff..667020c193c92f5ca0e21ca1257d953d294a67e2 100644 (file)
@@ -546,6 +546,7 @@ _mesa_bytes_per_vertex_attrib(GLint comps, GLenum type)
    case GL_FLOAT:
       return comps * sizeof(GLfloat);
    case GL_HALF_FLOAT_ARB:
+   case GL_HALF_FLOAT_OES:
       return comps * sizeof(GLhalfARB);
    case GL_DOUBLE:
       return comps * sizeof(GLdouble);
@@ -562,6 +563,8 @@ _mesa_bytes_per_vertex_attrib(GLint comps, GLenum type)
          return sizeof(GLuint);
       else
          return -1;
+   case GL_UNSIGNED_INT64_ARB:
+      return comps * 8;
    default:
       return -1;
    }
@@ -823,10 +826,10 @@ _mesa_is_enum_format_signed_int(GLenum format)
 }
 
 /**
- * Test if the given format is an ASTC format.
+ * Test if the given format is an ASTC 2D format.
  */
-GLboolean
-_mesa_is_astc_format(GLenum internalFormat)
+static bool
+is_astc_2d_format(GLenum internalFormat)
 {
    switch (internalFormat) {
    case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
@@ -863,6 +866,71 @@ _mesa_is_astc_format(GLenum internalFormat)
    }
 }
 
+/**
+ * Test if the given format is an ASTC 3D format.
+ */
+static bool
+is_astc_3d_format(GLenum internalFormat)
+{
+   switch (internalFormat) {
+   case GL_COMPRESSED_RGBA_ASTC_3x3x3_OES:
+   case GL_COMPRESSED_RGBA_ASTC_4x3x3_OES:
+   case GL_COMPRESSED_RGBA_ASTC_4x4x3_OES:
+   case GL_COMPRESSED_RGBA_ASTC_4x4x4_OES:
+   case GL_COMPRESSED_RGBA_ASTC_5x4x4_OES:
+   case GL_COMPRESSED_RGBA_ASTC_5x5x4_OES:
+   case GL_COMPRESSED_RGBA_ASTC_5x5x5_OES:
+   case GL_COMPRESSED_RGBA_ASTC_6x5x5_OES:
+   case GL_COMPRESSED_RGBA_ASTC_6x6x5_OES:
+   case GL_COMPRESSED_RGBA_ASTC_6x6x6_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES:
+      return true;
+   default:
+      return false;
+   }
+}
+
+/**
+ * Test if the given format is an ASTC format.
+ */
+GLboolean
+_mesa_is_astc_format(GLenum internalFormat)
+{
+   return is_astc_2d_format(internalFormat) ||
+          is_astc_3d_format(internalFormat);
+}
+
+/**
+ * Test if the given format is an ETC2 format.
+ */
+GLboolean
+_mesa_is_etc2_format(GLenum internalFormat)
+{
+   switch (internalFormat) {
+   case GL_COMPRESSED_RGB8_ETC2:
+   case GL_COMPRESSED_SRGB8_ETC2:
+   case GL_COMPRESSED_RGBA8_ETC2_EAC:
+   case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
+   case GL_COMPRESSED_R11_EAC:
+   case GL_COMPRESSED_RG11_EAC:
+   case GL_COMPRESSED_SIGNED_R11_EAC:
+   case GL_COMPRESSED_SIGNED_RG11_EAC:
+   case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
+   case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
+      return true;
+   default:
+      return false;
+   }
+}
 
 /**
  * Test if the given format is an integer (non-normalized) format.
@@ -2168,7 +2236,8 @@ _mesa_es_error_check_format_and_type(const struct gl_context *ctx,
                     || type == GL_UNSIGNED_SHORT_5_5_5_1
                     || type == GL_FLOAT
                     || type == GL_HALF_FLOAT_OES
-                    || type == GL_UNSIGNED_INT_2_10_10_10_REV);
+                    || (ctx->Extensions.EXT_texture_type_2_10_10_10_REV &&
+                        type == GL_UNSIGNED_INT_2_10_10_10_REV));
       break;
 
    case GL_DEPTH_COMPONENT:
@@ -2346,8 +2415,10 @@ _mesa_base_tex_format(const struct gl_context *ctx, GLint internalFormat)
             return base_compressed;
    }
 
-   if (ctx->Extensions.KHR_texture_compression_astc_ldr &&
-      _mesa_is_astc_format(internalFormat))
+   if ((ctx->Extensions.KHR_texture_compression_astc_ldr &&
+        is_astc_2d_format(internalFormat)) ||
+       (ctx->Extensions.OES_texture_compression_astc &&
+        is_astc_3d_format(internalFormat)))
         return GL_RGBA;
 
    if (ctx->Extensions.MESA_ycbcr_texture) {
@@ -2451,7 +2522,6 @@ _mesa_base_tex_format(const struct gl_context *ctx, GLint internalFormat)
       case GL_RGBA8I_EXT:
       case GL_RGBA16I_EXT:
       case GL_RGBA32I_EXT:
-      case GL_RGB10_A2UI:
          return GL_RGBA;
       case GL_RGB8UI_EXT:
       case GL_RGB16UI_EXT:
@@ -2463,6 +2533,13 @@ _mesa_base_tex_format(const struct gl_context *ctx, GLint internalFormat)
       }
    }
 
+   if (ctx->Extensions.ARB_texture_rgb10_a2ui) {
+      switch (internalFormat) {
+      case GL_RGB10_A2UI:
+         return GL_RGBA;
+      }
+   }
+
    if (ctx->Extensions.EXT_texture_integer) {
       switch (internalFormat) {
       case GL_ALPHA8UI_EXT:
@@ -2704,7 +2781,7 @@ _mesa_es3_effective_internal_format_for_format_and_type(GLenum format,
 
 /**
  * Do error checking of format/type combinations for OpenGL ES 3
- * glTex[Sub]Image.
+ * glTex[Sub]Image, or ES1/ES2 with GL_OES_required_internalformat.
  * \return error code, or GL_NO_ERROR.
  */
 GLenum
@@ -2767,7 +2844,10 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          case GL_RGBA8:
          case GL_RGB5_A1:
          case GL_RGBA4:
+            break;
          case GL_SRGB8_ALPHA8_EXT:
+            if (ctx->Version <= 20)
+               return GL_INVALID_OPERATION;
             break;
          default:
             return GL_INVALID_OPERATION;
@@ -2775,7 +2855,18 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          break;
 
       case GL_BYTE:
-         if (internalFormat != GL_RGBA8_SNORM)
+         if (ctx->Version <= 20 || internalFormat != GL_RGBA8_SNORM)
+            return GL_INVALID_OPERATION;
+         break;
+
+      case GL_UNSIGNED_SHORT:
+         if (!_mesa_has_EXT_texture_norm16(ctx) || internalFormat != GL_RGBA16)
+            return GL_INVALID_OPERATION;
+         break;
+
+      case GL_SHORT:
+         if (!_mesa_has_EXT_texture_norm16(ctx) ||
+             internalFormat != GL_RGBA16_SNORM)
             return GL_INVALID_OPERATION;
          break;
 
@@ -2801,9 +2892,11 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
 
       case GL_UNSIGNED_INT_2_10_10_10_REV:
          switch (internalFormat) {
-         case GL_RGBA: /* GL_EXT_texture_type_2_10_10_10_REV */
+         case GL_RGBA:
          case GL_RGB10_A2:
          case GL_RGB5_A1:
+            if (!ctx->Extensions.EXT_texture_type_2_10_10_10_REV)
+               return GL_INVALID_OPERATION;
             break;
          default:
             return GL_INVALID_OPERATION;
@@ -2811,7 +2904,7 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          break;
 
       case GL_HALF_FLOAT:
-         if (internalFormat != GL_RGBA16F)
+         if (ctx->Version <= 20 || internalFormat != GL_RGBA16F)
             return GL_INVALID_OPERATION;
          break;
 
@@ -2819,6 +2912,8 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          switch (internalFormat) {
          case GL_RGBA16F:
          case GL_RGBA32F:
+            if (ctx->Version <= 20)
+               return GL_INVALID_OPERATION;
             break;
          case GL_RGBA:
             if (ctx->Extensions.OES_texture_float && internalFormat == format)
@@ -2837,6 +2932,8 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
       break;
 
    case GL_RGBA_INTEGER:
+      if (ctx->Version <= 20)
+         return GL_INVALID_OPERATION;
       switch (type) {
       case GL_UNSIGNED_BYTE:
          if (internalFormat != GL_RGBA8UI)
@@ -2885,7 +2982,10 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          case GL_RGB:
          case GL_RGB8:
          case GL_RGB565:
+            break;
          case GL_SRGB8:
+            if (ctx->Version <= 20)
+               return GL_INVALID_OPERATION;
             break;
          default:
             return GL_INVALID_OPERATION;
@@ -2893,7 +2993,18 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          break;
 
       case GL_BYTE:
-         if (internalFormat != GL_RGB8_SNORM)
+         if (ctx->Version <= 20 || internalFormat != GL_RGB8_SNORM)
+            return GL_INVALID_OPERATION;
+         break;
+
+      case GL_UNSIGNED_SHORT:
+         if (!_mesa_has_EXT_texture_norm16(ctx) || internalFormat != GL_RGB16)
+            return GL_INVALID_OPERATION;
+         break;
+
+      case GL_SHORT:
+         if (!_mesa_has_EXT_texture_norm16(ctx) ||
+             internalFormat != GL_RGB16_SNORM)
             return GL_INVALID_OPERATION;
          break;
 
@@ -2908,16 +3019,18 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          break;
 
       case GL_UNSIGNED_INT_10F_11F_11F_REV:
-         if (internalFormat != GL_R11F_G11F_B10F)
+         if (ctx->Version <= 20 || internalFormat != GL_R11F_G11F_B10F)
             return GL_INVALID_OPERATION;
          break;
 
       case GL_UNSIGNED_INT_5_9_9_9_REV:
-         if (internalFormat != GL_RGB9_E5)
+         if (ctx->Version <= 20 || internalFormat != GL_RGB9_E5)
             return GL_INVALID_OPERATION;
          break;
 
       case GL_HALF_FLOAT:
+         if (ctx->Version <= 20)
+            return GL_INVALID_OPERATION;
          switch (internalFormat) {
          case GL_RGB16F:
          case GL_R11F_G11F_B10F:
@@ -2934,6 +3047,8 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          case GL_RGB32F:
          case GL_R11F_G11F_B10F:
          case GL_RGB9_E5:
+            if (ctx->Version <= 20)
+               return GL_INVALID_OPERATION;
             break;
          case GL_RGB:
             if (ctx->Extensions.OES_texture_float && internalFormat == format)
@@ -2950,7 +3065,16 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
 
       case GL_UNSIGNED_INT_2_10_10_10_REV:
          switch (internalFormat) {
-         case GL_RGB: /* GL_EXT_texture_type_2_10_10_10_REV */
+         case GL_RGB:
+         case GL_RGB10:
+         case GL_RGB8:
+         case GL_RGB565:
+            /* GL_EXT_texture_type_2_10_10_10_REV allows GL_RGB even though
+             * GLES3 doesn't, and GL_OES_required_internalformat extends that
+             * to allow the sized RGB internalformats as well.
+             */
+            if (!ctx->Extensions.EXT_texture_type_2_10_10_10_REV)
+               return GL_INVALID_OPERATION;
             break;
          default:
             return GL_INVALID_OPERATION;
@@ -2963,6 +3087,8 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
       break;
 
    case GL_RGB_INTEGER:
+      if (ctx->Version <= 20)
+         return GL_INVALID_OPERATION;
       switch (type) {
       case GL_UNSIGNED_BYTE:
          if (internalFormat != GL_RGB8UI)
@@ -3000,6 +3126,8 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
       break;
 
    case GL_RG:
+      if (!ctx->Extensions.ARB_texture_rg)
+         return GL_INVALID_OPERATION;
       switch (type) {
       case GL_UNSIGNED_BYTE:
          if (internalFormat != GL_RG8)
@@ -3011,10 +3139,23 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
             return GL_INVALID_OPERATION;
          break;
 
+      case GL_UNSIGNED_SHORT:
+         if (!_mesa_has_EXT_texture_norm16(ctx) || internalFormat != GL_RG16)
+            return GL_INVALID_OPERATION;
+         break;
+
+      case GL_SHORT:
+         if (!_mesa_has_EXT_texture_norm16(ctx) ||
+             internalFormat != GL_RG16_SNORM)
+            return GL_INVALID_OPERATION;
+         break;
+
       case GL_HALF_FLOAT:
       case GL_HALF_FLOAT_OES:
          switch (internalFormat) {
             case GL_RG16F:
+               if (ctx->Version <= 20)
+                  return GL_INVALID_OPERATION;
                break;
             case GL_RG:
                if (ctx->Extensions.ARB_texture_rg &&
@@ -3047,6 +3188,8 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
       break;
 
    case GL_RG_INTEGER:
+      if (ctx->Version <= 20)
+         return GL_INVALID_OPERATION;
       switch (type) {
       case GL_UNSIGNED_BYTE:
          if (internalFormat != GL_RG8UI)
@@ -3084,6 +3227,8 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
       break;
 
    case GL_RED:
+      if (!ctx->Extensions.ARB_texture_rg)
+         return GL_INVALID_OPERATION;
       switch (type) {
       case GL_UNSIGNED_BYTE:
          if (internalFormat != GL_R8)
@@ -3095,10 +3240,23 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
             return GL_INVALID_OPERATION;
          break;
 
+      case GL_UNSIGNED_SHORT:
+         if (!_mesa_has_EXT_texture_norm16(ctx) || internalFormat != GL_R16)
+            return GL_INVALID_OPERATION;
+         break;
+
+      case GL_SHORT:
+         if (!_mesa_has_EXT_texture_norm16(ctx) ||
+             internalFormat != GL_R16_SNORM)
+            return GL_INVALID_OPERATION;
+         break;
+
       case GL_HALF_FLOAT:
       case GL_HALF_FLOAT_OES:
          switch (internalFormat) {
          case GL_R16F:
+            if (ctx->Version <= 20)
+               return GL_INVALID_OPERATION;
             break;
          case GL_RG:
          case GL_RED:
@@ -3132,6 +3290,8 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
       break;
 
    case GL_RED_INTEGER:
+      if (ctx->Version <= 20)
+         return GL_INVALID_OPERATION;
       switch (type) {
       case GL_UNSIGNED_BYTE:
          if (internalFormat != GL_R8UI)
@@ -3188,7 +3348,7 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          break;
 
       case GL_FLOAT:
-         if (internalFormat != GL_DEPTH_COMPONENT32F)
+         if (ctx->Version <= 20 || internalFormat != GL_DEPTH_COMPONENT32F)
             return GL_INVALID_OPERATION;
          break;
 
@@ -3206,7 +3366,7 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
          break;
 
       case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
-         if (internalFormat != GL_DEPTH32F_STENCIL8)
+         if (ctx->Version <= 20 || internalFormat != GL_DEPTH32F_STENCIL8)
             return GL_INVALID_OPERATION;
          break;
 
@@ -3228,15 +3388,27 @@ _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
    case GL_LUMINANCE_ALPHA:
       switch (type) {
       case GL_FLOAT:
-         if (ctx->Extensions.OES_texture_float && internalFormat == format)
-            break;
+         if (!ctx->Extensions.OES_texture_float || internalFormat != format)
+            return GL_INVALID_OPERATION;
+         break;
       case GL_HALF_FLOAT_OES:
-         if (ctx->Extensions.OES_texture_half_float && internalFormat == format)
-            break;
-      default:
-         if (type != GL_UNSIGNED_BYTE || format != internalFormat)
+         if (!ctx->Extensions.OES_texture_half_float || internalFormat != format)
+            return GL_INVALID_OPERATION;
+         break;
+      case GL_UNSIGNED_BYTE:
+         if (!(format == internalFormat ||
+               (format == GL_ALPHA && internalFormat == GL_ALPHA8) ||
+               (format == GL_LUMINANCE && internalFormat == GL_LUMINANCE8) ||
+               (format == GL_LUMINANCE_ALPHA &&
+                ((internalFormat == GL_LUMINANCE8_ALPHA8) ||
+                 (internalFormat == GL_LUMINANCE4_ALPHA4))))) {
             return GL_INVALID_OPERATION;
+         }
+         break;
+      default:
+         return GL_INVALID_OPERATION;
       }
+      break;
    }
 
    return GL_NO_ERROR;
@@ -3558,12 +3730,28 @@ _mesa_format_from_format_and_type(GLenum format, GLenum type)
    unreachable("Unsupported format");
 }
 
+uint32_t
+_mesa_tex_format_from_format_and_type(const struct gl_context *ctx,
+                                      GLenum gl_format, GLenum type)
+{
+   mesa_format format = _mesa_format_from_format_and_type(gl_format, type);
+
+   if (_mesa_format_is_mesa_array_format(format))
+      format = _mesa_format_from_array_format(format);
+      
+   if (format == MESA_FORMAT_NONE || !ctx->TextureFormatSupported[format])
+      return MESA_FORMAT_NONE;
+
+   return format;
+}
+
 /**
  * Returns true if \p internal_format is a sized internal format that
  * is marked "Color Renderable" in Table 8.10 of the ES 3.2 specification.
  */
 bool
-_mesa_is_es3_color_renderable(GLenum internal_format)
+_mesa_is_es3_color_renderable(const struct gl_context *ctx,
+                              GLenum internal_format)
 {
    switch (internal_format) {
    case GL_R8:
@@ -3602,6 +3790,10 @@ _mesa_is_es3_color_renderable(GLenum internal_format)
    case GL_RGBA32I:
    case GL_RGBA32UI:
       return true;
+   case GL_R16:
+   case GL_RG16:
+   case GL_RGBA16:
+      return _mesa_has_EXT_texture_norm16(ctx);
    default:
       return false;
    }
@@ -3612,7 +3804,8 @@ _mesa_is_es3_color_renderable(GLenum internal_format)
  * is marked "Texture Filterable" in Table 8.10 of the ES 3.2 specification.
  */
 bool
-_mesa_is_es3_texture_filterable(GLenum internal_format)
+_mesa_is_es3_texture_filterable(const struct gl_context *ctx,
+                                GLenum internal_format)
 {
    switch (internal_format) {
    case GL_R8:
@@ -3636,6 +3829,29 @@ _mesa_is_es3_texture_filterable(GLenum internal_format)
    case GL_R11F_G11F_B10F:
    case GL_RGB9_E5:
       return true;
+   case GL_R16:
+   case GL_R16_SNORM:
+   case GL_RG16:
+   case GL_RG16_SNORM:
+   case GL_RGB16:
+   case GL_RGB16_SNORM:
+   case GL_RGBA16:
+   case GL_RGBA16_SNORM:
+      return _mesa_has_EXT_texture_norm16(ctx);
+   case GL_R32F:
+   case GL_RG32F:
+   case GL_RGB32F:
+   case GL_RGBA32F:
+      /* The OES_texture_float_linear spec says:
+       *
+       *    "When implemented against OpenGL ES 3.0 or later versions, sized
+       *     32-bit floating-point formats become texture-filterable. This
+       *     should be noted by, for example, checking the ``TF'' column of
+       *     table 8.13 in the ES 3.1 Specification (``Correspondence of sized
+       *     internal formats to base internal formats ... and use cases ...'')
+       *     for the R32F, RG32F, RGB32F, and RGBA32F formats."
+       */
+      return ctx->Extensions.OES_texture_float_linear;
    default:
       return false;
    }