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