| Brian Paul | eb198d2 | 2001-02-26 18:25:25 +0000 | [diff] [blame] | 1 | /* $Id: texobj.c,v 1.41 2001/02/26 18:25:25 brianp Exp $ */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Mesa 3-D graphics library |
| Brian Paul | 6d04725 | 2000-08-02 00:38:25 +0000 | [diff] [blame] | 5 | * Version: 3.5 |
| Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 6 | * |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 7 | * Copyright (C) 1999-2000 Brian Paul All Rights Reserved. |
| Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 8 | * |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 9 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 10 | * copy of this software and associated documentation files (the "Software"), |
| 11 | * to deal in the Software without restriction, including without limitation |
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 | * and/or sell copies of the Software, and to permit persons to whom the |
| 14 | * Software is furnished to do so, subject to the following conditions: |
| Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 15 | * |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 16 | * The above copyright notice and this permission notice shall be included |
| 17 | * in all copies or substantial portions of the Software. |
| Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 18 | * |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 23 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | #ifdef PC_HEADER |
| 29 | #include "all.h" |
| 30 | #else |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 31 | #include "glheader.h" |
| Brian Paul | 4bdcfe5 | 2000-04-17 17:57:04 +0000 | [diff] [blame] | 32 | #include "colortab.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 33 | #include "context.h" |
| 34 | #include "enums.h" |
| 35 | #include "hash.h" |
| Brian Paul | ebb248a | 2000-10-29 18:23:16 +0000 | [diff] [blame] | 36 | #include "macros.h" |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 37 | #include "mem.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 38 | #include "teximage.h" |
| 39 | #include "texstate.h" |
| 40 | #include "texobj.h" |
| Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 41 | #include "mtypes.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * Allocate a new texture object and add it to the linked list of texture |
| 48 | * objects. If name>0 then also insert the new texture object into the hash |
| 49 | * table. |
| 50 | * Input: shared - the shared GL state structure to contain the texture object |
| 51 | * name - integer name for the texture object |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 52 | * dimensions - either 1, 2, 3 or 6 (cube map) |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 53 | * zero is ok for the sake of GenTextures() |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 54 | * Return: pointer to new texture object |
| 55 | */ |
| 56 | struct gl_texture_object * |
| Keith Whitwell | 5c1e7fa | 2001-01-29 20:47:39 +0000 | [diff] [blame] | 57 | _mesa_alloc_texture_object( struct gl_shared_state *shared, |
| 58 | GLuint name, GLuint dimensions ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 59 | { |
| 60 | struct gl_texture_object *obj; |
| 61 | |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 62 | ASSERT(dimensions <= 3 || dimensions == 6); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 63 | |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 64 | obj = CALLOC_STRUCT(gl_texture_object); |
| 65 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 66 | if (obj) { |
| 67 | /* init the non-zero fields */ |
| Brian Paul | e4b684c | 2000-09-12 21:07:40 +0000 | [diff] [blame] | 68 | _glthread_INIT_MUTEX(obj->Mutex); |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 69 | obj->RefCount = 1; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 70 | obj->Name = name; |
| 71 | obj->Dimensions = dimensions; |
| Brian Paul | 6d04725 | 2000-08-02 00:38:25 +0000 | [diff] [blame] | 72 | obj->Priority = 1.0F; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 73 | obj->WrapS = GL_REPEAT; |
| 74 | obj->WrapT = GL_REPEAT; |
| Brian Paul | 7351049 | 2000-11-11 20:23:47 +0000 | [diff] [blame] | 75 | obj->WrapR = GL_REPEAT; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 76 | obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR; |
| 77 | obj->MagFilter = GL_LINEAR; |
| 78 | obj->MinLod = -1000.0; |
| 79 | obj->MaxLod = 1000.0; |
| 80 | obj->BaseLevel = 0; |
| 81 | obj->MaxLevel = 1000; |
| Brian Paul | e75d242 | 2001-02-17 18:41:01 +0000 | [diff] [blame] | 82 | obj->CompareFlag = GL_FALSE; |
| 83 | obj->CompareOperator = GL_TEXTURE_LEQUAL_R_SGIX; |
| Brian Paul | c499ce3 | 2001-02-20 16:42:25 +0000 | [diff] [blame] | 84 | obj->ShadowAmbient = 0; |
| Brian Paul | 4bdcfe5 | 2000-04-17 17:57:04 +0000 | [diff] [blame] | 85 | _mesa_init_colortable(&obj->Palette); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 86 | |
| 87 | /* insert into linked list */ |
| 88 | if (shared) { |
| Brian Paul | 9560f05 | 2000-01-31 23:11:39 +0000 | [diff] [blame] | 89 | _glthread_LOCK_MUTEX(shared->Mutex); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 90 | obj->Next = shared->TexObjectList; |
| 91 | shared->TexObjectList = obj; |
| Brian Paul | 9560f05 | 2000-01-31 23:11:39 +0000 | [diff] [blame] | 92 | _glthread_UNLOCK_MUTEX(shared->Mutex); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if (name > 0) { |
| 96 | /* insert into hash table */ |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 97 | _mesa_HashInsert(shared->TexObjects, name, obj); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | return obj; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /* |
| 105 | * Deallocate a texture object struct and remove it from the given |
| 106 | * shared GL state. |
| 107 | * Input: shared - the shared GL state to which the object belongs |
| 108 | * t - the texture object to delete |
| 109 | */ |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 110 | void _mesa_free_texture_object( struct gl_shared_state *shared, |
| 111 | struct gl_texture_object *t ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 112 | { |
| 113 | struct gl_texture_object *tprev, *tcurr; |
| 114 | |
| 115 | assert(t); |
| 116 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 117 | /* unlink t from the linked list */ |
| 118 | if (shared) { |
| Brian Paul | 9560f05 | 2000-01-31 23:11:39 +0000 | [diff] [blame] | 119 | _glthread_LOCK_MUTEX(shared->Mutex); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 120 | tprev = NULL; |
| 121 | tcurr = shared->TexObjectList; |
| 122 | while (tcurr) { |
| 123 | if (tcurr==t) { |
| 124 | if (tprev) { |
| 125 | tprev->Next = t->Next; |
| 126 | } |
| 127 | else { |
| 128 | shared->TexObjectList = t->Next; |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | tprev = tcurr; |
| 133 | tcurr = tcurr->Next; |
| 134 | } |
| Brian Paul | 9560f05 | 2000-01-31 23:11:39 +0000 | [diff] [blame] | 135 | _glthread_UNLOCK_MUTEX(shared->Mutex); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (t->Name) { |
| 139 | /* remove from hash table */ |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 140 | _mesa_HashRemove(shared->TexObjects, t->Name); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| Brian Paul | 4bdcfe5 | 2000-04-17 17:57:04 +0000 | [diff] [blame] | 143 | _mesa_free_colortable_data(&t->Palette); |
| 144 | |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 145 | /* free the texture images */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 146 | { |
| 147 | GLuint i; |
| 148 | for (i=0;i<MAX_TEXTURE_LEVELS;i++) { |
| 149 | if (t->Image[i]) { |
| Brian Paul | 021a525 | 2000-03-27 17:54:17 +0000 | [diff] [blame] | 150 | _mesa_free_texture_image( t->Image[i] ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 154 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 155 | /* free this object */ |
| Brian Paul | bd5cdaf | 1999-10-13 18:42:49 +0000 | [diff] [blame] | 156 | FREE( t ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * Report why a texture object is incomplete. (for debug only) |
| 162 | */ |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 163 | #if 0 |
| 164 | static void |
| 165 | incomplete(const struct gl_texture_object *t, const char *why) |
| 166 | { |
| 167 | printf("Texture Obj %d incomplete because: %s\n", t->Name, why); |
| 168 | } |
| 169 | #else |
| 170 | #define incomplete(a, b) |
| 171 | #endif |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | /* |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 175 | * Examine a texture object to determine if it is complete. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 176 | * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly. |
| 177 | */ |
| Brian Paul | 35d5301 | 2000-05-23 17:14:49 +0000 | [diff] [blame] | 178 | void |
| 179 | _mesa_test_texobj_completeness( const GLcontext *ctx, |
| 180 | struct gl_texture_object *t ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 181 | { |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 182 | const GLint baseLevel = t->BaseLevel; |
| Keith Whitwell | 5c1e7fa | 2001-01-29 20:47:39 +0000 | [diff] [blame] | 183 | GLint maxLog2 = 0; |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 184 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 185 | t->Complete = GL_TRUE; /* be optimistic */ |
| 186 | |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 187 | /* Always need the base level image */ |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 188 | if (!t->Image[baseLevel]) { |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 189 | incomplete(t, "Image[baseLevel] == NULL"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 190 | t->Complete = GL_FALSE; |
| 191 | return; |
| 192 | } |
| 193 | |
| Brian Paul | 083e466 | 2000-12-14 20:25:56 +0000 | [diff] [blame] | 194 | /* Compute _MaxLevel */ |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 195 | if (t->Dimensions == 1) { |
| Brian Paul | 083e466 | 2000-12-14 20:25:56 +0000 | [diff] [blame] | 196 | maxLog2 = t->Image[baseLevel]->WidthLog2; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 197 | } |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 198 | else if (t->Dimensions == 2 || t->Dimensions == 6) { |
| Brian Paul | 083e466 | 2000-12-14 20:25:56 +0000 | [diff] [blame] | 199 | maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2, |
| 200 | t->Image[baseLevel]->HeightLog2); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 201 | } |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 202 | else if (t->Dimensions == 3) { |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 203 | GLint max = MAX2(t->Image[baseLevel]->WidthLog2, |
| 204 | t->Image[baseLevel]->HeightLog2); |
| Brian Paul | 083e466 | 2000-12-14 20:25:56 +0000 | [diff] [blame] | 205 | maxLog2 = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2)); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| Brian Paul | 083e466 | 2000-12-14 20:25:56 +0000 | [diff] [blame] | 208 | t->_MaxLevel = baseLevel + maxLog2; |
| 209 | t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel); |
| 210 | t->_MaxLevel = MIN2(t->_MaxLevel, ctx->Const.MaxTextureLevels - 1); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 211 | |
| Brian Paul | 083e466 | 2000-12-14 20:25:56 +0000 | [diff] [blame] | 212 | /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */ |
| 213 | t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 214 | |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 215 | if (t->Dimensions == 6) { |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 216 | /* make sure that all six cube map level 0 images are the same size */ |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 217 | const GLint w = t->Image[baseLevel]->Width2; |
| 218 | const GLint h = t->Image[baseLevel]->Height2; |
| 219 | if (!t->NegX[baseLevel] || |
| 220 | t->NegX[baseLevel]->Width2 != w || |
| 221 | t->NegX[baseLevel]->Height2 != h || |
| 222 | !t->PosY[baseLevel] || |
| 223 | t->PosY[baseLevel]->Width2 != w || |
| 224 | t->PosY[baseLevel]->Height2 != h || |
| 225 | !t->NegY[baseLevel] || |
| 226 | t->NegY[baseLevel]->Width2 != w || |
| 227 | t->NegY[baseLevel]->Height2 != h || |
| 228 | !t->PosZ[baseLevel] || |
| 229 | t->PosZ[baseLevel]->Width2 != w || |
| 230 | t->PosZ[baseLevel]->Height2 != h || |
| 231 | !t->NegZ[baseLevel] || |
| 232 | t->NegZ[baseLevel]->Width2 != w || |
| 233 | t->NegZ[baseLevel]->Height2 != h) { |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 234 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 235 | incomplete(t, "Non-quare cubemap image"); |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 241 | /* |
| 242 | * Mipmapping: determine if we have a complete set of mipmaps |
| 243 | */ |
| 244 | GLint i; |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 245 | GLint minLevel = baseLevel; |
| Brian Paul | 083e466 | 2000-12-14 20:25:56 +0000 | [diff] [blame] | 246 | GLint maxLevel = t->_MaxLevel; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 247 | |
| 248 | if (minLevel > maxLevel) { |
| 249 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 250 | incomplete(t, "minLevel > maxLevel"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 251 | return; |
| 252 | } |
| 253 | |
| 254 | /* Test dimension-independent attributes */ |
| 255 | for (i = minLevel; i <= maxLevel; i++) { |
| 256 | if (t->Image[i]) { |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 257 | if (t->Image[i]->Format != t->Image[baseLevel]->Format) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 258 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 259 | incomplete(t, "Format[i] != Format[baseLevel]"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 260 | return; |
| 261 | } |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 262 | if (t->Image[i]->Border != t->Image[baseLevel]->Border) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 263 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 264 | incomplete(t, "Border[i] != Border[baseLevel]"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /* Test things which depend on number of texture image dimensions */ |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 271 | if (t->Dimensions == 1) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 272 | /* Test 1-D mipmaps */ |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 273 | GLuint width = t->Image[baseLevel]->Width2; |
| 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 | } |
| 278 | if (i >= minLevel && i <= maxLevel) { |
| 279 | if (!t->Image[i]) { |
| 280 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 281 | incomplete(t, "1D Image[i] == NULL"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 282 | return; |
| 283 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 284 | if (t->Image[i]->Width2 != width ) { |
| 285 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 286 | incomplete(t, "1D Image[i] bad width"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 287 | return; |
| 288 | } |
| 289 | } |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 290 | if (width == 1) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 291 | return; /* found smallest needed mipmap, all done! */ |
| 292 | } |
| 293 | } |
| 294 | } |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 295 | else if (t->Dimensions == 2) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 296 | /* Test 2-D mipmaps */ |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 297 | GLuint width = t->Image[baseLevel]->Width2; |
| 298 | GLuint height = t->Image[baseLevel]->Height2; |
| 299 | for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) { |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 300 | if (width > 1) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 301 | width /= 2; |
| 302 | } |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 303 | if (height > 1) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 304 | height /= 2; |
| 305 | } |
| 306 | if (i >= minLevel && i <= maxLevel) { |
| 307 | if (!t->Image[i]) { |
| 308 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 309 | incomplete(t, "2D Image[i] == NULL"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | if (t->Image[i]->Width2 != width) { |
| 313 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 314 | incomplete(t, "2D Image[i] bad width"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | if (t->Image[i]->Height2 != height) { |
| 318 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 319 | incomplete(t, "2D Image[i] bad height"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | if (width==1 && height==1) { |
| 323 | return; /* found smallest needed mipmap, all done! */ |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 328 | else if (t->Dimensions == 3) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 329 | /* Test 3-D mipmaps */ |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 330 | GLuint width = t->Image[baseLevel]->Width2; |
| 331 | GLuint height = t->Image[baseLevel]->Height2; |
| 332 | GLuint depth = t->Image[baseLevel]->Depth2; |
| 333 | for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) { |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 334 | if (width > 1) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 335 | width /= 2; |
| 336 | } |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 337 | if (height > 1) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 338 | height /= 2; |
| 339 | } |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 340 | if (depth > 1) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 341 | depth /= 2; |
| 342 | } |
| 343 | if (i >= minLevel && i <= maxLevel) { |
| 344 | if (!t->Image[i]) { |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 345 | incomplete(t, "3D Image[i] == NULL"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 346 | t->Complete = GL_FALSE; |
| 347 | return; |
| 348 | } |
| 349 | if (t->Image[i]->Width2 != width) { |
| 350 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 351 | incomplete(t, "3D Image[i] bad width"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | if (t->Image[i]->Height2 != height) { |
| 355 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 356 | incomplete(t, "3D Image[i] bad height"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | if (t->Image[i]->Depth2 != depth) { |
| 360 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 361 | incomplete(t, "3D Image[i] bad depth"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 362 | return; |
| 363 | } |
| 364 | } |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 365 | if (width == 1 && height == 1 && depth == 1) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 366 | return; /* found smallest needed mipmap, all done! */ |
| 367 | } |
| 368 | } |
| 369 | } |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 370 | else if (t->Dimensions == 6) { |
| 371 | /* make sure 6 cube faces are consistant */ |
| Brian Paul | 63ec423 | 2000-06-12 16:09:49 +0000 | [diff] [blame] | 372 | GLuint width = t->Image[baseLevel]->Width2; |
| 373 | GLuint height = t->Image[baseLevel]->Height2; |
| 374 | for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) { |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 375 | if (width > 1) { |
| 376 | width /= 2; |
| 377 | } |
| 378 | if (height > 1) { |
| 379 | height /= 2; |
| 380 | } |
| 381 | if (i >= minLevel && i <= maxLevel) { |
| 382 | /* check that we have images defined */ |
| 383 | if (!t->Image[i] || !t->NegX[i] || |
| 384 | !t->PosY[i] || !t->NegY[i] || |
| 385 | !t->PosZ[i] || !t->NegZ[i]) { |
| 386 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 387 | incomplete(t, "CubeMap Image[i] == NULL"); |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | /* check that all six images have same size */ |
| 391 | if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height || |
| 392 | t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height || |
| 393 | t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height || |
| 394 | t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height || |
| 395 | t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) { |
| 396 | t->Complete = GL_FALSE; |
| Brian Paul | 21d073d | 2000-10-24 02:53:18 +0000 | [diff] [blame] | 397 | incomplete(t, "CubeMap Image[i] bad size"); |
| Brian Paul | ad81770 | 2000-05-30 00:27:24 +0000 | [diff] [blame] | 398 | return; |
| 399 | } |
| 400 | } |
| 401 | if (width == 1 && height == 1) { |
| 402 | return; /* found smallest needed mipmap, all done! */ |
| 403 | } |
| 404 | } |
| Brian Paul | 413d6a2 | 2000-05-26 14:44:59 +0000 | [diff] [blame] | 405 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 406 | else { |
| 407 | /* Dimensions = ??? */ |
| Brian Paul | eb198d2 | 2001-02-26 18:25:25 +0000 | [diff] [blame] | 408 | gl_problem(ctx, "Bug in gl_test_texture_object_completeness\n"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | |
| Brian Paul | 832179c | 2000-03-21 17:42:27 +0000 | [diff] [blame] | 414 | _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock); |
| 415 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 416 | |
| 417 | /* |
| 418 | * Execute glGenTextures |
| 419 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 420 | void |
| 421 | _mesa_GenTextures( GLsizei n, GLuint *texName ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 422 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 423 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 424 | GLuint first; |
| 425 | GLint i; |
| Keith Whitwell | cab974c | 2000-12-26 05:09:27 +0000 | [diff] [blame] | 426 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 427 | |
| Brian Paul | 507d83e | 2000-08-03 14:03:17 +0000 | [diff] [blame] | 428 | if (n < 0) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 429 | gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" ); |
| 430 | return; |
| 431 | } |
| 432 | |
| Brian Paul | 507d83e | 2000-08-03 14:03:17 +0000 | [diff] [blame] | 433 | if (!texName) |
| 434 | return; |
| Brian Paul | 832179c | 2000-03-21 17:42:27 +0000 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * This must be atomic (generation and allocation of texture IDs) |
| 438 | */ |
| 439 | _glthread_LOCK_MUTEX(GenTexturesLock); |
| 440 | |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 441 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 442 | |
| 443 | /* Return the texture names */ |
| 444 | for (i=0;i<n;i++) { |
| 445 | texName[i] = first + i; |
| 446 | } |
| 447 | |
| 448 | /* Allocate new, empty texture objects */ |
| 449 | for (i=0;i<n;i++) { |
| 450 | GLuint name = first + i; |
| 451 | GLuint dims = 0; |
| Keith Whitwell | 5c1e7fa | 2001-01-29 20:47:39 +0000 | [diff] [blame] | 452 | (void) _mesa_alloc_texture_object( ctx->Shared, name, dims); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 453 | } |
| Brian Paul | 832179c | 2000-03-21 17:42:27 +0000 | [diff] [blame] | 454 | |
| 455 | _glthread_UNLOCK_MUTEX(GenTexturesLock); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
| 459 | |
| 460 | /* |
| 461 | * Execute glDeleteTextures |
| 462 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 463 | void |
| 464 | _mesa_DeleteTextures( GLsizei n, const GLuint *texName) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 465 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 466 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 467 | GLint i; |
| Keith Whitwell | cab974c | 2000-12-26 05:09:27 +0000 | [diff] [blame] | 468 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 469 | |
| Brian Paul | 507d83e | 2000-08-03 14:03:17 +0000 | [diff] [blame] | 470 | if (!texName) |
| 471 | return; |
| 472 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 473 | for (i=0;i<n;i++) { |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 474 | if (texName[i] > 0) { |
| 475 | struct gl_texture_object *delObj = (struct gl_texture_object *) |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 476 | _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]); |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 477 | if (delObj) { |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 478 | /* First check if this texture is currently bound. |
| 479 | * If so, unbind it and decrement the reference count. |
| 480 | */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 481 | GLuint u; |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 482 | for (u = 0; u < MAX_TEXTURE_UNITS; u++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 483 | struct gl_texture_unit *unit = &ctx->Texture.Unit[u]; |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 484 | if (delObj == unit->Current1D) { |
| 485 | unit->Current1D = ctx->Shared->Default1D; |
| 486 | ctx->Shared->Default1D->RefCount++; |
| 487 | } |
| 488 | else if (delObj == unit->Current2D) { |
| 489 | unit->Current2D = ctx->Shared->Default2D; |
| 490 | ctx->Shared->Default2D->RefCount++; |
| 491 | } |
| 492 | else if (delObj == unit->Current3D) { |
| 493 | unit->Current3D = ctx->Shared->Default3D; |
| 494 | ctx->Shared->Default3D->RefCount++; |
| 495 | } |
| 496 | else if (delObj == unit->CurrentCubeMap) { |
| 497 | unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap; |
| 498 | ctx->Shared->DefaultCubeMap->RefCount++; |
| 499 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 500 | } |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 501 | ctx->NewState |= _NEW_TEXTURE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 502 | |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 503 | /* Decrement reference count and delete if zero */ |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 504 | delObj->RefCount--; |
| 505 | ASSERT( delObj->RefCount >= 0 ); |
| 506 | if (delObj->RefCount == 0) { |
| Brian Paul | 59d6da5 | 2000-02-12 01:59:19 +0000 | [diff] [blame] | 507 | if (ctx->Driver.DeleteTexture) |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 508 | (*ctx->Driver.DeleteTexture)( ctx, delObj ); |
| 509 | _mesa_free_texture_object(ctx->Shared, delObj); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | |
| 517 | |
| 518 | /* |
| 519 | * Execute glBindTexture |
| 520 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 521 | void |
| 522 | _mesa_BindTexture( GLenum target, GLuint texName ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 523 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 524 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 525 | GLuint unit = ctx->Texture.CurrentUnit; |
| 526 | struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; |
| 527 | struct gl_texture_object *oldTexObj; |
| Keith Whitwell | 5c1e7fa | 2001-01-29 20:47:39 +0000 | [diff] [blame] | 528 | struct gl_texture_object *newTexObj = 0; |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 529 | GLuint targetDim; |
| Keith Whitwell | cab974c | 2000-12-26 05:09:27 +0000 | [diff] [blame] | 530 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 531 | |
| 532 | if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) |
| 533 | fprintf(stderr, "glBindTexture %s %d\n", |
| 534 | gl_lookup_enum_by_nr(target), (GLint) texName); |
| 535 | |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 536 | switch (target) { |
| 537 | case GL_TEXTURE_1D: |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 538 | targetDim = 1; |
| 539 | oldTexObj = texUnit->Current1D; |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 540 | break; |
| 541 | case GL_TEXTURE_2D: |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 542 | targetDim = 2; |
| 543 | oldTexObj = texUnit->Current2D; |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 544 | break; |
| 545 | case GL_TEXTURE_3D: |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 546 | targetDim = 3; |
| 547 | oldTexObj = texUnit->Current3D; |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 548 | break; |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 549 | case GL_TEXTURE_CUBE_MAP_ARB: |
| Keith Whitwell | a96308c | 2000-10-30 13:31:59 +0000 | [diff] [blame] | 550 | if (ctx->Extensions.ARB_texture_cube_map) { |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 551 | targetDim = 6; |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 552 | oldTexObj = texUnit->CurrentCubeMap; |
| 553 | break; |
| 554 | } |
| 555 | /* fallthrough */ |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 556 | default: |
| 557 | gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" ); |
| 558 | return; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 561 | if (oldTexObj->Name == texName) |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 562 | return; /* rebinding the same texture- no change */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 563 | |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 564 | /* |
| 565 | * Get pointer to new texture object (newTexObj) |
| 566 | */ |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 567 | if (texName == 0) { |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 568 | /* newTexObj = a default texture object */ |
| 569 | switch (target) { |
| 570 | case GL_TEXTURE_1D: |
| 571 | newTexObj = ctx->Shared->Default1D; |
| 572 | break; |
| 573 | case GL_TEXTURE_2D: |
| 574 | newTexObj = ctx->Shared->Default2D; |
| 575 | break; |
| 576 | case GL_TEXTURE_3D: |
| 577 | newTexObj = ctx->Shared->Default3D; |
| 578 | break; |
| 579 | case GL_TEXTURE_CUBE_MAP_ARB: |
| 580 | newTexObj = ctx->Shared->DefaultCubeMap; |
| 581 | break; |
| 582 | default: |
| 583 | ; /* Bad targets are caught above */ |
| 584 | } |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 585 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 586 | else { |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 587 | /* non-default texture object */ |
| 588 | const struct _mesa_HashTable *hash = ctx->Shared->TexObjects; |
| Brian Paul | c79fab4 | 2000-01-24 20:53:32 +0000 | [diff] [blame] | 589 | newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName); |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 590 | if (newTexObj) { |
| 591 | /* error checking */ |
| 592 | if (newTexObj->Dimensions > 0 && newTexObj->Dimensions != targetDim) { |
| Brian Paul | 420ef64 | 1999-12-01 21:10:08 +0000 | [diff] [blame] | 593 | /* the named texture object's dimensions don't match the target */ |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 594 | gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" ); |
| 595 | return; |
| 596 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 597 | } |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 598 | else { |
| 599 | /* if this is a new texture id, allocate a texture object now */ |
| Keith Whitwell | 5c1e7fa | 2001-01-29 20:47:39 +0000 | [diff] [blame] | 600 | newTexObj = _mesa_alloc_texture_object( ctx->Shared, texName, |
| 601 | targetDim); |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 602 | if (!newTexObj) { |
| 603 | gl_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture"); |
| 604 | return; |
| 605 | } |
| 606 | } |
| 607 | newTexObj->Dimensions = targetDim; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 610 | newTexObj->RefCount++; |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 611 | |
| Keith Whitwell | cab974c | 2000-12-26 05:09:27 +0000 | [diff] [blame] | 612 | |
| 613 | /* do the actual binding, but first flush outstanding vertices: |
| 614 | */ |
| 615 | FLUSH_VERTICES(ctx, _NEW_TEXTURE); |
| 616 | |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 617 | switch (target) { |
| 618 | case GL_TEXTURE_1D: |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 619 | texUnit->Current1D = newTexObj; |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 620 | break; |
| 621 | case GL_TEXTURE_2D: |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 622 | texUnit->Current2D = newTexObj; |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 623 | break; |
| 624 | case GL_TEXTURE_3D: |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 625 | texUnit->Current3D = newTexObj; |
| Brian Paul | fc4b443 | 2000-05-23 15:17:12 +0000 | [diff] [blame] | 626 | break; |
| 627 | case GL_TEXTURE_CUBE_MAP_ARB: |
| 628 | texUnit->CurrentCubeMap = newTexObj; |
| 629 | break; |
| 630 | default: |
| 631 | gl_problem(ctx, "bad target in BindTexture"); |
| 632 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 633 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 634 | /* Pass BindTexture call to device driver */ |
| Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 635 | if (ctx->Driver.BindTexture) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 636 | (*ctx->Driver.BindTexture)( ctx, target, newTexObj ); |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 637 | |
| 638 | if (oldTexObj->Name > 0) { |
| 639 | /* never delete default (id=0) texture objects */ |
| 640 | oldTexObj->RefCount--; |
| 641 | if (oldTexObj->RefCount <= 0) { |
| 642 | if (ctx->Driver.DeleteTexture) { |
| 643 | (*ctx->Driver.DeleteTexture)( ctx, oldTexObj ); |
| 644 | } |
| Brian Paul | a852378 | 2000-11-19 23:10:25 +0000 | [diff] [blame] | 645 | _mesa_free_texture_object(ctx->Shared, oldTexObj); |
| Brian Paul | 6e6d4c6 | 1999-10-09 20:17:07 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | |
| 651 | |
| 652 | /* |
| 653 | * Execute glPrioritizeTextures |
| 654 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 655 | void |
| 656 | _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName, |
| 657 | const GLclampf *priorities ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 658 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 659 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 660 | GLint i; |
| Keith Whitwell | cab974c | 2000-12-26 05:09:27 +0000 | [diff] [blame] | 661 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 662 | |
| Brian Paul | 6d04725 | 2000-08-02 00:38:25 +0000 | [diff] [blame] | 663 | if (n < 0) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 664 | gl_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" ); |
| 665 | return; |
| 666 | } |
| 667 | |
| Brian Paul | 507d83e | 2000-08-03 14:03:17 +0000 | [diff] [blame] | 668 | if (!priorities) |
| 669 | return; |
| 670 | |
| Brian Paul | 6d04725 | 2000-08-02 00:38:25 +0000 | [diff] [blame] | 671 | for (i = 0; i < n; i++) { |
| 672 | if (texName[i] > 0) { |
| 673 | struct gl_texture_object *t = (struct gl_texture_object *) |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 674 | _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 675 | if (t) { |
| 676 | t->Priority = CLAMP( priorities[i], 0.0F, 1.0F ); |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 677 | if (ctx->Driver.PrioritizeTexture) |
| 678 | ctx->Driver.PrioritizeTexture( ctx, t, t->Priority ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
| Keith Whitwell | a96308c | 2000-10-30 13:31:59 +0000 | [diff] [blame] | 682 | |
| 683 | ctx->NewState |= _NEW_TEXTURE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | |
| 687 | |
| 688 | /* |
| Jouk Jansen | 5e3bc0c | 2000-11-22 07:32:16 +0000 | [diff] [blame] | 689 | * Execute glAreTexturesResident |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 690 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 691 | GLboolean |
| Brian Paul | bd0f7f4 | 2000-08-02 20:16:03 +0000 | [diff] [blame] | 692 | _mesa_AreTexturesResident(GLsizei n, const GLuint *texName, |
| 693 | GLboolean *residences) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 694 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 695 | GET_CURRENT_CONTEXT(ctx); |
| Brian Paul | bd0f7f4 | 2000-08-02 20:16:03 +0000 | [diff] [blame] | 696 | GLboolean allResident = GL_TRUE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 697 | GLint i; |
| Keith Whitwell | cab974c | 2000-12-26 05:09:27 +0000 | [diff] [blame] | 698 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 699 | |
| Brian Paul | bd0f7f4 | 2000-08-02 20:16:03 +0000 | [diff] [blame] | 700 | if (n < 0) { |
| 701 | gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 702 | return GL_FALSE; |
| 703 | } |
| 704 | |
| Brian Paul | 507d83e | 2000-08-03 14:03:17 +0000 | [diff] [blame] | 705 | if (!texName || !residences) |
| 706 | return GL_FALSE; |
| 707 | |
| Brian Paul | bd0f7f4 | 2000-08-02 20:16:03 +0000 | [diff] [blame] | 708 | for (i = 0; i < n; i++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 709 | struct gl_texture_object *t; |
| Brian Paul | bd0f7f4 | 2000-08-02 20:16:03 +0000 | [diff] [blame] | 710 | if (texName[i] == 0) { |
| 711 | gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 712 | return GL_FALSE; |
| 713 | } |
| 714 | t = (struct gl_texture_object *) |
| Brian Paul | bb79790 | 2000-01-24 16:19:54 +0000 | [diff] [blame] | 715 | _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 716 | if (t) { |
| Brian Paul | bd0f7f4 | 2000-08-02 20:16:03 +0000 | [diff] [blame] | 717 | if (ctx->Driver.IsTextureResident) { |
| 718 | residences[i] = ctx->Driver.IsTextureResident(ctx, t); |
| 719 | if (!residences[i]) |
| 720 | allResident = GL_FALSE; |
| 721 | } |
| 722 | else { |
| Keith Whitwell | 69cfdb2 | 1999-09-30 11:18:21 +0000 | [diff] [blame] | 723 | residences[i] = GL_TRUE; |
| Brian Paul | bd0f7f4 | 2000-08-02 20:16:03 +0000 | [diff] [blame] | 724 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 725 | } |
| 726 | else { |
| Brian Paul | bd0f7f4 | 2000-08-02 20:16:03 +0000 | [diff] [blame] | 727 | gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)"); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 728 | return GL_FALSE; |
| 729 | } |
| 730 | } |
| Brian Paul | bd0f7f4 | 2000-08-02 20:16:03 +0000 | [diff] [blame] | 731 | return allResident; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | |
| 735 | |
| 736 | /* |
| 737 | * Execute glIsTexture |
| 738 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 739 | GLboolean |
| 740 | _mesa_IsTexture( GLuint texture ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 741 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 742 | GET_CURRENT_CONTEXT(ctx); |
| Keith Whitwell | cab974c | 2000-12-26 05:09:27 +0000 | [diff] [blame] | 743 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 744 | return texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 745 | } |
| 746 | |