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