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