blob: 0bcc0deae8b30adf0b93d79c06ea0835af10bcff [file] [log] [blame]
Gareth Hughes22144ab2001-03-12 00:48:37 +00001/* $Id: texobj.c,v 1.44 2001/03/12 00:48:39 gareth Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paul6d047252000-08-02 00:38:25 +00005 * Version: 3.5
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00006 *
Gareth Hughes22144ab2001-03-12 00:48:37 +00007 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00008 *
jtgafb833d1999-08-19 00:55:39 +00009 * 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:
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000015 *
jtgafb833d1999-08-19 00:55:39 +000016 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000018 *
jtgafb833d1999-08-19 00:55:39 +000019 * 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 */
jtgafb833d1999-08-19 00:55:39 +000026
27
28#ifdef PC_HEADER
29#include "all.h"
30#else
Brian Paulfbd8f211999-11-11 01:22:25 +000031#include "glheader.h"
Brian Paul4bdcfe52000-04-17 17:57:04 +000032#include "colortab.h"
jtgafb833d1999-08-19 00:55:39 +000033#include "context.h"
34#include "enums.h"
35#include "hash.h"
Brian Paulebb248a2000-10-29 18:23:16 +000036#include "macros.h"
Brian Paulfbd8f211999-11-11 01:22:25 +000037#include "mem.h"
jtgafb833d1999-08-19 00:55:39 +000038#include "teximage.h"
39#include "texstate.h"
40#include "texobj.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000041#include "mtypes.h"
jtgafb833d1999-08-19 00:55:39 +000042#endif
43
44
45
46/*
47 * Allocate a new texture object and add it to the linked list of texture
48 * objects. If name>0 then also insert the new texture object into the hash
49 * table.
50 * Input: shared - the shared GL state structure to contain the texture object
51 * name - integer name for the texture object
Brian Paulfc4b4432000-05-23 15:17:12 +000052 * dimensions - either 1, 2, 3 or 6 (cube map)
Brian Paula8523782000-11-19 23:10:25 +000053 * zero is ok for the sake of GenTextures()
jtgafb833d1999-08-19 00:55:39 +000054 * Return: pointer to new texture object
55 */
56struct gl_texture_object *
Gareth Hughes22144ab2001-03-12 00:48:37 +000057_mesa_alloc_texture_object( struct gl_shared_state *shared,
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +000058 GLuint name, GLuint dimensions )
jtgafb833d1999-08-19 00:55:39 +000059{
60 struct gl_texture_object *obj;
61
Brian Paulfc4b4432000-05-23 15:17:12 +000062 ASSERT(dimensions <= 3 || dimensions == 6);
jtgafb833d1999-08-19 00:55:39 +000063
Brian Paul420ef641999-12-01 21:10:08 +000064 obj = CALLOC_STRUCT(gl_texture_object);
65
jtgafb833d1999-08-19 00:55:39 +000066 if (obj) {
67 /* init the non-zero fields */
Brian Paule4b684c2000-09-12 21:07:40 +000068 _glthread_INIT_MUTEX(obj->Mutex);
Brian Paul6e6d4c61999-10-09 20:17:07 +000069 obj->RefCount = 1;
jtgafb833d1999-08-19 00:55:39 +000070 obj->Name = name;
71 obj->Dimensions = dimensions;
Brian Paul6d047252000-08-02 00:38:25 +000072 obj->Priority = 1.0F;
jtgafb833d1999-08-19 00:55:39 +000073 obj->WrapS = GL_REPEAT;
74 obj->WrapT = GL_REPEAT;
Brian Paul73510492000-11-11 20:23:47 +000075 obj->WrapR = GL_REPEAT;
jtgafb833d1999-08-19 00:55:39 +000076 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
77 obj->MagFilter = GL_LINEAR;
78 obj->MinLod = -1000.0;
79 obj->MaxLod = 1000.0;
80 obj->BaseLevel = 0;
81 obj->MaxLevel = 1000;
Brian Paule75d2422001-02-17 18:41:01 +000082 obj->CompareFlag = GL_FALSE;
83 obj->CompareOperator = GL_TEXTURE_LEQUAL_R_SGIX;
Brian Paulc499ce32001-02-20 16:42:25 +000084 obj->ShadowAmbient = 0;
Brian Paul4bdcfe52000-04-17 17:57:04 +000085 _mesa_init_colortable(&obj->Palette);
jtgafb833d1999-08-19 00:55:39 +000086
87 /* insert into linked list */
88 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +000089 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000090 obj->Next = shared->TexObjectList;
91 shared->TexObjectList = obj;
Brian Paul9560f052000-01-31 23:11:39 +000092 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000093 }
94
95 if (name > 0) {
96 /* insert into hash table */
Brian Paulbb797902000-01-24 16:19:54 +000097 _mesa_HashInsert(shared->TexObjects, name, obj);
jtgafb833d1999-08-19 00:55:39 +000098 }
99 }
100 return obj;
101}
102
103
104/*
105 * Deallocate a texture object struct and remove it from the given
106 * shared GL state.
107 * Input: shared - the shared GL state to which the object belongs
108 * t - the texture object to delete
109 */
Brian Paula8523782000-11-19 23:10:25 +0000110void _mesa_free_texture_object( struct gl_shared_state *shared,
111 struct gl_texture_object *t )
jtgafb833d1999-08-19 00:55:39 +0000112{
113 struct gl_texture_object *tprev, *tcurr;
114
115 assert(t);
116
jtgafb833d1999-08-19 00:55:39 +0000117 /* unlink t from the linked list */
118 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +0000119 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000120 tprev = NULL;
121 tcurr = shared->TexObjectList;
122 while (tcurr) {
123 if (tcurr==t) {
124 if (tprev) {
125 tprev->Next = t->Next;
126 }
127 else {
128 shared->TexObjectList = t->Next;
129 }
130 break;
131 }
132 tprev = tcurr;
133 tcurr = tcurr->Next;
134 }
Brian Paul9560f052000-01-31 23:11:39 +0000135 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000136 }
137
138 if (t->Name) {
139 /* remove from hash table */
Brian Paulbb797902000-01-24 16:19:54 +0000140 _mesa_HashRemove(shared->TexObjects, t->Name);
jtgafb833d1999-08-19 00:55:39 +0000141 }
142
Brian Paul4bdcfe52000-04-17 17:57:04 +0000143 _mesa_free_colortable_data(&t->Palette);
144
Brian Paula8523782000-11-19 23:10:25 +0000145 /* free the texture images */
jtgafb833d1999-08-19 00:55:39 +0000146 {
147 GLuint i;
148 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
149 if (t->Image[i]) {
Brian Paul021a5252000-03-27 17:54:17 +0000150 _mesa_free_texture_image( t->Image[i] );
jtgafb833d1999-08-19 00:55:39 +0000151 }
152 }
153 }
Brian Paula8523782000-11-19 23:10:25 +0000154
jtgafb833d1999-08-19 00:55:39 +0000155 /* free this object */
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000156 FREE( t );
jtgafb833d1999-08-19 00:55:39 +0000157}
158
Brian Paula8523782000-11-19 23:10:25 +0000159
160/*
161 * Report why a texture object is incomplete. (for debug only)
162 */
Brian Paul21d073d2000-10-24 02:53:18 +0000163#if 0
164static void
165incomplete(const struct gl_texture_object *t, const char *why)
166{
167 printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
168}
169#else
170#define incomplete(a, b)
171#endif
jtgafb833d1999-08-19 00:55:39 +0000172
173
174/*
Brian Paula8523782000-11-19 23:10:25 +0000175 * Examine a texture object to determine if it is complete.
jtgafb833d1999-08-19 00:55:39 +0000176 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
177 */
Brian Paul35d53012000-05-23 17:14:49 +0000178void
179_mesa_test_texobj_completeness( const GLcontext *ctx,
180 struct gl_texture_object *t )
jtgafb833d1999-08-19 00:55:39 +0000181{
Brian Paul63ec4232000-06-12 16:09:49 +0000182 const GLint baseLevel = t->BaseLevel;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000183 GLint maxLog2 = 0;
Brian Paul63ec4232000-06-12 16:09:49 +0000184
jtgafb833d1999-08-19 00:55:39 +0000185 t->Complete = GL_TRUE; /* be optimistic */
186
Brian Paula8523782000-11-19 23:10:25 +0000187 /* Always need the base level image */
Brian Paul63ec4232000-06-12 16:09:49 +0000188 if (!t->Image[baseLevel]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000189 incomplete(t, "Image[baseLevel] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000190 t->Complete = GL_FALSE;
191 return;
192 }
193
Brian Paul083e4662000-12-14 20:25:56 +0000194 /* Compute _MaxLevel */
Brian Paulad817702000-05-30 00:27:24 +0000195 if (t->Dimensions == 1) {
Brian Paul083e4662000-12-14 20:25:56 +0000196 maxLog2 = t->Image[baseLevel]->WidthLog2;
jtgafb833d1999-08-19 00:55:39 +0000197 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000198 else if (t->Dimensions == 2 || t->Dimensions == 6) {
Brian Paul083e4662000-12-14 20:25:56 +0000199 maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2,
200 t->Image[baseLevel]->HeightLog2);
jtgafb833d1999-08-19 00:55:39 +0000201 }
Brian Paulad817702000-05-30 00:27:24 +0000202 else if (t->Dimensions == 3) {
Brian Paul63ec4232000-06-12 16:09:49 +0000203 GLint max = MAX2(t->Image[baseLevel]->WidthLog2,
204 t->Image[baseLevel]->HeightLog2);
Brian Paul083e4662000-12-14 20:25:56 +0000205 maxLog2 = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2));
jtgafb833d1999-08-19 00:55:39 +0000206 }
207
Brian Paul083e4662000-12-14 20:25:56 +0000208 t->_MaxLevel = baseLevel + maxLog2;
209 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
210 t->_MaxLevel = MIN2(t->_MaxLevel, ctx->Const.MaxTextureLevels - 1);
jtgafb833d1999-08-19 00:55:39 +0000211
Brian Paul083e4662000-12-14 20:25:56 +0000212 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
213 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
jtgafb833d1999-08-19 00:55:39 +0000214
Brian Paulad817702000-05-30 00:27:24 +0000215 if (t->Dimensions == 6) {
Brian Paula8523782000-11-19 23:10:25 +0000216 /* make sure that all six cube map level 0 images are the same size */
Brian Paul01915e92001-03-08 15:23:46 +0000217 const GLuint w = t->Image[baseLevel]->Width2;
218 const GLuint h = t->Image[baseLevel]->Height2;
Brian Paul63ec4232000-06-12 16:09:49 +0000219 if (!t->NegX[baseLevel] ||
220 t->NegX[baseLevel]->Width2 != w ||
221 t->NegX[baseLevel]->Height2 != h ||
222 !t->PosY[baseLevel] ||
223 t->PosY[baseLevel]->Width2 != w ||
224 t->PosY[baseLevel]->Height2 != h ||
225 !t->NegY[baseLevel] ||
226 t->NegY[baseLevel]->Width2 != w ||
227 t->NegY[baseLevel]->Height2 != h ||
228 !t->PosZ[baseLevel] ||
229 t->PosZ[baseLevel]->Width2 != w ||
230 t->PosZ[baseLevel]->Height2 != h ||
231 !t->NegZ[baseLevel] ||
232 t->NegZ[baseLevel]->Width2 != w ||
233 t->NegZ[baseLevel]->Height2 != h) {
Brian Paulad817702000-05-30 00:27:24 +0000234 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000235 incomplete(t, "Non-quare cubemap image");
Brian Paulad817702000-05-30 00:27:24 +0000236 return;
237 }
238 }
239
240 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
jtgafb833d1999-08-19 00:55:39 +0000241 /*
242 * Mipmapping: determine if we have a complete set of mipmaps
243 */
244 GLint i;
Brian Paul63ec4232000-06-12 16:09:49 +0000245 GLint minLevel = baseLevel;
Brian Paul083e4662000-12-14 20:25:56 +0000246 GLint maxLevel = t->_MaxLevel;
jtgafb833d1999-08-19 00:55:39 +0000247
248 if (minLevel > maxLevel) {
249 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000250 incomplete(t, "minLevel > maxLevel");
jtgafb833d1999-08-19 00:55:39 +0000251 return;
252 }
253
254 /* Test dimension-independent attributes */
255 for (i = minLevel; i <= maxLevel; i++) {
256 if (t->Image[i]) {
Brian Paul63ec4232000-06-12 16:09:49 +0000257 if (t->Image[i]->Format != t->Image[baseLevel]->Format) {
jtgafb833d1999-08-19 00:55:39 +0000258 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000259 incomplete(t, "Format[i] != Format[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000260 return;
261 }
Brian Paul63ec4232000-06-12 16:09:49 +0000262 if (t->Image[i]->Border != t->Image[baseLevel]->Border) {
jtgafb833d1999-08-19 00:55:39 +0000263 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000264 incomplete(t, "Border[i] != Border[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000265 return;
266 }
267 }
268 }
269
270 /* Test things which depend on number of texture image dimensions */
Brian Paulad817702000-05-30 00:27:24 +0000271 if (t->Dimensions == 1) {
jtgafb833d1999-08-19 00:55:39 +0000272 /* Test 1-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000273 GLuint width = t->Image[baseLevel]->Width2;
274 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000275 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000276 width /= 2;
277 }
278 if (i >= minLevel && i <= maxLevel) {
279 if (!t->Image[i]) {
280 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000281 incomplete(t, "1D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000282 return;
283 }
jtgafb833d1999-08-19 00:55:39 +0000284 if (t->Image[i]->Width2 != width ) {
285 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000286 incomplete(t, "1D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000287 return;
288 }
289 }
Brian Paulad817702000-05-30 00:27:24 +0000290 if (width == 1) {
jtgafb833d1999-08-19 00:55:39 +0000291 return; /* found smallest needed mipmap, all done! */
292 }
293 }
294 }
Brian Paulad817702000-05-30 00:27:24 +0000295 else if (t->Dimensions == 2) {
jtgafb833d1999-08-19 00:55:39 +0000296 /* Test 2-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000297 GLuint width = t->Image[baseLevel]->Width2;
298 GLuint height = t->Image[baseLevel]->Height2;
299 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000300 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000301 width /= 2;
302 }
Brian Paulad817702000-05-30 00:27:24 +0000303 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000304 height /= 2;
305 }
306 if (i >= minLevel && i <= maxLevel) {
307 if (!t->Image[i]) {
308 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000309 incomplete(t, "2D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000310 return;
311 }
312 if (t->Image[i]->Width2 != width) {
313 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000314 incomplete(t, "2D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000315 return;
316 }
317 if (t->Image[i]->Height2 != height) {
318 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000319 incomplete(t, "2D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000320 return;
321 }
322 if (width==1 && height==1) {
323 return; /* found smallest needed mipmap, all done! */
324 }
325 }
326 }
327 }
Brian Paulad817702000-05-30 00:27:24 +0000328 else if (t->Dimensions == 3) {
jtgafb833d1999-08-19 00:55:39 +0000329 /* Test 3-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000330 GLuint width = t->Image[baseLevel]->Width2;
331 GLuint height = t->Image[baseLevel]->Height2;
332 GLuint depth = t->Image[baseLevel]->Depth2;
333 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000334 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000335 width /= 2;
336 }
Brian Paulad817702000-05-30 00:27:24 +0000337 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000338 height /= 2;
339 }
Brian Paulad817702000-05-30 00:27:24 +0000340 if (depth > 1) {
jtgafb833d1999-08-19 00:55:39 +0000341 depth /= 2;
342 }
343 if (i >= minLevel && i <= maxLevel) {
344 if (!t->Image[i]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000345 incomplete(t, "3D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000346 t->Complete = GL_FALSE;
347 return;
348 }
349 if (t->Image[i]->Width2 != width) {
350 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000351 incomplete(t, "3D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000352 return;
353 }
354 if (t->Image[i]->Height2 != height) {
355 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000356 incomplete(t, "3D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000357 return;
358 }
359 if (t->Image[i]->Depth2 != depth) {
360 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000361 incomplete(t, "3D Image[i] bad depth");
jtgafb833d1999-08-19 00:55:39 +0000362 return;
363 }
364 }
Brian Paulad817702000-05-30 00:27:24 +0000365 if (width == 1 && height == 1 && depth == 1) {
jtgafb833d1999-08-19 00:55:39 +0000366 return; /* found smallest needed mipmap, all done! */
367 }
368 }
369 }
Brian Paulad817702000-05-30 00:27:24 +0000370 else if (t->Dimensions == 6) {
371 /* make sure 6 cube faces are consistant */
Brian Paul63ec4232000-06-12 16:09:49 +0000372 GLuint width = t->Image[baseLevel]->Width2;
373 GLuint height = t->Image[baseLevel]->Height2;
374 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000375 if (width > 1) {
376 width /= 2;
377 }
378 if (height > 1) {
379 height /= 2;
380 }
381 if (i >= minLevel && i <= maxLevel) {
382 /* check that we have images defined */
383 if (!t->Image[i] || !t->NegX[i] ||
384 !t->PosY[i] || !t->NegY[i] ||
385 !t->PosZ[i] || !t->NegZ[i]) {
386 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000387 incomplete(t, "CubeMap Image[i] == NULL");
Brian Paulad817702000-05-30 00:27:24 +0000388 return;
389 }
390 /* check that all six images have same size */
391 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
392 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
393 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
394 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
395 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
396 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000397 incomplete(t, "CubeMap Image[i] bad size");
Brian Paulad817702000-05-30 00:27:24 +0000398 return;
399 }
400 }
401 if (width == 1 && height == 1) {
402 return; /* found smallest needed mipmap, all done! */
403 }
404 }
Brian Paul413d6a22000-05-26 14:44:59 +0000405 }
jtgafb833d1999-08-19 00:55:39 +0000406 else {
407 /* Dimensions = ??? */
Brian Paul08836342001-03-03 20:33:27 +0000408 _mesa_problem(ctx, "Bug in gl_test_texture_object_completeness\n");
jtgafb833d1999-08-19 00:55:39 +0000409 }
410 }
411}
412
413
Brian Paul832179c2000-03-21 17:42:27 +0000414_glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
415
jtgafb833d1999-08-19 00:55:39 +0000416
417/*
418 * Execute glGenTextures
419 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000420void
421_mesa_GenTextures( GLsizei n, GLuint *texName )
jtgafb833d1999-08-19 00:55:39 +0000422{
Brian Paulfbd8f211999-11-11 01:22:25 +0000423 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000424 GLuint first;
425 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000426 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000427
Brian Paul507d83e2000-08-03 14:03:17 +0000428 if (n < 0) {
Brian Paul08836342001-03-03 20:33:27 +0000429 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
jtgafb833d1999-08-19 00:55:39 +0000430 return;
431 }
432
Brian Paul507d83e2000-08-03 14:03:17 +0000433 if (!texName)
434 return;
Brian Paul832179c2000-03-21 17:42:27 +0000435
436 /*
437 * This must be atomic (generation and allocation of texture IDs)
438 */
439 _glthread_LOCK_MUTEX(GenTexturesLock);
440
Brian Paulbb797902000-01-24 16:19:54 +0000441 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
jtgafb833d1999-08-19 00:55:39 +0000442
443 /* Return the texture names */
444 for (i=0;i<n;i++) {
445 texName[i] = first + i;
446 }
447
448 /* Allocate new, empty texture objects */
449 for (i=0;i<n;i++) {
450 GLuint name = first + i;
451 GLuint dims = 0;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000452 (void) _mesa_alloc_texture_object( ctx->Shared, name, dims);
jtgafb833d1999-08-19 00:55:39 +0000453 }
Brian Paul832179c2000-03-21 17:42:27 +0000454
455 _glthread_UNLOCK_MUTEX(GenTexturesLock);
jtgafb833d1999-08-19 00:55:39 +0000456}
457
458
459
460/*
461 * Execute glDeleteTextures
462 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000463void
464_mesa_DeleteTextures( GLsizei n, const GLuint *texName)
jtgafb833d1999-08-19 00:55:39 +0000465{
Brian Paulfbd8f211999-11-11 01:22:25 +0000466 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000467 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000468 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
jtgafb833d1999-08-19 00:55:39 +0000469
Brian Paul507d83e2000-08-03 14:03:17 +0000470 if (!texName)
471 return;
472
jtgafb833d1999-08-19 00:55:39 +0000473 for (i=0;i<n;i++) {
Brian Paula8523782000-11-19 23:10:25 +0000474 if (texName[i] > 0) {
475 struct gl_texture_object *delObj = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000476 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
Brian Paula8523782000-11-19 23:10:25 +0000477 if (delObj) {
Brian Paul59d6da52000-02-12 01:59:19 +0000478 /* First check if this texture is currently bound.
479 * If so, unbind it and decrement the reference count.
480 */
jtgafb833d1999-08-19 00:55:39 +0000481 GLuint u;
Brian Paul59d6da52000-02-12 01:59:19 +0000482 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
jtgafb833d1999-08-19 00:55:39 +0000483 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
Brian Paula8523782000-11-19 23:10:25 +0000484 if (delObj == unit->Current1D) {
485 unit->Current1D = ctx->Shared->Default1D;
486 ctx->Shared->Default1D->RefCount++;
487 }
488 else if (delObj == unit->Current2D) {
489 unit->Current2D = ctx->Shared->Default2D;
490 ctx->Shared->Default2D->RefCount++;
491 }
492 else if (delObj == unit->Current3D) {
493 unit->Current3D = ctx->Shared->Default3D;
494 ctx->Shared->Default3D->RefCount++;
495 }
496 else if (delObj == unit->CurrentCubeMap) {
497 unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
498 ctx->Shared->DefaultCubeMap->RefCount++;
499 }
jtgafb833d1999-08-19 00:55:39 +0000500 }
Brian Paula8523782000-11-19 23:10:25 +0000501 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +0000502
Brian Paul59d6da52000-02-12 01:59:19 +0000503 /* Decrement reference count and delete if zero */
Brian Paula8523782000-11-19 23:10:25 +0000504 delObj->RefCount--;
505 ASSERT( delObj->RefCount >= 0 );
506 if (delObj->RefCount == 0) {
Brian Paul59d6da52000-02-12 01:59:19 +0000507 if (ctx->Driver.DeleteTexture)
Brian Paula8523782000-11-19 23:10:25 +0000508 (*ctx->Driver.DeleteTexture)( ctx, delObj );
509 _mesa_free_texture_object(ctx->Shared, delObj);
jtgafb833d1999-08-19 00:55:39 +0000510 }
511 }
512 }
513 }
514}
515
516
517
518/*
519 * Execute glBindTexture
520 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000521void
522_mesa_BindTexture( GLenum target, GLuint texName )
jtgafb833d1999-08-19 00:55:39 +0000523{
Brian Paulfbd8f211999-11-11 01:22:25 +0000524 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000525 GLuint unit = ctx->Texture.CurrentUnit;
526 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
527 struct gl_texture_object *oldTexObj;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000528 struct gl_texture_object *newTexObj = 0;
Brian Paula8523782000-11-19 23:10:25 +0000529 GLuint targetDim;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000530 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000531
532 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
533 fprintf(stderr, "glBindTexture %s %d\n",
Brian Paul08836342001-03-03 20:33:27 +0000534 _mesa_lookup_enum_by_nr(target), (GLint) texName);
jtgafb833d1999-08-19 00:55:39 +0000535
Brian Paul420ef641999-12-01 21:10:08 +0000536 switch (target) {
537 case GL_TEXTURE_1D:
Brian Paula8523782000-11-19 23:10:25 +0000538 targetDim = 1;
539 oldTexObj = texUnit->Current1D;
Brian Paul420ef641999-12-01 21:10:08 +0000540 break;
541 case GL_TEXTURE_2D:
Brian Paula8523782000-11-19 23:10:25 +0000542 targetDim = 2;
543 oldTexObj = texUnit->Current2D;
Brian Paul420ef641999-12-01 21:10:08 +0000544 break;
545 case GL_TEXTURE_3D:
Brian Paula8523782000-11-19 23:10:25 +0000546 targetDim = 3;
547 oldTexObj = texUnit->Current3D;
Brian Paul420ef641999-12-01 21:10:08 +0000548 break;
Brian Paulfc4b4432000-05-23 15:17:12 +0000549 case GL_TEXTURE_CUBE_MAP_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000550 if (ctx->Extensions.ARB_texture_cube_map) {
Brian Paula8523782000-11-19 23:10:25 +0000551 targetDim = 6;
Brian Paulfc4b4432000-05-23 15:17:12 +0000552 oldTexObj = texUnit->CurrentCubeMap;
553 break;
554 }
555 /* fallthrough */
Brian Paul420ef641999-12-01 21:10:08 +0000556 default:
Brian Paul08836342001-03-03 20:33:27 +0000557 _mesa_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
Brian Paul420ef641999-12-01 21:10:08 +0000558 return;
jtgafb833d1999-08-19 00:55:39 +0000559 }
560
jtgafb833d1999-08-19 00:55:39 +0000561 if (oldTexObj->Name == texName)
Brian Paula8523782000-11-19 23:10:25 +0000562 return; /* rebinding the same texture- no change */
jtgafb833d1999-08-19 00:55:39 +0000563
Brian Paula8523782000-11-19 23:10:25 +0000564 /*
565 * Get pointer to new texture object (newTexObj)
566 */
Brian Paulfc4b4432000-05-23 15:17:12 +0000567 if (texName == 0) {
Brian Paula8523782000-11-19 23:10:25 +0000568 /* newTexObj = a default texture object */
569 switch (target) {
570 case GL_TEXTURE_1D:
571 newTexObj = ctx->Shared->Default1D;
572 break;
573 case GL_TEXTURE_2D:
574 newTexObj = ctx->Shared->Default2D;
575 break;
576 case GL_TEXTURE_3D:
577 newTexObj = ctx->Shared->Default3D;
578 break;
579 case GL_TEXTURE_CUBE_MAP_ARB:
580 newTexObj = ctx->Shared->DefaultCubeMap;
581 break;
582 default:
583 ; /* Bad targets are caught above */
584 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000585 }
jtgafb833d1999-08-19 00:55:39 +0000586 else {
Brian Paula8523782000-11-19 23:10:25 +0000587 /* non-default texture object */
588 const struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
Brian Paulc79fab42000-01-24 20:53:32 +0000589 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
Brian Paula8523782000-11-19 23:10:25 +0000590 if (newTexObj) {
591 /* error checking */
592 if (newTexObj->Dimensions > 0 && newTexObj->Dimensions != targetDim) {
Brian Paul420ef641999-12-01 21:10:08 +0000593 /* the named texture object's dimensions don't match the target */
Brian Paul08836342001-03-03 20:33:27 +0000594 _mesa_error( ctx, GL_INVALID_OPERATION, "glBindTexture" );
Brian Paula8523782000-11-19 23:10:25 +0000595 return;
596 }
jtgafb833d1999-08-19 00:55:39 +0000597 }
Brian Paula8523782000-11-19 23:10:25 +0000598 else {
599 /* if this is a new texture id, allocate a texture object now */
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000600 newTexObj = _mesa_alloc_texture_object( ctx->Shared, texName,
601 targetDim);
Brian Paula8523782000-11-19 23:10:25 +0000602 if (!newTexObj) {
Brian Paul08836342001-03-03 20:33:27 +0000603 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
Brian Paula8523782000-11-19 23:10:25 +0000604 return;
605 }
606 }
607 newTexObj->Dimensions = targetDim;
jtgafb833d1999-08-19 00:55:39 +0000608 }
609
jtgafb833d1999-08-19 00:55:39 +0000610 newTexObj->RefCount++;
Brian Paul6e6d4c61999-10-09 20:17:07 +0000611
Gareth Hughes22144ab2001-03-12 00:48:37 +0000612
Keith Whitwellcab974c2000-12-26 05:09:27 +0000613 /* do the actual binding, but first flush outstanding vertices:
614 */
615 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
616
Brian Paulfc4b4432000-05-23 15:17:12 +0000617 switch (target) {
618 case GL_TEXTURE_1D:
Brian Paula8523782000-11-19 23:10:25 +0000619 texUnit->Current1D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000620 break;
621 case GL_TEXTURE_2D:
Brian Paula8523782000-11-19 23:10:25 +0000622 texUnit->Current2D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000623 break;
624 case GL_TEXTURE_3D:
Brian Paula8523782000-11-19 23:10:25 +0000625 texUnit->Current3D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000626 break;
627 case GL_TEXTURE_CUBE_MAP_ARB:
628 texUnit->CurrentCubeMap = newTexObj;
629 break;
630 default:
Brian Paul08836342001-03-03 20:33:27 +0000631 _mesa_problem(ctx, "bad target in BindTexture");
Brian Paulfc4b4432000-05-23 15:17:12 +0000632 }
jtgafb833d1999-08-19 00:55:39 +0000633
jtgafb833d1999-08-19 00:55:39 +0000634 /* Pass BindTexture call to device driver */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000635 if (ctx->Driver.BindTexture)
jtgafb833d1999-08-19 00:55:39 +0000636 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
Brian Paul6e6d4c61999-10-09 20:17:07 +0000637
638 if (oldTexObj->Name > 0) {
639 /* never delete default (id=0) texture objects */
640 oldTexObj->RefCount--;
641 if (oldTexObj->RefCount <= 0) {
642 if (ctx->Driver.DeleteTexture) {
643 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
644 }
Brian Paula8523782000-11-19 23:10:25 +0000645 _mesa_free_texture_object(ctx->Shared, oldTexObj);
Brian Paul6e6d4c61999-10-09 20:17:07 +0000646 }
647 }
jtgafb833d1999-08-19 00:55:39 +0000648}
649
650
651
652/*
653 * Execute glPrioritizeTextures
654 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000655void
656_mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
657 const GLclampf *priorities )
jtgafb833d1999-08-19 00:55:39 +0000658{
Brian Paulfbd8f211999-11-11 01:22:25 +0000659 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000660 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000661 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +0000662
Brian Paul6d047252000-08-02 00:38:25 +0000663 if (n < 0) {
Brian Paul08836342001-03-03 20:33:27 +0000664 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
jtgafb833d1999-08-19 00:55:39 +0000665 return;
666 }
667
Brian Paul507d83e2000-08-03 14:03:17 +0000668 if (!priorities)
669 return;
670
Brian Paul6d047252000-08-02 00:38:25 +0000671 for (i = 0; i < n; i++) {
672 if (texName[i] > 0) {
673 struct gl_texture_object *t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000674 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000675 if (t) {
676 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000677 if (ctx->Driver.PrioritizeTexture)
678 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
jtgafb833d1999-08-19 00:55:39 +0000679 }
680 }
681 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000682
683 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +0000684}
685
686
687
688/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000689 * Execute glAreTexturesResident
jtgafb833d1999-08-19 00:55:39 +0000690 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000691GLboolean
Brian Paulbd0f7f42000-08-02 20:16:03 +0000692_mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
693 GLboolean *residences)
jtgafb833d1999-08-19 00:55:39 +0000694{
Brian Paulfbd8f211999-11-11 01:22:25 +0000695 GET_CURRENT_CONTEXT(ctx);
Brian Paulbd0f7f42000-08-02 20:16:03 +0000696 GLboolean allResident = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000697 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000698 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
jtgafb833d1999-08-19 00:55:39 +0000699
Brian Paulbd0f7f42000-08-02 20:16:03 +0000700 if (n < 0) {
Brian Paul08836342001-03-03 20:33:27 +0000701 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
jtgafb833d1999-08-19 00:55:39 +0000702 return GL_FALSE;
703 }
704
Brian Paul507d83e2000-08-03 14:03:17 +0000705 if (!texName || !residences)
706 return GL_FALSE;
707
Brian Paulbd0f7f42000-08-02 20:16:03 +0000708 for (i = 0; i < n; i++) {
jtgafb833d1999-08-19 00:55:39 +0000709 struct gl_texture_object *t;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000710 if (texName[i] == 0) {
Brian Paul08836342001-03-03 20:33:27 +0000711 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000712 return GL_FALSE;
713 }
714 t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000715 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000716 if (t) {
Brian Paulbd0f7f42000-08-02 20:16:03 +0000717 if (ctx->Driver.IsTextureResident) {
718 residences[i] = ctx->Driver.IsTextureResident(ctx, t);
719 if (!residences[i])
720 allResident = GL_FALSE;
721 }
722 else {
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000723 residences[i] = GL_TRUE;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000724 }
jtgafb833d1999-08-19 00:55:39 +0000725 }
726 else {
Brian Paul08836342001-03-03 20:33:27 +0000727 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000728 return GL_FALSE;
729 }
730 }
Brian Paulbd0f7f42000-08-02 20:16:03 +0000731 return allResident;
jtgafb833d1999-08-19 00:55:39 +0000732}
733
734
735
736/*
737 * Execute glIsTexture
738 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000739GLboolean
740_mesa_IsTexture( GLuint texture )
jtgafb833d1999-08-19 00:55:39 +0000741{
Brian Paulfbd8f211999-11-11 01:22:25 +0000742 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000743 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
744 return texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture);
jtgafb833d1999-08-19 00:55:39 +0000745}