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