| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
| 3 | * Version: 5.1 |
| 4 | * |
| 5 | * Copyright (C) 1999-2003 Brian Paul 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 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * \file bufferobj.c |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 28 | * \brief Functions for the GL_ARB_vertex_buffer_object extension. |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 29 | * \author Brian Paul, Ian Romanick |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | |
| 33 | #include "glheader.h" |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 34 | #include "hash.h" |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 35 | #include "imports.h" |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 36 | #include "context.h" |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 37 | #include "bufferobj.h" |
| 38 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 39 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Get the buffer object bound to the specified target in a GL context. |
| 42 | * |
| 43 | * \param ctx GL context |
| 44 | * \param target Buffer object target to be retrieved. Currently this must |
| 45 | * be either \c GL_ARRAY_BUFFER or \c GL_ELEMENT_ARRAY_BUFFER. |
| 46 | * \param str Name of caller for logging errors. |
| 47 | * \return A pointer to the buffer object bound to \c target in the |
| 48 | * specified context or \c NULL if \c target is invalid or no |
| 49 | * buffer object is bound. |
| 50 | */ |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 51 | static INLINE struct gl_buffer_object * |
| 52 | buffer_object_get_target( GLcontext *ctx, GLenum target, const char * str ) |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 53 | { |
| 54 | struct gl_buffer_object * bufObj = NULL; |
| 55 | |
| 56 | switch (target) { |
| 57 | case GL_ARRAY_BUFFER_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 58 | bufObj = ctx->Array.ArrayBufferObj; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 59 | break; |
| 60 | case GL_ELEMENT_ARRAY_BUFFER_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 61 | bufObj = ctx->Array.ElementArrayBufferObj; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 62 | break; |
| 63 | default: |
| 64 | _mesa_error(ctx, GL_INVALID_ENUM, "gl%s(target)", str); |
| Brian Paul | 4b6f6e1 | 2003-10-14 14:49:39 +0000 | [diff] [blame] | 65 | return NULL; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 66 | } |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 67 | |
| 68 | if (bufObj->Name == 0) |
| 69 | return NULL; |
| 70 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 71 | return bufObj; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * Tests the subdata range parameters and sets the GL error code for |
| 77 | * \c glBufferSubDataARB and \c glGetBufferSubDataARB. |
| 78 | * |
| 79 | * \param ctx GL context. |
| 80 | * \param target Buffer object target on which to operate. |
| 81 | * \param offset Offset of the first byte of the subdata range. |
| 82 | * \param size Size, in bytes, of the subdata range. |
| 83 | * \param str Name of caller for logging errors. |
| 84 | * \return A pointer to the buffer object bound to \c target in the |
| 85 | * specified context or \c NULL if any of the parameter or state |
| 86 | * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB |
| 87 | * are invalid. |
| 88 | * |
| 89 | * \sa glBufferSubDataARB, glGetBufferSubDataARB |
| 90 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 91 | static struct gl_buffer_object * |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 92 | buffer_object_subdata_range_good( GLcontext * ctx, GLenum target, |
| 93 | GLintptrARB offset, GLsizeiptrARB size, |
| 94 | const char * str ) |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 95 | { |
| 96 | struct gl_buffer_object *bufObj; |
| 97 | |
| 98 | if (size < 0) { |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 99 | _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", str); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | if (offset < 0) { |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 104 | _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", str); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
| 107 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 108 | bufObj = buffer_object_get_target( ctx, target, str ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 109 | if ( bufObj == NULL ) { |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| Karl Schultz | df8d337 | 2003-09-18 15:14:10 +0000 | [diff] [blame] | 113 | if ( (GLuint)(offset + size) > bufObj->Size ) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 114 | _mesa_error(ctx, GL_INVALID_VALUE, |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 115 | "%s(size + offset > buffer size)", str); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 116 | return NULL; |
| 117 | } |
| 118 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 119 | if ( bufObj->Pointer != NULL ) { |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 120 | _mesa_error(ctx, GL_INVALID_OPERATION, "%s", str); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | return bufObj; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /** |
| 129 | * Allocate and initialize a new buffer object. |
| 130 | * |
| 131 | * This function is intended to be called via |
| 132 | * \c dd_function_table::NewBufferObject. |
| 133 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 134 | struct gl_buffer_object * |
| 135 | _mesa_new_buffer_object( GLcontext *ctx, GLuint name, GLenum target ) |
| 136 | { |
| 137 | struct gl_buffer_object *obj; |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 138 | obj = MALLOC_STRUCT(gl_buffer_object); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 139 | _mesa_initialize_buffer_object(obj, name, target); |
| 140 | return obj; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 145 | * Delete a buffer object. |
| 146 | * |
| 147 | * This function is intended to be called via |
| 148 | * \c dd_function_table::DeleteBufferObject. |
| 149 | */ |
| 150 | void |
| 151 | _mesa_delete_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj ) |
| 152 | { |
| 153 | if (bufObj->Data) |
| 154 | _mesa_free(bufObj->Data); |
| 155 | _mesa_free(bufObj); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 160 | * Initialize a buffer object to default values. |
| 161 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 162 | void |
| 163 | _mesa_initialize_buffer_object( struct gl_buffer_object *obj, |
| 164 | GLuint name, GLenum target ) |
| 165 | { |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 166 | _mesa_bzero(obj, sizeof(struct gl_buffer_object)); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 167 | obj->RefCount = 1; |
| 168 | obj->Name = name; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Add the given buffer object to the buffer object pool. |
| 174 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 175 | void |
| 176 | _mesa_save_buffer_object( GLcontext *ctx, struct gl_buffer_object *obj ) |
| 177 | { |
| 178 | if (obj->Name > 0) { |
| 179 | /* insert into hash table */ |
| 180 | _mesa_HashInsert(ctx->Shared->BufferObjects, obj->Name, obj); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * Remove the given buffer object from the buffer object pool. |
| 187 | * Do not deallocate the buffer object though. |
| 188 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 189 | void |
| 190 | _mesa_remove_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj ) |
| 191 | { |
| 192 | if (bufObj->Name > 0) { |
| 193 | /* remove from hash table */ |
| 194 | _mesa_HashRemove(ctx->Shared->BufferObjects, bufObj->Name); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Allocate space for and store data in a buffer object. Any data that was |
| 201 | * previously stored in the buffer object is lost. If \c data is \c NULL, |
| 202 | * memory will be allocated, but no copy will occur. |
| 203 | * |
| 204 | * This function is intended to be called via |
| 205 | * \c dd_function_table::BufferData. This function need not set GL error |
| 206 | * codes. The input parameters will have been tested before calling. |
| 207 | * |
| 208 | * \param ctx GL context. |
| 209 | * \param target Buffer object target on which to operate. |
| 210 | * \param size Size, in bytes, of the new data store. |
| 211 | * \param data Pointer to the data to store in the buffer object. This |
| 212 | * pointer may be \c NULL. |
| 213 | * \param usage Hints about how the data will be used. |
| 214 | * \param bufObj Object to be used. |
| 215 | * |
| 216 | * \sa glBufferDataARB, dd_function_table::BufferData. |
| 217 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 218 | void |
| 219 | _mesa_buffer_data( GLcontext *ctx, GLenum target, GLsizeiptrARB size, |
| 220 | const GLvoid * data, GLenum usage, |
| 221 | struct gl_buffer_object * bufObj ) |
| 222 | { |
| 223 | void * new_data; |
| 224 | |
| 225 | (void) target; |
| 226 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 227 | new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 228 | if ( new_data != NULL ) { |
| Brian Paul | e4fcea2 | 2003-09-19 15:38:15 +0000 | [diff] [blame] | 229 | bufObj->Data = (GLubyte *) new_data; |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 230 | bufObj->Size = size; |
| 231 | bufObj->Usage = usage; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 232 | |
| 233 | if ( data != NULL ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 234 | _mesa_memcpy( bufObj->Data, data, size ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | * Replace data in a subrange of buffer object. If the data range |
| 242 | * specified by \c size + \c offset extends beyond the end of the buffer or |
| 243 | * if \c data is \c NULL, no copy is performed. |
| 244 | * |
| 245 | * This function is intended to be called by |
| 246 | * \c dd_function_table::BufferSubData. This function need not set GL error |
| 247 | * codes. The input parameters will have been tested before calling. |
| 248 | * |
| 249 | * \param ctx GL context. |
| 250 | * \param target Buffer object target on which to operate. |
| 251 | * \param offset Offset of the first byte to be modified. |
| 252 | * \param size Size, in bytes, of the data range. |
| 253 | * \param data Pointer to the data to store in the buffer object. |
| 254 | * \param bufObj Object to be used. |
| 255 | * |
| 256 | * \sa glBufferSubDataARB, dd_function_table::BufferSubData. |
| 257 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 258 | void |
| 259 | _mesa_buffer_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset, |
| 260 | GLsizeiptrARB size, const GLvoid * data, |
| 261 | struct gl_buffer_object * bufObj ) |
| 262 | { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 263 | if ( (bufObj->Data != NULL) |
| Karl Schultz | df8d337 | 2003-09-18 15:14:10 +0000 | [diff] [blame] | 264 | && ((GLuint)(size + offset) <= bufObj->Size) ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 265 | _mesa_memcpy( (GLubyte *) bufObj->Data + offset, data, size ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | |
| 270 | /** |
| 271 | * Retrieve data from a subrange of buffer object. If the data range |
| 272 | * specified by \c size + \c offset extends beyond the end of the buffer or |
| 273 | * if \c data is \c NULL, no copy is performed. |
| 274 | * |
| 275 | * This function is intended to be called by |
| 276 | * \c dd_function_table::BufferGetSubData. This function need not set GL error |
| 277 | * codes. The input parameters will have been tested before calling. |
| 278 | * |
| 279 | * \param ctx GL context. |
| 280 | * \param target Buffer object target on which to operate. |
| 281 | * \param offset Offset of the first byte to be modified. |
| 282 | * \param size Size, in bytes, of the data range. |
| 283 | * \param data Pointer to the data to store in the buffer object. |
| 284 | * \param bufObj Object to be used. |
| 285 | * |
| 286 | * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData. |
| 287 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 288 | void |
| 289 | _mesa_buffer_get_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset, |
| 290 | GLsizeiptrARB size, GLvoid * data, |
| 291 | struct gl_buffer_object * bufObj ) |
| 292 | { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 293 | if ( (bufObj->Data != NULL) |
| Karl Schultz | df8d337 | 2003-09-18 15:14:10 +0000 | [diff] [blame] | 294 | && ((GLuint)(size + offset) <= bufObj->Size) ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 295 | _mesa_memcpy( data, (GLubyte *) bufObj->Data + offset, size ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /** |
| 301 | * Maps the private data buffer into the processor's address space. |
| 302 | * |
| 303 | * This function is intended to be called by \c dd_function_table::MapBuffer. |
| 304 | * This function need not set GL error codes. The input parameters will have |
| 305 | * been tested before calling. |
| 306 | * |
| 307 | * \param ctx GL context. |
| 308 | * \param target Buffer object target on which to operate. |
| 309 | * \param access Information about how the buffer will be accessed. |
| 310 | * \param bufObj Object to be used. |
| 311 | * \return A pointer to the object's internal data store that can be accessed |
| 312 | * by the processor |
| 313 | * |
| 314 | * \sa glMapBufferARB, dd_function_table::MapBuffer |
| 315 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 316 | void * |
| 317 | _mesa_buffer_map( GLcontext *ctx, GLenum target, GLenum access, |
| 318 | struct gl_buffer_object * bufObj ) |
| 319 | { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 320 | return bufObj->Data; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 323 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 324 | /** |
| 325 | * Initialize the state associated with buffer objects |
| 326 | */ |
| 327 | void |
| 328 | _mesa_init_buffer_objects( GLcontext *ctx ) |
| 329 | { |
| 330 | GLuint i; |
| 331 | |
| 332 | ctx->Array.NullBufferObj = _mesa_new_buffer_object(ctx, 0, 0); |
| 333 | ctx->Array.ArrayBufferObj = ctx->Array.NullBufferObj; |
| 334 | ctx->Array.ElementArrayBufferObj = ctx->Array.NullBufferObj; |
| 335 | |
| 336 | /* Vertex array buffers */ |
| 337 | ctx->Array.Vertex.BufferObj = ctx->Array.NullBufferObj; |
| 338 | ctx->Array.Normal.BufferObj = ctx->Array.NullBufferObj; |
| 339 | ctx->Array.Color.BufferObj = ctx->Array.NullBufferObj; |
| 340 | ctx->Array.SecondaryColor.BufferObj = ctx->Array.NullBufferObj; |
| 341 | ctx->Array.FogCoord.BufferObj = ctx->Array.NullBufferObj; |
| 342 | ctx->Array.Index.BufferObj = ctx->Array.NullBufferObj; |
| 343 | for (i = 0; i < MAX_TEXTURE_UNITS; i++) { |
| 344 | ctx->Array.TexCoord[i].BufferObj = ctx->Array.NullBufferObj; |
| 345 | } |
| 346 | ctx->Array.EdgeFlag.BufferObj = ctx->Array.NullBufferObj; |
| 347 | for (i = 0; i < VERT_ATTRIB_MAX; i++) { |
| 348 | ctx->Array.VertexAttrib[i].BufferObj = ctx->Array.NullBufferObj; |
| 349 | } |
| Brian Paul | 0f85b91 | 2003-10-19 15:10:36 +0000 | [diff] [blame] | 350 | |
| 351 | /* Device drivers might override these assignments after the Mesa |
| 352 | * context is initialized. |
| 353 | */ |
| 354 | ctx->Driver.NewBufferObject = _mesa_new_buffer_object; |
| 355 | ctx->Driver.DeleteBuffer = _mesa_delete_buffer_object; |
| 356 | ctx->Driver.BindBuffer = NULL; |
| 357 | ctx->Driver.BufferData = _mesa_buffer_data; |
| 358 | ctx->Driver.BufferSubData = _mesa_buffer_subdata; |
| 359 | ctx->Driver.GetBufferSubData = _mesa_buffer_get_subdata; |
| 360 | ctx->Driver.MapBuffer = _mesa_buffer_map; |
| 361 | ctx->Driver.UnmapBuffer = NULL; |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
| 365 | |
| 366 | /**********************************************************************/ |
| 367 | /* API Functions */ |
| 368 | /**********************************************************************/ |
| 369 | |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 370 | void |
| 371 | _mesa_BindBufferARB(GLenum target, GLuint buffer) |
| 372 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 373 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 374 | struct gl_buffer_object *oldBufObj; |
| 375 | struct gl_buffer_object *newBufObj = 0; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 376 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 377 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 378 | oldBufObj = buffer_object_get_target( ctx, target, "BindBufferARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 379 | if ( (oldBufObj != NULL) && (oldBufObj->Name == buffer) ) |
| 380 | return; /* rebinding the same buffer object- no change */ |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 381 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 382 | /* |
| 383 | * Get pointer to new buffer object (newBufObj) |
| 384 | */ |
| 385 | if ( buffer == 0 ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 386 | /* The spec says there's not a buffer object named 0, but we use |
| 387 | * one internally because it simplifies things. |
| 388 | */ |
| 389 | newBufObj = ctx->Array.NullBufferObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 390 | } |
| 391 | else { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 392 | /* non-default buffer object */ |
| 393 | const struct _mesa_HashTable *hash = ctx->Shared->BufferObjects; |
| 394 | newBufObj = (struct gl_buffer_object *) _mesa_HashLookup(hash, buffer); |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 395 | if (!newBufObj) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 396 | /* if this is a new buffer object id, allocate a buffer object now */ |
| 397 | newBufObj = (*ctx->Driver.NewBufferObject)(ctx, buffer, target); |
| 398 | if (!newBufObj) { |
| 399 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB"); |
| 400 | return; |
| 401 | } |
| 402 | _mesa_save_buffer_object(ctx, newBufObj); |
| 403 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 404 | newBufObj->RefCount++; |
| 405 | } |
| 406 | |
| 407 | switch (target) { |
| 408 | case GL_ARRAY_BUFFER_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 409 | ctx->Array.ArrayBufferObj = newBufObj; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 410 | break; |
| 411 | case GL_ELEMENT_ARRAY_BUFFER_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 412 | ctx->Array.ElementArrayBufferObj = newBufObj; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | |
| 416 | /* Pass BindBuffer call to device driver */ |
| 417 | if ( (ctx->Driver.BindBuffer != NULL) && (newBufObj != NULL) ) |
| 418 | (*ctx->Driver.BindBuffer)( ctx, target, newBufObj ); |
| 419 | |
| 420 | if ( oldBufObj != NULL ) { |
| 421 | oldBufObj->RefCount--; |
| 422 | assert(oldBufObj->RefCount >= 0); |
| 423 | if (oldBufObj->RefCount == 0) { |
| 424 | assert(oldBufObj->Name != 0); |
| 425 | _mesa_remove_buffer_object(ctx, oldBufObj); |
| 426 | ASSERT(ctx->Driver.DeleteBuffer); |
| 427 | (*ctx->Driver.DeleteBuffer)( ctx, oldBufObj ); |
| 428 | } |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 429 | } |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 432 | |
| 433 | /** |
| 434 | * Delete a set of buffer objects. |
| 435 | * |
| 436 | * \param n Number of buffer objects to delete. |
| 437 | * \param buffer Array of \c n buffer object IDs. |
| 438 | */ |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 439 | void |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 440 | _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids) |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 441 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 442 | GET_CURRENT_CONTEXT(ctx); |
| Karl Schultz | df8d337 | 2003-09-18 15:14:10 +0000 | [diff] [blame] | 443 | GLsizei i; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 444 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 445 | |
| 446 | if (n < 0) { |
| 447 | _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)"); |
| 448 | return; |
| 449 | } |
| 450 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 451 | _glthread_LOCK_MUTEX(ctx->Shared->Mutex); |
| 452 | |
| 453 | for (i = 0; i < n; i++) { |
| 454 | if (ids[i] != 0) { |
| 455 | struct gl_buffer_object *bufObj = (struct gl_buffer_object *) |
| 456 | _mesa_HashLookup(ctx->Shared->BufferObjects, ids[i]); |
| 457 | if (bufObj) { |
| Brian Paul | 6296276 | 2003-09-17 18:58:09 +0000 | [diff] [blame] | 458 | /* unbind any vertex pointers bound to this buffer */ |
| 459 | GLuint j; |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 460 | |
| 461 | ASSERT(bufObj->Name != 0); |
| 462 | |
| Brian Paul | 6296276 | 2003-09-17 18:58:09 +0000 | [diff] [blame] | 463 | if (ctx->Array.Vertex.BufferObj == bufObj) |
| 464 | ctx->Array.Vertex.BufferObj = ctx->Array.NullBufferObj; |
| 465 | if (ctx->Array.Normal.BufferObj == bufObj) |
| 466 | ctx->Array.Normal.BufferObj = ctx->Array.NullBufferObj; |
| 467 | if (ctx->Array.Color.BufferObj == bufObj) |
| 468 | ctx->Array.Color.BufferObj = ctx->Array.NullBufferObj; |
| 469 | if (ctx->Array.SecondaryColor.BufferObj == bufObj) |
| 470 | ctx->Array.SecondaryColor.BufferObj = ctx->Array.NullBufferObj; |
| 471 | if (ctx->Array.FogCoord.BufferObj == bufObj) |
| 472 | ctx->Array.FogCoord.BufferObj = ctx->Array.NullBufferObj; |
| 473 | if (ctx->Array.Index.BufferObj == bufObj) |
| 474 | ctx->Array.Index.BufferObj = ctx->Array.NullBufferObj; |
| 475 | if (ctx->Array.EdgeFlag.BufferObj == bufObj) |
| 476 | ctx->Array.EdgeFlag.BufferObj = ctx->Array.NullBufferObj; |
| 477 | for (j = 0; j < MAX_TEXTURE_UNITS; j++) { |
| 478 | if (ctx->Array.TexCoord[j].BufferObj == bufObj) |
| 479 | ctx->Array.TexCoord[j].BufferObj = ctx->Array.NullBufferObj; |
| 480 | } |
| 481 | for (j = 0; j < VERT_ATTRIB_MAX; j++) { |
| 482 | if (ctx->Array.VertexAttrib[j].BufferObj == bufObj) |
| 483 | ctx->Array.VertexAttrib[j].BufferObj = ctx->Array.NullBufferObj; |
| 484 | } |
| 485 | |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 486 | /* if deleting bound buffers, rebind to zero */ |
| 487 | if (ctx->Array.ArrayBufferObj == bufObj) { |
| 488 | _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 489 | } |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 490 | if (ctx->Array.ElementArrayBufferObj == bufObj) { |
| 491 | _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 492 | } |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 493 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 494 | bufObj->RefCount--; |
| 495 | if (bufObj->RefCount <= 0) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 496 | _mesa_remove_buffer_object(ctx, bufObj); |
| 497 | ASSERT(ctx->Driver.DeleteBuffer); |
| 498 | (*ctx->Driver.DeleteBuffer)(ctx, bufObj); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 507 | |
| 508 | /** |
| 509 | * Generate a set of unique buffer object IDs and store them in \c buffer. |
| 510 | * |
| 511 | * \param n Number of IDs to generate. |
| 512 | * \param buffer Array of \c n locations to store the IDs. |
| 513 | */ |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 514 | void |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 515 | _mesa_GenBuffersARB(GLsizei n, GLuint *buffer) |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 516 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 517 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 518 | GLuint first; |
| 519 | GLint i; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 520 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 521 | |
| 522 | if (n < 0) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 523 | _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB"); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 524 | return; |
| 525 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 526 | |
| 527 | if ( buffer == NULL ) { |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * This must be atomic (generation and allocation of buffer object IDs) |
| 533 | */ |
| 534 | _glthread_LOCK_MUTEX(ctx->Shared->Mutex); |
| 535 | |
| 536 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n); |
| 537 | |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 538 | /* Allocate new, empty buffer objects and return identifiers */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 539 | for (i = 0; i < n; i++) { |
| 540 | struct gl_buffer_object *bufObj; |
| 541 | GLuint name = first + i; |
| 542 | GLenum target = 0; |
| 543 | bufObj = (*ctx->Driver.NewBufferObject)( ctx, name, target ); |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 544 | if (!bufObj) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 545 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenBuffersARB"); |
| 546 | return; |
| 547 | } |
| 548 | _mesa_save_buffer_object(ctx, bufObj); |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 549 | buffer[i] = first + i; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 555 | |
| 556 | /** |
| 557 | * Determine if ID is the name of a buffer object. |
| 558 | * |
| 559 | * \param id ID of the potential buffer object. |
| 560 | * \return \c GL_TRUE if \c id is the name of a buffer object, |
| 561 | * \c GL_FALSE otherwise. |
| 562 | */ |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 563 | GLboolean |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 564 | _mesa_IsBufferARB(GLuint id) |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 565 | { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 566 | struct gl_buffer_object * bufObj; |
| 567 | GET_CURRENT_CONTEXT(ctx); |
| 568 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 569 | |
| 570 | if (id == 0) |
| 571 | return GL_FALSE; |
| 572 | |
| 573 | _glthread_LOCK_MUTEX(ctx->Shared->Mutex); |
| 574 | bufObj = (struct gl_buffer_object *) _mesa_HashLookup(ctx->Shared->BufferObjects, id); |
| 575 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| 576 | |
| 577 | return (bufObj != NULL); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 580 | |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 581 | void |
| 582 | _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size, |
| 583 | const GLvoid * data, GLenum usage) |
| 584 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 585 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 586 | struct gl_buffer_object *bufObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 587 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 588 | |
| 589 | if (size < 0) { |
| 590 | _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)"); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | switch (usage) { |
| 595 | case GL_STREAM_DRAW_ARB: |
| 596 | case GL_STREAM_READ_ARB: |
| 597 | case GL_STREAM_COPY_ARB: |
| 598 | case GL_STATIC_DRAW_ARB: |
| 599 | case GL_STATIC_READ_ARB: |
| 600 | case GL_STATIC_COPY_ARB: |
| 601 | case GL_DYNAMIC_DRAW_ARB: |
| 602 | case GL_DYNAMIC_READ_ARB: |
| 603 | case GL_DYNAMIC_COPY_ARB: |
| 604 | /* OK */ |
| 605 | break; |
| 606 | default: |
| 607 | _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)"); |
| 608 | return; |
| 609 | } |
| 610 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 611 | bufObj = buffer_object_get_target( ctx, target, "BufferDataARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 612 | if ( bufObj == NULL ) { |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 613 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB" ); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 614 | return; |
| 615 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 616 | |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 617 | if (bufObj->Pointer) { |
| Brian Paul | ce8e13d | 2003-10-21 14:54:16 +0000 | [diff] [blame^] | 618 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB(buffer is mapped)" ); |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 619 | return; |
| 620 | } |
| 621 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 622 | ASSERT(ctx->Driver.BufferData); |
| 623 | |
| 624 | /* Give the buffer object to the driver! <data> may be null! */ |
| 625 | (*ctx->Driver.BufferData)( ctx, target, size, data, usage, bufObj ); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 628 | |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 629 | void |
| 630 | _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset, |
| 631 | GLsizeiptrARB size, const GLvoid * data) |
| 632 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 633 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 634 | struct gl_buffer_object *bufObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 635 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 636 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 637 | bufObj = buffer_object_subdata_range_good( ctx, target, offset, size, |
| Brian Paul | 49aefce | 2003-10-15 20:50:41 +0000 | [diff] [blame] | 638 | "BufferSubDataARB" ); |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 639 | if (!bufObj) { |
| 640 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferSubDataARB" ); |
| 641 | return; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 642 | } |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 643 | |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 644 | if (bufObj->Pointer) { |
| 645 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferSubDataARB(buffer is mapped)" ); |
| 646 | return; |
| 647 | } |
| 648 | |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 649 | ASSERT(ctx->Driver.BufferSubData); |
| 650 | (*ctx->Driver.BufferSubData)( ctx, target, offset, size, data, bufObj ); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 653 | |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 654 | void |
| 655 | _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset, |
| 656 | GLsizeiptrARB size, void * data) |
| 657 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 658 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 659 | struct gl_buffer_object *bufObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 660 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 661 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 662 | bufObj = buffer_object_subdata_range_good( ctx, target, offset, size, |
| Brian Paul | 49aefce | 2003-10-15 20:50:41 +0000 | [diff] [blame] | 663 | "GetBufferSubDataARB" ); |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 664 | if (!bufObj) { |
| 665 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferSubDataARB" ); |
| 666 | return; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 667 | } |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 668 | |
| 669 | if (bufObj->Pointer) { |
| 670 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferSubDataARB(buffer is mapped)" ); |
| 671 | return; |
| 672 | } |
| 673 | |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 674 | ASSERT(ctx->Driver.GetBufferSubData); |
| 675 | (*ctx->Driver.GetBufferSubData)( ctx, target, offset, size, data, bufObj ); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 678 | |
| Brian Paul | ea31ca4 | 2003-05-10 04:35:09 +0000 | [diff] [blame] | 679 | void * |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 680 | _mesa_MapBufferARB(GLenum target, GLenum access) |
| 681 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 682 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 683 | struct gl_buffer_object * bufObj; |
| Brian Paul | ea31ca4 | 2003-05-10 04:35:09 +0000 | [diff] [blame] | 684 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 685 | |
| 686 | switch (access) { |
| 687 | case GL_READ_ONLY_ARB: |
| 688 | case GL_WRITE_ONLY_ARB: |
| 689 | case GL_READ_WRITE_ARB: |
| 690 | /* OK */ |
| 691 | break; |
| 692 | default: |
| 693 | _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)"); |
| Brian Paul | ea31ca4 | 2003-05-10 04:35:09 +0000 | [diff] [blame] | 694 | return NULL; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 697 | bufObj = buffer_object_get_target( ctx, target, "MapBufferARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 698 | if ( bufObj == NULL ) { |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 699 | _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB" ); |
| Brian Paul | ea31ca4 | 2003-05-10 04:35:09 +0000 | [diff] [blame] | 700 | return NULL; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 701 | } |
| Brian Paul | ea31ca4 | 2003-05-10 04:35:09 +0000 | [diff] [blame] | 702 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 703 | if ( bufObj->Pointer != NULL ) { |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 704 | _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)"); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 705 | return NULL; |
| 706 | } |
| 707 | |
| 708 | ASSERT(ctx->Driver.MapBuffer); |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 709 | bufObj->Pointer = (*ctx->Driver.MapBuffer)( ctx, target, access, bufObj ); |
| 710 | if ( bufObj->Pointer == NULL ) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 711 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(access)"); |
| 712 | } |
| 713 | |
| Brian Paul | 0bb281b | 2003-10-14 15:48:39 +0000 | [diff] [blame] | 714 | bufObj->Access = access; |
| 715 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 716 | return bufObj->Pointer; |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 719 | |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 720 | GLboolean |
| 721 | _mesa_UnmapBufferARB(GLenum target) |
| 722 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 723 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 724 | struct gl_buffer_object *bufObj; |
| 725 | GLboolean status = GL_TRUE; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 726 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 727 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 728 | bufObj = buffer_object_get_target( ctx, target, "UnmapBufferARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 729 | if ( bufObj == NULL ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 730 | _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB" ); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 731 | return GL_FALSE; |
| 732 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 733 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 734 | if ( bufObj->Pointer == NULL ) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 735 | _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB"); |
| 736 | return GL_FALSE; |
| 737 | } |
| 738 | |
| 739 | if ( ctx->Driver.UnmapBuffer != NULL ) { |
| 740 | status = (*ctx->Driver.UnmapBuffer)( ctx, target, bufObj ); |
| 741 | } |
| 742 | |
| Brian Paul | 0bb281b | 2003-10-14 15:48:39 +0000 | [diff] [blame] | 743 | bufObj->Access = GL_READ_WRITE_ARB; /* initial value, OK? */ |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 744 | bufObj->Pointer = NULL; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 745 | |
| 746 | return status; |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 749 | |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 750 | void |
| 751 | _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params) |
| 752 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 753 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 754 | struct gl_buffer_object *bufObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 755 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 756 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 757 | bufObj = buffer_object_get_target( ctx, target, "GetBufferParameterivARB" ); |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 758 | if (!bufObj) { |
| 759 | _mesa_error(ctx, GL_INVALID_OPERATION, "GetBufferParameterivARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 763 | switch (pname) { |
| 764 | case GL_BUFFER_SIZE_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 765 | *params = bufObj->Size; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 766 | break; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 767 | case GL_BUFFER_USAGE_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 768 | *params = bufObj->Usage; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 769 | break; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 770 | case GL_BUFFER_ACCESS_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 771 | *params = bufObj->Access; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 772 | break; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 773 | case GL_BUFFER_MAPPED_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 774 | *params = (bufObj->Pointer != NULL); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 775 | break; |
| 776 | default: |
| 777 | _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname)"); |
| 778 | return; |
| 779 | } |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 782 | |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 783 | void |
| 784 | _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params) |
| 785 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 786 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 787 | struct gl_buffer_object * bufObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 788 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 789 | |
| 790 | if (pname != GL_BUFFER_MAP_POINTER_ARB) { |
| 791 | _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)"); |
| 792 | return; |
| 793 | } |
| 794 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 795 | bufObj = buffer_object_get_target( ctx, target, "GetBufferPointervARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 796 | if ( bufObj == NULL ) { |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 797 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" ); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 798 | return; |
| 799 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 800 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 801 | *params = bufObj->Pointer; |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 802 | } |