mesa: Implement GL_ARB_texture_filter_anisotropic
[mesa.git] / src / mesa / main / bufferobj.c
index 2d1b652dfc06ea1284d9cd7ec554da7183260602..2da21280819aa83cc9038a249da28d47f2588ae1 100644 (file)
@@ -39,6 +39,7 @@
 #include "imports.h"
 #include "context.h"
 #include "bufferobj.h"
+#include "externalobjects.h"
 #include "mtypes.h"
 #include "teximage.h"
 #include "glformats.h"
@@ -1820,9 +1821,12 @@ validate_buffer_storage(struct gl_context *ctx,
 
 static void
 buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
-               GLenum target, GLsizeiptr size, const GLvoid *data,
-               GLbitfield flags, const char *func)
+               struct gl_memory_object *memObj, GLenum target,
+               GLsizeiptr size, const GLvoid *data, GLbitfield flags,
+               GLuint64 offset, const char *func)
 {
+   GLboolean res;
+
    /* Unmap the existing buffer.  We'll replace it now.  Not an error. */
    _mesa_buffer_unmap_all_mappings(ctx, bufObj);
 
@@ -1832,9 +1836,18 @@ buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
    bufObj->Immutable = GL_TRUE;
    bufObj->MinMaxCacheDirty = true;
 
-   assert(ctx->Driver.BufferData);
-   if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
-                               flags, bufObj)) {
+   if (memObj) {
+      assert(ctx->Driver.BufferDataMem);
+      res = ctx->Driver.BufferDataMem(ctx, target, size, memObj, offset,
+                                      GL_DYNAMIC_DRAW, bufObj);
+   }
+   else {
+      assert(ctx->Driver.BufferData);
+      res = ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
+                                   flags, bufObj);
+   }
+
+   if (!res) {
       if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
          /* Even though the interaction between AMD_pinned_memory and
           * glBufferStorage is not described in the spec, Graham Sellers
@@ -1851,11 +1864,46 @@ buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
 
 static ALWAYS_INLINE void
 inlined_buffer_storage(GLenum target, GLuint buffer, GLsizeiptr size,
-                       const GLvoid *data, GLbitfield flags, bool dsa,
-                       bool no_error, const char *func)
+                       const GLvoid *data, GLbitfield flags,
+                       GLuint memory, GLuint64 offset,
+                       bool dsa, bool mem, bool no_error, const char *func)
 {
    GET_CURRENT_CONTEXT(ctx);
    struct gl_buffer_object *bufObj;
+   struct gl_memory_object *memObj = NULL;
+
+   if (mem) {
+      if (!no_error) {
+         if (!ctx->Extensions.EXT_memory_object) {
+            _mesa_error(ctx, GL_INVALID_OPERATION, "%s(unsupported)", func);
+            return;
+         }
+
+         /* From the EXT_external_objects spec:
+          *
+          *   "An INVALID_VALUE error is generated by BufferStorageMemEXT and
+          *   NamedBufferStorageMemEXT if <memory> is 0, or ..."
+          */
+         if (memory == 0) {
+            _mesa_error(ctx, GL_INVALID_VALUE, "%s(memory == 0)", func);
+         }
+      }
+
+      memObj = _mesa_lookup_memory_object(ctx, memory);
+      if (!memObj)
+         return;
+
+      /* From the EXT_external_objects spec:
+       *
+       *   "An INVALID_OPERATION error is generated if <memory> names a
+       *   valid memory object which has no associated memory."
+       */
+      if (!no_error && !memObj->Immutable) {
+         _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no associated memory)",
+                     func);
+         return;
+      }
+   }
 
    if (dsa) {
       if (no_error) {
@@ -1877,7 +1925,7 @@ inlined_buffer_storage(GLenum target, GLuint buffer, GLsizeiptr size,
    }
 
    if (no_error || validate_buffer_storage(ctx, bufObj, size, flags, func))
-      buffer_storage(ctx, bufObj, target, size, data, flags, func);
+      buffer_storage(ctx, bufObj, memObj, target, size, data, flags, offset, func);
 }
 
 
@@ -1885,8 +1933,8 @@ void GLAPIENTRY
 _mesa_BufferStorage_no_error(GLenum target, GLsizeiptr size,
                              const GLvoid *data, GLbitfield flags)
 {
-   inlined_buffer_storage(target, 0, size, data, flags, false, true,
-                          "glBufferStorage");
+   inlined_buffer_storage(target, 0, size, data, flags, GL_NONE, 0,
+                          false, false, true, "glBufferStorage");
 }
 
 
@@ -1894,8 +1942,26 @@ void GLAPIENTRY
 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
                     GLbitfield flags)
 {
-   inlined_buffer_storage(target, 0, size, data, flags, false, false,
-                          "glBufferStorage");
+   inlined_buffer_storage(target, 0, size, data, flags, GL_NONE, 0,
+                          false, false, false, "glBufferStorage");
+}
+
+
+void GLAPIENTRY
+_mesa_BufferStorageMemEXT(GLenum target, GLsizeiptr size,
+                          GLuint memory, GLuint64 offset)
+{
+   inlined_buffer_storage(target, 0, size, NULL, 0, memory, offset,
+                          false, true, false, "glBufferStorageMemEXT");
+}
+
+
+void GLAPIENTRY
+_mesa_BufferStorageMemEXT_no_error(GLenum target, GLsizeiptr size,
+                                   GLuint memory, GLuint64 offset)
+{
+   inlined_buffer_storage(target, 0, size, NULL, 0, memory, offset,
+                          false, true, true, "glBufferStorageMemEXT");
 }
 
 
