[Mesa-dev] [PATCH 7/9] mesa: Add support for glBindBufferBase/Range on GL_UNIFORM_BUFFER.

Kenneth Graunke kenneth at whitecape.org
Wed Jun 20 03:07:11 PDT 2012


On 06/18/2012 06:35 PM, Eric Anholt wrote:
> Fixes piglits:
> GL_ARB_uniform_buffer_object/bindbuffer-general-point.
> GL_ARB_uniform_buffer_object/negative-bindbuffer-buffer
> GL_ARB_uniform_buffer_object/negative-bindbuffer-index
> GL_ARB_uniform_buffer_object/negative-bindbuffer-target
> GL_ARB_uniform_buffer_object/negative-bindbufferrange-range
> ---
>  src/mesa/main/bufferobj.c |   98 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 98 insertions(+)
> 
> diff --git a/src/mesa/main/bufferobj.c b/src/mesa/main/bufferobj.c
> index b69ddc9..e00eaf8 100644
> --- a/src/mesa/main/bufferobj.c
> +++ b/src/mesa/main/bufferobj.c
> @@ -1994,6 +1994,97 @@ _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
>  
>  #endif /* FEATURE_APPLE_object_purgeable */
>  
> +static void
> +_set_ubo_binding(struct gl_context *ctx,
> +		 int index,
> +		 struct gl_buffer_object *bufObj,
> +		 GLintptr offset,
> +		 GLsizeiptr size,
> +		 GLboolean autoSize)
> +{
> +   struct gl_uniform_buffer_binding *binding;
> +
> +   binding = &ctx->UniformBufferBindings[index];
> +   if (binding->BufferObject == bufObj &&
> +       binding->Offset == offset &&
> +       binding->Size == size &&
> +       binding->AutomaticSize == autoSize) {
> +      return;
> +   }
> +
> +   FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
> +
> +   _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
> +   binding->Offset = offset;
> +   binding->Size = size;
> +   binding->AutomaticSize = autoSize;
> +}
> +
> +/**
> + * Specify a buffer object to receive vertex shader results.  Plus,
> + * specify the starting offset to place the results, and max size.
> + */
> +static void
> +_mesa_bind_buffer_range_uniform_buffer(struct gl_context *ctx,
> +				       GLuint index,
> +				       struct gl_buffer_object *bufObj,
> +				       GLintptr offset,
> +				       GLsizeiptr size)
> +{
> +   if (index >= ctx->Const.MaxUniformBufferBindings) {
> +      _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
> +      return;
> +   }
> +
> +   if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
> +      _mesa_error(ctx, GL_INVALID_VALUE,
> +                  "glBindBufferRange(offset=%d)", (int) offset);
> +      return;
> +   }
> +
> +   if (size < 0) {
> +      _mesa_error(ctx, GL_INVALID_VALUE,
> +                  "glBindBufferRange(size %d < 0)", (int) size);
> +      return;
> +   }
> +
> +   if (offset + size > bufObj->Size) {
> +      _mesa_error(ctx, GL_INVALID_VALUE,
> +                  "glBindBufferRange(offset + size %d > buffer size %d)",
> +		  (int) (offset + size), (int) (bufObj->Size));
> +      return;
> +   }

Why not put both of these checks into _mesa_BindBufferRange and remove
them from both the UBO and XFB implementations?  I think basically every
version of BindBufferRange will need size < 0 and offset + size >
bufObj->Size checks.

(The XFB version ensures it's > 0, not >=, but still...better to add the
tiny redundancy and be more future-proof IMO.)

> +   if (bufObj == ctx->Shared->NullBufferObj) {
> +      offset = -1;
> +      size = -1;
> +   }
> +
> +   _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
> +   _set_ubo_binding(ctx, index, bufObj, offset, size, GL_FALSE);
> +}
> +
> +
> +/**
> + * Specify a buffer object to receive vertex shader results.
> + * As above, but start at offset = 0.
> + */
> +static void
> +_mesa_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)
> +      _set_ubo_binding(ctx, index, bufObj, -1, -1, GL_TRUE);
> +   else
> +      _set_ubo_binding(ctx, index, bufObj, 0, 0, GL_TRUE);
> +}
>  
>  void GLAPIENTRY
>  _mesa_BindBufferRange(GLenum target, GLuint index,
> @@ -2019,6 +2110,10 @@ _mesa_BindBufferRange(GLenum target, GLuint index,
>        _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
>  						 offset, size);
>        return;
> +   case GL_UNIFORM_BUFFER:
> +      _mesa_bind_buffer_range_uniform_buffer(ctx, index, bufObj,
> +					     offset, size);
> +      return;
>     default:
>        _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
>        return;
> @@ -2047,6 +2142,9 @@ _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
>     case GL_TRANSFORM_FEEDBACK_BUFFER:
>        _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
>        return;
> +   case GL_UNIFORM_BUFFER:
> +      _mesa_bind_buffer_base_uniform_buffer(ctx, index, bufObj);
> +      return;
>     default:
>        _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
>        return;


More information about the mesa-dev mailing list