[MESA]
[reactos.git] / reactos / dll / opengl / mesa / src / mesa / main / texobj.c
1 /**
2 * \file texobj.c
3 * Texture object management.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 7.1
9 *
10 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "mfeatures.h"
32 #include "bufferobj.h"
33 #include "colortab.h"
34 #include "context.h"
35 #include "enums.h"
36 #include "fbobject.h"
37 #include "formats.h"
38 #include "hash.h"
39 #include "imports.h"
40 #include "macros.h"
41 #include "teximage.h"
42 #include "texobj.h"
43 #include "texstate.h"
44 #include "mtypes.h"
45 #include "program/prog_instruction.h"
46
47
48
49 /**********************************************************************/
50 /** \name Internal functions */
51 /*@{*/
52
53
54 /**
55 * Return the gl_texture_object for a given ID.
56 */
57 struct gl_texture_object *
58 _mesa_lookup_texture(struct gl_context *ctx, GLuint id)
59 {
60 return (struct gl_texture_object *)
61 _mesa_HashLookup(ctx->Shared->TexObjects, id);
62 }
63
64
65
66 /**
67 * Allocate and initialize a new texture object. But don't put it into the
68 * texture object hash table.
69 *
70 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
71 * driver.
72 *
73 * \param shared the shared GL state structure to contain the texture object
74 * \param name integer name for the texture object
75 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
76 * GL_TEXTURE_CUBE_MAP_ARB or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
77 * of GenTextures()
78 *
79 * \return pointer to new texture object.
80 */
81 struct gl_texture_object *
82 _mesa_new_texture_object( struct gl_context *ctx, GLuint name, GLenum target )
83 {
84 struct gl_texture_object *obj;
85 (void) ctx;
86 obj = MALLOC_STRUCT(gl_texture_object);
87 _mesa_initialize_texture_object(obj, name, target);
88 return obj;
89 }
90
91
92 /**
93 * Initialize a new texture object to default values.
94 * \param obj the texture object
95 * \param name the texture name
96 * \param target the texture target
97 */
98 void
99 _mesa_initialize_texture_object( struct gl_texture_object *obj,
100 GLuint name, GLenum target )
101 {
102 ASSERT(target == 0 ||
103 target == GL_TEXTURE_1D ||
104 target == GL_TEXTURE_2D ||
105 target == GL_TEXTURE_3D ||
106 target == GL_TEXTURE_CUBE_MAP_ARB ||
107 target == GL_TEXTURE_1D_ARRAY_EXT ||
108 target == GL_TEXTURE_2D_ARRAY_EXT ||
109 target == GL_TEXTURE_BUFFER);
110
111 memset(obj, 0, sizeof(*obj));
112 /* init the non-zero fields */
113 _glthread_INIT_MUTEX(obj->Mutex);
114 obj->RefCount = 1;
115 obj->Name = name;
116 obj->Target = target;
117 obj->Priority = 1.0F;
118 obj->BaseLevel = 0;
119 obj->MaxLevel = 1000;
120
121 /* must be one; no support for (YUV) planes in separate buffers */
122 obj->RequiredTextureImageUnits = 1;
123
124 /* sampler state */
125 obj->Sampler.WrapS = GL_REPEAT;
126 obj->Sampler.WrapT = GL_REPEAT;
127 obj->Sampler.WrapR = GL_REPEAT;
128 obj->Sampler.MinFilter = GL_NEAREST_MIPMAP_LINEAR;
129
130 obj->Sampler.MagFilter = GL_LINEAR;
131 obj->Sampler.MinLod = -1000.0;
132 obj->Sampler.MaxLod = 1000.0;
133 obj->Sampler.LodBias = 0.0;
134 obj->Sampler.MaxAnisotropy = 1.0;
135 }
136
137
138 /**
139 * Deallocate a texture object struct. It should have already been
140 * removed from the texture object pool.
141 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
142 *
143 * \param shared the shared GL state to which the object belongs.
144 * \param texObj the texture object to delete.
145 */
146 void
147 _mesa_delete_texture_object(struct gl_context *ctx,
148 struct gl_texture_object *texObj)
149 {
150 GLuint i, face;
151
152 /* Set Target to an invalid value. With some assertions elsewhere
153 * we can try to detect possible use of deleted textures.
154 */
155 texObj->Target = 0x99;
156
157 /* free the texture images */
158 for (face = 0; face < 6; face++) {
159 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
160 if (texObj->Image[face][i]) {
161 ctx->Driver.DeleteTextureImage(ctx, texObj->Image[face][i]);
162 }
163 }
164 }
165
166 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, NULL);
167
168 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
169 _glthread_DESTROY_MUTEX(texObj->Mutex);
170
171 /* free this object */
172 free(texObj);
173 }
174
175
176
177 /**
178 * Copy texture object state from one texture object to another.
179 * Use for glPush/PopAttrib.
180 *
181 * \param dest destination texture object.
182 * \param src source texture object.
183 */
184 void
185 _mesa_copy_texture_object( struct gl_texture_object *dest,
186 const struct gl_texture_object *src )
187 {
188 dest->Target = src->Target;
189 dest->Name = src->Name;
190 dest->Priority = src->Priority;
191 dest->Sampler.BorderColor.f[0] = src->Sampler.BorderColor.f[0];
192 dest->Sampler.BorderColor.f[1] = src->Sampler.BorderColor.f[1];
193 dest->Sampler.BorderColor.f[2] = src->Sampler.BorderColor.f[2];
194 dest->Sampler.BorderColor.f[3] = src->Sampler.BorderColor.f[3];
195 dest->Sampler.WrapS = src->Sampler.WrapS;
196 dest->Sampler.WrapT = src->Sampler.WrapT;
197 dest->Sampler.WrapR = src->Sampler.WrapR;
198 dest->Sampler.MinFilter = src->Sampler.MinFilter;
199 dest->Sampler.MagFilter = src->Sampler.MagFilter;
200 dest->Sampler.MinLod = src->Sampler.MinLod;
201 dest->Sampler.MaxLod = src->Sampler.MaxLod;
202 dest->Sampler.LodBias = src->Sampler.LodBias;
203 dest->BaseLevel = src->BaseLevel;
204 dest->MaxLevel = src->MaxLevel;
205 dest->Sampler.MaxAnisotropy = src->Sampler.MaxAnisotropy;
206 dest->_MaxLevel = src->_MaxLevel;
207 dest->_MaxLambda = src->_MaxLambda;
208 dest->GenerateMipmap = src->GenerateMipmap;
209 dest->_Complete = src->_Complete;
210
211 dest->RequiredTextureImageUnits = src->RequiredTextureImageUnits;
212 }
213
214
215 /**
216 * Free all texture images of the given texture object.
217 *
218 * \param ctx GL context.
219 * \param t texture object.
220 *
221 * \sa _mesa_clear_texture_image().
222 */
223 void
224 _mesa_clear_texture_object(struct gl_context *ctx,
225 struct gl_texture_object *texObj)
226 {
227 GLuint i, j;
228
229 if (texObj->Target == 0)
230 return;
231
232 for (i = 0; i < MAX_FACES; i++) {
233 for (j = 0; j < MAX_TEXTURE_LEVELS; j++) {
234 struct gl_texture_image *texImage = texObj->Image[i][j];
235 if (texImage)
236 _mesa_clear_texture_image(ctx, texImage);
237 }
238 }
239 }
240
241
242 /**
243 * Check if the given texture object is valid by examining its Target field.
244 * For debugging only.
245 */
246 static GLboolean
247 valid_texture_object(const struct gl_texture_object *tex)
248 {
249 switch (tex->Target) {
250 case 0:
251 case GL_TEXTURE_1D:
252 case GL_TEXTURE_2D:
253 case GL_TEXTURE_3D:
254 case GL_TEXTURE_CUBE_MAP_ARB:
255 case GL_TEXTURE_1D_ARRAY_EXT:
256 case GL_TEXTURE_2D_ARRAY_EXT:
257 case GL_TEXTURE_BUFFER:
258 return GL_TRUE;
259 case 0x99:
260 _mesa_problem(NULL, "invalid reference to a deleted texture object");
261 return GL_FALSE;
262 default:
263 _mesa_problem(NULL, "invalid texture object Target 0x%x, Id = %u",
264 tex->Target, tex->Name);
265 return GL_FALSE;
266 }
267 }
268
269
270 /**
271 * Reference (or unreference) a texture object.
272 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
273 * If 'tex' is non-null, increment its refcount.
274 * This is normally only called from the _mesa_reference_texobj() macro
275 * when there's a real pointer change.
276 */
277 void
278 _mesa_reference_texobj_(struct gl_texture_object **ptr,
279 struct gl_texture_object *tex)
280 {
281 assert(ptr);
282
283 if (*ptr) {
284 /* Unreference the old texture */
285 GLboolean deleteFlag = GL_FALSE;
286 struct gl_texture_object *oldTex = *ptr;
287
288 ASSERT(valid_texture_object(oldTex));
289 (void) valid_texture_object; /* silence warning in release builds */
290
291 _glthread_LOCK_MUTEX(oldTex->Mutex);
292 ASSERT(oldTex->RefCount > 0);
293 oldTex->RefCount--;
294
295 deleteFlag = (oldTex->RefCount == 0);
296 _glthread_UNLOCK_MUTEX(oldTex->Mutex);
297
298 if (deleteFlag) {
299 GET_CURRENT_CONTEXT(ctx);
300 if (ctx)
301 ctx->Driver.DeleteTexture(ctx, oldTex);
302 else
303 _mesa_problem(NULL, "Unable to delete texture, no context");
304 }
305
306 *ptr = NULL;
307 }
308 assert(!*ptr);
309
310 if (tex) {
311 /* reference new texture */
312 ASSERT(valid_texture_object(tex));
313 _glthread_LOCK_MUTEX(tex->Mutex);
314 if (tex->RefCount == 0) {
315 /* this texture's being deleted (look just above) */
316 /* Not sure this can every really happen. Warn if it does. */
317 _mesa_problem(NULL, "referencing deleted texture object");
318 *ptr = NULL;
319 }
320 else {
321 tex->RefCount++;
322 *ptr = tex;
323 }
324 _glthread_UNLOCK_MUTEX(tex->Mutex);
325 }
326 }
327
328
329
330 /**
331 * Mark a texture object as incomplete.
332 * \param t texture object
333 * \param fmt... string describing why it's incomplete (for debugging).
334 */
335 static void
336 incomplete(struct gl_texture_object *t, const char *fmt, ...)
337 {
338 #if 0
339 va_list args;
340 char s[100];
341
342 va_start(args, fmt);
343 vsnprintf(s, sizeof(s), fmt, args);
344 va_end(args);
345
346 printf("Texture Obj %d incomplete because: %s\n", t->Name, s);
347 #endif
348 t->_Complete = GL_FALSE;
349 }
350
351
352 /**
353 * Examine a texture object to determine if it is complete.
354 *
355 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
356 * accordingly.
357 *
358 * \param ctx GL context.
359 * \param t texture object.
360 *
361 * According to the texture target, verifies that each of the mipmaps is
362 * present and has the expected size.
363 */
364 void
365 _mesa_test_texobj_completeness( const struct gl_context *ctx,
366 struct gl_texture_object *t )
367 {
368 const GLint baseLevel = t->BaseLevel;
369 GLint maxLog2 = 0, maxLevels = 0;
370
371 t->_Complete = GL_TRUE; /* be optimistic */
372
373 /* Detect cases where the application set the base level to an invalid
374 * value.
375 */
376 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS)) {
377 incomplete(t, "base level = %d is invalid", baseLevel);
378 return;
379 }
380
381 /* Always need the base level image */
382 if (!t->Image[0][baseLevel]) {
383 incomplete(t, "Image[baseLevel=%d] == NULL", baseLevel);
384 return;
385 }
386
387 /* Check width/height/depth for zero */
388 if (t->Image[0][baseLevel]->Width == 0 ||
389 t->Image[0][baseLevel]->Height == 0 ||
390 t->Image[0][baseLevel]->Depth == 0) {
391 incomplete(t, "texture width = 0");
392 return;
393 }
394
395 /* Compute _MaxLevel */
396 if ((t->Target == GL_TEXTURE_1D) ||
397 (t->Target == GL_TEXTURE_1D_ARRAY_EXT)) {
398 maxLog2 = t->Image[0][baseLevel]->WidthLog2;
399 maxLevels = ctx->Const.MaxTextureLevels;
400 }
401 else if ((t->Target == GL_TEXTURE_2D) ||
402 (t->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
403 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
404 t->Image[0][baseLevel]->HeightLog2);
405 maxLevels = ctx->Const.MaxTextureLevels;
406 }
407 else if (t->Target == GL_TEXTURE_3D) {
408 GLint max = MAX2(t->Image[0][baseLevel]->WidthLog2,
409 t->Image[0][baseLevel]->HeightLog2);
410 maxLog2 = MAX2(max, (GLint)(t->Image[0][baseLevel]->DepthLog2));
411 maxLevels = ctx->Const.Max3DTextureLevels;
412 }
413 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
414 maxLog2 = MAX2(t->Image[0][baseLevel]->WidthLog2,
415 t->Image[0][baseLevel]->HeightLog2);
416 maxLevels = ctx->Const.MaxCubeTextureLevels;
417 }
418 else {
419 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
420 return;
421 }
422
423 ASSERT(maxLevels > 0);
424
425 if (t->MaxLevel < t->BaseLevel) {
426 incomplete(t, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
427 t->MaxLevel, t->BaseLevel);
428 return;
429 }
430
431 t->_MaxLevel = baseLevel + maxLog2;
432 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
433 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1);
434
435 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
436 t->_MaxLambda = (GLfloat) (t->_MaxLevel - t->BaseLevel);
437
438 if (t->Immutable) {
439 /* This texture object was created with glTexStorage1/2/3D() so we
440 * know that all the mipmap levels are the right size and all cube
441 * map faces are the same size.
442 * We don't need to do any of the additional checks below.
443 */
444 return;
445 }
446
447 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
448 /* make sure that all six cube map level 0 images are the same size */
449 const GLuint w = t->Image[0][baseLevel]->Width2;
450 const GLuint h = t->Image[0][baseLevel]->Height2;
451 GLuint face;
452 for (face = 1; face < 6; face++) {
453 if (t->Image[face][baseLevel] == NULL ||
454 t->Image[face][baseLevel]->Width2 != w ||
455 t->Image[face][baseLevel]->Height2 != h) {
456 incomplete(t, "Cube face missing or mismatched size");
457 return;
458 }
459 }
460 }
461
462 /* extra checking for mipmaps */
463 if (t->Sampler.MinFilter != GL_NEAREST && t->Sampler.MinFilter != GL_LINEAR) {
464 /*
465 * Mipmapping: determine if we have a complete set of mipmaps
466 */
467 GLint i;
468 GLint minLevel = baseLevel;
469 GLint maxLevel = t->_MaxLevel;
470
471 if (minLevel > maxLevel) {
472 incomplete(t, "minLevel > maxLevel");
473 return;
474 }
475
476 /* Test dimension-independent attributes */
477 for (i = minLevel; i <= maxLevel; i++) {
478 if (t->Image[0][i]) {
479 if (t->Image[0][i]->TexFormat != t->Image[0][baseLevel]->TexFormat) {
480 incomplete(t, "Format[i] != Format[baseLevel]");
481 return;
482 }
483 if (t->Image[0][i]->Border != t->Image[0][baseLevel]->Border) {
484 incomplete(t, "Border[i] != Border[baseLevel]");
485 return;
486 }
487 }
488 }
489
490 /* Test things which depend on number of texture image dimensions */
491 if ((t->Target == GL_TEXTURE_1D) ||
492 (t->Target == GL_TEXTURE_1D_ARRAY_EXT)) {
493 /* Test 1-D mipmaps */
494 GLuint width = t->Image[0][baseLevel]->Width2;
495 for (i = baseLevel + 1; i < maxLevels; i++) {
496 if (width > 1) {
497 width /= 2;
498 }
499 if (i >= minLevel && i <= maxLevel) {
500 const struct gl_texture_image *img = t->Image[0][i];
501 if (!img) {
502 incomplete(t, "1D Image[%d] is missing", i);
503 return;
504 }
505 if (img->Width2 != width ) {
506 incomplete(t, "1D Image[%d] bad width %u", i, img->Width2);
507 return;
508 }
509 }
510 if (width == 1) {
511 return; /* found smallest needed mipmap, all done! */
512 }
513 }
514 }
515 else if ((t->Target == GL_TEXTURE_2D) ||
516 (t->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
517 /* Test 2-D mipmaps */
518 GLuint width = t->Image[0][baseLevel]->Width2;
519 GLuint height = t->Image[0][baseLevel]->Height2;
520 for (i = baseLevel + 1; i < maxLevels; i++) {
521 if (width > 1) {
522 width /= 2;
523 }
524 if (height > 1) {
525 height /= 2;
526 }
527 if (i >= minLevel && i <= maxLevel) {
528 const struct gl_texture_image *img = t->Image[0][i];
529 if (!img) {
530 incomplete(t, "2D Image[%d of %d] is missing", i, maxLevel);
531 return;
532 }
533 if (img->Width2 != width) {
534 incomplete(t, "2D Image[%d] bad width %u", i, img->Width2);
535 return;
536 }
537 if (img->Height2 != height) {
538 incomplete(t, "2D Image[i] bad height %u", i, img->Height2);
539 return;
540 }
541 if (width==1 && height==1) {
542 return; /* found smallest needed mipmap, all done! */
543 }
544 }
545 }
546 }
547 else if (t->Target == GL_TEXTURE_3D) {
548 /* Test 3-D mipmaps */
549 GLuint width = t->Image[0][baseLevel]->Width2;
550 GLuint height = t->Image[0][baseLevel]->Height2;
551 GLuint depth = t->Image[0][baseLevel]->Depth2;
552 for (i = baseLevel + 1; i < maxLevels; i++) {
553 if (width > 1) {
554 width /= 2;
555 }
556 if (height > 1) {
557 height /= 2;
558 }
559 if (depth > 1) {
560 depth /= 2;
561 }
562 if (i >= minLevel && i <= maxLevel) {
563 const struct gl_texture_image *img = t->Image[0][i];
564 if (!img) {
565 incomplete(t, "3D Image[%d] is missing", i);
566 return;
567 }
568 if (img->_BaseFormat == GL_DEPTH_COMPONENT) {
569 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
570 return;
571 }
572 if (img->Width2 != width) {
573 incomplete(t, "3D Image[%d] bad width %u", i, img->Width2);
574 return;
575 }
576 if (img->Height2 != height) {
577 incomplete(t, "3D Image[%d] bad height %u", i, img->Height2);
578 return;
579 }
580 if (img->Depth2 != depth) {
581 incomplete(t, "3D Image[%d] bad depth %u", i, img->Depth2);
582 return;
583 }
584 }
585 if (width == 1 && height == 1 && depth == 1) {
586 return; /* found smallest needed mipmap, all done! */
587 }
588 }
589 }
590 else if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
591 /* make sure 6 cube faces are consistant */
592 GLuint width = t->Image[0][baseLevel]->Width2;
593 GLuint height = t->Image[0][baseLevel]->Height2;
594 for (i = baseLevel + 1; i < maxLevels; i++) {
595 if (width > 1) {
596 width /= 2;
597 }
598 if (height > 1) {
599 height /= 2;
600 }
601 if (i >= minLevel && i <= maxLevel) {
602 GLuint face;
603 for (face = 0; face < 6; face++) {
604 /* check that we have images defined */
605 if (!t->Image[face][i]) {
606 incomplete(t, "CubeMap Image[n][i] == NULL");
607 return;
608 }
609 /* Don't support GL_DEPTH_COMPONENT for cube maps */
610 if (ctx->VersionMajor < 3 && !ctx->Extensions.EXT_gpu_shader4) {
611 if (t->Image[face][i]->_BaseFormat == GL_DEPTH_COMPONENT) {
612 incomplete(t, "GL_DEPTH_COMPONENT only works with 1/2D tex");
613 return;
614 }
615 }
616 /* check that all six images have same size */
617 if (t->Image[face][i]->Width2 != width ||
618 t->Image[face][i]->Height2 != height) {
619 incomplete(t, "CubeMap Image[n][i] bad size");
620 return;
621 }
622 }
623 }
624 if (width == 1 && height == 1) {
625 return; /* found smallest needed mipmap, all done! */
626 }
627 }
628 }
629 else {
630 /* Target = ??? */
631 _mesa_problem(ctx, "Bug in gl_test_texture_object_completeness\n");
632 }
633 }
634 }
635
636
637 /**
638 * Check if the given cube map texture is "cube complete" as defined in
639 * the OpenGL specification.
640 */
641 GLboolean
642 _mesa_cube_complete(const struct gl_texture_object *texObj)
643 {
644 const GLint baseLevel = texObj->BaseLevel;
645 const struct gl_texture_image *img0, *img;
646 GLuint face;
647
648 if (texObj->Target != GL_TEXTURE_CUBE_MAP)
649 return GL_FALSE;
650
651 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS))
652 return GL_FALSE;
653
654 /* check first face */
655 img0 = texObj->Image[0][baseLevel];
656 if (!img0 ||
657 img0->Width < 1 ||
658 img0->Width != img0->Height)
659 return GL_FALSE;
660
661 /* check remaining faces vs. first face */
662 for (face = 1; face < 6; face++) {
663 img = texObj->Image[face][baseLevel];
664 if (!img ||
665 img->Width != img0->Width ||
666 img->Height != img0->Height ||
667 img->TexFormat != img0->TexFormat)
668 return GL_FALSE;
669 }
670
671 return GL_TRUE;
672 }
673
674
675 /**
676 * Mark a texture object dirty. It forces the object to be incomplete
677 * and optionally forces the context to re-validate its state.
678 *
679 * \param ctx GL context.
680 * \param texObj texture object.
681 * \param invalidate_state also invalidate context state.
682 */
683 void
684 _mesa_dirty_texobj(struct gl_context *ctx, struct gl_texture_object *texObj,
685 GLboolean invalidate_state)
686 {
687 texObj->_Complete = GL_FALSE;
688 if (invalidate_state)
689 ctx->NewState |= _NEW_TEXTURE;
690 }
691
692
693 /**
694 * Return pointer to a default/fallback texture.
695 * The texture is a 2D 8x8 RGBA texture with all texels = (0,0,0,1).
696 * That's the value a sampler should get when sampling from an
697 * incomplete texture.
698 */
699 struct gl_texture_object *
700 _mesa_get_fallback_texture(struct gl_context *ctx)
701 {
702 if (!ctx->Shared->FallbackTex) {
703 /* create fallback texture now */
704 static GLubyte texels[8 * 8][4];
705 struct gl_texture_object *texObj;
706 struct gl_texture_image *texImage;
707 gl_format texFormat;
708 GLuint i;
709
710 for (i = 0; i < 8 * 8; i++) {
711 texels[i][0] =
712 texels[i][1] =
713 texels[i][2] = 0x0;
714 texels[i][3] = 0xff;
715 }
716
717 /* create texture object */
718 texObj = ctx->Driver.NewTextureObject(ctx, 0, GL_TEXTURE_2D);
719 assert(texObj->RefCount == 1);
720 texObj->Sampler.MinFilter = GL_NEAREST;
721 texObj->Sampler.MagFilter = GL_NEAREST;
722
723 /* create level[0] texture image */
724 texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, 0);
725
726 texFormat = ctx->Driver.ChooseTextureFormat(ctx, GL_RGBA, GL_RGBA,
727 GL_UNSIGNED_BYTE);
728
729 /* init the image fields */
730 _mesa_init_teximage_fields(ctx, texImage,
731 8, 8, 1, 0, GL_RGBA, texFormat);
732
733 ASSERT(texImage->TexFormat != MESA_FORMAT_NONE);
734
735 /* set image data */
736 ctx->Driver.TexImage2D(ctx, texImage, GL_RGBA,
737 8, 8, 0,
738 GL_RGBA, GL_UNSIGNED_BYTE, texels,
739 &ctx->DefaultPacking);
740
741 _mesa_test_texobj_completeness(ctx, texObj);
742 assert(texObj->_Complete);
743
744 ctx->Shared->FallbackTex = texObj;
745 }
746 return ctx->Shared->FallbackTex;
747 }
748
749
750 /*@}*/
751
752
753 /***********************************************************************/
754 /** \name API functions */
755 /*@{*/
756
757
758 /**
759 * Generate texture names.
760 *
761 * \param n number of texture names to be generated.
762 * \param textures an array in which will hold the generated texture names.
763 *
764 * \sa glGenTextures().
765 *
766 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
767 * IDs which are stored in \p textures. Corresponding empty texture
768 * objects are also generated.
769 */
770 void GLAPIENTRY
771 _mesa_GenTextures( GLsizei n, GLuint *textures )
772 {
773 GET_CURRENT_CONTEXT(ctx);
774 GLuint first;
775 GLint i;
776 ASSERT_OUTSIDE_BEGIN_END(ctx);
777
778 if (n < 0) {
779 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
780 return;
781 }
782
783 if (!textures)
784 return;
785
786 /*
787 * This must be atomic (generation and allocation of texture IDs)
788 */
789 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
790
791 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
792
793 /* Allocate new, empty texture objects */
794 for (i = 0; i < n; i++) {
795 struct gl_texture_object *texObj;
796 GLuint name = first + i;
797 GLenum target = 0;
798 texObj = ctx->Driver.NewTextureObject(ctx, name, target);
799 if (!texObj) {
800 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
801 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
802 return;
803 }
804
805 /* insert into hash table */
806 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
807
808 textures[i] = name;
809 }
810
811 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
812 }
813
814
815 /**
816 * Check if the given texture object is bound to the current draw or
817 * read framebuffer. If so, Unbind it.
818 */
819 static void
820 unbind_texobj_from_fbo(struct gl_context *ctx,
821 struct gl_texture_object *texObj)
822 {
823 const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
824 GLuint i;
825
826 for (i = 0; i < n; i++) {
827 struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
828 if (fb->Name) {
829 GLuint j;
830 for (j = 0; j < BUFFER_COUNT; j++) {
831 if (fb->Attachment[j].Type == GL_TEXTURE &&
832 fb->Attachment[j].Texture == texObj) {
833 /* Vertices are already flushed by _mesa_DeleteTextures */
834 ctx->NewState |= _NEW_BUFFERS;
835 _mesa_remove_attachment(ctx, fb->Attachment + j);
836 }
837 }
838 }
839 }
840 }
841
842
843 /**
844 * Check if the given texture object is bound to any texture image units and
845 * unbind it if so (revert to default textures).
846 */
847 static void
848 unbind_texobj_from_texunits(struct gl_context *ctx,
849 struct gl_texture_object *texObj)
850 {
851 GLuint u, tex;
852
853 for (u = 0; u < Elements(ctx->Texture.Unit); u++) {
854 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
855 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
856 if (texObj == unit->CurrentTex[tex]) {
857 _mesa_reference_texobj(&unit->CurrentTex[tex],
858 ctx->Shared->DefaultTex[tex]);
859 ASSERT(unit->CurrentTex[tex]);
860 break;
861 }
862 }
863 }
864 }
865
866
867 /**
868 * Delete named textures.
869 *
870 * \param n number of textures to be deleted.
871 * \param textures array of texture IDs to be deleted.
872 *
873 * \sa glDeleteTextures().
874 *
875 * If we're about to delete a texture that's currently bound to any
876 * texture unit, unbind the texture first. Decrement the reference
877 * count on the texture object and delete it if it's zero.
878 * Recall that texture objects can be shared among several rendering
879 * contexts.
880 */
881 void GLAPIENTRY
882 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
883 {
884 GET_CURRENT_CONTEXT(ctx);
885 GLint i;
886 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* too complex */
887
888 if (!textures)
889 return;
890
891 for (i = 0; i < n; i++) {
892 if (textures[i] > 0) {
893 struct gl_texture_object *delObj
894 = _mesa_lookup_texture(ctx, textures[i]);
895
896 if (delObj) {
897 _mesa_lock_texture(ctx, delObj);
898
899 /* Check if texture is bound to any framebuffer objects.
900 * If so, unbind.
901 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
902 */
903 unbind_texobj_from_fbo(ctx, delObj);
904
905 /* Check if this texture is currently bound to any texture units.
906 * If so, unbind it.
907 */
908 unbind_texobj_from_texunits(ctx, delObj);
909
910 _mesa_unlock_texture(ctx, delObj);
911
912 ctx->NewState |= _NEW_TEXTURE;
913
914 /* The texture _name_ is now free for re-use.
915 * Remove it from the hash table now.
916 */
917 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
918 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
919 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
920
921 /* Unreference the texobj. If refcount hits zero, the texture
922 * will be deleted.
923 */
924 _mesa_reference_texobj(&delObj, NULL);
925 }
926 }
927 }
928 }
929
930
931 /**
932 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
933 * into the corresponding Mesa texture target index.
934 * Note that proxy targets are not valid here.
935 * \return TEXTURE_x_INDEX or -1 if target is invalid
936 */
937 static GLint
938 target_enum_to_index(GLenum target)
939 {
940 switch (target) {
941 case GL_TEXTURE_1D:
942 return TEXTURE_1D_INDEX;
943 case GL_TEXTURE_2D:
944 return TEXTURE_2D_INDEX;
945 case GL_TEXTURE_3D:
946 return TEXTURE_3D_INDEX;
947 case GL_TEXTURE_CUBE_MAP_ARB:
948 return TEXTURE_CUBE_INDEX;
949 case GL_TEXTURE_1D_ARRAY_EXT:
950 return TEXTURE_1D_ARRAY_INDEX;
951 case GL_TEXTURE_2D_ARRAY_EXT:
952 return TEXTURE_2D_ARRAY_INDEX;
953 case GL_TEXTURE_BUFFER_ARB:
954 return TEXTURE_BUFFER_INDEX;
955 default:
956 return -1;
957 }
958 }
959
960
961 /**
962 * Bind a named texture to a texturing target.
963 *
964 * \param target texture target.
965 * \param texName texture name.
966 *
967 * \sa glBindTexture().
968 *
969 * Determines the old texture object bound and returns immediately if rebinding
970 * the same texture. Get the current texture which is either a default texture
971 * if name is null, a named texture from the hash, or a new texture if the
972 * given texture name is new. Increments its reference count, binds it, and
973 * calls dd_function_table::BindTexture. Decrements the old texture reference
974 * count and deletes it if it reaches zero.
975 */
976 void GLAPIENTRY
977 _mesa_BindTexture( GLenum target, GLuint texName )
978 {
979 GET_CURRENT_CONTEXT(ctx);
980 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
981 struct gl_texture_object *newTexObj = NULL;
982 GLint targetIndex;
983 ASSERT_OUTSIDE_BEGIN_END(ctx);
984
985 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
986 _mesa_debug(ctx, "glBindTexture %s %d\n",
987 _mesa_lookup_enum_by_nr(target), (GLint) texName);
988
989 targetIndex = target_enum_to_index(target);
990 if (targetIndex < 0) {
991 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target)");
992 return;
993 }
994 assert(targetIndex < NUM_TEXTURE_TARGETS);
995
996 /*
997 * Get pointer to new texture object (newTexObj)
998 */
999 if (texName == 0) {
1000 /* Use a default texture object */
1001 newTexObj = ctx->Shared->DefaultTex[targetIndex];
1002 }
1003 else {
1004 /* non-default texture object */
1005 newTexObj = _mesa_lookup_texture(ctx, texName);
1006 if (newTexObj) {
1007 /* error checking */
1008 if (newTexObj->Target != 0 && newTexObj->Target != target) {
1009 /* the named texture object's target doesn't match the given target */
1010 _mesa_error( ctx, GL_INVALID_OPERATION,
1011 "glBindTexture(target mismatch)" );
1012 return;
1013 }
1014 }
1015 else {
1016 /* if this is a new texture id, allocate a texture object now */
1017 newTexObj = ctx->Driver.NewTextureObject(ctx, texName, target);
1018 if (!newTexObj) {
1019 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
1020 return;
1021 }
1022
1023 /* and insert it into hash table */
1024 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1025 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
1026 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1027 }
1028 newTexObj->Target = target;
1029 }
1030
1031 assert(valid_texture_object(newTexObj));
1032
1033 /* Check if this texture is only used by this context and is already bound.
1034 * If so, just return.
1035 */
1036 {
1037 GLboolean early_out;
1038 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1039 early_out = ((ctx->Shared->RefCount == 1)
1040 && (newTexObj == texUnit->CurrentTex[targetIndex]));
1041 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1042 if (early_out) {
1043 return;
1044 }
1045 }
1046
1047 /* flush before changing binding */
1048 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1049
1050 /* Do the actual binding. The refcount on the previously bound
1051 * texture object will be decremented. It'll be deleted if the
1052 * count hits zero.
1053 */
1054 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], newTexObj);
1055 ASSERT(texUnit->CurrentTex[targetIndex]);
1056
1057 /* Pass BindTexture call to device driver */
1058 if (ctx->Driver.BindTexture)
1059 ctx->Driver.BindTexture(ctx, target, newTexObj);
1060 }
1061
1062
1063 /**
1064 * Set texture priorities.
1065 *
1066 * \param n number of textures.
1067 * \param texName texture names.
1068 * \param priorities corresponding texture priorities.
1069 *
1070 * \sa glPrioritizeTextures().
1071 *
1072 * Looks up each texture in the hash, clamps the corresponding priority between
1073 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1074 */
1075 void GLAPIENTRY
1076 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1077 const GLclampf *priorities )
1078 {
1079 GET_CURRENT_CONTEXT(ctx);
1080 GLint i;
1081 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1082
1083 if (n < 0) {
1084 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1085 return;
1086 }
1087
1088 if (!priorities)
1089 return;
1090
1091 for (i = 0; i < n; i++) {
1092 if (texName[i] > 0) {
1093 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1094 if (t) {
1095 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1096 }
1097 }
1098 }
1099
1100 ctx->NewState |= _NEW_TEXTURE;
1101 }
1102
1103
1104
1105 /**
1106 * See if textures are loaded in texture memory.
1107 *
1108 * \param n number of textures to query.
1109 * \param texName array with the texture names.
1110 * \param residences array which will hold the residence status.
1111 *
1112 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1113 *
1114 * Note: we assume all textures are always resident
1115 */
1116 GLboolean GLAPIENTRY
1117 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
1118 GLboolean *residences)
1119 {
1120 GET_CURRENT_CONTEXT(ctx);
1121 GLboolean allResident = GL_TRUE;
1122 GLint i;
1123 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1124
1125 if (n < 0) {
1126 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1127 return GL_FALSE;
1128 }
1129
1130 if (!texName || !residences)
1131 return GL_FALSE;
1132
1133 /* We only do error checking on the texture names */
1134 for (i = 0; i < n; i++) {
1135 struct gl_texture_object *t;
1136 if (texName[i] == 0) {
1137 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1138 return GL_FALSE;
1139 }
1140 t = _mesa_lookup_texture(ctx, texName[i]);
1141 if (!t) {
1142 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1143 return GL_FALSE;
1144 }
1145 }
1146
1147 return allResident;
1148 }
1149
1150
1151 /**
1152 * See if a name corresponds to a texture.
1153 *
1154 * \param texture texture name.
1155 *
1156 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1157 * otherwise.
1158 *
1159 * \sa glIsTexture().
1160 *
1161 * Calls _mesa_HashLookup().
1162 */
1163 GLboolean GLAPIENTRY
1164 _mesa_IsTexture( GLuint texture )
1165 {
1166 struct gl_texture_object *t;
1167 GET_CURRENT_CONTEXT(ctx);
1168 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1169
1170 if (!texture)
1171 return GL_FALSE;
1172
1173 t = _mesa_lookup_texture(ctx, texture);
1174
1175 /* IsTexture is true only after object has been bound once. */
1176 return t && t->Target;
1177 }
1178
1179
1180 /**
1181 * Simplest implementation of texture locking: grab the shared tex
1182 * mutex. Examine the shared context state timestamp and if there has
1183 * been a change, set the appropriate bits in ctx->NewState.
1184 *
1185 * This is used to deal with synchronizing things when a texture object
1186 * is used/modified by different contexts (or threads) which are sharing
1187 * the texture.
1188 *
1189 * See also _mesa_lock/unlock_texture() in teximage.h
1190 */
1191 void
1192 _mesa_lock_context_textures( struct gl_context *ctx )
1193 {
1194 _glthread_LOCK_MUTEX(ctx->Shared->TexMutex);
1195
1196 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
1197 ctx->NewState |= _NEW_TEXTURE;
1198 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
1199 }
1200 }
1201
1202
1203 void
1204 _mesa_unlock_context_textures( struct gl_context *ctx )
1205 {
1206 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
1207 _glthread_UNLOCK_MUTEX(ctx->Shared->TexMutex);
1208 }
1209
1210 /*@}*/