mesa: move the NullBufferObj from GLcontext to gl_shared_state
[mesa.git] / src / mesa / main / arrayobj.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.2
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * (C) Copyright IBM Corporation 2006
7 *
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 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
24 * SOFTWARE.
25 */
26
27
28 /**
29 * \file arrayobj.c
30 * Functions for the GL_APPLE_vertex_array_object extension.
31 *
32 * \todo
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.
35 *
36 * \author Ian Romanick <idr@us.ibm.com>
37 * \author Brian Paul
38 */
39
40
41 #include "glheader.h"
42 #include "hash.h"
43 #include "imports.h"
44 #include "context.h"
45 #if FEATURE_ARB_vertex_buffer_object
46 #include "bufferobj.h"
47 #endif
48 #include "arrayobj.h"
49 #include "glapi/dispatch.h"
50
51
52 /**
53 * Look up the array object for the given ID.
54 *
55 * \returns
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
58 * non-existent.
59 */
60
61 static INLINE struct gl_array_object *
62 lookup_arrayobj(GLcontext *ctx, GLuint id)
63 {
64 return (id == 0)
65 ? NULL
66 : (struct gl_array_object *) _mesa_HashLookup(ctx->Shared->ArrayObjects,
67 id);
68 }
69
70
71 /**
72 * Allocate and initialize a new vertex array object.
73 *
74 * This function is intended to be called via
75 * \c dd_function_table::NewArrayObject.
76 */
77 struct gl_array_object *
78 _mesa_new_array_object( GLcontext *ctx, GLuint name )
79 {
80 struct gl_array_object *obj = CALLOC_STRUCT(gl_array_object);
81 if (obj)
82 _mesa_initialize_array_object(ctx, obj, name);
83 return obj;
84 }
85
86
87 /**
88 * Delete an array object.
89 *
90 * This function is intended to be called via
91 * \c dd_function_table::DeleteArrayObject.
92 */
93 void
94 _mesa_delete_array_object( GLcontext *ctx, struct gl_array_object *obj )
95 {
96 (void) ctx;
97 _mesa_free(obj);
98 }
99
100
101 static void
102 init_array(GLcontext *ctx,
103 struct gl_client_array *array, GLint size, GLint type)
104 {
105 array->Size = size;
106 array->Type = type;
107 array->Format = GL_RGBA; /* only significant for GL_EXT_vertex_array_bgra */
108 array->Stride = 0;
109 array->StrideB = 0;
110 array->Ptr = NULL;
111 array->Enabled = GL_FALSE;
112 array->Normalized = GL_FALSE;
113 #if FEATURE_ARB_vertex_buffer_object
114 /* Vertex array buffers */
115 _mesa_reference_buffer_object(ctx, &array->BufferObj,
116 ctx->Shared->NullBufferObj);
117 #endif
118 }
119
120
121 /**
122 * Initialize a gl_array_object's arrays.
123 */
124 void
125 _mesa_initialize_array_object( GLcontext *ctx,
126 struct gl_array_object *obj,
127 GLuint name )
128 {
129 GLuint i;
130
131 obj->Name = name;
132
133 /* Init the individual arrays */
134 init_array(ctx, &obj->Vertex, 4, GL_FLOAT);
135 init_array(ctx, &obj->Normal, 3, GL_FLOAT);
136 init_array(ctx, &obj->Color, 4, GL_FLOAT);
137 init_array(ctx, &obj->SecondaryColor, 4, GL_FLOAT);
138 init_array(ctx, &obj->FogCoord, 1, GL_FLOAT);
139 init_array(ctx, &obj->Index, 1, GL_FLOAT);
140 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
141 init_array(ctx, &obj->TexCoord[i], 4, GL_FLOAT);
142 }
143 init_array(ctx, &obj->EdgeFlag, 1, GL_BOOL);
144 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
145 init_array(ctx, &obj->VertexAttrib[i], 4, GL_FLOAT);
146 }
147
148 #if FEATURE_point_size_array
149 init_array(ctx, &obj->PointSize, 1, GL_FLOAT);
150 #endif
151 }
152
153
154 /**
155 * Add the given array object to the array object pool.
156 */
157 void
158 _mesa_save_array_object( GLcontext *ctx, struct gl_array_object *obj )
159 {
160 if (obj->Name > 0) {
161 /* insert into hash table */
162 _mesa_HashInsert(ctx->Shared->ArrayObjects, obj->Name, obj);
163 }
164 }
165
166
167 /**
168 * Remove the given array object from the array object pool.
169 * Do not deallocate the array object though.
170 */
171 void
172 _mesa_remove_array_object( GLcontext *ctx, struct gl_array_object *obj )
173 {
174 if (obj->Name > 0) {
175 /* remove from hash table */
176 _mesa_HashRemove(ctx->Shared->ArrayObjects, obj->Name);
177 }
178 }
179
180
181 static void
182 unbind_buffer_object( GLcontext *ctx, struct gl_buffer_object *bufObj )
183 {
184 if (bufObj != ctx->Shared->NullBufferObj) {
185 _mesa_reference_buffer_object(ctx, &bufObj, NULL);
186 }
187 }
188
189
190 /**********************************************************************/
191 /* API Functions */
192 /**********************************************************************/
193
194 /**
195 * Bind a new array.
196 *
197 * \todo
198 * The binding could be done more efficiently by comparing the non-NULL
199 * pointers in the old and new objects. The only arrays that are "dirty" are
200 * the ones that are non-NULL in either object.
201 */
202 void GLAPIENTRY
203 _mesa_BindVertexArrayAPPLE( GLuint id )
204 {
205 GET_CURRENT_CONTEXT(ctx);
206 struct gl_array_object * const oldObj = ctx->Array.ArrayObj;
207 struct gl_array_object *newObj = NULL;
208 ASSERT_OUTSIDE_BEGIN_END(ctx);
209
210 ASSERT(oldObj != NULL);
211
212 if ( oldObj->Name == id )
213 return; /* rebinding the same array object- no change */
214
215 /*
216 * Get pointer to new array object (newBufObj)
217 */
218 if (id == 0) {
219 /* The spec says there is no array object named 0, but we use
220 * one internally because it simplifies things.
221 */
222 newObj = ctx->Array.DefaultArrayObj;
223 }
224 else {
225 /* non-default array object */
226 newObj = lookup_arrayobj(ctx, id);
227 if (!newObj) {
228 /* If this is a new array object id, allocate an array object now.
229 */
230
231 newObj = (*ctx->Driver.NewArrayObject)(ctx, id);
232 if (!newObj) {
233 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE");
234 return;
235 }
236 _mesa_save_array_object(ctx, newObj);
237 }
238 }
239
240
241 ctx->NewState |= _NEW_ARRAY;
242 ctx->Array.NewState |= _NEW_ARRAY_ALL;
243 ctx->Array.ArrayObj = newObj;
244
245
246 /* Pass BindVertexArray call to device driver */
247 if (ctx->Driver.BindArrayObject && newObj)
248 (*ctx->Driver.BindArrayObject)( ctx, newObj );
249 }
250
251
252 /**
253 * Delete a set of array objects.
254 *
255 * \param n Number of array objects to delete.
256 * \param ids Array of \c n array object IDs.
257 */
258 void GLAPIENTRY
259 _mesa_DeleteVertexArraysAPPLE(GLsizei n, const GLuint *ids)
260 {
261 GET_CURRENT_CONTEXT(ctx);
262 GLsizei i;
263 ASSERT_OUTSIDE_BEGIN_END(ctx);
264
265 if (n < 0) {
266 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArrayAPPLE(n)");
267 return;
268 }
269
270 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
271
272 for (i = 0; i < n; i++) {
273 struct gl_array_object *obj = lookup_arrayobj(ctx, ids[i]);
274
275 if ( obj != NULL ) {
276 ASSERT( obj->Name == ids[i] );
277
278
279 /* If the array object is currently bound, the spec says "the binding
280 * for that object reverts to zero and the default vertex array
281 * becomes current."
282 */
283 if ( obj == ctx->Array.ArrayObj ) {
284 CALL_BindVertexArrayAPPLE( ctx->Exec, (0) );
285 }
286
287 #if FEATURE_ARB_vertex_buffer_object
288 /* Unbind any buffer objects that might be bound to arrays in
289 * this array object.
290 */
291 unbind_buffer_object( ctx, obj->Vertex.BufferObj );
292 unbind_buffer_object( ctx, obj->Normal.BufferObj );
293 unbind_buffer_object( ctx, obj->Color.BufferObj );
294 unbind_buffer_object( ctx, obj->SecondaryColor.BufferObj );
295 unbind_buffer_object( ctx, obj->FogCoord.BufferObj );
296 unbind_buffer_object( ctx, obj->Index.BufferObj );
297 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
298 unbind_buffer_object( ctx, obj->TexCoord[i].BufferObj );
299 }
300 unbind_buffer_object( ctx, obj->EdgeFlag.BufferObj );
301 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
302 unbind_buffer_object( ctx, obj->VertexAttrib[i].BufferObj );
303 }
304 #endif
305
306 /* The ID is immediately freed for re-use */
307 _mesa_remove_array_object(ctx, obj);
308 ctx->Driver.DeleteArrayObject(ctx, obj);
309 }
310 }
311
312 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
313 }
314
315
316 /**
317 * Generate a set of unique array object IDs and store them in \c arrays.
318 *
319 * \param n Number of IDs to generate.
320 * \param arrays Array of \c n locations to store the IDs.
321 */
322 void GLAPIENTRY
323 _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays)
324 {
325 GET_CURRENT_CONTEXT(ctx);
326 GLuint first;
327 GLint i;
328 ASSERT_OUTSIDE_BEGIN_END(ctx);
329
330 if (n < 0) {
331 _mesa_error(ctx, GL_INVALID_VALUE, "glGenVertexArraysAPPLE");
332 return;
333 }
334
335 if (!arrays) {
336 return;
337 }
338
339 /*
340 * This must be atomic (generation and allocation of array object IDs)
341 */
342 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
343
344 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->ArrayObjects, n);
345
346 /* Allocate new, empty array objects and return identifiers */
347 for (i = 0; i < n; i++) {
348 struct gl_array_object *obj;
349 GLuint name = first + i;
350
351 obj = (*ctx->Driver.NewArrayObject)( ctx, name );
352 if (!obj) {
353 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
354 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenVertexArraysAPPLE");
355 return;
356 }
357 _mesa_save_array_object(ctx, obj);
358 arrays[i] = first + i;
359 }
360
361 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
362 }
363
364
365 /**
366 * Determine if ID is the name of an array object.
367 *
368 * \param id ID of the potential array object.
369 * \return \c GL_TRUE if \c id is the name of a array object,
370 * \c GL_FALSE otherwise.
371 */
372 GLboolean GLAPIENTRY
373 _mesa_IsVertexArrayAPPLE( GLuint id )
374 {
375 struct gl_array_object * obj;
376 GET_CURRENT_CONTEXT(ctx);
377 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
378
379 if (id == 0)
380 return GL_FALSE;
381
382 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
383 obj = lookup_arrayobj(ctx, id);
384 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
385
386 return (obj != NULL) ? GL_TRUE : GL_FALSE;
387 }