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