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