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