39c61d492534aa37d163f4f62a59a9f8f4a4fc1c
[mesa.git] / src / mesa / main / texobj.c
1 /* $Id: texobj.c,v 1.59 2002/10/22 15:08:59 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 else if (delObj == unit->CurrentRect) {
597 unit->CurrentRect = ctx->Shared->DefaultRect;
598 ctx->Shared->DefaultRect->RefCount++;
599 if (delObj == unit->_Current)
600 unit->_Current = unit->CurrentRect;
601 }
602 }
603 ctx->NewState |= _NEW_TEXTURE;
604
605 /* Decrement reference count and delete if zero */
606 delObj->RefCount--;
607 ASSERT(delObj->RefCount >= 0);
608
609 if (delObj->RefCount == 0) {
610 ASSERT(delObj->Name != 0);
611 if (ctx->Driver.DeleteTexture)
612 (*ctx->Driver.DeleteTexture)( ctx, delObj );
613 _mesa_free_texture_object(ctx->Shared, delObj);
614 }
615 }
616 }
617 }
618 }
619
620
621
622 /*
623 * Execute glBindTexture
624 */
625 void
626 _mesa_BindTexture( GLenum target, GLuint texName )
627 {
628 GET_CURRENT_CONTEXT(ctx);
629 GLuint unit = ctx->Texture.CurrentUnit;
630 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
631 struct gl_texture_object *oldTexObj;
632 struct gl_texture_object *newTexObj = 0;
633 ASSERT_OUTSIDE_BEGIN_END(ctx);
634
635 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
636 _mesa_debug(ctx, "glBindTexture %s %d\n",
637 _mesa_lookup_enum_by_nr(target), (GLint) texName);
638
639 switch (target) {
640 case GL_TEXTURE_1D:
641 oldTexObj = texUnit->Current1D;
642 break;
643 case GL_TEXTURE_2D:
644 oldTexObj = texUnit->Current2D;
645 break;
646 case GL_TEXTURE_3D:
647 oldTexObj = texUnit->Current3D;
648 break;
649 case GL_TEXTURE_CUBE_MAP_ARB:
650 if (!ctx->Extensions.ARB_texture_cube_map) {
651 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
652 return;
653 }
654 oldTexObj = texUnit->CurrentCubeMap;
655 break;
656 case GL_TEXTURE_RECTANGLE_NV:
657 if (!ctx->Extensions.NV_texture_rectangle) {
658 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
659 return;
660 }
661 oldTexObj = texUnit->CurrentRect;
662 break;
663 default:
664 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
665 return;
666 }
667
668 if (oldTexObj->Name == texName)
669 return; /* rebinding the same texture- no change */
670
671 /*
672 * Get pointer to new texture object (newTexObj)
673 */
674 if (texName == 0) {
675 /* newTexObj = a default texture object */
676 switch (target) {
677 case GL_TEXTURE_1D:
678 newTexObj = ctx->Shared->Default1D;
679 break;
680 case GL_TEXTURE_2D:
681 newTexObj = ctx->Shared->Default2D;
682 break;
683 case GL_TEXTURE_3D:
684 newTexObj = ctx->Shared->Default3D;
685 break;
686 case GL_TEXTURE_CUBE_MAP_ARB:
687 newTexObj = ctx->Shared->DefaultCubeMap;
688 break;
689 case GL_TEXTURE_RECTANGLE_NV:
690 newTexObj = ctx->Shared->DefaultRect;
691 break;
692 default:
693 ; /* Bad targets are caught above */
694 }
695 }
696 else {
697 /* non-default texture object */
698 const struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
699 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
700 if (newTexObj) {
701 /* error checking */
702 if (newTexObj->Target != 0 && newTexObj->Target != target) {
703 /* the named texture object's dimensions don't match the target */
704 _mesa_error( ctx, GL_INVALID_OPERATION,
705 "glBindTexture(wrong dimensionality)" );
706 return;
707 }
708 if (newTexObj->Target == 0 && target == GL_TEXTURE_RECTANGLE_NV) {
709 /* have to init wrap and filter state here - kind of klunky */
710 newTexObj->WrapS = GL_CLAMP_TO_EDGE;
711 newTexObj->WrapT = GL_CLAMP_TO_EDGE;
712 newTexObj->WrapR = GL_CLAMP_TO_EDGE;
713 newTexObj->MinFilter = GL_LINEAR;
714 }
715 }
716 else {
717 /* if this is a new texture id, allocate a texture object now */
718 newTexObj = _mesa_alloc_texture_object( ctx->Shared, texName,
719 target);
720 if (!newTexObj) {
721 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
722 return;
723 }
724 }
725 newTexObj->Target = target;
726 }
727
728 newTexObj->RefCount++;
729
730 /* do the actual binding, but first flush outstanding vertices:
731 */
732 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
733
734 switch (target) {
735 case GL_TEXTURE_1D:
736 texUnit->Current1D = newTexObj;
737 break;
738 case GL_TEXTURE_2D:
739 texUnit->Current2D = newTexObj;
740 break;
741 case GL_TEXTURE_3D:
742 texUnit->Current3D = newTexObj;
743 break;
744 case GL_TEXTURE_CUBE_MAP_ARB:
745 texUnit->CurrentCubeMap = newTexObj;
746 break;
747 case GL_TEXTURE_RECTANGLE_NV:
748 texUnit->CurrentRect = newTexObj;
749 break;
750 default:
751 _mesa_problem(ctx, "bad target in BindTexture");
752 return;
753 }
754
755 /* Pass BindTexture call to device driver */
756 if (ctx->Driver.BindTexture)
757 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
758
759 oldTexObj->RefCount--;
760 assert(oldTexObj->RefCount >= 0);
761 if (oldTexObj->RefCount == 0) {
762 assert(oldTexObj->Name != 0);
763 if (ctx->Driver.DeleteTexture) {
764 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
765 }
766 _mesa_free_texture_object(ctx->Shared, oldTexObj);
767 }
768 }
769
770
771
772 /*
773 * Execute glPrioritizeTextures
774 */
775 void
776 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
777 const GLclampf *priorities )
778 {
779 GET_CURRENT_CONTEXT(ctx);
780 GLint i;
781 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
782
783 if (n < 0) {
784 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
785 return;
786 }
787
788 if (!priorities)
789 return;
790
791 for (i = 0; i < n; i++) {
792 if (texName[i] > 0) {
793 struct gl_texture_object *t = (struct gl_texture_object *)
794 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
795 if (t) {
796 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
797 if (ctx->Driver.PrioritizeTexture)
798 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
799 }
800 }
801 }
802
803 ctx->NewState |= _NEW_TEXTURE;
804 }
805
806
807
808 /*
809 * Execute glAreTexturesResident
810 */
811 GLboolean
812 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
813 GLboolean *residences)
814 {
815 GET_CURRENT_CONTEXT(ctx);
816 GLboolean allResident = GL_TRUE;
817 GLint i;
818 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
819
820 if (n < 0) {
821 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
822 return GL_FALSE;
823 }
824
825 if (!texName || !residences)
826 return GL_FALSE;
827
828 for (i = 0; i < n; i++) {
829 struct gl_texture_object *t;
830 if (texName[i] == 0) {
831 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
832 return GL_FALSE;
833 }
834 t = (struct gl_texture_object *)
835 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
836 if (t) {
837 if (ctx->Driver.IsTextureResident) {
838 residences[i] = ctx->Driver.IsTextureResident(ctx, t);
839 if (!residences[i])
840 allResident = GL_FALSE;
841 }
842 else {
843 residences[i] = GL_TRUE;
844 }
845 }
846 else {
847 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
848 return GL_FALSE;
849 }
850 }
851 return allResident;
852 }
853
854
855
856 /*
857 * Execute glIsTexture
858 */
859 GLboolean
860 _mesa_IsTexture( GLuint texture )
861 {
862 GET_CURRENT_CONTEXT(ctx);
863 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
864 return texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture);
865 }