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