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