c96226df5976e499fedd30f7a7702683432093d5
[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 "mfeatures.h"
32 #include "colortab.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "fbobject.h"
36 #include "formats.h"
37 #include "hash.h"
38 #include "imports.h"
39 #include "macros.h"
40 #include "teximage.h"
41 #include "texobj.h"
42 #include "mtypes.h"
43 #include "program/prog_instruction.h"
44
45
46
47 /**********************************************************************/
48 /** \name Internal functions */
49 /*@{*/
50
51
52 /**
53 * Return the gl_texture_object for a given ID.
54 */
55 struct gl_texture_object *
56 _mesa_lookup_texture(GLcontext *ctx, GLuint id)
57 {
58 return (struct gl_texture_object *)
59 _mesa_HashLookup(ctx->Shared->TexObjects, id);
60 }
61
62
63
64 /**
65 * Allocate and initialize a new texture object. But don't put it into the
66 * texture object hash table.
67 *
68 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
69 * driver.
70 *
71 * \param shared the shared GL state structure to contain the texture object
72 * \param name integer name for the texture object
73 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
74 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
75 * of GenTextures()
76 *
77 * \return pointer to new texture object.
78 */
79 struct gl_texture_object *
80 _mesa_new_texture_object( GLcontext *ctx, GLuint name, GLenum target )
81 {
82 struct gl_texture_object *obj;
83 (void) ctx;
84 obj = MALLOC_STRUCT(gl_texture_object);
85 _mesa_initialize_texture_object(obj, name, target);
86 return obj;
87 }
88
89
90 /**
91 * Initialize a new texture object to default values.
92 * \param obj the texture object
93 * \param name the texture name
94 * \param target the texture target
95 */
96 void
97 _mesa_initialize_texture_object( struct gl_texture_object *obj,
98 GLuint name, GLenum target )
99 {
100 ASSERT(target == 0 ||
101 target == GL_TEXTURE_1D ||
102 target == GL_TEXTURE_2D ||
103 target == GL_TEXTURE_3D ||
104 target == GL_TEXTURE_CUBE_MAP_ARB ||
105 target == GL_TEXTURE_RECTANGLE_NV ||
106 target == GL_TEXTURE_1D_ARRAY_EXT ||
107 target == GL_TEXTURE_2D_ARRAY_EXT);
108
109 memset(obj, 0, sizeof(*obj));
110 /* init the non-zero fields */
111 _glthread_INIT_MUTEX(obj->Mutex);
112 obj->RefCount = 1;
113 obj->Name = name;
114 obj->Target = target;
115 obj->Priority = 1.0F;
116 if (target == GL_TEXTURE_RECTANGLE_NV) {
117 obj->WrapS = GL_CLAMP_TO_EDGE;
118 obj->WrapT = GL_CLAMP_TO_EDGE;
119 obj->WrapR = GL_CLAMP_TO_EDGE;
120 obj->MinFilter = GL_LINEAR;
121 }
122 else {
123 obj->WrapS = GL_REPEAT;
124 obj->WrapT = GL_REPEAT;
125 obj->WrapR = GL_REPEAT;
126 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
127 }
128 obj->MagFilter = GL_LINEAR;
129 obj->MinLod = -1000.0;
130 obj->MaxLod = 1000.0;
131 obj->LodBias = 0.0;
132 obj->BaseLevel = 0;
133 obj->MaxLevel = 1000;
134 obj->MaxAnisotropy = 1.0;
135 obj->CompareMode = GL_NONE; /* ARB_shadow */
136 obj->CompareFunc = GL_LEQUAL; /* ARB_shadow */
137 obj->CompareFailValue = 0.0F; /* ARB_shadow_ambient */
138 obj->DepthMode = GL_LUMINANCE; /* ARB_depth_texture */
139 obj->Swizzle[0] = GL_RED;
140 obj->Swizzle[1] = GL_GREEN;
141 obj->Swizzle[2] = GL_BLUE;
142 obj->Swizzle[3] = GL_ALPHA;
143 obj->_Swizzle = SWIZZLE_NOOP;
144 }
145
146
147 /**
148 * Some texture initialization can't be finished until we know which
149 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
150 */
151 static void
152 finish_texture_init(GLcontext *ctx, GLenum target,
153 struct gl_texture_object *obj)
154 {
155 assert(obj->Target == 0);
156
157 if (target == GL_TEXTURE_RECTANGLE_NV) {
158 /* have to init wrap and filter state here - kind of klunky */
159 obj->WrapS = GL_CLAMP_TO_EDGE;
160 obj->WrapT = GL_CLAMP_TO_EDGE;
161 obj->WrapR = GL_CLAMP_TO_EDGE;
162 obj->MinFilter = GL_LINEAR;
163 if (ctx->Driver.TexParameter) {
164 static const GLfloat fparam_wrap[1] = {(GLfloat) GL_CLAMP_TO_EDGE};
165 static const GLfloat fparam_filter[1] = {(GLfloat) GL_LINEAR};
166 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_S, fparam_wrap);
167 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_T, fparam_wrap);
168 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_R, fparam_wrap);
169 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_MIN_FILTER, fparam_filter);
170 }
171 }
172 }
173
174
175 /**
176 * Deallocate a texture object struct. It should have already been
177 * removed from the texture object pool.
178 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
179 *
180 * \param shared the shared GL state to which the object belongs.
181 * \param texObj the texture object to delete.
182 */
183 void
184 _mesa_delete_texture_object( GLcontext *ctx, struct gl_texture_object *texObj )
185 {
186 GLuint i, face;
187
188 (void) ctx;
189
190 /* Set Target to an invalid value. With some assertions elsewhere
191 * we can try to detect possible use of deleted textures.
192 */
193 texObj->Target = 0x99;
194
195 _mesa_free_colortable_data(&texObj->Palette);
196
197 /* free the texture images */
198 for (face = 0; face < 6; face++) {
199 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
200 if (texObj->Image[face][i]) {
201 _mesa_delete_texture_image( ctx, texObj->Image[face][i] );
202 }
203 }
204 }
205
206 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
207 _glthread_DESTROY_MUTEX(texObj->Mutex);
208
209 /* free this object */
210 free(texObj);
211 }
212
213
214
215
216 /**
217 * Copy texture object state from one texture object to another.
218 * Use for glPush/PopAttrib.
219 *
220 * \param dest destination texture object.
221 * \param src source texture object.
222 */
223 void
224 _mesa_copy_texture_object( struct gl_texture_object *dest,
225 const struct gl_texture_object *src )
226 {
227 dest->Target = src->Target;
228 dest->Name = src->Name;
229 dest->Priority = src->Priority;
230 dest->BorderColor.f[0] = src->BorderColor.f[0];
231 dest->BorderColor.f[1] = src->BorderColor.f[1];
232 dest->BorderColor.f[2] = src->BorderColor.f[2];
233 dest->BorderColor.f[3] = src->BorderColor.f[3];
234 dest->WrapS = src->WrapS;
235 dest->WrapT = src->WrapT;
236 dest->WrapR = src->WrapR;
237 dest->MinFilter = src->MinFilter;
238 dest->MagFilter = src->MagFilter;
239 dest->MinLod = src->MinLod;
240 dest->MaxLod = src->MaxLod;
241 dest->LodBias = src->LodBias;
242 dest->BaseLevel = src->BaseLevel;
243 dest->MaxLevel = src->MaxLevel;
244 dest->MaxAnisotropy = src->MaxAnisotropy;
245 dest->CompareMode = src->CompareMode;
246 dest->CompareFunc = src->CompareFunc;
247 dest->CompareFailValue = src->CompareFailValue;
248 dest->DepthMode = src->DepthMode;
249 dest->_MaxLevel = src->_MaxLevel;
250 dest->_MaxLambda = src->_MaxLambda;
251 dest->GenerateMipmap = src->GenerateMipmap;
252 dest->Palette = src->Palette;
253 dest->_Complete = src->_Complete;
254 COPY_4V(dest->Swizzle, src->Swizzle);
255 dest->_Swizzle = src->_Swizzle;
256 }
257
258
259 /**
260 * Clear all texture images of the given texture object.
261 *
262 * \param ctx GL context.
263 * \param t texture object.
264 *
265 * \sa _mesa_clear_texture_image().
266 */
267 void
268 _mesa_clear_texture_object(GLcontext *ctx, struct gl_texture_object *texObj)
269 {
270 GLuint i, j;
271
272 if (texObj->Target == 0)
273 return;
274
275 for (i = 0; i < MAX_FACES; i++) {
276 for (j = 0; j < MAX_TEXTURE_LEVELS; j++) {
277 struct gl_texture_image *texImage = texObj->Image[i][j];
278 if (texImage)
279 _mesa_clear_texture_image(ctx, texImage);
280 }
281 }
282 }
283
284
285 /**
286 * Check if the given texture object is valid by examining its Target field.
287 * For debugging only.
288 */
289 static GLboolean
290 valid_texture_object(const struct gl_texture_object *tex)
291 {
292 switch (tex->Target) {
293 case 0:
294 case GL_TEXTURE_1D:
295 case GL_TEXTURE_2D:
296 case GL_TEXTURE_3D:
297 case GL_TEXTURE_CUBE_MAP_ARB:
298 case GL_TEXTURE_RECTANGLE_NV:
299 case GL_TEXTURE_1D_ARRAY_EXT:
300 case GL_TEXTURE_2D_ARRAY_EXT:
301 return GL_TRUE;
302 case 0x99:
303 _mesa_problem(NULL, "invalid reference to a deleted texture object");
304 return GL_FALSE;
305 default:
306 _mesa_problem(NULL, "invalid texture object Target 0x%x, Id = %u",
307 tex->Target, tex->Name);
308 return GL_FALSE;
309 }
310 }
311
312
313 /**
314 * Reference (or unreference) a texture object.
315 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
316 * If 'tex' is non-null, increment its refcount.
317 */
318 void
319 _mesa_reference_texobj(struct gl_texture_object **ptr,
320 struct gl_texture_object *tex)
321 {
322 assert(ptr);
323 if (*ptr == tex) {
324 /* no change */
325 return;
326 }
327
328 if (*ptr) {
329 /* Unreference the old texture */
330 GLboolean deleteFlag = GL_FALSE;
331 struct gl_texture_object *oldTex = *ptr;
332
333 {
334 GLboolean valid = valid_texture_object(oldTex);
335 ASSERT(valid);
336 (void) valid;
337 }
338
339 _glthread_LOCK_MUTEX(oldTex->Mutex);
340 ASSERT(oldTex->RefCount > 0);
341 oldTex->RefCount--;
342
343 deleteFlag = (oldTex->RefCount == 0);
344 _glthread_UNLOCK_MUTEX(oldTex->Mutex);
345
346 if (deleteFlag) {
347 GET_CURRENT_CONTEXT(ctx);
348 if (ctx)
349 ctx->Driver.DeleteTexture(ctx, oldTex);
350 else
351 _mesa_problem(NULL, "Unable to delete texture, no context");
352 }
353
354 *ptr = NULL;
355 }
356 assert(!*ptr);
357
358 if (tex) {
359 /* reference new texture */
360 ASSERT(valid_texture_object(tex));
361 _glthread_LOCK_MUTEX(tex->Mutex);
362 if (tex->RefCount == 0) {
363 /* this texture's being deleted (look just above) */
364 /* Not sure this can every really happen. Warn if it does. */
365 _mesa_problem(NULL, "referencing deleted texture object");
366 *ptr = NULL;
367 }
368 else {
369 tex->RefCount++;
370 *ptr = tex;
371 }
372 _glthread_UNLOCK_MUTEX(tex->Mutex);
373 }
374 }
375
376
377
378 /**
379 * Report why a texture object is incomplete.
380 *
381 * \param t texture object.
382 * \param why string describing why it's incomplete.
383 *
384 * \note For debug purposes only.
385 */
386 #if 0
387 static void
388 incomplete(const struct gl_texture_object *t, const char *why)
389 {
390 printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
391 }
392 #else
393 #define incomplete(t, why)
394 #endif
395
396
397 /**
398 * Examine a texture object to determine if it is complete.
399 *
400 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
401 * accordingly.
402 *
403 * \param ctx GL context.
404 * \param t texture object.
405 *
406 * According to the texture target, verifies that each of the mipmaps is
407 * present and has the expected size.
408 */
409 void
410 _mesa_test_texobj_completeness( const GLcontext *ctx,
411 struct gl_texture_object *t )
412 {
413 const GLint baseLevel = t->BaseLevel;
414 GLint maxLog2 = 0, maxLevels = 0;
415
416 t->_Complete = GL_TRUE; /* be optimistic */
417
418 /* Detect cases where the application set the base level to an invalid
419 * value.
420 */
421 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS)) {
422 char s[100];
423 _mesa_snprintf(s, sizeof(s), "base level = %d is invalid", baseLevel);
424 incomplete(t, s);
425 t->_Complete = GL_FALSE;
426 return;
427 }
428
429 /* Always need the base level image */
430 if (!t->Image[0][baseLevel]) {
431 char s[100];
432 _mesa_snprintf(s, sizeof(s), "Image[baseLevel=%d] == NULL", baseLevel);
433 incomplete(t, s);
434 t->_Complete = GL_FALSE;
435 return;
436 }
437
438 /* Check width/height/depth for zero */
439 if (t->Image[0][baseLevel]->Width == 0 ||
440 t->Image[0][baseLevel]->Height == 0 ||
441 t->Image[0][baseLevel]->Depth == 0) {
442 incomplete(t, "texture width = 0");
443 t->_Complete = GL_FALSE;
444 return;
445 }
446
447 /* Compute _MaxLevel */
448 if ((t->Target == GL_TEXTURE_1D) ||
449 (t->Target == GL_TEXTURE_1D_ARRAY_EXT)) {
450 maxLog2 = t->Image[0][baseLevel]->WidthLog2;
451 maxLevels = ctx->Const.MaxTextureLevels;
452 }
453 else if ((t->Target == GL_TEXTURE_2D) ||
454 (t->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
455 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
456 t->Image[0][baseLevel]->HeightLog2);
457 maxLevels = ctx->Const.MaxTextureLevels;
458 }
459 else if (t->Target == GL_TEXTURE_3D) {
460 GLint max = MAX2(t->Image[0][baseLevel]->WidthLog2,
461 t->Image[0][baseLevel]->HeightLog2);
462 maxLog2 = MAX2(max, (GLint)(t->Image[0][baseLevel]->DepthLog2));
463 maxLevels = ctx->Const.Max3DTextureLevels;
464 }
465 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
466 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
467 t->Image[0][baseLevel]->HeightLog2);
468 maxLevels = ctx->Const.MaxCubeTextureLevels;
469 }
470 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
471 maxLog2 = 0; /* not applicable */
472 maxLevels = 1; /* no mipmapping */
473 }
474 else {
475 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
476 return;
477 }
478
479 ASSERT(maxLevels > 0);
480
481 t->_MaxLevel = baseLevel + maxLog2;
482 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
483 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1);
484
485 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
486 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
487
488 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
489 /* make sure that all six cube map level 0 images are the same size */
490 const GLuint w = t->Image[0][baseLevel]->Width2;
491 const GLuint h = t->Image[0][baseLevel]->Height2;
492 GLuint face;
493 for (face = 1; face < 6; face++) {
494 if (t->Image[face][baseLevel] == NULL ||
495 t->Image[face][baseLevel]->Width2 != w ||
496 t->Image[face][baseLevel]->Height2 != h) {
497 t->_Complete = GL_FALSE;
498 incomplete(t, "Cube face missing or mismatched size");
499 return;
500 }
501 }
502 }
503
504 /* extra checking for mipmaps */
505 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
506 /*
507 * Mipmapping: determine if we have a complete set of mipmaps
508 */
509 GLint i;
510 GLint minLevel = baseLevel;
511 GLint maxLevel = t->_MaxLevel;
512
513 if (minLevel > maxLevel) {
514 t->_Complete = GL_FALSE;
515 incomplete(t, "minLevel > maxLevel");
516 return;
517 }
518
519 /* Test dimension-independent attributes */
520 for (i = minLevel; i <= maxLevel; i++) {
521 if (t->Image[0][i]) {
522 if (t->Image[0][i]->TexFormat != t->Image[0][baseLevel]->TexFormat) {
523 t->_Complete = GL_FALSE;
524 incomplete(t, "Format[i] != Format[baseLevel]");
525 return;
526 }
527 if (t->Image[0][i]->Border != t->Image[0][baseLevel]->Border) {
528 t->_Complete = GL_FALSE;
529 incomplete(t, "Border[i] != Border[baseLevel]");
530 return;
531 }
532 }
533 }
534
535 /* Test things which depend on number of texture image dimensions */
536 if ((t->Target == GL_TEXTURE_1D) ||
537 (t->Target == GL_TEXTURE_1D_ARRAY_EXT)) {
538 /* Test 1-D mipmaps */
539 GLuint width = t->Image[0][baseLevel]->Width2;
540 for (i = baseLevel + 1; i < maxLevels; i++) {
541 if (width > 1) {
542 width /= 2;
543 }
544 if (i >= minLevel && i <= maxLevel) {
545 if (!t->Image[0][i]) {
546 t->_Complete = GL_FALSE;
547 incomplete(t, "1D Image[0][i] == NULL");
548 return;
549 }
550 if (t->Image[0][i]->Width2 != width ) {
551 t->_Complete = GL_FALSE;
552 incomplete(t, "1D Image[0][i] bad width");
553 return;
554 }
555 }
556 if (width == 1) {
557 return; /* found smallest needed mipmap, all done! */
558 }
559 }
560 }
561 else if ((t->Target == GL_TEXTURE_2D) ||
562 (t->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
563 /* Test 2-D mipmaps */
564 GLuint width = t->Image[0][baseLevel]->Width2;
565 GLuint height = t->Image[0][baseLevel]->Height2;
566 for (i = baseLevel + 1; i < maxLevels; i++) {
567 if (width > 1) {
568 width /= 2;
569 }
570 if (height > 1) {
571 height /= 2;
572 }
573 if (i >= minLevel && i <= maxLevel) {
574 if (!t->Image[0][i]) {
575 t->_Complete = GL_FALSE;
576 incomplete(t, "2D Image[0][i] == NULL");
577 return;
578 }
579 if (t->Image[0][i]->Width2 != width) {
580 t->_Complete = GL_FALSE;
581 incomplete(t, "2D Image[0][i] bad width");
582 return;
583 }
584 if (t->Image[0][i]->Height2 != height) {
585 t->_Complete = GL_FALSE;
586 incomplete(t, "2D Image[0][i] bad height");
587 return;
588 }
589 if (width==1 && height==1) {
590 return; /* found smallest needed mipmap, all done! */
591 }
592 }
593 }
594 }
595 else if (t->Target == GL_TEXTURE_3D) {
596 /* Test 3-D mipmaps */
597 GLuint width = t->Image[0][baseLevel]->Width2;
598 GLuint height = t->Image[0][baseLevel]->Height2;
599 GLuint depth = t->Image[0][baseLevel]->Depth2;
600 for (i = baseLevel + 1; i < maxLevels; i++) {
601 if (width > 1) {
602 width /= 2;
603 }
604 if (height > 1) {
605 height /= 2;
606 }
607 if (depth > 1) {
608 depth /= 2;
609 }
610 if (i >= minLevel && i <= maxLevel) {
611 if (!t->Image[0][i]) {
612 incomplete(t, "3D Image[0][i] == NULL");
613 t->_Complete = GL_FALSE;
614 return;
615 }
616 if (t->Image[0][i]->_BaseFormat == GL_DEPTH_COMPONENT) {
617 t->_Complete = GL_FALSE;
618 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
619 return;
620 }
621 if (t->Image[0][i]->Width2 != width) {
622 t->_Complete = GL_FALSE;
623 incomplete(t, "3D Image[0][i] bad width");
624 return;
625 }
626 if (t->Image[0][i]->Height2 != height) {
627 t->_Complete = GL_FALSE;
628 incomplete(t, "3D Image[0][i] bad height");
629 return;
630 }
631 if (t->Image[0][i]->Depth2 != depth) {
632 t->_Complete = GL_FALSE;
633 incomplete(t, "3D Image[0][i] bad depth");
634 return;
635 }
636 }
637 if (width == 1 && height == 1 && depth == 1) {
638 return; /* found smallest needed mipmap, all done! */
639 }
640 }
641 }
642 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
643 /* make sure 6 cube faces are consistant */
644 GLuint width = t->Image[0][baseLevel]->Width2;
645 GLuint height = t->Image[0][baseLevel]->Height2;
646 for (i = baseLevel + 1; i < maxLevels; i++) {
647 if (width > 1) {
648 width /= 2;
649 }
650 if (height > 1) {
651 height /= 2;
652 }
653 if (i >= minLevel && i <= maxLevel) {
654 GLuint face;
655 for (face = 0; face < 6; face++) {
656 /* check that we have images defined */
657 if (!t->Image[face][i]) {
658 t->_Complete = GL_FALSE;
659 incomplete(t, "CubeMap Image[n][i] == NULL");
660 return;
661 }
662 /* Don't support GL_DEPTH_COMPONENT for cube maps */
663 if (t->Image[face][i]->_BaseFormat == GL_DEPTH_COMPONENT) {
664 t->_Complete = GL_FALSE;
665 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
666 return;
667 }
668 /* check that all six images have same size */
669 if (t->Image[face][i]->Width2!=width ||
670 t->Image[face][i]->Height2!=height) {
671 t->_Complete = GL_FALSE;
672 incomplete(t, "CubeMap Image[n][i] bad size");
673 return;
674 }
675 }
676 }
677 if (width == 1 && height == 1) {
678 return; /* found smallest needed mipmap, all done! */
679 }
680 }
681 }
682 else if (t->Target == GL_TEXTURE_RECTANGLE_NV) {
683 /* XXX special checking? */
684 }
685 else {
686 /* Target = ??? */
687 _mesa_problem(ctx, "Bug in gl_test_texture_object_completeness\n");
688 }
689 }
690 }
691
692
693 /**
694 * Mark a texture object dirty. It forces the object to be incomplete
695 * and optionally forces the context to re-validate its state.
696 *
697 * \param ctx GL context.
698 * \param texObj texture object.
699 * \param invalidate_state also invalidate context state.
700 */
701 void
702 _mesa_dirty_texobj(GLcontext *ctx, struct gl_texture_object *texObj,
703 GLboolean invalidate_state)
704 {
705 texObj->_Complete = GL_FALSE;
706 if (invalidate_state)
707 ctx->NewState |= _NEW_TEXTURE;
708 }
709
710
711 /**
712 * Return pointer to a default/fallback texture.
713 * The texture is a 2D 8x8 RGBA texture with all texels = (0,0,0,1).
714 * That's the value a sampler should get when sampling from an
715 * incomplete texture.
716 */
717 struct gl_texture_object *
718 _mesa_get_fallback_texture(GLcontext *ctx)
719 {
720 if (!ctx->Shared->FallbackTex) {
721 /* create fallback texture now */
722 static GLubyte texels[8 * 8][4];
723 struct gl_texture_object *texObj;
724 struct gl_texture_image *texImage;
725 GLuint i;
726
727 for (i = 0; i < 8 * 8; i++) {
728 texels[i][0] =
729 texels[i][1] =
730 texels[i][2] = 0x0;
731 texels[i][3] = 0xff;
732 }
733
734 /* create texture object */
735 texObj = ctx->Driver.NewTextureObject(ctx, 0, GL_TEXTURE_2D);
736 assert(texObj->RefCount == 1);
737 texObj->MinFilter = GL_NEAREST;
738 texObj->MagFilter = GL_NEAREST;
739
740 /* create level[0] texture image */
741 texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, 0);
742
743 /* init the image fields */
744 _mesa_init_teximage_fields(ctx, GL_TEXTURE_2D, texImage,
745 8, 8, 1, 0, GL_RGBA);
746
747 texImage->TexFormat =
748 ctx->Driver.ChooseTextureFormat(ctx, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
749 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
750
751 /* set image data */
752 ctx->Driver.TexImage2D(ctx, GL_TEXTURE_2D, 0, GL_RGBA,
753 8, 8, 0,
754 GL_RGBA, GL_UNSIGNED_BYTE, texels,
755 &ctx->DefaultPacking, texObj, texImage);
756
757 _mesa_test_texobj_completeness(ctx, texObj);
758 assert(texObj->_Complete);
759
760 ctx->Shared->FallbackTex = texObj;
761 }
762 return ctx->Shared->FallbackTex;
763 }
764
765
766 /*@}*/
767
768
769 /***********************************************************************/
770 /** \name API functions */
771 /*@{*/
772
773
774 /**
775 * Generate texture names.
776 *
777 * \param n number of texture names to be generated.
778 * \param textures an array in which will hold the generated texture names.
779 *
780 * \sa glGenTextures().
781 *
782 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
783 * IDs which are stored in \p textures. Corresponding empty texture
784 * objects are also generated.
785 */
786 void GLAPIENTRY
787 _mesa_GenTextures( GLsizei n, GLuint *textures )
788 {
789 GET_CURRENT_CONTEXT(ctx);
790 GLuint first;
791 GLint i;
792 ASSERT_OUTSIDE_BEGIN_END(ctx);
793
794 if (n < 0) {
795 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
796 return;
797 }
798
799 if (!textures)
800 return;
801
802 /*
803 * This must be atomic (generation and allocation of texture IDs)
804 */
805 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
806
807 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
808
809 /* Allocate new, empty texture objects */
810 for (i = 0; i < n; i++) {
811 struct gl_texture_object *texObj;
812 GLuint name = first + i;
813 GLenum target = 0;
814 texObj = (*ctx->Driver.NewTextureObject)( ctx, name, target);
815 if (!texObj) {
816 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
817 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
818 return;
819 }
820
821 /* insert into hash table */
822 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
823
824 textures[i] = name;
825 }
826
827 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
828 }
829
830
831 /**
832 * Check if the given texture object is bound to the current draw or
833 * read framebuffer. If so, Unbind it.
834 */
835 static void
836 unbind_texobj_from_fbo(GLcontext *ctx, struct gl_texture_object *texObj)
837 {
838 const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
839 GLuint i;
840
841 for (i = 0; i < n; i++) {
842 struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
843 if (fb->Name) {
844 GLuint j;
845 for (j = 0; j < BUFFER_COUNT; j++) {
846 if (fb->Attachment[j].Type == GL_TEXTURE &&
847 fb->Attachment[j].Texture == texObj) {
848 _mesa_remove_attachment(ctx, fb->Attachment + j);
849 }
850 }
851 }
852 }
853 }
854
855
856 /**
857 * Check if the given texture object is bound to any texture image units and
858 * unbind it if so (revert to default textures).
859 */
860 static void
861 unbind_texobj_from_texunits(GLcontext *ctx, struct gl_texture_object *texObj)
862 {
863 GLuint u, tex;
864
865 for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
866 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
867 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
868 if (texObj == unit->CurrentTex[tex]) {
869 _mesa_reference_texobj(&unit->CurrentTex[tex],
870 ctx->Shared->DefaultTex[tex]);
871 ASSERT(unit->CurrentTex[tex]);
872 break;
873 }
874 }
875 }
876 }
877
878
879 /**
880 * Delete named textures.
881 *
882 * \param n number of textures to be deleted.
883 * \param textures array of texture IDs to be deleted.
884 *
885 * \sa glDeleteTextures().
886 *
887 * If we're about to delete a texture that's currently bound to any
888 * texture unit, unbind the texture first. Decrement the reference
889 * count on the texture object and delete it if it's zero.
890 * Recall that texture objects can be shared among several rendering
891 * contexts.
892 */
893 void GLAPIENTRY
894 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
895 {
896 GET_CURRENT_CONTEXT(ctx);
897 GLint i;
898 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
899
900 if (!textures)
901 return;
902
903 for (i = 0; i < n; i++) {
904 if (textures[i] > 0) {
905 struct gl_texture_object *delObj
906 = _mesa_lookup_texture(ctx, textures[i]);
907
908 if (delObj) {
909 _mesa_lock_texture(ctx, delObj);
910
911 /* Check if texture is bound to any framebuffer objects.
912 * If so, unbind.
913 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
914 */
915 unbind_texobj_from_fbo(ctx, delObj);
916
917 /* Check if this texture is currently bound to any texture units.
918 * If so, unbind it.
919 */
920 unbind_texobj_from_texunits(ctx, delObj);
921
922 _mesa_unlock_texture(ctx, delObj);
923
924 ctx->NewState |= _NEW_TEXTURE;
925
926 /* The texture _name_ is now free for re-use.
927 * Remove it from the hash table now.
928 */
929 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
930 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
931 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
932
933 /* Unreference the texobj. If refcount hits zero, the texture
934 * will be deleted.
935 */
936 _mesa_reference_texobj(&delObj, NULL);
937 }
938 }
939 }
940 }
941
942
943 /**
944 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
945 * into the corresponding Mesa texture target index.
946 * Note that proxy targets are not valid here.
947 * \return TEXTURE_x_INDEX or -1 if target is invalid
948 */
949 static GLint
950 target_enum_to_index(GLenum target)
951 {
952 switch (target) {
953 case GL_TEXTURE_1D:
954 return TEXTURE_1D_INDEX;
955 case GL_TEXTURE_2D:
956 return TEXTURE_2D_INDEX;
957 case GL_TEXTURE_3D:
958 return TEXTURE_3D_INDEX;
959 case GL_TEXTURE_CUBE_MAP_ARB:
960 return TEXTURE_CUBE_INDEX;
961 case GL_TEXTURE_RECTANGLE_NV:
962 return TEXTURE_RECT_INDEX;
963 case GL_TEXTURE_1D_ARRAY_EXT:
964 return TEXTURE_1D_ARRAY_INDEX;
965 case GL_TEXTURE_2D_ARRAY_EXT:
966 return TEXTURE_2D_ARRAY_INDEX;
967 default:
968 return -1;
969 }
970 }
971
972
973 /**
974 * Bind a named texture to a texturing target.
975 *
976 * \param target texture target.
977 * \param texName texture name.
978 *
979 * \sa glBindTexture().
980 *
981 * Determines the old texture object bound and returns immediately if rebinding
982 * the same texture. Get the current texture which is either a default texture
983 * if name is null, a named texture from the hash, or a new texture if the
984 * given texture name is new. Increments its reference count, binds it, and
985 * calls dd_function_table::BindTexture. Decrements the old texture reference
986 * count and deletes it if it reaches zero.
987 */
988 void GLAPIENTRY
989 _mesa_BindTexture( GLenum target, GLuint texName )
990 {
991 GET_CURRENT_CONTEXT(ctx);
992 const GLuint unit = ctx->Texture.CurrentUnit;
993 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
994 struct gl_texture_object *newTexObj = NULL, *defaultTexObj = NULL;
995 GLint targetIndex;
996 GLboolean early_out = GL_FALSE;
997 ASSERT_OUTSIDE_BEGIN_END(ctx);
998
999 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1000 _mesa_debug(ctx, "glBindTexture %s %d\n",
1001 _mesa_lookup_enum_by_nr(target), (GLint) texName);
1002
1003 targetIndex = target_enum_to_index(target);
1004 if (targetIndex < 0) {
1005 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target)");
1006 return;
1007 }
1008 assert(targetIndex < NUM_TEXTURE_TARGETS);
1009 defaultTexObj = ctx->Shared->DefaultTex[targetIndex];
1010
1011 /*
1012 * Get pointer to new texture object (newTexObj)
1013 */
1014 if (texName == 0) {
1015 newTexObj = defaultTexObj;
1016 }
1017 else {
1018 /* non-default texture object */
1019 newTexObj = _mesa_lookup_texture(ctx, texName);
1020 if (newTexObj) {
1021 /* error checking */
1022 if (newTexObj->Target != 0 && newTexObj->Target != target) {
1023 /* the named texture object's target doesn't match the given target */
1024 _mesa_error( ctx, GL_INVALID_OPERATION,
1025 "glBindTexture(target mismatch)" );
1026 return;
1027 }
1028 if (newTexObj->Target == 0) {
1029 finish_texture_init(ctx, target, newTexObj);
1030 }
1031 }
1032 else {
1033 /* if this is a new texture id, allocate a texture object now */
1034 newTexObj = (*ctx->Driver.NewTextureObject)(ctx, texName, target);
1035 if (!newTexObj) {
1036 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
1037 return;
1038 }
1039
1040 /* and insert it into hash table */
1041 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1042 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
1043 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1044 }
1045 newTexObj->Target = target;
1046 }
1047
1048 assert(valid_texture_object(newTexObj));
1049
1050 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1051 if ((ctx->Shared->RefCount == 1)
1052 && (newTexObj == texUnit->CurrentTex[targetIndex])) {
1053 early_out = GL_TRUE;
1054 }
1055 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1056
1057 if (early_out) {
1058 return;
1059 }
1060
1061 /* flush before changing binding */
1062 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1063
1064 /* Do the actual binding. The refcount on the previously bound
1065 * texture object will be decremented. It'll be deleted if the
1066 * count hits zero.
1067 */
1068 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], newTexObj);
1069 ASSERT(texUnit->CurrentTex[targetIndex]);
1070
1071 /* Pass BindTexture call to device driver */
1072 if (ctx->Driver.BindTexture)
1073 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
1074 }
1075
1076
1077 /**
1078 * Set texture priorities.
1079 *
1080 * \param n number of textures.
1081 * \param texName texture names.
1082 * \param priorities corresponding texture priorities.
1083 *
1084 * \sa glPrioritizeTextures().
1085 *
1086 * Looks up each texture in the hash, clamps the corresponding priority between
1087 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1088 */
1089 void GLAPIENTRY
1090 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1091 const GLclampf *priorities )
1092 {
1093 GET_CURRENT_CONTEXT(ctx);
1094 GLint i;
1095 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1096
1097 if (n < 0) {
1098 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1099 return;
1100 }
1101
1102 if (!priorities)
1103 return;
1104
1105 for (i = 0; i < n; i++) {
1106 if (texName[i] > 0) {
1107 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1108 if (t) {
1109 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1110 }
1111 }
1112 }
1113
1114 ctx->NewState |= _NEW_TEXTURE;
1115 }
1116
1117 /**
1118 * See if textures are loaded in texture memory.
1119 *
1120 * \param n number of textures to query.
1121 * \param texName array with the texture names.
1122 * \param residences array which will hold the residence status.
1123 *
1124 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1125 *
1126 * \sa glAreTexturesResident().
1127 *
1128 * Looks up each texture in the hash and calls
1129 * dd_function_table::IsTextureResident.
1130 */
1131 GLboolean GLAPIENTRY
1132 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
1133 GLboolean *residences)
1134 {
1135 GET_CURRENT_CONTEXT(ctx);
1136 GLboolean allResident = GL_TRUE;
1137 GLint i, j;
1138 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1139
1140 if (n < 0) {
1141 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1142 return GL_FALSE;
1143 }
1144
1145 if (!texName || !residences)
1146 return GL_FALSE;
1147
1148 for (i = 0; i < n; i++) {
1149 struct gl_texture_object *t;
1150 if (texName[i] == 0) {
1151 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1152 return GL_FALSE;
1153 }
1154 t = _mesa_lookup_texture(ctx, texName[i]);
1155 if (!t) {
1156 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1157 return GL_FALSE;
1158 }
1159 if (!ctx->Driver.IsTextureResident ||
1160 ctx->Driver.IsTextureResident(ctx, t)) {
1161 /* The texture is resident */
1162 if (!allResident)
1163 residences[i] = GL_TRUE;
1164 }
1165 else {
1166 /* The texture is not resident */
1167 if (allResident) {
1168 allResident = GL_FALSE;
1169 for (j = 0; j < i; j++)
1170 residences[j] = GL_TRUE;
1171 }
1172 residences[i] = GL_FALSE;
1173 }
1174 }
1175
1176 return allResident;
1177 }
1178
1179 /**
1180 * See if a name corresponds to a texture.
1181 *
1182 * \param texture texture name.
1183 *
1184 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1185 * otherwise.
1186 *
1187 * \sa glIsTexture().
1188 *
1189 * Calls _mesa_HashLookup().
1190 */
1191 GLboolean GLAPIENTRY
1192 _mesa_IsTexture( GLuint texture )
1193 {
1194 struct gl_texture_object *t;
1195 GET_CURRENT_CONTEXT(ctx);
1196 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1197
1198 if (!texture)
1199 return GL_FALSE;
1200
1201 t = _mesa_lookup_texture(ctx, texture);
1202
1203 /* IsTexture is true only after object has been bound once. */
1204 return t && t->Target;
1205 }
1206
1207
1208 /**
1209 * Simplest implementation of texture locking: grab the shared tex
1210 * mutex. Examine the shared context state timestamp and if there has
1211 * been a change, set the appropriate bits in ctx->NewState.
1212 *
1213 * This is used to deal with synchronizing things when a texture object
1214 * is used/modified by different contexts (or threads) which are sharing
1215 * the texture.
1216 *
1217 * See also _mesa_lock/unlock_texture() in teximage.h
1218 */
1219 void
1220 _mesa_lock_context_textures( GLcontext *ctx )
1221 {
1222 _glthread_LOCK_MUTEX(ctx->Shared->TexMutex);
1223
1224 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
1225 ctx->NewState |= _NEW_TEXTURE;
1226 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
1227 }
1228 }
1229
1230
1231 void
1232 _mesa_unlock_context_textures( GLcontext *ctx )
1233 {
1234 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
1235 _glthread_UNLOCK_MUTEX(ctx->Shared->TexMutex);
1236 }
1237
1238 /*@}*/
1239
1240