blob: 91876bf5b338a897c82e63dc4619c545196295ab [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001
2/*
3 * Mesa 3-D graphics library
Brian Paulfbd8f211999-11-11 01:22:25 +00004 * Version: 3.3
jtgafb833d1999-08-19 00:55:39 +00005 *
Brian Paulbb797902000-01-24 16:19:54 +00006 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
jtgafb833d1999-08-19 00:55:39 +00007 *
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 */
jtgafb833d1999-08-19 00:55:39 +000025
26
27#ifdef PC_HEADER
28#include "all.h"
29#else
Brian Paulfbd8f211999-11-11 01:22:25 +000030#include "glheader.h"
Brian Paul4bdcfe52000-04-17 17:57:04 +000031#include "colortab.h"
jtgafb833d1999-08-19 00:55:39 +000032#include "context.h"
33#include "enums.h"
34#include "hash.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000035#include "mem.h"
jtgafb833d1999-08-19 00:55:39 +000036#include "teximage.h"
37#include "texstate.h"
38#include "texobj.h"
39#include "types.h"
jtgafb833d1999-08-19 00:55:39 +000040#endif
41
42
43
44/*
45 * Allocate a new texture object and add it to the linked list of texture
46 * objects. If name>0 then also insert the new texture object into the hash
47 * table.
48 * Input: shared - the shared GL state structure to contain the texture object
49 * name - integer name for the texture object
Brian Paulfc4b4432000-05-23 15:17:12 +000050 * dimensions - either 1, 2, 3 or 6 (cube map)
jtgafb833d1999-08-19 00:55:39 +000051 * Return: pointer to new texture object
52 */
53struct gl_texture_object *
54gl_alloc_texture_object( struct gl_shared_state *shared, GLuint name,
55 GLuint dimensions)
56{
57 struct gl_texture_object *obj;
58
Brian Paulfc4b4432000-05-23 15:17:12 +000059 ASSERT(dimensions <= 3 || dimensions == 6);
jtgafb833d1999-08-19 00:55:39 +000060
Brian Paul420ef641999-12-01 21:10:08 +000061 obj = CALLOC_STRUCT(gl_texture_object);
62
jtgafb833d1999-08-19 00:55:39 +000063 if (obj) {
64 /* init the non-zero fields */
Brian Paul6e6d4c61999-10-09 20:17:07 +000065 obj->RefCount = 1;
jtgafb833d1999-08-19 00:55:39 +000066 obj->Name = name;
67 obj->Dimensions = dimensions;
68 obj->WrapS = GL_REPEAT;
69 obj->WrapT = GL_REPEAT;
70 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
71 obj->MagFilter = GL_LINEAR;
72 obj->MinLod = -1000.0;
73 obj->MaxLod = 1000.0;
74 obj->BaseLevel = 0;
75 obj->MaxLevel = 1000;
76 obj->MinMagThresh = 0.0F;
Brian Paul4bdcfe52000-04-17 17:57:04 +000077 _mesa_init_colortable(&obj->Palette);
jtgafb833d1999-08-19 00:55:39 +000078
79 /* insert into linked list */
80 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +000081 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000082 obj->Next = shared->TexObjectList;
83 shared->TexObjectList = obj;
Brian Paul9560f052000-01-31 23:11:39 +000084 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000085 }
86
87 if (name > 0) {
88 /* insert into hash table */
Brian Paulbb797902000-01-24 16:19:54 +000089 _mesa_HashInsert(shared->TexObjects, name, obj);
jtgafb833d1999-08-19 00:55:39 +000090 }
91 }
92 return obj;
93}
94
95
96/*
97 * Deallocate a texture object struct and remove it from the given
98 * shared GL state.
99 * Input: shared - the shared GL state to which the object belongs
100 * t - the texture object to delete
101 */
102void gl_free_texture_object( struct gl_shared_state *shared,
103 struct gl_texture_object *t )
104{
105 struct gl_texture_object *tprev, *tcurr;
106
107 assert(t);
108
109 /* Remove t from dirty list so we don't touch free'd memory later.
110 * Test for shared since Proxy texture aren't in global linked list.
111 */
112 if (shared)
113 gl_remove_texobj_from_dirty_list( shared, t );
114
115 /* unlink t from the linked list */
116 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +0000117 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000118 tprev = NULL;
119 tcurr = shared->TexObjectList;
120 while (tcurr) {
121 if (tcurr==t) {
122 if (tprev) {
123 tprev->Next = t->Next;
124 }
125 else {
126 shared->TexObjectList = t->Next;
127 }
128 break;
129 }
130 tprev = tcurr;
131 tcurr = tcurr->Next;
132 }
Brian Paul9560f052000-01-31 23:11:39 +0000133 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000134 }
135
136 if (t->Name) {
137 /* remove from hash table */
Brian Paulbb797902000-01-24 16:19:54 +0000138 _mesa_HashRemove(shared->TexObjects, t->Name);
jtgafb833d1999-08-19 00:55:39 +0000139 }
140
Brian Paul4bdcfe52000-04-17 17:57:04 +0000141 _mesa_free_colortable_data(&t->Palette);
142
143 /* free texture images */
jtgafb833d1999-08-19 00:55:39 +0000144 {
145 GLuint i;
146 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
147 if (t->Image[i]) {
Brian Paul021a5252000-03-27 17:54:17 +0000148 _mesa_free_texture_image( t->Image[i] );
jtgafb833d1999-08-19 00:55:39 +0000149 }
150 }
151 }
152 /* free this object */
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000153 FREE( t );
jtgafb833d1999-08-19 00:55:39 +0000154}
155
156
157
158/*
159 * Examine a texture object to determine if it is complete or not.
160 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
161 */
Brian Paul35d53012000-05-23 17:14:49 +0000162void
163_mesa_test_texobj_completeness( const GLcontext *ctx,
164 struct gl_texture_object *t )
jtgafb833d1999-08-19 00:55:39 +0000165{
166 t->Complete = GL_TRUE; /* be optimistic */
167
168 /* Always need level zero image */
Brian Paul48271792000-03-29 18:13:59 +0000169 if (!t->Image[0]) {
jtgafb833d1999-08-19 00:55:39 +0000170 t->Complete = GL_FALSE;
171 return;
172 }
173
174 /* Compute number of mipmap levels */
Brian Paulad817702000-05-30 00:27:24 +0000175 if (t->Dimensions == 1) {
jtgafb833d1999-08-19 00:55:39 +0000176 t->P = t->Image[0]->WidthLog2;
177 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000178 else if (t->Dimensions == 2 || t->Dimensions == 6) {
jtgafb833d1999-08-19 00:55:39 +0000179 t->P = MAX2(t->Image[0]->WidthLog2, t->Image[0]->HeightLog2);
180 }
Brian Paulad817702000-05-30 00:27:24 +0000181 else if (t->Dimensions == 3) {
jtgafb833d1999-08-19 00:55:39 +0000182 GLint max = MAX2(t->Image[0]->WidthLog2, t->Image[0]->HeightLog2);
183 max = MAX2(max, (GLint)(t->Image[0]->DepthLog2));
184 t->P = max;
185 }
186
187 /* Compute M (see the 1.2 spec) used during mipmapping */
188 t->M = (GLfloat) (MIN2(t->MaxLevel, t->P) - t->BaseLevel);
189
190
Brian Paulad817702000-05-30 00:27:24 +0000191 if (t->Dimensions == 6) {
192 /* make sure all six level 0 images are same size */
193 const GLint w = t->Image[0]->Width2;
194 const GLint h = t->Image[0]->Height2;
195 if (!t->NegX[0] || t->NegX[0]->Width2 != w || t->NegX[0]->Height2 != h ||
196 !t->PosY[0] || t->PosY[0]->Width2 != w || t->PosY[0]->Height2 != h ||
197 !t->NegY[0] || t->NegY[0]->Width2 != w || t->NegY[0]->Height2 != h ||
198 !t->PosZ[0] || t->PosZ[0]->Width2 != w || t->PosZ[0]->Height2 != h ||
199 !t->NegZ[0] || t->NegZ[0]->Width2 != w || t->NegZ[0]->Height2 != h) {
200 t->Complete = GL_FALSE;
201 return;
202 }
203 }
204
205 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
jtgafb833d1999-08-19 00:55:39 +0000206 /*
207 * Mipmapping: determine if we have a complete set of mipmaps
208 */
209 GLint i;
210 GLint minLevel = t->BaseLevel;
211 GLint maxLevel = MIN2(t->P, ctx->Const.MaxTextureLevels-1);
212 maxLevel = MIN2(maxLevel, t->MaxLevel);
213
214 if (minLevel > maxLevel) {
215 t->Complete = GL_FALSE;
216 return;
217 }
218
219 /* Test dimension-independent attributes */
220 for (i = minLevel; i <= maxLevel; i++) {
221 if (t->Image[i]) {
jtgafb833d1999-08-19 00:55:39 +0000222 if (t->Image[i]->Format != t->Image[0]->Format) {
223 t->Complete = GL_FALSE;
224 return;
225 }
226 if (t->Image[i]->Border != t->Image[0]->Border) {
227 t->Complete = GL_FALSE;
228 return;
229 }
230 }
231 }
232
233 /* Test things which depend on number of texture image dimensions */
Brian Paulad817702000-05-30 00:27:24 +0000234 if (t->Dimensions == 1) {
jtgafb833d1999-08-19 00:55:39 +0000235 /* Test 1-D mipmaps */
236 GLuint width = t->Image[0]->Width2;
Brian Paulad817702000-05-30 00:27:24 +0000237 for (i = 1; i < ctx->Const.MaxTextureLevels; i++) {
238 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000239 width /= 2;
240 }
241 if (i >= minLevel && i <= maxLevel) {
242 if (!t->Image[i]) {
243 t->Complete = GL_FALSE;
244 return;
245 }
jtgafb833d1999-08-19 00:55:39 +0000246 if (t->Image[i]->Width2 != width ) {
247 t->Complete = GL_FALSE;
248 return;
249 }
250 }
Brian Paulad817702000-05-30 00:27:24 +0000251 if (width == 1) {
jtgafb833d1999-08-19 00:55:39 +0000252 return; /* found smallest needed mipmap, all done! */
253 }
254 }
255 }
Brian Paulad817702000-05-30 00:27:24 +0000256 else if (t->Dimensions == 2) {
jtgafb833d1999-08-19 00:55:39 +0000257 /* Test 2-D mipmaps */
258 GLuint width = t->Image[0]->Width2;
259 GLuint height = t->Image[0]->Height2;
Brian Paulad817702000-05-30 00:27:24 +0000260 for (i = 1; i < ctx->Const.MaxTextureLevels; i++) {
261 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000262 width /= 2;
263 }
Brian Paulad817702000-05-30 00:27:24 +0000264 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000265 height /= 2;
266 }
267 if (i >= minLevel && i <= maxLevel) {
268 if (!t->Image[i]) {
269 t->Complete = GL_FALSE;
270 return;
271 }
272 if (t->Image[i]->Width2 != width) {
273 t->Complete = GL_FALSE;
274 return;
275 }
276 if (t->Image[i]->Height2 != height) {
277 t->Complete = GL_FALSE;
278 return;
279 }
280 if (width==1 && height==1) {
281 return; /* found smallest needed mipmap, all done! */
282 }
283 }
284 }
285 }
Brian Paulad817702000-05-30 00:27:24 +0000286 else if (t->Dimensions == 3) {
jtgafb833d1999-08-19 00:55:39 +0000287 /* Test 3-D mipmaps */
288 GLuint width = t->Image[0]->Width2;
289 GLuint height = t->Image[0]->Height2;
290 GLuint depth = t->Image[0]->Depth2;
Brian Paulad817702000-05-30 00:27:24 +0000291 for (i = 1; i < ctx->Const.MaxTextureLevels; i++) {
292 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000293 width /= 2;
294 }
Brian Paulad817702000-05-30 00:27:24 +0000295 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000296 height /= 2;
297 }
Brian Paulad817702000-05-30 00:27:24 +0000298 if (depth > 1) {
jtgafb833d1999-08-19 00:55:39 +0000299 depth /= 2;
300 }
301 if (i >= minLevel && i <= maxLevel) {
302 if (!t->Image[i]) {
303 t->Complete = GL_FALSE;
304 return;
305 }
306 if (t->Image[i]->Width2 != width) {
307 t->Complete = GL_FALSE;
308 return;
309 }
310 if (t->Image[i]->Height2 != height) {
311 t->Complete = GL_FALSE;
312 return;
313 }
314 if (t->Image[i]->Depth2 != depth) {
315 t->Complete = GL_FALSE;
316 return;
317 }
318 }
Brian Paulad817702000-05-30 00:27:24 +0000319 if (width == 1 && height == 1 && depth == 1) {
jtgafb833d1999-08-19 00:55:39 +0000320 return; /* found smallest needed mipmap, all done! */
321 }
322 }
323 }
Brian Paulad817702000-05-30 00:27:24 +0000324 else if (t->Dimensions == 6) {
325 /* make sure 6 cube faces are consistant */
326 GLuint width = t->Image[0]->Width2;
327 GLuint height = t->Image[0]->Height2;
328 for (i = 1; i < ctx->Const.MaxTextureLevels; i++) {
329 if (width > 1) {
330 width /= 2;
331 }
332 if (height > 1) {
333 height /= 2;
334 }
335 if (i >= minLevel && i <= maxLevel) {
336 /* check that we have images defined */
337 if (!t->Image[i] || !t->NegX[i] ||
338 !t->PosY[i] || !t->NegY[i] ||
339 !t->PosZ[i] || !t->NegZ[i]) {
340 t->Complete = GL_FALSE;
341 return;
342 }
343 /* check that all six images have same size */
344 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
345 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
346 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
347 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
348 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
349 t->Complete = GL_FALSE;
350 return;
351 }
352 }
353 if (width == 1 && height == 1) {
354 return; /* found smallest needed mipmap, all done! */
355 }
356 }
Brian Paul413d6a22000-05-26 14:44:59 +0000357 }
jtgafb833d1999-08-19 00:55:39 +0000358 else {
359 /* Dimensions = ??? */
360 gl_problem(NULL, "Bug in gl_test_texture_object_completeness\n");
361 }
362 }
363}
364
365
Brian Paul832179c2000-03-21 17:42:27 +0000366_glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
367
jtgafb833d1999-08-19 00:55:39 +0000368
369/*
370 * Execute glGenTextures
371 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000372void
373_mesa_GenTextures( GLsizei n, GLuint *texName )
jtgafb833d1999-08-19 00:55:39 +0000374{
Brian Paulfbd8f211999-11-11 01:22:25 +0000375 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000376 GLuint first;
377 GLint i;
378
379 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGenTextures");
380 if (n<0) {
381 gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
382 return;
383 }
384
Brian Paul832179c2000-03-21 17:42:27 +0000385
386 /*
387 * This must be atomic (generation and allocation of texture IDs)
388 */
389 _glthread_LOCK_MUTEX(GenTexturesLock);
390
Brian Paulbb797902000-01-24 16:19:54 +0000391 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
jtgafb833d1999-08-19 00:55:39 +0000392
393 /* Return the texture names */
394 for (i=0;i<n;i++) {
395 texName[i] = first + i;
396 }
397
398 /* Allocate new, empty texture objects */
399 for (i=0;i<n;i++) {
400 GLuint name = first + i;
401 GLuint dims = 0;
402 (void) gl_alloc_texture_object(ctx->Shared, name, dims);
403 }
Brian Paul832179c2000-03-21 17:42:27 +0000404
405 _glthread_UNLOCK_MUTEX(GenTexturesLock);
jtgafb833d1999-08-19 00:55:39 +0000406}
407
408
409
410/*
411 * Execute glDeleteTextures
412 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000413void
414_mesa_DeleteTextures( GLsizei n, const GLuint *texName)
jtgafb833d1999-08-19 00:55:39 +0000415{
Brian Paulfbd8f211999-11-11 01:22:25 +0000416 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000417 GLint i;
418
419 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDeleteTextures");
420
421 for (i=0;i<n;i++) {
422 struct gl_texture_object *t;
423 if (texName[i]>0) {
424 t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000425 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000426 if (t) {
Brian Paul59d6da52000-02-12 01:59:19 +0000427 /* First check if this texture is currently bound.
428 * If so, unbind it and decrement the reference count.
429 */
jtgafb833d1999-08-19 00:55:39 +0000430 GLuint u;
Brian Paul59d6da52000-02-12 01:59:19 +0000431 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
jtgafb833d1999-08-19 00:55:39 +0000432 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
433 GLuint d;
434 for (d = 1 ; d <= 3 ; d++) {
Brian Paul59d6da52000-02-12 01:59:19 +0000435 if (unit->CurrentD[d] == t) {
Brian Paul6e6d4c61999-10-09 20:17:07 +0000436 unit->CurrentD[d] = ctx->Shared->DefaultD[d];
437 ctx->Shared->DefaultD[d]->RefCount++;
jtgafb833d1999-08-19 00:55:39 +0000438 t->RefCount--;
Brian Paul59d6da52000-02-12 01:59:19 +0000439 ASSERT( t->RefCount >= 0 );
jtgafb833d1999-08-19 00:55:39 +0000440 }
441 }
442 }
443
Brian Paul59d6da52000-02-12 01:59:19 +0000444 /* Decrement reference count and delete if zero */
445 t->RefCount--;
446 ASSERT( t->RefCount >= 0 );
447 if (t->RefCount == 0) {
448 if (ctx->Driver.DeleteTexture)
449 (*ctx->Driver.DeleteTexture)( ctx, t );
jtgafb833d1999-08-19 00:55:39 +0000450 gl_free_texture_object(ctx->Shared, t);
451 }
452 }
453 }
454 }
455}
456
457
458
459/*
460 * Execute glBindTexture
461 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000462void
463_mesa_BindTexture( GLenum target, GLuint texName )
jtgafb833d1999-08-19 00:55:39 +0000464{
Brian Paulfbd8f211999-11-11 01:22:25 +0000465 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000466 GLuint unit = ctx->Texture.CurrentUnit;
467 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
468 struct gl_texture_object *oldTexObj;
469 struct gl_texture_object *newTexObj;
Brian Paul5b37c321999-11-05 06:43:10 +0000470 GLuint dim;
jtgafb833d1999-08-19 00:55:39 +0000471
472 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
473 fprintf(stderr, "glBindTexture %s %d\n",
474 gl_lookup_enum_by_nr(target), (GLint) texName);
475
476 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glBindTexture");
477
Brian Paul420ef641999-12-01 21:10:08 +0000478 switch (target) {
479 case GL_TEXTURE_1D:
480 dim = 1;
Brian Paulfc4b4432000-05-23 15:17:12 +0000481 oldTexObj = texUnit->CurrentD[1];
Brian Paul420ef641999-12-01 21:10:08 +0000482 break;
483 case GL_TEXTURE_2D:
484 dim = 2;
Brian Paulfc4b4432000-05-23 15:17:12 +0000485 oldTexObj = texUnit->CurrentD[2];
Brian Paul420ef641999-12-01 21:10:08 +0000486 break;
487 case GL_TEXTURE_3D:
488 dim = 3;
Brian Paulfc4b4432000-05-23 15:17:12 +0000489 oldTexObj = texUnit->CurrentD[3];
Brian Paul420ef641999-12-01 21:10:08 +0000490 break;
Brian Paulfc4b4432000-05-23 15:17:12 +0000491 case GL_TEXTURE_CUBE_MAP_ARB:
492 if (ctx->Extensions.HaveTextureCubeMap) {
493 dim = 6;
494 oldTexObj = texUnit->CurrentCubeMap;
495 break;
496 }
497 /* fallthrough */
Brian Paul420ef641999-12-01 21:10:08 +0000498 default:
499 gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
500 return;
jtgafb833d1999-08-19 00:55:39 +0000501 }
502
jtgafb833d1999-08-19 00:55:39 +0000503 if (oldTexObj->Name == texName)
504 return;
505
Brian Paulfc4b4432000-05-23 15:17:12 +0000506 if (texName == 0) {
507 if (target == GL_TEXTURE_CUBE_MAP_ARB)
508 newTexObj = ctx->Shared->DefaultCubeMap;
509 else
510 newTexObj = ctx->Shared->DefaultD[dim];
511 }
jtgafb833d1999-08-19 00:55:39 +0000512 else {
Brian Paulbb797902000-01-24 16:19:54 +0000513 struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
Brian Paulc79fab42000-01-24 20:53:32 +0000514 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
jtgafb833d1999-08-19 00:55:39 +0000515
516 if (!newTexObj)
517 newTexObj = gl_alloc_texture_object(ctx->Shared, texName, dim);
518
519 if (newTexObj->Dimensions != dim) {
520 if (newTexObj->Dimensions) {
Brian Paul420ef641999-12-01 21:10:08 +0000521 /* the named texture object's dimensions don't match the target */
jtgafb833d1999-08-19 00:55:39 +0000522 gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" );
523 return;
524 }
525 newTexObj->Dimensions = dim;
526 }
527 }
528
jtgafb833d1999-08-19 00:55:39 +0000529 newTexObj->RefCount++;
Brian Paul6e6d4c61999-10-09 20:17:07 +0000530
Brian Paulfc4b4432000-05-23 15:17:12 +0000531 switch (target) {
532 case GL_TEXTURE_1D:
533 texUnit->CurrentD[1] = newTexObj;
534 break;
535 case GL_TEXTURE_2D:
536 texUnit->CurrentD[2] = newTexObj;
537 break;
538 case GL_TEXTURE_3D:
539 texUnit->CurrentD[3] = newTexObj;
540 break;
541 case GL_TEXTURE_CUBE_MAP_ARB:
542 texUnit->CurrentCubeMap = newTexObj;
543 break;
544 default:
545 gl_problem(ctx, "bad target in BindTexture");
546 }
jtgafb833d1999-08-19 00:55:39 +0000547
548 /* If we've changed the CurrentD[123] texture object then update the
549 * ctx->Texture.Current pointer to point to the new texture object.
550 */
551 texUnit->Current = texUnit->CurrentD[texUnit->CurrentDimension];
552
553 /* Check if we may have to use a new triangle rasterizer */
554 if ((ctx->IndirectTriangles & DD_SW_RASTERIZE) &&
555 ( oldTexObj->WrapS != newTexObj->WrapS
556 || oldTexObj->WrapT != newTexObj->WrapT
557 || oldTexObj->WrapR != newTexObj->WrapR
558 || oldTexObj->MinFilter != newTexObj->MinFilter
559 || oldTexObj->MagFilter != newTexObj->MagFilter
560 || (oldTexObj->Image[0] && newTexObj->Image[0] &&
561 (oldTexObj->Image[0]->Format!=newTexObj->Image[0]->Format))))
562 {
563 ctx->NewState |= (NEW_RASTER_OPS | NEW_TEXTURING);
564 }
565
566 if (oldTexObj->Complete != newTexObj->Complete)
567 ctx->NewState |= NEW_TEXTURING;
568
569 /* Pass BindTexture call to device driver */
570 if (ctx->Driver.BindTexture) {
571 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
572 }
Brian Paul6e6d4c61999-10-09 20:17:07 +0000573
574 if (oldTexObj->Name > 0) {
575 /* never delete default (id=0) texture objects */
576 oldTexObj->RefCount--;
577 if (oldTexObj->RefCount <= 0) {
578 if (ctx->Driver.DeleteTexture) {
579 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
580 }
581 gl_free_texture_object(ctx->Shared, oldTexObj);
582 }
583 }
jtgafb833d1999-08-19 00:55:39 +0000584}
585
586
587
588/*
589 * Execute glPrioritizeTextures
590 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000591void
592_mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
593 const GLclampf *priorities )
jtgafb833d1999-08-19 00:55:39 +0000594{
Brian Paulfbd8f211999-11-11 01:22:25 +0000595 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000596 GLint i;
597
598 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPrioritizeTextures");
599 if (n<0) {
600 gl_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
601 return;
602 }
603
604 for (i=0;i<n;i++) {
605 struct gl_texture_object *t;
606 if (texName[i]>0) {
607 t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000608 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000609 if (t) {
610 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000611
612 if (ctx->Driver.PrioritizeTexture)
613 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
jtgafb833d1999-08-19 00:55:39 +0000614 }
615 }
616 }
617}
618
619
620
621/*
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000622 * Execute glAreTexturesResident
jtgafb833d1999-08-19 00:55:39 +0000623 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000624GLboolean
625_mesa_AreTexturesResident( GLsizei n, const GLuint *texName,
626 GLboolean *residences )
jtgafb833d1999-08-19 00:55:39 +0000627{
Brian Paulfbd8f211999-11-11 01:22:25 +0000628 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000629 GLboolean resident = GL_TRUE;
630 GLint i;
631
632 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx,
633 "glAreTexturesResident",
634 GL_FALSE);
635 if (n<0) {
636 gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)" );
637 return GL_FALSE;
638 }
639
640 for (i=0;i<n;i++) {
641 struct gl_texture_object *t;
642 if (texName[i]==0) {
643 gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)" );
644 return GL_FALSE;
645 }
646 t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000647 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000648 if (t) {
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000649 if (ctx->Driver.IsTextureResident)
650 residences[i] = ctx->Driver.IsTextureResident( ctx, t );
651 else
652 residences[i] = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000653 }
654 else {
655 gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)" );
656 return GL_FALSE;
657 }
658 }
659 return resident;
660}
661
662
663
664/*
665 * Execute glIsTexture
666 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000667GLboolean
668_mesa_IsTexture( GLuint texture )
jtgafb833d1999-08-19 00:55:39 +0000669{
Brian Paulfbd8f211999-11-11 01:22:25 +0000670 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000671 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glIsTextures",
672 GL_FALSE);
Brian Paulbb797902000-01-24 16:19:54 +0000673 if (texture>0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture)) {
jtgafb833d1999-08-19 00:55:39 +0000674 return GL_TRUE;
675 }
676 else {
677 return GL_FALSE;
678 }
679}
680