@@ -1906,8 +1972,8 @@ _mesa_NamedBufferStorage_no_error(GLuint buffer, GLsizeiptr size,
    /* In direct state access, buffer objects have an unspecified target
     * since they are not required to be bound.
     */
-   inlined_buffer_storage(GL_NONE, buffer, size, data, flags, true, true,
-                          "glNamedBufferStorage");
+   inlined_buffer_storage(GL_NONE, buffer, size, data, flags, GL_NONE, 0,
+                          true, false, true, "glNamedBufferStorage");
 }
 
 
@@ -1918,8 +1984,25 @@ _mesa_NamedBufferStorage(GLuint buffer, GLsizeiptr size, const GLvoid *data,
    /* In direct state access, buffer objects have an unspecified target
     * since they are not required to be bound.
     */
-   inlined_buffer_storage(GL_NONE, buffer, size, data, flags, true, false,
-                          "glNamedBufferStorage");
+   inlined_buffer_storage(GL_NONE, buffer, size, data, flags, GL_NONE, 0,
+                          true, false, false, "glNamedBufferStorage");
+}
+
+void GLAPIENTRY
+_mesa_NamedBufferStorageMemEXT(GLuint buffer, GLsizeiptr size,
+                               GLuint memory, GLuint64 offset)
+{
+   inlined_buffer_storage(GL_NONE, buffer, size, GL_NONE, 0, memory, offset,
+                          true, true, false, "glNamedBufferStorageMemEXT");
+}
+
+
+void GLAPIENTRY
+_mesa_NamedBufferStorageMemEXT_no_error(GLuint buffer, GLsizeiptr size,
+                                        GLuint memory, GLuint64 offset)
+{
+   inlined_buffer_storage(GL_NONE, buffer, size, GL_NONE, 0, memory, offset,
+                          true, true, true, "glNamedBufferStorageMemEXT");
 }
 
 
@@ -2270,31 +2353,35 @@ _mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset,
 /**
  * \param subdata   true if caller is *SubData, false if *Data
  */
-static void
+static ALWAYS_INLINE void
 clear_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
                       GLenum internalformat, GLintptr offset, GLsizeiptr size,
                       GLenum format, GLenum type, const GLvoid *data,
-                      const char *func, bool subdata)
+                      const char *func, bool subdata, bool no_error)
 {
    mesa_format mesaFormat;
    GLubyte clearValue[MAX_PIXEL_BYTES];
    GLsizeiptr clearValueSize;
 
    /* This checks for disallowed mappings. */
-   if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
-                                         subdata, func)) {
+   if (!no_error && !buffer_object_subdata_range_good(ctx, bufObj, offset, size,
+                                                      subdata, func)) {
       return;
    }
 
-   mesaFormat = validate_clear_buffer_format(ctx, internalformat,
-                                             format, type, func);
+   if (no_error) {
+      mesaFormat = _mesa_get_texbuffer_format(ctx, internalformat);
+   } else {
+      mesaFormat = validate_clear_buffer_format(ctx, internalformat,
+                                                format, type, func);
+   }
 
-   if (mesaFormat == MESA_FORMAT_NONE) {
+   if (mesaFormat == MESA_FORMAT_NONE)
       return;
-   }
 
    clearValueSize = _mesa_get_format_bytes(mesaFormat);
-   if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
+   if (!no_error &&
+       (offset % clearValueSize != 0 || size % clearValueSize != 0)) {
       _mesa_error(ctx, GL_INVALID_VALUE,
                   "%s(offset or size is not a multiple of "
                   "internalformat size)", func);
@@ -2323,6 +2410,44 @@ clear_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
                                   clearValue, clearValueSize, bufObj);
 }
 
+static void
+clear_buffer_sub_data_error(struct gl_context *ctx,
+                            struct gl_buffer_object *bufObj,
+                            GLenum internalformat, GLintptr offset,
+                            GLsizeiptr size, GLenum format, GLenum type,
+                            const GLvoid *data, const char *func, bool subdata)
+{
+   clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, format,
+                         type, data, func, subdata, false);
+}
+
+
+static void
+clear_buffer_sub_data_no_error(struct gl_context *ctx,
+                               struct gl_buffer_object *bufObj,
+                               GLenum internalformat, GLintptr offset,
+                               GLsizeiptr size, GLenum format, GLenum type,
+                               const GLvoid *data, const char *func,
+                               bool subdata)
+{
+   clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, format,
+                         type, data, func, subdata, true);
+}
+
+
+void GLAPIENTRY
+_mesa_ClearBufferData_no_error(GLenum target, GLenum internalformat,
+                               GLenum format, GLenum type, const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
+   clear_buffer_sub_data_no_error(ctx, *bufObj, internalformat, 0,
+                                  (*bufObj)->Size, format, type, data,
+                                  "glClearBufferData", false);
+}
+
+
 void GLAPIENTRY
 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
                       GLenum type, const GLvoid *data)
