Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  5.  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a
  8.  * copy of this software and associated documentation files (the "Software"),
  9.  * to deal in the Software without restriction, including without limitation
  10.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11.  * and/or sell copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included
  15.  * in all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23.  * OTHER DEALINGS IN THE SOFTWARE.
  24.  */
  25.  
  26.  
  27. /**
  28.  * \file bufferobj.c
  29.  * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
  30.  * \author Brian Paul, Ian Romanick
  31.  */
  32.  
  33. #include <stdbool.h>
  34. #include "glheader.h"
  35. #include "enums.h"
  36. #include "hash.h"
  37. #include "imports.h"
  38. #include "image.h"
  39. #include "context.h"
  40. #include "bufferobj.h"
  41. #include "fbobject.h"
  42. #include "mtypes.h"
  43. #include "texobj.h"
  44. #include "transformfeedback.h"
  45. #include "dispatch.h"
  46.  
  47.  
  48. /* Debug flags */
  49. /*#define VBO_DEBUG*/
  50. /*#define BOUNDS_CHECK*/
  51.  
  52.  
  53. /**
  54.  * Used as a placeholder for buffer objects between glGenBuffers() and
  55.  * glBindBuffer() so that glIsBuffer() can work correctly.
  56.  */
  57. static struct gl_buffer_object DummyBufferObject;
  58.  
  59.  
  60. /**
  61.  * Return pointer to address of a buffer object target.
  62.  * \param ctx  the GL context
  63.  * \param target  the buffer object target to be retrieved.
  64.  * \return   pointer to pointer to the buffer object bound to \c target in the
  65.  *           specified context or \c NULL if \c target is invalid.
  66.  */
  67. static inline struct gl_buffer_object **
  68. get_buffer_target(struct gl_context *ctx, GLenum target)
  69. {
  70.    /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
  71.     */
  72.    if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)
  73.        && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER)
  74.       return NULL;
  75.  
  76.    switch (target) {
  77.    case GL_ARRAY_BUFFER_ARB:
  78.       return &ctx->Array.ArrayBufferObj;
  79.    case GL_ELEMENT_ARRAY_BUFFER_ARB:
  80.       return &ctx->Array.ArrayObj->ElementArrayBufferObj;
  81.    case GL_PIXEL_PACK_BUFFER_EXT:
  82.       return &ctx->Pack.BufferObj;
  83.    case GL_PIXEL_UNPACK_BUFFER_EXT:
  84.       return &ctx->Unpack.BufferObj;
  85.    case GL_COPY_READ_BUFFER:
  86.       return &ctx->CopyReadBuffer;
  87.    case GL_COPY_WRITE_BUFFER:
  88.       return &ctx->CopyWriteBuffer;
  89.    case GL_TRANSFORM_FEEDBACK_BUFFER:
  90.       if (ctx->Extensions.EXT_transform_feedback) {
  91.          return &ctx->TransformFeedback.CurrentBuffer;
  92.       }
  93.       break;
  94.    case GL_TEXTURE_BUFFER:
  95.       if (ctx->API == API_OPENGL_CORE &&
  96.           ctx->Extensions.ARB_texture_buffer_object) {
  97.          return &ctx->Texture.BufferObject;
  98.       }
  99.       break;
  100.    case GL_UNIFORM_BUFFER:
  101.       if (ctx->Extensions.ARB_uniform_buffer_object) {
  102.          return &ctx->UniformBuffer;
  103.       }
  104.       break;
  105.    default:
  106.       return NULL;
  107.    }
  108.    return NULL;
  109. }
  110.  
  111.  
  112. /**
  113.  * Get the buffer object bound to the specified target in a GL context.
  114.  * \param ctx  the GL context
  115.  * \param target  the buffer object target to be retrieved.
  116.  * \return   pointer to the buffer object bound to \c target in the
  117.  *           specified context or \c NULL if \c target is invalid.
  118.  */
  119. static inline struct gl_buffer_object *
  120. get_buffer(struct gl_context *ctx, const char *func, GLenum target)
  121. {
  122.    struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
  123.  
  124.    if (!bufObj) {
  125.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
  126.       return NULL;
  127.    }
  128.  
  129.    if (!_mesa_is_bufferobj(*bufObj)) {
  130.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(buffer 0)", func);
  131.       return NULL;
  132.    }
  133.  
  134.    return *bufObj;
  135. }
  136.  
  137.  
  138. /**
  139.  * Convert a GLbitfield describing the mapped buffer access flags
  140.  * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
  141.  */
  142. static GLenum
  143. simplified_access_mode(struct gl_context *ctx, GLbitfield access)
  144. {
  145.    const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
  146.    if ((access & rwFlags) == rwFlags)
  147.       return GL_READ_WRITE;
  148.    if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
  149.       return GL_READ_ONLY;
  150.    if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
  151.       return GL_WRITE_ONLY;
  152.  
  153.    /* Otherwise, AccessFlags is zero (the default state).
  154.     *
  155.     * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
  156.     *
  157.     * Name           Type  Initial Value  Legal Values
  158.     * ...            ...   ...            ...
  159.     * BUFFER_ACCESS  enum  READ_WRITE     READ_ONLY, WRITE_ONLY
  160.     *                                     READ_WRITE
  161.     *
  162.     * However, table 6.8 in the GL_OES_mapbuffer extension says:
  163.     *
  164.     * Get Value         Type Get Command          Value          Description
  165.     * ---------         ---- -----------          -----          -----------
  166.     * BUFFER_ACCESS_OES Z1   GetBufferParameteriv WRITE_ONLY_OES buffer map flag
  167.     *
  168.     * The difference is because GL_OES_mapbuffer only supports mapping buffers
  169.     * write-only.
  170.     */
  171.    assert(access == 0);
  172.  
  173.    return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
  174. }
  175.  
  176.  
  177. /**
  178.  * Tests the subdata range parameters and sets the GL error code for
  179.  * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
  180.  *
  181.  * \param ctx     GL context.
  182.  * \param target  Buffer object target on which to operate.
  183.  * \param offset  Offset of the first byte of the subdata range.
  184.  * \param size    Size, in bytes, of the subdata range.
  185.  * \param caller  Name of calling function for recording errors.
  186.  * \return   A pointer to the buffer object bound to \c target in the
  187.  *           specified context or \c NULL if any of the parameter or state
  188.  *           conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
  189.  *           are invalid.
  190.  *
  191.  * \sa glBufferSubDataARB, glGetBufferSubDataARB
  192.  */
  193. static struct gl_buffer_object *
  194. buffer_object_subdata_range_good( struct gl_context * ctx, GLenum target,
  195.                                   GLintptrARB offset, GLsizeiptrARB size,
  196.                                   const char *caller )
  197. {
  198.    struct gl_buffer_object *bufObj;
  199.  
  200.    if (size < 0) {
  201.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
  202.       return NULL;
  203.    }
  204.  
  205.    if (offset < 0) {
  206.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
  207.       return NULL;
  208.    }
  209.  
  210.    bufObj = get_buffer(ctx, caller, target);
  211.    if (!bufObj)
  212.       return NULL;
  213.  
  214.    if (offset + size > bufObj->Size) {
  215.       _mesa_error(ctx, GL_INVALID_VALUE,
  216.                   "%s(offset %lu + size %lu > buffer size %lu)", caller,
  217.                   (unsigned long) offset,
  218.                   (unsigned long) size,
  219.                   (unsigned long) bufObj->Size);
  220.       return NULL;
  221.    }
  222.    if (_mesa_bufferobj_mapped(bufObj)) {
  223.       /* Buffer is currently mapped */
  224.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
  225.       return NULL;
  226.    }
  227.  
  228.    return bufObj;
  229. }
  230.  
  231.  
  232. /**
  233.  * Allocate and initialize a new buffer object.
  234.  *
  235.  * Default callback for the \c dd_function_table::NewBufferObject() hook.
  236.  */
  237. static struct gl_buffer_object *
  238. _mesa_new_buffer_object( struct gl_context *ctx, GLuint name, GLenum target )
  239. {
  240.    struct gl_buffer_object *obj;
  241.  
  242.    (void) ctx;
  243.  
  244.    obj = MALLOC_STRUCT(gl_buffer_object);
  245.    _mesa_initialize_buffer_object(ctx, obj, name, target);
  246.    return obj;
  247. }
  248.  
  249.  
  250. /**
  251.  * Delete a buffer object.
  252.  *
  253.  * Default callback for the \c dd_function_table::DeleteBuffer() hook.
  254.  */
  255. static void
  256. _mesa_delete_buffer_object(struct gl_context *ctx,
  257.                            struct gl_buffer_object *bufObj)
  258. {
  259.    (void) ctx;
  260.  
  261.    free(bufObj->Data);
  262.  
  263.    /* assign strange values here to help w/ debugging */
  264.    bufObj->RefCount = -1000;
  265.    bufObj->Name = ~0;
  266.  
  267.    _glthread_DESTROY_MUTEX(bufObj->Mutex);
  268.    free(bufObj);
  269. }
  270.  
  271.  
  272.  
  273. /**
  274.  * Set ptr to bufObj w/ reference counting.
  275.  * This is normally only called from the _mesa_reference_buffer_object() macro
  276.  * when there's a real pointer change.
  277.  */
  278. void
  279. _mesa_reference_buffer_object_(struct gl_context *ctx,
  280.                                struct gl_buffer_object **ptr,
  281.                                struct gl_buffer_object *bufObj)
  282. {
  283.    if (*ptr) {
  284.       /* Unreference the old buffer */
  285.       GLboolean deleteFlag = GL_FALSE;
  286.       struct gl_buffer_object *oldObj = *ptr;
  287.  
  288.       _glthread_LOCK_MUTEX(oldObj->Mutex);
  289.       ASSERT(oldObj->RefCount > 0);
  290.       oldObj->RefCount--;
  291. #if 0
  292.       printf("BufferObj %p %d DECR to %d\n",
  293.              (void *) oldObj, oldObj->Name, oldObj->RefCount);
  294. #endif
  295.       deleteFlag = (oldObj->RefCount == 0);
  296.       _glthread_UNLOCK_MUTEX(oldObj->Mutex);
  297.  
  298.       if (deleteFlag) {
  299.  
  300.          /* some sanity checking: don't delete a buffer still in use */
  301. #if 0
  302.          /* unfortunately, these tests are invalid during context tear-down */
  303.          ASSERT(ctx->Array.ArrayBufferObj != bufObj);
  304.          ASSERT(ctx->Array.ArrayObj->ElementArrayBufferObj != bufObj);
  305.          ASSERT(ctx->Array.ArrayObj->Vertex.BufferObj != bufObj);
  306. #endif
  307.  
  308.          ASSERT(ctx->Driver.DeleteBuffer);
  309.          ctx->Driver.DeleteBuffer(ctx, oldObj);
  310.       }
  311.  
  312.       *ptr = NULL;
  313.    }
  314.    ASSERT(!*ptr);
  315.  
  316.    if (bufObj) {
  317.       /* reference new buffer */
  318.       _glthread_LOCK_MUTEX(bufObj->Mutex);
  319.       if (bufObj->RefCount == 0) {
  320.          /* this buffer's being deleted (look just above) */
  321.          /* Not sure this can every really happen.  Warn if it does. */
  322.          _mesa_problem(NULL, "referencing deleted buffer object");
  323.          *ptr = NULL;
  324.       }
  325.       else {
  326.          bufObj->RefCount++;
  327. #if 0
  328.          printf("BufferObj %p %d INCR to %d\n",
  329.                 (void *) bufObj, bufObj->Name, bufObj->RefCount);
  330. #endif
  331.          *ptr = bufObj;
  332.       }
  333.       _glthread_UNLOCK_MUTEX(bufObj->Mutex);
  334.    }
  335. }
  336.  
  337.  
  338. /**
  339.  * Initialize a buffer object to default values.
  340.  */
  341. void
  342. _mesa_initialize_buffer_object( struct gl_context *ctx,
  343.                                 struct gl_buffer_object *obj,
  344.                                 GLuint name, GLenum target )
  345. {
  346.    (void) target;
  347.  
  348.    memset(obj, 0, sizeof(struct gl_buffer_object));
  349.    _glthread_INIT_MUTEX(obj->Mutex);
  350.    obj->RefCount = 1;
  351.    obj->Name = name;
  352.    obj->Usage = GL_STATIC_DRAW_ARB;
  353.    obj->AccessFlags = 0;
  354. }
  355.  
  356.  
  357.  
  358. /**
  359.  * Callback called from _mesa_HashWalk()
  360.  */
  361. static void
  362. count_buffer_size(GLuint key, void *data, void *userData)
  363. {
  364.    const struct gl_buffer_object *bufObj =
  365.       (const struct gl_buffer_object *) data;
  366.    GLuint *total = (GLuint *) userData;
  367.  
  368.    *total = *total + bufObj->Size;
  369. }
  370.  
  371.  
  372. /**
  373.  * Compute total size (in bytes) of all buffer objects for the given context.
  374.  * For debugging purposes.
  375.  */
  376. GLuint
  377. _mesa_total_buffer_object_memory(struct gl_context *ctx)
  378. {
  379.    GLuint total = 0;
  380.  
  381.    _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
  382.  
  383.    return total;
  384. }
  385.  
  386.  
  387. /**
  388.  * Allocate space for and store data in a buffer object.  Any data that was
  389.  * previously stored in the buffer object is lost.  If \c data is \c NULL,
  390.  * memory will be allocated, but no copy will occur.
  391.  *
  392.  * This is the default callback for \c dd_function_table::BufferData()
  393.  * Note that all GL error checking will have been done already.
  394.  *
  395.  * \param ctx     GL context.
  396.  * \param target  Buffer object target on which to operate.
  397.  * \param size    Size, in bytes, of the new data store.
  398.  * \param data    Pointer to the data to store in the buffer object.  This
  399.  *                pointer may be \c NULL.
  400.  * \param usage   Hints about how the data will be used.
  401.  * \param bufObj  Object to be used.
  402.  *
  403.  * \return GL_TRUE for success, GL_FALSE for failure
  404.  * \sa glBufferDataARB, dd_function_table::BufferData.
  405.  */
  406. static GLboolean
  407. _mesa_buffer_data( struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
  408.                    const GLvoid * data, GLenum usage,
  409.                    struct gl_buffer_object * bufObj )
  410. {
  411.    void * new_data;
  412.  
  413.    (void) ctx; (void) target;
  414.  
  415.    new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size );
  416.    if (new_data) {
  417.       bufObj->Data = (GLubyte *) new_data;
  418.       bufObj->Size = size;
  419.       bufObj->Usage = usage;
  420.  
  421.       if (data) {
  422.          memcpy( bufObj->Data, data, size );
  423.       }
  424.  
  425.       return GL_TRUE;
  426.    }
  427.    else {
  428.       return GL_FALSE;
  429.    }
  430. }
  431.  
  432.  
  433. /**
  434.  * Replace data in a subrange of buffer object.  If the data range
  435.  * specified by \c size + \c offset extends beyond the end of the buffer or
  436.  * if \c data is \c NULL, no copy is performed.
  437.  *
  438.  * This is the default callback for \c dd_function_table::BufferSubData()
  439.  * Note that all GL error checking will have been done already.
  440.  *
  441.  * \param ctx     GL context.
  442.  * \param target  Buffer object target on which to operate.
  443.  * \param offset  Offset of the first byte to be modified.
  444.  * \param size    Size, in bytes, of the data range.
  445.  * \param data    Pointer to the data to store in the buffer object.
  446.  * \param bufObj  Object to be used.
  447.  *
  448.  * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
  449.  */
  450. static void
  451. _mesa_buffer_subdata( struct gl_context *ctx, GLintptrARB offset,
  452.                       GLsizeiptrARB size, const GLvoid * data,
  453.                       struct gl_buffer_object * bufObj )
  454. {
  455.    (void) ctx;
  456.  
  457.    /* this should have been caught in _mesa_BufferSubData() */
  458.    ASSERT(size + offset <= bufObj->Size);
  459.  
  460.    if (bufObj->Data) {
  461.       memcpy( (GLubyte *) bufObj->Data + offset, data, size );
  462.    }
  463. }
  464.  
  465.  
  466. /**
  467.  * Retrieve data from a subrange of buffer object.  If the data range
  468.  * specified by \c size + \c offset extends beyond the end of the buffer or
  469.  * if \c data is \c NULL, no copy is performed.
  470.  *
  471.  * This is the default callback for \c dd_function_table::GetBufferSubData()
  472.  * Note that all GL error checking will have been done already.
  473.  *
  474.  * \param ctx     GL context.
  475.  * \param target  Buffer object target on which to operate.
  476.  * \param offset  Offset of the first byte to be fetched.
  477.  * \param size    Size, in bytes, of the data range.
  478.  * \param data    Destination for data
  479.  * \param bufObj  Object to be used.
  480.  *
  481.  * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
  482.  */
  483. static void
  484. _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
  485.                           GLsizeiptrARB size, GLvoid * data,
  486.                           struct gl_buffer_object * bufObj )
  487. {
  488.    (void) ctx;
  489.  
  490.    if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
  491.       memcpy( data, (GLubyte *) bufObj->Data + offset, size );
  492.    }
  493. }
  494.  
  495.  
  496. /**
  497.  * Default fallback for \c dd_function_table::MapBufferRange().
  498.  * Called via glMapBufferRange().
  499.  */
  500. static void *
  501. _mesa_buffer_map_range( struct gl_context *ctx, GLintptr offset,
  502.                         GLsizeiptr length, GLbitfield access,
  503.                         struct gl_buffer_object *bufObj )
  504. {
  505.    (void) ctx;
  506.    assert(!_mesa_bufferobj_mapped(bufObj));
  507.    /* Just return a direct pointer to the data */
  508.    bufObj->Pointer = bufObj->Data + offset;
  509.    bufObj->Length = length;
  510.    bufObj->Offset = offset;
  511.    bufObj->AccessFlags = access;
  512.    return bufObj->Pointer;
  513. }
  514.  
  515.  
  516. /**
  517.  * Default fallback for \c dd_function_table::FlushMappedBufferRange().
  518.  * Called via glFlushMappedBufferRange().
  519.  */
  520. static void
  521. _mesa_buffer_flush_mapped_range( struct gl_context *ctx,
  522.                                  GLintptr offset, GLsizeiptr length,
  523.                                  struct gl_buffer_object *obj )
  524. {
  525.    (void) ctx;
  526.    (void) offset;
  527.    (void) length;
  528.    (void) obj;
  529.    /* no-op */
  530. }
  531.  
  532.  
  533. /**
  534.  * Default callback for \c dd_function_table::MapBuffer().
  535.  *
  536.  * The input parameters will have been already tested for errors.
  537.  *
  538.  * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
  539.  */
  540. static GLboolean
  541. _mesa_buffer_unmap( struct gl_context *ctx, struct gl_buffer_object *bufObj )
  542. {
  543.    (void) ctx;
  544.    /* XXX we might assert here that bufObj->Pointer is non-null */
  545.    bufObj->Pointer = NULL;
  546.    bufObj->Length = 0;
  547.    bufObj->Offset = 0;
  548.    bufObj->AccessFlags = 0x0;
  549.    return GL_TRUE;
  550. }
  551.  
  552.  
  553. /**
  554.  * Default fallback for \c dd_function_table::CopyBufferSubData().
  555.  * Called via glCopyBufferSubData().
  556.  */
  557. static void
  558. _mesa_copy_buffer_subdata(struct gl_context *ctx,
  559.                           struct gl_buffer_object *src,
  560.                           struct gl_buffer_object *dst,
  561.                           GLintptr readOffset, GLintptr writeOffset,
  562.                           GLsizeiptr size)
  563. {
  564.    GLubyte *srcPtr, *dstPtr;
  565.  
  566.    /* the buffers should not be mapped */
  567.    assert(!_mesa_bufferobj_mapped(src));
  568.    assert(!_mesa_bufferobj_mapped(dst));
  569.  
  570.    if (src == dst) {
  571.       srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
  572.                                                    GL_MAP_READ_BIT |
  573.                                                    GL_MAP_WRITE_BIT, src);
  574.  
  575.       if (!srcPtr)
  576.          return;
  577.  
  578.       srcPtr += readOffset;
  579.       dstPtr += writeOffset;
  580.    } else {
  581.       srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
  582.                                           GL_MAP_READ_BIT, src);
  583.       dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
  584.                                           (GL_MAP_WRITE_BIT |
  585.                                            GL_MAP_INVALIDATE_RANGE_BIT), dst);
  586.    }
  587.  
  588.    /* Note: the src and dst regions will never overlap.  Trying to do so
  589.     * would generate GL_INVALID_VALUE earlier.
  590.     */
  591.    if (srcPtr && dstPtr)
  592.       memcpy(dstPtr, srcPtr, size);
  593.  
  594.    ctx->Driver.UnmapBuffer(ctx, src);
  595.    if (dst != src)
  596.       ctx->Driver.UnmapBuffer(ctx, dst);
  597. }
  598.  
  599.  
  600.  
  601. /**
  602.  * Initialize the state associated with buffer objects
  603.  */
  604. void
  605. _mesa_init_buffer_objects( struct gl_context *ctx )
  606. {
  607.    GLuint i;
  608.  
  609.    memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
  610.    _glthread_INIT_MUTEX(DummyBufferObject.Mutex);
  611.    DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
  612.  
  613.    _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
  614.                                  ctx->Shared->NullBufferObj);
  615.  
  616.    _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
  617.                                  ctx->Shared->NullBufferObj);
  618.    _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
  619.                                  ctx->Shared->NullBufferObj);
  620.  
  621.    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
  622.                                  ctx->Shared->NullBufferObj);
  623.  
  624.    for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
  625.       _mesa_reference_buffer_object(ctx,
  626.                                     &ctx->UniformBufferBindings[i].BufferObject,
  627.                                     ctx->Shared->NullBufferObj);
  628.       ctx->UniformBufferBindings[i].Offset = -1;
  629.       ctx->UniformBufferBindings[i].Size = -1;
  630.    }
  631. }
  632.  
  633.  
  634. void
  635. _mesa_free_buffer_objects( struct gl_context *ctx )
  636. {
  637.    GLuint i;
  638.  
  639.    _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
  640.  
  641.    _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
  642.    _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
  643.  
  644.    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
  645.  
  646.    for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
  647.       _mesa_reference_buffer_object(ctx,
  648.                                     &ctx->UniformBufferBindings[i].BufferObject,
  649.                                     NULL);
  650.    }
  651. }
  652.  
  653. static bool
  654. handle_bind_buffer_gen(struct gl_context *ctx,
  655.                        GLenum target,
  656.                        GLuint buffer,
  657.                        struct gl_buffer_object **buf_handle)
  658. {
  659.    struct gl_buffer_object *buf = *buf_handle;
  660.  
  661.    if (!buf && ctx->API == API_OPENGL_CORE) {
  662.       _mesa_error(ctx, GL_INVALID_OPERATION, "glBindBuffer(non-gen name)");
  663.       return false;
  664.    }
  665.  
  666.    if (!buf || buf == &DummyBufferObject) {
  667.       /* If this is a new buffer object id, or one which was generated but
  668.        * never used before, allocate a buffer object now.
  669.        */
  670.       ASSERT(ctx->Driver.NewBufferObject);
  671.       buf = ctx->Driver.NewBufferObject(ctx, buffer, target);
  672.       if (!buf) {
  673.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
  674.          return false;
  675.       }
  676.       _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
  677.       *buf_handle = buf;
  678.    }
  679.  
  680.    return true;
  681. }
  682.  
  683. /**
  684.  * Bind the specified target to buffer for the specified context.
  685.  * Called by glBindBuffer() and other functions.
  686.  */
  687. static void
  688. bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
  689. {
  690.    struct gl_buffer_object *oldBufObj;
  691.    struct gl_buffer_object *newBufObj = NULL;
  692.    struct gl_buffer_object **bindTarget = NULL;
  693.  
  694.    bindTarget = get_buffer_target(ctx, target);
  695.    if (!bindTarget) {
  696.       _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
  697.       return;
  698.    }
  699.  
  700.    /* Get pointer to old buffer object (to be unbound) */
  701.    oldBufObj = *bindTarget;
  702.    if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
  703.       return;   /* rebinding the same buffer object- no change */
  704.  
  705.    /*
  706.     * Get pointer to new buffer object (newBufObj)
  707.     */
  708.    if (buffer == 0) {
  709.       /* The spec says there's not a buffer object named 0, but we use
  710.        * one internally because it simplifies things.
  711.        */
  712.       newBufObj = ctx->Shared->NullBufferObj;
  713.    }
  714.    else {
  715.       /* non-default buffer object */
  716.       newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
  717.       if (!handle_bind_buffer_gen(ctx, target, buffer, &newBufObj))
  718.          return;
  719.    }
  720.    
  721.    /* bind new buffer */
  722.    _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
  723.  
  724.    /* Pass BindBuffer call to device driver */
  725.    if (ctx->Driver.BindBuffer)
  726.       ctx->Driver.BindBuffer( ctx, target, newBufObj );
  727. }
  728.  
  729.  
  730. /**
  731.  * Update the default buffer objects in the given context to reference those
  732.  * specified in the shared state and release those referencing the old
  733.  * shared state.
  734.  */
  735. void
  736. _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
  737. {
  738.    /* Bind the NullBufferObj to remove references to those
  739.     * in the shared context hash table.
  740.     */
  741.    bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
  742.    bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
  743.    bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
  744.    bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
  745. }
  746.  
  747.  
  748.  
  749. /**
  750.  * Return the gl_buffer_object for the given ID.
  751.  * Always return NULL for ID 0.
  752.  */
  753. struct gl_buffer_object *
  754. _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
  755. {
  756.    if (buffer == 0)
  757.       return NULL;
  758.    else
  759.       return (struct gl_buffer_object *)
  760.          _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
  761. }
  762.  
  763.  
  764. /**
  765.  * If *ptr points to obj, set ptr = the Null/default buffer object.
  766.  * This is a helper for buffer object deletion.
  767.  * The GL spec says that deleting a buffer object causes it to get
  768.  * unbound from all arrays in the current context.
  769.  */
  770. static void
  771. unbind(struct gl_context *ctx,
  772.        struct gl_buffer_object **ptr,
  773.        struct gl_buffer_object *obj)
  774. {
  775.    if (*ptr == obj) {
  776.       _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
  777.    }
  778. }
  779.  
  780.  
  781. /**
  782.  * Plug default/fallback buffer object functions into the device
  783.  * driver hooks.
  784.  */
  785. void
  786. _mesa_init_buffer_object_functions(struct dd_function_table *driver)
  787. {
  788.    /* GL_ARB_vertex/pixel_buffer_object */
  789.    driver->NewBufferObject = _mesa_new_buffer_object;
  790.    driver->DeleteBuffer = _mesa_delete_buffer_object;
  791.    driver->BindBuffer = NULL;
  792.    driver->BufferData = _mesa_buffer_data;
  793.    driver->BufferSubData = _mesa_buffer_subdata;
  794.    driver->GetBufferSubData = _mesa_buffer_get_subdata;
  795.    driver->UnmapBuffer = _mesa_buffer_unmap;
  796.  
  797.    /* GL_ARB_map_buffer_range */
  798.    driver->MapBufferRange = _mesa_buffer_map_range;
  799.    driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
  800.  
  801.    /* GL_ARB_copy_buffer */
  802.    driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
  803. }
  804.  
  805.  
  806.  
  807. /**********************************************************************/
  808. /* API Functions                                                      */
  809. /**********************************************************************/
  810.  
  811. void GLAPIENTRY
  812. _mesa_BindBuffer(GLenum target, GLuint buffer)
  813. {
  814.    GET_CURRENT_CONTEXT(ctx);
  815.  
  816.    if (MESA_VERBOSE & VERBOSE_API)
  817.       _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
  818.                   _mesa_lookup_enum_by_nr(target), buffer);
  819.  
  820.    bind_buffer_object(ctx, target, buffer);
  821. }
  822.  
  823.  
  824. /**
  825.  * Delete a set of buffer objects.
  826.  *
  827.  * \param n      Number of buffer objects to delete.
  828.  * \param ids    Array of \c n buffer object IDs.
  829.  */
  830. void GLAPIENTRY
  831. _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
  832. {
  833.    GET_CURRENT_CONTEXT(ctx);
  834.    GLsizei i;
  835.    FLUSH_VERTICES(ctx, 0);
  836.  
  837.    if (n < 0) {
  838.       _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
  839.       return;
  840.    }
  841.  
  842.    _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
  843.  
  844.    for (i = 0; i < n; i++) {
  845.       struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
  846.       if (bufObj) {
  847.          struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
  848.          GLuint j;
  849.  
  850.          ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
  851.  
  852.          if (_mesa_bufferobj_mapped(bufObj)) {
  853.             /* if mapped, unmap it now */
  854.             ctx->Driver.UnmapBuffer(ctx, bufObj);
  855.             bufObj->AccessFlags = 0;
  856.             bufObj->Pointer = NULL;
  857.          }
  858.  
  859.          /* unbind any vertex pointers bound to this buffer */
  860.          for (j = 0; j < Elements(arrayObj->VertexAttrib); j++) {
  861.             unbind(ctx, &arrayObj->VertexAttrib[j].BufferObj, bufObj);
  862.          }
  863.  
  864.          if (ctx->Array.ArrayBufferObj == bufObj) {
  865.             _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
  866.          }
  867.          if (arrayObj->ElementArrayBufferObj == bufObj) {
  868.             _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
  869.          }
  870.  
  871.          /* unbind ARB_copy_buffer binding points */
  872.          if (ctx->CopyReadBuffer == bufObj) {
  873.             _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
  874.          }
  875.          if (ctx->CopyWriteBuffer == bufObj) {
  876.             _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
  877.          }
  878.  
  879.          /* unbind transform feedback binding points */
  880.          if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
  881.             _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
  882.          }
  883.          for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
  884.             if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
  885.                _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
  886.             }
  887.          }
  888.  
  889.          /* unbind UBO binding points */
  890.          for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
  891.             if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
  892.                _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
  893.             }
  894.          }
  895.  
  896.          if (ctx->UniformBuffer == bufObj) {
  897.             _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
  898.          }
  899.  
  900.          /* unbind any pixel pack/unpack pointers bound to this buffer */
  901.          if (ctx->Pack.BufferObj == bufObj) {
  902.             _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
  903.          }
  904.          if (ctx->Unpack.BufferObj == bufObj) {
  905.             _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
  906.          }
  907.  
  908.          if (ctx->Texture.BufferObject == bufObj) {
  909.             _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
  910.          }
  911.  
  912.          /* The ID is immediately freed for re-use */
  913.          _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
  914.          /* Make sure we do not run into the classic ABA problem on bind.
  915.           * We don't want to allow re-binding a buffer object that's been
  916.           * "deleted" by glDeleteBuffers().
  917.           *
  918.           * The explicit rebinding to the default object in the current context
  919.           * prevents the above in the current context, but another context
  920.           * sharing the same objects might suffer from this problem.
  921.           * The alternative would be to do the hash lookup in any case on bind
  922.           * which would introduce more runtime overhead than this.
  923.           */
  924.          bufObj->DeletePending = GL_TRUE;
  925.          _mesa_reference_buffer_object(ctx, &bufObj, NULL);
  926.       }
  927.    }
  928.  
  929.    _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
  930. }
  931.  
  932.  
  933. /**
  934.  * Generate a set of unique buffer object IDs and store them in \c buffer.
  935.  *
  936.  * \param n       Number of IDs to generate.
  937.  * \param buffer  Array of \c n locations to store the IDs.
  938.  */
  939. void GLAPIENTRY
  940. _mesa_GenBuffers(GLsizei n, GLuint *buffer)
  941. {
  942.    GET_CURRENT_CONTEXT(ctx);
  943.    GLuint first;
  944.    GLint i;
  945.  
  946.    if (MESA_VERBOSE & VERBOSE_API)
  947.       _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
  948.  
  949.    if (n < 0) {
  950.       _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
  951.       return;
  952.    }
  953.  
  954.    if (!buffer) {
  955.       return;
  956.    }
  957.  
  958.    /*
  959.     * This must be atomic (generation and allocation of buffer object IDs)
  960.     */
  961.    _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
  962.  
  963.    first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
  964.  
  965.    /* Insert the ID and pointer to dummy buffer object into hash table */
  966.    for (i = 0; i < n; i++) {
  967.       _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
  968.                        &DummyBufferObject);
  969.       buffer[i] = first + i;
  970.    }
  971.  
  972.    _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
  973. }
  974.  
  975.  
  976. /**
  977.  * Determine if ID is the name of a buffer object.
  978.  *
  979.  * \param id  ID of the potential buffer object.
  980.  * \return  \c GL_TRUE if \c id is the name of a buffer object,
  981.  *          \c GL_FALSE otherwise.
  982.  */
  983. GLboolean GLAPIENTRY
  984. _mesa_IsBuffer(GLuint id)
  985. {
  986.    struct gl_buffer_object *bufObj;
  987.    GET_CURRENT_CONTEXT(ctx);
  988.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
  989.  
  990.    _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
  991.    bufObj = _mesa_lookup_bufferobj(ctx, id);
  992.    _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
  993.  
  994.    return bufObj && bufObj != &DummyBufferObject;
  995. }
  996.  
  997.  
  998. void GLAPIENTRY
  999. _mesa_BufferData(GLenum target, GLsizeiptrARB size,
  1000.                     const GLvoid * data, GLenum usage)
  1001. {
  1002.    GET_CURRENT_CONTEXT(ctx);
  1003.    struct gl_buffer_object *bufObj;
  1004.    bool valid_usage;
  1005.  
  1006.    if (MESA_VERBOSE & VERBOSE_API)
  1007.       _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
  1008.                   _mesa_lookup_enum_by_nr(target),
  1009.                   (long int) size, data,
  1010.                   _mesa_lookup_enum_by_nr(usage));
  1011.  
  1012.    if (size < 0) {
  1013.       _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
  1014.       return;
  1015.    }
  1016.  
  1017.    switch (usage) {
  1018.    case GL_STREAM_DRAW_ARB:
  1019.       valid_usage = (ctx->API != API_OPENGLES);
  1020.       break;
  1021.  
  1022.    case GL_STATIC_DRAW_ARB:
  1023.    case GL_DYNAMIC_DRAW_ARB:
  1024.       valid_usage = true;
  1025.       break;
  1026.  
  1027.    case GL_STREAM_READ_ARB:
  1028.    case GL_STREAM_COPY_ARB:
  1029.    case GL_STATIC_READ_ARB:
  1030.    case GL_STATIC_COPY_ARB:
  1031.    case GL_DYNAMIC_READ_ARB:
  1032.    case GL_DYNAMIC_COPY_ARB:
  1033.       valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
  1034.       break;
  1035.  
  1036.    default:
  1037.       valid_usage = false;
  1038.       break;
  1039.    }
  1040.  
  1041.    if (!valid_usage) {
  1042.       _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
  1043.       return;
  1044.    }
  1045.  
  1046.    bufObj = get_buffer(ctx, "glBufferDataARB", target);
  1047.    if (!bufObj)
  1048.       return;
  1049.  
  1050.    if (_mesa_bufferobj_mapped(bufObj)) {
  1051.       /* Unmap the existing buffer.  We'll replace it now.  Not an error. */
  1052.       ctx->Driver.UnmapBuffer(ctx, bufObj);
  1053.       bufObj->AccessFlags = 0;
  1054.       ASSERT(bufObj->Pointer == NULL);
  1055.    }  
  1056.  
  1057.    FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
  1058.  
  1059.    bufObj->Written = GL_TRUE;
  1060.  
  1061. #ifdef VBO_DEBUG
  1062.    printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
  1063.                 bufObj->Name, size, data, usage);
  1064. #endif
  1065.  
  1066. #ifdef BOUNDS_CHECK
  1067.    size += 100;
  1068. #endif
  1069.  
  1070.    ASSERT(ctx->Driver.BufferData);
  1071.    if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
  1072.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
  1073.    }
  1074. }
  1075.  
  1076.  
  1077. void GLAPIENTRY
  1078. _mesa_BufferSubData(GLenum target, GLintptrARB offset,
  1079.                        GLsizeiptrARB size, const GLvoid * data)
  1080. {
  1081.    GET_CURRENT_CONTEXT(ctx);
  1082.    struct gl_buffer_object *bufObj;
  1083.  
  1084.    bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
  1085.                                               "glBufferSubDataARB" );
  1086.    if (!bufObj) {
  1087.       /* error already recorded */
  1088.       return;
  1089.    }
  1090.  
  1091.    if (size == 0)
  1092.       return;
  1093.  
  1094.    bufObj->Written = GL_TRUE;
  1095.  
  1096.    ASSERT(ctx->Driver.BufferSubData);
  1097.    ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
  1098. }
  1099.  
  1100.  
  1101. void GLAPIENTRY
  1102. _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
  1103.                           GLsizeiptrARB size, void * data)
  1104. {
  1105.    GET_CURRENT_CONTEXT(ctx);
  1106.    struct gl_buffer_object *bufObj;
  1107.  
  1108.    bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
  1109.                                               "glGetBufferSubDataARB" );
  1110.    if (!bufObj) {
  1111.       /* error already recorded */
  1112.       return;
  1113.    }
  1114.  
  1115.    ASSERT(ctx->Driver.GetBufferSubData);
  1116.    ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
  1117. }
  1118.  
  1119.  
  1120. void * GLAPIENTRY
  1121. _mesa_MapBuffer(GLenum target, GLenum access)
  1122. {
  1123.    GET_CURRENT_CONTEXT(ctx);
  1124.    struct gl_buffer_object * bufObj;
  1125.    GLbitfield accessFlags;
  1126.    void *map;
  1127.    bool valid_access;
  1128.  
  1129.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
  1130.  
  1131.    switch (access) {
  1132.    case GL_READ_ONLY_ARB:
  1133.       accessFlags = GL_MAP_READ_BIT;
  1134.       valid_access = _mesa_is_desktop_gl(ctx);
  1135.       break;
  1136.    case GL_WRITE_ONLY_ARB:
  1137.       accessFlags = GL_MAP_WRITE_BIT;
  1138.       valid_access = true;
  1139.       break;
  1140.    case GL_READ_WRITE_ARB:
  1141.       accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
  1142.       valid_access = _mesa_is_desktop_gl(ctx);
  1143.       break;
  1144.    default:
  1145.       valid_access = false;
  1146.       break;
  1147.    }
  1148.  
  1149.    if (!valid_access) {
  1150.       _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
  1151.       return NULL;
  1152.    }
  1153.  
  1154.    bufObj = get_buffer(ctx, "glMapBufferARB", target);
  1155.    if (!bufObj)
  1156.       return NULL;
  1157.  
  1158.    if (_mesa_bufferobj_mapped(bufObj)) {
  1159.       _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
  1160.       return NULL;
  1161.    }
  1162.  
  1163.    if (!bufObj->Size) {
  1164.       _mesa_error(ctx, GL_OUT_OF_MEMORY,
  1165.                   "glMapBuffer(buffer size = 0)");
  1166.       return NULL;
  1167.    }
  1168.  
  1169.    ASSERT(ctx->Driver.MapBufferRange);
  1170.    map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
  1171.    if (!map) {
  1172.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
  1173.       return NULL;
  1174.    }
  1175.    else {
  1176.       /* The driver callback should have set these fields.
  1177.        * This is important because other modules (like VBO) might call
  1178.        * the driver function directly.
  1179.        */
  1180.       ASSERT(bufObj->Pointer == map);
  1181.       ASSERT(bufObj->Length == bufObj->Size);
  1182.       ASSERT(bufObj->Offset == 0);
  1183.       bufObj->AccessFlags = accessFlags;
  1184.    }
  1185.  
  1186.    if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
  1187.       bufObj->Written = GL_TRUE;
  1188.  
  1189. #ifdef VBO_DEBUG
  1190.    printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
  1191.           bufObj->Name, bufObj->Size, access);
  1192.    if (access == GL_WRITE_ONLY_ARB) {
  1193.       GLuint i;
  1194.       GLubyte *b = (GLubyte *) bufObj->Pointer;
  1195.       for (i = 0; i < bufObj->Size; i++)
  1196.          b[i] = i & 0xff;
  1197.    }
  1198. #endif
  1199.  
  1200. #ifdef BOUNDS_CHECK
  1201.    {
  1202.       GLubyte *buf = (GLubyte *) bufObj->Pointer;
  1203.       GLuint i;
  1204.       /* buffer is 100 bytes larger than requested, fill with magic value */
  1205.       for (i = 0; i < 100; i++) {
  1206.          buf[bufObj->Size - i - 1] = 123;
  1207.       }
  1208.    }
  1209. #endif
  1210.  
  1211.    return bufObj->Pointer;
  1212. }
  1213.  
  1214.  
  1215. GLboolean GLAPIENTRY
  1216. _mesa_UnmapBuffer(GLenum target)
  1217. {
  1218.    GET_CURRENT_CONTEXT(ctx);
  1219.    struct gl_buffer_object *bufObj;
  1220.    GLboolean status = GL_TRUE;
  1221.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
  1222.  
  1223.    bufObj = get_buffer(ctx, "glUnmapBufferARB", target);
  1224.    if (!bufObj)
  1225.       return GL_FALSE;
  1226.  
  1227.    if (!_mesa_bufferobj_mapped(bufObj)) {
  1228.       _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
  1229.       return GL_FALSE;
  1230.    }
  1231.  
  1232. #ifdef BOUNDS_CHECK
  1233.    if (bufObj->Access != GL_READ_ONLY_ARB) {
  1234.       GLubyte *buf = (GLubyte *) bufObj->Pointer;
  1235.       GLuint i;
  1236.       /* check that last 100 bytes are still = magic value */
  1237.       for (i = 0; i < 100; i++) {
  1238.          GLuint pos = bufObj->Size - i - 1;
  1239.          if (buf[pos] != 123) {
  1240.             _mesa_warning(ctx, "Out of bounds buffer object write detected"
  1241.                           " at position %d (value = %u)\n",
  1242.                           pos, buf[pos]);
  1243.          }
  1244.       }
  1245.    }
  1246. #endif
  1247.  
  1248. #ifdef VBO_DEBUG
  1249.    if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
  1250.       GLuint i, unchanged = 0;
  1251.       GLubyte *b = (GLubyte *) bufObj->Pointer;
  1252.       GLint pos = -1;
  1253.       /* check which bytes changed */
  1254.       for (i = 0; i < bufObj->Size - 1; i++) {
  1255.          if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
  1256.             unchanged++;
  1257.             if (pos == -1)
  1258.                pos = i;
  1259.          }
  1260.       }
  1261.       if (unchanged) {
  1262.          printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
  1263.                       bufObj->Name, unchanged, bufObj->Size, pos);
  1264.       }
  1265.    }
  1266. #endif
  1267.  
  1268.    status = ctx->Driver.UnmapBuffer( ctx, bufObj );
  1269.    bufObj->AccessFlags = 0;
  1270.    ASSERT(bufObj->Pointer == NULL);
  1271.    ASSERT(bufObj->Offset == 0);
  1272.    ASSERT(bufObj->Length == 0);
  1273.  
  1274.    return status;
  1275. }
  1276.  
  1277.  
  1278. void GLAPIENTRY
  1279. _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
  1280. {
  1281.    GET_CURRENT_CONTEXT(ctx);
  1282.    struct gl_buffer_object *bufObj;
  1283.  
  1284.    bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target);
  1285.    if (!bufObj)
  1286.       return;
  1287.  
  1288.    switch (pname) {
  1289.    case GL_BUFFER_SIZE_ARB:
  1290.       *params = (GLint) bufObj->Size;
  1291.       return;
  1292.    case GL_BUFFER_USAGE_ARB:
  1293.       *params = bufObj->Usage;
  1294.       return;
  1295.    case GL_BUFFER_ACCESS_ARB:
  1296.       *params = simplified_access_mode(ctx, bufObj->AccessFlags);
  1297.       return;
  1298.    case GL_BUFFER_MAPPED_ARB:
  1299.       *params = _mesa_bufferobj_mapped(bufObj);
  1300.       return;
  1301.    case GL_BUFFER_ACCESS_FLAGS:
  1302.       if (!ctx->Extensions.ARB_map_buffer_range)
  1303.          goto invalid_pname;
  1304.       *params = bufObj->AccessFlags;
  1305.       return;
  1306.    case GL_BUFFER_MAP_OFFSET:
  1307.       if (!ctx->Extensions.ARB_map_buffer_range)
  1308.          goto invalid_pname;
  1309.       *params = (GLint) bufObj->Offset;
  1310.       return;
  1311.    case GL_BUFFER_MAP_LENGTH:
  1312.       if (!ctx->Extensions.ARB_map_buffer_range)
  1313.          goto invalid_pname;
  1314.       *params = (GLint) bufObj->Length;
  1315.       return;
  1316.    default:
  1317.       ; /* fall-through */
  1318.    }
  1319.  
  1320. invalid_pname:
  1321.    _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
  1322.                _mesa_lookup_enum_by_nr(pname));
  1323. }
  1324.  
  1325.  
  1326. /**
  1327.  * New in GL 3.2
  1328.  * This is pretty much a duplicate of GetBufferParameteriv() but the
  1329.  * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
  1330.  */
  1331. void GLAPIENTRY
  1332. _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
  1333. {
  1334.    GET_CURRENT_CONTEXT(ctx);
  1335.    struct gl_buffer_object *bufObj;
  1336.  
  1337.    bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target);
  1338.    if (!bufObj)
  1339.       return;
  1340.  
  1341.    switch (pname) {
  1342.    case GL_BUFFER_SIZE_ARB:
  1343.       *params = bufObj->Size;
  1344.       return;
  1345.    case GL_BUFFER_USAGE_ARB:
  1346.       *params = bufObj->Usage;
  1347.       return;
  1348.    case GL_BUFFER_ACCESS_ARB:
  1349.       *params = simplified_access_mode(ctx, bufObj->AccessFlags);
  1350.       return;
  1351.    case GL_BUFFER_ACCESS_FLAGS:
  1352.       if (!ctx->Extensions.ARB_map_buffer_range)
  1353.          goto invalid_pname;
  1354.       *params = bufObj->AccessFlags;
  1355.       return;
  1356.    case GL_BUFFER_MAPPED_ARB:
  1357.       *params = _mesa_bufferobj_mapped(bufObj);
  1358.       return;
  1359.    case GL_BUFFER_MAP_OFFSET:
  1360.       if (!ctx->Extensions.ARB_map_buffer_range)
  1361.          goto invalid_pname;
  1362.       *params = bufObj->Offset;
  1363.       return;
  1364.    case GL_BUFFER_MAP_LENGTH:
  1365.       if (!ctx->Extensions.ARB_map_buffer_range)
  1366.          goto invalid_pname;
  1367.       *params = bufObj->Length;
  1368.       return;
  1369.    default:
  1370.       ; /* fall-through */
  1371.    }
  1372.  
  1373. invalid_pname:
  1374.    _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
  1375.                _mesa_lookup_enum_by_nr(pname));
  1376. }
  1377.  
  1378.  
  1379. void GLAPIENTRY
  1380. _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
  1381. {
  1382.    GET_CURRENT_CONTEXT(ctx);
  1383.    struct gl_buffer_object * bufObj;
  1384.  
  1385.    if (pname != GL_BUFFER_MAP_POINTER_ARB) {
  1386.       _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
  1387.       return;
  1388.    }
  1389.  
  1390.    bufObj = get_buffer(ctx, "glGetBufferPointervARB", target);
  1391.    if (!bufObj)
  1392.       return;
  1393.  
  1394.    *params = bufObj->Pointer;
  1395. }
  1396.  
  1397.  
  1398. void GLAPIENTRY
  1399. _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
  1400.                         GLintptr readOffset, GLintptr writeOffset,
  1401.                         GLsizeiptr size)
  1402. {
  1403.    GET_CURRENT_CONTEXT(ctx);
  1404.    struct gl_buffer_object *src, *dst;
  1405.  
  1406.    src = get_buffer(ctx, "glCopyBufferSubData", readTarget);
  1407.    if (!src)
  1408.       return;
  1409.  
  1410.    dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget);
  1411.    if (!dst)
  1412.       return;
  1413.  
  1414.    if (_mesa_bufferobj_mapped(src)) {
  1415.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1416.                   "glCopyBufferSubData(readBuffer is mapped)");
  1417.       return;
  1418.    }
  1419.  
  1420.    if (_mesa_bufferobj_mapped(dst)) {
  1421.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1422.                   "glCopyBufferSubData(writeBuffer is mapped)");
  1423.       return;
  1424.    }
  1425.  
  1426.    if (readOffset < 0) {
  1427.       _mesa_error(ctx, GL_INVALID_VALUE,
  1428.                   "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
  1429.       return;
  1430.    }
  1431.  
  1432.    if (writeOffset < 0) {
  1433.       _mesa_error(ctx, GL_INVALID_VALUE,
  1434.                   "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
  1435.       return;
  1436.    }
  1437.  
  1438.    if (size < 0) {
  1439.       _mesa_error(ctx, GL_INVALID_VALUE,
  1440.                   "glCopyBufferSubData(writeOffset = %d)", (int) size);
  1441.       return;
  1442.    }
  1443.  
  1444.    if (readOffset + size > src->Size) {
  1445.       _mesa_error(ctx, GL_INVALID_VALUE,
  1446.                   "glCopyBufferSubData(readOffset + size = %d)",
  1447.                   (int) (readOffset + size));
  1448.       return;
  1449.    }
  1450.  
  1451.    if (writeOffset + size > dst->Size) {
  1452.       _mesa_error(ctx, GL_INVALID_VALUE,
  1453.                   "glCopyBufferSubData(writeOffset + size = %d)",
  1454.                   (int) (writeOffset + size));
  1455.       return;
  1456.    }
  1457.  
  1458.    if (src == dst) {
  1459.       if (readOffset + size <= writeOffset) {
  1460.          /* OK */
  1461.       }
  1462.       else if (writeOffset + size <= readOffset) {
  1463.          /* OK */
  1464.       }
  1465.       else {
  1466.          /* overlapping src/dst is illegal */
  1467.          _mesa_error(ctx, GL_INVALID_VALUE,
  1468.                      "glCopyBufferSubData(overlapping src/dst)");
  1469.          return;
  1470.       }
  1471.    }
  1472.  
  1473.    ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
  1474. }
  1475.  
  1476.  
  1477. /**
  1478.  * See GL_ARB_map_buffer_range spec
  1479.  */
  1480. void * GLAPIENTRY
  1481. _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
  1482.                      GLbitfield access)
  1483. {
  1484.    GET_CURRENT_CONTEXT(ctx);
  1485.    struct gl_buffer_object *bufObj;
  1486.    void *map;
  1487.  
  1488.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
  1489.  
  1490.    if (!ctx->Extensions.ARB_map_buffer_range) {
  1491.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1492.                   "glMapBufferRange(extension not supported)");
  1493.       return NULL;
  1494.    }
  1495.  
  1496.    if (offset < 0) {
  1497.       _mesa_error(ctx, GL_INVALID_VALUE,
  1498.                   "glMapBufferRange(offset = %ld)", (long)offset);
  1499.       return NULL;
  1500.    }
  1501.  
  1502.    if (length < 0) {
  1503.       _mesa_error(ctx, GL_INVALID_VALUE,
  1504.                   "glMapBufferRange(length = %ld)", (long)length);
  1505.       return NULL;
  1506.    }
  1507.  
  1508.    /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
  1509.     *
  1510.     *     "An INVALID_OPERATION error is generated for any of the following
  1511.     *     conditions:
  1512.     *
  1513.     *     * <length> is zero."
  1514.     */
  1515.    if (_mesa_is_gles(ctx) && length == 0) {
  1516.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1517.                   "glMapBufferRange(length = 0)");
  1518.       return NULL;
  1519.    }
  1520.  
  1521.    if (access & ~(GL_MAP_READ_BIT |
  1522.                   GL_MAP_WRITE_BIT |
  1523.                   GL_MAP_INVALIDATE_RANGE_BIT |
  1524.                   GL_MAP_INVALIDATE_BUFFER_BIT |
  1525.                   GL_MAP_FLUSH_EXPLICIT_BIT |
  1526.                   GL_MAP_UNSYNCHRONIZED_BIT)) {
  1527.       /* generate an error if any undefind bit is set */
  1528.       _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
  1529.       return NULL;
  1530.    }
  1531.  
  1532.    if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
  1533.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1534.                   "glMapBufferRange(access indicates neither read or write)");
  1535.       return NULL;
  1536.    }
  1537.  
  1538.    if ((access & GL_MAP_READ_BIT) &&
  1539.        (access & (GL_MAP_INVALIDATE_RANGE_BIT |
  1540.                   GL_MAP_INVALIDATE_BUFFER_BIT |
  1541.                   GL_MAP_UNSYNCHRONIZED_BIT))) {
  1542.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1543.                   "glMapBufferRange(invalid access flags)");
  1544.       return NULL;
  1545.    }
  1546.  
  1547.    if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
  1548.        ((access & GL_MAP_WRITE_BIT) == 0)) {
  1549.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1550.                   "glMapBufferRange(invalid access flags)");
  1551.       return NULL;
  1552.    }
  1553.  
  1554.    bufObj = get_buffer(ctx, "glMapBufferRange", target);
  1555.    if (!bufObj)
  1556.       return NULL;
  1557.  
  1558.    if (offset + length > bufObj->Size) {
  1559.       _mesa_error(ctx, GL_INVALID_VALUE,
  1560.                   "glMapBufferRange(offset + length > size)");
  1561.       return NULL;
  1562.    }
  1563.  
  1564.    if (_mesa_bufferobj_mapped(bufObj)) {
  1565.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1566.                   "glMapBufferRange(buffer already mapped)");
  1567.       return NULL;
  1568.    }
  1569.  
  1570.    if (!bufObj->Size) {
  1571.       _mesa_error(ctx, GL_OUT_OF_MEMORY,
  1572.                   "glMapBufferRange(buffer size = 0)");
  1573.       return NULL;
  1574.    }
  1575.  
  1576.    /* Mapping zero bytes should return a non-null pointer. */
  1577.    if (!length) {
  1578.       static long dummy = 0;
  1579.       bufObj->Pointer = &dummy;
  1580.       bufObj->Length = length;
  1581.       bufObj->Offset = offset;
  1582.       bufObj->AccessFlags = access;
  1583.       return bufObj->Pointer;
  1584.    }
  1585.  
  1586.    ASSERT(ctx->Driver.MapBufferRange);
  1587.    map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
  1588.    if (!map) {
  1589.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
  1590.    }
  1591.    else {
  1592.       /* The driver callback should have set all these fields.
  1593.        * This is important because other modules (like VBO) might call
  1594.        * the driver function directly.
  1595.        */
  1596.       ASSERT(bufObj->Pointer == map);
  1597.       ASSERT(bufObj->Length == length);
  1598.       ASSERT(bufObj->Offset == offset);
  1599.       ASSERT(bufObj->AccessFlags == access);
  1600.    }
  1601.  
  1602.    return map;
  1603. }
  1604.  
  1605.  
  1606. /**
  1607.  * See GL_ARB_map_buffer_range spec
  1608.  */
  1609. void GLAPIENTRY
  1610. _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
  1611. {
  1612.    GET_CURRENT_CONTEXT(ctx);
  1613.    struct gl_buffer_object *bufObj;
  1614.  
  1615.    if (!ctx->Extensions.ARB_map_buffer_range) {
  1616.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1617.                   "glFlushMappedBufferRange(extension not supported)");
  1618.       return;
  1619.    }
  1620.  
  1621.    if (offset < 0) {
  1622.       _mesa_error(ctx, GL_INVALID_VALUE,
  1623.                   "glFlushMappedBufferRange(offset = %ld)", (long)offset);
  1624.       return;
  1625.    }
  1626.  
  1627.    if (length < 0) {
  1628.       _mesa_error(ctx, GL_INVALID_VALUE,
  1629.                   "glFlushMappedBufferRange(length = %ld)", (long)length);
  1630.       return;
  1631.    }
  1632.  
  1633.    bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target);
  1634.    if (!bufObj)
  1635.       return;
  1636.  
  1637.    if (!_mesa_bufferobj_mapped(bufObj)) {
  1638.       /* buffer is not mapped */
  1639.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1640.                   "glFlushMappedBufferRange(buffer is not mapped)");
  1641.       return;
  1642.    }
  1643.  
  1644.    if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
  1645.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1646.                   "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
  1647.       return;
  1648.    }
  1649.  
  1650.    if (offset + length > bufObj->Length) {
  1651.       _mesa_error(ctx, GL_INVALID_VALUE,
  1652.                   "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
  1653.                   (long)offset, (long)length, (long)bufObj->Length);
  1654.       return;
  1655.    }
  1656.  
  1657.    ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
  1658.  
  1659.    if (ctx->Driver.FlushMappedBufferRange)
  1660.       ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj);
  1661. }
  1662.  
  1663.  
  1664. static GLenum
  1665. buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
  1666. {
  1667.    struct gl_buffer_object *bufObj;
  1668.    GLenum retval;
  1669.  
  1670.    bufObj = _mesa_lookup_bufferobj(ctx, name);
  1671.    if (!bufObj) {
  1672.       _mesa_error(ctx, GL_INVALID_VALUE,
  1673.                   "glObjectPurgeable(name = 0x%x)", name);
  1674.       return 0;
  1675.    }
  1676.    if (!_mesa_is_bufferobj(bufObj)) {
  1677.       _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
  1678.       return 0;
  1679.    }
  1680.  
  1681.    if (bufObj->Purgeable) {
  1682.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1683.                   "glObjectPurgeable(name = 0x%x) is already purgeable", name);
  1684.       return GL_VOLATILE_APPLE;
  1685.    }
  1686.  
  1687.    bufObj->Purgeable = GL_TRUE;
  1688.  
  1689.    retval = GL_VOLATILE_APPLE;
  1690.    if (ctx->Driver.BufferObjectPurgeable)
  1691.       retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
  1692.  
  1693.    return retval;
  1694. }
  1695.  
  1696.  
  1697. static GLenum
  1698. renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
  1699. {
  1700.    struct gl_renderbuffer *bufObj;
  1701.    GLenum retval;
  1702.  
  1703.    bufObj = _mesa_lookup_renderbuffer(ctx, name);
  1704.    if (!bufObj) {
  1705.       _mesa_error(ctx, GL_INVALID_VALUE,
  1706.                   "glObjectUnpurgeable(name = 0x%x)", name);
  1707.       return 0;
  1708.    }
  1709.  
  1710.    if (bufObj->Purgeable) {
  1711.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1712.                   "glObjectPurgeable(name = 0x%x) is already purgeable", name);
  1713.       return GL_VOLATILE_APPLE;
  1714.    }
  1715.  
  1716.    bufObj->Purgeable = GL_TRUE;
  1717.  
  1718.    retval = GL_VOLATILE_APPLE;
  1719.    if (ctx->Driver.RenderObjectPurgeable)
  1720.       retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
  1721.  
  1722.    return retval;
  1723. }
  1724.  
  1725.  
  1726. static GLenum
  1727. texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
  1728. {
  1729.    struct gl_texture_object *bufObj;
  1730.    GLenum retval;
  1731.  
  1732.    bufObj = _mesa_lookup_texture(ctx, name);
  1733.    if (!bufObj) {
  1734.       _mesa_error(ctx, GL_INVALID_VALUE,
  1735.                   "glObjectPurgeable(name = 0x%x)", name);
  1736.       return 0;
  1737.    }
  1738.  
  1739.    if (bufObj->Purgeable) {
  1740.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1741.                   "glObjectPurgeable(name = 0x%x) is already purgeable", name);
  1742.       return GL_VOLATILE_APPLE;
  1743.    }
  1744.  
  1745.    bufObj->Purgeable = GL_TRUE;
  1746.  
  1747.    retval = GL_VOLATILE_APPLE;
  1748.    if (ctx->Driver.TextureObjectPurgeable)
  1749.       retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
  1750.  
  1751.    return retval;
  1752. }
  1753.  
  1754.  
  1755. GLenum GLAPIENTRY
  1756. _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
  1757. {
  1758.    GLenum retval;
  1759.  
  1760.    GET_CURRENT_CONTEXT(ctx);
  1761.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
  1762.  
  1763.    if (name == 0) {
  1764.       _mesa_error(ctx, GL_INVALID_VALUE,
  1765.                   "glObjectPurgeable(name = 0x%x)", name);
  1766.       return 0;
  1767.    }
  1768.  
  1769.    switch (option) {
  1770.    case GL_VOLATILE_APPLE:
  1771.    case GL_RELEASED_APPLE:
  1772.       /* legal */
  1773.       break;
  1774.    default:
  1775.       _mesa_error(ctx, GL_INVALID_ENUM,
  1776.                   "glObjectPurgeable(name = 0x%x) invalid option: %d",
  1777.                   name, option);
  1778.       return 0;
  1779.    }
  1780.  
  1781.    switch (objectType) {
  1782.    case GL_TEXTURE:
  1783.       retval = texture_object_purgeable(ctx, name, option);
  1784.       break;
  1785.    case GL_RENDERBUFFER_EXT:
  1786.       retval = renderbuffer_purgeable(ctx, name, option);
  1787.       break;
  1788.    case GL_BUFFER_OBJECT_APPLE:
  1789.       retval = buffer_object_purgeable(ctx, name, option);
  1790.       break;
  1791.    default:
  1792.       _mesa_error(ctx, GL_INVALID_ENUM,
  1793.                   "glObjectPurgeable(name = 0x%x) invalid type: %d",
  1794.                   name, objectType);
  1795.       return 0;
  1796.    }
  1797.  
  1798.    /* In strict conformance to the spec, we must only return VOLATILE when
  1799.     * when passed the VOLATILE option. Madness.
  1800.     *
  1801.     * XXX First fix the spec, then fix me.
  1802.     */
  1803.    return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
  1804. }
  1805.  
  1806.  
  1807. static GLenum
  1808. buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
  1809. {
  1810.    struct gl_buffer_object *bufObj;
  1811.    GLenum retval;
  1812.  
  1813.    bufObj = _mesa_lookup_bufferobj(ctx, name);
  1814.    if (!bufObj) {
  1815.       _mesa_error(ctx, GL_INVALID_VALUE,
  1816.                   "glObjectUnpurgeable(name = 0x%x)", name);
  1817.       return 0;
  1818.    }
  1819.  
  1820.    if (! bufObj->Purgeable) {
  1821.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1822.                   "glObjectUnpurgeable(name = 0x%x) object is "
  1823.                   " already \"unpurged\"", name);
  1824.       return 0;
  1825.    }
  1826.  
  1827.    bufObj->Purgeable = GL_FALSE;
  1828.  
  1829.    retval = option;
  1830.    if (ctx->Driver.BufferObjectUnpurgeable)
  1831.       retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
  1832.  
  1833.    return retval;
  1834. }
  1835.  
  1836.  
  1837. static GLenum
  1838. renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
  1839. {
  1840.    struct gl_renderbuffer *bufObj;
  1841.    GLenum retval;
  1842.  
  1843.    bufObj = _mesa_lookup_renderbuffer(ctx, name);
  1844.    if (!bufObj) {
  1845.       _mesa_error(ctx, GL_INVALID_VALUE,
  1846.                   "glObjectUnpurgeable(name = 0x%x)", name);
  1847.       return 0;
  1848.    }
  1849.  
  1850.    if (! bufObj->Purgeable) {
  1851.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1852.                   "glObjectUnpurgeable(name = 0x%x) object is "
  1853.                   " already \"unpurged\"", name);
  1854.       return 0;
  1855.    }
  1856.  
  1857.    bufObj->Purgeable = GL_FALSE;
  1858.  
  1859.    retval = option;
  1860.    if (ctx->Driver.RenderObjectUnpurgeable)
  1861.       retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
  1862.  
  1863.    return retval;
  1864. }
  1865.  
  1866.  
  1867. static GLenum
  1868. texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
  1869. {
  1870.    struct gl_texture_object *bufObj;
  1871.    GLenum retval;
  1872.  
  1873.    bufObj = _mesa_lookup_texture(ctx, name);
  1874.    if (!bufObj) {
  1875.       _mesa_error(ctx, GL_INVALID_VALUE,
  1876.                   "glObjectUnpurgeable(name = 0x%x)", name);
  1877.       return 0;
  1878.    }
  1879.  
  1880.    if (! bufObj->Purgeable) {
  1881.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1882.                   "glObjectUnpurgeable(name = 0x%x) object is"
  1883.                   " already \"unpurged\"", name);
  1884.       return 0;
  1885.    }
  1886.  
  1887.    bufObj->Purgeable = GL_FALSE;
  1888.  
  1889.    retval = option;
  1890.    if (ctx->Driver.TextureObjectUnpurgeable)
  1891.       retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
  1892.  
  1893.    return retval;
  1894. }
  1895.  
  1896.  
  1897. GLenum GLAPIENTRY
  1898. _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
  1899. {
  1900.    GET_CURRENT_CONTEXT(ctx);
  1901.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
  1902.  
  1903.    if (name == 0) {
  1904.       _mesa_error(ctx, GL_INVALID_VALUE,
  1905.                   "glObjectUnpurgeable(name = 0x%x)", name);
  1906.       return 0;
  1907.    }
  1908.  
  1909.    switch (option) {
  1910.    case GL_RETAINED_APPLE:
  1911.    case GL_UNDEFINED_APPLE:
  1912.       /* legal */
  1913.       break;
  1914.    default:
  1915.       _mesa_error(ctx, GL_INVALID_ENUM,
  1916.                   "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
  1917.                   name, option);
  1918.       return 0;
  1919.    }
  1920.  
  1921.    switch (objectType) {
  1922.    case GL_BUFFER_OBJECT_APPLE:
  1923.       return buffer_object_unpurgeable(ctx, name, option);
  1924.    case GL_TEXTURE:
  1925.       return texture_object_unpurgeable(ctx, name, option);
  1926.    case GL_RENDERBUFFER_EXT:
  1927.       return renderbuffer_unpurgeable(ctx, name, option);
  1928.    default:
  1929.       _mesa_error(ctx, GL_INVALID_ENUM,
  1930.                   "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
  1931.                   name, objectType);
  1932.       return 0;
  1933.    }
  1934. }
  1935.  
  1936.  
  1937. static void
  1938. get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
  1939.                               GLenum pname, GLint *params)
  1940. {
  1941.    struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
  1942.    if (!bufObj) {
  1943.       _mesa_error(ctx, GL_INVALID_VALUE,
  1944.                   "glGetObjectParameteriv(name = 0x%x) invalid object", name);
  1945.       return;
  1946.    }
  1947.  
  1948.    switch (pname) {
  1949.    case GL_PURGEABLE_APPLE:
  1950.       *params = bufObj->Purgeable;
  1951.       break;
  1952.    default:
  1953.       _mesa_error(ctx, GL_INVALID_ENUM,
  1954.                   "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
  1955.                   name, pname);
  1956.       break;
  1957.    }
  1958. }
  1959.  
  1960.  
  1961. static void
  1962. get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
  1963.                              GLenum pname, GLint *params)
  1964. {
  1965.    struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
  1966.    if (!rb) {
  1967.       _mesa_error(ctx, GL_INVALID_VALUE,
  1968.                   "glObjectUnpurgeable(name = 0x%x)", name);
  1969.       return;
  1970.    }
  1971.  
  1972.    switch (pname) {
  1973.    case GL_PURGEABLE_APPLE:
  1974.       *params = rb->Purgeable;
  1975.       break;
  1976.    default:
  1977.       _mesa_error(ctx, GL_INVALID_ENUM,
  1978.                   "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
  1979.                   name, pname);
  1980.       break;
  1981.    }
  1982. }
  1983.  
  1984.  
  1985. static void
  1986. get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
  1987.                                GLenum pname, GLint *params)
  1988. {
  1989.    struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
  1990.    if (!texObj) {
  1991.       _mesa_error(ctx, GL_INVALID_VALUE,
  1992.                   "glObjectUnpurgeable(name = 0x%x)", name);
  1993.       return;
  1994.    }
  1995.  
  1996.    switch (pname) {
  1997.    case GL_PURGEABLE_APPLE:
  1998.       *params = texObj->Purgeable;
  1999.       break;
  2000.    default:
  2001.       _mesa_error(ctx, GL_INVALID_ENUM,
  2002.                   "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
  2003.                   name, pname);
  2004.       break;
  2005.    }
  2006. }
  2007.  
  2008.  
  2009. void GLAPIENTRY
  2010. _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
  2011.                                 GLint *params)
  2012. {
  2013.    GET_CURRENT_CONTEXT(ctx);
  2014.  
  2015.    if (name == 0) {
  2016.       _mesa_error(ctx, GL_INVALID_VALUE,
  2017.                   "glGetObjectParameteriv(name = 0x%x)", name);
  2018.       return;
  2019.    }
  2020.  
  2021.    switch (objectType) {
  2022.    case GL_TEXTURE:
  2023.       get_texture_object_parameteriv(ctx, name, pname, params);
  2024.       break;
  2025.    case GL_BUFFER_OBJECT_APPLE:
  2026.       get_buffer_object_parameteriv(ctx, name, pname, params);
  2027.       break;
  2028.    case GL_RENDERBUFFER_EXT:
  2029.       get_renderbuffer_parameteriv(ctx, name, pname, params);
  2030.       break;
  2031.    default:
  2032.       _mesa_error(ctx, GL_INVALID_ENUM,
  2033.                   "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
  2034.                   name, objectType);
  2035.    }
  2036. }
  2037.  
  2038. static void
  2039. set_ubo_binding(struct gl_context *ctx,
  2040.                 int index,
  2041.                 struct gl_buffer_object *bufObj,
  2042.                 GLintptr offset,
  2043.                 GLsizeiptr size,
  2044.                 GLboolean autoSize)
  2045. {
  2046.    struct gl_uniform_buffer_binding *binding;
  2047.  
  2048.    binding = &ctx->UniformBufferBindings[index];
  2049.    if (binding->BufferObject == bufObj &&
  2050.        binding->Offset == offset &&
  2051.        binding->Size == size &&
  2052.        binding->AutomaticSize == autoSize) {
  2053.       return;
  2054.    }
  2055.  
  2056.    FLUSH_VERTICES(ctx, 0);
  2057.    ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
  2058.  
  2059.    _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
  2060.    binding->Offset = offset;
  2061.    binding->Size = size;
  2062.    binding->AutomaticSize = autoSize;
  2063. }
  2064.  
  2065. /**
  2066.  * Bind a region of a buffer object to a uniform block binding point.
  2067.  * \param index  the uniform buffer binding point index
  2068.  * \param bufObj  the buffer object
  2069.  * \param offset  offset to the start of buffer object region
  2070.  * \param size  size of the buffer object region
  2071.  */
  2072. static void
  2073. bind_buffer_range_uniform_buffer(struct gl_context *ctx,
  2074.                                  GLuint index,
  2075.                                  struct gl_buffer_object *bufObj,
  2076.                                  GLintptr offset,
  2077.                                  GLsizeiptr size)
  2078. {
  2079.    if (index >= ctx->Const.MaxUniformBufferBindings) {
  2080.       _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
  2081.       return;
  2082.    }
  2083.  
  2084.    if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
  2085.       _mesa_error(ctx, GL_INVALID_VALUE,
  2086.                   "glBindBufferRange(offset misalgned %d/%d)", (int) offset,
  2087.                   ctx->Const.UniformBufferOffsetAlignment);
  2088.       return;
  2089.    }
  2090.  
  2091.    if (bufObj == ctx->Shared->NullBufferObj) {
  2092.       offset = -1;
  2093.       size = -1;
  2094.    }
  2095.  
  2096.    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
  2097.    set_ubo_binding(ctx, index, bufObj, offset, size, GL_FALSE);
  2098. }
  2099.  
  2100.  
  2101. /**
  2102.  * Bind a buffer object to a uniform block binding point.
  2103.  * As above, but offset = 0.
  2104.  */
  2105. static void
  2106. bind_buffer_base_uniform_buffer(struct gl_context *ctx,
  2107.                                 GLuint index,
  2108.                                 struct gl_buffer_object *bufObj)
  2109. {
  2110.    if (index >= ctx->Const.MaxUniformBufferBindings) {
  2111.       _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
  2112.       return;
  2113.    }
  2114.  
  2115.    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
  2116.    if (bufObj == ctx->Shared->NullBufferObj)
  2117.       set_ubo_binding(ctx, index, bufObj, -1, -1, GL_TRUE);
  2118.    else
  2119.       set_ubo_binding(ctx, index, bufObj, 0, 0, GL_TRUE);
  2120. }
  2121.  
  2122. void GLAPIENTRY
  2123. _mesa_BindBufferRange(GLenum target, GLuint index,
  2124.                       GLuint buffer, GLintptr offset, GLsizeiptr size)
  2125. {
  2126.    GET_CURRENT_CONTEXT(ctx);
  2127.    struct gl_buffer_object *bufObj;
  2128.  
  2129.    if (buffer == 0) {
  2130.       bufObj = ctx->Shared->NullBufferObj;
  2131.    } else {
  2132.       bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  2133.    }
  2134.    if (!handle_bind_buffer_gen(ctx, target, buffer, &bufObj))
  2135.       return;
  2136.  
  2137.    if (!bufObj) {
  2138.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2139.                   "glBindBufferRange(invalid buffer=%u)", buffer);
  2140.       return;
  2141.    }
  2142.  
  2143.    if (buffer != 0) {
  2144.       if (size <= 0) {
  2145.          _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
  2146.                      (int) size);
  2147.          return;
  2148.       }
  2149.    }
  2150.  
  2151.    switch (target) {
  2152.    case GL_TRANSFORM_FEEDBACK_BUFFER:
  2153.       _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
  2154.                                                  offset, size);
  2155.       return;
  2156.    case GL_UNIFORM_BUFFER:
  2157.       bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
  2158.       return;
  2159.    default:
  2160.       _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
  2161.       return;
  2162.    }
  2163. }
  2164.  
  2165. void GLAPIENTRY
  2166. _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
  2167. {
  2168.    GET_CURRENT_CONTEXT(ctx);
  2169.    struct gl_buffer_object *bufObj;
  2170.  
  2171.    if (buffer == 0) {
  2172.       bufObj = ctx->Shared->NullBufferObj;
  2173.    } else {
  2174.       bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  2175.    }
  2176.    if (!handle_bind_buffer_gen(ctx, target, buffer, &bufObj))
  2177.       return;
  2178.  
  2179.    if (!bufObj) {
  2180.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2181.                   "glBindBufferBase(invalid buffer=%u)", buffer);
  2182.       return;
  2183.    }
  2184.  
  2185.    /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
  2186.     * regards to BindBufferBase.  It says (GL 3.1 core spec, page 63):
  2187.     *
  2188.     *     "BindBufferBase is equivalent to calling BindBufferRange with offset
  2189.     *      zero and size equal to the size of buffer."
  2190.     *
  2191.     * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
  2192.     *
  2193.     *     "If the parameter (starting offset or size) was not specified when the
  2194.     *      buffer object was bound, zero is returned."
  2195.     *
  2196.     * What happens if the size of the buffer changes?  Does the size of the
  2197.     * buffer at the moment glBindBufferBase was called still play a role, like
  2198.     * the first quote would imply, or is the size meaningless in the
  2199.     * glBindBufferBase case like the second quote would suggest?  The GL 4.1
  2200.     * core spec page 45 says:
  2201.     *
  2202.     *     "It is equivalent to calling BindBufferRange with offset zero, while
  2203.     *      size is determined by the size of the bound buffer at the time the
  2204.     *      binding is used."
  2205.     *
  2206.     * My interpretation is that the GL 4.1 spec was a clarification of the
  2207.     * behavior, not a change.  In particular, this choice will only make
  2208.     * rendering work in cases where it would have had undefined results.
  2209.     */
  2210.  
  2211.    switch (target) {
  2212.    case GL_TRANSFORM_FEEDBACK_BUFFER:
  2213.       _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
  2214.       return;
  2215.    case GL_UNIFORM_BUFFER:
  2216.       bind_buffer_base_uniform_buffer(ctx, index, bufObj);
  2217.       return;
  2218.    default:
  2219.       _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
  2220.       return;
  2221.    }
  2222. }
  2223.  
  2224. void GLAPIENTRY
  2225. _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
  2226.                               GLsizeiptr length)
  2227. {
  2228.    GET_CURRENT_CONTEXT(ctx);
  2229.    struct gl_buffer_object *bufObj;
  2230.    const GLintptr end = offset + length;
  2231.  
  2232.    bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  2233.    if (!bufObj) {
  2234.       _mesa_error(ctx, GL_INVALID_VALUE,
  2235.                   "glInvalidateBufferSubData(name = 0x%x) invalid object",
  2236.                   buffer);
  2237.       return;
  2238.    }
  2239.  
  2240.    /* The GL_ARB_invalidate_subdata spec says:
  2241.     *
  2242.     *     "An INVALID_VALUE error is generated if <offset> or <length> is
  2243.     *     negative, or if <offset> + <length> is greater than the value of
  2244.     *     BUFFER_SIZE."
  2245.     */
  2246.    if (end < 0 || end > bufObj->Size) {
  2247.       _mesa_error(ctx, GL_INVALID_VALUE,
  2248.                   "glInvalidateBufferSubData(invalid offset or length)");
  2249.       return;
  2250.    }
  2251.  
  2252.    /* The GL_ARB_invalidate_subdata spec says:
  2253.     *
  2254.     *     "An INVALID_OPERATION error is generated if the buffer is currently
  2255.     *     mapped by MapBuffer, or if the invalidate range intersects the range
  2256.     *     currently mapped by MapBufferRange."
  2257.     */
  2258.    if (_mesa_bufferobj_mapped(bufObj)) {
  2259.       const GLintptr mapEnd = bufObj->Offset + bufObj->Length;
  2260.  
  2261.       /* The regions do not overlap if and only if the end of the discard
  2262.        * region is before the mapped region or the start of the discard region
  2263.        * is after the mapped region.
  2264.        *
  2265.        * Note that 'end' and 'mapEnd' are the first byte *after* the discard
  2266.        * region and the mapped region, repsectively.  It is okay for that byte
  2267.        * to be mapped (for 'end') or discarded (for 'mapEnd').
  2268.        */
  2269.       if (!(end <= bufObj->Offset || offset >= mapEnd)) {
  2270.          _mesa_error(ctx, GL_INVALID_OPERATION,
  2271.                      "glInvalidateBufferSubData(intersection with mapped "
  2272.                      "range)");
  2273.          return;
  2274.       }
  2275.    }
  2276.  
  2277.    /* We don't actually do anything for this yet.  Just return after
  2278.     * validating the parameters and generating the required errors.
  2279.     */
  2280.    return;
  2281. }
  2282.  
  2283. void GLAPIENTRY
  2284. _mesa_InvalidateBufferData(GLuint buffer)
  2285. {
  2286.    GET_CURRENT_CONTEXT(ctx);
  2287.    struct gl_buffer_object *bufObj;
  2288.  
  2289.    bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  2290.    if (!bufObj) {
  2291.       _mesa_error(ctx, GL_INVALID_VALUE,
  2292.                   "glInvalidateBufferData(name = 0x%x) invalid object",
  2293.                   buffer);
  2294.       return;
  2295.    }
  2296.  
  2297.    /* The GL_ARB_invalidate_subdata spec says:
  2298.     *
  2299.     *     "An INVALID_OPERATION error is generated if the buffer is currently
  2300.     *     mapped by MapBuffer, or if the invalidate range intersects the range
  2301.     *     currently mapped by MapBufferRange."
  2302.     */
  2303.    if (_mesa_bufferobj_mapped(bufObj)) {
  2304.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2305.                   "glInvalidateBufferData(intersection with mapped "
  2306.                   "range)");
  2307.       return;
  2308.    }
  2309.  
  2310.    /* We don't actually do anything for this yet.  Just return after
  2311.     * validating the parameters and generating the required errors.
  2312.     */
  2313.    return;
  2314. }
  2315.