blob: 4d62f54a95ae17d0eb97f20d6ebf311d42f849c4 [file] [log] [blame]
Brian Paul6061df02003-03-29 17:01:00 +00001/*
2 * Mesa 3-D graphics library
Brian Pauldc0b71f2009-06-02 20:29:57 -06003 * Version: 7.6
Brian Paul6061df02003-03-29 17:01:00 +00004 *
Briana429a252008-03-21 13:41:00 -06005 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
Brian Pauldc0b71f2009-06-02 20:29:57 -06006 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
Brian Paul6061df02003-03-29 17:01:00 +00007 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27/**
28 * \file bufferobj.c
Brian Paul395bcad2009-02-27 12:41:11 -070029 * \brief Functions for the GL_ARB_vertex/pixel_buffer_object extensions.
Brian Paulaa00d122003-09-15 19:55:10 +000030 * \author Brian Paul, Ian Romanick
Brian Paul6061df02003-03-29 17:01:00 +000031 */
32
33
34#include "glheader.h"
Brian Pauld6a9f5b2010-03-20 11:52:12 -060035#include "enums.h"
Ian Romanick0207b472003-09-09 00:10:12 +000036#include "hash.h"
Brian Paul6061df02003-03-29 17:01:00 +000037#include "imports.h"
Brian Paul7a6b71e2004-03-13 18:21:40 +000038#include "image.h"
Brian Paulaac73252003-04-09 02:31:35 +000039#include "context.h"
Brian Paul6061df02003-03-29 17:01:00 +000040#include "bufferobj.h"
Chris Wilson99864d52009-11-13 12:19:35 +000041#include "fbobject.h"
42#include "texobj.h"
Brian Paul6061df02003-03-29 17:01:00 +000043
Brian Paulc7b872a2003-09-09 13:44:40 +000044
Brian Paul4a7fd632009-06-09 11:53:19 -060045/* Debug flags */
46/*#define VBO_DEBUG*/
47/*#define BOUNDS_CHECK*/
48
49
Chia-I Wu066477a2010-03-30 13:13:20 +080050#if FEATURE_OES_mapbuffer
Brian Paule75b2832009-06-08 17:02:36 -060051#define DEFAULT_ACCESS GL_MAP_WRITE_BIT
Brian Paul6c72bc82008-09-25 11:46:27 -060052#else
Brian Paule75b2832009-06-08 17:02:36 -060053#define DEFAULT_ACCESS (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)
Brian Paul6c72bc82008-09-25 11:46:27 -060054#endif
55
56
Ian Romanick0207b472003-09-09 00:10:12 +000057/**
Brian Paulc552f272010-11-11 14:47:30 -070058 * Used as a placeholder for buffer objects between glGenBuffers() and
59 * glBindBuffer() so that glIsBuffer() can work correctly.
60 */
61static struct gl_buffer_object DummyBufferObject;
62
63
64/**
Brian Pauldd5691e2009-10-27 20:10:30 -060065 * Return pointer to address of a buffer object target.
66 * \param ctx the GL context
67 * \param target the buffer object target to be retrieved.
68 * \return pointer to pointer to the buffer object bound to \c target in the
69 * specified context or \c NULL if \c target is invalid.
70 */
71static INLINE struct gl_buffer_object **
Kristian Høgsbergf9995b32010-10-12 12:26:10 -040072get_buffer_target(struct gl_context *ctx, GLenum target)
Brian Pauldd5691e2009-10-27 20:10:30 -060073{
74 switch (target) {
75 case GL_ARRAY_BUFFER_ARB:
76 return &ctx->Array.ArrayBufferObj;
77 case GL_ELEMENT_ARRAY_BUFFER_ARB:
78 return &ctx->Array.ElementArrayBufferObj;
79 case GL_PIXEL_PACK_BUFFER_EXT:
80 return &ctx->Pack.BufferObj;
81 case GL_PIXEL_UNPACK_BUFFER_EXT:
82 return &ctx->Unpack.BufferObj;
83 case GL_COPY_READ_BUFFER:
Ian Romanick4da5f1b2010-09-27 14:29:12 -070084 return &ctx->CopyReadBuffer;
Brian Pauldd5691e2009-10-27 20:10:30 -060085 case GL_COPY_WRITE_BUFFER:
Ian Romanick4da5f1b2010-09-27 14:29:12 -070086 return &ctx->CopyWriteBuffer;
Brian Paul423860a2010-03-30 19:53:01 -060087#if FEATURE_EXT_transform_feedback
88 case GL_TRANSFORM_FEEDBACK_BUFFER:
89 if (ctx->Extensions.EXT_transform_feedback) {
90 return &ctx->TransformFeedback.CurrentBuffer;
91 }
92 break;
93#endif
Brian Pauldd5691e2009-10-27 20:10:30 -060094 default:
95 return NULL;
96 }
97 return NULL;
98}
99
100
101/**
Ian Romanick0207b472003-09-09 00:10:12 +0000102 * Get the buffer object bound to the specified target in a GL context.
Brian Pauldd5691e2009-10-27 20:10:30 -0600103 * \param ctx the GL context
104 * \param target the buffer object target to be retrieved.
105 * \return pointer to the buffer object bound to \c target in the
Brian Paul4277ea42006-08-25 22:06:02 +0000106 * specified context or \c NULL if \c target is invalid.
Ian Romanick0207b472003-09-09 00:10:12 +0000107 */
Brian Paulc7b872a2003-09-09 13:44:40 +0000108static INLINE struct gl_buffer_object *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400109get_buffer(struct gl_context *ctx, GLenum target)
Ian Romanick0207b472003-09-09 00:10:12 +0000110{
Brian Pauldd5691e2009-10-27 20:10:30 -0600111 struct gl_buffer_object **bufObj = get_buffer_target(ctx, target);
112 if (bufObj)
113 return *bufObj;
114 return NULL;
Ian Romanick0207b472003-09-09 00:10:12 +0000115}
116
117
118/**
Brian Paule75b2832009-06-08 17:02:36 -0600119 * Convert a GLbitfield describing the mapped buffer access flags
120 * into one of GL_READ_WRITE, GL_READ_ONLY, or GL_WRITE_ONLY.
121 */
122static GLenum
123simplified_access_mode(GLbitfield access)
124{
125 const GLbitfield rwFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
126 if ((access & rwFlags) == rwFlags)
127 return GL_READ_WRITE;
128 if ((access & GL_MAP_READ_BIT) == GL_MAP_READ_BIT)
129 return GL_READ_ONLY;
130 if ((access & GL_MAP_WRITE_BIT) == GL_MAP_WRITE_BIT)
131 return GL_WRITE_ONLY;
132 return GL_READ_WRITE; /* this should never happen, but no big deal */
133}
134
135
136/**
Ian Romanick0207b472003-09-09 00:10:12 +0000137 * Tests the subdata range parameters and sets the GL error code for
138 * \c glBufferSubDataARB and \c glGetBufferSubDataARB.
139 *
140 * \param ctx GL context.
141 * \param target Buffer object target on which to operate.
142 * \param offset Offset of the first byte of the subdata range.
143 * \param size Size, in bytes, of the subdata range.
Brian Paul6f172f72006-06-02 22:51:45 +0000144 * \param caller Name of calling function for recording errors.
Ian Romanick0207b472003-09-09 00:10:12 +0000145 * \return A pointer to the buffer object bound to \c target in the
146 * specified context or \c NULL if any of the parameter or state
147 * conditions for \c glBufferSubDataARB or \c glGetBufferSubDataARB
148 * are invalid.
149 *
150 * \sa glBufferSubDataARB, glGetBufferSubDataARB
151 */
Ian Romanick0207b472003-09-09 00:10:12 +0000152static struct gl_buffer_object *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400153buffer_object_subdata_range_good( struct gl_context * ctx, GLenum target,
Brian Paulc7b872a2003-09-09 13:44:40 +0000154 GLintptrARB offset, GLsizeiptrARB size,
Brian Paul6f172f72006-06-02 22:51:45 +0000155 const char *caller )
Ian Romanick0207b472003-09-09 00:10:12 +0000156{
157 struct gl_buffer_object *bufObj;
158
159 if (size < 0) {
Brian Paul6f172f72006-06-02 22:51:45 +0000160 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size < 0)", caller);
Ian Romanick0207b472003-09-09 00:10:12 +0000161 return NULL;
162 }
163
164 if (offset < 0) {
Brian Paul6f172f72006-06-02 22:51:45 +0000165 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset < 0)", caller);
Ian Romanick0207b472003-09-09 00:10:12 +0000166 return NULL;
167 }
168
Brian Paul4277ea42006-08-25 22:06:02 +0000169 bufObj = get_buffer(ctx, target);
170 if (!bufObj) {
171 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", caller);
Ian Romanick0207b472003-09-09 00:10:12 +0000172 return NULL;
173 }
Brian Paul60403152009-08-12 13:46:03 -0600174 if (!_mesa_is_bufferobj(bufObj)) {
Brian Paul4277ea42006-08-25 22:06:02 +0000175 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
176 return NULL;
177 }
Brian7e85b0a2007-01-23 12:50:08 -0700178 if (offset + size > bufObj->Size) {
Ian Romanick0207b472003-09-09 00:10:12 +0000179 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul6f172f72006-06-02 22:51:45 +0000180 "%s(size + offset > buffer size)", caller);
Ian Romanick0207b472003-09-09 00:10:12 +0000181 return NULL;
182 }
Brian Paul7bf6efe2009-08-07 09:40:37 -0600183 if (_mesa_bufferobj_mapped(bufObj)) {
Brian Paul2daa4132004-10-31 00:17:42 +0000184 /* Buffer is currently mapped */
Brian Paul6f172f72006-06-02 22:51:45 +0000185 _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
Ian Romanick0207b472003-09-09 00:10:12 +0000186 return NULL;
187 }
188
189 return bufObj;
190}
191
192
193/**
194 * Allocate and initialize a new buffer object.
195 *
Brian Paul395bcad2009-02-27 12:41:11 -0700196 * Default callback for the \c dd_function_table::NewBufferObject() hook.
Ian Romanick0207b472003-09-09 00:10:12 +0000197 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600198static struct gl_buffer_object *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400199_mesa_new_buffer_object( struct gl_context *ctx, GLuint name, GLenum target )
Ian Romanick0207b472003-09-09 00:10:12 +0000200{
201 struct gl_buffer_object *obj;
Brian Paula6c423d2004-08-25 15:59:48 +0000202
203 (void) ctx;
204
Brian Paulaa00d122003-09-15 19:55:10 +0000205 obj = MALLOC_STRUCT(gl_buffer_object);
Ian Romanick0207b472003-09-09 00:10:12 +0000206 _mesa_initialize_buffer_object(obj, name, target);
207 return obj;
208}
209
210
211/**
Brian Paul148a2842003-09-17 03:40:11 +0000212 * Delete a buffer object.
213 *
Brian Paul395bcad2009-02-27 12:41:11 -0700214 * Default callback for the \c dd_function_table::DeleteBuffer() hook.
Brian Paul148a2842003-09-17 03:40:11 +0000215 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600216static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400217_mesa_delete_buffer_object( struct gl_context *ctx, struct gl_buffer_object *bufObj )
Brian Paul148a2842003-09-17 03:40:11 +0000218{
Brian Paula6c423d2004-08-25 15:59:48 +0000219 (void) ctx;
220
Brian Paul148a2842003-09-17 03:40:11 +0000221 if (bufObj->Data)
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500222 free(bufObj->Data);
Brian Paul37c74af2008-09-04 14:58:02 -0600223
224 /* assign strange values here to help w/ debugging */
225 bufObj->RefCount = -1000;
226 bufObj->Name = ~0;
227
Michal Kroleeec2c32010-02-08 19:43:42 +0100228 _glthread_DESTROY_MUTEX(bufObj->Mutex);
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500229 free(bufObj);
Brian Paul148a2842003-09-17 03:40:11 +0000230}
231
232
Brian Paul37c74af2008-09-04 14:58:02 -0600233
234/**
235 * Set ptr to bufObj w/ reference counting.
236 */
Ian Romanickee34e6e2006-06-12 16:26:29 +0000237void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400238_mesa_reference_buffer_object(struct gl_context *ctx,
Brian Paul37c74af2008-09-04 14:58:02 -0600239 struct gl_buffer_object **ptr,
240 struct gl_buffer_object *bufObj)
Ian Romanickee34e6e2006-06-12 16:26:29 +0000241{
Brian Paul37c74af2008-09-04 14:58:02 -0600242 if (*ptr == bufObj)
243 return;
244
245 if (*ptr) {
Brian Paul105c8522009-05-07 13:10:48 -0600246 /* Unreference the old buffer */
Brian Paul37c74af2008-09-04 14:58:02 -0600247 GLboolean deleteFlag = GL_FALSE;
248 struct gl_buffer_object *oldObj = *ptr;
249
Michal Kroleeec2c32010-02-08 19:43:42 +0100250 _glthread_LOCK_MUTEX(oldObj->Mutex);
Brian Paul37c74af2008-09-04 14:58:02 -0600251 ASSERT(oldObj->RefCount > 0);
252 oldObj->RefCount--;
253#if 0
254 printf("BufferObj %p %d DECR to %d\n",
255 (void *) oldObj, oldObj->Name, oldObj->RefCount);
256#endif
257 deleteFlag = (oldObj->RefCount == 0);
Michal Kroleeec2c32010-02-08 19:43:42 +0100258 _glthread_UNLOCK_MUTEX(oldObj->Mutex);
Brian Paul37c74af2008-09-04 14:58:02 -0600259
260 if (deleteFlag) {
261
262 /* some sanity checking: don't delete a buffer still in use */
Brian Pauld36f4ef2008-09-15 09:07:32 -0600263#if 0
264 /* unfortunately, these tests are invalid during context tear-down */
Ian Romanickee34e6e2006-06-12 16:26:29 +0000265 ASSERT(ctx->Array.ArrayBufferObj != bufObj);
266 ASSERT(ctx->Array.ElementArrayBufferObj != bufObj);
267 ASSERT(ctx->Array.ArrayObj->Vertex.BufferObj != bufObj);
Brian Pauld36f4ef2008-09-15 09:07:32 -0600268#endif
Brian Paul37c74af2008-09-04 14:58:02 -0600269
Brian Pauld36f4ef2008-09-15 09:07:32 -0600270 ASSERT(ctx->Driver.DeleteBuffer);
Brian Paul37c74af2008-09-04 14:58:02 -0600271 ctx->Driver.DeleteBuffer(ctx, oldObj);
Ian Romanickee34e6e2006-06-12 16:26:29 +0000272 }
Brian Paul37c74af2008-09-04 14:58:02 -0600273
274 *ptr = NULL;
275 }
276 ASSERT(!*ptr);
277
278 if (bufObj) {
Brian Paul105c8522009-05-07 13:10:48 -0600279 /* reference new buffer */
Michal Kroleeec2c32010-02-08 19:43:42 +0100280 _glthread_LOCK_MUTEX(bufObj->Mutex);
Brian Paul37c74af2008-09-04 14:58:02 -0600281 if (bufObj->RefCount == 0) {
282 /* this buffer's being deleted (look just above) */
283 /* Not sure this can every really happen. Warn if it does. */
284 _mesa_problem(NULL, "referencing deleted buffer object");
285 *ptr = NULL;
286 }
287 else {
288 bufObj->RefCount++;
289#if 0
290 printf("BufferObj %p %d INCR to %d\n",
291 (void *) bufObj, bufObj->Name, bufObj->RefCount);
292#endif
293 *ptr = bufObj;
294 }
Michal Kroleeec2c32010-02-08 19:43:42 +0100295 _glthread_UNLOCK_MUTEX(bufObj->Mutex);
Ian Romanickee34e6e2006-06-12 16:26:29 +0000296 }
297}
298
299
Brian Paul148a2842003-09-17 03:40:11 +0000300/**
Ian Romanick0207b472003-09-09 00:10:12 +0000301 * Initialize a buffer object to default values.
302 */
Ian Romanick0207b472003-09-09 00:10:12 +0000303void
304_mesa_initialize_buffer_object( struct gl_buffer_object *obj,
305 GLuint name, GLenum target )
306{
Brian Paula6c423d2004-08-25 15:59:48 +0000307 (void) target;
308
Brian Paul6bf1ea82010-02-19 08:32:36 -0700309 memset(obj, 0, sizeof(struct gl_buffer_object));
Michal Kroleeec2c32010-02-08 19:43:42 +0100310 _glthread_INIT_MUTEX(obj->Mutex);
Ian Romanick0207b472003-09-09 00:10:12 +0000311 obj->RefCount = 1;
312 obj->Name = name;
Brian Paul85ad44b2004-02-13 14:04:26 +0000313 obj->Usage = GL_STATIC_DRAW_ARB;
Brian Paule75b2832009-06-08 17:02:36 -0600314 obj->AccessFlags = DEFAULT_ACCESS;
Ian Romanick0207b472003-09-09 00:10:12 +0000315}
316
317
318/**
Ian Romanick0207b472003-09-09 00:10:12 +0000319 * Allocate space for and store data in a buffer object. Any data that was
320 * previously stored in the buffer object is lost. If \c data is \c NULL,
321 * memory will be allocated, but no copy will occur.
322 *
Brian Paul395bcad2009-02-27 12:41:11 -0700323 * This is the default callback for \c dd_function_table::BufferData()
324 * Note that all GL error checking will have been done already.
Ian Romanick0207b472003-09-09 00:10:12 +0000325 *
326 * \param ctx GL context.
327 * \param target Buffer object target on which to operate.
328 * \param size Size, in bytes, of the new data store.
329 * \param data Pointer to the data to store in the buffer object. This
330 * pointer may be \c NULL.
331 * \param usage Hints about how the data will be used.
332 * \param bufObj Object to be used.
333 *
Brian Paul2f6d2a92009-09-03 09:41:41 -0600334 * \return GL_TRUE for success, GL_FALSE for failure
Ian Romanick0207b472003-09-09 00:10:12 +0000335 * \sa glBufferDataARB, dd_function_table::BufferData.
336 */
Brian Paul2f6d2a92009-09-03 09:41:41 -0600337static GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400338_mesa_buffer_data( struct gl_context *ctx, GLenum target, GLsizeiptrARB size,
Ian Romanick0207b472003-09-09 00:10:12 +0000339 const GLvoid * data, GLenum usage,
340 struct gl_buffer_object * bufObj )
341{
342 void * new_data;
343
Brian Paula6c423d2004-08-25 15:59:48 +0000344 (void) ctx; (void) target;
Ian Romanick0207b472003-09-09 00:10:12 +0000345
Brian Paul148a2842003-09-17 03:40:11 +0000346 new_data = _mesa_realloc( bufObj->Data, bufObj->Size, size );
Brian Paula7892522004-11-22 20:01:25 +0000347 if (new_data) {
Brian Paule4fcea22003-09-19 15:38:15 +0000348 bufObj->Data = (GLubyte *) new_data;
Brian Paul148a2842003-09-17 03:40:11 +0000349 bufObj->Size = size;
350 bufObj->Usage = usage;
Ian Romanick0207b472003-09-09 00:10:12 +0000351
Brian Paula7892522004-11-22 20:01:25 +0000352 if (data) {
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800353 memcpy( bufObj->Data, data, size );
Ian Romanick0207b472003-09-09 00:10:12 +0000354 }
Brian Paul2f6d2a92009-09-03 09:41:41 -0600355
356 return GL_TRUE;
357 }
358 else {
359 return GL_FALSE;
Ian Romanick0207b472003-09-09 00:10:12 +0000360 }
361}
362
363
364/**
365 * Replace data in a subrange of buffer object. If the data range
366 * specified by \c size + \c offset extends beyond the end of the buffer or
367 * if \c data is \c NULL, no copy is performed.
368 *
Brian Paul395bcad2009-02-27 12:41:11 -0700369 * This is the default callback for \c dd_function_table::BufferSubData()
370 * Note that all GL error checking will have been done already.
Ian Romanick0207b472003-09-09 00:10:12 +0000371 *
372 * \param ctx GL context.
373 * \param target Buffer object target on which to operate.
374 * \param offset Offset of the first byte to be modified.
375 * \param size Size, in bytes, of the data range.
376 * \param data Pointer to the data to store in the buffer object.
377 * \param bufObj Object to be used.
378 *
379 * \sa glBufferSubDataARB, dd_function_table::BufferSubData.
380 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600381static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400382_mesa_buffer_subdata( struct gl_context *ctx, GLenum target, GLintptrARB offset,
Ian Romanick0207b472003-09-09 00:10:12 +0000383 GLsizeiptrARB size, const GLvoid * data,
384 struct gl_buffer_object * bufObj )
385{
Brian Paula6c423d2004-08-25 15:59:48 +0000386 (void) ctx; (void) target;
387
Brian Paul76785cb2006-09-21 22:59:50 +0000388 /* this should have been caught in _mesa_BufferSubData() */
Brian7e85b0a2007-01-23 12:50:08 -0700389 ASSERT(size + offset <= bufObj->Size);
Brian Paul76785cb2006-09-21 22:59:50 +0000390
391 if (bufObj->Data) {
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800392 memcpy( (GLubyte *) bufObj->Data + offset, data, size );
Ian Romanick0207b472003-09-09 00:10:12 +0000393 }
394}
395
396
397/**
398 * Retrieve data from a subrange of buffer object. If the data range
399 * specified by \c size + \c offset extends beyond the end of the buffer or
400 * if \c data is \c NULL, no copy is performed.
401 *
Brian Paul395bcad2009-02-27 12:41:11 -0700402 * This is the default callback for \c dd_function_table::GetBufferSubData()
403 * Note that all GL error checking will have been done already.
Ian Romanick0207b472003-09-09 00:10:12 +0000404 *
405 * \param ctx GL context.
406 * \param target Buffer object target on which to operate.
Brian Paul395bcad2009-02-27 12:41:11 -0700407 * \param offset Offset of the first byte to be fetched.
Ian Romanick0207b472003-09-09 00:10:12 +0000408 * \param size Size, in bytes, of the data range.
Brian Paul395bcad2009-02-27 12:41:11 -0700409 * \param data Destination for data
Ian Romanick0207b472003-09-09 00:10:12 +0000410 * \param bufObj Object to be used.
411 *
412 * \sa glBufferGetSubDataARB, dd_function_table::GetBufferSubData.
413 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600414static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400415_mesa_buffer_get_subdata( struct gl_context *ctx, GLenum target, GLintptrARB offset,
Ian Romanick0207b472003-09-09 00:10:12 +0000416 GLsizeiptrARB size, GLvoid * data,
417 struct gl_buffer_object * bufObj )
418{
Brian Paula6c423d2004-08-25 15:59:48 +0000419 (void) ctx; (void) target;
420
Brian Paulc1f2f902005-03-03 02:05:33 +0000421 if (bufObj->Data && ((GLsizeiptrARB) (size + offset) <= bufObj->Size)) {
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800422 memcpy( data, (GLubyte *) bufObj->Data + offset, size );
Ian Romanick0207b472003-09-09 00:10:12 +0000423 }
424}
425
426
427/**
Brian Paul395bcad2009-02-27 12:41:11 -0700428 * Default callback for \c dd_function_tabel::MapBuffer().
Ian Romanick0207b472003-09-09 00:10:12 +0000429 *
Brian Paul76785cb2006-09-21 22:59:50 +0000430 * The function parameters will have been already tested for errors.
Ian Romanick0207b472003-09-09 00:10:12 +0000431 *
432 * \param ctx GL context.
433 * \param target Buffer object target on which to operate.
434 * \param access Information about how the buffer will be accessed.
Brian Paul2daa4132004-10-31 00:17:42 +0000435 * \param bufObj Object to be mapped.
Ian Romanick0207b472003-09-09 00:10:12 +0000436 * \return A pointer to the object's internal data store that can be accessed
437 * by the processor
438 *
439 * \sa glMapBufferARB, dd_function_table::MapBuffer
440 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600441static void *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400442_mesa_buffer_map( struct gl_context *ctx, GLenum target, GLenum access,
Brian Paul2daa4132004-10-31 00:17:42 +0000443 struct gl_buffer_object *bufObj )
Ian Romanick0207b472003-09-09 00:10:12 +0000444{
Brian Paul2daa4132004-10-31 00:17:42 +0000445 (void) ctx;
446 (void) target;
447 (void) access;
Brian Paul2daa4132004-10-31 00:17:42 +0000448 /* Just return a direct pointer to the data */
Brian Paul7bf6efe2009-08-07 09:40:37 -0600449 if (_mesa_bufferobj_mapped(bufObj)) {
Brian Paul2daa4132004-10-31 00:17:42 +0000450 /* already mapped! */
451 return NULL;
452 }
453 bufObj->Pointer = bufObj->Data;
Brian Pauld1da8ac2009-08-31 09:05:28 -0600454 bufObj->Length = bufObj->Size;
455 bufObj->Offset = 0;
Brian Paul2daa4132004-10-31 00:17:42 +0000456 return bufObj->Pointer;
457}
458
459
460/**
Brian Paule75b2832009-06-08 17:02:36 -0600461 * Default fallback for \c dd_function_table::MapBufferRange().
462 * Called via glMapBufferRange().
463 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600464static void *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400465_mesa_buffer_map_range( struct gl_context *ctx, GLenum target, GLintptr offset,
Brian Paule75b2832009-06-08 17:02:36 -0600466 GLsizeiptr length, GLbitfield access,
467 struct gl_buffer_object *bufObj )
468{
469 (void) ctx;
470 (void) target;
Brian Paul7bf6efe2009-08-07 09:40:37 -0600471 assert(!_mesa_bufferobj_mapped(bufObj));
Brian Paule75b2832009-06-08 17:02:36 -0600472 /* Just return a direct pointer to the data */
Brian Paul2b6ab612009-08-31 09:12:04 -0600473 bufObj->Pointer = bufObj->Data + offset;
474 bufObj->Length = length;
475 bufObj->Offset = offset;
476 bufObj->AccessFlags = access;
477 return bufObj->Pointer;
Brian Paule75b2832009-06-08 17:02:36 -0600478}
479
480
481/**
482 * Default fallback for \c dd_function_table::FlushMappedBufferRange().
483 * Called via glFlushMappedBufferRange().
484 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600485static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400486_mesa_buffer_flush_mapped_range( struct gl_context *ctx, GLenum target,
Brian Paule75b2832009-06-08 17:02:36 -0600487 GLintptr offset, GLsizeiptr length,
488 struct gl_buffer_object *obj )
489{
490 (void) ctx;
491 (void) target;
492 (void) offset;
493 (void) length;
494 (void) obj;
495 /* no-op */
496}
497
498
499/**
Brian Paul395bcad2009-02-27 12:41:11 -0700500 * Default callback for \c dd_function_table::MapBuffer().
Brian Paul2daa4132004-10-31 00:17:42 +0000501 *
502 * The input parameters will have been already tested for errors.
503 *
504 * \sa glUnmapBufferARB, dd_function_table::UnmapBuffer
505 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600506static GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400507_mesa_buffer_unmap( struct gl_context *ctx, GLenum target,
Brian Paul2daa4132004-10-31 00:17:42 +0000508 struct gl_buffer_object *bufObj )
509{
510 (void) ctx;
511 (void) target;
Brian Paul2daa4132004-10-31 00:17:42 +0000512 /* XXX we might assert here that bufObj->Pointer is non-null */
513 bufObj->Pointer = NULL;
Brian Paul822c7962009-08-31 08:59:38 -0600514 bufObj->Length = 0;
515 bufObj->Offset = 0;
516 bufObj->AccessFlags = 0x0;
Brian Paul2daa4132004-10-31 00:17:42 +0000517 return GL_TRUE;
Ian Romanick0207b472003-09-09 00:10:12 +0000518}
519
Brian Paul6061df02003-03-29 17:01:00 +0000520
Brian Paul148a2842003-09-17 03:40:11 +0000521/**
Brian Pauldc0b71f2009-06-02 20:29:57 -0600522 * Default fallback for \c dd_function_table::CopyBufferSubData().
523 * Called via glCopyBuffserSubData().
524 */
Brian Paul331eb58f2009-06-19 10:00:03 -0600525static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400526_mesa_copy_buffer_subdata(struct gl_context *ctx,
Brian Pauldc0b71f2009-06-02 20:29:57 -0600527 struct gl_buffer_object *src,
528 struct gl_buffer_object *dst,
529 GLintptr readOffset, GLintptr writeOffset,
530 GLsizeiptr size)
531{
532 GLubyte *srcPtr, *dstPtr;
533
534 /* buffer should not already be mapped */
Brian Paul7bf6efe2009-08-07 09:40:37 -0600535 assert(!_mesa_bufferobj_mapped(src));
536 assert(!_mesa_bufferobj_mapped(dst));
Brian Pauldc0b71f2009-06-02 20:29:57 -0600537
538 srcPtr = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_COPY_READ_BUFFER,
539 GL_READ_ONLY, src);
540 dstPtr = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_COPY_WRITE_BUFFER,
541 GL_WRITE_ONLY, dst);
542
543 if (srcPtr && dstPtr)
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800544 memcpy(dstPtr + writeOffset, srcPtr + readOffset, size);
Brian Pauldc0b71f2009-06-02 20:29:57 -0600545
546 ctx->Driver.UnmapBuffer(ctx, GL_COPY_READ_BUFFER, src);
547 ctx->Driver.UnmapBuffer(ctx, GL_COPY_WRITE_BUFFER, dst);
548}
549
550
551
552/**
Brian Paul148a2842003-09-17 03:40:11 +0000553 * Initialize the state associated with buffer objects
554 */
555void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400556_mesa_init_buffer_objects( struct gl_context *ctx )
Brian Paul148a2842003-09-17 03:40:11 +0000557{
Brian Paulc552f272010-11-11 14:47:30 -0700558 memset(&DummyBufferObject, 0, sizeof(DummyBufferObject));
559 DummyBufferObject.RefCount = 1000*1000*1000; /* never delete */
560
Brian Paul0854b7e2009-06-10 13:45:01 -0600561 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj,
562 ctx->Shared->NullBufferObj);
563 _mesa_reference_buffer_object(ctx, &ctx->Array.ElementArrayBufferObj,
564 ctx->Shared->NullBufferObj);
Brian Pauldc0b71f2009-06-02 20:29:57 -0600565
Brian Paul0854b7e2009-06-10 13:45:01 -0600566 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer,
567 ctx->Shared->NullBufferObj);
568 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer,
569 ctx->Shared->NullBufferObj);
Brian Paul148a2842003-09-17 03:40:11 +0000570}
571
Brian Paul395bcad2009-02-27 12:41:11 -0700572
Michal Krol01d7e3d2010-02-09 14:25:41 +0100573void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400574_mesa_free_buffer_objects( struct gl_context *ctx )
Michal Krol01d7e3d2010-02-09 14:25:41 +0100575{
576 _mesa_reference_buffer_object(ctx, &ctx->Array.ArrayBufferObj, NULL);
577 _mesa_reference_buffer_object(ctx, &ctx->Array.ElementArrayBufferObj, NULL);
578
579 _mesa_reference_buffer_object(ctx, &ctx->CopyReadBuffer, NULL);
580 _mesa_reference_buffer_object(ctx, &ctx->CopyWriteBuffer, NULL);
581}
582
583
Brian4b654d42007-08-23 08:53:43 +0100584/**
585 * Bind the specified target to buffer for the specified context.
Brian Pauldd5691e2009-10-27 20:10:30 -0600586 * Called by glBindBuffer() and other functions.
Brian4b654d42007-08-23 08:53:43 +0100587 */
588static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400589bind_buffer_object(struct gl_context *ctx, GLenum target, GLuint buffer)
Brian4b654d42007-08-23 08:53:43 +0100590{
591 struct gl_buffer_object *oldBufObj;
592 struct gl_buffer_object *newBufObj = NULL;
593 struct gl_buffer_object **bindTarget = NULL;
594
Brian Pauldd5691e2009-10-27 20:10:30 -0600595 bindTarget = get_buffer_target(ctx, target);
Brian Pauldc0b71f2009-06-02 20:29:57 -0600596 if (!bindTarget) {
Eric Anholt86af0372010-09-01 14:46:22 -0700597 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferARB(target 0x%x)", target);
Brian4b654d42007-08-23 08:53:43 +0100598 return;
599 }
600
601 /* Get pointer to old buffer object (to be unbound) */
Brian Pauldd5691e2009-10-27 20:10:30 -0600602 oldBufObj = *bindTarget;
Brian4b654d42007-08-23 08:53:43 +0100603 if (oldBufObj && oldBufObj->Name == buffer)
604 return; /* rebinding the same buffer object- no change */
605
606 /*
607 * Get pointer to new buffer object (newBufObj)
608 */
609 if (buffer == 0) {
610 /* The spec says there's not a buffer object named 0, but we use
611 * one internally because it simplifies things.
612 */
Brian Paul4f6b7042009-05-07 13:30:39 -0600613 newBufObj = ctx->Shared->NullBufferObj;
Brian4b654d42007-08-23 08:53:43 +0100614 }
615 else {
616 /* non-default buffer object */
617 newBufObj = _mesa_lookup_bufferobj(ctx, buffer);
Brian Paulc552f272010-11-11 14:47:30 -0700618 if (!newBufObj || newBufObj == &DummyBufferObject) {
619 /* If this is a new buffer object id, or one which was generated but
620 * never used before, allocate a buffer object now.
621 */
Brian4b654d42007-08-23 08:53:43 +0100622 ASSERT(ctx->Driver.NewBufferObject);
623 newBufObj = ctx->Driver.NewBufferObject(ctx, buffer, target);
624 if (!newBufObj) {
625 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindBufferARB");
626 return;
627 }
Brian Paul37c74af2008-09-04 14:58:02 -0600628 _mesa_HashInsert(ctx->Shared->BufferObjects, buffer, newBufObj);
Brian4b654d42007-08-23 08:53:43 +0100629 }
630 }
631
Brian Paul37c74af2008-09-04 14:58:02 -0600632 /* bind new buffer */
633 _mesa_reference_buffer_object(ctx, bindTarget, newBufObj);
Brian4b654d42007-08-23 08:53:43 +0100634
635 /* Pass BindBuffer call to device driver */
Brian Paulf7ca97f2009-05-14 16:51:10 -0600636 if (ctx->Driver.BindBuffer)
Brian4b654d42007-08-23 08:53:43 +0100637 ctx->Driver.BindBuffer( ctx, target, newBufObj );
Brian4b654d42007-08-23 08:53:43 +0100638}
639
640
641/**
642 * Update the default buffer objects in the given context to reference those
643 * specified in the shared state and release those referencing the old
644 * shared state.
645 */
646void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400647_mesa_update_default_objects_buffer_objects(struct gl_context *ctx)
Brian4b654d42007-08-23 08:53:43 +0100648{
649 /* Bind the NullBufferObj to remove references to those
650 * in the shared context hash table.
651 */
652 bind_buffer_object( ctx, GL_ARRAY_BUFFER_ARB, 0);
653 bind_buffer_object( ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
654 bind_buffer_object( ctx, GL_PIXEL_PACK_BUFFER_ARB, 0);
655 bind_buffer_object( ctx, GL_PIXEL_UNPACK_BUFFER_ARB, 0);
656}
657
Brian Paul148a2842003-09-17 03:40:11 +0000658
Brian Paul7a6b71e2004-03-13 18:21:40 +0000659/**
660 * When we're about to read pixel data out of a PBO (via glDrawPixels,
661 * glTexImage, etc) or write data into a PBO (via glReadPixels,
662 * glGetTexImage, etc) we call this function to check that we're not
663 * going to read out of bounds.
664 *
Brian Paul2daa4132004-10-31 00:17:42 +0000665 * XXX This would also be a convenient time to check that the PBO isn't
666 * currently mapped. Whoever calls this function should check for that.
667 * Remember, we can't use a PBO when it's mapped!
668 *
Brian Paul1b448c72009-09-03 11:29:18 -0600669 * If we're not using a PBO, this is a no-op.
670 *
Brian Paul7a6b71e2004-03-13 18:21:40 +0000671 * \param width width of image to read/write
672 * \param height height of image to read/write
673 * \param depth depth of image to read/write
674 * \param format format of image to read/write
675 * \param type datatype of image to read/write
676 * \param ptr the user-provided pointer/offset
677 * \return GL_TRUE if the PBO access is OK, GL_FALSE if the access would
678 * go out of bounds.
679 */
680GLboolean
Brian Paul60909382004-11-10 15:46:52 +0000681_mesa_validate_pbo_access(GLuint dimensions,
682 const struct gl_pixelstore_attrib *pack,
Brian Paul7a6b71e2004-03-13 18:21:40 +0000683 GLsizei width, GLsizei height, GLsizei depth,
684 GLenum format, GLenum type, const GLvoid *ptr)
685{
686 GLvoid *start, *end;
Brian Paul6b251932005-12-01 00:59:51 +0000687 const GLubyte *sizeAddr; /* buffer size, cast to a pointer */
Brian Paul7a6b71e2004-03-13 18:21:40 +0000688
Brian Paul1b448c72009-09-03 11:29:18 -0600689 if (!_mesa_is_bufferobj(pack->BufferObj))
690 return GL_TRUE; /* no PBO, OK */
Brian Paul7a6b71e2004-03-13 18:21:40 +0000691
692 if (pack->BufferObj->Size == 0)
693 /* no buffer! */
694 return GL_FALSE;
695
696 /* get address of first pixel we'll read */
Brian Paul60909382004-11-10 15:46:52 +0000697 start = _mesa_image_address(dimensions, pack, ptr, width, height,
Brian Paul7a6b71e2004-03-13 18:21:40 +0000698 format, type, 0, 0, 0);
699
700 /* get address just past the last pixel we'll read */
Brian Paul60909382004-11-10 15:46:52 +0000701 end = _mesa_image_address(dimensions, pack, ptr, width, height,
Brian Paul7a6b71e2004-03-13 18:21:40 +0000702 format, type, depth-1, height-1, width);
703
704
Brian Paul6b251932005-12-01 00:59:51 +0000705 sizeAddr = ((const GLubyte *) 0) + pack->BufferObj->Size;
706
707 if ((const GLubyte *) start > sizeAddr) {
Brian Paul7a6b71e2004-03-13 18:21:40 +0000708 /* This will catch negative values / wrap-around */
709 return GL_FALSE;
710 }
Brian Paul6b251932005-12-01 00:59:51 +0000711 if ((const GLubyte *) end > sizeAddr) {
Brian Paul7a6b71e2004-03-13 18:21:40 +0000712 /* Image read goes beyond end of buffer */
713 return GL_FALSE;
714 }
715
716 /* OK! */
717 return GL_TRUE;
718}
719
720
Brian Paul4cb9fff2006-06-03 15:32:27 +0000721/**
Brian Paul1b448c72009-09-03 11:29:18 -0600722 * For commands that read from a PBO (glDrawPixels, glTexImage,
723 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
724 * and return the pointer into the PBO. If we're not reading from a
725 * PBO, return \p src as-is.
726 * If non-null return, must call _mesa_unmap_pbo_source() when done.
727 *
728 * \return NULL if error, else pointer to start of data
Briana429a252008-03-21 13:41:00 -0600729 */
Brian Paul1b448c72009-09-03 11:29:18 -0600730const GLvoid *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400731_mesa_map_pbo_source(struct gl_context *ctx,
Briand933be62008-03-21 14:19:28 -0600732 const struct gl_pixelstore_attrib *unpack,
Brian Paul1b448c72009-09-03 11:29:18 -0600733 const GLvoid *src)
Briana429a252008-03-21 13:41:00 -0600734{
735 const GLubyte *buf;
736
Brian Paul60403152009-08-12 13:46:03 -0600737 if (_mesa_is_bufferobj(unpack->BufferObj)) {
Briana429a252008-03-21 13:41:00 -0600738 /* unpack from PBO */
Briana429a252008-03-21 13:41:00 -0600739 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
740 GL_READ_ONLY_ARB,
741 unpack->BufferObj);
742 if (!buf)
743 return NULL;
744
Brian Paul1b448c72009-09-03 11:29:18 -0600745 buf = ADD_POINTERS(buf, src);
Briana429a252008-03-21 13:41:00 -0600746 }
747 else {
748 /* unpack from normal memory */
Brian Paul1b448c72009-09-03 11:29:18 -0600749 buf = src;
Briana429a252008-03-21 13:41:00 -0600750 }
751
752 return buf;
753}
754
755
756/**
Brian Pauldc947c82009-09-03 11:22:27 -0600757 * Combine PBO-read validation and mapping.
758 * If any GL errors are detected, they'll be recorded and NULL returned.
759 * \sa _mesa_validate_pbo_access
760 * \sa _mesa_map_pbo_source
761 * A call to this function should have a matching call to
762 * _mesa_unmap_pbo_source().
763 */
764const GLvoid *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400765_mesa_map_validate_pbo_source(struct gl_context *ctx,
Brian Pauldc947c82009-09-03 11:22:27 -0600766 GLuint dimensions,
767 const struct gl_pixelstore_attrib *unpack,
768 GLsizei width, GLsizei height, GLsizei depth,
769 GLenum format, GLenum type, const GLvoid *ptr,
770 const char *where)
771{
772 ASSERT(dimensions == 1 || dimensions == 2 || dimensions == 3);
773
774 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
775 /* non-PBO access: no validation to be done */
776 return ptr;
777 }
778
779 if (!_mesa_validate_pbo_access(dimensions, unpack,
780 width, height, depth, format, type, ptr)) {
781 _mesa_error(ctx, GL_INVALID_OPERATION,
782 "%s(out of bounds PBO access)", where);
783 return NULL;
784 }
785
786 if (_mesa_bufferobj_mapped(unpack->BufferObj)) {
787 /* buffer is already mapped - that's an error */
788 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
789 return NULL;
790 }
791
792 ptr = _mesa_map_pbo_source(ctx, unpack, ptr);
793 return ptr;
794}
795
796
797/**
Brian Paul1b448c72009-09-03 11:29:18 -0600798 * Counterpart to _mesa_map_pbo_source()
Briana429a252008-03-21 13:41:00 -0600799 */
800void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400801_mesa_unmap_pbo_source(struct gl_context *ctx,
Briana429a252008-03-21 13:41:00 -0600802 const struct gl_pixelstore_attrib *unpack)
803{
Brian Paul1b448c72009-09-03 11:29:18 -0600804 ASSERT(unpack != &ctx->Pack); /* catch pack/unpack mismatch */
Brian Paul60403152009-08-12 13:46:03 -0600805 if (_mesa_is_bufferobj(unpack->BufferObj)) {
Briana429a252008-03-21 13:41:00 -0600806 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
807 unpack->BufferObj);
808 }
809}
810
811
812/**
Brian Paul1b448c72009-09-03 11:29:18 -0600813 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
814 * if we're writing to a PBO, map it write-only and return the pointer
815 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
816 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
817 *
818 * \return NULL if error, else pointer to start of data
Briana429a252008-03-21 13:41:00 -0600819 */
820void *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400821_mesa_map_pbo_dest(struct gl_context *ctx,
Brian Paul1b448c72009-09-03 11:29:18 -0600822 const struct gl_pixelstore_attrib *pack,
823 GLvoid *dest)
Briana429a252008-03-21 13:41:00 -0600824{
825 void *buf;
826
Brian Paul60403152009-08-12 13:46:03 -0600827 if (_mesa_is_bufferobj(pack->BufferObj)) {
Briana429a252008-03-21 13:41:00 -0600828 /* pack into PBO */
Briana429a252008-03-21 13:41:00 -0600829 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
830 GL_WRITE_ONLY_ARB,
831 pack->BufferObj);
832 if (!buf)
833 return NULL;
834
835 buf = ADD_POINTERS(buf, dest);
836 }
837 else {
838 /* pack to normal memory */
839 buf = dest;
840 }
841
842 return buf;
843}
844
845
846/**
Brian Pauldc947c82009-09-03 11:22:27 -0600847 * Combine PBO-write validation and mapping.
848 * If any GL errors are detected, they'll be recorded and NULL returned.
849 * \sa _mesa_validate_pbo_access
850 * \sa _mesa_map_pbo_dest
851 * A call to this function should have a matching call to
852 * _mesa_unmap_pbo_dest().
853 */
854GLvoid *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400855_mesa_map_validate_pbo_dest(struct gl_context *ctx,
Brian Pauldc947c82009-09-03 11:22:27 -0600856 GLuint dimensions,
857 const struct gl_pixelstore_attrib *unpack,
858 GLsizei width, GLsizei height, GLsizei depth,
859 GLenum format, GLenum type, GLvoid *ptr,
860 const char *where)
861{
862 ASSERT(dimensions == 1 || dimensions == 2 || dimensions == 3);
863
864 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
865 /* non-PBO access: no validation to be done */
866 return ptr;
867 }
868
869 if (!_mesa_validate_pbo_access(dimensions, unpack,
870 width, height, depth, format, type, ptr)) {
871 _mesa_error(ctx, GL_INVALID_OPERATION,
872 "%s(out of bounds PBO access)", where);
873 return NULL;
874 }
875
876 if (_mesa_bufferobj_mapped(unpack->BufferObj)) {
877 /* buffer is already mapped - that's an error */
878 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
879 return NULL;
880 }
881
882 ptr = _mesa_map_pbo_dest(ctx, unpack, ptr);
883 return ptr;
884}
885
886
887/**
Brian Paul1b448c72009-09-03 11:29:18 -0600888 * Counterpart to _mesa_map_pbo_dest()
Briana429a252008-03-21 13:41:00 -0600889 */
890void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400891_mesa_unmap_pbo_dest(struct gl_context *ctx,
Brian Paul1b448c72009-09-03 11:29:18 -0600892 const struct gl_pixelstore_attrib *pack)
Briana429a252008-03-21 13:41:00 -0600893{
Brian Paul1b448c72009-09-03 11:29:18 -0600894 ASSERT(pack != &ctx->Unpack); /* catch pack/unpack mismatch */
Brian Paul60403152009-08-12 13:46:03 -0600895 if (_mesa_is_bufferobj(pack->BufferObj)) {
Briana429a252008-03-21 13:41:00 -0600896 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT, pack->BufferObj);
897 }
898}
899
900
Briand933be62008-03-21 14:19:28 -0600901
Briana429a252008-03-21 13:41:00 -0600902/**
Brian Paul4cb9fff2006-06-03 15:32:27 +0000903 * Return the gl_buffer_object for the given ID.
904 * Always return NULL for ID 0.
905 */
Brian Paul4d12a052006-08-23 23:10:14 +0000906struct gl_buffer_object *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400907_mesa_lookup_bufferobj(struct gl_context *ctx, GLuint buffer)
Brian Paul4cb9fff2006-06-03 15:32:27 +0000908{
909 if (buffer == 0)
910 return NULL;
911 else
912 return (struct gl_buffer_object *)
913 _mesa_HashLookup(ctx->Shared->BufferObjects, buffer);
914}
915
Brian Paul7a6b71e2004-03-13 18:21:40 +0000916
Brian Paul37c74af2008-09-04 14:58:02 -0600917/**
918 * If *ptr points to obj, set ptr = the Null/default buffer object.
919 * This is a helper for buffer object deletion.
920 * The GL spec says that deleting a buffer object causes it to get
921 * unbound from all arrays in the current context.
922 */
923static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400924unbind(struct gl_context *ctx,
Brian Paul37c74af2008-09-04 14:58:02 -0600925 struct gl_buffer_object **ptr,
926 struct gl_buffer_object *obj)
927{
928 if (*ptr == obj) {
Brian Paul4f6b7042009-05-07 13:30:39 -0600929 _mesa_reference_buffer_object(ctx, ptr, ctx->Shared->NullBufferObj);
Brian Paul37c74af2008-09-04 14:58:02 -0600930 }
931}
932
933
Brian Paul331eb58f2009-06-19 10:00:03 -0600934/**
935 * Plug default/fallback buffer object functions into the device
936 * driver hooks.
937 */
938void
939_mesa_init_buffer_object_functions(struct dd_function_table *driver)
940{
941 /* GL_ARB_vertex/pixel_buffer_object */
942 driver->NewBufferObject = _mesa_new_buffer_object;
943 driver->DeleteBuffer = _mesa_delete_buffer_object;
944 driver->BindBuffer = NULL;
945 driver->BufferData = _mesa_buffer_data;
946 driver->BufferSubData = _mesa_buffer_subdata;
947 driver->GetBufferSubData = _mesa_buffer_get_subdata;
948 driver->MapBuffer = _mesa_buffer_map;
949 driver->UnmapBuffer = _mesa_buffer_unmap;
950
951 /* GL_ARB_map_buffer_range */
952 driver->MapBufferRange = _mesa_buffer_map_range;
953 driver->FlushMappedBufferRange = _mesa_buffer_flush_mapped_range;
954
955 /* GL_ARB_copy_buffer */
956 driver->CopyBufferSubData = _mesa_copy_buffer_subdata;
957}
958
959
Brian Paul148a2842003-09-17 03:40:11 +0000960
961/**********************************************************************/
962/* API Functions */
963/**********************************************************************/
964
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000965void GLAPIENTRY
Brian Paul6061df02003-03-29 17:01:00 +0000966_mesa_BindBufferARB(GLenum target, GLuint buffer)
967{
Brian Paulaac73252003-04-09 02:31:35 +0000968 GET_CURRENT_CONTEXT(ctx);
969 ASSERT_OUTSIDE_BEGIN_END(ctx);
970
Brian4b654d42007-08-23 08:53:43 +0100971 bind_buffer_object(ctx, target, buffer);
Brian Paul6061df02003-03-29 17:01:00 +0000972}
973
Ian Romanick0207b472003-09-09 00:10:12 +0000974
975/**
976 * Delete a set of buffer objects.
977 *
978 * \param n Number of buffer objects to delete.
Jose Fonseca375457b2004-09-09 22:23:24 +0000979 * \param ids Array of \c n buffer object IDs.
Ian Romanick0207b472003-09-09 00:10:12 +0000980 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000981void GLAPIENTRY
Ian Romanick0207b472003-09-09 00:10:12 +0000982_mesa_DeleteBuffersARB(GLsizei n, const GLuint *ids)
Brian Paul6061df02003-03-29 17:01:00 +0000983{
Brian Paulaac73252003-04-09 02:31:35 +0000984 GET_CURRENT_CONTEXT(ctx);
Karl Schultzdf8d3372003-09-18 15:14:10 +0000985 GLsizei i;
Brian Paulaac73252003-04-09 02:31:35 +0000986 ASSERT_OUTSIDE_BEGIN_END(ctx);
987
988 if (n < 0) {
989 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteBuffersARB(n)");
990 return;
991 }
992
Ian Romanick0207b472003-09-09 00:10:12 +0000993 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
994
995 for (i = 0; i < n; i++) {
Brian Paul4d12a052006-08-23 23:10:14 +0000996 struct gl_buffer_object *bufObj = _mesa_lookup_bufferobj(ctx, ids[i]);
Brian Paul4cb9fff2006-06-03 15:32:27 +0000997 if (bufObj) {
Brian Paula65f3852009-05-22 14:14:53 -0600998 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
Brian Paul4cb9fff2006-06-03 15:32:27 +0000999 GLuint j;
Brian Paul94ec5252004-03-04 14:46:00 +00001000
Brian Paul78587ea2010-11-11 15:31:28 -07001001 ASSERT(bufObj->Name == ids[i] || bufObj == &DummyBufferObject);
Brian Paul94ec5252004-03-04 14:46:00 +00001002
Brian Paul7bf6efe2009-08-07 09:40:37 -06001003 if (_mesa_bufferobj_mapped(bufObj)) {
Brian Paula7f434b2009-02-27 13:04:38 -07001004 /* if mapped, unmap it now */
1005 ctx->Driver.UnmapBuffer(ctx, 0, bufObj);
Brian Paule75b2832009-06-08 17:02:36 -06001006 bufObj->AccessFlags = DEFAULT_ACCESS;
Brian Paul67025f72009-02-27 13:10:45 -07001007 bufObj->Pointer = NULL;
Brian Paula7f434b2009-02-27 13:04:38 -07001008 }
1009
Brian Paul395bcad2009-02-27 12:41:11 -07001010 /* unbind any vertex pointers bound to this buffer */
Brian Paula65f3852009-05-22 14:14:53 -06001011 unbind(ctx, &arrayObj->Vertex.BufferObj, bufObj);
1012 unbind(ctx, &arrayObj->Weight.BufferObj, bufObj);
1013 unbind(ctx, &arrayObj->Normal.BufferObj, bufObj);
1014 unbind(ctx, &arrayObj->Color.BufferObj, bufObj);
1015 unbind(ctx, &arrayObj->SecondaryColor.BufferObj, bufObj);
1016 unbind(ctx, &arrayObj->FogCoord.BufferObj, bufObj);
1017 unbind(ctx, &arrayObj->Index.BufferObj, bufObj);
1018 unbind(ctx, &arrayObj->EdgeFlag.BufferObj, bufObj);
Brian Paulebb991c2009-05-22 14:16:00 -06001019 for (j = 0; j < Elements(arrayObj->TexCoord); j++) {
Brian Paula65f3852009-05-22 14:14:53 -06001020 unbind(ctx, &arrayObj->TexCoord[j].BufferObj, bufObj);
Ian Romanick0207b472003-09-09 00:10:12 +00001021 }
Brian Paulebb991c2009-05-22 14:16:00 -06001022 for (j = 0; j < Elements(arrayObj->VertexAttrib); j++) {
Brian Paula65f3852009-05-22 14:14:53 -06001023 unbind(ctx, &arrayObj->VertexAttrib[j].BufferObj, bufObj);
Brian Paul4cb9fff2006-06-03 15:32:27 +00001024 }
1025
1026 if (ctx->Array.ArrayBufferObj == bufObj) {
1027 _mesa_BindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
1028 }
1029 if (ctx->Array.ElementArrayBufferObj == bufObj) {
1030 _mesa_BindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
1031 }
1032
Brian Paul395bcad2009-02-27 12:41:11 -07001033 /* unbind any pixel pack/unpack pointers bound to this buffer */
Brian Paul4cb9fff2006-06-03 15:32:27 +00001034 if (ctx->Pack.BufferObj == bufObj) {
1035 _mesa_BindBufferARB( GL_PIXEL_PACK_BUFFER_EXT, 0 );
1036 }
1037 if (ctx->Unpack.BufferObj == bufObj) {
1038 _mesa_BindBufferARB( GL_PIXEL_UNPACK_BUFFER_EXT, 0 );
1039 }
1040
Brian4b654d42007-08-23 08:53:43 +01001041 /* The ID is immediately freed for re-use */
Brian Paul78587ea2010-11-11 15:31:28 -07001042 _mesa_HashRemove(ctx->Shared->BufferObjects, ids[i]);
Brian Paul37c74af2008-09-04 14:58:02 -06001043 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
Ian Romanick0207b472003-09-09 00:10:12 +00001044 }
1045 }
1046
1047 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul6061df02003-03-29 17:01:00 +00001048}
1049
Ian Romanick0207b472003-09-09 00:10:12 +00001050
1051/**
1052 * Generate a set of unique buffer object IDs and store them in \c buffer.
1053 *
1054 * \param n Number of IDs to generate.
1055 * \param buffer Array of \c n locations to store the IDs.
1056 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001057void GLAPIENTRY
Brian Paulaac73252003-04-09 02:31:35 +00001058_mesa_GenBuffersARB(GLsizei n, GLuint *buffer)
Brian Paul6061df02003-03-29 17:01:00 +00001059{
Brian Paulaac73252003-04-09 02:31:35 +00001060 GET_CURRENT_CONTEXT(ctx);
Ian Romanick0207b472003-09-09 00:10:12 +00001061 GLuint first;
1062 GLint i;
Brian Paulaac73252003-04-09 02:31:35 +00001063 ASSERT_OUTSIDE_BEGIN_END(ctx);
1064
1065 if (n < 0) {
Ian Romanick0207b472003-09-09 00:10:12 +00001066 _mesa_error(ctx, GL_INVALID_VALUE, "glGenBuffersARB");
Brian Paulaac73252003-04-09 02:31:35 +00001067 return;
1068 }
Ian Romanick0207b472003-09-09 00:10:12 +00001069
Brian Paula7892522004-11-22 20:01:25 +00001070 if (!buffer) {
Ian Romanick0207b472003-09-09 00:10:12 +00001071 return;
1072 }
1073
1074 /*
1075 * This must be atomic (generation and allocation of buffer object IDs)
1076 */
1077 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1078
1079 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
1080
Brian Paulc552f272010-11-11 14:47:30 -07001081 /* Insert the ID and pointer to dummy buffer object into hash table */
Ian Romanick0207b472003-09-09 00:10:12 +00001082 for (i = 0; i < n; i++) {
Brian Paulc552f272010-11-11 14:47:30 -07001083 _mesa_HashInsert(ctx->Shared->BufferObjects, first + i,
1084 &DummyBufferObject);
Brian Paulaa00d122003-09-15 19:55:10 +00001085 buffer[i] = first + i;
Ian Romanick0207b472003-09-09 00:10:12 +00001086 }
1087
1088 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul6061df02003-03-29 17:01:00 +00001089}
1090
Ian Romanick0207b472003-09-09 00:10:12 +00001091
1092/**
1093 * Determine if ID is the name of a buffer object.
1094 *
1095 * \param id ID of the potential buffer object.
1096 * \return \c GL_TRUE if \c id is the name of a buffer object,
1097 * \c GL_FALSE otherwise.
1098 */
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001099GLboolean GLAPIENTRY
Ian Romanick0207b472003-09-09 00:10:12 +00001100_mesa_IsBufferARB(GLuint id)
Brian Paul6061df02003-03-29 17:01:00 +00001101{
Brian Paul4cb9fff2006-06-03 15:32:27 +00001102 struct gl_buffer_object *bufObj;
Ian Romanick0207b472003-09-09 00:10:12 +00001103 GET_CURRENT_CONTEXT(ctx);
1104 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1105
Ian Romanick0207b472003-09-09 00:10:12 +00001106 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
Brian Paul4d12a052006-08-23 23:10:14 +00001107 bufObj = _mesa_lookup_bufferobj(ctx, id);
Ian Romanick0207b472003-09-09 00:10:12 +00001108 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1109
Brian Paulc552f272010-11-11 14:47:30 -07001110 return bufObj && bufObj != &DummyBufferObject;
Brian Paul6061df02003-03-29 17:01:00 +00001111}
1112
Ian Romanick0207b472003-09-09 00:10:12 +00001113
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001114void GLAPIENTRY
Brian Paul6061df02003-03-29 17:01:00 +00001115_mesa_BufferDataARB(GLenum target, GLsizeiptrARB size,
1116 const GLvoid * data, GLenum usage)
1117{
Brian Paulaac73252003-04-09 02:31:35 +00001118 GET_CURRENT_CONTEXT(ctx);
Ian Romanick0207b472003-09-09 00:10:12 +00001119 struct gl_buffer_object *bufObj;
Brian Paulaac73252003-04-09 02:31:35 +00001120 ASSERT_OUTSIDE_BEGIN_END(ctx);
1121
1122 if (size < 0) {
1123 _mesa_error(ctx, GL_INVALID_VALUE, "glBufferDataARB(size < 0)");
1124 return;
1125 }
1126
1127 switch (usage) {
Brian Paule176ae52010-03-05 09:23:43 -07001128 case GL_STREAM_DRAW_ARB:
1129 case GL_STREAM_READ_ARB:
1130 case GL_STREAM_COPY_ARB:
1131 case GL_STATIC_DRAW_ARB:
1132 case GL_STATIC_READ_ARB:
1133 case GL_STATIC_COPY_ARB:
1134 case GL_DYNAMIC_DRAW_ARB:
1135 case GL_DYNAMIC_READ_ARB:
1136 case GL_DYNAMIC_COPY_ARB:
1137 /* OK */
1138 break;
1139 default:
1140 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(usage)");
1141 return;
Brian Paulaac73252003-04-09 02:31:35 +00001142 }
1143
Brian Paul4277ea42006-08-25 22:06:02 +00001144 bufObj = get_buffer(ctx, target);
1145 if (!bufObj) {
1146 _mesa_error(ctx, GL_INVALID_ENUM, "glBufferDataARB(target)" );
1147 return;
1148 }
Brian Paul60403152009-08-12 13:46:03 -06001149 if (!_mesa_is_bufferobj(bufObj)) {
Brian Paulf7ca97f2009-05-14 16:51:10 -06001150 _mesa_error(ctx, GL_INVALID_OPERATION, "glBufferDataARB(buffer 0)" );
Brian Paulaac73252003-04-09 02:31:35 +00001151 return;
1152 }
Ian Romanick0207b472003-09-09 00:10:12 +00001153
Brian Paul7bf6efe2009-08-07 09:40:37 -06001154 if (_mesa_bufferobj_mapped(bufObj)) {
Brian Paul75e3ccf2009-02-27 12:30:21 -07001155 /* Unmap the existing buffer. We'll replace it now. Not an error. */
1156 ctx->Driver.UnmapBuffer(ctx, target, bufObj);
Brian Paule75b2832009-06-08 17:02:36 -06001157 bufObj->AccessFlags = DEFAULT_ACCESS;
Brian Paul822c7962009-08-31 08:59:38 -06001158 ASSERT(bufObj->Pointer == NULL);
Brian Paul66e6e3e2003-09-17 21:18:22 +00001159 }
1160
Brian Paulb9d0f942009-05-06 15:17:25 -06001161 FLUSH_VERTICES(ctx, _NEW_BUFFER_OBJECT);
1162
Brian Pauldcca97a2009-05-06 15:23:09 -06001163 bufObj->Written = GL_TRUE;
1164
Brian Paule7927622009-06-03 15:38:57 -06001165#ifdef VBO_DEBUG
Kristian Høgsberg298be2b2010-02-19 12:32:24 -05001166 printf("glBufferDataARB(%u, sz %ld, from %p, usage 0x%x)\n",
Brian Paule7927622009-06-03 15:38:57 -06001167 bufObj->Name, size, data, usage);
1168#endif
1169
Brian Paul4a7fd632009-06-09 11:53:19 -06001170#ifdef BOUNDS_CHECK
1171 size += 100;
1172#endif
Brian Paul2f6d2a92009-09-03 09:41:41 -06001173
1174 ASSERT(ctx->Driver.BufferData);
1175 if (!ctx->Driver.BufferData( ctx, target, size, data, usage, bufObj )) {
Brian Paulf338de42009-09-22 13:47:49 -06001176 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB()");
Brian Paul2f6d2a92009-09-03 09:41:41 -06001177 }
Brian Paul6061df02003-03-29 17:01:00 +00001178}
1179
Brian Paulc7b872a2003-09-09 13:44:40 +00001180
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001181void GLAPIENTRY
Brian Paul6061df02003-03-29 17:01:00 +00001182_mesa_BufferSubDataARB(GLenum target, GLintptrARB offset,
1183 GLsizeiptrARB size, const GLvoid * data)
1184{
Brian Paulaac73252003-04-09 02:31:35 +00001185 GET_CURRENT_CONTEXT(ctx);
Ian Romanick0207b472003-09-09 00:10:12 +00001186 struct gl_buffer_object *bufObj;
Brian Paulaac73252003-04-09 02:31:35 +00001187 ASSERT_OUTSIDE_BEGIN_END(ctx);
1188
Brian Paulc7b872a2003-09-09 13:44:40 +00001189 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
Brian Paul4277ea42006-08-25 22:06:02 +00001190 "glBufferSubDataARB" );
Brian Paulaa00d122003-09-15 19:55:10 +00001191 if (!bufObj) {
Brian Paul2daa4132004-10-31 00:17:42 +00001192 /* error already recorded */
Brian Paulaa00d122003-09-15 19:55:10 +00001193 return;
Brian Paulaac73252003-04-09 02:31:35 +00001194 }
Brian Paulaa00d122003-09-15 19:55:10 +00001195
Brian Paul6e2e1362010-11-09 12:24:49 -07001196 if (size == 0)
1197 return;
1198
Brian Pauldcca97a2009-05-06 15:23:09 -06001199 bufObj->Written = GL_TRUE;
1200
Brian Paulaa00d122003-09-15 19:55:10 +00001201 ASSERT(ctx->Driver.BufferSubData);
Brian Paul6f172f72006-06-02 22:51:45 +00001202 ctx->Driver.BufferSubData( ctx, target, offset, size, data, bufObj );
Brian Paul6061df02003-03-29 17:01:00 +00001203}
1204
Brian Paulc7b872a2003-09-09 13:44:40 +00001205
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001206void GLAPIENTRY
Brian Paul6061df02003-03-29 17:01:00 +00001207_mesa_GetBufferSubDataARB(GLenum target, GLintptrARB offset,
1208 GLsizeiptrARB size, void * data)
1209{
Brian Paulaac73252003-04-09 02:31:35 +00001210 GET_CURRENT_CONTEXT(ctx);
Ian Romanick0207b472003-09-09 00:10:12 +00001211 struct gl_buffer_object *bufObj;
Brian Paulaac73252003-04-09 02:31:35 +00001212 ASSERT_OUTSIDE_BEGIN_END(ctx);
1213
Brian Paulc7b872a2003-09-09 13:44:40 +00001214 bufObj = buffer_object_subdata_range_good( ctx, target, offset, size,
Brian Paul4277ea42006-08-25 22:06:02 +00001215 "glGetBufferSubDataARB" );
Brian Paulaa00d122003-09-15 19:55:10 +00001216 if (!bufObj) {
Brian Paul2daa4132004-10-31 00:17:42 +00001217 /* error already recorded */
Brian Paulaa00d122003-09-15 19:55:10 +00001218 return;
Brian Paulaac73252003-04-09 02:31:35 +00001219 }
Brian Paul66e6e3e2003-09-17 21:18:22 +00001220
Brian Paulaa00d122003-09-15 19:55:10 +00001221 ASSERT(ctx->Driver.GetBufferSubData);
Brian Paul6f172f72006-06-02 22:51:45 +00001222 ctx->Driver.GetBufferSubData( ctx, target, offset, size, data, bufObj );
Brian Paul6061df02003-03-29 17:01:00 +00001223}
1224
Brian Paulc7b872a2003-09-09 13:44:40 +00001225
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001226void * GLAPIENTRY
Brian Paul6061df02003-03-29 17:01:00 +00001227_mesa_MapBufferARB(GLenum target, GLenum access)
1228{
Brian Paulaac73252003-04-09 02:31:35 +00001229 GET_CURRENT_CONTEXT(ctx);
Ian Romanick0207b472003-09-09 00:10:12 +00001230 struct gl_buffer_object * bufObj;
Brian Paule75b2832009-06-08 17:02:36 -06001231 GLbitfield accessFlags;
Brian Paul92033a92009-08-31 08:50:15 -06001232 void *map;
1233
Brian Paulea31ca42003-05-10 04:35:09 +00001234 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
Brian Paulaac73252003-04-09 02:31:35 +00001235
1236 switch (access) {
Brian Paule176ae52010-03-05 09:23:43 -07001237 case GL_READ_ONLY_ARB:
1238 accessFlags = GL_MAP_READ_BIT;
1239 break;
1240 case GL_WRITE_ONLY_ARB:
1241 accessFlags = GL_MAP_WRITE_BIT;
1242 break;
1243 case GL_READ_WRITE_ARB:
1244 accessFlags = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1245 break;
1246 default:
1247 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(access)");
1248 return NULL;
Brian Paulaac73252003-04-09 02:31:35 +00001249 }
1250
Brian Paul4277ea42006-08-25 22:06:02 +00001251 bufObj = get_buffer(ctx, target);
1252 if (!bufObj) {
1253 _mesa_error(ctx, GL_INVALID_ENUM, "glMapBufferARB(target)" );
1254 return NULL;
1255 }
Brian Paul60403152009-08-12 13:46:03 -06001256 if (!_mesa_is_bufferobj(bufObj)) {
Brian Paulf7ca97f2009-05-14 16:51:10 -06001257 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(buffer 0)" );
Brian Paulea31ca42003-05-10 04:35:09 +00001258 return NULL;
Brian Paulaac73252003-04-09 02:31:35 +00001259 }
Brian Paul7bf6efe2009-08-07 09:40:37 -06001260 if (_mesa_bufferobj_mapped(bufObj)) {
Brian Paul66e6e3e2003-09-17 21:18:22 +00001261 _mesa_error(ctx, GL_INVALID_OPERATION, "glMapBufferARB(already mapped)");
Ian Romanick0207b472003-09-09 00:10:12 +00001262 return NULL;
1263 }
1264
1265 ASSERT(ctx->Driver.MapBuffer);
Brian Paul92033a92009-08-31 08:50:15 -06001266 map = ctx->Driver.MapBuffer( ctx, target, access, bufObj );
1267 if (!map) {
Brian Paulf338de42009-09-22 13:47:49 -06001268 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
Brian Paul2f6d2a92009-09-03 09:41:41 -06001269 return NULL;
Ian Romanick0207b472003-09-09 00:10:12 +00001270 }
Brian Paul2f6d2a92009-09-03 09:41:41 -06001271 else {
Brian Paul92033a92009-08-31 08:50:15 -06001272 /* The driver callback should have set these fields.
1273 * This is important because other modules (like VBO) might call
1274 * the driver function directly.
1275 */
1276 ASSERT(bufObj->Pointer == map);
1277 ASSERT(bufObj->Length == bufObj->Size);
1278 ASSERT(bufObj->Offset == 0);
1279 bufObj->AccessFlags = accessFlags;
1280 }
Brian Paule75b2832009-06-08 17:02:36 -06001281
Brian Pauldcca97a2009-05-06 15:23:09 -06001282 if (access == GL_WRITE_ONLY_ARB || access == GL_READ_WRITE_ARB)
1283 bufObj->Written = GL_TRUE;
Brian Paul0bb281b2003-10-14 15:48:39 +00001284
Brian Paule7927622009-06-03 15:38:57 -06001285#ifdef VBO_DEBUG
Kristian Høgsberg298be2b2010-02-19 12:32:24 -05001286 printf("glMapBufferARB(%u, sz %ld, access 0x%x)\n",
1287 bufObj->Name, bufObj->Size, access);
Brian Paule7927622009-06-03 15:38:57 -06001288 if (access == GL_WRITE_ONLY_ARB) {
1289 GLuint i;
1290 GLubyte *b = (GLubyte *) bufObj->Pointer;
1291 for (i = 0; i < bufObj->Size; i++)
1292 b[i] = i & 0xff;
1293 }
1294#endif
1295
Brian Paul4a7fd632009-06-09 11:53:19 -06001296#ifdef BOUNDS_CHECK
1297 {
1298 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1299 GLuint i;
1300 /* buffer is 100 bytes larger than requested, fill with magic value */
1301 for (i = 0; i < 100; i++) {
1302 buf[bufObj->Size - i - 1] = 123;
1303 }
1304 }
1305#endif
1306
Brian Paul148a2842003-09-17 03:40:11 +00001307 return bufObj->Pointer;
Brian Paul6061df02003-03-29 17:01:00 +00001308}
1309
Brian Paulc7b872a2003-09-09 13:44:40 +00001310
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001311GLboolean GLAPIENTRY
Brian Paul6061df02003-03-29 17:01:00 +00001312_mesa_UnmapBufferARB(GLenum target)
1313{
Brian Paulaac73252003-04-09 02:31:35 +00001314 GET_CURRENT_CONTEXT(ctx);
Ian Romanick0207b472003-09-09 00:10:12 +00001315 struct gl_buffer_object *bufObj;
1316 GLboolean status = GL_TRUE;
Brian Paulaac73252003-04-09 02:31:35 +00001317 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1318
Brian Paul4277ea42006-08-25 22:06:02 +00001319 bufObj = get_buffer(ctx, target);
1320 if (!bufObj) {
1321 _mesa_error(ctx, GL_INVALID_ENUM, "glUnmapBufferARB(target)" );
1322 return GL_FALSE;
1323 }
Brian Paul60403152009-08-12 13:46:03 -06001324 if (!_mesa_is_bufferobj(bufObj)) {
Brian Paul148a2842003-09-17 03:40:11 +00001325 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB" );
Brian Paulaac73252003-04-09 02:31:35 +00001326 return GL_FALSE;
1327 }
Brian Paul7bf6efe2009-08-07 09:40:37 -06001328 if (!_mesa_bufferobj_mapped(bufObj)) {
Ian Romanick0207b472003-09-09 00:10:12 +00001329 _mesa_error(ctx, GL_INVALID_OPERATION, "glUnmapBufferARB");
1330 return GL_FALSE;
1331 }
1332
Brian Paul4a7fd632009-06-09 11:53:19 -06001333#ifdef BOUNDS_CHECK
1334 if (bufObj->Access != GL_READ_ONLY_ARB) {
1335 GLubyte *buf = (GLubyte *) bufObj->Pointer;
1336 GLuint i;
1337 /* check that last 100 bytes are still = magic value */
1338 for (i = 0; i < 100; i++) {
1339 GLuint pos = bufObj->Size - i - 1;
1340 if (buf[pos] != 123) {
1341 _mesa_warning(ctx, "Out of bounds buffer object write detected"
1342 " at position %d (value = %u)\n",
1343 pos, buf[pos]);
1344 }
1345 }
1346 }
1347#endif
1348
Brian Paule7927622009-06-03 15:38:57 -06001349#ifdef VBO_DEBUG
Brian Paule75b2832009-06-08 17:02:36 -06001350 if (bufObj->AccessFlags & GL_MAP_WRITE_BIT) {
Brian Paule7927622009-06-03 15:38:57 -06001351 GLuint i, unchanged = 0;
1352 GLubyte *b = (GLubyte *) bufObj->Pointer;
1353 GLint pos = -1;
1354 /* check which bytes changed */
1355 for (i = 0; i < bufObj->Size - 1; i++) {
1356 if (b[i] == (i & 0xff) && b[i+1] == ((i+1) & 0xff)) {
1357 unchanged++;
1358 if (pos == -1)
1359 pos = i;
1360 }
1361 }
1362 if (unchanged) {
Kristian Høgsberg298be2b2010-02-19 12:32:24 -05001363 printf("glUnmapBufferARB(%u): %u of %ld unchanged, starting at %d\n",
Brian Paule7927622009-06-03 15:38:57 -06001364 bufObj->Name, unchanged, bufObj->Size, pos);
1365 }
1366 }
1367#endif
1368
Brian Paul67025f72009-02-27 13:10:45 -07001369 status = ctx->Driver.UnmapBuffer( ctx, target, bufObj );
Brian Paule75b2832009-06-08 17:02:36 -06001370 bufObj->AccessFlags = DEFAULT_ACCESS;
Brian Paul822c7962009-08-31 08:59:38 -06001371 ASSERT(bufObj->Pointer == NULL);
1372 ASSERT(bufObj->Offset == 0);
1373 ASSERT(bufObj->Length == 0);
Ian Romanick0207b472003-09-09 00:10:12 +00001374
1375 return status;
Brian Paul6061df02003-03-29 17:01:00 +00001376}
1377
Brian Paulc7b872a2003-09-09 13:44:40 +00001378
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001379void GLAPIENTRY
Brian Paul6061df02003-03-29 17:01:00 +00001380_mesa_GetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
1381{
Brian Paulaac73252003-04-09 02:31:35 +00001382 GET_CURRENT_CONTEXT(ctx);
Ian Romanick0207b472003-09-09 00:10:12 +00001383 struct gl_buffer_object *bufObj;
Brian Paulaac73252003-04-09 02:31:35 +00001384 ASSERT_OUTSIDE_BEGIN_END(ctx);
1385
Brian Paul4277ea42006-08-25 22:06:02 +00001386 bufObj = get_buffer(ctx, target);
1387 if (!bufObj) {
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001388 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(target)" );
Brian Paul4277ea42006-08-25 22:06:02 +00001389 return;
1390 }
Brian Paul60403152009-08-12 13:46:03 -06001391 if (!_mesa_is_bufferobj(bufObj)) {
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001392 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferParameterivARB" );
Ian Romanick0207b472003-09-09 00:10:12 +00001393 return;
1394 }
1395
Brian Paulaac73252003-04-09 02:31:35 +00001396 switch (pname) {
Brian Paule176ae52010-03-05 09:23:43 -07001397 case GL_BUFFER_SIZE_ARB:
1398 *params = (GLint) bufObj->Size;
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001399 return;
Brian Paule176ae52010-03-05 09:23:43 -07001400 case GL_BUFFER_USAGE_ARB:
1401 *params = bufObj->Usage;
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001402 return;
Brian Paule176ae52010-03-05 09:23:43 -07001403 case GL_BUFFER_ACCESS_ARB:
1404 *params = simplified_access_mode(bufObj->AccessFlags);
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001405 return;
Brian Paule176ae52010-03-05 09:23:43 -07001406 case GL_BUFFER_MAPPED_ARB:
1407 *params = _mesa_bufferobj_mapped(bufObj);
Brian Paule176ae52010-03-05 09:23:43 -07001408 return;
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001409 case GL_BUFFER_ACCESS_FLAGS:
1410 if (ctx->VersionMajor < 3)
1411 goto invalid_pname;
1412 *params = bufObj->AccessFlags;
1413 return;
1414 case GL_BUFFER_MAP_OFFSET:
1415 if (ctx->VersionMajor < 3)
1416 goto invalid_pname;
1417 *params = (GLint) bufObj->Offset;
1418 return;
1419 case GL_BUFFER_MAP_LENGTH:
1420 if (ctx->VersionMajor < 3)
1421 goto invalid_pname;
1422 *params = (GLint) bufObj->Length;
1423 return;
1424 default:
1425 ; /* fall-through */
Brian Paulaac73252003-04-09 02:31:35 +00001426 }
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001427
1428invalid_pname:
1429 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameterivARB(pname=%s)",
1430 _mesa_lookup_enum_by_nr(pname));
Brian Paul6061df02003-03-29 17:01:00 +00001431}
1432
Brian Paulc7b872a2003-09-09 13:44:40 +00001433
Brian Paul1fbc7192010-01-01 17:50:02 -07001434/**
1435 * New in GL 3.2
1436 * This is pretty much a duplicate of GetBufferParameteriv() but the
1437 * GL_BUFFER_SIZE_ARB attribute will be 64-bits on a 64-bit system.
1438 */
1439void GLAPIENTRY
1440_mesa_GetBufferParameteri64v(GLenum target, GLenum pname, GLint64 *params)
1441{
1442 GET_CURRENT_CONTEXT(ctx);
1443 struct gl_buffer_object *bufObj;
1444 ASSERT_OUTSIDE_BEGIN_END(ctx);
1445
1446 bufObj = get_buffer(ctx, target);
1447 if (!bufObj) {
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001448 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(target)" );
Brian Paul1fbc7192010-01-01 17:50:02 -07001449 return;
1450 }
1451 if (!_mesa_is_bufferobj(bufObj)) {
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001452 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferParameteri64v" );
Brian Paul1fbc7192010-01-01 17:50:02 -07001453 return;
1454 }
1455
1456 switch (pname) {
Brian Paule176ae52010-03-05 09:23:43 -07001457 case GL_BUFFER_SIZE_ARB:
1458 *params = bufObj->Size;
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001459 return;
Brian Paule176ae52010-03-05 09:23:43 -07001460 case GL_BUFFER_USAGE_ARB:
1461 *params = bufObj->Usage;
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001462 return;
Brian Paule176ae52010-03-05 09:23:43 -07001463 case GL_BUFFER_ACCESS_ARB:
1464 *params = simplified_access_mode(bufObj->AccessFlags);
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001465 return;
1466 case GL_BUFFER_ACCESS_FLAGS:
1467 if (ctx->VersionMajor < 3)
1468 goto invalid_pname;
1469 *params = bufObj->AccessFlags;
1470 return;
Brian Paule176ae52010-03-05 09:23:43 -07001471 case GL_BUFFER_MAPPED_ARB:
1472 *params = _mesa_bufferobj_mapped(bufObj);
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001473 return;
1474 case GL_BUFFER_MAP_OFFSET:
1475 if (ctx->VersionMajor < 3)
1476 goto invalid_pname;
1477 *params = bufObj->Offset;
1478 return;
1479 case GL_BUFFER_MAP_LENGTH:
1480 if (ctx->VersionMajor < 3)
1481 goto invalid_pname;
1482 *params = bufObj->Length;
1483 return;
Brian Paule176ae52010-03-05 09:23:43 -07001484 default:
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001485 ; /* fall-through */
Brian Paul1fbc7192010-01-01 17:50:02 -07001486 }
Brian Pauld6a9f5b2010-03-20 11:52:12 -06001487
1488invalid_pname:
1489 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferParameteri64v(pname=%s)",
1490 _mesa_lookup_enum_by_nr(pname));
Brian Paul1fbc7192010-01-01 17:50:02 -07001491}
1492
1493
Kendall Bennettc40d1dd2003-10-21 22:22:17 +00001494void GLAPIENTRY
Brian Paul6061df02003-03-29 17:01:00 +00001495_mesa_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
1496{
Brian Paulaac73252003-04-09 02:31:35 +00001497 GET_CURRENT_CONTEXT(ctx);
Ian Romanick0207b472003-09-09 00:10:12 +00001498 struct gl_buffer_object * bufObj;
Brian Paulaac73252003-04-09 02:31:35 +00001499 ASSERT_OUTSIDE_BEGIN_END(ctx);
1500
1501 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
1502 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
1503 return;
1504 }
1505
Brian Paul4277ea42006-08-25 22:06:02 +00001506 bufObj = get_buffer(ctx, target);
1507 if (!bufObj) {
1508 _mesa_error(ctx, GL_INVALID_ENUM, "glGetBufferPointervARB(target)" );
1509 return;
1510 }
Brian Paul60403152009-08-12 13:46:03 -06001511 if (!_mesa_is_bufferobj(bufObj)) {
Brian Paulaa00d122003-09-15 19:55:10 +00001512 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetBufferPointervARB" );
Brian Paulaac73252003-04-09 02:31:35 +00001513 return;
1514 }
Ian Romanick0207b472003-09-09 00:10:12 +00001515
Brian Paul148a2842003-09-17 03:40:11 +00001516 *params = bufObj->Pointer;
Brian Paul6061df02003-03-29 17:01:00 +00001517}
Brian Pauldc0b71f2009-06-02 20:29:57 -06001518
1519
1520void GLAPIENTRY
1521_mesa_CopyBufferSubData(GLenum readTarget, GLenum writeTarget,
1522 GLintptr readOffset, GLintptr writeOffset,
1523 GLsizeiptr size)
1524{
1525 GET_CURRENT_CONTEXT(ctx);
1526 struct gl_buffer_object *src, *dst;
1527 ASSERT_OUTSIDE_BEGIN_END(ctx);
1528
1529 src = get_buffer(ctx, readTarget);
Brian Paul60403152009-08-12 13:46:03 -06001530 if (!src || !_mesa_is_bufferobj(src)) {
Brian Pauldc0b71f2009-06-02 20:29:57 -06001531 _mesa_error(ctx, GL_INVALID_ENUM,
1532 "glCopyBuffserSubData(readTarget = 0x%x)", readTarget);
1533 return;
1534 }
1535
1536 dst = get_buffer(ctx, writeTarget);
Brian Paul60403152009-08-12 13:46:03 -06001537 if (!dst || !_mesa_is_bufferobj(dst)) {
Brian Pauldc0b71f2009-06-02 20:29:57 -06001538 _mesa_error(ctx, GL_INVALID_ENUM,
1539 "glCopyBuffserSubData(writeTarget = 0x%x)", writeTarget);
1540 return;
1541 }
1542
Brian Paul7bf6efe2009-08-07 09:40:37 -06001543 if (_mesa_bufferobj_mapped(src)) {
Brian Pauldc0b71f2009-06-02 20:29:57 -06001544 _mesa_error(ctx, GL_INVALID_OPERATION,
1545 "glCopyBuffserSubData(readBuffer is mapped)");
1546 return;
1547 }
1548
Brian Paul7bf6efe2009-08-07 09:40:37 -06001549 if (_mesa_bufferobj_mapped(dst)) {
Brian Pauldc0b71f2009-06-02 20:29:57 -06001550 _mesa_error(ctx, GL_INVALID_OPERATION,
1551 "glCopyBuffserSubData(writeBuffer is mapped)");
1552 return;
1553 }
1554
1555 if (readOffset < 0) {
1556 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul9eca0e22010-09-02 07:57:16 -06001557 "glCopyBuffserSubData(readOffset = %d)", (int) readOffset);
Brian Pauldc0b71f2009-06-02 20:29:57 -06001558 return;
1559 }
1560
1561 if (writeOffset < 0) {
1562 _mesa_error(ctx, GL_INVALID_VALUE,
Brian Paul9eca0e22010-09-02 07:57:16 -06001563 "glCopyBuffserSubData(writeOffset = %d)", (int) writeOffset);
Brian Pauldc0b71f2009-06-02 20:29:57 -06001564 return;
1565 }
1566
1567 if (readOffset + size > src->Size) {
1568 _mesa_error(ctx, GL_INVALID_VALUE,
1569 "glCopyBuffserSubData(readOffset + size = %d)",
Brian Paul9eca0e22010-09-02 07:57:16 -06001570 (int) (readOffset + size));
Brian Pauldc0b71f2009-06-02 20:29:57 -06001571 return;
1572 }
1573
Brian Paul2813c082009-06-02 21:23:28 -06001574 if (writeOffset + size > dst->Size) {
Brian Pauldc0b71f2009-06-02 20:29:57 -06001575 _mesa_error(ctx, GL_INVALID_VALUE,
1576 "glCopyBuffserSubData(writeOffset + size = %d)",
Brian Paul9eca0e22010-09-02 07:57:16 -06001577 (int) (writeOffset + size));
Brian Pauldc0b71f2009-06-02 20:29:57 -06001578 return;
1579 }
1580
1581 if (src == dst) {
1582 if (readOffset + size <= writeOffset) {
1583 /* OK */
1584 }
1585 else if (writeOffset + size <= readOffset) {
1586 /* OK */
1587 }
1588 else {
1589 /* overlapping src/dst is illegal */
1590 _mesa_error(ctx, GL_INVALID_VALUE,
1591 "glCopyBuffserSubData(overlapping src/dst)");
1592 return;
1593 }
1594 }
1595
1596 ctx->Driver.CopyBufferSubData(ctx, src, dst, readOffset, writeOffset, size);
1597}
1598
Brian Paule75b2832009-06-08 17:02:36 -06001599
1600/**
1601 * See GL_ARB_map_buffer_range spec
1602 */
1603void * GLAPIENTRY
1604_mesa_MapBufferRange(GLenum target, GLintptr offset, GLsizeiptr length,
1605 GLbitfield access)
1606{
1607 GET_CURRENT_CONTEXT(ctx);
1608 struct gl_buffer_object *bufObj;
Brian Paul92033a92009-08-31 08:50:15 -06001609 void *map;
1610
Brian Paule75b2832009-06-08 17:02:36 -06001611 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, NULL);
1612
1613 if (!ctx->Extensions.ARB_map_buffer_range) {
1614 _mesa_error(ctx, GL_INVALID_OPERATION,
1615 "glMapBufferRange(extension not supported)");
1616 return NULL;
1617 }
1618
1619 if (offset < 0) {
1620 _mesa_error(ctx, GL_INVALID_VALUE,
Eric Anholt86af0372010-09-01 14:46:22 -07001621 "glMapBufferRange(offset = %ld)", (long)offset);
Brian Paule75b2832009-06-08 17:02:36 -06001622 return NULL;
1623 }
1624
1625 if (length < 0) {
1626 _mesa_error(ctx, GL_INVALID_VALUE,
Eric Anholt86af0372010-09-01 14:46:22 -07001627 "glMapBufferRange(length = %ld)", (long)length);
Brian Paule75b2832009-06-08 17:02:36 -06001628 return NULL;
1629 }
1630
1631 if ((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) == 0) {
1632 _mesa_error(ctx, GL_INVALID_OPERATION,
1633 "glMapBufferRange(access indicates neither read or write)");
1634 return NULL;
1635 }
1636
1637 if (access & GL_MAP_READ_BIT) {
1638 if ((access & GL_MAP_INVALIDATE_RANGE_BIT) ||
1639 (access & GL_MAP_INVALIDATE_BUFFER_BIT) ||
1640 (access & GL_MAP_UNSYNCHRONIZED_BIT)) {
1641 _mesa_error(ctx, GL_INVALID_OPERATION,
1642 "glMapBufferRange(invalid access flags)");
1643 return NULL;
1644 }
1645 }
1646
1647 if ((access & GL_MAP_FLUSH_EXPLICIT_BIT) &&
1648 ((access & GL_MAP_WRITE_BIT) == 0)) {
1649 _mesa_error(ctx, GL_INVALID_OPERATION,
1650 "glMapBufferRange(invalid access flags)");
1651 return NULL;
1652 }
1653
1654 bufObj = get_buffer(ctx, target);
Brian Paul60403152009-08-12 13:46:03 -06001655 if (!bufObj || !_mesa_is_bufferobj(bufObj)) {
Brian Paule75b2832009-06-08 17:02:36 -06001656 _mesa_error(ctx, GL_INVALID_ENUM,
1657 "glMapBufferRange(target = 0x%x)", target);
1658 return NULL;
1659 }
1660
1661 if (offset + length > bufObj->Size) {
1662 _mesa_error(ctx, GL_INVALID_VALUE,
1663 "glMapBufferRange(offset + length > size)");
1664 return NULL;
1665 }
1666
Brian Paul7bf6efe2009-08-07 09:40:37 -06001667 if (_mesa_bufferobj_mapped(bufObj)) {
Brian Paule75b2832009-06-08 17:02:36 -06001668 _mesa_error(ctx, GL_INVALID_OPERATION,
1669 "glMapBufferRange(buffer already mapped)");
1670 return NULL;
1671 }
1672
1673 ASSERT(ctx->Driver.MapBufferRange);
Brian Paul92033a92009-08-31 08:50:15 -06001674 map = ctx->Driver.MapBufferRange(ctx, target, offset, length,
1675 access, bufObj);
Brian Paul2f6d2a92009-09-03 09:41:41 -06001676 if (!map) {
Brian Paulf338de42009-09-22 13:47:49 -06001677 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMapBufferARB(map failed)");
Brian Paul2f6d2a92009-09-03 09:41:41 -06001678 }
1679 else {
Brian Paul92033a92009-08-31 08:50:15 -06001680 /* The driver callback should have set all these fields.
1681 * This is important because other modules (like VBO) might call
1682 * the driver function directly.
1683 */
1684 ASSERT(bufObj->Pointer == map);
1685 ASSERT(bufObj->Length == length);
1686 ASSERT(bufObj->Offset == offset);
1687 ASSERT(bufObj->AccessFlags == access);
1688 }
Brian Paule75b2832009-06-08 17:02:36 -06001689
Brian Paul92033a92009-08-31 08:50:15 -06001690 return map;
Brian Paule75b2832009-06-08 17:02:36 -06001691}
1692
1693
1694/**
1695 * See GL_ARB_map_buffer_range spec
1696 */
1697void GLAPIENTRY
1698_mesa_FlushMappedBufferRange(GLenum target, GLintptr offset, GLsizeiptr length)
1699{
1700 GET_CURRENT_CONTEXT(ctx);
1701 struct gl_buffer_object *bufObj;
1702 ASSERT_OUTSIDE_BEGIN_END(ctx);
1703
1704 if (!ctx->Extensions.ARB_map_buffer_range) {
1705 _mesa_error(ctx, GL_INVALID_OPERATION,
1706 "glMapBufferRange(extension not supported)");
1707 return;
1708 }
1709
1710 if (offset < 0) {
1711 _mesa_error(ctx, GL_INVALID_VALUE,
Eric Anholt86af0372010-09-01 14:46:22 -07001712 "glMapBufferRange(offset = %ld)", (long)offset);
Brian Paule75b2832009-06-08 17:02:36 -06001713 return;
1714 }
1715
1716 if (length < 0) {
1717 _mesa_error(ctx, GL_INVALID_VALUE,
Eric Anholt86af0372010-09-01 14:46:22 -07001718 "glMapBufferRange(length = %ld)", (long)length);
Brian Paule75b2832009-06-08 17:02:36 -06001719 return;
1720 }
1721
1722 bufObj = get_buffer(ctx, target);
1723 if (!bufObj) {
1724 _mesa_error(ctx, GL_INVALID_ENUM,
1725 "glMapBufferRange(target = 0x%x)", target);
1726 return;
1727 }
1728
Brian Paul60403152009-08-12 13:46:03 -06001729 if (!_mesa_is_bufferobj(bufObj)) {
Brian Paule75b2832009-06-08 17:02:36 -06001730 _mesa_error(ctx, GL_INVALID_OPERATION,
1731 "glMapBufferRange(current buffer is 0)");
1732 return;
1733 }
1734
Brian Paul7bf6efe2009-08-07 09:40:37 -06001735 if (!_mesa_bufferobj_mapped(bufObj)) {
Brian Paule75b2832009-06-08 17:02:36 -06001736 /* buffer is not mapped */
1737 _mesa_error(ctx, GL_INVALID_OPERATION,
1738 "glMapBufferRange(buffer is not mapped)");
1739 return;
1740 }
1741
1742 if ((bufObj->AccessFlags & GL_MAP_FLUSH_EXPLICIT_BIT) == 0) {
1743 _mesa_error(ctx, GL_INVALID_OPERATION,
1744 "glMapBufferRange(GL_MAP_FLUSH_EXPLICIT_BIT not set)");
1745 return;
1746 }
1747
1748 if (offset + length > bufObj->Length) {
1749 _mesa_error(ctx, GL_INVALID_VALUE,
Eric Anholt86af0372010-09-01 14:46:22 -07001750 "glMapBufferRange(offset %ld + length %ld > mapped length %ld)",
1751 (long)offset, (long)length, (long)bufObj->Length);
Brian Paule75b2832009-06-08 17:02:36 -06001752 return;
1753 }
1754
1755 ASSERT(bufObj->AccessFlags & GL_MAP_WRITE_BIT);
1756
1757 if (ctx->Driver.FlushMappedBufferRange)
1758 ctx->Driver.FlushMappedBufferRange(ctx, target, offset, length, bufObj);
1759}
Chris Wilson99864d52009-11-13 12:19:35 +00001760
Brian Paule176ae52010-03-05 09:23:43 -07001761
Chris Wilson99864d52009-11-13 12:19:35 +00001762#if FEATURE_APPLE_object_purgeable
1763static GLenum
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001764_mesa_BufferObjectPurgeable(struct gl_context *ctx, GLuint name, GLenum option)
Chris Wilson99864d52009-11-13 12:19:35 +00001765{
1766 struct gl_buffer_object *bufObj;
1767 GLenum retval;
1768
1769 bufObj = _mesa_lookup_bufferobj(ctx, name);
1770 if (!bufObj) {
1771 _mesa_error(ctx, GL_INVALID_VALUE,
1772 "glObjectPurgeable(name = 0x%x)", name);
1773 return 0;
1774 }
1775 if (!_mesa_is_bufferobj(bufObj)) {
1776 _mesa_error(ctx, GL_INVALID_OPERATION, "glObjectPurgeable(buffer 0)" );
1777 return 0;
1778 }
1779
1780 if (bufObj->Purgeable) {
1781 _mesa_error(ctx, GL_INVALID_OPERATION,
1782 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1783 return GL_VOLATILE_APPLE;
1784 }
1785
1786 bufObj->Purgeable = GL_TRUE;
1787
1788 retval = GL_VOLATILE_APPLE;
1789 if (ctx->Driver.BufferObjectPurgeable)
1790 retval = ctx->Driver.BufferObjectPurgeable(ctx, bufObj, option);
1791
1792 return retval;
1793}
1794
Brian Paule176ae52010-03-05 09:23:43 -07001795
Chris Wilson99864d52009-11-13 12:19:35 +00001796static GLenum
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001797_mesa_RenderObjectPurgeable(struct gl_context *ctx, GLuint name, GLenum option)
Chris Wilson99864d52009-11-13 12:19:35 +00001798{
1799 struct gl_renderbuffer *bufObj;
1800 GLenum retval;
1801
1802 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1803 if (!bufObj) {
1804 _mesa_error(ctx, GL_INVALID_VALUE,
1805 "glObjectUnpurgeable(name = 0x%x)", name);
1806 return 0;
1807 }
1808
1809 if (bufObj->Purgeable) {
1810 _mesa_error(ctx, GL_INVALID_OPERATION,
1811 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1812 return GL_VOLATILE_APPLE;
1813 }
1814
1815 bufObj->Purgeable = GL_TRUE;
1816
1817 retval = GL_VOLATILE_APPLE;
1818 if (ctx->Driver.RenderObjectPurgeable)
1819 retval = ctx->Driver.RenderObjectPurgeable(ctx, bufObj, option);
1820
1821 return retval;
1822}
1823
Brian Paule176ae52010-03-05 09:23:43 -07001824
Chris Wilson99864d52009-11-13 12:19:35 +00001825static GLenum
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001826_mesa_TextureObjectPurgeable(struct gl_context *ctx, GLuint name, GLenum option)
Chris Wilson99864d52009-11-13 12:19:35 +00001827{
1828 struct gl_texture_object *bufObj;
1829 GLenum retval;
1830
1831 bufObj = _mesa_lookup_texture(ctx, name);
1832 if (!bufObj) {
1833 _mesa_error(ctx, GL_INVALID_VALUE,
1834 "glObjectPurgeable(name = 0x%x)", name);
1835 return 0;
1836 }
1837
1838 if (bufObj->Purgeable) {
1839 _mesa_error(ctx, GL_INVALID_OPERATION,
1840 "glObjectPurgeable(name = 0x%x) is already purgeable", name);
1841 return GL_VOLATILE_APPLE;
1842 }
1843
1844 bufObj->Purgeable = GL_TRUE;
1845
1846 retval = GL_VOLATILE_APPLE;
1847 if (ctx->Driver.TextureObjectPurgeable)
1848 retval = ctx->Driver.TextureObjectPurgeable(ctx, bufObj, option);
1849
1850 return retval;
1851}
1852
Brian Paule176ae52010-03-05 09:23:43 -07001853
Chris Wilson99864d52009-11-13 12:19:35 +00001854GLenum GLAPIENTRY
1855_mesa_ObjectPurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1856{
Chris Wilson24f90112010-03-05 23:10:45 +00001857 GLenum retval;
1858
Chris Wilson99864d52009-11-13 12:19:35 +00001859 GET_CURRENT_CONTEXT(ctx);
1860 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1861
1862 if (name == 0) {
1863 _mesa_error(ctx, GL_INVALID_VALUE,
1864 "glObjectPurgeable(name = 0x%x)", name);
1865 return 0;
1866 }
1867
1868 switch (option) {
1869 case GL_VOLATILE_APPLE:
1870 case GL_RELEASED_APPLE:
Brian Paule176ae52010-03-05 09:23:43 -07001871 /* legal */
Chris Wilson99864d52009-11-13 12:19:35 +00001872 break;
Chris Wilson99864d52009-11-13 12:19:35 +00001873 default:
1874 _mesa_error(ctx, GL_INVALID_ENUM,
Brian Paule176ae52010-03-05 09:23:43 -07001875 "glObjectPurgeable(name = 0x%x) invalid option: %d",
1876 name, option);
Chris Wilson99864d52009-11-13 12:19:35 +00001877 return 0;
1878 }
1879
1880 switch (objectType) {
1881 case GL_TEXTURE:
Chris Wilson24f90112010-03-05 23:10:45 +00001882 retval = _mesa_TextureObjectPurgeable (ctx, name, option);
1883 break;
Chris Wilson99864d52009-11-13 12:19:35 +00001884 case GL_RENDERBUFFER_EXT:
Chris Wilson24f90112010-03-05 23:10:45 +00001885 retval = _mesa_RenderObjectPurgeable (ctx, name, option);
1886 break;
Chris Wilson99864d52009-11-13 12:19:35 +00001887 case GL_BUFFER_OBJECT_APPLE:
Chris Wilson24f90112010-03-05 23:10:45 +00001888 retval = _mesa_BufferObjectPurgeable (ctx, name, option);
1889 break;
Chris Wilson99864d52009-11-13 12:19:35 +00001890 default:
1891 _mesa_error(ctx, GL_INVALID_ENUM,
Brian Paule176ae52010-03-05 09:23:43 -07001892 "glObjectPurgeable(name = 0x%x) invalid type: %d",
1893 name, objectType);
Chris Wilson99864d52009-11-13 12:19:35 +00001894 return 0;
1895 }
Chris Wilson24f90112010-03-05 23:10:45 +00001896
1897 /* In strict conformance to the spec, we must only return VOLATILE when
1898 * when passed the VOLATILE option. Madness.
1899 *
1900 * XXX First fix the spec, then fix me.
1901 */
1902 return option == GL_VOLATILE_APPLE ? GL_VOLATILE_APPLE : retval;
Chris Wilson99864d52009-11-13 12:19:35 +00001903}
1904
Brian Paule176ae52010-03-05 09:23:43 -07001905
Chris Wilson99864d52009-11-13 12:19:35 +00001906static GLenum
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001907_mesa_BufferObjectUnpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
Chris Wilson99864d52009-11-13 12:19:35 +00001908{
1909 struct gl_buffer_object *bufObj;
1910 GLenum retval;
1911
1912 bufObj = _mesa_lookup_bufferobj(ctx, name);
1913 if (!bufObj) {
1914 _mesa_error(ctx, GL_INVALID_VALUE,
1915 "glObjectUnpurgeable(name = 0x%x)", name);
1916 return 0;
1917 }
1918
1919 if (! bufObj->Purgeable) {
1920 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paule176ae52010-03-05 09:23:43 -07001921 "glObjectUnpurgeable(name = 0x%x) object is "
1922 " already \"unpurged\"", name);
Chris Wilson99864d52009-11-13 12:19:35 +00001923 return 0;
1924 }
1925
1926 bufObj->Purgeable = GL_FALSE;
1927
Brian Paul2f4ce252010-07-14 15:11:32 -06001928 retval = option;
Chris Wilson99864d52009-11-13 12:19:35 +00001929 if (ctx->Driver.BufferObjectUnpurgeable)
1930 retval = ctx->Driver.BufferObjectUnpurgeable(ctx, bufObj, option);
1931
1932 return retval;
1933}
1934
Brian Paule176ae52010-03-05 09:23:43 -07001935
Chris Wilson99864d52009-11-13 12:19:35 +00001936static GLenum
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001937_mesa_RenderObjectUnpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
Chris Wilson99864d52009-11-13 12:19:35 +00001938{
1939 struct gl_renderbuffer *bufObj;
1940 GLenum retval;
1941
1942 bufObj = _mesa_lookup_renderbuffer(ctx, name);
1943 if (!bufObj) {
1944 _mesa_error(ctx, GL_INVALID_VALUE,
1945 "glObjectUnpurgeable(name = 0x%x)", name);
1946 return 0;
1947 }
1948
1949 if (! bufObj->Purgeable) {
1950 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paule176ae52010-03-05 09:23:43 -07001951 "glObjectUnpurgeable(name = 0x%x) object is "
1952 " already \"unpurged\"", name);
Chris Wilson99864d52009-11-13 12:19:35 +00001953 return 0;
1954 }
1955
1956 bufObj->Purgeable = GL_FALSE;
1957
Brian Paul2f4ce252010-07-14 15:11:32 -06001958 retval = option;
Chris Wilson99864d52009-11-13 12:19:35 +00001959 if (ctx->Driver.RenderObjectUnpurgeable)
1960 retval = ctx->Driver.RenderObjectUnpurgeable(ctx, bufObj, option);
1961
Brian Paul41bcd8c2010-07-16 07:36:19 -06001962 return retval;
Chris Wilson99864d52009-11-13 12:19:35 +00001963}
1964
Brian Paule176ae52010-03-05 09:23:43 -07001965
Chris Wilson99864d52009-11-13 12:19:35 +00001966static GLenum
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001967_mesa_TextureObjectUnpurgeable(struct gl_context *ctx, GLuint name, GLenum option)
Chris Wilson99864d52009-11-13 12:19:35 +00001968{
1969 struct gl_texture_object *bufObj;
1970 GLenum retval;
1971
1972 bufObj = _mesa_lookup_texture(ctx, name);
1973 if (!bufObj) {
1974 _mesa_error(ctx, GL_INVALID_VALUE,
1975 "glObjectUnpurgeable(name = 0x%x)", name);
1976 return 0;
1977 }
1978
1979 if (! bufObj->Purgeable) {
1980 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paule176ae52010-03-05 09:23:43 -07001981 "glObjectUnpurgeable(name = 0x%x) object is"
1982 " already \"unpurged\"", name);
Chris Wilson99864d52009-11-13 12:19:35 +00001983 return 0;
1984 }
1985
1986 bufObj->Purgeable = GL_FALSE;
1987
Brian Paul2f4ce252010-07-14 15:11:32 -06001988 retval = option;
Chris Wilson99864d52009-11-13 12:19:35 +00001989 if (ctx->Driver.TextureObjectUnpurgeable)
1990 retval = ctx->Driver.TextureObjectUnpurgeable(ctx, bufObj, option);
1991
1992 return retval;
1993}
1994
Brian Paule176ae52010-03-05 09:23:43 -07001995
Chris Wilson99864d52009-11-13 12:19:35 +00001996GLenum GLAPIENTRY
1997_mesa_ObjectUnpurgeableAPPLE(GLenum objectType, GLuint name, GLenum option)
1998{
1999 GET_CURRENT_CONTEXT(ctx);
2000 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2001
2002 if (name == 0) {
2003 _mesa_error(ctx, GL_INVALID_VALUE,
2004 "glObjectUnpurgeable(name = 0x%x)", name);
2005 return 0;
2006 }
2007
2008 switch (option) {
2009 case GL_RETAINED_APPLE:
2010 case GL_UNDEFINED_APPLE:
Brian Paule176ae52010-03-05 09:23:43 -07002011 /* legal */
Chris Wilson99864d52009-11-13 12:19:35 +00002012 break;
Chris Wilson99864d52009-11-13 12:19:35 +00002013 default:
2014 _mesa_error(ctx, GL_INVALID_ENUM,
Brian Paule176ae52010-03-05 09:23:43 -07002015 "glObjectUnpurgeable(name = 0x%x) invalid option: %d",
2016 name, option);
Chris Wilson99864d52009-11-13 12:19:35 +00002017 return 0;
2018 }
2019
2020 switch (objectType) {
2021 case GL_BUFFER_OBJECT_APPLE:
2022 return _mesa_BufferObjectUnpurgeable(ctx, name, option);
Chris Wilson99864d52009-11-13 12:19:35 +00002023 case GL_TEXTURE:
2024 return _mesa_TextureObjectUnpurgeable(ctx, name, option);
Chris Wilson99864d52009-11-13 12:19:35 +00002025 case GL_RENDERBUFFER_EXT:
2026 return _mesa_RenderObjectUnpurgeable(ctx, name, option);
Chris Wilson99864d52009-11-13 12:19:35 +00002027 default:
2028 _mesa_error(ctx, GL_INVALID_ENUM,
Brian Paule176ae52010-03-05 09:23:43 -07002029 "glObjectUnpurgeable(name = 0x%x) invalid type: %d",
2030 name, objectType);
Chris Wilson99864d52009-11-13 12:19:35 +00002031 return 0;
2032 }
2033}
2034
Brian Paule176ae52010-03-05 09:23:43 -07002035
Chris Wilson99864d52009-11-13 12:19:35 +00002036static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002037_mesa_GetBufferObjectParameterivAPPLE(struct gl_context *ctx, GLuint name,
Brian Paule176ae52010-03-05 09:23:43 -07002038 GLenum pname, GLint* params)
Chris Wilson99864d52009-11-13 12:19:35 +00002039{
2040 struct gl_buffer_object *bufObj;
2041
2042 bufObj = _mesa_lookup_bufferobj(ctx, name);
2043 if (!bufObj) {
2044 _mesa_error(ctx, GL_INVALID_VALUE,
2045 "glGetObjectParameteriv(name = 0x%x) invalid object", name);
2046 return;
2047 }
2048
2049 switch (pname) {
2050 case GL_PURGEABLE_APPLE:
2051 *params = bufObj->Purgeable;
2052 break;
Chris Wilson99864d52009-11-13 12:19:35 +00002053 default:
2054 _mesa_error(ctx, GL_INVALID_ENUM,
Brian Paule176ae52010-03-05 09:23:43 -07002055 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2056 name, pname);
Chris Wilson99864d52009-11-13 12:19:35 +00002057 break;
2058 }
2059}
2060
Brian Paule176ae52010-03-05 09:23:43 -07002061
Chris Wilson99864d52009-11-13 12:19:35 +00002062static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002063_mesa_GetRenderObjectParameterivAPPLE(struct gl_context *ctx, GLuint name,
Brian Paule176ae52010-03-05 09:23:43 -07002064 GLenum pname, GLint* params)
Chris Wilson99864d52009-11-13 12:19:35 +00002065{
2066 struct gl_renderbuffer *bufObj;
2067
2068 bufObj = _mesa_lookup_renderbuffer(ctx, name);
2069 if (!bufObj) {
2070 _mesa_error(ctx, GL_INVALID_VALUE,
2071 "glObjectUnpurgeable(name = 0x%x)", name);
2072 return;
2073 }
2074
2075 switch (pname) {
2076 case GL_PURGEABLE_APPLE:
2077 *params = bufObj->Purgeable;
2078 break;
Chris Wilson99864d52009-11-13 12:19:35 +00002079 default:
2080 _mesa_error(ctx, GL_INVALID_ENUM,
Brian Paule176ae52010-03-05 09:23:43 -07002081 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2082 name, pname);
Chris Wilson99864d52009-11-13 12:19:35 +00002083 break;
2084 }
2085}
2086
Brian Paule176ae52010-03-05 09:23:43 -07002087
Chris Wilson99864d52009-11-13 12:19:35 +00002088static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04002089_mesa_GetTextureObjectParameterivAPPLE(struct gl_context *ctx, GLuint name,
Brian Paule176ae52010-03-05 09:23:43 -07002090 GLenum pname, GLint* params)
Chris Wilson99864d52009-11-13 12:19:35 +00002091{
2092 struct gl_texture_object *bufObj;
2093
2094 bufObj = _mesa_lookup_texture(ctx, name);
2095 if (!bufObj) {
2096 _mesa_error(ctx, GL_INVALID_VALUE,
2097 "glObjectUnpurgeable(name = 0x%x)", name);
2098 return;
2099 }
2100
2101 switch (pname) {
2102 case GL_PURGEABLE_APPLE:
2103 *params = bufObj->Purgeable;
2104 break;
Chris Wilson99864d52009-11-13 12:19:35 +00002105 default:
2106 _mesa_error(ctx, GL_INVALID_ENUM,
Brian Paule176ae52010-03-05 09:23:43 -07002107 "glGetObjectParameteriv(name = 0x%x) invalid enum: %d",
2108 name, pname);
Chris Wilson99864d52009-11-13 12:19:35 +00002109 break;
2110 }
2111}
2112
Brian Paule176ae52010-03-05 09:23:43 -07002113
Chris Wilson99864d52009-11-13 12:19:35 +00002114void GLAPIENTRY
Brian Paule176ae52010-03-05 09:23:43 -07002115_mesa_GetObjectParameterivAPPLE(GLenum objectType, GLuint name, GLenum pname,
2116 GLint* params)
Chris Wilson99864d52009-11-13 12:19:35 +00002117{
2118 GET_CURRENT_CONTEXT(ctx);
2119
2120 if (name == 0) {
2121 _mesa_error(ctx, GL_INVALID_VALUE,
2122 "glGetObjectParameteriv(name = 0x%x)", name);
2123 return;
2124 }
2125
2126 switch (objectType) {
2127 case GL_TEXTURE:
2128 _mesa_GetTextureObjectParameterivAPPLE (ctx, name, pname, params);
2129 break;
Chris Wilson99864d52009-11-13 12:19:35 +00002130 case GL_BUFFER_OBJECT_APPLE:
2131 _mesa_GetBufferObjectParameterivAPPLE (ctx, name, pname, params);
2132 break;
Chris Wilson99864d52009-11-13 12:19:35 +00002133 case GL_RENDERBUFFER_EXT:
2134 _mesa_GetRenderObjectParameterivAPPLE (ctx, name, pname, params);
2135 break;
Chris Wilson99864d52009-11-13 12:19:35 +00002136 default:
2137 _mesa_error(ctx, GL_INVALID_ENUM,
Brian Paule176ae52010-03-05 09:23:43 -07002138 "glGetObjectParameteriv(name = 0x%x) invalid type: %d",
2139 name, objectType);
Chris Wilson99864d52009-11-13 12:19:35 +00002140 }
2141}
Brian Paule176ae52010-03-05 09:23:43 -07002142
2143#endif /* FEATURE_APPLE_object_purgeable */