Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/texobj.c

raw
1 /* $Id: texobj.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include "context.h"
26 #include "macros.h"
27 #include "teximage.h"
28 #include "texobj.h"
29 #include "types.h"
30
31
32
33 /*
34 * Allocate a new texture object structure. The name and dimensionality are
35 * set to zero here and must be initialized by the caller.
36 */
37 struct gl_texture_object *gl_alloc_texture_object( void )
38 {
39 struct gl_texture_object *obj;
40
41 obj = (struct gl_texture_object *)
42 calloc(1,sizeof(struct gl_texture_object));
43 if (obj) {
44 /* init the non-zero fields */
45 obj->WrapS = GL_REPEAT;
46 obj->WrapT = GL_REPEAT;
47 obj->MinFilter = GL_NEAREST_MIPMAP_LINEAR;
48 obj->MagFilter = GL_LINEAR;
49 }
50 return obj;
51 }
52
53
54 /*
55 * Append a gl_texture_object struct to a list of texture objects.
56 */
57 static void append_texture_object( struct gl_texture_object *list,
58 struct gl_texture_object *obj )
59 {
60 struct gl_texture_object *t;
61
62 t = list;
63 while (t->Next) {
64 t = t->Next;
65 }
66 t->Next = obj;
67 }
68
69
70
71 /*
72 * Deallocate a texture object struct and all children structures
73 * and image data.
74 */
75 void gl_free_texture_object( struct gl_texture_object *t )
76 {
77 GLuint i;
78
79 for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
80 if (t->Image[i]) {
81 gl_free_texture_image( t->Image[i] );
82 }
83 }
84 free( t );
85 }
86
87
88 /*
89 * Given a texture object name, return a pointer to the texture object.
90 */
91 static struct gl_texture_object *
92 find_texture_object( GLcontext *ctx, GLuint name )
93 {
94 struct gl_texture_object *t;
95
96 assert( name>0 );
97 t = ctx->Shared->TexObjectList;
98 while (t) {
99 if (t->Name == name) {
100 return t;
101 }
102 t = t->Next;
103 }
104 return NULL;
105 }
106
107
108
109
110 void gl_GenTextures( GLcontext *ctx, GLsizei n, GLuint *textures )
111 {
112 struct gl_texture_object *t;
113 GLuint i, max;
114
115 if (INSIDE_BEGIN_END(ctx)) {
116 gl_error( ctx, GL_INVALID_OPERATION, "glGenTextures" );
117 return;
118 }
119 if (n<0) {
120 gl_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
121 return;
122 }
123
124 /* Find maximum texture object name in use */
125 t = ctx->Shared->TexObjectList;
126 max = 0;
127 while (t) {
128 if (t->Name>max) {
129 max = t->Name;
130 }
131 t = t->Next;
132 }
133
134 /* Return new texture names starting at max+1 */
135 for (i=0;i<n;i++) {
136 max++;
137 textures[i] = max;
138 }
139 }
140
141
142
143 void gl_DeleteTextures( GLcontext *ctx, GLsizei n, const GLuint *textures)
144 {
145 GLuint i;
146
147 if (INSIDE_BEGIN_END(ctx)) {
148 gl_error( ctx, GL_INVALID_OPERATION, "glAreTexturesResident" );
149 return;
150 }
151
152 for (i=0;i<n;i++) {
153 struct gl_texture_object *t, *tprev, *tcurr;
154 if (textures[i]>0) {
155 t = find_texture_object( ctx, textures[i] );
156 if (t) {
157 if (ctx->Texture.Current1D==t) {
158 /* revert to default 1-D texture */
159 ctx->Texture.Current1D = ctx->Shared->TexObjectList;
160 t->RefCount--;
161 assert( t->RefCount >= 0 );
162 }
163 else if (ctx->Texture.Current2D==t) {
164 /* revert to default 2-D texture */
165 ctx->Texture.Current2D = ctx->Shared->TexObjectList->Next;
166 t->RefCount--;
167 assert( t->RefCount >= 0 );
168 }
169 else if (ctx->Texture.Current3D==t) {
170 /* revert to default 3-D texture */
171 ctx->Texture.Current3D = ctx->Shared->TexObjectList->Next;
172 t->RefCount--;
173 assert( t->RefCount >= 0 );
174 }
175 if (t->RefCount==0) {
176 /* remove texture object t from the linked list */
177 tprev = NULL;
178 tcurr = ctx->Shared->TexObjectList;
179 while (tcurr) {
180 if (tcurr==t) {
181 assert( tprev );
182 tprev->Next = t->Next;
183 gl_free_texture_object( t );
184 break;
185 }
186 tprev = tcurr;
187 tcurr = tcurr->Next;
188 }
189 }
190
191 /* tell device driver to delete texture */
192 if (ctx->Driver.DeleteTexture) {
193 (*ctx->Driver.DeleteTexture)( ctx, textures[i] );
194 }
195 }
196 }
197 }
198 }
199
200
201
202 void gl_BindTexture( GLcontext *ctx, GLenum target, GLuint texture )
203 {
204 struct gl_texture_object *oldtexobj;
205 if (INSIDE_BEGIN_END(ctx)) {
206 gl_error( ctx, GL_INVALID_OPERATION, "glAreTexturesResident" );
207 return;
208 }
209 switch (target) {
210 case GL_TEXTURE_1D:
211 oldtexobj = ctx->Texture.Current1D;
212 if (texture==0) {
213 /* use default 1-D texture */
214 ctx->Texture.Current1D = ctx->Shared->TexObjectList;
215 }
216 else {
217 struct gl_texture_object *t;
218 t = find_texture_object( ctx, texture );
219 if (t) {
220 if (t->Dimensions==1) {
221 /* success! */
222 ctx->Texture.Current1D = t;
223 }
224 else {
225 /* wrong dimensionality */
226 gl_error( ctx, GL_INVALID_OPERATION, "glBindTextureEXT" );
227 return;
228 }
229 }
230 else {
231 /* create new texture object */
232 t = gl_alloc_texture_object();
233 append_texture_object( ctx->Shared->TexObjectList, t );
234 t->Name = texture;
235 t->Dimensions = 1;
236 ctx->Texture.Current1D = t;
237 }
238 }
239 /* Tidy up reference counting */
240 if (ctx->Texture.Current1D != oldtexobj && oldtexobj->Name>0) {
241 /* decr reference count of the prev texture object */
242 oldtexobj->RefCount--;
243 assert( oldtexobj->RefCount >= 0 );
244 }
245 if (ctx->Texture.Current1D->Name>0) {
246 ctx->Texture.Current1D->RefCount++;
247 }
248 #ifdef FOO
249 if (!ctx->Texture.Current1D->Complete) {
250 /* re-examine texture completeness */
251 gl_update_texture_state();
252 }
253 #endif
254
255 /* Check if we may have to use a new triangle rasterizer */
256 if (oldtexobj->WrapS != ctx->Texture.Current1D->WrapS
257 || oldtexobj->MinFilter != ctx->Texture.Current1D->MinFilter
258 || oldtexobj->MagFilter != ctx->Texture.Current1D->MagFilter) {
259 ctx->NewState |= NEW_RASTER_OPS;
260 }
261
262 /* The current 1D texture object can never be NULL! */
263 assert(ctx->Texture.Current1D);
264 break;
265
266 case GL_TEXTURE_2D:
267 oldtexobj = ctx->Texture.Current2D;
268 if (texture==0) {
269 /* use default 2-D texture */
270 ctx->Texture.Current1D = ctx->Shared->TexObjectList->Next;
271 }
272 else {
273 struct gl_texture_object *t;
274 t = find_texture_object( ctx, texture );
275 if (t) {
276 if (t->Dimensions==2) {
277 /* success */
278 ctx->Texture.Current2D = t;
279 }
280 else {
281 /* wrong dimensionality */
282 gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" );
283 return;
284 }
285 }
286 else {
287 /* create new texture object */
288 t = gl_alloc_texture_object();
289 append_texture_object( ctx->Shared->TexObjectList, t );
290 t->Name = texture;
291 t->Dimensions = 2;
292 ctx->Texture.Current2D = t;
293 }
294 }
295 /* Tidy up reference counting */
296 if (ctx->Texture.Current2D != oldtexobj && oldtexobj->Name>0) {
297 /* decr reference count of the prev texture object */
298 oldtexobj->RefCount--;
299 assert( oldtexobj->RefCount >= 0 );
300 }
301 if (ctx->Texture.Current2D->Name>0) {
302 ctx->Texture.Current2D->RefCount++;
303 }
304 #ifdef FOO
305 if (!ctx->Texture.Current2D->Complete) {
306 /* re-examine texture completeness */
307 gl_update_texture_state();
308 }
309 #endif
310
311 /* Check if we may have to use a new triangle rasterizer */
312 if (oldtexobj->WrapS != ctx->Texture.Current1D->WrapS
313 || oldtexobj->WrapT != ctx->Texture.Current1D->WrapT
314 || oldtexobj->MinFilter != ctx->Texture.Current1D->MinFilter
315 || oldtexobj->MagFilter != ctx->Texture.Current1D->MagFilter) {
316 ctx->NewState |= NEW_RASTER_OPS;
317 }
318
319 /* The current 2D texture object can never be NULL! */
320 assert(ctx->Texture.Current2D);
321 break;
322
323 case GL_TEXTURE_3D_EXT:
324 oldtexobj = ctx->Texture.Current3D;
325 if (texture==0) {
326 /* use default 3-D texture */
327 ctx->Texture.Current1D = ctx->Shared->TexObjectList->Next->Next;
328 }
329 else {
330 struct gl_texture_object *t;
331 t = find_texture_object( ctx, texture );
332 if (t) {
333 if (t->Dimensions==3) {
334 /* success */
335 ctx->Texture.Current3D = t;
336 }
337 else {
338 /* wrong dimensionality */
339 gl_error( ctx, GL_INVALID_OPERATION, "glBindTexture" );
340 return;
341 }
342 }
343 else {
344 /* create new texture object */
345 t = gl_alloc_texture_object();
346 append_texture_object( ctx->Shared->TexObjectList, t );
347 t->Name = texture;
348 t->Dimensions = 3;
349 ctx->Texture.Current3D = t;
350 }
351 }
352 /* Tidy up reference counting */
353 if (ctx->Texture.Current3D != oldtexobj && oldtexobj->Name>0) {
354 /* decr reference count of the prev texture object */
355 oldtexobj->RefCount--;
356 assert( oldtexobj->RefCount >= 0 );
357 }
358 if (ctx->Texture.Current3D->Name>0) {
359 ctx->Texture.Current3D->RefCount++;
360 }
361 #ifdef FOO
362 if (!ctx->Texture.Current3D->Complete) {
363 /* re-examine texture completeness */
364 gl_update_texture_state();
365 }
366 #endif
367
368 /* Check if we may have to use a new triangle rasterizer */
369 if (oldtexobj->WrapS != ctx->Texture.Current3D->WrapS
370 || oldtexobj->WrapT != ctx->Texture.Current3D->WrapT
371 || oldtexobj->MinFilter != ctx->Texture.Current3D->MinFilter
372 || oldtexobj->MagFilter != ctx->Texture.Current3D->MagFilter) {
373 ctx->NewState |= NEW_RASTER_OPS;
374 }
375
376 /* The current 3D texture object can never be NULL! */
377 assert(ctx->Texture.Current3D);
378 break;
379
380 default:
381 gl_error( ctx, GL_INVALID_ENUM, "glBindTexture" );
382 return;
383 }
384
385 /* Pass BindTexture to device driver */
386 if (ctx->Driver.BindTexture) {
387 (*ctx->Driver.BindTexture)( ctx, target, texture );
388 }
389 }
390
391
392
393 void gl_PrioritizeTextures( GLcontext *ctx,
394 GLsizei n, const GLuint *textures,
395 const GLclampf *priorities )
396 {
397 GLuint i;
398
399 if (INSIDE_BEGIN_END(ctx)) {
400 gl_error( ctx, GL_INVALID_OPERATION, "glAreTexturesResident" );
401 return;
402 }
403 if (n<0) {
404 gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)" );
405 return;
406 }
407
408 for (i=0;i<n;i++) {
409 struct gl_texture_object *t;
410 if (textures[i]>0) {
411 t = find_texture_object( ctx, textures[i] );
412 if (t) {
413 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
414 }
415 }
416 }
417 }
418
419
420
421 GLboolean gl_AreTexturesResident( GLcontext *ctx, GLsizei n,
422 const GLuint *textures,
423 GLboolean *residences )
424 {
425 GLboolean resident = GL_TRUE;
426 GLuint i;
427
428 if (INSIDE_BEGIN_END(ctx)) {
429 gl_error( ctx, GL_INVALID_OPERATION, "glAreTexturesResident" );
430 return GL_FALSE;
431 }
432 if (n<0) {
433 gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)" );
434 return GL_FALSE;
435 }
436
437 for (i=0;i<n;i++) {
438 struct gl_texture_object *t;
439 if (textures[i]==0) {
440 gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)" );
441 return GL_FALSE;
442 }
443 t = find_texture_object( ctx, textures[i] );
444 if (t) {
445 /* we consider all valid texture objects to be resident */
446 residences[i] = GL_TRUE;
447 }
448 else {
449 gl_error( ctx, GL_INVALID_VALUE, "glAreTexturesResident(textures)" );
450 return GL_FALSE;
451 }
452 }
453 return resident;
454 }
455
456
457
458 GLboolean gl_IsTexture( GLcontext *ctx, GLuint texture )
459 {
460 if (INSIDE_BEGIN_END(ctx)) {
461 gl_error( ctx, GL_INVALID_OPERATION, "glIsTextures" );
462 return GL_FALSE;
463 }
464 if (texture>0 && find_texture_object(ctx,texture)) {
465 return GL_TRUE;
466 }
467 else {
468 return GL_FALSE;
469 }
470 }
471