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