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