blob: 0ce694ae1d7ccf950118f13ccdca0f3736a060c3 [file] [log] [blame]
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +00001/* $Id: texobj.c,v 1.38 2001/01/29 20:47:39 keithw 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 *
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +000057_mesa_alloc_texture_object( struct gl_shared_state *shared,
58 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 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;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000180 GLint maxLog2 = 0;
Brian Paul63ec4232000-06-12 16:09:49 +0000181
jtgafb833d1999-08-19 00:55:39 +0000182 t->Complete = GL_TRUE; /* be optimistic */
183
Brian Paula8523782000-11-19 23:10:25 +0000184 /* Always need the base level image */
Brian Paul63ec4232000-06-12 16:09:49 +0000185 if (!t->Image[baseLevel]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000186 incomplete(t, "Image[baseLevel] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000187 t->Complete = GL_FALSE;
188 return;
189 }
190
Brian Paul083e4662000-12-14 20:25:56 +0000191 /* Compute _MaxLevel */
Brian Paulad817702000-05-30 00:27:24 +0000192 if (t->Dimensions == 1) {
Brian Paul083e4662000-12-14 20:25:56 +0000193 maxLog2 = t->Image[baseLevel]->WidthLog2;
jtgafb833d1999-08-19 00:55:39 +0000194 }
Brian Paulfc4b4432000-05-23 15:17:12 +0000195 else if (t->Dimensions == 2 || t->Dimensions == 6) {
Brian Paul083e4662000-12-14 20:25:56 +0000196 maxLog2 = MAX2(t->Image[baseLevel]->WidthLog2,
197 t->Image[baseLevel]->HeightLog2);
jtgafb833d1999-08-19 00:55:39 +0000198 }
Brian Paulad817702000-05-30 00:27:24 +0000199 else if (t->Dimensions == 3) {
Brian Paul63ec4232000-06-12 16:09:49 +0000200 GLint max = MAX2(t->Image[baseLevel]->WidthLog2,
201 t->Image[baseLevel]->HeightLog2);
Brian Paul083e4662000-12-14 20:25:56 +0000202 maxLog2 = MAX2(max, (GLint)(t->Image[baseLevel]->DepthLog2));
jtgafb833d1999-08-19 00:55:39 +0000203 }
204
Brian Paul083e4662000-12-14 20:25:56 +0000205 t->_MaxLevel = baseLevel + maxLog2;
206 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
207 t->_MaxLevel = MIN2(t->_MaxLevel, ctx->Const.MaxTextureLevels - 1);
jtgafb833d1999-08-19 00:55:39 +0000208
Brian Paul083e4662000-12-14 20:25:56 +0000209 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
210 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
jtgafb833d1999-08-19 00:55:39 +0000211
Brian Paulad817702000-05-30 00:27:24 +0000212 if (t->Dimensions == 6) {
Brian Paula8523782000-11-19 23:10:25 +0000213 /* make sure that all six cube map level 0 images are the same size */
Brian Paul63ec4232000-06-12 16:09:49 +0000214 const GLint w = t->Image[baseLevel]->Width2;
215 const GLint h = t->Image[baseLevel]->Height2;
216 if (!t->NegX[baseLevel] ||
217 t->NegX[baseLevel]->Width2 != w ||
218 t->NegX[baseLevel]->Height2 != h ||
219 !t->PosY[baseLevel] ||
220 t->PosY[baseLevel]->Width2 != w ||
221 t->PosY[baseLevel]->Height2 != h ||
222 !t->NegY[baseLevel] ||
223 t->NegY[baseLevel]->Width2 != w ||
224 t->NegY[baseLevel]->Height2 != h ||
225 !t->PosZ[baseLevel] ||
226 t->PosZ[baseLevel]->Width2 != w ||
227 t->PosZ[baseLevel]->Height2 != h ||
228 !t->NegZ[baseLevel] ||
229 t->NegZ[baseLevel]->Width2 != w ||
230 t->NegZ[baseLevel]->Height2 != h) {
Brian Paulad817702000-05-30 00:27:24 +0000231 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000232 incomplete(t, "Non-quare cubemap image");
Brian Paulad817702000-05-30 00:27:24 +0000233 return;
234 }
235 }
236
237 if (t->MinFilter != GL_NEAREST && t->MinFilter != GL_LINEAR) {
jtgafb833d1999-08-19 00:55:39 +0000238 /*
239 * Mipmapping: determine if we have a complete set of mipmaps
240 */
241 GLint i;
Brian Paul63ec4232000-06-12 16:09:49 +0000242 GLint minLevel = baseLevel;
Brian Paul083e4662000-12-14 20:25:56 +0000243 GLint maxLevel = t->_MaxLevel;
jtgafb833d1999-08-19 00:55:39 +0000244
245 if (minLevel > maxLevel) {
246 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000247 incomplete(t, "minLevel > maxLevel");
jtgafb833d1999-08-19 00:55:39 +0000248 return;
249 }
250
251 /* Test dimension-independent attributes */
252 for (i = minLevel; i <= maxLevel; i++) {
253 if (t->Image[i]) {
Brian Paul63ec4232000-06-12 16:09:49 +0000254 if (t->Image[i]->Format != t->Image[baseLevel]->Format) {
jtgafb833d1999-08-19 00:55:39 +0000255 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000256 incomplete(t, "Format[i] != Format[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000257 return;
258 }
Brian Paul63ec4232000-06-12 16:09:49 +0000259 if (t->Image[i]->Border != t->Image[baseLevel]->Border) {
jtgafb833d1999-08-19 00:55:39 +0000260 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000261 incomplete(t, "Border[i] != Border[baseLevel]");
jtgafb833d1999-08-19 00:55:39 +0000262 return;
263 }
264 }
265 }
266
267 /* Test things which depend on number of texture image dimensions */
Brian Paulad817702000-05-30 00:27:24 +0000268 if (t->Dimensions == 1) {
jtgafb833d1999-08-19 00:55:39 +0000269 /* Test 1-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000270 GLuint width = t->Image[baseLevel]->Width2;
271 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000272 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000273 width /= 2;
274 }
275 if (i >= minLevel && i <= maxLevel) {
276 if (!t->Image[i]) {
277 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000278 incomplete(t, "1D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000279 return;
280 }
jtgafb833d1999-08-19 00:55:39 +0000281 if (t->Image[i]->Width2 != width ) {
282 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000283 incomplete(t, "1D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000284 return;
285 }
286 }
Brian Paulad817702000-05-30 00:27:24 +0000287 if (width == 1) {
jtgafb833d1999-08-19 00:55:39 +0000288 return; /* found smallest needed mipmap, all done! */
289 }
290 }
291 }
Brian Paulad817702000-05-30 00:27:24 +0000292 else if (t->Dimensions == 2) {
jtgafb833d1999-08-19 00:55:39 +0000293 /* Test 2-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000294 GLuint width = t->Image[baseLevel]->Width2;
295 GLuint height = t->Image[baseLevel]->Height2;
296 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000297 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000298 width /= 2;
299 }
Brian Paulad817702000-05-30 00:27:24 +0000300 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000301 height /= 2;
302 }
303 if (i >= minLevel && i <= maxLevel) {
304 if (!t->Image[i]) {
305 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000306 incomplete(t, "2D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000307 return;
308 }
309 if (t->Image[i]->Width2 != width) {
310 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000311 incomplete(t, "2D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000312 return;
313 }
314 if (t->Image[i]->Height2 != height) {
315 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000316 incomplete(t, "2D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000317 return;
318 }
319 if (width==1 && height==1) {
320 return; /* found smallest needed mipmap, all done! */
321 }
322 }
323 }
324 }
Brian Paulad817702000-05-30 00:27:24 +0000325 else if (t->Dimensions == 3) {
jtgafb833d1999-08-19 00:55:39 +0000326 /* Test 3-D mipmaps */
Brian Paul63ec4232000-06-12 16:09:49 +0000327 GLuint width = t->Image[baseLevel]->Width2;
328 GLuint height = t->Image[baseLevel]->Height2;
329 GLuint depth = t->Image[baseLevel]->Depth2;
330 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000331 if (width > 1) {
jtgafb833d1999-08-19 00:55:39 +0000332 width /= 2;
333 }
Brian Paulad817702000-05-30 00:27:24 +0000334 if (height > 1) {
jtgafb833d1999-08-19 00:55:39 +0000335 height /= 2;
336 }
Brian Paulad817702000-05-30 00:27:24 +0000337 if (depth > 1) {
jtgafb833d1999-08-19 00:55:39 +0000338 depth /= 2;
339 }
340 if (i >= minLevel && i <= maxLevel) {
341 if (!t->Image[i]) {
Brian Paul21d073d2000-10-24 02:53:18 +0000342 incomplete(t, "3D Image[i] == NULL");
jtgafb833d1999-08-19 00:55:39 +0000343 t->Complete = GL_FALSE;
344 return;
345 }
346 if (t->Image[i]->Width2 != width) {
347 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000348 incomplete(t, "3D Image[i] bad width");
jtgafb833d1999-08-19 00:55:39 +0000349 return;
350 }
351 if (t->Image[i]->Height2 != height) {
352 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000353 incomplete(t, "3D Image[i] bad height");
jtgafb833d1999-08-19 00:55:39 +0000354 return;
355 }
356 if (t->Image[i]->Depth2 != depth) {
357 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000358 incomplete(t, "3D Image[i] bad depth");
jtgafb833d1999-08-19 00:55:39 +0000359 return;
360 }
361 }
Brian Paulad817702000-05-30 00:27:24 +0000362 if (width == 1 && height == 1 && depth == 1) {
jtgafb833d1999-08-19 00:55:39 +0000363 return; /* found smallest needed mipmap, all done! */
364 }
365 }
366 }
Brian Paulad817702000-05-30 00:27:24 +0000367 else if (t->Dimensions == 6) {
368 /* make sure 6 cube faces are consistant */
Brian Paul63ec4232000-06-12 16:09:49 +0000369 GLuint width = t->Image[baseLevel]->Width2;
370 GLuint height = t->Image[baseLevel]->Height2;
371 for (i = baseLevel + 1; i < ctx->Const.MaxTextureLevels; i++) {
Brian Paulad817702000-05-30 00:27:24 +0000372 if (width > 1) {
373 width /= 2;
374 }
375 if (height > 1) {
376 height /= 2;
377 }
378 if (i >= minLevel && i <= maxLevel) {
379 /* check that we have images defined */
380 if (!t->Image[i] || !t->NegX[i] ||
381 !t->PosY[i] || !t->NegY[i] ||
382 !t->PosZ[i] || !t->NegZ[i]) {
383 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000384 incomplete(t, "CubeMap Image[i] == NULL");
Brian Paulad817702000-05-30 00:27:24 +0000385 return;
386 }
387 /* check that all six images have same size */
388 if (t->NegX[i]->Width2!=width || t->NegX[i]->Height2!=height ||
389 t->PosY[i]->Width2!=width || t->PosY[i]->Height2!=height ||
390 t->NegY[i]->Width2!=width || t->NegY[i]->Height2!=height ||
391 t->PosZ[i]->Width2!=width || t->PosZ[i]->Height2!=height ||
392 t->NegZ[i]->Width2!=width || t->NegZ[i]->Height2!=height) {
393 t->Complete = GL_FALSE;
Brian Paul21d073d2000-10-24 02:53:18 +0000394 incomplete(t, "CubeMap Image[i] bad size");
Brian Paulad817702000-05-30 00:27:24 +0000395 return;
396 }
397 }
398 if (width == 1 && height == 1) {
399 return; /* found smallest needed mipmap, all done! */
400 }
401 }
Brian Paul413d6a22000-05-26 14:44:59 +0000402 }
jtgafb833d1999-08-19 00:55:39 +0000403 else {
404 /* Dimensions = ??? */
405 gl_problem(NULL, "Bug in gl_test_texture_object_completeness\n");
406 }
407 }
408}
409
410
Brian Paul832179c2000-03-21 17:42:27 +0000411_glthread_DECLARE_STATIC_MUTEX(GenTexturesLock);
412
jtgafb833d1999-08-19 00:55:39 +0000413
414/*
415 * Execute glGenTextures
416 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000417void
418_mesa_GenTextures( GLsizei n, GLuint *texName )
jtgafb833d1999-08-19 00:55:39 +0000419{
Brian Paulfbd8f211999-11-11 01:22:25 +0000420 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000421 GLuint first;
422 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000423 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000424
Brian Paul507d83e2000-08-03 14:03:17 +0000425 if (n < 0) {
jtgafb833d1999-08-19 00:55:39 +0000426 gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
427 return;
428 }
429
Brian Paul507d83e2000-08-03 14:03:17 +0000430 if (!texName)
431 return;
Brian Paul832179c2000-03-21 17:42:27 +0000432
433 /*
434 * This must be atomic (generation and allocation of texture IDs)
435 */
436 _glthread_LOCK_MUTEX(GenTexturesLock);
437
Brian Paulbb797902000-01-24 16:19:54 +0000438 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
jtgafb833d1999-08-19 00:55:39 +0000439
440 /* Return the texture names */
441 for (i=0;i<n;i++) {
442 texName[i] = first + i;
443 }
444
445 /* Allocate new, empty texture objects */
446 for (i=0;i<n;i++) {
447 GLuint name = first + i;
448 GLuint dims = 0;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000449 (void) _mesa_alloc_texture_object( ctx->Shared, name, dims);
jtgafb833d1999-08-19 00:55:39 +0000450 }
Brian Paul832179c2000-03-21 17:42:27 +0000451
452 _glthread_UNLOCK_MUTEX(GenTexturesLock);
jtgafb833d1999-08-19 00:55:39 +0000453}
454
455
456
457/*
458 * Execute glDeleteTextures
459 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000460void
461_mesa_DeleteTextures( GLsizei n, const GLuint *texName)
jtgafb833d1999-08-19 00:55:39 +0000462{
Brian Paulfbd8f211999-11-11 01:22:25 +0000463 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000464 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000465 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
jtgafb833d1999-08-19 00:55:39 +0000466
Brian Paul507d83e2000-08-03 14:03:17 +0000467 if (!texName)
468 return;
469
jtgafb833d1999-08-19 00:55:39 +0000470 for (i=0;i<n;i++) {
Brian Paula8523782000-11-19 23:10:25 +0000471 if (texName[i] > 0) {
472 struct gl_texture_object *delObj = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000473 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
Brian Paula8523782000-11-19 23:10:25 +0000474 if (delObj) {
Brian Paul59d6da52000-02-12 01:59:19 +0000475 /* First check if this texture is currently bound.
476 * If so, unbind it and decrement the reference count.
477 */
jtgafb833d1999-08-19 00:55:39 +0000478 GLuint u;
Brian Paul59d6da52000-02-12 01:59:19 +0000479 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
jtgafb833d1999-08-19 00:55:39 +0000480 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
Brian Paula8523782000-11-19 23:10:25 +0000481 if (delObj == unit->Current1D) {
482 unit->Current1D = ctx->Shared->Default1D;
483 ctx->Shared->Default1D->RefCount++;
484 }
485 else if (delObj == unit->Current2D) {
486 unit->Current2D = ctx->Shared->Default2D;
487 ctx->Shared->Default2D->RefCount++;
488 }
489 else if (delObj == unit->Current3D) {
490 unit->Current3D = ctx->Shared->Default3D;
491 ctx->Shared->Default3D->RefCount++;
492 }
493 else if (delObj == unit->CurrentCubeMap) {
494 unit->CurrentCubeMap = ctx->Shared->DefaultCubeMap;
495 ctx->Shared->DefaultCubeMap->RefCount++;
496 }
jtgafb833d1999-08-19 00:55:39 +0000497 }
Brian Paula8523782000-11-19 23:10:25 +0000498 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +0000499
Brian Paul59d6da52000-02-12 01:59:19 +0000500 /* Decrement reference count and delete if zero */
Brian Paula8523782000-11-19 23:10:25 +0000501 delObj->RefCount--;
502 ASSERT( delObj->RefCount >= 0 );
503 if (delObj->RefCount == 0) {
Brian Paul59d6da52000-02-12 01:59:19 +0000504 if (ctx->Driver.DeleteTexture)
Brian Paula8523782000-11-19 23:10:25 +0000505 (*ctx->Driver.DeleteTexture)( ctx, delObj );
506 _mesa_free_texture_object(ctx->Shared, delObj);
jtgafb833d1999-08-19 00:55:39 +0000507 }
508 }
509 }
510 }
511}
512
513
514
515/*
516 * Execute glBindTexture
517 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000518void
519_mesa_BindTexture( GLenum target, GLuint texName )
jtgafb833d1999-08-19 00:55:39 +0000520{
Brian Paulfbd8f211999-11-11 01:22:25 +0000521 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000522 GLuint unit = ctx->Texture.CurrentUnit;
523 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
524 struct gl_texture_object *oldTexObj;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000525 struct gl_texture_object *newTexObj = 0;
Brian Paula8523782000-11-19 23:10:25 +0000526 GLuint targetDim;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000527 ASSERT_OUTSIDE_BEGIN_END(ctx);
jtgafb833d1999-08-19 00:55:39 +0000528
529 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
530 fprintf(stderr, "glBindTexture %s %d\n",
531 gl_lookup_enum_by_nr(target), (GLint) texName);
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 */
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000597 newTexObj = _mesa_alloc_texture_object( ctx->Shared, texName,
598 targetDim);
Brian Paula8523782000-11-19 23:10:25 +0000599 if (!newTexObj) {
600 gl_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
601 return;
602 }
603 }
604 newTexObj->Dimensions = targetDim;
jtgafb833d1999-08-19 00:55:39 +0000605 }
606
jtgafb833d1999-08-19 00:55:39 +0000607 newTexObj->RefCount++;
Brian Paul6e6d4c61999-10-09 20:17:07 +0000608
Keith Whitwellcab974c2000-12-26 05:09:27 +0000609
610 /* do the actual binding, but first flush outstanding vertices:
611 */
612 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
613
Brian Paulfc4b4432000-05-23 15:17:12 +0000614 switch (target) {
615 case GL_TEXTURE_1D:
Brian Paula8523782000-11-19 23:10:25 +0000616 texUnit->Current1D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000617 break;
618 case GL_TEXTURE_2D:
Brian Paula8523782000-11-19 23:10:25 +0000619 texUnit->Current2D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000620 break;
621 case GL_TEXTURE_3D:
Brian Paula8523782000-11-19 23:10:25 +0000622 texUnit->Current3D = newTexObj;
Brian Paulfc4b4432000-05-23 15:17:12 +0000623 break;
624 case GL_TEXTURE_CUBE_MAP_ARB:
625 texUnit->CurrentCubeMap = newTexObj;
626 break;
627 default:
628 gl_problem(ctx, "bad target in BindTexture");
629 }
jtgafb833d1999-08-19 00:55:39 +0000630
jtgafb833d1999-08-19 00:55:39 +0000631 /* Pass BindTexture call to device driver */
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000632 if (ctx->Driver.BindTexture)
jtgafb833d1999-08-19 00:55:39 +0000633 (*ctx->Driver.BindTexture)( ctx, target, newTexObj );
Brian Paul6e6d4c61999-10-09 20:17:07 +0000634
635 if (oldTexObj->Name > 0) {
636 /* never delete default (id=0) texture objects */
637 oldTexObj->RefCount--;
638 if (oldTexObj->RefCount <= 0) {
639 if (ctx->Driver.DeleteTexture) {
640 (*ctx->Driver.DeleteTexture)( ctx, oldTexObj );
641 }
Brian Paula8523782000-11-19 23:10:25 +0000642 _mesa_free_texture_object(ctx->Shared, oldTexObj);
Brian Paul6e6d4c61999-10-09 20:17:07 +0000643 }
644 }
jtgafb833d1999-08-19 00:55:39 +0000645}
646
647
648
649/*
650 * Execute glPrioritizeTextures
651 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000652void
653_mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
654 const GLclampf *priorities )
jtgafb833d1999-08-19 00:55:39 +0000655{
Brian Paulfbd8f211999-11-11 01:22:25 +0000656 GET_CURRENT_CONTEXT(ctx);
jtgafb833d1999-08-19 00:55:39 +0000657 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000658 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
jtgafb833d1999-08-19 00:55:39 +0000659
Brian Paul6d047252000-08-02 00:38:25 +0000660 if (n < 0) {
jtgafb833d1999-08-19 00:55:39 +0000661 gl_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
662 return;
663 }
664
Brian Paul507d83e2000-08-03 14:03:17 +0000665 if (!priorities)
666 return;
667
Brian Paul6d047252000-08-02 00:38:25 +0000668 for (i = 0; i < n; i++) {
669 if (texName[i] > 0) {
670 struct gl_texture_object *t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000671 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000672 if (t) {
673 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000674 if (ctx->Driver.PrioritizeTexture)
675 ctx->Driver.PrioritizeTexture( ctx, t, t->Priority );
jtgafb833d1999-08-19 00:55:39 +0000676 }
677 }
678 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000679
680 ctx->NewState |= _NEW_TEXTURE;
jtgafb833d1999-08-19 00:55:39 +0000681}
682
683
684
685/*
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000686 * Execute glAreTexturesResident
jtgafb833d1999-08-19 00:55:39 +0000687 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000688GLboolean
Brian Paulbd0f7f42000-08-02 20:16:03 +0000689_mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
690 GLboolean *residences)
jtgafb833d1999-08-19 00:55:39 +0000691{
Brian Paulfbd8f211999-11-11 01:22:25 +0000692 GET_CURRENT_CONTEXT(ctx);
Brian Paulbd0f7f42000-08-02 20:16:03 +0000693 GLboolean allResident = GL_TRUE;
jtgafb833d1999-08-19 00:55:39 +0000694 GLint i;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000695 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
jtgafb833d1999-08-19 00:55:39 +0000696
Brian Paulbd0f7f42000-08-02 20:16:03 +0000697 if (n < 0) {
698 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
jtgafb833d1999-08-19 00:55:39 +0000699 return GL_FALSE;
700 }
701
Brian Paul507d83e2000-08-03 14:03:17 +0000702 if (!texName || !residences)
703 return GL_FALSE;
704
Brian Paulbd0f7f42000-08-02 20:16:03 +0000705 for (i = 0; i < n; i++) {
jtgafb833d1999-08-19 00:55:39 +0000706 struct gl_texture_object *t;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000707 if (texName[i] == 0) {
708 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000709 return GL_FALSE;
710 }
711 t = (struct gl_texture_object *)
Brian Paulbb797902000-01-24 16:19:54 +0000712 _mesa_HashLookup(ctx->Shared->TexObjects, texName[i]);
jtgafb833d1999-08-19 00:55:39 +0000713 if (t) {
Brian Paulbd0f7f42000-08-02 20:16:03 +0000714 if (ctx->Driver.IsTextureResident) {
715 residences[i] = ctx->Driver.IsTextureResident(ctx, t);
716 if (!residences[i])
717 allResident = GL_FALSE;
718 }
719 else {
Keith Whitwell69cfdb21999-09-30 11:18:21 +0000720 residences[i] = GL_TRUE;
Brian Paulbd0f7f42000-08-02 20:16:03 +0000721 }
jtgafb833d1999-08-19 00:55:39 +0000722 }
723 else {
Brian Paulbd0f7f42000-08-02 20:16:03 +0000724 gl_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)");
jtgafb833d1999-08-19 00:55:39 +0000725 return GL_FALSE;
726 }
727 }
Brian Paulbd0f7f42000-08-02 20:16:03 +0000728 return allResident;
jtgafb833d1999-08-19 00:55:39 +0000729}
730
731
732
733/*
734 * Execute glIsTexture
735 */
Brian Paulfbd8f211999-11-11 01:22:25 +0000736GLboolean
737_mesa_IsTexture( GLuint texture )
jtgafb833d1999-08-19 00:55:39 +0000738{
Brian Paulfbd8f211999-11-11 01:22:25 +0000739 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000740 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
741 return texture > 0 && _mesa_HashLookup(ctx->Shared->TexObjects, texture);
jtgafb833d1999-08-19 00:55:39 +0000742}
743