Subversion Repositories Kolibri OS

Rev

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 <inttypes.h>  /* for PRId64 macro */
  35. #include "glheader.h"
  36. #include "enums.h"
  37. #include "hash.h"
  38. #include "imports.h"
  39. #include "image.h"
  40. #include "context.h"
  41. #include "bufferobj.h"
  42. #include "fbobject.h"
  43. #include "mtypes.h"
  44. #include "texobj.h"
  45. #include "teximage.h"
  46. #include "glformats.h"
  47. #include "texstore.h"
  48. #include "transformfeedback.h"
  49. #include "dispatch.h"
  50.  
  51.  
  52. /* Debug flags */
  53. /*#define VBO_DEBUG*/
  54. /*#define BOUNDS_CHECK*/
  55.  
  56.  
  57. /**
  58.  * Used as a placeholder for buffer objects between glGenBuffers() and
  59.  * glBindBuffer() so that glIsBuffer() can work correctly.
  60.  */
  61. static struct gl_buffer_object DummyBufferObject;
  62.  
  63.  
  64. /**
  65.  * Return pointer to address of a buffer object target.
  66.  * \param ctx  the GL context
  67.  * \param target  the buffer object target to be retrieved.
  68.  * \return   pointer to pointer to the buffer object bound to \c target in the
  69.  *           specified context or \c NULL if \c target is invalid.
  70.  */
  71. static inline struct gl_buffer_object **
  72. get_buffer_target(struct gl_context *ctx, GLenum target)
  73. {
  74.    /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
  75.     */
  76.    if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)
  77.        && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER)
  78.       return NULL;
  79.  
  80.    switch (target) {
  81.    case GL_ARRAY_BUFFER_ARB:
  82.       return &ctx->Array.ArrayBufferObj;
  83.    case GL_ELEMENT_ARRAY_BUFFER_ARB:
  84.       return &ctx->Array.VAO->IndexBufferObj;
  85.    case GL_PIXEL_PACK_BUFFER_EXT:
  86.       return &ctx->Pack.BufferObj;
  87.    case GL_PIXEL_UNPACK_BUFFER_EXT:
  88.       return &ctx->Unpack.BufferObj;
  89.    case GL_COPY_READ_BUFFER:
  90.       return &ctx->CopyReadBuffer;
  91.    case GL_COPY_WRITE_BUFFER:
  92.       return &ctx->CopyWriteBuffer;
  93.    case GL_DRAW_INDIRECT_BUFFER:
  94.       if (ctx->API == API_OPENGL_CORE &&
  95.           ctx->Extensions.ARB_draw_indirect) {
  96.          return &ctx->DrawIndirectBuffer;
  97.       }
  98.       break;
  99.    case GL_TRANSFORM_FEEDBACK_BUFFER:
  100.       if (ctx->Extensions.EXT_transform_feedback) {
  101.          return &ctx->TransformFeedback.CurrentBuffer;
  102.       }
  103.       break;
  104.    case GL_TEXTURE_BUFFER:
  105.       if (ctx->API == API_OPENGL_CORE &&
  106.           ctx->Extensions.ARB_texture_buffer_object) {
  107.          return &ctx->Texture.BufferObject;
  108.       }
  109.       break;
  110.    case GL_UNIFORM_BUFFER:
  111.       if (ctx->Extensions.ARB_uniform_buffer_object) {
  112.          return &ctx->UniformBuffer;
  113.       }
  114.       break;
  115.    case GL_ATOMIC_COUNTER_BUFFER:
  116.       if (ctx->Extensions.ARB_shader_atomic_counters) {
  117.          return &ctx->AtomicBuffer;
  118.       }
  119.       break;
  120.    case GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD:
  121.       if (ctx->Extensions.AMD_pinned_memory) {
  122.          return &ctx->ExternalVirtualMemoryBuffer;
  123.       }
  124.       break;
  125.    default:
  126.       return NULL;
  127.    }
  128.    return NULL;
  129. }
  130.  
  131.  
  132. /**
  133.  * Get the buffer object bound to the specified target in a GL context.
  134.  * \param ctx  the GL context
  135.  * \param target  the buffer object target to be retrieved.
  136.  * \param error  the GL error to record if target is illegal.
  137.  * \return   pointer to the buffer object bound to \c target in the
  138.  *           specified context or \c NULL if \c target is invalid.
  139.  */
  140. static inline struct gl_buffer_object *
  141. get_buffer(struct gl_context *ctx, const char *func, GLenum target,
  142.            GLenum error)
  143. {
  144.    struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
  145.  
  146.    if (!bufObj) {
  147.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
  148.       return NULL;
  149.    }
  150.  
  151.    if (!_mesa_is_bufferobj(*bufObj)) {
  152.       _mesa_error(ctx, error, "%s(no buffer bound)", func);
  153.       return NULL;
  154.    }
  155.  
  156.    return *bufObj;
  157. }
  158.  
  159.  
  160. /**
  161.  * Convert a GLbitfield describing the mapped buffer access flags
  162.  * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
  163.  */
  164. static GLenum
  165. simplified_access_mode(struct gl_context *ctx, GLbitfield access)
  166. {
  167.    const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
  168.    if ((access & rwFlags) == rwFlags)
  169.       return GL_READ_WRITE;
  170.    if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
  171.       return GL_READ_ONLY;
  172.    if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
  173.       return GL_WRITE_ONLY;
  174.  
  175.    /* Otherwise, AccessFlags is zero (the default state).
  176.     *
  177.     * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
  178.     *
  179.     * Name           Type  Initial Value  Legal Values
  180.     * ...            ...   ...            ...
  181.     * BUFFER_ACCESS  enum  READ_WRITE     READ_ONLY, WRITE_ONLY
  182.     *                                     READ_WRITE
  183.     *
  184.     * However, table 6.8 in the GL_OES_mapbuffer extension says:
  185.     *
  186.     * Get Value         Type Get Command          Value          Description
  187.     * ---------         ---- -----------          -----          -----------
  188.     * BUFFER_ACCESS_OES Z1   GetBufferParameteriv WRITE_ONLY_OES buffer map flag
  189.     *
  190.     * The difference is because GL_OES_mapbuffer only supports mapping buffers
  191.     * write-only.
  192.     */
  193.    assert(access == 0);
  194.  
  195.    return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
  196. }
  197.  
  198.  
  199. /**
  200.  * Test if the buffer is mapped, and if so, if the mapped range overlaps the
  201.  * given range.
  202.  * The regions do not overlap if and only if the end of the given
  203.  * region is before the mapped region or the start of the given region
  204.  * is after the mapped region.
  205.  *
  206.  * \param obj     Buffer object target on which to operate.
  207.  * \param offset  Offset of the first byte of the subdata range.
  208.  * \param size    Size, in bytes, of the subdata range.
  209.  * \return   true if ranges overlap, false otherwise
  210.  *
  211.  */
  212. static bool
  213. bufferobj_range_mapped(const struct gl_buffer_object *obj,
  214.                        GLintptr offset, GLsizeiptr size)
  215. {
  216.    if (_mesa_bufferobj_mapped(obj, MAP_USER)) {
  217.       const GLintptr end = offset + size;
  218.       const GLintptr mapEnd = obj->Mappings[MAP_USER].Offset +
  219.                               obj->Mappings[MAP_USER].Length;
  220.  
  221.       if (!(end <= obj->Mappings[MAP_USER].Offset || offset >= mapEnd)) {
  222.          return true;
  223.       }
  224.    }
  225.    return false;
  226. }
  227.  
  228.  
  229. /**
  230.  * Tests the subdata range parameters and sets the GL error code for
  231.  * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
  232.  * \c glClearBufferSubData.
  233.  *
  234.  * \param ctx     GL context.
  235.  * \param bufObj  The buffer object.
  236.  * \param offset  Offset of the first byte of the subdata range.
  237.  * \param size    Size, in bytes, of the subdata range.
  238.  * \param mappedRange  If true, checks if an overlapping range is mapped.
  239.  *                     If false, checks if buffer is mapped.
  240.  * \param caller  Name of calling function for recording errors.
  241.  * \return   false if error, true otherwise
  242.  *
  243.  * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
  244.  */
  245. static bool
  246. buffer_object_subdata_range_good(struct gl_context *ctx,
  247.                                  struct gl_buffer_object *bufObj,
  248.                                  GLintptr offset, GLsizeiptr size,
  249.                                  bool mappedRange, const char *caller)
  250. {
  251.    if (size < 0) {
  252.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
  253.       return false;
  254.    }
  255.  
  256.    if (offset < 0) {
  257.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
  258.       return false;
  259.    }
  260.  
  261.    if (offset + size > bufObj->Size) {
  262.       _mesa_error(ctx, GL_INVALID_VALUE,
  263.                   "%s(offset %lu + size %lu > buffer size %lu)", caller,
  264.                   (unsigned long) offset,
  265.                   (unsigned long) size,
  266.                   (unsigned long) bufObj->Size);
  267.       return false;
  268.    }
  269.  
  270.    if (bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT)
  271.       return true;
  272.  
  273.    if (mappedRange) {
  274.       if (bufferobj_range_mapped(bufObj, offset, size)) {
  275.          _mesa_error(ctx, GL_INVALID_OPERATION,
  276.                      "%s(range is mapped without persistent bit)",
  277.                      caller);
  278.          return false;
  279.       }
  280.    }
  281.    else {
  282.       if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
  283.          _mesa_error(ctx, GL_INVALID_OPERATION,
  284.                      "%s(buffer is mapped without persistent bit)",
  285.                      caller);
  286.          return false;
  287.       }
  288.    }
  289.  
  290.    return true;
  291. }
  292.  
  293.  
  294. /**
  295.  * Test the format and type parameters and set the GL error code for
  296.  * \c glClearBufferData and \c glClearBufferSubData.
  297.  *
  298.  * \param ctx             GL context.
  299.  * \param internalformat  Format to which the data is to be converted.
  300.  * \param format          Format of the supplied data.
  301.  * \param type            Type of the supplied data.
  302.  * \param caller          Name of calling function for recording errors.
  303.  * \return   If internalformat, format and type are legal the mesa_format
  304.  *           corresponding to internalformat, otherwise MESA_FORMAT_NONE.
  305.  *
  306.  * \sa glClearBufferData and glClearBufferSubData
  307.  */
  308. static mesa_format
  309. validate_clear_buffer_format(struct gl_context *ctx,
  310.                              GLenum internalformat,
  311.                              GLenum format, GLenum type,
  312.                              const char *caller)
  313. {
  314.    mesa_format mesaFormat;
  315.    GLenum errorFormatType;
  316.  
  317.    mesaFormat = _mesa_validate_texbuffer_format(ctx, internalformat);
  318.    if (mesaFormat == MESA_FORMAT_NONE) {
  319.       _mesa_error(ctx, GL_INVALID_ENUM,
  320.                   "%s(invalid internalformat)", caller);
  321.       return MESA_FORMAT_NONE;
  322.    }
  323.  
  324.    /* NOTE: not mentioned in ARB_clear_buffer_object but according to
  325.     * EXT_texture_integer there is no conversion between integer and
  326.     * non-integer formats
  327.    */
  328.    if (_mesa_is_enum_format_signed_int(format) !=
  329.        _mesa_is_format_integer_color(mesaFormat)) {
  330.       _mesa_error(ctx, GL_INVALID_OPERATION,
  331.                   "%s(integer vs non-integer)", caller);
  332.       return MESA_FORMAT_NONE;
  333.    }
  334.  
  335.    if (!_mesa_is_color_format(format)) {
  336.       _mesa_error(ctx, GL_INVALID_ENUM,
  337.                   "%s(format is not a color format)", caller);
  338.       return MESA_FORMAT_NONE;
  339.    }
  340.  
  341.    errorFormatType = _mesa_error_check_format_and_type(ctx, format, type);
  342.    if (errorFormatType != GL_NO_ERROR) {
  343.       _mesa_error(ctx, GL_INVALID_ENUM,
  344.                   "%s(invalid format or type)", caller);
  345.       return MESA_FORMAT_NONE;
  346.    }
  347.  
  348.    return mesaFormat;
  349. }
  350.  
  351.  
  352. /**
  353.  * Convert user-specified clear value to the specified internal format.
  354.  *
  355.  * \param ctx             GL context.
  356.  * \param internalformat  Format to which the data is converted.
  357.  * \param clearValue      Points to the converted clear value.
  358.  * \param format          Format of the supplied data.
  359.  * \param type            Type of the supplied data.
  360.  * \param data            Data which is to be converted to internalformat.
  361.  * \param caller          Name of calling function for recording errors.
  362.  * \return   true if data could be converted, false otherwise.
  363.  *
  364.  * \sa glClearBufferData, glClearBufferSubData
  365.  */
  366. static bool
  367. convert_clear_buffer_data(struct gl_context *ctx,
  368.                           mesa_format internalformat,
  369.                           GLubyte *clearValue, GLenum format, GLenum type,
  370.                           const GLvoid *data, const char *caller)
  371. {
  372.    GLenum internalformatBase = _mesa_get_format_base_format(internalformat);
  373.  
  374.    if (_mesa_texstore(ctx, 1, internalformatBase, internalformat,
  375.                       0, &clearValue, 1, 1, 1,
  376.                       format, type, data, &ctx->Unpack)) {
  377.       return true;
  378.    }
  379.    else {
  380.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
  381.       return false;
  382.    }
  383. }
  384.  
  385.  
  386. /**
  387.  * Allocate and initialize a new buffer object.
  388.  *
  389.  * Default callback for the \c dd_function_table::NewBufferObject() hook.
  390.  */
  391. static struct gl_buffer_object *
  392. _mesa_new_buffer_object(struct gl_context *ctx, GLuint name)
  393. {
  394.    struct gl_buffer_object *obj;
  395.  
  396.    (void) ctx;
  397.  
  398.    obj = MALLOC_STRUCT(gl_buffer_object);
  399.    _mesa_initialize_buffer_object(ctx, obj, name);
  400.    return obj;
  401. }
  402.  
  403.  
  404. /**
  405.  * Delete a buffer object.
  406.  *
  407.  * Default callback for the \c dd_function_table::DeleteBuffer() hook.
  408.  */
  409. static void
  410. _mesa_delete_buffer_object(struct gl_context *ctx,
  411.                            struct gl_buffer_object *bufObj)
  412. {
  413.    (void) ctx;
  414.  
  415.    _mesa_align_free(bufObj->Data);
  416.  
  417.    /* assign strange values here to help w/ debugging */
  418.    bufObj->RefCount = -1000;
  419.    bufObj->Name = ~0;
  420.  
  421.    mtx_destroy(&bufObj->Mutex);
  422.    free(bufObj->Label);
  423.    free(bufObj);
  424. }
  425.  
  426.  
  427.  
  428. /**
  429.  * Set ptr to bufObj w/ reference counting.
  430.  * This is normally only called from the _mesa_reference_buffer_object() macro
  431.  * when there's a real pointer change.
  432.  */
  433. void
  434. _mesa_reference_buffer_object_(struct gl_context *ctx,
  435.                                struct gl_buffer_object **ptr,
  436.                                struct gl_buffer_object *bufObj)
  437. {
  438.    if (*ptr) {
  439.       /* Unreference the old buffer */
  440.       GLboolean deleteFlag = GL_FALSE;
  441.       struct gl_buffer_object *oldObj = *ptr;
  442.  
  443.       mtx_lock(&oldObj->Mutex);
  444.       assert(oldObj->RefCount > 0);
  445.       oldObj->RefCount--;
  446. #if 0
  447.       printf("BufferObj %p %d DECR to %d\n",
  448.              (void *) oldObj, oldObj->Name, oldObj->RefCount);
  449. #endif
  450.       deleteFlag = (oldObj->RefCount == 0);
  451.       mtx_unlock(&oldObj->Mutex);
  452.  
  453.       if (deleteFlag) {
  454.  
  455.          /* some sanity checking: don't delete a buffer still in use */
  456. #if 0
  457.          /* unfortunately, these tests are invalid during context tear-down */
  458.          assert(ctx->Array.ArrayBufferObj != bufObj);
  459.          assert(ctx->Array.VAO->IndexBufferObj != bufObj);
  460.          assert(ctx->Array.VAO->Vertex.BufferObj != bufObj);
  461. #endif
  462.  
  463.          assert(ctx->Driver.DeleteBuffer);
  464.          ctx->Driver.DeleteBuffer(ctx, oldObj);
  465.       }
  466.  
  467.       *ptr = NULL;
  468.    }
  469.    assert(!*ptr);
  470.  
  471.    if (bufObj) {
  472.       /* reference new buffer */
  473.       mtx_lock(&bufObj->Mutex);
  474.       if (bufObj->RefCount == 0) {
  475.          /* this buffer's being deleted (look just above) */
  476.          /* Not sure this can every really happen.  Warn if it does. */
  477.          _mesa_problem(NULL, "referencing deleted buffer object");
  478.          *ptr = NULL;
  479.       }
  480.       else {
  481.          bufObj->RefCount++;
  482. #if 0
  483.          printf("BufferObj %p %d INCR to %d\n",
  484.                 (void *) bufObj, bufObj->Name, bufObj->RefCount);
  485. #endif
  486.          *ptr = bufObj;
  487.       }
  488.       mtx_unlock(&bufObj->Mutex);
  489.    }
  490. }
  491.  
  492.  
  493. /**
  494.  * Initialize a buffer object to default values.
  495.  */
  496. void
  497. _mesa_initialize_buffer_object(struct gl_context *ctx,
  498.                                struct gl_buffer_object *obj,
  499.                                GLuint name)
  500. {
  501.    memset(obj, 0, sizeof(struct gl_buffer_object));
  502.    mtx_init(&obj->Mutex, mtx_plain);
  503.    obj->RefCount = 1;
  504.    obj->Name = name;
  505.    obj->Usage = GL_STATIC_DRAW_ARB;
  506. }
  507.  
  508.  
  509.  
  510. /**
  511.  * Callback called from _mesa_HashWalk()
  512.  */
  513. static void
  514. count_buffer_size(GLuint key, void *data, void *userData)
  515. {
  516.    const struct gl_buffer_object *bufObj =
  517.       (const struct gl_buffer_object *) data;
  518.    GLuint *total = (GLuint *) userData;
  519.  
  520.    *total = *total + bufObj->Size;
  521. }
  522.  
  523.  
  524. /**
  525.  * Compute total size (in bytes) of all buffer objects for the given context.
  526.  * For debugging purposes.
  527.  */
  528. GLuint
  529. _mesa_total_buffer_object_memory(struct gl_context *ctx)
  530. {
  531.    GLuint total = 0;
  532.  
  533.    _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
  534.  
  535.    return total;
  536. }
  537.  
  538.  
  539. /**
  540.  * Allocate space for and store data in a buffer object.  Any data that was
  541.  * previously stored in the buffer object is lost.  If \c data is \c NULL,
  542.  * memory will be allocated, but no copy will occur.
  543.  *
  544.  * This is the default callback for \c dd_function_table::BufferData()
  545.  * Note that all GL error checking will have been done already.
  546.  *
  547.  * \param ctx     GL context.
  548.  * \param target  Buffer object target on which to operate.
  549.  * \param size    Size, in bytes, of the new data store.
  550.  * \param data    Pointer to the data to store in the buffer object.  This
  551.  *                pointer may be \c NULL.
  552.  * \param usage   Hints about how the data will be used.
  553.  * \param bufObj  Object to be used.
  554.  *
  555.  * \return GL_TRUE for success, GL_FALSE for failure
  556.  * \sa glBufferDataARB, dd_function_table::BufferData.
  557.  */
  558. static GLboolean
  559. buffer_data_fallback(struct gl_context *ctx, GLenum target, GLsizeiptr size,
  560.                      const GLvoid *data, GLenum usage, GLenum storageFlags,
  561.                      struct gl_buffer_object *bufObj)
  562. {
  563.    void * new_data;
  564.  
  565.    (void) target;
  566.  
  567.    _mesa_align_free( bufObj->Data );
  568.  
  569.    new_data = _mesa_align_malloc( size, ctx->Const.MinMapBufferAlignment );
  570.    if (new_data) {
  571.       bufObj->Data = (GLubyte *) new_data;
  572.       bufObj->Size = size;
  573.       bufObj->Usage = usage;
  574.       bufObj->StorageFlags = storageFlags;
  575.  
  576.       if (data) {
  577.          memcpy( bufObj->Data, data, size );
  578.       }
  579.  
  580.       return GL_TRUE;
  581.    }
  582.    else {
  583.       return GL_FALSE;
  584.    }
  585. }
  586.  
  587.  
  588. /**
  589.  * Replace data in a subrange of buffer object.  If the data range
  590.  * specified by \c size + \c offset extends beyond the end of the buffer or
  591.  * if \c data is \c NULL, no copy is performed.
  592.  *
  593.  * This is the default callback for \c dd_function_table::BufferSubData()
  594.  * Note that all GL error checking will have been done already.
  595.  *
  596.  * \param ctx     GL context.
  597.  * \param offset  Offset of the first byte to be modified.
  598.  * \param size    Size, in bytes, of the data range.
  599.  * \param data    Pointer to the data to store in the buffer object.
  600.  * \param bufObj  Object to be used.
  601.  *
  602.  * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
  603.  */
  604. static void
  605. buffer_sub_data_fallback(struct gl_context *ctx, GLintptr offset,
  606.                          GLsizeiptr size, const GLvoid *data,
  607.                          struct gl_buffer_object *bufObj)
  608. {
  609.    (void) ctx;
  610.  
  611.    /* this should have been caught in _mesa_BufferSubData() */
  612.    assert(size + offset <= bufObj->Size);
  613.  
  614.    if (bufObj->Data) {
  615.       memcpy( (GLubyte *) bufObj->Data + offset, data, size );
  616.    }
  617. }
  618.  
  619.  
  620. /**
  621.  * Retrieve data from a subrange of buffer object.  If the data range
  622.  * specified by \c size + \c offset extends beyond the end of the buffer or
  623.  * if \c data is \c NULL, no copy is performed.
  624.  *
  625.  * This is the default callback for \c dd_function_table::GetBufferSubData()
  626.  * Note that all GL error checking will have been done already.
  627.  *
  628.  * \param ctx     GL context.
  629.  * \param target  Buffer object target on which to operate.
  630.  * \param offset  Offset of the first byte to be fetched.
  631.  * \param size    Size, in bytes, of the data range.
  632.  * \param data    Destination for data
  633.  * \param bufObj  Object to be used.
  634.  *
  635.  * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
  636.  */
  637. static void
  638. _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
  639.                           GLsizeiptrARB size, GLvoid * data,
  640.                           struct gl_buffer_object * bufObj )
  641. {
  642.    (void) ctx;
  643.  
  644.    if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
  645.       memcpy( data, (GLubyte *) bufObj->Data + offset, size );
  646.    }
  647. }
  648.  
  649.  
  650. /**
  651.  * Clear a subrange of the buffer object with copies of the supplied data.
  652.  * If data is NULL the buffer is filled with zeros.
  653.  *
  654.  * This is the default callback for \c dd_function_table::ClearBufferSubData()
  655.  * Note that all GL error checking will have been done already.
  656.  *
  657.  * \param ctx             GL context.
  658.  * \param offset          Offset of the first byte to be cleared.
  659.  * \param size            Size, in bytes, of the to be cleared range.
  660.  * \param clearValue      Source of the data.
  661.  * \param clearValueSize  Size, in bytes, of the supplied data.
  662.  * \param bufObj          Object to be cleared.
  663.  *
  664.  * \sa glClearBufferSubData, glClearBufferData and
  665.  * dd_function_table::ClearBufferSubData.
  666.  */
  667. void
  668. _mesa_ClearBufferSubData_sw(struct gl_context *ctx,
  669.                             GLintptr offset, GLsizeiptr size,
  670.                             const GLvoid *clearValue,
  671.                             GLsizeiptr clearValueSize,
  672.                             struct gl_buffer_object *bufObj)
  673. {
  674.    GLsizeiptr i;
  675.    GLubyte *dest;
  676.  
  677.    assert(ctx->Driver.MapBufferRange);
  678.    dest = ctx->Driver.MapBufferRange(ctx, offset, size,
  679.                                      GL_MAP_WRITE_BIT |
  680.                                      GL_MAP_INVALIDATE_RANGE_BIT,
  681.                                      bufObj, MAP_INTERNAL);
  682.  
  683.    if (!dest) {
  684.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
  685.       return;
  686.    }
  687.  
  688.    if (clearValue == NULL) {
  689.       /* Clear with zeros, per the spec */
  690.       memset(dest, 0, size);
  691.       ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
  692.       return;
  693.    }
  694.  
  695.    for (i = 0; i < size/clearValueSize; ++i) {
  696.       memcpy(dest, clearValue, clearValueSize);
  697.       dest += clearValueSize;
  698.    }
  699.  
  700.    ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_INTERNAL);
  701. }
  702.  
  703.  
  704. /**
  705.  * Default fallback for \c dd_function_table::MapBufferRange().
  706.  * Called via glMapBufferRange().
  707.  */
  708. static void *
  709. map_buffer_range_fallback(struct gl_context *ctx, GLintptr offset,
  710.                           GLsizeiptr length, GLbitfield access,
  711.                           struct gl_buffer_object *bufObj,
  712.                           gl_map_buffer_index index)
  713. {
  714.    (void) ctx;
  715.    assert(!_mesa_bufferobj_mapped(bufObj, index));
  716.    /* Just return a direct pointer to the data */
  717.    bufObj->Mappings[index].Pointer = bufObj->Data + offset;
  718.    bufObj->Mappings[index].Length = length;
  719.    bufObj->Mappings[index].Offset = offset;
  720.    bufObj->Mappings[index].AccessFlags = access;
  721.    return bufObj->Mappings[index].Pointer;
  722. }
  723.  
  724.  
  725. /**
  726.  * Default fallback for \c dd_function_table::FlushMappedBufferRange().
  727.  * Called via glFlushMappedBufferRange().
  728.  */
  729. static void
  730. flush_mapped_buffer_range_fallback(struct gl_context *ctx,
  731.                                    GLintptr offset, GLsizeiptr length,
  732.                                    struct gl_buffer_object *obj,
  733.                                    gl_map_buffer_index index)
  734. {
  735.    (void) ctx;
  736.    (void) offset;
  737.    (void) length;
  738.    (void) obj;
  739.    /* no-op */
  740. }
  741.  
  742.  
  743. /**
  744.  * Default callback for \c dd_function_table::UnmapBuffer().
  745.  *
  746.  * The input parameters will have been already tested for errors.
  747.  *
  748.  * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
  749.  */
  750. static GLboolean
  751. unmap_buffer_fallback(struct gl_context *ctx, struct gl_buffer_object *bufObj,
  752.                       gl_map_buffer_index index)
  753. {
  754.    (void) ctx;
  755.    /* XXX we might assert here that bufObj->Pointer is non-null */
  756.    bufObj->Mappings[index].Pointer = NULL;
  757.    bufObj->Mappings[index].Length = 0;
  758.    bufObj->Mappings[index].Offset = 0;
  759.    bufObj->Mappings[index].AccessFlags = 0x0;
  760.    return GL_TRUE;
  761. }
  762.  
  763.  
  764. /**
  765.  * Default fallback for \c dd_function_table::CopyBufferSubData().
  766.  * Called via glCopyBufferSubData().
  767.  */
  768. static void
  769. copy_buffer_sub_data_fallback(struct gl_context *ctx,
  770.                               struct gl_buffer_object *src,
  771.                               struct gl_buffer_object *dst,
  772.                               GLintptr readOffset, GLintptr writeOffset,
  773.                               GLsizeiptr size)
  774. {
  775.    GLubyte *srcPtr, *dstPtr;
  776.  
  777.    if (src == dst) {
  778.       srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
  779.                                                    GL_MAP_READ_BIT |
  780.                                                    GL_MAP_WRITE_BIT, src,
  781.                                                    MAP_INTERNAL);
  782.  
  783.       if (!srcPtr)
  784.          return;
  785.  
  786.       srcPtr += readOffset;
  787.       dstPtr += writeOffset;
  788.    } else {
  789.       srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
  790.                                           GL_MAP_READ_BIT, src,
  791.                                           MAP_INTERNAL);
  792.       dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
  793.                                           (GL_MAP_WRITE_BIT |
  794.                                            GL_MAP_INVALIDATE_RANGE_BIT), dst,
  795.                                           MAP_INTERNAL);
  796.    }
  797.  
  798.    /* Note: the src and dst regions will never overlap.  Trying to do so
  799.     * would generate GL_INVALID_VALUE earlier.
  800.     */
  801.    if (srcPtr && dstPtr)
  802.       memcpy(dstPtr, srcPtr, size);
  803.  
  804.    ctx->Driver.UnmapBuffer(ctx, src, MAP_INTERNAL);
  805.    if (dst != src)
  806.       ctx->Driver.UnmapBuffer(ctx, dst, MAP_INTERNAL);
  807. }
  808.  
  809.  
  810.  
  811. /**
  812.  * Initialize the state associated with buffer objects
  813.  */
  814. void
  815. _mesa_init_buffer_objects( struct gl_context *ctx )
  816. {
  817.    GLuint i;
  818.  
  819.    memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
  820.    mtx_init(&DummyBufferObject.Mutex, mtx_plain);
  821.    DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
  822.  
  823.    _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
  824.                                  ctx->Shared->NullBufferObj);
  825.  
  826.    _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
  827.                                  ctx->Shared->NullBufferObj);
  828.    _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
  829.                                  ctx->Shared->NullBufferObj);
  830.  
  831.    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
  832.                                  ctx->Shared->NullBufferObj);
  833.  
  834.    _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer,
  835.                                  ctx->Shared->NullBufferObj);
  836.  
  837.    _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer,
  838.                                  ctx->Shared->NullBufferObj);
  839.  
  840.    for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
  841.       _mesa_reference_buffer_object(ctx,
  842.                                     &ctx->UniformBufferBindings[i].BufferObject,
  843.                                     ctx->Shared->NullBufferObj);
  844.       ctx->UniformBufferBindings[i].Offset = -1;
  845.       ctx->UniformBufferBindings[i].Size = -1;
  846.    }
  847.  
  848.    for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
  849.       _mesa_reference_buffer_object(ctx,
  850.                                     &ctx->AtomicBufferBindings[i].BufferObject,
  851.                                     ctx->Shared->NullBufferObj);
  852.       ctx->AtomicBufferBindings[i].Offset = -1;
  853.       ctx->AtomicBufferBindings[i].Size = -1;
  854.    }
  855. }
  856.  
  857.  
  858. void
  859. _mesa_free_buffer_objects( struct gl_context *ctx )
  860. {
  861.    GLuint i;
  862.  
  863.    _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
  864.  
  865.    _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
  866.    _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
  867.  
  868.    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
  869.  
  870.    _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, NULL);
  871.  
  872.    _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, NULL);
  873.  
  874.    for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
  875.       _mesa_reference_buffer_object(ctx,
  876.                                     &ctx->UniformBufferBindings[i].BufferObject,
  877.                                     NULL);
  878.    }
  879.  
  880.    for (i = 0; i < MAX_COMBINED_ATOMIC_BUFFERS; i++) {
  881.       _mesa_reference_buffer_object(ctx,
  882.                                     &ctx->AtomicBufferBindings[i].BufferObject,
  883.                                     NULL);
  884.    }
  885.  
  886. }
  887.  
  888. bool
  889. _mesa_handle_bind_buffer_gen(struct gl_context *ctx,
  890.                              GLenum target,
  891.                              GLuint buffer,
  892.                              struct gl_buffer_object **buf_handle,
  893.                              const char *caller)
  894. {
  895.    struct gl_buffer_object *buf = *buf_handle;
  896.  
  897.    if (!buf && ctx->API == API_OPENGL_CORE) {
  898.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
  899.       return false;
  900.    }
  901.  
  902.    if (!buf || buf == &DummyBufferObject) {
  903.       /* If this is a new buffer object id, or one which was generated but
  904.        * never used before, allocate a buffer object now.
  905.        */
  906.       assert(ctx->Driver.NewBufferObject);
  907.       buf = ctx->Driver.NewBufferObject(ctx, buffer);
  908.       if (!buf) {
  909.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
  910.          return false;
  911.       }
  912.       _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
  913.       *buf_handle = buf;
  914.    }
  915.  
  916.    return true;
  917. }
  918.  
  919. /**
  920.  * Bind the specified target to buffer for the specified context.
  921.  * Called by glBindBuffer() and other functions.
  922.  */
  923. static void
  924. bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
  925. {
  926.    struct gl_buffer_object *oldBufObj;
  927.    struct gl_buffer_object *newBufObj = NULL;
  928.    struct gl_buffer_object **bindTarget = NULL;
  929.  
  930.    bindTarget = get_buffer_target(ctx, target);
  931.    if (!bindTarget) {
  932.       _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
  933.       return;
  934.    }
  935.  
  936.    /* Get pointer to old buffer object (to be unbound) */
  937.    oldBufObj = *bindTarget;
  938.    if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
  939.       return;   /* rebinding the same buffer object- no change */
  940.  
  941.    /*
  942.     * Get pointer to new buffer object (newBufObj)
  943.     */
  944.    if (buffer == 0) {
  945.       /* The spec says there's not a buffer object named 0, but we use
  946.        * one internally because it simplifies things.
  947.        */
  948.       newBufObj = ctx->Shared->NullBufferObj;
  949.    }
  950.    else {
  951.       /* non-default buffer object */
  952.       newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
  953.       if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
  954.                                         &newBufObj, "glBindBuffer"))
  955.          return;
  956.    }
  957.    
  958.    /* bind new buffer */
  959.    _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
  960. }
  961.  
  962.  
  963. /**
  964.  * Update the default buffer objects in the given context to reference those
  965.  * specified in the shared state and release those referencing the old
  966.  * shared state.
  967.  */
  968. void
  969. _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
  970. {
  971.    /* Bind the NullBufferObj to remove references to those
  972.     * in the shared context hash table.
  973.     */
  974.    bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
  975.    bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
  976.    bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
  977.    bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
  978. }
  979.  
  980.  
  981.  
  982. /**
  983.  * Return the gl_buffer_object for the given ID.
  984.  * Always return NULL for ID 0.
  985.  */
  986. struct gl_buffer_object *
  987. _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
  988. {
  989.    if (buffer == 0)
  990.       return NULL;
  991.    else
  992.       return (struct gl_buffer_object *)
  993.          _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
  994. }
  995.  
  996.  
  997. struct gl_buffer_object *
  998. _mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer)
  999. {
  1000.    return (struct gl_buffer_object *)
  1001.       _mesa_HashLookupLocked(ctx->Shared->BufferObjects, buffer);
  1002. }
  1003.  
  1004. /**
  1005.  * A convenience function for direct state access functions that throws
  1006.  * GL_INVALID_OPERATION if buffer is not the name of an existing
  1007.  * buffer object.
  1008.  */
  1009. struct gl_buffer_object *
  1010. _mesa_lookup_bufferobj_err(struct gl_context *ctx, GLuint buffer,
  1011.                            const char *caller)
  1012. {
  1013.    struct gl_buffer_object *bufObj;
  1014.  
  1015.    bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  1016.    if (!bufObj || bufObj == &DummyBufferObject) {
  1017.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1018.                   "%s(non-existent buffer object %u)", caller, buffer);
  1019.       return NULL;
  1020.    }
  1021.  
  1022.    return bufObj;
  1023. }
  1024.  
  1025.  
  1026. void
  1027. _mesa_begin_bufferobj_lookups(struct gl_context *ctx)
  1028. {
  1029.    _mesa_HashLockMutex(ctx->Shared->BufferObjects);
  1030. }
  1031.  
  1032.  
  1033. void
  1034. _mesa_end_bufferobj_lookups(struct gl_context *ctx)
  1035. {
  1036.    _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
  1037. }
  1038.  
  1039.  
  1040. /**
  1041.  * Look up a buffer object for a multi-bind function.
  1042.  *
  1043.  * Unlike _mesa_lookup_bufferobj(), this function also takes care
  1044.  * of generating an error if the buffer ID is not zero or the name
  1045.  * of an existing buffer object.
  1046.  *
  1047.  * If the buffer ID refers to an existing buffer object, a pointer
  1048.  * to the buffer object is returned.  If the ID is zero, a pointer
  1049.  * to the shared NullBufferObj is returned.  If the ID is not zero
  1050.  * and does not refer to a valid buffer object, this function
  1051.  * returns NULL.
  1052.  *
  1053.  * This function assumes that the caller has already locked the
  1054.  * hash table mutex by calling _mesa_begin_bufferobj_lookups().
  1055.  */
  1056. struct gl_buffer_object *
  1057. _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx,
  1058.                                   const GLuint *buffers,
  1059.                                   GLuint index, const char *caller)
  1060. {
  1061.    struct gl_buffer_object *bufObj;
  1062.  
  1063.    if (buffers[index] != 0) {
  1064.       bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]);
  1065.  
  1066.       /* The multi-bind functions don't create the buffer objects
  1067.          when they don't exist. */
  1068.       if (bufObj == &DummyBufferObject)
  1069.          bufObj = NULL;
  1070.    } else
  1071.       bufObj = ctx->Shared->NullBufferObj;
  1072.  
  1073.    if (!bufObj) {
  1074.       /* The ARB_multi_bind spec says:
  1075.        *
  1076.        *    "An INVALID_OPERATION error is generated if any value
  1077.        *     in <buffers> is not zero or the name of an existing
  1078.        *     buffer object (per binding)."
  1079.        */
  1080.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1081.                   "%s(buffers[%u]=%u is not zero or the name "
  1082.                   "of an existing buffer object)",
  1083.                   caller, index, buffers[index]);
  1084.    }
  1085.  
  1086.    return bufObj;
  1087. }
  1088.  
  1089.  
  1090. /**
  1091.  * If *ptr points to obj, set ptr = the Null/default buffer object.
  1092.  * This is a helper for buffer object deletion.
  1093.  * The GL spec says that deleting a buffer object causes it to get
  1094.  * unbound from all arrays in the current context.
  1095.  */
  1096. static void
  1097. unbind(struct gl_context *ctx,
  1098.        struct gl_buffer_object **ptr,
  1099.        struct gl_buffer_object *obj)
  1100. {
  1101.    if (*ptr == obj) {
  1102.       _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
  1103.    }
  1104. }
  1105.  
  1106.  
  1107. /**
  1108.  * Plug default/fallback buffer object functions into the device
  1109.  * driver hooks.
  1110.  */
  1111. void
  1112. _mesa_init_buffer_object_functions(struct dd_function_table *driver)
  1113. {
  1114.    /* GL_ARB_vertex/pixel_buffer_object */
  1115.    driver->NewBufferObject = _mesa_new_buffer_object;
  1116.    driver->DeleteBuffer = _mesa_delete_buffer_object;
  1117.    driver->BufferData = buffer_data_fallback;
  1118.    driver->BufferSubData = buffer_sub_data_fallback;
  1119.    driver->GetBufferSubData = _mesa_buffer_get_subdata;
  1120.    driver->UnmapBuffer = unmap_buffer_fallback;
  1121.  
  1122.    /* GL_ARB_clear_buffer_object */
  1123.    driver->ClearBufferSubData = _mesa_ClearBufferSubData_sw;
  1124.  
  1125.    /* GL_ARB_map_buffer_range */
  1126.    driver->MapBufferRange = map_buffer_range_fallback;
  1127.    driver->FlushMappedBufferRange = flush_mapped_buffer_range_fallback;
  1128.  
  1129.    /* GL_ARB_copy_buffer */
  1130.    driver->CopyBufferSubData = copy_buffer_sub_data_fallback;
  1131. }
  1132.  
  1133.  
  1134. void
  1135. _mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
  1136.                                 struct gl_buffer_object *bufObj)
  1137. {
  1138.    int i;
  1139.  
  1140.    for (i = 0; i < MAP_COUNT; i++) {
  1141.       if (_mesa_bufferobj_mapped(bufObj, i)) {
  1142.          ctx->Driver.UnmapBuffer(ctx, bufObj, i);
  1143.          assert(bufObj->Mappings[i].Pointer == NULL);
  1144.          bufObj->Mappings[i].AccessFlags = 0;
  1145.       }
  1146.    }
  1147. }
  1148.  
  1149.  
  1150. /**********************************************************************/
  1151. /* API Functions                                                      */
  1152. /**********************************************************************/
  1153.  
  1154. void GLAPIENTRY
  1155. _mesa_BindBuffer(GLenum target, GLuint buffer)
  1156. {
  1157.    GET_CURRENT_CONTEXT(ctx);
  1158.  
  1159.    if (MESA_VERBOSE & VERBOSE_API)
  1160.       _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
  1161.                   _mesa_lookup_enum_by_nr(target), buffer);
  1162.  
  1163.    bind_buffer_object(ctx, target, buffer);
  1164. }
  1165.  
  1166.  
  1167. /**
  1168.  * Delete a set of buffer objects.
  1169.  *
  1170.  * \param n      Number of buffer objects to delete.
  1171.  * \param ids    Array of \c n buffer object IDs.
  1172.  */
  1173. void GLAPIENTRY
  1174. _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
  1175. {
  1176.    GET_CURRENT_CONTEXT(ctx);
  1177.    GLsizei i;
  1178.    FLUSH_VERTICES(ctx, 0);
  1179.  
  1180.    if (n < 0) {
  1181.       _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
  1182.       return;
  1183.    }
  1184.  
  1185.    mtx_lock(&ctx->Shared->Mutex);
  1186.  
  1187.    for (i = 0; i < n; i++) {
  1188.       struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
  1189.       if (bufObj) {
  1190.          struct gl_vertex_array_object *vao = ctx->Array.VAO;
  1191.          GLuint j;
  1192.  
  1193.          assert(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
  1194.  
  1195.          _mesa_buffer_unmap_all_mappings(ctx, bufObj);
  1196.  
  1197.          /* unbind any vertex pointers bound to this buffer */
  1198.          for (j = 0; j < ARRAY_SIZE(vao->VertexBinding); j++) {
  1199.             unbind(ctx, &vao->VertexBinding[j].BufferObj, bufObj);
  1200.          }
  1201.  
  1202.          if (ctx->Array.ArrayBufferObj == bufObj) {
  1203.             _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
  1204.          }
  1205.          if (vao->IndexBufferObj == bufObj) {
  1206.             _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
  1207.          }
  1208.  
  1209.          /* unbind ARB_draw_indirect binding point */
  1210.          if (ctx->DrawIndirectBuffer == bufObj) {
  1211.             _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 );
  1212.          }
  1213.  
  1214.          /* unbind ARB_copy_buffer binding points */
  1215.          if (ctx->CopyReadBuffer == bufObj) {
  1216.             _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
  1217.          }
  1218.          if (ctx->CopyWriteBuffer == bufObj) {
  1219.             _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
  1220.          }
  1221.  
  1222.          /* unbind transform feedback binding points */
  1223.          if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
  1224.             _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
  1225.          }
  1226.          for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
  1227.             if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
  1228.                _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
  1229.             }
  1230.          }
  1231.  
  1232.          /* unbind UBO binding points */
  1233.          for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
  1234.             if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
  1235.                _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
  1236.             }
  1237.          }
  1238.  
  1239.          if (ctx->UniformBuffer == bufObj) {
  1240.             _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
  1241.          }
  1242.  
  1243.          /* unbind Atomci Buffer binding points */
  1244.          for (j = 0; j < ctx->Const.MaxAtomicBufferBindings; j++) {
  1245.             if (ctx->AtomicBufferBindings[j].BufferObject == bufObj) {
  1246.                _mesa_BindBufferBase( GL_ATOMIC_COUNTER_BUFFER, j, 0 );
  1247.             }
  1248.          }
  1249.  
  1250.          if (ctx->AtomicBuffer == bufObj) {
  1251.             _mesa_BindBuffer( GL_ATOMIC_COUNTER_BUFFER, 0 );
  1252.          }
  1253.  
  1254.          /* unbind any pixel pack/unpack pointers bound to this buffer */
  1255.          if (ctx->Pack.BufferObj == bufObj) {
  1256.             _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
  1257.          }
  1258.          if (ctx->Unpack.BufferObj == bufObj) {
  1259.             _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
  1260.          }
  1261.  
  1262.          if (ctx->Texture.BufferObject == bufObj) {
  1263.             _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
  1264.          }
  1265.  
  1266.          if (ctx->ExternalVirtualMemoryBuffer == bufObj) {
  1267.             _mesa_BindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0);
  1268.          }
  1269.  
  1270.          /* The ID is immediately freed for re-use */
  1271.          _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
  1272.          /* Make sure we do not run into the classic ABA problem on bind.
  1273.           * We don't want to allow re-binding a buffer object that's been
  1274.           * "deleted" by glDeleteBuffers().
  1275.           *
  1276.           * The explicit rebinding to the default object in the current context
  1277.           * prevents the above in the current context, but another context
  1278.           * sharing the same objects might suffer from this problem.
  1279.           * The alternative would be to do the hash lookup in any case on bind
  1280.           * which would introduce more runtime overhead than this.
  1281.           */
  1282.          bufObj->DeletePending = GL_TRUE;
  1283.          _mesa_reference_buffer_object(ctx, &bufObj, NULL);
  1284.       }
  1285.    }
  1286.  
  1287.    mtx_unlock(&ctx->Shared->Mutex);
  1288. }
  1289.  
  1290.  
  1291. /**
  1292.  * This is the implementation for glGenBuffers and glCreateBuffers. It is not
  1293.  * exposed to the rest of Mesa to encourage the use of nameless buffers in
  1294.  * driver internals.
  1295.  */
  1296. static void
  1297. create_buffers(GLsizei n, GLuint *buffers, bool dsa)
  1298. {
  1299.    GET_CURRENT_CONTEXT(ctx);
  1300.    GLuint first;
  1301.    GLint i;
  1302.    struct gl_buffer_object *buf;
  1303.  
  1304.    const char *func = dsa ? "glCreateBuffers" : "glGenBuffers";
  1305.  
  1306.    if (dsa && !ctx->Extensions.ARB_direct_state_access) {
  1307.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1308.                   "%s(GL_ARB_direct_state_access is not supported)", func);
  1309.       return;
  1310.    }
  1311.  
  1312.    if (MESA_VERBOSE & VERBOSE_API)
  1313.       _mesa_debug(ctx, "%s(%d)\n", func, n);
  1314.  
  1315.    if (n < 0) {
  1316.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(n %d < 0)", func, n);
  1317.       return;
  1318.    }
  1319.  
  1320.    if (!buffers) {
  1321.       return;
  1322.    }
  1323.  
  1324.    /*
  1325.     * This must be atomic (generation and allocation of buffer object IDs)
  1326.     */
  1327.    mtx_lock(&ctx->Shared->Mutex);
  1328.  
  1329.    first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
  1330.  
  1331.    /* Insert the ID and pointer into the hash table. If non-DSA, insert a
  1332.     * DummyBufferObject.  Otherwise, create a new buffer object and insert
  1333.     * it.
  1334.     */
  1335.    for (i = 0; i < n; i++) {
  1336.       buffers[i] = first + i;
  1337.       if (dsa) {
  1338.          assert(ctx->Driver.NewBufferObject);
  1339.          buf = ctx->Driver.NewBufferObject(ctx, buffers[i]);
  1340.          if (!buf) {
  1341.             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
  1342.             mtx_unlock(&ctx->Shared->Mutex);
  1343.             return;
  1344.          }
  1345.       }
  1346.       else
  1347.          buf = &DummyBufferObject;
  1348.  
  1349.       _mesa_HashInsert(ctx->Shared->BufferObjects, buffers[i], buf);
  1350.    }
  1351.  
  1352.    mtx_unlock(&ctx->Shared->Mutex);
  1353. }
  1354.  
  1355. /**
  1356.  * Generate a set of unique buffer object IDs and store them in \c buffers.
  1357.  *
  1358.  * \param n        Number of IDs to generate.
  1359.  * \param buffers  Array of \c n locations to store the IDs.
  1360.  */
  1361. void GLAPIENTRY
  1362. _mesa_GenBuffers(GLsizei n, GLuint *buffers)
  1363. {
  1364.    create_buffers(n, buffers, false);
  1365. }
  1366.  
  1367. /**
  1368.  * Create a set of buffer objects and store their unique IDs in \c buffers.
  1369.  *
  1370.  * \param n        Number of IDs to generate.
  1371.  * \param buffers  Array of \c n locations to store the IDs.
  1372.  */
  1373. void GLAPIENTRY
  1374. _mesa_CreateBuffers(GLsizei n, GLuint *buffers)
  1375. {
  1376.    create_buffers(n, buffers, true);
  1377. }
  1378.  
  1379.  
  1380. /**
  1381.  * Determine if ID is the name of a buffer object.
  1382.  *
  1383.  * \param id  ID of the potential buffer object.
  1384.  * \return  \c GL_TRUE if \c id is the name of a buffer object,
  1385.  *          \c GL_FALSE otherwise.
  1386.  */
  1387. GLboolean GLAPIENTRY
  1388. _mesa_IsBuffer(GLuint id)
  1389. {
  1390.    struct gl_buffer_object *bufObj;
  1391.    GET_CURRENT_CONTEXT(ctx);
  1392.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
  1393.  
  1394.    mtx_lock(&ctx->Shared->Mutex);
  1395.    bufObj = _mesa_lookup_bufferobj(ctx, id);
  1396.    mtx_unlock(&ctx->Shared->Mutex);
  1397.  
  1398.    return bufObj && bufObj != &DummyBufferObject;
  1399. }
  1400.  
  1401.  
  1402. void
  1403. _mesa_buffer_storage(struct gl_context *ctx, struct gl_buffer_object *bufObj,
  1404.                      GLenum target, GLsizeiptr size, const GLvoid *data,
  1405.                      GLbitfield flags, const char *func)
  1406. {
  1407.    if (size <= 0) {
  1408.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size <= 0)", func);
  1409.       return;
  1410.    }
  1411.  
  1412.    if (flags & ~(GL_MAP_READ_BIT |
  1413.                  GL_MAP_WRITE_BIT |
  1414.                  GL_MAP_PERSISTENT_BIT |
  1415.                  GL_MAP_COHERENT_BIT |
  1416.                  GL_DYNAMIC_STORAGE_BIT |
  1417.                  GL_CLIENT_STORAGE_BIT)) {
  1418.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid flag bits set)", func);
  1419.       return;
  1420.    }
  1421.  
  1422.    if (flags & GL_MAP_PERSISTENT_BIT &&
  1423.        !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
  1424.       _mesa_error(ctx, GL_INVALID_VALUE,
  1425.                   "%s(PERSISTENT and flags!=READ/WRITE)", func);
  1426.       return;
  1427.    }
  1428.  
  1429.    if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
  1430.       _mesa_error(ctx, GL_INVALID_VALUE,
  1431.                   "%s(COHERENT and flags!=PERSISTENT)", func);
  1432.       return;
  1433.    }
  1434.  
  1435.    if (bufObj->Immutable) {
  1436.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
  1437.       return;
  1438.    }
  1439.  
  1440.    /* Unmap the existing buffer.  We'll replace it now.  Not an error. */
  1441.    _mesa_buffer_unmap_all_mappings(ctx, bufObj);
  1442.  
  1443.    FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
  1444.  
  1445.    bufObj->Written = GL_TRUE;
  1446.    bufObj->Immutable = GL_TRUE;
  1447.  
  1448.    assert(ctx->Driver.BufferData);
  1449.    if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
  1450.                                flags, bufObj)) {
  1451.       if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
  1452.          /* Even though the interaction between AMD_pinned_memory and
  1453.           * glBufferStorage is not described in the spec, Graham Sellers
  1454.           * said that it should behave the same as glBufferData.
  1455.           */
  1456.          _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
  1457.       }
  1458.       else {
  1459.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
  1460.       }
  1461.    }
  1462. }
  1463.  
  1464. void GLAPIENTRY
  1465. _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
  1466.                     GLbitfield flags)
  1467. {
  1468.    GET_CURRENT_CONTEXT(ctx);
  1469.    struct gl_buffer_object *bufObj;
  1470.  
  1471.    bufObj = get_buffer(ctx, "glBufferStorage", target, GL_INVALID_OPERATION);
  1472.    if (!bufObj)
  1473.       return;
  1474.  
  1475.    _mesa_buffer_storage(ctx, bufObj, target, size, data, flags,
  1476.                         "glBufferStorage");
  1477. }
  1478.  
  1479. void GLAPIENTRY
  1480. _mesa_NamedBufferStorage(GLuint buffer, GLsizeiptr size, const GLvoid *data,
  1481.                          GLbitfield flags)
  1482. {
  1483.    GET_CURRENT_CONTEXT(ctx);
  1484.    struct gl_buffer_object *bufObj;
  1485.  
  1486.    if (!ctx->Extensions.ARB_direct_state_access) {
  1487.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1488.                   "glNamedBufferStorage(GL_ARB_direct_state_access "
  1489.                   "is not supported)");
  1490.       return;
  1491.    }
  1492.  
  1493.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferStorage");
  1494.    if (!bufObj)
  1495.       return;
  1496.  
  1497.    /*
  1498.     * In direct state access, buffer objects have an unspecified target since
  1499.     * they are not required to be bound.
  1500.     */
  1501.    _mesa_buffer_storage(ctx, bufObj, GL_NONE, size, data, flags,
  1502.                         "glNamedBufferStorage");
  1503. }
  1504.  
  1505.  
  1506. void
  1507. _mesa_buffer_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
  1508.                   GLenum target, GLsizeiptr size, const GLvoid *data,
  1509.                   GLenum usage, const char *func)
  1510. {
  1511.    bool valid_usage;
  1512.  
  1513.    if (MESA_VERBOSE & VERBOSE_API)
  1514.       _mesa_debug(ctx, "%s(%s, %ld, %p, %s)\n",
  1515.                   func,
  1516.                   _mesa_lookup_enum_by_nr(target),
  1517.                   (long int) size, data,
  1518.                   _mesa_lookup_enum_by_nr(usage));
  1519.  
  1520.    if (size < 0) {
  1521.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", func);
  1522.       return;
  1523.    }
  1524.  
  1525.    switch (usage) {
  1526.    case GL_STREAM_DRAW_ARB:
  1527.       valid_usage = (ctx->API != API_OPENGLES);
  1528.       break;
  1529.  
  1530.    case GL_STATIC_DRAW_ARB:
  1531.    case GL_DYNAMIC_DRAW_ARB:
  1532.       valid_usage = true;
  1533.       break;
  1534.  
  1535.    case GL_STREAM_READ_ARB:
  1536.    case GL_STREAM_COPY_ARB:
  1537.    case GL_STATIC_READ_ARB:
  1538.    case GL_STATIC_COPY_ARB:
  1539.    case GL_DYNAMIC_READ_ARB:
  1540.    case GL_DYNAMIC_COPY_ARB:
  1541.       valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
  1542.       break;
  1543.  
  1544.    default:
  1545.       valid_usage = false;
  1546.       break;
  1547.    }
  1548.  
  1549.    if (!valid_usage) {
  1550.       _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid usage: %s)", func,
  1551.                   _mesa_lookup_enum_by_nr(usage));
  1552.       return;
  1553.    }
  1554.  
  1555.    if (bufObj->Immutable) {
  1556.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(immutable)", func);
  1557.       return;
  1558.    }
  1559.  
  1560.    /* Unmap the existing buffer.  We'll replace it now.  Not an error. */
  1561.    _mesa_buffer_unmap_all_mappings(ctx, bufObj);
  1562.  
  1563.    FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
  1564.  
  1565.    bufObj->Written = GL_TRUE;
  1566.  
  1567. #ifdef VBO_DEBUG
  1568.    printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
  1569.                 bufObj->Name, size, data, usage);
  1570. #endif
  1571.  
  1572. #ifdef BOUNDS_CHECK
  1573.    size += 100;
  1574. #endif
  1575.  
  1576.    assert(ctx->Driver.BufferData);
  1577.    if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
  1578.                                GL_MAP_READ_BIT |
  1579.                                GL_MAP_WRITE_BIT |
  1580.                                GL_DYNAMIC_STORAGE_BIT,
  1581.                                bufObj)) {
  1582.       if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {
  1583.          /* From GL_AMD_pinned_memory:
  1584.           *
  1585.           *   INVALID_OPERATION is generated by BufferData if <target> is
  1586.           *   EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, and the store cannot be
  1587.           *   mapped to the GPU address space.
  1588.           */
  1589.          _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
  1590.       }
  1591.       else {
  1592.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
  1593.       }
  1594.    }
  1595. }
  1596.  
  1597. void GLAPIENTRY
  1598. _mesa_BufferData(GLenum target, GLsizeiptr size,
  1599.                  const GLvoid *data, GLenum usage)
  1600. {
  1601.    GET_CURRENT_CONTEXT(ctx);
  1602.    struct gl_buffer_object *bufObj;
  1603.  
  1604.    bufObj = get_buffer(ctx, "glBufferData", target, GL_INVALID_OPERATION);
  1605.    if (!bufObj)
  1606.       return;
  1607.  
  1608.    _mesa_buffer_data(ctx, bufObj, target, size, data, usage,
  1609.                      "glBufferData");
  1610. }
  1611.  
  1612. void GLAPIENTRY
  1613. _mesa_NamedBufferData(GLuint buffer, GLsizeiptr size, const GLvoid *data,
  1614.                       GLenum usage)
  1615. {
  1616.    GET_CURRENT_CONTEXT(ctx);
  1617.    struct gl_buffer_object *bufObj;
  1618.  
  1619.    if (!ctx->Extensions.ARB_direct_state_access) {
  1620.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1621.                   "glNamedBufferData(GL_ARB_direct_state_access "
  1622.                   "is not supported)");
  1623.       return;
  1624.    }
  1625.  
  1626.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferData");
  1627.    if (!bufObj)
  1628.       return;
  1629.  
  1630.    /* In direct state access, buffer objects have an unspecified target since
  1631.     * they are not required to be bound.
  1632.     */
  1633.    _mesa_buffer_data(ctx, bufObj, GL_NONE, size, data, usage,
  1634.                      "glNamedBufferData");
  1635. }
  1636.  
  1637.  
  1638. /**
  1639.  * Implementation for glBufferSubData and glNamedBufferSubData.
  1640.  *
  1641.  * \param ctx     GL context.
  1642.  * \param bufObj  The buffer object.
  1643.  * \param offset  Offset of the first byte of the subdata range.
  1644.  * \param size    Size, in bytes, of the subdata range.
  1645.  * \param data    The data store.
  1646.  * \param func  Name of calling function for recording errors.
  1647.  *
  1648.  */
  1649. void
  1650. _mesa_buffer_sub_data(struct gl_context *ctx, struct gl_buffer_object *bufObj,
  1651.                       GLintptr offset, GLsizeiptr size, const GLvoid *data,
  1652.                       const char *func)
  1653. {
  1654.    if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
  1655.                                          false, func)) {
  1656.       /* error already recorded */
  1657.       return;
  1658.    }
  1659.  
  1660.    if (bufObj->Immutable &&
  1661.        !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
  1662.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
  1663.       return;
  1664.    }
  1665.  
  1666.    if (size == 0)
  1667.       return;
  1668.  
  1669.    bufObj->Written = GL_TRUE;
  1670.  
  1671.    assert(ctx->Driver.BufferSubData);
  1672.    ctx->Driver.BufferSubData(ctx, offset, size, data, bufObj);
  1673. }
  1674.  
  1675. void GLAPIENTRY
  1676. _mesa_BufferSubData(GLenum target, GLintptr offset,
  1677.                     GLsizeiptr size, const GLvoid *data)
  1678. {
  1679.    GET_CURRENT_CONTEXT(ctx);
  1680.    struct gl_buffer_object *bufObj;
  1681.  
  1682.    bufObj = get_buffer(ctx, "glBufferSubData", target, GL_INVALID_OPERATION);
  1683.    if (!bufObj)
  1684.       return;
  1685.  
  1686.    _mesa_buffer_sub_data(ctx, bufObj, offset, size, data, "glBufferSubData");
  1687. }
  1688.  
  1689. void GLAPIENTRY
  1690. _mesa_NamedBufferSubData(GLuint buffer, GLintptr offset,
  1691.                          GLsizeiptr size, const GLvoid *data)
  1692. {
  1693.    GET_CURRENT_CONTEXT(ctx);
  1694.    struct gl_buffer_object *bufObj;
  1695.  
  1696.    if (!ctx->Extensions.ARB_direct_state_access) {
  1697.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1698.                   "glNamedBufferSubData(GL_ARB_direct_state_access "
  1699.                   "is not supported)");
  1700.       return;
  1701.    }
  1702.  
  1703.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glNamedBufferSubData");
  1704.    if (!bufObj)
  1705.       return;
  1706.  
  1707.    _mesa_buffer_sub_data(ctx, bufObj, offset, size, data,
  1708.                          "glNamedBufferSubData");
  1709. }
  1710.  
  1711.  
  1712. void GLAPIENTRY
  1713. _mesa_GetBufferSubData(GLenum target, GLintptr offset,
  1714.                        GLsizeiptr size, GLvoid *data)
  1715. {
  1716.    GET_CURRENT_CONTEXT(ctx);
  1717.    struct gl_buffer_object *bufObj;
  1718.  
  1719.    bufObj = get_buffer(ctx, "glGetBufferSubData", target,
  1720.                        GL_INVALID_OPERATION);
  1721.    if (!bufObj)
  1722.       return;
  1723.  
  1724.    if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
  1725.                                          "glGetBufferSubData")) {
  1726.       return;
  1727.    }
  1728.  
  1729.    assert(ctx->Driver.GetBufferSubData);
  1730.    ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
  1731. }
  1732.  
  1733. void GLAPIENTRY
  1734. _mesa_GetNamedBufferSubData(GLuint buffer, GLintptr offset,
  1735.                             GLsizeiptr size, GLvoid *data)
  1736. {
  1737.    GET_CURRENT_CONTEXT(ctx);
  1738.    struct gl_buffer_object *bufObj;
  1739.  
  1740.    if (!ctx->Extensions.ARB_direct_state_access) {
  1741.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1742.                   "glGetNamedBufferSubData(GL_ARB_direct_state_access "
  1743.                   "is not supported)");
  1744.       return;
  1745.    }
  1746.  
  1747.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
  1748.                                        "glGetNamedBufferSubData");
  1749.    if (!bufObj)
  1750.       return;
  1751.  
  1752.    if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size, false,
  1753.                                          "glGetNamedBufferSubData")) {
  1754.       return;
  1755.    }
  1756.  
  1757.    assert(ctx->Driver.GetBufferSubData);
  1758.    ctx->Driver.GetBufferSubData(ctx, offset, size, data, bufObj);
  1759. }
  1760.  
  1761.  
  1762. /**
  1763.  * \param subdata   true if caller is *SubData, false if *Data
  1764.  */
  1765. void
  1766. _mesa_clear_buffer_sub_data(struct gl_context *ctx,
  1767.                             struct gl_buffer_object *bufObj,
  1768.                             GLenum internalformat,
  1769.                             GLintptr offset, GLsizeiptr size,
  1770.                             GLenum format, GLenum type,
  1771.                             const GLvoid *data,
  1772.                             const char *func, bool subdata)
  1773. {
  1774.    mesa_format mesaFormat;
  1775.    GLubyte clearValue[MAX_PIXEL_BYTES];
  1776.    GLsizeiptr clearValueSize;
  1777.  
  1778.    /* This checks for disallowed mappings. */
  1779.    if (!buffer_object_subdata_range_good(ctx, bufObj, offset, size,
  1780.                                          subdata, func)) {
  1781.       return;
  1782.    }
  1783.  
  1784.    mesaFormat = validate_clear_buffer_format(ctx, internalformat,
  1785.                                              format, type, func);
  1786.  
  1787.    if (mesaFormat == MESA_FORMAT_NONE) {
  1788.       return;
  1789.    }
  1790.  
  1791.    clearValueSize = _mesa_get_format_bytes(mesaFormat);
  1792.    if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
  1793.       _mesa_error(ctx, GL_INVALID_VALUE,
  1794.                   "%s(offset or size is not a multiple of "
  1795.                   "internalformat size)", func);
  1796.       return;
  1797.    }
  1798.  
  1799.    if (data == NULL) {
  1800.       /* clear to zeros, per the spec */
  1801.       if (size > 0) {
  1802.          ctx->Driver.ClearBufferSubData(ctx, offset, size,
  1803.                                         NULL, clearValueSize, bufObj);
  1804.       }
  1805.       return;
  1806.    }
  1807.  
  1808.    if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
  1809.                                   format, type, data, func)) {
  1810.       return;
  1811.    }
  1812.  
  1813.    if (size > 0) {
  1814.       ctx->Driver.ClearBufferSubData(ctx, offset, size,
  1815.                                      clearValue, clearValueSize, bufObj);
  1816.    }
  1817. }
  1818.  
  1819. void GLAPIENTRY
  1820. _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
  1821.                       GLenum type, const GLvoid *data)
  1822. {
  1823.    GET_CURRENT_CONTEXT(ctx);
  1824.    struct gl_buffer_object *bufObj;
  1825.  
  1826.    bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
  1827.    if (!bufObj)
  1828.       return;
  1829.  
  1830.    _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
  1831.                                format, type, data,
  1832.                                "glClearBufferData", false);
  1833. }
  1834.  
  1835. void GLAPIENTRY
  1836. _mesa_ClearNamedBufferData(GLuint buffer, GLenum internalformat,
  1837.                            GLenum format, GLenum type, const GLvoid *data)
  1838. {
  1839.    GET_CURRENT_CONTEXT(ctx);
  1840.    struct gl_buffer_object *bufObj;
  1841.  
  1842.    if (!ctx->Extensions.ARB_direct_state_access) {
  1843.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1844.                   "glClearNamedBufferData(GL_ARB_direct_state_access "
  1845.                   "is not supported)");
  1846.       return;
  1847.    }
  1848.  
  1849.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glClearNamedBufferData");
  1850.    if (!bufObj)
  1851.       return;
  1852.  
  1853.    _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, 0, bufObj->Size,
  1854.                                format, type, data,
  1855.                                "glClearNamedBufferData", false);
  1856. }
  1857.  
  1858.  
  1859. void GLAPIENTRY
  1860. _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
  1861.                          GLintptr offset, GLsizeiptr size,
  1862.                          GLenum format, GLenum type,
  1863.                          const GLvoid *data)
  1864. {
  1865.    GET_CURRENT_CONTEXT(ctx);
  1866.    struct gl_buffer_object *bufObj;
  1867.  
  1868.    bufObj = get_buffer(ctx, "glClearBufferSubData", target, GL_INVALID_VALUE);
  1869.    if (!bufObj)
  1870.       return;
  1871.  
  1872.    _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size,
  1873.                                format, type, data,
  1874.                                "glClearBufferSubData", true);
  1875. }
  1876.  
  1877. void GLAPIENTRY
  1878. _mesa_ClearNamedBufferSubData(GLuint buffer, GLenum internalformat,
  1879.                               GLintptr offset, GLsizeiptr size,
  1880.                               GLenum format, GLenum type,
  1881.                               const GLvoid *data)
  1882. {
  1883.    GET_CURRENT_CONTEXT(ctx);
  1884.    struct gl_buffer_object *bufObj;
  1885.  
  1886.    if (!ctx->Extensions.ARB_direct_state_access) {
  1887.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1888.                   "glClearNamedBufferSubData(GL_ARB_direct_state_access "
  1889.                   "is not supported)");
  1890.       return;
  1891.    }
  1892.  
  1893.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
  1894.                                        "glClearNamedBufferSubData");
  1895.    if (!bufObj)
  1896.       return;
  1897.  
  1898.    _mesa_clear_buffer_sub_data(ctx, bufObj, internalformat, offset, size,
  1899.                                format, type, data,
  1900.                                "glClearNamedBufferSubData", true);
  1901. }
  1902.  
  1903.  
  1904. GLboolean
  1905. _mesa_unmap_buffer(struct gl_context *ctx, struct gl_buffer_object *bufObj,
  1906.                    const char *func)
  1907. {
  1908.    GLboolean status = GL_TRUE;
  1909.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
  1910.  
  1911.    if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
  1912.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1913.                   "%s(buffer is not mapped)", func);
  1914.       return GL_FALSE;
  1915.    }
  1916.  
  1917. #ifdef BOUNDS_CHECK
  1918.    if (bufObj->Access != GL_READ_ONLY_ARB) {
  1919.       GLubyte *buf = (GLubyte *) bufObj->Pointer;
  1920.       GLuint i;
  1921.       /* check that last 100 bytes are still = magic value */
  1922.       for (i = 0; i < 100; i++) {
  1923.          GLuint pos = bufObj->Size - i - 1;
  1924.          if (buf[pos] != 123) {
  1925.             _mesa_warning(ctx, "Out of bounds buffer object write detected"
  1926.                           " at position %d (value = %u)\n",
  1927.                           pos, buf[pos]);
  1928.          }
  1929.       }
  1930.    }
  1931. #endif
  1932.  
  1933. #ifdef VBO_DEBUG
  1934.    if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
  1935.       GLuint i, unchanged = 0;
  1936.       GLubyte *b = (GLubyte *) bufObj->Pointer;
  1937.       GLint pos = -1;
  1938.       /* check which bytes changed */
  1939.       for (i = 0; i < bufObj->Size - 1; i++) {
  1940.          if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
  1941.             unchanged++;
  1942.             if (pos == -1)
  1943.                pos = i;
  1944.          }
  1945.       }
  1946.       if (unchanged) {
  1947.          printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
  1948.                       bufObj->Name, unchanged, bufObj->Size, pos);
  1949.       }
  1950.    }
  1951. #endif
  1952.  
  1953.    status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
  1954.    bufObj->Mappings[MAP_USER].AccessFlags = 0;
  1955.    assert(bufObj->Mappings[MAP_USER].Pointer == NULL);
  1956.    assert(bufObj->Mappings[MAP_USER].Offset == 0);
  1957.    assert(bufObj->Mappings[MAP_USER].Length == 0);
  1958.  
  1959.    return status;
  1960. }
  1961.  
  1962. GLboolean GLAPIENTRY
  1963. _mesa_UnmapBuffer(GLenum target)
  1964. {
  1965.    GET_CURRENT_CONTEXT(ctx);
  1966.    struct gl_buffer_object *bufObj;
  1967.  
  1968.    bufObj = get_buffer(ctx, "glUnmapBuffer", target, GL_INVALID_OPERATION);
  1969.    if (!bufObj)
  1970.       return GL_FALSE;
  1971.  
  1972.    return _mesa_unmap_buffer(ctx, bufObj, "glUnmapBuffer");
  1973. }
  1974.  
  1975. GLboolean GLAPIENTRY
  1976. _mesa_UnmapNamedBuffer(GLuint buffer)
  1977. {
  1978.    GET_CURRENT_CONTEXT(ctx);
  1979.    struct gl_buffer_object *bufObj;
  1980.  
  1981.    if (!ctx->Extensions.ARB_direct_state_access) {
  1982.       _mesa_error(ctx, GL_INVALID_OPERATION,
  1983.                   "glUnmapNamedBuffer(GL_ARB_direct_state_access "
  1984.                   "is not supported)");
  1985.       return GL_FALSE;
  1986.    }
  1987.  
  1988.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glUnmapNamedBuffer");
  1989.    if (!bufObj)
  1990.       return GL_FALSE;
  1991.  
  1992.    return _mesa_unmap_buffer(ctx, bufObj, "glUnmapNamedBuffer");
  1993. }
  1994.  
  1995.  
  1996. static bool
  1997. get_buffer_parameter(struct gl_context *ctx,
  1998.                      struct gl_buffer_object *bufObj, GLenum pname,
  1999.                      GLint64 *params, const char *func)
  2000. {
  2001.    switch (pname) {
  2002.    case GL_BUFFER_SIZE_ARB:
  2003.       *params = bufObj->Size;
  2004.       break;
  2005.    case GL_BUFFER_USAGE_ARB:
  2006.       *params = bufObj->Usage;
  2007.       break;
  2008.    case GL_BUFFER_ACCESS_ARB:
  2009.       *params = simplified_access_mode(ctx,
  2010.                             bufObj->Mappings[MAP_USER].AccessFlags);
  2011.       break;
  2012.    case GL_BUFFER_MAPPED_ARB:
  2013.       *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
  2014.       break;
  2015.    case GL_BUFFER_ACCESS_FLAGS:
  2016.       if (!ctx->Extensions.ARB_map_buffer_range)
  2017.          goto invalid_pname;
  2018.       *params = bufObj->Mappings[MAP_USER].AccessFlags;
  2019.       break;
  2020.    case GL_BUFFER_MAP_OFFSET:
  2021.       if (!ctx->Extensions.ARB_map_buffer_range)
  2022.          goto invalid_pname;
  2023.       *params = bufObj->Mappings[MAP_USER].Offset;
  2024.       break;
  2025.    case GL_BUFFER_MAP_LENGTH:
  2026.       if (!ctx->Extensions.ARB_map_buffer_range)
  2027.          goto invalid_pname;
  2028.       *params = bufObj->Mappings[MAP_USER].Length;
  2029.       break;
  2030.    case GL_BUFFER_IMMUTABLE_STORAGE:
  2031.       if (!ctx->Extensions.ARB_buffer_storage)
  2032.          goto invalid_pname;
  2033.       *params = bufObj->Immutable;
  2034.       break;
  2035.    case GL_BUFFER_STORAGE_FLAGS:
  2036.       if (!ctx->Extensions.ARB_buffer_storage)
  2037.          goto invalid_pname;
  2038.       *params = bufObj->StorageFlags;
  2039.       break;
  2040.    default:
  2041.       goto invalid_pname;
  2042.    }
  2043.  
  2044.    return true;
  2045.  
  2046. invalid_pname:
  2047.    _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname: %s)", func,
  2048.                _mesa_lookup_enum_by_nr(pname));
  2049.    return false;
  2050. }
  2051.  
  2052. void GLAPIENTRY
  2053. _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
  2054. {
  2055.    GET_CURRENT_CONTEXT(ctx);
  2056.    struct gl_buffer_object *bufObj;
  2057.    GLint64 parameter;
  2058.  
  2059.    bufObj = get_buffer(ctx, "glGetBufferParameteriv", target,
  2060.                        GL_INVALID_OPERATION);
  2061.    if (!bufObj)
  2062.       return;
  2063.  
  2064.    if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
  2065.                              "glGetBufferParameteriv"))
  2066.       return; /* Error already recorded. */
  2067.  
  2068.    *params = (GLint) parameter;
  2069. }
  2070.  
  2071. void GLAPIENTRY
  2072. _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
  2073. {
  2074.    GET_CURRENT_CONTEXT(ctx);
  2075.    struct gl_buffer_object *bufObj;
  2076.    GLint64 parameter;
  2077.  
  2078.    bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
  2079.                        GL_INVALID_OPERATION);
  2080.    if (!bufObj)
  2081.       return;
  2082.  
  2083.    if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
  2084.                              "glGetBufferParameteri64v"))
  2085.       return; /* Error already recorded. */
  2086.  
  2087.    *params = parameter;
  2088. }
  2089.  
  2090. void GLAPIENTRY
  2091. _mesa_GetNamedBufferParameteriv(GLuint buffer, GLenum pname, GLint *params)
  2092. {
  2093.    GET_CURRENT_CONTEXT(ctx);
  2094.    struct gl_buffer_object *bufObj;
  2095.    GLint64 parameter;
  2096.  
  2097.    if (!ctx->Extensions.ARB_direct_state_access) {
  2098.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2099.                   "glGetNamedBufferParameteriv(GL_ARB_direct_state_access "
  2100.                   "is not supported)");
  2101.       return;
  2102.    }
  2103.  
  2104.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
  2105.                                        "glGetNamedBufferParameteriv");
  2106.    if (!bufObj)
  2107.       return;
  2108.  
  2109.    if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
  2110.                              "glGetNamedBufferParameteriv"))
  2111.       return; /* Error already recorded. */
  2112.  
  2113.    *params = (GLint) parameter;
  2114. }
  2115.  
  2116. void GLAPIENTRY
  2117. _mesa_GetNamedBufferParameteri64v(GLuint buffer, GLenum pname,
  2118.                                   GLint64 *params)
  2119. {
  2120.    GET_CURRENT_CONTEXT(ctx);
  2121.    struct gl_buffer_object *bufObj;
  2122.    GLint64 parameter;
  2123.  
  2124.    if (!ctx->Extensions.ARB_direct_state_access) {
  2125.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2126.                   "glGetNamedBufferParameteri64v(GL_ARB_direct_state_access "
  2127.                   "is not supported)");
  2128.       return;
  2129.    }
  2130.  
  2131.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
  2132.                                        "glGetNamedBufferParameteri64v");
  2133.    if (!bufObj)
  2134.       return;
  2135.  
  2136.    if (!get_buffer_parameter(ctx, bufObj, pname, &parameter,
  2137.                              "glGetNamedBufferParameteri64v"))
  2138.       return; /* Error already recorded. */
  2139.  
  2140.    *params = parameter;
  2141. }
  2142.  
  2143.  
  2144. void GLAPIENTRY
  2145. _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
  2146. {
  2147.    GET_CURRENT_CONTEXT(ctx);
  2148.    struct gl_buffer_object *bufObj;
  2149.  
  2150.    if (pname != GL_BUFFER_MAP_POINTER) {
  2151.       _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointerv(pname != "
  2152.                   "GL_BUFFER_MAP_POINTER)");
  2153.       return;
  2154.    }
  2155.  
  2156.    bufObj = get_buffer(ctx, "glGetBufferPointerv", target,
  2157.                        GL_INVALID_OPERATION);
  2158.    if (!bufObj)
  2159.       return;
  2160.  
  2161.    *params = bufObj->Mappings[MAP_USER].Pointer;
  2162. }
  2163.  
  2164. void GLAPIENTRY
  2165. _mesa_GetNamedBufferPointerv(GLuint buffer, GLenum pname, GLvoid **params)
  2166. {
  2167.    GET_CURRENT_CONTEXT(ctx);
  2168.    struct gl_buffer_object *bufObj;
  2169.  
  2170.    if (!ctx->Extensions.ARB_direct_state_access) {
  2171.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2172.                   "glGetNamedBufferPointerv(GL_ARB_direct_state_access "
  2173.                   "is not supported)");
  2174.       return;
  2175.    }
  2176.  
  2177.    if (pname != GL_BUFFER_MAP_POINTER) {
  2178.       _mesa_error(ctx, GL_INVALID_ENUM, "glGetNamedBufferPointerv(pname != "
  2179.                   "GL_BUFFER_MAP_POINTER)");
  2180.       return;
  2181.    }
  2182.  
  2183.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
  2184.                                        "glGetNamedBufferPointerv");
  2185.    if (!bufObj)
  2186.       return;
  2187.  
  2188.    *params = bufObj->Mappings[MAP_USER].Pointer;
  2189. }
  2190.  
  2191.  
  2192. void
  2193. _mesa_copy_buffer_sub_data(struct gl_context *ctx,
  2194.                            struct gl_buffer_object *src,
  2195.                            struct gl_buffer_object *dst,
  2196.                            GLintptr readOffset, GLintptr writeOffset,
  2197.                            GLsizeiptr size, const char *func)
  2198. {
  2199.    if (_mesa_check_disallowed_mapping(src)) {
  2200.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2201.                   "%s(readBuffer is mapped)", func);
  2202.       return;
  2203.    }
  2204.  
  2205.    if (_mesa_check_disallowed_mapping(dst)) {
  2206.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2207.                   "%s(writeBuffer is mapped)", func);
  2208.       return;
  2209.    }
  2210.  
  2211.    if (readOffset < 0) {
  2212.       _mesa_error(ctx, GL_INVALID_VALUE,
  2213.                   "%s(readOffset %d < 0)", func, (int) readOffset);
  2214.       return;
  2215.    }
  2216.  
  2217.    if (writeOffset < 0) {
  2218.       _mesa_error(ctx, GL_INVALID_VALUE,
  2219.                   "%s(writeOffset %d < 0)", func, (int) writeOffset);
  2220.       return;
  2221.    }
  2222.  
  2223.    if (size < 0) {
  2224.       _mesa_error(ctx, GL_INVALID_VALUE,
  2225.                   "%s(size %d < 0)", func, (int) size);
  2226.       return;
  2227.    }
  2228.  
  2229.    if (readOffset + size > src->Size) {
  2230.       _mesa_error(ctx, GL_INVALID_VALUE,
  2231.                   "%s(readOffset %d + size %d > src_buffer_size %d)", func,
  2232.                   (int) readOffset, (int) size, (int) src->Size);
  2233.       return;
  2234.    }
  2235.  
  2236.    if (writeOffset + size > dst->Size) {
  2237.       _mesa_error(ctx, GL_INVALID_VALUE,
  2238.                   "%s(writeOffset %d + size %d > dst_buffer_size %d)", func,
  2239.                   (int) writeOffset, (int) size, (int) dst->Size);
  2240.       return;
  2241.    }
  2242.  
  2243.    if (src == dst) {
  2244.       if (readOffset + size <= writeOffset) {
  2245.          /* OK */
  2246.       }
  2247.       else if (writeOffset + size <= readOffset) {
  2248.          /* OK */
  2249.       }
  2250.       else {
  2251.          /* overlapping src/dst is illegal */
  2252.          _mesa_error(ctx, GL_INVALID_VALUE,
  2253.                      "%s(overlapping src/dst)", func);
  2254.          return;
  2255.       }
  2256.    }
  2257.  
  2258.    ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
  2259. }
  2260.  
  2261. void GLAPIENTRY
  2262. _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
  2263.                         GLintptr readOffset, GLintptr writeOffset,
  2264.                         GLsizeiptr size)
  2265. {
  2266.    GET_CURRENT_CONTEXT(ctx);
  2267.    struct gl_buffer_object *src, *dst;
  2268.  
  2269.    src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
  2270.                     GL_INVALID_OPERATION);
  2271.    if (!src)
  2272.       return;
  2273.  
  2274.    dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
  2275.                     GL_INVALID_OPERATION);
  2276.    if (!dst)
  2277.       return;
  2278.  
  2279.    _mesa_copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
  2280.                               "glCopyBufferSubData");
  2281. }
  2282.  
  2283. void GLAPIENTRY
  2284. _mesa_CopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer,
  2285.                              GLintptr readOffset, GLintptr writeOffset,
  2286.                              GLsizeiptr size)
  2287. {
  2288.    GET_CURRENT_CONTEXT(ctx);
  2289.    struct gl_buffer_object *src, *dst;
  2290.  
  2291.    if (!ctx->Extensions.ARB_direct_state_access) {
  2292.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2293.                   "glCopyNamedBufferSubData(GL_ARB_direct_state_access "
  2294.                   "is not supported)");
  2295.       return;
  2296.    }
  2297.  
  2298.    src = _mesa_lookup_bufferobj_err(ctx, readBuffer,
  2299.                                     "glCopyNamedBufferSubData");
  2300.    if (!src)
  2301.       return;
  2302.  
  2303.    dst = _mesa_lookup_bufferobj_err(ctx, writeBuffer,
  2304.                                     "glCopyNamedBufferSubData");
  2305.    if (!dst)
  2306.       return;
  2307.  
  2308.    _mesa_copy_buffer_sub_data(ctx, src, dst, readOffset, writeOffset, size,
  2309.                               "glCopyNamedBufferSubData");
  2310. }
  2311.  
  2312.  
  2313. void *
  2314. _mesa_map_buffer_range(struct gl_context *ctx,
  2315.                        struct gl_buffer_object *bufObj,
  2316.                        GLintptr offset, GLsizeiptr length,
  2317.                        GLbitfield access, const char *func)
  2318. {
  2319.    void *map;
  2320.    GLbitfield allowed_access;
  2321.  
  2322.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
  2323.  
  2324.    if (offset < 0) {
  2325.       _mesa_error(ctx, GL_INVALID_VALUE,
  2326.                   "%s(offset %ld < 0)", func, (long) offset);
  2327.       return NULL;
  2328.    }
  2329.  
  2330.    if (length < 0) {
  2331.       _mesa_error(ctx, GL_INVALID_VALUE,
  2332.                   "%s(length %ld < 0)", func, (long) length);
  2333.       return NULL;
  2334.    }
  2335.  
  2336.    /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
  2337.     *
  2338.     *     "An INVALID_OPERATION error is generated for any of the following
  2339.     *     conditions:
  2340.     *
  2341.     *     * <length> is zero."
  2342.     *
  2343.     * Additionally, page 94 of the PDF of the OpenGL 4.5 core spec
  2344.     * (30.10.2014) also says this, so it's no longer allowed for desktop GL,
  2345.     * either.
  2346.     */
  2347.    if (length == 0) {
  2348.       _mesa_error(ctx, GL_INVALID_OPERATION, "%s(length = 0)", func);
  2349.       return NULL;
  2350.    }
  2351.  
  2352.    allowed_access = GL_MAP_READ_BIT |
  2353.                     GL_MAP_WRITE_BIT |
  2354.                     GL_MAP_INVALIDATE_RANGE_BIT |
  2355.                     GL_MAP_INVALIDATE_BUFFER_BIT |
  2356.                     GL_MAP_FLUSH_EXPLICIT_BIT |
  2357.                     GL_MAP_UNSYNCHRONIZED_BIT;
  2358.  
  2359.    if (ctx->Extensions.ARB_buffer_storage) {
  2360.          allowed_access |= GL_MAP_PERSISTENT_BIT |
  2361.                            GL_MAP_COHERENT_BIT;
  2362.    }
  2363.  
  2364.    if (access & ~allowed_access) {
  2365.       /* generate an error if any bits other than those allowed are set */
  2366.       _mesa_error(ctx, GL_INVALID_VALUE,
  2367.                   "%s(access has undefined bits set)", func);
  2368.       return NULL;
  2369.    }
  2370.  
  2371.    if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
  2372.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2373.                   "%s(access indicates neither read or write)", func);
  2374.       return NULL;
  2375.    }
  2376.  
  2377.    if ((access & GL_MAP_READ_BIT) &&
  2378.        (access & (GL_MAP_INVALIDATE_RANGE_BIT |
  2379.                   GL_MAP_INVALIDATE_BUFFER_BIT |
  2380.                   GL_MAP_UNSYNCHRONIZED_BIT))) {
  2381.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2382.                   "%s(read access with disallowed bits)", func);
  2383.       return NULL;
  2384.    }
  2385.  
  2386.    if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
  2387.        ((access & GL_MAP_WRITE_BIT) == 0)) {
  2388.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2389.                   "%s(access has flush explicit without write)", func);
  2390.       return NULL;
  2391.    }
  2392.  
  2393.    if (access & GL_MAP_READ_BIT &&
  2394.        !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
  2395.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2396.                   "%s(buffer does not allow read access)", func);
  2397.       return NULL;
  2398.    }
  2399.  
  2400.    if (access & GL_MAP_WRITE_BIT &&
  2401.        !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
  2402.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2403.                   "%s(buffer does not allow write access)", func);
  2404.       return NULL;
  2405.    }
  2406.  
  2407.    if (access & GL_MAP_COHERENT_BIT &&
  2408.        !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
  2409.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2410.                   "%s(buffer does not allow coherent access)", func);
  2411.       return NULL;
  2412.    }
  2413.  
  2414.    if (access & GL_MAP_PERSISTENT_BIT &&
  2415.        !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
  2416.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2417.                   "%s(buffer does not allow persistent access)", func);
  2418.       return NULL;
  2419.    }
  2420.  
  2421.    if (offset + length > bufObj->Size) {
  2422.       _mesa_error(ctx, GL_INVALID_VALUE,
  2423.                   "%s(offset %ld + length %ld > buffer_size %ld)", func,
  2424.                   offset, length, bufObj->Size);
  2425.       return NULL;
  2426.    }
  2427.  
  2428.    if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
  2429.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2430.                   "%s(buffer already mapped)", func);
  2431.       return NULL;
  2432.    }
  2433.  
  2434.    if (!bufObj->Size) {
  2435.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(buffer size = 0)", func);
  2436.       return NULL;
  2437.    }
  2438.  
  2439.  
  2440.    assert(ctx->Driver.MapBufferRange);
  2441.    map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
  2442.                                     MAP_USER);
  2443.    if (!map) {
  2444.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s(map failed)", func);
  2445.    }
  2446.    else {
  2447.       /* The driver callback should have set all these fields.
  2448.        * This is important because other modules (like VBO) might call
  2449.        * the driver function directly.
  2450.        */
  2451.       assert(bufObj->Mappings[MAP_USER].Pointer == map);
  2452.       assert(bufObj->Mappings[MAP_USER].Length == length);
  2453.       assert(bufObj->Mappings[MAP_USER].Offset == offset);
  2454.       assert(bufObj->Mappings[MAP_USER].AccessFlags == access);
  2455.    }
  2456.  
  2457.    if (access & GL_MAP_WRITE_BIT)
  2458.       bufObj->Written = GL_TRUE;
  2459.  
  2460. #ifdef VBO_DEBUG
  2461.    if (strstr(func, "Range") == NULL) { /* If not MapRange */
  2462.       printf("glMapBuffer(%u, sz %ld, access 0x%x)\n",
  2463.             bufObj->Name, bufObj->Size, access);
  2464.       /* Access must be write only */
  2465.       if ((access & GL_MAP_WRITE_BIT) && (!(access & ~GL_MAP_WRITE_BIT))) {
  2466.          GLuint i;
  2467.          GLubyte *b = (GLubyte *) bufObj->Pointer;
  2468.          for (i = 0; i < bufObj->Size; i++)
  2469.             b[i] = i & 0xff;
  2470.       }
  2471.    }
  2472. #endif
  2473.  
  2474. #ifdef BOUNDS_CHECK
  2475.    if (strstr(func, "Range") == NULL) { /* If not MapRange */
  2476.       GLubyte *buf = (GLubyte *) bufObj->Pointer;
  2477.       GLuint i;
  2478.       /* buffer is 100 bytes larger than requested, fill with magic value */
  2479.       for (i = 0; i < 100; i++) {
  2480.          buf[bufObj->Size - i - 1] = 123;
  2481.       }
  2482.    }
  2483. #endif
  2484.  
  2485.    return map;
  2486. }
  2487.  
  2488. void * GLAPIENTRY
  2489. _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
  2490.                      GLbitfield access)
  2491. {
  2492.    GET_CURRENT_CONTEXT(ctx);
  2493.    struct gl_buffer_object *bufObj;
  2494.  
  2495.    if (!ctx->Extensions.ARB_map_buffer_range) {
  2496.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2497.                   "glMapBufferRange(ARB_map_buffer_range not supported)");
  2498.       return NULL;
  2499.    }
  2500.  
  2501.    bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
  2502.    if (!bufObj)
  2503.       return NULL;
  2504.  
  2505.    return _mesa_map_buffer_range(ctx, bufObj, offset, length, access,
  2506.                                  "glMapBufferRange");
  2507. }
  2508.  
  2509. void * GLAPIENTRY
  2510. _mesa_MapNamedBufferRange(GLuint buffer, GLintptr offset, GLsizeiptr length,
  2511.                           GLbitfield access)
  2512. {
  2513.    GET_CURRENT_CONTEXT(ctx);
  2514.    struct gl_buffer_object *bufObj;
  2515.  
  2516.    if (!ctx->Extensions.ARB_direct_state_access) {
  2517.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2518.                   "glMapNamedBufferRange(GL_ARB_direct_state_access "
  2519.                   "is not supported)");
  2520.       return NULL;
  2521.    }
  2522.  
  2523.    if (!ctx->Extensions.ARB_map_buffer_range) {
  2524.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2525.                   "glMapNamedBufferRange("
  2526.                   "ARB_map_buffer_range not supported)");
  2527.       return NULL;
  2528.    }
  2529.  
  2530.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBufferRange");
  2531.    if (!bufObj)
  2532.       return NULL;
  2533.  
  2534.    return _mesa_map_buffer_range(ctx, bufObj, offset, length, access,
  2535.                                  "glMapNamedBufferRange");
  2536. }
  2537.  
  2538. /**
  2539.  * Converts GLenum access from MapBuffer and MapNamedBuffer into
  2540.  * flags for input to _mesa_map_buffer_range.
  2541.  *
  2542.  * \return true if the type of requested access is permissible.
  2543.  */
  2544. static bool
  2545. get_map_buffer_access_flags(struct gl_context *ctx, GLenum access,
  2546.                             GLbitfield *flags)
  2547. {
  2548.    switch (access) {
  2549.    case GL_READ_ONLY_ARB:
  2550.       *flags = GL_MAP_READ_BIT;
  2551.       return _mesa_is_desktop_gl(ctx);
  2552.    case GL_WRITE_ONLY_ARB:
  2553.       *flags = GL_MAP_WRITE_BIT;
  2554.       return true;
  2555.    case GL_READ_WRITE_ARB:
  2556.       *flags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
  2557.       return _mesa_is_desktop_gl(ctx);
  2558.    default:
  2559.       return false;
  2560.    }
  2561. }
  2562.  
  2563. void * GLAPIENTRY
  2564. _mesa_MapBuffer(GLenum target, GLenum access)
  2565. {
  2566.    GET_CURRENT_CONTEXT(ctx);
  2567.    struct gl_buffer_object *bufObj;
  2568.    GLbitfield accessFlags;
  2569.  
  2570.    if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
  2571.       _mesa_error(ctx, GL_INVALID_ENUM, "glMapBuffer(invalid access)");
  2572.       return NULL;
  2573.    }
  2574.  
  2575.    bufObj = get_buffer(ctx, "glMapBuffer", target, GL_INVALID_OPERATION);
  2576.    if (!bufObj)
  2577.       return NULL;
  2578.  
  2579.    return _mesa_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
  2580.                                  "glMapBuffer");
  2581. }
  2582.  
  2583. void * GLAPIENTRY
  2584. _mesa_MapNamedBuffer(GLuint buffer, GLenum access)
  2585. {
  2586.    GET_CURRENT_CONTEXT(ctx);
  2587.    struct gl_buffer_object *bufObj;
  2588.    GLbitfield accessFlags;
  2589.  
  2590.    if (!ctx->Extensions.ARB_direct_state_access) {
  2591.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2592.                   "glMapNamedBuffer(GL_ARB_direct_state_access "
  2593.                   "is not supported)");
  2594.       return NULL;
  2595.    }
  2596.  
  2597.    if (!get_map_buffer_access_flags(ctx, access, &accessFlags)) {
  2598.       _mesa_error(ctx, GL_INVALID_ENUM, "glMapNamedBuffer(invalid access)");
  2599.       return NULL;
  2600.    }
  2601.  
  2602.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer, "glMapNamedBuffer");
  2603.    if (!bufObj)
  2604.       return NULL;
  2605.  
  2606.    return _mesa_map_buffer_range(ctx, bufObj, 0, bufObj->Size, accessFlags,
  2607.                                  "glMapNamedBuffer");
  2608. }
  2609.  
  2610.  
  2611. void
  2612. _mesa_flush_mapped_buffer_range(struct gl_context *ctx,
  2613.                                 struct gl_buffer_object *bufObj,
  2614.                                 GLintptr offset, GLsizeiptr length,
  2615.                                 const char *func)
  2616. {
  2617.    if (!ctx->Extensions.ARB_map_buffer_range) {
  2618.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2619.                   "%s(ARB_map_buffer_range not supported)", func);
  2620.       return;
  2621.    }
  2622.  
  2623.    if (offset < 0) {
  2624.       _mesa_error(ctx, GL_INVALID_VALUE,
  2625.                   "%s(offset %ld < 0)", func, (long) offset);
  2626.       return;
  2627.    }
  2628.  
  2629.    if (length < 0) {
  2630.       _mesa_error(ctx, GL_INVALID_VALUE,
  2631.                   "%s(length %ld < 0)", func, (long) length);
  2632.       return;
  2633.    }
  2634.  
  2635.    if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
  2636.       /* buffer is not mapped */
  2637.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2638.                   "%s(buffer is not mapped)", func);
  2639.       return;
  2640.    }
  2641.  
  2642.    if ((bufObj->Mappings[MAP_USER].AccessFlags &
  2643.         GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
  2644.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2645.                   "%s(GL_MAP_FLUSH_EXPLICIT_BIT not set)", func);
  2646.       return;
  2647.    }
  2648.  
  2649.    if (offset + length > bufObj->Mappings[MAP_USER].Length) {
  2650.       _mesa_error(ctx, GL_INVALID_VALUE,
  2651.                   "%s(offset %ld + length %ld > mapped length %ld)", func,
  2652.                   (long) offset, (long) length,
  2653.                   (long) bufObj->Mappings[MAP_USER].Length);
  2654.       return;
  2655.    }
  2656.  
  2657.    assert(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
  2658.  
  2659.    if (ctx->Driver.FlushMappedBufferRange)
  2660.       ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
  2661.                                          MAP_USER);
  2662. }
  2663.  
  2664. void GLAPIENTRY
  2665. _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset,
  2666.                              GLsizeiptr length)
  2667. {
  2668.    GET_CURRENT_CONTEXT(ctx);
  2669.    struct gl_buffer_object *bufObj;
  2670.  
  2671.    bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
  2672.                        GL_INVALID_OPERATION);
  2673.    if (!bufObj)
  2674.       return;
  2675.  
  2676.    _mesa_flush_mapped_buffer_range(ctx, bufObj, offset, length,
  2677.                                    "glFlushMappedBufferRange");
  2678. }
  2679.  
  2680. void GLAPIENTRY
  2681. _mesa_FlushMappedNamedBufferRange(GLuint buffer, GLintptr offset,
  2682.                                   GLsizeiptr length)
  2683. {
  2684.    GET_CURRENT_CONTEXT(ctx);
  2685.    struct gl_buffer_object *bufObj;
  2686.  
  2687.    if (!ctx->Extensions.ARB_direct_state_access) {
  2688.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2689.                   "glFlushMappedNamedBufferRange(GL_ARB_direct_state_access "
  2690.                   "is not supported)");
  2691.       return;
  2692.    }
  2693.  
  2694.  
  2695.    bufObj = _mesa_lookup_bufferobj_err(ctx, buffer,
  2696.                                        "glFlushMappedNamedBufferRange");
  2697.    if (!bufObj)
  2698.       return;
  2699.  
  2700.    _mesa_flush_mapped_buffer_range(ctx, bufObj, offset, length,
  2701.                                    "glFlushMappedNamedBufferRange");
  2702. }
  2703.  
  2704.  
  2705. static GLenum
  2706. buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
  2707. {
  2708.    struct gl_buffer_object *bufObj;
  2709.    GLenum retval;
  2710.  
  2711.    bufObj = _mesa_lookup_bufferobj(ctx, name);
  2712.    if (!bufObj) {
  2713.       _mesa_error(ctx, GL_INVALID_VALUE,
  2714.                   "glObjectPurgeable(name = 0x%x)", name);
  2715.       return 0;
  2716.    }
  2717.    if (!_mesa_is_bufferobj(bufObj)) {
  2718.       _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
  2719.       return 0;
  2720.    }
  2721.  
  2722.    if (bufObj->Purgeable) {
  2723.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2724.                   "glObjectPurgeable(name = 0x%x) is already purgeable", name);
  2725.       return GL_VOLATILE_APPLE;
  2726.    }
  2727.  
  2728.    bufObj->Purgeable = GL_TRUE;
  2729.  
  2730.    retval = GL_VOLATILE_APPLE;
  2731.    if (ctx->Driver.BufferObjectPurgeable)
  2732.       retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
  2733.  
  2734.    return retval;
  2735. }
  2736.  
  2737.  
  2738. static GLenum
  2739. renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
  2740. {
  2741.    struct gl_renderbuffer *bufObj;
  2742.    GLenum retval;
  2743.  
  2744.    bufObj = _mesa_lookup_renderbuffer(ctx, name);
  2745.    if (!bufObj) {
  2746.       _mesa_error(ctx, GL_INVALID_VALUE,
  2747.                   "glObjectUnpurgeable(name = 0x%x)", name);
  2748.       return 0;
  2749.    }
  2750.  
  2751.    if (bufObj->Purgeable) {
  2752.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2753.                   "glObjectPurgeable(name = 0x%x) is already purgeable", name);
  2754.       return GL_VOLATILE_APPLE;
  2755.    }
  2756.  
  2757.    bufObj->Purgeable = GL_TRUE;
  2758.  
  2759.    retval = GL_VOLATILE_APPLE;
  2760.    if (ctx->Driver.RenderObjectPurgeable)
  2761.       retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
  2762.  
  2763.    return retval;
  2764. }
  2765.  
  2766.  
  2767. static GLenum
  2768. texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
  2769. {
  2770.    struct gl_texture_object *bufObj;
  2771.    GLenum retval;
  2772.  
  2773.    bufObj = _mesa_lookup_texture(ctx, name);
  2774.    if (!bufObj) {
  2775.       _mesa_error(ctx, GL_INVALID_VALUE,
  2776.                   "glObjectPurgeable(name = 0x%x)", name);
  2777.       return 0;
  2778.    }
  2779.  
  2780.    if (bufObj->Purgeable) {
  2781.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2782.                   "glObjectPurgeable(name = 0x%x) is already purgeable", name);
  2783.       return GL_VOLATILE_APPLE;
  2784.    }
  2785.  
  2786.    bufObj->Purgeable = GL_TRUE;
  2787.  
  2788.    retval = GL_VOLATILE_APPLE;
  2789.    if (ctx->Driver.TextureObjectPurgeable)
  2790.       retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
  2791.  
  2792.    return retval;
  2793. }
  2794.  
  2795.  
  2796. GLenum GLAPIENTRY
  2797. _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
  2798. {
  2799.    GLenum retval;
  2800.  
  2801.    GET_CURRENT_CONTEXT(ctx);
  2802.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
  2803.  
  2804.    if (name == 0) {
  2805.       _mesa_error(ctx, GL_INVALID_VALUE,
  2806.                   "glObjectPurgeable(name = 0x%x)", name);
  2807.       return 0;
  2808.    }
  2809.  
  2810.    switch (option) {
  2811.    case GL_VOLATILE_APPLE:
  2812.    case GL_RELEASED_APPLE:
  2813.       /* legal */
  2814.       break;
  2815.    default:
  2816.       _mesa_error(ctx, GL_INVALID_ENUM,
  2817.                   "glObjectPurgeable(name = 0x%x) invalid option: %d",
  2818.                   name, option);
  2819.       return 0;
  2820.    }
  2821.  
  2822.    switch (objectType) {
  2823.    case GL_TEXTURE:
  2824.       retval = texture_object_purgeable(ctx, name, option);
  2825.       break;
  2826.    case GL_RENDERBUFFER_EXT:
  2827.       retval = renderbuffer_purgeable(ctx, name, option);
  2828.       break;
  2829.    case GL_BUFFER_OBJECT_APPLE:
  2830.       retval = buffer_object_purgeable(ctx, name, option);
  2831.       break;
  2832.    default:
  2833.       _mesa_error(ctx, GL_INVALID_ENUM,
  2834.                   "glObjectPurgeable(name = 0x%x) invalid type: %d",
  2835.                   name, objectType);
  2836.       return 0;
  2837.    }
  2838.  
  2839.    /* In strict conformance to the spec, we must only return VOLATILE when
  2840.     * when passed the VOLATILE option. Madness.
  2841.     *
  2842.     * XXX First fix the spec, then fix me.
  2843.     */
  2844.    return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
  2845. }
  2846.  
  2847.  
  2848. static GLenum
  2849. buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
  2850. {
  2851.    struct gl_buffer_object *bufObj;
  2852.    GLenum retval;
  2853.  
  2854.    bufObj = _mesa_lookup_bufferobj(ctx, name);
  2855.    if (!bufObj) {
  2856.       _mesa_error(ctx, GL_INVALID_VALUE,
  2857.                   "glObjectUnpurgeable(name = 0x%x)", name);
  2858.       return 0;
  2859.    }
  2860.  
  2861.    if (! bufObj->Purgeable) {
  2862.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2863.                   "glObjectUnpurgeable(name = 0x%x) object is "
  2864.                   " already \"unpurged\"", name);
  2865.       return 0;
  2866.    }
  2867.  
  2868.    bufObj->Purgeable = GL_FALSE;
  2869.  
  2870.    retval = option;
  2871.    if (ctx->Driver.BufferObjectUnpurgeable)
  2872.       retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
  2873.  
  2874.    return retval;
  2875. }
  2876.  
  2877.  
  2878. static GLenum
  2879. renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
  2880. {
  2881.    struct gl_renderbuffer *bufObj;
  2882.    GLenum retval;
  2883.  
  2884.    bufObj = _mesa_lookup_renderbuffer(ctx, name);
  2885.    if (!bufObj) {
  2886.       _mesa_error(ctx, GL_INVALID_VALUE,
  2887.                   "glObjectUnpurgeable(name = 0x%x)", name);
  2888.       return 0;
  2889.    }
  2890.  
  2891.    if (! bufObj->Purgeable) {
  2892.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2893.                   "glObjectUnpurgeable(name = 0x%x) object is "
  2894.                   " already \"unpurged\"", name);
  2895.       return 0;
  2896.    }
  2897.  
  2898.    bufObj->Purgeable = GL_FALSE;
  2899.  
  2900.    retval = option;
  2901.    if (ctx->Driver.RenderObjectUnpurgeable)
  2902.       retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
  2903.  
  2904.    return retval;
  2905. }
  2906.  
  2907.  
  2908. static GLenum
  2909. texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
  2910. {
  2911.    struct gl_texture_object *bufObj;
  2912.    GLenum retval;
  2913.  
  2914.    bufObj = _mesa_lookup_texture(ctx, name);
  2915.    if (!bufObj) {
  2916.       _mesa_error(ctx, GL_INVALID_VALUE,
  2917.                   "glObjectUnpurgeable(name = 0x%x)", name);
  2918.       return 0;
  2919.    }
  2920.  
  2921.    if (! bufObj->Purgeable) {
  2922.       _mesa_error(ctx, GL_INVALID_OPERATION,
  2923.                   "glObjectUnpurgeable(name = 0x%x) object is"
  2924.                   " already \"unpurged\"", name);
  2925.       return 0;
  2926.    }
  2927.  
  2928.    bufObj->Purgeable = GL_FALSE;
  2929.  
  2930.    retval = option;
  2931.    if (ctx->Driver.TextureObjectUnpurgeable)
  2932.       retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
  2933.  
  2934.    return retval;
  2935. }
  2936.  
  2937.  
  2938. GLenum GLAPIENTRY
  2939. _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
  2940. {
  2941.    GET_CURRENT_CONTEXT(ctx);
  2942.    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
  2943.  
  2944.    if (name == 0) {
  2945.       _mesa_error(ctx, GL_INVALID_VALUE,
  2946.                   "glObjectUnpurgeable(name = 0x%x)", name);
  2947.       return 0;
  2948.    }
  2949.  
  2950.    switch (option) {
  2951.    case GL_RETAINED_APPLE:
  2952.    case GL_UNDEFINED_APPLE:
  2953.       /* legal */
  2954.       break;
  2955.    default:
  2956.       _mesa_error(ctx, GL_INVALID_ENUM,
  2957.                   "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
  2958.                   name, option);
  2959.       return 0;
  2960.    }
  2961.  
  2962.    switch (objectType) {
  2963.    case GL_BUFFER_OBJECT_APPLE:
  2964.       return buffer_object_unpurgeable(ctx, name, option);
  2965.    case GL_TEXTURE:
  2966.       return texture_object_unpurgeable(ctx, name, option);
  2967.    case GL_RENDERBUFFER_EXT:
  2968.       return renderbuffer_unpurgeable(ctx, name, option);
  2969.    default:
  2970.       _mesa_error(ctx, GL_INVALID_ENUM,
  2971.                   "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
  2972.                   name, objectType);
  2973.       return 0;
  2974.    }
  2975. }
  2976.  
  2977.  
  2978. static void
  2979. get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
  2980.                               GLenum pname, GLint *params)
  2981. {
  2982.    struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
  2983.    if (!bufObj) {
  2984.       _mesa_error(ctx, GL_INVALID_VALUE,
  2985.                   "glGetObjectParameteriv(name = 0x%x) invalid object", name);
  2986.       return;
  2987.    }
  2988.  
  2989.    switch (pname) {
  2990.    case GL_PURGEABLE_APPLE:
  2991.       *params = bufObj->Purgeable;
  2992.       break;
  2993.    default:
  2994.       _mesa_error(ctx, GL_INVALID_ENUM,
  2995.                   "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
  2996.                   name, pname);
  2997.       break;
  2998.    }
  2999. }
  3000.  
  3001.  
  3002. static void
  3003. get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
  3004.                              GLenum pname, GLint *params)
  3005. {
  3006.    struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
  3007.    if (!rb) {
  3008.       _mesa_error(ctx, GL_INVALID_VALUE,
  3009.                   "glObjectUnpurgeable(name = 0x%x)", name);
  3010.       return;
  3011.    }
  3012.  
  3013.    switch (pname) {
  3014.    case GL_PURGEABLE_APPLE:
  3015.       *params = rb->Purgeable;
  3016.       break;
  3017.    default:
  3018.       _mesa_error(ctx, GL_INVALID_ENUM,
  3019.                   "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
  3020.                   name, pname);
  3021.       break;
  3022.    }
  3023. }
  3024.  
  3025.  
  3026. static void
  3027. get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
  3028.                                GLenum pname, GLint *params)
  3029. {
  3030.    struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
  3031.    if (!texObj) {
  3032.       _mesa_error(ctx, GL_INVALID_VALUE,
  3033.                   "glObjectUnpurgeable(name = 0x%x)", name);
  3034.       return;
  3035.    }
  3036.  
  3037.    switch (pname) {
  3038.    case GL_PURGEABLE_APPLE:
  3039.       *params = texObj->Purgeable;
  3040.       break;
  3041.    default:
  3042.       _mesa_error(ctx, GL_INVALID_ENUM,
  3043.                   "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
  3044.                   name, pname);
  3045.       break;
  3046.    }
  3047. }
  3048.  
  3049.  
  3050. void GLAPIENTRY
  3051. _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
  3052.                                 GLint *params)
  3053. {
  3054.    GET_CURRENT_CONTEXT(ctx);
  3055.  
  3056.    if (name == 0) {
  3057.       _mesa_error(ctx, GL_INVALID_VALUE,
  3058.                   "glGetObjectParameteriv(name = 0x%x)", name);
  3059.       return;
  3060.    }
  3061.  
  3062.    switch (objectType) {
  3063.    case GL_TEXTURE:
  3064.       get_texture_object_parameteriv(ctx, name, pname, params);
  3065.       break;
  3066.    case GL_BUFFER_OBJECT_APPLE:
  3067.       get_buffer_object_parameteriv(ctx, name, pname, params);
  3068.       break;
  3069.    case GL_RENDERBUFFER_EXT:
  3070.       get_renderbuffer_parameteriv(ctx, name, pname, params);
  3071.       break;
  3072.    default:
  3073.       _mesa_error(ctx, GL_INVALID_ENUM,
  3074.                   "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
  3075.                   name, objectType);
  3076.    }
  3077. }
  3078.  
  3079. /**
  3080.  * Binds a buffer object to a uniform buffer binding point.
  3081.  *
  3082.  * The caller is responsible for flushing vertices and updating
  3083.  * NewDriverState.
  3084.  */
  3085. static void
  3086. set_ubo_binding(struct gl_context *ctx,
  3087.                 struct gl_uniform_buffer_binding *binding,
  3088.                 struct gl_buffer_object *bufObj,
  3089.                 GLintptr offset,
  3090.                 GLsizeiptr size,
  3091.                 GLboolean autoSize)
  3092. {
  3093.    _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
  3094.  
  3095.    binding->Offset = offset;
  3096.    binding->Size = size;
  3097.    binding->AutomaticSize = autoSize;
  3098.  
  3099.    /* If this is a real buffer object, mark it has having been used
  3100.     * at some point as a UBO.
  3101.     */
  3102.    if (size >= 0)
  3103.       bufObj->UsageHistory |= USAGE_UNIFORM_BUFFER;
  3104. }
  3105.  
  3106. /**
  3107.  * Binds a buffer object to a uniform buffer binding point.
  3108.  *
  3109.  * Unlike set_ubo_binding(), this function also flushes vertices
  3110.  * and updates NewDriverState.  It also checks if the binding
  3111.  * has actually changed before updating it.
  3112.  */
  3113. static void
  3114. bind_uniform_buffer(struct gl_context *ctx,
  3115.                     GLuint index,
  3116.                     struct gl_buffer_object *bufObj,
  3117.                     GLintptr offset,
  3118.                     GLsizeiptr size,
  3119.                     GLboolean autoSize)
  3120. {
  3121.    struct gl_uniform_buffer_binding *binding =
  3122.       &ctx->UniformBufferBindings[index];
  3123.  
  3124.    if (binding->BufferObject == bufObj &&
  3125.        binding->Offset == offset &&
  3126.        binding->Size == size &&
  3127.        binding->AutomaticSize == autoSize) {
  3128.       return;
  3129.    }
  3130.  
  3131.    FLUSH_VERTICES(ctx, 0);
  3132.    ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
  3133.  
  3134.    set_ubo_binding(ctx, binding, bufObj, offset, size, autoSize);
  3135. }
  3136.  
  3137. /**
  3138.  * Bind a region of a buffer object to a uniform block binding point.
  3139.  * \param index  the uniform buffer binding point index
  3140.  * \param bufObj  the buffer object
  3141.  * \param offset  offset to the start of buffer object region
  3142.  * \param size  size of the buffer object region
  3143.  */
  3144. static void
  3145. bind_buffer_range_uniform_buffer(struct gl_context *ctx,
  3146.                                  GLuint index,
  3147.                                  struct gl_buffer_object *bufObj,
  3148.                                  GLintptr offset,
  3149.                                  GLsizeiptr size)
  3150. {
  3151.    if (index >= ctx->Const.MaxUniformBufferBindings) {
  3152.       _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
  3153.       return;
  3154.    }
  3155.  
  3156.    if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
  3157.       _mesa_error(ctx, GL_INVALID_VALUE,
  3158.                   "glBindBufferRange(offset misaligned %d/%d)", (int) offset,
  3159.                   ctx->Const.UniformBufferOffsetAlignment);
  3160.       return;
  3161.    }
  3162.  
  3163.    if (bufObj == ctx->Shared->NullBufferObj) {
  3164.       offset = -1;
  3165.       size = -1;
  3166.    }
  3167.  
  3168.    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
  3169.    bind_uniform_buffer(ctx, index, bufObj, offset, size, GL_FALSE);
  3170. }
  3171.  
  3172.  
  3173. /**
  3174.  * Bind a buffer object to a uniform block binding point.
  3175.  * As above, but offset = 0.
  3176.  */
  3177. static void
  3178. bind_buffer_base_uniform_buffer(struct gl_context *ctx,
  3179.                                 GLuint index,
  3180.                                 struct gl_buffer_object *bufObj)
  3181. {
  3182.    if (index >= ctx->Const.MaxUniformBufferBindings) {
  3183.       _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
  3184.       return;
  3185.    }
  3186.  
  3187.    _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
  3188.  
  3189.    if (bufObj == ctx->Shared->NullBufferObj)
  3190.       bind_uniform_buffer(ctx, index, bufObj, -1, -1, GL_TRUE);
  3191.    else
  3192.       bind_uniform_buffer(ctx, index, bufObj, 0, 0, GL_TRUE);
  3193. }
  3194.  
  3195. /**
  3196.  * Binds a buffer object to an atomic buffer binding point.
  3197.  *
  3198.  * The caller is responsible for validating the offset,
  3199.  * flushing the vertices and updating NewDriverState.
  3200.  */
  3201. static void
  3202. set_atomic_buffer_binding(struct gl_context *ctx,
  3203.                           struct gl_atomic_buffer_binding *binding,
  3204.                           struct gl_buffer_object *bufObj,
  3205.                           GLintptr offset,
  3206.                           GLsizeiptr size)
  3207. {
  3208.    _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
  3209.  
  3210.    if (bufObj == ctx->Shared->NullBufferObj) {
  3211.       binding->Offset = -1;
  3212.       binding->Size = -1;
  3213.    } else {
  3214.       binding->Offset = offset;
  3215.       binding->Size = size;
  3216.       bufObj->UsageHistory |= USAGE_ATOMIC_COUNTER_BUFFER;
  3217.    }
  3218. }
  3219.  
  3220. /**
  3221.  * Binds a buffer object to an atomic buffer binding point.
  3222.  *
  3223.  * Unlike set_atomic_buffer_binding(), this function also validates the
  3224.  * index and offset, flushes vertices, and updates NewDriverState.
  3225.  * It also checks if the binding has actually changing before
  3226.  * updating it.
  3227.  */
  3228. static void
  3229. bind_atomic_buffer(struct gl_context *ctx,
  3230.                    unsigned index,
  3231.                    struct gl_buffer_object *bufObj,
  3232.                    GLintptr offset,
  3233.                    GLsizeiptr size,
  3234.                    const char *name)
  3235. {
  3236.    struct gl_atomic_buffer_binding *binding;
  3237.  
  3238.    if (index >= ctx->Const.MaxAtomicBufferBindings) {
  3239.       _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
  3240.       return;
  3241.    }
  3242.  
  3243.    if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
  3244.       _mesa_error(ctx, GL_INVALID_VALUE,
  3245.                   "%s(offset misaligned %d/%d)", name, (int) offset,
  3246.                   ATOMIC_COUNTER_SIZE);
  3247.       return;
  3248.    }
  3249.  
  3250.    _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
  3251.  
  3252.    binding = &ctx->AtomicBufferBindings[index];
  3253.    if (binding->BufferObject == bufObj &&
  3254.        binding->Offset == offset &&
  3255.        binding->Size == size) {
  3256.       return;
  3257.    }
  3258.  
  3259.    FLUSH_VERTICES(ctx, 0);
  3260.    ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
  3261.  
  3262.    set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
  3263. }
  3264.  
  3265. static inline bool
  3266. bind_buffers_check_offset_and_size(struct gl_context *ctx,
  3267.                                    GLuint index,
  3268.                                    const GLintptr *offsets,
  3269.                                    const GLsizeiptr *sizes)
  3270. {
  3271.    if (offsets[index] < 0) {
  3272.      /* The ARB_multi_bind spec says:
  3273.       *
  3274.       *    "An INVALID_VALUE error is generated by BindBuffersRange if any
  3275.       *     value in <offsets> is less than zero (per binding)."
  3276.       */
  3277.       _mesa_error(ctx, GL_INVALID_VALUE,
  3278.                   "glBindBuffersRange(offsets[%u]=%" PRId64 " < 0)",
  3279.                   index, (int64_t) offsets[index]);
  3280.       return false;
  3281.    }
  3282.  
  3283.    if (sizes[index] <= 0) {
  3284.      /* The ARB_multi_bind spec says:
  3285.       *
  3286.       *     "An INVALID_VALUE error is generated by BindBuffersRange if any
  3287.       *      value in <sizes> is less than or equal to zero (per binding)."
  3288.       */
  3289.       _mesa_error(ctx, GL_INVALID_VALUE,
  3290.                   "glBindBuffersRange(sizes[%u]=%" PRId64 " <= 0)",
  3291.                   index, (int64_t) sizes[index]);
  3292.       return false;
  3293.    }
  3294.  
  3295.    return true;
  3296. }
  3297.  
  3298. static bool
  3299. error_check_bind_uniform_buffers(struct gl_context *ctx,
  3300.                                  GLuint first, GLsizei count,
  3301.                                  const char *caller)
  3302. {
  3303.    if (!ctx->Extensions.ARB_uniform_buffer_object) {
  3304.       _mesa_error(ctx, GL_INVALID_ENUM,
  3305.                   "%s(target=GL_UNIFORM_BUFFER)", caller);
  3306.       return false;
  3307.    }
  3308.  
  3309.    /* The ARB_multi_bind_spec says:
  3310.     *
  3311.     *     "An INVALID_OPERATION error is generated if <first> + <count> is
  3312.     *      greater than the number of target-specific indexed binding points,
  3313.     *      as described in section 6.7.1."
  3314.     */
  3315.    if (first + count > ctx->Const.MaxUniformBufferBindings) {
  3316.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3317.                   "%s(first=%u + count=%d > the value of "
  3318.                   "GL_MAX_UNIFORM_BUFFER_BINDINGS=%u)",
  3319.                   caller, first, count,
  3320.                   ctx->Const.MaxUniformBufferBindings);
  3321.       return false;
  3322.    }
  3323.  
  3324.    return true;
  3325. }
  3326.  
  3327. /**
  3328.  * Unbind all uniform buffers in the range
  3329.  * <first> through <first>+<count>-1
  3330.  */
  3331. static void
  3332. unbind_uniform_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
  3333. {
  3334.    struct gl_buffer_object *bufObj = ctx->Shared->NullBufferObj;
  3335.    GLint i;
  3336.  
  3337.    for (i = 0; i < count; i++)
  3338.       set_ubo_binding(ctx, &ctx->UniformBufferBindings[first + i],
  3339.                       bufObj, -1, -1, GL_TRUE);
  3340. }
  3341.  
  3342. static void
  3343. bind_uniform_buffers_base(struct gl_context *ctx, GLuint first, GLsizei count,
  3344.                           const GLuint *buffers)
  3345. {
  3346.    GLint i;
  3347.  
  3348.    if (!error_check_bind_uniform_buffers(ctx, first, count, "glBindBuffersBase"))
  3349.       return;
  3350.  
  3351.    /* Assume that at least one binding will be changed */
  3352.    FLUSH_VERTICES(ctx, 0);
  3353.    ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
  3354.  
  3355.    if (!buffers) {
  3356.       /* The ARB_multi_bind spec says:
  3357.        *
  3358.        *   "If <buffers> is NULL, all bindings from <first> through
  3359.        *    <first>+<count>-1 are reset to their unbound (zero) state."
  3360.        */
  3361.       unbind_uniform_buffers(ctx, first, count);
  3362.       return;
  3363.    }
  3364.  
  3365.    /* Note that the error semantics for multi-bind commands differ from
  3366.     * those of other GL commands.
  3367.     *
  3368.     * The Issues section in the ARB_multi_bind spec says:
  3369.     *
  3370.     *    "(11) Typically, OpenGL specifies that if an error is generated by a
  3371.     *          command, that command has no effect.  This is somewhat
  3372.     *          unfortunate for multi-bind commands, because it would require a
  3373.     *          first pass to scan the entire list of bound objects for errors
  3374.     *          and then a second pass to actually perform the bindings.
  3375.     *          Should we have different error semantics?
  3376.     *
  3377.     *       RESOLVED:  Yes.  In this specification, when the parameters for
  3378.     *       one of the <count> binding points are invalid, that binding point
  3379.     *       is not updated and an error will be generated.  However, other
  3380.     *       binding points in the same command will be updated if their
  3381.     *       parameters are valid and no other error occurs."
  3382.     */
  3383.  
  3384.    _mesa_begin_bufferobj_lookups(ctx);
  3385.  
  3386.    for (i = 0; i < count; i++) {
  3387.       struct gl_uniform_buffer_binding *binding =
  3388.           &ctx->UniformBufferBindings[first + i];
  3389.       struct gl_buffer_object *bufObj;
  3390.  
  3391.       if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
  3392.          bufObj = binding->BufferObject;
  3393.       else
  3394.          bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
  3395.                                                     "glBindBuffersBase");
  3396.  
  3397.       if (bufObj) {
  3398.          if (bufObj == ctx->Shared->NullBufferObj)
  3399.             set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_TRUE);
  3400.          else
  3401.             set_ubo_binding(ctx, binding, bufObj, 0, 0, GL_TRUE);
  3402.       }
  3403.    }
  3404.  
  3405.    _mesa_end_bufferobj_lookups(ctx);
  3406. }
  3407.  
  3408. static void
  3409. bind_uniform_buffers_range(struct gl_context *ctx, GLuint first, GLsizei count,
  3410.                            const GLuint *buffers,
  3411.                            const GLintptr *offsets, const GLsizeiptr *sizes)
  3412. {
  3413.    GLint i;
  3414.  
  3415.    if (!error_check_bind_uniform_buffers(ctx, first, count,
  3416.                                          "glBindBuffersRange"))
  3417.       return;
  3418.  
  3419.    /* Assume that at least one binding will be changed */
  3420.    FLUSH_VERTICES(ctx, 0);
  3421.    ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
  3422.  
  3423.    if (!buffers) {
  3424.       /* The ARB_multi_bind spec says:
  3425.        *
  3426.        *    "If <buffers> is NULL, all bindings from <first> through
  3427.        *     <first>+<count>-1 are reset to their unbound (zero) state.
  3428.        *     In this case, the offsets and sizes associated with the
  3429.        *     binding points are set to default values, ignoring
  3430.        *     <offsets> and <sizes>."
  3431.        */
  3432.       unbind_uniform_buffers(ctx, first, count);
  3433.       return;
  3434.    }
  3435.  
  3436.    /* Note that the error semantics for multi-bind commands differ from
  3437.     * those of other GL commands.
  3438.     *
  3439.     * The Issues section in the ARB_multi_bind spec says:
  3440.     *
  3441.     *    "(11) Typically, OpenGL specifies that if an error is generated by a
  3442.     *          command, that command has no effect.  This is somewhat
  3443.     *          unfortunate for multi-bind commands, because it would require a
  3444.     *          first pass to scan the entire list of bound objects for errors
  3445.     *          and then a second pass to actually perform the bindings.
  3446.     *          Should we have different error semantics?
  3447.     *
  3448.     *       RESOLVED:  Yes.  In this specification, when the parameters for
  3449.     *       one of the <count> binding points are invalid, that binding point
  3450.     *       is not updated and an error will be generated.  However, other
  3451.     *       binding points in the same command will be updated if their
  3452.     *       parameters are valid and no other error occurs."
  3453.     */
  3454.  
  3455.    _mesa_begin_bufferobj_lookups(ctx);
  3456.  
  3457.    for (i = 0; i < count; i++) {
  3458.       struct gl_uniform_buffer_binding *binding =
  3459.          &ctx->UniformBufferBindings[first + i];
  3460.       struct gl_buffer_object *bufObj;
  3461.  
  3462.       if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
  3463.          continue;
  3464.  
  3465.       /* The ARB_multi_bind spec says:
  3466.        *
  3467.        *     "An INVALID_VALUE error is generated by BindBuffersRange if any
  3468.        *      pair of values in <offsets> and <sizes> does not respectively
  3469.        *      satisfy the constraints described for those parameters for the
  3470.        *      specified target, as described in section 6.7.1 (per binding)."
  3471.        *
  3472.        * Section 6.7.1 refers to table 6.5, which says:
  3473.        *
  3474.        *     "┌───────────────────────────────────────────────────────────────┐
  3475.        *      │ Uniform buffer array bindings (see sec. 7.6)                  │
  3476.        *      ├─────────────────────┬─────────────────────────────────────────┤
  3477.        *      │  ...                │  ...                                    │
  3478.        *      │  offset restriction │  multiple of value of UNIFORM_BUFFER_-  │
  3479.        *      │                     │  OFFSET_ALIGNMENT                       │
  3480.        *      │  ...                │  ...                                    │
  3481.        *      │  size restriction   │  none                                   │
  3482.        *      └─────────────────────┴─────────────────────────────────────────┘"
  3483.        */
  3484.       if (offsets[i] & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
  3485.          _mesa_error(ctx, GL_INVALID_VALUE,
  3486.                      "glBindBuffersRange(offsets[%u]=%" PRId64
  3487.                      " is misaligned; it must be a multiple of the value of "
  3488.                      "GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT=%u when "
  3489.                      "target=GL_UNIFORM_BUFFER)",
  3490.                      i, (int64_t) offsets[i],
  3491.                      ctx->Const.UniformBufferOffsetAlignment);
  3492.          continue;
  3493.       }
  3494.  
  3495.       if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
  3496.          bufObj = binding->BufferObject;
  3497.       else
  3498.          bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
  3499.                                                     "glBindBuffersRange");
  3500.  
  3501.       if (bufObj) {
  3502.          if (bufObj == ctx->Shared->NullBufferObj)
  3503.             set_ubo_binding(ctx, binding, bufObj, -1, -1, GL_FALSE);
  3504.          else
  3505.             set_ubo_binding(ctx, binding, bufObj,
  3506.                             offsets[i], sizes[i], GL_FALSE);
  3507.       }
  3508.    }
  3509.  
  3510.    _mesa_end_bufferobj_lookups(ctx);
  3511. }
  3512.  
  3513. static bool
  3514. error_check_bind_xfb_buffers(struct gl_context *ctx,
  3515.                              struct gl_transform_feedback_object *tfObj,
  3516.                              GLuint first, GLsizei count, const char *caller)
  3517. {
  3518.    if (!ctx->Extensions.EXT_transform_feedback) {
  3519.       _mesa_error(ctx, GL_INVALID_ENUM,
  3520.                   "%s(target=GL_TRANSFORM_FEEDBACK_BUFFER)", caller);
  3521.       return false;
  3522.    }
  3523.  
  3524.    /* Page 398 of the PDF of the OpenGL 4.4 (Core Profile) spec says:
  3525.     *
  3526.     *     "An INVALID_OPERATION error is generated :
  3527.     *
  3528.     *     ...
  3529.     *     • by BindBufferRange or BindBufferBase if target is TRANSFORM_-
  3530.     *       FEEDBACK_BUFFER and transform feedback is currently active."
  3531.     *
  3532.     * We assume that this is also meant to apply to BindBuffersRange
  3533.     * and BindBuffersBase.
  3534.     */
  3535.    if (tfObj->Active) {
  3536.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3537.                   "%s(Changing transform feedback buffers while "
  3538.                   "transform feedback is active)", caller);
  3539.       return false;
  3540.    }
  3541.  
  3542.    /* The ARB_multi_bind_spec says:
  3543.     *
  3544.     *     "An INVALID_OPERATION error is generated if <first> + <count> is
  3545.     *      greater than the number of target-specific indexed binding points,
  3546.     *      as described in section 6.7.1."
  3547.     */
  3548.    if (first + count > ctx->Const.MaxTransformFeedbackBuffers) {
  3549.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3550.                   "%s(first=%u + count=%d > the value of "
  3551.                   "GL_MAX_TRANSFORM_FEEDBACK_BUFFERS=%u)",
  3552.                   caller, first, count,
  3553.                   ctx->Const.MaxTransformFeedbackBuffers);
  3554.       return false;
  3555.    }
  3556.  
  3557.    return true;
  3558. }
  3559.  
  3560. /**
  3561.  * Unbind all transform feedback buffers in the range
  3562.  * <first> through <first>+<count>-1
  3563.  */
  3564. static void
  3565. unbind_xfb_buffers(struct gl_context *ctx,
  3566.                    struct gl_transform_feedback_object *tfObj,
  3567.                    GLuint first, GLsizei count)
  3568. {
  3569.    struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
  3570.    GLint i;
  3571.  
  3572.    for (i = 0; i < count; i++)
  3573.       _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
  3574.                                            bufObj, 0, 0);
  3575. }
  3576.  
  3577. static void
  3578. bind_xfb_buffers_base(struct gl_context *ctx,
  3579.                       GLuint first, GLsizei count,
  3580.                       const GLuint *buffers)
  3581. {
  3582.    struct gl_transform_feedback_object *tfObj =
  3583.       ctx->TransformFeedback.CurrentObject;
  3584.    GLint i;
  3585.  
  3586.    if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
  3587.                                      "glBindBuffersBase"))
  3588.       return;
  3589.  
  3590.    /* Assume that at least one binding will be changed */
  3591.    FLUSH_VERTICES(ctx, 0);
  3592.    ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
  3593.  
  3594.    if (!buffers) {
  3595.       /* The ARB_multi_bind spec says:
  3596.        *
  3597.        *   "If <buffers> is NULL, all bindings from <first> through
  3598.        *    <first>+<count>-1 are reset to their unbound (zero) state."
  3599.        */
  3600.       unbind_xfb_buffers(ctx, tfObj, first, count);
  3601.       return;
  3602.    }
  3603.  
  3604.    /* Note that the error semantics for multi-bind commands differ from
  3605.     * those of other GL commands.
  3606.     *
  3607.     * The Issues section in the ARB_multi_bind spec says:
  3608.     *
  3609.     *    "(11) Typically, OpenGL specifies that if an error is generated by a
  3610.     *          command, that command has no effect.  This is somewhat
  3611.     *          unfortunate for multi-bind commands, because it would require a
  3612.     *          first pass to scan the entire list of bound objects for errors
  3613.     *          and then a second pass to actually perform the bindings.
  3614.     *          Should we have different error semantics?
  3615.     *
  3616.     *       RESOLVED:  Yes.  In this specification, when the parameters for
  3617.     *       one of the <count> binding points are invalid, that binding point
  3618.     *       is not updated and an error will be generated.  However, other
  3619.     *       binding points in the same command will be updated if their
  3620.     *       parameters are valid and no other error occurs."
  3621.     */
  3622.  
  3623.    _mesa_begin_bufferobj_lookups(ctx);
  3624.  
  3625.    for (i = 0; i < count; i++) {
  3626.       struct gl_buffer_object * const boundBufObj = tfObj->Buffers[first + i];
  3627.       struct gl_buffer_object *bufObj;
  3628.  
  3629.       if (boundBufObj && boundBufObj->Name == buffers[i])
  3630.          bufObj = boundBufObj;
  3631.       else
  3632.          bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
  3633.                                                     "glBindBuffersBase");
  3634.  
  3635.       if (bufObj)
  3636.          _mesa_set_transform_feedback_binding(ctx, tfObj, first + i,
  3637.                                               bufObj, 0, 0);
  3638.    }
  3639.  
  3640.    _mesa_end_bufferobj_lookups(ctx);
  3641. }
  3642.  
  3643. static void
  3644. bind_xfb_buffers_range(struct gl_context *ctx,
  3645.                        GLuint first, GLsizei count,
  3646.                        const GLuint *buffers,
  3647.                        const GLintptr *offsets,
  3648.                        const GLsizeiptr *sizes)
  3649. {
  3650.    struct gl_transform_feedback_object *tfObj =
  3651.        ctx->TransformFeedback.CurrentObject;
  3652.    GLint i;
  3653.  
  3654.    if (!error_check_bind_xfb_buffers(ctx, tfObj, first, count,
  3655.                                      "glBindBuffersRange"))
  3656.       return;
  3657.  
  3658.    /* Assume that at least one binding will be changed */
  3659.    FLUSH_VERTICES(ctx, 0);
  3660.    ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
  3661.  
  3662.    if (!buffers) {
  3663.       /* The ARB_multi_bind spec says:
  3664.        *
  3665.        *    "If <buffers> is NULL, all bindings from <first> through
  3666.        *     <first>+<count>-1 are reset to their unbound (zero) state.
  3667.        *     In this case, the offsets and sizes associated with the
  3668.        *     binding points are set to default values, ignoring
  3669.        *     <offsets> and <sizes>."
  3670.        */
  3671.       unbind_xfb_buffers(ctx, tfObj, first, count);
  3672.       return;
  3673.    }
  3674.  
  3675.    /* Note that the error semantics for multi-bind commands differ from
  3676.     * those of other GL commands.
  3677.     *
  3678.     * The Issues section in the ARB_multi_bind spec says:
  3679.     *
  3680.     *    "(11) Typically, OpenGL specifies that if an error is generated by a
  3681.     *          command, that command has no effect.  This is somewhat
  3682.     *          unfortunate for multi-bind commands, because it would require a
  3683.     *          first pass to scan the entire list of bound objects for errors
  3684.     *          and then a second pass to actually perform the bindings.
  3685.     *          Should we have different error semantics?
  3686.     *
  3687.     *       RESOLVED:  Yes.  In this specification, when the parameters for
  3688.     *       one of the <count> binding points are invalid, that binding point
  3689.     *       is not updated and an error will be generated.  However, other
  3690.     *       binding points in the same command will be updated if their
  3691.     *       parameters are valid and no other error occurs."
  3692.     */
  3693.  
  3694.    _mesa_begin_bufferobj_lookups(ctx);
  3695.  
  3696.    for (i = 0; i < count; i++) {
  3697.       const GLuint index = first + i;
  3698.       struct gl_buffer_object * const boundBufObj = tfObj->Buffers[index];
  3699.       struct gl_buffer_object *bufObj;
  3700.  
  3701.       if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
  3702.          continue;
  3703.  
  3704.       /* The ARB_multi_bind spec says:
  3705.        *
  3706.        *     "An INVALID_VALUE error is generated by BindBuffersRange if any
  3707.        *      pair of values in <offsets> and <sizes> does not respectively
  3708.        *      satisfy the constraints described for those parameters for the
  3709.        *      specified target, as described in section 6.7.1 (per binding)."
  3710.        *
  3711.        * Section 6.7.1 refers to table 6.5, which says:
  3712.        *
  3713.        *     "┌───────────────────────────────────────────────────────────────┐
  3714.        *      │ Transform feedback array bindings (see sec. 13.2.2)           │
  3715.        *      ├───────────────────────┬───────────────────────────────────────┤
  3716.        *      │    ...                │    ...                                │
  3717.        *      │    offset restriction │    multiple of 4                      │
  3718.        *      │    ...                │    ...                                │
  3719.        *      │    size restriction   │    multiple of 4                      │
  3720.        *      └───────────────────────┴───────────────────────────────────────┘"
  3721.        */
  3722.       if (offsets[i] & 0x3) {
  3723.          _mesa_error(ctx, GL_INVALID_VALUE,
  3724.                      "glBindBuffersRange(offsets[%u]=%" PRId64
  3725.                      " is misaligned; it must be a multiple of 4 when "
  3726.                      "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
  3727.                      i, (int64_t) offsets[i]);
  3728.          continue;
  3729.       }
  3730.  
  3731.       if (sizes[i] & 0x3) {
  3732.          _mesa_error(ctx, GL_INVALID_VALUE,
  3733.                      "glBindBuffersRange(sizes[%u]=%" PRId64
  3734.                      " is misaligned; it must be a multiple of 4 when "
  3735.                      "target=GL_TRANSFORM_FEEDBACK_BUFFER)",
  3736.                      i, (int64_t) sizes[i]);
  3737.          continue;
  3738.       }
  3739.  
  3740.       if (boundBufObj && boundBufObj->Name == buffers[i])
  3741.          bufObj = boundBufObj;
  3742.       else
  3743.          bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
  3744.                                                     "glBindBuffersRange");
  3745.  
  3746.       if (bufObj)
  3747.          _mesa_set_transform_feedback_binding(ctx, tfObj, index, bufObj,
  3748.                                               offsets[i], sizes[i]);
  3749.    }
  3750.  
  3751.    _mesa_end_bufferobj_lookups(ctx);
  3752. }
  3753.  
  3754. static bool
  3755. error_check_bind_atomic_buffers(struct gl_context *ctx,
  3756.                                 GLuint first, GLsizei count,
  3757.                                 const char *caller)
  3758. {
  3759.    if (!ctx->Extensions.ARB_shader_atomic_counters) {
  3760.       _mesa_error(ctx, GL_INVALID_ENUM,
  3761.                   "%s(target=GL_ATOMIC_COUNTER_BUFFER)", caller);
  3762.       return false;
  3763.    }
  3764.  
  3765.    /* The ARB_multi_bind_spec says:
  3766.     *
  3767.     *     "An INVALID_OPERATION error is generated if <first> + <count> is
  3768.     *      greater than the number of target-specific indexed binding points,
  3769.     *      as described in section 6.7.1."
  3770.     */
  3771.    if (first + count > ctx->Const.MaxAtomicBufferBindings) {
  3772.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3773.                   "%s(first=%u + count=%d > the value of "
  3774.                   "GL_MAX_ATOMIC_BUFFER_BINDINGS=%u)",
  3775.                   caller, first, count, ctx->Const.MaxAtomicBufferBindings);
  3776.       return false;
  3777.    }
  3778.  
  3779.    return true;
  3780. }
  3781.  
  3782. /**
  3783.  * Unbind all atomic counter buffers in the range
  3784.  * <first> through <first>+<count>-1
  3785.  */
  3786. static void
  3787. unbind_atomic_buffers(struct gl_context *ctx, GLuint first, GLsizei count)
  3788. {
  3789.    struct gl_buffer_object * const bufObj = ctx->Shared->NullBufferObj;
  3790.    GLint i;
  3791.  
  3792.    for (i = 0; i < count; i++)
  3793.       set_atomic_buffer_binding(ctx, &ctx->AtomicBufferBindings[first + i],
  3794.                                 bufObj, -1, -1);
  3795. }
  3796.  
  3797. static void
  3798. bind_atomic_buffers_base(struct gl_context *ctx,
  3799.                          GLuint first,
  3800.                          GLsizei count,
  3801.                          const GLuint *buffers)
  3802. {
  3803.    GLint i;
  3804.  
  3805.    if (!error_check_bind_atomic_buffers(ctx, first, count,
  3806.                                         "glBindBuffersBase"))
  3807.      return;
  3808.  
  3809.    /* Assume that at least one binding will be changed */
  3810.    FLUSH_VERTICES(ctx, 0);
  3811.    ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
  3812.  
  3813.    if (!buffers) {
  3814.       /* The ARB_multi_bind spec says:
  3815.        *
  3816.        *   "If <buffers> is NULL, all bindings from <first> through
  3817.        *    <first>+<count>-1 are reset to their unbound (zero) state."
  3818.        */
  3819.       unbind_atomic_buffers(ctx, first, count);
  3820.       return;
  3821.    }
  3822.  
  3823.    /* Note that the error semantics for multi-bind commands differ from
  3824.     * those of other GL commands.
  3825.     *
  3826.     * The Issues section in the ARB_multi_bind spec says:
  3827.     *
  3828.     *    "(11) Typically, OpenGL specifies that if an error is generated by a
  3829.     *          command, that command has no effect.  This is somewhat
  3830.     *          unfortunate for multi-bind commands, because it would require a
  3831.     *          first pass to scan the entire list of bound objects for errors
  3832.     *          and then a second pass to actually perform the bindings.
  3833.     *          Should we have different error semantics?
  3834.     *
  3835.     *       RESOLVED:  Yes.  In this specification, when the parameters for
  3836.     *       one of the <count> binding points are invalid, that binding point
  3837.     *       is not updated and an error will be generated.  However, other
  3838.     *       binding points in the same command will be updated if their
  3839.     *       parameters are valid and no other error occurs."
  3840.     */
  3841.  
  3842.    _mesa_begin_bufferobj_lookups(ctx);
  3843.  
  3844.    for (i = 0; i < count; i++) {
  3845.       struct gl_atomic_buffer_binding *binding =
  3846.          &ctx->AtomicBufferBindings[first + i];
  3847.       struct gl_buffer_object *bufObj;
  3848.  
  3849.       if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
  3850.          bufObj = binding->BufferObject;
  3851.       else
  3852.          bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
  3853.                                                     "glBindBuffersBase");
  3854.  
  3855.       if (bufObj)
  3856.          set_atomic_buffer_binding(ctx, binding, bufObj, 0, 0);
  3857.    }
  3858.  
  3859.    _mesa_end_bufferobj_lookups(ctx);
  3860. }
  3861.  
  3862. static void
  3863. bind_atomic_buffers_range(struct gl_context *ctx,
  3864.                           GLuint first,
  3865.                           GLsizei count,
  3866.                           const GLuint *buffers,
  3867.                           const GLintptr *offsets,
  3868.                           const GLsizeiptr *sizes)
  3869. {
  3870.    GLint i;
  3871.  
  3872.    if (!error_check_bind_atomic_buffers(ctx, first, count,
  3873.                                         "glBindBuffersRange"))
  3874.      return;
  3875.  
  3876.    /* Assume that at least one binding will be changed */
  3877.    FLUSH_VERTICES(ctx, 0);
  3878.    ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
  3879.  
  3880.    if (!buffers) {
  3881.       /* The ARB_multi_bind spec says:
  3882.        *
  3883.        *    "If <buffers> is NULL, all bindings from <first> through
  3884.        *     <first>+<count>-1 are reset to their unbound (zero) state.
  3885.        *     In this case, the offsets and sizes associated with the
  3886.        *     binding points are set to default values, ignoring
  3887.        *     <offsets> and <sizes>."
  3888.        */
  3889.       unbind_atomic_buffers(ctx, first, count);
  3890.       return;
  3891.    }
  3892.  
  3893.    /* Note that the error semantics for multi-bind commands differ from
  3894.     * those of other GL commands.
  3895.     *
  3896.     * The Issues section in the ARB_multi_bind spec says:
  3897.     *
  3898.     *    "(11) Typically, OpenGL specifies that if an error is generated by a
  3899.     *          command, that command has no effect.  This is somewhat
  3900.     *          unfortunate for multi-bind commands, because it would require a
  3901.     *          first pass to scan the entire list of bound objects for errors
  3902.     *          and then a second pass to actually perform the bindings.
  3903.     *          Should we have different error semantics?
  3904.     *
  3905.     *       RESOLVED:  Yes.  In this specification, when the parameters for
  3906.     *       one of the <count> binding points are invalid, that binding point
  3907.     *       is not updated and an error will be generated.  However, other
  3908.     *       binding points in the same command will be updated if their
  3909.     *       parameters are valid and no other error occurs."
  3910.     */
  3911.  
  3912.    _mesa_begin_bufferobj_lookups(ctx);
  3913.  
  3914.    for (i = 0; i < count; i++) {
  3915.       struct gl_atomic_buffer_binding *binding =
  3916.          &ctx->AtomicBufferBindings[first + i];
  3917.       struct gl_buffer_object *bufObj;
  3918.  
  3919.       if (!bind_buffers_check_offset_and_size(ctx, i, offsets, sizes))
  3920.          continue;
  3921.  
  3922.       /* The ARB_multi_bind spec says:
  3923.        *
  3924.        *     "An INVALID_VALUE error is generated by BindBuffersRange if any
  3925.        *      pair of values in <offsets> and <sizes> does not respectively
  3926.        *      satisfy the constraints described for those parameters for the
  3927.        *      specified target, as described in section 6.7.1 (per binding)."
  3928.        *
  3929.        * Section 6.7.1 refers to table 6.5, which says:
  3930.        *
  3931.        *     "┌───────────────────────────────────────────────────────────────┐
  3932.        *      │ Atomic counter array bindings (see sec. 7.7.2)                │
  3933.        *      ├───────────────────────┬───────────────────────────────────────┤
  3934.        *      │    ...                │    ...                                │
  3935.        *      │    offset restriction │    multiple of 4                      │
  3936.        *      │    ...                │    ...                                │
  3937.        *      │    size restriction   │    none                               │
  3938.        *      └───────────────────────┴───────────────────────────────────────┘"
  3939.        */
  3940.       if (offsets[i] & (ATOMIC_COUNTER_SIZE - 1)) {
  3941.          _mesa_error(ctx, GL_INVALID_VALUE,
  3942.                      "glBindBuffersRange(offsets[%u]=%" PRId64
  3943.                      " is misaligned; it must be a multiple of %d when "
  3944.                      "target=GL_ATOMIC_COUNTER_BUFFER)",
  3945.                      i, (int64_t) offsets[i], ATOMIC_COUNTER_SIZE);
  3946.          continue;
  3947.       }
  3948.  
  3949.       if (binding->BufferObject && binding->BufferObject->Name == buffers[i])
  3950.          bufObj = binding->BufferObject;
  3951.       else
  3952.          bufObj = _mesa_multi_bind_lookup_bufferobj(ctx, buffers, i,
  3953.                                                     "glBindBuffersRange");
  3954.  
  3955.       if (bufObj)
  3956.          set_atomic_buffer_binding(ctx, binding, bufObj, offsets[i], sizes[i]);
  3957.    }
  3958.  
  3959.    _mesa_end_bufferobj_lookups(ctx);
  3960. }
  3961.  
  3962. void GLAPIENTRY
  3963. _mesa_BindBufferRange(GLenum target, GLuint index,
  3964.                       GLuint buffer, GLintptr offset, GLsizeiptr size)
  3965. {
  3966.    GET_CURRENT_CONTEXT(ctx);
  3967.    struct gl_buffer_object *bufObj;
  3968.  
  3969.    if (buffer == 0) {
  3970.       bufObj = ctx->Shared->NullBufferObj;
  3971.    } else {
  3972.       bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  3973.    }
  3974.    if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
  3975.                                      &bufObj, "glBindBufferRange"))
  3976.       return;
  3977.  
  3978.    if (!bufObj) {
  3979.       _mesa_error(ctx, GL_INVALID_OPERATION,
  3980.                   "glBindBufferRange(invalid buffer=%u)", buffer);
  3981.       return;
  3982.    }
  3983.  
  3984.    if (buffer != 0) {
  3985.       if (size <= 0) {
  3986.          _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
  3987.                      (int) size);
  3988.          return;
  3989.       }
  3990.    }
  3991.  
  3992.    switch (target) {
  3993.    case GL_TRANSFORM_FEEDBACK_BUFFER:
  3994.       _mesa_bind_buffer_range_transform_feedback(ctx,
  3995.                                                  ctx->TransformFeedback.CurrentObject,
  3996.                                                  index, bufObj, offset, size,
  3997.                                                  false);
  3998.       return;
  3999.    case GL_UNIFORM_BUFFER:
  4000.       bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
  4001.       return;
  4002.    case GL_ATOMIC_COUNTER_BUFFER:
  4003.       bind_atomic_buffer(ctx, index, bufObj, offset, size,
  4004.                          "glBindBufferRange");
  4005.       return;
  4006.    default:
  4007.       _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
  4008.       return;
  4009.    }
  4010. }
  4011.  
  4012. void GLAPIENTRY
  4013. _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
  4014. {
  4015.    GET_CURRENT_CONTEXT(ctx);
  4016.    struct gl_buffer_object *bufObj;
  4017.  
  4018.    if (buffer == 0) {
  4019.       bufObj = ctx->Shared->NullBufferObj;
  4020.    } else {
  4021.       bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  4022.    }
  4023.    if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
  4024.                                      &bufObj, "glBindBufferBase"))
  4025.       return;
  4026.  
  4027.    if (!bufObj) {
  4028.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4029.                   "glBindBufferBase(invalid buffer=%u)", buffer);
  4030.       return;
  4031.    }
  4032.  
  4033.    /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
  4034.     * regards to BindBufferBase.  It says (GL 3.1 core spec, page 63):
  4035.     *
  4036.     *     "BindBufferBase is equivalent to calling BindBufferRange with offset
  4037.     *      zero and size equal to the size of buffer."
  4038.     *
  4039.     * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
  4040.     *
  4041.     *     "If the parameter (starting offset or size) was not specified when the
  4042.     *      buffer object was bound, zero is returned."
  4043.     *
  4044.     * What happens if the size of the buffer changes?  Does the size of the
  4045.     * buffer at the moment glBindBufferBase was called still play a role, like
  4046.     * the first quote would imply, or is the size meaningless in the
  4047.     * glBindBufferBase case like the second quote would suggest?  The GL 4.1
  4048.     * core spec page 45 says:
  4049.     *
  4050.     *     "It is equivalent to calling BindBufferRange with offset zero, while
  4051.     *      size is determined by the size of the bound buffer at the time the
  4052.     *      binding is used."
  4053.     *
  4054.     * My interpretation is that the GL 4.1 spec was a clarification of the
  4055.     * behavior, not a change.  In particular, this choice will only make
  4056.     * rendering work in cases where it would have had undefined results.
  4057.     */
  4058.  
  4059.    switch (target) {
  4060.    case GL_TRANSFORM_FEEDBACK_BUFFER:
  4061.       _mesa_bind_buffer_base_transform_feedback(ctx,
  4062.                                                 ctx->TransformFeedback.CurrentObject,
  4063.                                                 index, bufObj, false);
  4064.       return;
  4065.    case GL_UNIFORM_BUFFER:
  4066.       bind_buffer_base_uniform_buffer(ctx, index, bufObj);
  4067.       return;
  4068.    case GL_ATOMIC_COUNTER_BUFFER:
  4069.       bind_atomic_buffer(ctx, index, bufObj, 0, 0,
  4070.                          "glBindBufferBase");
  4071.       return;
  4072.    default:
  4073.       _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
  4074.       return;
  4075.    }
  4076. }
  4077.  
  4078. void GLAPIENTRY
  4079. _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
  4080.                        const GLuint *buffers,
  4081.                        const GLintptr *offsets, const GLsizeiptr *sizes)
  4082. {
  4083.    GET_CURRENT_CONTEXT(ctx);
  4084.  
  4085.    switch (target) {
  4086.    case GL_TRANSFORM_FEEDBACK_BUFFER:
  4087.       bind_xfb_buffers_range(ctx, first, count, buffers, offsets, sizes);
  4088.       return;
  4089.    case GL_UNIFORM_BUFFER:
  4090.       bind_uniform_buffers_range(ctx, first, count, buffers, offsets, sizes);
  4091.       return;
  4092.    case GL_ATOMIC_COUNTER_BUFFER:
  4093.       bind_atomic_buffers_range(ctx, first, count, buffers,
  4094.                                 offsets, sizes);
  4095.       return;
  4096.    default:
  4097.       _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersRange(target=%s)",
  4098.                   _mesa_lookup_enum_by_nr(target));
  4099.       break;
  4100.    }
  4101. }
  4102.  
  4103. void GLAPIENTRY
  4104. _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
  4105.                       const GLuint *buffers)
  4106. {
  4107.    GET_CURRENT_CONTEXT(ctx);
  4108.  
  4109.    switch (target) {
  4110.    case GL_TRANSFORM_FEEDBACK_BUFFER:
  4111.       bind_xfb_buffers_base(ctx, first, count, buffers);
  4112.       return;
  4113.    case GL_UNIFORM_BUFFER:
  4114.       bind_uniform_buffers_base(ctx, first, count, buffers);
  4115.       return;
  4116.    case GL_ATOMIC_COUNTER_BUFFER:
  4117.       bind_atomic_buffers_base(ctx, first, count, buffers);
  4118.       return;
  4119.    default:
  4120.       _mesa_error(ctx, GL_INVALID_ENUM, "glBindBuffersBase(target=%s)",
  4121.                   _mesa_lookup_enum_by_nr(target));
  4122.       break;
  4123.    }
  4124. }
  4125.  
  4126. void GLAPIENTRY
  4127. _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
  4128.                               GLsizeiptr length)
  4129. {
  4130.    GET_CURRENT_CONTEXT(ctx);
  4131.    struct gl_buffer_object *bufObj;
  4132.    const GLintptr end = offset + length;
  4133.  
  4134.    bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  4135.    if (!bufObj) {
  4136.       _mesa_error(ctx, GL_INVALID_VALUE,
  4137.                   "glInvalidateBufferSubData(name = 0x%x) invalid object",
  4138.                   buffer);
  4139.       return;
  4140.    }
  4141.  
  4142.    /* The GL_ARB_invalidate_subdata spec says:
  4143.     *
  4144.     *     "An INVALID_VALUE error is generated if <offset> or <length> is
  4145.     *     negative, or if <offset> + <length> is greater than the value of
  4146.     *     BUFFER_SIZE."
  4147.     */
  4148.    if (end < 0 || end > bufObj->Size) {
  4149.       _mesa_error(ctx, GL_INVALID_VALUE,
  4150.                   "glInvalidateBufferSubData(invalid offset or length)");
  4151.       return;
  4152.    }
  4153.  
  4154.    /* The OpenGL 4.4 (Core Profile) spec says:
  4155.     *
  4156.     *     "An INVALID_OPERATION error is generated if buffer is currently
  4157.     *     mapped by MapBuffer or if the invalidate range intersects the range
  4158.     *     currently mapped by MapBufferRange, unless it was mapped
  4159.     *     with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
  4160.     */
  4161.    if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
  4162.        bufferobj_range_mapped(bufObj, offset, length)) {
  4163.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4164.                   "glInvalidateBufferSubData(intersection with mapped "
  4165.                   "range)");
  4166.       return;
  4167.    }
  4168.  
  4169.    /* We don't actually do anything for this yet.  Just return after
  4170.     * validating the parameters and generating the required errors.
  4171.     */
  4172.    return;
  4173. }
  4174.  
  4175. void GLAPIENTRY
  4176. _mesa_InvalidateBufferData(GLuint buffer)
  4177. {
  4178.    GET_CURRENT_CONTEXT(ctx);
  4179.    struct gl_buffer_object *bufObj;
  4180.  
  4181.    bufObj = _mesa_lookup_bufferobj(ctx, buffer);
  4182.    if (!bufObj) {
  4183.       _mesa_error(ctx, GL_INVALID_VALUE,
  4184.                   "glInvalidateBufferData(name = 0x%x) invalid object",
  4185.                   buffer);
  4186.       return;
  4187.    }
  4188.  
  4189.    /* The OpenGL 4.4 (Core Profile) spec says:
  4190.     *
  4191.     *     "An INVALID_OPERATION error is generated if buffer is currently
  4192.     *     mapped by MapBuffer or if the invalidate range intersects the range
  4193.     *     currently mapped by MapBufferRange, unless it was mapped
  4194.     *     with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
  4195.     */
  4196.    if (_mesa_check_disallowed_mapping(bufObj)) {
  4197.       _mesa_error(ctx, GL_INVALID_OPERATION,
  4198.                   "glInvalidateBufferData(intersection with mapped "
  4199.                   "range)");
  4200.       return;
  4201.    }
  4202.  
  4203.    /* We don't actually do anything for this yet.  Just return after
  4204.     * validating the parameters and generating the required errors.
  4205.     */
  4206.    return;
  4207. }
  4208.