| 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 |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 50 | * dimensions - either 1, 2, 3 or 6 (cube map) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 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 | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 59 | ASSERT(dimensions <= 3 || dimensions == 6); |
| 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 | */ |
| Brian Paul | 35d5301 | 2000-05-23 17:14:49 +0000 | [diff] [blame] | 162 | void |
| 163 | _mesa_test_texobj_completeness( const GLcontext *ctx, |
| 164 | struct gl_texture_object *t ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 165 | { |
| 166 | t->Complete = GL_TRUE; /* be optimistic */ |
| 167 | |
| 168 | /* Always need level zero image */ |
| Brian Paul | 4827179 | 2000-03-29 18:13:59 +0000 | [diff] [blame] | 169 | if (!t->Image[0]) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 170 | t->Complete = GL_FALSE; |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | /* Compute number of mipmap levels */ |
| 175 | if (t->Dimensions==1) { |
| 176 | t->P = t->Image[0]->WidthLog2; |
| 177 | } |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 178 | else if (t->Dimensions == 2 || t->Dimensions == 6) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 179 | t->P = MAX2(t->Image[0]->WidthLog2, t->Image[0]->HeightLog2); |
| 180 | } |
| 181 | else if (t->Dimensions==3) { |
| 182 | GLint max = MAX2(t->Image[0]->WidthLog2, t->Image[0]->HeightLog2); |
| 183 | max = MAX2(max, (GLint)(t->Image[0]->DepthLog2)); |
| 184 | t->P = max; |
| 185 | } |
| 186 | |
| 187 | /* Compute M (see the 1.2 spec) used during mipmapping */ |
| 188 | t->M = (GLfloat) (MIN2(t->MaxLevel, t->P) - t->BaseLevel); |
| 189 | |
| 190 | |
| 191 | if (t->MinFilter!=GL_NEAREST && t->MinFilter!=GL_LINEAR) { |
| 192 | /* |
| 193 | * Mipmapping: determine if we have a complete set of mipmaps |
| 194 | */ |
| 195 | GLint i; |
| 196 | GLint minLevel = t->BaseLevel; |
| 197 | GLint maxLevel = MIN2(t->P, ctx->Const.MaxTextureLevels-1); |
| 198 | maxLevel = MIN2(maxLevel, t->MaxLevel); |
| 199 | |
| 200 | if (minLevel > maxLevel) { |
| 201 | t->Complete = GL_FALSE; |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | /* Test dimension-independent attributes */ |
| 206 | for (i = minLevel; i <= maxLevel; i++) { |
| 207 | if (t->Image[i]) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 208 | if (t->Image[i]->Format != t->Image[0]->Format) { |
| 209 | t->Complete = GL_FALSE; |
| 210 | return; |
| 211 | } |
| 212 | if (t->Image[i]->Border != t->Image[0]->Border) { |
| 213 | t->Complete = GL_FALSE; |
| 214 | return; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* Test things which depend on number of texture image dimensions */ |
| 220 | if (t->Dimensions==1) { |
| 221 | /* Test 1-D mipmaps */ |
| 222 | GLuint width = t->Image[0]->Width2; |
| 223 | for (i=1; i<ctx->Const.MaxTextureLevels; i++) { |
| 224 | if (width>1) { |
| 225 | width /= 2; |
| 226 | } |
| 227 | if (i >= minLevel && i <= maxLevel) { |
| 228 | if (!t->Image[i]) { |
| 229 | t->Complete = GL_FALSE; |
| 230 | return; |
| 231 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 232 | if (t->Image[i]->Width2 != width ) { |
| 233 | t->Complete = GL_FALSE; |
| 234 | return; |
| 235 | } |
| 236 | } |
| 237 | if (width==1) { |
| 238 | return; /* found smallest needed mipmap, all done! */ |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | else if (t->Dimensions==2) { |
| 243 | /* Test 2-D mipmaps */ |
| 244 | GLuint width = t->Image[0]->Width2; |
| 245 | GLuint height = t->Image[0]->Height2; |
| 246 | for (i=1; i<ctx->Const.MaxTextureLevels; i++) { |
| 247 | if (width>1) { |
| 248 | width /= 2; |
| 249 | } |
| 250 | if (height>1) { |
| 251 | height /= 2; |
| 252 | } |
| 253 | if (i >= minLevel && i <= maxLevel) { |
| 254 | if (!t->Image[i]) { |
| 255 | t->Complete = GL_FALSE; |
| 256 | return; |
| 257 | } |
| 258 | if (t->Image[i]->Width2 != width) { |
| 259 | t->Complete = GL_FALSE; |
| 260 | return; |
| 261 | } |
| 262 | if (t->Image[i]->Height2 != height) { |
| 263 | t->Complete = GL_FALSE; |
| 264 | return; |
| 265 | } |
| 266 | if (width==1 && height==1) { |
| 267 | return; /* found smallest needed mipmap, all done! */ |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | else if (t->Dimensions==3) { |
| 273 | /* Test 3-D mipmaps */ |
| 274 | GLuint width = t->Image[0]->Width2; |
| 275 | GLuint height = t->Image[0]->Height2; |
| 276 | GLuint depth = t->Image[0]->Depth2; |
| 277 | for (i=1; i<ctx->Const.MaxTextureLevels; i++) { |
| 278 | if (width>1) { |
| 279 | width /= 2; |
| 280 | } |
| 281 | if (height>1) { |
| 282 | height /= 2; |
| 283 | } |
| 284 | if (depth>1) { |
| 285 | depth /= 2; |
| 286 | } |
| 287 | if (i >= minLevel && i <= maxLevel) { |
| 288 | if (!t->Image[i]) { |
| 289 | t->Complete = GL_FALSE; |
| 290 | return; |
| 291 | } |
| 292 | if (t->Image[i]->Width2 != width) { |
| 293 | t->Complete = GL_FALSE; |
| 294 | return; |
| 295 | } |
| 296 | if (t->Image[i]->Height2 != height) { |
| 297 | t->Complete = GL_FALSE; |
| 298 | return; |
| 299 | } |
| 300 | if (t->Image[i]->Depth2 != depth) { |
| 301 | t->Complete = GL_FALSE; |
| 302 | return; |
| 303 | } |
| 304 | } |
| 305 | if (width==1 && height==1 && depth==1) { |
| 306 | return; /* found smallest needed mipmap, all done! */ |
| 307 | } |
| 308 | } |
| 309 | } |
| Brian Paul | 413d6a2 | 2000-05-26 14:44:59 +0000 | [diff] [blame^] | 310 | else if (t->Dimensions == 6) { /* cube map */ |
| 311 | |
| 312 | |
| 313 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 314 | else { |
| 315 | /* Dimensions = ??? */ |
| 316 | gl_problem(NULL, "Bug in gl_test_texture_object_completeness\n"); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | |
| Brian Paul | 832179c | 2000-03-21 17:42:27 +0000 | [diff] [blame] | 322 | _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock); |
| 323 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 324 | |
| 325 | /* |
| 326 | * Execute glGenTextures |
| 327 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 328 | void |
| 329 | _mesa_GenTextures( GLsizei n, GLuint *texName ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 330 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 331 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 332 | GLuint first; |
| 333 | GLint i; |
| 334 | |
| 335 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGenTextures"); |
| 336 | if (n<0) { |
| 337 | gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" ); |
| 338 | return; |
| 339 | } |
| 340 | |
| Brian Paul | 832179c | 2000-03-21 17:42:27 +0000 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * This must be atomic (generation and allocation of texture IDs) |
| 344 | */ |
| 345 | _glthread_LOCK_MUTEX(GenTexturesLock); |
| 346 | |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 347 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 348 | |
| 349 | /* Return the texture names */ |
| 350 | for (i=0;i<n;i++) { |
| 351 | texName[i] = first + i; |
| 352 | } |
| 353 | |
| 354 | /* Allocate new, empty texture objects */ |
| 355 | for (i=0;i<n;i++) { |
| 356 | GLuint name = first + i; |
| 357 | GLuint dims = 0; |
| 358 | (void) gl_alloc_texture_object(ctx->Shared, name, dims); |
| 359 | } |
| Brian Paul | 832179c | 2000-03-21 17:42:27 +0000 | [diff] [blame] | 360 | |
| 361 | _glthread_UNLOCK_MUTEX(GenTexturesLock); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
| 365 | |
| 366 | /* |
| 367 | * Execute glDeleteTextures |
| 368 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 369 | void |
| 370 | _mesa_DeleteTextures( GLsizei n, const GLuint *texName) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 371 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 372 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 373 | GLint i; |
| 374 | |
| 375 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDeleteTextures"); |
| 376 | |
| 377 | for (i=0;i<n;i++) { |
| 378 | struct gl_texture_object *t; |
| 379 | if (texName[i]>0) { |
| 380 | t = (struct gl_texture_object *) |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 381 | _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 382 | if (t) { |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 383 | /* First check if this texture is currently bound. |
| 384 | * If so, unbind it and decrement the reference count. |
| 385 | */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 386 | GLuint u; |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 387 | for (u = 0; u < MAX_TEXTURE_UNITS; u++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 388 | struct gl_texture_unit *unit = &ctx->Texture.Unit[u]; |
| 389 | GLuint d; |
| 390 | for (d = 1 ; d <= 3 ; d++) { |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 391 | if (unit->CurrentD[d] == t) { |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 392 | unit->CurrentD[d] = ctx->Shared->DefaultD[d]; |
| 393 | ctx->Shared->DefaultD[d]->RefCount++; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 394 | t->RefCount--; |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 395 | ASSERT( t->RefCount >= 0 ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 400 | /* Decrement reference count and delete if zero */ |
| 401 | t->RefCount--; |
| 402 | ASSERT( t->RefCount >= 0 ); |
| 403 | if (t->RefCount == 0) { |
| 404 | if (ctx->Driver.DeleteTexture) |
| 405 | (*ctx->Driver.DeleteTexture)( ctx, t ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 406 | gl_free_texture_object(ctx->Shared, t); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | |
| 414 | |
| 415 | /* |
| 416 | * Execute glBindTexture |
| 417 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 418 | void |
| 419 | _mesa_BindTexture( GLenum target, GLuint texName ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 420 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 421 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 422 | GLuint unit = ctx->Texture.CurrentUnit; |
| 423 | struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; |
| 424 | struct gl_texture_object *oldTexObj; |
| 425 | struct gl_texture_object *newTexObj; |
| Brian Paul | 5b37c32 | 1999-11-05 06:43:10 +0000 | [diff] [blame] | 426 | GLuint dim; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 427 | |
| 428 | if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) |
| 429 | fprintf(stderr, "glBindTexture %s %d\n", |
| 430 | gl_lookup_enum_by_nr(target), (GLint) texName); |
| 431 | |
| 432 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glBindTexture"); |
| 433 | |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 434 | switch (target) { |
| 435 | case GL_TEXTURE_1D: |
| 436 | dim = 1; |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 437 | oldTexObj = texUnit->CurrentD[1]; |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 438 | break; |
| 439 | case GL_TEXTURE_2D: |
| 440 | dim = 2; |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 441 | oldTexObj = texUnit->CurrentD[2]; |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 442 | break; |
| 443 | case GL_TEXTURE_3D: |
| 444 | dim = 3; |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 445 | oldTexObj = texUnit->CurrentD[3]; |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 446 | break; |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 447 | case GL_TEXTURE_CUBE_MAP_ARB: |
| 448 | if (ctx->Extensions.HaveTextureCubeMap) { |
| 449 | dim = 6; |
| 450 | oldTexObj = texUnit->CurrentCubeMap; |
| 451 | break; |
| 452 | } |
| 453 | /* fallthrough */ |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 454 | default: |
| 455 | gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" ); |
| 456 | return; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 459 | if (oldTexObj->Name == texName) |
| 460 | return; |
| 461 | |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 462 | if (texName == 0) { |
| 463 | if (target == GL_TEXTURE_CUBE_MAP_ARB) |
| 464 | newTexObj = ctx->Shared->DefaultCubeMap; |
| 465 | else |
| 466 | newTexObj = ctx->Shared->DefaultD[dim]; |
| 467 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 468 | else { |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 469 | struct _mesa_HashTable *hash = ctx->Shared->TexObjects; |
| Brian Paul | c79fab4 | 2000-01-24 20:53:32 +0000 | [diff] [blame] | 470 | newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 471 | |
| 472 | if (!newTexObj) |
| 473 | newTexObj = gl_alloc_texture_object(ctx->Shared, texName, dim); |
| 474 | |
| 475 | if (newTexObj->Dimensions != dim) { |
| 476 | if (newTexObj->Dimensions) { |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 477 | /* the named texture object's dimensions don't match the target */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 478 | gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" ); |
| 479 | return; |
| 480 | } |
| 481 | newTexObj->Dimensions = dim; |
| 482 | } |
| 483 | } |
| 484 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 485 | newTexObj->RefCount++; |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 486 | |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 487 | switch (target) { |
| 488 | case GL_TEXTURE_1D: |
| 489 | texUnit->CurrentD[1] = newTexObj; |
| 490 | break; |
| 491 | case GL_TEXTURE_2D: |
| 492 | texUnit->CurrentD[2] = newTexObj; |
| 493 | break; |
| 494 | case GL_TEXTURE_3D: |
| 495 | texUnit->CurrentD[3] = newTexObj; |
| 496 | break; |
| 497 | case GL_TEXTURE_CUBE_MAP_ARB: |
| 498 | texUnit->CurrentCubeMap = newTexObj; |
| 499 | break; |
| 500 | default: |
| 501 | gl_problem(ctx, "bad target in BindTexture"); |
| 502 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 503 | |
| 504 | /* If we've changed the CurrentD[123] texture object then update the |
| 505 | * ctx->Texture.Current pointer to point to the new texture object. |
| 506 | */ |
| 507 | texUnit->Current = texUnit->CurrentD[texUnit->CurrentDimension]; |
| 508 | |
| 509 | /* Check if we may have to use a new triangle rasterizer */ |
| 510 | if ((ctx->IndirectTriangles & DD_SW_RASTERIZE) && |
| 511 | ( oldTexObj->WrapS != newTexObj->WrapS |
| 512 | || oldTexObj->WrapT != newTexObj->WrapT |
| 513 | || oldTexObj->WrapR != newTexObj->WrapR |
| 514 | || oldTexObj->MinFilter != newTexObj->MinFilter |
| 515 | || oldTexObj->MagFilter != newTexObj->MagFilter |
| 516 | || (oldTexObj->Image[0] && newTexObj->Image[0] && |
| 517 | (oldTexObj->Image[0]->Format!=newTexObj->Image[0]->Format)))) |
| 518 | { |
| 519 | ctx->NewState |= (NEW_RASTER_OPS | NEW_TEXTURING); |
| 520 | } |
| 521 | |
| 522 | if (oldTexObj->Complete != newTexObj->Complete) |
| 523 | ctx->NewState |= NEW_TEXTURING; |
| 524 | |
| 525 | /* Pass BindTexture call to device driver */ |
| 526 | if (ctx->Driver.BindTexture) { |
| 527 | (*ctx->Driver.BindTexture)( ctx, target, newTexObj ); |
| 528 | } |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 529 | |
| 530 | if (oldTexObj->Name > 0) { |
| 531 | /* never delete default (id=0) texture objects */ |
| 532 | oldTexObj->RefCount--; |
| 533 | if (oldTexObj->RefCount <= 0) { |
| 534 | if (ctx->Driver.DeleteTexture) { |
| 535 | (*ctx->Driver.DeleteTexture)( ctx, oldTexObj ); |
| 536 | } |
| 537 | gl_free_texture_object(ctx->Shared, oldTexObj); |
| 538 | } |
| 539 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
| 543 | |
| 544 | /* |
| 545 | * Execute glPrioritizeTextures |
| 546 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 547 | void |
| 548 | _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName, |
| 549 | const GLclampf *priorities ) |
| 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 | GLint i; |
| 553 | |
| 554 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPrioritizeTextures"); |
| 555 | if (n<0) { |
| 556 | gl_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" ); |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | for (i=0;i<n;i++) { |
| 561 | struct gl_texture_object *t; |
| 562 | if (texName[i]>0) { |
| 563 | t = (struct gl_texture_object *) |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 564 | _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 565 | if (t) { |
| 566 | t->Priority = CLAMP( priorities[i], 0.0F, 1.0F ); |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 567 | |
| 568 | if (ctx->Driver.PrioritizeTexture) |
| 569 | ctx->Driver.PrioritizeTexture( ctx, t, t->Priority ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | |
| 576 | |
| 577 | /* |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 578 | * Execute glAreTexturesResident |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 579 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 580 | GLboolean |
| 581 | _mesa_AreTexturesResident( GLsizei n, const GLuint *texName, |
| 582 | GLboolean *residences ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 583 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 584 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 585 | GLboolean resident = GL_TRUE; |
| 586 | GLint i; |
| 587 | |
| 588 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, |
| 589 | "glAreTexturesResident", |
| 590 | GL_FALSE); |
| 591 | if (n<0) { |
| 592 | gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)" ); |
| 593 | return GL_FALSE; |
| 594 | } |
| 595 | |
| 596 | for (i=0;i<n;i++) { |
| 597 | struct gl_texture_object *t; |
| 598 | if (texName[i]==0) { |
| 599 | gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)" ); |
| 600 | return GL_FALSE; |
| 601 | } |
| 602 | t = (struct gl_texture_object *) |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 603 | _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 604 | if (t) { |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 605 | if (ctx->Driver.IsTextureResident) |
| 606 | residences[i] = ctx->Driver.IsTextureResident( ctx, t ); |
| 607 | else |
| 608 | residences[i] = GL_TRUE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 609 | } |
| 610 | else { |
| 611 | gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)" ); |
| 612 | return GL_FALSE; |
| 613 | } |
| 614 | } |
| 615 | return resident; |
| 616 | } |
| 617 | |
| 618 | |
| 619 | |
| 620 | /* |
| 621 | * Execute glIsTexture |
| 622 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 623 | GLboolean |
| 624 | _mesa_IsTexture( GLuint texture ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 625 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 626 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 627 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glIsTextures", |
| 628 | GL_FALSE); |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 629 | if (texture>0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture)) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 630 | return GL_TRUE; |
| 631 | } |
| 632 | else { |
| 633 | return GL_FALSE; |
| 634 | } |
| 635 | } |
| 636 | |