2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * (C) Copyright IBM Corporation 2006
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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 OR IBM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * Functions for the GL_APPLE_vertex_array_object extension.
33 * The code in this file borrows a lot from bufferobj.c. There's a certain
34 * amount of cruft left over from that origin that may be unnecessary.
36 * \author Ian Romanick <idr@us.ibm.com>
45 #if FEATURE_ARB_vertex_buffer_object
46 #include "bufferobj.h"
49 #include "glapi/dispatch.h"
53 * Look up the array object for the given ID.
56 * Either a pointer to the array object with the specified ID or \c NULL for
57 * a non-existent ID. The spec defines ID 0 as being technically
61 static INLINE
struct gl_array_object
*
62 lookup_arrayobj(GLcontext
*ctx
, GLuint id
)
66 : (struct gl_array_object
*) _mesa_HashLookup(ctx
->Shared
->ArrayObjects
,
72 * Allocate and initialize a new vertex array object.
74 * This function is intended to be called via
75 * \c dd_function_table::NewArrayObject.
77 struct gl_array_object
*
78 _mesa_new_array_object( GLcontext
*ctx
, GLuint name
)
80 struct gl_array_object
*obj
= CALLOC_STRUCT(gl_array_object
);
82 _mesa_initialize_array_object(ctx
, obj
, name
);
88 * Delete an array object.
90 * This function is intended to be called via
91 * \c dd_function_table::DeleteArrayObject.
94 _mesa_delete_array_object( GLcontext
*ctx
, struct gl_array_object
*obj
)
102 _mesa_initialize_array_object( GLcontext
*ctx
,
103 struct gl_array_object
*obj
,
111 obj
->Vertex
.Size
= 4;
112 obj
->Vertex
.Type
= GL_FLOAT
;
113 obj
->Vertex
.Stride
= 0;
114 obj
->Vertex
.StrideB
= 0;
115 obj
->Vertex
.Ptr
= NULL
;
116 obj
->Vertex
.Enabled
= GL_FALSE
;
117 obj
->Normal
.Type
= GL_FLOAT
;
118 obj
->Normal
.Stride
= 0;
119 obj
->Normal
.StrideB
= 0;
120 obj
->Normal
.Ptr
= NULL
;
121 obj
->Normal
.Enabled
= GL_FALSE
;
123 obj
->Color
.Type
= GL_FLOAT
;
124 obj
->Color
.Stride
= 0;
125 obj
->Color
.StrideB
= 0;
126 obj
->Color
.Ptr
= NULL
;
127 obj
->Color
.Enabled
= GL_FALSE
;
128 obj
->SecondaryColor
.Size
= 4;
129 obj
->SecondaryColor
.Type
= GL_FLOAT
;
130 obj
->SecondaryColor
.Stride
= 0;
131 obj
->SecondaryColor
.StrideB
= 0;
132 obj
->SecondaryColor
.Ptr
= NULL
;
133 obj
->SecondaryColor
.Enabled
= GL_FALSE
;
134 obj
->FogCoord
.Size
= 1;
135 obj
->FogCoord
.Type
= GL_FLOAT
;
136 obj
->FogCoord
.Stride
= 0;
137 obj
->FogCoord
.StrideB
= 0;
138 obj
->FogCoord
.Ptr
= NULL
;
139 obj
->FogCoord
.Enabled
= GL_FALSE
;
140 obj
->Index
.Type
= GL_FLOAT
;
141 obj
->Index
.Stride
= 0;
142 obj
->Index
.StrideB
= 0;
143 obj
->Index
.Ptr
= NULL
;
144 obj
->Index
.Enabled
= GL_FALSE
;
145 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
146 obj
->TexCoord
[i
].Size
= 4;
147 obj
->TexCoord
[i
].Type
= GL_FLOAT
;
148 obj
->TexCoord
[i
].Stride
= 0;
149 obj
->TexCoord
[i
].StrideB
= 0;
150 obj
->TexCoord
[i
].Ptr
= NULL
;
151 obj
->TexCoord
[i
].Enabled
= GL_FALSE
;
153 obj
->EdgeFlag
.Stride
= 0;
154 obj
->EdgeFlag
.StrideB
= 0;
155 obj
->EdgeFlag
.Ptr
= NULL
;
156 obj
->EdgeFlag
.Enabled
= GL_FALSE
;
157 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
158 obj
->VertexAttrib
[i
].Size
= 4;
159 obj
->VertexAttrib
[i
].Type
= GL_FLOAT
;
160 obj
->VertexAttrib
[i
].Stride
= 0;
161 obj
->VertexAttrib
[i
].StrideB
= 0;
162 obj
->VertexAttrib
[i
].Ptr
= NULL
;
163 obj
->VertexAttrib
[i
].Enabled
= GL_FALSE
;
164 obj
->VertexAttrib
[i
].Normalized
= GL_FALSE
;
167 #if FEATURE_point_size_array
168 obj
->PointSize
.Type
= GL_FLOAT
;
169 obj
->PointSize
.Stride
= 0;
170 obj
->PointSize
.StrideB
= 0;
171 obj
->PointSize
.Ptr
= NULL
;
172 obj
->PointSize
.Enabled
= GL_FALSE
;
173 obj
->PointSize
.BufferObj
= ctx
->Array
.NullBufferObj
;
176 #if FEATURE_ARB_vertex_buffer_object
177 /* Vertex array buffers */
178 obj
->Vertex
.BufferObj
= ctx
->Array
.NullBufferObj
;
179 obj
->Normal
.BufferObj
= ctx
->Array
.NullBufferObj
;
180 obj
->Color
.BufferObj
= ctx
->Array
.NullBufferObj
;
181 obj
->SecondaryColor
.BufferObj
= ctx
->Array
.NullBufferObj
;
182 obj
->FogCoord
.BufferObj
= ctx
->Array
.NullBufferObj
;
183 obj
->Index
.BufferObj
= ctx
->Array
.NullBufferObj
;
184 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
185 obj
->TexCoord
[i
].BufferObj
= ctx
->Array
.NullBufferObj
;
187 obj
->EdgeFlag
.BufferObj
= ctx
->Array
.NullBufferObj
;
188 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
189 obj
->VertexAttrib
[i
].BufferObj
= ctx
->Array
.NullBufferObj
;
196 * Add the given array object to the array object pool.
199 _mesa_save_array_object( GLcontext
*ctx
, struct gl_array_object
*obj
)
202 /* insert into hash table */
203 _mesa_HashInsert(ctx
->Shared
->ArrayObjects
, obj
->Name
, obj
);
209 * Remove the given array object from the array object pool.
210 * Do not deallocate the array object though.
213 _mesa_remove_array_object( GLcontext
*ctx
, struct gl_array_object
*obj
)
216 /* remove from hash table */
217 _mesa_HashRemove(ctx
->Shared
->ArrayObjects
, obj
->Name
);
223 unbind_buffer_object( GLcontext
*ctx
, struct gl_buffer_object
*bufObj
)
225 if (bufObj
!= ctx
->Array
.NullBufferObj
) {
226 _mesa_reference_buffer_object(ctx
, &bufObj
, NULL
);
231 /**********************************************************************/
233 /**********************************************************************/
239 * The binding could be done more efficiently by comparing the non-NULL
240 * pointers in the old and new objects. The only arrays that are "dirty" are
241 * the ones that are non-NULL in either object.
244 _mesa_BindVertexArrayAPPLE( GLuint id
)
246 GET_CURRENT_CONTEXT(ctx
);
247 struct gl_array_object
* const oldObj
= ctx
->Array
.ArrayObj
;
248 struct gl_array_object
*newObj
= NULL
;
249 ASSERT_OUTSIDE_BEGIN_END(ctx
);
251 ASSERT(oldObj
!= NULL
);
253 if ( oldObj
->Name
== id
)
254 return; /* rebinding the same array object- no change */
257 * Get pointer to new array object (newBufObj)
260 /* The spec says there is no array object named 0, but we use
261 * one internally because it simplifies things.
263 newObj
= ctx
->Array
.DefaultArrayObj
;
266 /* non-default array object */
267 newObj
= lookup_arrayobj(ctx
, id
);
269 /* If this is a new array object id, allocate an array object now.
272 newObj
= (*ctx
->Driver
.NewArrayObject
)(ctx
, id
);
274 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBindVertexArrayAPPLE");
277 _mesa_save_array_object(ctx
, newObj
);
282 ctx
->NewState
|= _NEW_ARRAY
;
283 ctx
->Array
.NewState
|= _NEW_ARRAY_ALL
;
284 ctx
->Array
.ArrayObj
= newObj
;
287 /* Pass BindVertexArray call to device driver */
288 if (ctx
->Driver
.BindArrayObject
&& newObj
)
289 (*ctx
->Driver
.BindArrayObject
)( ctx
, newObj
);
294 * Delete a set of array objects.
296 * \param n Number of array objects to delete.
297 * \param ids Array of \c n array object IDs.
300 _mesa_DeleteVertexArraysAPPLE(GLsizei n
, const GLuint
*ids
)
302 GET_CURRENT_CONTEXT(ctx
);
304 ASSERT_OUTSIDE_BEGIN_END(ctx
);
307 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteVertexArrayAPPLE(n)");
311 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
313 for (i
= 0; i
< n
; i
++) {
314 struct gl_array_object
*obj
= lookup_arrayobj(ctx
, ids
[i
]);
317 ASSERT( obj
->Name
== ids
[i
] );
320 /* If the array object is currently bound, the spec says "the binding
321 * for that object reverts to zero and the default vertex array
324 if ( obj
== ctx
->Array
.ArrayObj
) {
325 CALL_BindVertexArrayAPPLE( ctx
->Exec
, (0) );
328 #if FEATURE_ARB_vertex_buffer_object
329 /* Unbind any buffer objects that might be bound to arrays in
332 unbind_buffer_object( ctx
, obj
->Vertex
.BufferObj
);
333 unbind_buffer_object( ctx
, obj
->Normal
.BufferObj
);
334 unbind_buffer_object( ctx
, obj
->Color
.BufferObj
);
335 unbind_buffer_object( ctx
, obj
->SecondaryColor
.BufferObj
);
336 unbind_buffer_object( ctx
, obj
->FogCoord
.BufferObj
);
337 unbind_buffer_object( ctx
, obj
->Index
.BufferObj
);
338 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
339 unbind_buffer_object( ctx
, obj
->TexCoord
[i
].BufferObj
);
341 unbind_buffer_object( ctx
, obj
->EdgeFlag
.BufferObj
);
342 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
343 unbind_buffer_object( ctx
, obj
->VertexAttrib
[i
].BufferObj
);
347 /* The ID is immediately freed for re-use */
348 _mesa_remove_array_object(ctx
, obj
);
349 ctx
->Driver
.DeleteArrayObject(ctx
, obj
);
353 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
358 * Generate a set of unique array object IDs and store them in \c arrays.
360 * \param n Number of IDs to generate.
361 * \param arrays Array of \c n locations to store the IDs.
364 _mesa_GenVertexArraysAPPLE(GLsizei n
, GLuint
*arrays
)
366 GET_CURRENT_CONTEXT(ctx
);
369 ASSERT_OUTSIDE_BEGIN_END(ctx
);
372 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenVertexArraysAPPLE");
381 * This must be atomic (generation and allocation of array object IDs)
383 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
385 first
= _mesa_HashFindFreeKeyBlock(ctx
->Shared
->ArrayObjects
, n
);
387 /* Allocate new, empty array objects and return identifiers */
388 for (i
= 0; i
< n
; i
++) {
389 struct gl_array_object
*obj
;
390 GLuint name
= first
+ i
;
392 obj
= (*ctx
->Driver
.NewArrayObject
)( ctx
, name
);
394 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
395 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenVertexArraysAPPLE");
398 _mesa_save_array_object(ctx
, obj
);
399 arrays
[i
] = first
+ i
;
402 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
407 * Determine if ID is the name of an array object.
409 * \param id ID of the potential array object.
410 * \return \c GL_TRUE if \c id is the name of a array object,
411 * \c GL_FALSE otherwise.
414 _mesa_IsVertexArrayAPPLE( GLuint id
)
416 struct gl_array_object
* obj
;
417 GET_CURRENT_CONTEXT(ctx
);
418 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
423 _glthread_LOCK_MUTEX(ctx
->Shared
->Mutex
);
424 obj
= lookup_arrayobj(ctx
, id
);
425 _glthread_UNLOCK_MUTEX(ctx
->Shared
->Mutex
);
427 return (obj
!= NULL
) ? GL_TRUE
: GL_FALSE
;