mesa/teximage: Make _mesa_format_no_online_compression public
[mesa.git] / src / mesa / main / shaderimage.c
index f8120731e5aebe41050b49cd50831b8016faee5b..040e9fd6e3ce0ea75706787c50055f9c06995a35 100644 (file)
@@ -331,45 +331,138 @@ get_image_format_class(mesa_format format)
    }
 }
 
-static GLboolean
-validate_image_unit(struct gl_context *ctx, struct gl_image_unit *u)
+/**
+ * Return whether an image format should be supported based on the current API
+ * version of the context.
+ */
+static bool
+is_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)
+      ctx->ImageUnits[i] = _mesa_default_image_unit(ctx);
+}
+
+GLboolean
+_mesa_is_image_unit_valid(struct gl_context *ctx, struct gl_image_unit *u)
 {
    struct gl_texture_object *t = u->TexObj;
-   struct gl_texture_image *img;
+   mesa_format tex_format;
 
-   if (!t || u->Level < t->BaseLevel ||
-       u->Level > t->_MaxLevel)
+   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 && !t->_BaseComplete) ||
+   if (u->Level < t->BaseLevel ||
+       u->Level > t->_MaxLevel ||
+       (u->Level == t->BaseLevel && !t->_BaseComplete) ||
        (u->Level != t->BaseLevel && !t->_MipmapComplete))
       return GL_FALSE;
 
    if (_mesa_tex_target_is_layered(t->Target) &&
-       u->Layer >= _mesa_get_texture_layers(t, u->Level))
+       u->_Layer >= _mesa_get_texture_layers(t, u->Level))
       return GL_FALSE;
 
-   if (t->Target == GL_TEXTURE_CUBE_MAP)
-      img = t->Image[u->Layer][u->Level];
-   else
-      img = t->Image[0][u->Level];
+   if (t->Target == GL_TEXTURE_BUFFER) {
+      tex_format = _mesa_get_shader_image_format(t->BufferObjectFormat);
+
+   } else {
+      struct gl_texture_image *img = (t->Target == GL_TEXTURE_CUBE_MAP ?
+                                      t->Image[u->_Layer][u->Level] :
+                                      t->Image[0][u->Level]);
+
+      if (!img || img->Border || img->NumSamples > ctx->Const.MaxImageSamples)
+         return GL_FALSE;
+
+      tex_format = _mesa_get_shader_image_format(img->InternalFormat);
+   }
 
