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