| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
| Brian Paul | 85ad44b | 2004-02-13 14:04:26 +0000 | [diff] [blame] | 3 | * Version: 6.1 |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 4 | * |
| Brian Paul | 85ad44b | 2004-02-13 14:04:26 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 Brian Paul All Rights Reserved. |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 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 |
| Brian Paul | 26895aa | 2004-03-03 15:35:08 +0000 | [diff] [blame^] | 148 | * \c dd_function_table::DeleteBuffer. |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 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; |
| Brian Paul | 85ad44b | 2004-02-13 14:04:26 +0000 | [diff] [blame] | 169 | obj->Usage = GL_STATIC_DRAW_ARB; |
| 170 | obj->Access = GL_READ_WRITE_ARB; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * Add the given buffer object to the buffer object pool. |
| 176 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 177 | void |
| 178 | _mesa_save_buffer_object( GLcontext *ctx, struct gl_buffer_object *obj ) |
| 179 | { |
| 180 | if (obj->Name > 0) { |
| 181 | /* insert into hash table */ |
| 182 | _mesa_HashInsert(ctx->Shared->BufferObjects, obj->Name, obj); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * Remove the given buffer object from the buffer object pool. |
| 189 | * Do not deallocate the buffer object though. |
| 190 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 191 | void |
| 192 | _mesa_remove_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj ) |
| 193 | { |
| 194 | if (bufObj->Name > 0) { |
| 195 | /* remove from hash table */ |
| 196 | _mesa_HashRemove(ctx->Shared->BufferObjects, bufObj->Name); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * Allocate space for and store data in a buffer object. Any data that was |
| 203 | * previously stored in the buffer object is lost. If \c data is \c NULL, |
| 204 | * memory will be allocated, but no copy will occur. |
| 205 | * |
| 206 | * This function is intended to be called via |
| 207 | * \c dd_function_table::BufferData. This function need not set GL error |
| 208 | * codes. The input parameters will have been tested before calling. |
| 209 | * |
| 210 | * \param ctx GL context. |
| 211 | * \param target Buffer object target on which to operate. |
| 212 | * \param size Size, in bytes, of the new data store. |
| 213 | * \param data Pointer to the data to store in the buffer object. This |
| 214 | * pointer may be \c NULL. |
| 215 | * \param usage Hints about how the data will be used. |
| 216 | * \param bufObj Object to be used. |
| 217 | * |
| 218 | * \sa glBufferDataARB, dd_function_table::BufferData. |
| 219 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 220 | void |
| 221 | _mesa_buffer_data( GLcontext *ctx, GLenum target, GLsizeiptrARB size, |
| 222 | const GLvoid * data, GLenum usage, |
| 223 | struct gl_buffer_object * bufObj ) |
| 224 | { |
| 225 | void * new_data; |
| 226 | |
| 227 | (void) target; |
| 228 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 229 | new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 230 | if ( new_data != NULL ) { |
| Brian Paul | e4fcea2 | 2003-09-19 15:38:15 +0000 | [diff] [blame] | 231 | bufObj->Data = (GLubyte *) new_data; |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 232 | bufObj->Size = size; |
| 233 | bufObj->Usage = usage; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 234 | |
| 235 | if ( data != NULL ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 236 | _mesa_memcpy( bufObj->Data, data, size ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /** |
| 243 | * Replace data in a subrange of buffer object. If the data range |
| 244 | * specified by \c size + \c offset extends beyond the end of the buffer or |
| 245 | * if \c data is \c NULL, no copy is performed. |
| 246 | * |
| 247 | * This function is intended to be called by |
| 248 | * \c dd_function_table::BufferSubData. This function need not set GL error |
| 249 | * codes. The input parameters will have been tested before calling. |
| 250 | * |
| 251 | * \param ctx GL context. |
| 252 | * \param target Buffer object target on which to operate. |
| 253 | * \param offset Offset of the first byte to be modified. |
| 254 | * \param size Size, in bytes, of the data range. |
| 255 | * \param data Pointer to the data to store in the buffer object. |
| 256 | * \param bufObj Object to be used. |
| 257 | * |
| 258 | * \sa glBufferSubDataARB, dd_function_table::BufferSubData. |
| 259 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 260 | void |
| 261 | _mesa_buffer_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset, |
| 262 | GLsizeiptrARB size, const GLvoid * data, |
| 263 | struct gl_buffer_object * bufObj ) |
| 264 | { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 265 | if ( (bufObj->Data != NULL) |
| Karl Schultz | df8d337 | 2003-09-18 15:14:10 +0000 | [diff] [blame] | 266 | && ((GLuint)(size + offset) <= bufObj->Size) ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 267 | _mesa_memcpy( (GLubyte *) bufObj->Data + offset, data, size ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /** |
| 273 | * Retrieve data from a subrange of buffer object. If the data range |
| 274 | * specified by \c size + \c offset extends beyond the end of the buffer or |
| 275 | * if \c data is \c NULL, no copy is performed. |
| 276 | * |
| 277 | * This function is intended to be called by |
| 278 | * \c dd_function_table::BufferGetSubData. This function need not set GL error |
| 279 | * codes. The input parameters will have been tested before calling. |
| 280 | * |
| 281 | * \param ctx GL context. |
| 282 | * \param target Buffer object target on which to operate. |
| 283 | * \param offset Offset of the first byte to be modified. |
| 284 | * \param size Size, in bytes, of the data range. |
| 285 | * \param data Pointer to the data to store in the buffer object. |
| 286 | * \param bufObj Object to be used. |
| 287 | * |
| 288 | * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData. |
| 289 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 290 | void |
| 291 | _mesa_buffer_get_subdata( GLcontext *ctx, GLenum target, GLintptrARB offset, |
| 292 | GLsizeiptrARB size, GLvoid * data, |
| 293 | struct gl_buffer_object * bufObj ) |
| 294 | { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 295 | if ( (bufObj->Data != NULL) |
| Karl Schultz | df8d337 | 2003-09-18 15:14:10 +0000 | [diff] [blame] | 296 | && ((GLuint)(size + offset) <= bufObj->Size) ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 297 | _mesa_memcpy( data, (GLubyte *) bufObj->Data + offset, size ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /** |
| 303 | * Maps the private data buffer into the processor's address space. |
| 304 | * |
| 305 | * This function is intended to be called by \c dd_function_table::MapBuffer. |
| 306 | * This function need not set GL error codes. The input parameters will have |
| 307 | * been tested before calling. |
| 308 | * |
| 309 | * \param ctx GL context. |
| 310 | * \param target Buffer object target on which to operate. |
| 311 | * \param access Information about how the buffer will be accessed. |
| 312 | * \param bufObj Object to be used. |
| 313 | * \return A pointer to the object's internal data store that can be accessed |
| 314 | * by the processor |
| 315 | * |
| 316 | * \sa glMapBufferARB, dd_function_table::MapBuffer |
| 317 | */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 318 | void * |
| 319 | _mesa_buffer_map( GLcontext *ctx, GLenum target, GLenum access, |
| 320 | struct gl_buffer_object * bufObj ) |
| 321 | { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 322 | return bufObj->Data; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 325 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 326 | /** |
| 327 | * Initialize the state associated with buffer objects |
| 328 | */ |
| 329 | void |
| 330 | _mesa_init_buffer_objects( GLcontext *ctx ) |
| 331 | { |
| 332 | GLuint i; |
| 333 | |
| Brian Paul | 2020278 | 2004-02-11 22:06:05 +0000 | [diff] [blame] | 334 | /* Allocate the default buffer object and set refcount so high that |
| 335 | * it never gets deleted. |
| 336 | */ |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 337 | ctx->Array.NullBufferObj = _mesa_new_buffer_object(ctx, 0, 0); |
| Brian Paul | 2020278 | 2004-02-11 22:06:05 +0000 | [diff] [blame] | 338 | if (ctx->Array.NullBufferObj) |
| 339 | ctx->Array.NullBufferObj->RefCount = 1000; |
| 340 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 341 | ctx->Array.ArrayBufferObj = ctx->Array.NullBufferObj; |
| 342 | ctx->Array.ElementArrayBufferObj = ctx->Array.NullBufferObj; |
| 343 | |
| 344 | /* Vertex array buffers */ |
| 345 | ctx->Array.Vertex.BufferObj = ctx->Array.NullBufferObj; |
| 346 | ctx->Array.Normal.BufferObj = ctx->Array.NullBufferObj; |
| 347 | ctx->Array.Color.BufferObj = ctx->Array.NullBufferObj; |
| 348 | ctx->Array.SecondaryColor.BufferObj = ctx->Array.NullBufferObj; |
| 349 | ctx->Array.FogCoord.BufferObj = ctx->Array.NullBufferObj; |
| 350 | ctx->Array.Index.BufferObj = ctx->Array.NullBufferObj; |
| 351 | for (i = 0; i < MAX_TEXTURE_UNITS; i++) { |
| 352 | ctx->Array.TexCoord[i].BufferObj = ctx->Array.NullBufferObj; |
| 353 | } |
| 354 | ctx->Array.EdgeFlag.BufferObj = ctx->Array.NullBufferObj; |
| 355 | for (i = 0; i < VERT_ATTRIB_MAX; i++) { |
| 356 | ctx->Array.VertexAttrib[i].BufferObj = ctx->Array.NullBufferObj; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
| 361 | |
| 362 | /**********************************************************************/ |
| 363 | /* API Functions */ |
| 364 | /**********************************************************************/ |
| 365 | |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 366 | void GLAPIENTRY |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 367 | _mesa_BindBufferARB(GLenum target, GLuint buffer) |
| 368 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 369 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 370 | struct gl_buffer_object *oldBufObj; |
| 371 | struct gl_buffer_object *newBufObj = 0; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 372 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 373 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 374 | oldBufObj = buffer_object_get_target( ctx, target, "BindBufferARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 375 | if ( (oldBufObj != NULL) && (oldBufObj->Name == buffer) ) |
| 376 | return; /* rebinding the same buffer object- no change */ |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 377 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 378 | /* |
| 379 | * Get pointer to new buffer object (newBufObj) |
| 380 | */ |
| 381 | if ( buffer == 0 ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 382 | /* The spec says there's not a buffer object named 0, but we use |
| 383 | * one internally because it simplifies things. |
| 384 | */ |
| 385 | newBufObj = ctx->Array.NullBufferObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 386 | } |
| 387 | else { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 388 | /* non-default buffer object */ |
| 389 | const struct _mesa_HashTable *hash = ctx->Shared->BufferObjects; |
| 390 | newBufObj = (struct gl_buffer_object *) _mesa_HashLookup(hash, buffer); |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 391 | if (!newBufObj) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 392 | /* if this is a new buffer object id, allocate a buffer object now */ |
| 393 | newBufObj = (*ctx->Driver.NewBufferObject)(ctx, buffer, target); |
| 394 | if (!newBufObj) { |
| 395 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB"); |
| 396 | return; |
| 397 | } |
| 398 | _mesa_save_buffer_object(ctx, newBufObj); |
| 399 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 400 | newBufObj->RefCount++; |
| 401 | } |
| 402 | |
| 403 | switch (target) { |
| 404 | case GL_ARRAY_BUFFER_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 405 | ctx->Array.ArrayBufferObj = newBufObj; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 406 | break; |
| 407 | case GL_ELEMENT_ARRAY_BUFFER_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 408 | ctx->Array.ElementArrayBufferObj = newBufObj; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 409 | break; |
| 410 | } |
| 411 | |
| 412 | /* Pass BindBuffer call to device driver */ |
| 413 | if ( (ctx->Driver.BindBuffer != NULL) && (newBufObj != NULL) ) |
| 414 | (*ctx->Driver.BindBuffer)( ctx, target, newBufObj ); |
| 415 | |
| 416 | if ( oldBufObj != NULL ) { |
| 417 | oldBufObj->RefCount--; |
| 418 | assert(oldBufObj->RefCount >= 0); |
| 419 | if (oldBufObj->RefCount == 0) { |
| 420 | assert(oldBufObj->Name != 0); |
| 421 | _mesa_remove_buffer_object(ctx, oldBufObj); |
| 422 | ASSERT(ctx->Driver.DeleteBuffer); |
| 423 | (*ctx->Driver.DeleteBuffer)( ctx, oldBufObj ); |
| 424 | } |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 425 | } |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 428 | |
| 429 | /** |
| 430 | * Delete a set of buffer objects. |
| 431 | * |
| 432 | * \param n Number of buffer objects to delete. |
| 433 | * \param buffer Array of \c n buffer object IDs. |
| 434 | */ |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 435 | void GLAPIENTRY |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 436 | _mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids) |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 437 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 438 | GET_CURRENT_CONTEXT(ctx); |
| Karl Schultz | df8d337 | 2003-09-18 15:14:10 +0000 | [diff] [blame] | 439 | GLsizei i; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 440 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 441 | |
| 442 | if (n < 0) { |
| 443 | _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)"); |
| 444 | return; |
| 445 | } |
| 446 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 447 | _glthread_LOCK_MUTEX(ctx->Shared->Mutex); |
| 448 | |
| 449 | for (i = 0; i < n; i++) { |
| 450 | if (ids[i] != 0) { |
| 451 | struct gl_buffer_object *bufObj = (struct gl_buffer_object *) |
| 452 | _mesa_HashLookup(ctx->Shared->BufferObjects, ids[i]); |
| 453 | if (bufObj) { |
| Brian Paul | 6296276 | 2003-09-17 18:58:09 +0000 | [diff] [blame] | 454 | /* unbind any vertex pointers bound to this buffer */ |
| Brian Paul | 26895aa | 2004-03-03 15:35:08 +0000 | [diff] [blame^] | 455 | ASSERT(bufObj->Name == ids[i]); |
| 456 | /* decrement refcount and delete if <= 0 */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 457 | bufObj->RefCount--; |
| 458 | if (bufObj->RefCount <= 0) { |
| Brian Paul | 26895aa | 2004-03-03 15:35:08 +0000 | [diff] [blame^] | 459 | /* buffer should not be bound anymore! */ |
| 460 | ASSERT(ctx->Array.ArrayBufferObj != bufObj); |
| 461 | ASSERT(ctx->Array.ElementArrayBufferObj != bufObj); |
| 462 | ASSERT(ctx->Array.Vertex.BufferObj != bufObj); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 463 | _mesa_remove_buffer_object(ctx, bufObj); |
| 464 | ASSERT(ctx->Driver.DeleteBuffer); |
| 465 | (*ctx->Driver.DeleteBuffer)(ctx, bufObj); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 474 | |
| 475 | /** |
| 476 | * Generate a set of unique buffer object IDs and store them in \c buffer. |
| 477 | * |
| 478 | * \param n Number of IDs to generate. |
| 479 | * \param buffer Array of \c n locations to store the IDs. |
| 480 | */ |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 481 | void GLAPIENTRY |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 482 | _mesa_GenBuffersARB(GLsizei n, GLuint *buffer) |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 483 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 484 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 485 | GLuint first; |
| 486 | GLint i; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 487 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 488 | |
| 489 | if (n < 0) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 490 | _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB"); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 491 | return; |
| 492 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 493 | |
| 494 | if ( buffer == NULL ) { |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * This must be atomic (generation and allocation of buffer object IDs) |
| 500 | */ |
| 501 | _glthread_LOCK_MUTEX(ctx->Shared->Mutex); |
| 502 | |
| 503 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n); |
| 504 | |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 505 | /* Allocate new, empty buffer objects and return identifiers */ |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 506 | for (i = 0; i < n; i++) { |
| 507 | struct gl_buffer_object *bufObj; |
| 508 | GLuint name = first + i; |
| 509 | GLenum target = 0; |
| 510 | bufObj = (*ctx->Driver.NewBufferObject)( ctx, name, target ); |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 511 | if (!bufObj) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 512 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenBuffersARB"); |
| 513 | return; |
| 514 | } |
| 515 | _mesa_save_buffer_object(ctx, bufObj); |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 516 | buffer[i] = first + i; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 522 | |
| 523 | /** |
| 524 | * Determine if ID is the name of a buffer object. |
| 525 | * |
| 526 | * \param id ID of the potential buffer object. |
| 527 | * \return \c GL_TRUE if \c id is the name of a buffer object, |
| 528 | * \c GL_FALSE otherwise. |
| 529 | */ |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 530 | GLboolean GLAPIENTRY |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 531 | _mesa_IsBufferARB(GLuint id) |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 532 | { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 533 | struct gl_buffer_object * bufObj; |
| 534 | GET_CURRENT_CONTEXT(ctx); |
| 535 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 536 | |
| 537 | if (id == 0) |
| 538 | return GL_FALSE; |
| 539 | |
| 540 | _glthread_LOCK_MUTEX(ctx->Shared->Mutex); |
| 541 | bufObj = (struct gl_buffer_object *) _mesa_HashLookup(ctx->Shared->BufferObjects, id); |
| 542 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| 543 | |
| 544 | return (bufObj != NULL); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 547 | |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 548 | void GLAPIENTRY |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 549 | _mesa_BufferDataARB(GLenum target, GLsizeiptrARB size, |
| 550 | const GLvoid * data, GLenum usage) |
| 551 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 552 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 553 | struct gl_buffer_object *bufObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 554 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 555 | |
| 556 | if (size < 0) { |
| 557 | _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)"); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | switch (usage) { |
| 562 | case GL_STREAM_DRAW_ARB: |
| 563 | case GL_STREAM_READ_ARB: |
| 564 | case GL_STREAM_COPY_ARB: |
| 565 | case GL_STATIC_DRAW_ARB: |
| 566 | case GL_STATIC_READ_ARB: |
| 567 | case GL_STATIC_COPY_ARB: |
| 568 | case GL_DYNAMIC_DRAW_ARB: |
| 569 | case GL_DYNAMIC_READ_ARB: |
| 570 | case GL_DYNAMIC_COPY_ARB: |
| 571 | /* OK */ |
| 572 | break; |
| 573 | default: |
| 574 | _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)"); |
| 575 | return; |
| 576 | } |
| 577 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 578 | bufObj = buffer_object_get_target( ctx, target, "BufferDataARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 579 | if ( bufObj == NULL ) { |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 580 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB" ); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 581 | return; |
| 582 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 583 | |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 584 | if (bufObj->Pointer) { |
| Brian Paul | ce8e13d | 2003-10-21 14:54:16 +0000 | [diff] [blame] | 585 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB(buffer is mapped)" ); |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 586 | return; |
| 587 | } |
| 588 | |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 589 | ASSERT(ctx->Driver.BufferData); |
| 590 | |
| 591 | /* Give the buffer object to the driver! <data> may be null! */ |
| 592 | (*ctx->Driver.BufferData)( ctx, target, size, data, usage, bufObj ); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 595 | |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 596 | void GLAPIENTRY |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 597 | _mesa_BufferSubDataARB(GLenum target, GLintptrARB offset, |
| 598 | GLsizeiptrARB size, const GLvoid * data) |
| 599 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 600 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 601 | struct gl_buffer_object *bufObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 602 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 603 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 604 | bufObj = buffer_object_subdata_range_good( ctx, target, offset, size, |
| Brian Paul | 49aefce | 2003-10-15 20:50:41 +0000 | [diff] [blame] | 605 | "BufferSubDataARB" ); |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 606 | if (!bufObj) { |
| 607 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferSubDataARB" ); |
| 608 | return; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 609 | } |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 610 | |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 611 | if (bufObj->Pointer) { |
| 612 | _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferSubDataARB(buffer is mapped)" ); |
| 613 | return; |
| 614 | } |
| 615 | |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 616 | ASSERT(ctx->Driver.BufferSubData); |
| 617 | (*ctx->Driver.BufferSubData)( ctx, target, offset, size, data, bufObj ); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 620 | |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 621 | void GLAPIENTRY |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 622 | _mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset, |
| 623 | GLsizeiptrARB size, void * data) |
| 624 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 625 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 626 | struct gl_buffer_object *bufObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 627 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 628 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 629 | bufObj = buffer_object_subdata_range_good( ctx, target, offset, size, |
| Brian Paul | 49aefce | 2003-10-15 20:50:41 +0000 | [diff] [blame] | 630 | "GetBufferSubDataARB" ); |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 631 | if (!bufObj) { |
| 632 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferSubDataARB" ); |
| 633 | return; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 634 | } |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 635 | |
| 636 | if (bufObj->Pointer) { |
| 637 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferSubDataARB(buffer is mapped)" ); |
| 638 | return; |
| 639 | } |
| 640 | |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 641 | ASSERT(ctx->Driver.GetBufferSubData); |
| 642 | (*ctx->Driver.GetBufferSubData)( ctx, target, offset, size, data, bufObj ); |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 645 | |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 646 | void * GLAPIENTRY |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 647 | _mesa_MapBufferARB(GLenum target, GLenum access) |
| 648 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 649 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 650 | struct gl_buffer_object * bufObj; |
| Brian Paul | ea31ca4 | 2003-05-10 04:35:09 +0000 | [diff] [blame] | 651 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 652 | |
| 653 | switch (access) { |
| 654 | case GL_READ_ONLY_ARB: |
| 655 | case GL_WRITE_ONLY_ARB: |
| 656 | case GL_READ_WRITE_ARB: |
| 657 | /* OK */ |
| 658 | break; |
| 659 | default: |
| 660 | _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)"); |
| Brian Paul | ea31ca4 | 2003-05-10 04:35:09 +0000 | [diff] [blame] | 661 | return NULL; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 664 | bufObj = buffer_object_get_target( ctx, target, "MapBufferARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 665 | if ( bufObj == NULL ) { |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 666 | _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB" ); |
| Brian Paul | ea31ca4 | 2003-05-10 04:35:09 +0000 | [diff] [blame] | 667 | return NULL; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 668 | } |
| Brian Paul | ea31ca4 | 2003-05-10 04:35:09 +0000 | [diff] [blame] | 669 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 670 | if ( bufObj->Pointer != NULL ) { |
| Brian Paul | 66e6e3e | 2003-09-17 21:18:22 +0000 | [diff] [blame] | 671 | _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)"); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 672 | return NULL; |
| 673 | } |
| 674 | |
| 675 | ASSERT(ctx->Driver.MapBuffer); |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 676 | bufObj->Pointer = (*ctx->Driver.MapBuffer)( ctx, target, access, bufObj ); |
| 677 | if ( bufObj->Pointer == NULL ) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 678 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(access)"); |
| 679 | } |
| 680 | |
| Brian Paul | 0bb281b | 2003-10-14 15:48:39 +0000 | [diff] [blame] | 681 | bufObj->Access = access; |
| 682 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 683 | return bufObj->Pointer; |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 686 | |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 687 | GLboolean GLAPIENTRY |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 688 | _mesa_UnmapBufferARB(GLenum target) |
| 689 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 690 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 691 | struct gl_buffer_object *bufObj; |
| 692 | GLboolean status = GL_TRUE; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 693 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 694 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 695 | bufObj = buffer_object_get_target( ctx, target, "UnmapBufferARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 696 | if ( bufObj == NULL ) { |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 697 | _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB" ); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 698 | return GL_FALSE; |
| 699 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 700 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 701 | if ( bufObj->Pointer == NULL ) { |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 702 | _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB"); |
| 703 | return GL_FALSE; |
| 704 | } |
| 705 | |
| 706 | if ( ctx->Driver.UnmapBuffer != NULL ) { |
| 707 | status = (*ctx->Driver.UnmapBuffer)( ctx, target, bufObj ); |
| 708 | } |
| 709 | |
| Brian Paul | 0bb281b | 2003-10-14 15:48:39 +0000 | [diff] [blame] | 710 | bufObj->Access = GL_READ_WRITE_ARB; /* initial value, OK? */ |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 711 | bufObj->Pointer = NULL; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 712 | |
| 713 | return status; |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 716 | |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 717 | void GLAPIENTRY |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 718 | _mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params) |
| 719 | { |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 720 | GET_CURRENT_CONTEXT(ctx); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 721 | struct gl_buffer_object *bufObj; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 722 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 723 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 724 | bufObj = buffer_object_get_target( ctx, target, "GetBufferParameterivARB" ); |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 725 | if (!bufObj) { |
| 726 | _mesa_error(ctx, GL_INVALID_OPERATION, "GetBufferParameterivARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 727 | return; |
| 728 | } |
| 729 | |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 730 | switch (pname) { |
| 731 | case GL_BUFFER_SIZE_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 732 | *params = bufObj->Size; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 733 | break; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 734 | case GL_BUFFER_USAGE_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 735 | *params = bufObj->Usage; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 736 | break; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 737 | case GL_BUFFER_ACCESS_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 738 | *params = bufObj->Access; |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 739 | break; |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 740 | case GL_BUFFER_MAPPED_ARB: |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 741 | *params = (bufObj->Pointer != NULL); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 742 | break; |
| 743 | default: |
| 744 | _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname)"); |
| 745 | return; |
| 746 | } |
| 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 | |
| Kendall Bennett | c40d1dd | 2003-10-21 22:22:17 +0000 | [diff] [blame] | 750 | void GLAPIENTRY |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 751 | _mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **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 | |
| 757 | if (pname != GL_BUFFER_MAP_POINTER_ARB) { |
| 758 | _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)"); |
| 759 | return; |
| 760 | } |
| 761 | |
| Brian Paul | c7b872a | 2003-09-09 13:44:40 +0000 | [diff] [blame] | 762 | bufObj = buffer_object_get_target( ctx, target, "GetBufferPointervARB" ); |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 763 | if ( bufObj == NULL ) { |
| Brian Paul | aa00d12 | 2003-09-15 19:55:10 +0000 | [diff] [blame] | 764 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" ); |
| Brian Paul | aac7325 | 2003-04-09 02:31:35 +0000 | [diff] [blame] | 765 | return; |
| 766 | } |
| Ian Romanick | 0207b47 | 2003-09-09 00:10:12 +0000 | [diff] [blame] | 767 | |
| Brian Paul | 148a284 | 2003-09-17 03:40:11 +0000 | [diff] [blame] | 768 | *params = bufObj->Pointer; |
| Brian Paul | 6061df0 | 2003-03-29 17:01:00 +0000 | [diff] [blame] | 769 | } |