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