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