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