mesa/main: Free ctx->DrawIndirectBuffer during teardown
[mesa.git] / src / mesa / main / bufferobj.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file bufferobj.c
29 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
30 * \author Brian Paul, Ian Romanick
31 */
32
33 #include <stdbool.h>
34 #include "glheader.h"
35 #include "enums.h"
36 #include "hash.h"
37 #include "imports.h"
38 #include "image.h"
39 #include "context.h"
40 #include "bufferobj.h"
41 #include "fbobject.h"
42 #include "mtypes.h"
43 #include "texobj.h"
44 #include "teximage.h"
45 #include "glformats.h"
46 #include "texstore.h"
47 #include "transformfeedback.h"
48 #include "dispatch.h"
49
50
51 /* Debug flags */
52 /*#define VBO_DEBUG*/
53 /*#define BOUNDS_CHECK*/
54
55
56 /**
57 * Used as a placeholder for buffer objects between glGenBuffers() and
58 * glBindBuffer() so that glIsBuffer() can work correctly.
59 */
60 static struct gl_buffer_object DummyBufferObject;
61
62
63 /**
64 * Return pointer to address of a buffer object target.
65 * \param ctx the GL context
66 * \param target the buffer object target to be retrieved.
67 * \return pointer to pointer to the buffer object bound to \c target in the
68 * specified context or \c NULL if \c target is invalid.
69 */
70 static inline struct gl_buffer_object **
71 get_buffer_target(struct gl_context *ctx, GLenum target)
72 {
73 /* Other targets are only supported in desktop OpenGL and OpenGL ES 3.0.
74 */
75 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx)
76 && target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER)
77 return NULL;
78
79 switch (target) {
80 case GL_ARRAY_BUFFER_ARB:
81 return &ctx->Array.ArrayBufferObj;
82 case GL_ELEMENT_ARRAY_BUFFER_ARB:
83 return &ctx->Array.ArrayObj->ElementArrayBufferObj;
84 case GL_PIXEL_PACK_BUFFER_EXT:
85 return &ctx->Pack.BufferObj;
86 case GL_PIXEL_UNPACK_BUFFER_EXT:
87 return &ctx->Unpack.BufferObj;
88 case GL_COPY_READ_BUFFER:
89 return &ctx->CopyReadBuffer;
90 case GL_COPY_WRITE_BUFFER:
91 return &ctx->CopyWriteBuffer;
92 case GL_DRAW_INDIRECT_BUFFER:
93 if (ctx->API == API_OPENGL_CORE &&
94 ctx->Extensions.ARB_draw_indirect) {
95 return &ctx->DrawIndirectBuffer;
96 }
97 break;
98 case GL_TRANSFORM_FEEDBACK_BUFFER:
99 if (ctx->Extensions.EXT_transform_feedback) {
100 return &ctx->TransformFeedback.CurrentBuffer;
101 }
102 break;
103 case GL_TEXTURE_BUFFER:
104 if (ctx->API == API_OPENGL_CORE &&
105 ctx->Extensions.ARB_texture_buffer_object) {
106 return &ctx->Texture.BufferObject;
107 }
108 break;
109 case GL_UNIFORM_BUFFER:
110 if (ctx->Extensions.ARB_uniform_buffer_object) {
111 return &ctx->UniformBuffer;
112 }
113 break;
114 case GL_ATOMIC_COUNTER_BUFFER:
115 if (ctx->Extensions.ARB_shader_atomic_counters) {
116 return &ctx->AtomicBuffer;
117 }
118 break;
119 default:
120 return NULL;
121 }
122 return NULL;
123 }
124
125
126 /**
127 * Get the buffer object bound to the specified target in a GL context.
128 * \param ctx the GL context
129 * \param target the buffer object target to be retrieved.
130 * \param error the GL error to record if target is illegal.
131 * \return pointer to the buffer object bound to \c target in the
132 * specified context or \c NULL if \c target is invalid.
133 */
134 static inline struct gl_buffer_object *
135 get_buffer(struct gl_context *ctx, const char *func, GLenum target,
136 GLenum error)
137 {
138 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
139
140 if (!bufObj) {
141 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
142 return NULL;
143 }
144
145 if (!_mesa_is_bufferobj(*bufObj)) {
146 _mesa_error(ctx, error, "%s(no buffer bound)", func);
147 return NULL;
148 }
149
150 return *bufObj;
151 }
152
153
154 /**
155 * Convert a GLbitfield describing the mapped buffer access flags
156 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
157 */
158 static GLenum
159 simplified_access_mode(struct gl_context *ctx, GLbitfield access)
160 {
161 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
162 if ((access & rwFlags) == rwFlags)
163 return GL_READ_WRITE;
164 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
165 return GL_READ_ONLY;
166 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
167 return GL_WRITE_ONLY;
168
169 /* Otherwise, AccessFlags is zero (the default state).
170 *
171 * Table 2.6 on page 31 (page 44 of the PDF) of the OpenGL 1.5 spec says:
172 *
173 * Name Type Initial Value Legal Values
174 * ... ... ... ...
175 * BUFFER_ACCESS enum READ_WRITE READ_ONLY, WRITE_ONLY
176 * READ_WRITE
177 *
178 * However, table 6.8 in the GL_OES_mapbuffer extension says:
179 *
180 * Get Value Type Get Command Value Description
181 * --------- ---- ----------- ----- -----------
182 * BUFFER_ACCESS_OES Z1 GetBufferParameteriv WRITE_ONLY_OES buffer map flag
183 *
184 * The difference is because GL_OES_mapbuffer only supports mapping buffers
185 * write-only.
186 */
187 assert(access == 0);
188
189 return _mesa_is_gles(ctx) ? GL_WRITE_ONLY : GL_READ_WRITE;
190 }
191
192
193 /**
194 * Test if the buffer is mapped, and if so, if the mapped range overlaps the
195 * given range.
196 * The regions do not overlap if and only if the end of the given
197 * region is before the mapped region or the start of the given region
198 * is after the mapped region.
199 *
200 * \param obj Buffer object target on which to operate.
201 * \param offset Offset of the first byte of the subdata range.
202 * \param size Size, in bytes, of the subdata range.
203 * \return true if ranges overlap, false otherwise
204 *
205 */
206 static bool
207 bufferobj_range_mapped(const struct gl_buffer_object *obj,
208 GLintptr offset, GLsizeiptr size)
209 {
210 if (_mesa_bufferobj_mapped(obj)) {
211 const GLintptr end = offset + size;
212 const GLintptr mapEnd = obj->Offset + obj->Length;
213
214 if (!(end <= obj->Offset || offset >= mapEnd)) {
215 return true;
216 }
217 }
218 return false;
219 }
220
221
222 /**
223 * Tests the subdata range parameters and sets the GL error code for
224 * \c glBufferSubDataARB, \c glGetBufferSubDataARB and
225 * \c glClearBufferSubData.
226 *
227 * \param ctx GL context.
228 * \param target Buffer object target on which to operate.
229 * \param offset Offset of the first byte of the subdata range.
230 * \param size Size, in bytes, of the subdata range.
231 * \param mappedRange If true, checks if an overlapping range is mapped.
232 * If false, checks if buffer is mapped.
233 * \param errorNoBuffer Error code if no buffer is bound to target.
234 * \param caller Name of calling function for recording errors.
235 * \return A pointer to the buffer object bound to \c target in the
236 * specified context or \c NULL if any of the parameter or state
237 * conditions are invalid.
238 *
239 * \sa glBufferSubDataARB, glGetBufferSubDataARB, glClearBufferSubData
240 */
241 static struct gl_buffer_object *
242 buffer_object_subdata_range_good(struct gl_context * ctx, GLenum target,
243 GLintptrARB offset, GLsizeiptrARB size,
244 bool mappedRange, GLenum errorNoBuffer,
245 const char *caller)
246 {
247 struct gl_buffer_object *bufObj;
248
249 if (size < 0) {
250 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
251 return NULL;
252 }
253
254 if (offset < 0) {
255 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
256 return NULL;
257 }
258
259 bufObj = get_buffer(ctx, caller, target, errorNoBuffer);
260 if (!bufObj)
261 return NULL;
262
263 if (offset + size > bufObj->Size) {
264 _mesa_error(ctx, GL_INVALID_VALUE,
265 "%s(offset %lu + size %lu > buffer size %lu)", caller,
266 (unsigned long) offset,
267 (unsigned long) size,
268 (unsigned long) bufObj->Size);
269 return NULL;
270 }
271
272 if (mappedRange) {
273 if (bufferobj_range_mapped(bufObj, offset, size)) {
274 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
275 return NULL;
276 }
277 }
278 else {
279 if (_mesa_bufferobj_mapped(bufObj)) {
280 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
281 return NULL;
282 }
283 }
284
285 return bufObj;
286 }
287
288
289 /**
290 * Test the format and type parameters and set the GL error code for
291 * \c glClearBufferData and \c glClearBufferSubData.
292 *
293 * \param ctx GL context.
294 * \param internalformat Format to which the data is to be converted.
295 * \param format Format of the supplied data.
296 * \param type Type of the supplied data.
297 * \param caller Name of calling function for recording errors.
298 * \return If internalformat, format and type are legal the gl_format
299 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
300 *
301 * \sa glClearBufferData and glClearBufferSubData
302 */
303 static gl_format
304 validate_clear_buffer_format(struct gl_context *ctx,
305 GLenum internalformat,
306 GLenum format, GLenum type,
307 const char *caller)
308 {
309 gl_format mesaFormat;
310 GLenum errorFormatType;
311
312 mesaFormat = _mesa_validate_texbuffer_format(ctx, internalformat);
313 if (mesaFormat == MESA_FORMAT_NONE) {
314 _mesa_error(ctx, GL_INVALID_ENUM,
315 "%s(invalid internalformat)", caller);
316 return MESA_FORMAT_NONE;
317 }
318
319 /* NOTE: not mentioned in ARB_clear_buffer_object but according to
320 * EXT_texture_integer there is no conversion between integer and
321 * non-integer formats
322 */
323 if (_mesa_is_enum_format_signed_int(format) !=
324 _mesa_is_format_integer_color(mesaFormat)) {
325 _mesa_error(ctx, GL_INVALID_OPERATION,
326 "%s(integer vs non-integer)", caller);
327 return MESA_FORMAT_NONE;
328 }
329
330 if (!_mesa_is_color_format(format)) {
331 _mesa_error(ctx, GL_INVALID_ENUM,
332 "%s(format is not a color format)", caller);
333 return MESA_FORMAT_NONE;
334 }
335
336 errorFormatType = _mesa_error_check_format_and_type(ctx, format, type);
337 if (errorFormatType != GL_NO_ERROR) {
338 _mesa_error(ctx, GL_INVALID_ENUM,
339 "%s(invalid format or type)", caller);
340 return MESA_FORMAT_NONE;
341 }
342
343 return mesaFormat;
344 }
345
346
347 /**
348 * Convert user-specified clear value to the specified internal format.
349 *
350 * \param ctx GL context.
351 * \param internalformat Format to which the data is converted.
352 * \param clearValue Points to the converted clear value.
353 * \param format Format of the supplied data.
354 * \param type Type of the supplied data.
355 * \param data Data which is to be converted to internalformat.
356 * \param caller Name of calling function for recording errors.
357 * \return true if data could be converted, false otherwise.
358 *
359 * \sa glClearBufferData, glClearBufferSubData
360 */
361 static bool
362 convert_clear_buffer_data(struct gl_context *ctx,
363 gl_format internalformat,
364 GLubyte *clearValue, GLenum format, GLenum type,
365 const GLvoid *data, const char *caller)
366 {
367 GLenum internalformatBase = _mesa_get_format_base_format(internalformat);
368
369 if (_mesa_texstore(ctx, 1, internalformatBase, internalformat,
370 0, &clearValue, 1, 1, 1,
371 format, type, data, &ctx->Unpack)) {
372 return true;
373 }
374 else {
375 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
376 return false;
377 }
378 }
379
380
381 /**
382 * Allocate and initialize a new buffer object.
383 *
384 * Default callback for the \c dd_function_table::NewBufferObject() hook.
385 */
386 static struct gl_buffer_object *
387 _mesa_new_buffer_object( struct gl_context *ctx, GLuint name, GLenum target )
388 {
389 struct gl_buffer_object *obj;
390
391 (void) ctx;
392
393 obj = MALLOC_STRUCT(gl_buffer_object);
394 _mesa_initialize_buffer_object(ctx, obj, name, target);
395 return obj;
396 }
397
398
399 /**
400 * Delete a buffer object.
401 *
402 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
403 */
404 static void
405 _mesa_delete_buffer_object(struct gl_context *ctx,
406 struct gl_buffer_object *bufObj)
407 {
408 (void) ctx;
409
410 free(bufObj->Data);
411
412 /* assign strange values here to help w/ debugging */
413 bufObj->RefCount = -1000;
414 bufObj->Name = ~0;
415
416 _glthread_DESTROY_MUTEX(bufObj->Mutex);
417 free(bufObj->Label);
418 free(bufObj);
419 }
420
421
422
423 /**
424 * Set ptr to bufObj w/ reference counting.
425 * This is normally only called from the _mesa_reference_buffer_object() macro
426 * when there's a real pointer change.
427 */
428 void
429 _mesa_reference_buffer_object_(struct gl_context *ctx,
430 struct gl_buffer_object **ptr,
431 struct gl_buffer_object *bufObj)
432 {
433 if (*ptr) {
434 /* Unreference the old buffer */
435 GLboolean deleteFlag = GL_FALSE;
436 struct gl_buffer_object *oldObj = *ptr;
437
438 _glthread_LOCK_MUTEX(oldObj->Mutex);
439 ASSERT(oldObj->RefCount > 0);
440 oldObj->RefCount--;
441 #if 0
442 printf("BufferObj %p %d DECR to %d\n",
443 (void *) oldObj, oldObj->Name, oldObj->RefCount);
444 #endif
445 deleteFlag = (oldObj->RefCount == 0);
446 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
447
448 if (deleteFlag) {
449
450 /* some sanity checking: don't delete a buffer still in use */
451 #if 0
452 /* unfortunately, these tests are invalid during context tear-down */
453 ASSERT(ctx->Array.ArrayBufferObj != bufObj);
454 ASSERT(ctx->Array.ArrayObj->ElementArrayBufferObj != bufObj);
455 ASSERT(ctx->Array.ArrayObj->Vertex.BufferObj != bufObj);
456 #endif
457
458 ASSERT(ctx->Driver.DeleteBuffer);
459 ctx->Driver.DeleteBuffer(ctx, oldObj);
460 }
461
462 *ptr = NULL;
463 }
464 ASSERT(!*ptr);
465
466 if (bufObj) {
467 /* reference new buffer */
468 _glthread_LOCK_MUTEX(bufObj->Mutex);
469 if (bufObj->RefCount == 0) {
470 /* this buffer's being deleted (look just above) */
471 /* Not sure this can every really happen. Warn if it does. */
472 _mesa_problem(NULL, "referencing deleted buffer object");
473 *ptr = NULL;
474 }
475 else {
476 bufObj->RefCount++;
477 #if 0
478 printf("BufferObj %p %d INCR to %d\n",
479 (void *) bufObj, bufObj->Name, bufObj->RefCount);
480 #endif
481 *ptr = bufObj;
482 }
483 _glthread_UNLOCK_MUTEX(bufObj->Mutex);
484 }
485 }
486
487
488 /**
489 * Initialize a buffer object to default values.
490 */
491 void
492 _mesa_initialize_buffer_object( struct gl_context *ctx,
493 struct gl_buffer_object *obj,
494 GLuint name, GLenum target )
495 {
496 (void) target;
497
498 memset(obj, 0, sizeof(struct gl_buffer_object));
499 _glthread_INIT_MUTEX(obj->Mutex);
500 obj->RefCount = 1;
501 obj->Name = name;
502 obj->Usage = GL_STATIC_DRAW_ARB;
503 obj->AccessFlags = 0;
504 }
505
506
507
508 /**
509 * Callback called from _mesa_HashWalk()
510 */
511 static void
512 count_buffer_size(GLuint key, void *data, void *userData)
513 {
514 const struct gl_buffer_object *bufObj =
515 (const struct gl_buffer_object *) data;
516 GLuint *total = (GLuint *) userData;
517
518 *total = *total + bufObj->Size;
519 }
520
521
522 /**
523 * Compute total size (in bytes) of all buffer objects for the given context.
524 * For debugging purposes.
525 */
526 GLuint
527 _mesa_total_buffer_object_memory(struct gl_context *ctx)
528 {
529 GLuint total = 0;
530
531 _mesa_HashWalk(ctx->Shared->BufferObjects, count_buffer_size, &total);
532
533 return total;
534 }
535
536
537 /**
538 * Allocate space for and store data in a buffer object. Any data that was
539 * previously stored in the buffer object is lost. If \c data is \c NULL,
540 * memory will be allocated, but no copy will occur.
541 *
542 * This is the default callback for \c dd_function_table::BufferData()
543 * Note that all GL error checking will have been done already.
544 *
545 * \param ctx GL context.
546 * \param target Buffer object target on which to operate.
547 * \param size Size, in bytes, of the new data store.
548 * \param data Pointer to the data to store in the buffer object. This
549 * pointer may be \c NULL.
550 * \param usage Hints about how the data will be used.
551 * \param bufObj Object to be used.
552 *
553 * \return GL_TRUE for success, GL_FALSE for failure
554 * \sa glBufferDataARB, dd_function_table::BufferData.
555 */
556 static GLboolean
557 _mesa_buffer_data( struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
558 const GLvoid * data, GLenum usage,
559 struct gl_buffer_object * bufObj )
560 {
561 void * new_data;
562
563 (void) ctx; (void) target;
564
565 new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size );
566 if (new_data) {
567 bufObj->Data = (GLubyte *) new_data;
568 bufObj->Size = size;
569 bufObj->Usage = usage;
570
571 if (data) {
572 memcpy( bufObj->Data, data, size );
573 }
574
575 return GL_TRUE;
576 }
577 else {
578 return GL_FALSE;
579 }
580 }
581
582
583 /**
584 * Replace data in a subrange of buffer object. If the data range
585 * specified by \c size + \c offset extends beyond the end of the buffer or
586 * if \c data is \c NULL, no copy is performed.
587 *
588 * This is the default callback for \c dd_function_table::BufferSubData()
589 * Note that all GL error checking will have been done already.
590 *
591 * \param ctx GL context.
592 * \param offset Offset of the first byte to be modified.
593 * \param size Size, in bytes, of the data range.
594 * \param data Pointer to the data to store in the buffer object.
595 * \param bufObj Object to be used.
596 *
597 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
598 */
599 static void
600 _mesa_buffer_subdata( struct gl_context *ctx, GLintptrARB offset,
601 GLsizeiptrARB size, const GLvoid * data,
602 struct gl_buffer_object * bufObj )
603 {
604 (void) ctx;
605
606 /* this should have been caught in _mesa_BufferSubData() */
607 ASSERT(size + offset <= bufObj->Size);
608
609 if (bufObj->Data) {
610 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
611 }
612 }
613
614
615 /**
616 * Retrieve data from a subrange of buffer object. If the data range
617 * specified by \c size + \c offset extends beyond the end of the buffer or
618 * if \c data is \c NULL, no copy is performed.
619 *
620 * This is the default callback for \c dd_function_table::GetBufferSubData()
621 * Note that all GL error checking will have been done already.
622 *
623 * \param ctx GL context.
624 * \param target Buffer object target on which to operate.
625 * \param offset Offset of the first byte to be fetched.
626 * \param size Size, in bytes, of the data range.
627 * \param data Destination for data
628 * \param bufObj Object to be used.
629 *
630 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
631 */
632 static void
633 _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
634 GLsizeiptrARB size, GLvoid * data,
635 struct gl_buffer_object * bufObj )
636 {
637 (void) ctx;
638
639 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
640 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
641 }
642 }
643
644
645 /**
646 * Clear a subrange of the buffer object with copies of the supplied data.
647 * If data is NULL the buffer is filled with zeros.
648 *
649 * This is the default callback for \c dd_function_table::ClearBufferSubData()
650 * Note that all GL error checking will have been done already.
651 *
652 * \param ctx GL context.
653 * \param offset Offset of the first byte to be cleared.
654 * \param size Size, in bytes, of the to be cleared range.
655 * \param clearValue Source of the data.
656 * \param clearValueSize Size, in bytes, of the supplied data.
657 * \param bufObj Object to be cleared.
658 *
659 * \sa glClearBufferSubData, glClearBufferData and
660 * dd_function_table::ClearBufferSubData.
661 */
662 static void
663 _mesa_buffer_clear_subdata(struct gl_context *ctx,
664 GLintptr offset, GLsizeiptr size,
665 const GLvoid *clearValue,
666 GLsizeiptr clearValueSize,
667 struct gl_buffer_object *bufObj)
668 {
669 GLsizeiptr i;
670 GLubyte *dest;
671
672 if (_mesa_bufferobj_mapped(bufObj)) {
673 GLubyte *data = malloc(size);
674 GLubyte *dataStart = data;
675 if (data == NULL) {
676 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
677 return;
678 }
679
680 if (clearValue == NULL) {
681 /* Clear with zeros, per the spec */
682 memset(data, 0, size);
683 }
684 else {
685 for (i = 0; i < size/clearValueSize; ++i) {
686 memcpy(data, clearValue, clearValueSize);
687 data += clearValueSize;
688 }
689 }
690 ctx->Driver.BufferSubData(ctx, offset, size, dataStart, bufObj);
691 return;
692 }
693
694 ASSERT(ctx->Driver.MapBufferRange);
695 dest = ctx->Driver.MapBufferRange(ctx, offset, size,
696 GL_MAP_WRITE_BIT |
697 GL_MAP_INVALIDATE_RANGE_BIT,
698 bufObj);
699
700 if (!dest) {
701 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
702 return;
703 }
704
705 if (clearValue == NULL) {
706 /* Clear with zeros, per the spec */
707 memset(dest, 0, size);
708 ctx->Driver.UnmapBuffer(ctx, bufObj);
709 return;
710 }
711
712 for (i = 0; i < size/clearValueSize; ++i) {
713 memcpy(dest, clearValue, clearValueSize);
714 dest += clearValueSize;
715 }
716
717 ctx->Driver.UnmapBuffer(ctx, bufObj);
718 }
719
720
721 /**
722 * Default fallback for \c dd_function_table::MapBufferRange().
723 * Called via glMapBufferRange().
724 */
725 static void *
726 _mesa_buffer_map_range( struct gl_context *ctx, GLintptr offset,
727 GLsizeiptr length, GLbitfield access,
728 struct gl_buffer_object *bufObj )
729 {
730 (void) ctx;
731 assert(!_mesa_bufferobj_mapped(bufObj));
732 /* Just return a direct pointer to the data */
733 bufObj->Pointer = bufObj->Data + offset;
734 bufObj->Length = length;
735 bufObj->Offset = offset;
736 bufObj->AccessFlags = access;
737 return bufObj->Pointer;
738 }
739
740
741 /**
742 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
743 * Called via glFlushMappedBufferRange().
744 */
745 static void
746 _mesa_buffer_flush_mapped_range( struct gl_context *ctx,
747 GLintptr offset, GLsizeiptr length,
748 struct gl_buffer_object *obj )
749 {
750 (void) ctx;
751 (void) offset;
752 (void) length;
753 (void) obj;
754 /* no-op */
755 }
756
757
758 /**
759 * Default callback for \c dd_function_table::MapBuffer().
760 *
761 * The input parameters will have been already tested for errors.
762 *
763 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
764 */
765 static GLboolean
766 _mesa_buffer_unmap( struct gl_context *ctx, struct gl_buffer_object *bufObj )
767 {
768 (void) ctx;
769 /* XXX we might assert here that bufObj->Pointer is non-null */
770 bufObj->Pointer = NULL;
771 bufObj->Length = 0;
772 bufObj->Offset = 0;
773 bufObj->AccessFlags = 0x0;
774 return GL_TRUE;
775 }
776
777
778 /**
779 * Default fallback for \c dd_function_table::CopyBufferSubData().
780 * Called via glCopyBufferSubData().
781 */
782 static void
783 _mesa_copy_buffer_subdata(struct gl_context *ctx,
784 struct gl_buffer_object *src,
785 struct gl_buffer_object *dst,
786 GLintptr readOffset, GLintptr writeOffset,
787 GLsizeiptr size)
788 {
789 GLubyte *srcPtr, *dstPtr;
790
791 /* the buffers should not be mapped */
792 assert(!_mesa_bufferobj_mapped(src));
793 assert(!_mesa_bufferobj_mapped(dst));
794
795 if (src == dst) {
796 srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
797 GL_MAP_READ_BIT |
798 GL_MAP_WRITE_BIT, src);
799
800 if (!srcPtr)
801 return;
802
803 srcPtr += readOffset;
804 dstPtr += writeOffset;
805 } else {
806 srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
807 GL_MAP_READ_BIT, src);
808 dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
809 (GL_MAP_WRITE_BIT |
810 GL_MAP_INVALIDATE_RANGE_BIT), dst);
811 }
812
813 /* Note: the src and dst regions will never overlap. Trying to do so
814 * would generate GL_INVALID_VALUE earlier.
815 */
816 if (srcPtr && dstPtr)
817 memcpy(dstPtr, srcPtr, size);
818
819 ctx->Driver.UnmapBuffer(ctx, src);
820 if (dst != src)
821 ctx->Driver.UnmapBuffer(ctx, dst);
822 }
823
824
825
826 /**
827 * Initialize the state associated with buffer objects
828 */
829 void
830 _mesa_init_buffer_objects( struct gl_context *ctx )
831 {
832 GLuint i;
833
834 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
835 _glthread_INIT_MUTEX(DummyBufferObject.Mutex);
836 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
837
838 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
839 ctx->Shared->NullBufferObj);
840
841 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
842 ctx->Shared->NullBufferObj);
843 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
844 ctx->Shared->NullBufferObj);
845
846 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
847 ctx->Shared->NullBufferObj);
848
849 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer,
850 ctx->Shared->NullBufferObj);
851
852 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
853 _mesa_reference_buffer_object(ctx,
854 &ctx->UniformBufferBindings[i].BufferObject,
855 ctx->Shared->NullBufferObj);
856 ctx->UniformBufferBindings[i].Offset = -1;
857 ctx->UniformBufferBindings[i].Size = -1;
858 }
859 }
860
861
862 void
863 _mesa_free_buffer_objects( struct gl_context *ctx )
864 {
865 GLuint i;
866
867 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
868
869 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
870 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
871
872 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
873
874 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, NULL);
875
876 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
877 _mesa_reference_buffer_object(ctx,
878 &ctx->UniformBufferBindings[i].BufferObject,
879 NULL);
880 }
881 }
882
883 bool
884 _mesa_handle_bind_buffer_gen(struct gl_context *ctx,
885 GLenum target,
886 GLuint buffer,
887 struct gl_buffer_object **buf_handle,
888 const char *caller)
889 {
890 struct gl_buffer_object *buf = *buf_handle;
891
892 if (!buf && ctx->API == API_OPENGL_CORE) {
893 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
894 return false;
895 }
896
897 if (!buf || buf == &DummyBufferObject) {
898 /* If this is a new buffer object id, or one which was generated but
899 * never used before, allocate a buffer object now.
900 */
901 ASSERT(ctx->Driver.NewBufferObject);
902 buf = ctx->Driver.NewBufferObject(ctx, buffer, target);
903 if (!buf) {
904 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
905 return false;
906 }
907 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
908 *buf_handle = buf;
909 }
910
911 return true;
912 }
913
914 /**
915 * Bind the specified target to buffer for the specified context.
916 * Called by glBindBuffer() and other functions.
917 */
918 static void
919 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
920 {
921 struct gl_buffer_object *oldBufObj;
922 struct gl_buffer_object *newBufObj = NULL;
923 struct gl_buffer_object **bindTarget = NULL;
924
925 bindTarget = get_buffer_target(ctx, target);
926 if (!bindTarget) {
927 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
928 return;
929 }
930
931 /* Get pointer to old buffer object (to be unbound) */
932 oldBufObj = *bindTarget;
933 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
934 return; /* rebinding the same buffer object- no change */
935
936 /*
937 * Get pointer to new buffer object (newBufObj)
938 */
939 if (buffer == 0) {
940 /* The spec says there's not a buffer object named 0, but we use
941 * one internally because it simplifies things.
942 */
943 newBufObj = ctx->Shared->NullBufferObj;
944 }
945 else {
946 /* non-default buffer object */
947 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
948 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
949 &newBufObj, "glBindBuffer"))
950 return;
951 }
952
953 /* bind new buffer */
954 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
955
956 /* Pass BindBuffer call to device driver */
957 if (ctx->Driver.BindBuffer)
958 ctx->Driver.BindBuffer( ctx, target, newBufObj );
959 }
960
961
962 /**
963 * Update the default buffer objects in the given context to reference those
964 * specified in the shared state and release those referencing the old
965 * shared state.
966 */
967 void
968 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
969 {
970 /* Bind the NullBufferObj to remove references to those
971 * in the shared context hash table.
972 */
973 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
974 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
975 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
976 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
977 }
978
979
980
981 /**
982 * Return the gl_buffer_object for the given ID.
983 * Always return NULL for ID 0.
984 */
985 struct gl_buffer_object *
986 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
987 {
988 if (buffer == 0)
989 return NULL;
990 else
991 return (struct gl_buffer_object *)
992 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
993 }
994
995
996 /**
997 * If *ptr points to obj, set ptr = the Null/default buffer object.
998 * This is a helper for buffer object deletion.
999 * The GL spec says that deleting a buffer object causes it to get
1000 * unbound from all arrays in the current context.
1001 */
1002 static void
1003 unbind(struct gl_context *ctx,
1004 struct gl_buffer_object **ptr,
1005 struct gl_buffer_object *obj)
1006 {
1007 if (*ptr == obj) {
1008 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
1009 }
1010 }
1011
1012
1013 /**
1014 * Plug default/fallback buffer object functions into the device
1015 * driver hooks.
1016 */
1017 void
1018 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
1019 {
1020 /* GL_ARB_vertex/pixel_buffer_object */
1021 driver->NewBufferObject = _mesa_new_buffer_object;
1022 driver->DeleteBuffer = _mesa_delete_buffer_object;
1023 driver->BindBuffer = NULL;
1024 driver->BufferData = _mesa_buffer_data;
1025 driver->BufferSubData = _mesa_buffer_subdata;
1026 driver->GetBufferSubData = _mesa_buffer_get_subdata;
1027 driver->UnmapBuffer = _mesa_buffer_unmap;
1028
1029 /* GL_ARB_clear_buffer_object */
1030 driver->ClearBufferSubData = _mesa_buffer_clear_subdata;
1031
1032 /* GL_ARB_map_buffer_range */
1033 driver->MapBufferRange = _mesa_buffer_map_range;
1034 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
1035
1036 /* GL_ARB_copy_buffer */
1037 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
1038 }
1039
1040
1041
1042 /**********************************************************************/
1043 /* API Functions */
1044 /**********************************************************************/
1045
1046 void GLAPIENTRY
1047 _mesa_BindBuffer(GLenum target, GLuint buffer)
1048 {
1049 GET_CURRENT_CONTEXT(ctx);
1050
1051 if (MESA_VERBOSE & VERBOSE_API)
1052 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
1053 _mesa_lookup_enum_by_nr(target), buffer);
1054
1055 bind_buffer_object(ctx, target, buffer);
1056 }
1057
1058
1059 /**
1060 * Delete a set of buffer objects.
1061 *
1062 * \param n Number of buffer objects to delete.
1063 * \param ids Array of \c n buffer object IDs.
1064 */
1065 void GLAPIENTRY
1066 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1067 {
1068 GET_CURRENT_CONTEXT(ctx);
1069 GLsizei i;
1070 FLUSH_VERTICES(ctx, 0);
1071
1072 if (n < 0) {
1073 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1074 return;
1075 }
1076
1077 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1078
1079 for (i = 0; i < n; i++) {
1080 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
1081 if (bufObj) {
1082 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
1083 GLuint j;
1084
1085 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
1086
1087 if (_mesa_bufferobj_mapped(bufObj)) {
1088 /* if mapped, unmap it now */
1089 ctx->Driver.UnmapBuffer(ctx, bufObj);
1090 bufObj->AccessFlags = 0;
1091 bufObj->Pointer = NULL;
1092 }
1093
1094 /* unbind any vertex pointers bound to this buffer */
1095 for (j = 0; j < Elements(arrayObj->VertexBinding); j++) {
1096 unbind(ctx, &arrayObj->VertexBinding[j].BufferObj, bufObj);
1097 }
1098
1099 if (ctx->Array.ArrayBufferObj == bufObj) {
1100 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
1101 }
1102 if (arrayObj->ElementArrayBufferObj == bufObj) {
1103 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
1104 }
1105
1106 /* unbind ARB_draw_indirect binding point */
1107 if (ctx->DrawIndirectBuffer == bufObj) {
1108 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 );
1109 }
1110
1111 /* unbind ARB_copy_buffer binding points */
1112 if (ctx->CopyReadBuffer == bufObj) {
1113 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
1114 }
1115 if (ctx->CopyWriteBuffer == bufObj) {
1116 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
1117 }
1118
1119 /* unbind transform feedback binding points */
1120 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
1121 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
1122 }
1123 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1124 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
1125 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
1126 }
1127 }
1128
1129 /* unbind UBO binding points */
1130 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1131 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
1132 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
1133 }
1134 }
1135
1136 if (ctx->UniformBuffer == bufObj) {
1137 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
1138 }
1139
1140 /* unbind any pixel pack/unpack pointers bound to this buffer */
1141 if (ctx->Pack.BufferObj == bufObj) {
1142 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
1143 }
1144 if (ctx->Unpack.BufferObj == bufObj) {
1145 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
1146 }
1147
1148 if (ctx->Texture.BufferObject == bufObj) {
1149 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
1150 }
1151
1152 /* The ID is immediately freed for re-use */
1153 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
1154 /* Make sure we do not run into the classic ABA problem on bind.
1155 * We don't want to allow re-binding a buffer object that's been
1156 * "deleted" by glDeleteBuffers().
1157 *
1158 * The explicit rebinding to the default object in the current context
1159 * prevents the above in the current context, but another context
1160 * sharing the same objects might suffer from this problem.
1161 * The alternative would be to do the hash lookup in any case on bind
1162 * which would introduce more runtime overhead than this.
1163 */
1164 bufObj->DeletePending = GL_TRUE;
1165 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
1166 }
1167 }
1168
1169 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1170 }
1171
1172
1173 /**
1174 * Generate a set of unique buffer object IDs and store them in \c buffer.
1175 *
1176 * \param n Number of IDs to generate.
1177 * \param buffer Array of \c n locations to store the IDs.
1178 */
1179 void GLAPIENTRY
1180 _mesa_GenBuffers(GLsizei n, GLuint *buffer)
1181 {
1182 GET_CURRENT_CONTEXT(ctx);
1183 GLuint first;
1184 GLint i;
1185
1186 if (MESA_VERBOSE & VERBOSE_API)
1187 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
1188
1189 if (n < 0) {
1190 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
1191 return;
1192 }
1193
1194 if (!buffer) {
1195 return;
1196 }
1197
1198 /*
1199 * This must be atomic (generation and allocation of buffer object IDs)
1200 */
1201 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1202
1203 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1204
1205 /* Insert the ID and pointer to dummy buffer object into hash table */
1206 for (i = 0; i < n; i++) {
1207 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
1208 &DummyBufferObject);
1209 buffer[i] = first + i;
1210 }
1211
1212 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1213 }
1214
1215
1216 /**
1217 * Determine if ID is the name of a buffer object.
1218 *
1219 * \param id ID of the potential buffer object.
1220 * \return \c GL_TRUE if \c id is the name of a buffer object,
1221 * \c GL_FALSE otherwise.
1222 */
1223 GLboolean GLAPIENTRY
1224 _mesa_IsBuffer(GLuint id)
1225 {
1226 struct gl_buffer_object *bufObj;
1227 GET_CURRENT_CONTEXT(ctx);
1228 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1229
1230 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1231 bufObj = _mesa_lookup_bufferobj(ctx, id);
1232 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1233
1234 return bufObj && bufObj != &DummyBufferObject;
1235 }
1236
1237
1238 void GLAPIENTRY
1239 _mesa_BufferData(GLenum target, GLsizeiptrARB size,
1240 const GLvoid * data, GLenum usage)
1241 {
1242 GET_CURRENT_CONTEXT(ctx);
1243 struct gl_buffer_object *bufObj;
1244 bool valid_usage;
1245
1246 if (MESA_VERBOSE & VERBOSE_API)
1247 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
1248 _mesa_lookup_enum_by_nr(target),
1249 (long int) size, data,
1250 _mesa_lookup_enum_by_nr(usage));
1251
1252 if (size < 0) {
1253 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1254 return;
1255 }
1256
1257 switch (usage) {
1258 case GL_STREAM_DRAW_ARB:
1259 valid_usage = (ctx->API != API_OPENGLES);
1260 break;
1261
1262 case GL_STATIC_DRAW_ARB:
1263 case GL_DYNAMIC_DRAW_ARB:
1264 valid_usage = true;
1265 break;
1266
1267 case GL_STREAM_READ_ARB:
1268 case GL_STREAM_COPY_ARB:
1269 case GL_STATIC_READ_ARB:
1270 case GL_STATIC_COPY_ARB:
1271 case GL_DYNAMIC_READ_ARB:
1272 case GL_DYNAMIC_COPY_ARB:
1273 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1274 break;
1275
1276 default:
1277 valid_usage = false;
1278 break;
1279 }
1280
1281 if (!valid_usage) {
1282 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
1283 return;
1284 }
1285
1286 bufObj = get_buffer(ctx, "glBufferDataARB", target, GL_INVALID_OPERATION);
1287 if (!bufObj)
1288 return;
1289
1290 if (_mesa_bufferobj_mapped(bufObj)) {
1291 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1292 ctx->Driver.UnmapBuffer(ctx, bufObj);
1293 bufObj->AccessFlags = 0;
1294 ASSERT(bufObj->Pointer == NULL);
1295 }
1296
1297 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1298
1299 bufObj->Written = GL_TRUE;
1300
1301 #ifdef VBO_DEBUG
1302 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1303 bufObj->Name, size, data, usage);
1304 #endif
1305
1306 #ifdef BOUNDS_CHECK
1307 size += 100;
1308 #endif
1309
1310 ASSERT(ctx->Driver.BufferData);
1311 if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
1312 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
1313 }
1314 }
1315
1316
1317 void GLAPIENTRY
1318 _mesa_BufferSubData(GLenum target, GLintptrARB offset,
1319 GLsizeiptrARB size, const GLvoid * data)
1320 {
1321 GET_CURRENT_CONTEXT(ctx);
1322 struct gl_buffer_object *bufObj;
1323
1324 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1325 false, GL_INVALID_OPERATION,
1326 "glBufferSubDataARB" );
1327 if (!bufObj) {
1328 /* error already recorded */
1329 return;
1330 }
1331
1332 if (size == 0)
1333 return;
1334
1335 bufObj->Written = GL_TRUE;
1336
1337 ASSERT(ctx->Driver.BufferSubData);
1338 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1339 }
1340
1341
1342 void GLAPIENTRY
1343 _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
1344 GLsizeiptrARB size, void * data)
1345 {
1346 GET_CURRENT_CONTEXT(ctx);
1347 struct gl_buffer_object *bufObj;
1348
1349 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1350 false, GL_INVALID_OPERATION,
1351 "glGetBufferSubDataARB");
1352 if (!bufObj) {
1353 /* error already recorded */
1354 return;
1355 }
1356
1357 ASSERT(ctx->Driver.GetBufferSubData);
1358 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1359 }
1360
1361
1362 void GLAPIENTRY
1363 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
1364 GLenum type, const GLvoid* data)
1365 {
1366 GET_CURRENT_CONTEXT(ctx);
1367 struct gl_buffer_object* bufObj;
1368 gl_format mesaFormat;
1369 GLubyte clearValue[MAX_PIXEL_BYTES];
1370 GLsizeiptr clearValueSize;
1371
1372 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
1373 if (!bufObj) {
1374 return;
1375 }
1376
1377 if (_mesa_bufferobj_mapped(bufObj)) {
1378 _mesa_error(ctx, GL_INVALID_OPERATION,
1379 "glClearBufferData(buffer currently mapped)");
1380 return;
1381 }
1382
1383 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1384 format, type,
1385 "glClearBufferData");
1386 if (mesaFormat == MESA_FORMAT_NONE) {
1387 return;
1388 }
1389
1390 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1391 if (bufObj->Size % clearValueSize != 0) {
1392 _mesa_error(ctx, GL_INVALID_VALUE,
1393 "glClearBufferData(size is not a multiple of "
1394 "internalformat size)");
1395 return;
1396 }
1397
1398 if (data == NULL) {
1399 /* clear to zeros, per the spec */
1400 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1401 NULL, 0, bufObj);
1402 return;
1403 }
1404
1405 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1406 format, type, data, "glClearBufferData")) {
1407 return;
1408 }
1409
1410 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1411 clearValue, clearValueSize, bufObj);
1412 }
1413
1414
1415 void GLAPIENTRY
1416 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
1417 GLintptr offset, GLsizeiptr size,
1418 GLenum format, GLenum type,
1419 const GLvoid* data)
1420 {
1421 GET_CURRENT_CONTEXT(ctx);
1422 struct gl_buffer_object* bufObj;
1423 gl_format mesaFormat;
1424 GLubyte clearValue[MAX_PIXEL_BYTES];
1425 GLsizeiptr clearValueSize;
1426
1427 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1428 true, GL_INVALID_VALUE,
1429 "glClearBufferSubData");
1430 if (!bufObj) {
1431 return;
1432 }
1433
1434 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1435 format, type,
1436 "glClearBufferSubData");
1437 if (mesaFormat == MESA_FORMAT_NONE) {
1438 return;
1439 }
1440
1441 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1442 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
1443 _mesa_error(ctx, GL_INVALID_VALUE,
1444 "glClearBufferSubData(offset or size is not a multiple of "
1445 "internalformat size)");
1446 return;
1447 }
1448
1449 if (data == NULL) {
1450 /* clear to zeros, per the spec */
1451 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1452 NULL, 0, bufObj);
1453 return;
1454 }
1455
1456 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1457 format, type, data,
1458 "glClearBufferSubData")) {
1459 return;
1460 }
1461
1462 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1463 clearValue, clearValueSize, bufObj);
1464 }
1465
1466
1467 void * GLAPIENTRY
1468 _mesa_MapBuffer(GLenum target, GLenum access)
1469 {
1470 GET_CURRENT_CONTEXT(ctx);
1471 struct gl_buffer_object * bufObj;
1472 GLbitfield accessFlags;
1473 void *map;
1474 bool valid_access;
1475
1476 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1477
1478 switch (access) {
1479 case GL_READ_ONLY_ARB:
1480 accessFlags = GL_MAP_READ_BIT;
1481 valid_access = _mesa_is_desktop_gl(ctx);
1482 break;
1483 case GL_WRITE_ONLY_ARB:
1484 accessFlags = GL_MAP_WRITE_BIT;
1485 valid_access = true;
1486 break;
1487 case GL_READ_WRITE_ARB:
1488 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1489 valid_access = _mesa_is_desktop_gl(ctx);
1490 break;
1491 default:
1492 valid_access = false;
1493 break;
1494 }
1495
1496 if (!valid_access) {
1497 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1498 return NULL;
1499 }
1500
1501 bufObj = get_buffer(ctx, "glMapBufferARB", target, GL_INVALID_OPERATION);
1502 if (!bufObj)
1503 return NULL;
1504
1505 if (_mesa_bufferobj_mapped(bufObj)) {
1506 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1507 return NULL;
1508 }
1509
1510 if (!bufObj->Size) {
1511 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1512 "glMapBuffer(buffer size = 0)");
1513 return NULL;
1514 }
1515
1516 ASSERT(ctx->Driver.MapBufferRange);
1517 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
1518 if (!map) {
1519 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1520 return NULL;
1521 }
1522 else {
1523 /* The driver callback should have set these fields.
1524 * This is important because other modules (like VBO) might call
1525 * the driver function directly.
1526 */
1527 ASSERT(bufObj->Pointer == map);
1528 ASSERT(bufObj->Length == bufObj->Size);
1529 ASSERT(bufObj->Offset == 0);
1530 bufObj->AccessFlags = accessFlags;
1531 }
1532
1533 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1534 bufObj->Written = GL_TRUE;
1535
1536 #ifdef VBO_DEBUG
1537 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1538 bufObj->Name, bufObj->Size, access);
1539 if (access == GL_WRITE_ONLY_ARB) {
1540 GLuint i;
1541 GLubyte *b = (GLubyte *) bufObj->Pointer;
1542 for (i = 0; i < bufObj->Size; i++)
1543 b[i] = i & 0xff;
1544 }
1545 #endif
1546
1547 #ifdef BOUNDS_CHECK
1548 {
1549 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1550 GLuint i;
1551 /* buffer is 100 bytes larger than requested, fill with magic value */
1552 for (i = 0; i < 100; i++) {
1553 buf[bufObj->Size - i - 1] = 123;
1554 }
1555 }
1556 #endif
1557
1558 return bufObj->Pointer;
1559 }
1560
1561
1562 GLboolean GLAPIENTRY
1563 _mesa_UnmapBuffer(GLenum target)
1564 {
1565 GET_CURRENT_CONTEXT(ctx);
1566 struct gl_buffer_object *bufObj;
1567 GLboolean status = GL_TRUE;
1568 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1569
1570 bufObj = get_buffer(ctx, "glUnmapBufferARB", target, GL_INVALID_OPERATION);
1571 if (!bufObj)
1572 return GL_FALSE;
1573
1574 if (!_mesa_bufferobj_mapped(bufObj)) {
1575 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1576 return GL_FALSE;
1577 }
1578
1579 #ifdef BOUNDS_CHECK
1580 if (bufObj->Access != GL_READ_ONLY_ARB) {
1581 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1582 GLuint i;
1583 /* check that last 100 bytes are still = magic value */
1584 for (i = 0; i < 100; i++) {
1585 GLuint pos = bufObj->Size - i - 1;
1586 if (buf[pos] != 123) {
1587 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1588 " at position %d (value = %u)\n",
1589 pos, buf[pos]);
1590 }
1591 }
1592 }
1593 #endif
1594
1595 #ifdef VBO_DEBUG
1596 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1597 GLuint i, unchanged = 0;
1598 GLubyte *b = (GLubyte *) bufObj->Pointer;
1599 GLint pos = -1;
1600 /* check which bytes changed */
1601 for (i = 0; i < bufObj->Size - 1; i++) {
1602 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1603 unchanged++;
1604 if (pos == -1)
1605 pos = i;
1606 }
1607 }
1608 if (unchanged) {
1609 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1610 bufObj->Name, unchanged, bufObj->Size, pos);
1611 }
1612 }
1613 #endif
1614
1615 status = ctx->Driver.UnmapBuffer( ctx, bufObj );
1616 bufObj->AccessFlags = 0;
1617 ASSERT(bufObj->Pointer == NULL);
1618 ASSERT(bufObj->Offset == 0);
1619 ASSERT(bufObj->Length == 0);
1620
1621 return status;
1622 }
1623
1624
1625 void GLAPIENTRY
1626 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
1627 {
1628 GET_CURRENT_CONTEXT(ctx);
1629 struct gl_buffer_object *bufObj;
1630
1631 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target,
1632 GL_INVALID_OPERATION);
1633 if (!bufObj)
1634 return;
1635
1636 switch (pname) {
1637 case GL_BUFFER_SIZE_ARB:
1638 *params = (GLint) bufObj->Size;
1639 return;
1640 case GL_BUFFER_USAGE_ARB:
1641 *params = bufObj->Usage;
1642 return;
1643 case GL_BUFFER_ACCESS_ARB:
1644 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1645 return;
1646 case GL_BUFFER_MAPPED_ARB:
1647 *params = _mesa_bufferobj_mapped(bufObj);
1648 return;
1649 case GL_BUFFER_ACCESS_FLAGS:
1650 if (!ctx->Extensions.ARB_map_buffer_range)
1651 goto invalid_pname;
1652 *params = bufObj->AccessFlags;
1653 return;
1654 case GL_BUFFER_MAP_OFFSET:
1655 if (!ctx->Extensions.ARB_map_buffer_range)
1656 goto invalid_pname;
1657 *params = (GLint) bufObj->Offset;
1658 return;
1659 case GL_BUFFER_MAP_LENGTH:
1660 if (!ctx->Extensions.ARB_map_buffer_range)
1661 goto invalid_pname;
1662 *params = (GLint) bufObj->Length;
1663 return;
1664 default:
1665 ; /* fall-through */
1666 }
1667
1668 invalid_pname:
1669 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1670 _mesa_lookup_enum_by_nr(pname));
1671 }
1672
1673
1674 /**
1675 * New in GL 3.2
1676 * This is pretty much a duplicate of GetBufferParameteriv() but the
1677 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1678 */
1679 void GLAPIENTRY
1680 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1681 {
1682 GET_CURRENT_CONTEXT(ctx);
1683 struct gl_buffer_object *bufObj;
1684
1685 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
1686 GL_INVALID_OPERATION);
1687 if (!bufObj)
1688 return;
1689
1690 switch (pname) {
1691 case GL_BUFFER_SIZE_ARB:
1692 *params = bufObj->Size;
1693 return;
1694 case GL_BUFFER_USAGE_ARB:
1695 *params = bufObj->Usage;
1696 return;
1697 case GL_BUFFER_ACCESS_ARB:
1698 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1699 return;
1700 case GL_BUFFER_ACCESS_FLAGS:
1701 if (!ctx->Extensions.ARB_map_buffer_range)
1702 goto invalid_pname;
1703 *params = bufObj->AccessFlags;
1704 return;
1705 case GL_BUFFER_MAPPED_ARB:
1706 *params = _mesa_bufferobj_mapped(bufObj);
1707 return;
1708 case GL_BUFFER_MAP_OFFSET:
1709 if (!ctx->Extensions.ARB_map_buffer_range)
1710 goto invalid_pname;
1711 *params = bufObj->Offset;
1712 return;
1713 case GL_BUFFER_MAP_LENGTH:
1714 if (!ctx->Extensions.ARB_map_buffer_range)
1715 goto invalid_pname;
1716 *params = bufObj->Length;
1717 return;
1718 default:
1719 ; /* fall-through */
1720 }
1721
1722 invalid_pname:
1723 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1724 _mesa_lookup_enum_by_nr(pname));
1725 }
1726
1727
1728 void GLAPIENTRY
1729 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
1730 {
1731 GET_CURRENT_CONTEXT(ctx);
1732 struct gl_buffer_object * bufObj;
1733
1734 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1735 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1736 return;
1737 }
1738
1739 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target,
1740 GL_INVALID_OPERATION);
1741 if (!bufObj)
1742 return;
1743
1744 *params = bufObj->Pointer;
1745 }
1746
1747
1748 void GLAPIENTRY
1749 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1750 GLintptr readOffset, GLintptr writeOffset,
1751 GLsizeiptr size)
1752 {
1753 GET_CURRENT_CONTEXT(ctx);
1754 struct gl_buffer_object *src, *dst;
1755
1756 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
1757 GL_INVALID_OPERATION);
1758 if (!src)
1759 return;
1760
1761 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
1762 GL_INVALID_OPERATION);
1763 if (!dst)
1764 return;
1765
1766 if (_mesa_bufferobj_mapped(src)) {
1767 _mesa_error(ctx, GL_INVALID_OPERATION,
1768 "glCopyBufferSubData(readBuffer is mapped)");
1769 return;
1770 }
1771
1772 if (_mesa_bufferobj_mapped(dst)) {
1773 _mesa_error(ctx, GL_INVALID_OPERATION,
1774 "glCopyBufferSubData(writeBuffer is mapped)");
1775 return;
1776 }
1777
1778 if (readOffset < 0) {
1779 _mesa_error(ctx, GL_INVALID_VALUE,
1780 "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
1781 return;
1782 }
1783
1784 if (writeOffset < 0) {
1785 _mesa_error(ctx, GL_INVALID_VALUE,
1786 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
1787 return;
1788 }
1789
1790 if (size < 0) {
1791 _mesa_error(ctx, GL_INVALID_VALUE,
1792 "glCopyBufferSubData(writeOffset = %d)", (int) size);
1793 return;
1794 }
1795
1796 if (readOffset + size > src->Size) {
1797 _mesa_error(ctx, GL_INVALID_VALUE,
1798 "glCopyBufferSubData(readOffset + size = %d)",
1799 (int) (readOffset + size));
1800 return;
1801 }
1802
1803 if (writeOffset + size > dst->Size) {
1804 _mesa_error(ctx, GL_INVALID_VALUE,
1805 "glCopyBufferSubData(writeOffset + size = %d)",
1806 (int) (writeOffset + size));
1807 return;
1808 }
1809
1810 if (src == dst) {
1811 if (readOffset + size <= writeOffset) {
1812 /* OK */
1813 }
1814 else if (writeOffset + size <= readOffset) {
1815 /* OK */
1816 }
1817 else {
1818 /* overlapping src/dst is illegal */
1819 _mesa_error(ctx, GL_INVALID_VALUE,
1820 "glCopyBufferSubData(overlapping src/dst)");
1821 return;
1822 }
1823 }
1824
1825 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1826 }
1827
1828
1829 /**
1830 * See GL_ARB_map_buffer_range spec
1831 */
1832 void * GLAPIENTRY
1833 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1834 GLbitfield access)
1835 {
1836 GET_CURRENT_CONTEXT(ctx);
1837 struct gl_buffer_object *bufObj;
1838 void *map;
1839
1840 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1841
1842 if (!ctx->Extensions.ARB_map_buffer_range) {
1843 _mesa_error(ctx, GL_INVALID_OPERATION,
1844 "glMapBufferRange(extension not supported)");
1845 return NULL;
1846 }
1847
1848 if (offset < 0) {
1849 _mesa_error(ctx, GL_INVALID_VALUE,
1850 "glMapBufferRange(offset = %ld)", (long)offset);
1851 return NULL;
1852 }
1853
1854 if (length < 0) {
1855 _mesa_error(ctx, GL_INVALID_VALUE,
1856 "glMapBufferRange(length = %ld)", (long)length);
1857 return NULL;
1858 }
1859
1860 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
1861 *
1862 * "An INVALID_OPERATION error is generated for any of the following
1863 * conditions:
1864 *
1865 * * <length> is zero."
1866 */
1867 if (_mesa_is_gles(ctx) && length == 0) {
1868 _mesa_error(ctx, GL_INVALID_OPERATION,
1869 "glMapBufferRange(length = 0)");
1870 return NULL;
1871 }
1872
1873 if (access & ~(GL_MAP_READ_BIT |
1874 GL_MAP_WRITE_BIT |
1875 GL_MAP_INVALIDATE_RANGE_BIT |
1876 GL_MAP_INVALIDATE_BUFFER_BIT |
1877 GL_MAP_FLUSH_EXPLICIT_BIT |
1878 GL_MAP_UNSYNCHRONIZED_BIT)) {
1879 /* generate an error if any undefind bit is set */
1880 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
1881 return NULL;
1882 }
1883
1884 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1885 _mesa_error(ctx, GL_INVALID_OPERATION,
1886 "glMapBufferRange(access indicates neither read or write)");
1887 return NULL;
1888 }
1889
1890 if ((access & GL_MAP_READ_BIT) &&
1891 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
1892 GL_MAP_INVALIDATE_BUFFER_BIT |
1893 GL_MAP_UNSYNCHRONIZED_BIT))) {
1894 _mesa_error(ctx, GL_INVALID_OPERATION,
1895 "glMapBufferRange(invalid access flags)");
1896 return NULL;
1897 }
1898
1899 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1900 ((access & GL_MAP_WRITE_BIT) == 0)) {
1901 _mesa_error(ctx, GL_INVALID_OPERATION,
1902 "glMapBufferRange(invalid access flags)");
1903 return NULL;
1904 }
1905
1906 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
1907 if (!bufObj)
1908 return NULL;
1909
1910 if (offset + length > bufObj->Size) {
1911 _mesa_error(ctx, GL_INVALID_VALUE,
1912 "glMapBufferRange(offset + length > size)");
1913 return NULL;
1914 }
1915
1916 if (_mesa_bufferobj_mapped(bufObj)) {
1917 _mesa_error(ctx, GL_INVALID_OPERATION,
1918 "glMapBufferRange(buffer already mapped)");
1919 return NULL;
1920 }
1921
1922 if (!bufObj->Size) {
1923 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1924 "glMapBufferRange(buffer size = 0)");
1925 return NULL;
1926 }
1927
1928 /* Mapping zero bytes should return a non-null pointer. */
1929 if (!length) {
1930 static long dummy = 0;
1931 bufObj->Pointer = &dummy;
1932 bufObj->Length = length;
1933 bufObj->Offset = offset;
1934 bufObj->AccessFlags = access;
1935 return bufObj->Pointer;
1936 }
1937
1938 ASSERT(ctx->Driver.MapBufferRange);
1939 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
1940 if (!map) {
1941 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1942 }
1943 else {
1944 /* The driver callback should have set all these fields.
1945 * This is important because other modules (like VBO) might call
1946 * the driver function directly.
1947 */
1948 ASSERT(bufObj->Pointer == map);
1949 ASSERT(bufObj->Length == length);
1950 ASSERT(bufObj->Offset == offset);
1951 ASSERT(bufObj->AccessFlags == access);
1952 }
1953
1954 return map;
1955 }
1956
1957
1958 /**
1959 * See GL_ARB_map_buffer_range spec
1960 */
1961 void GLAPIENTRY
1962 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1963 {
1964 GET_CURRENT_CONTEXT(ctx);
1965 struct gl_buffer_object *bufObj;
1966
1967 if (!ctx->Extensions.ARB_map_buffer_range) {
1968 _mesa_error(ctx, GL_INVALID_OPERATION,
1969 "glFlushMappedBufferRange(extension not supported)");
1970 return;
1971 }
1972
1973 if (offset < 0) {
1974 _mesa_error(ctx, GL_INVALID_VALUE,
1975 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
1976 return;
1977 }
1978
1979 if (length < 0) {
1980 _mesa_error(ctx, GL_INVALID_VALUE,
1981 "glFlushMappedBufferRange(length = %ld)", (long)length);
1982 return;
1983 }
1984
1985 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
1986 GL_INVALID_OPERATION);
1987 if (!bufObj)
1988 return;
1989
1990 if (!_mesa_bufferobj_mapped(bufObj)) {
1991 /* buffer is not mapped */
1992 _mesa_error(ctx, GL_INVALID_OPERATION,
1993 "glFlushMappedBufferRange(buffer is not mapped)");
1994 return;
1995 }
1996
1997 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1998 _mesa_error(ctx, GL_INVALID_OPERATION,
1999 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
2000 return;
2001 }
2002
2003 if (offset + length > bufObj->Length) {
2004 _mesa_error(ctx, GL_INVALID_VALUE,
2005 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
2006 (long)offset, (long)length, (long)bufObj->Length);
2007 return;
2008 }
2009
2010 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
2011
2012 if (ctx->Driver.FlushMappedBufferRange)
2013 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj);
2014 }
2015
2016
2017 static GLenum
2018 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2019 {
2020 struct gl_buffer_object *bufObj;
2021 GLenum retval;
2022
2023 bufObj = _mesa_lookup_bufferobj(ctx, name);
2024 if (!bufObj) {
2025 _mesa_error(ctx, GL_INVALID_VALUE,
2026 "glObjectPurgeable(name = 0x%x)", name);
2027 return 0;
2028 }
2029 if (!_mesa_is_bufferobj(bufObj)) {
2030 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
2031 return 0;
2032 }
2033
2034 if (bufObj->Purgeable) {
2035 _mesa_error(ctx, GL_INVALID_OPERATION,
2036 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2037 return GL_VOLATILE_APPLE;
2038 }
2039
2040 bufObj->Purgeable = GL_TRUE;
2041
2042 retval = GL_VOLATILE_APPLE;
2043 if (ctx->Driver.BufferObjectPurgeable)
2044 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
2045
2046 return retval;
2047 }
2048
2049
2050 static GLenum
2051 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2052 {
2053 struct gl_renderbuffer *bufObj;
2054 GLenum retval;
2055
2056 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2057 if (!bufObj) {
2058 _mesa_error(ctx, GL_INVALID_VALUE,
2059 "glObjectUnpurgeable(name = 0x%x)", name);
2060 return 0;
2061 }
2062
2063 if (bufObj->Purgeable) {
2064 _mesa_error(ctx, GL_INVALID_OPERATION,
2065 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2066 return GL_VOLATILE_APPLE;
2067 }
2068
2069 bufObj->Purgeable = GL_TRUE;
2070
2071 retval = GL_VOLATILE_APPLE;
2072 if (ctx->Driver.RenderObjectPurgeable)
2073 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
2074
2075 return retval;
2076 }
2077
2078
2079 static GLenum
2080 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2081 {
2082 struct gl_texture_object *bufObj;
2083 GLenum retval;
2084
2085 bufObj = _mesa_lookup_texture(ctx, name);
2086 if (!bufObj) {
2087 _mesa_error(ctx, GL_INVALID_VALUE,
2088 "glObjectPurgeable(name = 0x%x)", name);
2089 return 0;
2090 }
2091
2092 if (bufObj->Purgeable) {
2093 _mesa_error(ctx, GL_INVALID_OPERATION,
2094 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2095 return GL_VOLATILE_APPLE;
2096 }
2097
2098 bufObj->Purgeable = GL_TRUE;
2099
2100 retval = GL_VOLATILE_APPLE;
2101 if (ctx->Driver.TextureObjectPurgeable)
2102 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
2103
2104 return retval;
2105 }
2106
2107
2108 GLenum GLAPIENTRY
2109 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2110 {
2111 GLenum retval;
2112
2113 GET_CURRENT_CONTEXT(ctx);
2114 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2115
2116 if (name == 0) {
2117 _mesa_error(ctx, GL_INVALID_VALUE,
2118 "glObjectPurgeable(name = 0x%x)", name);
2119 return 0;
2120 }
2121
2122 switch (option) {
2123 case GL_VOLATILE_APPLE:
2124 case GL_RELEASED_APPLE:
2125 /* legal */
2126 break;
2127 default:
2128 _mesa_error(ctx, GL_INVALID_ENUM,
2129 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2130 name, option);
2131 return 0;
2132 }
2133
2134 switch (objectType) {
2135 case GL_TEXTURE:
2136 retval = texture_object_purgeable(ctx, name, option);
2137 break;
2138 case GL_RENDERBUFFER_EXT:
2139 retval = renderbuffer_purgeable(ctx, name, option);
2140 break;
2141 case GL_BUFFER_OBJECT_APPLE:
2142 retval = buffer_object_purgeable(ctx, name, option);
2143 break;
2144 default:
2145 _mesa_error(ctx, GL_INVALID_ENUM,
2146 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2147 name, objectType);
2148 return 0;
2149 }
2150
2151 /* In strict conformance to the spec, we must only return VOLATILE when
2152 * when passed the VOLATILE option. Madness.
2153 *
2154 * XXX First fix the spec, then fix me.
2155 */
2156 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
2157 }
2158
2159
2160 static GLenum
2161 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2162 {
2163 struct gl_buffer_object *bufObj;
2164 GLenum retval;
2165
2166 bufObj = _mesa_lookup_bufferobj(ctx, name);
2167 if (!bufObj) {
2168 _mesa_error(ctx, GL_INVALID_VALUE,
2169 "glObjectUnpurgeable(name = 0x%x)", name);
2170 return 0;
2171 }
2172
2173 if (! bufObj->Purgeable) {
2174 _mesa_error(ctx, GL_INVALID_OPERATION,
2175 "glObjectUnpurgeable(name = 0x%x) object is "
2176 " already \"unpurged\"", name);
2177 return 0;
2178 }
2179
2180 bufObj->Purgeable = GL_FALSE;
2181
2182 retval = option;
2183 if (ctx->Driver.BufferObjectUnpurgeable)
2184 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
2185
2186 return retval;
2187 }
2188
2189
2190 static GLenum
2191 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2192 {
2193 struct gl_renderbuffer *bufObj;
2194 GLenum retval;
2195
2196 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2197 if (!bufObj) {
2198 _mesa_error(ctx, GL_INVALID_VALUE,
2199 "glObjectUnpurgeable(name = 0x%x)", name);
2200 return 0;
2201 }
2202
2203 if (! bufObj->Purgeable) {
2204 _mesa_error(ctx, GL_INVALID_OPERATION,
2205 "glObjectUnpurgeable(name = 0x%x) object is "
2206 " already \"unpurged\"", name);
2207 return 0;
2208 }
2209
2210 bufObj->Purgeable = GL_FALSE;
2211
2212 retval = option;
2213 if (ctx->Driver.RenderObjectUnpurgeable)
2214 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
2215
2216 return retval;
2217 }
2218
2219
2220 static GLenum
2221 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2222 {
2223 struct gl_texture_object *bufObj;
2224 GLenum retval;
2225
2226 bufObj = _mesa_lookup_texture(ctx, name);
2227 if (!bufObj) {
2228 _mesa_error(ctx, GL_INVALID_VALUE,
2229 "glObjectUnpurgeable(name = 0x%x)", name);
2230 return 0;
2231 }
2232
2233 if (! bufObj->Purgeable) {
2234 _mesa_error(ctx, GL_INVALID_OPERATION,
2235 "glObjectUnpurgeable(name = 0x%x) object is"
2236 " already \"unpurged\"", name);
2237 return 0;
2238 }
2239
2240 bufObj->Purgeable = GL_FALSE;
2241
2242 retval = option;
2243 if (ctx->Driver.TextureObjectUnpurgeable)
2244 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
2245
2246 return retval;
2247 }
2248
2249
2250 GLenum GLAPIENTRY
2251 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2252 {
2253 GET_CURRENT_CONTEXT(ctx);
2254 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2255
2256 if (name == 0) {
2257 _mesa_error(ctx, GL_INVALID_VALUE,
2258 "glObjectUnpurgeable(name = 0x%x)", name);
2259 return 0;
2260 }
2261
2262 switch (option) {
2263 case GL_RETAINED_APPLE:
2264 case GL_UNDEFINED_APPLE:
2265 /* legal */
2266 break;
2267 default:
2268 _mesa_error(ctx, GL_INVALID_ENUM,
2269 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2270 name, option);
2271 return 0;
2272 }
2273
2274 switch (objectType) {
2275 case GL_BUFFER_OBJECT_APPLE:
2276 return buffer_object_unpurgeable(ctx, name, option);
2277 case GL_TEXTURE:
2278 return texture_object_unpurgeable(ctx, name, option);
2279 case GL_RENDERBUFFER_EXT:
2280 return renderbuffer_unpurgeable(ctx, name, option);
2281 default:
2282 _mesa_error(ctx, GL_INVALID_ENUM,
2283 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2284 name, objectType);
2285 return 0;
2286 }
2287 }
2288
2289
2290 static void
2291 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
2292 GLenum pname, GLint *params)
2293 {
2294 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
2295 if (!bufObj) {
2296 _mesa_error(ctx, GL_INVALID_VALUE,
2297 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
2298 return;
2299 }
2300
2301 switch (pname) {
2302 case GL_PURGEABLE_APPLE:
2303 *params = bufObj->Purgeable;
2304 break;
2305 default:
2306 _mesa_error(ctx, GL_INVALID_ENUM,
2307 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2308 name, pname);
2309 break;
2310 }
2311 }
2312
2313
2314 static void
2315 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
2316 GLenum pname, GLint *params)
2317 {
2318 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
2319 if (!rb) {
2320 _mesa_error(ctx, GL_INVALID_VALUE,
2321 "glObjectUnpurgeable(name = 0x%x)", name);
2322 return;
2323 }
2324
2325 switch (pname) {
2326 case GL_PURGEABLE_APPLE:
2327 *params = rb->Purgeable;
2328 break;
2329 default:
2330 _mesa_error(ctx, GL_INVALID_ENUM,
2331 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2332 name, pname);
2333 break;
2334 }
2335 }
2336
2337
2338 static void
2339 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
2340 GLenum pname, GLint *params)
2341 {
2342 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
2343 if (!texObj) {
2344 _mesa_error(ctx, GL_INVALID_VALUE,
2345 "glObjectUnpurgeable(name = 0x%x)", name);
2346 return;
2347 }
2348
2349 switch (pname) {
2350 case GL_PURGEABLE_APPLE:
2351 *params = texObj->Purgeable;
2352 break;
2353 default:
2354 _mesa_error(ctx, GL_INVALID_ENUM,
2355 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2356 name, pname);
2357 break;
2358 }
2359 }
2360
2361
2362 void GLAPIENTRY
2363 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2364 GLint *params)
2365 {
2366 GET_CURRENT_CONTEXT(ctx);
2367
2368 if (name == 0) {
2369 _mesa_error(ctx, GL_INVALID_VALUE,
2370 "glGetObjectParameteriv(name = 0x%x)", name);
2371 return;
2372 }
2373
2374 switch (objectType) {
2375 case GL_TEXTURE:
2376 get_texture_object_parameteriv(ctx, name, pname, params);
2377 break;
2378 case GL_BUFFER_OBJECT_APPLE:
2379 get_buffer_object_parameteriv(ctx, name, pname, params);
2380 break;
2381 case GL_RENDERBUFFER_EXT:
2382 get_renderbuffer_parameteriv(ctx, name, pname, params);
2383 break;
2384 default:
2385 _mesa_error(ctx, GL_INVALID_ENUM,
2386 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2387 name, objectType);
2388 }
2389 }
2390
2391 static void
2392 set_ubo_binding(struct gl_context *ctx,
2393 int index,
2394 struct gl_buffer_object *bufObj,
2395 GLintptr offset,
2396 GLsizeiptr size,
2397 GLboolean autoSize)
2398 {
2399 struct gl_uniform_buffer_binding *binding;
2400
2401 binding = &ctx->UniformBufferBindings[index];
2402 if (binding->BufferObject == bufObj &&
2403 binding->Offset == offset &&
2404 binding->Size == size &&
2405 binding->AutomaticSize == autoSize) {
2406 return;
2407 }
2408
2409 FLUSH_VERTICES(ctx, 0);
2410 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2411
2412 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2413 binding->Offset = offset;
2414 binding->Size = size;
2415 binding->AutomaticSize = autoSize;
2416 }
2417
2418 /**
2419 * Bind a region of a buffer object to a uniform block binding point.
2420 * \param index the uniform buffer binding point index
2421 * \param bufObj the buffer object
2422 * \param offset offset to the start of buffer object region
2423 * \param size size of the buffer object region
2424 */
2425 static void
2426 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2427 GLuint index,
2428 struct gl_buffer_object *bufObj,
2429 GLintptr offset,
2430 GLsizeiptr size)
2431 {
2432 if (index >= ctx->Const.MaxUniformBufferBindings) {
2433 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2434 return;
2435 }
2436
2437 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2438 _mesa_error(ctx, GL_INVALID_VALUE,
2439 "glBindBufferRange(offset misalgned %d/%d)", (int) offset,
2440 ctx->Const.UniformBufferOffsetAlignment);
2441 return;
2442 }
2443
2444 if (bufObj == ctx->Shared->NullBufferObj) {
2445 offset = -1;
2446 size = -1;
2447 }
2448
2449 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2450 set_ubo_binding(ctx, index, bufObj, offset, size, GL_FALSE);
2451 }
2452
2453
2454 /**
2455 * Bind a buffer object to a uniform block binding point.
2456 * As above, but offset = 0.
2457 */
2458 static void
2459 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2460 GLuint index,
2461 struct gl_buffer_object *bufObj)
2462 {
2463 if (index >= ctx->Const.MaxUniformBufferBindings) {
2464 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2465 return;
2466 }
2467
2468 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2469 if (bufObj == ctx->Shared->NullBufferObj)
2470 set_ubo_binding(ctx, index, bufObj, -1, -1, GL_TRUE);
2471 else
2472 set_ubo_binding(ctx, index, bufObj, 0, 0, GL_TRUE);
2473 }
2474
2475 static void
2476 set_atomic_buffer_binding(struct gl_context *ctx,
2477 unsigned index,
2478 struct gl_buffer_object *bufObj,
2479 GLintptr offset,
2480 GLsizeiptr size,
2481 const char *name)
2482 {
2483 struct gl_atomic_buffer_binding *binding;
2484
2485 if (index >= ctx->Const.MaxAtomicBufferBindings) {
2486 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
2487 return;
2488 }
2489
2490 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
2491 _mesa_error(ctx, GL_INVALID_VALUE,
2492 "%s(offset misalgned %d/%d)", name, (int) offset,
2493 ATOMIC_COUNTER_SIZE);
2494 return;
2495 }
2496
2497 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
2498
2499 binding = &ctx->AtomicBufferBindings[index];
2500 if (binding->BufferObject == bufObj &&
2501 binding->Offset == offset &&
2502 binding->Size == size) {
2503 return;
2504 }
2505
2506 FLUSH_VERTICES(ctx, 0);
2507 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
2508
2509 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2510
2511 if (bufObj == ctx->Shared->NullBufferObj) {
2512 binding->Offset = -1;
2513 binding->Size = -1;
2514 } else {
2515 binding->Offset = offset;
2516 binding->Size = size;
2517 }
2518 }
2519
2520 void GLAPIENTRY
2521 _mesa_BindBufferRange(GLenum target, GLuint index,
2522 GLuint buffer, GLintptr offset, GLsizeiptr size)
2523 {
2524 GET_CURRENT_CONTEXT(ctx);
2525 struct gl_buffer_object *bufObj;
2526
2527 if (buffer == 0) {
2528 bufObj = ctx->Shared->NullBufferObj;
2529 } else {
2530 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2531 }
2532 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
2533 &bufObj, "glBindBufferRange"))
2534 return;
2535
2536 if (!bufObj) {
2537 _mesa_error(ctx, GL_INVALID_OPERATION,
2538 "glBindBufferRange(invalid buffer=%u)", buffer);
2539 return;
2540 }
2541
2542 if (buffer != 0) {
2543 if (size <= 0) {
2544 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
2545 (int) size);
2546 return;
2547 }
2548 }
2549
2550 switch (target) {
2551 case GL_TRANSFORM_FEEDBACK_BUFFER:
2552 _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
2553 offset, size);
2554 return;
2555 case GL_UNIFORM_BUFFER:
2556 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
2557 return;
2558 case GL_ATOMIC_COUNTER_BUFFER:
2559 set_atomic_buffer_binding(ctx, index, bufObj, offset, size,
2560 "glBindBufferRange");
2561 return;
2562 default:
2563 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
2564 return;
2565 }
2566 }
2567
2568 void GLAPIENTRY
2569 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
2570 {
2571 GET_CURRENT_CONTEXT(ctx);
2572 struct gl_buffer_object *bufObj;
2573
2574 if (buffer == 0) {
2575 bufObj = ctx->Shared->NullBufferObj;
2576 } else {
2577 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2578 }
2579 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
2580 &bufObj, "glBindBufferBase"))
2581 return;
2582
2583 if (!bufObj) {
2584 _mesa_error(ctx, GL_INVALID_OPERATION,
2585 "glBindBufferBase(invalid buffer=%u)", buffer);
2586 return;
2587 }
2588
2589 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2590 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2591 *
2592 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2593 * zero and size equal to the size of buffer."
2594 *
2595 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2596 *
2597 * "If the parameter (starting offset or size) was not specified when the
2598 * buffer object was bound, zero is returned."
2599 *
2600 * What happens if the size of the buffer changes? Does the size of the
2601 * buffer at the moment glBindBufferBase was called still play a role, like
2602 * the first quote would imply, or is the size meaningless in the
2603 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2604 * core spec page 45 says:
2605 *
2606 * "It is equivalent to calling BindBufferRange with offset zero, while
2607 * size is determined by the size of the bound buffer at the time the
2608 * binding is used."
2609 *
2610 * My interpretation is that the GL 4.1 spec was a clarification of the
2611 * behavior, not a change. In particular, this choice will only make
2612 * rendering work in cases where it would have had undefined results.
2613 */
2614
2615 switch (target) {
2616 case GL_TRANSFORM_FEEDBACK_BUFFER:
2617 _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
2618 return;
2619 case GL_UNIFORM_BUFFER:
2620 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
2621 return;
2622 case GL_ATOMIC_COUNTER_BUFFER:
2623 set_atomic_buffer_binding(ctx, index, bufObj, 0, 0,
2624 "glBindBufferBase");
2625 return;
2626 default:
2627 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
2628 return;
2629 }
2630 }
2631
2632 void GLAPIENTRY
2633 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
2634 GLsizeiptr length)
2635 {
2636 GET_CURRENT_CONTEXT(ctx);
2637 struct gl_buffer_object *bufObj;
2638 const GLintptr end = offset + length;
2639
2640 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2641 if (!bufObj) {
2642 _mesa_error(ctx, GL_INVALID_VALUE,
2643 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2644 buffer);
2645 return;
2646 }
2647
2648 /* The GL_ARB_invalidate_subdata spec says:
2649 *
2650 * "An INVALID_VALUE error is generated if <offset> or <length> is
2651 * negative, or if <offset> + <length> is greater than the value of
2652 * BUFFER_SIZE."
2653 */
2654 if (end < 0 || end > bufObj->Size) {
2655 _mesa_error(ctx, GL_INVALID_VALUE,
2656 "glInvalidateBufferSubData(invalid offset or length)");
2657 return;
2658 }
2659
2660 /* The GL_ARB_invalidate_subdata spec says:
2661 *
2662 * "An INVALID_OPERATION error is generated if the buffer is currently
2663 * mapped by MapBuffer, or if the invalidate range intersects the range
2664 * currently mapped by MapBufferRange."
2665 */
2666 if (bufferobj_range_mapped(bufObj, offset, length)) {
2667 _mesa_error(ctx, GL_INVALID_OPERATION,
2668 "glInvalidateBufferSubData(intersection with mapped "
2669 "range)");
2670 return;
2671 }
2672
2673 /* We don't actually do anything for this yet. Just return after
2674 * validating the parameters and generating the required errors.
2675 */
2676 return;
2677 }
2678
2679 void GLAPIENTRY
2680 _mesa_InvalidateBufferData(GLuint buffer)
2681 {
2682 GET_CURRENT_CONTEXT(ctx);
2683 struct gl_buffer_object *bufObj;
2684
2685 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2686 if (!bufObj) {
2687 _mesa_error(ctx, GL_INVALID_VALUE,
2688 "glInvalidateBufferData(name = 0x%x) invalid object",
2689 buffer);
2690 return;
2691 }
2692
2693 /* The GL_ARB_invalidate_subdata spec says:
2694 *
2695 * "An INVALID_OPERATION error is generated if the buffer is currently
2696 * mapped by MapBuffer, or if the invalidate range intersects the range
2697 * currently mapped by MapBufferRange."
2698 */
2699 if (_mesa_bufferobj_mapped(bufObj)) {
2700 _mesa_error(ctx, GL_INVALID_OPERATION,
2701 "glInvalidateBufferData(intersection with mapped "
2702 "range)");
2703 return;
2704 }
2705
2706 /* We don't actually do anything for this yet. Just return after
2707 * validating the parameters and generating the required errors.
2708 */
2709 return;
2710 }