Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
| 3 | * Version: 6.5 |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 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 "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 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; |
| 81 | |
| 82 | (void) ctx; |
| 83 | |
| 84 | obj = MALLOC_STRUCT(gl_array_object); |
| 85 | _mesa_initialize_array_object(ctx, obj, name); |
| 86 | return obj; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Delete an array object. |
| 92 | * |
| 93 | * This function is intended to be called via |
| 94 | * \c dd_function_table::DeleteArrayObject. |
| 95 | */ |
| 96 | void |
| 97 | _mesa_delete_array_object( GLcontext *ctx, struct gl_array_object *obj ) |
| 98 | { |
| 99 | (void) ctx; |
| 100 | |
| 101 | _mesa_free(obj); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void |
| 106 | _mesa_initialize_array_object( GLcontext *ctx, |
| 107 | struct gl_array_object *obj, |
| 108 | GLuint name ) |
| 109 | { |
| 110 | GLuint i; |
| 111 | |
| 112 | |
| 113 | obj->Name = name; |
| 114 | |
| 115 | /* Vertex arrays */ |
| 116 | obj->Vertex.Size = 4; |
| 117 | obj->Vertex.Type = GL_FLOAT; |
| 118 | obj->Vertex.Stride = 0; |
| 119 | obj->Vertex.StrideB = 0; |
| 120 | obj->Vertex.Ptr = NULL; |
| 121 | obj->Vertex.Enabled = GL_FALSE; |
| 122 | obj->Vertex.Flags = CA_CLIENT_DATA; |
| 123 | obj->Normal.Type = GL_FLOAT; |
| 124 | obj->Normal.Stride = 0; |
| 125 | obj->Normal.StrideB = 0; |
| 126 | obj->Normal.Ptr = NULL; |
| 127 | obj->Normal.Enabled = GL_FALSE; |
| 128 | obj->Normal.Flags = CA_CLIENT_DATA; |
| 129 | obj->Color.Size = 4; |
| 130 | obj->Color.Type = GL_FLOAT; |
| 131 | obj->Color.Stride = 0; |
| 132 | obj->Color.StrideB = 0; |
| 133 | obj->Color.Ptr = NULL; |
| 134 | obj->Color.Enabled = GL_FALSE; |
| 135 | obj->Color.Flags = CA_CLIENT_DATA; |
| 136 | obj->SecondaryColor.Size = 4; |
| 137 | obj->SecondaryColor.Type = GL_FLOAT; |
| 138 | obj->SecondaryColor.Stride = 0; |
| 139 | obj->SecondaryColor.StrideB = 0; |
| 140 | obj->SecondaryColor.Ptr = NULL; |
| 141 | obj->SecondaryColor.Enabled = GL_FALSE; |
| 142 | obj->SecondaryColor.Flags = CA_CLIENT_DATA; |
| 143 | obj->FogCoord.Size = 1; |
| 144 | obj->FogCoord.Type = GL_FLOAT; |
| 145 | obj->FogCoord.Stride = 0; |
| 146 | obj->FogCoord.StrideB = 0; |
| 147 | obj->FogCoord.Ptr = NULL; |
| 148 | obj->FogCoord.Enabled = GL_FALSE; |
| 149 | obj->FogCoord.Flags = CA_CLIENT_DATA; |
| 150 | obj->Index.Type = GL_FLOAT; |
| 151 | obj->Index.Stride = 0; |
| 152 | obj->Index.StrideB = 0; |
| 153 | obj->Index.Ptr = NULL; |
| 154 | obj->Index.Enabled = GL_FALSE; |
| 155 | obj->Index.Flags = CA_CLIENT_DATA; |
| 156 | for (i = 0; i < MAX_TEXTURE_UNITS; i++) { |
| 157 | obj->TexCoord[i].Size = 4; |
| 158 | obj->TexCoord[i].Type = GL_FLOAT; |
| 159 | obj->TexCoord[i].Stride = 0; |
| 160 | obj->TexCoord[i].StrideB = 0; |
| 161 | obj->TexCoord[i].Ptr = NULL; |
| 162 | obj->TexCoord[i].Enabled = GL_FALSE; |
| 163 | obj->TexCoord[i].Flags = CA_CLIENT_DATA; |
| 164 | } |
| 165 | obj->EdgeFlag.Stride = 0; |
| 166 | obj->EdgeFlag.StrideB = 0; |
| 167 | obj->EdgeFlag.Ptr = NULL; |
| 168 | obj->EdgeFlag.Enabled = GL_FALSE; |
| 169 | obj->EdgeFlag.Flags = CA_CLIENT_DATA; |
| 170 | for (i = 0; i < VERT_ATTRIB_MAX; i++) { |
| 171 | obj->VertexAttrib[i].Size = 4; |
| 172 | obj->VertexAttrib[i].Type = GL_FLOAT; |
| 173 | obj->VertexAttrib[i].Stride = 0; |
| 174 | obj->VertexAttrib[i].StrideB = 0; |
| 175 | obj->VertexAttrib[i].Ptr = NULL; |
| 176 | obj->VertexAttrib[i].Enabled = GL_FALSE; |
| 177 | obj->VertexAttrib[i].Normalized = GL_FALSE; |
| 178 | obj->VertexAttrib[i].Flags = CA_CLIENT_DATA; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | #if FEATURE_ARB_vertex_buffer_object |
| 183 | /* Vertex array buffers */ |
| 184 | obj->Vertex.BufferObj = ctx->Array.NullBufferObj; |
| 185 | obj->Normal.BufferObj = ctx->Array.NullBufferObj; |
| 186 | obj->Color.BufferObj = ctx->Array.NullBufferObj; |
| 187 | obj->SecondaryColor.BufferObj = ctx->Array.NullBufferObj; |
| 188 | obj->FogCoord.BufferObj = ctx->Array.NullBufferObj; |
| 189 | obj->Index.BufferObj = ctx->Array.NullBufferObj; |
| 190 | for (i = 0; i < MAX_TEXTURE_UNITS; i++) { |
| 191 | obj->TexCoord[i].BufferObj = ctx->Array.NullBufferObj; |
| 192 | } |
| 193 | obj->EdgeFlag.BufferObj = ctx->Array.NullBufferObj; |
| 194 | for (i = 0; i < VERT_ATTRIB_MAX; i++) { |
| 195 | obj->VertexAttrib[i].BufferObj = ctx->Array.NullBufferObj; |
| 196 | } |
| 197 | #endif |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * Add the given array object to the array object pool. |
| 203 | */ |
| 204 | void |
| 205 | _mesa_save_array_object( GLcontext *ctx, struct gl_array_object *obj ) |
| 206 | { |
| 207 | if (obj->Name > 0) { |
| 208 | /* insert into hash table */ |
| 209 | _mesa_HashInsert(ctx->Shared->ArrayObjects, obj->Name, obj); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * Remove the given array object from the array object pool. |
| 216 | * Do not deallocate the array object though. |
| 217 | */ |
| 218 | void |
| 219 | _mesa_remove_array_object( GLcontext *ctx, struct gl_array_object *obj ) |
| 220 | { |
| 221 | if (obj->Name > 0) { |
| 222 | /* remove from hash table */ |
| 223 | _mesa_HashRemove(ctx->Shared->ArrayObjects, obj->Name); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /**********************************************************************/ |
| 229 | /* API Functions */ |
| 230 | /**********************************************************************/ |
| 231 | |
| 232 | /** |
| 233 | * Bind a new array. |
| 234 | * |
| 235 | * \todo |
| 236 | * The binding could be done more efficiently by comparing the non-NULL |
| 237 | * pointers in the old and new objects. The only arrays that are "dirty" are |
| 238 | * the ones that are non-NULL in either object. |
| 239 | */ |
| 240 | void GLAPIENTRY |
| 241 | _mesa_BindVertexArrayAPPLE( GLuint id ) |
| 242 | { |
| 243 | GET_CURRENT_CONTEXT(ctx); |
| 244 | struct gl_array_object * const oldObj = ctx->Array.ArrayObj; |
| 245 | struct gl_array_object *newObj = NULL; |
| 246 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 247 | |
| 248 | |
| 249 | ASSERT(oldObj != NULL); |
| 250 | |
| 251 | if ( oldObj->Name == id ) |
| 252 | return; /* rebinding the same array object- no change */ |
| 253 | |
| 254 | /* |
| 255 | * Get pointer to new array object (newBufObj) |
| 256 | */ |
| 257 | if (id == 0) { |
| 258 | /* The spec says there is no array object named 0, but we use |
| 259 | * one internally because it simplifies things. |
| 260 | */ |
| 261 | newObj = ctx->Array.DefaultArrayObj; |
| 262 | } |
| 263 | else { |
| 264 | /* non-default array object */ |
| 265 | newObj = lookup_arrayobj(ctx, id); |
| 266 | if (!newObj) { |
| 267 | /* If this is a new array object id, allocate an array object now. |
| 268 | */ |
| 269 | |
| 270 | newObj = (*ctx->Driver.NewArrayObject)(ctx, id); |
| 271 | if (!newObj) { |
| 272 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE"); |
| 273 | return; |
| 274 | } |
| 275 | _mesa_save_array_object(ctx, newObj); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | |
| 280 | ctx->NewState |= _NEW_ARRAY; |
| 281 | ctx->Array.NewState |= _NEW_ARRAY_ALL; |
| 282 | ctx->Array.ArrayObj = newObj; |
| 283 | |
| 284 | |
| 285 | /* Pass BindVertexArray call to device driver */ |
| 286 | if (ctx->Driver.BindArrayObject && newObj) |
| 287 | (*ctx->Driver.BindArrayObject)( ctx, newObj ); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * Delete a set of array objects. |
| 293 | * |
| 294 | * \param n Number of array objects to delete. |
| 295 | * \param ids Array of \c n array object IDs. |
| 296 | */ |
| 297 | void GLAPIENTRY |
| 298 | _mesa_DeleteVertexArraysAPPLE(GLsizei n, const GLuint *ids) |
| 299 | { |
| 300 | GET_CURRENT_CONTEXT(ctx); |
| 301 | GLsizei i; |
| 302 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 303 | |
| 304 | if (n < 0) { |
| 305 | _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArrayAPPLE(n)"); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | _glthread_LOCK_MUTEX(ctx->Shared->Mutex); |
| 310 | |
| 311 | for (i = 0; i < n; i++) { |
| 312 | struct gl_array_object *obj = lookup_arrayobj(ctx, ids[i]); |
| 313 | |
| 314 | if ( obj != NULL ) { |
| 315 | ASSERT( obj->Name == ids[i] ); |
| 316 | |
| 317 | |
| 318 | /* If the array object is currently bound, the spec says "the binding |
| 319 | * for that object reverts to zero and the default vertex array |
| 320 | * becomes current." |
| 321 | */ |
| 322 | if ( obj == ctx->Array.ArrayObj ) { |
| 323 | CALL_BindVertexArrayAPPLE( ctx->Exec, (0) ); |
| 324 | } |
| 325 | |
| 326 | #if FEATURE_ARB_vertex_buffer_object |
| 327 | /* Unbind any buffer objects that might be bound to arrays in |
| 328 | * this array object. |
| 329 | */ |
| 330 | _mesa_unbind_buffer_object( ctx, obj->Vertex.BufferObj ); |
| 331 | _mesa_unbind_buffer_object( ctx, obj->Normal.BufferObj ); |
| 332 | _mesa_unbind_buffer_object( ctx, obj->Color.BufferObj ); |
| 333 | _mesa_unbind_buffer_object( ctx, obj->SecondaryColor.BufferObj ); |
| 334 | _mesa_unbind_buffer_object( ctx, obj->FogCoord.BufferObj ); |
| 335 | _mesa_unbind_buffer_object( ctx, obj->Index.BufferObj ); |
| 336 | for (i = 0; i < MAX_TEXTURE_UNITS; i++) { |
| 337 | _mesa_unbind_buffer_object( ctx, obj->TexCoord[i].BufferObj ); |
| 338 | } |
| 339 | _mesa_unbind_buffer_object( ctx, obj->EdgeFlag.BufferObj ); |
| 340 | for (i = 0; i < VERT_ATTRIB_MAX; i++) { |
| 341 | _mesa_unbind_buffer_object( ctx, obj->VertexAttrib[i].BufferObj ); |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | /* The ID is immediately freed for re-use */ |
| 346 | _mesa_remove_array_object(ctx, obj); |
| 347 | ctx->Driver.DeleteArrayObject(ctx, obj); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /** |
| 356 | * Generate a set of unique array object IDs and store them in \c buffer. |
| 357 | * |
| 358 | * \param n Number of IDs to generate. |
| 359 | * \param buffer Array of \c n locations to store the IDs. |
| 360 | */ |
| 361 | void GLAPIENTRY |
| 362 | _mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *buffer) |
| 363 | { |
| 364 | GET_CURRENT_CONTEXT(ctx); |
| 365 | GLuint first; |
| 366 | GLint i; |
| 367 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 368 | |
| 369 | if (n < 0) { |
| 370 | _mesa_error(ctx, GL_INVALID_VALUE, "glGenVertexArraysAPPLE"); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | if (!buffer) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * This must be atomic (generation and allocation of array object IDs) |
| 380 | */ |
| 381 | _glthread_LOCK_MUTEX(ctx->Shared->Mutex); |
| 382 | |
| 383 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->ArrayObjects, n); |
| 384 | |
| 385 | /* Allocate new, empty array objects and return identifiers */ |
| 386 | for (i = 0; i < n; i++) { |
| 387 | struct gl_array_object *obj; |
| 388 | GLuint name = first + i; |
| 389 | |
| 390 | obj = (*ctx->Driver.NewArrayObject)( ctx, name ); |
| 391 | if (!obj) { |
| 392 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| 393 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenVertexArraysAPPLE"); |
| 394 | return; |
| 395 | } |
| 396 | _mesa_save_array_object(ctx, obj); |
| 397 | buffer[i] = first + i; |
| 398 | } |
| 399 | |
| 400 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /** |
| 405 | * Determine if ID is the name of an array object. |
| 406 | * |
| 407 | * \param id ID of the potential array object. |
| 408 | * \return \c GL_TRUE if \c id is the name of a array object, |
| 409 | * \c GL_FALSE otherwise. |
| 410 | */ |
| 411 | GLboolean GLAPIENTRY |
| 412 | _mesa_IsVertexArrayAPPLE( GLuint id ) |
| 413 | { |
| 414 | struct gl_array_object * obj; |
| 415 | GET_CURRENT_CONTEXT(ctx); |
| 416 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 417 | |
| 418 | if (id == 0) |
| 419 | return GL_FALSE; |
| 420 | |
| 421 | _glthread_LOCK_MUTEX(ctx->Shared->Mutex); |
| 422 | obj = lookup_arrayobj(ctx, id); |
| 423 | _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); |
| 424 | |
| 425 | return (obj != NULL) ? GL_TRUE : GL_FALSE; |
| 426 | } |