mesa: refactor GenerateTextureMipmap handling
[mesa.git] / src / mesa / main / genmipmap.c
index 6eacd424df7f15974f288e39bc34d7434c8eccce..6729631b16b4282ac1b33bfd3567aa28575fe402 100644 (file)
@@ -65,8 +65,7 @@ _mesa_is_valid_generate_texture_mipmap_target(struct gl_context *ctx,
          || !ctx->Extensions.EXT_texture_array;
       break;
    case GL_TEXTURE_CUBE_MAP_ARRAY:
-      error = _mesa_is_gles(ctx) ||
-              !ctx->Extensions.ARB_texture_cube_map_array;
+      error = !_mesa_has_texture_cube_map_array(ctx);
       break;
    default:
       error = true;
@@ -79,6 +78,25 @@ bool
 _mesa_is_valid_generate_texture_mipmap_internalformat(struct gl_context *ctx,
                                                       GLenum internalformat)
 {
+   if (_mesa_is_gles3(ctx)) {
+      /* From the ES 3.2 specification's description of GenerateMipmap():
+       * "An INVALID_OPERATION error is generated if the levelbase array was
+       *  not specified with an unsized internal format from table 8.3 or a
+       *  sized internal format that is both color-renderable and
+       *  texture-filterable according to table 8.10."
+       *
+       * GL_EXT_texture_format_BGRA8888 adds a GL_BGRA_EXT unsized internal
+       * format, and includes it in a very similar looking table.  So we
+       * include it here as well.
+       */
+      return internalformat == GL_RGBA || internalformat == GL_RGB ||
+             internalformat == GL_LUMINANCE_ALPHA ||
+             internalformat == GL_LUMINANCE || internalformat == GL_ALPHA ||
+             internalformat == GL_BGRA_EXT ||
+             (_mesa_is_es3_color_renderable(ctx, internalformat) &&
+              _mesa_is_es3_texture_filterable(ctx, internalformat));
+   }
+
    return (!_mesa_is_enum_format_integer(internalformat) &&
            !_mesa_is_depthstencil_format(internalformat) &&
            !_mesa_is_astc_format(internalformat) &&
@@ -88,14 +106,14 @@ _mesa_is_valid_generate_texture_mipmap_internalformat(struct gl_context *ctx,
 /**
  * Implements glGenerateMipmap and glGenerateTextureMipmap.
  * Generates all the mipmap levels below the base level.
+ * Error-checking is done only if caller is not NULL.
  */
-void
-_mesa_generate_texture_mipmap(struct gl_context *ctx,
-                              struct gl_texture_object *texObj, GLenum target,
-                              bool dsa)
+static ALWAYS_INLINE void
+generate_texture_mipmap(struct gl_context *ctx,
+                        struct gl_texture_object *texObj, GLenum target,
+                        const char* caller)
 {
    struct gl_texture_image *srcImage;
-   const char *suffix = dsa ? "Texture" : "";
 
    FLUSH_VERTICES(ctx, 0);
 
@@ -104,28 +122,36 @@ _mesa_generate_texture_mipmap(struct gl_context *ctx,
       return;
    }
 
-   if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
+   if (caller && texObj->Target == GL_TEXTURE_CUBE_MAP &&
        !_mesa_cube_complete(texObj)) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glGenerate%sMipmap(incomplete cube map)", suffix);
+                  "%s(incomplete cube map)", caller);
       return;
    }
 
    _mesa_lock_texture(ctx, texObj);
 
    srcImage = _mesa_select_tex_image(texObj, target, texObj->BaseLevel);
-   if (!srcImage) {
-      _mesa_unlock_texture(ctx, texObj);
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glGenerate%sMipmap(zero size base image)", suffix);
-      return;
+   if (caller) {
+      if (!srcImage) {
+         _mesa_unlock_texture(ctx, texObj);
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "%s(zero size base image)", caller);
+         return;
+      }
+
+      if (!_mesa_is_valid_generate_texture_mipmap_internalformat(ctx,
+                                                                 srcImage->InternalFormat)) {
+         _mesa_unlock_texture(ctx, texObj);
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "%s(invalid internal format %s)", caller,
+                     _mesa_enum_to_string(srcImage->InternalFormat));
+         return;
+      }
    }
 
-   if (!_mesa_is_valid_generate_texture_mipmap_internalformat(ctx,
-                                                              srcImage->InternalFormat)) {
+   if (srcImage->Width == 0 || srcImage->Height == 0) {
       _mesa_unlock_texture(ctx, texObj);
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glGenerate%sMipmap(invalid internal format)", suffix);
       return;
    }
 
@@ -147,6 +173,15 @@ _mesa_generate_texture_mipmap(struct gl_context *ctx,
  * Note: this GL function would be more useful if one could specify a
  * cube face, a set of array slices, etc.
  */
+void GLAPIENTRY
+_mesa_GenerateMipmap_no_error(GLenum target)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
+   generate_texture_mipmap(ctx, texObj, target, NULL);
+}
+
 void GLAPIENTRY
 _mesa_GenerateMipmap(GLenum target)
 {
@@ -163,27 +198,45 @@ _mesa_GenerateMipmap(GLenum target)
    if (!texObj)
       return;
 
-   _mesa_generate_texture_mipmap(ctx, texObj, target, false);
+   generate_texture_mipmap(ctx, texObj, target, "glGenerateMipmap");
 }
 
 /**
  * Generate all the mipmap levels below the base level.
  */
 void GLAPIENTRY
-_mesa_GenerateTextureMipmap(GLuint texture)
+_mesa_GenerateTextureMipmap_no_error(GLuint texture)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, texture);
+   generate_texture_mipmap(ctx, texObj, texObj->Target, NULL);
+}
+
+static void
+validate_params_and_generate_mipmap(struct gl_texture_object *texObj, const char* caller)
 {
-   struct gl_texture_object *texObj;
    GET_CURRENT_CONTEXT(ctx);
 
-   texObj = _mesa_lookup_texture_err(ctx, texture, "glGenerateTextureMipmap");
    if (!texObj)
       return;
 
    if (!_mesa_is_valid_generate_texture_mipmap_target(ctx, texObj->Target)) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateTextureMipmap(target=%s)",
+      _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%s)",
+                  caller,
                   _mesa_enum_to_string(texObj->Target));
       return;
    }
 
-   _mesa_generate_texture_mipmap(ctx, texObj, texObj->Target, true);
+   generate_texture_mipmap(ctx, texObj, texObj->Target, caller);
+}
+
+void GLAPIENTRY
+_mesa_GenerateTextureMipmap(GLuint texture)
+{
+   struct gl_texture_object *texObj;
+   GET_CURRENT_CONTEXT(ctx);
+
+   texObj = _mesa_lookup_texture_err(ctx, texture, "glGenerateTextureMipmap");
+   validate_params_and_generate_mipmap(texObj, "glGenerateTextureMipmap");
 }