a9248a78f40f69c3dc4b98e191617b2b97a6760a
[mesa.git] / src / mesa / main / texobj.c
1 /* $Id: texobj.c,v 1.27 2000/09/12 21:07:41 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "colortab.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "hash.h"
36 #include "mem.h"
37 #include "teximage.h"
38 #include "texstate.h"
39 #include "texobj.h"
40 #include "types.h"
41 #endif
42
43
44
45 /*
46 * Allocate a new texture object and add it to the linked list of texture
47 * objects. If name>0 then also insert the new texture object into the hash
48 * table.
49 * Input: shared - the shared GL state structure to contain the texture object
50 * name - integer name for the texture object
51 * dimensions - either 1, 2, 3 or 6 (cube map)
52 * Return: pointer to new texture object
53 */
54 struct gl_texture_object *
55 gl_alloc_texture_object( struct gl_shared_state *shared, GLuint name,
56 GLuint dimensions)
57 {
58 struct gl_texture_object *obj;
59
60 ASSERT(dimensions <= 3 || dimensions == 6);
61
62 obj = CALLOC_STRUCT(gl_texture_object);
63
64 if (obj) {
65 /* init the non-zero fields */
66 _glthread_INIT_MUTEX(obj->Mutex);
67 obj->RefCount = 1;
68 obj->Name = name;
69 obj->Dimensions = dimensions;
70 obj->Priority = 1.0F;
71 obj->WrapS = GL_REPEAT;
72 obj->WrapT = GL_REPEAT;
73 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
74 obj->MagFilter = GL_LINEAR;
75 obj->MinLod = -1000.0;
76 obj->MaxLod = 1000.0;
77 obj->BaseLevel = 0;
78 obj->MaxLevel = 1000;
79 obj->MinMagThresh = 0.0F;
80 _mesa_init_colortable(&obj->Palette);
81
82 /* insert into linked list */
83 if (shared) {
84 _glthread_LOCK_MUTEX(shared->Mutex);
85 obj->Next = shared->TexObjectList;
86 shared->TexObjectList = obj;
87 _glthread_UNLOCK_MUTEX(shared->Mutex);
88 }
89
90 if (name > 0) {
91 /* insert into hash table */
92 _mesa_HashInsert(shared->TexObjects, name, obj);
93 }
94 }
95 return obj;
96 }
97
98
99 /*
100 * Deallocate a texture object struct and remove it from the given
101 * shared GL state.
102 * Input: shared - the shared GL state to which the object belongs
103 * t - the texture object to delete
104 */
105 void gl_free_texture_object( struct gl_shared_state *shared,
106 struct gl_texture_object *t )
107 {
108 struct gl_texture_object *tprev, *tcurr;
109
110 assert(t);
111
112 /* Remove t from dirty list so we don't touch free'd memory later.
113 * Test for shared since Proxy texture aren't in global linked list.
114 */
115 if (shared)
116 gl_remove_texobj_from_dirty_list( shared, t );
117
118 /* unlink t from the linked list */
119 if (shared) {
120 _glthread_LOCK_MUTEX(shared->Mutex);
121 tprev = NULL;
122 tcurr = shared->TexObjectList;
123 while (tcurr) {
124 if (tcurr==t) {
125 if (tprev) {
126 tprev->Next = t->Next;
127 }
128 else {
129 shared->TexObjectList = t->Next;
130 }
131 break;
132 }
133 tprev = tcurr;
134 tcurr = tcurr->Next;
135 }
136 _glthread_UNLOCK_MUTEX(shared->Mutex);
137 }
138
139 if (t->Name) {
140 /* remove from hash table */
141 _mesa_HashRemove(shared->TexObjects, t->Name);
142 }
143
144 _mesa_free_colortable_data(&t->Palette);
145
146 /* free texture images */
147 {
148 GLuint i;
149 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
150 if (t->Image[i]) {
151 _mesa_free_texture_image( t->Image[i] );
152 }
153 }
154 }
155 /* free this object */
156 FREE( t );
157 }
158
159
160
161 /*
162 * Examine a texture object to determine if it is complete or not.
163 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
164 */
165 void
166 _mesa_test_texobj_completeness( const GLcontext *ctx,
167 struct gl_texture_object *t )
168 {
169 const GLint baseLevel = t->BaseLevel;
170
171 t->Complete = GL_TRUE; /* be optimistic */
172
173 /* Always need level zero image */
174 if (!t->Image[baseLevel]) {
175 t->Complete = GL_FALSE;
176 return;
177 }
178
179 /* Compute number of mipmap levels */
180 if (t->Dimensions == 1) {
181 t->P = t->Image[baseLevel]->WidthLog2;
182 }
183 else if (t->Dimensions == 2 || t->Dimensions == 6) {
184 t->P = MAX2(t->Image[baseLevel]->WidthLog2,
185 t->Image[baseLevel]->HeightLog2);
186 }
187 else if (t->Dimensions == 3) {
188 GLint max = MAX2(t->Image[baseLevel]->WidthLog2,
189 t->Image[baseLevel]->HeightLog2);
190 max = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2));
191 t->P = max;
192 }
193
194 /* Compute M (see the 1.2 spec) used during mipmapping */
195 t->M = (GLfloat) (MIN2(t->MaxLevel, t->P) - t->BaseLevel);
196
197
198 if (t->Dimensions == 6) {
199 /* make sure all six level 0 images are same size */
200 const GLint w = t->Image[baseLevel]->Width2;
201 const GLint h = t->Image[baseLevel]->Height2;
202 if (!t->NegX[baseLevel] ||
203 t->NegX[baseLevel]->Width2 != w ||
204 t->NegX[baseLevel]->Height2 != h ||
205 !t->PosY[baseLevel] ||
206 t->PosY[baseLevel]->Width2 != w ||
207 t->PosY[baseLevel]->Height2 != h ||
208 !t->NegY[baseLevel] ||
209 t->NegY[baseLevel]->Width2 != w ||
210 t->NegY[baseLevel]->Height2 != h ||
211 !t->PosZ[baseLevel] ||
212 t->PosZ[baseLevel]->Width2 != w ||
213 t->PosZ[baseLevel]->Height2 != h ||
214 !t->NegZ[baseLevel] ||
215 t->NegZ[baseLevel]->Width2 != w ||
216 t->NegZ[baseLevel]->Height2 != h) {
217 t->Complete = GL_FALSE;
218 return;
219 }
220 }
221
222 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
223 /*
224 * Mipmapping: determine if we have a complete set of mipmaps
225 */
226 GLint i;
227 GLint minLevel = baseLevel;
228 GLint maxLevel = MIN2(t->P, ctx->Const.MaxTextureLevels-1);
229 maxLevel = MIN2(maxLevel, t->MaxLevel);
230
231 if (minLevel > maxLevel) {
232 t->Complete = GL_FALSE;
233 return;
234 }
235
236 /* Test dimension-independent attributes */
237 for (i = minLevel; i <= maxLevel; i++) {
238 if (t->Image[i]) {
239 if (t->Image[i]->Format != t->Image[baseLevel]->Format) {
240 t->Complete = GL_FALSE;
241 return;
242 }
243 if (t->Image[i]->Border != t->Image[baseLevel]->Border) {
244 t->Complete = GL_FALSE;
245 return;
246 }
247 }
248 }
249
250 /* Test things which depend on number of texture image dimensions */
251 if (t->Dimensions == 1) {
252 /* Test 1-D mipmaps */
253 GLuint width = t->Image[baseLevel]->Width2;
254 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
255 if (width > 1) {
256 width /= 2;
257 }
258 if (i >= minLevel && i <= maxLevel) {
259 if (!t->Image[i]) {
260 t->Complete = GL_FALSE;
261 return;
262 }
263 if (t->Image[i]->Width2 != width ) {
264 t->Complete = GL_FALSE;
265 return;
266 }
267 }
268 if (width == 1) {
269 return; /* found smallest needed mipmap, all done! */
270 }
271 }
272 }
273 else if (t->Dimensions == 2) {
274 /* Test 2-D mipmaps */
275 GLuint width = t->Image[baseLevel]->Width2;
276 GLuint height = t->Image[baseLevel]->Height2;
277 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
278 if (width > 1) {
279 width /= 2;
280 }
281 if (height > 1) {
282 height /= 2;
283 }
284 if (i >= minLevel && i <= maxLevel) {
285 if (!t->Image[i]) {
286 t->Complete = GL_FALSE;
287 return;
288 }
289 if (t->Image[i]->Width2 != width) {
290 t->Complete = GL_FALSE;
291 return;
292 }
293 if (t->Image[i]->Height2 != height) {
294 t->Complete = GL_FALSE;
295 return;
296 }
297 if (width==1 && height==1) {
298 return; /* found smallest needed mipmap, all done! */
299 }
300 }
301 }
302 }
303 else if (t->Dimensions == 3) {
304 /* Test 3-D mipmaps */
305 GLuint width = t->Image[baseLevel]->Width2;
306 GLuint height = t->Image[baseLevel]->Height2;
307 GLuint depth = t->Image[baseLevel]->Depth2;
308 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
309 if (width > 1) {
310 width /= 2;
311 }
312 if (height > 1) {
313 height /= 2;
314 }
315 if (depth > 1) {
316 depth /= 2;
317 }
318 if (i >= minLevel && i <= maxLevel) {
319 if (!t->Image[i]) {
320 t->Complete = GL_FALSE;
321 return;
322 }
323 if (t->Image[i]->Width2 != width) {
324 t->Complete = GL_FALSE;
325 return;
326 }
327 if (t->Image[i]->Height2 != height) {
328 t->Complete = GL_FALSE;
329 return;
330 }
331 if (t->Image[i]->Depth2 != depth) {
332 t->Complete = GL_FALSE;
333 return;
334 }
335 }
336 if (width == 1 && height == 1 && depth == 1) {
337 return; /* found smallest needed mipmap, all done! */
338 }
339 }
340 }
341 else if (t->Dimensions == 6) {
342 /* make sure 6 cube faces are consistant */
343 GLuint width = t->Image[baseLevel]->Width2;
344 GLuint height = t->Image[baseLevel]->Height2;
345 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
346 if (width > 1) {
347 width /= 2;
348 }
349 if (height > 1) {
350 height /= 2;
351 }
352 if (i >= minLevel && i <= maxLevel) {
353 /* check that we have images defined */
354 if (!t->Image[i] || !t->NegX[i] ||
355 !t->PosY[i] || !t->NegY[i] ||
356 !t->PosZ[i] || !t->NegZ[i]) {
357 t->Complete = GL_FALSE;
358 return;
359 }
360 /* check that all six images have same size */
361 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
362 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
363 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
364 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
365 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
366 t->Complete = GL_FALSE;
367 return;
368 }
369 }
370 if (width == 1 && height == 1) {
371 return; /* found smallest needed mipmap, all done! */
372 }
373 }
374 }
375 else {
376 /* Dimensions = ??? */
377 gl_problem(NULL, "Bug in gl_test_texture_object_completeness\n");
378 }
379 }
380 }
381
382
383 _glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
384
385
386 /*
387 * Execute glGenTextures
388 */
389 void
390 _mesa_GenTextures( GLsizei n, GLuint *texName )
391 {
392 GET_CURRENT_CONTEXT(ctx);
393 GLuint first;
394 GLint i;
395
396 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGenTextures");
397 if (n < 0) {
398 gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
399 return;
400 }
401
402 if (!texName)
403 return;
404
405 /*
406 * This must be atomic (generation and allocation of texture IDs)
407 */
408 _glthread_LOCK_MUTEX(GenTexturesLock);
409
410 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
411
412 /* Return the texture names */
413 for (i=0;i<n;i++) {
414 texName[i] = first + i;
415 }
416
417 /* Allocate new, empty texture objects */
418 for (i=0;i<n;i++) {
419 GLuint name = first + i;
420 GLuint dims = 0;
421 (void) gl_alloc_texture_object(ctx->Shared, name, dims);
422 }
423
424 _glthread_UNLOCK_MUTEX(GenTexturesLock);
425 }
426
427
428
429 /*
430 * Execute glDeleteTextures
431 */
432 void
433 _mesa_DeleteTextures( GLsizei n, const GLuint *texName)
434 {
435 GET_CURRENT_CONTEXT(ctx);
436 GLint i;
437
438 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDeleteTextures");
439
440 if (!texName)
441 return;
442
443 for (i=0;i<n;i++) {
444 struct gl_texture_object *t;
445 if (texName[i]>0) {
446 t = (struct gl_texture_object *)
447 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
448 if (t) {
449 /* First check if this texture is currently bound.
450 * If so, unbind it and decrement the reference count.
451 */
452 GLuint u;
453 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
454 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
455 GLuint d;
456 for (d = 1 ; d <= 3 ; d++) {
457 if (unit->CurrentD[d] == t) {
458 unit->CurrentD[d] = ctx->Shared->DefaultD[d];
459 ctx->Shared->DefaultD[d]->RefCount++;
460 t->RefCount--;
461 ASSERT( t->RefCount >= 0 );
462 }
463 }
464 }
465
466 /* Decrement reference count and delete if zero */
467 t->RefCount--;
468 ASSERT( t->RefCount >= 0 );
469 if (t->RefCount == 0) {
470 if (ctx->Driver.DeleteTexture)
471 (*ctx->Driver.DeleteTexture)( ctx, t );
472 gl_free_texture_object(ctx->Shared, t);
473 }
474 }
475 }
476 }
477 }
478
479
480
481 /*
482 * Execute glBindTexture
483 */
484 void
485 _mesa_BindTexture( GLenum target, GLuint texName )
486 {
487 GET_CURRENT_CONTEXT(ctx);
488 GLuint unit = ctx->Texture.CurrentUnit;
489 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
490 struct gl_texture_object *oldTexObj;
491 struct gl_texture_object *newTexObj;
492 GLuint dim;
493
494 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
495 fprintf(stderr, "glBindTexture %s %d\n",
496 gl_lookup_enum_by_nr(target), (GLint) texName);
497
498 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glBindTexture");
499
500 switch (target) {
501 case GL_TEXTURE_1D:
502 dim = 1;
503 oldTexObj = texUnit->CurrentD[1];
504 break;
505 case GL_TEXTURE_2D:
506 dim = 2;
507 oldTexObj = texUnit->CurrentD[2];
508 break;
509 case GL_TEXTURE_3D:
510 dim = 3;
511 oldTexObj = texUnit->CurrentD[3];
512 break;
513 case GL_TEXTURE_CUBE_MAP_ARB:
514 if (ctx->Extensions.HaveTextureCubeMap) {
515 dim = 6;
516 oldTexObj = texUnit->CurrentCubeMap;
517 break;
518 }
519 /* fallthrough */
520 default:
521 gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
522 return;
523 }
524
525 if (oldTexObj->Name == texName)
526 return;
527
528 if (texName == 0) {
529 if (target == GL_TEXTURE_CUBE_MAP_ARB)
530 newTexObj = ctx->Shared->DefaultCubeMap;
531 else
532 newTexObj = ctx->Shared->DefaultD[dim];
533 }
534 else {
535 struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
536 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
537
538 if (!newTexObj)
539 newTexObj = gl_alloc_texture_object(ctx->Shared, texName, dim);
540
541 if (newTexObj->Dimensions != dim) {
542 if (newTexObj->Dimensions) {
543 /* the named texture object's dimensions don't match the target */
544 gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" );
545 return;
546 }
547 newTexObj->Dimensions = dim;
548 }
549 }
550
551 newTexObj->RefCount++;
552
553 switch (target) {
554 case GL_TEXTURE_1D:
555 texUnit->CurrentD[1] = newTexObj;
556 break;
557 case GL_TEXTURE_2D:
558 texUnit->CurrentD[2] = newTexObj;
559 break;
560 case GL_TEXTURE_3D:
561 texUnit->CurrentD[3] = newTexObj;
562 break;
563 case GL_TEXTURE_CUBE_MAP_ARB:
564 texUnit->CurrentCubeMap = newTexObj;
565 break;
566 default:
567 gl_problem(ctx, "bad target in BindTexture");
568 }
569
570 /* If we've changed the CurrentD[123] texture object then update the
571 * ctx->Texture.Current pointer to point to the new texture object.
572 */
573 texUnit->Current = texUnit->CurrentD[texUnit->CurrentDimension];
574
575 /* Check if we may have to use a new triangle rasterizer */
576 if ((ctx->IndirectTriangles & DD_SW_RASTERIZE) &&
577 ( oldTexObj->WrapS != newTexObj->WrapS
578 || oldTexObj->WrapT != newTexObj->WrapT
579 || oldTexObj->WrapR != newTexObj->WrapR
580 || oldTexObj->MinFilter != newTexObj->MinFilter
581 || oldTexObj->MagFilter != newTexObj->MagFilter
582 || (oldTexObj->Image[0] && newTexObj->Image[0] &&
583 (oldTexObj->Image[0]->Format!=newTexObj->Image[0]->Format))))
584 {
585 ctx->NewState |= (NEW_RASTER_OPS | NEW_TEXTURING);
586 }
587
588 if (oldTexObj->Complete != newTexObj->Complete)
589 ctx->NewState |= NEW_TEXTURING;
590
591 /* Pass BindTexture call to device driver */
592 if (ctx->Driver.BindTexture) {
593 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
594 }
595
596 if (oldTexObj->Name > 0) {
597 /* never delete default (id=0) texture objects */
598 oldTexObj->RefCount--;
599 if (oldTexObj->RefCount <= 0) {
600 if (ctx->Driver.DeleteTexture) {
601 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
602 }
603 gl_free_texture_object(ctx->Shared, oldTexObj);
604 }
605 }
606 }
607
608
609
610 /*
611 * Execute glPrioritizeTextures
612 */
613 void
614 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
615 const GLclampf *priorities )
616 {
617 GET_CURRENT_CONTEXT(ctx);
618 GLint i;
619
620 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPrioritizeTextures");
621 if (n < 0) {
622 gl_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
623 return;
624 }
625
626 if (!priorities)
627 return;
628
629 for (i = 0; i < n; i++) {
630 if (texName[i] > 0) {
631 struct gl_texture_object *t = (struct gl_texture_object *)
632 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
633 if (t) {
634 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
635 if (ctx->Driver.PrioritizeTexture)
636 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
637 }
638 }
639 }
640 }
641
642
643
644 /*
645 * Execute glAreTexturesResident
646 */
647 GLboolean
648 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
649 GLboolean *residences)
650 {
651 GET_CURRENT_CONTEXT(ctx);
652 GLboolean allResident = GL_TRUE;
653 GLint i;
654
655 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx,
656 "glAreTexturesResident", GL_FALSE);
657 if (n < 0) {
658 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
659 return GL_FALSE;
660 }
661
662 if (!texName || !residences)
663 return GL_FALSE;
664
665 for (i = 0; i < n; i++) {
666 struct gl_texture_object *t;
667 if (texName[i] == 0) {
668 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
669 return GL_FALSE;
670 }
671 t = (struct gl_texture_object *)
672 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
673 if (t) {
674 if (ctx->Driver.IsTextureResident) {
675 residences[i] = ctx->Driver.IsTextureResident(ctx, t);
676 if (!residences[i])
677 allResident = GL_FALSE;
678 }
679 else {
680 residences[i] = GL_TRUE;
681 }
682 }
683 else {
684 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
685 return GL_FALSE;
686 }
687 }
688 return allResident;
689 }
690
691
692
693 /*
694 * Execute glIsTexture
695 */
696 GLboolean
697 _mesa_IsTexture( GLuint texture )
698 {
699 GET_CURRENT_CONTEXT(ctx);
700 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glIsTextures",
701 GL_FALSE);
702 if (texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture)) {
703 return GL_TRUE;
704 }
705 else {
706 return GL_FALSE;
707 }
708 }
709