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