Merge branch 'vbo_clean'
[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 #if FEATURE_colortable
33 #include "colortab.h"
34 #endif
35 #include "context.h"
36 #include "enums.h"
37 #include "fbobject.h"
38 #include "hash.h"
39 #include "imports.h"
40 #include "macros.h"
41 #include "teximage.h"
42 #include "texstate.h"
43 #include "texobj.h"
44 #include "mtypes.h"
45 #include "shader/prog_instruction.h"
46
47
48
49 /**********************************************************************/
50 /** \name Internal functions */
51 /*@{*/
52
53
54 /**
55 * Return the gl_texture_object for a given ID.
56 */
57 struct gl_texture_object *
58 _mesa_lookup_texture(GLcontext *ctx, GLuint id)
59 {
60 return (struct gl_texture_object *)
61 _mesa_HashLookup(ctx->Shared->TexObjects, id);
62 }
63
64
65
66 /**
67 * Allocate and initialize a new texture object. But don't put it into the
68 * texture object hash table.
69 *
70 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
71 * driver.
72 *
73 * \param shared the shared GL state structure to contain the texture object
74 * \param name integer name for the texture object
75 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
76 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
77 * of GenTextures()
78 *
79 * \return pointer to new texture object.
80 */
81 struct gl_texture_object *
82 _mesa_new_texture_object( GLcontext *ctx, GLuint name, GLenum target )
83 {
84 struct gl_texture_object *obj;
85 (void) ctx;
86 obj = MALLOC_STRUCT(gl_texture_object);
87 _mesa_initialize_texture_object(obj, name, target);
88 return obj;
89 }
90
91
92 /**
93 * Initialize a new texture object to default values.
94 * \param obj the texture object
95 * \param name the texture name
96 * \param target the texture target
97 */
98 void
99 _mesa_initialize_texture_object( struct gl_texture_object *obj,
100 GLuint name, GLenum target )
101 {
102 ASSERT(target == 0 ||
103 target == GL_TEXTURE_1D ||
104 target == GL_TEXTURE_2D ||
105 target == GL_TEXTURE_3D ||
106 target == GL_TEXTURE_CUBE_MAP_ARB ||
107 target == GL_TEXTURE_RECTANGLE_NV ||
108 target == GL_TEXTURE_1D_ARRAY_EXT ||
109 target == GL_TEXTURE_2D_ARRAY_EXT);
110
111 _mesa_bzero(obj, sizeof(*obj));
112 /* init the non-zero fields */
113 _glthread_INIT_MUTEX(obj->Mutex);
114 obj->RefCount = 1;
115 obj->Name = name;
116 obj->Target = target;
117 obj->Priority = 1.0F;
118 if (target == GL_TEXTURE_RECTANGLE_NV) {
119 obj->WrapS = GL_CLAMP_TO_EDGE;
120 obj->WrapT = GL_CLAMP_TO_EDGE;
121 obj->WrapR = GL_CLAMP_TO_EDGE;
122 obj->MinFilter = GL_LINEAR;
123 }
124 else {
125 obj->WrapS = GL_REPEAT;
126 obj->WrapT = GL_REPEAT;
127 obj->WrapR = GL_REPEAT;
128 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
129 }
130 obj->MagFilter = GL_LINEAR;
131 obj->MinLod = -1000.0;
132 obj->MaxLod = 1000.0;
133 obj->LodBias = 0.0;
134 obj->BaseLevel = 0;
135 obj->MaxLevel = 1000;
136 obj->MaxAnisotropy = 1.0;
137 obj->CompareMode = GL_NONE; /* ARB_shadow */
138 obj->CompareFunc = GL_LEQUAL; /* ARB_shadow */
139 obj->CompareFailValue = 0.0F; /* ARB_shadow_ambient */
140 obj->DepthMode = GL_LUMINANCE; /* ARB_depth_texture */
141 obj->Swizzle[0] = GL_RED;
142 obj->Swizzle[1] = GL_GREEN;
143 obj->Swizzle[2] = GL_BLUE;
144 obj->Swizzle[3] = GL_ALPHA;
145 obj->_Swizzle = SWIZZLE_NOOP;
146 }
147
148
149 /**
150 * Some texture initialization can't be finished until we know which
151 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
152 */
153 static void
154 finish_texture_init(GLcontext *ctx, GLenum target,
155 struct gl_texture_object *obj)
156 {
157 assert(obj->Target == 0);
158
159 if (target == GL_TEXTURE_RECTANGLE_NV) {
160 /* have to init wrap and filter state here - kind of klunky */
161 obj->WrapS = GL_CLAMP_TO_EDGE;
162 obj->WrapT = GL_CLAMP_TO_EDGE;
163 obj->WrapR = GL_CLAMP_TO_EDGE;
164 obj->MinFilter = GL_LINEAR;
165 if (ctx->Driver.TexParameter) {
166 static const GLfloat fparam_wrap[1] = {(GLfloat) GL_CLAMP_TO_EDGE};
167 static const GLfloat fparam_filter[1] = {(GLfloat) GL_LINEAR};
168 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_S, fparam_wrap);
169 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_T, fparam_wrap);
170 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_R, fparam_wrap);
171 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_MIN_FILTER, fparam_filter);
172 }
173 }
174 }
175
176
177 /**
178 * Deallocate a texture object struct. It should have already been
179 * removed from the texture object pool.
180 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
181 *
182 * \param shared the shared GL state to which the object belongs.
183 * \param texObj the texture object to delete.
184 */
185 void
186 _mesa_delete_texture_object( GLcontext *ctx, struct gl_texture_object *texObj )
187 {
188 GLuint i, face;
189
190 (void) ctx;
191
192 /* Set Target to an invalid value. With some assertions elsewhere
193 * we can try to detect possible use of deleted textures.
194 */
195 texObj->Target = 0x99;
196
197 #if FEATURE_colortable
198 _mesa_free_colortable_data(&texObj->Palette);
199 #endif
200
201 /* free the texture images */
202 for (face = 0; face < 6; face++) {
203 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
204 if (texObj->Image[face][i]) {
205 _mesa_delete_texture_image( ctx, texObj->Image[face][i] );
206 }
207 }
208 }
209
210 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
211 _glthread_DESTROY_MUTEX(texObj->Mutex);
212
213 /* free this object */
214 _mesa_free(texObj);
215 }
216
217
218
219
220 /**
221 * Copy texture object state from one texture object to another.
222 * Use for glPush/PopAttrib.
223 *
224 * \param dest destination texture object.
225 * \param src source texture object.
226 */
227 void
228 _mesa_copy_texture_object( struct gl_texture_object *dest,
229 const struct gl_texture_object *src )
230 {
231 dest->Target = src->Target;
232 dest->Name = src->Name;
233 dest->Priority = src->Priority;
234 dest->BorderColor[0] = src->BorderColor[0];
235 dest->BorderColor[1] = src->BorderColor[1];
236 dest->BorderColor[2] = src->BorderColor[2];
237 dest->BorderColor[3] = src->BorderColor[3];
238 dest->WrapS = src->WrapS;
239 dest->WrapT = src->WrapT;
240 dest->WrapR = src->WrapR;
241 dest->MinFilter = src->MinFilter;
242 dest->MagFilter = src->MagFilter;
243 dest->MinLod = src->MinLod;
244 dest->MaxLod = src->MaxLod;
245 dest->LodBias = src->LodBias;
246 dest->BaseLevel = src->BaseLevel;
247 dest->MaxLevel = src->MaxLevel;
248 dest->MaxAnisotropy = src->MaxAnisotropy;
249 dest->CompareMode = src->CompareMode;
250 dest->CompareFunc = src->CompareFunc;
251 dest->CompareFailValue = src->CompareFailValue;
252 dest->DepthMode = src->DepthMode;
253 dest->_MaxLevel = src->_MaxLevel;
254 dest->_MaxLambda = src->_MaxLambda;
255 dest->GenerateMipmap = src->GenerateMipmap;
256 dest->Palette = src->Palette;
257 dest->_Complete = src->_Complete;
258 COPY_4V(dest->Swizzle, src->Swizzle);
259 dest->_Swizzle = src->_Swizzle;
260 }
261
262
263 /**
264 * Clear all texture images of the given texture object.
265 *
266 * \param ctx GL context.
267 * \param t texture object.
268 *
269 * \sa _mesa_clear_texture_image().
270 */
271 void
272 _mesa_clear_texture_object(GLcontext *ctx, struct gl_texture_object *texObj)
273 {
274 GLuint i, j;
275
276 if (texObj->Target == 0)
277 return;
278
279 for (i = 0; i < MAX_FACES; i++) {
280 for (j = 0; j < MAX_TEXTURE_LEVELS; j++) {
281 struct gl_texture_image *texImage = texObj->Image[i][j];
282 if (texImage)
283 _mesa_clear_texture_image(ctx, texImage);
284 }
285 }
286 }
287
288
289 /**
290 * Check if the given texture object is valid by examining its Target field.
291 * For debugging only.
292 */
293 static GLboolean
294 valid_texture_object(const struct gl_texture_object *tex)
295 {
296 switch (tex->Target) {
297 case 0:
298 case GL_TEXTURE_1D:
299 case GL_TEXTURE_2D:
300 case GL_TEXTURE_3D:
301 case GL_TEXTURE_CUBE_MAP_ARB:
302 case GL_TEXTURE_RECTANGLE_NV:
303 case GL_TEXTURE_1D_ARRAY_EXT:
304 case GL_TEXTURE_2D_ARRAY_EXT:
305 return GL_TRUE;
306 case 0x99:
307 _mesa_problem(NULL, "invalid reference to a deleted texture object");
308 return GL_FALSE;
309 default:
310 _mesa_problem(NULL, "invalid texture object Target 0x%x, Id = %u",
311 tex->Target, tex->Name);
312 return GL_FALSE;
313 }
314 }
315
316
317 /**
318 * Reference (or unreference) a texture object.
319 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
320 * If 'tex' is non-null, increment its refcount.
321 */
322 void
323 _mesa_reference_texobj(struct gl_texture_object **ptr,
324 struct gl_texture_object *tex)
325 {
326 assert(ptr);
327 if (*ptr == tex) {
328 /* no change */
329 return;
330 }
331
332 if (*ptr) {
333 /* Unreference the old texture */
334 GLboolean deleteFlag = GL_FALSE;
335 struct gl_texture_object *oldTex = *ptr;
336
337 ASSERT(valid_texture_object(oldTex));
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 _mesa_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_sprintf(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_sprintf(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, "Non-quare cubemap image");
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 /* set image data */
748 ctx->Driver.TexImage2D(ctx, GL_TEXTURE_2D, 0, GL_RGBA,
749 8, 8, 0,
750 GL_RGBA, GL_UNSIGNED_BYTE, texels,
751 &ctx->DefaultPacking, texObj, texImage);
752
753 _mesa_test_texobj_completeness(ctx, texObj);
754 assert(texObj->_Complete);
755
756 ctx->Shared->FallbackTex = texObj;
757 }
758 return ctx->Shared->FallbackTex;
759 }
760
761
762 /*@}*/
763
764
765 /***********************************************************************/
766 /** \name API functions */
767 /*@{*/
768
769
770 /**
771 * Generate texture names.
772 *
773 * \param n number of texture names to be generated.
774 * \param textures an array in which will hold the generated texture names.
775 *
776 * \sa glGenTextures().
777 *
778 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
779 * IDs which are stored in \p textures. Corresponding empty texture
780 * objects are also generated.
781 */
782 void GLAPIENTRY
783 _mesa_GenTextures( GLsizei n, GLuint *textures )
784 {
785 GET_CURRENT_CONTEXT(ctx);
786 GLuint first;
787 GLint i;
788 ASSERT_OUTSIDE_BEGIN_END(ctx);
789
790 if (n < 0) {
791 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
792 return;
793 }
794
795 if (!textures)
796 return;
797
798 /*
799 * This must be atomic (generation and allocation of texture IDs)
800 */
801 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
802
803 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
804
805 /* Allocate new, empty texture objects */
806 for (i = 0; i < n; i++) {
807 struct gl_texture_object *texObj;
808 GLuint name = first + i;
809 GLenum target = 0;
810 texObj = (*ctx->Driver.NewTextureObject)( ctx, name, target);
811 if (!texObj) {
812 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
813 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
814 return;
815 }
816
817 /* insert into hash table */
818 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
819
820 textures[i] = name;
821 }
822
823 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
824 }
825
826
827 /**
828 * Check if the given texture object is bound to the current draw or
829 * read framebuffer. If so, Unbind it.
830 */
831 static void
832 unbind_texobj_from_fbo(GLcontext *ctx, struct gl_texture_object *texObj)
833 {
834 const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
835 GLuint i;
836
837 for (i = 0; i < n; i++) {
838 struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
839 if (fb->Name) {
840 GLuint j;
841 for (j = 0; j < BUFFER_COUNT; j++) {
842 if (fb->Attachment[j].Type == GL_TEXTURE &&
843 fb->Attachment[j].Texture == texObj) {
844 _mesa_remove_attachment(ctx, fb->Attachment + j);
845 }
846 }
847 }
848 }
849 }
850
851
852 /**
853 * Check if the given texture object is bound to any texture image units and
854 * unbind it if so (revert to default textures).
855 */
856 static void
857 unbind_texobj_from_texunits(GLcontext *ctx, struct gl_texture_object *texObj)
858 {
859 GLuint u, tex;
860
861 for (u = 0; u < MAX_TEXTURE_IMAGE_UNITS; u++) {
862 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
863 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
864 if (texObj == unit->CurrentTex[tex]) {
865 _mesa_reference_texobj(&unit->CurrentTex[tex],
866 ctx->Shared->DefaultTex[TEXTURE_1D_INDEX]);
867 ASSERT(unit->CurrentTex[tex]);
868 break;
869 }
870 }
871 }
872 }
873
874
875 /**
876 * Delete named textures.
877 *
878 * \param n number of textures to be deleted.
879 * \param textures array of texture IDs to be deleted.
880 *
881 * \sa glDeleteTextures().
882 *
883 * If we're about to delete a texture that's currently bound to any
884 * texture unit, unbind the texture first. Decrement the reference
885 * count on the texture object and delete it if it's zero.
886 * Recall that texture objects can be shared among several rendering
887 * contexts.
888 */
889 void GLAPIENTRY
890 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
891 {
892 GET_CURRENT_CONTEXT(ctx);
893 GLint i;
894 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
895
896 if (!textures)
897 return;
898
899 for (i = 0; i < n; i++) {
900 if (textures[i] > 0) {
901 struct gl_texture_object *delObj
902 = _mesa_lookup_texture(ctx, textures[i]);
903
904 if (delObj) {
905 _mesa_lock_texture(ctx, delObj);
906
907 /* Check if texture is bound to any framebuffer objects.
908 * If so, unbind.
909 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
910 */
911 unbind_texobj_from_fbo(ctx, delObj);
912
913 /* Check if this texture is currently bound to any texture units.
914 * If so, unbind it.
915 */
916 unbind_texobj_from_texunits(ctx, delObj);
917
918 _mesa_unlock_texture(ctx, delObj);
919
920 ctx->NewState |= _NEW_TEXTURE;
921
922 /* The texture _name_ is now free for re-use.
923 * Remove it from the hash table now.
924 */
925 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
926 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
927 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
928
929 /* Unreference the texobj. If refcount hits zero, the texture
930 * will be deleted.
931 */
932 _mesa_reference_texobj(&delObj, NULL);
933 }
934 }
935 }
936 }
937
938
939 /**
940 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
941 * into the corresponding Mesa texture target index.
942 * Return -1 if target is invalid.
943 */
944 static GLint
945 target_enum_to_index(GLenum target)
946 {
947 switch (target) {
948 case GL_TEXTURE_1D:
949 return TEXTURE_1D_INDEX;
950 case GL_TEXTURE_2D:
951 return TEXTURE_2D_INDEX;
952 case GL_TEXTURE_3D:
953 return TEXTURE_3D_INDEX;
954 case GL_TEXTURE_CUBE_MAP_ARB:
955 return TEXTURE_CUBE_INDEX;
956 case GL_TEXTURE_RECTANGLE_NV:
957 return TEXTURE_RECT_INDEX;
958 case GL_TEXTURE_1D_ARRAY_EXT:
959 return TEXTURE_1D_ARRAY_INDEX;
960 case GL_TEXTURE_2D_ARRAY_EXT:
961 return TEXTURE_2D_ARRAY_INDEX;
962 default:
963 return -1;
964 }
965 }
966
967
968 /**
969 * Bind a named texture to a texturing target.
970 *
971 * \param target texture target.
972 * \param texName texture name.
973 *
974 * \sa glBindTexture().
975 *
976 * Determines the old texture object bound and returns immediately if rebinding
977 * the same texture. Get the current texture which is either a default texture
978 * if name is null, a named texture from the hash, or a new texture if the
979 * given texture name is new. Increments its reference count, binds it, and
980 * calls dd_function_table::BindTexture. Decrements the old texture reference
981 * count and deletes it if it reaches zero.
982 */
983 void GLAPIENTRY
984 _mesa_BindTexture( GLenum target, GLuint texName )
985 {
986 GET_CURRENT_CONTEXT(ctx);
987 const GLuint unit = ctx->Texture.CurrentUnit;
988 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
989 struct gl_texture_object *newTexObj = NULL, *defaultTexObj = NULL;
990 GLint targetIndex;
991 GLboolean early_out = GL_FALSE;
992 ASSERT_OUTSIDE_BEGIN_END(ctx);
993
994 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
995 _mesa_debug(ctx, "glBindTexture %s %d\n",
996 _mesa_lookup_enum_by_nr(target), (GLint) texName);
997
998 targetIndex = target_enum_to_index(target);
999 if (targetIndex < 0) {
1000 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target)");
1001 return;
1002 }
1003 assert(targetIndex < NUM_TEXTURE_TARGETS);
1004 defaultTexObj = ctx->Shared->DefaultTex[targetIndex];
1005
1006 /*
1007 * Get pointer to new texture object (newTexObj)
1008 */
1009 if (texName == 0) {
1010 newTexObj = defaultTexObj;
1011 }
1012 else {
1013 /* non-default texture object */
1014 newTexObj = _mesa_lookup_texture(ctx, texName);
1015 if (newTexObj) {
1016 /* error checking */
1017 if (newTexObj->Target != 0 && newTexObj->Target != target) {
1018 /* the named texture object's target doesn't match the given target */
1019 _mesa_error( ctx, GL_INVALID_OPERATION,
1020 "glBindTexture(target mismatch)" );
1021 return;
1022 }
1023 if (newTexObj->Target == 0) {
1024 finish_texture_init(ctx, target, newTexObj);
1025 }
1026 }
1027 else {
1028 /* if this is a new texture id, allocate a texture object now */
1029 newTexObj = (*ctx->Driver.NewTextureObject)(ctx, texName, target);
1030 if (!newTexObj) {
1031 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
1032 return;
1033 }
1034
1035 /* and insert it into hash table */
1036 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1037 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
1038 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1039 }
1040 newTexObj->Target = target;
1041 }
1042
1043 assert(valid_texture_object(newTexObj));
1044
1045 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1046 if ((ctx->Shared->RefCount == 1)
1047 && (newTexObj == texUnit->CurrentTex[targetIndex])) {
1048 early_out = GL_TRUE;
1049 }
1050 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1051
1052 if (early_out) {
1053 return;
1054 }
1055
1056 /* flush before changing binding */
1057 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1058
1059 /* Do the actual binding. The refcount on the previously bound
1060 * texture object will be decremented. It'll be deleted if the
1061 * count hits zero.
1062 */
1063 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], newTexObj);
1064 ASSERT(texUnit->CurrentTex[targetIndex]);
1065
1066 /* Pass BindTexture call to device driver */
1067 if (ctx->Driver.BindTexture)
1068 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
1069 }
1070
1071
1072 /**
1073 * Set texture priorities.
1074 *
1075 * \param n number of textures.
1076 * \param texName texture names.
1077 * \param priorities corresponding texture priorities.
1078 *
1079 * \sa glPrioritizeTextures().
1080 *
1081 * Looks up each texture in the hash, clamps the corresponding priority between
1082 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1083 */
1084 void GLAPIENTRY
1085 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1086 const GLclampf *priorities )
1087 {
1088 GET_CURRENT_CONTEXT(ctx);
1089 GLint i;
1090 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1091
1092 if (n < 0) {
1093 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1094 return;
1095 }
1096
1097 if (!priorities)
1098 return;
1099
1100 for (i = 0; i < n; i++) {
1101 if (texName[i] > 0) {
1102 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1103 if (t) {
1104 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1105 if (ctx->Driver.PrioritizeTexture)
1106 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
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