summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/texturebindless.c
blob: a56d6e3b370626c6b805c2028cfc6b6c948ff626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
/*
 * Copyright © 2017 Valve Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "glheader.h"
#include "context.h"
#include "enums.h"
#include "imports.h"
#include "hash.h"
#include "mtypes.h"
#include "shaderimage.h"
#include "teximage.h"
#include "texobj.h"
#include "texturebindless.h"

#include "util/hash_table.h"

/**
 * Return the gl_texture_handle_object for a given 64-bit handle.
 */
static struct gl_texture_handle_object *
lookup_texture_handle(struct gl_context *ctx, GLuint64 id)
{
   struct gl_texture_handle_object *texHandleObj;

   mtx_lock(&ctx->Shared->HandlesMutex);
   texHandleObj = (struct gl_texture_handle_object *)
      _mesa_hash_table_u64_search(ctx->Shared->TextureHandles, id);
   mtx_unlock(&ctx->Shared->HandlesMutex);

   return texHandleObj;
}

/**
 * Return the gl_image_handle_object for a given 64-bit handle.
 */
static struct gl_image_handle_object *
lookup_image_handle(struct gl_context *ctx, GLuint64 id)
{
   struct gl_image_handle_object *imgHandleObj;

   mtx_lock(&ctx->Shared->HandlesMutex);
   imgHandleObj = (struct gl_image_handle_object *)
      _mesa_hash_table_u64_search(ctx->Shared->ImageHandles, id);
   mtx_unlock(&ctx->Shared->HandlesMutex);

   return imgHandleObj;
}

/**
 * Delete a texture handle in the shared state.
 */
static void
delete_texture_handle(struct gl_context *ctx, GLuint64 id)
{
   mtx_lock(&ctx->Shared->HandlesMutex);
   _mesa_hash_table_u64_remove(ctx->Shared->TextureHandles, id);
   mtx_unlock(&ctx->Shared->HandlesMutex);

   ctx->Driver.DeleteTextureHandle(ctx, id);
}

/**
 * Delete an image handle in the shared state.
 */
static void
delete_image_handle(struct gl_context *ctx, GLuint64 id)
{
   mtx_lock(&ctx->Shared->HandlesMutex);
   _mesa_hash_table_u64_remove(ctx->Shared->ImageHandles, id);
   mtx_unlock(&ctx->Shared->HandlesMutex);

   ctx->Driver.DeleteImageHandle(ctx, id);
}

/**
 * Return TRUE if the texture handle is resident in the current context.
 */
static inline bool
is_texture_handle_resident(struct gl_context *ctx, GLuint64 handle)
{
   return _mesa_hash_table_u64_search(ctx->ResidentTextureHandles,
                                      handle) != NULL;
}

/**
 * Return TRUE if the image handle is resident in the current context.
 */
static inline bool
is_image_handle_resident(struct gl_context *ctx, GLuint64 handle)
{
   return _mesa_hash_table_u64_search(ctx->ResidentImageHandles,
                                      handle) != NULL;
}

/**
 * Make a texture handle resident/non-resident in the current context.
 */
static void
make_texture_handle_resident(struct gl_context *ctx,
                             struct gl_texture_handle_object *texHandleObj,
                             bool resident)
{
   struct gl_sampler_object *sampObj = NULL;
   struct gl_texture_object *texObj = NULL;
   GLuint64 handle = texHandleObj->handle;

   if (resident) {
      assert(!is_texture_handle_resident(ctx, handle));

      _mesa_hash_table_u64_insert(ctx->ResidentTextureHandles, handle,
                                  texHandleObj);

      ctx->Driver.MakeTextureHandleResident(ctx, handle, GL_TRUE);

      /* Reference the texture object (and the separate sampler if needed) to
       * be sure it won't be deleted until it is not bound anywhere and there
       * are no handles using the object that are resident in any context.
       */
      _mesa_reference_texobj(&texObj, texHandleObj->texObj);
      if (texHandleObj->sampObj)
         _mesa_reference_sampler_object(ctx, &sampObj, texHandleObj->sampObj);
   } else {
      assert(is_texture_handle_resident(ctx, handle));

      _mesa_hash_table_u64_remove(ctx->ResidentTextureHandles, handle);

      ctx->Driver.MakeTextureHandleResident(ctx, handle, GL_FALSE);

      /* Unreference the texture object but keep the pointer intact, if
       * refcount hits zero, the texture and all handles will be deleted.
       */
      texObj = texHandleObj->texObj;
      _mesa_reference_texobj(&texObj, NULL);

      /* Unreference the separate sampler object but keep the pointer intact,
       * if refcount hits zero, the sampler and all handles will be deleted.
       */
      if (texHandleObj->sampObj) {
         sampObj = texHandleObj->sampObj;
         _mesa_reference_sampler_object(ctx, &sampObj, NULL);
      }
   }
}

