blob: 36a63f63ac5df09b4ba890391d1b910e40eea5d8 [file] [log] [blame]
Brian Paul21d073d2000-10-24 02:53:18 +00001/* $Id: texobj.c,v 1.29 2000/10/24 02:53:18 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
jtgafb833d1999-08-19 00:55:39 +00006 *
Brian Paulbb797902000-01-24 16:19:54 +00007 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
jtgafb833d1999-08-19 00:55:39 +00008 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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 */
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 Paulfbd8f211999-11-11 01:22:25 +000036#include "mem.h"
jtgafb833d1999-08-19 00:55:39 +000037#include "teximage.h"
38#include "texstate.h"
39#include "texobj.h"
40#include "types.h"
jtgafb833d1999-08-19 00:55:39 +000041#endif
42
43
44
45/*
46 * Allocate a new texture object and add it to the linked list of texture
47 * objects. If name>0 then also insert the new texture object into the hash
48 * table.
49 * Input: shared - the shared GL state structure to contain the texture object
50 * name - integer name for the texture object
Brian Paulfc4b4432000-05-23 15:17:12 +000051 * dimensions - either 1, 2, 3 or 6 (cube map)
jtgafb833d1999-08-19 00:55:39 +000052 * Return: pointer to new texture object
53 */
54struct gl_texture_object *
55gl_alloc_texture_object( struct gl_shared_state *shared, GLuint name,
56 GLuint dimensions)
57{
58 struct gl_texture_object *obj;
59
Brian Paulfc4b4432000-05-23 15:17:12 +000060 ASSERT(dimensions <= 3 || dimensions == 6);
jtgafb833d1999-08-19 00:55:39 +000061
Brian Paul420ef641999-12-01 21:10:08 +000062 obj = CALLOC_STRUCT(gl_texture_object);
63
jtgafb833d1999-08-19 00:55:39 +000064 if (obj) {
65 /* init the non-zero fields */
Brian Paule4b684c2000-09-12 21:07:40 +000066 _glthread_INIT_MUTEX(obj->Mutex);
Brian Paul6e6d4c61999-10-09 20:17:07 +000067 obj->RefCount = 1;
jtgafb833d1999-08-19 00:55:39 +000068 obj->Name = name;
69 obj->Dimensions = dimensions;
Brian Paul6d047252000-08-02 00:38:25 +000070 obj->Priority = 1.0F;
jtgafb833d1999-08-19 00:55:39 +000071 obj->WrapS = GL_REPEAT;
72 obj->WrapT = GL_REPEAT;
73 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
74 obj->MagFilter = GL_LINEAR;
75 obj->MinLod = -1000.0;
76 obj->MaxLod = 1000.0;
77 obj->BaseLevel = 0;
78 obj->MaxLevel = 1000;
79 obj->MinMagThresh = 0.0F;
Brian Paul4bdcfe52000-04-17 17:57:04 +000080 _mesa_init_colortable(&obj->Palette);
jtgafb833d1999-08-19 00:55:39 +000081
82 /* insert into linked list */
83 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +000084 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000085 obj->Next = shared->TexObjectList;
86 shared->TexObjectList = obj;
Brian Paul9560f052000-01-31 23:11:39 +000087 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000088 }
89
90 if (name > 0) {
91 /* insert into hash table */
Brian Paulbb797902000-01-24 16:19:54 +000092 _mesa_HashInsert(shared->TexObjects, name, obj);
jtgafb833d1999-08-19 00:55:39 +000093 }
94 }
95 return obj;
96}
97
98
99/*
100 * Deallocate a texture object struct and remove it from the given
101 * shared GL state.
102 * Input: shared - the shared GL state to which the object belongs
103 * t - the texture object to delete
104 */
105void gl_free_texture_object( struct gl_shared_state *shared,
106 struct gl_texture_object *t )
107{
108 struct gl_texture_object *tprev, *tcurr;
109
110 assert(t);
111
112 /* Remove t from dirty list so we don't touch free'd memory later.
113 * Test for shared since Proxy texture aren't in global linked list.
114 */
115 if (shared)
116 gl_remove_texobj_from_dirty_list( shared, t );
117
118 /* unlink t from the linked list */
119 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +0000120 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000121 tprev = NULL;
122 tcurr = shared->TexObjectList;
123 while (tcurr) {
124 if (tcurr==t) {
125 if (tprev) {
126 tprev->Next = t->Next;
127 }
128 else {
129 shared->TexObjectList = t->Next;
130 }
131 break;
132 }
133 tprev = tcurr;
134 tcurr = tcurr->Next;
135 }
Brian Paul9560f052000-01-31 23:11:39 +0000136 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000137 }
138
139 if (t->Name) {
140 /* remove from hash table */
Brian Paulbb797902000-01-24 16:19:54 +0000141 _mesa_HashRemove(shared->TexObjects, t->Name);
jtgafb833d1999-08-19 00:55:39 +0000142 }
143
Brian Paul4bdcfe52000-04-17 17:57:04 +0000144 _mesa_free_colortable_data(&t->Palette);
145
146 /* free texture images */
jtgafb833d1999-08-19 00:55:39 +0000147 {
148 GLuint i;
149 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
150 if (t->Image[i]) {
Brian Paul021a5252000-03-27 17:54:17 +0000151 _mesa_free_texture_image( t->Image[i] );
jtgafb833d1999-08-19 00:55:39 +0000152 }
153 }
154 }
155 /* free this object */
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000156 FREE( t );
jtgafb833d1999-08-19 00:55:39 +0000157}
158
Brian Paul21d073d2000-10-24 02:53:18 +0000159#if 0
160static void
161incomplete(const struct gl_texture_object *t, const char *why)
162{
163 printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
164}
165#else
166#define incomplete(a, b)
167#endif
jtgafb833d1999-08-19 00:55:39 +0000168
169
170/*
171 * Examine a texture object to determine if it is complete or not.
172 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
173 */
Brian Paul35d53012000-05-23 17:14:49 +0000174void
175_mesa_test_texobj_completeness( const GLcontext *ctx,
176 struct gl_texture_object *t )
jtgafb833d1999-08-19 00:55:39 +0000177{
Brian Paul63ec4232000-06-12 16:09:49 +0000178 const GLint baseLevel = t->BaseLevel;
179
jtgafb833d1999-08-19 00:55:39 +0000180 t->Complete = GL_TRUE; /* be optimistic */
181
182 /* Always need level zero image */
Brian Paul63ec4232000-06-12 16:09:49 +0000183 if (!t->Image[baseLevel]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000184 incomplete(t, "Image[baseLevel] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000185 t->Complete = GL_FALSE;
186 return;
187 }
188
189 /* Compute number of mipmap levels */
Brian Paulad817702000-05-30 00:27:24 +0000190 if (t->Dimensions == 1) {
Brian Paul63ec4232000-06-12 16:09:49 +0000191 t->P = t->Image[baseLevel]->WidthLog2;
jtgafb833d1999-08-19 00:55:39 +0000192 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000193 else if (t->Dimensions == 2 || t->Dimensions == 6) {
Brian Paul63ec4232000-06-12 16:09:49 +0000194 t->P = MAX2(t->Image[baseLevel]->WidthLog2,
195 t->Image[baseLevel]->HeightLog2);
jtgafb833d1999-08-19 00:55:39 +0000196 }
Brian Paulad817702000-05-30 00:27:24 +0000197 else if (t->Dimensions == 3) {
Brian Paul63ec4232000-06-12 16:09:49 +0000198 GLint max = MAX2(t->Image[baseLevel]->WidthLog2,
199 t->Image[baseLevel]->HeightLog2);
200 max = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2));
jtgafb833d1999-08-19 00:55:39 +0000201 t->P = max;
202 }
203
204 /* Compute M (see the 1.2 spec) used during mipmapping */
205 t->M = (GLfloat) (MIN2(t->MaxLevel, t->P) - t->BaseLevel);
206
207
Brian Paulad817702000-05-30 00:27:24 +0000208 if (t->Dimensions == 6) {
209 /* make sure all six level 0 images are same size */
Brian Paul63ec4232000-06-12 16:09:49 +0000210 const GLint w = t->Image[baseLevel]->Width2;
211 const GLint h = t->Image[baseLevel]->Height2;
212 if (!t->NegX[baseLevel] ||
213 t->NegX[baseLevel]->Width2 != w ||
214 t->NegX[baseLevel]->Height2 != h ||
215 !t->PosY[baseLevel] ||
216 t->PosY[baseLevel]->Width2 != w ||
217 t->PosY[baseLevel]->Height2 != h ||
218 !t->NegY[baseLevel] ||
219 t->NegY[baseLevel]->Width2 != w ||
220 t->NegY[baseLevel]->Height2 != h ||
221 !t->PosZ[baseLevel] ||
222 t->PosZ[baseLevel]->Width2 != w ||
223 t->PosZ[baseLevel]->Height2 != h ||
224 !t->NegZ[baseLevel] ||
225 t->NegZ[baseLevel]->Width2 != w ||
226 t->NegZ[baseLevel]->Height2 != h) {
Brian Paulad817702000-05-30 00:27:24 +0000227 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000228 incomplete(t, "Non-quare cubemap image");
Brian Paulad817702000-05-30 00:27:24 +0000229 return;
230 }
231 }
232
233 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
jtgafb833d1999-08-19 00:55:39 +0000234 /*
235 * Mipmapping: determine if we have a complete set of mipmaps
236 */
237 GLint i;
Brian Paul63ec4232000-06-12 16:09:49 +0000238 GLint minLevel = baseLevel;
jtgafb833d1999-08-19 00:55:39 +0000239 GLint maxLevel = MIN2(t->P, ctx->Const.MaxTextureLevels-1);
240 maxLevel = MIN2(maxLevel, t->MaxLevel);
241
242 if (minLevel > maxLevel) {
243 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000244 incomplete(t, "minLevel > maxLevel");
jtgafb833d1999-08-19 00:55:39 +0000245 return;
246 }
247
248 /* Test dimension-independent attributes */
249 for (i = minLevel; i <= maxLevel; i++) {
250 if (t->Image[i]) {
Brian Paul63ec4232000-06-12 16:09:49 +0000251 if (t->Image[i]->Format != t->Image[baseLevel]->Format) {
jtgafb833d1999-08-19 00:55:39 +0000252 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000253 incomplete(t, "Format[i] != Format[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000254 return;
255 }
Brian Paul63ec4232000-06-12 16:09:49 +0000256 if (t->Image[i]->Border != t->Image[baseLevel]->Border) {
jtgafb833d1999-08-19 00:55:39 +0000257 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000258 incomplete(t, "Border[i] != Border[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000259 return;
260 }
261 }
262 }
263
264 /* Test things which depend on number of texture image dimensions */
Brian Paulad817702000-05-30 00:27:24 +0000265 if (t->Dimensions == 1) {
jtgafb833d1999-08-19 00:55:39 +0000266 /* Test 1-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000267 GLuint width = t->Image[baseLevel]->Width2;
268 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000269 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000270 width /= 2;
271 }
272 if (i >= minLevel && i <= maxLevel) {
273 if (!t->Image[i]) {
274 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000275 incomplete(t, "1D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000276 return;
277 }
jtgafb833d1999-08-19 00:55:39 +0000278 if (t->Image[i]->Width2 != width ) {
279 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000280 incomplete(t, "1D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000281 return;
282 }
283 }
Brian Paulad817702000-05-30 00:27:24 +0000284 if (width == 1) {
jtgafb833d1999-08-19 00:55:39 +0000285 return; /* found smallest needed mipmap, all done! */
286 }
287 }
288 }
Brian Paulad817702000-05-30 00:27:24 +0000289 else if (t->Dimensions == 2) {
jtgafb833d1999-08-19 00:55:39 +0000290 /* Test 2-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000291 GLuint width = t->Image[baseLevel]->Width2;
292 GLuint height = t->Image[baseLevel]->Height2;
293 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000294 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000295 width /= 2;
296 }
Brian Paulad817702000-05-30 00:27:24 +0000297 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000298 height /= 2;
299 }
300 if (i >= minLevel && i <= maxLevel) {
301 if (!t->Image[i]) {
302 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000303 incomplete(t, "2D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000304 return;
305 }
306 if (t->Image[i]->Width2 != width) {
307 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000308 incomplete(t, "2D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000309 return;
310 }
311 if (t->Image[i]->Height2 != height) {
312 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000313 incomplete(t, "2D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000314 return;
315 }
316 if (width==1 && height==1) {
317 return; /* found smallest needed mipmap, all done! */
318 }
319 }
320 }
321 }
Brian Paulad817702000-05-30 00:27:24 +0000322 else if (t->Dimensions == 3) {
jtgafb833d1999-08-19 00:55:39 +0000323 /* Test 3-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000324 GLuint width = t->Image[baseLevel]->Width2;
325 GLuint height = t->Image[baseLevel]->Height2;
326 GLuint depth = t->Image[baseLevel]->Depth2;
327 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000328 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000329 width /= 2;
330 }
Brian Paulad817702000-05-30 00:27:24 +0000331 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000332 height /= 2;
333 }
Brian Paulad817702000-05-30 00:27:24 +0000334 if (depth > 1) {
jtgafb833d1999-08-19 00:55:39 +0000335 depth /= 2;
336 }
337 if (i >= minLevel && i <= maxLevel) {
338 if (!t->Image[i]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000339 incomplete(t, "3D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000340 t->Complete = GL_FALSE;
341 return;
342 }
343 if (t->Image[i]->Width2 != width) {
344 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000345 incomplete(t, "3D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000346 return;
347 }
348 if (t->Image[i]->Height2 != height) {
349 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000350 incomplete(t, "3D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000351 return;
352 }
353 if (t->Image[i]->Depth2 != depth) {
354 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000355 incomplete(t, "3D Image[i] bad depth");
jtgafb833d1999-08-19 00:55:39 +0000356 return;
357 }
358 }
Brian Paulad817702000-05-30 00:27:24 +0000359 if (width == 1 && height == 1 && depth == 1) {
jtgafb833d1999-08-19 00:55:39 +0000360 return; /* found smallest needed mipmap, all done! */
361 }
362 }
363 }
Brian Paulad817702000-05-30 00:27:24 +0000364 else if (t->Dimensions == 6) {
365 /* make sure 6 cube faces are consistant */
Brian Paul63ec4232000-06-12 16:09:49 +0000366 GLuint width = t->Image[baseLevel]->Width2;
367 GLuint height = t->Image[baseLevel]->Height2;
368 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000369 if (width > 1) {
370 width /= 2;
371 }
372 if (height > 1) {
373 height /= 2;
374 }
375 if (i >= minLevel && i <= maxLevel) {
376 /* check that we have images defined */
377 if (!t->Image[i] || !t->NegX[i] ||
378 !t->PosY[i] || !t->NegY[i] ||
379 !t->PosZ[i] || !t->NegZ[i]) {
380 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000381 incomplete(t, "CubeMap Image[i] == NULL");
Brian Paulad817702000-05-30 00:27:24 +0000382 return;
383 }
384 /* check that all six images have same size */
385 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
386 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
387 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
388 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
389 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
390 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000391 incomplete(t, "CubeMap Image[i] bad size");
Brian Paulad817702000-05-30 00:27:24 +0000392 return;
393 }
394 }
395 if (width == 1 && height == 1) {
396 return; /* found smallest needed mipmap, all done! */
397 }
398 }
Brian Paul413d6a22000-05-26 14:44:59 +0000399 }
jtgafb833d1999-08-19 00:55:39 +0000400 else {
401 /* Dimensions = ??? */
402 gl_problem(NULL, "Bug in gl_test_texture_object_completeness\n");
403 }
404 }
405}
406
407
Brian Paul832179c2000-03-21 17:42:27 +0000408_glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
409
jtgafb833d1999-08-19 00:55:39 +0000410
411/*
412 * Execute glGenTextures
413 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000414void
415_mesa_GenTextures( GLsizei n, GLuint *texName )
jtgafb833d1999-08-19 00:55:39 +0000416{
Brian Paulfbd8f211999-11-11 01:22:25 +0000417 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000418 GLuint first;
419 GLint i;
420
421 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGenTextures");
Brian Paul507d83e2000-08-03 14:03:17 +0000422 if (n < 0) {
jtgafb833d1999-08-19 00:55:39 +0000423 gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
424 return;
425 }
426
Brian Paul507d83e2000-08-03 14:03:17 +0000427 if (!texName)
428 return;
Brian Paul832179c2000-03-21 17:42:27 +0000429
430 /*
431 * This must be atomic (generation and allocation of texture IDs)
432 */
433 _glthread_LOCK_MUTEX(GenTexturesLock);
434
Brian Paulbb797902000-01-24 16:19:54 +0000435 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
jtgafb833d1999-08-19 00:55:39 +0000436
437 /* Return the texture names */
438 for (i=0;i<n;i++) {
439 texName[i] = first + i;
440 }
441
442 /* Allocate new, empty texture objects */
443 for (i=0;i<n;i++) {
444 GLuint name = first + i;
445 GLuint dims = 0;
446 (void) gl_alloc_texture_object(ctx->Shared, name, dims);
447 }
Brian Paul832179c2000-03-21 17:42:27 +0000448
449 _glthread_UNLOCK_MUTEX(GenTexturesLock);
jtgafb833d1999-08-19 00:55:39 +0000450}
451
452
453
454/*
455 * Execute glDeleteTextures
456 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000457void
458_mesa_DeleteTextures( GLsizei n, const GLuint *texName)
jtgafb833d1999-08-19 00:55:39 +0000459{
Brian Paulfbd8f211999-11-11 01:22:25 +0000460 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000461 GLint i;
462
463 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDeleteTextures");
464
Brian Paul507d83e2000-08-03 14:03:17 +0000465 if (!texName)
466 return;
467
jtgafb833d1999-08-19 00:55:39 +0000468 for (i=0;i<n;i++) {
469 struct gl_texture_object *t;
470 if (texName[i]>0) {
471 t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000472 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000473 if (t) {
Brian Paul59d6da52000-02-12 01:59:19 +0000474 /* First check if this texture is currently bound.
475 * If so, unbind it and decrement the reference count.
476 */
jtgafb833d1999-08-19 00:55:39 +0000477 GLuint u;
Brian Paul59d6da52000-02-12 01:59:19 +0000478 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
jtgafb833d1999-08-19 00:55:39 +0000479 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
480 GLuint d;
481 for (d = 1 ; d <= 3 ; d++) {
Brian Paul59d6da52000-02-12 01:59:19 +0000482 if (unit->CurrentD[d] == t) {
Brian Paul6e6d4c61999-10-09 20:17:07 +0000483 unit->CurrentD[d] = ctx->Shared->DefaultD[d];
484 ctx->Shared->DefaultD[d]->RefCount++;
jtgafb833d1999-08-19 00:55:39 +0000485 t->RefCount--;
Brian Paul59d6da52000-02-12 01:59:19 +0000486 ASSERT( t->RefCount >= 0 );
jtgafb833d1999-08-19 00:55:39 +0000487 }
488 }
489 }
490
Brian Paul59d6da52000-02-12 01:59:19 +0000491 /* Decrement reference count and delete if zero */
492 t->RefCount--;
493 ASSERT( t->RefCount >= 0 );
494 if (t->RefCount == 0) {
495 if (ctx->Driver.DeleteTexture)
496 (*ctx->Driver.DeleteTexture)( ctx, t );
jtgafb833d1999-08-19 00:55:39 +0000497 gl_free_texture_object(ctx->Shared, t);
498 }
499 }
500 }
501 }
502}
503
504
505
506/*
507 * Execute glBindTexture
508 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000509void
510_mesa_BindTexture( GLenum target, GLuint texName )
jtgafb833d1999-08-19 00:55:39 +0000511{
Brian Paulfbd8f211999-11-11 01:22:25 +0000512 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000513 GLuint unit = ctx->Texture.CurrentUnit;
514 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
515 struct gl_texture_object *oldTexObj;
516 struct gl_texture_object *newTexObj;
Brian Paul5b37c321999-11-05 06:43:10 +0000517 GLuint dim;
jtgafb833d1999-08-19 00:55:39 +0000518
519 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
520 fprintf(stderr, "glBindTexture %s %d\n",
521 gl_lookup_enum_by_nr(target), (GLint) texName);
522
523 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glBindTexture");
524
Brian Paul420ef641999-12-01 21:10:08 +0000525 switch (target) {
526 case GL_TEXTURE_1D:
527 dim = 1;
Brian Paulfc4b4432000-05-23 15:17:12 +0000528 oldTexObj = texUnit->CurrentD[1];
Brian Paul420ef641999-12-01 21:10:08 +0000529 break;
530 case GL_TEXTURE_2D:
531 dim = 2;
Brian Paulfc4b4432000-05-23 15:17:12 +0000532 oldTexObj = texUnit->CurrentD[2];
Brian Paul420ef641999-12-01 21:10:08 +0000533 break;
534 case GL_TEXTURE_3D:
535 dim = 3;
Brian Paulfc4b4432000-05-23 15:17:12 +0000536 oldTexObj = texUnit->CurrentD[3];
Brian Paul420ef641999-12-01 21:10:08 +0000537 break;
Brian Paulfc4b4432000-05-23 15:17:12 +0000538 case GL_TEXTURE_CUBE_MAP_ARB:
539 if (ctx->Extensions.HaveTextureCubeMap) {
540 dim = 6;
541 oldTexObj = texUnit->CurrentCubeMap;
542 break;
543 }
544 /* fallthrough */
Brian Paul420ef641999-12-01 21:10:08 +0000545 default:
546 gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
547 return;
jtgafb833d1999-08-19 00:55:39 +0000548 }
549
jtgafb833d1999-08-19 00:55:39 +0000550 if (oldTexObj->Name == texName)
551 return;
552
Brian Paulfc4b4432000-05-23 15:17:12 +0000553 if (texName == 0) {
554 if (target == GL_TEXTURE_CUBE_MAP_ARB)
555 newTexObj = ctx->Shared->DefaultCubeMap;
556 else
557 newTexObj = ctx->Shared->DefaultD[dim];
558 }
jtgafb833d1999-08-19 00:55:39 +0000559 else {
Brian Paulbb797902000-01-24 16:19:54 +0000560 struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
Brian Paulc79fab42000-01-24 20:53:32 +0000561 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
jtgafb833d1999-08-19 00:55:39 +0000562
563 if (!newTexObj)
564 newTexObj = gl_alloc_texture_object(ctx->Shared, texName, dim);
565
566 if (newTexObj->Dimensions != dim) {
567 if (newTexObj->Dimensions) {
Brian Paul420ef641999-12-01 21:10:08 +0000568 /* the named texture object's dimensions don't match the target */
jtgafb833d1999-08-19 00:55:39 +0000569 gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" );
570 return;
571 }
572 newTexObj->Dimensions = dim;
573 }
574 }
575
jtgafb833d1999-08-19 00:55:39 +0000576 newTexObj->RefCount++;
Brian Paul6e6d4c61999-10-09 20:17:07 +0000577
Brian Paulfc4b4432000-05-23 15:17:12 +0000578 switch (target) {
579 case GL_TEXTURE_1D:
580 texUnit->CurrentD[1] = newTexObj;
581 break;
582 case GL_TEXTURE_2D:
583 texUnit->CurrentD[2] = newTexObj;
584 break;
585 case GL_TEXTURE_3D:
586 texUnit->CurrentD[3] = newTexObj;
587 break;
588 case GL_TEXTURE_CUBE_MAP_ARB:
589 texUnit->CurrentCubeMap = newTexObj;
590 break;
591 default:
592 gl_problem(ctx, "bad target in BindTexture");
593 }
jtgafb833d1999-08-19 00:55:39 +0000594
595 /* If we've changed the CurrentD[123] texture object then update the
596 * ctx->Texture.Current pointer to point to the new texture object.
597 */
598 texUnit->Current = texUnit->CurrentD[texUnit->CurrentDimension];
599
600 /* Check if we may have to use a new triangle rasterizer */
601 if ((ctx->IndirectTriangles & DD_SW_RASTERIZE) &&
602 ( oldTexObj->WrapS != newTexObj->WrapS
603 || oldTexObj->WrapT != newTexObj->WrapT
604 || oldTexObj->WrapR != newTexObj->WrapR
605 || oldTexObj->MinFilter != newTexObj->MinFilter
606 || oldTexObj->MagFilter != newTexObj->MagFilter
607 || (oldTexObj->Image[0] && newTexObj->Image[0] &&
608 (oldTexObj->Image[0]->Format!=newTexObj->Image[0]->Format))))
609 {
610 ctx->NewState |= (NEW_RASTER_OPS | NEW_TEXTURING);
611 }
612
613 if (oldTexObj->Complete != newTexObj->Complete)
614 ctx->NewState |= NEW_TEXTURING;
615
616 /* Pass BindTexture call to device driver */
617 if (ctx->Driver.BindTexture) {
618 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
Brian Paul9a4a9582000-10-24 01:13:58 +0000619 /* Make sure the Driver.UpdateState() function gets called! */
620 ctx->NewState |= NEW_TEXTURING;
jtgafb833d1999-08-19 00:55:39 +0000621 }
Brian Paul6e6d4c61999-10-09 20:17:07 +0000622
623 if (oldTexObj->Name > 0) {
624 /* never delete default (id=0) texture objects */
625 oldTexObj->RefCount--;
626 if (oldTexObj->RefCount <= 0) {
627 if (ctx->Driver.DeleteTexture) {
628 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
629 }
630 gl_free_texture_object(ctx->Shared, oldTexObj);
631 }
632 }
jtgafb833d1999-08-19 00:55:39 +0000633}
634
635
636
637/*
638 * Execute glPrioritizeTextures
639 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000640void
641_mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
642 const GLclampf *priorities )
jtgafb833d1999-08-19 00:55:39 +0000643{
Brian Paulfbd8f211999-11-11 01:22:25 +0000644 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000645 GLint i;
646
647 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPrioritizeTextures");
Brian Paul6d047252000-08-02 00:38:25 +0000648 if (n < 0) {
jtgafb833d1999-08-19 00:55:39 +0000649 gl_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
650 return;
651 }
652
Brian Paul507d83e2000-08-03 14:03:17 +0000653 if (!priorities)
654 return;
655
Brian Paul6d047252000-08-02 00:38:25 +0000656 for (i = 0; i < n; i++) {
657 if (texName[i] > 0) {
658 struct gl_texture_object *t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000659 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000660 if (t) {
661 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000662 if (ctx->Driver.PrioritizeTexture)
663 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
jtgafb833d1999-08-19 00:55:39 +0000664 }
665 }
666 }
667}
668
669
670
671/*
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000672 * Execute glAreTexturesResident
jtgafb833d1999-08-19 00:55:39 +0000673 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000674GLboolean
Brian Paulbd0f7f42000-08-02 20:16:03 +0000675_mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
676 GLboolean *residences)
jtgafb833d1999-08-19 00:55:39 +0000677{
Brian Paulfbd8f211999-11-11 01:22:25 +0000678 GET_CURRENT_CONTEXT(ctx);
Brian Paulbd0f7f42000-08-02 20:16:03 +0000679 GLboolean allResident = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000680 GLint i;
681
682 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx,
Brian Paulbd0f7f42000-08-02 20:16:03 +0000683 "glAreTexturesResident", GL_FALSE);
684 if (n < 0) {
685 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
jtgafb833d1999-08-19 00:55:39 +0000686 return GL_FALSE;
687 }
688
Brian Paul507d83e2000-08-03 14:03:17 +0000689 if (!texName || !residences)
690 return GL_FALSE;
691
Brian Paulbd0f7f42000-08-02 20:16:03 +0000692 for (i = 0; i < n; i++) {
jtgafb833d1999-08-19 00:55:39 +0000693 struct gl_texture_object *t;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000694 if (texName[i] == 0) {
695 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000696 return GL_FALSE;
697 }
698 t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000699 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000700 if (t) {
Brian Paulbd0f7f42000-08-02 20:16:03 +0000701 if (ctx->Driver.IsTextureResident) {
702 residences[i] = ctx->Driver.IsTextureResident(ctx, t);
703 if (!residences[i])
704 allResident = GL_FALSE;
705 }
706 else {
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000707 residences[i] = GL_TRUE;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000708 }
jtgafb833d1999-08-19 00:55:39 +0000709 }
710 else {
Brian Paulbd0f7f42000-08-02 20:16:03 +0000711 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000712 return GL_FALSE;
713 }
714 }
Brian Paulbd0f7f42000-08-02 20:16:03 +0000715 return allResident;
jtgafb833d1999-08-19 00:55:39 +0000716}
717
718
719
720/*
721 * Execute glIsTexture
722 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000723GLboolean
724_mesa_IsTexture( GLuint texture )
jtgafb833d1999-08-19 00:55:39 +0000725{
Brian Paulfbd8f211999-11-11 01:22:25 +0000726 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000727 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glIsTextures",
728 GL_FALSE);
Brian Paulbd0f7f42000-08-02 20:16:03 +0000729 if (texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture)) {
jtgafb833d1999-08-19 00:55:39 +0000730 return GL_TRUE;
731 }
732 else {
733 return GL_FALSE;
734 }
735}
736