[Mesa-dev] [PATCH V2 07/10] mesa: Implement functions for clear_buffer_object extensions

Brian Paul brianp at vmware.com
Wed Dec 11 16:39:13 PST 2013


On 12/11/2013 02:55 PM, Pi Tabred wrote:
>   - _mesa_buffer_clear_subdata: default callback for dd function table
>   - _mesa_ClearBufferData: API function
>   - _mesa_ClearBufferSubData: API function
>   - buffer_object_format_good: helper function, check if the internalformat,
>     format and type parameter are legal
>   - buffer_object_convert_clear: helper function, convert the supplied data
>     to the desired internalformat and clear the buffer by calling the
>     callback for dd_function_table::ClearbufferSubData
> ---
>   src/mesa/main/bufferobj.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++
>   src/mesa/main/bufferobj.h |   4 +
>   2 files changed, 254 insertions(+)
>
> diff --git a/src/mesa/main/bufferobj.c b/src/mesa/main/bufferobj.c
> index 0e5b705..72515ef 100644
> --- a/src/mesa/main/bufferobj.c
> +++ b/src/mesa/main/bufferobj.c
> @@ -41,6 +41,9 @@
>   #include "fbobject.h"
>   #include "mtypes.h"
>   #include "texobj.h"
> +#include "teximage.h"
> +#include "glformats.h"
> +#include "texstore.h"
>   #include "transformfeedback.h"
>   #include "dispatch.h"
>
> @@ -283,6 +286,120 @@ buffer_object_subdata_range_good(struct gl_context * ctx, GLenum target,
>
>
>   /**
> + * Tests the format and type parameters and sets the GL error code for
> + * \c glClearBufferData and \c glClearBufferSubData.
> + *
> + * \param ctx            GL context.
> + * \param target         Buffer object target on which to operate.
> + * \param offset         Offset of the first byte of the subdata range.
> + * \param size           Size, in bytes, of the subdata range.
> + * \param mappedRange    If true, checks if an overlapping range is mapped.
> + *                       If false, checks if buffer is mapped.
> + * \param errorNoBuffer  Error code if no buffer is bound to target.
> + * \param caller         Name of calling function for recording errors.
> + * \return   A pointer to the buffer object bound to \c target in the
> + *           specified context or \c NULL if any of the parameter or state
> + *           conditions are invalid.

But the code below returns a gl_format, not a pointer.


> + *
> + * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
> + */
> +static gl_format
> +buffer_object_format_good(struct gl_context *ctx,
> +                          const struct gl_buffer_object *obj,
> +                          GLenum internalformat, GLenum format, GLenum type,
> +                          const char* caller)

Let's rename this function to something like "validate_buffer_clear_format".


> +{
> +   gl_format internalFormatMesa;

I think mesaFormat would be more concise.


> +   GLenum errorFormatType;
> +
> +   internalFormatMesa = _mesa_validate_texbuffer_format(ctx, internalformat);
> +   if (internalFormatMesa == MESA_FORMAT_NONE) {
> +      _mesa_error(ctx, GL_INVALID_ENUM,
> +                  "%s(invalid internalformat)", caller);
> +      return MESA_FORMAT_NONE;
> +   }
> +
> +   /* NOTE: not mentioned in ARB_clear_buffer_object but according to
> +    * EXT_texture_integer there is no conversion between integer and
> +    * non-integer formats
> +   */
> +   if (_mesa_is_enum_format_signed_int(format) !=
> +       _mesa_is_format_integer_color(internalFormatMesa)) {
> +      _mesa_error(ctx, GL_INVALID_OPERATION,
> +                  "%s(integer vs non-integer)", caller);
> +      return MESA_FORMAT_NONE;
> +   }
> +
> +   if (!_mesa_is_color_format(format)) {
> +      _mesa_error(ctx, GL_INVALID_ENUM,
> +                  "%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,
> +                  "%s(invalid format or type)", caller);
> +      return MESA_FORMAT_NONE;
> +   }
> +
> +   return internalFormatMesa;
> +}
> +
> +
> +/**
> + * Converts the supplied data to the internalformat and clears the desired
> + * range.
> + *
> + * \param ctx             GL context.
> + * \param offset          Offset of the to be cleared range.
> + * \param size            Size of the to be cleared range.
> + * \param internalformat  Format to which the data is converted.
> + * \param sizeOfFormat    Size of the internalformat in bytes.
> + * \param format          Format of the supplied data.
> + * \param type            Type of the supplied data.
> + * \param data            Data which is used to clear the buffer.
> + * \param bufObj          To be cleared buffer.
> + * \return   A pointer to the buffer object bound to \c target in the
> + *           specified context or \c NULL if any of the parameter or state
> + *           conditions are invalid.
> + *
> + * \sa glClearBufferData, glClearBufferSubData
> + */
> +static void
> +buffer_object_convert_clear(struct gl_context *ctx,
> +                            GLintptr offset, GLsizeiptr size,
> +                            gl_format internalformat,
> +                            unsigned int sizeOfFormat,
> +                            GLenum format, GLenum type, const GLvoid* data,
> +                            struct gl_buffer_object *bufObj)
> +{
> +   GLenum internalformatBase;
> +   GLubyte* src;
> +
> +   if (data == NULL) {
> +      ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
> +                                     NULL, 0, bufObj);
> +      return;
> +   }
> +
> +   internalformatBase = _mesa_get_format_base_format(internalformat);
> +
> +   src = (GLubyte*) malloc(sizeOfFormat);
> +   assert(src);
> +   GLboolean success = _mesa_texstore(ctx, 1, internalformatBase,
> +                                      internalformat, 0, &src, 1, 1, 1,
> +                                      format, type, data, &ctx->Unpack);
> +   assert(success);
> +
> +   ctx->Driver.ClearBufferSubData(ctx, offset, size,
> +                                  src, sizeOfFormat, bufObj);
> +   free(src);
> +}