/**
 * Make an image handle resident/non-resident in the current context.
 */
static void
make_image_handle_resident(struct gl_context *ctx,
                           struct gl_image_handle_object *imgHandleObj,
                           GLenum access, bool resident)
{
   struct gl_texture_object *texObj = NULL;
   GLuint64 handle = imgHandleObj->handle;

   if (resident) {
      assert(!is_image_handle_resident(ctx, handle));

      _mesa_hash_table_u64_insert(ctx->ResidentImageHandles, handle,
                                  imgHandleObj);

      ctx->Driver.MakeImageHandleResident(ctx, handle, access, GL_TRUE);

      /* Reference the texture object to be sure it won't be deleted until it
       * is not bound anywhere and there are no handles using the object that
       * are resident in any context.
       */
      _mesa_reference_texobj(&texObj, imgHandleObj->imgObj.TexObj);
   } else {
      assert(is_image_handle_resident(ctx, handle));

      _mesa_hash_table_u64_remove(ctx->ResidentImageHandles, handle);

      ctx->Driver.MakeImageHandleResident(ctx, handle, access, GL_FALSE);

      /* Unreference the texture object but keep the pointer intact, if
       * refcount hits zero, the texture and all handles will be deleted.
       */
      texObj = imgHandleObj->imgObj.TexObj;
      _mesa_reference_texobj(&texObj, NULL);
   }
}

static struct gl_texture_handle_object *
find_texhandleobj(struct gl_texture_object *texObj,
                  struct gl_sampler_object *sampObj)
{
   util_dynarray_foreach(&texObj->SamplerHandles,
                         struct gl_texture_handle_object *, texHandleObj) {
      if ((*texHandleObj)->sampObj == sampObj)
         return *texHandleObj;
   }
   return NULL;
}

static GLuint64
get_texture_handle(struct gl_context *ctx, struct gl_texture_object *texObj,
                   struct gl_sampler_object *sampObj)
{
   bool separate_sampler = &texObj->Sampler != sampObj;
   struct gl_texture_handle_object *texHandleObj;
   GLuint64 handle;

   /* The ARB_bindless_texture spec says:
    *
    * "The handle for each texture or texture/sampler pair is unique; the same
    *  handle will be returned if GetTextureHandleARB is called multiple times
    *  for the same texture or if GetTextureSamplerHandleARB is called multiple
    *  times for the same texture/sampler pair."
    */
   mtx_lock(&ctx->Shared->HandlesMutex);
   texHandleObj = find_texhandleobj(texObj, separate_sampler ? sampObj : NULL);
   if (texHandleObj) {
      mtx_unlock(&ctx->Shared->HandlesMutex);
      return texHandleObj->handle;
   }

   /* Request a new texture handle from the driver. */
   handle = ctx->Driver.NewTextureHandle(ctx, texObj, sampObj);
   if (!handle) {
      mtx_unlock(&ctx->Shared->HandlesMutex);
      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexture*HandleARB()");
      return 0;
   }

   texHandleObj = CALLOC_STRUCT(gl_texture_handle_object);
   if (!texHandleObj) {
      mtx_unlock(&ctx->Shared->HandlesMutex);
      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexture*HandleARB()");
      return 0;
   }

