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