mesa: avoid valgrind warning due to opaque only being set sometimes
[mesa.git] / src / mesa / main / shaderimage.c
index 67f17163d982a36319a324191b3ce31392c603bd..90643c4ed6d8a6d96dd585d57a7174508e8ad7c8 100644 (file)
@@ -331,21 +331,134 @@ get_image_format_class(mesa_format format)
    }
 }
 
+static GLenum
+_image_format_class_to_glenum(enum image_format_class class)
+{
+   switch (class) {
+   case IMAGE_FORMAT_CLASS_NONE:
+      return GL_NONE;
+   case IMAGE_FORMAT_CLASS_1X8:
+      return GL_IMAGE_CLASS_1_X_8;
+   case IMAGE_FORMAT_CLASS_1X16:
+      return GL_IMAGE_CLASS_1_X_16;
+   case IMAGE_FORMAT_CLASS_1X32:
+      return GL_IMAGE_CLASS_1_X_32;
+   case IMAGE_FORMAT_CLASS_2X8:
+      return GL_IMAGE_CLASS_2_X_8;
+   case IMAGE_FORMAT_CLASS_2X16:
+      return GL_IMAGE_CLASS_2_X_16;
+   case IMAGE_FORMAT_CLASS_2X32:
+      return GL_IMAGE_CLASS_2_X_32;
+   case IMAGE_FORMAT_CLASS_10_11_11:
+      return GL_IMAGE_CLASS_11_11_10;
+   case IMAGE_FORMAT_CLASS_4X8:
+      return GL_IMAGE_CLASS_4_X_8;
+   case IMAGE_FORMAT_CLASS_4X16:
+      return GL_IMAGE_CLASS_4_X_16;
+   case IMAGE_FORMAT_CLASS_4X32:
+      return GL_IMAGE_CLASS_4_X_32;
+   case IMAGE_FORMAT_CLASS_2_10_10_10:
+      return GL_IMAGE_CLASS_10_10_10_2;
+   default:
+      assert(!"Invalid image_format_class");
+      return GL_NONE;
+   }
+}
+
+GLenum
+_mesa_get_image_format_class(GLenum format)
+{
+   mesa_format tex_format = _mesa_get_shader_image_format(format);
+   if (tex_format == MESA_FORMAT_NONE)
+      return GL_NONE;
+
+   enum image_format_class class = get_image_format_class(tex_format);
+   return _image_format_class_to_glenum(class);
+}
+
+bool
+_mesa_is_shader_image_format_supported(const struct gl_context *ctx,
+                                       GLenum format)
+{
+   switch (format) {
+   /* Formats supported on both desktop and ES GL, c.f. table 8.27 of the
+    * OpenGL ES 3.1 specification.
+    */
+   case GL_RGBA32F:
+   case GL_RGBA16F:
+   case GL_R32F:
+   case GL_RGBA32UI:
+   case GL_RGBA16UI:
+   case GL_RGBA8UI:
+   case GL_R32UI:
+   case GL_RGBA32I:
+   case GL_RGBA16I:
+   case GL_RGBA8I:
+   case GL_R32I:
+   case GL_RGBA8:
+   case GL_RGBA8_SNORM:
+      return true;
+
+   /* Formats supported on unextended desktop GL and the original
+    * ARB_shader_image_load_store extension, c.f. table 3.21 of the OpenGL 4.2
+    * specification.
+    */
+   case GL_RG32F:
+   case GL_RG16F:
+   case GL_R11F_G11F_B10F:
+   case GL_R16F:
+   case GL_RGB10_A2UI:
+   case GL_RG32UI:
+   case GL_RG16UI:
+   case GL_RG8UI:
+   case GL_R16UI:
+   case GL_R8UI:
+   case GL_RG32I:
+   case GL_RG16I:
+   case GL_RG8I:
+   case GL_R16I:
+   case GL_R8I:
+   case GL_RGBA16:
+   case GL_RGB10_A2:
+   case GL_RG16:
+   case GL_RG8:
+   case GL_R16:
+   case GL_R8:
+   case GL_RGBA16_SNORM:
+   case GL_RG16_SNORM:
+   case GL_RG8_SNORM:
+   case GL_R16_SNORM:
+   case GL_R8_SNORM:
+      return _mesa_is_desktop_gl(ctx);
+
+   default:
+      return false;
+   }
+}
+
+struct gl_image_unit
+_mesa_default_image_unit(struct gl_context *ctx)
+{
+   const GLenum format = _mesa_is_desktop_gl(ctx) ? GL_R8 : GL_R32UI;
+   const struct gl_image_unit u = {
+      .Access = GL_READ_ONLY,
+      .Format = format,
+      ._ActualFormat = _mesa_get_shader_image_format(format)
+   };
+   return u;
+}
+
 void
 _mesa_init_image_units(struct gl_context *ctx)
 {
    unsigned i;
 
-   for (i = 0; i < ARRAY_SIZE(ctx->ImageUnits); ++i) {
-      struct gl_image_unit *u = &ctx->ImageUnits[i];
-      u->Access = GL_READ_ONLY;
-      u->Format = GL_R8;
-      u->_ActualFormat = _mesa_get_shader_image_format(u->Format);
-   }
+   for (i = 0; i < ARRAY_SIZE(ctx->ImageUnits); ++i)
+      ctx->ImageUnits[i] = _mesa_default_image_unit(ctx);
 }
 
