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