blob: 95427855fba019bd52e2981068b6d058d257aac1 [file] [log] [blame]
Brian Paule75d2422001-02-17 18:41:01 +00001/* $Id: texobj.c,v 1.39 2001/02/17 18:41:01 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paul6d047252000-08-02 00:38:25 +00005 * Version: 3.5
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00006 *
Brian Paulbb797902000-01-24 16:19:54 +00007 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00008 *
jtgafb833d1999-08-19 00:55:39 +00009 * 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 Jansen5e3bc0c2000-11-22 07:32:16 +000015 *
jtgafb833d1999-08-19 00:55:39 +000016 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000018 *
jtgafb833d1999-08-19 00:55:39 +000019 * 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 */
jtgafb833d1999-08-19 00:55:39 +000026
27
28#ifdef PC_HEADER
29#include "all.h"
30#else
Brian Paulfbd8f211999-11-11 01:22:25 +000031#include "glheader.h"
Brian Paul4bdcfe52000-04-17 17:57:04 +000032#include "colortab.h"
jtgafb833d1999-08-19 00:55:39 +000033#include "context.h"
34#include "enums.h"
35#include "hash.h"
Brian Paulebb248a2000-10-29 18:23:16 +000036#include "macros.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000037#include "mem.h"
jtgafb833d1999-08-19 00:55:39 +000038#include "teximage.h"
39#include "texstate.h"
40#include "texobj.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000041#include "mtypes.h"
jtgafb833d1999-08-19 00:55:39 +000042#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 Paulfc4b4432000-05-23 15:17:12 +000052 * dimensions - either 1, 2, 3 or 6 (cube map)
Brian Paula8523782000-11-19 23:10:25 +000053 * zero is ok for the sake of GenTextures()
jtgafb833d1999-08-19 00:55:39 +000054 * Return: pointer to new texture object
55 */
56struct gl_texture_object *
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +000057_mesa_alloc_texture_object( struct gl_shared_state *shared,
58 GLuint name, GLuint dimensions )
jtgafb833d1999-08-19 00:55:39 +000059{
60 struct gl_texture_object *obj;
61
Brian Paulfc4b4432000-05-23 15:17:12 +000062 ASSERT(dimensions <= 3 || dimensions == 6);
jtgafb833d1999-08-19 00:55:39 +000063
Brian Paul420ef641999-12-01 21:10:08 +000064 obj = CALLOC_STRUCT(gl_texture_object);
65
jtgafb833d1999-08-19 00:55:39 +000066 if (obj) {
67 /* init the non-zero fields */
Brian Paule4b684c2000-09-12 21:07:40 +000068 _glthread_INIT_MUTEX(obj->Mutex);
Brian Paul6e6d4c61999-10-09 20:17:07 +000069 obj->RefCount = 1;
jtgafb833d1999-08-19 00:55:39 +000070 obj->Name = name;
71 obj->Dimensions = dimensions;
Brian Paul6d047252000-08-02 00:38:25 +000072 obj->Priority = 1.0F;
jtgafb833d1999-08-19 00:55:39 +000073 obj->WrapS = GL_REPEAT;
74 obj->WrapT = GL_REPEAT;
Brian Paul73510492000-11-11 20:23:47 +000075 obj->WrapR = GL_REPEAT;
jtgafb833d1999-08-19 00:55:39 +000076 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 Paule75d2422001-02-17 18:41:01 +000082 obj->CompareFlag = GL_FALSE;
83 obj->CompareOperator = GL_TEXTURE_LEQUAL_R_SGIX;
Brian Paul4bdcfe52000-04-17 17:57:04 +000084 _mesa_init_colortable(&obj->Palette);
jtgafb833d1999-08-19 00:55:39 +000085
86 /* insert into linked list */
87 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +000088 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000089 obj->Next = shared->TexObjectList;
90 shared->TexObjectList = obj;
Brian Paul9560f052000-01-31 23:11:39 +000091 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000092 }
93
94 if (name > 0) {
95 /* insert into hash table */
Brian Paulbb797902000-01-24 16:19:54 +000096 _mesa_HashInsert(shared->TexObjects, name, obj);
jtgafb833d1999-08-19 00:55:39 +000097 }
98 }
99 return obj;
100}
101
102
103/*
104 * Deallocate a texture object struct and remove it from the given
105 * shared GL state.
106 * Input: shared - the shared GL state to which the object belongs
107 * t - the texture object to delete
108 */
Brian Paula8523782000-11-19 23:10:25 +0000109void _mesa_free_texture_object( struct gl_shared_state *shared,
110 struct gl_texture_object *t )
jtgafb833d1999-08-19 00:55:39 +0000111{
112 struct gl_texture_object *tprev, *tcurr;
113
114 assert(t);
115
jtgafb833d1999-08-19 00:55:39 +0000116 /* unlink t from the linked list */
117 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +0000118 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000119 tprev = NULL;
120 tcurr = shared->TexObjectList;
121 while (tcurr) {
122 if (tcurr==t) {
123 if (tprev) {
124 tprev->Next = t->Next;
125 }
126 else {
127 shared->TexObjectList = t->Next;
128 }
129 break;
130 }
131 tprev = tcurr;
132 tcurr = tcurr->Next;
133 }
Brian Paul9560f052000-01-31 23:11:39 +0000134 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000135 }
136
137 if (t->Name) {
138 /* remove from hash table */
Brian Paulbb797902000-01-24 16:19:54 +0000139 _mesa_HashRemove(shared->TexObjects, t->Name);
jtgafb833d1999-08-19 00:55:39 +0000140 }
141
Brian Paul4bdcfe52000-04-17 17:57:04 +0000142 _mesa_free_colortable_data(&t->Palette);
143
Brian Paula8523782000-11-19 23:10:25 +0000144 /* free the texture images */
jtgafb833d1999-08-19 00:55:39 +0000145 {
146 GLuint i;
147 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
148 if (t->Image[i]) {
Brian Paul021a5252000-03-27 17:54:17 +0000149 _mesa_free_texture_image( t->Image[i] );
jtgafb833d1999-08-19 00:55:39 +0000150 }
151 }
152 }
Brian Paula8523782000-11-19 23:10:25 +0000153
jtgafb833d1999-08-19 00:55:39 +0000154 /* free this object */
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000155 FREE( t );
jtgafb833d1999-08-19 00:55:39 +0000156}
157
Brian Paula8523782000-11-19 23:10:25 +0000158
159/*
160 * Report why a texture object is incomplete. (for debug only)
161 */
Brian Paul21d073d2000-10-24 02:53:18 +0000162#if 0
163static void
164incomplete(const struct gl_texture_object *t, const char *why)
165{
166 printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
167}
168#else
169#define incomplete(a, b)
170#endif
jtgafb833d1999-08-19 00:55:39 +0000171
172
173/*
Brian Paula8523782000-11-19 23:10:25 +0000174 * Examine a texture object to determine if it is complete.
jtgafb833d1999-08-19 00:55:39 +0000175 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
176 */
Brian Paul35d53012000-05-23 17:14:49 +0000177void
178_mesa_test_texobj_completeness( const GLcontext *ctx,
179 struct gl_texture_object *t )
jtgafb833d1999-08-19 00:55:39 +0000180{
Brian Paul63ec4232000-06-12 16:09:49 +0000181 const GLint baseLevel = t->BaseLevel;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000182 GLint maxLog2 = 0;
Brian Paul63ec4232000-06-12 16:09:49 +0000183
jtgafb833d1999-08-19 00:55:39 +0000184 t->Complete = GL_TRUE; /* be optimistic */
185
Brian Paula8523782000-11-19 23:10:25 +0000186 /* Always need the base level image */
Brian Paul63ec4232000-06-12 16:09:49 +0000187 if (!t->Image[baseLevel]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000188 incomplete(t, "Image[baseLevel] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000189 t->Complete = GL_FALSE;
190 return;
191 }
192
Brian Paul083e4662000-12-14 20:25:56 +0000193 /* Compute _MaxLevel */
Brian Paulad817702000-05-30 00:27:24 +0000194 if (t->Dimensions == 1) {
Brian Paul083e4662000-12-14 20:25:56 +0000195 maxLog2 = t->Image[baseLevel]->WidthLog2;
jtgafb833d1999-08-19 00:55:39 +0000196 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000197 else if (t->Dimensions == 2 || t->Dimensions == 6) {
Brian Paul083e4662000-12-14 20:25:56 +0000198 maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2,
199 t->Image[baseLevel]->HeightLog2);
jtgafb833d1999-08-19 00:55:39 +0000200 }
Brian Paulad817702000-05-30 00:27:24 +0000201 else if (t->Dimensions == 3) {
Brian Paul63ec4232000-06-12 16:09:49 +0000202 GLint max = MAX2(t->Image[baseLevel]->WidthLog2,
203 t->Image[baseLevel]->HeightLog2);
Brian Paul083e4662000-12-14 20:25:56 +0000204 maxLog2 = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2));
jtgafb833d1999-08-19 00:55:39 +0000205 }
206
Brian Paul083e4662000-12-14 20:25:56 +0000207 t->_MaxLevel = baseLevel + maxLog2;
208 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
209 t->_MaxLevel = MIN2(t->_MaxLevel, ctx->Const.MaxTextureLevels - 1);
jtgafb833d1999-08-19 00:55:39 +0000210
Brian Paul083e4662000-12-14 20:25:56 +0000211 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
212 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
jtgafb833d1999-08-19 00:55:39 +0000213
Brian Paulad817702000-05-30 00:27:24 +0000214 if (t->Dimensions == 6) {
Brian Paula8523782000-11-19 23:10:25 +0000215 /* make sure that all six cube map level 0 images are the same size */
Brian Paul63ec4232000-06-12 16:09:49 +0000216 const GLint w = t->Image[baseLevel]->Width2;
217 const GLint h = t->Image[baseLevel]->Height2;
218 if (!t->NegX[baseLevel] ||
219 t->NegX[baseLevel]->Width2 != w ||
220 t->NegX[baseLevel]->Height2 != h ||
221 !t->PosY[baseLevel] ||
222 t->PosY[baseLevel]->Width2 != w ||
223 t->PosY[baseLevel]->Height2 != h ||
224 !t->NegY[baseLevel] ||
225 t->NegY[baseLevel]->Width2 != w ||
226 t->NegY[baseLevel]->Height2 != h ||
227 !t->PosZ[baseLevel] ||
228 t->PosZ[baseLevel]->Width2 != w ||
229 t->PosZ[baseLevel]->Height2 != h ||
230 !t->NegZ[baseLevel] ||
231 t->NegZ[baseLevel]->Width2 != w ||
232 t->NegZ[baseLevel]->Height2 != h) {
Brian Paulad817702000-05-30 00:27:24 +0000233 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000234 incomplete(t, "Non-quare cubemap image");
Brian Paulad817702000-05-30 00:27:24 +0000235 return;
236 }
237 }
238
239 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
jtgafb833d1999-08-19 00:55:39 +0000240 /*
241 * Mipmapping: determine if we have a complete set of mipmaps
242 */
243 GLint i;
Brian Paul63ec4232000-06-12 16:09:49 +0000244 GLint minLevel = baseLevel;
Brian Paul083e4662000-12-14 20:25:56 +0000245 GLint maxLevel = t->_MaxLevel;
jtgafb833d1999-08-19 00:55:39 +0000246
247 if (minLevel > maxLevel) {
248 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000249 incomplete(t, "minLevel > maxLevel");
jtgafb833d1999-08-19 00:55:39 +0000250 return;
251 }
252
253 /* Test dimension-independent attributes */
254 for (i = minLevel; i <= maxLevel; i++) {
255 if (t->Image[i]) {
Brian Paul63ec4232000-06-12 16:09:49 +0000256 if (t->Image[i]->Format != t->Image[baseLevel]->Format) {
jtgafb833d1999-08-19 00:55:39 +0000257 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000258 incomplete(t, "Format[i] != Format[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000259 return;
260 }
Brian Paul63ec4232000-06-12 16:09:49 +0000261 if (t->Image[i]->Border != t->Image[baseLevel]->Border) {
jtgafb833d1999-08-19 00:55:39 +0000262 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000263 incomplete(t, "Border[i] != Border[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000264 return;
265 }
266 }
267 }
268
269 /* Test things which depend on number of texture image dimensions */
Brian Paulad817702000-05-30 00:27:24 +0000270 if (t->Dimensions == 1) {
jtgafb833d1999-08-19 00:55:39 +0000271 /* Test 1-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000272 GLuint width = t->Image[baseLevel]->Width2;
273 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000274 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000275 width /= 2;
276 }
277 if (i >= minLevel && i <= maxLevel) {
278 if (!t->Image[i]) {
279 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000280 incomplete(t, "1D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000281 return;
282 }
jtgafb833d1999-08-19 00:55:39 +0000283 if (t->Image[i]->Width2 != width ) {
284 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000285 incomplete(t, "1D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000286 return;
287 }
288 }
Brian Paulad817702000-05-30 00:27:24 +0000289 if (width == 1) {
jtgafb833d1999-08-19 00:55:39 +0000290 return; /* found smallest needed mipmap, all done! */
291 }
292 }
293 }
Brian Paulad817702000-05-30 00:27:24 +0000294 else if (t->Dimensions == 2) {
jtgafb833d1999-08-19 00:55:39 +0000295 /* Test 2-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000296 GLuint width = t->Image[baseLevel]->Width2;
297 GLuint height = t->Image[baseLevel]->Height2;
298 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000299 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000300 width /= 2;
301 }
Brian Paulad817702000-05-30 00:27:24 +0000302 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000303 height /= 2;
304 }
305 if (i >= minLevel && i <= maxLevel) {
306 if (!t->Image[i]) {
307 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000308 incomplete(t, "2D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000309 return;
310 }
311 if (t->Image[i]->Width2 != width) {
312 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000313 incomplete(t, "2D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000314 return;
315 }
316 if (t->Image[i]->Height2 != height) {
317 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000318 incomplete(t, "2D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000319 return;
320 }
321 if (width==1 && height==1) {
322 return; /* found smallest needed mipmap, all done! */
323 }
324 }
325 }
326 }
Brian Paulad817702000-05-30 00:27:24 +0000327 else if (t->Dimensions == 3) {
jtgafb833d1999-08-19 00:55:39 +0000328 /* Test 3-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000329 GLuint width = t->Image[baseLevel]->Width2;
330 GLuint height = t->Image[baseLevel]->Height2;
331 GLuint depth = t->Image[baseLevel]->Depth2;
332 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000333 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000334 width /= 2;
335 }
Brian Paulad817702000-05-30 00:27:24 +0000336 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000337 height /= 2;
338 }
Brian Paulad817702000-05-30 00:27:24 +0000339 if (depth > 1) {
jtgafb833d1999-08-19 00:55:39 +0000340 depth /= 2;
341 }
342 if (i >= minLevel && i <= maxLevel) {
343 if (!t->Image[i]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000344 incomplete(t, "3D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000345 t->Complete = GL_FALSE;
346 return;
347 }
348 if (t->Image[i]->Width2 != width) {
349 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000350 incomplete(t, "3D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000351 return;
352 }
353 if (t->Image[i]->Height2 != height) {
354 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000355 incomplete(t, "3D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000356 return;
357 }
358 if (t->Image[i]->Depth2 != depth) {
359 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000360 incomplete(t, "3D Image[i] bad depth");
jtgafb833d1999-08-19 00:55:39 +0000361 return;
362 }
363 }
Brian Paulad817702000-05-30 00:27:24 +0000364 if (width == 1 && height == 1 && depth == 1) {
jtgafb833d1999-08-19 00:55:39 +0000365 return; /* found smallest needed mipmap, all done! */
366 }
367 }
368 }
Brian Paulad817702000-05-30 00:27:24 +0000369 else if (t->Dimensions == 6) {
370 /* make sure 6 cube faces are consistant */
Brian Paul63ec4232000-06-12 16:09:49 +0000371 GLuint width = t->Image[baseLevel]->Width2;
372 GLuint height = t->Image[baseLevel]->Height2;
373 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000374 if (width > 1) {
375 width /= 2;
376 }
377 if (height > 1) {
378 height /= 2;
379 }
380 if (i >= minLevel && i <= maxLevel) {
381 /* check that we have images defined */
382 if (!t->Image[i] || !t->NegX[i] ||
383 !t->PosY[i] || !t->NegY[i] ||
384 !t->PosZ[i] || !t->NegZ[i]) {
385 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000386 incomplete(t, "CubeMap Image[i] == NULL");
Brian Paulad817702000-05-30 00:27:24 +0000387 return;
388 }
389 /* check that all six images have same size */
390 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
391 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
392 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
393 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
394 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
395 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000396 incomplete(t, "CubeMap Image[i] bad size");
Brian Paulad817702000-05-30 00:27:24 +0000397 return;
398 }
399 }
400 if (width == 1 && height == 1) {
401 return; /* found smallest needed mipmap, all done! */
402 }
403 }
Brian Paul413d6a22000-05-26 14:44:59 +0000404 }
jtgafb833d1999-08-19 00:55:39 +0000405 else {
406 /* Dimensions = ??? */
407 gl_problem(NULL, "Bug in gl_test_texture_object_completeness\n");
408 }
409 }
410}
411
412
Brian Paul832179c2000-03-21 17:42:27 +0000413_glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
414
jtgafb833d1999-08-19 00:55:39 +0000415
416/*
417 * Execute glGenTextures
418 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000419void
420_mesa_GenTextures( GLsizei n, GLuint *texName )
jtgafb833d1999-08-19 00:55:39 +0000421{
Brian Paulfbd8f211999-11-11 01:22:25 +0000422 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000423 GLuint first;
424 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000425 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000426
Brian Paul507d83e2000-08-03 14:03:17 +0000427 if (n < 0) {
jtgafb833d1999-08-19 00:55:39 +0000428 gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
429 return;
430 }
431
Brian Paul507d83e2000-08-03 14:03:17 +0000432 if (!texName)
433 return;
Brian Paul832179c2000-03-21 17:42:27 +0000434
435 /*
436 * This must be atomic (generation and allocation of texture IDs)
437 */
438 _glthread_LOCK_MUTEX(GenTexturesLock);
439
Brian Paulbb797902000-01-24 16:19:54 +0000440 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
jtgafb833d1999-08-19 00:55:39 +0000441
442 /* Return the texture names */
443 for (i=0;i<n;i++) {
444 texName[i] = first + i;
445 }
446
447 /* Allocate new, empty texture objects */
448 for (i=0;i<n;i++) {
449 GLuint name = first + i;
450 GLuint dims = 0;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000451 (void) _mesa_alloc_texture_object( ctx->Shared, name, dims);
jtgafb833d1999-08-19 00:55:39 +0000452 }
Brian Paul832179c2000-03-21 17:42:27 +0000453
454 _glthread_UNLOCK_MUTEX(GenTexturesLock);
jtgafb833d1999-08-19 00:55:39 +0000455}
456
457
458
459/*
460 * Execute glDeleteTextures
461 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000462void
463_mesa_DeleteTextures( GLsizei n, const GLuint *texName)
jtgafb833d1999-08-19 00:55:39 +0000464{
Brian Paulfbd8f211999-11-11 01:22:25 +0000465 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000466 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000467 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
jtgafb833d1999-08-19 00:55:39 +0000468
Brian Paul507d83e2000-08-03 14:03:17 +0000469 if (!texName)
470 return;
471
jtgafb833d1999-08-19 00:55:39 +0000472 for (i=0;i<n;i++) {
Brian Paula8523782000-11-19 23:10:25 +0000473 if (texName[i] > 0) {
474 struct gl_texture_object *delObj = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000475 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
Brian Paula8523782000-11-19 23:10:25 +0000476 if (delObj) {
Brian Paul59d6da52000-02-12 01:59:19 +0000477 /* First check if this texture is currently bound.
478 * If so, unbind it and decrement the reference count.
479 */
jtgafb833d1999-08-19 00:55:39 +0000480 GLuint u;
Brian Paul59d6da52000-02-12 01:59:19 +0000481 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
jtgafb833d1999-08-19 00:55:39 +0000482 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
Brian Paula8523782000-11-19 23:10:25 +0000483 if (delObj == unit->Current1D) {
484 unit->Current1D = ctx->Shared->Default1D;
485 ctx->Shared->Default1D->RefCount++;
486 }
487 else if (delObj == unit->Current2D) {
488 unit->Current2D = ctx->Shared->Default2D;
489 ctx->Shared->Default2D->RefCount++;
490 }
491 else if (delObj == unit->Current3D) {
492 unit->Current3D = ctx->Shared->Default3D;
493 ctx->Shared->Default3D->RefCount++;
494 }
495 else if (delObj == unit->CurrentCubeMap) {
496 unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
497 ctx->Shared->DefaultCubeMap->RefCount++;
498 }
jtgafb833d1999-08-19 00:55:39 +0000499 }
Brian Paula8523782000-11-19 23:10:25 +0000500 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +0000501
Brian Paul59d6da52000-02-12 01:59:19 +0000502 /* Decrement reference count and delete if zero */
Brian Paula8523782000-11-19 23:10:25 +0000503 delObj->RefCount--;
504 ASSERT( delObj->RefCount >= 0 );
505 if (delObj->RefCount == 0) {
Brian Paul59d6da52000-02-12 01:59:19 +0000506 if (ctx->Driver.DeleteTexture)
Brian Paula8523782000-11-19 23:10:25 +0000507 (*ctx->Driver.DeleteTexture)( ctx, delObj );
508 _mesa_free_texture_object(ctx->Shared, delObj);
jtgafb833d1999-08-19 00:55:39 +0000509 }
510 }
511 }
512 }
513}
514
515
516
517/*
518 * Execute glBindTexture
519 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000520void
521_mesa_BindTexture( GLenum target, GLuint texName )
jtgafb833d1999-08-19 00:55:39 +0000522{
Brian Paulfbd8f211999-11-11 01:22:25 +0000523 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000524 GLuint unit = ctx->Texture.CurrentUnit;
525 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
526 struct gl_texture_object *oldTexObj;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000527 struct gl_texture_object *newTexObj = 0;
Brian Paula8523782000-11-19 23:10:25 +0000528 GLuint targetDim;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000529 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000530
531 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
532 fprintf(stderr, "glBindTexture %s %d\n",
533 gl_lookup_enum_by_nr(target), (GLint) texName);
534
Brian Paul420ef641999-12-01 21:10:08 +0000535 switch (target) {
536 case GL_TEXTURE_1D:
Brian Paula8523782000-11-19 23:10:25 +0000537 targetDim = 1;
538 oldTexObj = texUnit->Current1D;
Brian Paul420ef641999-12-01 21:10:08 +0000539 break;
540 case GL_TEXTURE_2D:
Brian Paula8523782000-11-19 23:10:25 +0000541 targetDim = 2;
542 oldTexObj = texUnit->Current2D;
Brian Paul420ef641999-12-01 21:10:08 +0000543 break;
544 case GL_TEXTURE_3D:
Brian Paula8523782000-11-19 23:10:25 +0000545 targetDim = 3;
546 oldTexObj = texUnit->Current3D;
Brian Paul420ef641999-12-01 21:10:08 +0000547 break;
Brian Paulfc4b4432000-05-23 15:17:12 +0000548 case GL_TEXTURE_CUBE_MAP_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000549 if (ctx->Extensions.ARB_texture_cube_map) {
Brian Paula8523782000-11-19 23:10:25 +0000550 targetDim = 6;
Brian Paulfc4b4432000-05-23 15:17:12 +0000551 oldTexObj = texUnit->CurrentCubeMap;
552 break;
553 }
554 /* fallthrough */
Brian Paul420ef641999-12-01 21:10:08 +0000555 default:
556 gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
557 return;
jtgafb833d1999-08-19 00:55:39 +0000558 }
559
jtgafb833d1999-08-19 00:55:39 +0000560 if (oldTexObj->Name == texName)
Brian Paula8523782000-11-19 23:10:25 +0000561 return; /* rebinding the same texture- no change */
jtgafb833d1999-08-19 00:55:39 +0000562
Brian Paula8523782000-11-19 23:10:25 +0000563 /*
564 * Get pointer to new texture object (newTexObj)
565 */
Brian Paulfc4b4432000-05-23 15:17:12 +0000566 if (texName == 0) {
Brian Paula8523782000-11-19 23:10:25 +0000567 /* newTexObj = a default texture object */
568 switch (target) {
569 case GL_TEXTURE_1D:
570 newTexObj = ctx->Shared->Default1D;
571 break;
572 case GL_TEXTURE_2D:
573 newTexObj = ctx->Shared->Default2D;
574 break;
575 case GL_TEXTURE_3D:
576 newTexObj = ctx->Shared->Default3D;
577 break;
578 case GL_TEXTURE_CUBE_MAP_ARB:
579 newTexObj = ctx->Shared->DefaultCubeMap;
580 break;
581 default:
582 ; /* Bad targets are caught above */
583 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000584 }
jtgafb833d1999-08-19 00:55:39 +0000585 else {
Brian Paula8523782000-11-19 23:10:25 +0000586 /* non-default texture object */
587 const struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
Brian Paulc79fab42000-01-24 20:53:32 +0000588 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
Brian Paula8523782000-11-19 23:10:25 +0000589 if (newTexObj) {
590 /* error checking */
591 if (newTexObj->Dimensions > 0 && newTexObj->Dimensions != targetDim) {
Brian Paul420ef641999-12-01 21:10:08 +0000592 /* the named texture object's dimensions don't match the target */
Brian Paula8523782000-11-19 23:10:25 +0000593 gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" );
594 return;
595 }
jtgafb833d1999-08-19 00:55:39 +0000596 }
Brian Paula8523782000-11-19 23:10:25 +0000597 else {
598 /* if this is a new texture id, allocate a texture object now */
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000599 newTexObj = _mesa_alloc_texture_object( ctx->Shared, texName,
600 targetDim);
Brian Paula8523782000-11-19 23:10:25 +0000601 if (!newTexObj) {
602 gl_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
603 return;
604 }
605 }
606 newTexObj->Dimensions = targetDim;
jtgafb833d1999-08-19 00:55:39 +0000607 }
608
jtgafb833d1999-08-19 00:55:39 +0000609 newTexObj->RefCount++;
Brian Paul6e6d4c61999-10-09 20:17:07 +0000610
Keith Whitwellcab974c2000-12-26 05:09:27 +0000611
612 /* do the actual binding, but first flush outstanding vertices:
613 */
614 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
615
Brian Paulfc4b4432000-05-23 15:17:12 +0000616 switch (target) {
617 case GL_TEXTURE_1D:
Brian Paula8523782000-11-19 23:10:25 +0000618 texUnit->Current1D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000619 break;
620 case GL_TEXTURE_2D:
Brian Paula8523782000-11-19 23:10:25 +0000621 texUnit->Current2D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000622 break;
623 case GL_TEXTURE_3D:
Brian Paula8523782000-11-19 23:10:25 +0000624 texUnit->Current3D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000625 break;
626 case GL_TEXTURE_CUBE_MAP_ARB:
627 texUnit->CurrentCubeMap = newTexObj;
628 break;
629 default:
630 gl_problem(ctx, "bad target in BindTexture");
631 }
jtgafb833d1999-08-19 00:55:39 +0000632
jtgafb833d1999-08-19 00:55:39 +0000633 /* Pass BindTexture call to device driver */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000634 if (ctx->Driver.BindTexture)
jtgafb833d1999-08-19 00:55:39 +0000635 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
Brian Paul6e6d4c61999-10-09 20:17:07 +0000636
637 if (oldTexObj->Name > 0) {
638 /* never delete default (id=0) texture objects */
639 oldTexObj->RefCount--;
640 if (oldTexObj->RefCount <= 0) {
641 if (ctx->Driver.DeleteTexture) {
642 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
643 }
Brian Paula8523782000-11-19 23:10:25 +0000644 _mesa_free_texture_object(ctx->Shared, oldTexObj);
Brian Paul6e6d4c61999-10-09 20:17:07 +0000645 }
646 }
jtgafb833d1999-08-19 00:55:39 +0000647}
648
649
650
651/*
652 * Execute glPrioritizeTextures
653 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000654void
655_mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
656 const GLclampf *priorities )
jtgafb833d1999-08-19 00:55:39 +0000657{
Brian Paulfbd8f211999-11-11 01:22:25 +0000658 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000659 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000660 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +0000661
Brian Paul6d047252000-08-02 00:38:25 +0000662 if (n < 0) {
jtgafb833d1999-08-19 00:55:39 +0000663 gl_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
664 return;
665 }
666
Brian Paul507d83e2000-08-03 14:03:17 +0000667 if (!priorities)
668 return;
669
Brian Paul6d047252000-08-02 00:38:25 +0000670 for (i = 0; i < n; i++) {
671 if (texName[i] > 0) {
672 struct gl_texture_object *t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000673 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000674 if (t) {
675 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000676 if (ctx->Driver.PrioritizeTexture)
677 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
jtgafb833d1999-08-19 00:55:39 +0000678 }
679 }
680 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000681
682 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +0000683}
684
685
686
687/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000688 * Execute glAreTexturesResident
jtgafb833d1999-08-19 00:55:39 +0000689 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000690GLboolean
Brian Paulbd0f7f42000-08-02 20:16:03 +0000691_mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
692 GLboolean *residences)
jtgafb833d1999-08-19 00:55:39 +0000693{
Brian Paulfbd8f211999-11-11 01:22:25 +0000694 GET_CURRENT_CONTEXT(ctx);
Brian Paulbd0f7f42000-08-02 20:16:03 +0000695 GLboolean allResident = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000696 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000697 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
jtgafb833d1999-08-19 00:55:39 +0000698
Brian Paulbd0f7f42000-08-02 20:16:03 +0000699 if (n < 0) {
700 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
jtgafb833d1999-08-19 00:55:39 +0000701 return GL_FALSE;
702 }
703
Brian Paul507d83e2000-08-03 14:03:17 +0000704 if (!texName || !residences)
705 return GL_FALSE;
706
Brian Paulbd0f7f42000-08-02 20:16:03 +0000707 for (i = 0; i < n; i++) {
jtgafb833d1999-08-19 00:55:39 +0000708 struct gl_texture_object *t;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000709 if (texName[i] == 0) {
710 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000711 return GL_FALSE;
712 }
713 t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000714 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000715 if (t) {
Brian Paulbd0f7f42000-08-02 20:16:03 +0000716 if (ctx->Driver.IsTextureResident) {
717 residences[i] = ctx->Driver.IsTextureResident(ctx, t);
718 if (!residences[i])
719 allResident = GL_FALSE;
720 }
721 else {
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000722 residences[i] = GL_TRUE;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000723 }
jtgafb833d1999-08-19 00:55:39 +0000724 }
725 else {
Brian Paulbd0f7f42000-08-02 20:16:03 +0000726 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000727 return GL_FALSE;
728 }
729 }
Brian Paulbd0f7f42000-08-02 20:16:03 +0000730 return allResident;
jtgafb833d1999-08-19 00:55:39 +0000731}
732
733
734
735/*
736 * Execute glIsTexture
737 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000738GLboolean
739_mesa_IsTexture( GLuint texture )
jtgafb833d1999-08-19 00:55:39 +0000740{
Brian Paulfbd8f211999-11-11 01:22:25 +0000741 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000742 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
743 return texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture);
jtgafb833d1999-08-19 00:55:39 +0000744}
745