   /* Store the handle into the texture object. */
   texHandleObj->texObj = texObj;
   texHandleObj->sampObj = separate_sampler ? sampObj : NULL;
   texHandleObj->handle = handle;
   util_dynarray_append(&texObj->SamplerHandles,
                        struct gl_texture_handle_object *, texHandleObj);

   if (separate_sampler) {
      /* Store the handle into the separate sampler if needed. */
      util_dynarray_append(&sampObj->Handles,
                           struct gl_texture_handle_object *, texHandleObj);
   }

   /* When referenced by one or more handles, texture objects are immutable. */
   texObj->HandleAllocated = true;
   if (texObj->Target == GL_TEXTURE_BUFFER)
      texObj->BufferObject->HandleAllocated = true;
   sampObj->HandleAllocated = true;

   /* Store the handle in the shared state for all contexts. */
   _mesa_hash_table_u64_insert(ctx->Shared->TextureHandles, handle,
                               texHandleObj);
   mtx_unlock(&ctx->Shared->HandlesMutex);

   return handle;
}

static struct gl_image_handle_object *
find_imghandleobj(struct gl_texture_object *texObj, GLint level,
                  GLboolean layered, GLint layer, GLenum format)
{
   util_dynarray_foreach(&texObj->ImageHandles,
                         struct gl_image_handle_object *, imgHandleObj) {
      struct gl_image_unit *u = &(*imgHandleObj)->imgObj;

      if (u->TexObj == texObj && u->Level == level && u->Layered == layered &&
          u->Layer == layer && u->Format == format)
         return *imgHandleObj;
   }
   return NULL;
}

static GLuint64
get_image_handle(struct gl_context *ctx, struct gl_texture_object *texObj,
                 GLint level, GLboolean layered, GLint layer, GLenum format)
{
   struct gl_image_handle_object *imgHandleObj;
   struct gl_image_unit imgObj;
   GLuint64 handle;

   /* The ARB_bindless_texture spec says:
    *
    * "The handle returned for each combination of <texture>, <level>,
    * <layered>, <layer>, and <format> is unique; the same handle will be
    * returned if GetImageHandleARB is called multiple times with the same
    * parameters."
    */
   mtx_lock(&ctx->Shared->HandlesMutex);
   imgHandleObj = find_imghandleobj(texObj, level, layered, layer, format);
   if (imgHandleObj) {
      mtx_unlock(&ctx->Shared->HandlesMutex);
      return imgHandleObj->handle;
   }

   imgObj.TexObj = texObj; /* weak reference */
   imgObj.Level = level;
   imgObj.Access = GL_READ_WRITE;
   imgObj.Format = format;
   imgObj._ActualFormat = _mesa_get_shader_image_format(format);

   if (_mesa_tex_target_is_layered(texObj->Target)) {
      imgObj.Layered = layered;
      imgObj.Layer = layer;
      imgObj._Layer = (imgObj.Layered ? 0 : imgObj.Layer);
   } else {
      imgObj.Layered = GL_FALSE;
      imgObj.Layer = 0;
   }

   /* Request a new image handle from the driver. */
   handle = ctx->Driver.NewImageHandle(ctx, &imgObj);
   if (!handle) {
      mtx_unlock(&ctx->Shared->HandlesMutex);
      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetImageHandleARB()");
      return 0;
   }

   imgHandleObj = CALLOC_STRUCT(gl_image_handle_object);
   if (!imgHandleObj) {
      mtx_unlock(&ctx->Shared->HandlesMutex);
      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetImageHandleARB()");
      return 0;
   }

   /* Store the handle into the texture object. */
   memcpy(&imgHandleObj->imgObj, &imgObj, sizeof(struct gl_image_unit));
   imgHandleObj->handle = handle;
   util_dynarray_append(&texObj->ImageHandles,
                        struct gl_image_handle_object *, imgHandleObj);

   /* When referenced by one or more handles, texture objects are immutable. */
   texObj->HandleAllocated = true;
   if (texObj->Target == GL_TEXTURE_BUFFER)
      texObj->BufferObject->HandleAllocated = true;
   texObj->Sampler.HandleAllocated = true;

   /* Store the handle in the shared state for all contexts. */
   _mesa_hash_table_u64_insert(ctx->Shared->ImageHandles, handle, imgHandleObj);
   mtx_unlock(&ctx->Shared->HandlesMutex);

   return handle;
}

