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