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