c27d7a6a5f017470af7ffb5cfde927aa71ec342c
[mesa.git] / src / mesa / main / texobj.c
1 /* $Id: texobj.c,v 1.58 2002/10/04 19:10:08 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
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 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "colortab.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "hash.h"
36 #include "macros.h"
37 #include "mem.h"
38 #include "teximage.h"
39 #include "texstate.h"
40 #include "texobj.h"
41 #include "mtypes.h"
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
52 * target - either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
53 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV
54 * zero is ok for the sake of GenTextures()
55 * Return: pointer to new texture object
56 */
57 struct gl_texture_object *
58 _mesa_alloc_texture_object( struct gl_shared_state *shared,
59 GLuint name, GLenum target )
60 {
61 struct gl_texture_object *obj;
62
63 ASSERT(target == 0 ||
64 target == GL_TEXTURE_1D ||
65 target == GL_TEXTURE_2D ||
66 target == GL_TEXTURE_3D ||
67 target == GL_TEXTURE_CUBE_MAP_ARB ||
68 target == GL_TEXTURE_RECTANGLE_NV);
69
70 obj = CALLOC_STRUCT(gl_texture_object);
71
72 if (obj) {
73 /* init the non-zero fields */
74 _glthread_INIT_MUTEX(obj->Mutex);
75 obj->RefCount = 1;
76 obj->Name = name;
77 obj->Target = target;
78 obj->Priority = 1.0F;
79 if (target == GL_TEXTURE_RECTANGLE_NV) {
80 obj->WrapS = GL_CLAMP_TO_EDGE;
81 obj->WrapT = GL_CLAMP_TO_EDGE;
82 obj->WrapR = GL_CLAMP_TO_EDGE;
83 obj->MinFilter = GL_LINEAR;
84 }
85 else {
86 obj->WrapS = GL_REPEAT;
87 obj->WrapT = GL_REPEAT;
88 obj->WrapR = GL_REPEAT;
89 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
90 }
91 obj->MagFilter = GL_LINEAR;
92 obj->MinLod = -1000.0;
93 obj->MaxLod = 1000.0;
94 obj->BaseLevel = 0;
95 obj->MaxLevel = 1000;
96 obj->MaxAnisotropy = 1.0;
97 obj->CompareFlag = GL_FALSE; /* SGIX_shadow */
98 obj->CompareOperator = GL_TEXTURE_LEQUAL_R_SGIX; /* SGIX_shadow */
99 obj->CompareMode = GL_LUMINANCE; /* ARB_shadow */
100 obj->CompareFunc = GL_LEQUAL; /* ARB_shadow */
101 obj->DepthMode = GL_LUMINANCE; /* ARB_depth_texture */
102 obj->ShadowAmbient = 0.0F; /* ARB/SGIX_shadow_ambient */
103 _mesa_init_colortable(&obj->Palette);
104
105 /* insert into linked list */
106 if (shared) {
107 _glthread_LOCK_MUTEX(shared->Mutex);
108 obj->Next = shared->TexObjectList;
109 shared->TexObjectList = obj;
110 _glthread_UNLOCK_MUTEX(shared->Mutex);
111 }
112
113 if (name > 0) {
114 /* insert into hash table */
115 _mesa_HashInsert(shared->TexObjects, name, obj);
116 }
117 }
118 return obj;
119 }
120
121
122 /*
123 * Deallocate a texture object struct and remove it from the given
124 * shared GL state.
125 * Input: shared - the shared GL state to which the object belongs
126 * t - the texture object to delete
127 */
128 void _mesa_free_texture_object( struct gl_shared_state *shared,
129 struct gl_texture_object *t )
130 {
131 struct gl_texture_object *tprev, *tcurr;
132
133 assert(t);
134
135 /* unlink t from the linked list */
136 if (shared) {
137 _glthread_LOCK_MUTEX(shared->Mutex);
138 tprev = NULL;
139 tcurr = shared->TexObjectList;
140 while (tcurr) {
141 if (tcurr==t) {
142 if (tprev) {
143 tprev->Next = t->Next;
144 }
145 else {
146 shared->TexObjectList = t->Next;
147 }
148 break;
149 }
150 tprev = tcurr;
151 tcurr = tcurr->Next;
152 }
153 _glthread_UNLOCK_MUTEX(shared->Mutex);
154 }
155
156 if (t->Name) {
157 /* remove from hash table */
158 _mesa_HashRemove(shared->TexObjects, t->Name);
159 }
160
161 _mesa_free_colortable_data(&t->Palette);
162
163 /* free the texture images */
164 {
165 GLuint i;
166 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
167 if (t->Image[i]) {
168 _mesa_free_texture_image( t->Image[i] );
169 }
170 }
171 }
172
173 /* free this object */
174 FREE( t );
175 }
176
177
178 /*
179 * Copy texture object state from one texture object to another.
180 */
181 void
182 _mesa_copy_texture_object( struct gl_texture_object *dest,
183 const struct gl_texture_object *src )
184 {
185 dest->Name = src->Name;
186 dest->Priority = src->Priority;
187 dest->BorderColor[0] = src->BorderColor[0];
188 dest->BorderColor[1] = src->BorderColor[1];
189 dest->BorderColor[2] = src->BorderColor[2];
190 dest->BorderColor[3] = src->BorderColor[3];
191 dest->WrapS = src->WrapS;
192 dest->WrapT = src->WrapT;
193 dest->WrapR = src->WrapR;
194 dest->MinFilter = src->MinFilter;
195 dest->MagFilter = src->MagFilter;
196 dest->MinLod = src->MinLod;
197 dest->MaxLod = src->MaxLod;
198 dest->BaseLevel = src->BaseLevel;
199 dest->MaxLevel = src->MaxLevel;
200 dest->MaxAnisotropy = src->MaxAnisotropy;
201 dest->CompareFlag = src->CompareFlag;
202 dest->CompareOperator = src->CompareOperator;
203 dest->ShadowAmbient = src->ShadowAmbient;
204 dest->CompareMode = src->CompareMode;
205 dest->CompareFunc = src->CompareFunc;
206 dest->DepthMode = src->DepthMode;
207 dest->_MaxLevel = src->_MaxLevel;
208 dest->_MaxLambda = src->_MaxLambda;
209 dest->GenerateMipmap = src->GenerateMipmap;
210 dest->Palette = src->Palette;
211 dest->Complete = src->Complete;
212 }
213
214
215 /*
216 * Report why a texture object is incomplete. (for debug only)
217 */
218 #if 0
219 static void
220 incomplete(const struct gl_texture_object *t, const char *why)
221 {
222 _mesa_printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
223 }
224 #else
225 #define incomplete(a, b)
226 #endif
227
228
229 /*
230 * Examine a texture object to determine if it is complete.
231 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
232 */
233 void
234 _mesa_test_texobj_completeness( const GLcontext *ctx,
235 struct gl_texture_object *t )
236 {
237 const GLint baseLevel = t->BaseLevel;
238 GLint maxLog2 = 0, maxLevels = 0;
239
240 t->Complete = GL_TRUE; /* be optimistic */
241
242 /* Always need the base level image */
243 if (!t->Image[baseLevel]) {
244 incomplete(t, "Image[baseLevel] == NULL");
245 t->Complete = GL_FALSE;
246 return;
247 }
248
249 /* Compute _MaxLevel */
250 if (t->Target == GL_TEXTURE_1D) {
251 maxLog2 = t->Image[baseLevel]->WidthLog2;
252 maxLevels = ctx->Const.MaxTextureLevels;
253 }
254 else if (t->Target == GL_TEXTURE_2D) {
255 maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2,
256 t->Image[baseLevel]->HeightLog2);
257 maxLevels = ctx->Const.MaxTextureLevels;
258 }
259 else if (t->Target == GL_TEXTURE_3D) {
260 GLint max = MAX2(t->Image[baseLevel]->WidthLog2,
261 t->Image[baseLevel]->HeightLog2);
262 maxLog2 = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2));
263 maxLevels = ctx->Const.Max3DTextureLevels;
264 }
265 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
266 maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2,
267 t->Image[baseLevel]->HeightLog2);
268 maxLevels = ctx->Const.MaxCubeTextureLevels;
269 }
270 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
271 maxLog2 = 0; /* not applicable */
272 maxLevels = 1; /* no mipmapping */
273 }
274 else {
275 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
276 return;
277 }
278
279 ASSERT(maxLevels > 0);
280
281 t->_MaxLevel = baseLevel + maxLog2;
282 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
283 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1);
284
285 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
286 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
287
288 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
289 /* make sure that all six cube map level 0 images are the same size */
290 const GLuint w = t->Image[baseLevel]->Width2;
291 const GLuint h = t->Image[baseLevel]->Height2;
292 if (!t->NegX[baseLevel] ||
293 t->NegX[baseLevel]->Width2 != w ||
294 t->NegX[baseLevel]->Height2 != h ||
295 !t->PosY[baseLevel] ||
296 t->PosY[baseLevel]->Width2 != w ||
297 t->PosY[baseLevel]->Height2 != h ||
298 !t->NegY[baseLevel] ||
299 t->NegY[baseLevel]->Width2 != w ||
300 t->NegY[baseLevel]->Height2 != h ||
301 !t->PosZ[baseLevel] ||
302 t->PosZ[baseLevel]->Width2 != w ||
303 t->PosZ[baseLevel]->Height2 != h ||
304 !t->NegZ[baseLevel] ||
305 t->NegZ[baseLevel]->Width2 != w ||
306 t->NegZ[baseLevel]->Height2 != h) {
307 t->Complete = GL_FALSE;
308 incomplete(t, "Non-quare cubemap image");
309 return;
310 }
311 }
312
313 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
314 /*
315 * Mipmapping: determine if we have a complete set of mipmaps
316 */
317 GLint i;
318 GLint minLevel = baseLevel;
319 GLint maxLevel = t->_MaxLevel;
320
321 if (minLevel > maxLevel) {
322 t->Complete = GL_FALSE;
323 incomplete(t, "minLevel > maxLevel");
324 return;
325 }
326
327 /* Test dimension-independent attributes */
328 for (i = minLevel; i <= maxLevel; i++) {
329 if (t->Image[i]) {
330 if (t->Image[i]->TexFormat != t->Image[baseLevel]->TexFormat) {
331 t->Complete = GL_FALSE;
332 incomplete(t, "Format[i] != Format[baseLevel]");
333 return;
334 }
335 if (t->Image[i]->Border != t->Image[baseLevel]->Border) {
336 t->Complete = GL_FALSE;
337 incomplete(t, "Border[i] != Border[baseLevel]");
338 return;
339 }
340 }
341 }
342
343 /* Test things which depend on number of texture image dimensions */
344 if (t->Target == GL_TEXTURE_1D) {
345 /* Test 1-D mipmaps */
346 GLuint width = t->Image[baseLevel]->Width2;
347 for (i = baseLevel + 1; i < maxLevels; i++) {
348 if (width > 1) {
349 width /= 2;
350 }
351 if (i >= minLevel && i <= maxLevel) {
352 if (!t->Image[i]) {
353 t->Complete = GL_FALSE;
354 incomplete(t, "1D Image[i] == NULL");
355 return;
356 }
357 if (t->Image[i]->Width2 != width ) {
358 t->Complete = GL_FALSE;
359 incomplete(t, "1D Image[i] bad width");
360 return;
361 }
362 }
363 if (width == 1) {
364 return; /* found smallest needed mipmap, all done! */
365 }
366 }
367 }
368 else if (t->Target == GL_TEXTURE_2D) {
369 /* Test 2-D mipmaps */
370 GLuint width = t->Image[baseLevel]->Width2;
371 GLuint height = t->Image[baseLevel]->Height2;
372 for (i = baseLevel + 1; i < maxLevels; i++) {
373 if (width > 1) {
374 width /= 2;
375 }
376 if (height > 1) {
377 height /= 2;
378 }
379 if (i >= minLevel && i <= maxLevel) {
380 if (!t->Image[i]) {
381 t->Complete = GL_FALSE;
382 incomplete(t, "2D Image[i] == NULL");
383 return;
384 }
385 if (t->Image[i]->Width2 != width) {
386 t->Complete = GL_FALSE;
387 incomplete(t, "2D Image[i] bad width");
388 return;
389 }
390 if (t->Image[i]->Height2 != height) {
391 t->Complete = GL_FALSE;
392 incomplete(t, "2D Image[i] bad height");
393 return;
394 }
395 if (width==1 && height==1) {
396 return; /* found smallest needed mipmap, all done! */
397 }
398 }
399 }
400 }
401 else if (t->Target == GL_TEXTURE_3D) {
402 /* Test 3-D mipmaps */
403 GLuint width = t->Image[baseLevel]->Width2;
404 GLuint height = t->Image[baseLevel]->Height2;
405 GLuint depth = t->Image[baseLevel]->Depth2;
406 for (i = baseLevel + 1; i < maxLevels; i++) {
407 if (width > 1) {
408 width /= 2;
409 }
410 if (height > 1) {
411 height /= 2;
412 }
413 if (depth > 1) {
414 depth /= 2;
415 }
416 if (i >= minLevel && i <= maxLevel) {
417 if (!t->Image[i]) {
418 incomplete(t, "3D Image[i] == NULL");
419 t->Complete = GL_FALSE;
420 return;
421 }
422 if (t->Image[i]->Format == GL_DEPTH_COMPONENT) {
423 t->Complete = GL_FALSE;
424 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
425 return;
426 }
427 if (t->Image[i]->Width2 != width) {
428 t->Complete = GL_FALSE;
429 incomplete(t, "3D Image[i] bad width");
430 return;
431 }
432 if (t->Image[i]->Height2 != height) {
433 t->Complete = GL_FALSE;
434 incomplete(t, "3D Image[i] bad height");
435 return;
436 }
437 if (t->Image[i]->Depth2 != depth) {
438 t->Complete = GL_FALSE;
439 incomplete(t, "3D Image[i] bad depth");
440 return;
441 }
442 }
443 if (width == 1 && height == 1 && depth == 1) {
444 return; /* found smallest needed mipmap, all done! */
445 }
446 }
447 }
448 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
449 /* make sure 6 cube faces are consistant */
450 GLuint width = t->Image[baseLevel]->Width2;
451 GLuint height = t->Image[baseLevel]->Height2;
452 for (i = baseLevel + 1; i < maxLevels; i++) {
453 if (width > 1) {
454 width /= 2;
455 }
456 if (height > 1) {
457 height /= 2;
458 }
459 if (i >= minLevel && i <= maxLevel) {
460 /* Don't support GL_DEPTH_COMPONENT for cube maps */
461 if (t->Image[i]->Format == GL_DEPTH_COMPONENT) {
462 t->Complete = GL_FALSE;
463 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
464 return;
465 }
466 /* check that we have images defined */
467 if (!t->Image[i] || !t->NegX[i] ||
468 !t->PosY[i] || !t->NegY[i] ||
469 !t->PosZ[i] || !t->NegZ[i]) {
470 t->Complete = GL_FALSE;
471 incomplete(t, "CubeMap Image[i] == NULL");
472 return;
473 }
474 /* check that all six images have same size */
475 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
476 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
477 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
478 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
479 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
480 t->Complete = GL_FALSE;
481 incomplete(t, "CubeMap Image[i] bad size");
482 return;
483 }
484 }
485 if (width == 1 && height == 1) {
486 return; /* found smallest needed mipmap, all done! */
487 }
488 }
489 }
490 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
491 /* XXX special checking? */
492
493 }
494 else {
495 /* Target = ??? */
496 _mesa_problem(ctx, "Bug in gl_test_texture_object_completeness\n");
497 }
498 }
499 }
500
501
502 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
503
504
505 /*
506 * Execute glGenTextures
507 */
508 void
509 _mesa_GenTextures( GLsizei n, GLuint *texName )
510 {
511 GET_CURRENT_CONTEXT(ctx);
512 GLuint first;
513 GLint i;
514 ASSERT_OUTSIDE_BEGIN_END(ctx);
515
516 if (n < 0) {
517 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
518 return;
519 }
520
521 if (!texName)
522 return;
523
524 /*
525 * This must be atomic (generation and allocation of texture IDs)
526 */
527 _glthread_LOCK_MUTEX(GenTexturesLock);
528
529 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
530
531 /* Return the texture names */
532 for (i=0;i<n;i++) {
533 texName[i] = first + i;
534 }
535
536 /* Allocate new, empty texture objects */
537 for (i=0;i<n;i++) {
538 GLuint name = first + i;
539 GLenum target = 0;
540 (void) _mesa_alloc_texture_object( ctx->Shared, name, target);
541 }
542
543 _glthread_UNLOCK_MUTEX(GenTexturesLock);
544 }
545
546
547
548 /*
549 * Execute glDeleteTextures
550 */
551 void
552 _mesa_DeleteTextures( GLsizei n, const GLuint *texName)
553 {
554 GET_CURRENT_CONTEXT(ctx);
555 GLint i;
556 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
557
558 if (!texName)
559 return;
560
561 for (i=0;i<n;i++) {
562 if (texName[i] > 0) {
563 struct gl_texture_object *delObj = (struct gl_texture_object *)
564 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
565 if (delObj) {
566 /* First check if this texture is currently bound.
567 * If so, unbind it and decrement the reference count.
568 */
569 GLuint u;
570 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
571 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
572 if (delObj == unit->Current1D) {
573 unit->Current1D = ctx->Shared->Default1D;
574 ctx->Shared->Default1D->RefCount++;
575 if (delObj == unit->_Current)
576 unit->_Current = unit->Current1D;
577 }
578 else if (delObj == unit->Current2D) {
579 unit->Current2D = ctx->Shared->Default2D;
580 ctx->Shared->Default2D->RefCount++;
581 if (delObj == unit->_Current)
582 unit->_Current = unit->Current2D;
583 }
584 else if (delObj == unit->Current3D) {
585 unit->Current3D = ctx->Shared->Default3D;
586 ctx->Shared->Default3D->RefCount++;
587 if (delObj == unit->_Current)
588 unit->_Current = unit->Current3D;
589 }
590 else if (delObj == unit->CurrentCubeMap) {
591 unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
592 ctx->Shared->DefaultCubeMap->RefCount++;
593 if (delObj == unit->_Current)
594 unit->_Current = unit->CurrentCubeMap;
595 }
596 }
597 ctx->NewState |= _NEW_TEXTURE;
598
599 /* Decrement reference count and delete if zero */
600 delObj->RefCount--;
601 ASSERT(delObj->RefCount >= 0);
602
603 if (delObj->RefCount == 0) {
604 ASSERT(delObj->Name != 0);
605 if (ctx->Driver.DeleteTexture)
606 (*ctx->Driver.DeleteTexture)( ctx, delObj );
607 _mesa_free_texture_object(ctx->Shared, delObj);
608 }
609 }
610 }
611 }
612 }
613
614
615
616 /*
617 * Execute glBindTexture
618 */
619 void
620 _mesa_BindTexture( GLenum target, GLuint texName )
621 {
622 GET_CURRENT_CONTEXT(ctx);
623 GLuint unit = ctx->Texture.CurrentUnit;
624 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
625 struct gl_texture_object *oldTexObj;
626 struct gl_texture_object *newTexObj = 0;
627 ASSERT_OUTSIDE_BEGIN_END(ctx);
628
629 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
630 _mesa_debug(ctx, "glBindTexture %s %d\n",
631 _mesa_lookup_enum_by_nr(target), (GLint) texName);
632
633 switch (target) {
634 case GL_TEXTURE_1D:
635 oldTexObj = texUnit->Current1D;
636 break;
637 case GL_TEXTURE_2D:
638 oldTexObj = texUnit->Current2D;
639 break;
640 case GL_TEXTURE_3D:
641 oldTexObj = texUnit->Current3D;
642 break;
643 case GL_TEXTURE_CUBE_MAP_ARB:
644 if (!ctx->Extensions.ARB_texture_cube_map) {
645 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
646 return;
647 }
648 oldTexObj = texUnit->CurrentCubeMap;
649 break;
650 case GL_TEXTURE_RECTANGLE_NV:
651 if (!ctx->Extensions.NV_texture_rectangle) {
652 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
653 return;
654 }
655 oldTexObj = texUnit->CurrentRect;
656 break;
657 default:
658 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
659 return;
660 }
661
662 if (oldTexObj->Name == texName)
663 return; /* rebinding the same texture- no change */
664
665 /*
666 * Get pointer to new texture object (newTexObj)
667 */
668 if (texName == 0) {
669 /* newTexObj = a default texture object */
670 switch (target) {
671 case GL_TEXTURE_1D:
672 newTexObj = ctx->Shared->Default1D;
673 break;
674 case GL_TEXTURE_2D:
675 newTexObj = ctx->Shared->Default2D;
676 break;
677 case GL_TEXTURE_3D:
678 newTexObj = ctx->Shared->Default3D;
679 break;
680 case GL_TEXTURE_CUBE_MAP_ARB:
681 newTexObj = ctx->Shared->DefaultCubeMap;
682 break;
683 case GL_TEXTURE_RECTANGLE_NV:
684 newTexObj = ctx->Shared->DefaultRect;
685 break;
686 default:
687 ; /* Bad targets are caught above */
688 }
689 }
690 else {
691 /* non-default texture object */
692 const struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
693 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
694 if (newTexObj) {
695 /* error checking */
696 if (newTexObj->Target != 0 && newTexObj->Target != target) {
697 /* the named texture object's dimensions don't match the target */
698 _mesa_error( ctx, GL_INVALID_OPERATION,
699 "glBindTexture(wrong dimensionality)" );
700 return;
701 }
702 if (newTexObj->Target == 0 && target == GL_TEXTURE_RECTANGLE_NV) {
703 /* have to init wrap and filter state here - kind of klunky */
704 newTexObj->WrapS = GL_CLAMP_TO_EDGE;
705 newTexObj->WrapT = GL_CLAMP_TO_EDGE;
706 newTexObj->WrapR = GL_CLAMP_TO_EDGE;
707 newTexObj->MinFilter = GL_LINEAR;
708 }
709 }
710 else {
711 /* if this is a new texture id, allocate a texture object now */
712 newTexObj = _mesa_alloc_texture_object( ctx->Shared, texName,
713 target);
714 if (!newTexObj) {
715 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
716 return;
717 }
718 }
719 newTexObj->Target = target;
720 }
721
722 newTexObj->RefCount++;
723
724 /* do the actual binding, but first flush outstanding vertices:
725 */
726 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
727
728 switch (target) {
729 case GL_TEXTURE_1D:
730 texUnit->Current1D = newTexObj;
731 break;
732 case GL_TEXTURE_2D:
733 texUnit->Current2D = newTexObj;
734 break;
735 case GL_TEXTURE_3D:
736 texUnit->Current3D = newTexObj;
737 break;
738 case GL_TEXTURE_CUBE_MAP_ARB:
739 texUnit->CurrentCubeMap = newTexObj;
740 break;
741 case GL_TEXTURE_RECTANGLE_NV:
742 texUnit->CurrentRect = newTexObj;
743 break;
744 default:
745 _mesa_problem(ctx, "bad target in BindTexture");
746 return;
747 }
748
749 /* Pass BindTexture call to device driver */
750 if (ctx->Driver.BindTexture)
751 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
752
753 oldTexObj->RefCount--;
754 assert(oldTexObj->RefCount >= 0);
755 if (oldTexObj->RefCount == 0) {
756 assert(oldTexObj->Name != 0);
757 if (ctx->Driver.DeleteTexture) {
758 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
759 }
760 _mesa_free_texture_object(ctx->Shared, oldTexObj);
761 }
762 }
763
764
765
766 /*
767 * Execute glPrioritizeTextures
768 */
769 void
770 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
771 const GLclampf *priorities )
772 {
773 GET_CURRENT_CONTEXT(ctx);
774 GLint i;
775 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
776
777 if (n < 0) {
778 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
779 return;
780 }
781
782 if (!priorities)
783 return;
784
785 for (i = 0; i < n; i++) {
786 if (texName[i] > 0) {
787 struct gl_texture_object *t = (struct gl_texture_object *)
788 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
789 if (t) {
790 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
791 if (ctx->Driver.PrioritizeTexture)
792 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
793 }
794 }
795 }
796
797 ctx->NewState |= _NEW_TEXTURE;
798 }
799
800
801
802 /*
803 * Execute glAreTexturesResident
804 */
805 GLboolean
806 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
807 GLboolean *residences)
808 {
809 GET_CURRENT_CONTEXT(ctx);
810 GLboolean allResident = GL_TRUE;
811 GLint i;
812 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
813
814 if (n < 0) {
815 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
816 return GL_FALSE;
817 }
818
819 if (!texName || !residences)
820 return GL_FALSE;
821
822 for (i = 0; i < n; i++) {
823 struct gl_texture_object *t;
824 if (texName[i] == 0) {
825 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
826 return GL_FALSE;
827 }
828 t = (struct gl_texture_object *)
829 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
830 if (t) {
831 if (ctx->Driver.IsTextureResident) {
832 residences[i] = ctx->Driver.IsTextureResident(ctx, t);
833 if (!residences[i])
834 allResident = GL_FALSE;
835 }
836 else {
837 residences[i] = GL_TRUE;
838 }
839 }
840 else {
841 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
842 return GL_FALSE;
843 }
844 }
845 return allResident;
846 }
847
848
849
850 /*
851 * Execute glIsTexture
852 */
853 GLboolean
854 _mesa_IsTexture( GLuint texture )
855 {
856 GET_CURRENT_CONTEXT(ctx);
857 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
858 return texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture);
859 }