mesa: split bind_buffer_range_uniform_buffer() in two
[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 *
9 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include <stdio.h>
32 #include "bufferobj.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "fbobject.h"
36 #include "formats.h"
37 #include "hash.h"
38 #include "imports.h"
39 #include "macros.h"
40 #include "shaderimage.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 * This function checks for all valid combinations of Min and Mag filters for
55 * Float types, when extensions like OES_texture_float and
56 * OES_texture_float_linear are supported. OES_texture_float mentions support
57 * for NEAREST, NEAREST_MIPMAP_NEAREST magnification and minification filters.
58 * Mag filters like LINEAR and min filters like NEAREST_MIPMAP_LINEAR,
59 * LINEAR_MIPMAP_NEAREST and LINEAR_MIPMAP_LINEAR are only valid in case
60 * OES_texture_float_linear is supported.
61 *
62 * Returns true in case the filter is valid for given Float type else false.
63 */
64 static bool
65 valid_filter_for_float(const struct gl_context *ctx,
66 const struct gl_texture_object *obj)
67 {
68 switch (obj->Sampler.MagFilter) {
69 case GL_LINEAR:
70 if (obj->_IsHalfFloat && !ctx->Extensions.OES_texture_half_float_linear) {
71 return false;
72 } else if (obj->_IsFloat && !ctx->Extensions.OES_texture_float_linear) {
73 return false;
74 }
75 case GL_NEAREST:
76 case GL_NEAREST_MIPMAP_NEAREST:
77 break;
78 default:
79 unreachable("Invalid mag filter");
80 }
81
82 switch (obj->Sampler.MinFilter) {
83 case GL_LINEAR:
84 case GL_NEAREST_MIPMAP_LINEAR:
85 case GL_LINEAR_MIPMAP_NEAREST:
86 case GL_LINEAR_MIPMAP_LINEAR:
87 if (obj->_IsHalfFloat && !ctx->Extensions.OES_texture_half_float_linear) {
88 return false;
89 } else if (obj->_IsFloat && !ctx->Extensions.OES_texture_float_linear) {
90 return false;
91 }
92 case GL_NEAREST:
93 case GL_NEAREST_MIPMAP_NEAREST:
94 break;
95 default:
96 unreachable("Invalid min filter");
97 }
98
99 return true;
100 }
101
102 /**
103 * Return the gl_texture_object for a given ID.
104 */
105 struct gl_texture_object *
106 _mesa_lookup_texture(struct gl_context *ctx, GLuint id)
107 {
108 return (struct gl_texture_object *)
109 _mesa_HashLookup(ctx->Shared->TexObjects, id);
110 }
111
112 /**
113 * Wrapper around _mesa_lookup_texture that throws GL_INVALID_OPERATION if id
114 * is not in the hash table. After calling _mesa_error, it returns NULL.
115 */
116 struct gl_texture_object *
117 _mesa_lookup_texture_err(struct gl_context *ctx, GLuint id, const char* func)
118 {
119 struct gl_texture_object *texObj = NULL;
120
121 if (id > 0)
122 texObj = _mesa_lookup_texture(ctx, id); /* Returns NULL if not found. */
123
124 if (!texObj)
125 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(texture)", func);
126
127 return texObj;
128 }
129
130
131 struct gl_texture_object *
132 _mesa_lookup_texture_locked(struct gl_context *ctx, GLuint id)
133 {
134 return (struct gl_texture_object *)
135 _mesa_HashLookupLocked(ctx->Shared->TexObjects, id);
136 }
137
138 /**
139 * Return a pointer to the current texture object for the given target
140 * on the current texture unit.
141 * Note: all <target> error checking should have been done by this point.
142 */
143 struct gl_texture_object *
144 _mesa_get_current_tex_object(struct gl_context *ctx, GLenum target)
145 {
146 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
147 const GLboolean arrayTex = ctx->Extensions.EXT_texture_array;
148
149 switch (target) {
150 case GL_TEXTURE_1D:
151 return texUnit->CurrentTex[TEXTURE_1D_INDEX];
152 case GL_PROXY_TEXTURE_1D:
153 return ctx->Texture.ProxyTex[TEXTURE_1D_INDEX];
154 case GL_TEXTURE_2D:
155 return texUnit->CurrentTex[TEXTURE_2D_INDEX];
156 case GL_PROXY_TEXTURE_2D:
157 return ctx->Texture.ProxyTex[TEXTURE_2D_INDEX];
158 case GL_TEXTURE_3D:
159 return texUnit->CurrentTex[TEXTURE_3D_INDEX];
160 case GL_PROXY_TEXTURE_3D:
161 return ctx->Texture.ProxyTex[TEXTURE_3D_INDEX];
162 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
163 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
164 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
165 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
166 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
167 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
168 case GL_TEXTURE_CUBE_MAP:
169 return ctx->Extensions.ARB_texture_cube_map
170 ? texUnit->CurrentTex[TEXTURE_CUBE_INDEX] : NULL;
171 case GL_PROXY_TEXTURE_CUBE_MAP:
172 return ctx->Extensions.ARB_texture_cube_map
173 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_INDEX] : NULL;
174 case GL_TEXTURE_CUBE_MAP_ARRAY:
175 return _mesa_has_texture_cube_map_array(ctx)
176 ? texUnit->CurrentTex[TEXTURE_CUBE_ARRAY_INDEX] : NULL;
177 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
178 return _mesa_has_texture_cube_map_array(ctx)
179 ? ctx->Texture.ProxyTex[TEXTURE_CUBE_ARRAY_INDEX] : NULL;
180 case GL_TEXTURE_RECTANGLE_NV:
181 return ctx->Extensions.NV_texture_rectangle
182 ? texUnit->CurrentTex[TEXTURE_RECT_INDEX] : NULL;
183 case GL_PROXY_TEXTURE_RECTANGLE_NV:
184 return ctx->Extensions.NV_texture_rectangle
185 ? ctx->Texture.ProxyTex[TEXTURE_RECT_INDEX] : NULL;
186 case GL_TEXTURE_1D_ARRAY_EXT:
187 return arrayTex ? texUnit->CurrentTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
188 case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
189 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_1D_ARRAY_INDEX] : NULL;
190 case GL_TEXTURE_2D_ARRAY_EXT:
191 return arrayTex ? texUnit->CurrentTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
192 case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
193 return arrayTex ? ctx->Texture.ProxyTex[TEXTURE_2D_ARRAY_INDEX] : NULL;
194 case GL_TEXTURE_BUFFER:
195 return (_mesa_has_ARB_texture_buffer_object(ctx) ||
196 _mesa_has_OES_texture_buffer(ctx)) ?
197 texUnit->CurrentTex[TEXTURE_BUFFER_INDEX] : NULL;
198 case GL_TEXTURE_EXTERNAL_OES:
199 return _mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external
200 ? texUnit->CurrentTex[TEXTURE_EXTERNAL_INDEX] : NULL;
201 case GL_TEXTURE_2D_MULTISAMPLE:
202 return ctx->Extensions.ARB_texture_multisample
203 ? texUnit->CurrentTex[TEXTURE_2D_MULTISAMPLE_INDEX] : NULL;
204 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
205 return ctx->Extensions.ARB_texture_multisample
206 ? ctx->Texture.ProxyTex[TEXTURE_2D_MULTISAMPLE_INDEX] : NULL;
207 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
208 return ctx->Extensions.ARB_texture_multisample
209 ? texUnit->CurrentTex[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX] : NULL;
210 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
211 return ctx->Extensions.ARB_texture_multisample
212 ? ctx->Texture.ProxyTex[TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX] : NULL;
213 default:
214 _mesa_problem(NULL, "bad target in _mesa_get_current_tex_object()");
215 return NULL;
216 }
217 }
218
219
220 /**
221 * Allocate and initialize a new texture object. But don't put it into the
222 * texture object hash table.
223 *
224 * Called via ctx->Driver.NewTextureObject, unless overridden by a device
225 * driver.
226 *
227 * \param shared the shared GL state structure to contain the texture object
228 * \param name integer name for the texture object
229 * \param target either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D,
230 * GL_TEXTURE_CUBE_MAP or GL_TEXTURE_RECTANGLE_NV. zero is ok for the sake
231 * of GenTextures()
232 *
233 * \return pointer to new texture object.
234 */
235 struct gl_texture_object *
236 _mesa_new_texture_object( struct gl_context *ctx, GLuint name, GLenum target )
237 {
238 struct gl_texture_object *obj;
239 (void) ctx;
240 obj = MALLOC_STRUCT(gl_texture_object);
241 _mesa_initialize_texture_object(ctx, obj, name, target);
242 return obj;
243 }
244
245
246 /**
247 * Initialize a new texture object to default values.
248 * \param obj the texture object
249 * \param name the texture name
250 * \param target the texture target
251 */
252 void
253 _mesa_initialize_texture_object( struct gl_context *ctx,
254 struct gl_texture_object *obj,
255 GLuint name, GLenum target )
256 {
257 assert(target == 0 ||
258 target == GL_TEXTURE_1D ||
259 target == GL_TEXTURE_2D ||
260 target == GL_TEXTURE_3D ||
261 target == GL_TEXTURE_CUBE_MAP ||
262 target == GL_TEXTURE_RECTANGLE_NV ||
263 target == GL_TEXTURE_1D_ARRAY_EXT ||
264 target == GL_TEXTURE_2D_ARRAY_EXT ||
265 target == GL_TEXTURE_EXTERNAL_OES ||
266 target == GL_TEXTURE_CUBE_MAP_ARRAY ||
267 target == GL_TEXTURE_BUFFER ||
268 target == GL_TEXTURE_2D_MULTISAMPLE ||
269 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
270
271 memset(obj, 0, sizeof(*obj));
272 /* init the non-zero fields */
273 mtx_init(&obj->Mutex, mtx_plain);
274 obj->RefCount = 1;
275 obj->Name = name;
276 obj->Target = target;
277 if (target != 0) {
278 obj->TargetIndex = _mesa_tex_target_to_index(ctx, target);
279 }
280 else {
281 obj->TargetIndex = NUM_TEXTURE_TARGETS; /* invalid/error value */
282 }
283 obj->Priority = 1.0F;
284 obj->BaseLevel = 0;
285 obj->MaxLevel = 1000;
286
287 /* must be one; no support for (YUV) planes in separate buffers */
288 obj->RequiredTextureImageUnits = 1;
289
290 /* sampler state */
291 if (target == GL_TEXTURE_RECTANGLE_NV ||
292 target == GL_TEXTURE_EXTERNAL_OES) {
293 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
294 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
295 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
296 obj->Sampler.MinFilter = GL_LINEAR;
297 }
298 else {
299 obj->Sampler.WrapS = GL_REPEAT;
300 obj->Sampler.WrapT = GL_REPEAT;
301 obj->Sampler.WrapR = GL_REPEAT;
302 obj->Sampler.MinFilter = GL_NEAREST_MIPMAP_LINEAR;
303 }
304 obj->Sampler.MagFilter = GL_LINEAR;
305 obj->Sampler.MinLod = -1000.0;
306 obj->Sampler.MaxLod = 1000.0;
307 obj->Sampler.LodBias = 0.0;
308 obj->Sampler.MaxAnisotropy = 1.0;
309 obj->Sampler.CompareMode = GL_NONE; /* ARB_shadow */
310 obj->Sampler.CompareFunc = GL_LEQUAL; /* ARB_shadow */
311 obj->DepthMode = ctx->API == API_OPENGL_CORE ? GL_RED : GL_LUMINANCE;
312 obj->StencilSampling = false;
313 obj->Sampler.CubeMapSeamless = GL_FALSE;
314 obj->Swizzle[0] = GL_RED;
315 obj->Swizzle[1] = GL_GREEN;
316 obj->Swizzle[2] = GL_BLUE;
317 obj->Swizzle[3] = GL_ALPHA;
318 obj->_Swizzle = SWIZZLE_NOOP;
319 obj->Sampler.sRGBDecode = GL_DECODE_EXT;
320 obj->BufferObjectFormat = GL_R8;
321 obj->_BufferObjectFormat = MESA_FORMAT_R_UNORM8;
322 obj->ImageFormatCompatibilityType = GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE;
323 }
324
325
326 /**
327 * Some texture initialization can't be finished until we know which
328 * target it's getting bound to (GL_TEXTURE_1D/2D/etc).
329 */
330 static void
331 finish_texture_init(struct gl_context *ctx, GLenum target,
332 struct gl_texture_object *obj, int targetIndex)
333 {
334 GLenum filter = GL_LINEAR;
335 assert(obj->Target == 0);
336
337 obj->Target = target;
338 obj->TargetIndex = targetIndex;
339 assert(obj->TargetIndex < NUM_TEXTURE_TARGETS);
340
341 switch (target) {
342 case GL_TEXTURE_2D_MULTISAMPLE:
343 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
344 filter = GL_NEAREST;
345 /* fallthrough */
346
347 case GL_TEXTURE_RECTANGLE_NV:
348 case GL_TEXTURE_EXTERNAL_OES:
349 /* have to init wrap and filter state here - kind of klunky */
350 obj->Sampler.WrapS = GL_CLAMP_TO_EDGE;
351 obj->Sampler.WrapT = GL_CLAMP_TO_EDGE;
352 obj->Sampler.WrapR = GL_CLAMP_TO_EDGE;
353 obj->Sampler.MinFilter = filter;
354 obj->Sampler.MagFilter = filter;
355 if (ctx->Driver.TexParameter) {
356 /* XXX we probably don't need to make all these calls */
357 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_S);
358 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_T);
359 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_WRAP_R);
360 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_MIN_FILTER);
361 ctx->Driver.TexParameter(ctx, obj, GL_TEXTURE_MAG_FILTER);
362 }
363 break;
364
365 default:
366 /* nothing needs done */
367 break;
368 }
369 }
370
371
372 /**
373 * Deallocate a texture object struct. It should have already been
374 * removed from the texture object pool.
375 * Called via ctx->Driver.DeleteTexture() if not overriden by a driver.
376 *
377 * \param shared the shared GL state to which the object belongs.
378 * \param texObj the texture object to delete.
379 */
380 void
381 _mesa_delete_texture_object(struct gl_context *ctx,
382 struct gl_texture_object *texObj)
383 {
384 GLuint i, face;
385
386 /* Set Target to an invalid value. With some assertions elsewhere
387 * we can try to detect possible use of deleted textures.
388 */
389 texObj->Target = 0x99;
390
391 /* free the texture images */
392 for (face = 0; face < 6; face++) {
393 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
394 if (texObj->Image[face][i]) {
395 ctx->Driver.DeleteTextureImage(ctx, texObj->Image[face][i]);
396 }
397 }
398 }
399
400 _mesa_reference_buffer_object(ctx, &texObj->BufferObject, NULL);
401
402 /* destroy the mutex -- it may have allocated memory (eg on bsd) */
403 mtx_destroy(&texObj->Mutex);
404
405 free(texObj->Label);
406
407 /* free this object */
408 free(texObj);
409 }
410
411
412 /**
413 * Copy texture object state from one texture object to another.
414 * Use for glPush/PopAttrib.
415 *
416 * \param dest destination texture object.
417 * \param src source texture object.
418 */
419 void
420 _mesa_copy_texture_object( struct gl_texture_object *dest,
421 const struct gl_texture_object *src )
422 {
423 dest->Target = src->Target;
424 dest->TargetIndex = src->TargetIndex;
425 dest->Name = src->Name;
426 dest->Priority = src->Priority;
427 dest->Sampler.BorderColor.f[0] = src->Sampler.BorderColor.f[0];
428 dest->Sampler.BorderColor.f[1] = src->Sampler.BorderColor.f[1];
429 dest->Sampler.BorderColor.f[2] = src->Sampler.BorderColor.f[2];
430 dest->Sampler.BorderColor.f[3] = src->Sampler.BorderColor.f[3];
431 dest->Sampler.WrapS = src->Sampler.WrapS;
432 dest->Sampler.WrapT = src->Sampler.WrapT;
433 dest->Sampler.WrapR = src->Sampler.WrapR;
434 dest->Sampler.MinFilter = src->Sampler.MinFilter;
435 dest->Sampler.MagFilter = src->Sampler.MagFilter;
436 dest->Sampler.MinLod = src->Sampler.MinLod;
437 dest->Sampler.MaxLod = src->Sampler.MaxLod;
438 dest->Sampler.LodBias = src->Sampler.LodBias;
439 dest->BaseLevel = src->BaseLevel;
440 dest->MaxLevel = src->MaxLevel;
441 dest->Sampler.MaxAnisotropy = src->Sampler.MaxAnisotropy;
442 dest->Sampler.CompareMode = src->Sampler.CompareMode;
443 dest->Sampler.CompareFunc = src->Sampler.CompareFunc;
444 dest->Sampler.CubeMapSeamless = src->Sampler.CubeMapSeamless;
445 dest->DepthMode = src->DepthMode;
446 dest->StencilSampling = src->StencilSampling;
447 dest->Sampler.sRGBDecode = src->Sampler.sRGBDecode;
448 dest->_MaxLevel = src->_MaxLevel;
449 dest->_MaxLambda = src->_MaxLambda;
450 dest->GenerateMipmap = src->GenerateMipmap;
451 dest->_BaseComplete = src->_BaseComplete;
452 dest->_MipmapComplete = src->_MipmapComplete;
453 COPY_4V(dest->Swizzle, src->Swizzle);
454 dest->_Swizzle = src->_Swizzle;
455 dest->_IsHalfFloat = src->_IsHalfFloat;
456 dest->_IsFloat = src->_IsFloat;
457
458 dest->RequiredTextureImageUnits = src->RequiredTextureImageUnits;
459 }
460
461
462 /**
463 * Free all texture images of the given texture object.
464 *
465 * \param ctx GL context.
466 * \param t texture object.
467 *
468 * \sa _mesa_clear_texture_image().
469 */
470 void
471 _mesa_clear_texture_object(struct gl_context *ctx,
472 struct gl_texture_object *texObj)
473 {
474 GLuint i, j;
475
476 if (texObj->Target == 0)
477 return;
478
479 for (i = 0; i < MAX_FACES; i++) {
480 for (j = 0; j < MAX_TEXTURE_LEVELS; j++) {
481 struct gl_texture_image *texImage = texObj->Image[i][j];
482 if (texImage)
483 _mesa_clear_texture_image(ctx, texImage);
484 }
485 }
486 }
487
488
489 /**
490 * Check if the given texture object is valid by examining its Target field.
491 * For debugging only.
492 */
493 static GLboolean
494 valid_texture_object(const struct gl_texture_object *tex)
495 {
496 switch (tex->Target) {
497 case 0:
498 case GL_TEXTURE_1D:
499 case GL_TEXTURE_2D:
500 case GL_TEXTURE_3D:
501 case GL_TEXTURE_CUBE_MAP:
502 case GL_TEXTURE_RECTANGLE_NV:
503 case GL_TEXTURE_1D_ARRAY_EXT:
504 case GL_TEXTURE_2D_ARRAY_EXT:
505 case GL_TEXTURE_BUFFER:
506 case GL_TEXTURE_EXTERNAL_OES:
507 case GL_TEXTURE_CUBE_MAP_ARRAY:
508 case GL_TEXTURE_2D_MULTISAMPLE:
509 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
510 return GL_TRUE;
511 case 0x99:
512 _mesa_problem(NULL, "invalid reference to a deleted texture object");
513 return GL_FALSE;
514 default:
515 _mesa_problem(NULL, "invalid texture object Target 0x%x, Id = %u",
516 tex->Target, tex->Name);
517 return GL_FALSE;
518 }
519 }
520
521
522 /**
523 * Reference (or unreference) a texture object.
524 * If '*ptr', decrement *ptr's refcount (and delete if it becomes zero).
525 * If 'tex' is non-null, increment its refcount.
526 * This is normally only called from the _mesa_reference_texobj() macro
527 * when there's a real pointer change.
528 */
529 void
530 _mesa_reference_texobj_(struct gl_texture_object **ptr,
531 struct gl_texture_object *tex)
532 {
533 assert(ptr);
534
535 if (*ptr) {
536 /* Unreference the old texture */
537 GLboolean deleteFlag = GL_FALSE;
538 struct gl_texture_object *oldTex = *ptr;
539
540 assert(valid_texture_object(oldTex));
541 (void) valid_texture_object; /* silence warning in release builds */
542
543 mtx_lock(&oldTex->Mutex);
544 assert(oldTex->RefCount > 0);
545 oldTex->RefCount--;
546
547 deleteFlag = (oldTex->RefCount == 0);
548 mtx_unlock(&oldTex->Mutex);
549
550 if (deleteFlag) {
551 /* Passing in the context drastically changes the driver code for
552 * framebuffer deletion.
553 */
554 GET_CURRENT_CONTEXT(ctx);
555 if (ctx)
556 ctx->Driver.DeleteTexture(ctx, oldTex);
557 else
558 _mesa_problem(NULL, "Unable to delete texture, no context");
559 }
560
561 *ptr = NULL;
562 }
563 assert(!*ptr);
564
565 if (tex) {
566 /* reference new texture */
567 assert(valid_texture_object(tex));
568 mtx_lock(&tex->Mutex);
569 assert(tex->RefCount > 0);
570
571 tex->RefCount++;
572 *ptr = tex;
573 mtx_unlock(&tex->Mutex);
574 }
575 }
576
577
578 enum base_mipmap { BASE, MIPMAP };
579
580
581 /**
582 * Mark a texture object as incomplete. There are actually three kinds of
583 * (in)completeness:
584 * 1. "base incomplete": the base level of the texture is invalid so no
585 * texturing is possible.
586 * 2. "mipmap incomplete": a non-base level of the texture is invalid so
587 * mipmap filtering isn't possible, but non-mipmap filtering is.
588 * 3. "texture incompleteness": some combination of texture state and
589 * sampler state renders the texture incomplete.
590 *
591 * \param t texture object
592 * \param bm either BASE or MIPMAP to indicate what's incomplete
593 * \param fmt... string describing why it's incomplete (for debugging).
594 */
595 static void
596 incomplete(struct gl_texture_object *t, enum base_mipmap bm,
597 const char *fmt, ...)
598 {
599 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_TEXTURE) {
600 va_list args;
601 char s[100];
602
603 va_start(args, fmt);
604 vsnprintf(s, sizeof(s), fmt, args);
605 va_end(args);
606
607 _mesa_debug(NULL, "Texture Obj %d incomplete because: %s\n", t->Name, s);
608 }
609
610 if (bm == BASE)
611 t->_BaseComplete = GL_FALSE;
612 t->_MipmapComplete = GL_FALSE;
613 }
614
615
616 /**
617 * Examine a texture object to determine if it is complete.
618 *
619 * The gl_texture_object::Complete flag will be set to GL_TRUE or GL_FALSE
620 * accordingly.
621 *
622 * \param ctx GL context.
623 * \param t texture object.
624 *
625 * According to the texture target, verifies that each of the mipmaps is
626 * present and has the expected size.
627 */
628 void
629 _mesa_test_texobj_completeness( const struct gl_context *ctx,
630 struct gl_texture_object *t )
631 {
632 const GLint baseLevel = t->BaseLevel;
633 const struct gl_texture_image *baseImage;
634 GLint maxLevels = 0;
635
636 /* We'll set these to FALSE if tests fail below */
637 t->_BaseComplete = GL_TRUE;
638 t->_MipmapComplete = GL_TRUE;
639
640 if (t->Target == GL_TEXTURE_BUFFER) {
641 /* Buffer textures are always considered complete. The obvious case where
642 * they would be incomplete (no BO attached) is actually specced to be
643 * undefined rendering results.
644 */
645 return;
646 }
647
648 /* Detect cases where the application set the base level to an invalid
649 * value.
650 */
651 if ((baseLevel < 0) || (baseLevel >= MAX_TEXTURE_LEVELS)) {
652 incomplete(t, BASE, "base level = %d is invalid", baseLevel);
653 return;
654 }
655
656 if (t->MaxLevel < baseLevel) {
657 incomplete(t, MIPMAP, "MAX_LEVEL (%d) < BASE_LEVEL (%d)",
658 t->MaxLevel, baseLevel);
659 return;
660 }
661
662 baseImage = t->Image[0][baseLevel];
663
664 /* Always need the base level image */
665 if (!baseImage) {
666 incomplete(t, BASE, "Image[baseLevel=%d] == NULL", baseLevel);
667 return;
668 }
669
670 /* Check width/height/depth for zero */
671 if (baseImage->Width == 0 ||
672 baseImage->Height == 0 ||
673 baseImage->Depth == 0) {
674 incomplete(t, BASE, "texture width or height or depth = 0");
675 return;
676 }
677
678 /* Check if the texture values are integer */
679 {
680 GLenum datatype = _mesa_get_format_datatype(baseImage->TexFormat);
681 t->_IsIntegerFormat = datatype == GL_INT || datatype == GL_UNSIGNED_INT;
682 }
683
684 /* Check if the texture type is Float or HalfFloatOES and ensure Min and Mag
685 * filters are supported in this case.
686 */
687 if (_mesa_is_gles(ctx) && !valid_filter_for_float(ctx, t)) {
688 incomplete(t, BASE, "Filter is not supported with Float types.");
689 return;
690 }
691
692 /* Compute _MaxLevel (the maximum mipmap level we'll sample from given the
693 * mipmap image sizes and GL_TEXTURE_MAX_LEVEL state).
694 */
695 switch (t->Target) {
696 case GL_TEXTURE_1D:
697 case GL_TEXTURE_1D_ARRAY_EXT:
698 maxLevels = ctx->Const.MaxTextureLevels;
699 break;
700 case GL_TEXTURE_2D:
701 case GL_TEXTURE_2D_ARRAY_EXT:
702 maxLevels = ctx->Const.MaxTextureLevels;
703 break;
704 case GL_TEXTURE_3D:
705 maxLevels = ctx->Const.Max3DTextureLevels;
706 break;
707 case GL_TEXTURE_CUBE_MAP:
708 case GL_TEXTURE_CUBE_MAP_ARRAY:
709 maxLevels = ctx->Const.MaxCubeTextureLevels;
710 break;
711 case GL_TEXTURE_RECTANGLE_NV:
712 case GL_TEXTURE_BUFFER:
713 case GL_TEXTURE_EXTERNAL_OES:
714 case GL_TEXTURE_2D_MULTISAMPLE:
715 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
716 maxLevels = 1; /* no mipmapping */
717 break;
718 default:
719 _mesa_problem(ctx, "Bad t->Target in _mesa_test_texobj_completeness");
720 return;
721 }
722
723 assert(maxLevels > 0);
724
725 t->_MaxLevel = MIN3(t->MaxLevel,
726 /* 'p' in the GL spec */
727 (int) (baseLevel + baseImage->MaxNumLevels - 1),
728 /* 'q' in the GL spec */
729 maxLevels - 1);
730
731 if (t->Immutable) {
732 /* Adjust max level for views: the data store may have more levels than
733 * the view exposes.
734 */
735 t->_MaxLevel = MIN2(t->_MaxLevel, t->NumLevels - 1);
736 }
737
738 /* Compute _MaxLambda = q - p in the spec used during mipmapping */
739 t->_MaxLambda = (GLfloat) (t->_MaxLevel - baseLevel);
740
741 if (t->Immutable) {
742 /* This texture object was created with glTexStorage1/2/3D() so we
743 * know that all the mipmap levels are the right size and all cube
744 * map faces are the same size.
745 * We don't need to do any of the additional checks below.
746 */
747 return;
748 }
749
750 if (t->Target == GL_TEXTURE_CUBE_MAP) {
751 /* Make sure that all six cube map level 0 images are the same size and
752 * format.
753 * Note: we know that the image's width==height (we enforce that
754 * at glTexImage time) so we only need to test the width here.
755 */
756 GLuint face;
757 assert(baseImage->Width2 == baseImage->Height);
758 for (face = 1; face < 6; face++) {
759 assert(t->Image[face][baseLevel] == NULL ||
760 t->Image[face][baseLevel]->Width2 ==
761 t->Image[face][baseLevel]->Height2);
762 if (t->Image[face][baseLevel] == NULL ||
763 t->Image[face][baseLevel]->Width2 != baseImage->Width2) {
764 incomplete(t, BASE, "Cube face missing or mismatched size");
765 return;
766 }
767 if (t->Image[face][baseLevel]->InternalFormat !=
768 baseImage->InternalFormat) {
769 incomplete(t, BASE, "Cube face format mismatch");
770 return;
771 }
772 if (t->Image[face][baseLevel]->Border != baseImage->Border) {
773 incomplete(t, BASE, "Cube face border size mismatch");
774 return;
775 }
776 }
777 }
778
779 /*
780 * Do mipmap consistency checking.
781 * Note: we don't care about the current texture sampler state here.
782 * To determine texture completeness we'll either look at _BaseComplete
783 * or _MipmapComplete depending on the current minification filter mode.
784 */
785 {
786 GLint i;
787 const GLint minLevel = baseLevel;
788 const GLint maxLevel = t->_MaxLevel;
789 const GLuint numFaces = _mesa_num_tex_faces(t->Target);
790 GLuint width, height, depth, face;
791
792 if (minLevel > maxLevel) {
793 incomplete(t, MIPMAP, "minLevel > maxLevel");
794 return;
795 }
796
797 /* Get the base image's dimensions */
798 width = baseImage->Width2;
799 height = baseImage->Height2;
800 depth = baseImage->Depth2;
801
802 /* Note: this loop will be a no-op for RECT, BUFFER, EXTERNAL,
803 * MULTISAMPLE and MULTISAMPLE_ARRAY textures
804 */
805 for (i = baseLevel + 1; i < maxLevels; i++) {
806 /* Compute the expected size of image at level[i] */
807 if (width > 1) {
808 width /= 2;
809 }
810 if (height > 1 && t->Target != GL_TEXTURE_1D_ARRAY) {
811 height /= 2;
812 }
813 if (depth > 1 && t->Target != GL_TEXTURE_2D_ARRAY
814 && t->Target != GL_TEXTURE_CUBE_MAP_ARRAY) {
815 depth /= 2;
816 }
817
818 /* loop over cube faces (or single face otherwise) */
819 for (face = 0; face < numFaces; face++) {
820 if (i >= minLevel && i <= maxLevel) {
821 const struct gl_texture_image *img = t->Image[face][i];
822
823 if (!img) {
824 incomplete(t, MIPMAP, "TexImage[%d] is missing", i);
825 return;
826 }
827 if (img->InternalFormat != baseImage->InternalFormat) {
828 incomplete(t, MIPMAP, "Format[i] != Format[baseLevel]");
829 return;
830 }
831 if (img->Border != baseImage->Border) {
832 incomplete(t, MIPMAP, "Border[i] != Border[baseLevel]");
833 return;
834 }
835 if (img->Width2 != width) {
836 incomplete(t, MIPMAP, "TexImage[%d] bad width %u", i,
837 img->Width2);
838 return;
839 }
840 if (img->Height2 != height) {
841 incomplete(t, MIPMAP, "TexImage[%d] bad height %u", i,
842 img->Height2);
843 return;
844 }
845 if (img->Depth2 != depth) {
846 incomplete(t, MIPMAP, "TexImage[%d] bad depth %u", i,
847 img->Depth2);
848 return;
849 }
850 }
851 }
852
853 if (width == 1 && height == 1 && depth == 1) {
854 return; /* found smallest needed mipmap, all done! */
855 }
856 }
857 }
858 }
859
860
861 GLboolean
862 _mesa_cube_level_complete(const struct gl_texture_object *texObj,
863 const GLint level)
864 {
865 const struct gl_texture_image *img0, *img;
866 GLuint face;
867
868 if (texObj->Target != GL_TEXTURE_CUBE_MAP)
869 return GL_FALSE;
870
871 if ((level < 0) || (level >= MAX_TEXTURE_LEVELS))
872 return GL_FALSE;
873
874 /* check first face */
875 img0 = texObj->Image[0][level];
876 if (!img0 ||
877 img0->Width < 1 ||
878 img0->Width != img0->Height)
879 return GL_FALSE;
880
881 /* check remaining faces vs. first face */
882 for (face = 1; face < 6; face++) {
883 img = texObj->Image[face][level];
884 if (!img ||
885 img->Width != img0->Width ||
886 img->Height != img0->Height ||
887 img->TexFormat != img0->TexFormat)
888 return GL_FALSE;
889 }
890
891 return GL_TRUE;
892 }
893
894 /**
895 * Check if the given cube map texture is "cube complete" as defined in
896 * the OpenGL specification.
897 */
898 GLboolean
899 _mesa_cube_complete(const struct gl_texture_object *texObj)
900 {
901 return _mesa_cube_level_complete(texObj, texObj->BaseLevel);
902 }
903
904 /**
905 * Mark a texture object dirty. It forces the object to be incomplete
906 * and forces the context to re-validate its state.
907 *
908 * \param ctx GL context.
909 * \param texObj texture object.
910 */
911 void
912 _mesa_dirty_texobj(struct gl_context *ctx, struct gl_texture_object *texObj)
913 {
914 texObj->_BaseComplete = GL_FALSE;
915 texObj->_MipmapComplete = GL_FALSE;
916 ctx->NewState |= _NEW_TEXTURE_OBJECT;
917 }
918
919
920 /**
921 * Return pointer to a default/fallback texture of the given type/target.
922 * The texture is an RGBA texture with all texels = (0,0,0,1).
923 * That's the value a GLSL sampler should get when sampling from an
924 * incomplete texture.
925 */
926 struct gl_texture_object *
927 _mesa_get_fallback_texture(struct gl_context *ctx, gl_texture_index tex)
928 {
929 if (!ctx->Shared->FallbackTex[tex]) {
930 /* create fallback texture now */
931 const GLsizei width = 1, height = 1;
932 GLsizei depth = 1;
933 GLubyte texel[24];
934 struct gl_texture_object *texObj;
935 struct gl_texture_image *texImage;
936 mesa_format texFormat;
937 GLuint dims, face, numFaces = 1;
938 GLenum target;
939
940 for (face = 0; face < 6; face++) {
941 texel[4*face + 0] =
942 texel[4*face + 1] =
943 texel[4*face + 2] = 0x0;
944 texel[4*face + 3] = 0xff;
945 }
946
947 switch (tex) {
948 case TEXTURE_2D_ARRAY_INDEX:
949 dims = 3;
950 target = GL_TEXTURE_2D_ARRAY;
951 break;
952 case TEXTURE_1D_ARRAY_INDEX:
953 dims = 2;
954 target = GL_TEXTURE_1D_ARRAY;
955 break;
956 case TEXTURE_CUBE_INDEX:
957 dims = 2;
958 target = GL_TEXTURE_CUBE_MAP;
959 numFaces = 6;
960 break;
961 case TEXTURE_3D_INDEX:
962 dims = 3;
963 target = GL_TEXTURE_3D;
964 break;
965 case TEXTURE_RECT_INDEX:
966 dims = 2;
967 target = GL_TEXTURE_RECTANGLE;
968 break;
969 case TEXTURE_2D_INDEX:
970 dims = 2;
971 target = GL_TEXTURE_2D;
972 break;
973 case TEXTURE_1D_INDEX:
974 dims = 1;
975 target = GL_TEXTURE_1D;
976 break;
977 case TEXTURE_BUFFER_INDEX:
978 dims = 0;
979 target = GL_TEXTURE_BUFFER;
980 break;
981 case TEXTURE_CUBE_ARRAY_INDEX:
982 dims = 3;
983 target = GL_TEXTURE_CUBE_MAP_ARRAY;
984 depth = 6;
985 break;
986 case TEXTURE_EXTERNAL_INDEX:
987 dims = 2;
988 target = GL_TEXTURE_EXTERNAL_OES;
989 break;
990 case TEXTURE_2D_MULTISAMPLE_INDEX:
991 dims = 2;
992 target = GL_TEXTURE_2D_MULTISAMPLE;
993 break;
994 case TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX:
995 dims = 3;
996 target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
997 break;
998 default:
999 /* no-op */
1000 return NULL;
1001 }
1002
1003 /* create texture object */
1004 texObj = ctx->Driver.NewTextureObject(ctx, 0, target);
1005 if (!texObj)
1006 return NULL;
1007
1008 assert(texObj->RefCount == 1);
1009 texObj->Sampler.MinFilter = GL_NEAREST;
1010 texObj->Sampler.MagFilter = GL_NEAREST;
1011
1012 texFormat = ctx->Driver.ChooseTextureFormat(ctx, target,
1013 GL_RGBA, GL_RGBA,
1014 GL_UNSIGNED_BYTE);
1015
1016 /* need a loop here just for cube maps */
1017 for (face = 0; face < numFaces; face++) {
1018 const GLenum faceTarget = _mesa_cube_face_target(target, face);
1019
1020 /* initialize level[0] texture image */
1021 texImage = _mesa_get_tex_image(ctx, texObj, faceTarget, 0);
1022
1023 _mesa_init_teximage_fields(ctx, texImage,
1024 width,
1025 (dims > 1) ? height : 1,
1026 (dims > 2) ? depth : 1,
1027 0, /* border */
1028 GL_RGBA, texFormat);
1029
1030 ctx->Driver.TexImage(ctx, dims, texImage,
1031 GL_RGBA, GL_UNSIGNED_BYTE, texel,
1032 &ctx->DefaultPacking);
1033 }
1034
1035 _mesa_test_texobj_completeness(ctx, texObj);
1036 assert(texObj->_BaseComplete);
1037 assert(texObj->_MipmapComplete);
1038
1039 ctx->Shared->FallbackTex[tex] = texObj;
1040 }
1041 return ctx->Shared->FallbackTex[tex];
1042 }
1043
1044
1045 /**
1046 * Compute the size of the given texture object, in bytes.
1047 */
1048 static GLuint
1049 texture_size(const struct gl_texture_object *texObj)
1050 {
1051 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
1052 GLuint face, level, size = 0;
1053
1054 for (face = 0; face < numFaces; face++) {
1055 for (level = 0; level < MAX_TEXTURE_LEVELS; level++) {
1056 const struct gl_texture_image *img = texObj->Image[face][level];
1057 if (img) {
1058 GLuint sz = _mesa_format_image_size(img->TexFormat, img->Width,
1059 img->Height, img->Depth);
1060 size += sz;
1061 }
1062 }
1063 }
1064
1065 return size;
1066 }
1067
1068
1069 /**
1070 * Callback called from _mesa_HashWalk()
1071 */
1072 static void
1073 count_tex_size(GLuint key, void *data, void *userData)
1074 {
1075 const struct gl_texture_object *texObj =
1076 (const struct gl_texture_object *) data;
1077 GLuint *total = (GLuint *) userData;
1078
1079 (void) key;
1080
1081 *total = *total + texture_size(texObj);
1082 }
1083
1084
1085 /**
1086 * Compute total size (in bytes) of all textures for the given context.
1087 * For debugging purposes.
1088 */
1089 GLuint
1090 _mesa_total_texture_memory(struct gl_context *ctx)
1091 {
1092 GLuint tgt, total = 0;
1093
1094 _mesa_HashWalk(ctx->Shared->TexObjects, count_tex_size, &total);
1095
1096 /* plus, the default texture objects */
1097 for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
1098 total += texture_size(ctx->Shared->DefaultTex[tgt]);
1099 }
1100
1101 return total;
1102 }
1103
1104
1105 /**
1106 * Return the base format for the given texture object by looking
1107 * at the base texture image.
1108 * \return base format (such as GL_RGBA) or GL_NONE if it can't be determined
1109 */
1110 GLenum
1111 _mesa_texture_base_format(const struct gl_texture_object *texObj)
1112 {
1113 const struct gl_texture_image *texImage = _mesa_base_tex_image(texObj);
1114
1115 return texImage ? texImage->_BaseFormat : GL_NONE;
1116 }
1117
1118
1119 static struct gl_texture_object *
1120 invalidate_tex_image_error_check(struct gl_context *ctx, GLuint texture,
1121 GLint level, const char *name)
1122 {
1123 /* The GL_ARB_invalidate_subdata spec says:
1124 *
1125 * "If <texture> is zero or is not the name of a texture, the error
1126 * INVALID_VALUE is generated."
1127 *
1128 * This performs the error check in a different order than listed in the
1129 * spec. We have to get the texture object before we can validate the
1130 * other parameters against values in the texture object.
1131 */
1132 struct gl_texture_object *const t = _mesa_lookup_texture(ctx, texture);
1133 if (texture == 0 || t == NULL) {
1134 _mesa_error(ctx, GL_INVALID_VALUE, "%s(texture)", name);
1135 return NULL;
1136 }
1137
1138 /* The GL_ARB_invalidate_subdata spec says:
1139 *
1140 * "If <level> is less than zero or greater than the base 2 logarithm
1141 * of the maximum texture width, height, or depth, the error
1142 * INVALID_VALUE is generated."
1143 */
1144 if (level < 0 || level > t->MaxLevel) {
1145 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name);
1146 return NULL;
1147 }
1148
1149 /* The GL_ARB_invalidate_subdata spec says:
1150 *
1151 * "If the target of <texture> is TEXTURE_RECTANGLE, TEXTURE_BUFFER,
1152 * TEXTURE_2D_MULTISAMPLE, or TEXTURE_2D_MULTISAMPLE_ARRAY, and <level>
1153 * is not zero, the error INVALID_VALUE is generated."
1154 */
1155 if (level != 0) {
1156 switch (t->Target) {
1157 case GL_TEXTURE_RECTANGLE:
1158 case GL_TEXTURE_BUFFER:
1159 case GL_TEXTURE_2D_MULTISAMPLE:
1160 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1161 _mesa_error(ctx, GL_INVALID_VALUE, "%s(level)", name);
1162 return NULL;
1163
1164 default:
1165 break;
1166 }
1167 }
1168
1169 return t;
1170 }
1171
1172
1173 /**
1174 * Helper function for glCreateTextures and glGenTextures. Need this because
1175 * glCreateTextures should throw errors if target = 0. This is not exposed to
1176 * the rest of Mesa to encourage Mesa internals to use nameless textures,
1177 * which do not require expensive hash lookups.
1178 * \param target either 0 or a valid / error-checked texture target enum
1179 */
1180 static void
1181 create_textures(struct gl_context *ctx, GLenum target,
1182 GLsizei n, GLuint *textures, const char *caller)
1183 {
1184 GLuint first;
1185 GLint i;
1186
1187 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1188 _mesa_debug(ctx, "%s %d\n", caller, n);
1189
1190 if (n < 0) {
1191 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", caller);
1192 return;
1193 }
1194
1195 if (!textures)
1196 return;
1197
1198 /*
1199 * This must be atomic (generation and allocation of texture IDs)
1200 */
1201 _mesa_HashLockMutex(ctx->Shared->TexObjects);
1202
1203 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
1204
1205 /* Allocate new, empty texture objects */
1206 for (i = 0; i < n; i++) {
1207 struct gl_texture_object *texObj;
1208 GLuint name = first + i;
1209 texObj = ctx->Driver.NewTextureObject(ctx, name, target);
1210 if (!texObj) {
1211 _mesa_HashUnlockMutex(ctx->Shared->TexObjects);
1212 _mesa_error(ctx, GL_OUT_OF_MEMORY, "gl%sTextures", caller);
1213 return;
1214 }
1215
1216 /* insert into hash table */
1217 _mesa_HashInsertLocked(ctx->Shared->TexObjects, texObj->Name, texObj);
1218
1219 textures[i] = name;
1220 }
1221
1222 _mesa_HashUnlockMutex(ctx->Shared->TexObjects);
1223 }
1224
1225 /*@}*/
1226
1227
1228 /***********************************************************************/
1229 /** \name API functions */
1230 /*@{*/
1231
1232
1233 /**
1234 * Generate texture names.
1235 *
1236 * \param n number of texture names to be generated.
1237 * \param textures an array in which will hold the generated texture names.
1238 *
1239 * \sa glGenTextures(), glCreateTextures().
1240 *
1241 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1242 * IDs which are stored in \p textures. Corresponding empty texture
1243 * objects are also generated.
1244 */
1245 void GLAPIENTRY
1246 _mesa_GenTextures(GLsizei n, GLuint *textures)
1247 {
1248 GET_CURRENT_CONTEXT(ctx);
1249 create_textures(ctx, 0, n, textures, "glGenTextures");
1250 }
1251
1252 /**
1253 * Create texture objects.
1254 *
1255 * \param target the texture target for each name to be generated.
1256 * \param n number of texture names to be generated.
1257 * \param textures an array in which will hold the generated texture names.
1258 *
1259 * \sa glCreateTextures(), glGenTextures().
1260 *
1261 * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
1262 * IDs which are stored in \p textures. Corresponding empty texture
1263 * objects are also generated.
1264 */
1265 void GLAPIENTRY
1266 _mesa_CreateTextures(GLenum target, GLsizei n, GLuint *textures)
1267 {
1268 GLint targetIndex;
1269 GET_CURRENT_CONTEXT(ctx);
1270
1271 /*
1272 * The 4.5 core profile spec (30.10.2014) doesn't specify what
1273 * glCreateTextures should do with invalid targets, which was probably an
1274 * oversight. This conforms to the spec for glBindTexture.
1275 */
1276 targetIndex = _mesa_tex_target_to_index(ctx, target);
1277 if (targetIndex < 0) {
1278 _mesa_error(ctx, GL_INVALID_ENUM, "glCreateTextures(target)");
1279 return;
1280 }
1281
1282 create_textures(ctx, target, n, textures, "glCreateTextures");
1283 }
1284
1285 /**
1286 * Check if the given texture object is bound to the current draw or
1287 * read framebuffer. If so, Unbind it.
1288 */
1289 static void
1290 unbind_texobj_from_fbo(struct gl_context *ctx,
1291 struct gl_texture_object *texObj)
1292 {
1293 bool progress = false;
1294
1295 /* Section 4.4.2 (Attaching Images to Framebuffer Objects), subsection
1296 * "Attaching Texture Images to a Framebuffer," of the OpenGL 3.1 spec
1297 * says:
1298 *
1299 * "If a texture object is deleted while its image is attached to one
1300 * or more attachment points in the currently bound framebuffer, then
1301 * it is as if FramebufferTexture* had been called, with a texture of
1302 * zero, for each attachment point to which this image was attached in
1303 * the currently bound framebuffer. In other words, this texture image
1304 * is first detached from all attachment points in the currently bound
1305 * framebuffer. Note that the texture image is specifically not
1306 * detached from any other framebuffer objects. Detaching the texture
1307 * image from any other framebuffer objects is the responsibility of
1308 * the application."
1309 */
1310 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1311 progress = _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, texObj);
1312 }
1313 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1314 && ctx->ReadBuffer != ctx->DrawBuffer) {
1315 progress = _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, texObj)
1316 || progress;
1317 }
1318
1319 if (progress)
1320 /* Vertices are already flushed by _mesa_DeleteTextures */
1321 ctx->NewState |= _NEW_BUFFERS;
1322 }
1323
1324
1325 /**
1326 * Check if the given texture object is bound to any texture image units and
1327 * unbind it if so (revert to default textures).
1328 */
1329 static void
1330 unbind_texobj_from_texunits(struct gl_context *ctx,
1331 struct gl_texture_object *texObj)
1332 {
1333 const gl_texture_index index = texObj->TargetIndex;
1334 GLuint u;
1335
1336 if (texObj->Target == 0) {
1337 /* texture was never bound */
1338 return;
1339 }
1340
1341 assert(index < NUM_TEXTURE_TARGETS);
1342
1343 for (u = 0; u < ctx->Texture.NumCurrentTexUsed; u++) {
1344 struct gl_texture_unit *unit = &ctx->Texture.Unit[u];
1345
1346 if (texObj == unit->CurrentTex[index]) {
1347 /* Bind the default texture for this unit/target */
1348 _mesa_reference_texobj(&unit->CurrentTex[index],
1349 ctx->Shared->DefaultTex[index]);
1350 unit->_BoundTextures &= ~(1 << index);
1351 }
1352 }
1353 }
1354
1355
1356 /**
1357 * Check if the given texture object is bound to any shader image unit
1358 * and unbind it if that's the case.
1359 */
1360 static void
1361 unbind_texobj_from_image_units(struct gl_context *ctx,
1362 struct gl_texture_object *texObj)
1363 {
1364 GLuint i;
1365
1366 for (i = 0; i < ctx->Const.MaxImageUnits; i++) {
1367 struct gl_image_unit *unit = &ctx->ImageUnits[i];
1368
1369 if (texObj == unit->TexObj) {
1370 _mesa_reference_texobj(&unit->TexObj, NULL);
1371 *unit = _mesa_default_image_unit(ctx);
1372 }
1373 }
1374 }
1375
1376
1377 /**
1378 * Unbinds all textures bound to the given texture image unit.
1379 */
1380 static void
1381 unbind_textures_from_unit(struct gl_context *ctx, GLuint unit)
1382 {
1383 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
1384
1385 while (texUnit->_BoundTextures) {
1386 const GLuint index = ffs(texUnit->_BoundTextures) - 1;
1387 struct gl_texture_object *texObj = ctx->Shared->DefaultTex[index];
1388
1389 _mesa_reference_texobj(&texUnit->CurrentTex[index], texObj);
1390
1391 /* Pass BindTexture call to device driver */
1392 if (ctx->Driver.BindTexture)
1393 ctx->Driver.BindTexture(ctx, unit, 0, texObj);
1394
1395 texUnit->_BoundTextures &= ~(1 << index);
1396 ctx->NewState |= _NEW_TEXTURE_OBJECT;
1397 }
1398 }
1399
1400
1401 /**
1402 * Delete named textures.
1403 *
1404 * \param n number of textures to be deleted.
1405 * \param textures array of texture IDs to be deleted.
1406 *
1407 * \sa glDeleteTextures().
1408 *
1409 * If we're about to delete a texture that's currently bound to any
1410 * texture unit, unbind the texture first. Decrement the reference
1411 * count on the texture object and delete it if it's zero.
1412 * Recall that texture objects can be shared among several rendering
1413 * contexts.
1414 */
1415 void GLAPIENTRY
1416 _mesa_DeleteTextures( GLsizei n, const GLuint *textures)
1417 {
1418 GET_CURRENT_CONTEXT(ctx);
1419 GLint i;
1420
1421 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1422 _mesa_debug(ctx, "glDeleteTextures %d\n", n);
1423
1424 if (n < 0) {
1425 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTextures(n < 0)");
1426 return;
1427 }
1428
1429 FLUSH_VERTICES(ctx, 0); /* too complex */
1430
1431 if (n < 0) {
1432 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTextures(n)");
1433 return;
1434 }
1435
1436 if (!textures)
1437 return;
1438
1439 for (i = 0; i < n; i++) {
1440 if (textures[i] > 0) {
1441 struct gl_texture_object *delObj
1442 = _mesa_lookup_texture(ctx, textures[i]);
1443
1444 if (delObj) {
1445 _mesa_lock_texture(ctx, delObj);
1446
1447 /* Check if texture is bound to any framebuffer objects.
1448 * If so, unbind.
1449 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1450 */
1451 unbind_texobj_from_fbo(ctx, delObj);
1452
1453 /* Check if this texture is currently bound to any texture units.
1454 * If so, unbind it.
1455 */
1456 unbind_texobj_from_texunits(ctx, delObj);
1457
1458 /* Check if this texture is currently bound to any shader
1459 * image unit. If so, unbind it.
1460 * See section 3.9.X of GL_ARB_shader_image_load_store.
1461 */
1462 unbind_texobj_from_image_units(ctx, delObj);
1463
1464 _mesa_unlock_texture(ctx, delObj);
1465
1466 ctx->NewState |= _NEW_TEXTURE_OBJECT;
1467
1468 /* The texture _name_ is now free for re-use.
1469 * Remove it from the hash table now.
1470 */
1471 _mesa_HashRemove(ctx->Shared->TexObjects, delObj->Name);
1472
1473 /* Unreference the texobj. If refcount hits zero, the texture
1474 * will be deleted.
1475 */
1476 _mesa_reference_texobj(&delObj, NULL);
1477 }
1478 }
1479 }
1480 }
1481
1482 /**
1483 * This deletes a texObj without altering the hash table.
1484 */
1485 void
1486 _mesa_delete_nameless_texture(struct gl_context *ctx,
1487 struct gl_texture_object *texObj)
1488 {
1489 if (!texObj)
1490 return;
1491
1492 FLUSH_VERTICES(ctx, 0);
1493
1494 _mesa_lock_texture(ctx, texObj);
1495 {
1496 /* Check if texture is bound to any framebuffer objects.
1497 * If so, unbind.
1498 * See section 4.4.2.3 of GL_EXT_framebuffer_object.
1499 */
1500 unbind_texobj_from_fbo(ctx, texObj);
1501
1502 /* Check if this texture is currently bound to any texture units.
1503 * If so, unbind it.
1504 */
1505 unbind_texobj_from_texunits(ctx, texObj);
1506
1507 /* Check if this texture is currently bound to any shader
1508 * image unit. If so, unbind it.
1509 * See section 3.9.X of GL_ARB_shader_image_load_store.
1510 */
1511 unbind_texobj_from_image_units(ctx, texObj);
1512 }
1513 _mesa_unlock_texture(ctx, texObj);
1514
1515 ctx->NewState |= _NEW_TEXTURE_OBJECT;
1516
1517 /* Unreference the texobj. If refcount hits zero, the texture
1518 * will be deleted.
1519 */
1520 _mesa_reference_texobj(&texObj, NULL);
1521 }
1522
1523
1524 /**
1525 * Convert a GL texture target enum such as GL_TEXTURE_2D or GL_TEXTURE_3D
1526 * into the corresponding Mesa texture target index.
1527 * Note that proxy targets are not valid here.
1528 * \return TEXTURE_x_INDEX or -1 if target is invalid
1529 */
1530 int
1531 _mesa_tex_target_to_index(const struct gl_context *ctx, GLenum target)
1532 {
1533 switch (target) {
1534 case GL_TEXTURE_1D:
1535 return _mesa_is_desktop_gl(ctx) ? TEXTURE_1D_INDEX : -1;
1536 case GL_TEXTURE_2D:
1537 return TEXTURE_2D_INDEX;
1538 case GL_TEXTURE_3D:
1539 return ctx->API != API_OPENGLES ? TEXTURE_3D_INDEX : -1;
1540 case GL_TEXTURE_CUBE_MAP:
1541 return ctx->Extensions.ARB_texture_cube_map
1542 ? TEXTURE_CUBE_INDEX : -1;
1543 case GL_TEXTURE_RECTANGLE:
1544 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle
1545 ? TEXTURE_RECT_INDEX : -1;
1546 case GL_TEXTURE_1D_ARRAY:
1547 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array
1548 ? TEXTURE_1D_ARRAY_INDEX : -1;
1549 case GL_TEXTURE_2D_ARRAY:
1550 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_array)
1551 || _mesa_is_gles3(ctx)
1552 ? TEXTURE_2D_ARRAY_INDEX : -1;
1553 case GL_TEXTURE_BUFFER:
1554 return (_mesa_has_ARB_texture_buffer_object(ctx) ||
1555 _mesa_has_OES_texture_buffer(ctx)) ?
1556 TEXTURE_BUFFER_INDEX : -1;
1557 case GL_TEXTURE_EXTERNAL_OES:
1558 return _mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external
1559 ? TEXTURE_EXTERNAL_INDEX : -1;
1560 case GL_TEXTURE_CUBE_MAP_ARRAY:
1561 return _mesa_has_texture_cube_map_array(ctx)
1562 ? TEXTURE_CUBE_ARRAY_INDEX : -1;
1563 case GL_TEXTURE_2D_MULTISAMPLE:
1564 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_multisample) ||
1565 _mesa_is_gles31(ctx)) ? TEXTURE_2D_MULTISAMPLE_INDEX: -1;
1566 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1567 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_multisample) ||
1568 _mesa_is_gles31(ctx))
1569 ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX: -1;
1570 default:
1571 return -1;
1572 }
1573 }
1574
1575
1576 /**
1577 * Do actual texture binding. All error checking should have been done prior
1578 * to calling this function. Note that the texture target (1D, 2D, etc) is
1579 * always specified by the texObj->TargetIndex.
1580 *
1581 * \param unit index of texture unit to update
1582 * \param texObj the new texture object (cannot be NULL)
1583 */
1584 static void
1585 bind_texture(struct gl_context *ctx,
1586 unsigned unit,
1587 struct gl_texture_object *texObj)
1588 {
1589 struct gl_texture_unit *texUnit;
1590 int targetIndex;
1591
1592 assert(unit < ARRAY_SIZE(ctx->Texture.Unit));
1593 texUnit = &ctx->Texture.Unit[unit];
1594
1595 assert(texObj);
1596 assert(valid_texture_object(texObj));
1597
1598 targetIndex = texObj->TargetIndex;
1599 assert(targetIndex >= 0);
1600 assert(targetIndex < NUM_TEXTURE_TARGETS);
1601
1602 /* Check if this texture is only used by this context and is already bound.
1603 * If so, just return. For GL_OES_image_external, rebinding the texture
1604 * always must invalidate cached resources.
1605 */
1606 if (targetIndex != TEXTURE_EXTERNAL_INDEX) {
1607 bool early_out;
1608 mtx_lock(&ctx->Shared->Mutex);
1609 early_out = ((ctx->Shared->RefCount == 1)
1610 && (texObj == texUnit->CurrentTex[targetIndex]));
1611 mtx_unlock(&ctx->Shared->Mutex);
1612 if (early_out) {
1613 return;
1614 }
1615 }
1616
1617 /* flush before changing binding */
1618 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT);
1619
1620 /* If the refcount on the previously bound texture is decremented to
1621 * zero, it'll be deleted here.
1622 */
1623 _mesa_reference_texobj(&texUnit->CurrentTex[targetIndex], texObj);
1624
1625 ctx->Texture.NumCurrentTexUsed = MAX2(ctx->Texture.NumCurrentTexUsed,
1626 unit + 1);
1627
1628 if (texObj->Name != 0)
1629 texUnit->_BoundTextures |= (1 << targetIndex);
1630 else
1631 texUnit->_BoundTextures &= ~(1 << targetIndex);
1632
1633 /* Pass BindTexture call to device driver */
1634 if (ctx->Driver.BindTexture) {
1635 ctx->Driver.BindTexture(ctx, unit, texObj->Target, texObj);
1636 }
1637 }
1638
1639
1640 /**
1641 * Implement glBindTexture(). Do error checking, look-up or create a new
1642 * texture object, then bind it in the current texture unit.
1643 *
1644 * \param target texture target.
1645 * \param texName texture name.
1646 */
1647 void GLAPIENTRY
1648 _mesa_BindTexture( GLenum target, GLuint texName )
1649 {
1650 GET_CURRENT_CONTEXT(ctx);
1651 struct gl_texture_object *newTexObj = NULL;
1652
1653 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1654 _mesa_debug(ctx, "glBindTexture %s %d\n",
1655 _mesa_enum_to_string(target), (GLint) texName);
1656
1657 int targetIndex = _mesa_tex_target_to_index(ctx, target);
1658 if (targetIndex < 0) {
1659 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTexture(target = %s)",
1660 _mesa_enum_to_string(target));
1661 return;
1662 }
1663 assert(targetIndex < NUM_TEXTURE_TARGETS);
1664
1665 /*
1666 * Get pointer to new texture object (newTexObj)
1667 */
1668 if (texName == 0) {
1669 /* Use a default texture object */
1670 newTexObj = ctx->Shared->DefaultTex[targetIndex];
1671 }
1672 else {
1673 /* non-default texture object */
1674 newTexObj = _mesa_lookup_texture(ctx, texName);
1675 if (newTexObj) {
1676 /* error checking */
1677 if (newTexObj->Target != 0 && newTexObj->Target != target) {
1678 /* The named texture object's target doesn't match the
1679 * given target
1680 */
1681 _mesa_error( ctx, GL_INVALID_OPERATION,
1682 "glBindTexture(target mismatch)" );
1683 return;
1684 }
1685 if (newTexObj->Target == 0) {
1686 finish_texture_init(ctx, target, newTexObj, targetIndex);
1687 }
1688 }
1689 else {
1690 if (ctx->API == API_OPENGL_CORE) {
1691 _mesa_error(ctx, GL_INVALID_OPERATION,
1692 "glBindTexture(non-gen name)");
1693 return;
1694 }
1695
1696 /* if this is a new texture id, allocate a texture object now */
1697 newTexObj = ctx->Driver.NewTextureObject(ctx, texName, target);
1698 if (!newTexObj) {
1699 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindTexture");
1700 return;
1701 }
1702
1703 /* and insert it into hash table */
1704 _mesa_HashInsert(ctx->Shared->TexObjects, texName, newTexObj);
1705 }
1706 }
1707
1708 assert(newTexObj->Target == target);
1709 assert(newTexObj->TargetIndex == targetIndex);
1710
1711 bind_texture(ctx, ctx->Texture.CurrentUnit, newTexObj);
1712 }
1713
1714
1715 /**
1716 * OpenGL 4.5 / GL_ARB_direct_state_access glBindTextureUnit().
1717 *
1718 * \param unit texture unit.
1719 * \param texture texture name.
1720 *
1721 * \sa glBindTexture().
1722 *
1723 * If the named texture is 0, this will reset each target for the specified
1724 * texture unit to its default texture.
1725 * If the named texture is not 0 or a recognized texture name, this throws
1726 * GL_INVALID_OPERATION.
1727 */
1728 void GLAPIENTRY
1729 _mesa_BindTextureUnit(GLuint unit, GLuint texture)
1730 {
1731 GET_CURRENT_CONTEXT(ctx);
1732 struct gl_texture_object *texObj;
1733
1734 if (unit >= _mesa_max_tex_unit(ctx)) {
1735 _mesa_error(ctx, GL_INVALID_VALUE, "glBindTextureUnit(unit=%u)", unit);
1736 return;
1737 }
1738
1739 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1740 _mesa_debug(ctx, "glBindTextureUnit %s %d\n",
1741 _mesa_enum_to_string(GL_TEXTURE0+unit), (GLint) texture);
1742
1743 /* Section 8.1 (Texture Objects) of the OpenGL 4.5 core profile spec
1744 * (20141030) says:
1745 * "When texture is zero, each of the targets enumerated at the
1746 * beginning of this section is reset to its default texture for the
1747 * corresponding texture image unit."
1748 */
1749 if (texture == 0) {
1750 unbind_textures_from_unit(ctx, unit);
1751 return;
1752 }
1753
1754 /* Get the non-default texture object */
1755 texObj = _mesa_lookup_texture(ctx, texture);
1756
1757 /* Error checking */
1758 if (!texObj) {
1759 _mesa_error(ctx, GL_INVALID_OPERATION,
1760 "glBindTextureUnit(non-gen name)");
1761 return;
1762 }
1763 if (texObj->Target == 0) {
1764 /* Texture object was gen'd but never bound so the target is not set */
1765 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindTextureUnit(target)");
1766 return;
1767 }
1768 assert(valid_texture_object(texObj));
1769
1770 bind_texture(ctx, unit, texObj);
1771 }
1772
1773
1774 /**
1775 * OpenGL 4.4 / GL_ARB_multi_bind glBindTextures().
1776 */
1777 void GLAPIENTRY
1778 _mesa_BindTextures(GLuint first, GLsizei count, const GLuint *textures)
1779 {
1780 GET_CURRENT_CONTEXT(ctx);
1781 GLint i;
1782
1783 /* The ARB_multi_bind spec says:
1784 *
1785 * "An INVALID_OPERATION error is generated if <first> + <count>
1786 * is greater than the number of texture image units supported
1787 * by the implementation."
1788 */
1789 if (first + count > ctx->Const.MaxCombinedTextureImageUnits) {
1790 _mesa_error(ctx, GL_INVALID_OPERATION,
1791 "glBindTextures(first=%u + count=%d > the value of "
1792 "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS=%u)",
1793 first, count, ctx->Const.MaxCombinedTextureImageUnits);
1794 return;
1795 }
1796
1797 if (textures) {
1798 /* Note that the error semantics for multi-bind commands differ from
1799 * those of other GL commands.
1800 *
1801 * The issues section in the ARB_multi_bind spec says:
1802 *
1803 * "(11) Typically, OpenGL specifies that if an error is generated by
1804 * a command, that command has no effect. This is somewhat
1805 * unfortunate for multi-bind commands, because it would require
1806 * a first pass to scan the entire list of bound objects for
1807 * errors and then a second pass to actually perform the
1808 * bindings. Should we have different error semantics?
1809 *
1810 * RESOLVED: Yes. In this specification, when the parameters for
1811 * one of the <count> binding points are invalid, that binding
1812 * point is not updated and an error will be generated. However,
1813 * other binding points in the same command will be updated if
1814 * their parameters are valid and no other error occurs."
1815 */
1816
1817 _mesa_HashLockMutex(ctx->Shared->TexObjects);
1818
1819 for (i = 0; i < count; i++) {
1820 if (textures[i] != 0) {
1821 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[first + i];
1822 struct gl_texture_object *current = texUnit->_Current;
1823 struct gl_texture_object *texObj;
1824
1825 if (current && current->Name == textures[i])
1826 texObj = current;
1827 else
1828 texObj = _mesa_lookup_texture_locked(ctx, textures[i]);
1829
1830 if (texObj && texObj->Target != 0) {
1831 bind_texture(ctx, first + i, texObj);
1832 } else {
1833 /* The ARB_multi_bind spec says:
1834 *
1835 * "An INVALID_OPERATION error is generated if any value
1836 * in <textures> is not zero or the name of an existing
1837 * texture object (per binding)."
1838 */
1839 _mesa_error(ctx, GL_INVALID_OPERATION,
1840 "glBindTextures(textures[%d]=%u is not zero "
1841 "or the name of an existing texture object)",
1842 i, textures[i]);
1843 }
1844 } else {
1845 unbind_textures_from_unit(ctx, first + i);
1846 }
1847 }
1848
1849 _mesa_HashUnlockMutex(ctx->Shared->TexObjects);
1850 } else {
1851 /* Unbind all textures in the range <first> through <first>+<count>-1 */
1852 for (i = 0; i < count; i++)
1853 unbind_textures_from_unit(ctx, first + i);
1854 }
1855 }
1856
1857
1858 /**
1859 * Set texture priorities.
1860 *
1861 * \param n number of textures.
1862 * \param texName texture names.
1863 * \param priorities corresponding texture priorities.
1864 *
1865 * \sa glPrioritizeTextures().
1866 *
1867 * Looks up each texture in the hash, clamps the corresponding priority between
1868 * 0.0 and 1.0, and calls dd_function_table::PrioritizeTexture.
1869 */
1870 void GLAPIENTRY
1871 _mesa_PrioritizeTextures( GLsizei n, const GLuint *texName,
1872 const GLclampf *priorities )
1873 {
1874 GET_CURRENT_CONTEXT(ctx);
1875 GLint i;
1876
1877 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1878 _mesa_debug(ctx, "glPrioritizeTextures %d\n", n);
1879
1880 FLUSH_VERTICES(ctx, 0);
1881
1882 if (n < 0) {
1883 _mesa_error( ctx, GL_INVALID_VALUE, "glPrioritizeTextures" );
1884 return;
1885 }
1886
1887 if (!priorities)
1888 return;
1889
1890 for (i = 0; i < n; i++) {
1891 if (texName[i] > 0) {
1892 struct gl_texture_object *t = _mesa_lookup_texture(ctx, texName[i]);
1893 if (t) {
1894 t->Priority = CLAMP( priorities[i], 0.0F, 1.0F );
1895 }
1896 }
1897 }
1898
1899 ctx->NewState |= _NEW_TEXTURE_OBJECT;
1900 }
1901
1902
1903
1904 /**
1905 * See if textures are loaded in texture memory.
1906 *
1907 * \param n number of textures to query.
1908 * \param texName array with the texture names.
1909 * \param residences array which will hold the residence status.
1910 *
1911 * \return GL_TRUE if all textures are resident and
1912 * residences is left unchanged,
1913 *
1914 * Note: we assume all textures are always resident
1915 */
1916 GLboolean GLAPIENTRY
1917 _mesa_AreTexturesResident(GLsizei n, const GLuint *texName,
1918 GLboolean *residences)
1919 {
1920 GET_CURRENT_CONTEXT(ctx);
1921 GLboolean allResident = GL_TRUE;
1922 GLint i;
1923 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1924
1925 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1926 _mesa_debug(ctx, "glAreTexturesResident %d\n", n);
1927
1928 if (n < 0) {
1929 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident(n)");
1930 return GL_FALSE;
1931 }
1932
1933 if (!texName || !residences)
1934 return GL_FALSE;
1935
1936 /* We only do error checking on the texture names */
1937 for (i = 0; i < n; i++) {
1938 struct gl_texture_object *t;
1939 if (texName[i] == 0) {
1940 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1941 return GL_FALSE;
1942 }
1943 t = _mesa_lookup_texture(ctx, texName[i]);
1944 if (!t) {
1945 _mesa_error(ctx, GL_INVALID_VALUE, "glAreTexturesResident");
1946 return GL_FALSE;
1947 }
1948 }
1949
1950 return allResident;
1951 }
1952
1953
1954 /**
1955 * See if a name corresponds to a texture.
1956 *
1957 * \param texture texture name.
1958 *
1959 * \return GL_TRUE if texture name corresponds to a texture, or GL_FALSE
1960 * otherwise.
1961 *
1962 * \sa glIsTexture().
1963 *
1964 * Calls _mesa_HashLookup().
1965 */
1966 GLboolean GLAPIENTRY
1967 _mesa_IsTexture( GLuint texture )
1968 {
1969 struct gl_texture_object *t;
1970 GET_CURRENT_CONTEXT(ctx);
1971 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1972
1973 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
1974 _mesa_debug(ctx, "glIsTexture %d\n", texture);
1975
1976 if (!texture)
1977 return GL_FALSE;
1978
1979 t = _mesa_lookup_texture(ctx, texture);
1980
1981 /* IsTexture is true only after object has been bound once. */
1982 return t && t->Target;
1983 }
1984
1985
1986 /**
1987 * Simplest implementation of texture locking: grab the shared tex
1988 * mutex. Examine the shared context state timestamp and if there has
1989 * been a change, set the appropriate bits in ctx->NewState.
1990 *
1991 * This is used to deal with synchronizing things when a texture object
1992 * is used/modified by different contexts (or threads) which are sharing
1993 * the texture.
1994 *
1995 * See also _mesa_lock/unlock_texture() in teximage.h
1996 */
1997 void
1998 _mesa_lock_context_textures( struct gl_context *ctx )
1999 {
2000 mtx_lock(&ctx->Shared->TexMutex);
2001
2002 if (ctx->Shared->TextureStateStamp != ctx->TextureStateTimestamp) {
2003 ctx->NewState |= _NEW_TEXTURE_OBJECT;
2004 ctx->TextureStateTimestamp = ctx->Shared->TextureStateStamp;
2005 }
2006 }
2007
2008
2009 void
2010 _mesa_unlock_context_textures( struct gl_context *ctx )
2011 {
2012 assert(ctx->Shared->TextureStateStamp == ctx->TextureStateTimestamp);
2013 mtx_unlock(&ctx->Shared->TexMutex);
2014 }
2015
2016
2017 void GLAPIENTRY
2018 _mesa_InvalidateTexSubImage(GLuint texture, GLint level, GLint xoffset,
2019 GLint yoffset, GLint zoffset, GLsizei width,
2020 GLsizei height, GLsizei depth)
2021 {
2022 struct gl_texture_object *t;
2023 struct gl_texture_image *image;
2024 GET_CURRENT_CONTEXT(ctx);
2025
2026 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2027 _mesa_debug(ctx, "glInvalidateTexSubImage %d\n", texture);
2028
2029 t = invalidate_tex_image_error_check(ctx, texture, level,
2030 "glInvalidateTexSubImage");
2031
2032 /* The GL_ARB_invalidate_subdata spec says:
2033 *
2034 * "...the specified subregion must be between -<b> and <dim>+<b> where
2035 * <dim> is the size of the dimension of the texture image, and <b> is
2036 * the size of the border of that texture image, otherwise
2037 * INVALID_VALUE is generated (border is not applied to dimensions that
2038 * don't exist in a given texture target)."
2039 */
2040 image = t->Image[0][level];
2041 if (image) {
2042 int xBorder;
2043 int yBorder;
2044 int zBorder;
2045 int imageWidth;
2046 int imageHeight;
2047 int imageDepth;
2048
2049 /* The GL_ARB_invalidate_subdata spec says:
2050 *
2051 * "For texture targets that don't have certain dimensions, this
2052 * command treats those dimensions as having a size of 1. For
2053 * example, to invalidate a portion of a two-dimensional texture,
2054 * the application would use <zoffset> equal to zero and <depth>
2055 * equal to one."
2056 */
2057 switch (t->Target) {
2058 case GL_TEXTURE_BUFFER:
2059 xBorder = 0;
2060 yBorder = 0;
2061 zBorder = 0;
2062 imageWidth = 1;
2063 imageHeight = 1;
2064 imageDepth = 1;
2065 break;
2066 case GL_TEXTURE_1D:
2067 xBorder = image->Border;
2068 yBorder = 0;
2069 zBorder = 0;
2070 imageWidth = image->Width;
2071 imageHeight = 1;
2072 imageDepth = 1;
2073 break;
2074 case GL_TEXTURE_1D_ARRAY:
2075 xBorder = image->Border;
2076 yBorder = 0;
2077 zBorder = 0;
2078 imageWidth = image->Width;
2079 imageHeight = image->Height;
2080 imageDepth = 1;
2081 break;
2082 case GL_TEXTURE_2D:
2083 case GL_TEXTURE_CUBE_MAP:
2084 case GL_TEXTURE_RECTANGLE:
2085 case GL_TEXTURE_2D_MULTISAMPLE:
2086 xBorder = image->Border;
2087 yBorder = image->Border;
2088 zBorder = 0;
2089 imageWidth = image->Width;
2090 imageHeight = image->Height;
2091 imageDepth = 1;
2092 break;
2093 case GL_TEXTURE_2D_ARRAY:
2094 case GL_TEXTURE_CUBE_MAP_ARRAY:
2095 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2096 xBorder = image->Border;
2097 yBorder = image->Border;
2098 zBorder = 0;
2099 imageWidth = image->Width;
2100 imageHeight = image->Height;
2101 imageDepth = image->Depth;
2102 break;
2103 case GL_TEXTURE_3D:
2104 xBorder = image->Border;
2105 yBorder = image->Border;
2106 zBorder = image->Border;
2107 imageWidth = image->Width;
2108 imageHeight = image->Height;
2109 imageDepth = image->Depth;
2110 break;
2111 default:
2112 assert(!"Should not get here.");
2113 xBorder = 0;
2114 yBorder = 0;
2115 zBorder = 0;
2116 imageWidth = 0;
2117 imageHeight = 0;
2118 imageDepth = 0;
2119 break;
2120 }
2121
2122 if (xoffset < -xBorder) {
2123 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(xoffset)");
2124 return;
2125 }
2126
2127 if (xoffset + width > imageWidth + xBorder) {
2128 _mesa_error(ctx, GL_INVALID_VALUE,
2129 "glInvalidateSubTexImage(xoffset+width)");
2130 return;
2131 }
2132
2133 if (yoffset < -yBorder) {
2134 _mesa_error(ctx, GL_INVALID_VALUE, "glInvalidateSubTexImage(yoffset)");
2135 return;
2136 }
2137
2138 if (yoffset + height > imageHeight + yBorder) {
2139 _mesa_error(ctx, GL_INVALID_VALUE,
2140 "glInvalidateSubTexImage(yoffset+height)");
2141 return;
2142 }
2143
2144 if (zoffset < -zBorder) {
2145 _mesa_error(ctx, GL_INVALID_VALUE,
2146 "glInvalidateSubTexImage(zoffset)");
2147 return;
2148 }
2149
2150 if (zoffset + depth > imageDepth + zBorder) {
2151 _mesa_error(ctx, GL_INVALID_VALUE,
2152 "glInvalidateSubTexImage(zoffset+depth)");
2153 return;
2154 }
2155 }
2156
2157 /* We don't actually do anything for this yet. Just return after
2158 * validating the parameters and generating the required errors.
2159 */
2160 return;
2161 }
2162
2163
2164 void GLAPIENTRY
2165 _mesa_InvalidateTexImage(GLuint texture, GLint level)
2166 {
2167 GET_CURRENT_CONTEXT(ctx);
2168
2169 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
2170 _mesa_debug(ctx, "glInvalidateTexImage(%d, %d)\n", texture, level);
2171
2172 invalidate_tex_image_error_check(ctx, texture, level,
2173 "glInvalidateTexImage");
2174
2175 /* We don't actually do anything for this yet. Just return after
2176 * validating the parameters and generating the required errors.
2177 */
2178 return;
2179 }
2180
2181 /*@}*/