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