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