r600g: pad the DMA CS to a multiple of 8 dwords
[mesa.git] / 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_CUBE_MAP_ARRAY ||
112 target == GL_TEXTURE_BUFFER);
113
114 memset(obj, 0, sizeof(*obj));
115 /* init the non-zero fields */
116 _glthread_INIT_MUTEX(obj->Mutex);
117 obj->RefCount = 1;
118 obj->Name = name;
119 obj->Target = target;
120 obj->Priority = 1.0F;
121 obj->BaseLevel = 0;
122 obj->MaxLevel = 1000;
123
124 /* must be one; no support for (YUV) planes in separate buffers */
125 obj->RequiredTextureImageUnits = 1;
126
127 /* sampler state */
128 if (target == GL_TEXTURE_RECTANGLE_NV ||
129 target == GL_TEXTURE_EXTERNAL_OES) {
130 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
131 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
132 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
133 obj->Sampler.MinFilter = GL_LINEAR;
134 }
135 else {
136 obj->Sampler.WrapS = GL_REPEAT;
137 obj->Sampler.WrapT = GL_REPEAT;
138 obj->Sampler.WrapR = GL_REPEAT;
139 obj->Sampler.MinFilter = GL_NEAREST_MIPMAP_LINEAR;
140 }
141 obj->Sampler.MagFilter = GL_LINEAR;
142 obj->Sampler.MinLod = -1000.0;
143 obj->Sampler.MaxLod = 1000.0;
144 obj->Sampler.LodBias = 0.0;
145 obj->Sampler.MaxAnisotropy = 1.0;
146 obj->Sampler.CompareMode = GL_NONE; /* ARB_shadow */
147 obj->Sampler.CompareFunc = GL_LEQUAL; /* ARB_shadow */
148 obj->DepthMode = GL_LUMINANCE;
149 obj->Sampler.CubeMapSeamless = GL_FALSE;
150 obj->Swizzle[0] = GL_RED;
151 obj->Swizzle[1] = GL_GREEN;
152 obj->Swizzle[2] = GL_BLUE;
153 obj->Swizzle[3] = GL_ALPHA;
154 obj->_Swizzle = SWIZZLE_NOOP;
155 obj->Sampler.sRGBDecode = GL_DECODE_EXT;
156 obj->BufferObjectFormat = GL_LUMINANCE8;
157 obj->_BufferObjectFormat = MESA_FORMAT_L8;
158 }
159
160
161 /**
162 * Some texture initialization can't be finished until we know which
163 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
164 */
165 static void
166 finish_texture_init(struct gl_context *ctx, GLenum target,
167 struct gl_texture_object *obj)
168 {
169 assert(obj->Target == 0);
170
171 if (target == GL_TEXTURE_RECTANGLE_NV ||
172 target == GL_TEXTURE_EXTERNAL_OES) {
173 /* have to init wrap and filter state here - kind of klunky */
174 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
175 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
176 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
177 obj->Sampler.MinFilter = GL_LINEAR;
178 if (ctx->Driver.TexParameter) {
179 static const GLfloat fparam_wrap[1] = {(GLfloat) GL_CLAMP_TO_EDGE};
180 static const GLfloat fparam_filter[1] = {(GLfloat) GL_LINEAR};
181 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_S, fparam_wrap);
182 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_T, fparam_wrap);
183 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_WRAP_R, fparam_wrap);
184 ctx->Driver.TexParameter(ctx, target, obj, GL_TEXTURE_MIN_FILTER, fparam_filter);
185 }
186 }
187 }
188
189
190 /**
191 * Deallocate a texture object struct. It should have already been
192 * removed from the texture object pool.
193 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
194 *
195 * \param shared the shared GL state to which the object belongs.
196 * \param texObj the texture object to delete.
197 */
198 void
199 _mesa_delete_texture_object(struct gl_context *ctx,
200 struct gl_texture_object *texObj)
201 {
202 GLuint i, face;
203
204 /* Set Target to an invalid value. With some assertions elsewhere
205 * we can try to detect possible use of deleted textures.
206 */
207 texObj->Target = 0x99;
208
209 /* free the texture images */
210 for (face = 0; face < 6; face++) {
211 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
212 if (texObj->Image[face][i]) {
213 ctx->Driver.DeleteTextureImage(ctx, texObj->Image[face][i]);
214 }
215 }
216 }
217
218 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, NULL);
219
220 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
221 _glthread_DESTROY_MUTEX(texObj->Mutex);
222
223 /* free this object */
224 free(texObj);
225 }
226
227
228
229 /**
230 * Copy texture object state from one texture object to another.
231 * Use for glPush/PopAttrib.
232 *
233 * \param dest destination texture object.
234 * \param src source texture object.
235 */
236 void
237 _mesa_copy_texture_object( struct gl_texture_object *dest,
238 const struct gl_texture_object *src )
239 {
240 dest->Target = src->Target;
241 dest->Name = src->Name;
242 dest->Priority = src->Priority;
243 dest->Sampler.BorderColor.f[0] = src->Sampler.BorderColor.f[0];
244 dest->Sampler.BorderColor.f[1] = src->Sampler.BorderColor.f[1];
245 dest->Sampler.BorderColor.f[2] = src->Sampler.BorderColor.f[2];
246 dest->Sampler.BorderColor.f[3] = src->Sampler.BorderColor.f[3];
247 dest->Sampler.WrapS = src->Sampler.WrapS;
248 dest->Sampler.WrapT = src->Sampler.WrapT;
249 dest->Sampler.WrapR = src->Sampler.WrapR;
250 dest->Sampler.MinFilter = src->Sampler.MinFilter;
251 dest->Sampler.MagFilter = src->Sampler.MagFilter;
252 dest->Sampler.MinLod = src->Sampler.MinLod;
253 dest->Sampler.MaxLod = src->Sampler.MaxLod;
254 dest->Sampler.LodBias = src->Sampler.LodBias;
255 dest->BaseLevel = src->BaseLevel;
256 dest->MaxLevel = src->MaxLevel;
257 dest->Sampler.MaxAnisotropy = src->Sampler.MaxAnisotropy;
258 dest->Sampler.CompareMode = src->Sampler.CompareMode;
259 dest->Sampler.CompareFunc = src->Sampler.CompareFunc;
260 dest->Sampler.CubeMapSeamless = src->Sampler.CubeMapSeamless;
261 dest->DepthMode = src->DepthMode;
262 dest->Sampler.sRGBDecode = src->Sampler.sRGBDecode;
263 dest->_MaxLevel = src->_MaxLevel;
264 dest->_MaxLambda = src->_MaxLambda;
265 dest->GenerateMipmap = src->GenerateMipmap;
266 dest->_BaseComplete = src->_BaseComplete;
267 dest->_MipmapComplete = src->_MipmapComplete;
268 COPY_4V(dest->Swizzle, src->Swizzle);
269 dest->_Swizzle = src->_Swizzle;
270
271 dest->RequiredTextureImageUnits = src->RequiredTextureImageUnits;
272 }
273
274
275 /**
276 * Free all texture images of the given texture object.
277 *
278 * \param ctx GL context.
279 * \param t texture object.
280 *
281 * \sa _mesa_clear_texture_image().
282 */
283 void
284 _mesa_clear_texture_object(struct gl_context *ctx,
285 struct gl_texture_object *texObj)
286 {
287 GLuint i, j;
288
289 if (texObj->Target == 0)
290 return;
291
292 for (i = 0; i < MAX_FACES; i++) {
293 for (j = 0; j < MAX_TEXTURE_LEVELS; j++) {
294 struct gl_texture_image *texImage = texObj->Image[i][j];
295 if (texImage)
296 _mesa_clear_texture_image(ctx, texImage);
297 }
298 }
299 }
300
301
302 /**
303 * Check if the given texture object is valid by examining its Target field.
304 * For debugging only.
305 */
306 static GLboolean
307 valid_texture_object(const struct gl_texture_object *tex)
308 {
309 switch (tex->Target) {
310 case 0:
311 case GL_TEXTURE_1D:
312 case GL_TEXTURE_2D:
313 case GL_TEXTURE_3D:
314 case GL_TEXTURE_CUBE_MAP_ARB:
315 case GL_TEXTURE_RECTANGLE_NV:
316 case GL_TEXTURE_1D_ARRAY_EXT:
317 case GL_TEXTURE_2D_ARRAY_EXT:
318 case GL_TEXTURE_BUFFER:
319 case GL_TEXTURE_EXTERNAL_OES:
320 case GL_TEXTURE_CUBE_MAP_ARRAY:
321 return GL_TRUE;
322 case 0x99:
323 _mesa_problem(NULL, "invalid reference to a deleted texture object");
324 return GL_FALSE;
325 default:
326 _mesa_problem(NULL, "invalid texture object Target 0x%x, Id = %u",
327 tex->Target, tex->Name);
328 return GL_FALSE;
329 }
330 }
331
332
333 /**
334 * Reference (or unreference) a texture object.
335 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
336 * If 'tex' is non-null, increment its refcount.
337 * This is normally only called from the _mesa_reference_texobj() macro
338 * when there's a real pointer change.
339 */
340 void
341 _mesa_reference_texobj_(struct gl_texture_object **ptr,
342 struct gl_texture_object *tex)
343 {
344 assert(ptr);
345
346 if (*ptr) {
347 /* Unreference the old texture */
348 GLboolean deleteFlag = GL_FALSE;
349 struct gl_texture_object *oldTex = *ptr;
350
351 ASSERT(valid_texture_object(oldTex));
352 (void) valid_texture_object; /* silence warning in release builds */
353
354 _glthread_LOCK_MUTEX(oldTex->Mutex);
355 ASSERT(oldTex->RefCount > 0);
356 oldTex->RefCount--;
357
358 deleteFlag = (oldTex->RefCount == 0);
359 _glthread_UNLOCK_MUTEX(oldTex->Mutex);
360
361 if (deleteFlag) {
362 GET_CURRENT_CONTEXT(ctx);
363 if (ctx)
364 ctx->Driver.DeleteTexture(ctx, oldTex);
365 else
366 _mesa_problem(NULL, "Unable to delete texture, no context");
367 }
368
369 *ptr = NULL;
370 }
371 assert(!*ptr);
372
373 if (tex) {
374 /* reference new texture */
375 ASSERT(valid_texture_object(tex));
376 _glthread_LOCK_MUTEX(tex->Mutex);
377 if (tex->RefCount == 0) {
378 /* this texture's being deleted (look just above) */
379 /* Not sure this can every really happen. Warn if it does. */
380 _mesa_problem(NULL, "referencing deleted texture object");
381 *ptr = NULL;
382 }
383 else {
384 tex->RefCount++;
385 *ptr = tex;
386 }
387 _glthread_UNLOCK_MUTEX(tex->Mutex);
388 }
389 }
390
391
392 enum base_mipmap { BASE, MIPMAP };
393
394
395 /**
396 * Mark a texture object as incomplete. There are actually three kinds of
397 * (in)completeness:
398 * 1. "base incomplete": the base level of the texture is invalid so no
399 * texturing is possible.
400 * 2. "mipmap incomplete": a non-base level of the texture is invalid so
401 * mipmap filtering isn't possible, but non-mipmap filtering is.
402 * 3. "texture incompleteness": some combination of texture state and
403 * sampler state renders the texture incomplete.
404 *
405 * \param t texture object
406 * \param bm either BASE or MIPMAP to indicate what's incomplete
407 * \param fmt... string describing why it's incomplete (for debugging).
408 */
409 static void
410 incomplete(struct gl_texture_object *t, enum base_mipmap bm,
411 const char *fmt, ...)
412 {
413 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_TEXTURE) {
414 va_list args;
415 char s[100];
416
417 va_start(args, fmt);
418 vsnprintf(s, sizeof(s), fmt, args);
419 va_end(args);
420
421 _mesa_debug(NULL, "Texture Obj %d incomplete because: %s\n", t->Name, s);
422 }
423
424 if (bm == BASE)
425 t->_BaseComplete = GL_FALSE;
426 t->_MipmapComplete = GL_FALSE;
427 }
428
429
430 /**
431 * Examine a texture object to determine if it is complete.
432 *
433 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
434 * accordingly.
435 *
436 * \param ctx GL context.
437 * \param t texture object.
438 *
439 * According to the texture target, verifies that each of the mipmaps is
440 * present and has the expected size.
441 */
442 void
443 _mesa_test_texobj_completeness( const struct gl_context *ctx,
444 struct gl_texture_object *t )
445 {
446 const GLint baseLevel = t->BaseLevel;
447 const struct gl_texture_image *baseImage;
448 GLint maxLevels = 0;
449
450 /* We'll set these to FALSE if tests fail below */
451 t->_BaseComplete = GL_TRUE;
452 t->_MipmapComplete = GL_TRUE;
453
454 if (t->Target == GL_TEXTURE_BUFFER) {
455 /* Buffer textures are always considered complete. The obvious case where
456 * they would be incomplete (no BO attached) is actually specced to be
457 * undefined rendering results.
458 */
459 return;
460 }
461
462 /* Detect cases where the application set the base level to an invalid
463 * value.
464 */
465 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS)) {
466 incomplete(t, BASE, "base level = %d is invalid", baseLevel);
467 return;
468 }
469
470 if (t->MaxLevel < baseLevel) {
471 incomplete(t, MIPMAP, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
472 t->MaxLevel, baseLevel);
473 return;
474 }
475
476 baseImage = t->Image[0][baseLevel];
477
478 /* Always need the base level image */
479 if (!baseImage) {
480 incomplete(t, BASE, "Image[baseLevel=%d] == NULL", baseLevel);
481 return;
482 }
483
484 /* Check width/height/depth for zero */
485 if (baseImage->Width == 0 ||
486 baseImage->Height == 0 ||
487 baseImage->Depth == 0) {
488 incomplete(t, BASE, "texture width or height or depth = 0");
489 return;
490 }
491
492 /* Check if the texture values are integer */
493 {
494 GLenum datatype = _mesa_get_format_datatype(baseImage->TexFormat);
495 t->_IsIntegerFormat = datatype == GL_INT || datatype == GL_UNSIGNED_INT;
496 }
497
498 /* Compute _MaxLevel (the maximum mipmap level we'll sample from given the
499 * mipmap image sizes and GL_TEXTURE_MAX_LEVEL state).
500 */
501 switch (t->Target) {
502 case GL_TEXTURE_1D:
503 case GL_TEXTURE_1D_ARRAY_EXT:
504 maxLevels = ctx->Const.MaxTextureLevels;
505 break;
506 case GL_TEXTURE_2D:
507 case GL_TEXTURE_2D_ARRAY_EXT:
508 maxLevels = ctx->Const.MaxTextureLevels;
509 break;
510 case GL_TEXTURE_3D:
511 maxLevels = ctx->Const.Max3DTextureLevels;
512 break;
513 case GL_TEXTURE_CUBE_MAP_ARB:
514 case GL_TEXTURE_CUBE_MAP_ARRAY:
515 maxLevels = ctx->Const.MaxCubeTextureLevels;
516 break;
517 case GL_TEXTURE_RECTANGLE_NV:
518 case GL_TEXTURE_BUFFER:
519 case GL_TEXTURE_EXTERNAL_OES:
520 maxLevels = 1; /* no mipmapping */
521 break;
522 default:
523 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
524 return;
525 }
526
527 ASSERT(maxLevels > 0);
528
529 t->_MaxLevel =
530 baseLevel + baseImage->MaxNumLevels - 1; /* 'p' in the GL spec */
531 t->_MaxLevel = MIN2(t->_MaxLevel, t->MaxLevel);
532 t->_MaxLevel = MIN2(t->_MaxLevel, maxLevels - 1); /* 'q' in the GL spec */
533
534 /* Compute _MaxLambda = q - b (see the 1.2 spec) used during mipmapping */
535 t->_MaxLambda = (GLfloat) (t->_MaxLevel - baseLevel);
536
537 if (t->Immutable) {
538 /* This texture object was created with glTexStorage1/2/3D() so we
539 * know that all the mipmap levels are the right size and all cube
540 * map faces are the same size.
541 * We don't need to do any of the additional checks below.
542 */
543 return;
544 }
545
546 if (t->Target == GL_TEXTURE_CUBE_MAP_ARB) {
547 /* Make sure that all six cube map level 0 images are the same size.
548 * Note: we know that the image's width==height (we enforce that
549 * at glTexImage time) so we only need to test the width here.
550 */
551 GLuint face;
552 assert(baseImage->Width2 == baseImage->Height);
553 for (face = 1; face < 6; face++) {
554 assert(t->Image[face][baseLevel] == NULL ||
555 t->Image[face][baseLevel]->Width2 ==
556 t->Image[face][baseLevel]->Height2);
557 if (t->Image[face][baseLevel] == NULL ||
558 t->Image[face][baseLevel]->Width2 != baseImage->Width2) {
559 incomplete(t, BASE, "Cube face missing or mismatched size");
560 return;
561 }
562 }
563 }
564
565 /*
566 * Do mipmap consistency checking.
567 * Note: we don't care about the current texture sampler state here.
568 * To determine texture completeness we'll either look at _BaseComplete
569 * or _MipmapComplete depending on the current minification filter mode.
570 */
571 {
572 GLint i;
573 const GLint minLevel = baseLevel;
574 const GLint maxLevel = t->_MaxLevel;
575 const GLuint numFaces = _mesa_num_tex_faces(t->Target);
576 GLuint width, height, depth, face;
577
578 if (minLevel > maxLevel) {
579 incomplete(t, MIPMAP, "minLevel > maxLevel");
580 return;
581 }
582
583 /* Get the base image's dimensions */
584 width = baseImage->Width2;
585 height = baseImage->Height2;
586 depth = baseImage->Depth2;
587
588 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL textures */
589 for (i = baseLevel + 1; i < maxLevels; i++) {
590 /* Compute the expected size of image at level[i] */
591 if (width > 1) {
592 width /= 2;
593 }
594 if (height > 1 && t->Target != GL_TEXTURE_1D_ARRAY) {
595 height /= 2;
596 }
597 if (depth > 1 && t->Target != GL_TEXTURE_2D_ARRAY && t->Target != GL_TEXTURE_CUBE_MAP_ARRAY) {
598 depth /= 2;
599 }
600
601 /* loop over cube faces (or single face otherwise) */
602 for (face = 0; face < numFaces; face++) {
603 if (i >= minLevel && i <= maxLevel) {
604 const struct gl_texture_image *img = t->Image[face][i];
605
606 if (!img) {
607 incomplete(t, MIPMAP, "TexImage[%d] is missing", i);
608 return;
609 }
610 if (img->TexFormat != baseImage->TexFormat) {
611 incomplete(t, MIPMAP, "Format[i] != Format[baseLevel]");
612 return;
613 }
614 if (img->Border != baseImage->Border) {
615 incomplete(t, MIPMAP, "Border[i] != Border[baseLevel]");
616 return;
617 }
618 if (img->Width2 != width) {
619 incomplete(t, MIPMAP, "TexImage[%d] bad width %u", i, img->Width2);
620 return;
621 }
622 if (img->Height2 != height) {
623 incomplete(t, MIPMAP, "TexImage[%d] bad height %u", i, img->Height2);
624 return;
625 }
626 if (img->Depth2 != depth) {
627 incomplete(t, MIPMAP, "TexImage[%d] bad depth %u", i, img->Depth2);
628 return;
629 }
630
631 /* Extra checks for cube textures */
632 if (face > 0) {
633 /* check that cube faces are the same size */
634 if (img->Width2 != t->Image[0][i]->Width2 ||
635 img->Height2 != t->Image[0][i]->Height2) {
636 incomplete(t, MIPMAP, "CubeMap Image[n][i] bad size");
637 return;
638 }
639 }
640 }
641 }
642
643 if (width == 1 && height == 1 && depth == 1) {
644 return; /* found smallest needed mipmap, all done! */
645 }
646 }
647 }
648 }
649
650
651 /**
652 * Check if the given cube map texture is "cube complete" as defined in
653 * the OpenGL specification.
654 */
655 GLboolean
656 _mesa_cube_complete(const struct gl_texture_object *texObj)
657 {
658 const GLint baseLevel = texObj->BaseLevel;
659 const struct gl_texture_image *img0, *img;
660 GLuint face;
661
662 if (texObj->Target != GL_TEXTURE_CUBE_MAP)
663 return GL_FALSE;
664
665 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS))
666 return GL_FALSE;
667
668 /* check first face */
669 img0 = texObj->Image[0][baseLevel];
670 if (!img0 ||
671 img0->Width < 1 ||
672 img0->Width != img0->Height)
673 return GL_FALSE;
674
675 /* check remaining faces vs. first face */
676 for (face = 1; face < 6; face++) {
677 img = texObj->Image[face][baseLevel];
678 if (!img ||
679 img->Width != img0->Width ||
680 img->Height != img0->Height ||
681 img->TexFormat != img0->TexFormat)
682 return GL_FALSE;
683 }
684
685 return GL_TRUE;
686 }
687
688
689 /**
690 * Mark a texture object dirty. It forces the object to be incomplete
691 * and optionally forces the context to re-validate its state.
692 *
693 * \param ctx GL context.
694 * \param texObj texture object.
695 * \param invalidate_state also invalidate context state.
696 */
697 void
698 _mesa_dirty_texobj(struct gl_context *ctx, struct gl_texture_object *texObj,
699 GLboolean invalidate_state)
700 {
701 texObj->_BaseComplete = GL_FALSE;
702 texObj->_MipmapComplete = GL_FALSE;
703 if (invalidate_state)
704 ctx->NewState |= _NEW_TEXTURE;
705 }
706
707
708 /**
709 * Return pointer to a default/fallback texture of the given type/target.
710 * The texture is an RGBA texture with all texels = (0,0,0,1).
711 * That's the value a GLSL sampler should get when sampling from an
712 * incomplete texture.
713 */
714 struct gl_texture_object *
715 _mesa_get_fallback_texture(struct gl_context *ctx, gl_texture_index tex)
716 {
717 if (!ctx->Shared->FallbackTex[tex]) {
718 /* create fallback texture now */
719 const GLsizei width = 1, height = 1, depth = 1;
720 GLubyte texel[4];
721 struct gl_texture_object *texObj;
722 struct gl_texture_image *texImage;
723 gl_format texFormat;
724 GLuint dims, face, numFaces = 1;
725 GLenum target;
726
727 texel[0] =
728 texel[1] =
729 texel[2] = 0x0;
730 texel[3] = 0xff;
731
732 switch (tex) {
733 case TEXTURE_2D_ARRAY_INDEX:
734 dims = 3;
735 target = GL_TEXTURE_2D_ARRAY;
736 break;
737 case TEXTURE_1D_ARRAY_INDEX:
738 dims = 2;
739 target = GL_TEXTURE_1D_ARRAY;
740 break;
741 case TEXTURE_CUBE_INDEX:
742 dims = 2;
743 target = GL_TEXTURE_CUBE_MAP;
744 numFaces = 6;
745 break;
746 case TEXTURE_3D_INDEX:
747 dims = 3;
748 target = GL_TEXTURE_3D;
749 break;
750 case TEXTURE_RECT_INDEX:
751 dims = 2;
752 target = GL_TEXTURE_RECTANGLE;
753 break;
754 case TEXTURE_2D_INDEX:
755 dims = 2;
756 target = GL_TEXTURE_2D;
757 break;
758 case TEXTURE_1D_INDEX:
759 dims = 1;
760 target = GL_TEXTURE_1D;
761 break;
762 case TEXTURE_BUFFER_INDEX:
763 dims = 0;
764 target = GL_TEXTURE_BUFFER;
765 break;
766 case TEXTURE_CUBE_ARRAY_INDEX:
767 dims = 3;
768 target = GL_TEXTURE_CUBE_MAP_ARRAY;
769 break;
770 case TEXTURE_EXTERNAL_INDEX:
771 dims = 2;
772 target = GL_TEXTURE_EXTERNAL_OES;
773 break;
774 default:
775 /* no-op */
776 return NULL;
777 }
778
779 /* create texture object */
780 texObj = ctx->Driver.NewTextureObject(ctx, 0, target);
781 if (!texObj)
782 return NULL;
783
784 assert(texObj->RefCount == 1);
785 texObj->Sampler.MinFilter = GL_NEAREST;
786 texObj->Sampler.MagFilter = GL_NEAREST;
787
788 texFormat = ctx->Driver.ChooseTextureFormat(ctx, target,
789 GL_RGBA, GL_RGBA,
790 GL_UNSIGNED_BYTE);
791
792 /* need a loop here just for cube maps */
793 for (face = 0; face < numFaces; face++) {
794 GLenum faceTarget;
795
796 if (target == GL_TEXTURE_CUBE_MAP)
797 faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
798 else
799 faceTarget = target;
800
801 /* initialize level[0] texture image */
802 texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, 0);
803
804 _mesa_init_teximage_fields(ctx, texImage,
805 width,
806 (dims > 1) ? height : 1,
807 (dims > 2) ? depth : 1,
808 0, /* border */
809 GL_RGBA, texFormat);
810
811 ctx->Driver.TexImage(ctx, dims, texImage,
812 GL_RGBA, GL_UNSIGNED_BYTE, texel,
813 &ctx->DefaultPacking);
814 }
815
816 _mesa_test_texobj_completeness(ctx, texObj);
817 assert(texObj->_BaseComplete);
818 assert(texObj->_MipmapComplete);
819
820 ctx->Shared->FallbackTex[tex] = texObj;
821 }
822 return ctx->Shared->FallbackTex[tex];
823 }
824
825
826 /**
827 * Compute the size of the given texture object, in bytes.
828 */
829 static GLuint
830 texture_size(const struct gl_texture_object *texObj)
831 {
832 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
833 GLuint face, level, size = 0;
834
835 for (face = 0; face < numFaces; face++) {
836 for (level = 0; level < MAX_TEXTURE_LEVELS; level++) {
837 const struct gl_texture_image *img = texObj->Image[face][level];
838 if (img) {
839 GLuint sz = _mesa_format_image_size(img->TexFormat, img->Width,
840 img->Height, img->Depth);
841 size += sz;
842 }
843 }
844 }
845
846 return size;
847 }
848
849
850 /**
851 * Callback called from _mesa_HashWalk()
852 */
853 static void
854 count_tex_size(GLuint key, void *data, void *userData)
855 {
856 const struct gl_texture_object *texObj =
857 (const struct gl_texture_object *) data;
858 GLuint *total = (GLuint *) userData;
859
860 *total = *total + texture_size(texObj);
861 }
862
863
864 /**
865 * Compute total size (in bytes) of all textures for the given context.
866 * For debugging purposes.
867 */
868 GLuint
869 _mesa_total_texture_memory(struct gl_context *ctx)
870 {
871 GLuint tgt, total = 0;
872
873 _mesa_HashWalk(ctx->Shared->TexObjects, count_tex_size, &total);
874
875 /* plus, the default texture objects */
876 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
877 total += texture_size(ctx->Shared->DefaultTex[tgt]);
878 }
879
880 return total;
881 }
882
883 static struct gl_texture_object *
884 invalidate_tex_image_error_check(struct gl_context *ctx, GLuint texture,
885 GLint level, const char *name)
886 {
887 /* The GL_ARB_invalidate_subdata spec says:
888 *
889 * "If <texture> is zero or is not the name of a texture, the error
890 * INVALID_VALUE is generated."
891 *
892 * This performs the error check in a different order than listed in the
893 * spec. We have to get the texture object before we can validate the
894 * other parameters against values in the texture object.
895 */
896 struct gl_texture_object *const t = _mesa_lookup_texture(ctx, texture);
897 if (texture == 0 || t == NULL) {
898 _mesa_error(ctx, GL_INVALID_VALUE, "%s(texture)", name);
899 return NULL;
900 }
901
902 /* The GL_ARB_invalidate_subdata spec says:
903 *
904 * "If <level> is less than zero or greater than the base 2 logarithm
905 * of the maximum texture width, height, or depth, the error
906 * INVALID_VALUE is generated."
907 */
908 if (level < 0 || level > t->MaxLevel) {
909 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name);
910 return NULL;
911 }
912
913 /* The GL_ARB_invalidate_subdata spec says:
914 *
915 * "If the target of <texture> is TEXTURE_RECTANGLE, TEXTURE_BUFFER,
916 * TEXTURE_2D_MULTISAMPLE, or TEXTURE_2D_MULTISAMPLE_ARRAY, and <level>
917 * is not zero, the error INVALID_VALUE is generated."
918 */
919 if (level != 0) {
920 switch (t->Target) {
921 case GL_TEXTURE_RECTANGLE:
922 case GL_TEXTURE_BUFFER:
923 case GL_TEXTURE_2D_MULTISAMPLE:
924 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
925 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name);
926 return NULL;
927
928 default:
929 break;
930 }
931 }
932
933 return t;
934 }
935
936 /*@}*/
937
938
939 /***********************************************************************/
940 /** \name API functions */
941 /*@{*/
942
943
944 /**
945 * Generate texture names.
946 *
947 * \param n number of texture names to be generated.
948 * \param textures an array in which will hold the generated texture names.
949 *
950 * \sa glGenTextures().
951 *
952 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
953 * IDs which are stored in \p textures. Corresponding empty texture
954 * objects are also generated.
955 */
956 void GLAPIENTRY
957 _mesa_GenTextures( GLsizei n, GLuint *textures )
958 {
959 GET_CURRENT_CONTEXT(ctx);
960 GLuint first;
961 GLint i;
962
963 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
964 _mesa_debug(ctx, "glGenTextures %d\n", n);
965
966 if (n < 0) {
967 _mesa_error( ctx, GL_INVALID_VALUE, "glGenTextures" );
968 return;
969 }
970
971 if (!textures)
972 return;
973
974 /*
975 * This must be atomic (generation and allocation of texture IDs)
976 */
977 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
978
979 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
980
981 /* Allocate new, empty texture objects */
982 for (i = 0; i < n; i++) {
983 struct gl_texture_object *texObj;
984 GLuint name = first + i;
985 GLenum target = 0;
986 texObj = ctx->Driver.NewTextureObject(ctx, name, target);
987 if (!texObj) {
988 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
989 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTextures");
990 return;
991 }
992
993 /* insert into hash table */
994 _mesa_HashInsert(ctx->Shared->TexObjects, texObj->Name, texObj);
995
996 textures[i] = name;
997 }
998
999 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1000 }
1001
1002
1003 /**
1004 * Check if the given texture object is bound to the current draw or
1005 * read framebuffer. If so, Unbind it.
1006 */
1007 static void
1008 unbind_texobj_from_fbo(struct gl_context *ctx,
1009 struct gl_texture_object *texObj)
1010 {
1011 const GLuint n = (ctx->DrawBuffer == ctx->ReadBuffer) ? 1 : 2;
1012 GLuint i;
1013
1014 for (i = 0; i < n; i++) {
1015 struct gl_framebuffer *fb = (i == 0) ? ctx->DrawBuffer : ctx->ReadBuffer;
1016 if (_mesa_is_user_fbo(fb)) {
1017 GLuint j;
1018 for (j = 0; j < BUFFER_COUNT; j++) {
1019 if (fb->Attachment[j].Type == GL_TEXTURE &&
1020 fb->Attachment[j].Texture == texObj) {
1021 /* Vertices are already flushed by _mesa_DeleteTextures */
1022 ctx->NewState |= _NEW_BUFFERS;
1023 _mesa_remove_attachment(ctx, fb->Attachment + j);
1024 }
1025 }
1026 }
1027 }
1028 }
1029
1030
1031 /**
1032 * Check if the given texture object is bound to any texture image units and
1033 * unbind it if so (revert to default textures).
1034 */
1035 static void
1036 unbind_texobj_from_texunits(struct gl_context *ctx,
1037 struct gl_texture_object *texObj)
1038 {
1039 GLuint u, tex;
1040
1041 for (u = 0; u < Elements(ctx->Texture.Unit); u++) {
1042 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
1043 for (tex = 0; tex < NUM_TEXTURE_TARGETS; tex++) {
1044 if (texObj == unit->CurrentTex[tex]) {
1045 _mesa_reference_texobj(&unit->CurrentTex[tex],
1046 ctx->Shared->DefaultTex[tex]);
1047 ASSERT(unit->CurrentTex[tex]);
1048 break;
1049 }
1050 }
1051 }
1052 }
1053
1054
1055 /**
1056 * Delete named textures.
1057 *
1058 * \param n number of textures to be deleted.
1059 * \param textures array of texture IDs to be deleted.
1060 *
1061 * \sa glDeleteTextures().
1062 *
1063 * If we're about to delete a texture that's currently bound to any
1064 * texture unit, unbind the texture first. Decrement the reference
1065 * count on the texture object and delete it if it's zero.
1066 * Recall that texture objects can be shared among several rendering
1067 * contexts.
1068 */
1069 void GLAPIENTRY
1070 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
1071 {
1072 GET_CURRENT_CONTEXT(ctx);
1073 GLint i;
1074
1075 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1076 _mesa_debug(ctx, "glDeleteTextures %d\n", n);
1077
1078 FLUSH_VERTICES(ctx, 0); /* too complex */
1079
1080 if (!textures)
1081 return;
1082
1083 for (i = 0; i < n; i++) {
1084 if (textures[i] > 0) {
1085 struct gl_texture_object *delObj
1086 = _mesa_lookup_texture(ctx, textures[i]);
1087
1088 if (delObj) {
1089 _mesa_lock_texture(ctx, delObj);
1090
1091 /* Check if texture is bound to any framebuffer objects.
1092 * If so, unbind.
1093 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1094 */
1095 unbind_texobj_from_fbo(ctx, delObj);
1096
1097 /* Check if this texture is currently bound to any texture units.
1098 * If so, unbind it.
1099 */
1100 unbind_texobj_from_texunits(ctx, delObj);
1101
1102 _mesa_unlock_texture(ctx, delObj);
1103
1104 ctx->NewState |= _NEW_TEXTURE;
1105
1106 /* The texture _name_ is now free for re-use.
1107 * Remove it from the hash table now.
1108 */
1109 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1110 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
1111 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1112
1113 /* Unreference the texobj. If refcount hits zero, the texture
1114 * will be deleted.
1115 */
1116 _mesa_reference_texobj(&delObj, NULL);
1117 }
1118 }
1119 }
1120 }
1121
1122
1123 /**
1124 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1125 * into the corresponding Mesa texture target index.
1126 * Note that proxy targets are not valid here.
1127 * \return TEXTURE_x_INDEX or -1 if target is invalid
1128 */
1129 static GLint
1130 target_enum_to_index(struct gl_context *ctx, GLenum target)
1131 {
1132 switch (target) {
1133 case GL_TEXTURE_1D:
1134 return _mesa_is_desktop_gl(ctx) ? TEXTURE_1D_INDEX : -1;
1135 case GL_TEXTURE_2D:
1136 return TEXTURE_2D_INDEX;
1137 case GL_TEXTURE_3D:
1138 return TEXTURE_3D_INDEX;
1139 case GL_TEXTURE_CUBE_MAP_ARB:
1140 return ctx->Extensions.ARB_texture_cube_map
1141 ? TEXTURE_CUBE_INDEX : -1;
1142 case GL_TEXTURE_RECTANGLE_NV:
1143 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle
1144 ? TEXTURE_RECT_INDEX : -1;
1145 case GL_TEXTURE_1D_ARRAY_EXT:
1146 return _mesa_is_desktop_gl(ctx)
1147 && (ctx->Extensions.EXT_texture_array
1148 || ctx->Extensions.MESA_texture_array)
1149 ? TEXTURE_1D_ARRAY_INDEX : -1;
1150 case GL_TEXTURE_2D_ARRAY_EXT:
1151 return (_mesa_is_desktop_gl(ctx)
1152 && (ctx->Extensions.EXT_texture_array
1153 || ctx->Extensions.MESA_texture_array))
1154 || _mesa_is_gles3(ctx)
1155 ? TEXTURE_2D_ARRAY_INDEX : -1;
1156 case GL_TEXTURE_BUFFER_ARB:
1157 return ctx->API == API_OPENGL_CORE &&
1158 ctx->Extensions.ARB_texture_buffer_object ?
1159 TEXTURE_BUFFER_INDEX : -1;
1160 case GL_TEXTURE_EXTERNAL_OES:
1161 return _mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external
1162 ? TEXTURE_EXTERNAL_INDEX : -1;
1163 case GL_TEXTURE_CUBE_MAP_ARRAY:
1164 return TEXTURE_CUBE_ARRAY_INDEX;
1165 default:
1166 return -1;
1167 }
1168 }
1169
1170
1171 /**
1172 * Bind a named texture to a texturing target.
1173 *
1174 * \param target texture target.
1175 * \param texName texture name.
1176 *
1177 * \sa glBindTexture().
1178 *
1179 * Determines the old texture object bound and returns immediately if rebinding
1180 * the same texture. Get the current texture which is either a default texture
1181 * if name is null, a named texture from the hash, or a new texture if the
1182 * given texture name is new. Increments its reference count, binds it, and
1183 * calls dd_function_table::BindTexture. Decrements the old texture reference
1184 * count and deletes it if it reaches zero.
1185 */
1186 void GLAPIENTRY
1187 _mesa_BindTexture( GLenum target, GLuint texName )
1188 {
1189 GET_CURRENT_CONTEXT(ctx);
1190 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
1191 struct gl_texture_object *newTexObj = NULL;
1192 GLint targetIndex;
1193
1194 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1195 _mesa_debug(ctx, "glBindTexture %s %d\n",
1196 _mesa_lookup_enum_by_nr(target), (GLint) texName);
1197
1198 targetIndex = target_enum_to_index(ctx, target);
1199 if (targetIndex < 0) {
1200 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target)");
1201 return;
1202 }
1203 assert(targetIndex < NUM_TEXTURE_TARGETS);
1204
1205 /*
1206 * Get pointer to new texture object (newTexObj)
1207 */
1208 if (texName == 0) {
1209 /* Use a default texture object */
1210 newTexObj = ctx->Shared->DefaultTex[targetIndex];
1211 }
1212 else {
1213 /* non-default texture object */
1214 newTexObj = _mesa_lookup_texture(ctx, texName);
1215 if (newTexObj) {
1216 /* error checking */
1217 if (newTexObj->Target != 0 && newTexObj->Target != target) {
1218 /* the named texture object's target doesn't match the given target */
1219 _mesa_error( ctx, GL_INVALID_OPERATION,
1220 "glBindTexture(target mismatch)" );
1221 return;
1222 }
1223 if (newTexObj->Target == 0) {
1224 finish_texture_init(ctx, target, newTexObj);
1225 }
1226 }
1227 else {
1228 if (ctx->API == API_OPENGL_CORE) {
1229 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindTexture(non-gen name)");
1230 return;
1231 }
1232
1233 /* if this is a new texture id, allocate a texture object now */
1234 newTexObj = ctx->Driver.NewTextureObject(ctx, texName, target);
1235 if (!newTexObj) {
1236 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
1237 return;
1238 }
1239
1240 /* and insert it into hash table */
1241 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1242 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
1243 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1244 }
1245 newTexObj->Target = target;
1246 }
1247
1248 assert(valid_texture_object(newTexObj));
1249
1250 /* Check if this texture is only used by this context and is already bound.
1251 * If so, just return.
1252 */
1253 {
1254 GLboolean early_out;
1255 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1256 early_out = ((ctx->Shared->RefCount == 1)
1257 && (newTexObj == texUnit->CurrentTex[targetIndex]));
1258 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1259 if (early_out) {
1260 return;
1261 }
1262 }
1263
1264 /* flush before changing binding */
1265 FLUSH_VERTICES(ctx, _NEW_TEXTURE);
1266
1267 /* Do the actual binding. The refcount on the previously bound
1268 * texture object will be decremented. It'll be deleted if the
1269 * count hits zero.
1270 */
1271 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], newTexObj);
1272 ASSERT(texUnit->CurrentTex[targetIndex]);
1273
1274 /* Pass BindTexture call to device driver */
1275 if (ctx->Driver.BindTexture)
1276 ctx->Driver.BindTexture(ctx, target, newTexObj);
1277 }
1278
1279
1280 /**
1281 * Set texture priorities.
1282 *
1283 * \param n number of textures.
1284 * \param texName texture names.
1285 * \param priorities corresponding texture priorities.
1286 *
1287 * \sa glPrioritizeTextures().
1288 *
1289 * Looks up each texture in the hash, clamps the corresponding priority between
1290 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1291 */
1292 void GLAPIENTRY
1293 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1294 const GLclampf *priorities )
1295 {
1296 GET_CURRENT_CONTEXT(ctx);
1297 GLint i;
1298
1299 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1300 _mesa_debug(ctx, "glPrioritizeTextures %d\n", n);
1301
1302 FLUSH_VERTICES(ctx, 0);
1303
1304 if (n < 0) {
1305 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1306 return;
1307 }
1308
1309 if (!priorities)
1310 return;
1311
1312 for (i = 0; i < n; i++) {
1313 if (texName[i] > 0) {
1314 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1315 if (t) {
1316 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1317 }
1318 }
1319 }
1320
1321 ctx->NewState |= _NEW_TEXTURE;
1322 }
1323
1324
1325
1326 /**
1327 * See if textures are loaded in texture memory.
1328 *
1329 * \param n number of textures to query.
1330 * \param texName array with the texture names.
1331 * \param residences array which will hold the residence status.
1332 *
1333 * \return GL_TRUE if all textures are resident and \p residences is left unchanged,
1334 *
1335 * Note: we assume all textures are always resident
1336 */
1337 GLboolean GLAPIENTRY
1338 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
1339 GLboolean *residences)
1340 {
1341 GET_CURRENT_CONTEXT(ctx);
1342 GLboolean allResident = GL_TRUE;
1343 GLint i;
1344 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1345
1346 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1347 _mesa_debug(ctx, "glAreTexturesResident %d\n", n);
1348
1349 if (n < 0) {
1350 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1351 return GL_FALSE;
1352 }
1353
1354 if (!texName || !residences)
1355 return GL_FALSE;
1356
1357 /* We only do error checking on the texture names */
1358 for (i = 0; i < n; i++) {
1359 struct gl_texture_object *t;
1360 if (texName[i] == 0) {
1361 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1362 return GL_FALSE;
1363 }
1364 t = _mesa_lookup_texture(ctx, texName[i]);
1365 if (!t) {
1366 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1367 return GL_FALSE;
1368 }
1369 }
1370
1371 return allResident;
1372 }
1373
1374
1375 /**
1376 * See if a name corresponds to a texture.
1377 *
1378 * \param texture texture name.
1379 *
1380 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1381 * otherwise.
1382 *
1383 * \sa glIsTexture().
1384 *
1385 * Calls _mesa_HashLookup().
1386 */
1387 GLboolean GLAPIENTRY
1388 _mesa_IsTexture( GLuint texture )
1389 {
1390 struct gl_texture_object *t;
1391 GET_CURRENT_CONTEXT(ctx);
1392 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1393
1394 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1395 _mesa_debug(ctx, "glIsTexture %d\n", texture);
1396
1397 if (!texture)
1398 return GL_FALSE;
1399
1400 t = _mesa_lookup_texture(ctx, texture);
1401
1402 /* IsTexture is true only after object has been bound once. */
1403 return t && t->Target;
1404 }
1405
1406
1407 /**
1408 * Simplest implementation of texture locking: grab the shared tex
1409 * mutex. Examine the shared context state timestamp and if there has
1410 * been a change, set the appropriate bits in ctx->NewState.
1411 *
1412 * This is used to deal with synchronizing things when a texture object
1413 * is used/modified by different contexts (or threads) which are sharing
1414 * the texture.
1415 *
1416 * See also _mesa_lock/unlock_texture() in teximage.h
1417 */
1418 void
1419 _mesa_lock_context_textures( struct gl_context *ctx )
1420 {
1421 _glthread_LOCK_MUTEX(ctx->Shared->TexMutex);
1422
1423 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
1424 ctx->NewState |= _NEW_TEXTURE;
1425 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
1426 }
1427 }
1428
1429
1430 void
1431 _mesa_unlock_context_textures( struct gl_context *ctx )
1432 {
1433 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
1434 _glthread_UNLOCK_MUTEX(ctx->Shared->TexMutex);
1435 }
1436
1437 void GLAPIENTRY
1438 _mesa_InvalidateTexSubImage(GLuint texture, GLint level, GLint xoffset,
1439 GLint yoffset, GLint zoffset, GLsizei width,
1440 GLsizei height, GLsizei depth)
1441 {
1442 struct gl_texture_object *t;
1443 struct gl_texture_image *image;
1444 GET_CURRENT_CONTEXT(ctx);
1445
1446 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1447 _mesa_debug(ctx, "glInvalidateTexSubImage %d\n", texture);
1448
1449 t = invalidate_tex_image_error_check(ctx, texture, level,
1450 "glInvalidateTexSubImage");
1451
1452 /* The GL_ARB_invalidate_subdata spec says:
1453 *
1454 * "...the specified subregion must be between -<b> and <dim>+<b> where
1455 * <dim> is the size of the dimension of the texture image, and <b> is
1456 * the size of the border of that texture image, otherwise
1457 * INVALID_VALUE is generated (border is not applied to dimensions that
1458 * don't exist in a given texture target)."
1459 */
1460 image = t->Image[0][level];
1461 if (image) {
1462 int xBorder;
1463 int yBorder;
1464 int zBorder;
1465 int imageWidth;
1466 int imageHeight;
1467 int imageDepth;
1468
1469 /* The GL_ARB_invalidate_subdata spec says:
1470 *
1471 * "For texture targets that don't have certain dimensions, this
1472 * command treats those dimensions as having a size of 1. For
1473 * example, to invalidate a portion of a two-dimensional texture,
1474 * the application would use <zoffset> equal to zero and <depth>
1475 * equal to one."
1476 */
1477 switch (t->Target) {
1478 case GL_TEXTURE_BUFFER:
1479 xBorder = 0;
1480 yBorder = 0;
1481 zBorder = 0;
1482 imageWidth = 1;
1483 imageHeight = 1;
1484 imageDepth = 1;
1485 break;
1486 case GL_TEXTURE_1D:
1487 xBorder = image->Border;
1488 yBorder = 0;
1489 zBorder = 0;
1490 imageWidth = image->Width;
1491 imageHeight = 1;
1492 imageDepth = 1;
1493 break;
1494 case GL_TEXTURE_1D_ARRAY:
1495 xBorder = image->Border;
1496 yBorder = 0;
1497 zBorder = 0;
1498 imageWidth = image->Width;
1499 imageHeight = image->Height;
1500 imageDepth = 1;
1501 break;
1502 case GL_TEXTURE_2D:
1503 case GL_TEXTURE_CUBE_MAP:
1504 case GL_TEXTURE_RECTANGLE:
1505 case GL_TEXTURE_2D_MULTISAMPLE:
1506 xBorder = image->Border;
1507 yBorder = image->Border;
1508 zBorder = 0;
1509 imageWidth = image->Width;
1510 imageHeight = image->Height;
1511 imageDepth = 1;
1512 break;
1513 case GL_TEXTURE_2D_ARRAY:
1514 case GL_TEXTURE_CUBE_MAP_ARRAY:
1515 xBorder = image->Border;
1516 yBorder = image->Border;
1517 zBorder = 0;
1518 imageWidth = image->Width;
1519 imageHeight = image->Height;
1520 imageDepth = image->Depth;
1521 break;
1522 case GL_TEXTURE_3D:
1523 xBorder = image->Border;
1524 yBorder = image->Border;
1525 zBorder = image->Border;
1526 imageWidth = image->Width;
1527 imageHeight = image->Height;
1528 imageDepth = image->Depth;
1529 break;
1530 default:
1531 assert(!"Should not get here.");
1532 xBorder = 0;
1533 yBorder = 0;
1534 zBorder = 0;
1535 imageWidth = 0;
1536 imageHeight = 0;
1537 imageDepth = 0;
1538 break;
1539 }
1540
1541 if (xoffset < -xBorder) {
1542 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(xoffset)");
1543 return;
1544 }
1545
1546 if (xoffset + width > imageWidth + xBorder) {
1547 _mesa_error(ctx, GL_INVALID_VALUE,
1548 "glInvalidateSubTexImage(xoffset+width)");
1549 return;
1550 }
1551
1552 if (yoffset < -yBorder) {
1553 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(yoffset)");
1554 return;
1555 }
1556
1557 if (yoffset + height > imageHeight + yBorder) {
1558 _mesa_error(ctx, GL_INVALID_VALUE,
1559 "glInvalidateSubTexImage(yoffset+height)");
1560 return;
1561 }
1562
1563 if (zoffset < -zBorder) {
1564 _mesa_error(ctx, GL_INVALID_VALUE,
1565 "glInvalidateSubTexImage(zoffset)");
1566 return;
1567 }
1568
1569 if (zoffset + depth > imageDepth + zBorder) {
1570 _mesa_error(ctx, GL_INVALID_VALUE,
1571 "glInvalidateSubTexImage(zoffset+depth)");
1572 return;
1573 }
1574 }
1575
1576 /* We don't actually do anything for this yet. Just return after
1577 * validating the parameters and generating the required errors.
1578 */
1579 return;
1580 }
1581
1582 void GLAPIENTRY
1583 _mesa_InvalidateTexImage(GLuint texture, GLint level)
1584 {
1585 GET_CURRENT_CONTEXT(ctx);
1586
1587 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1588 _mesa_debug(ctx, "glInvalidateTexImage(%d, %d)\n", texture, level);
1589
1590 invalidate_tex_image_error_check(ctx, texture, level,
1591 "glInvalidateTexImage");
1592
1593 /* We don't actually do anything for this yet. Just return after
1594 * validating the parameters and generating the required errors.
1595 */
1596 return;
1597 }
1598
1599 /*@}*/