/**
 * Init/free per-context resident handles.
 */
void
_mesa_init_resident_handles(struct gl_context *ctx)
{
   ctx->ResidentTextureHandles = _mesa_hash_table_u64_create(NULL);
   ctx->ResidentImageHandles = _mesa_hash_table_u64_create(NULL);
}

void
_mesa_free_resident_handles(struct gl_context *ctx)
{
   _mesa_hash_table_u64_destroy(ctx->ResidentTextureHandles, NULL);
   _mesa_hash_table_u64_destroy(ctx->ResidentImageHandles, NULL);
}

/**
 * Init/free shared allocated handles.
 */
void
_mesa_init_shared_handles(struct gl_shared_state *shared)
{
   shared->TextureHandles = _mesa_hash_table_u64_create(NULL);
   shared->ImageHandles = _mesa_hash_table_u64_create(NULL);
   mtx_init(&shared->HandlesMutex, mtx_recursive);
}

void
_mesa_free_shared_handles(struct gl_shared_state *shared)
{
   _mesa_hash_table_u64_destroy(shared->TextureHandles, NULL);
   _mesa_hash_table_u64_destroy(shared->ImageHandles, NULL);
   mtx_destroy(&shared->HandlesMutex);
}

/**
 * Init/free texture/image handles per-texture object.
 */
void
_mesa_init_texture_handles(struct gl_texture_object *texObj)
{
   util_dynarray_init(&texObj->SamplerHandles, NULL);
   util_dynarray_init(&texObj->ImageHandles, NULL);
}

void
_mesa_make_texture_handles_non_resident(struct gl_context *ctx,
                                        struct gl_texture_object *texObj)
{
   mtx_lock(&ctx->Shared->HandlesMutex);

   /* Texture handles */
   util_dynarray_foreach(&texObj->SamplerHandles,
                         struct gl_texture_handle_object *, texHandleObj) {
      if (is_texture_handle_resident(ctx, (*texHandleObj)->handle))
         make_texture_handle_resident(ctx, *texHandleObj, false);
   }

   /* Image handles */
   util_dynarray_foreach(&texObj->ImageHandles,
                         struct gl_image_handle_object *, imgHandleObj) {
      if (is_image_handle_resident(ctx, (*imgHandleObj)->handle))
         make_image_handle_resident(ctx, *imgHandleObj, GL_READ_ONLY, false);
   }

   mtx_unlock(&ctx->Shared->HandlesMutex);
}

void
_mesa_delete_texture_handles(struct gl_context *ctx,
                             struct gl_texture_object *texObj)
{
   /* Texture handles */
   util_dynarray_foreach(&texObj->SamplerHandles,
                         struct gl_texture_handle_object *, texHandleObj) {
      struct gl_sampler_object *sampObj = (*texHandleObj)->sampObj;

      if (sampObj) {
         /* Delete the handle in the separate sampler object. */
         util_dynarray_delete_unordered(&sampObj->Handles,
                                        struct gl_texture_handle_object *,
                                        *texHandleObj);
      }
      delete_texture_handle(ctx, (*texHandleObj)->handle);
      free(*texHandleObj);
   }
   util_dynarray_fini(&texObj->SamplerHandles);

   /* Image handles */
   util_dynarray_foreach(&texObj->ImageHandles,
                         struct gl_image_handle_object *, imgHandleObj) {
      delete_image_handle(ctx, (*imgHandleObj)->handle);
      free(*imgHandleObj);
   }
   util_dynarray_fini(&texObj->ImageHandles);
}

/**
 * Init/free texture handles per-sampler object.
 */
void
_mesa_init_sampler_handles(struct gl_sampler_object *sampObj)
{
   util_dynarray_init(&sampObj->Handles, NULL);
}

