X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fmesa%2Fmain%2Fbufferobj.c;h=db5fdaf6075e99cb617d08ccac42a3e616436883;hb=6aea39641a6c32f780c40e3344096d3e0eccb590;hp=5aae579eaf4046c15f8c79ee282588441ba07b92;hpb=b8174a837f785087861d1f83a684bad1d8e281fa;p=mesa.git diff --git a/src/mesa/main/bufferobj.c b/src/mesa/main/bufferobj.c index 5aae579eaf4..db5fdaf6075 100644 --- a/src/mesa/main/bufferobj.c +++ b/src/mesa/main/bufferobj.c @@ -36,15 +36,17 @@ #include "glheader.h" #include "enums.h" #include "hash.h" -#include "imports.h" #include "context.h" #include "bufferobj.h" +#include "externalobjects.h" #include "mtypes.h" #include "teximage.h" #include "glformats.h" #include "texstore.h" #include "transformfeedback.h" #include "varray.h" +#include "util/u_atomic.h" +#include "util/u_memory.h" /* Debug flags */ @@ -71,11 +73,11 @@ buffer_usage_warning(struct gl_context *ctx, GLuint *id, const char *fmt, ...) va_list args; va_start(args, fmt); - _mesa_gl_vdebug(ctx, id, - MESA_DEBUG_SOURCE_API, - MESA_DEBUG_TYPE_PERFORMANCE, - MESA_DEBUG_SEVERITY_MEDIUM, - fmt, args); + _mesa_gl_vdebugf(ctx, id, + MESA_DEBUG_SOURCE_API, + MESA_DEBUG_TYPE_PERFORMANCE, + MESA_DEBUG_SEVERITY_MEDIUM, + fmt, args); va_end(args); } @@ -103,16 +105,31 @@ static struct gl_buffer_object DummyBufferObject; static inline struct gl_buffer_object ** get_buffer_target(struct gl_context *ctx, GLenum target) { - /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0. - */ - if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx) - && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER) - return NULL; + /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0. */ + if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)) { + switch (target) { + case GL_ARRAY_BUFFER: + case GL_ELEMENT_ARRAY_BUFFER: + break; + case GL_PIXEL_PACK_BUFFER: + case GL_PIXEL_UNPACK_BUFFER: + if (!ctx->Extensions.EXT_pixel_buffer_object) + return NULL; + break; + default: + return NULL; + } + } switch (target) { case GL_ARRAY_BUFFER_ARB: + if (ctx->Array.ArrayBufferObj) + ctx->Array.ArrayBufferObj->UsageHistory |= USAGE_ARRAY_BUFFER; return &ctx->Array.ArrayBufferObj; case GL_ELEMENT_ARRAY_BUFFER_ARB: + if (ctx->Array.VAO->IndexBufferObj) + ctx->Array.VAO->IndexBufferObj->UsageHistory + |= USAGE_ELEMENT_ARRAY_BUFFER; return &ctx->Array.VAO->IndexBufferObj; case GL_PIXEL_PACK_BUFFER_EXT: return &ctx->Pack.BufferObj; @@ -127,8 +144,7 @@ get_buffer_target(struct gl_context *ctx, GLenum target) return &ctx->QueryBuffer; break; case GL_DRAW_INDIRECT_BUFFER: - if ((ctx->API == API_OPENGL_CORE && - ctx->Extensions.ARB_draw_indirect) || + if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_draw_indirect) || _mesa_is_gles31(ctx)) { return &ctx->DrawIndirectBuffer; } @@ -200,7 +216,7 @@ get_buffer(struct gl_context *ctx, const char *func, GLenum target, return NULL; } - if (!_mesa_is_bufferobj(*bufObj)) { + if (!*bufObj) { _mesa_error(ctx, error, "%s(no buffer bound)", func); return NULL; } @@ -345,7 +361,8 @@ buffer_object_subdata_range_good(struct gl_context *ctx, /** * Test the format and type parameters and set the GL error code for - * \c glClearBufferData and \c glClearBufferSubData. + * \c glClearBufferData, \c glClearNamedBufferData, \c glClearBufferSubData + * and \c glClearNamedBufferSubData. * * \param ctx GL context. * \param internalformat Format to which the data is to be converted. @@ -355,7 +372,8 @@ buffer_object_subdata_range_good(struct gl_context *ctx, * \return If internalformat, format and type are legal the mesa_format * corresponding to internalformat, otherwise MESA_FORMAT_NONE. * - * \sa glClearBufferData and glClearBufferSubData + * \sa glClearBufferData, glClearNamedBufferData, glClearBufferSubData and + * glClearNamedBufferSubData. */ static mesa_format validate_clear_buffer_format(struct gl_context *ctx, @@ -385,14 +403,14 @@ validate_clear_buffer_format(struct gl_context *ctx, } if (!_mesa_is_color_format(format)) { - _mesa_error(ctx, GL_INVALID_ENUM, + _mesa_error(ctx, GL_INVALID_VALUE, "%s(format is not a color format)", caller); return MESA_FORMAT_NONE; } errorFormatType = _mesa_error_check_format_and_type(ctx, format, type); if (errorFormatType != GL_NO_ERROR) { - _mesa_error(ctx, GL_INVALID_ENUM, + _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid format or type)", caller); return MESA_FORMAT_NONE; } @@ -443,11 +461,10 @@ convert_clear_buffer_data(struct gl_context *ctx, static struct gl_buffer_object * _mesa_new_buffer_object(struct gl_context *ctx, GLuint name) { - struct gl_buffer_object *obj; - - (void) ctx; + struct gl_buffer_object *obj = MALLOC_STRUCT(gl_buffer_object); + if (!obj) + return NULL; - obj = MALLOC_STRUCT(gl_buffer_object); _mesa_initialize_buffer_object(ctx, obj, name); return obj; } @@ -465,13 +482,13 @@ _mesa_delete_buffer_object(struct gl_context *ctx, (void) ctx; vbo_delete_minmax_cache(bufObj); - _mesa_align_free(bufObj->Data); + align_free(bufObj->Data); /* assign strange values here to help w/ debugging */ bufObj->RefCount = -1000; bufObj->Name = ~0; - mtx_destroy(&bufObj->Mutex); + simple_mtx_destroy(&bufObj->MinMaxCacheMutex); free(bufObj->Label); free(bufObj); } @@ -490,16 +507,9 @@ _mesa_reference_buffer_object_(struct gl_context *ctx, { if (*ptr) { /* Unreference the old buffer */ - GLboolean deleteFlag = GL_FALSE; struct gl_buffer_object *oldObj = *ptr; - mtx_lock(&oldObj->Mutex); - assert(oldObj->RefCount > 0); - oldObj->RefCount--; - deleteFlag = (oldObj->RefCount == 0); - mtx_unlock(&oldObj->Mutex); - - if (deleteFlag) { + if (p_atomic_dec_zero(&oldObj->RefCount)) { assert(ctx->Driver.DeleteBuffer); ctx->Driver.DeleteBuffer(ctx, oldObj); } @@ -510,12 +520,8 @@ _mesa_reference_buffer_object_(struct gl_context *ctx, if (bufObj) { /* reference new buffer */ - mtx_lock(&bufObj->Mutex); - assert(bufObj->RefCount > 0); - - bufObj->RefCount++; + p_atomic_inc(&bufObj->RefCount); *ptr = bufObj; - mtx_unlock(&bufObj->Mutex); } } @@ -547,11 +553,11 @@ _mesa_initialize_buffer_object(struct gl_context *ctx, GLuint name) { memset(obj, 0, sizeof(struct gl_buffer_object)); - mtx_init(&obj->Mutex, mtx_plain); obj->RefCount = 1; obj->Name = name; obj->Usage = GL_STATIC_DRAW_ARB; + simple_mtx_init(&obj->MinMaxCacheMutex, mtx_plain); if (get_no_minmax_cache()) obj->UsageHistory |= USAGE_DISABLE_MINMAX_CACHE; } @@ -608,7 +614,7 @@ _mesa_total_buffer_object_memory(struct gl_context *ctx) * \sa glBufferDataARB, dd_function_table::BufferData. */ static GLboolean -buffer_data_fallback(struct gl_context *ctx, GLenum target, GLsizeiptr size, +buffer_data_fallback(struct gl_context *ctx, GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage, GLenum storageFlags, struct gl_buffer_object *bufObj) { @@ -616,9 +622,9 @@ buffer_data_fallback(struct gl_context *ctx, GLenum target, GLsizeiptr size, (void) target; - _mesa_align_free( bufObj->Data ); + align_free( bufObj->Data ); - new_data = _mesa_align_malloc( size, ctx->Const.MinMapBufferAlignment ); + new_data = align_malloc( size, ctx->Const.MinMapBufferAlignment ); if (new_data) { bufObj->Data = (GLubyte *) new_data; bufObj->Size = size; @@ -654,8 +660,8 @@ buffer_data_fallback(struct gl_context *ctx, GLenum target, GLsizeiptr size, * \sa glBufferSubDataARB, dd_function_table::BufferSubData. */ static void -buffer_sub_data_fallback(struct gl_context *ctx, GLintptr offset, - GLsizeiptr size, const GLvoid *data, +buffer_sub_data_fallback(struct gl_context *ctx, GLintptrARB offset, + GLsizeiptrARB size, const GLvoid *data, struct gl_buffer_object *bufObj) { (void) ctx; @@ -870,42 +876,13 @@ _mesa_init_buffer_objects( struct gl_context *ctx ) GLuint i; memset(&DummyBufferObject, 0, sizeof(DummyBufferObject)); - mtx_init(&DummyBufferObject.Mutex, mtx_plain); + simple_mtx_init(&DummyBufferObject.MinMaxCacheMutex, mtx_plain); DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */ - _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, - ctx->Shared->NullBufferObj); - - _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, - ctx->Shared->NullBufferObj); - _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, - ctx->Shared->NullBufferObj); - - _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, - ctx->Shared->NullBufferObj); - - _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, - ctx->Shared->NullBufferObj); - - _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, - ctx->Shared->NullBufferObj); - - _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, - ctx->Shared->NullBufferObj); - - _mesa_reference_buffer_object(ctx, &ctx->ParameterBuffer, - ctx->Shared->NullBufferObj); - - _mesa_reference_buffer_object(ctx, &ctx->DispatchIndirectBuffer, - ctx->Shared->NullBufferObj); - - _mesa_reference_buffer_object(ctx, &ctx->QueryBuffer, - ctx->Shared->NullBufferObj); - for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) { _mesa_reference_buffer_object(ctx, &ctx->UniformBufferBindings[i].BufferObject, - ctx->Shared->NullBufferObj); + NULL); ctx->UniformBufferBindings[i].Offset = -1; ctx->UniformBufferBindings[i].Size = -1; } @@ -913,7 +890,7 @@ _mesa_init_buffer_objects( struct gl_context *ctx ) for (i = 0; i < MAX_COMBINED_SHADER_STORAGE_BUFFERS; i++) { _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBufferBindings[i].BufferObject, - ctx->Shared->NullBufferObj); + NULL); ctx->ShaderStorageBufferBindings[i].Offset = -1; ctx->ShaderStorageBufferBindings[i].Size = -1; } @@ -921,7 +898,7 @@ _mesa_init_buffer_objects( struct gl_context *ctx ) for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) { _mesa_reference_buffer_object(ctx, &ctx->AtomicBufferBindings[i].BufferObject, - ctx->Shared->NullBufferObj); + NULL); ctx->AtomicBufferBindings[i].Offset = 0; ctx->AtomicBufferBindings[i].Size = 0; } @@ -989,7 +966,6 @@ _mesa_handle_bind_buffer_gen(struct gl_context *ctx, /* If this is a new buffer object id, or one which was generated but * never used before, allocate a buffer object now. */ - assert(ctx->Driver.NewBufferObject); buf = ctx->Driver.NewBufferObject(ctx, buffer); if (!buf) { _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller); @@ -1017,29 +993,23 @@ bind_buffer_object(struct gl_context *ctx, /* Get pointer to old buffer object (to be unbound) */ oldBufObj = *bindTarget; - if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending) + if ((oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending) || + (!oldBufObj && buffer == 0)) return; /* rebinding the same buffer object- no change */ /* * Get pointer to new buffer object (newBufObj) */ - if (buffer == 0) { - /* The spec says there's not a buffer object named 0, but we use - * one internally because it simplifies things. - */ - newBufObj = ctx->Shared->NullBufferObj; - } - else { + if (buffer != 0) { /* non-default buffer object */ newBufObj = _mesa_lookup_bufferobj(ctx, buffer); if (!_mesa_handle_bind_buffer_gen(ctx, buffer, &newBufObj, "glBindBuffer")) return; - } - /* record usage history */ - if (bindTarget == &ctx->Pack.BufferObj) { - newBufObj->UsageHistory |= USAGE_PIXEL_PACK_BUFFER; + /* record usage history */ + if (bindTarget == &ctx->Pack.BufferObj) + newBufObj->UsageHistory |= USAGE_PIXEL_PACK_BUFFER; } /* bind new buffer */ @@ -1055,9 +1025,7 @@ bind_buffer_object(struct gl_context *ctx, void _mesa_update_default_objects_buffer_objects(struct gl_context *ctx) { - /* Bind the NullBufferObj to remove references to those - * in the shared context hash table. - */ + /* Bind 0 to remove references to those in the shared context hash table. */ bind_buffer_object(ctx, &ctx->Array.ArrayBufferObj, 0); bind_buffer_object(ctx, &ctx->Array.VAO->IndexBufferObj, 0); bind_buffer_object(ctx, &ctx->Pack.BufferObj, 0); @@ -1121,10 +1089,9 @@ _mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer, * of an existing buffer object. * * If the buffer ID refers to an existing buffer object, a pointer - * to the buffer object is returned. If the ID is zero, a pointer - * to the shared NullBufferObj is returned. If the ID is not zero - * and does not refer to a valid buffer object, this function - * returns NULL. + * to the buffer object is returned. If the ID is zero, NULL is returned. + * If the ID is not zero and does not refer to a valid buffer object, this + * function returns NULL. * * This function assumes that the caller has already locked the * hash table mutex by calling @@ -1133,9 +1100,12 @@ _mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer, struct gl_buffer_object * _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx, const GLuint *buffers, - GLuint index, const char *caller) + GLuint index, const char *caller, + bool *error) { - struct gl_buffer_object *bufObj; + struct gl_buffer_object *bufObj = NULL; + + *error = false; if (buffers[index] != 0) { bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]); @@ -1144,20 +1114,20 @@ _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx, when they don't exist. */ if (bufObj == &DummyBufferObject) bufObj = NULL; - } else - bufObj = ctx->Shared->NullBufferObj; - if (!bufObj) { - /* The ARB_multi_bind spec says: - * - * "An INVALID_OPERATION error is generated if any value - * in is not zero or the name of an existing - * buffer object (per binding)." - */ - _mesa_error(ctx, GL_INVALID_OPERATION, - "%s(buffers[%u]=%u is not zero or the name " - "of an existing buffer object)", - caller, index, buffers[index]); + if (!bufObj) { + /* The ARB_multi_bind spec says: + * + * "An INVALID_OPERATION error is generated if any value + * in is not zero or the name of an existing + * buffer object (per binding)." + */ + _mesa_error(ctx, GL_INVALID_OPERATION, + "%s(buffers[%u]=%u is not zero or the name " + "of an existing buffer object)", + caller, index, buffers[index]); + *error = true; + } } return bufObj; @@ -1176,9 +1146,9 @@ unbind(struct gl_context *ctx, struct gl_buffer_object *obj) { if (vao->BufferBinding[index].BufferObj == obj) { - _mesa_bind_vertex_buffer(ctx, vao, index, ctx->Shared->NullBufferObj, + _mesa_bind_vertex_buffer(ctx, vao, index, NULL, vao->BufferBinding[index].Offset, - vao->BufferBinding[index].Stride); + vao->BufferBinding[index].Stride, true, false); } } @@ -1228,6 +1198,16 @@ _mesa_buffer_unmap_all_mappings(struct gl_context *ctx, /* API Functions */ /**********************************************************************/ +void GLAPIENTRY +_mesa_BindBuffer_no_error(GLenum target, GLuint buffer) +{ + GET_CURRENT_CONTEXT(ctx); + + struct gl_buffer_object **bindTarget = get_buffer_target(ctx, target); + bind_buffer_object(ctx, bindTarget, buffer); +} + + void GLAPIENTRY _mesa_BindBuffer(GLenum target, GLuint buffer) { @@ -1248,28 +1228,238 @@ _mesa_BindBuffer(GLenum target, GLuint buffer) bind_buffer_object(ctx, bindTarget, buffer); } +void +_mesa_InternalBindElementBuffer(struct gl_context *ctx, + struct gl_buffer_object *buf) +{ + struct gl_buffer_object **bindTarget = + get_buffer_target(ctx, GL_ELEMENT_ARRAY_BUFFER); + + /* Move the buffer reference from the parameter to the bind point. */ + _mesa_reference_buffer_object(ctx, bindTarget, NULL); + if (buf) + *bindTarget = buf; +} /** - * Delete a set of buffer objects. + * Binds a buffer object to a binding point. * - * \param n Number of buffer objects to delete. - * \param ids Array of \c n buffer object IDs. + * The caller is responsible for validating the offset, + * flushing the vertices and updating NewDriverState. */ -void GLAPIENTRY -_mesa_DeleteBuffers(GLsizei n, const GLuint *ids) +static void +set_buffer_binding(struct gl_context *ctx, + struct gl_buffer_binding *binding, + struct gl_buffer_object *bufObj, + GLintptr offset, + GLsizeiptr size, + bool autoSize, gl_buffer_usage usage) { - GET_CURRENT_CONTEXT(ctx); - GLsizei i; + _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj); + + binding->Offset = offset; + binding->Size = size; + binding->AutomaticSize = autoSize; + + /* If this is a real buffer object, mark it has having been used + * at some point as an atomic counter buffer. + */ + if (size >= 0) + bufObj->UsageHistory |= usage; +} + +static void +set_buffer_multi_binding(struct gl_context *ctx, + const GLuint *buffers, + int idx, + const char *caller, + struct gl_buffer_binding *binding, + GLintptr offset, + GLsizeiptr size, + bool range, + gl_buffer_usage usage) +{ + struct gl_buffer_object *bufObj; + + if (binding->BufferObject && binding->BufferObject->Name == buffers[idx]) + bufObj = binding->BufferObject; + else { + bool error; + bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, idx, caller, + &error); + if (error) + return; + } + + if (!bufObj) + set_buffer_binding(ctx, binding, bufObj, -1, -1, !range, usage); + else + set_buffer_binding(ctx, binding, bufObj, offset, size, !range, usage); +} + +static void +bind_buffer(struct gl_context *ctx, + struct gl_buffer_binding *binding, + struct gl_buffer_object *bufObj, + GLintptr offset, + GLsizeiptr size, + GLboolean autoSize, + uint64_t driver_state, + gl_buffer_usage usage) +{ + if (binding->BufferObject == bufObj && + binding->Offset == offset && + binding->Size == size && + binding->AutomaticSize == autoSize) { + return; + } + FLUSH_VERTICES(ctx, 0); + ctx->NewDriverState |= driver_state; - if (n < 0) { - _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)"); + set_buffer_binding(ctx, binding, bufObj, offset, size, autoSize, usage); +} + +/** + * Binds a buffer object to a uniform buffer binding point. + * + * Unlike set_buffer_binding(), this function also flushes vertices + * and updates NewDriverState. It also checks if the binding + * has actually changed before updating it. + */ +static void +bind_uniform_buffer(struct gl_context *ctx, + GLuint index, + struct gl_buffer_object *bufObj, + GLintptr offset, + GLsizeiptr size, + GLboolean autoSize) +{ + bind_buffer(ctx, &ctx->UniformBufferBindings[index], + bufObj, offset, size, autoSize, + ctx->DriverFlags.NewUniformBuffer, + USAGE_UNIFORM_BUFFER); +} + +/** + * Binds a buffer object to a shader storage buffer binding point. + * + * Unlike set_ssbo_binding(), this function also flushes vertices + * and updates NewDriverState. It also checks if the binding + * has actually changed before updating it. + */ +static void +bind_shader_storage_buffer(struct gl_context *ctx, + GLuint index, + struct gl_buffer_object *bufObj, + GLintptr offset, + GLsizeiptr size, + GLboolean autoSize) +{ + bind_buffer(ctx, &ctx->ShaderStorageBufferBindings[index], + bufObj, offset, size, autoSize, + ctx->DriverFlags.NewShaderStorageBuffer, + USAGE_SHADER_STORAGE_BUFFER); +} + +/** + * Binds a buffer object to an atomic buffer binding point. + * + * Unlike set_atomic_binding(), this function also flushes vertices + * and updates NewDriverState. It also checks if the binding + * has actually changed before updating it. + */ +static void +bind_atomic_buffer(struct gl_context *ctx, unsigned index, + struct gl_buffer_object *bufObj, GLintptr offset, + GLsizeiptr size, GLboolean autoSize) +{ + bind_buffer(ctx, &ctx->AtomicBufferBindings[index], + bufObj, offset, size, autoSize, + ctx->DriverFlags.NewAtomicBuffer, + USAGE_ATOMIC_COUNTER_BUFFER); +} + +/** + * Bind a buffer object to a uniform block binding point. + * As above, but offset = 0. + */ +static void +bind_buffer_base_uniform_buffer(struct gl_context *ctx, + GLuint index, + struct gl_buffer_object *bufObj) +{ + if (index >= ctx->Const.MaxUniformBufferBindings) { + _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index); + return; + } + + _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj); + + if (!bufObj) + bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE); + else + bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE); +} + +/** + * Bind a buffer object to a shader storage block binding point. + * As above, but offset = 0. + */ +static void +bind_buffer_base_shader_storage_buffer(struct gl_context *ctx, + GLuint index, + struct gl_buffer_object *bufObj) +{ + if (index >= ctx->Const.MaxShaderStorageBufferBindings) { + _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index); + return; + } + + _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj); + + if (!bufObj) + bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE); + else + bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE); +} + +/** + * Bind a buffer object to a shader storage block binding point. + * As above, but offset = 0. + */ +static void +bind_buffer_base_atomic_buffer(struct gl_context *ctx, + GLuint index, + struct gl_buffer_object *bufObj) +{ + if (index >= ctx->Const.MaxAtomicBufferBindings) { + _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index); return; } + _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj); + + if (!bufObj) + bind_atomic_buffer(ctx, index, bufObj, -1, -1, GL_TRUE); + else + bind_atomic_buffer(ctx, index, bufObj, 0, 0, GL_TRUE); +} + +/** + * Delete a set of buffer objects. + * + * \param n Number of buffer objects to delete. + * \param ids Array of \c n buffer object IDs. + */ +static void +delete_buffers(struct gl_context *ctx, GLsizei n, const GLuint *ids) +{ + FLUSH_VERTICES(ctx, 0); + _mesa_HashLockMutex(ctx->Shared->BufferObjects); - for (i = 0; i < n; i++) { + for (GLsizei i = 0; i < n; i++) { struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj_locked(ctx, ids[i]); if (bufObj) { @@ -1321,14 +1511,16 @@ _mesa_DeleteBuffers(GLsizei n, const GLuint *ids) } for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) { if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) { - _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 ); + _mesa_bind_buffer_base_transform_feedback(ctx, + ctx->TransformFeedback.CurrentObject, + j, NULL, false); } } /* unbind UBO binding points */ for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) { if (ctx->UniformBufferBindings[j].BufferObject == bufObj) { - _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 ); + bind_buffer_base_uniform_buffer(ctx, j, NULL); } } @@ -1339,7 +1531,7 @@ _mesa_DeleteBuffers(GLsizei n, const GLuint *ids) /* unbind SSBO binding points */ for (j = 0; j < ctx->Const.MaxShaderStorageBufferBindings; j++) { if (ctx->ShaderStorageBufferBindings[j].BufferObject == bufObj) { - _mesa_BindBufferBase(GL_SHADER_STORAGE_BUFFER, j, 0); + bind_buffer_base_shader_storage_buffer(ctx, j, NULL); } } @@ -1350,7 +1542,7 @@ _mesa_DeleteBuffers(GLsizei n, const GLuint *ids) /* unbind Atomci Buffer binding points */ for (j = 0; j < ctx->Const.MaxAtomicBufferBindings; j++) { if (ctx->AtomicBufferBindings[j].BufferObject == bufObj) { - _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER, j, 0 ); + bind_buffer_base_atomic_buffer(ctx, j, NULL); } } @@ -1400,31 +1592,41 @@ _mesa_DeleteBuffers(GLsizei n, const GLuint *ids) } +void GLAPIENTRY +_mesa_DeleteBuffers_no_error(GLsizei n, const GLuint *ids) +{ + GET_CURRENT_CONTEXT(ctx); + delete_buffers(ctx, n, ids); +} + + +void GLAPIENTRY +_mesa_DeleteBuffers(GLsizei n, const GLuint *ids) +{ + GET_CURRENT_CONTEXT(ctx); + + if (n < 0) { + _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)"); + return; + } + + delete_buffers(ctx, n, ids); +} + + /** * This is the implementation for glGenBuffers and glCreateBuffers. It is not * exposed to the rest of Mesa to encourage the use of nameless buffers in * driver internals. */ static void -create_buffers(GLsizei n, GLuint *buffers, bool dsa) +create_buffers(struct gl_context *ctx, GLsizei n, GLuint *buffers, bool dsa) { - GET_CURRENT_CONTEXT(ctx); GLuint first; struct gl_buffer_object *buf; - const char *func = dsa ? "glCreateBuffers" : "glGenBuffers"; - - if (MESA_VERBOSE & VERBOSE_API) - _mesa_debug(ctx, "%s(%d)\n", func, n); - - if (n < 0) { - _mesa_error(ctx, GL_INVALID_VALUE, "%s(n %d < 0)", func, n); - return; - } - - if (!buffers) { + if (!buffers) return; - } /* * This must be atomic (generation and allocation of buffer object IDs) @@ -1443,7 +1645,7 @@ create_buffers(GLsizei n, GLuint *buffers, bool dsa) assert(ctx->Driver.NewBufferObject); buf = ctx->Driver.NewBufferObject(ctx, buffers[i]); if (!buf) { - _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func); + _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCreateBuffers"); _mesa_HashUnlockMutex(ctx->Shared->BufferObjects); return; } @@ -1457,16 +1659,42 @@ create_buffers(GLsizei n, GLuint *buffers, bool dsa) _mesa_HashUnlockMutex(ctx->Shared->BufferObjects); } -/** - * Generate a set of unique buffer object IDs and store them in \c buffers. - * - * \param n Number of IDs to generate. - * \param buffers Array of \c n locations to store the IDs. - */ -void GLAPIENTRY + +static void +create_buffers_err(struct gl_context *ctx, GLsizei n, GLuint *buffers, bool dsa) +{ + const char *func = dsa ? "glCreateBuffers" : "glGenBuffers"; + + if (MESA_VERBOSE & VERBOSE_API) + _mesa_debug(ctx, "%s(%d)\n", func, n); + + if (n < 0) { + _mesa_error(ctx, GL_INVALID_VALUE, "%s(n %d < 0)", func, n); + return; + } + + create_buffers(ctx, n, buffers, dsa); +} + +/** + * Generate a set of unique buffer object IDs and store them in \c buffers. + * + * \param n Number of IDs to generate. + * \param buffers Array of \c n locations to store the IDs. + */ +void GLAPIENTRY +_mesa_GenBuffers_no_error(GLsizei n, GLuint *buffers) +{ + GET_CURRENT_CONTEXT(ctx); + create_buffers(ctx, n, buffers, false); +} + + +void GLAPIENTRY _mesa_GenBuffers(GLsizei n, GLuint *buffers) { - create_buffers(n, buffers, false); + GET_CURRENT_CONTEXT(ctx); + create_buffers_err(ctx, n, buffers, false); } /** @@ -1475,10 +1703,19 @@ _mesa_GenBuffers(GLsizei n, GLuint *buffers) * \param n Number of IDs to generate. * \param buffers Array of \c n locations to store the IDs. */ +void GLAPIENTRY +_mesa_CreateBuffers_no_error(GLsizei n, GLuint *buffers) +{ + GET_CURRENT_CONTEXT(ctx); + create_buffers(ctx, n, buffers, true); +} + + void GLAPIENTRY _mesa_CreateBuffers(GLsizei n, GLuint *buffers) { - create_buffers(n, buffers, true); + GET_CURRENT_CONTEXT(ctx); + create_buffers_err(ctx, n, buffers, true); } @@ -1552,7 +1789,7 @@ validate_buffer_storage(struct gl_context *ctx, return false; } - if (bufObj->Immutable) { + if (bufObj->Immutable || bufObj->HandleAllocated) { _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func); return false; } @@ -1563,21 +1800,33 @@ 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); - FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT); + FLUSH_VERTICES(ctx, 0); bufObj->Written = GL_TRUE; 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 @@ -1594,11 +1843,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 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 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) { @@ -1620,7 +1904,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); } @@ -1628,8 +1912,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"); } @@ -1637,8 +1921,41 @@ 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_NamedBufferStorageEXT(GLuint buffer, GLsizeiptr size, + const GLvoid *data, GLbitfield flags) +{ + GET_CURRENT_CONTEXT(ctx); + + struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glNamedBufferStorageEXT")) + return; + + inlined_buffer_storage(GL_NONE, buffer, size, data, flags, GL_NONE, 0, + true, false, false, "glNamedBufferStorageEXT"); +} + + +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"); } @@ -1649,8 +1966,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"); } @@ -1661,15 +1978,32 @@ _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 -_mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj, - GLenum target, GLsizeiptr size, const GLvoid *data, - GLenum usage, const char *func) + +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"); +} + + +static ALWAYS_INLINE void +buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj, + GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage, + const char *func, bool no_error) { bool valid_usage; @@ -1681,50 +2015,49 @@ _mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj, _mesa_enum_to_string(usage)); } - if (size < 0) { - _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func); - return; - } - - switch (usage) { - case GL_STREAM_DRAW_ARB: - valid_usage = (ctx->API != API_OPENGLES); - break; - - case GL_STATIC_DRAW_ARB: - case GL_DYNAMIC_DRAW_ARB: - valid_usage = true; - break; - - case GL_STREAM_READ_ARB: - case GL_STREAM_COPY_ARB: - case GL_STATIC_READ_ARB: - case GL_STATIC_COPY_ARB: - case GL_DYNAMIC_READ_ARB: - case GL_DYNAMIC_COPY_ARB: - valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx); - break; + if (!no_error) { + if (size < 0) { + _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func); + return; + } - default: - valid_usage = false; - break; - } + switch (usage) { + case GL_STREAM_DRAW_ARB: + valid_usage = (ctx->API != API_OPENGLES); + break; + case GL_STATIC_DRAW_ARB: + case GL_DYNAMIC_DRAW_ARB: + valid_usage = true; + break; + case GL_STREAM_READ_ARB: + case GL_STREAM_COPY_ARB: + case GL_STATIC_READ_ARB: + case GL_STATIC_COPY_ARB: + case GL_DYNAMIC_READ_ARB: + case GL_DYNAMIC_COPY_ARB: + valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx); + break; + default: + valid_usage = false; + break; + } - if (!valid_usage) { - _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func, - _mesa_enum_to_string(usage)); - return; - } + if (!valid_usage) { + _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func, + _mesa_enum_to_string(usage)); + return; + } - if (bufObj->Immutable) { - _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func); - return; + if (bufObj->Immutable || bufObj->HandleAllocated) { + _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func); + return; + } } /* Unmap the existing buffer. We'll replace it now. Not an error. */ _mesa_buffer_unmap_all_mappings(ctx, bufObj); - FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT); + FLUSH_VERTICES(ctx, 0); bufObj->Written = GL_TRUE; bufObj->MinMaxCacheDirty = true; @@ -1745,20 +2078,56 @@ _mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj, GL_DYNAMIC_STORAGE_BIT, bufObj)) { if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) { - /* From GL_AMD_pinned_memory: - * - * INVALID_OPERATION is generated by BufferData if is - * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be - * mapped to the GPU address space. - */ - _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func); - } - else { + if (!no_error) { + /* From GL_AMD_pinned_memory: + * + * INVALID_OPERATION is generated by BufferData if is + * EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be + * mapped to the GPU address space. + */ + _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func); + } + } else { _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func); } } } +static void +buffer_data_error(struct gl_context *ctx, struct gl_buffer_object *bufObj, + GLenum target, GLsizeiptr size, const GLvoid *data, + GLenum usage, const char *func) +{ + buffer_data(ctx, bufObj, target, size, data, usage, func, false); +} + +static void +buffer_data_no_error(struct gl_context *ctx, struct gl_buffer_object *bufObj, + GLenum target, GLsizeiptr size, const GLvoid *data, + GLenum usage, const char *func) +{ + buffer_data(ctx, bufObj, target, size, data, usage, func, true); +} + +void +_mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj, + GLenum target, GLsizeiptr size, const GLvoid *data, + GLenum usage, const char *func) +{ + buffer_data_error(ctx, bufObj, target, size, data, usage, func); +} + +void GLAPIENTRY +_mesa_BufferData_no_error(GLenum target, GLsizeiptr size, const GLvoid *data, + GLenum usage) +{ + GET_CURRENT_CONTEXT(ctx); + + struct gl_buffer_object **bufObj = get_buffer_target(ctx, target); + buffer_data_no_error(ctx, *bufObj, target, size, data, usage, + "glBufferData"); +} + void GLAPIENTRY _mesa_BufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage) @@ -1774,6 +2143,17 @@ _mesa_BufferData(GLenum target, GLsizeiptr size, "glBufferData"); } +void GLAPIENTRY +_mesa_NamedBufferData_no_error(GLuint buffer, GLsizeiptr size, + const GLvoid *data, GLenum usage) +{ + GET_CURRENT_CONTEXT(ctx); + + struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer); + buffer_data_no_error(ctx, bufObj, GL_NONE, size, data, usage, + "glNamedBufferData"); +} + void GLAPIENTRY _mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data, GLenum usage) @@ -1792,6 +2172,27 @@ _mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data, "glNamedBufferData"); } +void GLAPIENTRY +_mesa_NamedBufferDataEXT(GLuint buffer, GLsizeiptr size, const GLvoid *data, + GLenum usage) +{ + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *bufObj; + + if (!buffer) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glNamedBufferDataEXT(buffer=0)"); + return; + } + + bufObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glNamedBufferDataEXT")) + return; + + _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage, + "glNamedBufferDataEXT"); +} static bool validate_buffer_sub_data(struct gl_context *ctx, @@ -1920,6 +2321,30 @@ _mesa_NamedBufferSubData(GLuint buffer, GLintptr offset, "glNamedBufferSubData"); } +void GLAPIENTRY +_mesa_NamedBufferSubDataEXT(GLuint buffer, GLintptr offset, + GLsizeiptr size, const GLvoid *data) +{ + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *bufObj; + + if (!buffer) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glNamedBufferSubDataEXT(buffer=0)"); + return; + } + + bufObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glNamedBufferSubDataEXT")) + return; + + if (validate_buffer_sub_data(ctx, bufObj, offset, size, + "glNamedBufferSubDataEXT")) { + _mesa_buffer_sub_data(ctx, bufObj, offset, size, data); + } +} + void GLAPIENTRY _mesa_GetBufferSubData(GLenum target, GLintptr offset, @@ -1964,34 +2389,65 @@ _mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset, } +void GLAPIENTRY +_mesa_GetNamedBufferSubDataEXT(GLuint buffer, GLintptr offset, + GLsizeiptr size, GLvoid *data) +{ + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *bufObj; + + if (!buffer) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glGetNamedBufferSubDataEXT(buffer=0)"); + return; + } + + bufObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glGetNamedBufferSubDataEXT")) + return; + + if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false, + "glGetNamedBufferSubDataEXT")) { + return; + } + + assert(ctx->Driver.GetBufferSubData); + ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj); +} + /** * \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); @@ -2020,6 +2476,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) @@ -2031,10 +2525,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) @@ -2046,8 +2555,40 @@ _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_ClearNamedBufferDataEXT(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); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glClearNamedBufferDataEXT")) + return; + + clear_buffer_sub_data_error(ctx, bufObj, internalformat, 0, bufObj->Size, + format, type, data, "glClearNamedBufferDataEXT", + 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); } @@ -2064,10 +2605,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, @@ -2082,8 +2640,26 @@ _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); +} + +void GLAPIENTRY +_mesa_ClearNamedBufferSubDataEXT(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); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glClearNamedBufferSubDataEXT")) + return; + + clear_buffer_sub_data_error(ctx, bufObj, internalformat, offset, size, + format, type, data, "glClearNamedBufferSubDataEXT", + true); } static GLboolean @@ -2174,7 +2750,7 @@ _mesa_UnmapBuffer(GLenum target) } GLboolean GLAPIENTRY -_mesa_UnmapNamedBuffer_no_error(GLuint buffer) +_mesa_UnmapNamedBufferEXT_no_error(GLuint buffer) { GET_CURRENT_CONTEXT(ctx); struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer); @@ -2183,11 +2759,17 @@ _mesa_UnmapNamedBuffer_no_error(GLuint buffer) } GLboolean GLAPIENTRY -_mesa_UnmapNamedBuffer(GLuint buffer) +_mesa_UnmapNamedBufferEXT(GLuint buffer) { GET_CURRENT_CONTEXT(ctx); struct gl_buffer_object *bufObj; + if (!buffer) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glUnmapNamedBufferEXT(buffer=0)"); + return GL_FALSE; + } + bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glUnmapNamedBuffer"); if (!bufObj) return GL_FALSE; @@ -2309,6 +2891,31 @@ _mesa_GetNamedBufferParameteriv(GLuint buffer, GLenum pname, GLint *params) *params = (GLint) parameter; } +void GLAPIENTRY +_mesa_GetNamedBufferParameterivEXT(GLuint buffer, GLenum pname, GLint *params) +{ + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *bufObj; + GLint64 parameter; + + if (!buffer) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glGetNamedBufferParameterivEXT: buffer=0"); + return; + } + + bufObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glGetNamedBufferParameterivEXT")) + return; + + if (!get_buffer_parameter(ctx, bufObj, pname, ¶meter, + "glGetNamedBufferParameterivEXT")) + return; /* Error already recorded. */ + + *params = (GLint) parameter; +} + void GLAPIENTRY _mesa_GetNamedBufferParameteri64v(GLuint buffer, GLenum pname, GLint64 *params) @@ -2370,6 +2977,30 @@ _mesa_GetNamedBufferPointerv(GLuint buffer, GLenum pname, GLvoid **params) *params = bufObj->Mappings[MAP_USER].Pointer; } +void GLAPIENTRY +_mesa_GetNamedBufferPointervEXT(GLuint buffer, GLenum pname, GLvoid **params) +{ + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *bufObj; + + if (!buffer) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glGetNamedBufferPointervEXT(buffer=0)"); + return; + } + if (pname != GL_BUFFER_MAP_POINTER) { + _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointervEXT(pname != " + "GL_BUFFER_MAP_POINTER)"); + return; + } + + bufObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glGetNamedBufferPointervEXT")) + return; + + *params = bufObj->Mappings[MAP_USER].Pointer; +} static void copy_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *src, @@ -2480,6 +3111,30 @@ _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget, "glCopyBufferSubData"); } +void GLAPIENTRY +_mesa_NamedCopyBufferSubDataEXT(GLuint readBuffer, GLuint writeBuffer, + GLintptr readOffset, GLintptr writeOffset, + GLsizeiptr size) +{ + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *src, *dst; + + src = _mesa_lookup_bufferobj(ctx, readBuffer); + if (!_mesa_handle_bind_buffer_gen(ctx, readBuffer, + &src, + "glNamedCopyBufferSubDataEXT")) + return; + + dst = _mesa_lookup_bufferobj(ctx, writeBuffer); + if (!_mesa_handle_bind_buffer_gen(ctx, writeBuffer, + &dst, + "glNamedCopyBufferSubDataEXT")) + return; + + copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size, + "glNamedCopyBufferSubDataEXT"); +} + void GLAPIENTRY _mesa_CopyNamedBufferSubData_no_error(GLuint readBuffer, GLuint writeBuffer, GLintptr readOffset, @@ -2517,6 +3172,47 @@ _mesa_CopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer, "glCopyNamedBufferSubData"); } +void GLAPIENTRY +_mesa_InternalBufferSubDataCopyMESA(GLintptr srcBuffer, GLuint srcOffset, + GLuint dstTargetOrName, GLintptr dstOffset, + GLsizeiptr size, GLboolean named, + GLboolean ext_dsa) +{ + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *src = (struct gl_buffer_object *)srcBuffer; + struct gl_buffer_object *dst; + const char *func; + + /* Handle behavior for all 3 variants. */ + if (named && ext_dsa) { + func = "glNamedBufferSubDataEXT"; + dst = _mesa_lookup_bufferobj(ctx, dstTargetOrName); + if (!_mesa_handle_bind_buffer_gen(ctx, dstTargetOrName, &dst, func)) + goto done; + } else if (named) { + func = "glNamedBufferSubData"; + dst = _mesa_lookup_bufferobj_err(ctx, dstTargetOrName, func); + if (!dst) + goto done; + } else { + assert(!ext_dsa); + func = "glBufferSubData"; + dst = get_buffer(ctx, func, dstTargetOrName, GL_INVALID_OPERATION); + if (!dst) + goto done; + } + + if (!validate_buffer_sub_data(ctx, dst, dstOffset, size, func)) + goto done; /* the error is already set */ + + dst->MinMaxCacheDirty = true; + ctx->Driver.CopyBufferSubData(ctx, src, dst, srcOffset, dstOffset, size); + +done: + /* The caller passes the reference to this function, so unreference it. */ + _mesa_reference_buffer_object(ctx, &src, NULL); +} + static bool validate_map_buffer_range(struct gl_context *ctx, struct gl_buffer_object *bufObj, GLintptr offset, @@ -2763,30 +3459,55 @@ _mesa_MapNamedBufferRange_no_error(GLuint buffer, GLintptr offset, "glMapNamedBufferRange"); } -void * GLAPIENTRY -_mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length, - GLbitfield access) +static void * +map_named_buffer_range(GLuint buffer, GLintptr offset, GLsizeiptr length, + GLbitfield access, bool dsa_ext, const char *func) { GET_CURRENT_CONTEXT(ctx); - struct gl_buffer_object *bufObj; + struct gl_buffer_object *bufObj = NULL; if (!ctx->Extensions.ARB_map_buffer_range) { _mesa_error(ctx, GL_INVALID_OPERATION, - "glMapNamedBufferRange(" - "ARB_map_buffer_range not supported)"); + "%s(ARB_map_buffer_range not supported)", func); return NULL; } - bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBufferRange"); - if (!bufObj) + if (dsa_ext) { + bufObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, &bufObj, func)) + return NULL; + } else { + bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, func); + if (!bufObj) + return NULL; + } + + if (!validate_map_buffer_range(ctx, bufObj, offset, length, access, func)) return NULL; - if (!validate_map_buffer_range(ctx, bufObj, offset, length, access, - "glMapNamedBufferRange")) + return map_buffer_range(ctx, bufObj, offset, length, access, func); +} + +void * GLAPIENTRY +_mesa_MapNamedBufferRangeEXT(GLuint buffer, GLintptr offset, GLsizeiptr length, + GLbitfield access) +{ + GET_CURRENT_CONTEXT(ctx); + if (!buffer) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glMapNamedBufferRangeEXT(buffer=0)"); return NULL; + } + return map_named_buffer_range(buffer, offset, length, access, true, + "glMapNamedBufferRangeEXT"); +} - return map_buffer_range(ctx, bufObj, offset, length, access, - "glMapNamedBufferRange"); +void * GLAPIENTRY +_mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length, + GLbitfield access) +{ + return map_named_buffer_range(buffer, offset, length, access, false, + "glMapNamedBufferRange"); } /** @@ -2810,6 +3531,7 @@ get_map_buffer_access_flags(struct gl_context *ctx, GLenum access, *flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT; return _mesa_is_desktop_gl(ctx); default: + *flags = 0; return false; } } @@ -2891,6 +3613,34 @@ _mesa_MapNamedBuffer(GLuint buffer, GLenum access) "glMapNamedBuffer"); } +void * GLAPIENTRY +_mesa_MapNamedBufferEXT(GLuint buffer, GLenum access) +{ + GET_CURRENT_CONTEXT(ctx); + + GLbitfield accessFlags; + if (!buffer) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glMapNamedBufferEXT(buffer=0)"); + return NULL; + } + if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) { + _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBufferEXT(invalid access)"); + return NULL; + } + + struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glMapNamedBufferEXT")) + return NULL; + + if (!validate_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags, + "glMapNamedBufferEXT")) + return NULL; + + return map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags, + "glMapNamedBufferEXT"); +} static void flush_mapped_buffer_range(struct gl_context *ctx, @@ -2975,148 +3725,53 @@ _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, } void GLAPIENTRY -_mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer, GLintptr offset, - GLsizeiptr length) -{ - GET_CURRENT_CONTEXT(ctx); - struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer); - - if (ctx->Driver.FlushMappedBufferRange) - ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj, - MAP_USER); -} - -void GLAPIENTRY -_mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset, - GLsizeiptr length) -{ - GET_CURRENT_CONTEXT(ctx); - struct gl_buffer_object *bufObj; - - bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, - "glFlushMappedNamedBufferRange"); - if (!bufObj) - return; - - flush_mapped_buffer_range(ctx, bufObj, offset, length, - "glFlushMappedNamedBufferRange"); -} - - -/** - * Binds a buffer object to a uniform buffer binding point. - * - * The caller is responsible for flushing vertices and updating - * NewDriverState. - */ -static void -set_ubo_binding(struct gl_context *ctx, - struct gl_uniform_buffer_binding *binding, - struct gl_buffer_object *bufObj, - GLintptr offset, - GLsizeiptr size, - GLboolean autoSize) -{ - _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj); - - binding->Offset = offset; - binding->Size = size; - binding->AutomaticSize = autoSize; - - /* If this is a real buffer object, mark it has having been used - * at some point as a UBO. - */ - if (size >= 0) - bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER; -} - -/** - * Binds a buffer object to a shader storage buffer binding point. - * - * The caller is responsible for flushing vertices and updating - * NewDriverState. - */ -static void -set_ssbo_binding(struct gl_context *ctx, - struct gl_shader_storage_buffer_binding *binding, - struct gl_buffer_object *bufObj, - GLintptr offset, - GLsizeiptr size, - GLboolean autoSize) -{ - _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj); - - binding->Offset = offset; - binding->Size = size; - binding->AutomaticSize = autoSize; - - /* If this is a real buffer object, mark it has having been used - * at some point as a SSBO. - */ - if (size >= 0) - bufObj->UsageHistory |= USAGE_SHADER_STORAGE_BUFFER; -} - -/** - * Binds a buffer object to a uniform buffer binding point. - * - * Unlike set_ubo_binding(), this function also flushes vertices - * and updates NewDriverState. It also checks if the binding - * has actually changed before updating it. - */ -static void -bind_uniform_buffer(struct gl_context *ctx, - GLuint index, - struct gl_buffer_object *bufObj, - GLintptr offset, - GLsizeiptr size, - GLboolean autoSize) +_mesa_FlushMappedNamedBufferRange_no_error(GLuint buffer, GLintptr offset, + GLsizeiptr length) { - struct gl_uniform_buffer_binding *binding = - &ctx->UniformBufferBindings[index]; + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer); - if (binding->BufferObject == bufObj && - binding->Offset == offset && - binding->Size == size && - binding->AutomaticSize == autoSize) { - return; - } + if (ctx->Driver.FlushMappedBufferRange) + ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj, + MAP_USER); +} - FLUSH_VERTICES(ctx, 0); - ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer; +void GLAPIENTRY +_mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset, + GLsizeiptr length) +{ + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *bufObj; + + bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, + "glFlushMappedNamedBufferRange"); + if (!bufObj) + return; - set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize); + flush_mapped_buffer_range(ctx, bufObj, offset, length, + "glFlushMappedNamedBufferRange"); } -/** - * Binds a buffer object to a shader storage buffer binding point. - * - * Unlike set_ssbo_binding(), this function also flushes vertices - * and updates NewDriverState. It also checks if the binding - * has actually changed before updating it. - */ -static void -bind_shader_storage_buffer(struct gl_context *ctx, - GLuint index, - struct gl_buffer_object *bufObj, - GLintptr offset, - GLsizeiptr size, - GLboolean autoSize) +void GLAPIENTRY +_mesa_FlushMappedNamedBufferRangeEXT(GLuint buffer, GLintptr offset, + GLsizeiptr length) { - struct gl_shader_storage_buffer_binding *binding = - &ctx->ShaderStorageBufferBindings[index]; + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *bufObj; - if (binding->BufferObject == bufObj && - binding->Offset == offset && - binding->Size == size && - binding->AutomaticSize == autoSize) { + if (!buffer) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glFlushMappedNamedBufferRangeEXT(buffer=0)"); return; } - FLUSH_VERTICES(ctx, 0); - ctx->NewDriverState |= ctx->DriverFlags.NewShaderStorageBuffer; + bufObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glFlushMappedNamedBufferRangeEXT")) + return; - set_ssbo_binding(ctx, binding, bufObj, offset, size, autoSize); + flush_mapped_buffer_range(ctx, bufObj, offset, length, + "glFlushMappedNamedBufferRangeEXT"); } static void @@ -3124,7 +3779,7 @@ bind_buffer_range_uniform_buffer(struct gl_context *ctx, GLuint index, struct gl_buffer_object *bufObj, GLintptr offset, GLsizeiptr size) { - if (bufObj == ctx->Shared->NullBufferObj) { + if (!bufObj) { offset = -1; size = -1; } @@ -3167,7 +3822,7 @@ bind_buffer_range_shader_storage_buffer(struct gl_context *ctx, GLintptr offset, GLsizeiptr size) { - if (bufObj == ctx->Shared->NullBufferObj) { + if (!bufObj) { offset = -1; size = -1; } @@ -3204,122 +3859,46 @@ bind_buffer_range_shader_storage_buffer_err(struct gl_context *ctx, bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset, size); } -/** - * Bind a buffer object to a uniform block binding point. - * As above, but offset = 0. - */ -static void -bind_buffer_base_uniform_buffer(struct gl_context *ctx, - GLuint index, - struct gl_buffer_object *bufObj) -{ - if (index >= ctx->Const.MaxUniformBufferBindings) { - _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index); - return; - } - - _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj); - - if (bufObj == ctx->Shared->NullBufferObj) - bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE); - else - bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE); -} - -/** - * Bind a buffer object to a shader storage block binding point. - * As above, but offset = 0. - */ -static void -bind_buffer_base_shader_storage_buffer(struct gl_context *ctx, - GLuint index, - struct gl_buffer_object *bufObj) -{ - if (index >= ctx->Const.MaxShaderStorageBufferBindings) { - _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index); - return; - } - - _mesa_reference_buffer_object(ctx, &ctx->ShaderStorageBuffer, bufObj); - - if (bufObj == ctx->Shared->NullBufferObj) - bind_shader_storage_buffer(ctx, index, bufObj, -1, -1, GL_TRUE); - else - bind_shader_storage_buffer(ctx, index, bufObj, 0, 0, GL_TRUE); -} - -/** - * Binds a buffer object to an atomic buffer binding point. - * - * The caller is responsible for validating the offset, - * flushing the vertices and updating NewDriverState. - */ static void -set_atomic_buffer_binding(struct gl_context *ctx, - struct gl_atomic_buffer_binding *binding, - struct gl_buffer_object *bufObj, - GLintptr offset, - GLsizeiptr size) +bind_buffer_range_atomic_buffer(struct gl_context *ctx, GLuint index, + struct gl_buffer_object *bufObj, + GLintptr offset, GLsizeiptr size) { - _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj); - - if (bufObj == ctx->Shared->NullBufferObj) { - binding->Offset = 0; - binding->Size = 0; - } else { - binding->Offset = offset; - binding->Size = size; - bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER; + if (!bufObj) { + offset = -1; + size = -1; } -} -static void -bind_atomic_buffer(struct gl_context *ctx, unsigned index, - struct gl_buffer_object *bufObj, GLintptr offset, - GLsizeiptr size) -{ _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj); - - struct gl_atomic_buffer_binding *binding = - &ctx->AtomicBufferBindings[index]; - if (binding->BufferObject == bufObj && - binding->Offset == offset && - binding->Size == size) { - return; - } - - FLUSH_VERTICES(ctx, 0); - ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer; - - set_atomic_buffer_binding(ctx, binding, bufObj, offset, size); + bind_atomic_buffer(ctx, index, bufObj, offset, size, GL_FALSE); } /** - * Binds a buffer object to an atomic buffer binding point. - * - * Unlike set_atomic_buffer_binding(), this function also validates the - * index and offset, flushes vertices, and updates NewDriverState. - * It also checks if the binding has actually changing before - * updating it. + * Bind a region of a buffer object to an atomic storage block binding point. + * \param index the shader storage buffer binding point index + * \param bufObj the buffer object + * \param offset offset to the start of buffer object region + * \param size size of the buffer object region */ static void -bind_atomic_buffer_err(struct gl_context *ctx, unsigned index, - struct gl_buffer_object *bufObj, GLintptr offset, - GLsizeiptr size, const char *name) +bind_buffer_range_atomic_buffer_err(struct gl_context *ctx, + GLuint index, + struct gl_buffer_object *bufObj, + GLintptr offset, GLsizeiptr size) { if (index >= ctx->Const.MaxAtomicBufferBindings) { - _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index); + _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index); return; } if (offset & (ATOMIC_COUNTER_SIZE - 1)) { _mesa_error(ctx, GL_INVALID_VALUE, - "%s(offset misaligned %d/%d)", name, (int) offset, - ATOMIC_COUNTER_SIZE); + "glBindBufferRange(offset misaligned %d/%d)", (int) offset, + ATOMIC_COUNTER_SIZE); return; } - bind_atomic_buffer(ctx, index, bufObj, offset, size); + bind_buffer_range_atomic_buffer(ctx, index, bufObj, offset, size); } static inline bool @@ -3420,11 +3999,9 @@ error_check_bind_shader_storage_buffers(struct gl_context *ctx, static void unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count) { - struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj; - for (int i = 0; i < count; i++) - set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i], - bufObj, -1, -1, GL_TRUE); + set_buffer_binding(ctx, &ctx->UniformBufferBindings[first + i], + NULL, -1, -1, GL_TRUE, 0); } /** @@ -3435,11 +4012,9 @@ static void unbind_shader_storage_buffers(struct gl_context *ctx, GLuint first, GLsizei count) { - struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj; - for (int i = 0; i < count; i++) - set_ssbo_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i], - bufObj, -1, -1, GL_TRUE); + set_buffer_binding(ctx, &ctx->ShaderStorageBufferBindings[first + i], + NULL, -1, -1, GL_TRUE, 0); } static void @@ -3491,9 +4066,8 @@ bind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count, _mesa_HashLockMutex(ctx->Shared->BufferObjects); for (int i = 0; i < count; i++) { - struct gl_uniform_buffer_binding *binding = + struct gl_buffer_binding *binding = &ctx->UniformBufferBindings[first + i]; - struct gl_buffer_object *bufObj; GLintptr offset = 0; GLsizeiptr size = 0; @@ -3535,17 +4109,9 @@ bind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count, size = sizes[i]; } - if (binding->BufferObject && binding->BufferObject->Name == buffers[i]) - bufObj = binding->BufferObject; - else - bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller); - - if (bufObj) { - if (bufObj == ctx->Shared->NullBufferObj) - set_ubo_binding(ctx, binding, bufObj, -1, -1, !range); - else - set_ubo_binding(ctx, binding, bufObj, offset, size, !range); - } + set_buffer_multi_binding(ctx, buffers, i, caller, + binding, offset, size, range, + USAGE_UNIFORM_BUFFER); } _mesa_HashUnlockMutex(ctx->Shared->BufferObjects); @@ -3601,9 +4167,8 @@ bind_shader_storage_buffers(struct gl_context *ctx, GLuint first, _mesa_HashLockMutex(ctx->Shared->BufferObjects); for (int i = 0; i < count; i++) { - struct gl_shader_storage_buffer_binding *binding = + struct gl_buffer_binding *binding = &ctx->ShaderStorageBufferBindings[first + i]; - struct gl_buffer_object *bufObj; GLintptr offset = 0; GLsizeiptr size = 0; @@ -3645,17 +4210,9 @@ bind_shader_storage_buffers(struct gl_context *ctx, GLuint first, size = sizes[i]; } - if (binding->BufferObject && binding->BufferObject->Name == buffers[i]) - bufObj = binding->BufferObject; - else - bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller); - - if (bufObj) { - if (bufObj == ctx->Shared->NullBufferObj) - set_ssbo_binding(ctx, binding, bufObj, -1, -1, !range); - else - set_ssbo_binding(ctx, binding, bufObj, offset, size, !range); - } + set_buffer_multi_binding(ctx, buffers, i, caller, + binding, offset, size, range, + USAGE_SHADER_STORAGE_BUFFER); } _mesa_HashUnlockMutex(ctx->Shared->BufferObjects); @@ -3717,11 +4274,9 @@ unbind_xfb_buffers(struct gl_context *ctx, struct gl_transform_feedback_object *tfObj, GLuint first, GLsizei count) { - struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj; - for (int i = 0; i < count; i++) _mesa_set_transform_feedback_binding(ctx, tfObj, first + i, - bufObj, 0, 0); + NULL, 0, 0); } static void @@ -3785,9 +4340,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; @@ -3833,12 +4385,16 @@ bind_xfb_buffers(struct gl_context *ctx, if (boundBufObj && boundBufObj->Name == buffers[i]) bufObj = boundBufObj; - else - bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller); + else { + bool error; + bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller, + &error); + if (error) + continue; + } - if (bufObj) - _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj, - offset, size); + _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj, + offset, size); } _mesa_HashUnlockMutex(ctx->Shared->BufferObjects); @@ -3879,11 +4435,9 @@ error_check_bind_atomic_buffers(struct gl_context *ctx, static void unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count) { - struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj; - for (int i = 0; i < count; i++) - set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i], - bufObj, -1, -1); + set_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i], + NULL, -1, -1, GL_TRUE, 0); } static void @@ -3938,9 +4492,8 @@ bind_atomic_buffers(struct gl_context *ctx, _mesa_HashLockMutex(ctx->Shared->BufferObjects); for (int i = 0; i < count; i++) { - struct gl_atomic_buffer_binding *binding = + struct gl_buffer_binding *binding = &ctx->AtomicBufferBindings[first + i]; - struct gl_buffer_object *bufObj; GLintptr offset = 0; GLsizeiptr size = 0; @@ -3979,13 +4532,9 @@ bind_atomic_buffers(struct gl_context *ctx, size = sizes[i]; } - if (binding->BufferObject && binding->BufferObject->Name == buffers[i]) - bufObj = binding->BufferObject; - else - bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i, caller); - - if (bufObj) - set_atomic_buffer_binding(ctx, binding, bufObj, offset, size); + set_buffer_multi_binding(ctx, buffers, i, caller, + binding, offset, size, range, + USAGE_ATOMIC_COUNTER_BUFFER); } _mesa_HashUnlockMutex(ctx->Shared->BufferObjects); @@ -3993,7 +4542,7 @@ bind_atomic_buffers(struct gl_context *ctx, static ALWAYS_INLINE void bind_buffer_range(GLenum target, GLuint index, GLuint buffer, GLintptr offset, - GLsizeiptr size) + GLsizeiptr size, bool no_error) { GET_CURRENT_CONTEXT(ctx); struct gl_buffer_object *bufObj; @@ -4005,61 +4554,90 @@ bind_buffer_range(GLenum target, GLuint index, GLuint buffer, GLintptr offset, } if (buffer == 0) { - bufObj = ctx->Shared->NullBufferObj; + bufObj = NULL; } else { bufObj = _mesa_lookup_bufferobj(ctx, buffer); - } - if (!_mesa_handle_bind_buffer_gen(ctx, buffer, - &bufObj, "glBindBufferRange")) - return; - - if (!bufObj) { - _mesa_error(ctx, GL_INVALID_OPERATION, - "glBindBufferRange(invalid buffer=%u)", buffer); - return; - } + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, + &bufObj, "glBindBufferRange")) + return; - if (buffer != 0) { - if (size <= 0) { - _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)", - (int) size); + if (!no_error && !bufObj) { + _mesa_error(ctx, GL_INVALID_OPERATION, + "glBindBufferRange(invalid buffer=%u)", buffer); return; } } - switch (target) { - case GL_TRANSFORM_FEEDBACK_BUFFER: - if (!_mesa_validate_buffer_range_xfb(ctx, - ctx->TransformFeedback.CurrentObject, - index, bufObj, offset, size, - false)) + if (no_error) { + switch (target) { + case GL_TRANSFORM_FEEDBACK_BUFFER: + _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject, + index, bufObj, offset, size); return; + case GL_UNIFORM_BUFFER: + bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size); + return; + case GL_SHADER_STORAGE_BUFFER: + bind_buffer_range_shader_storage_buffer(ctx, index, bufObj, offset, + size); + return; + case GL_ATOMIC_COUNTER_BUFFER: + bind_buffer_range_atomic_buffer(ctx, index, bufObj, offset, size); + return; + default: + unreachable("invalid BindBufferRange target with KHR_no_error"); + } + } else { + if (buffer != 0) { + if (size <= 0) { + _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)", + (int) size); + return; + } + } - _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject, - index, bufObj, offset, size); - return; - case GL_UNIFORM_BUFFER: - bind_buffer_range_uniform_buffer_err(ctx, index, bufObj, offset, size); - return; - case GL_SHADER_STORAGE_BUFFER: - bind_buffer_range_shader_storage_buffer_err(ctx, index, bufObj, offset, - size); - return; - case GL_ATOMIC_COUNTER_BUFFER: - bind_atomic_buffer_err(ctx, index, bufObj, offset, size, - "glBindBufferRange"); - return; - default: - _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)"); - return; + switch (target) { + case GL_TRANSFORM_FEEDBACK_BUFFER: + if (!_mesa_validate_buffer_range_xfb(ctx, + ctx->TransformFeedback.CurrentObject, + index, bufObj, offset, size, + false)) + return; + + _mesa_bind_buffer_range_xfb(ctx, ctx->TransformFeedback.CurrentObject, + index, bufObj, offset, size); + return; + case GL_UNIFORM_BUFFER: + bind_buffer_range_uniform_buffer_err(ctx, index, bufObj, offset, + size); + return; + case GL_SHADER_STORAGE_BUFFER: + bind_buffer_range_shader_storage_buffer_err(ctx, index, bufObj, + offset, size); + return; + case GL_ATOMIC_COUNTER_BUFFER: + bind_buffer_range_atomic_buffer_err(ctx, index, bufObj, + offset, size); + return; + default: + _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)"); + return; + } } } +void GLAPIENTRY +_mesa_BindBufferRange_no_error(GLenum target, GLuint index, GLuint buffer, + GLintptr offset, GLsizeiptr size) +{ + bind_buffer_range(target, index, buffer, offset, size, true); +} + void GLAPIENTRY _mesa_BindBufferRange(GLenum target, GLuint index, GLuint buffer, GLintptr offset, GLsizeiptr size) { - bind_buffer_range(target, index, buffer, offset, size); + bind_buffer_range(target, index, buffer, offset, size, false); } void GLAPIENTRY @@ -4074,18 +4652,18 @@ _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer) } if (buffer == 0) { - bufObj = ctx->Shared->NullBufferObj; + bufObj = NULL; } else { bufObj = _mesa_lookup_bufferobj(ctx, buffer); - } - if (!_mesa_handle_bind_buffer_gen(ctx, buffer, - &bufObj, "glBindBufferBase")) - return; + 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 @@ -4127,8 +4705,7 @@ _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer) bind_buffer_base_shader_storage_buffer(ctx, index, bufObj); return; case GL_ATOMIC_COUNTER_BUFFER: - bind_atomic_buffer_err(ctx, index, bufObj, 0, 0, - "glBindBufferBase"); + bind_buffer_base_atomic_buffer(ctx, index, bufObj); return; default: _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)"); @@ -4208,6 +4785,25 @@ _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count, } } +static ALWAYS_INLINE void +invalidate_buffer_subdata(struct gl_context *ctx, + struct gl_buffer_object *bufObj, GLintptr offset, + GLsizeiptr length) +{ + if (ctx->Driver.InvalidateBufferSubData) + ctx->Driver.InvalidateBufferSubData(ctx, bufObj, offset, length); +} + +void GLAPIENTRY +_mesa_InvalidateBufferSubData_no_error(GLuint buffer, GLintptr offset, + GLsizeiptr length) +{ + GET_CURRENT_CONTEXT(ctx); + + struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, buffer); + invalidate_buffer_subdata(ctx, bufObj, offset, length); +} + void GLAPIENTRY _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset, GLsizeiptr length) @@ -4257,8 +4853,16 @@ _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset, return; } - if (ctx->Driver.InvalidateBufferSubData) - ctx->Driver.InvalidateBufferSubData(ctx, bufObj, offset, length); + invalidate_buffer_subdata(ctx, bufObj, offset, length); +} + +void GLAPIENTRY +_mesa_InvalidateBufferData_no_error(GLuint buffer) +{ + GET_CURRENT_CONTEXT(ctx); + + struct gl_buffer_object *bufObj =_mesa_lookup_bufferobj(ctx, buffer); + invalidate_buffer_subdata(ctx, bufObj, 0, bufObj->Size); } void GLAPIENTRY @@ -4295,8 +4899,7 @@ _mesa_InvalidateBufferData(GLuint buffer) return; } - if (ctx->Driver.InvalidateBufferSubData) - ctx->Driver.InvalidateBufferSubData(ctx, bufObj, 0, bufObj->Size); + invalidate_buffer_subdata(ctx, bufObj, 0, bufObj->Size); } static void @@ -4376,3 +4979,31 @@ _mesa_NamedBufferPageCommitmentARB(GLuint buffer, GLintptr offset, buffer_page_commitment(ctx, bufferObj, offset, size, commit, "glNamedBufferPageCommitmentARB"); } + +void GLAPIENTRY +_mesa_NamedBufferPageCommitmentEXT(GLuint buffer, GLintptr offset, + GLsizeiptr size, GLboolean commit) +{ + GET_CURRENT_CONTEXT(ctx); + struct gl_buffer_object *bufferObj; + + /* Use NamedBuffer* functions logic from EXT_direct_state_access */ + if (buffer != 0) { + bufferObj = _mesa_lookup_bufferobj(ctx, buffer); + if (!_mesa_handle_bind_buffer_gen(ctx, buffer, &bufferObj, + "glNamedBufferPageCommitmentEXT")) + return; + } else { + /* GL_EXT_direct_state_access says about NamedBuffer* functions: + * + * There is no buffer corresponding to the name zero, these commands + * generate the INVALID_OPERATION error if the buffer parameter is + * zero. + */ + _mesa_error(ctx, GL_INVALID_OPERATION, + "glNamedBufferPageCommitmentEXT(buffer = 0)"); + return; + } + buffer_page_commitment(ctx, bufferObj, offset, size, commit, + "glNamedBufferPageCommitmentEXT"); +}