mesa: Rename "struct gl_array_object" to gl_vertex_array_object.
[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.VAO->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 mesa_format
299 * corresponding to internalformat, otherwise MESA_FORMAT_NONE.
300 *
301 * \sa glClearBufferData and glClearBufferSubData
302 */
303 static mesa_format
304 validate_clear_buffer_format(struct gl_context *ctx,
305 GLenum internalformat,
306 GLenum format, GLenum type,
307 const char *caller)
308 {
309 mesa_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 mesa_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 _mesa_align_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.VAO->ElementArrayBufferObj != bufObj);
455 ASSERT(ctx->Array.VAO->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) target;
564
565 if (bufObj->Data)
566 _mesa_align_free( bufObj->Data );
567
568 new_data = _mesa_align_malloc( size, ctx->Const.MinMapBufferAlignment );
569 if (new_data) {
570 bufObj->Data = (GLubyte *) new_data;
571 bufObj->Size = size;
572 bufObj->Usage = usage;
573
574 if (data) {
575 memcpy( bufObj->Data, data, size );
576 }
577
578 return GL_TRUE;
579 }
580 else {
581 return GL_FALSE;
582 }
583 }
584
585
586 /**
587 * Replace data in a subrange of buffer object. If the data range
588 * specified by \c size + \c offset extends beyond the end of the buffer or
589 * if \c data is \c NULL, no copy is performed.
590 *
591 * This is the default callback for \c dd_function_table::BufferSubData()
592 * Note that all GL error checking will have been done already.
593 *
594 * \param ctx GL context.
595 * \param offset Offset of the first byte to be modified.
596 * \param size Size, in bytes, of the data range.
597 * \param data Pointer to the data to store in the buffer object.
598 * \param bufObj Object to be used.
599 *
600 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
601 */
602 static void
603 _mesa_buffer_subdata( struct gl_context *ctx, GLintptrARB offset,
604 GLsizeiptrARB size, const GLvoid * data,
605 struct gl_buffer_object * bufObj )
606 {
607 (void) ctx;
608
609 /* this should have been caught in _mesa_BufferSubData() */
610 ASSERT(size + offset <= bufObj->Size);
611
612 if (bufObj->Data) {
613 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
614 }
615 }
616
617
618 /**
619 * Retrieve data from a subrange of buffer object. If the data range
620 * specified by \c size + \c offset extends beyond the end of the buffer or
621 * if \c data is \c NULL, no copy is performed.
622 *
623 * This is the default callback for \c dd_function_table::GetBufferSubData()
624 * Note that all GL error checking will have been done already.
625 *
626 * \param ctx GL context.
627 * \param target Buffer object target on which to operate.
628 * \param offset Offset of the first byte to be fetched.
629 * \param size Size, in bytes, of the data range.
630 * \param data Destination for data
631 * \param bufObj Object to be used.
632 *
633 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
634 */
635 static void
636 _mesa_buffer_get_subdata( struct gl_context *ctx, GLintptrARB offset,
637 GLsizeiptrARB size, GLvoid * data,
638 struct gl_buffer_object * bufObj )
639 {
640 (void) ctx;
641
642 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
643 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
644 }
645 }
646
647
648 /**
649 * Clear a subrange of the buffer object with copies of the supplied data.
650 * If data is NULL the buffer is filled with zeros.
651 *
652 * This is the default callback for \c dd_function_table::ClearBufferSubData()
653 * Note that all GL error checking will have been done already.
654 *
655 * \param ctx GL context.
656 * \param offset Offset of the first byte to be cleared.
657 * \param size Size, in bytes, of the to be cleared range.
658 * \param clearValue Source of the data.
659 * \param clearValueSize Size, in bytes, of the supplied data.
660 * \param bufObj Object to be cleared.
661 *
662 * \sa glClearBufferSubData, glClearBufferData and
663 * dd_function_table::ClearBufferSubData.
664 */
665 static void
666 _mesa_buffer_clear_subdata(struct gl_context *ctx,
667 GLintptr offset, GLsizeiptr size,
668 const GLvoid *clearValue,
669 GLsizeiptr clearValueSize,
670 struct gl_buffer_object *bufObj)
671 {
672 GLsizeiptr i;
673 GLubyte *dest;
674
675 if (_mesa_bufferobj_mapped(bufObj)) {
676 GLubyte *data = malloc(size);
677 GLubyte *dataStart = data;
678 if (data == NULL) {
679 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
680 return;
681 }
682
683 if (clearValue == NULL) {
684 /* Clear with zeros, per the spec */
685 memset(data, 0, size);
686 }
687 else {
688 for (i = 0; i < size/clearValueSize; ++i) {
689 memcpy(data, clearValue, clearValueSize);
690 data += clearValueSize;
691 }
692 }
693 ctx->Driver.BufferSubData(ctx, offset, size, dataStart, bufObj);
694 return;
695 }
696
697 ASSERT(ctx->Driver.MapBufferRange);
698 dest = ctx->Driver.MapBufferRange(ctx, offset, size,
699 GL_MAP_WRITE_BIT |
700 GL_MAP_INVALIDATE_RANGE_BIT,
701 bufObj);
702
703 if (!dest) {
704 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearBuffer[Sub]Data");
705 return;
706 }
707
708 if (clearValue == NULL) {
709 /* Clear with zeros, per the spec */
710 memset(dest, 0, size);
711 ctx->Driver.UnmapBuffer(ctx, bufObj);
712 return;
713 }
714
715 for (i = 0; i < size/clearValueSize; ++i) {
716 memcpy(dest, clearValue, clearValueSize);
717 dest += clearValueSize;
718 }
719
720 ctx->Driver.UnmapBuffer(ctx, bufObj);
721 }
722
723
724 /**
725 * Default fallback for \c dd_function_table::MapBufferRange().
726 * Called via glMapBufferRange().
727 */
728 static void *
729 _mesa_buffer_map_range( struct gl_context *ctx, GLintptr offset,
730 GLsizeiptr length, GLbitfield access,
731 struct gl_buffer_object *bufObj )
732 {
733 (void) ctx;
734 assert(!_mesa_bufferobj_mapped(bufObj));
735 /* Just return a direct pointer to the data */
736 bufObj->Pointer = bufObj->Data + offset;
737 bufObj->Length = length;
738 bufObj->Offset = offset;
739 bufObj->AccessFlags = access;
740 return bufObj->Pointer;
741 }
742
743
744 /**
745 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
746 * Called via glFlushMappedBufferRange().
747 */
748 static void
749 _mesa_buffer_flush_mapped_range( struct gl_context *ctx,
750 GLintptr offset, GLsizeiptr length,
751 struct gl_buffer_object *obj )
752 {
753 (void) ctx;
754 (void) offset;
755 (void) length;
756 (void) obj;
757 /* no-op */
758 }
759
760
761 /**
762 * Default callback for \c dd_function_table::MapBuffer().
763 *
764 * The input parameters will have been already tested for errors.
765 *
766 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
767 */
768 static GLboolean
769 _mesa_buffer_unmap( struct gl_context *ctx, struct gl_buffer_object *bufObj )
770 {
771 (void) ctx;
772 /* XXX we might assert here that bufObj->Pointer is non-null */
773 bufObj->Pointer = NULL;
774 bufObj->Length = 0;
775 bufObj->Offset = 0;
776 bufObj->AccessFlags = 0x0;
777 return GL_TRUE;
778 }
779
780
781 /**
782 * Default fallback for \c dd_function_table::CopyBufferSubData().
783 * Called via glCopyBufferSubData().
784 */
785 static void
786 _mesa_copy_buffer_subdata(struct gl_context *ctx,
787 struct gl_buffer_object *src,
788 struct gl_buffer_object *dst,
789 GLintptr readOffset, GLintptr writeOffset,
790 GLsizeiptr size)
791 {
792 GLubyte *srcPtr, *dstPtr;
793
794 /* the buffers should not be mapped */
795 assert(!_mesa_bufferobj_mapped(src));
796 assert(!_mesa_bufferobj_mapped(dst));
797
798 if (src == dst) {
799 srcPtr = dstPtr = ctx->Driver.MapBufferRange(ctx, 0, src->Size,
800 GL_MAP_READ_BIT |
801 GL_MAP_WRITE_BIT, src);
802
803 if (!srcPtr)
804 return;
805
806 srcPtr += readOffset;
807 dstPtr += writeOffset;
808 } else {
809 srcPtr = ctx->Driver.MapBufferRange(ctx, readOffset, size,
810 GL_MAP_READ_BIT, src);
811 dstPtr = ctx->Driver.MapBufferRange(ctx, writeOffset, size,
812 (GL_MAP_WRITE_BIT |
813 GL_MAP_INVALIDATE_RANGE_BIT), dst);
814 }
815
816 /* Note: the src and dst regions will never overlap. Trying to do so
817 * would generate GL_INVALID_VALUE earlier.
818 */
819 if (srcPtr && dstPtr)
820 memcpy(dstPtr, srcPtr, size);
821
822 ctx->Driver.UnmapBuffer(ctx, src);
823 if (dst != src)
824 ctx->Driver.UnmapBuffer(ctx, dst);
825 }
826
827
828
829 /**
830 * Initialize the state associated with buffer objects
831 */
832 void
833 _mesa_init_buffer_objects( struct gl_context *ctx )
834 {
835 GLuint i;
836
837 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
838 _glthread_INIT_MUTEX(DummyBufferObject.Mutex);
839 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
840
841 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
842 ctx->Shared->NullBufferObj);
843
844 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
845 ctx->Shared->NullBufferObj);
846 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
847 ctx->Shared->NullBufferObj);
848
849 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer,
850 ctx->Shared->NullBufferObj);
851
852 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer,
853 ctx->Shared->NullBufferObj);
854
855 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
856 _mesa_reference_buffer_object(ctx,
857 &ctx->UniformBufferBindings[i].BufferObject,
858 ctx->Shared->NullBufferObj);
859 ctx->UniformBufferBindings[i].Offset = -1;
860 ctx->UniformBufferBindings[i].Size = -1;
861 }
862 }
863
864
865 void
866 _mesa_free_buffer_objects( struct gl_context *ctx )
867 {
868 GLuint i;
869
870 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
871
872 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
873 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
874
875 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, NULL);
876
877 _mesa_reference_buffer_object(ctx, &ctx->DrawIndirectBuffer, NULL);
878
879 for (i = 0; i < MAX_COMBINED_UNIFORM_BUFFERS; i++) {
880 _mesa_reference_buffer_object(ctx,
881 &ctx->UniformBufferBindings[i].BufferObject,
882 NULL);
883 }
884 }
885
886 bool
887 _mesa_handle_bind_buffer_gen(struct gl_context *ctx,
888 GLenum target,
889 GLuint buffer,
890 struct gl_buffer_object **buf_handle,
891 const char *caller)
892 {
893 struct gl_buffer_object *buf = *buf_handle;
894
895 if (!buf && ctx->API == API_OPENGL_CORE) {
896 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(non-gen name)", caller);
897 return false;
898 }
899
900 if (!buf || buf == &DummyBufferObject) {
901 /* If this is a new buffer object id, or one which was generated but
902 * never used before, allocate a buffer object now.
903 */
904 ASSERT(ctx->Driver.NewBufferObject);
905 buf = ctx->Driver.NewBufferObject(ctx, buffer, target);
906 if (!buf) {
907 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
908 return false;
909 }
910 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, buf);
911 *buf_handle = buf;
912 }
913
914 return true;
915 }
916
917 /**
918 * Bind the specified target to buffer for the specified context.
919 * Called by glBindBuffer() and other functions.
920 */
921 static void
922 bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
923 {
924 struct gl_buffer_object *oldBufObj;
925 struct gl_buffer_object *newBufObj = NULL;
926 struct gl_buffer_object **bindTarget = NULL;
927
928 bindTarget = get_buffer_target(ctx, target);
929 if (!bindTarget) {
930 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
931 return;
932 }
933
934 /* Get pointer to old buffer object (to be unbound) */
935 oldBufObj = *bindTarget;
936 if (oldBufObj && oldBufObj->Name == buffer && !oldBufObj->DeletePending)
937 return; /* rebinding the same buffer object- no change */
938
939 /*
940 * Get pointer to new buffer object (newBufObj)
941 */
942 if (buffer == 0) {
943 /* The spec says there's not a buffer object named 0, but we use
944 * one internally because it simplifies things.
945 */
946 newBufObj = ctx->Shared->NullBufferObj;
947 }
948 else {
949 /* non-default buffer object */
950 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
951 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
952 &newBufObj, "glBindBuffer"))
953 return;
954 }
955
956 /* bind new buffer */
957 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
958
959 /* Pass BindBuffer call to device driver */
960 if (ctx->Driver.BindBuffer)
961 ctx->Driver.BindBuffer( ctx, target, newBufObj );
962 }
963
964
965 /**
966 * Update the default buffer objects in the given context to reference those
967 * specified in the shared state and release those referencing the old
968 * shared state.
969 */
970 void
971 _mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
972 {
973 /* Bind the NullBufferObj to remove references to those
974 * in the shared context hash table.
975 */
976 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
977 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
978 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
979 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
980 }
981
982
983
984 /**
985 * Return the gl_buffer_object for the given ID.
986 * Always return NULL for ID 0.
987 */
988 struct gl_buffer_object *
989 _mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
990 {
991 if (buffer == 0)
992 return NULL;
993 else
994 return (struct gl_buffer_object *)
995 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
996 }
997
998
999 /**
1000 * If *ptr points to obj, set ptr = the Null/default buffer object.
1001 * This is a helper for buffer object deletion.
1002 * The GL spec says that deleting a buffer object causes it to get
1003 * unbound from all arrays in the current context.
1004 */
1005 static void
1006 unbind(struct gl_context *ctx,
1007 struct gl_buffer_object **ptr,
1008 struct gl_buffer_object *obj)
1009 {
1010 if (*ptr == obj) {
1011 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
1012 }
1013 }
1014
1015
1016 /**
1017 * Plug default/fallback buffer object functions into the device
1018 * driver hooks.
1019 */
1020 void
1021 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
1022 {
1023 /* GL_ARB_vertex/pixel_buffer_object */
1024 driver->NewBufferObject = _mesa_new_buffer_object;
1025 driver->DeleteBuffer = _mesa_delete_buffer_object;
1026 driver->BindBuffer = NULL;
1027 driver->BufferData = _mesa_buffer_data;
1028 driver->BufferSubData = _mesa_buffer_subdata;
1029 driver->GetBufferSubData = _mesa_buffer_get_subdata;
1030 driver->UnmapBuffer = _mesa_buffer_unmap;
1031
1032 /* GL_ARB_clear_buffer_object */
1033 driver->ClearBufferSubData = _mesa_buffer_clear_subdata;
1034
1035 /* GL_ARB_map_buffer_range */
1036 driver->MapBufferRange = _mesa_buffer_map_range;
1037 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
1038
1039 /* GL_ARB_copy_buffer */
1040 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
1041 }
1042
1043
1044
1045 /**********************************************************************/
1046 /* API Functions */
1047 /**********************************************************************/
1048
1049 void GLAPIENTRY
1050 _mesa_BindBuffer(GLenum target, GLuint buffer)
1051 {
1052 GET_CURRENT_CONTEXT(ctx);
1053
1054 if (MESA_VERBOSE & VERBOSE_API)
1055 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
1056 _mesa_lookup_enum_by_nr(target), buffer);
1057
1058 bind_buffer_object(ctx, target, buffer);
1059 }
1060
1061
1062 /**
1063 * Delete a set of buffer objects.
1064 *
1065 * \param n Number of buffer objects to delete.
1066 * \param ids Array of \c n buffer object IDs.
1067 */
1068 void GLAPIENTRY
1069 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1070 {
1071 GET_CURRENT_CONTEXT(ctx);
1072 GLsizei i;
1073 FLUSH_VERTICES(ctx, 0);
1074
1075 if (n < 0) {
1076 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1077 return;
1078 }
1079
1080 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1081
1082 for (i = 0; i < n; i++) {
1083 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
1084 if (bufObj) {
1085 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1086 GLuint j;
1087
1088 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
1089
1090 if (_mesa_bufferobj_mapped(bufObj)) {
1091 /* if mapped, unmap it now */
1092 ctx->Driver.UnmapBuffer(ctx, bufObj);
1093 bufObj->AccessFlags = 0;
1094 bufObj->Pointer = NULL;
1095 }
1096
1097 /* unbind any vertex pointers bound to this buffer */
1098 for (j = 0; j < Elements(vao->VertexBinding); j++) {
1099 unbind(ctx, &vao->VertexBinding[j].BufferObj, bufObj);
1100 }
1101
1102 if (ctx->Array.ArrayBufferObj == bufObj) {
1103 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
1104 }
1105 if (vao->ElementArrayBufferObj == bufObj) {
1106 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
1107 }
1108
1109 /* unbind ARB_draw_indirect binding point */
1110 if (ctx->DrawIndirectBuffer == bufObj) {
1111 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 );
1112 }
1113
1114 /* unbind ARB_copy_buffer binding points */
1115 if (ctx->CopyReadBuffer == bufObj) {
1116 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
1117 }
1118 if (ctx->CopyWriteBuffer == bufObj) {
1119 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
1120 }
1121
1122 /* unbind transform feedback binding points */
1123 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
1124 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
1125 }
1126 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1127 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
1128 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
1129 }
1130 }
1131
1132 /* unbind UBO binding points */
1133 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1134 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
1135 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
1136 }
1137 }
1138
1139 if (ctx->UniformBuffer == bufObj) {
1140 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
1141 }
1142
1143 /* unbind any pixel pack/unpack pointers bound to this buffer */
1144 if (ctx->Pack.BufferObj == bufObj) {
1145 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
1146 }
1147 if (ctx->Unpack.BufferObj == bufObj) {
1148 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
1149 }
1150
1151 if (ctx->Texture.BufferObject == bufObj) {
1152 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
1153 }
1154
1155 /* The ID is immediately freed for re-use */
1156 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
1157 /* Make sure we do not run into the classic ABA problem on bind.
1158 * We don't want to allow re-binding a buffer object that's been
1159 * "deleted" by glDeleteBuffers().
1160 *
1161 * The explicit rebinding to the default object in the current context
1162 * prevents the above in the current context, but another context
1163 * sharing the same objects might suffer from this problem.
1164 * The alternative would be to do the hash lookup in any case on bind
1165 * which would introduce more runtime overhead than this.
1166 */
1167 bufObj->DeletePending = GL_TRUE;
1168 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
1169 }
1170 }
1171
1172 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1173 }
1174
1175
1176 /**
1177 * Generate a set of unique buffer object IDs and store them in \c buffer.
1178 *
1179 * \param n Number of IDs to generate.
1180 * \param buffer Array of \c n locations to store the IDs.
1181 */
1182 void GLAPIENTRY
1183 _mesa_GenBuffers(GLsizei n, GLuint *buffer)
1184 {
1185 GET_CURRENT_CONTEXT(ctx);
1186 GLuint first;
1187 GLint i;
1188
1189 if (MESA_VERBOSE & VERBOSE_API)
1190 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
1191
1192 if (n < 0) {
1193 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
1194 return;
1195 }
1196
1197 if (!buffer) {
1198 return;
1199 }
1200
1201 /*
1202 * This must be atomic (generation and allocation of buffer object IDs)
1203 */
1204 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1205
1206 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1207
1208 /* Insert the ID and pointer to dummy buffer object into hash table */
1209 for (i = 0; i < n; i++) {
1210 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
1211 &DummyBufferObject);
1212 buffer[i] = first + i;
1213 }
1214
1215 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1216 }
1217
1218
1219 /**
1220 * Determine if ID is the name of a buffer object.
1221 *
1222 * \param id ID of the potential buffer object.
1223 * \return \c GL_TRUE if \c id is the name of a buffer object,
1224 * \c GL_FALSE otherwise.
1225 */
1226 GLboolean GLAPIENTRY
1227 _mesa_IsBuffer(GLuint id)
1228 {
1229 struct gl_buffer_object *bufObj;
1230 GET_CURRENT_CONTEXT(ctx);
1231 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1232
1233 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1234 bufObj = _mesa_lookup_bufferobj(ctx, id);
1235 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1236
1237 return bufObj && bufObj != &DummyBufferObject;
1238 }
1239
1240
1241 void GLAPIENTRY
1242 _mesa_BufferData(GLenum target, GLsizeiptrARB size,
1243 const GLvoid * data, GLenum usage)
1244 {
1245 GET_CURRENT_CONTEXT(ctx);
1246 struct gl_buffer_object *bufObj;
1247 bool valid_usage;
1248
1249 if (MESA_VERBOSE & VERBOSE_API)
1250 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
1251 _mesa_lookup_enum_by_nr(target),
1252 (long int) size, data,
1253 _mesa_lookup_enum_by_nr(usage));
1254
1255 if (size < 0) {
1256 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1257 return;
1258 }
1259
1260 switch (usage) {
1261 case GL_STREAM_DRAW_ARB:
1262 valid_usage = (ctx->API != API_OPENGLES);
1263 break;
1264
1265 case GL_STATIC_DRAW_ARB:
1266 case GL_DYNAMIC_DRAW_ARB:
1267 valid_usage = true;
1268 break;
1269
1270 case GL_STREAM_READ_ARB:
1271 case GL_STREAM_COPY_ARB:
1272 case GL_STATIC_READ_ARB:
1273 case GL_STATIC_COPY_ARB:
1274 case GL_DYNAMIC_READ_ARB:
1275 case GL_DYNAMIC_COPY_ARB:
1276 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1277 break;
1278
1279 default:
1280 valid_usage = false;
1281 break;
1282 }
1283
1284 if (!valid_usage) {
1285 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
1286 return;
1287 }
1288
1289 bufObj = get_buffer(ctx, "glBufferDataARB", target, GL_INVALID_OPERATION);
1290 if (!bufObj)
1291 return;
1292
1293 if (_mesa_bufferobj_mapped(bufObj)) {
1294 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1295 ctx->Driver.UnmapBuffer(ctx, bufObj);
1296 bufObj->AccessFlags = 0;
1297 ASSERT(bufObj->Pointer == NULL);
1298 }
1299
1300 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1301
1302 bufObj->Written = GL_TRUE;
1303
1304 #ifdef VBO_DEBUG
1305 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1306 bufObj->Name, size, data, usage);
1307 #endif
1308
1309 #ifdef BOUNDS_CHECK
1310 size += 100;
1311 #endif
1312
1313 ASSERT(ctx->Driver.BufferData);
1314 if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
1315 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
1316 }
1317 }
1318
1319
1320 void GLAPIENTRY
1321 _mesa_BufferSubData(GLenum target, GLintptrARB offset,
1322 GLsizeiptrARB size, const GLvoid * data)
1323 {
1324 GET_CURRENT_CONTEXT(ctx);
1325 struct gl_buffer_object *bufObj;
1326
1327 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1328 false, GL_INVALID_OPERATION,
1329 "glBufferSubDataARB" );
1330 if (!bufObj) {
1331 /* error already recorded */
1332 return;
1333 }
1334
1335 if (size == 0)
1336 return;
1337
1338 bufObj->Written = GL_TRUE;
1339
1340 ASSERT(ctx->Driver.BufferSubData);
1341 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1342 }
1343
1344
1345 void GLAPIENTRY
1346 _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
1347 GLsizeiptrARB size, void * data)
1348 {
1349 GET_CURRENT_CONTEXT(ctx);
1350 struct gl_buffer_object *bufObj;
1351
1352 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1353 false, GL_INVALID_OPERATION,
1354 "glGetBufferSubDataARB");
1355 if (!bufObj) {
1356 /* error already recorded */
1357 return;
1358 }
1359
1360 ASSERT(ctx->Driver.GetBufferSubData);
1361 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1362 }
1363
1364
1365 void GLAPIENTRY
1366 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
1367 GLenum type, const GLvoid* data)
1368 {
1369 GET_CURRENT_CONTEXT(ctx);
1370 struct gl_buffer_object* bufObj;
1371 mesa_format mesaFormat;
1372 GLubyte clearValue[MAX_PIXEL_BYTES];
1373 GLsizeiptr clearValueSize;
1374
1375 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
1376 if (!bufObj) {
1377 return;
1378 }
1379
1380 if (_mesa_bufferobj_mapped(bufObj)) {
1381 _mesa_error(ctx, GL_INVALID_OPERATION,
1382 "glClearBufferData(buffer currently mapped)");
1383 return;
1384 }
1385
1386 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1387 format, type,
1388 "glClearBufferData");
1389 if (mesaFormat == MESA_FORMAT_NONE) {
1390 return;
1391 }
1392
1393 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1394 if (bufObj->Size % clearValueSize != 0) {
1395 _mesa_error(ctx, GL_INVALID_VALUE,
1396 "glClearBufferData(size is not a multiple of "
1397 "internalformat size)");
1398 return;
1399 }
1400
1401 if (data == NULL) {
1402 /* clear to zeros, per the spec */
1403 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1404 NULL, 0, bufObj);
1405 return;
1406 }
1407
1408 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1409 format, type, data, "glClearBufferData")) {
1410 return;
1411 }
1412
1413 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1414 clearValue, clearValueSize, bufObj);
1415 }
1416
1417
1418 void GLAPIENTRY
1419 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
1420 GLintptr offset, GLsizeiptr size,
1421 GLenum format, GLenum type,
1422 const GLvoid* data)
1423 {
1424 GET_CURRENT_CONTEXT(ctx);
1425 struct gl_buffer_object* bufObj;
1426 mesa_format mesaFormat;
1427 GLubyte clearValue[MAX_PIXEL_BYTES];
1428 GLsizeiptr clearValueSize;
1429
1430 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1431 true, GL_INVALID_VALUE,
1432 "glClearBufferSubData");
1433 if (!bufObj) {
1434 return;
1435 }
1436
1437 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1438 format, type,
1439 "glClearBufferSubData");
1440 if (mesaFormat == MESA_FORMAT_NONE) {
1441 return;
1442 }
1443
1444 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1445 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
1446 _mesa_error(ctx, GL_INVALID_VALUE,
1447 "glClearBufferSubData(offset or size is not a multiple of "
1448 "internalformat size)");
1449 return;
1450 }
1451
1452 if (data == NULL) {
1453 /* clear to zeros, per the spec */
1454 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1455 NULL, 0, bufObj);
1456 return;
1457 }
1458
1459 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1460 format, type, data,
1461 "glClearBufferSubData")) {
1462 return;
1463 }
1464
1465 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1466 clearValue, clearValueSize, bufObj);
1467 }
1468
1469
1470 void * GLAPIENTRY
1471 _mesa_MapBuffer(GLenum target, GLenum access)
1472 {
1473 GET_CURRENT_CONTEXT(ctx);
1474 struct gl_buffer_object * bufObj;
1475 GLbitfield accessFlags;
1476 void *map;
1477 bool valid_access;
1478
1479 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1480
1481 switch (access) {
1482 case GL_READ_ONLY_ARB:
1483 accessFlags = GL_MAP_READ_BIT;
1484 valid_access = _mesa_is_desktop_gl(ctx);
1485 break;
1486 case GL_WRITE_ONLY_ARB:
1487 accessFlags = GL_MAP_WRITE_BIT;
1488 valid_access = true;
1489 break;
1490 case GL_READ_WRITE_ARB:
1491 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1492 valid_access = _mesa_is_desktop_gl(ctx);
1493 break;
1494 default:
1495 valid_access = false;
1496 break;
1497 }
1498
1499 if (!valid_access) {
1500 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1501 return NULL;
1502 }
1503
1504 bufObj = get_buffer(ctx, "glMapBufferARB", target, GL_INVALID_OPERATION);
1505 if (!bufObj)
1506 return NULL;
1507
1508 if (_mesa_bufferobj_mapped(bufObj)) {
1509 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1510 return NULL;
1511 }
1512
1513 if (!bufObj->Size) {
1514 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1515 "glMapBuffer(buffer size = 0)");
1516 return NULL;
1517 }
1518
1519 ASSERT(ctx->Driver.MapBufferRange);
1520 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj);
1521 if (!map) {
1522 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1523 return NULL;
1524 }
1525 else {
1526 /* The driver callback should have set these fields.
1527 * This is important because other modules (like VBO) might call
1528 * the driver function directly.
1529 */
1530 ASSERT(bufObj->Pointer == map);
1531 ASSERT(bufObj->Length == bufObj->Size);
1532 ASSERT(bufObj->Offset == 0);
1533 bufObj->AccessFlags = accessFlags;
1534 }
1535
1536 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1537 bufObj->Written = GL_TRUE;
1538
1539 #ifdef VBO_DEBUG
1540 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1541 bufObj->Name, bufObj->Size, access);
1542 if (access == GL_WRITE_ONLY_ARB) {
1543 GLuint i;
1544 GLubyte *b = (GLubyte *) bufObj->Pointer;
1545 for (i = 0; i < bufObj->Size; i++)
1546 b[i] = i & 0xff;
1547 }
1548 #endif
1549
1550 #ifdef BOUNDS_CHECK
1551 {
1552 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1553 GLuint i;
1554 /* buffer is 100 bytes larger than requested, fill with magic value */
1555 for (i = 0; i < 100; i++) {
1556 buf[bufObj->Size - i - 1] = 123;
1557 }
1558 }
1559 #endif
1560
1561 return bufObj->Pointer;
1562 }
1563
1564
1565 GLboolean GLAPIENTRY
1566 _mesa_UnmapBuffer(GLenum target)
1567 {
1568 GET_CURRENT_CONTEXT(ctx);
1569 struct gl_buffer_object *bufObj;
1570 GLboolean status = GL_TRUE;
1571 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1572
1573 bufObj = get_buffer(ctx, "glUnmapBufferARB", target, GL_INVALID_OPERATION);
1574 if (!bufObj)
1575 return GL_FALSE;
1576
1577 if (!_mesa_bufferobj_mapped(bufObj)) {
1578 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1579 return GL_FALSE;
1580 }
1581
1582 #ifdef BOUNDS_CHECK
1583 if (bufObj->Access != GL_READ_ONLY_ARB) {
1584 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1585 GLuint i;
1586 /* check that last 100 bytes are still = magic value */
1587 for (i = 0; i < 100; i++) {
1588 GLuint pos = bufObj->Size - i - 1;
1589 if (buf[pos] != 123) {
1590 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1591 " at position %d (value = %u)\n",
1592 pos, buf[pos]);
1593 }
1594 }
1595 }
1596 #endif
1597
1598 #ifdef VBO_DEBUG
1599 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1600 GLuint i, unchanged = 0;
1601 GLubyte *b = (GLubyte *) bufObj->Pointer;
1602 GLint pos = -1;
1603 /* check which bytes changed */
1604 for (i = 0; i < bufObj->Size - 1; i++) {
1605 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1606 unchanged++;
1607 if (pos == -1)
1608 pos = i;
1609 }
1610 }
1611 if (unchanged) {
1612 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1613 bufObj->Name, unchanged, bufObj->Size, pos);
1614 }
1615 }
1616 #endif
1617
1618 status = ctx->Driver.UnmapBuffer( ctx, bufObj );
1619 bufObj->AccessFlags = 0;
1620 ASSERT(bufObj->Pointer == NULL);
1621 ASSERT(bufObj->Offset == 0);
1622 ASSERT(bufObj->Length == 0);
1623
1624 return status;
1625 }
1626
1627
1628 void GLAPIENTRY
1629 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
1630 {
1631 GET_CURRENT_CONTEXT(ctx);
1632 struct gl_buffer_object *bufObj;
1633
1634 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target,
1635 GL_INVALID_OPERATION);
1636 if (!bufObj)
1637 return;
1638
1639 switch (pname) {
1640 case GL_BUFFER_SIZE_ARB:
1641 *params = (GLint) bufObj->Size;
1642 return;
1643 case GL_BUFFER_USAGE_ARB:
1644 *params = bufObj->Usage;
1645 return;
1646 case GL_BUFFER_ACCESS_ARB:
1647 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1648 return;
1649 case GL_BUFFER_MAPPED_ARB:
1650 *params = _mesa_bufferobj_mapped(bufObj);
1651 return;
1652 case GL_BUFFER_ACCESS_FLAGS:
1653 if (!ctx->Extensions.ARB_map_buffer_range)
1654 goto invalid_pname;
1655 *params = bufObj->AccessFlags;
1656 return;
1657 case GL_BUFFER_MAP_OFFSET:
1658 if (!ctx->Extensions.ARB_map_buffer_range)
1659 goto invalid_pname;
1660 *params = (GLint) bufObj->Offset;
1661 return;
1662 case GL_BUFFER_MAP_LENGTH:
1663 if (!ctx->Extensions.ARB_map_buffer_range)
1664 goto invalid_pname;
1665 *params = (GLint) bufObj->Length;
1666 return;
1667 default:
1668 ; /* fall-through */
1669 }
1670
1671 invalid_pname:
1672 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1673 _mesa_lookup_enum_by_nr(pname));
1674 }
1675
1676
1677 /**
1678 * New in GL 3.2
1679 * This is pretty much a duplicate of GetBufferParameteriv() but the
1680 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1681 */
1682 void GLAPIENTRY
1683 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1684 {
1685 GET_CURRENT_CONTEXT(ctx);
1686 struct gl_buffer_object *bufObj;
1687
1688 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
1689 GL_INVALID_OPERATION);
1690 if (!bufObj)
1691 return;
1692
1693 switch (pname) {
1694 case GL_BUFFER_SIZE_ARB:
1695 *params = bufObj->Size;
1696 return;
1697 case GL_BUFFER_USAGE_ARB:
1698 *params = bufObj->Usage;
1699 return;
1700 case GL_BUFFER_ACCESS_ARB:
1701 *params = simplified_access_mode(ctx, bufObj->AccessFlags);
1702 return;
1703 case GL_BUFFER_ACCESS_FLAGS:
1704 if (!ctx->Extensions.ARB_map_buffer_range)
1705 goto invalid_pname;
1706 *params = bufObj->AccessFlags;
1707 return;
1708 case GL_BUFFER_MAPPED_ARB:
1709 *params = _mesa_bufferobj_mapped(bufObj);
1710 return;
1711 case GL_BUFFER_MAP_OFFSET:
1712 if (!ctx->Extensions.ARB_map_buffer_range)
1713 goto invalid_pname;
1714 *params = bufObj->Offset;
1715 return;
1716 case GL_BUFFER_MAP_LENGTH:
1717 if (!ctx->Extensions.ARB_map_buffer_range)
1718 goto invalid_pname;
1719 *params = bufObj->Length;
1720 return;
1721 default:
1722 ; /* fall-through */
1723 }
1724
1725 invalid_pname:
1726 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1727 _mesa_lookup_enum_by_nr(pname));
1728 }
1729
1730
1731 void GLAPIENTRY
1732 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
1733 {
1734 GET_CURRENT_CONTEXT(ctx);
1735 struct gl_buffer_object * bufObj;
1736
1737 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1738 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1739 return;
1740 }
1741
1742 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target,
1743 GL_INVALID_OPERATION);
1744 if (!bufObj)
1745 return;
1746
1747 *params = bufObj->Pointer;
1748 }
1749
1750
1751 void GLAPIENTRY
1752 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1753 GLintptr readOffset, GLintptr writeOffset,
1754 GLsizeiptr size)
1755 {
1756 GET_CURRENT_CONTEXT(ctx);
1757 struct gl_buffer_object *src, *dst;
1758
1759 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
1760 GL_INVALID_OPERATION);
1761 if (!src)
1762 return;
1763
1764 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
1765 GL_INVALID_OPERATION);
1766 if (!dst)
1767 return;
1768
1769 if (_mesa_bufferobj_mapped(src)) {
1770 _mesa_error(ctx, GL_INVALID_OPERATION,
1771 "glCopyBufferSubData(readBuffer is mapped)");
1772 return;
1773 }
1774
1775 if (_mesa_bufferobj_mapped(dst)) {
1776 _mesa_error(ctx, GL_INVALID_OPERATION,
1777 "glCopyBufferSubData(writeBuffer is mapped)");
1778 return;
1779 }
1780
1781 if (readOffset < 0) {
1782 _mesa_error(ctx, GL_INVALID_VALUE,
1783 "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
1784 return;
1785 }
1786
1787 if (writeOffset < 0) {
1788 _mesa_error(ctx, GL_INVALID_VALUE,
1789 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
1790 return;
1791 }
1792
1793 if (size < 0) {
1794 _mesa_error(ctx, GL_INVALID_VALUE,
1795 "glCopyBufferSubData(writeOffset = %d)", (int) size);
1796 return;
1797 }
1798
1799 if (readOffset + size > src->Size) {
1800 _mesa_error(ctx, GL_INVALID_VALUE,
1801 "glCopyBufferSubData(readOffset + size = %d)",
1802 (int) (readOffset + size));
1803 return;
1804 }
1805
1806 if (writeOffset + size > dst->Size) {
1807 _mesa_error(ctx, GL_INVALID_VALUE,
1808 "glCopyBufferSubData(writeOffset + size = %d)",
1809 (int) (writeOffset + size));
1810 return;
1811 }
1812
1813 if (src == dst) {
1814 if (readOffset + size <= writeOffset) {
1815 /* OK */
1816 }
1817 else if (writeOffset + size <= readOffset) {
1818 /* OK */
1819 }
1820 else {
1821 /* overlapping src/dst is illegal */
1822 _mesa_error(ctx, GL_INVALID_VALUE,
1823 "glCopyBufferSubData(overlapping src/dst)");
1824 return;
1825 }
1826 }
1827
1828 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1829 }
1830
1831
1832 /**
1833 * See GL_ARB_map_buffer_range spec
1834 */
1835 void * GLAPIENTRY
1836 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1837 GLbitfield access)
1838 {
1839 GET_CURRENT_CONTEXT(ctx);
1840 struct gl_buffer_object *bufObj;
1841 void *map;
1842
1843 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1844
1845 if (!ctx->Extensions.ARB_map_buffer_range) {
1846 _mesa_error(ctx, GL_INVALID_OPERATION,
1847 "glMapBufferRange(extension not supported)");
1848 return NULL;
1849 }
1850
1851 if (offset < 0) {
1852 _mesa_error(ctx, GL_INVALID_VALUE,
1853 "glMapBufferRange(offset = %ld)", (long)offset);
1854 return NULL;
1855 }
1856
1857 if (length < 0) {
1858 _mesa_error(ctx, GL_INVALID_VALUE,
1859 "glMapBufferRange(length = %ld)", (long)length);
1860 return NULL;
1861 }
1862
1863 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
1864 *
1865 * "An INVALID_OPERATION error is generated for any of the following
1866 * conditions:
1867 *
1868 * * <length> is zero."
1869 */
1870 if (_mesa_is_gles(ctx) && length == 0) {
1871 _mesa_error(ctx, GL_INVALID_OPERATION,
1872 "glMapBufferRange(length = 0)");
1873 return NULL;
1874 }
1875
1876 if (access & ~(GL_MAP_READ_BIT |
1877 GL_MAP_WRITE_BIT |
1878 GL_MAP_INVALIDATE_RANGE_BIT |
1879 GL_MAP_INVALIDATE_BUFFER_BIT |
1880 GL_MAP_FLUSH_EXPLICIT_BIT |
1881 GL_MAP_UNSYNCHRONIZED_BIT)) {
1882 /* generate an error if any undefind bit is set */
1883 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
1884 return NULL;
1885 }
1886
1887 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1888 _mesa_error(ctx, GL_INVALID_OPERATION,
1889 "glMapBufferRange(access indicates neither read or write)");
1890 return NULL;
1891 }
1892
1893 if ((access & GL_MAP_READ_BIT) &&
1894 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
1895 GL_MAP_INVALIDATE_BUFFER_BIT |
1896 GL_MAP_UNSYNCHRONIZED_BIT))) {
1897 _mesa_error(ctx, GL_INVALID_OPERATION,
1898 "glMapBufferRange(invalid access flags)");
1899 return NULL;
1900 }
1901
1902 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1903 ((access & GL_MAP_WRITE_BIT) == 0)) {
1904 _mesa_error(ctx, GL_INVALID_OPERATION,
1905 "glMapBufferRange(invalid access flags)");
1906 return NULL;
1907 }
1908
1909 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
1910 if (!bufObj)
1911 return NULL;
1912
1913 if (offset + length > bufObj->Size) {
1914 _mesa_error(ctx, GL_INVALID_VALUE,
1915 "glMapBufferRange(offset + length > size)");
1916 return NULL;
1917 }
1918
1919 if (_mesa_bufferobj_mapped(bufObj)) {
1920 _mesa_error(ctx, GL_INVALID_OPERATION,
1921 "glMapBufferRange(buffer already mapped)");
1922 return NULL;
1923 }
1924
1925 if (!bufObj->Size) {
1926 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1927 "glMapBufferRange(buffer size = 0)");
1928 return NULL;
1929 }
1930
1931 /* Mapping zero bytes should return a non-null pointer. */
1932 if (!length) {
1933 static long dummy = 0;
1934 bufObj->Pointer = &dummy;
1935 bufObj->Length = length;
1936 bufObj->Offset = offset;
1937 bufObj->AccessFlags = access;
1938 return bufObj->Pointer;
1939 }
1940
1941 ASSERT(ctx->Driver.MapBufferRange);
1942 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj);
1943 if (!map) {
1944 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1945 }
1946 else {
1947 /* The driver callback should have set all these fields.
1948 * This is important because other modules (like VBO) might call
1949 * the driver function directly.
1950 */
1951 ASSERT(bufObj->Pointer == map);
1952 ASSERT(bufObj->Length == length);
1953 ASSERT(bufObj->Offset == offset);
1954 ASSERT(bufObj->AccessFlags == access);
1955 }
1956
1957 return map;
1958 }
1959
1960
1961 /**
1962 * See GL_ARB_map_buffer_range spec
1963 */
1964 void GLAPIENTRY
1965 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1966 {
1967 GET_CURRENT_CONTEXT(ctx);
1968 struct gl_buffer_object *bufObj;
1969
1970 if (!ctx->Extensions.ARB_map_buffer_range) {
1971 _mesa_error(ctx, GL_INVALID_OPERATION,
1972 "glFlushMappedBufferRange(extension not supported)");
1973 return;
1974 }
1975
1976 if (offset < 0) {
1977 _mesa_error(ctx, GL_INVALID_VALUE,
1978 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
1979 return;
1980 }
1981
1982 if (length < 0) {
1983 _mesa_error(ctx, GL_INVALID_VALUE,
1984 "glFlushMappedBufferRange(length = %ld)", (long)length);
1985 return;
1986 }
1987
1988 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
1989 GL_INVALID_OPERATION);
1990 if (!bufObj)
1991 return;
1992
1993 if (!_mesa_bufferobj_mapped(bufObj)) {
1994 /* buffer is not mapped */
1995 _mesa_error(ctx, GL_INVALID_OPERATION,
1996 "glFlushMappedBufferRange(buffer is not mapped)");
1997 return;
1998 }
1999
2000 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
2001 _mesa_error(ctx, GL_INVALID_OPERATION,
2002 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
2003 return;
2004 }
2005
2006 if (offset + length > bufObj->Length) {
2007 _mesa_error(ctx, GL_INVALID_VALUE,
2008 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
2009 (long)offset, (long)length, (long)bufObj->Length);
2010 return;
2011 }
2012
2013 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
2014
2015 if (ctx->Driver.FlushMappedBufferRange)
2016 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj);
2017 }
2018
2019
2020 static GLenum
2021 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2022 {
2023 struct gl_buffer_object *bufObj;
2024 GLenum retval;
2025
2026 bufObj = _mesa_lookup_bufferobj(ctx, name);
2027 if (!bufObj) {
2028 _mesa_error(ctx, GL_INVALID_VALUE,
2029 "glObjectPurgeable(name = 0x%x)", name);
2030 return 0;
2031 }
2032 if (!_mesa_is_bufferobj(bufObj)) {
2033 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
2034 return 0;
2035 }
2036
2037 if (bufObj->Purgeable) {
2038 _mesa_error(ctx, GL_INVALID_OPERATION,
2039 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2040 return GL_VOLATILE_APPLE;
2041 }
2042
2043 bufObj->Purgeable = GL_TRUE;
2044
2045 retval = GL_VOLATILE_APPLE;
2046 if (ctx->Driver.BufferObjectPurgeable)
2047 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
2048
2049 return retval;
2050 }
2051
2052
2053 static GLenum
2054 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2055 {
2056 struct gl_renderbuffer *bufObj;
2057 GLenum retval;
2058
2059 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2060 if (!bufObj) {
2061 _mesa_error(ctx, GL_INVALID_VALUE,
2062 "glObjectUnpurgeable(name = 0x%x)", name);
2063 return 0;
2064 }
2065
2066 if (bufObj->Purgeable) {
2067 _mesa_error(ctx, GL_INVALID_OPERATION,
2068 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2069 return GL_VOLATILE_APPLE;
2070 }
2071
2072 bufObj->Purgeable = GL_TRUE;
2073
2074 retval = GL_VOLATILE_APPLE;
2075 if (ctx->Driver.RenderObjectPurgeable)
2076 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
2077
2078 return retval;
2079 }
2080
2081
2082 static GLenum
2083 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2084 {
2085 struct gl_texture_object *bufObj;
2086 GLenum retval;
2087
2088 bufObj = _mesa_lookup_texture(ctx, name);
2089 if (!bufObj) {
2090 _mesa_error(ctx, GL_INVALID_VALUE,
2091 "glObjectPurgeable(name = 0x%x)", name);
2092 return 0;
2093 }
2094
2095 if (bufObj->Purgeable) {
2096 _mesa_error(ctx, GL_INVALID_OPERATION,
2097 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2098 return GL_VOLATILE_APPLE;
2099 }
2100
2101 bufObj->Purgeable = GL_TRUE;
2102
2103 retval = GL_VOLATILE_APPLE;
2104 if (ctx->Driver.TextureObjectPurgeable)
2105 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
2106
2107 return retval;
2108 }
2109
2110
2111 GLenum GLAPIENTRY
2112 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2113 {
2114 GLenum retval;
2115
2116 GET_CURRENT_CONTEXT(ctx);
2117 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2118
2119 if (name == 0) {
2120 _mesa_error(ctx, GL_INVALID_VALUE,
2121 "glObjectPurgeable(name = 0x%x)", name);
2122 return 0;
2123 }
2124
2125 switch (option) {
2126 case GL_VOLATILE_APPLE:
2127 case GL_RELEASED_APPLE:
2128 /* legal */
2129 break;
2130 default:
2131 _mesa_error(ctx, GL_INVALID_ENUM,
2132 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2133 name, option);
2134 return 0;
2135 }
2136
2137 switch (objectType) {
2138 case GL_TEXTURE:
2139 retval = texture_object_purgeable(ctx, name, option);
2140 break;
2141 case GL_RENDERBUFFER_EXT:
2142 retval = renderbuffer_purgeable(ctx, name, option);
2143 break;
2144 case GL_BUFFER_OBJECT_APPLE:
2145 retval = buffer_object_purgeable(ctx, name, option);
2146 break;
2147 default:
2148 _mesa_error(ctx, GL_INVALID_ENUM,
2149 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2150 name, objectType);
2151 return 0;
2152 }
2153
2154 /* In strict conformance to the spec, we must only return VOLATILE when
2155 * when passed the VOLATILE option. Madness.
2156 *
2157 * XXX First fix the spec, then fix me.
2158 */
2159 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
2160 }
2161
2162
2163 static GLenum
2164 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2165 {
2166 struct gl_buffer_object *bufObj;
2167 GLenum retval;
2168
2169 bufObj = _mesa_lookup_bufferobj(ctx, name);
2170 if (!bufObj) {
2171 _mesa_error(ctx, GL_INVALID_VALUE,
2172 "glObjectUnpurgeable(name = 0x%x)", name);
2173 return 0;
2174 }
2175
2176 if (! bufObj->Purgeable) {
2177 _mesa_error(ctx, GL_INVALID_OPERATION,
2178 "glObjectUnpurgeable(name = 0x%x) object is "
2179 " already \"unpurged\"", name);
2180 return 0;
2181 }
2182
2183 bufObj->Purgeable = GL_FALSE;
2184
2185 retval = option;
2186 if (ctx->Driver.BufferObjectUnpurgeable)
2187 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
2188
2189 return retval;
2190 }
2191
2192
2193 static GLenum
2194 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2195 {
2196 struct gl_renderbuffer *bufObj;
2197 GLenum retval;
2198
2199 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2200 if (!bufObj) {
2201 _mesa_error(ctx, GL_INVALID_VALUE,
2202 "glObjectUnpurgeable(name = 0x%x)", name);
2203 return 0;
2204 }
2205
2206 if (! bufObj->Purgeable) {
2207 _mesa_error(ctx, GL_INVALID_OPERATION,
2208 "glObjectUnpurgeable(name = 0x%x) object is "
2209 " already \"unpurged\"", name);
2210 return 0;
2211 }
2212
2213 bufObj->Purgeable = GL_FALSE;
2214
2215 retval = option;
2216 if (ctx->Driver.RenderObjectUnpurgeable)
2217 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
2218
2219 return retval;
2220 }
2221
2222
2223 static GLenum
2224 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2225 {
2226 struct gl_texture_object *bufObj;
2227 GLenum retval;
2228
2229 bufObj = _mesa_lookup_texture(ctx, name);
2230 if (!bufObj) {
2231 _mesa_error(ctx, GL_INVALID_VALUE,
2232 "glObjectUnpurgeable(name = 0x%x)", name);
2233 return 0;
2234 }
2235
2236 if (! bufObj->Purgeable) {
2237 _mesa_error(ctx, GL_INVALID_OPERATION,
2238 "glObjectUnpurgeable(name = 0x%x) object is"
2239 " already \"unpurged\"", name);
2240 return 0;
2241 }
2242
2243 bufObj->Purgeable = GL_FALSE;
2244
2245 retval = option;
2246 if (ctx->Driver.TextureObjectUnpurgeable)
2247 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
2248
2249 return retval;
2250 }
2251
2252
2253 GLenum GLAPIENTRY
2254 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2255 {
2256 GET_CURRENT_CONTEXT(ctx);
2257 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2258
2259 if (name == 0) {
2260 _mesa_error(ctx, GL_INVALID_VALUE,
2261 "glObjectUnpurgeable(name = 0x%x)", name);
2262 return 0;
2263 }
2264
2265 switch (option) {
2266 case GL_RETAINED_APPLE:
2267 case GL_UNDEFINED_APPLE:
2268 /* legal */
2269 break;
2270 default:
2271 _mesa_error(ctx, GL_INVALID_ENUM,
2272 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2273 name, option);
2274 return 0;
2275 }
2276
2277 switch (objectType) {
2278 case GL_BUFFER_OBJECT_APPLE:
2279 return buffer_object_unpurgeable(ctx, name, option);
2280 case GL_TEXTURE:
2281 return texture_object_unpurgeable(ctx, name, option);
2282 case GL_RENDERBUFFER_EXT:
2283 return renderbuffer_unpurgeable(ctx, name, option);
2284 default:
2285 _mesa_error(ctx, GL_INVALID_ENUM,
2286 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2287 name, objectType);
2288 return 0;
2289 }
2290 }
2291
2292
2293 static void
2294 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
2295 GLenum pname, GLint *params)
2296 {
2297 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
2298 if (!bufObj) {
2299 _mesa_error(ctx, GL_INVALID_VALUE,
2300 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
2301 return;
2302 }
2303
2304 switch (pname) {
2305 case GL_PURGEABLE_APPLE:
2306 *params = bufObj->Purgeable;
2307 break;
2308 default:
2309 _mesa_error(ctx, GL_INVALID_ENUM,
2310 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2311 name, pname);
2312 break;
2313 }
2314 }
2315
2316
2317 static void
2318 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
2319 GLenum pname, GLint *params)
2320 {
2321 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
2322 if (!rb) {
2323 _mesa_error(ctx, GL_INVALID_VALUE,
2324 "glObjectUnpurgeable(name = 0x%x)", name);
2325 return;
2326 }
2327
2328 switch (pname) {
2329 case GL_PURGEABLE_APPLE:
2330 *params = rb->Purgeable;
2331 break;
2332 default:
2333 _mesa_error(ctx, GL_INVALID_ENUM,
2334 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2335 name, pname);
2336 break;
2337 }
2338 }
2339
2340
2341 static void
2342 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
2343 GLenum pname, GLint *params)
2344 {
2345 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
2346 if (!texObj) {
2347 _mesa_error(ctx, GL_INVALID_VALUE,
2348 "glObjectUnpurgeable(name = 0x%x)", name);
2349 return;
2350 }
2351
2352 switch (pname) {
2353 case GL_PURGEABLE_APPLE:
2354 *params = texObj->Purgeable;
2355 break;
2356 default:
2357 _mesa_error(ctx, GL_INVALID_ENUM,
2358 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2359 name, pname);
2360 break;
2361 }
2362 }
2363
2364
2365 void GLAPIENTRY
2366 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2367 GLint *params)
2368 {
2369 GET_CURRENT_CONTEXT(ctx);
2370
2371 if (name == 0) {
2372 _mesa_error(ctx, GL_INVALID_VALUE,
2373 "glGetObjectParameteriv(name = 0x%x)", name);
2374 return;
2375 }
2376
2377 switch (objectType) {
2378 case GL_TEXTURE:
2379 get_texture_object_parameteriv(ctx, name, pname, params);
2380 break;
2381 case GL_BUFFER_OBJECT_APPLE:
2382 get_buffer_object_parameteriv(ctx, name, pname, params);
2383 break;
2384 case GL_RENDERBUFFER_EXT:
2385 get_renderbuffer_parameteriv(ctx, name, pname, params);
2386 break;
2387 default:
2388 _mesa_error(ctx, GL_INVALID_ENUM,
2389 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2390 name, objectType);
2391 }
2392 }
2393
2394 static void
2395 set_ubo_binding(struct gl_context *ctx,
2396 int index,
2397 struct gl_buffer_object *bufObj,
2398 GLintptr offset,
2399 GLsizeiptr size,
2400 GLboolean autoSize)
2401 {
2402 struct gl_uniform_buffer_binding *binding;
2403
2404 binding = &ctx->UniformBufferBindings[index];
2405 if (binding->BufferObject == bufObj &&
2406 binding->Offset == offset &&
2407 binding->Size == size &&
2408 binding->AutomaticSize == autoSize) {
2409 return;
2410 }
2411
2412 FLUSH_VERTICES(ctx, 0);
2413 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2414
2415 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2416 binding->Offset = offset;
2417 binding->Size = size;
2418 binding->AutomaticSize = autoSize;
2419 }
2420
2421 /**
2422 * Bind a region of a buffer object to a uniform block binding point.
2423 * \param index the uniform buffer binding point index
2424 * \param bufObj the buffer object
2425 * \param offset offset to the start of buffer object region
2426 * \param size size of the buffer object region
2427 */
2428 static void
2429 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2430 GLuint index,
2431 struct gl_buffer_object *bufObj,
2432 GLintptr offset,
2433 GLsizeiptr size)
2434 {
2435 if (index >= ctx->Const.MaxUniformBufferBindings) {
2436 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2437 return;
2438 }
2439
2440 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2441 _mesa_error(ctx, GL_INVALID_VALUE,
2442 "glBindBufferRange(offset misalgned %d/%d)", (int) offset,
2443 ctx->Const.UniformBufferOffsetAlignment);
2444 return;
2445 }
2446
2447 if (bufObj == ctx->Shared->NullBufferObj) {
2448 offset = -1;
2449 size = -1;
2450 }
2451
2452 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2453 set_ubo_binding(ctx, index, bufObj, offset, size, GL_FALSE);
2454 }
2455
2456
2457 /**
2458 * Bind a buffer object to a uniform block binding point.
2459 * As above, but offset = 0.
2460 */
2461 static void
2462 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2463 GLuint index,
2464 struct gl_buffer_object *bufObj)
2465 {
2466 if (index >= ctx->Const.MaxUniformBufferBindings) {
2467 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2468 return;
2469 }
2470
2471 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2472 if (bufObj == ctx->Shared->NullBufferObj)
2473 set_ubo_binding(ctx, index, bufObj, -1, -1, GL_TRUE);
2474 else
2475 set_ubo_binding(ctx, index, bufObj, 0, 0, GL_TRUE);
2476 }
2477
2478 static void
2479 set_atomic_buffer_binding(struct gl_context *ctx,
2480 unsigned index,
2481 struct gl_buffer_object *bufObj,
2482 GLintptr offset,
2483 GLsizeiptr size,
2484 const char *name)
2485 {
2486 struct gl_atomic_buffer_binding *binding;
2487
2488 if (index >= ctx->Const.MaxAtomicBufferBindings) {
2489 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
2490 return;
2491 }
2492
2493 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
2494 _mesa_error(ctx, GL_INVALID_VALUE,
2495 "%s(offset misalgned %d/%d)", name, (int) offset,
2496 ATOMIC_COUNTER_SIZE);
2497 return;
2498 }
2499
2500 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
2501
2502 binding = &ctx->AtomicBufferBindings[index];
2503 if (binding->BufferObject == bufObj &&
2504 binding->Offset == offset &&
2505 binding->Size == size) {
2506 return;
2507 }
2508
2509 FLUSH_VERTICES(ctx, 0);
2510 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
2511
2512 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2513
2514 if (bufObj == ctx->Shared->NullBufferObj) {
2515 binding->Offset = -1;
2516 binding->Size = -1;
2517 } else {
2518 binding->Offset = offset;
2519 binding->Size = size;
2520 }
2521 }
2522
2523 void GLAPIENTRY
2524 _mesa_BindBufferRange(GLenum target, GLuint index,
2525 GLuint buffer, GLintptr offset, GLsizeiptr size)
2526 {
2527 GET_CURRENT_CONTEXT(ctx);
2528 struct gl_buffer_object *bufObj;
2529
2530 if (buffer == 0) {
2531 bufObj = ctx->Shared->NullBufferObj;
2532 } else {
2533 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2534 }
2535 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
2536 &bufObj, "glBindBufferRange"))
2537 return;
2538
2539 if (!bufObj) {
2540 _mesa_error(ctx, GL_INVALID_OPERATION,
2541 "glBindBufferRange(invalid buffer=%u)", buffer);
2542 return;
2543 }
2544
2545 if (buffer != 0) {
2546 if (size <= 0) {
2547 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
2548 (int) size);
2549 return;
2550 }
2551 }
2552
2553 switch (target) {
2554 case GL_TRANSFORM_FEEDBACK_BUFFER:
2555 _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
2556 offset, size);
2557 return;
2558 case GL_UNIFORM_BUFFER:
2559 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
2560 return;
2561 case GL_ATOMIC_COUNTER_BUFFER:
2562 set_atomic_buffer_binding(ctx, index, bufObj, offset, size,
2563 "glBindBufferRange");
2564 return;
2565 default:
2566 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
2567 return;
2568 }
2569 }
2570
2571 void GLAPIENTRY
2572 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
2573 {
2574 GET_CURRENT_CONTEXT(ctx);
2575 struct gl_buffer_object *bufObj;
2576
2577 if (buffer == 0) {
2578 bufObj = ctx->Shared->NullBufferObj;
2579 } else {
2580 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2581 }
2582 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
2583 &bufObj, "glBindBufferBase"))
2584 return;
2585
2586 if (!bufObj) {
2587 _mesa_error(ctx, GL_INVALID_OPERATION,
2588 "glBindBufferBase(invalid buffer=%u)", buffer);
2589 return;
2590 }
2591
2592 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2593 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2594 *
2595 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2596 * zero and size equal to the size of buffer."
2597 *
2598 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2599 *
2600 * "If the parameter (starting offset or size) was not specified when the
2601 * buffer object was bound, zero is returned."
2602 *
2603 * What happens if the size of the buffer changes? Does the size of the
2604 * buffer at the moment glBindBufferBase was called still play a role, like
2605 * the first quote would imply, or is the size meaningless in the
2606 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2607 * core spec page 45 says:
2608 *
2609 * "It is equivalent to calling BindBufferRange with offset zero, while
2610 * size is determined by the size of the bound buffer at the time the
2611 * binding is used."
2612 *
2613 * My interpretation is that the GL 4.1 spec was a clarification of the
2614 * behavior, not a change. In particular, this choice will only make
2615 * rendering work in cases where it would have had undefined results.
2616 */
2617
2618 switch (target) {
2619 case GL_TRANSFORM_FEEDBACK_BUFFER:
2620 _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
2621 return;
2622 case GL_UNIFORM_BUFFER:
2623 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
2624 return;
2625 case GL_ATOMIC_COUNTER_BUFFER:
2626 set_atomic_buffer_binding(ctx, index, bufObj, 0, 0,
2627 "glBindBufferBase");
2628 return;
2629 default:
2630 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
2631 return;
2632 }
2633 }
2634
2635 void GLAPIENTRY
2636 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
2637 GLsizeiptr length)
2638 {
2639 GET_CURRENT_CONTEXT(ctx);
2640 struct gl_buffer_object *bufObj;
2641 const GLintptr end = offset + length;
2642
2643 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2644 if (!bufObj) {
2645 _mesa_error(ctx, GL_INVALID_VALUE,
2646 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2647 buffer);
2648 return;
2649 }
2650
2651 /* The GL_ARB_invalidate_subdata spec says:
2652 *
2653 * "An INVALID_VALUE error is generated if <offset> or <length> is
2654 * negative, or if <offset> + <length> is greater than the value of
2655 * BUFFER_SIZE."
2656 */
2657 if (end < 0 || end > bufObj->Size) {
2658 _mesa_error(ctx, GL_INVALID_VALUE,
2659 "glInvalidateBufferSubData(invalid offset or length)");
2660 return;
2661 }
2662
2663 /* The GL_ARB_invalidate_subdata spec says:
2664 *
2665 * "An INVALID_OPERATION error is generated if the buffer is currently
2666 * mapped by MapBuffer, or if the invalidate range intersects the range
2667 * currently mapped by MapBufferRange."
2668 */
2669 if (bufferobj_range_mapped(bufObj, offset, length)) {
2670 _mesa_error(ctx, GL_INVALID_OPERATION,
2671 "glInvalidateBufferSubData(intersection with mapped "
2672 "range)");
2673 return;
2674 }
2675
2676 /* We don't actually do anything for this yet. Just return after
2677 * validating the parameters and generating the required errors.
2678 */
2679 return;
2680 }
2681
2682 void GLAPIENTRY
2683 _mesa_InvalidateBufferData(GLuint buffer)
2684 {
2685 GET_CURRENT_CONTEXT(ctx);
2686 struct gl_buffer_object *bufObj;
2687
2688 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2689 if (!bufObj) {
2690 _mesa_error(ctx, GL_INVALID_VALUE,
2691 "glInvalidateBufferData(name = 0x%x) invalid object",
2692 buffer);
2693 return;
2694 }
2695
2696 /* The GL_ARB_invalidate_subdata spec says:
2697 *
2698 * "An INVALID_OPERATION error is generated if the buffer is currently
2699 * mapped by MapBuffer, or if the invalidate range intersects the range
2700 * currently mapped by MapBufferRange."
2701 */
2702 if (_mesa_bufferobj_mapped(bufObj)) {
2703 _mesa_error(ctx, GL_INVALID_OPERATION,
2704 "glInvalidateBufferData(intersection with mapped "
2705 "range)");
2706 return;
2707 }
2708
2709 /* We don't actually do anything for this yet. Just return after
2710 * validating the parameters and generating the required errors.
2711 */
2712 return;
2713 }