arrayobj.c revision 7117f1b4
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 61static INLINE struct gl_array_object * 62lookup_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 */ 77struct gl_array_object * 78_mesa_new_array_object( GLcontext *ctx, GLuint name ) 79{ 80 struct gl_array_object *obj = MALLOC_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 */ 93void 94_mesa_delete_array_object( GLcontext *ctx, struct gl_array_object *obj ) 95{ 96 (void) ctx; 97 _mesa_free(obj); 98} 99 100 101void 102_mesa_initialize_array_object( GLcontext *ctx, 103 struct gl_array_object *obj, 104 GLuint name ) 105{ 106 GLuint i; 107 108 obj->Name = name; 109 110 /* Vertex arrays */ 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; 122 obj->Color.Size = 4; 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; 152 } 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; 165 } 166 167#if FEATURE_ARB_vertex_buffer_object 168 /* Vertex array buffers */ 169 obj->Vertex.BufferObj = ctx->Array.NullBufferObj; 170 obj->Normal.BufferObj = ctx->Array.NullBufferObj; 171 obj->Color.BufferObj = ctx->Array.NullBufferObj; 172 obj->SecondaryColor.BufferObj = ctx->Array.NullBufferObj; 173 obj->FogCoord.BufferObj = ctx->Array.NullBufferObj; 174 obj->Index.BufferObj = ctx->Array.NullBufferObj; 175 for (i = 0; i < MAX_TEXTURE_UNITS; i++) { 176 obj->TexCoord[i].BufferObj = ctx->Array.NullBufferObj; 177 } 178 obj->EdgeFlag.BufferObj = ctx->Array.NullBufferObj; 179 for (i = 0; i < VERT_ATTRIB_MAX; i++) { 180 obj->VertexAttrib[i].BufferObj = ctx->Array.NullBufferObj; 181 } 182#endif 183} 184 185 186/** 187 * Add the given array object to the array object pool. 188 */ 189void 190_mesa_save_array_object( GLcontext *ctx, struct gl_array_object *obj ) 191{ 192 if (obj->Name > 0) { 193 /* insert into hash table */ 194 _mesa_HashInsert(ctx->Shared->ArrayObjects, obj->Name, obj); 195 } 196} 197 198 199/** 200 * Remove the given array object from the array object pool. 201 * Do not deallocate the array object though. 202 */ 203void 204_mesa_remove_array_object( GLcontext *ctx, struct gl_array_object *obj ) 205{ 206 if (obj->Name > 0) { 207 /* remove from hash table */ 208 _mesa_HashRemove(ctx->Shared->ArrayObjects, obj->Name); 209 } 210} 211 212 213/**********************************************************************/ 214/* API Functions */ 215/**********************************************************************/ 216 217/** 218 * Bind a new array. 219 * 220 * \todo 221 * The binding could be done more efficiently by comparing the non-NULL 222 * pointers in the old and new objects. The only arrays that are "dirty" are 223 * the ones that are non-NULL in either object. 224 */ 225void GLAPIENTRY 226_mesa_BindVertexArrayAPPLE( GLuint id ) 227{ 228 GET_CURRENT_CONTEXT(ctx); 229 struct gl_array_object * const oldObj = ctx->Array.ArrayObj; 230 struct gl_array_object *newObj = NULL; 231 ASSERT_OUTSIDE_BEGIN_END(ctx); 232 233 ASSERT(oldObj != NULL); 234 235 if ( oldObj->Name == id ) 236 return; /* rebinding the same array object- no change */ 237 238 /* 239 * Get pointer to new array object (newBufObj) 240 */ 241 if (id == 0) { 242 /* The spec says there is no array object named 0, but we use 243 * one internally because it simplifies things. 244 */ 245 newObj = ctx->Array.DefaultArrayObj; 246 } 247 else { 248 /* non-default array object */ 249 newObj = lookup_arrayobj(ctx, id); 250 if (!newObj) { 251 /* If this is a new array object id, allocate an array object now. 252 */ 253 254 newObj = (*ctx->Driver.NewArrayObject)(ctx, id); 255 if (!newObj) { 256 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindVertexArrayAPPLE"); 257 return; 258 } 259 _mesa_save_array_object(ctx, newObj); 260 } 261 } 262 263 264 ctx->NewState |= _NEW_ARRAY; 265 ctx->Array.NewState |= _NEW_ARRAY_ALL; 266 ctx->Array.ArrayObj = newObj; 267 268 269 /* Pass BindVertexArray call to device driver */ 270 if (ctx->Driver.BindArrayObject && newObj) 271 (*ctx->Driver.BindArrayObject)( ctx, newObj ); 272} 273 274 275/** 276 * Delete a set of array objects. 277 * 278 * \param n Number of array objects to delete. 279 * \param ids Array of \c n array object IDs. 280 */ 281void GLAPIENTRY 282_mesa_DeleteVertexArraysAPPLE(GLsizei n, const GLuint *ids) 283{ 284 GET_CURRENT_CONTEXT(ctx); 285 GLsizei i; 286 ASSERT_OUTSIDE_BEGIN_END(ctx); 287 288 if (n < 0) { 289 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteVertexArrayAPPLE(n)"); 290 return; 291 } 292 293 _glthread_LOCK_MUTEX(ctx->Shared->Mutex); 294 295 for (i = 0; i < n; i++) { 296 struct gl_array_object *obj = lookup_arrayobj(ctx, ids[i]); 297 298 if ( obj != NULL ) { 299 ASSERT( obj->Name == ids[i] ); 300 301 302 /* If the array object is currently bound, the spec says "the binding 303 * for that object reverts to zero and the default vertex array 304 * becomes current." 305 */ 306 if ( obj == ctx->Array.ArrayObj ) { 307 CALL_BindVertexArrayAPPLE( ctx->Exec, (0) ); 308 } 309 310#if FEATURE_ARB_vertex_buffer_object 311 /* Unbind any buffer objects that might be bound to arrays in 312 * this array object. 313 */ 314 _mesa_unbind_buffer_object( ctx, obj->Vertex.BufferObj ); 315 _mesa_unbind_buffer_object( ctx, obj->Normal.BufferObj ); 316 _mesa_unbind_buffer_object( ctx, obj->Color.BufferObj ); 317 _mesa_unbind_buffer_object( ctx, obj->SecondaryColor.BufferObj ); 318 _mesa_unbind_buffer_object( ctx, obj->FogCoord.BufferObj ); 319 _mesa_unbind_buffer_object( ctx, obj->Index.BufferObj ); 320 for (i = 0; i < MAX_TEXTURE_UNITS; i++) { 321 _mesa_unbind_buffer_object( ctx, obj->TexCoord[i].BufferObj ); 322 } 323 _mesa_unbind_buffer_object( ctx, obj->EdgeFlag.BufferObj ); 324 for (i = 0; i < VERT_ATTRIB_MAX; i++) { 325 _mesa_unbind_buffer_object( ctx, obj->VertexAttrib[i].BufferObj ); 326 } 327#endif 328 329 /* The ID is immediately freed for re-use */ 330 _mesa_remove_array_object(ctx, obj); 331 ctx->Driver.DeleteArrayObject(ctx, obj); 332 } 333 } 334 335 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); 336} 337 338 339/** 340 * Generate a set of unique array object IDs and store them in \c arrays. 341 * 342 * \param n Number of IDs to generate. 343 * \param arrays Array of \c n locations to store the IDs. 344 */ 345void GLAPIENTRY 346_mesa_GenVertexArraysAPPLE(GLsizei n, GLuint *arrays) 347{ 348 GET_CURRENT_CONTEXT(ctx); 349 GLuint first; 350 GLint i; 351 ASSERT_OUTSIDE_BEGIN_END(ctx); 352 353 if (n < 0) { 354 _mesa_error(ctx, GL_INVALID_VALUE, "glGenVertexArraysAPPLE"); 355 return; 356 } 357 358 if (!arrays) { 359 return; 360 } 361 362 /* 363 * This must be atomic (generation and allocation of array object IDs) 364 */ 365 _glthread_LOCK_MUTEX(ctx->Shared->Mutex); 366 367 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->ArrayObjects, n); 368 369 /* Allocate new, empty array objects and return identifiers */ 370 for (i = 0; i < n; i++) { 371 struct gl_array_object *obj; 372 GLuint name = first + i; 373 374 obj = (*ctx->Driver.NewArrayObject)( ctx, name ); 375 if (!obj) { 376 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); 377 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenVertexArraysAPPLE"); 378 return; 379 } 380 _mesa_save_array_object(ctx, obj); 381 arrays[i] = first + i; 382 } 383 384 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); 385} 386 387 388/** 389 * Determine if ID is the name of an array object. 390 * 391 * \param id ID of the potential array object. 392 * \return \c GL_TRUE if \c id is the name of a array object, 393 * \c GL_FALSE otherwise. 394 */ 395GLboolean GLAPIENTRY 396_mesa_IsVertexArrayAPPLE( GLuint id ) 397{ 398 struct gl_array_object * obj; 399 GET_CURRENT_CONTEXT(ctx); 400 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); 401 402 if (id == 0) 403 return GL_FALSE; 404 405 _glthread_LOCK_MUTEX(ctx->Shared->Mutex); 406 obj = lookup_arrayobj(ctx, id); 407 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex); 408 409 return (obj != NULL) ? GL_TRUE : GL_FALSE; 410} 411