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