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