I think this function should just convert the user's clear value to the 
internal representation.  Then have the callers do the 
ctx->Driver.ClearBufferSubData() call.

And since sizeOfFormat will always be <=16 bytes, we could just use a 
GLubyte clearValue[16] buffer instead of malloc'ing the buffer.


> +
> +
> +/**
>    * Allocate and initialize a new buffer object.
>    *
>    * Default callback for the \c dd_function_table::NewBufferObject() hook.
> @@ -547,6 +664,55 @@ _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
>
>
>   /**
> + * Clear a subrange of buffer object with supplied data. If the data range
> + * specified by \c size + \c offset extends beyond the end of the buffer
> + * the error INVALID_VALUE is generated, if data is NULL the buffer is filled
> + * with zeros.
> + *
> + * This is the default callback for \c dd_function_table::ClearBufferSubData()
> + * Note that all GL error checking will have been done already.
> + *
> + * \param ctx             GL context.
> + * \param internalformat  Internal format of the buffer data.

There's no such parameter below.

> + * \param offset          Offset of the first byte to be cleared.
> + * \param size            Size, in bytes, of the to be cleared range.
> + * \param data            Source of the data.

Maybe rename this to "clearValue"

> + * \param dataSize        Size, in bytes, of the supplied data.

Maybe "clearValueSize"

> + * \param bufObj          Object to be used.

"buffer object to be cleared"

> + *
> + * \sa glClearBufferSubDataARB, dd_function_table::ClearBufferSubData.
> + */
> +static void
> +_mesa_buffer_clear_subdata(struct gl_context *ctx,
> +                           GLintptr offset, GLsizeiptr size,
> +                           const GLvoid *data, unsigned int dataSize,
> +                           struct gl_buffer_object *bufObj)
> +{
> +   int i;

Just to be extra safe for the future, 'i' should probably be a GLintptr.

> +   void* dest;
> +
> +   ASSERT(ctx->Driver.MapBufferRange);
> +   dest = ctx->Driver.MapBufferRange(ctx, offset, size,
> +                                     GL_MAP_WRITE_BIT |
> +                                     GL_MAP_INVALIDATE_RANGE_BIT,
> +                                     bufObj);
> +
> +   if (data == NULL) {
> +      memset(dest, 0, size);
> +      ctx->Driver.UnmapBuffer(ctx, bufObj);
> +      return;
> +   }
> +
> +   for (i = 0; i < size/dataSize; ++i)
> +   {
> +      memcpy(dest, data, dataSize);
> +      dest += dataSize;

this void pointer arithmetic will generate a warning (or error?) with 
MSVC.  Use GLubyte instead.


> +   }
> +   ctx->Driver.UnmapBuffer(ctx, bufObj);
> +}
> +
> +
> +/**
>    * Default fallback for \c dd_function_table::MapBufferRange().
>    * Called via glMapBufferRange().
>    */
> @@ -852,6 +1018,9 @@ _mesa_init_buffer_object_functions(struct dd_function_table *driver)
>      driver->GetBufferSubData = _mesa_buffer_get_subdata;
>      driver->UnmapBuffer = _mesa_buffer_unmap;
>
> +   /* GL_ARB_clear_buffer_object */
> +   driver->ClearBufferSubData = _mesa_buffer_clear_subdata;
> +
>      /* GL_ARB_map_buffer_range */
>      driver->MapBufferRange = _mesa_buffer_map_range;
>      driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
> @@ -1182,6 +1351,87 @@ _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
>   }
>
>
> +void GLAPIENTRY
> +_mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
> +                      GLenum type, const GLvoid* data)
> +{
> +   GET_CURRENT_CONTEXT(ctx);
> +   struct gl_buffer_object* bufObj;
> +   gl_format internalformatMesa;
> +   GLint sizeOfFormat;
> +
> +   bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
> +   if (!bufObj) {
> +      return;
> +   }
> +
> +   if (_mesa_bufferobj_mapped(bufObj)) {
> +       _mesa_error(ctx, GL_INVALID_OPERATION,
> +                   "glClearBufferData(buffer currently mapped)");
> +       return;
> +   }
> +
> +   internalformatMesa = buffer_object_format_good(ctx, bufObj,
> +                                                  internalformat,
> +                                                  format, type,
> +                                                  "glClearBufferData");
> +   if (internalformatMesa == MESA_FORMAT_NONE) {
> +      return;
> +   }
> +
> +
> +   sizeOfFormat = _mesa_get_format_bytes(internalformatMesa);
> +   if (bufObj->Size % sizeOfFormat != 0) {
> +      _mesa_error(ctx, GL_INVALID_VALUE,
> +                  "glClearBufferData(size is not a multiple of internalformat size)");
> +      return;
> +   }
> +
> +   buffer_object_convert_clear(ctx, 0, bufObj->Size,
> +                               internalformatMesa, sizeOfFormat,
> +                               format, type, data, bufObj);
> +}
> +
> +
> +void GLAPIENTRY
> +_mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
> +                         GLintptr offset, GLsizeiptr size,
> +                         GLenum format, GLenum type,
> +                         const GLvoid* data)
> +{
> +   GET_CURRENT_CONTEXT(ctx);
> +   struct gl_buffer_object* bufObj;
> +   gl_format internalformatMesa;
> +   GLint sizeOfFormat;
> +
> +   bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
> +                                             true, GL_INVALID_VALUE,
> +                                             "glClearBufferSubData");
> +   if (!bufObj) {
> +      return;
> +   }
> +
> +   internalformatMesa = buffer_object_format_good(ctx, bufObj,
> +                                                  internalformat,
> +                                                  format, type,
> +                                                  "glClearBufferData");
> +   if (internalformatMesa == MESA_FORMAT_NONE) {
> +      return;
> +   }
> +
> +   sizeOfFormat = _mesa_get_format_bytes(internalformatMesa);
> +   if (offset % sizeOfFormat != 0 || size % sizeOfFormat != 0) {
> +      _mesa_error(ctx, GL_INVALID_VALUE,
> +                  "glClearBufferSubData(offset or size is not a multiple of internalformat size)");
> +      return;
> +   }
> +
> +   buffer_object_convert_clear(ctx, offset, size,
> +                               internalformatMesa, sizeOfFormat,
> +                               format, type, data, bufObj);
> +}
> +
> +
>   void * GLAPIENTRY
>   _mesa_MapBuffer(GLenum target, GLenum access)
>   {
> diff --git a/src/mesa/main/bufferobj.h b/src/mesa/main/bufferobj.h
> index 0b898a2..b38519f 100644
> --- a/src/mesa/main/bufferobj.h
> +++ b/src/mesa/main/bufferobj.h
> @@ -120,6 +120,10 @@ void GLAPIENTRY
>   _mesa_BufferSubData(GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid * data);
>   void GLAPIENTRY
>   _mesa_GetBufferSubData(GLenum target, GLintptrARB offset, GLsizeiptrARB size, void * data);
> +void GLAPIENTRY
> +_mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format, GLenum type, const GLvoid * data);
> +void GLAPIENTRY
> +_mesa_ClearBufferSubData(GLenum target, GLenum internalformat, GLintptr offset, GLsizeiptr size, GLenum format, GLenum type, const GLvoid * data);
>   void * GLAPIENTRY
>   _mesa_MapBuffer(GLenum target, GLenum access);
>   GLboolean GLAPIENTRY
>



More information about the mesa-dev mailing list