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