void
_mesa_delete_sampler_handles(struct gl_context *ctx,
                             struct gl_sampler_object *sampObj)
{
   util_dynarray_foreach(&sampObj->Handles,
                         struct gl_texture_handle_object *, texHandleObj) {
      struct gl_texture_object *texObj = (*texHandleObj)->texObj;

      /* Delete the handle in the texture object. */
      util_dynarray_delete_unordered(&texObj->SamplerHandles,
                                     struct gl_texture_handle_object *,
                                     *texHandleObj);

      delete_texture_handle(ctx, (*texHandleObj)->handle);
      free(*texHandleObj);
   }
   util_dynarray_fini(&sampObj->Handles);
}

static GLboolean
is_sampler_border_color_valid(struct gl_sampler_object *samp)
{
   static const GLfloat valid_float_border_colors[4][4] = {
      { 0.0, 0.0, 0.0, 0.0 },
      { 0.0, 0.0, 0.0, 1.0 },
      { 1.0, 1.0, 1.0, 0.0 },
      { 1.0, 1.0, 1.0, 1.0 },
   };
   static const GLint valid_integer_border_colors[4][4] = {
      { 0, 0, 0, 0 },
      { 0, 0, 0, 1 },
      { 1, 1, 1, 0 },
      { 1, 1, 1, 1 },
   };
   size_t size = sizeof(samp->BorderColor.ui);

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION is generated if the border color (taken from
    *  the embedded sampler for GetTextureHandleARB or from the <sampler> for
    *  GetTextureSamplerHandleARB) is not one of the following allowed values.
    *  If the texture's base internal format is signed or unsigned integer,
    *  allowed values are (0,0,0,0), (0,0,0,1), (1,1,1,0), and (1,1,1,1). If
    *  the base internal format is not integer, allowed values are
    *  (0.0,0.0,0.0,0.0), (0.0,0.0,0.0,1.0), (1.0,1.0,1.0,0.0), and
    *  (1.0,1.0,1.0,1.0)."
    */
   if (!memcmp(samp->BorderColor.f, valid_float_border_colors[0], size) ||
       !memcmp(samp->BorderColor.f, valid_float_border_colors[1], size) ||
       !memcmp(samp->BorderColor.f, valid_float_border_colors[2], size) ||
       !memcmp(samp->BorderColor.f, valid_float_border_colors[3], size))
      return GL_TRUE;

   if (!memcmp(samp->BorderColor.ui, valid_integer_border_colors[0], size) ||
       !memcmp(samp->BorderColor.ui, valid_integer_border_colors[1], size) ||
       !memcmp(samp->BorderColor.ui, valid_integer_border_colors[2], size) ||
       !memcmp(samp->BorderColor.ui, valid_integer_border_colors[3], size))
      return GL_TRUE;

   return GL_FALSE;
}

GLuint64 GLAPIENTRY
_mesa_GetTextureHandleARB_no_error(GLuint texture)
{
   struct gl_texture_object *texObj;

   GET_CURRENT_CONTEXT(ctx);

   texObj = _mesa_lookup_texture(ctx, texture);
   if (!_mesa_is_texture_complete(texObj, &texObj->Sampler))
      _mesa_test_texobj_completeness(ctx, texObj);

   return get_texture_handle(ctx, texObj, &texObj->Sampler);
}