-static GLboolean
-validate_image_unit(struct gl_context *ctx, struct gl_image_unit *u)
+GLboolean
+_mesa_is_image_unit_valid(struct gl_context *ctx, struct gl_image_unit *u)
 {
    struct gl_texture_object *t = u->TexObj;
    mesa_format tex_format;
@@ -353,7 +466,8 @@ validate_image_unit(struct gl_context *ctx, struct gl_image_unit *u)
    if (!t)
       return GL_FALSE;
 
-   _mesa_test_texobj_completeness(ctx, t);
+   if (!t->_BaseComplete && !t->_MipmapComplete)
+       _mesa_test_texobj_completeness(ctx, t);
 
    if (u->Level < t->BaseLevel ||
        u->Level > t->_MaxLevel ||
@@ -402,17 +516,6 @@ validate_image_unit(struct gl_context *ctx, struct gl_image_unit *u)
    return GL_TRUE;
 }
 
-void
-_mesa_validate_image_units(struct gl_context *ctx)
-{
-   unsigned i;
-
-   for (i = 0; i < ctx->Const.MaxImageUnits; ++i) {
-      struct gl_image_unit *u = &ctx->ImageUnits[i];
-      u->_Valid = validate_image_unit(ctx, u);
-   }
-}
-
 static GLboolean
 validate_bind_image_texture(struct gl_context *ctx, GLuint unit,
                             GLuint texture, GLint level, GLboolean layered,
@@ -442,7 +545,7 @@ validate_bind_image_texture(struct gl_context *ctx, GLuint unit,
       return GL_FALSE;
    }
 
-   if (!_mesa_get_shader_image_format(format)) {
+   if (!_mesa_is_shader_image_format_supported(ctx, format)) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(format)");
       return GL_FALSE;
    }
@@ -475,6 +578,23 @@ _mesa_BindImageTexture(GLuint unit, GLuint texture, GLint level,
          return;
       }
 
+      /* From section 8.22 "Texture Image Loads and Stores" of the OpenGL ES
+       * 3.1 spec:
+       *
+       * "An INVALID_OPERATION error is generated if texture is not the name
+       *  of an immutable texture object."
+       *
+       * However note that issue 7 of the GL_OES_texture_buffer spec
+       * recognizes that there is no way to create immutable buffer textures,
+       * so those are excluded from this requirement.
+       */
+      if (_mesa_is_gles(ctx) && !t->Immutable &&
+          t->Target != GL_TEXTURE_BUFFER) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glBindImageTexture(!immutable)");
+         return;
+      }
+
       _mesa_reference_texobj(&u->TexObj, t);
    } else {
       _mesa_reference_texobj(&u->TexObj, NULL);
@@ -484,7 +604,6 @@ _mesa_BindImageTexture(GLuint unit, GLuint texture, GLint level,
    u->Access = access;
    u->Format = format;
    u->_ActualFormat = _mesa_get_shader_image_format(format);
-   u->_Valid = validate_image_unit(ctx, u);
 
    if (u->TexObj && _mesa_tex_target_is_layered(u->TexObj->Target)) {
       u->Layered = layered;
@@ -494,10 +613,6 @@ _mesa_BindImageTexture(GLuint unit, GLuint texture, GLint level,
       u->Layered = GL_FALSE;
       u->Layer = 0;
    }
-
-   if (ctx->Driver.BindImageTexture)
-      ctx->Driver.BindImageTexture(ctx, u, u->TexObj, level, layered,
-                                   layer, access, format);
 }
 
 void GLAPIENTRY
@@ -600,7 +715,7 @@ _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
             tex_format = image->InternalFormat;
          }
 
-         if (_mesa_get_shader_image_format(tex_format) == MESA_FORMAT_NONE) {
+         if (!_mesa_is_shader_image_format_supported(ctx, tex_format)) {
             /* The ARB_multi_bind spec says:
              *
              *   "An INVALID_OPERATION error is generated if the internal
@@ -624,7 +739,6 @@ _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
          u->Access = GL_READ_WRITE;
          u->Format = tex_format;
          u->_ActualFormat = _mesa_get_shader_image_format(tex_format);
-         u->_Valid = validate_image_unit(ctx, u);
       } else {
          /* Unbind the texture from the unit */
          _mesa_reference_texobj(&u->TexObj, NULL);
@@ -634,13 +748,7 @@ _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
          u->Access = GL_READ_ONLY;
          u->Format = GL_R8;
          u->_ActualFormat = MESA_FORMAT_R_UNORM8;
-         u->_Valid = GL_FALSE;
       }
-
-      /* Pass the BindImageTexture call down to the device driver */
-      if (ctx->Driver.BindImageTexture)
-         ctx->Driver.BindImageTexture(ctx, u, u->TexObj, u->Level, u->Layered,
-                                      u->Layer, u->Access, u->Format);
    }
 
    _mesa_end_texture_lookups(ctx);
@@ -677,8 +785,10 @@ _mesa_MemoryBarrierByRegion(GLbitfield barriers)
        * That is, if barriers is the special value GL_ALL_BARRIER_BITS, then all
        * barriers allowed by glMemoryBarrierByRegion should be activated."
        */
-      if (barriers == GL_ALL_BARRIER_BITS)
-         return ctx->Driver.MemoryBarrier(ctx, all_allowed_bits);
+      if (barriers == GL_ALL_BARRIER_BITS) {
+         ctx->Driver.MemoryBarrier(ctx, all_allowed_bits);
+         return;
+      }
 
       /* From section 7.11.2 of the OpenGL ES 3.1 specification:
        *