fixes for C++ warnings/errors
[mesa.git] / src / mesa / main / texobj.c
1 /**
2 * \file texobj.c
3 * Texture object management.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 6.5
9 *
10 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "glheader.h"
32 #include "colortab.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "fbobject.h"
36 #include "hash.h"
37 #include "imports.h"
38 #include "macros.h"
39 #include "teximage.h"
40 #include "texstate.h"
41 #include "texobj.h"
42 #include "mtypes.h"
43
44
45 #ifdef __VMS
46 #define _mesa_sprintf sprintf
47 #endif
48
49 /**********************************************************************/
50 /** \name Internal functions */
51 /*@{*/
52
53
54 /**
55 * Return the gl_texture_object for a given ID.
56 */
57 struct gl_texture_object *
58 _mesa_lookup_texture(GLcontext *ctx, GLuint id)
59 {
60 return (struct gl_texture_object *)
61 _mesa_HashLookup(ctx->Shared->TexObjects, id);
62 }
63
64
65
66 /**
67 * Allocate and initialize a new texture object. But don't put it into the
68 * texture object hash table.
69 *
70 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
71 * driver.
72 *
73 * \param shared the shared GL state structure to contain the texture object
74 * \param name integer name for the texture object
75 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
76 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
77 * of GenTextures()
78 *
79 * \return pointer to new texture object.
80 */
81 struct gl_texture_object *
82 _mesa_new_texture_object( GLcontext *ctx, GLuint name, GLenum target )
83 {
84 struct gl_texture_object *obj;
85 (void) ctx;
86 obj = MALLOC_STRUCT(gl_texture_object);
87 _mesa_initialize_texture_object(obj, name, target);
88 return obj;
89 }
90
91
92 /**
93 * Initialize a new texture object to default values.
94 * \param obj the texture object
95 * \param name the texture name
96 * \param target the texture target
97 */
98 void
99 _mesa_initialize_texture_object( struct gl_texture_object *obj,
100 GLuint name, GLenum target )
101 {
102 ASSERT(target == 0 ||
103 target == GL_TEXTURE_1D ||
104 target == GL_TEXTURE_2D ||
105 target == GL_TEXTURE_3D ||
106 target == GL_TEXTURE_CUBE_MAP_ARB ||
107 target == GL_TEXTURE_RECTANGLE_NV);
108
109 _mesa_bzero(obj, sizeof(*obj));
110 /* init the non-zero fields */
111 _glthread_INIT_MUTEX(obj->Mutex);
112 obj->RefCount = 1;
113 obj->Name = name;
114 obj->Target = target;
115 obj->Priority = 1.0F;
116 if (target == GL_TEXTURE_RECTANGLE_NV) {
117 obj->WrapS = GL_CLAMP_TO_EDGE;
118 obj->WrapT = GL_CLAMP_TO_EDGE;
119 obj->WrapR = GL_CLAMP_TO_EDGE;
120 obj->MinFilter = GL_LINEAR;
121 }
122 else {
123 obj->WrapS = GL_REPEAT;
124 obj->WrapT = GL_REPEAT;
125 obj->WrapR = GL_REPEAT;
126 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
127 }
128 obj->MagFilter = GL_LINEAR;
129 obj->MinLod = -1000.0;
130 obj->MaxLod = 1000.0;
131 obj->LodBias = 0.0;
132 obj->BaseLevel = 0;
133 obj->MaxLevel = 1000;
134 obj->MaxAnisotropy = 1.0;
135 obj->CompareFlag = GL_FALSE; /* SGIX_shadow */
136 obj->CompareOperator = GL_TEXTURE_LEQUAL_R_SGIX; /* SGIX_shadow */
137 obj->CompareMode = GL_NONE; /* ARB_shadow */
138 obj->CompareFunc = GL_LEQUAL; /* ARB_shadow */
139 obj->DepthMode = GL_LUMINANCE; /* ARB_depth_texture */
140 obj->ShadowAmbient = 0.0F; /* ARB/SGIX_shadow_ambient */
141 _mesa_init_colortable(&obj->Palette);
142 }
143
144
145 /**
146 * Deallocate a texture object struct. It should have already been
147 * removed from the texture object pool.
148 *
149 * \param shared the shared GL state to which the object belongs.
150 * \param texOjb the texture object to delete.
151 */
152 void
153 _mesa_delete_texture_object( GLcontext *ctx, struct gl_texture_object *texObj )
154 {
155 GLuint i, face;
156
157 (void) ctx;
158
159 _mesa_free_colortable_data(&texObj->Palette);
160
161 /* free the texture images */
162 for (face = 0; face < 6; face++) {
163 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
164 if (texObj->Image[face][i]) {
165 _mesa_delete_texture_image( ctx, texObj->Image[face][i] );
166 }
167 }
168 }
169
170 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
171 _glthread_DESTROY_MUTEX(texObj->Mutex);
172
173 /* free this object */
174 _mesa_free(texObj);
175 }
176
177
178
179
180 /**
181 * Copy texture object state from one texture object to another.
182 * Use for glPush/PopAttrib.
183 *
184 * \param dest destination texture object.
185 * \param src source texture object.
186 */
187 void
188 _mesa_copy_texture_object( struct gl_texture_object *dest,
189 const struct gl_texture_object *src )
190 {
191 dest->Name = src->Name;
192 dest->Priority = src->Priority;
193 dest->BorderColor[0] = src->BorderColor[0];
194 dest->BorderColor[1] = src->BorderColor[1];
195 dest->BorderColor[2] = src->BorderColor[2];
196 dest->BorderColor[3] = src->BorderColor[3];
197 dest->WrapS = src->WrapS;
198 dest->WrapT = src->WrapT;
199 dest->WrapR = src->WrapR;
200 dest->MinFilter = src->MinFilter;
201 dest->MagFilter = src->MagFilter;
202 dest->MinLod = src->MinLod;
203 dest->MaxLod = src->MaxLod;
204 dest->LodBias = src->LodBias;
205 dest->BaseLevel = src->BaseLevel;
206 dest->MaxLevel = src->MaxLevel;
207 dest->MaxAnisotropy = src->MaxAnisotropy;
208 dest->CompareFlag = src->CompareFlag;
209 dest->CompareOperator = src->CompareOperator;
210 dest->ShadowAmbient = src->ShadowAmbient;
211 dest->CompareMode = src->CompareMode;
212 dest->CompareFunc = src->CompareFunc;
213 dest->DepthMode = src->DepthMode;
214 dest->_MaxLevel = src->_MaxLevel;
215 dest->_MaxLambda = src->_MaxLambda;
216 dest->GenerateMipmap = src->GenerateMipmap;
217 dest->Palette = src->Palette;
218 dest->Complete = src->Complete;
219 }
220
221
222 /**
223 * Report why a texture object is incomplete.
224 *
225 * \param t texture object.
226 * \param why string describing why it's incomplete.
227 *
228 * \note For debug purposes only.
229 */
230 #if 0
231 static void
232 incomplete(const struct gl_texture_object *t, const char *why)
233 {
234 _mesa_printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
235 }
236 #else
237 #define incomplete(t, why)
238 #endif
239
240
241 /**
242 * Examine a texture object to determine if it is complete.
243 *
244 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
245 * accordingly.
246 *
247 * \param ctx GL context.
248 * \param t texture object.
249 *
250 * According to the texture target, verifies that each of the mipmaps is
251 * present and has the expected size.
252 */
253 void
254 _mesa_test_texobj_completeness( const GLcontext *ctx,
255 struct gl_texture_object *t )
256 {
257 const GLint baseLevel = t->BaseLevel;
258 GLint maxLog2 = 0, maxLevels = 0;
259
260 t->Complete = GL_TRUE; /* be optimistic */
261
262 /* Always need the base level image */
263 if (!t->Image[0][baseLevel]) {
264 char s[100];
265 _mesa_sprintf(s, "obj %p (%d) Image[baseLevel=%d] == NULL",
266 (void *) t, t->Name, baseLevel);
267 incomplete(t, s);
268 t->Complete = GL_FALSE;
269 return;
270 }
271
272 /* Check width/height/depth for zero */
273 if (t->Image[0][baseLevel]->Width == 0 ||
274 t->Image[0][baseLevel]->Height == 0 ||
275 t->Image[0][baseLevel]->Depth == 0) {
276 incomplete(t, "texture width = 0");
277 t->Complete = GL_FALSE;
278 return;
279 }
280
281 /* Compute _MaxLevel */
282 if (t->Target == GL_TEXTURE_1D) {
283 maxLog2 = t->Image[0][baseLevel]->WidthLog2;
284 maxLevels = ctx->Const.MaxTextureLevels;
285 }
286 else if (t->Target == GL_TEXTURE_2D) {
287 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
288 t->Image[0][baseLevel]->HeightLog2);
289 maxLevels = ctx->Const.MaxTextureLevels;
290 }
291 else if (t->Target == GL_TEXTURE_3D) {
292 GLint max = MAX2(t->Image[0][baseLevel]->WidthLog2,
293 t->Image[0][baseLevel]->HeightLog2);
294 maxLog2 = MAX2(max, (GLint)(t->Image[0][baseLevel]->DepthLog2));
295 maxLevels = ctx->Const.Max3DTextureLevels;
296 }
297 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
298 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
299 t->Image[0][baseLevel]->HeightLog2);
300 maxLevels = ctx->Const.MaxCubeTextureLevels;
301 }
302 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
303 maxLog2 = 0; /* not applicable */
304 maxLevels = 1; /* no mipmapping */
305 }
306 else {
307 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
308 return;
309 }
310
311 ASSERT(maxLevels > 0);
312
313 t->_MaxLevel = baseLevel + maxLog2;
314 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
315 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1);
316
317 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
318 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
319
320 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
321 /* make sure that all six cube map level 0 images are the same size */
322 const GLuint w = t->Image[0][baseLevel]->Width2;
323 const GLuint h = t->Image[0][baseLevel]->Height2;
324 GLuint face;
325 for (face = 1; face < 6; face++) {
326 if (t->Image[face][baseLevel] == NULL ||
327 t->Image[face][baseLevel]->Width2 != w ||
328 t->Image[face][baseLevel]->Height2 != h) {
329 t->Complete = GL_FALSE;
330 incomplete(t, "Non-quare cubemap image");
331 return;
332 }
333 }
334 }
335
336 /* extra checking for mipmaps */
337 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
338 /*
339 * Mipmapping: determine if we have a complete set of mipmaps
340 */
341 GLint i;
342 GLint minLevel = baseLevel;
343 GLint maxLevel = t->_MaxLevel;
344
345 if (minLevel > maxLevel) {
346 t->Complete = GL_FALSE;
347 incomplete(t, "minLevel > maxLevel");
348 return;
349 }
350
351 /* Test dimension-independent attributes */
352 for (i = minLevel; i <= maxLevel; i++) {
353 if (t->Image[0][i]) {
354 if (t->Image[0][i]->TexFormat != t->Image[0][baseLevel]->TexFormat) {
355 t->Complete = GL_FALSE;
356 incomplete(t, "Format[i] != Format[baseLevel]");
357 return;
358 }
359 if (t->Image[0][i]->Border != t->Image[0][baseLevel]->Border) {
360 t->Complete = GL_FALSE;
361 incomplete(t, "Border[i] != Border[baseLevel]");
362 return;
363 }
364 }
365 }
366
367 /* Test things which depend on number of texture image dimensions */
368 if (t->Target == GL_TEXTURE_1D) {
369 /* Test 1-D mipmaps */
370 GLuint width = t->Image[0][baseLevel]->Width2;
371 for (i = baseLevel + 1; i < maxLevels; i++) {
372 if (width > 1) {
373 width /= 2;
374 }
375 if (i >= minLevel && i <= maxLevel) {
376 if (!t->Image[0][i]) {
377 t->Complete = GL_FALSE;
378 incomplete(t, "1D Image[0][i] == NULL");
379 return;
380 }
381 if (t->Image[0][i]->Width2 != width ) {
382 t->Complete = GL_FALSE;
383 incomplete(t, "1D Image[0][i] bad width");
384 return;
385 }
386 }
387 if (width == 1) {
388 return; /* found smallest needed mipmap, all done! */
389 }
390 }
391 }
392 else if (t->Target == GL_TEXTURE_2D) {
393 /* Test 2-D mipmaps */
394 GLuint width = t->Image[0][baseLevel]->Width2;
395 GLuint height = t->Image[0][baseLevel]->Height2;
396 for (i = baseLevel + 1; i < maxLevels; i++) {
397 if (width > 1) {
398 width /= 2;
399 }
400 if (height > 1) {
401 height /= 2;
402 }
403 if (i >= minLevel && i <= maxLevel) {
404 if (!t->Image[0][i]) {
405 t->Complete = GL_FALSE;
406 incomplete(t, "2D Image[0][i] == NULL");
407 return;
408 }
409 if (t->Image[0][i]->Width2 != width) {
410 t->Complete = GL_FALSE;
411 incomplete(t, "2D Image[0][i] bad width");
412 return;
413 }
414 if (t->Image[0][i]->Height2 != height) {
415 t->Complete = GL_FALSE;
416 incomplete(t, "2D Image[0][i] bad height");
417 return;
418 }
419 if (width==1 && height==1) {
420 return; /* found smallest needed mipmap, all done! */
421 }
422 }
423 }
424 }
425 else if (t->Target == GL_TEXTURE_3D) {
426 /* Test 3-D mipmaps */
427 GLuint width = t->Image[0][baseLevel]->Width2;
428 GLuint height = t->Image[0][baseLevel]->Height2;
429 GLuint depth = t->Image[0][baseLevel]->Depth2;
430 for (i = baseLevel + 1; i < maxLevels; i++) {
431 if (width > 1) {
432 width /= 2;
433 }
434 if (height > 1) {
435 height /= 2;
436 }
437 if (depth > 1) {
438 depth /= 2;
439 }
440 if (i >= minLevel && i <= maxLevel) {
441 if (!t->Image[0][i]) {
442 incomplete(t, "3D Image[0][i] == NULL");
443 t->Complete = GL_FALSE;
444 return;
445 }
446 if (t->Image[0][i]->_BaseFormat == GL_DEPTH_COMPONENT) {
447 t->Complete = GL_FALSE;
448 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
449 return;
450 }
451 if (t->Image[0][i]->Width2 != width) {
452 t->Complete = GL_FALSE;
453 incomplete(t, "3D Image[0][i] bad width");
454 return;
455 }
456 if (t->Image[0][i]->Height2 != height) {
457 t->Complete = GL_FALSE;
458 incomplete(t, "3D Image[0][i] bad height");
459 return;
460 }
461 if (t->Image[0][i]->Depth2 != depth) {
462 t->Complete = GL_FALSE;
463 incomplete(t, "3D Image[0][i] bad depth");
464 return;
465 }
466 }
467 if (width == 1 && height == 1 && depth == 1) {
468 return; /* found smallest needed mipmap, all done! */
469 }
470 }
471 }
472 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
473 /* make sure 6 cube faces are consistant */
474 GLuint width = t->Image[0][baseLevel]->Width2;
475 GLuint height = t->Image[0][baseLevel]->Height2;
476 for (i = baseLevel + 1; i < maxLevels; i++) {
477 if (width > 1) {
478 width /= 2;
479 }
480 if (height > 1) {
481 height /= 2;
482 }
483 if (i >= minLevel && i <= maxLevel) {
484 GLuint face;
485 for (face = 0; face < 6; face++) {
486 /* check that we have images defined */
487 if (!t->Image[face][i]) {
488 t->Complete = GL_FALSE;
489 incomplete(t, "CubeMap Image[n][i] == NULL");
490 return;
491 }
492 /* Don't support GL_DEPTH_COMPONENT for cube maps */
493 if (t->Image[face][i]->_BaseFormat == GL_DEPTH_COMPONENT) {
494 t->Complete = GL_FALSE;
495 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
496 return;
497 }
498 /* check that all six images have same size */
499 if (t->Image[face][i]->Width2!=width ||
500 t->Image[face][i]->Height2!=height) {
501 t->Complete = GL_FALSE;
502 incomplete(t, "CubeMap Image[n][i] bad size");
503 return;
504 }
505 }
506 }
507 if (width == 1 && height == 1) {
508 return; /* found smallest needed mipmap, all done! */
509 }
510 }
511 }
512 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
513 /* XXX special checking? */
514 }
515 else {
516 /* Target = ??? */
517 _mesa_problem(ctx, "Bug in gl_test_texture_object_completeness\n");
518 }
519 }
520 }
521
522 /*@}*/
523
524
525 /***********************************************************************/
526 /** \name API functions */
527 /*@{*/
528
529 /**
530 * Texture name generation lock.
531 *
532 * Used by _mesa_GenTextures() to guarantee that the generation and allocation
533 * of texture IDs is atomic.
534 */
535 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
536
537 /**
538 * Generate texture names.
539 *
540 * \param n number of texture names to be generated.
541 * \param textures an array in which will hold the generated texture names.
542 *
543 * \sa glGenTextures().
544 *
545 * While holding the GenTexturesLock lock, calls _mesa_HashFindFreeKeyBlock()
546 * to find a block of free texture IDs which are stored in \p textures.
547 * Corresponding empty texture objects are also generated.
548 */
549 void GLAPIENTRY
550 _mesa_GenTextures( GLsizei n, GLuint *textures )
551 {
552 GET_CURRENT_CONTEXT(ctx);
553 GLuint first;
554 GLint i;
555 ASSERT_OUTSIDE_BEGIN_END(ctx);
556
557 if (n < 0) {
558 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
559 return;
560 }
561
562 if (!textures)
563 return;
564
565 /*
566 * This must be atomic (generation and allocation of texture IDs)
567 */
568 _glthread_LOCK_MUTEX(GenTexturesLock);
569
570 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
571
572 /* Allocate new, empty texture objects */
573 for (i = 0; i < n; i++) {
574 struct gl_texture_object *texObj;
575 GLuint name = first + i;
576 GLenum target = 0;
577 texObj = (*ctx->Driver.NewTextureObject)( ctx, name, target);
578 if (!texObj) {
579 _glthread_UNLOCK_MUTEX(GenTexturesLock);
580 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
581 return;
582 }
583
584 /* insert into hash table */
585 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
586 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
587 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
588
589 textures[i] = name;
590 }
591
592 _glthread_UNLOCK_MUTEX(GenTexturesLock);
593 }
594
595
596 /**
597 * Check if the given texture object is bound to the current draw or
598 * read framebuffer. If so, Unbind it.
599 */
600 static void
601 unbind_texobj_from_fbo(GLcontext *ctx, struct gl_texture_object *texObj)
602 {
603 const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
604 GLuint i;
605
606 for (i = 0; i < n; i++) {
607 struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
608 if (fb->Name) {
609 GLuint j;
610 for (j = 0; j < BUFFER_COUNT; j++) {
611 if (fb->Attachment[j].Type == GL_TEXTURE &&
612 fb->Attachment[j].Texture == texObj) {
613 _mesa_remove_attachment(ctx, fb->Attachment + j);
614 }
615 }
616 }
617 }
618 }
619
620
621 /**
622 * Check if the given texture object is bound to any texture image units and
623 * unbind it if so.
624 * XXX all RefCount accesses should be protected by a mutex.
625 */
626 static void
627 unbind_texobj_from_texunits(GLcontext *ctx, struct gl_texture_object *texObj)
628 {
629 GLuint u;
630
631 for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
632 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
633 if (texObj == unit->Current1D) {
634 unit->Current1D = ctx->Shared->Default1D;
635 ctx->Shared->Default1D->RefCount++;
636 texObj->RefCount--;
637 if (texObj == unit->_Current)
638 unit->_Current = unit->Current1D;
639 }
640 else if (texObj == unit->Current2D) {
641 unit->Current2D = ctx->Shared->Default2D;
642 ctx->Shared->Default2D->RefCount++;
643 texObj->RefCount--;
644 if (texObj == unit->_Current)
645 unit->_Current = unit->Current2D;
646 }
647 else if (texObj == unit->Current3D) {
648 unit->Current3D = ctx->Shared->Default3D;
649 ctx->Shared->Default3D->RefCount++;
650 texObj->RefCount--;
651 if (texObj == unit->_Current)
652 unit->_Current = unit->Current3D;
653 }
654 else if (texObj == unit->CurrentCubeMap) {
655 unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
656 ctx->Shared->DefaultCubeMap->RefCount++;
657 texObj->RefCount--;
658 if (texObj == unit->_Current)
659 unit->_Current = unit->CurrentCubeMap;
660 }
661 else if (texObj == unit->CurrentRect) {
662 unit->CurrentRect = ctx->Shared->DefaultRect;
663 ctx->Shared->DefaultRect->RefCount++;
664 texObj->RefCount--;
665 if (texObj == unit->_Current)
666 unit->_Current = unit->CurrentRect;
667 }
668 }
669 }
670
671
672 /**
673 * Delete named textures.
674 *
675 * \param n number of textures to be deleted.
676 * \param textures array of texture IDs to be deleted.
677 *
678 * \sa glDeleteTextures().
679 *
680 * If we're about to delete a texture that's currently bound to any
681 * texture unit, unbind the texture first. Decrement the reference
682 * count on the texture object and delete it if it's zero.
683 * Recall that texture objects can be shared among several rendering
684 * contexts.
685 */
686 void GLAPIENTRY
687 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
688 {
689 GET_CURRENT_CONTEXT(ctx);
690 GLint i;
691 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
692
693 if (!textures)
694 return;
695
696 for (i = 0; i < n; i++) {
697 if (textures[i] > 0) {
698 struct gl_texture_object *delObj
699 = _mesa_lookup_texture(ctx, textures[i]);
700
701 if (delObj) {
702 GLboolean deleted;
703
704 _mesa_lock_texture(ctx, delObj);
705
706 /* Check if texture is bound to any framebuffer objects.
707 * If so, unbind.
708 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
709 */
710 unbind_texobj_from_fbo(ctx, delObj);
711
712 /* Check if this texture is currently bound to any texture units.
713 * If so, unbind it and decrement the reference count.
714 */
715 unbind_texobj_from_texunits(ctx, delObj);
716
717 ctx->NewState |= _NEW_TEXTURE;
718
719 /* The texture _name_ is now free for re-use.
720 * Remove it from the hash table now.
721 */
722 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
723 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
724 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
725
726 /* The actual texture object will not be freed until it's no
727 * longer bound in any context.
728 * XXX all RefCount accesses should be protected by a mutex.
729 */
730 delObj->RefCount--;
731 deleted = (delObj->RefCount == 0);
732 _mesa_unlock_texture(ctx, delObj);
733
734 /* We know that refcount went to zero above, so this is
735 * the only pointer left to delObj, so we don't have to
736 * worry about locking any more:
737 */
738 if (deleted) {
739 ASSERT(delObj->Name != 0); /* Never delete default tex objs */
740 ASSERT(ctx->Driver.DeleteTexture);
741 (*ctx->Driver.DeleteTexture)(ctx, delObj);
742 }
743 }
744 }
745 }
746 }
747
748
749 /**
750 * Bind a named texture to a texturing target.
751 *
752 * \param target texture target.
753 * \param texName texture name.
754 *
755 * \sa glBindTexture().
756 *
757 * Determines the old texture object bound and returns immediately if rebinding
758 * the same texture. Get the current texture which is either a default texture
759 * if name is null, a named texture from the hash, or a new texture if the
760 * given texture name is new. Increments its reference count, binds it, and
761 * calls dd_function_table::BindTexture. Decrements the old texture reference
762 * count and deletes it if it reaches zero.
763 */
764 void GLAPIENTRY
765 _mesa_BindTexture( GLenum target, GLuint texName )
766 {
767 GET_CURRENT_CONTEXT(ctx);
768 const GLuint unit = ctx->Texture.CurrentUnit;
769 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
770 struct gl_texture_object *oldTexObj;
771 struct gl_texture_object *newTexObj = NULL;
772 ASSERT_OUTSIDE_BEGIN_END(ctx);
773
774 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
775 _mesa_debug(ctx, "glBindTexture %s %d\n",
776 _mesa_lookup_enum_by_nr(target), (GLint) texName);
777
778 /*
779 * Get pointer to currently bound texture object (oldTexObj)
780 */
781 switch (target) {
782 case GL_TEXTURE_1D:
783 oldTexObj = texUnit->Current1D;
784 break;
785 case GL_TEXTURE_2D:
786 oldTexObj = texUnit->Current2D;
787 break;
788 case GL_TEXTURE_3D:
789 oldTexObj = texUnit->Current3D;
790 break;
791 case GL_TEXTURE_CUBE_MAP_ARB:
792 if (!ctx->Extensions.ARB_texture_cube_map) {
793 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
794 return;
795 }
796 oldTexObj = texUnit->CurrentCubeMap;
797 break;
798 case GL_TEXTURE_RECTANGLE_NV:
799 if (!ctx->Extensions.NV_texture_rectangle) {
800 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
801 return;
802 }
803 oldTexObj = texUnit->CurrentRect;
804 break;
805 default:
806 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
807 return;
808 }
809
810 if (oldTexObj->Name == texName) {
811 /* XXX this might be wrong. If the texobj is in use by another
812 * context and a texobj parameter was changed, this might be our
813 * only chance to update this context's hardware state.
814 * Note that some applications re-bind the same texture a lot so we
815 * want to handle that case quickly.
816 */
817 return; /* rebinding the same texture- no change */
818 }
819
820 /*
821 * Get pointer to new texture object (newTexObj)
822 */
823 if (texName == 0) {
824 /* newTexObj = a default texture object */
825 switch (target) {
826 case GL_TEXTURE_1D:
827 newTexObj = ctx->Shared->Default1D;
828 break;
829 case GL_TEXTURE_2D:
830 newTexObj = ctx->Shared->Default2D;
831 break;
832 case GL_TEXTURE_3D:
833 newTexObj = ctx->Shared->Default3D;
834 break;
835 case GL_TEXTURE_CUBE_MAP_ARB:
836 newTexObj = ctx->Shared->DefaultCubeMap;
837 break;
838 case GL_TEXTURE_RECTANGLE_NV:
839 newTexObj = ctx->Shared->DefaultRect;
840 break;
841 default:
842 ; /* Bad targets are caught above */
843 }
844 }
845 else {
846 /* non-default texture object */
847 newTexObj = _mesa_lookup_texture(ctx, texName);
848 if (newTexObj) {
849 /* error checking */
850 if (newTexObj->Target != 0 && newTexObj->Target != target) {
851 /* the named texture object's dimensions don't match the target */
852 _mesa_error( ctx, GL_INVALID_OPERATION,
853 "glBindTexture(wrong dimensionality)" );
854 return;
855 }
856 if (newTexObj->Target == 0 && target == GL_TEXTURE_RECTANGLE_NV) {
857 /* have to init wrap and filter state here - kind of klunky */
858 newTexObj->WrapS = GL_CLAMP_TO_EDGE;
859 newTexObj->WrapT = GL_CLAMP_TO_EDGE;
860 newTexObj->WrapR = GL_CLAMP_TO_EDGE;
861 newTexObj->MinFilter = GL_LINEAR;
862 if (ctx->Driver.TexParameter) {
863 static const GLfloat fparam_wrap[1] = {(GLfloat) GL_CLAMP_TO_EDGE};
864 static const GLfloat fparam_filter[1] = {(GLfloat) GL_LINEAR};
865 (*ctx->Driver.TexParameter)( ctx, target, newTexObj, GL_TEXTURE_WRAP_S, fparam_wrap );
866 (*ctx->Driver.TexParameter)( ctx, target, newTexObj, GL_TEXTURE_WRAP_T, fparam_wrap );
867 (*ctx->Driver.TexParameter)( ctx, target, newTexObj, GL_TEXTURE_WRAP_R, fparam_wrap );
868 (*ctx->Driver.TexParameter)( ctx, target, newTexObj, GL_TEXTURE_MIN_FILTER, fparam_filter );
869 }
870 }
871 }
872 else {
873 /* if this is a new texture id, allocate a texture object now */
874 newTexObj = (*ctx->Driver.NewTextureObject)(ctx, texName, target);
875 if (!newTexObj) {
876 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
877 return;
878 }
879
880 /* and insert it into hash table */
881 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
882 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
883 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
884 }
885 newTexObj->Target = target;
886 }
887
888 /* XXX all RefCount accesses should be protected by a mutex. */
889 newTexObj->RefCount++;
890
891 /* do the actual binding, but first flush outstanding vertices:
892 */
893 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
894
895 switch (target) {
896 case GL_TEXTURE_1D:
897 texUnit->Current1D = newTexObj;
898 break;
899 case GL_TEXTURE_2D:
900 texUnit->Current2D = newTexObj;
901 break;
902 case GL_TEXTURE_3D:
903 texUnit->Current3D = newTexObj;
904 break;
905 case GL_TEXTURE_CUBE_MAP_ARB:
906 texUnit->CurrentCubeMap = newTexObj;
907 break;
908 case GL_TEXTURE_RECTANGLE_NV:
909 texUnit->CurrentRect = newTexObj;
910 break;
911 default:
912 _mesa_problem(ctx, "bad target in BindTexture");
913 return;
914 }
915
916 /* Pass BindTexture call to device driver */
917 if (ctx->Driver.BindTexture)
918 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
919
920 /* Decrement the reference count on the old texture and check if it's
921 * time to delete it.
922 */
923 /* XXX all RefCount accesses should be protected by a mutex. */
924 oldTexObj->RefCount--;
925 ASSERT(oldTexObj->RefCount >= 0);
926 if (oldTexObj->RefCount == 0) {
927 ASSERT(oldTexObj->Name != 0);
928 ASSERT(ctx->Driver.DeleteTexture);
929 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
930 }
931 }
932
933
934 /**
935 * Set texture priorities.
936 *
937 * \param n number of textures.
938 * \param texName texture names.
939 * \param priorities corresponding texture priorities.
940 *
941 * \sa glPrioritizeTextures().
942 *
943 * Looks up each texture in the hash, clamps the corresponding priority between
944 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
945 */
946 void GLAPIENTRY
947 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
948 const GLclampf *priorities )
949 {
950 GET_CURRENT_CONTEXT(ctx);
951 GLint i;
952 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
953
954 if (n < 0) {
955 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
956 return;
957 }
958
959 if (!priorities)
960 return;
961
962 for (i = 0; i < n; i++) {
963 if (texName[i] > 0) {
964 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
965 if (t) {
966 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
967 if (ctx->Driver.PrioritizeTexture)
968 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
969 }
970 }
971 }
972
973 ctx->NewState |= _NEW_TEXTURE;
974 }
975
976 /**
977 * See if textures are loaded in texture memory.
978 *
979 * \param n number of textures to query.
980 * \param texName array with the texture names.
981 * \param residences array which will hold the residence status.
982 *
983 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
984 *
985 * \sa glAreTexturesResident().
986 *
987 * Looks up each texture in the hash and calls
988 * dd_function_table::IsTextureResident.
989 */
990 GLboolean GLAPIENTRY
991 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
992 GLboolean *residences)
993 {
994 GET_CURRENT_CONTEXT(ctx);
995 GLboolean allResident = GL_TRUE;
996 GLint i, j;
997 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
998
999 if (n < 0) {
1000 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1001 return GL_FALSE;
1002 }
1003
1004 if (!texName || !residences)
1005 return GL_FALSE;
1006
1007 for (i = 0; i < n; i++) {
1008 struct gl_texture_object *t;
1009 if (texName[i] == 0) {
1010 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1011 return GL_FALSE;
1012 }
1013 t = _mesa_lookup_texture(ctx, texName[i]);
1014 if (!t) {
1015 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1016 return GL_FALSE;
1017 }
1018 if (!ctx->Driver.IsTextureResident ||
1019 ctx->Driver.IsTextureResident(ctx, t)) {
1020 /* The texture is resident */
1021 if (!allResident)
1022 residences[i] = GL_TRUE;
1023 }
1024 else {
1025 /* The texture is not resident */
1026 if (allResident) {
1027 allResident = GL_FALSE;
1028 for (j = 0; j < i; j++)
1029 residences[j] = GL_TRUE;
1030 }
1031 residences[i] = GL_FALSE;
1032 }
1033 }
1034
1035 return allResident;
1036 }
1037
1038 /**
1039 * See if a name corresponds to a texture.
1040 *
1041 * \param texture texture name.
1042 *
1043 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1044 * otherwise.
1045 *
1046 * \sa glIsTexture().
1047 *
1048 * Calls _mesa_HashLookup().
1049 */
1050 GLboolean GLAPIENTRY
1051 _mesa_IsTexture( GLuint texture )
1052 {
1053 struct gl_texture_object *t;
1054 GET_CURRENT_CONTEXT(ctx);
1055 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1056
1057 if (!texture)
1058 return GL_FALSE;
1059
1060 t = _mesa_lookup_texture(ctx, texture);
1061
1062 /* IsTexture is true only after object has been bound once. */
1063 return t && t->Target;
1064 }
1065
1066 /* Simplest implementation of texture locking: Grab the a new mutex in
1067 * the shared context. Examine the shared context state timestamp and
1068 * if there has been a change, set the appropriate bits in
1069 * ctx->NewState.
1070 *
1071 * See also _mesa_lock/unlock_texture in texobj.h
1072 */
1073 void _mesa_lock_context_textures( GLcontext *ctx )
1074 {
1075 _glthread_LOCK_MUTEX(ctx->Shared->TexMutex);
1076
1077 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
1078 ctx->NewState |= _NEW_TEXTURE;
1079 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
1080 }
1081 }
1082
1083
1084 void _mesa_unlock_context_textures( GLcontext *ctx )
1085 {
1086 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
1087 _glthread_UNLOCK_MUTEX(ctx->Shared->TexMutex);
1088 }
1089
1090 /*@}*/
1091
1092