GLuint64 GLAPIENTRY
_mesa_GetTextureHandleARB(GLuint texture)
{
   struct gl_texture_object *texObj = NULL;

   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_bindless_texture(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glGetTextureHandleARB(unsupported)");
      return 0;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_VALUE is generated by GetTextureHandleARB or
    *  GetTextureSamplerHandleARB if <texture> is zero or not the name of an
    *  existing texture object."
    */
   if (texture > 0)
      texObj = _mesa_lookup_texture(ctx, texture);

   if (!texObj) {
      _mesa_error(ctx, GL_INVALID_VALUE, "glGetTextureHandleARB(texture)");
      return 0;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION is generated by GetTextureHandleARB or
    *  GetTextureSamplerHandleARB if the texture object specified by <texture>
    *  is not complete."
    */
   if (!_mesa_is_texture_complete(texObj, &texObj->Sampler)) {
      _mesa_test_texobj_completeness(ctx, texObj);
      if (!_mesa_is_texture_complete(texObj, &texObj->Sampler)) {
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "glGetTextureHandleARB(incomplete texture)");
         return 0;
      }
   }

   if (!is_sampler_border_color_valid(&texObj->Sampler)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glGetTextureHandleARB(invalid border color)");
      return 0;
   }

   return get_texture_handle(ctx, texObj, &texObj->Sampler);
}

GLuint64 GLAPIENTRY
_mesa_GetTextureSamplerHandleARB_no_error(GLuint texture, GLuint sampler)
{
   struct gl_texture_object *texObj;
   struct gl_sampler_object *sampObj;

   GET_CURRENT_CONTEXT(ctx);

   texObj = _mesa_lookup_texture(ctx, texture);
   sampObj = _mesa_lookup_samplerobj(ctx, sampler);

   if (!_mesa_is_texture_complete(texObj, sampObj))
      _mesa_test_texobj_completeness(ctx, texObj);

   return get_texture_handle(ctx, texObj, sampObj);
}

GLuint64 GLAPIENTRY
_mesa_GetTextureSamplerHandleARB(GLuint texture, GLuint sampler)
{
   struct gl_texture_object *texObj = NULL;
   struct gl_sampler_object *sampObj;

   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_bindless_texture(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glGetTextureSamplerHandleARB(unsupported)");
      return 0;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_VALUE is generated by GetTextureHandleARB or
    *  GetTextureSamplerHandleARB if <texture> is zero or not the name of an
    *  existing texture object."
    */
   if (texture > 0)
      texObj = _mesa_lookup_texture(ctx, texture);

   if (!texObj) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetTextureSamplerHandleARB(texture)");
      return 0;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_VALUE is generated by GetTextureSamplerHandleARB if
    *  <sampler> is zero or is not the name of an existing sampler object."
    */
   sampObj = _mesa_lookup_samplerobj(ctx, sampler);
   if (!sampObj) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetTextureSamplerHandleARB(sampler)");
      return 0;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION is generated by GetTextureHandleARB or
    *  GetTextureSamplerHandleARB if the texture object specified by <texture>
    *  is not complete."
    */
   if (!_mesa_is_texture_complete(texObj, sampObj)) {
      _mesa_test_texobj_completeness(ctx, texObj);
      if (!_mesa_is_texture_complete(texObj, sampObj)) {
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "glGetTextureSamplerHandleARB(incomplete texture)");
         return 0;
      }
   }

   if (!is_sampler_border_color_valid(sampObj)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glGetTextureSamplerHandleARB(invalid border color)");
      return 0;
   }

   return get_texture_handle(ctx, texObj, sampObj);
}

void GLAPIENTRY
_mesa_MakeTextureHandleResidentARB_no_error(GLuint64 handle)
{
   struct gl_texture_handle_object *texHandleObj;

   GET_CURRENT_CONTEXT(ctx);

   texHandleObj = lookup_texture_handle(ctx, handle);
   make_texture_handle_resident(ctx, texHandleObj, true);
}

void GLAPIENTRY
_mesa_MakeTextureHandleResidentARB(GLuint64 handle)
{
   struct gl_texture_handle_object *texHandleObj;

   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_bindless_texture(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeTextureHandleResidentARB(unsupported)");
      return;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION is generated by MakeTextureHandleResidentARB
    *  if <handle> is not a valid texture handle, or if <handle> is already
    *  resident in the current GL context."
    */
   texHandleObj = lookup_texture_handle(ctx, handle);
   if (!texHandleObj) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeTextureHandleResidentARB(handle)");
      return;
   }

   if (is_texture_handle_resident(ctx, handle)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeTextureHandleResidentARB(already resident)");
      return;
   }

   make_texture_handle_resident(ctx, texHandleObj, true);
}

void GLAPIENTRY
_mesa_MakeTextureHandleNonResidentARB_no_error(GLuint64 handle)
{
   struct gl_texture_handle_object *texHandleObj;

   GET_CURRENT_CONTEXT(ctx);

   texHandleObj = lookup_texture_handle(ctx, handle);
   make_texture_handle_resident(ctx, texHandleObj, false);
}

void GLAPIENTRY
_mesa_MakeTextureHandleNonResidentARB(GLuint64 handle)
{
   struct gl_texture_handle_object *texHandleObj;

   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_bindless_texture(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeTextureHandleNonResidentARB(unsupported)");
      return;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION is generated by
    *  MakeTextureHandleNonResidentARB if <handle> is not a valid texture
    *  handle, or if <handle> is not resident in the current GL context."
    */
   texHandleObj = lookup_texture_handle(ctx, handle);
   if (!texHandleObj) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeTextureHandleNonResidentARB(handle)");
      return;
   }

   if (!is_texture_handle_resident(ctx, handle)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeTextureHandleNonResidentARB(not resident)");
      return;
   }

   make_texture_handle_resident(ctx, texHandleObj, false);
}

GLuint64 GLAPIENTRY
_mesa_GetImageHandleARB(GLuint texture, GLint level, GLboolean layered,
                        GLint layer, GLenum format)
{
   struct gl_texture_object *texObj = NULL;

   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_bindless_texture(ctx) ||
       !_mesa_has_ARB_shader_image_load_store(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glGetImageHandleARB(unsupported)");
      return 0;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_VALUE is generated by GetImageHandleARB if <texture>
    *  is zero or not the name of an existing texture object, if the image for
    *  <level> does not existing in <texture>, or if <layered> is FALSE and
    *  <layer> is greater than or equal to the number of layers in the image at
    *  <level>."
    */
   if (texture > 0)
      texObj = _mesa_lookup_texture(ctx, texture);

   if (!texObj) {
      _mesa_error(ctx, GL_INVALID_VALUE, "glGetImageHandleARB(texture)");
      return 0;
   }

   if (level < 0 || level >= _mesa_max_texture_levels(ctx, texObj->Target)) {
      _mesa_error(ctx, GL_INVALID_VALUE, "glGetImageHandleARB(level)");
      return 0;
   }

   if (!layered && layer > _mesa_get_texture_layers(texObj, level)) {
      _mesa_error(ctx, GL_INVALID_VALUE, "glGetImageHandleARB(layer)");
      return 0;
   }

   if (!_mesa_is_shader_image_format_supported(ctx, format)) {
      _mesa_error(ctx, GL_INVALID_VALUE, "glGetImageHandleARB(format)");
      return 0;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION is generated by GetImageHandleARB if the
    *  texture object <texture> is not complete or if <layered> is TRUE and
    *  <texture> is not a three-dimensional, one-dimensional array, two
    *  dimensional array, cube map, or cube map array texture."
    */
   if (!_mesa_is_texture_complete(texObj, &texObj->Sampler)) {
      _mesa_test_texobj_completeness(ctx, texObj);
      if (!_mesa_is_texture_complete(texObj, &texObj->Sampler)) {
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "glGetImageHandleARB(incomplete texture)");
         return 0;
      }
   }

   if (layered && !_mesa_tex_target_is_layered(texObj->Target)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glGetImageHandleARB(not layered)");
      return 0;
   }

   return get_image_handle(ctx, texObj, level, layered, layer, format);
}

void GLAPIENTRY
_mesa_MakeImageHandleResidentARB_no_error(GLuint64 handle, GLenum access)
{
   struct gl_image_handle_object *imgHandleObj;

   GET_CURRENT_CONTEXT(ctx);

   imgHandleObj = lookup_image_handle(ctx, handle);
   make_image_handle_resident(ctx, imgHandleObj, access, true);
}

void GLAPIENTRY
_mesa_MakeImageHandleResidentARB(GLuint64 handle, GLenum access)
{
   struct gl_image_handle_object *imgHandleObj;

   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_bindless_texture(ctx) ||
       !_mesa_has_ARB_shader_image_load_store(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeImageHandleResidentARB(unsupported)");
      return;
   }

   if (access != GL_READ_ONLY &&
       access != GL_WRITE_ONLY &&
       access != GL_READ_WRITE) {
      _mesa_error(ctx, GL_INVALID_ENUM,
                  "glMakeImageHandleResidentARB(access)");
      return;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION is generated by MakeImageHandleResidentARB
    *  if <handle> is not a valid image handle, or if <handle> is already
    *  resident in the current GL context."
    */
   imgHandleObj = lookup_image_handle(ctx, handle);
   if (!imgHandleObj) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeImageHandleResidentARB(handle)");
      return;
   }

   if (is_image_handle_resident(ctx, handle)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeImageHandleResidentARB(already resident)");
      return;
   }

   make_image_handle_resident(ctx, imgHandleObj, access, true);
}

void GLAPIENTRY
_mesa_MakeImageHandleNonResidentARB_no_error(GLuint64 handle)
{
   struct gl_image_handle_object *imgHandleObj;

   GET_CURRENT_CONTEXT(ctx);

   imgHandleObj = lookup_image_handle(ctx, handle);
   make_image_handle_resident(ctx, imgHandleObj, GL_READ_ONLY, false);
}

void GLAPIENTRY
_mesa_MakeImageHandleNonResidentARB(GLuint64 handle)
{
   struct gl_image_handle_object *imgHandleObj;

   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_bindless_texture(ctx) ||
       !_mesa_has_ARB_shader_image_load_store(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeImageHandleNonResidentARB(unsupported)");
      return;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION is generated by
    *  MakeImageHandleNonResidentARB if <handle> is not a valid image handle,
    *  or if <handle> is not resident in the current GL context."
    */
   imgHandleObj = lookup_image_handle(ctx, handle);
   if (!imgHandleObj) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeImageHandleNonResidentARB(handle)");
      return;
   }

   if (!is_image_handle_resident(ctx, handle)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glMakeImageHandleNonResidentARB(not resident)");
      return;
   }

   make_image_handle_resident(ctx, imgHandleObj, GL_READ_ONLY, false);
}

GLboolean GLAPIENTRY
_mesa_IsTextureHandleResidentARB_no_error(GLuint64 handle)
{
   GET_CURRENT_CONTEXT(ctx);
   return is_texture_handle_resident(ctx, handle);
}

GLboolean GLAPIENTRY
_mesa_IsTextureHandleResidentARB(GLuint64 handle)
{
   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_bindless_texture(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glIsTextureHandleResidentARB(unsupported)");
      return GL_FALSE;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION will be generated by
    *  IsTextureHandleResidentARB and IsImageHandleResidentARB if <handle> is
    *  not a valid texture or image handle, respectively."
    */
   if (!lookup_texture_handle(ctx, handle)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glIsTextureHandleResidentARB(handle)");
      return GL_FALSE;
   }

   return is_texture_handle_resident(ctx, handle);
}

GLboolean GLAPIENTRY
_mesa_IsImageHandleResidentARB_no_error(GLuint64 handle)
{
   GET_CURRENT_CONTEXT(ctx);
   return is_image_handle_resident(ctx, handle);
}

GLboolean GLAPIENTRY
_mesa_IsImageHandleResidentARB(GLuint64 handle)
{
   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_has_ARB_bindless_texture(ctx) ||
       !_mesa_has_ARB_shader_image_load_store(ctx)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glIsImageHandleResidentARB(unsupported)");
      return GL_FALSE;
   }

   /* The ARB_bindless_texture spec says:
    *
    * "The error INVALID_OPERATION will be generated by
    *  IsTextureHandleResidentARB and IsImageHandleResidentARB if <handle> is
    *  not a valid texture or image handle, respectively."
    */
   if (!lookup_image_handle(ctx, handle)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glIsImageHandleResidentARB(handle)");
      return GL_FALSE;
   }

   return is_image_handle_resident(ctx, handle);
}