blob: f090fcd734c9c5651900888b9e6459f03a24c243 [file] [log] [blame]
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00001/* $Id: texobj.c,v 1.35 2000/11/22 07:32:17 joukj 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 *
Brian Paulbb797902000-01-24 16:19:54 +00007 * Copyright (C) 1999-2000 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 *
Brian Paula8523782000-11-19 23:10:25 +000057_mesa_alloc_texture_object( struct gl_shared_state *shared, GLuint name,
58 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 Paul4bdcfe52000-04-17 17:57:04 +000082 _mesa_init_colortable(&obj->Palette);
jtgafb833d1999-08-19 00:55:39 +000083
84 /* insert into linked list */
85 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +000086 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000087 obj->Next = shared->TexObjectList;
88 shared->TexObjectList = obj;
Brian Paul9560f052000-01-31 23:11:39 +000089 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +000090 }
91
92 if (name > 0) {
93 /* insert into hash table */
Brian Paulbb797902000-01-24 16:19:54 +000094 _mesa_HashInsert(shared->TexObjects, name, obj);
jtgafb833d1999-08-19 00:55:39 +000095 }
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 */
Brian Paula8523782000-11-19 23:10:25 +0000107void _mesa_free_texture_object( struct gl_shared_state *shared,
108 struct gl_texture_object *t )
jtgafb833d1999-08-19 00:55:39 +0000109{
110 struct gl_texture_object *tprev, *tcurr;
111
112 assert(t);
113
jtgafb833d1999-08-19 00:55:39 +0000114 /* unlink t from the linked list */
115 if (shared) {
Brian Paul9560f052000-01-31 23:11:39 +0000116 _glthread_LOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000117 tprev = NULL;
118 tcurr = shared->TexObjectList;
119 while (tcurr) {
120 if (tcurr==t) {
121 if (tprev) {
122 tprev->Next = t->Next;
123 }
124 else {
125 shared->TexObjectList = t->Next;
126 }
127 break;
128 }
129 tprev = tcurr;
130 tcurr = tcurr->Next;
131 }
Brian Paul9560f052000-01-31 23:11:39 +0000132 _glthread_UNLOCK_MUTEX(shared->Mutex);
jtgafb833d1999-08-19 00:55:39 +0000133 }
134
135 if (t->Name) {
136 /* remove from hash table */
Brian Paulbb797902000-01-24 16:19:54 +0000137 _mesa_HashRemove(shared->TexObjects, t->Name);
jtgafb833d1999-08-19 00:55:39 +0000138 }
139
Brian Paul4bdcfe52000-04-17 17:57:04 +0000140 _mesa_free_colortable_data(&t->Palette);
141
Brian Paula8523782000-11-19 23:10:25 +0000142 /* free the texture images */
jtgafb833d1999-08-19 00:55:39 +0000143 {
144 GLuint i;
145 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
146 if (t->Image[i]) {
Brian Paul021a5252000-03-27 17:54:17 +0000147 _mesa_free_texture_image( t->Image[i] );
jtgafb833d1999-08-19 00:55:39 +0000148 }
149 }
150 }
Brian Paula8523782000-11-19 23:10:25 +0000151
jtgafb833d1999-08-19 00:55:39 +0000152 /* free this object */
Brian Paulbd5cdaf1999-10-13 18:42:49 +0000153 FREE( t );
jtgafb833d1999-08-19 00:55:39 +0000154}
155
Brian Paula8523782000-11-19 23:10:25 +0000156
157/*
158 * Report why a texture object is incomplete. (for debug only)
159 */
Brian Paul21d073d2000-10-24 02:53:18 +0000160#if 0
161static void
162incomplete(const struct gl_texture_object *t, const char *why)
163{
164 printf("Texture Obj %d incomplete because: %s\n", t->Name, why);
165}
166#else
167#define incomplete(a, b)
168#endif
jtgafb833d1999-08-19 00:55:39 +0000169
170
171/*
Brian Paula8523782000-11-19 23:10:25 +0000172 * Examine a texture object to determine if it is complete.
jtgafb833d1999-08-19 00:55:39 +0000173 * The t->Complete flag will be set to GL_TRUE or GL_FALSE accordingly.
174 */
Brian Paul35d53012000-05-23 17:14:49 +0000175void
176_mesa_test_texobj_completeness( const GLcontext *ctx,
177 struct gl_texture_object *t )
jtgafb833d1999-08-19 00:55:39 +0000178{
Brian Paul63ec4232000-06-12 16:09:49 +0000179 const GLint baseLevel = t->BaseLevel;
180
jtgafb833d1999-08-19 00:55:39 +0000181 t->Complete = GL_TRUE; /* be optimistic */
182
Brian Paula8523782000-11-19 23:10:25 +0000183 /* Always need the base level image */
Brian Paul63ec4232000-06-12 16:09:49 +0000184 if (!t->Image[baseLevel]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000185 incomplete(t, "Image[baseLevel] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000186 t->Complete = GL_FALSE;
187 return;
188 }
189
190 /* Compute number of mipmap levels */
Brian Paulad817702000-05-30 00:27:24 +0000191 if (t->Dimensions == 1) {
Keith Whitwell14940c42000-11-05 18:40:57 +0000192 t->_P = t->Image[baseLevel]->WidthLog2;
jtgafb833d1999-08-19 00:55:39 +0000193 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000194 else if (t->Dimensions == 2 || t->Dimensions == 6) {
Keith Whitwell14940c42000-11-05 18:40:57 +0000195 t->_P = MAX2(t->Image[baseLevel]->WidthLog2,
196 t->Image[baseLevel]->HeightLog2);
jtgafb833d1999-08-19 00:55:39 +0000197 }
Brian Paulad817702000-05-30 00:27:24 +0000198 else if (t->Dimensions == 3) {
Brian Paul63ec4232000-06-12 16:09:49 +0000199 GLint max = MAX2(t->Image[baseLevel]->WidthLog2,
200 t->Image[baseLevel]->HeightLog2);
201 max = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2));
Keith Whitwell14940c42000-11-05 18:40:57 +0000202 t->_P = max;
jtgafb833d1999-08-19 00:55:39 +0000203 }
204
205 /* Compute M (see the 1.2 spec) used during mipmapping */
Keith Whitwell14940c42000-11-05 18:40:57 +0000206 t->_M = (GLfloat) (MIN2(t->MaxLevel, t->_P) - t->BaseLevel);
jtgafb833d1999-08-19 00:55:39 +0000207
208
Brian Paulad817702000-05-30 00:27:24 +0000209 if (t->Dimensions == 6) {
Brian Paula8523782000-11-19 23:10:25 +0000210 /* make sure that all six cube map level 0 images are the same size */
Brian Paul63ec4232000-06-12 16:09:49 +0000211 const GLint w = t->Image[baseLevel]->Width2;
212 const GLint h = t->Image[baseLevel]->Height2;
213 if (!t->NegX[baseLevel] ||
214 t->NegX[baseLevel]->Width2 != w ||
215 t->NegX[baseLevel]->Height2 != h ||
216 !t->PosY[baseLevel] ||
217 t->PosY[baseLevel]->Width2 != w ||
218 t->PosY[baseLevel]->Height2 != h ||
219 !t->NegY[baseLevel] ||
220 t->NegY[baseLevel]->Width2 != w ||
221 t->NegY[baseLevel]->Height2 != h ||
222 !t->PosZ[baseLevel] ||
223 t->PosZ[baseLevel]->Width2 != w ||
224 t->PosZ[baseLevel]->Height2 != h ||
225 !t->NegZ[baseLevel] ||
226 t->NegZ[baseLevel]->Width2 != w ||
227 t->NegZ[baseLevel]->Height2 != h) {
Brian Paulad817702000-05-30 00:27:24 +0000228 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000229 incomplete(t, "Non-quare cubemap image");
Brian Paulad817702000-05-30 00:27:24 +0000230 return;
231 }
232 }
233
234 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
jtgafb833d1999-08-19 00:55:39 +0000235 /*
236 * Mipmapping: determine if we have a complete set of mipmaps
237 */
238 GLint i;
Brian Paul63ec4232000-06-12 16:09:49 +0000239 GLint minLevel = baseLevel;
Keith Whitwell14940c42000-11-05 18:40:57 +0000240 GLint maxLevel = MIN2(t->_P, ctx->Const.MaxTextureLevels-1);
jtgafb833d1999-08-19 00:55:39 +0000241 maxLevel = MIN2(maxLevel, t->MaxLevel);
242
243 if (minLevel > maxLevel) {
244 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000245 incomplete(t, "minLevel > maxLevel");
jtgafb833d1999-08-19 00:55:39 +0000246 return;
247 }
248
249 /* Test dimension-independent attributes */
250 for (i = minLevel; i <= maxLevel; i++) {
251 if (t->Image[i]) {
Brian Paul63ec4232000-06-12 16:09:49 +0000252 if (t->Image[i]->Format != t->Image[baseLevel]->Format) {
jtgafb833d1999-08-19 00:55:39 +0000253 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000254 incomplete(t, "Format[i] != Format[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000255 return;
256 }
Brian Paul63ec4232000-06-12 16:09:49 +0000257 if (t->Image[i]->Border != t->Image[baseLevel]->Border) {
jtgafb833d1999-08-19 00:55:39 +0000258 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000259 incomplete(t, "Border[i] != Border[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000260 return;
261 }
262 }
263 }
264
265 /* Test things which depend on number of texture image dimensions */
Brian Paulad817702000-05-30 00:27:24 +0000266 if (t->Dimensions == 1) {
jtgafb833d1999-08-19 00:55:39 +0000267 /* Test 1-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000268 GLuint width = t->Image[baseLevel]->Width2;
269 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000270 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000271 width /= 2;
272 }
273 if (i >= minLevel && i <= maxLevel) {
274 if (!t->Image[i]) {
275 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000276 incomplete(t, "1D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000277 return;
278 }
jtgafb833d1999-08-19 00:55:39 +0000279 if (t->Image[i]->Width2 != width ) {
280 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000281 incomplete(t, "1D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000282 return;
283 }
284 }
Brian Paulad817702000-05-30 00:27:24 +0000285 if (width == 1) {
jtgafb833d1999-08-19 00:55:39 +0000286 return; /* found smallest needed mipmap, all done! */
287 }
288 }
289 }
Brian Paulad817702000-05-30 00:27:24 +0000290 else if (t->Dimensions == 2) {
jtgafb833d1999-08-19 00:55:39 +0000291 /* Test 2-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000292 GLuint width = t->Image[baseLevel]->Width2;
293 GLuint height = t->Image[baseLevel]->Height2;
294 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000295 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000296 width /= 2;
297 }
Brian Paulad817702000-05-30 00:27:24 +0000298 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000299 height /= 2;
300 }
301 if (i >= minLevel && i <= maxLevel) {
302 if (!t->Image[i]) {
303 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000304 incomplete(t, "2D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000305 return;
306 }
307 if (t->Image[i]->Width2 != width) {
308 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000309 incomplete(t, "2D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000310 return;
311 }
312 if (t->Image[i]->Height2 != height) {
313 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000314 incomplete(t, "2D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000315 return;
316 }
317 if (width==1 && height==1) {
318 return; /* found smallest needed mipmap, all done! */
319 }
320 }
321 }
322 }
Brian Paulad817702000-05-30 00:27:24 +0000323 else if (t->Dimensions == 3) {
jtgafb833d1999-08-19 00:55:39 +0000324 /* Test 3-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000325 GLuint width = t->Image[baseLevel]->Width2;
326 GLuint height = t->Image[baseLevel]->Height2;
327 GLuint depth = t->Image[baseLevel]->Depth2;
328 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000329 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000330 width /= 2;
331 }
Brian Paulad817702000-05-30 00:27:24 +0000332 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000333 height /= 2;
334 }
Brian Paulad817702000-05-30 00:27:24 +0000335 if (depth > 1) {
jtgafb833d1999-08-19 00:55:39 +0000336 depth /= 2;
337 }
338 if (i >= minLevel && i <= maxLevel) {
339 if (!t->Image[i]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000340 incomplete(t, "3D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000341 t->Complete = GL_FALSE;
342 return;
343 }
344 if (t->Image[i]->Width2 != width) {
345 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000346 incomplete(t, "3D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000347 return;
348 }
349 if (t->Image[i]->Height2 != height) {
350 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000351 incomplete(t, "3D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000352 return;
353 }
354 if (t->Image[i]->Depth2 != depth) {
355 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000356 incomplete(t, "3D Image[i] bad depth");
jtgafb833d1999-08-19 00:55:39 +0000357 return;
358 }
359 }
Brian Paulad817702000-05-30 00:27:24 +0000360 if (width == 1 && height == 1 && depth == 1) {
jtgafb833d1999-08-19 00:55:39 +0000361 return; /* found smallest needed mipmap, all done! */
362 }
363 }
364 }
Brian Paulad817702000-05-30 00:27:24 +0000365 else if (t->Dimensions == 6) {
366 /* make sure 6 cube faces are consistant */
Brian Paul63ec4232000-06-12 16:09:49 +0000367 GLuint width = t->Image[baseLevel]->Width2;
368 GLuint height = t->Image[baseLevel]->Height2;
369 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000370 if (width > 1) {
371 width /= 2;
372 }
373 if (height > 1) {
374 height /= 2;
375 }
376 if (i >= minLevel && i <= maxLevel) {
377 /* check that we have images defined */
378 if (!t->Image[i] || !t->NegX[i] ||
379 !t->PosY[i] || !t->NegY[i] ||
380 !t->PosZ[i] || !t->NegZ[i]) {
381 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000382 incomplete(t, "CubeMap Image[i] == NULL");
Brian Paulad817702000-05-30 00:27:24 +0000383 return;
384 }
385 /* check that all six images have same size */
386 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
387 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
388 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
389 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
390 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
391 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000392 incomplete(t, "CubeMap Image[i] bad size");
Brian Paulad817702000-05-30 00:27:24 +0000393 return;
394 }
395 }
396 if (width == 1 && height == 1) {
397 return; /* found smallest needed mipmap, all done! */
398 }
399 }
Brian Paul413d6a22000-05-26 14:44:59 +0000400 }
jtgafb833d1999-08-19 00:55:39 +0000401 else {
402 /* Dimensions = ??? */
403 gl_problem(NULL, "Bug in gl_test_texture_object_completeness\n");
404 }
405 }
406}
407
408
Brian Paul832179c2000-03-21 17:42:27 +0000409_glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
410
jtgafb833d1999-08-19 00:55:39 +0000411
412/*
413 * Execute glGenTextures
414 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000415void
416_mesa_GenTextures( GLsizei n, GLuint *texName )
jtgafb833d1999-08-19 00:55:39 +0000417{
Brian Paulfbd8f211999-11-11 01:22:25 +0000418 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000419 GLuint first;
420 GLint i;
421
422 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGenTextures");
Brian Paul507d83e2000-08-03 14:03:17 +0000423 if (n < 0) {
jtgafb833d1999-08-19 00:55:39 +0000424 gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
425 return;
426 }
427
Brian Paul507d83e2000-08-03 14:03:17 +0000428 if (!texName)
429 return;
Brian Paul832179c2000-03-21 17:42:27 +0000430
431 /*
432 * This must be atomic (generation and allocation of texture IDs)
433 */
434 _glthread_LOCK_MUTEX(GenTexturesLock);
435
Brian Paulbb797902000-01-24 16:19:54 +0000436 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
jtgafb833d1999-08-19 00:55:39 +0000437
438 /* Return the texture names */
439 for (i=0;i<n;i++) {
440 texName[i] = first + i;
441 }
442
443 /* Allocate new, empty texture objects */
444 for (i=0;i<n;i++) {
445 GLuint name = first + i;
446 GLuint dims = 0;
Brian Paula8523782000-11-19 23:10:25 +0000447 (void) _mesa_alloc_texture_object(ctx->Shared, name, dims);
jtgafb833d1999-08-19 00:55:39 +0000448 }
Brian Paul832179c2000-03-21 17:42:27 +0000449
450 _glthread_UNLOCK_MUTEX(GenTexturesLock);
jtgafb833d1999-08-19 00:55:39 +0000451}
452
453
454
455/*
456 * Execute glDeleteTextures
457 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000458void
459_mesa_DeleteTextures( GLsizei n, const GLuint *texName)
jtgafb833d1999-08-19 00:55:39 +0000460{
Brian Paulfbd8f211999-11-11 01:22:25 +0000461 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000462 GLint i;
463
464 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDeleteTextures");
465
Brian Paul507d83e2000-08-03 14:03:17 +0000466 if (!texName)
467 return;
468
jtgafb833d1999-08-19 00:55:39 +0000469 for (i=0;i<n;i++) {
Brian Paula8523782000-11-19 23:10:25 +0000470 if (texName[i] > 0) {
471 struct gl_texture_object *delObj = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000472 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
Brian Paula8523782000-11-19 23:10:25 +0000473 if (delObj) {
Brian Paul59d6da52000-02-12 01:59:19 +0000474 /* First check if this texture is currently bound.
475 * If so, unbind it and decrement the reference count.
476 */
jtgafb833d1999-08-19 00:55:39 +0000477 GLuint u;
Brian Paul59d6da52000-02-12 01:59:19 +0000478 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
jtgafb833d1999-08-19 00:55:39 +0000479 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
Brian Paula8523782000-11-19 23:10:25 +0000480 if (delObj == unit->Current1D) {
481 unit->Current1D = ctx->Shared->Default1D;
482 ctx->Shared->Default1D->RefCount++;
483 }
484 else if (delObj == unit->Current2D) {
485 unit->Current2D = ctx->Shared->Default2D;
486 ctx->Shared->Default2D->RefCount++;
487 }
488 else if (delObj == unit->Current3D) {
489 unit->Current3D = ctx->Shared->Default3D;
490 ctx->Shared->Default3D->RefCount++;
491 }
492 else if (delObj == unit->CurrentCubeMap) {
493 unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
494 ctx->Shared->DefaultCubeMap->RefCount++;
495 }
jtgafb833d1999-08-19 00:55:39 +0000496 }
Brian Paula8523782000-11-19 23:10:25 +0000497 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +0000498
Brian Paul59d6da52000-02-12 01:59:19 +0000499 /* Decrement reference count and delete if zero */
Brian Paula8523782000-11-19 23:10:25 +0000500 delObj->RefCount--;
501 ASSERT( delObj->RefCount >= 0 );
502 if (delObj->RefCount == 0) {
Brian Paul59d6da52000-02-12 01:59:19 +0000503 if (ctx->Driver.DeleteTexture)
Brian Paula8523782000-11-19 23:10:25 +0000504 (*ctx->Driver.DeleteTexture)( ctx, delObj );
505 _mesa_free_texture_object(ctx->Shared, delObj);
jtgafb833d1999-08-19 00:55:39 +0000506 }
507 }
508 }
509 }
510}
511
512
513
514/*
515 * Execute glBindTexture
516 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000517void
518_mesa_BindTexture( GLenum target, GLuint texName )
jtgafb833d1999-08-19 00:55:39 +0000519{
Brian Paulfbd8f211999-11-11 01:22:25 +0000520 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000521 GLuint unit = ctx->Texture.CurrentUnit;
522 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
523 struct gl_texture_object *oldTexObj;
524 struct gl_texture_object *newTexObj;
Brian Paula8523782000-11-19 23:10:25 +0000525 GLuint targetDim;
jtgafb833d1999-08-19 00:55:39 +0000526
527 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
528 fprintf(stderr, "glBindTexture %s %d\n",
529 gl_lookup_enum_by_nr(target), (GLint) texName);
530
531 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glBindTexture");
532
Brian Paul420ef641999-12-01 21:10:08 +0000533 switch (target) {
534 case GL_TEXTURE_1D:
Brian Paula8523782000-11-19 23:10:25 +0000535 targetDim = 1;
536 oldTexObj = texUnit->Current1D;
Brian Paul420ef641999-12-01 21:10:08 +0000537 break;
538 case GL_TEXTURE_2D:
Brian Paula8523782000-11-19 23:10:25 +0000539 targetDim = 2;
540 oldTexObj = texUnit->Current2D;
Brian Paul420ef641999-12-01 21:10:08 +0000541 break;
542 case GL_TEXTURE_3D:
Brian Paula8523782000-11-19 23:10:25 +0000543 targetDim = 3;
544 oldTexObj = texUnit->Current3D;
Brian Paul420ef641999-12-01 21:10:08 +0000545 break;
Brian Paulfc4b4432000-05-23 15:17:12 +0000546 case GL_TEXTURE_CUBE_MAP_ARB:
Keith Whitwella96308c2000-10-30 13:31:59 +0000547 if (ctx->Extensions.ARB_texture_cube_map) {
Brian Paula8523782000-11-19 23:10:25 +0000548 targetDim = 6;
Brian Paulfc4b4432000-05-23 15:17:12 +0000549 oldTexObj = texUnit->CurrentCubeMap;
550 break;
551 }
552 /* fallthrough */
Brian Paul420ef641999-12-01 21:10:08 +0000553 default:
554 gl_error( ctx, GL_INVALID_ENUM, "glBindTexture(target)" );
555 return;
jtgafb833d1999-08-19 00:55:39 +0000556 }
557
jtgafb833d1999-08-19 00:55:39 +0000558 if (oldTexObj->Name == texName)
Brian Paula8523782000-11-19 23:10:25 +0000559 return; /* rebinding the same texture- no change */
jtgafb833d1999-08-19 00:55:39 +0000560
Brian Paula8523782000-11-19 23:10:25 +0000561 /*
562 * Get pointer to new texture object (newTexObj)
563 */
Brian Paulfc4b4432000-05-23 15:17:12 +0000564 if (texName == 0) {
Brian Paula8523782000-11-19 23:10:25 +0000565 /* newTexObj = a default texture object */
566 switch (target) {
567 case GL_TEXTURE_1D:
568 newTexObj = ctx->Shared->Default1D;
569 break;
570 case GL_TEXTURE_2D:
571 newTexObj = ctx->Shared->Default2D;
572 break;
573 case GL_TEXTURE_3D:
574 newTexObj = ctx->Shared->Default3D;
575 break;
576 case GL_TEXTURE_CUBE_MAP_ARB:
577 newTexObj = ctx->Shared->DefaultCubeMap;
578 break;
579 default:
580 ; /* Bad targets are caught above */
581 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000582 }
jtgafb833d1999-08-19 00:55:39 +0000583 else {
Brian Paula8523782000-11-19 23:10:25 +0000584 /* non-default texture object */
585 const struct _mesa_HashTable *hash = ctx->Shared->TexObjects;
Brian Paulc79fab42000-01-24 20:53:32 +0000586 newTexObj = (struct gl_texture_object *) _mesa_HashLookup(hash, texName);
Brian Paula8523782000-11-19 23:10:25 +0000587 if (newTexObj) {
588 /* error checking */
589 if (newTexObj->Dimensions > 0 && newTexObj->Dimensions != targetDim) {
Brian Paul420ef641999-12-01 21:10:08 +0000590 /* the named texture object's dimensions don't match the target */
Brian Paula8523782000-11-19 23:10:25 +0000591 gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" );
592 return;
593 }
jtgafb833d1999-08-19 00:55:39 +0000594 }
Brian Paula8523782000-11-19 23:10:25 +0000595 else {
596 /* if this is a new texture id, allocate a texture object now */
597 newTexObj = _mesa_alloc_texture_object(ctx->Shared, texName, targetDim);
598 if (!newTexObj) {
599 gl_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
600 return;
601 }
602 }
603 newTexObj->Dimensions = targetDim;
jtgafb833d1999-08-19 00:55:39 +0000604 }
605
jtgafb833d1999-08-19 00:55:39 +0000606 newTexObj->RefCount++;
Brian Paul6e6d4c61999-10-09 20:17:07 +0000607
Brian Paula8523782000-11-19 23:10:25 +0000608 /* do the actual binding */
Brian Paulfc4b4432000-05-23 15:17:12 +0000609 switch (target) {
610 case GL_TEXTURE_1D:
Brian Paula8523782000-11-19 23:10:25 +0000611 texUnit->Current1D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000612 break;
613 case GL_TEXTURE_2D:
Brian Paula8523782000-11-19 23:10:25 +0000614 texUnit->Current2D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000615 break;
616 case GL_TEXTURE_3D:
Brian Paula8523782000-11-19 23:10:25 +0000617 texUnit->Current3D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000618 break;
619 case GL_TEXTURE_CUBE_MAP_ARB:
620 texUnit->CurrentCubeMap = newTexObj;
621 break;
622 default:
623 gl_problem(ctx, "bad target in BindTexture");
624 }
jtgafb833d1999-08-19 00:55:39 +0000625
Keith Whitwella96308c2000-10-30 13:31:59 +0000626 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +0000627
628 /* Pass BindTexture call to device driver */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000629 if (ctx->Driver.BindTexture)
jtgafb833d1999-08-19 00:55:39 +0000630 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
Brian Paul6e6d4c61999-10-09 20:17:07 +0000631
632 if (oldTexObj->Name > 0) {
633 /* never delete default (id=0) texture objects */
634 oldTexObj->RefCount--;
635 if (oldTexObj->RefCount <= 0) {
636 if (ctx->Driver.DeleteTexture) {
637 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
638 }
Brian Paula8523782000-11-19 23:10:25 +0000639 _mesa_free_texture_object(ctx->Shared, oldTexObj);
Brian Paul6e6d4c61999-10-09 20:17:07 +0000640 }
641 }
jtgafb833d1999-08-19 00:55:39 +0000642}
643
644
645
646/*
647 * Execute glPrioritizeTextures
648 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000649void
650_mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
651 const GLclampf *priorities )
jtgafb833d1999-08-19 00:55:39 +0000652{
Brian Paulfbd8f211999-11-11 01:22:25 +0000653 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000654 GLint i;
655
656 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPrioritizeTextures");
Brian Paul6d047252000-08-02 00:38:25 +0000657 if (n < 0) {
jtgafb833d1999-08-19 00:55:39 +0000658 gl_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
659 return;
660 }
661
Brian Paul507d83e2000-08-03 14:03:17 +0000662 if (!priorities)
663 return;
664
Brian Paul6d047252000-08-02 00:38:25 +0000665 for (i = 0; i < n; i++) {
666 if (texName[i] > 0) {
667 struct gl_texture_object *t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000668 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000669 if (t) {
670 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000671 if (ctx->Driver.PrioritizeTexture)
672 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
jtgafb833d1999-08-19 00:55:39 +0000673 }
674 }
675 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000676
677 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +0000678}
679
680
681
682/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000683 * Execute glAreTexturesResident
jtgafb833d1999-08-19 00:55:39 +0000684 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000685GLboolean
Brian Paulbd0f7f42000-08-02 20:16:03 +0000686_mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
687 GLboolean *residences)
jtgafb833d1999-08-19 00:55:39 +0000688{
Brian Paulfbd8f211999-11-11 01:22:25 +0000689 GET_CURRENT_CONTEXT(ctx);
Brian Paulbd0f7f42000-08-02 20:16:03 +0000690 GLboolean allResident = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000691 GLint i;
692
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000693 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx,
Brian Paulbd0f7f42000-08-02 20:16:03 +0000694 "glAreTexturesResident", GL_FALSE);
695 if (n < 0) {
696 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
jtgafb833d1999-08-19 00:55:39 +0000697 return GL_FALSE;
698 }
699
Brian Paul507d83e2000-08-03 14:03:17 +0000700 if (!texName || !residences)
701 return GL_FALSE;
702
Brian Paulbd0f7f42000-08-02 20:16:03 +0000703 for (i = 0; i < n; i++) {
jtgafb833d1999-08-19 00:55:39 +0000704 struct gl_texture_object *t;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000705 if (texName[i] == 0) {
706 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000707 return GL_FALSE;
708 }
709 t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000710 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000711 if (t) {
Brian Paulbd0f7f42000-08-02 20:16:03 +0000712 if (ctx->Driver.IsTextureResident) {
713 residences[i] = ctx->Driver.IsTextureResident(ctx, t);
714 if (!residences[i])
715 allResident = GL_FALSE;
716 }
717 else {
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000718 residences[i] = GL_TRUE;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000719 }
jtgafb833d1999-08-19 00:55:39 +0000720 }
721 else {
Brian Paulbd0f7f42000-08-02 20:16:03 +0000722 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000723 return GL_FALSE;
724 }
725 }
Brian Paulbd0f7f42000-08-02 20:16:03 +0000726 return allResident;
jtgafb833d1999-08-19 00:55:39 +0000727}
728
729
730
731/*
732 * Execute glIsTexture
733 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000734GLboolean
735_mesa_IsTexture( GLuint texture )
jtgafb833d1999-08-19 00:55:39 +0000736{
Brian Paulfbd8f211999-11-11 01:22:25 +0000737 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000738 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glIsTextures",
739 GL_FALSE);
Brian Paulbd0f7f42000-08-02 20:16:03 +0000740 if (texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture)) {
jtgafb833d1999-08-19 00:55:39 +0000741 return GL_TRUE;
742 }
743 else {
744 return GL_FALSE;
745 }
746}
747