mesa: Add helper functions for looking up multiple buffers
[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 struct gl_buffer_object *
979 _mesa_lookup_bufferobj_locked(struct gl_context *ctx, GLuint buffer)
980 {
981 return (struct gl_buffer_object *)
982 _mesa_HashLookupLocked(ctx->Shared->BufferObjects, buffer);
983 }
984
985
986 void
987 _mesa_begin_bufferobj_lookups(struct gl_context *ctx)
988 {
989 _mesa_HashLockMutex(ctx->Shared->BufferObjects);
990 }
991
992
993 void
994 _mesa_end_bufferobj_lookups(struct gl_context *ctx)
995 {
996 _mesa_HashUnlockMutex(ctx->Shared->BufferObjects);
997 }
998
999
1000 /**
1001 * Look up a buffer object for a multi-bind function.
1002 *
1003 * Unlike _mesa_lookup_bufferobj(), this function also takes care
1004 * of generating an error if the buffer ID is not zero or the name
1005 * of an existing buffer object.
1006 *
1007 * If the buffer ID refers to an existing buffer object, a pointer
1008 * to the buffer object is returned. If the ID is zero, a pointer
1009 * to the shared NullBufferObj is returned. If the ID is not zero
1010 * and does not refer to a valid buffer object, this function
1011 * returns NULL.
1012 *
1013 * This function assumes that the caller has already locked the
1014 * hash table mutex by calling _mesa_begin_bufferobj_lookups().
1015 */
1016 struct gl_buffer_object *
1017 _mesa_multi_bind_lookup_bufferobj(struct gl_context *ctx,
1018 const GLuint *buffers,
1019 GLuint index, const char *caller)
1020 {
1021 struct gl_buffer_object *bufObj;
1022
1023 if (buffers[index] != 0) {
1024 bufObj = _mesa_lookup_bufferobj_locked(ctx, buffers[index]);
1025
1026 /* The multi-bind functions don't create the buffer objects
1027 when they don't exist. */
1028 if (bufObj == &DummyBufferObject)
1029 bufObj = NULL;
1030 } else
1031 bufObj = ctx->Shared->NullBufferObj;
1032
1033 if (!bufObj) {
1034 /* The ARB_multi_bind spec says:
1035 *
1036 * "An INVALID_OPERATION error is generated if any value
1037 * in <buffers> is not zero or the name of an existing
1038 * buffer object (per binding)."
1039 */
1040 _mesa_error(ctx, GL_INVALID_OPERATION,
1041 "%s(buffers[%u]=%u is not zero or the name "
1042 "of an existing buffer object)",
1043 caller, index, buffers[index]);
1044 }
1045
1046 return bufObj;
1047 }
1048
1049
1050 /**
1051 * If *ptr points to obj, set ptr = the Null/default buffer object.
1052 * This is a helper for buffer object deletion.
1053 * The GL spec says that deleting a buffer object causes it to get
1054 * unbound from all arrays in the current context.
1055 */
1056 static void
1057 unbind(struct gl_context *ctx,
1058 struct gl_buffer_object **ptr,
1059 struct gl_buffer_object *obj)
1060 {
1061 if (*ptr == obj) {
1062 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
1063 }
1064 }
1065
1066
1067 /**
1068 * Plug default/fallback buffer object functions into the device
1069 * driver hooks.
1070 */
1071 void
1072 _mesa_init_buffer_object_functions(struct dd_function_table *driver)
1073 {
1074 /* GL_ARB_vertex/pixel_buffer_object */
1075 driver->NewBufferObject = _mesa_new_buffer_object;
1076 driver->DeleteBuffer = _mesa_delete_buffer_object;
1077 driver->BufferData = _mesa_buffer_data;
1078 driver->BufferSubData = _mesa_buffer_subdata;
1079 driver->GetBufferSubData = _mesa_buffer_get_subdata;
1080 driver->UnmapBuffer = _mesa_buffer_unmap;
1081
1082 /* GL_ARB_clear_buffer_object */
1083 driver->ClearBufferSubData = _mesa_buffer_clear_subdata;
1084
1085 /* GL_ARB_map_buffer_range */
1086 driver->MapBufferRange = _mesa_buffer_map_range;
1087 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
1088
1089 /* GL_ARB_copy_buffer */
1090 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
1091 }
1092
1093
1094 void
1095 _mesa_buffer_unmap_all_mappings(struct gl_context *ctx,
1096 struct gl_buffer_object *bufObj)
1097 {
1098 int i;
1099
1100 for (i = 0; i < MAP_COUNT; i++) {
1101 if (_mesa_bufferobj_mapped(bufObj, i)) {
1102 ctx->Driver.UnmapBuffer(ctx, bufObj, i);
1103 ASSERT(bufObj->Mappings[i].Pointer == NULL);
1104 bufObj->Mappings[i].AccessFlags = 0;
1105 }
1106 }
1107 }
1108
1109
1110 /**********************************************************************/
1111 /* API Functions */
1112 /**********************************************************************/
1113
1114 void GLAPIENTRY
1115 _mesa_BindBuffer(GLenum target, GLuint buffer)
1116 {
1117 GET_CURRENT_CONTEXT(ctx);
1118
1119 if (MESA_VERBOSE & VERBOSE_API)
1120 _mesa_debug(ctx, "glBindBuffer(%s, %u)\n",
1121 _mesa_lookup_enum_by_nr(target), buffer);
1122
1123 bind_buffer_object(ctx, target, buffer);
1124 }
1125
1126
1127 /**
1128 * Delete a set of buffer objects.
1129 *
1130 * \param n Number of buffer objects to delete.
1131 * \param ids Array of \c n buffer object IDs.
1132 */
1133 void GLAPIENTRY
1134 _mesa_DeleteBuffers(GLsizei n, const GLuint *ids)
1135 {
1136 GET_CURRENT_CONTEXT(ctx);
1137 GLsizei i;
1138 FLUSH_VERTICES(ctx, 0);
1139
1140 if (n < 0) {
1141 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
1142 return;
1143 }
1144
1145 mtx_lock(&ctx->Shared->Mutex);
1146
1147 for (i = 0; i < n; i++) {
1148 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
1149 if (bufObj) {
1150 struct gl_vertex_array_object *vao = ctx->Array.VAO;
1151 GLuint j;
1152
1153 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
1154
1155 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1156
1157 /* unbind any vertex pointers bound to this buffer */
1158 for (j = 0; j < Elements(vao->VertexBinding); j++) {
1159 unbind(ctx, &vao->VertexBinding[j].BufferObj, bufObj);
1160 }
1161
1162 if (ctx->Array.ArrayBufferObj == bufObj) {
1163 _mesa_BindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
1164 }
1165 if (vao->IndexBufferObj == bufObj) {
1166 _mesa_BindBuffer( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
1167 }
1168
1169 /* unbind ARB_draw_indirect binding point */
1170 if (ctx->DrawIndirectBuffer == bufObj) {
1171 _mesa_BindBuffer( GL_DRAW_INDIRECT_BUFFER, 0 );
1172 }
1173
1174 /* unbind ARB_copy_buffer binding points */
1175 if (ctx->CopyReadBuffer == bufObj) {
1176 _mesa_BindBuffer( GL_COPY_READ_BUFFER, 0 );
1177 }
1178 if (ctx->CopyWriteBuffer == bufObj) {
1179 _mesa_BindBuffer( GL_COPY_WRITE_BUFFER, 0 );
1180 }
1181
1182 /* unbind transform feedback binding points */
1183 if (ctx->TransformFeedback.CurrentBuffer == bufObj) {
1184 _mesa_BindBuffer( GL_TRANSFORM_FEEDBACK_BUFFER, 0 );
1185 }
1186 for (j = 0; j < MAX_FEEDBACK_BUFFERS; j++) {
1187 if (ctx->TransformFeedback.CurrentObject->Buffers[j] == bufObj) {
1188 _mesa_BindBufferBase( GL_TRANSFORM_FEEDBACK_BUFFER, j, 0 );
1189 }
1190 }
1191
1192 /* unbind UBO binding points */
1193 for (j = 0; j < ctx->Const.MaxUniformBufferBindings; j++) {
1194 if (ctx->UniformBufferBindings[j].BufferObject == bufObj) {
1195 _mesa_BindBufferBase( GL_UNIFORM_BUFFER, j, 0 );
1196 }
1197 }
1198
1199 if (ctx->UniformBuffer == bufObj) {
1200 _mesa_BindBuffer( GL_UNIFORM_BUFFER, 0 );
1201 }
1202
1203 /* unbind any pixel pack/unpack pointers bound to this buffer */
1204 if (ctx->Pack.BufferObj == bufObj) {
1205 _mesa_BindBuffer( GL_PIXEL_PACK_BUFFER_EXT, 0 );
1206 }
1207 if (ctx->Unpack.BufferObj == bufObj) {
1208 _mesa_BindBuffer( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
1209 }
1210
1211 if (ctx->Texture.BufferObject == bufObj) {
1212 _mesa_BindBuffer( GL_TEXTURE_BUFFER, 0 );
1213 }
1214
1215 /* The ID is immediately freed for re-use */
1216 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
1217 /* Make sure we do not run into the classic ABA problem on bind.
1218 * We don't want to allow re-binding a buffer object that's been
1219 * "deleted" by glDeleteBuffers().
1220 *
1221 * The explicit rebinding to the default object in the current context
1222 * prevents the above in the current context, but another context
1223 * sharing the same objects might suffer from this problem.
1224 * The alternative would be to do the hash lookup in any case on bind
1225 * which would introduce more runtime overhead than this.
1226 */
1227 bufObj->DeletePending = GL_TRUE;
1228 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
1229 }
1230 }
1231
1232 mtx_unlock(&ctx->Shared->Mutex);
1233 }
1234
1235
1236 /**
1237 * Generate a set of unique buffer object IDs and store them in \c buffer.
1238 *
1239 * \param n Number of IDs to generate.
1240 * \param buffer Array of \c n locations to store the IDs.
1241 */
1242 void GLAPIENTRY
1243 _mesa_GenBuffers(GLsizei n, GLuint *buffer)
1244 {
1245 GET_CURRENT_CONTEXT(ctx);
1246 GLuint first;
1247 GLint i;
1248
1249 if (MESA_VERBOSE & VERBOSE_API)
1250 _mesa_debug(ctx, "glGenBuffers(%d)\n", n);
1251
1252 if (n < 0) {
1253 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
1254 return;
1255 }
1256
1257 if (!buffer) {
1258 return;
1259 }
1260
1261 /*
1262 * This must be atomic (generation and allocation of buffer object IDs)
1263 */
1264 mtx_lock(&ctx->Shared->Mutex);
1265
1266 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1267
1268 /* Insert the ID and pointer to dummy buffer object into hash table */
1269 for (i = 0; i < n; i++) {
1270 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
1271 &DummyBufferObject);
1272 buffer[i] = first + i;
1273 }
1274
1275 mtx_unlock(&ctx->Shared->Mutex);
1276 }
1277
1278
1279 /**
1280 * Determine if ID is the name of a buffer object.
1281 *
1282 * \param id ID of the potential buffer object.
1283 * \return \c GL_TRUE if \c id is the name of a buffer object,
1284 * \c GL_FALSE otherwise.
1285 */
1286 GLboolean GLAPIENTRY
1287 _mesa_IsBuffer(GLuint id)
1288 {
1289 struct gl_buffer_object *bufObj;
1290 GET_CURRENT_CONTEXT(ctx);
1291 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1292
1293 mtx_lock(&ctx->Shared->Mutex);
1294 bufObj = _mesa_lookup_bufferobj(ctx, id);
1295 mtx_unlock(&ctx->Shared->Mutex);
1296
1297 return bufObj && bufObj != &DummyBufferObject;
1298 }
1299
1300
1301 void GLAPIENTRY
1302 _mesa_BufferStorage(GLenum target, GLsizeiptr size, const GLvoid *data,
1303 GLbitfield flags)
1304 {
1305 GET_CURRENT_CONTEXT(ctx);
1306 struct gl_buffer_object *bufObj;
1307
1308 if (size <= 0) {
1309 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(size <= 0)");
1310 return;
1311 }
1312
1313 if (flags & ~(GL_MAP_READ_BIT |
1314 GL_MAP_WRITE_BIT |
1315 GL_MAP_PERSISTENT_BIT |
1316 GL_MAP_COHERENT_BIT |
1317 GL_DYNAMIC_STORAGE_BIT |
1318 GL_CLIENT_STORAGE_BIT)) {
1319 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags)");
1320 return;
1321 }
1322
1323 if (flags & GL_MAP_PERSISTENT_BIT &&
1324 !(flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))) {
1325 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags!=READ/WRITE)");
1326 return;
1327 }
1328
1329 if (flags & GL_MAP_COHERENT_BIT && !(flags & GL_MAP_PERSISTENT_BIT)) {
1330 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferStorage(flags!=PERSISTENT)");
1331 return;
1332 }
1333
1334 bufObj = get_buffer(ctx, "glBufferStorage", target, GL_INVALID_OPERATION);
1335 if (!bufObj)
1336 return;
1337
1338 if (bufObj->Immutable) {
1339 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferStorage(immutable)");
1340 return;
1341 }
1342
1343 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1344 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1345
1346 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1347
1348 bufObj->Written = GL_TRUE;
1349 bufObj->Immutable = GL_TRUE;
1350
1351 ASSERT(ctx->Driver.BufferData);
1352 if (!ctx->Driver.BufferData(ctx, target, size, data, GL_DYNAMIC_DRAW,
1353 flags, bufObj)) {
1354 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferStorage()");
1355 }
1356 }
1357
1358
1359 void GLAPIENTRY
1360 _mesa_BufferData(GLenum target, GLsizeiptrARB size,
1361 const GLvoid * data, GLenum usage)
1362 {
1363 GET_CURRENT_CONTEXT(ctx);
1364 struct gl_buffer_object *bufObj;
1365 bool valid_usage;
1366
1367 if (MESA_VERBOSE & VERBOSE_API)
1368 _mesa_debug(ctx, "glBufferData(%s, %ld, %p, %s)\n",
1369 _mesa_lookup_enum_by_nr(target),
1370 (long int) size, data,
1371 _mesa_lookup_enum_by_nr(usage));
1372
1373 if (size < 0) {
1374 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1375 return;
1376 }
1377
1378 switch (usage) {
1379 case GL_STREAM_DRAW_ARB:
1380 valid_usage = (ctx->API != API_OPENGLES);
1381 break;
1382
1383 case GL_STATIC_DRAW_ARB:
1384 case GL_DYNAMIC_DRAW_ARB:
1385 valid_usage = true;
1386 break;
1387
1388 case GL_STREAM_READ_ARB:
1389 case GL_STREAM_COPY_ARB:
1390 case GL_STATIC_READ_ARB:
1391 case GL_STATIC_COPY_ARB:
1392 case GL_DYNAMIC_READ_ARB:
1393 case GL_DYNAMIC_COPY_ARB:
1394 valid_usage = _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx);
1395 break;
1396
1397 default:
1398 valid_usage = false;
1399 break;
1400 }
1401
1402 if (!valid_usage) {
1403 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferData(usage)");
1404 return;
1405 }
1406
1407 bufObj = get_buffer(ctx, "glBufferDataARB", target, GL_INVALID_OPERATION);
1408 if (!bufObj)
1409 return;
1410
1411 if (bufObj->Immutable) {
1412 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferData(immutable)");
1413 return;
1414 }
1415
1416 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1417 _mesa_buffer_unmap_all_mappings(ctx, bufObj);
1418
1419 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1420
1421 bufObj->Written = GL_TRUE;
1422
1423 #ifdef VBO_DEBUG
1424 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
1425 bufObj->Name, size, data, usage);
1426 #endif
1427
1428 #ifdef BOUNDS_CHECK
1429 size += 100;
1430 #endif
1431
1432 ASSERT(ctx->Driver.BufferData);
1433 if (!ctx->Driver.BufferData(ctx, target, size, data, usage,
1434 GL_MAP_READ_BIT |
1435 GL_MAP_WRITE_BIT |
1436 GL_DYNAMIC_STORAGE_BIT,
1437 bufObj)) {
1438 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
1439 }
1440 }
1441
1442
1443 void GLAPIENTRY
1444 _mesa_BufferSubData(GLenum target, GLintptrARB offset,
1445 GLsizeiptrARB size, const GLvoid * data)
1446 {
1447 GET_CURRENT_CONTEXT(ctx);
1448 struct gl_buffer_object *bufObj;
1449
1450 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
1451 false, GL_INVALID_OPERATION,
1452 "glBufferSubDataARB" );
1453 if (!bufObj) {
1454 /* error already recorded */
1455 return;
1456 }
1457
1458 if (bufObj->Immutable &&
1459 !(bufObj->StorageFlags & GL_DYNAMIC_STORAGE_BIT)) {
1460 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferSubData");
1461 return;
1462 }
1463
1464 if (size == 0)
1465 return;
1466
1467 bufObj->Written = GL_TRUE;
1468
1469 ASSERT(ctx->Driver.BufferSubData);
1470 ctx->Driver.BufferSubData( ctx, offset, size, data, bufObj );
1471 }
1472
1473
1474 void GLAPIENTRY
1475 _mesa_GetBufferSubData(GLenum target, GLintptrARB offset,
1476 GLsizeiptrARB size, void * data)
1477 {
1478 GET_CURRENT_CONTEXT(ctx);
1479 struct gl_buffer_object *bufObj;
1480
1481 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1482 false, GL_INVALID_OPERATION,
1483 "glGetBufferSubDataARB");
1484 if (!bufObj) {
1485 /* error already recorded */
1486 return;
1487 }
1488
1489 ASSERT(ctx->Driver.GetBufferSubData);
1490 ctx->Driver.GetBufferSubData( ctx, offset, size, data, bufObj );
1491 }
1492
1493
1494 void GLAPIENTRY
1495 _mesa_ClearBufferData(GLenum target, GLenum internalformat, GLenum format,
1496 GLenum type, const GLvoid* data)
1497 {
1498 GET_CURRENT_CONTEXT(ctx);
1499 struct gl_buffer_object* bufObj;
1500 mesa_format mesaFormat;
1501 GLubyte clearValue[MAX_PIXEL_BYTES];
1502 GLsizeiptr clearValueSize;
1503
1504 bufObj = get_buffer(ctx, "glClearBufferData", target, GL_INVALID_VALUE);
1505 if (!bufObj) {
1506 return;
1507 }
1508
1509 if (_mesa_check_disallowed_mapping(bufObj)) {
1510 _mesa_error(ctx, GL_INVALID_OPERATION,
1511 "glClearBufferData(buffer currently mapped)");
1512 return;
1513 }
1514
1515 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1516 format, type,
1517 "glClearBufferData");
1518 if (mesaFormat == MESA_FORMAT_NONE) {
1519 return;
1520 }
1521
1522 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1523 if (bufObj->Size % clearValueSize != 0) {
1524 _mesa_error(ctx, GL_INVALID_VALUE,
1525 "glClearBufferData(size is not a multiple of "
1526 "internalformat size)");
1527 return;
1528 }
1529
1530 if (data == NULL) {
1531 /* clear to zeros, per the spec */
1532 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1533 NULL, clearValueSize, bufObj);
1534 return;
1535 }
1536
1537 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1538 format, type, data, "glClearBufferData")) {
1539 return;
1540 }
1541
1542 ctx->Driver.ClearBufferSubData(ctx, 0, bufObj->Size,
1543 clearValue, clearValueSize, bufObj);
1544 }
1545
1546
1547 void GLAPIENTRY
1548 _mesa_ClearBufferSubData(GLenum target, GLenum internalformat,
1549 GLintptr offset, GLsizeiptr size,
1550 GLenum format, GLenum type,
1551 const GLvoid* data)
1552 {
1553 GET_CURRENT_CONTEXT(ctx);
1554 struct gl_buffer_object* bufObj;
1555 mesa_format mesaFormat;
1556 GLubyte clearValue[MAX_PIXEL_BYTES];
1557 GLsizeiptr clearValueSize;
1558
1559 bufObj = buffer_object_subdata_range_good(ctx, target, offset, size,
1560 true, GL_INVALID_VALUE,
1561 "glClearBufferSubData");
1562 if (!bufObj) {
1563 return;
1564 }
1565
1566 mesaFormat = validate_clear_buffer_format(ctx, internalformat,
1567 format, type,
1568 "glClearBufferSubData");
1569 if (mesaFormat == MESA_FORMAT_NONE) {
1570 return;
1571 }
1572
1573 clearValueSize = _mesa_get_format_bytes(mesaFormat);
1574 if (offset % clearValueSize != 0 || size % clearValueSize != 0) {
1575 _mesa_error(ctx, GL_INVALID_VALUE,
1576 "glClearBufferSubData(offset or size is not a multiple of "
1577 "internalformat size)");
1578 return;
1579 }
1580
1581 if (data == NULL) {
1582 /* clear to zeros, per the spec */
1583 if (size > 0) {
1584 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1585 NULL, clearValueSize, bufObj);
1586 }
1587 return;
1588 }
1589
1590 if (!convert_clear_buffer_data(ctx, mesaFormat, clearValue,
1591 format, type, data,
1592 "glClearBufferSubData")) {
1593 return;
1594 }
1595
1596 if (size > 0) {
1597 ctx->Driver.ClearBufferSubData(ctx, offset, size,
1598 clearValue, clearValueSize, bufObj);
1599 }
1600 }
1601
1602
1603 void * GLAPIENTRY
1604 _mesa_MapBuffer(GLenum target, GLenum access)
1605 {
1606 GET_CURRENT_CONTEXT(ctx);
1607 struct gl_buffer_object * bufObj;
1608 GLbitfield accessFlags;
1609 void *map;
1610 bool valid_access;
1611
1612 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1613
1614 switch (access) {
1615 case GL_READ_ONLY_ARB:
1616 accessFlags = GL_MAP_READ_BIT;
1617 valid_access = _mesa_is_desktop_gl(ctx);
1618 break;
1619 case GL_WRITE_ONLY_ARB:
1620 accessFlags = GL_MAP_WRITE_BIT;
1621 valid_access = true;
1622 break;
1623 case GL_READ_WRITE_ARB:
1624 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1625 valid_access = _mesa_is_desktop_gl(ctx);
1626 break;
1627 default:
1628 valid_access = false;
1629 break;
1630 }
1631
1632 if (!valid_access) {
1633 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1634 return NULL;
1635 }
1636
1637 bufObj = get_buffer(ctx, "glMapBufferARB", target, GL_INVALID_OPERATION);
1638 if (!bufObj)
1639 return NULL;
1640
1641 if (accessFlags & GL_MAP_READ_BIT &&
1642 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
1643 _mesa_error(ctx, GL_INVALID_OPERATION,
1644 "glMapBuffer(invalid read flag)");
1645 return NULL;
1646 }
1647
1648 if (accessFlags & GL_MAP_WRITE_BIT &&
1649 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
1650 _mesa_error(ctx, GL_INVALID_OPERATION,
1651 "glMapBuffer(invalid write flag)");
1652 return NULL;
1653 }
1654
1655 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
1656 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
1657 return NULL;
1658 }
1659
1660 if (!bufObj->Size) {
1661 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1662 "glMapBuffer(buffer size = 0)");
1663 return NULL;
1664 }
1665
1666 ASSERT(ctx->Driver.MapBufferRange);
1667 map = ctx->Driver.MapBufferRange(ctx, 0, bufObj->Size, accessFlags, bufObj,
1668 MAP_USER);
1669 if (!map) {
1670 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
1671 return NULL;
1672 }
1673 else {
1674 /* The driver callback should have set these fields.
1675 * This is important because other modules (like VBO) might call
1676 * the driver function directly.
1677 */
1678 ASSERT(bufObj->Mappings[MAP_USER].Pointer == map);
1679 ASSERT(bufObj->Mappings[MAP_USER].Length == bufObj->Size);
1680 ASSERT(bufObj->Mappings[MAP_USER].Offset == 0);
1681 bufObj->Mappings[MAP_USER].AccessFlags = accessFlags;
1682 }
1683
1684 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1685 bufObj->Written = GL_TRUE;
1686
1687 #ifdef VBO_DEBUG
1688 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1689 bufObj->Name, bufObj->Size, access);
1690 if (access == GL_WRITE_ONLY_ARB) {
1691 GLuint i;
1692 GLubyte *b = (GLubyte *) bufObj->Pointer;
1693 for (i = 0; i < bufObj->Size; i++)
1694 b[i] = i & 0xff;
1695 }
1696 #endif
1697
1698 #ifdef BOUNDS_CHECK
1699 {
1700 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1701 GLuint i;
1702 /* buffer is 100 bytes larger than requested, fill with magic value */
1703 for (i = 0; i < 100; i++) {
1704 buf[bufObj->Size - i - 1] = 123;
1705 }
1706 }
1707 #endif
1708
1709 return bufObj->Mappings[MAP_USER].Pointer;
1710 }
1711
1712
1713 GLboolean GLAPIENTRY
1714 _mesa_UnmapBuffer(GLenum target)
1715 {
1716 GET_CURRENT_CONTEXT(ctx);
1717 struct gl_buffer_object *bufObj;
1718 GLboolean status = GL_TRUE;
1719 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1720
1721 bufObj = get_buffer(ctx, "glUnmapBufferARB", target, GL_INVALID_OPERATION);
1722 if (!bufObj)
1723 return GL_FALSE;
1724
1725 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
1726 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1727 return GL_FALSE;
1728 }
1729
1730 #ifdef BOUNDS_CHECK
1731 if (bufObj->Access != GL_READ_ONLY_ARB) {
1732 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1733 GLuint i;
1734 /* check that last 100 bytes are still = magic value */
1735 for (i = 0; i < 100; i++) {
1736 GLuint pos = bufObj->Size - i - 1;
1737 if (buf[pos] != 123) {
1738 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1739 " at position %d (value = %u)\n",
1740 pos, buf[pos]);
1741 }
1742 }
1743 }
1744 #endif
1745
1746 #ifdef VBO_DEBUG
1747 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
1748 GLuint i, unchanged = 0;
1749 GLubyte *b = (GLubyte *) bufObj->Pointer;
1750 GLint pos = -1;
1751 /* check which bytes changed */
1752 for (i = 0; i < bufObj->Size - 1; i++) {
1753 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1754 unchanged++;
1755 if (pos == -1)
1756 pos = i;
1757 }
1758 }
1759 if (unchanged) {
1760 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
1761 bufObj->Name, unchanged, bufObj->Size, pos);
1762 }
1763 }
1764 #endif
1765
1766 status = ctx->Driver.UnmapBuffer(ctx, bufObj, MAP_USER);
1767 bufObj->Mappings[MAP_USER].AccessFlags = 0;
1768 ASSERT(bufObj->Mappings[MAP_USER].Pointer == NULL);
1769 ASSERT(bufObj->Mappings[MAP_USER].Offset == 0);
1770 ASSERT(bufObj->Mappings[MAP_USER].Length == 0);
1771
1772 return status;
1773 }
1774
1775
1776 void GLAPIENTRY
1777 _mesa_GetBufferParameteriv(GLenum target, GLenum pname, GLint *params)
1778 {
1779 GET_CURRENT_CONTEXT(ctx);
1780 struct gl_buffer_object *bufObj;
1781
1782 bufObj = get_buffer(ctx, "glGetBufferParameterivARB", target,
1783 GL_INVALID_OPERATION);
1784 if (!bufObj)
1785 return;
1786
1787 switch (pname) {
1788 case GL_BUFFER_SIZE_ARB:
1789 *params = (GLint) bufObj->Size;
1790 return;
1791 case GL_BUFFER_USAGE_ARB:
1792 *params = bufObj->Usage;
1793 return;
1794 case GL_BUFFER_ACCESS_ARB:
1795 *params = simplified_access_mode(ctx,
1796 bufObj->Mappings[MAP_USER].AccessFlags);
1797 return;
1798 case GL_BUFFER_MAPPED_ARB:
1799 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
1800 return;
1801 case GL_BUFFER_ACCESS_FLAGS:
1802 if (!ctx->Extensions.ARB_map_buffer_range)
1803 goto invalid_pname;
1804 *params = bufObj->Mappings[MAP_USER].AccessFlags;
1805 return;
1806 case GL_BUFFER_MAP_OFFSET:
1807 if (!ctx->Extensions.ARB_map_buffer_range)
1808 goto invalid_pname;
1809 *params = (GLint) bufObj->Mappings[MAP_USER].Offset;
1810 return;
1811 case GL_BUFFER_MAP_LENGTH:
1812 if (!ctx->Extensions.ARB_map_buffer_range)
1813 goto invalid_pname;
1814 *params = (GLint) bufObj->Mappings[MAP_USER].Length;
1815 return;
1816 case GL_BUFFER_IMMUTABLE_STORAGE:
1817 if (!ctx->Extensions.ARB_buffer_storage)
1818 goto invalid_pname;
1819 *params = bufObj->Immutable;
1820 return;
1821 case GL_BUFFER_STORAGE_FLAGS:
1822 if (!ctx->Extensions.ARB_buffer_storage)
1823 goto invalid_pname;
1824 *params = bufObj->StorageFlags;
1825 return;
1826 default:
1827 ; /* fall-through */
1828 }
1829
1830 invalid_pname:
1831 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1832 _mesa_lookup_enum_by_nr(pname));
1833 }
1834
1835
1836 /**
1837 * New in GL 3.2
1838 * This is pretty much a duplicate of GetBufferParameteriv() but the
1839 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1840 */
1841 void GLAPIENTRY
1842 _mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1843 {
1844 GET_CURRENT_CONTEXT(ctx);
1845 struct gl_buffer_object *bufObj;
1846
1847 bufObj = get_buffer(ctx, "glGetBufferParameteri64v", target,
1848 GL_INVALID_OPERATION);
1849 if (!bufObj)
1850 return;
1851
1852 switch (pname) {
1853 case GL_BUFFER_SIZE_ARB:
1854 *params = bufObj->Size;
1855 return;
1856 case GL_BUFFER_USAGE_ARB:
1857 *params = bufObj->Usage;
1858 return;
1859 case GL_BUFFER_ACCESS_ARB:
1860 *params = simplified_access_mode(ctx,
1861 bufObj->Mappings[MAP_USER].AccessFlags);
1862 return;
1863 case GL_BUFFER_ACCESS_FLAGS:
1864 if (!ctx->Extensions.ARB_map_buffer_range)
1865 goto invalid_pname;
1866 *params = bufObj->Mappings[MAP_USER].AccessFlags;
1867 return;
1868 case GL_BUFFER_MAPPED_ARB:
1869 *params = _mesa_bufferobj_mapped(bufObj, MAP_USER);
1870 return;
1871 case GL_BUFFER_MAP_OFFSET:
1872 if (!ctx->Extensions.ARB_map_buffer_range)
1873 goto invalid_pname;
1874 *params = bufObj->Mappings[MAP_USER].Offset;
1875 return;
1876 case GL_BUFFER_MAP_LENGTH:
1877 if (!ctx->Extensions.ARB_map_buffer_range)
1878 goto invalid_pname;
1879 *params = bufObj->Mappings[MAP_USER].Length;
1880 return;
1881 case GL_BUFFER_IMMUTABLE_STORAGE:
1882 if (!ctx->Extensions.ARB_buffer_storage)
1883 goto invalid_pname;
1884 *params = bufObj->Immutable;
1885 return;
1886 case GL_BUFFER_STORAGE_FLAGS:
1887 if (!ctx->Extensions.ARB_buffer_storage)
1888 goto invalid_pname;
1889 *params = bufObj->StorageFlags;
1890 return;
1891 default:
1892 ; /* fall-through */
1893 }
1894
1895 invalid_pname:
1896 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1897 _mesa_lookup_enum_by_nr(pname));
1898 }
1899
1900
1901 void GLAPIENTRY
1902 _mesa_GetBufferPointerv(GLenum target, GLenum pname, GLvoid **params)
1903 {
1904 GET_CURRENT_CONTEXT(ctx);
1905 struct gl_buffer_object * bufObj;
1906
1907 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1908 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1909 return;
1910 }
1911
1912 bufObj = get_buffer(ctx, "glGetBufferPointervARB", target,
1913 GL_INVALID_OPERATION);
1914 if (!bufObj)
1915 return;
1916
1917 *params = bufObj->Mappings[MAP_USER].Pointer;
1918 }
1919
1920
1921 void GLAPIENTRY
1922 _mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1923 GLintptr readOffset, GLintptr writeOffset,
1924 GLsizeiptr size)
1925 {
1926 GET_CURRENT_CONTEXT(ctx);
1927 struct gl_buffer_object *src, *dst;
1928
1929 src = get_buffer(ctx, "glCopyBufferSubData", readTarget,
1930 GL_INVALID_OPERATION);
1931 if (!src)
1932 return;
1933
1934 dst = get_buffer(ctx, "glCopyBufferSubData", writeTarget,
1935 GL_INVALID_OPERATION);
1936 if (!dst)
1937 return;
1938
1939 if (_mesa_check_disallowed_mapping(src)) {
1940 _mesa_error(ctx, GL_INVALID_OPERATION,
1941 "glCopyBufferSubData(readBuffer is mapped)");
1942 return;
1943 }
1944
1945 if (_mesa_check_disallowed_mapping(dst)) {
1946 _mesa_error(ctx, GL_INVALID_OPERATION,
1947 "glCopyBufferSubData(writeBuffer is mapped)");
1948 return;
1949 }
1950
1951 if (readOffset < 0) {
1952 _mesa_error(ctx, GL_INVALID_VALUE,
1953 "glCopyBufferSubData(readOffset = %d)", (int) readOffset);
1954 return;
1955 }
1956
1957 if (writeOffset < 0) {
1958 _mesa_error(ctx, GL_INVALID_VALUE,
1959 "glCopyBufferSubData(writeOffset = %d)", (int) writeOffset);
1960 return;
1961 }
1962
1963 if (size < 0) {
1964 _mesa_error(ctx, GL_INVALID_VALUE,
1965 "glCopyBufferSubData(writeOffset = %d)", (int) size);
1966 return;
1967 }
1968
1969 if (readOffset + size > src->Size) {
1970 _mesa_error(ctx, GL_INVALID_VALUE,
1971 "glCopyBufferSubData(readOffset + size = %d)",
1972 (int) (readOffset + size));
1973 return;
1974 }
1975
1976 if (writeOffset + size > dst->Size) {
1977 _mesa_error(ctx, GL_INVALID_VALUE,
1978 "glCopyBufferSubData(writeOffset + size = %d)",
1979 (int) (writeOffset + size));
1980 return;
1981 }
1982
1983 if (src == dst) {
1984 if (readOffset + size <= writeOffset) {
1985 /* OK */
1986 }
1987 else if (writeOffset + size <= readOffset) {
1988 /* OK */
1989 }
1990 else {
1991 /* overlapping src/dst is illegal */
1992 _mesa_error(ctx, GL_INVALID_VALUE,
1993 "glCopyBufferSubData(overlapping src/dst)");
1994 return;
1995 }
1996 }
1997
1998 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1999 }
2000
2001
2002 /**
2003 * See GL_ARB_map_buffer_range spec
2004 */
2005 void * GLAPIENTRY
2006 _mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
2007 GLbitfield access)
2008 {
2009 GET_CURRENT_CONTEXT(ctx);
2010 struct gl_buffer_object *bufObj;
2011 void *map;
2012 GLbitfield allowed_access;
2013
2014 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
2015
2016 if (!ctx->Extensions.ARB_map_buffer_range) {
2017 _mesa_error(ctx, GL_INVALID_OPERATION,
2018 "glMapBufferRange(extension not supported)");
2019 return NULL;
2020 }
2021
2022 if (offset < 0) {
2023 _mesa_error(ctx, GL_INVALID_VALUE,
2024 "glMapBufferRange(offset = %ld)", (long)offset);
2025 return NULL;
2026 }
2027
2028 if (length < 0) {
2029 _mesa_error(ctx, GL_INVALID_VALUE,
2030 "glMapBufferRange(length = %ld)", (long)length);
2031 return NULL;
2032 }
2033
2034 /* Page 38 of the PDF of the OpenGL ES 3.0 spec says:
2035 *
2036 * "An INVALID_OPERATION error is generated for any of the following
2037 * conditions:
2038 *
2039 * * <length> is zero."
2040 */
2041 if (_mesa_is_gles(ctx) && length == 0) {
2042 _mesa_error(ctx, GL_INVALID_OPERATION,
2043 "glMapBufferRange(length = 0)");
2044 return NULL;
2045 }
2046
2047 allowed_access = GL_MAP_READ_BIT |
2048 GL_MAP_WRITE_BIT |
2049 GL_MAP_INVALIDATE_RANGE_BIT |
2050 GL_MAP_INVALIDATE_BUFFER_BIT |
2051 GL_MAP_FLUSH_EXPLICIT_BIT |
2052 GL_MAP_UNSYNCHRONIZED_BIT;
2053
2054 if (ctx->Extensions.ARB_buffer_storage) {
2055 allowed_access |= GL_MAP_PERSISTENT_BIT |
2056 GL_MAP_COHERENT_BIT;
2057 }
2058
2059 if (access & ~allowed_access) {
2060 /* generate an error if any other than allowed bit is set */
2061 _mesa_error(ctx, GL_INVALID_VALUE, "glMapBufferRange(access)");
2062 return NULL;
2063 }
2064
2065 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
2066 _mesa_error(ctx, GL_INVALID_OPERATION,
2067 "glMapBufferRange(access indicates neither read or write)");
2068 return NULL;
2069 }
2070
2071 if ((access & GL_MAP_READ_BIT) &&
2072 (access & (GL_MAP_INVALIDATE_RANGE_BIT |
2073 GL_MAP_INVALIDATE_BUFFER_BIT |
2074 GL_MAP_UNSYNCHRONIZED_BIT))) {
2075 _mesa_error(ctx, GL_INVALID_OPERATION,
2076 "glMapBufferRange(invalid access flags)");
2077 return NULL;
2078 }
2079
2080 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
2081 ((access & GL_MAP_WRITE_BIT) == 0)) {
2082 _mesa_error(ctx, GL_INVALID_OPERATION,
2083 "glMapBufferRange(invalid access flags)");
2084 return NULL;
2085 }
2086
2087 bufObj = get_buffer(ctx, "glMapBufferRange", target, GL_INVALID_OPERATION);
2088 if (!bufObj)
2089 return NULL;
2090
2091 if (access & GL_MAP_READ_BIT &&
2092 !(bufObj->StorageFlags & GL_MAP_READ_BIT)) {
2093 _mesa_error(ctx, GL_INVALID_OPERATION,
2094 "glMapBufferRange(invalid read flag)");
2095 return NULL;
2096 }
2097
2098 if (access & GL_MAP_WRITE_BIT &&
2099 !(bufObj->StorageFlags & GL_MAP_WRITE_BIT)) {
2100 _mesa_error(ctx, GL_INVALID_OPERATION,
2101 "glMapBufferRange(invalid write flag)");
2102 return NULL;
2103 }
2104
2105 if (access & GL_MAP_COHERENT_BIT &&
2106 !(bufObj->StorageFlags & GL_MAP_COHERENT_BIT)) {
2107 _mesa_error(ctx, GL_INVALID_OPERATION,
2108 "glMapBufferRange(invalid coherent flag)");
2109 return NULL;
2110 }
2111
2112 if (access & GL_MAP_PERSISTENT_BIT &&
2113 !(bufObj->StorageFlags & GL_MAP_PERSISTENT_BIT)) {
2114 _mesa_error(ctx, GL_INVALID_OPERATION,
2115 "glMapBufferRange(invalid persistent flag)");
2116 return NULL;
2117 }
2118
2119 if (offset + length > bufObj->Size) {
2120 _mesa_error(ctx, GL_INVALID_VALUE,
2121 "glMapBufferRange(offset + length > size)");
2122 return NULL;
2123 }
2124
2125 if (_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2126 _mesa_error(ctx, GL_INVALID_OPERATION,
2127 "glMapBufferRange(buffer already mapped)");
2128 return NULL;
2129 }
2130
2131 if (!bufObj->Size) {
2132 _mesa_error(ctx, GL_OUT_OF_MEMORY,
2133 "glMapBufferRange(buffer size = 0)");
2134 return NULL;
2135 }
2136
2137 /* Mapping zero bytes should return a non-null pointer. */
2138 if (!length) {
2139 static long dummy = 0;
2140 bufObj->Mappings[MAP_USER].Pointer = &dummy;
2141 bufObj->Mappings[MAP_USER].Length = length;
2142 bufObj->Mappings[MAP_USER].Offset = offset;
2143 bufObj->Mappings[MAP_USER].AccessFlags = access;
2144 return bufObj->Mappings[MAP_USER].Pointer;
2145 }
2146
2147 ASSERT(ctx->Driver.MapBufferRange);
2148 map = ctx->Driver.MapBufferRange(ctx, offset, length, access, bufObj,
2149 MAP_USER);
2150 if (!map) {
2151 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
2152 }
2153 else {
2154 /* The driver callback should have set all these fields.
2155 * This is important because other modules (like VBO) might call
2156 * the driver function directly.
2157 */
2158 ASSERT(bufObj->Mappings[MAP_USER].Pointer == map);
2159 ASSERT(bufObj->Mappings[MAP_USER].Length == length);
2160 ASSERT(bufObj->Mappings[MAP_USER].Offset == offset);
2161 ASSERT(bufObj->Mappings[MAP_USER].AccessFlags == access);
2162 }
2163
2164 return map;
2165 }
2166
2167
2168 /**
2169 * See GL_ARB_map_buffer_range spec
2170 */
2171 void GLAPIENTRY
2172 _mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
2173 {
2174 GET_CURRENT_CONTEXT(ctx);
2175 struct gl_buffer_object *bufObj;
2176
2177 if (!ctx->Extensions.ARB_map_buffer_range) {
2178 _mesa_error(ctx, GL_INVALID_OPERATION,
2179 "glFlushMappedBufferRange(extension not supported)");
2180 return;
2181 }
2182
2183 if (offset < 0) {
2184 _mesa_error(ctx, GL_INVALID_VALUE,
2185 "glFlushMappedBufferRange(offset = %ld)", (long)offset);
2186 return;
2187 }
2188
2189 if (length < 0) {
2190 _mesa_error(ctx, GL_INVALID_VALUE,
2191 "glFlushMappedBufferRange(length = %ld)", (long)length);
2192 return;
2193 }
2194
2195 bufObj = get_buffer(ctx, "glFlushMappedBufferRange", target,
2196 GL_INVALID_OPERATION);
2197 if (!bufObj)
2198 return;
2199
2200 if (!_mesa_bufferobj_mapped(bufObj, MAP_USER)) {
2201 /* buffer is not mapped */
2202 _mesa_error(ctx, GL_INVALID_OPERATION,
2203 "glFlushMappedBufferRange(buffer is not mapped)");
2204 return;
2205 }
2206
2207 if ((bufObj->Mappings[MAP_USER].AccessFlags &
2208 GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
2209 _mesa_error(ctx, GL_INVALID_OPERATION,
2210 "glFlushMappedBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
2211 return;
2212 }
2213
2214 if (offset + length > bufObj->Mappings[MAP_USER].Length) {
2215 _mesa_error(ctx, GL_INVALID_VALUE,
2216 "glFlushMappedBufferRange(offset %ld + length %ld > mapped length %ld)",
2217 (long)offset, (long)length,
2218 (long)bufObj->Mappings[MAP_USER].Length);
2219 return;
2220 }
2221
2222 ASSERT(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_WRITE_BIT);
2223
2224 if (ctx->Driver.FlushMappedBufferRange)
2225 ctx->Driver.FlushMappedBufferRange(ctx, offset, length, bufObj,
2226 MAP_USER);
2227 }
2228
2229
2230 static GLenum
2231 buffer_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2232 {
2233 struct gl_buffer_object *bufObj;
2234 GLenum retval;
2235
2236 bufObj = _mesa_lookup_bufferobj(ctx, name);
2237 if (!bufObj) {
2238 _mesa_error(ctx, GL_INVALID_VALUE,
2239 "glObjectPurgeable(name = 0x%x)", name);
2240 return 0;
2241 }
2242 if (!_mesa_is_bufferobj(bufObj)) {
2243 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
2244 return 0;
2245 }
2246
2247 if (bufObj->Purgeable) {
2248 _mesa_error(ctx, GL_INVALID_OPERATION,
2249 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2250 return GL_VOLATILE_APPLE;
2251 }
2252
2253 bufObj->Purgeable = GL_TRUE;
2254
2255 retval = GL_VOLATILE_APPLE;
2256 if (ctx->Driver.BufferObjectPurgeable)
2257 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
2258
2259 return retval;
2260 }
2261
2262
2263 static GLenum
2264 renderbuffer_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2265 {
2266 struct gl_renderbuffer *bufObj;
2267 GLenum retval;
2268
2269 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2270 if (!bufObj) {
2271 _mesa_error(ctx, GL_INVALID_VALUE,
2272 "glObjectUnpurgeable(name = 0x%x)", name);
2273 return 0;
2274 }
2275
2276 if (bufObj->Purgeable) {
2277 _mesa_error(ctx, GL_INVALID_OPERATION,
2278 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2279 return GL_VOLATILE_APPLE;
2280 }
2281
2282 bufObj->Purgeable = GL_TRUE;
2283
2284 retval = GL_VOLATILE_APPLE;
2285 if (ctx->Driver.RenderObjectPurgeable)
2286 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
2287
2288 return retval;
2289 }
2290
2291
2292 static GLenum
2293 texture_object_purgeable(struct gl_context *ctx, GLuint name, GLenum option)
2294 {
2295 struct gl_texture_object *bufObj;
2296 GLenum retval;
2297
2298 bufObj = _mesa_lookup_texture(ctx, name);
2299 if (!bufObj) {
2300 _mesa_error(ctx, GL_INVALID_VALUE,
2301 "glObjectPurgeable(name = 0x%x)", name);
2302 return 0;
2303 }
2304
2305 if (bufObj->Purgeable) {
2306 _mesa_error(ctx, GL_INVALID_OPERATION,
2307 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
2308 return GL_VOLATILE_APPLE;
2309 }
2310
2311 bufObj->Purgeable = GL_TRUE;
2312
2313 retval = GL_VOLATILE_APPLE;
2314 if (ctx->Driver.TextureObjectPurgeable)
2315 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
2316
2317 return retval;
2318 }
2319
2320
2321 GLenum GLAPIENTRY
2322 _mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2323 {
2324 GLenum retval;
2325
2326 GET_CURRENT_CONTEXT(ctx);
2327 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2328
2329 if (name == 0) {
2330 _mesa_error(ctx, GL_INVALID_VALUE,
2331 "glObjectPurgeable(name = 0x%x)", name);
2332 return 0;
2333 }
2334
2335 switch (option) {
2336 case GL_VOLATILE_APPLE:
2337 case GL_RELEASED_APPLE:
2338 /* legal */
2339 break;
2340 default:
2341 _mesa_error(ctx, GL_INVALID_ENUM,
2342 "glObjectPurgeable(name = 0x%x) invalid option: %d",
2343 name, option);
2344 return 0;
2345 }
2346
2347 switch (objectType) {
2348 case GL_TEXTURE:
2349 retval = texture_object_purgeable(ctx, name, option);
2350 break;
2351 case GL_RENDERBUFFER_EXT:
2352 retval = renderbuffer_purgeable(ctx, name, option);
2353 break;
2354 case GL_BUFFER_OBJECT_APPLE:
2355 retval = buffer_object_purgeable(ctx, name, option);
2356 break;
2357 default:
2358 _mesa_error(ctx, GL_INVALID_ENUM,
2359 "glObjectPurgeable(name = 0x%x) invalid type: %d",
2360 name, objectType);
2361 return 0;
2362 }
2363
2364 /* In strict conformance to the spec, we must only return VOLATILE when
2365 * when passed the VOLATILE option. Madness.
2366 *
2367 * XXX First fix the spec, then fix me.
2368 */
2369 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
2370 }
2371
2372
2373 static GLenum
2374 buffer_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2375 {
2376 struct gl_buffer_object *bufObj;
2377 GLenum retval;
2378
2379 bufObj = _mesa_lookup_bufferobj(ctx, name);
2380 if (!bufObj) {
2381 _mesa_error(ctx, GL_INVALID_VALUE,
2382 "glObjectUnpurgeable(name = 0x%x)", name);
2383 return 0;
2384 }
2385
2386 if (! bufObj->Purgeable) {
2387 _mesa_error(ctx, GL_INVALID_OPERATION,
2388 "glObjectUnpurgeable(name = 0x%x) object is "
2389 " already \"unpurged\"", name);
2390 return 0;
2391 }
2392
2393 bufObj->Purgeable = GL_FALSE;
2394
2395 retval = option;
2396 if (ctx->Driver.BufferObjectUnpurgeable)
2397 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
2398
2399 return retval;
2400 }
2401
2402
2403 static GLenum
2404 renderbuffer_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2405 {
2406 struct gl_renderbuffer *bufObj;
2407 GLenum retval;
2408
2409 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2410 if (!bufObj) {
2411 _mesa_error(ctx, GL_INVALID_VALUE,
2412 "glObjectUnpurgeable(name = 0x%x)", name);
2413 return 0;
2414 }
2415
2416 if (! bufObj->Purgeable) {
2417 _mesa_error(ctx, GL_INVALID_OPERATION,
2418 "glObjectUnpurgeable(name = 0x%x) object is "
2419 " already \"unpurged\"", name);
2420 return 0;
2421 }
2422
2423 bufObj->Purgeable = GL_FALSE;
2424
2425 retval = option;
2426 if (ctx->Driver.RenderObjectUnpurgeable)
2427 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
2428
2429 return retval;
2430 }
2431
2432
2433 static GLenum
2434 texture_object_unpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
2435 {
2436 struct gl_texture_object *bufObj;
2437 GLenum retval;
2438
2439 bufObj = _mesa_lookup_texture(ctx, name);
2440 if (!bufObj) {
2441 _mesa_error(ctx, GL_INVALID_VALUE,
2442 "glObjectUnpurgeable(name = 0x%x)", name);
2443 return 0;
2444 }
2445
2446 if (! bufObj->Purgeable) {
2447 _mesa_error(ctx, GL_INVALID_OPERATION,
2448 "glObjectUnpurgeable(name = 0x%x) object is"
2449 " already \"unpurged\"", name);
2450 return 0;
2451 }
2452
2453 bufObj->Purgeable = GL_FALSE;
2454
2455 retval = option;
2456 if (ctx->Driver.TextureObjectUnpurgeable)
2457 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
2458
2459 return retval;
2460 }
2461
2462
2463 GLenum GLAPIENTRY
2464 _mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
2465 {
2466 GET_CURRENT_CONTEXT(ctx);
2467 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2468
2469 if (name == 0) {
2470 _mesa_error(ctx, GL_INVALID_VALUE,
2471 "glObjectUnpurgeable(name = 0x%x)", name);
2472 return 0;
2473 }
2474
2475 switch (option) {
2476 case GL_RETAINED_APPLE:
2477 case GL_UNDEFINED_APPLE:
2478 /* legal */
2479 break;
2480 default:
2481 _mesa_error(ctx, GL_INVALID_ENUM,
2482 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2483 name, option);
2484 return 0;
2485 }
2486
2487 switch (objectType) {
2488 case GL_BUFFER_OBJECT_APPLE:
2489 return buffer_object_unpurgeable(ctx, name, option);
2490 case GL_TEXTURE:
2491 return texture_object_unpurgeable(ctx, name, option);
2492 case GL_RENDERBUFFER_EXT:
2493 return renderbuffer_unpurgeable(ctx, name, option);
2494 default:
2495 _mesa_error(ctx, GL_INVALID_ENUM,
2496 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2497 name, objectType);
2498 return 0;
2499 }
2500 }
2501
2502
2503 static void
2504 get_buffer_object_parameteriv(struct gl_context *ctx, GLuint name,
2505 GLenum pname, GLint *params)
2506 {
2507 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, name);
2508 if (!bufObj) {
2509 _mesa_error(ctx, GL_INVALID_VALUE,
2510 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
2511 return;
2512 }
2513
2514 switch (pname) {
2515 case GL_PURGEABLE_APPLE:
2516 *params = bufObj->Purgeable;
2517 break;
2518 default:
2519 _mesa_error(ctx, GL_INVALID_ENUM,
2520 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2521 name, pname);
2522 break;
2523 }
2524 }
2525
2526
2527 static void
2528 get_renderbuffer_parameteriv(struct gl_context *ctx, GLuint name,
2529 GLenum pname, GLint *params)
2530 {
2531 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, name);
2532 if (!rb) {
2533 _mesa_error(ctx, GL_INVALID_VALUE,
2534 "glObjectUnpurgeable(name = 0x%x)", name);
2535 return;
2536 }
2537
2538 switch (pname) {
2539 case GL_PURGEABLE_APPLE:
2540 *params = rb->Purgeable;
2541 break;
2542 default:
2543 _mesa_error(ctx, GL_INVALID_ENUM,
2544 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2545 name, pname);
2546 break;
2547 }
2548 }
2549
2550
2551 static void
2552 get_texture_object_parameteriv(struct gl_context *ctx, GLuint name,
2553 GLenum pname, GLint *params)
2554 {
2555 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, name);
2556 if (!texObj) {
2557 _mesa_error(ctx, GL_INVALID_VALUE,
2558 "glObjectUnpurgeable(name = 0x%x)", name);
2559 return;
2560 }
2561
2562 switch (pname) {
2563 case GL_PURGEABLE_APPLE:
2564 *params = texObj->Purgeable;
2565 break;
2566 default:
2567 _mesa_error(ctx, GL_INVALID_ENUM,
2568 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2569 name, pname);
2570 break;
2571 }
2572 }
2573
2574
2575 void GLAPIENTRY
2576 _mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2577 GLint *params)
2578 {
2579 GET_CURRENT_CONTEXT(ctx);
2580
2581 if (name == 0) {
2582 _mesa_error(ctx, GL_INVALID_VALUE,
2583 "glGetObjectParameteriv(name = 0x%x)", name);
2584 return;
2585 }
2586
2587 switch (objectType) {
2588 case GL_TEXTURE:
2589 get_texture_object_parameteriv(ctx, name, pname, params);
2590 break;
2591 case GL_BUFFER_OBJECT_APPLE:
2592 get_buffer_object_parameteriv(ctx, name, pname, params);
2593 break;
2594 case GL_RENDERBUFFER_EXT:
2595 get_renderbuffer_parameteriv(ctx, name, pname, params);
2596 break;
2597 default:
2598 _mesa_error(ctx, GL_INVALID_ENUM,
2599 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2600 name, objectType);
2601 }
2602 }
2603
2604 static void
2605 set_ubo_binding(struct gl_context *ctx,
2606 int index,
2607 struct gl_buffer_object *bufObj,
2608 GLintptr offset,
2609 GLsizeiptr size,
2610 GLboolean autoSize)
2611 {
2612 struct gl_uniform_buffer_binding *binding;
2613
2614 binding = &ctx->UniformBufferBindings[index];
2615 if (binding->BufferObject == bufObj &&
2616 binding->Offset == offset &&
2617 binding->Size == size &&
2618 binding->AutomaticSize == autoSize) {
2619 return;
2620 }
2621
2622 FLUSH_VERTICES(ctx, 0);
2623 ctx->NewDriverState |= ctx->DriverFlags.NewUniformBuffer;
2624
2625 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2626 binding->Offset = offset;
2627 binding->Size = size;
2628 binding->AutomaticSize = autoSize;
2629 }
2630
2631 /**
2632 * Bind a region of a buffer object to a uniform block binding point.
2633 * \param index the uniform buffer binding point index
2634 * \param bufObj the buffer object
2635 * \param offset offset to the start of buffer object region
2636 * \param size size of the buffer object region
2637 */
2638 static void
2639 bind_buffer_range_uniform_buffer(struct gl_context *ctx,
2640 GLuint index,
2641 struct gl_buffer_object *bufObj,
2642 GLintptr offset,
2643 GLsizeiptr size)
2644 {
2645 if (index >= ctx->Const.MaxUniformBufferBindings) {
2646 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
2647 return;
2648 }
2649
2650 if (offset & (ctx->Const.UniformBufferOffsetAlignment - 1)) {
2651 _mesa_error(ctx, GL_INVALID_VALUE,
2652 "glBindBufferRange(offset misalgned %d/%d)", (int) offset,
2653 ctx->Const.UniformBufferOffsetAlignment);
2654 return;
2655 }
2656
2657 if (bufObj == ctx->Shared->NullBufferObj) {
2658 offset = -1;
2659 size = -1;
2660 }
2661
2662 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2663 set_ubo_binding(ctx, index, bufObj, offset, size, GL_FALSE);
2664 }
2665
2666
2667 /**
2668 * Bind a buffer object to a uniform block binding point.
2669 * As above, but offset = 0.
2670 */
2671 static void
2672 bind_buffer_base_uniform_buffer(struct gl_context *ctx,
2673 GLuint index,
2674 struct gl_buffer_object *bufObj)
2675 {
2676 if (index >= ctx->Const.MaxUniformBufferBindings) {
2677 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
2678 return;
2679 }
2680
2681 _mesa_reference_buffer_object(ctx, &ctx->UniformBuffer, bufObj);
2682 if (bufObj == ctx->Shared->NullBufferObj)
2683 set_ubo_binding(ctx, index, bufObj, -1, -1, GL_TRUE);
2684 else
2685 set_ubo_binding(ctx, index, bufObj, 0, 0, GL_TRUE);
2686 }
2687
2688 /**
2689 * Binds a buffer object to an atomic buffer binding point.
2690 *
2691 * The caller is responsible for validating the offset,
2692 * flushing the vertices and updating NewDriverState.
2693 */
2694 static void
2695 set_atomic_buffer_binding(struct gl_context *ctx,
2696 struct gl_atomic_buffer_binding *binding,
2697 struct gl_buffer_object *bufObj,
2698 GLintptr offset,
2699 GLsizeiptr size)
2700 {
2701 _mesa_reference_buffer_object(ctx, &binding->BufferObject, bufObj);
2702
2703 if (bufObj == ctx->Shared->NullBufferObj) {
2704 binding->Offset = -1;
2705 binding->Size = -1;
2706 } else {
2707 binding->Offset = offset;
2708 binding->Size = size;
2709 }
2710 }
2711
2712 /**
2713 * Binds a buffer object to an atomic buffer binding point.
2714 *
2715 * Unlike set_atomic_buffer_binding(), this function also validates the
2716 * index and offset, flushes vertices, and updates NewDriverState.
2717 * It also checks if the binding has actually changing before
2718 * updating it.
2719 */
2720 static void
2721 bind_atomic_buffer(struct gl_context *ctx,
2722 unsigned index,
2723 struct gl_buffer_object *bufObj,
2724 GLintptr offset,
2725 GLsizeiptr size,
2726 const char *name)
2727 {
2728 struct gl_atomic_buffer_binding *binding;
2729
2730 if (index >= ctx->Const.MaxAtomicBufferBindings) {
2731 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d)", name, index);
2732 return;
2733 }
2734
2735 if (offset & (ATOMIC_COUNTER_SIZE - 1)) {
2736 _mesa_error(ctx, GL_INVALID_VALUE,
2737 "%s(offset misalgned %d/%d)", name, (int) offset,
2738 ATOMIC_COUNTER_SIZE);
2739 return;
2740 }
2741
2742 _mesa_reference_buffer_object(ctx, &ctx->AtomicBuffer, bufObj);
2743
2744 binding = &ctx->AtomicBufferBindings[index];
2745 if (binding->BufferObject == bufObj &&
2746 binding->Offset == offset &&
2747 binding->Size == size) {
2748 return;
2749 }
2750
2751 FLUSH_VERTICES(ctx, 0);
2752 ctx->NewDriverState |= ctx->DriverFlags.NewAtomicBuffer;
2753
2754 set_atomic_buffer_binding(ctx, binding, bufObj, offset, size);
2755 }
2756
2757 void GLAPIENTRY
2758 _mesa_BindBufferRange(GLenum target, GLuint index,
2759 GLuint buffer, GLintptr offset, GLsizeiptr size)
2760 {
2761 GET_CURRENT_CONTEXT(ctx);
2762 struct gl_buffer_object *bufObj;
2763
2764 if (buffer == 0) {
2765 bufObj = ctx->Shared->NullBufferObj;
2766 } else {
2767 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2768 }
2769 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
2770 &bufObj, "glBindBufferRange"))
2771 return;
2772
2773 if (!bufObj) {
2774 _mesa_error(ctx, GL_INVALID_OPERATION,
2775 "glBindBufferRange(invalid buffer=%u)", buffer);
2776 return;
2777 }
2778
2779 if (buffer != 0) {
2780 if (size <= 0) {
2781 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)",
2782 (int) size);
2783 return;
2784 }
2785 }
2786
2787 switch (target) {
2788 case GL_TRANSFORM_FEEDBACK_BUFFER:
2789 _mesa_bind_buffer_range_transform_feedback(ctx, index, bufObj,
2790 offset, size);
2791 return;
2792 case GL_UNIFORM_BUFFER:
2793 bind_buffer_range_uniform_buffer(ctx, index, bufObj, offset, size);
2794 return;
2795 case GL_ATOMIC_COUNTER_BUFFER:
2796 bind_atomic_buffer(ctx, index, bufObj, offset, size,
2797 "glBindBufferRange");
2798 return;
2799 default:
2800 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
2801 return;
2802 }
2803 }
2804
2805 void GLAPIENTRY
2806 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
2807 {
2808 GET_CURRENT_CONTEXT(ctx);
2809 struct gl_buffer_object *bufObj;
2810
2811 if (buffer == 0) {
2812 bufObj = ctx->Shared->NullBufferObj;
2813 } else {
2814 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2815 }
2816 if (!_mesa_handle_bind_buffer_gen(ctx, target, buffer,
2817 &bufObj, "glBindBufferBase"))
2818 return;
2819
2820 if (!bufObj) {
2821 _mesa_error(ctx, GL_INVALID_OPERATION,
2822 "glBindBufferBase(invalid buffer=%u)", buffer);
2823 return;
2824 }
2825
2826 /* Note that there's some oddness in the GL 3.1-GL 3.3 specifications with
2827 * regards to BindBufferBase. It says (GL 3.1 core spec, page 63):
2828 *
2829 * "BindBufferBase is equivalent to calling BindBufferRange with offset
2830 * zero and size equal to the size of buffer."
2831 *
2832 * but it says for glGetIntegeri_v (GL 3.1 core spec, page 230):
2833 *
2834 * "If the parameter (starting offset or size) was not specified when the
2835 * buffer object was bound, zero is returned."
2836 *
2837 * What happens if the size of the buffer changes? Does the size of the
2838 * buffer at the moment glBindBufferBase was called still play a role, like
2839 * the first quote would imply, or is the size meaningless in the
2840 * glBindBufferBase case like the second quote would suggest? The GL 4.1
2841 * core spec page 45 says:
2842 *
2843 * "It is equivalent to calling BindBufferRange with offset zero, while
2844 * size is determined by the size of the bound buffer at the time the
2845 * binding is used."
2846 *
2847 * My interpretation is that the GL 4.1 spec was a clarification of the
2848 * behavior, not a change. In particular, this choice will only make
2849 * rendering work in cases where it would have had undefined results.
2850 */
2851
2852 switch (target) {
2853 case GL_TRANSFORM_FEEDBACK_BUFFER:
2854 _mesa_bind_buffer_base_transform_feedback(ctx, index, bufObj);
2855 return;
2856 case GL_UNIFORM_BUFFER:
2857 bind_buffer_base_uniform_buffer(ctx, index, bufObj);
2858 return;
2859 case GL_ATOMIC_COUNTER_BUFFER:
2860 bind_atomic_buffer(ctx, index, bufObj, 0, 0,
2861 "glBindBufferBase");
2862 return;
2863 default:
2864 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
2865 return;
2866 }
2867 }
2868
2869 void GLAPIENTRY
2870 _mesa_BindBuffersRange(GLenum target, GLuint first, GLsizei count,
2871 const GLuint *buffers,
2872 const GLintptr *offsets, const GLsizeiptr *sizes)
2873 {
2874 }
2875
2876 void GLAPIENTRY
2877 _mesa_BindBuffersBase(GLenum target, GLuint first, GLsizei count,
2878 const GLuint *buffers)
2879 {
2880 }
2881
2882 void GLAPIENTRY
2883 _mesa_InvalidateBufferSubData(GLuint buffer, GLintptr offset,
2884 GLsizeiptr length)
2885 {
2886 GET_CURRENT_CONTEXT(ctx);
2887 struct gl_buffer_object *bufObj;
2888 const GLintptr end = offset + length;
2889
2890 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2891 if (!bufObj) {
2892 _mesa_error(ctx, GL_INVALID_VALUE,
2893 "glInvalidateBufferSubData(name = 0x%x) invalid object",
2894 buffer);
2895 return;
2896 }
2897
2898 /* The GL_ARB_invalidate_subdata spec says:
2899 *
2900 * "An INVALID_VALUE error is generated if <offset> or <length> is
2901 * negative, or if <offset> + <length> is greater than the value of
2902 * BUFFER_SIZE."
2903 */
2904 if (end < 0 || end > bufObj->Size) {
2905 _mesa_error(ctx, GL_INVALID_VALUE,
2906 "glInvalidateBufferSubData(invalid offset or length)");
2907 return;
2908 }
2909
2910 /* The OpenGL 4.4 (Core Profile) spec says:
2911 *
2912 * "An INVALID_OPERATION error is generated if buffer is currently
2913 * mapped by MapBuffer or if the invalidate range intersects the range
2914 * currently mapped by MapBufferRange, unless it was mapped
2915 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
2916 */
2917 if (!(bufObj->Mappings[MAP_USER].AccessFlags & GL_MAP_PERSISTENT_BIT) &&
2918 bufferobj_range_mapped(bufObj, offset, length)) {
2919 _mesa_error(ctx, GL_INVALID_OPERATION,
2920 "glInvalidateBufferSubData(intersection with mapped "
2921 "range)");
2922 return;
2923 }
2924
2925 /* We don't actually do anything for this yet. Just return after
2926 * validating the parameters and generating the required errors.
2927 */
2928 return;
2929 }
2930
2931 void GLAPIENTRY
2932 _mesa_InvalidateBufferData(GLuint buffer)
2933 {
2934 GET_CURRENT_CONTEXT(ctx);
2935 struct gl_buffer_object *bufObj;
2936
2937 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
2938 if (!bufObj) {
2939 _mesa_error(ctx, GL_INVALID_VALUE,
2940 "glInvalidateBufferData(name = 0x%x) invalid object",
2941 buffer);
2942 return;
2943 }
2944
2945 /* The OpenGL 4.4 (Core Profile) spec says:
2946 *
2947 * "An INVALID_OPERATION error is generated if buffer is currently
2948 * mapped by MapBuffer or if the invalidate range intersects the range
2949 * currently mapped by MapBufferRange, unless it was mapped
2950 * with MAP_PERSISTENT_BIT set in the MapBufferRange access flags."
2951 */
2952 if (_mesa_check_disallowed_mapping(bufObj)) {
2953 _mesa_error(ctx, GL_INVALID_OPERATION,
2954 "glInvalidateBufferData(intersection with mapped "
2955 "range)");
2956 return;
2957 }
2958
2959 /* We don't actually do anything for this yet. Just return after
2960 * validating the parameters and generating the required errors.
2961 */
2962 return;
2963 }