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