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