-   if (!img || img->Border ||
-       get_image_format_class(img->TexFormat) == IMAGE_FORMAT_CLASS_NONE ||
-       img->NumSamples > ctx->Const.MaxImageSamples)
+   if (!tex_format)
       return GL_FALSE;
 
    switch (t->ImageFormatCompatibilityType) {
    case GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE:
-      if (_mesa_get_format_bytes(img->TexFormat) !=
+      if (_mesa_get_format_bytes(tex_format) !=
           _mesa_get_format_bytes(u->_ActualFormat))
          return GL_FALSE;
       break;
 
    case GL_IMAGE_FORMAT_COMPATIBILITY_BY_CLASS:
-      if (get_image_format_class(img->TexFormat) !=
+      if (get_image_format_class(tex_format) !=
           get_image_format_class(u->_ActualFormat))
          return GL_FALSE;
       break;
@@ -381,17 +474,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,
@@ -421,7 +503,7 @@ validate_bind_image_texture(struct gl_context *ctx, GLuint unit,
       return GL_FALSE;
    }
 
-   if (!_mesa_get_shader_image_format(format)) {
+   if (!is_image_format_supported(ctx, format)) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(format)");
       return GL_FALSE;
    }
@@ -435,7 +517,6 @@ _mesa_BindImageTexture(GLuint unit, GLuint texture, GLint level,
                        GLenum format)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_texture_object *t = NULL;
    struct gl_image_unit *u;
 
    if (!validate_bind_image_texture(ctx, unit, texture, level,
@@ -448,35 +529,43 @@ _mesa_BindImageTexture(GLuint unit, GLuint texture, GLint level,
    ctx->NewDriverState |= ctx->DriverFlags.NewImageUnits;
 
    if (texture) {
-      t = _mesa_lookup_texture(ctx, texture);
+      struct gl_texture_object *t = _mesa_lookup_texture(ctx, texture);
+
       if (!t) {
          _mesa_error(ctx, GL_INVALID_VALUE, "glBindImageTexture(texture)");
          return;
       }
 
-      _mesa_reference_texobj(&u->TexObj, t);
-      u->Level = level;
-      u->Access = access;
-      u->Format = format;
-      u->_ActualFormat = _mesa_get_shader_image_format(format);
-
-      if (_mesa_tex_target_is_layered(t->Target)) {
-         u->Layered = layered;
-         u->Layer = (layered ? 0 : layer);
-      } else {
-         u->Layered = GL_FALSE;
-         u->Layer = 0;
+      /* 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."
+       */
+      if (_mesa_is_gles(ctx) && !t->Immutable) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glBindImageTexture(!immutable)");
+         return;
       }
 
+      _mesa_reference_texobj(&u->TexObj, t);
    } else {
       _mesa_reference_texobj(&u->TexObj, NULL);
    }
 
-   u->_Valid = validate_image_unit(ctx, u);
+   u->Level = level;
+   u->Access = access;
+   u->Format = format;
+   u->_ActualFormat = _mesa_get_shader_image_format(format);
 
-   if (ctx->Driver.BindImageTexture)
-      ctx->Driver.BindImageTexture(ctx, u, t, level, layered,
-                                   layer, access, format);
+   if (u->TexObj && _mesa_tex_target_is_layered(u->TexObj->Target)) {
+      u->Layered = layered;
+      u->Layer = layer;
+      u->_Layer = (u->Layered ? 0 : u->Layer);
+   } else {
+      u->Layered = GL_FALSE;
+      u->Layer = 0;
+   }
 }
 
 void GLAPIENTRY
@@ -535,8 +624,7 @@ _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
 
       if (texture != 0) {
          struct gl_texture_object *texObj;
-         struct gl_texture_image *image;
-         mesa_format actualFormat;
+         GLenum tex_format;
 
          if (!u->TexObj || u->TexObj->Name != texture) {
             texObj = _mesa_lookup_texture_locked(ctx, texture);
@@ -557,25 +645,30 @@ _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
             texObj = u->TexObj;
          }
 
-         image = texObj->Image[0][0];
+         if (texObj->Target == GL_TEXTURE_BUFFER) {
+            tex_format = texObj->BufferObjectFormat;
+         } else {
+            struct gl_texture_image *image = texObj->Image[0][0];
 
-         if (!image || image->Width == 0 || image->Height == 0 || image->Depth == 0) {
-            /* The ARB_multi_bind spec says:
-             *
-             *    "An INVALID_OPERATION error is generated if the width,
-             *     height, or depth of the level zero texture image of
-             *     any texture in <textures> is zero (per binding)."
-             */
-            _mesa_error(ctx, GL_INVALID_OPERATION,
-                        "glBindImageTextures(the width, height or depth "
-                        "of the level zero texture image of "
-                        "textures[%d]=%u is zero)", i, texture);
-            continue;
-         }
+            if (!image || image->Width == 0 || image->Height == 0 ||
+                image->Depth == 0) {
+               /* The ARB_multi_bind spec says:
+                *
+                *    "An INVALID_OPERATION error is generated if the width,
+                *     height, or depth of the level zero texture image of
+                *     any texture in <textures> is zero (per binding)."
+                */
+               _mesa_error(ctx, GL_INVALID_OPERATION,
+                           "glBindImageTextures(the width, height or depth "
+                           "of the level zero texture image of "
+                           "textures[%d]=%u is zero)", i, texture);
+               continue;
+            }
 
-         actualFormat = _mesa_get_shader_image_format(image->InternalFormat);
+            tex_format = image->InternalFormat;
+         }
 
-         if (actualFormat == MESA_FORMAT_NONE) {
+         if (!is_image_format_supported(ctx, tex_format)) {
             /* The ARB_multi_bind spec says:
              *
              *   "An INVALID_OPERATION error is generated if the internal
@@ -586,7 +679,7 @@ _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
                         "glBindImageTextures(the internal format %s of "
                         "the level zero texture image of textures[%d]=%u "
                         "is not supported)",
-                        _mesa_lookup_enum_by_nr(image->InternalFormat),
+                        _mesa_enum_to_string(tex_format),
                         i, texture);
             continue;
          }
@@ -595,27 +688,20 @@ _mesa_BindImageTextures(GLuint first, GLsizei count, const GLuint *textures)
          _mesa_reference_texobj(&u->TexObj, texObj);
          u->Level = 0;
          u->Layered = _mesa_tex_target_is_layered(texObj->Target);
-         u->Layer = 0;
+         u->_Layer = u->Layer = 0;
          u->Access = GL_READ_WRITE;
-         u->Format = image->InternalFormat;
-         u->_ActualFormat = actualFormat;
-         u->_Valid = validate_image_unit(ctx, u);
+         u->Format = tex_format;
+         u->_ActualFormat = _mesa_get_shader_image_format(tex_format);
       } else {
          /* Unbind the texture from the unit */
          _mesa_reference_texobj(&u->TexObj, NULL);
          u->Level = 0;
          u->Layered = GL_FALSE;
-         u->Layer = 0;
+         u->_Layer = u->Layer = 0;
          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);
@@ -629,3 +715,45 @@ _mesa_MemoryBarrier(GLbitfield barriers)
    if (ctx->Driver.MemoryBarrier)
       ctx->Driver.MemoryBarrier(ctx, barriers);
 }
+
+void GLAPIENTRY
+_mesa_MemoryBarrierByRegion(GLbitfield barriers)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   GLbitfield all_allowed_bits = GL_ATOMIC_COUNTER_BARRIER_BIT |
+                                 GL_FRAMEBUFFER_BARRIER_BIT |
+                                 GL_SHADER_IMAGE_ACCESS_BARRIER_BIT |
+                                 GL_SHADER_STORAGE_BARRIER_BIT |
+                                 GL_TEXTURE_FETCH_BARRIER_BIT |
+                                 GL_UNIFORM_BARRIER_BIT;
+
+   if (ctx->Driver.MemoryBarrier) {
+      /* From section 7.11.2 of the OpenGL ES 3.1 specification:
+       *
+       *    "When barriers is ALL_BARRIER_BITS, shader memory accesses will be
+       *     synchronized relative to all these barrier bits, but not to other
+       *     barrier bits specific to MemoryBarrier."
+       *
+       * 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) {
+         ctx->Driver.MemoryBarrier(ctx, all_allowed_bits);
+         return;
+      }
+
+      /* From section 7.11.2 of the OpenGL ES 3.1 specification:
+       *
+       *    "An INVALID_VALUE error is generated if barriers is not the special
+       *     value ALL_BARRIER_BITS, and has any bits set other than those
+       *     described above."
+       */
+      if ((barriers & ~all_allowed_bits) != 0) {
+         _mesa_error(ctx, GL_INVALID_VALUE,
+                     "glMemoryBarrierByRegion(unsupported barrier bit");
+      }
+
+      ctx->Driver.MemoryBarrier(ctx, barriers);
+   }
+}