| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Mesa 3-D graphics library |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 4 | * Version: 3.3 |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 5 | * |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2000 Brian Paul All Rights Reserved. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 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 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 | */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | #ifdef PC_HEADER |
| 28 | #include "all.h" |
| 29 | #else |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 30 | #include "glheader.h" |
| Brian Paul | 4bdcfe5 | 2000-04-17 17:57:04 +0000 | [diff] [blame] | 31 | #include "colortab.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 32 | #include "context.h" |
| 33 | #include "enums.h" |
| 34 | #include "hash.h" |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 35 | #include "mem.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 36 | #include "teximage.h" |
| 37 | #include "texstate.h" |
| 38 | #include "texobj.h" |
| 39 | #include "types.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | |
| 43 | |
| 44 | /* |
| 45 | * Allocate a new texture object and add it to the linked list of texture |
| 46 | * objects. If name>0 then also insert the new texture object into the hash |
| 47 | * table. |
| 48 | * Input: shared - the shared GL state structure to contain the texture object |
| 49 | * name - integer name for the texture object |
| 50 | * dimensions - either 1, 2 or 3 |
| 51 | * Return: pointer to new texture object |
| 52 | */ |
| 53 | struct gl_texture_object * |
| 54 | gl_alloc_texture_object( struct gl_shared_state *shared, GLuint name, |
| 55 | GLuint dimensions) |
| 56 | { |
| 57 | struct gl_texture_object *obj; |
| 58 | |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 59 | ASSERT(dimensions <= 3); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 60 | |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 61 | obj = CALLOC_STRUCT(gl_texture_object); |
| 62 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 63 | if (obj) { |
| 64 | /* init the non-zero fields */ |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 65 | obj->RefCount = 1; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 66 | obj->Name = name; |
| 67 | obj->Dimensions = dimensions; |
| 68 | obj->WrapS = GL_REPEAT; |
| 69 | obj->WrapT = GL_REPEAT; |
| 70 | obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR; |
| 71 | obj->MagFilter = GL_LINEAR; |
| 72 | obj->MinLod = -1000.0; |
| 73 | obj->MaxLod = 1000.0; |
| 74 | obj->BaseLevel = 0; |
| 75 | obj->MaxLevel = 1000; |
| 76 | obj->MinMagThresh = 0.0F; |
| Brian Paul | 4bdcfe5 | 2000-04-17 17:57:04 +0000 | [diff] [blame] | 77 | _mesa_init_colortable(&obj->Palette); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 78 | |
| 79 | /* insert into linked list */ |
| 80 | if (shared) { |
| Brian Paul | 9560f05 | 2000-01-31 23:11:39 +0000 | [diff] [blame] | 81 | _glthread_LOCK_MUTEX(shared->Mutex); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 82 | obj->Next = shared->TexObjectList; |
| 83 | shared->TexObjectList = obj; |
| Brian Paul | 9560f05 | 2000-01-31 23:11:39 +0000 | [diff] [blame] | 84 | _glthread_UNLOCK_MUTEX(shared->Mutex); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (name > 0) { |
| 88 | /* insert into hash table */ |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 89 | _mesa_HashInsert(shared->TexObjects, name, obj); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | return obj; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* |
| 97 | * Deallocate a texture object struct and remove it from the given |
| 98 | * shared GL state. |
| 99 | * Input: shared - the shared GL state to which the object belongs |
| 100 | * t - the texture object to delete |
| 101 | */ |
| 102 | void gl_free_texture_object( struct gl_shared_state *shared, |
| 103 | struct gl_texture_object *t ) |
| 104 | { |
| 105 | struct gl_texture_object *tprev, *tcurr; |
| 106 | |
| 107 | assert(t); |
| 108 | |
| 109 | /* Remove t from dirty list so we don't touch free'd memory later. |
| 110 | * Test for shared since Proxy texture aren't in global linked list. |
| 111 | */ |
| 112 | if (shared) |
| 113 | gl_remove_texobj_from_dirty_list( shared, t ); |
| 114 | |
| 115 | /* unlink t from the linked list */ |
| 116 | if (shared) { |
| Brian Paul | 9560f05 | 2000-01-31 23:11:39 +0000 | [diff] [blame] | 117 | _glthread_LOCK_MUTEX(shared->Mutex); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 118 | tprev = NULL; |
| 119 | tcurr = shared->TexObjectList; |
| 120 | while (tcurr) { |
| 121 | if (tcurr==t) { |
| 122 | if (tprev) { |
| 123 | tprev->Next = t->Next; |
| 124 | } |
| 125 | else { |
| 126 | shared->TexObjectList = t->Next; |
| 127 | } |
| 128 | break; |
| 129 | } |
| 130 | tprev = tcurr; |
| 131 | tcurr = tcurr->Next; |
| 132 | } |
| Brian Paul | 9560f05 | 2000-01-31 23:11:39 +0000 | [diff] [blame] | 133 | _glthread_UNLOCK_MUTEX(shared->Mutex); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | if (t->Name) { |
| 137 | /* remove from hash table */ |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 138 | _mesa_HashRemove(shared->TexObjects, t->Name); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| Brian Paul | 4bdcfe5 | 2000-04-17 17:57:04 +0000 | [diff] [blame] | 141 | _mesa_free_colortable_data(&t->Palette); |
| 142 | |
| 143 | /* free texture images */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 144 | { |
| 145 | GLuint i; |
| 146 | for (i=0;i<MAX_TEXTURE_LEVELS;i++) { |
| 147 | if (t->Image[i]) { |
| Brian Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 148 | _mesa_free_texture_image( t->Image[i] ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | /* free this object */ |
| Brian Paul | bd5cdaf | 1999-10-13 18:42:49 +0000 | [diff] [blame] | 153 | FREE( t ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | /* |
| 159 | * Examine a texture object to determine if it is complete or not. |
| 160 | * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly. |
| 161 | */ |
| 162 | void gl_test_texture_object_completeness( const GLcontext *ctx, struct gl_texture_object *t ) |
| 163 | { |
| 164 | t->Complete = GL_TRUE; /* be optimistic */ |
| 165 | |
| 166 | /* Always need level zero image */ |
| Brian Paul | 4827179 | 2000-03-29 18:13:59 +0000 | [diff] [blame] | 167 | if (!t->Image[0]) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 168 | t->Complete = GL_FALSE; |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | /* Compute number of mipmap levels */ |
| 173 | if (t->Dimensions==1) { |
| 174 | t->P = t->Image[0]->WidthLog2; |
| 175 | } |
| 176 | else if (t->Dimensions==2) { |
| 177 | t->P = MAX2(t->Image[0]->WidthLog2, t->Image[0]->HeightLog2); |
| 178 | } |
| 179 | else if (t->Dimensions==3) { |
| 180 | GLint max = MAX2(t->Image[0]->WidthLog2, t->Image[0]->HeightLog2); |
| 181 | max = MAX2(max, (GLint)(t->Image[0]->DepthLog2)); |
| 182 | t->P = max; |
| 183 | } |
| 184 | |
| 185 | /* Compute M (see the 1.2 spec) used during mipmapping */ |
| 186 | t->M = (GLfloat) (MIN2(t->MaxLevel, t->P) - t->BaseLevel); |
| 187 | |
| 188 | |
| 189 | if (t->MinFilter!=GL_NEAREST && t->MinFilter!=GL_LINEAR) { |
| 190 | /* |
| 191 | * Mipmapping: determine if we have a complete set of mipmaps |
| 192 | */ |
| 193 | GLint i; |
| 194 | GLint minLevel = t->BaseLevel; |
| 195 | GLint maxLevel = MIN2(t->P, ctx->Const.MaxTextureLevels-1); |
| 196 | maxLevel = MIN2(maxLevel, t->MaxLevel); |
| 197 | |
| 198 | if (minLevel > maxLevel) { |
| 199 | t->Complete = GL_FALSE; |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | /* Test dimension-independent attributes */ |
| 204 | for (i = minLevel; i <= maxLevel; i++) { |
| 205 | if (t->Image[i]) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 206 | if (t->Image[i]->Format != t->Image[0]->Format) { |
| 207 | t->Complete = GL_FALSE; |
| 208 | return; |
| 209 | } |
| 210 | if (t->Image[i]->Border != t->Image[0]->Border) { |
| 211 | t->Complete = GL_FALSE; |
| 212 | return; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* Test things which depend on number of texture image dimensions */ |
| 218 | if (t->Dimensions==1) { |
| 219 | /* Test 1-D mipmaps */ |
| 220 | GLuint width = t->Image[0]->Width2; |
| 221 | for (i=1; i<ctx->Const.MaxTextureLevels; i++) { |
| 222 | if (width>1) { |
| 223 | width /= 2; |
| 224 | } |
| 225 | if (i >= minLevel && i <= maxLevel) { |
| 226 | if (!t->Image[i]) { |
| 227 | t->Complete = GL_FALSE; |
| 228 | return; |
| 229 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 230 | if (t->Image[i]->Width2 != width ) { |
| 231 | t->Complete = GL_FALSE; |
| 232 | return; |
| 233 | } |
| 234 | } |
| 235 | if (width==1) { |
| 236 | return; /* found smallest needed mipmap, all done! */ |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | else if (t->Dimensions==2) { |
| 241 | /* Test 2-D mipmaps */ |
| 242 | GLuint width = t->Image[0]->Width2; |
| 243 | GLuint height = t->Image[0]->Height2; |
| 244 | for (i=1; i<ctx->Const.MaxTextureLevels; i++) { |
| 245 | if (width>1) { |
| 246 | width /= 2; |
| 247 | } |
| 248 | if (height>1) { |
| 249 | height /= 2; |
| 250 | } |
| 251 | if (i >= minLevel && i <= maxLevel) { |
| 252 | if (!t->Image[i]) { |
| 253 | t->Complete = GL_FALSE; |
| 254 | return; |
| 255 | } |
| 256 | if (t->Image[i]->Width2 != width) { |
| 257 | t->Complete = GL_FALSE; |
| 258 | return; |
| 259 | } |
| 260 | if (t->Image[i]->Height2 != height) { |
| 261 | t->Complete = GL_FALSE; |
| 262 | return; |
| 263 | } |
| 264 | if (width==1 && height==1) { |
| 265 | return; /* found smallest needed mipmap, all done! */ |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | else if (t->Dimensions==3) { |
| 271 | /* Test 3-D mipmaps */ |
| 272 | GLuint width = t->Image[0]->Width2; |
| 273 | GLuint height = t->Image[0]->Height2; |
| 274 | GLuint depth = t->Image[0]->Depth2; |
| 275 | for (i=1; i<ctx->Const.MaxTextureLevels; i++) { |
| 276 | if (width>1) { |
| 277 | width /= 2; |
| 278 | } |
| 279 | if (height>1) { |
| 280 | height /= 2; |
| 281 | } |
| 282 | if (depth>1) { |
| 283 | depth /= 2; |
| 284 | } |
| 285 | if (i >= minLevel && i <= maxLevel) { |
| 286 | if (!t->Image[i]) { |
| 287 | t->Complete = GL_FALSE; |
| 288 | return; |
| 289 | } |
| 290 | if (t->Image[i]->Width2 != width) { |
| 291 | t->Complete = GL_FALSE; |
| 292 | return; |
| 293 | } |
| 294 | if (t->Image[i]->Height2 != height) { |
| 295 | t->Complete = GL_FALSE; |
| 296 | return; |
| 297 | } |
| 298 | if (t->Image[i]->Depth2 != depth) { |
| 299 | t->Complete = GL_FALSE; |
| 300 | return; |
| 301 | } |
| 302 | } |
| 303 | if (width==1 && height==1 && depth==1) { |
| 304 | return; /* found smallest needed mipmap, all done! */ |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | else { |
| 309 | /* Dimensions = ??? */ |
| 310 | gl_problem(NULL, "Bug in gl_test_texture_object_completeness\n"); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | |
| Brian Paul | 832179c | 2000-03-21 17:42:27 +0000 | [diff] [blame] | 316 | _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock); |
| 317 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 318 | |
| 319 | /* |
| 320 | * Execute glGenTextures |
| 321 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 322 | void |
| 323 | _mesa_GenTextures( GLsizei n, GLuint *texName ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 324 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 325 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 326 | GLuint first; |
| 327 | GLint i; |
| 328 | |
| 329 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGenTextures"); |
| 330 | if (n<0) { |
| 331 | gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" ); |
| 332 | return; |
| 333 | } |
| 334 | |
| Brian Paul | 832179c | 2000-03-21 17:42:27 +0000 | [diff] [blame] | 335 | |
| 336 | /* |
| 337 | * This must be atomic (generation and allocation of texture IDs) |
| 338 | */ |
| 339 | _glthread_LOCK_MUTEX(GenTexturesLock); |
| 340 | |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 341 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 342 | |
| 343 | /* Return the texture names */ |
| 344 | for (i=0;i<n;i++) { |
| 345 | texName[i] = first + i; |
| 346 | } |
| 347 | |
| 348 | /* Allocate new, empty texture objects */ |
| 349 | for (i=0;i<n;i++) { |
| 350 | GLuint name = first + i; |
| 351 | GLuint dims = 0; |
| 352 | (void) gl_alloc_texture_object(ctx->Shared, name, dims); |
| 353 | } |
| Brian Paul | 832179c | 2000-03-21 17:42:27 +0000 | [diff] [blame] | 354 | |
| 355 | _glthread_UNLOCK_MUTEX(GenTexturesLock); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
| 359 | |
| 360 | /* |
| 361 | * Execute glDeleteTextures |
| 362 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 363 | void |
| 364 | _mesa_DeleteTextures( GLsizei n, const GLuint *texName) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 365 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 366 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 367 | GLint i; |
| 368 | |
| 369 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDeleteTextures"); |
| 370 | |
| 371 | for (i=0;i<n;i++) { |
| 372 | struct gl_texture_object *t; |
| 373 | if (texName[i]>0) { |
| 374 | t = (struct gl_texture_object *) |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 375 | _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 376 | if (t) { |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 377 | /* First check if this texture is currently bound. |
| 378 | * If so, unbind it and decrement the reference count. |
| 379 | */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 380 | GLuint u; |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 381 | for (u = 0; u < MAX_TEXTURE_UNITS; u++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 382 | struct gl_texture_unit *unit = &ctx->Texture.Unit[u]; |
| 383 | GLuint d; |
| 384 | for (d = 1 ; d <= 3 ; d++) { |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 385 | if (unit->CurrentD[d] == t) { |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 386 | unit->CurrentD[d] = ctx->Shared->DefaultD[d]; |
| 387 | ctx->Shared->DefaultD[d]->RefCount++; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 388 | t->RefCount--; |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 389 | ASSERT( t->RefCount >= 0 ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 394 | /* Decrement reference count and delete if zero */ |
| 395 | t->RefCount--; |
| 396 | ASSERT( t->RefCount >= 0 ); |
| 397 | if (t->RefCount == 0) { |
| 398 | if (ctx->Driver.DeleteTexture) |
| 399 | (*ctx->Driver.DeleteTexture)( ctx, t ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 400 | gl_free_texture_object(ctx->Shared, t); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | |
| 408 | |
| 409 | /* |
| 410 | * Execute glBindTexture |
| 411 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 412 | void |
| 413 | _mesa_BindTexture( GLenum target, GLuint texName ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 414 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 415 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 416 | GLuint unit = ctx->Texture.CurrentUnit; |
| 417 | struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; |
| 418 | struct gl_texture_object *oldTexObj; |
| 419 | struct gl_texture_object *newTexObj; |
| Brian Paul | 5b37c32 | 1999-11-05 06:43:10 +0000 | [diff] [blame] | 420 | GLuint dim; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 421 | |
| 422 | if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) |
| 423 | fprintf(stderr, "glBindTexture %s %d\n", |
| 424 | gl_lookup_enum_by_nr(target), (GLint) texName); |
| 425 | |
| 426 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glBindTexture"); |
| 427 | |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 428 | switch (target) { |
| 429 | case GL_TEXTURE_1D: |
| 430 | dim = 1; |
| 431 | break; |
| 432 | case GL_TEXTURE_2D: |
| 433 | dim = 2; |
| 434 | break; |
| 435 | case GL_TEXTURE_3D: |
| 436 | dim = 3; |
| 437 | break; |
| 438 | default: |
| 439 | gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" ); |
| 440 | return; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 443 | oldTexObj = texUnit->CurrentD[dim]; |
| 444 | |
| 445 | if (oldTexObj->Name == texName) |
| 446 | return; |
| 447 | |
| 448 | if (texName == 0) |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 449 | newTexObj = ctx->Shared->DefaultD[dim]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 450 | else { |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 451 | struct _mesa_HashTable *hash = ctx->Shared->TexObjects; |
| Brian Paul | c79fab4 | 2000-01-24 20:53:32 +0000 | [diff] [blame] | 452 | newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 453 | |
| 454 | if (!newTexObj) |
| 455 | newTexObj = gl_alloc_texture_object(ctx->Shared, texName, dim); |
| 456 | |
| 457 | if (newTexObj->Dimensions != dim) { |
| 458 | if (newTexObj->Dimensions) { |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 459 | /* the named texture object's dimensions don't match the target */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 460 | gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" ); |
| 461 | return; |
| 462 | } |
| 463 | newTexObj->Dimensions = dim; |
| 464 | } |
| 465 | } |
| 466 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 467 | newTexObj->RefCount++; |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 468 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 469 | texUnit->CurrentD[dim] = newTexObj; |
| 470 | |
| 471 | /* If we've changed the CurrentD[123] texture object then update the |
| 472 | * ctx->Texture.Current pointer to point to the new texture object. |
| 473 | */ |
| 474 | texUnit->Current = texUnit->CurrentD[texUnit->CurrentDimension]; |
| 475 | |
| 476 | /* Check if we may have to use a new triangle rasterizer */ |
| 477 | if ((ctx->IndirectTriangles & DD_SW_RASTERIZE) && |
| 478 | ( oldTexObj->WrapS != newTexObj->WrapS |
| 479 | || oldTexObj->WrapT != newTexObj->WrapT |
| 480 | || oldTexObj->WrapR != newTexObj->WrapR |
| 481 | || oldTexObj->MinFilter != newTexObj->MinFilter |
| 482 | || oldTexObj->MagFilter != newTexObj->MagFilter |
| 483 | || (oldTexObj->Image[0] && newTexObj->Image[0] && |
| 484 | (oldTexObj->Image[0]->Format!=newTexObj->Image[0]->Format)))) |
| 485 | { |
| 486 | ctx->NewState |= (NEW_RASTER_OPS | NEW_TEXTURING); |
| 487 | } |
| 488 | |
| 489 | if (oldTexObj->Complete != newTexObj->Complete) |
| 490 | ctx->NewState |= NEW_TEXTURING; |
| 491 | |
| 492 | /* Pass BindTexture call to device driver */ |
| 493 | if (ctx->Driver.BindTexture) { |
| 494 | (*ctx->Driver.BindTexture)( ctx, target, newTexObj ); |
| 495 | } |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 496 | |
| 497 | if (oldTexObj->Name > 0) { |
| 498 | /* never delete default (id=0) texture objects */ |
| 499 | oldTexObj->RefCount--; |
| 500 | if (oldTexObj->RefCount <= 0) { |
| 501 | if (ctx->Driver.DeleteTexture) { |
| 502 | (*ctx->Driver.DeleteTexture)( ctx, oldTexObj ); |
| 503 | } |
| 504 | gl_free_texture_object(ctx->Shared, oldTexObj); |
| 505 | } |
| 506 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | |
| 510 | |
| 511 | /* |
| 512 | * Execute glPrioritizeTextures |
| 513 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 514 | void |
| 515 | _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName, |
| 516 | const GLclampf *priorities ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 517 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 518 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 519 | GLint i; |
| 520 | |
| 521 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPrioritizeTextures"); |
| 522 | if (n<0) { |
| 523 | gl_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" ); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | for (i=0;i<n;i++) { |
| 528 | struct gl_texture_object *t; |
| 529 | if (texName[i]>0) { |
| 530 | t = (struct gl_texture_object *) |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 531 | _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 532 | if (t) { |
| 533 | t->Priority = CLAMP( priorities[i], 0.0F, 1.0F ); |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 534 | |
| 535 | if (ctx->Driver.PrioritizeTexture) |
| 536 | ctx->Driver.PrioritizeTexture( ctx, t, t->Priority ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | |
| 543 | |
| 544 | /* |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 545 | * Execute glAreTexturesResident |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 546 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 547 | GLboolean |
| 548 | _mesa_AreTexturesResident( GLsizei n, const GLuint *texName, |
| 549 | GLboolean *residences ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 550 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 551 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 552 | GLboolean resident = GL_TRUE; |
| 553 | GLint i; |
| 554 | |
| 555 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, |
| 556 | "glAreTexturesResident", |
| 557 | GL_FALSE); |
| 558 | if (n<0) { |
| 559 | gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)" ); |
| 560 | return GL_FALSE; |
| 561 | } |
| 562 | |
| 563 | for (i=0;i<n;i++) { |
| 564 | struct gl_texture_object *t; |
| 565 | if (texName[i]==0) { |
| 566 | gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)" ); |
| 567 | return GL_FALSE; |
| 568 | } |
| 569 | t = (struct gl_texture_object *) |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 570 | _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 571 | if (t) { |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 572 | if (ctx->Driver.IsTextureResident) |
| 573 | residences[i] = ctx->Driver.IsTextureResident( ctx, t ); |
| 574 | else |
| 575 | residences[i] = GL_TRUE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 576 | } |
| 577 | else { |
| 578 | gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)" ); |
| 579 | return GL_FALSE; |
| 580 | } |
| 581 | } |
| 582 | return resident; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | |
| 587 | /* |
| 588 | * Execute glIsTexture |
| 589 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 590 | GLboolean |
| 591 | _mesa_IsTexture( GLuint texture ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 592 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 593 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 594 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glIsTextures", |
| 595 | GL_FALSE); |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 596 | if (texture>0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture)) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 597 | return GL_TRUE; |
| 598 | } |
| 599 | else { |
| 600 | return GL_FALSE; |
| 601 | } |
| 602 | } |
| 603 | |