@@ -2334,10 +2459,25 @@ _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
    if (!bufObj)
       return;
 
-   clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
-                         format, type, data, "glClearBufferData", false);
+   clear_buffer_sub_data_error(ctx, bufObj, internalformat, 0, bufObj->Size,
+                               format, type, data, "glClearBufferData", false);
+}
+
+
+void GLAPIENTRY
+_mesa_ClearNamedBufferData_no_error(GLuint buffer, GLenum internalformat,
+                                    GLenum format, GLenum type,
+                                    const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
+   clear_buffer_sub_data_no_error(ctx, bufObj, internalformat, 0, bufObj->Size,
+                                  format, type, data, "glClearNamedBufferData",
+                                  false);
 }
 
+
 void GLAPIENTRY
 _mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat,
                            GLenum format, GLenum type, const GLvoid *data)
@@ -2349,8 +2489,24 @@ _mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat,
    if (!bufObj)
       return;
 
-   clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
-                         format, type, data, "glClearNamedBufferData", false);
+   clear_buffer_sub_data_error(ctx, bufObj, internalformat, 0, bufObj->Size,
+                               format, type, data, "glClearNamedBufferData",
+                               false);
+}
+
+
+void GLAPIENTRY
+_mesa_ClearBufferSubData_no_error(GLenum target, GLenum internalformat,
+                                  GLintptr offset, GLsizeiptr size,
+                                  GLenum format, GLenum type,
+                                  const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
+   clear_buffer_sub_data_no_error(ctx, *bufObj, internalformat, offset, size,
+                                  format, type, data, "glClearBufferSubData",
+                                  true);
 }
 
 
@@ -2367,10 +2523,27 @@ _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
    if (!bufObj)
       return;
 
-   clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size,
-                         format, type, data, "glClearBufferSubData", true);
+   clear_buffer_sub_data_error(ctx, bufObj, internalformat, offset, size,
+                               format, type, data, "glClearBufferSubData",
+                               true);
 }
 
+
+void GLAPIENTRY
+_mesa_ClearNamedBufferSubData_no_error(GLuint buffer, GLenum internalformat,
+                                       GLintptr offset, GLsizeiptr size,
+                                       GLenum format, GLenum type,
+                                       const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer);
+   clear_buffer_sub_data_no_error(ctx, bufObj, internalformat, offset, size,
+                                  format, type, data,
+                                  "glClearNamedBufferSubData", true);
+}
+
+
 void GLAPIENTRY
 _mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat,
                               GLintptr offset, GLsizeiptr size,
@@ -2385,8 +2558,9 @@ _mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat,
    if (!bufObj)
       return;
 
-   clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size, format,
-                         type, data, "glClearNamedBufferSubData", true);
+   clear_buffer_sub_data_error(ctx, bufObj, internalformat, offset, size,
+                               format, type, data, "glClearNamedBufferSubData",
+                               true);
 }
 
 static GLboolean
@@ -3881,9 +4055,6 @@ bind_xfb_buffers(struct gl_context *ctx,
       GLsizeiptr size = 0;
 
       if (range) {
-         offset = offsets[i];
-         size = sizes[i];
-
          if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
             continue;
 
@@ -4107,6 +4278,12 @@ bind_buffer_range(GLenum target, GLuint index, GLuint buffer, GLintptr offset,
       if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
                                         &bufObj, "glBindBufferRange"))
          return;
+
+      if (!no_error && !bufObj) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glBindBufferRange(invalid buffer=%u)", buffer);
+         return;
+      }
    }
 
    if (no_error) {
@@ -4129,12 +4306,6 @@ bind_buffer_range(GLenum target, GLuint index, GLuint buffer, GLintptr offset,
          unreachable("invalid BindBufferRange target with KHR_no_error");
       }
    } else {
-      if (!bufObj) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                     "glBindBufferRange(invalid buffer=%u)", buffer);
-         return;
-      }
-
       if (buffer != 0) {
          if (size <= 0) {
             _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
@@ -4205,12 +4376,12 @@ _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
       if (!_mesa_handle_bind_buffer_gen(ctx, buffer,
                                         &bufObj, "glBindBufferBase"))
          return;
-   }
 
-   if (!bufObj) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glBindBufferBase(invalid buffer=%u)", buffer);
-      return;
+      if (!bufObj) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glBindBufferBase(invalid buffer=%u)", buffer);
+         return;
+      }
    }
 
    /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with